2 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include "prov/ciphercommon.h"
13 * The generic cipher functions for cipher modes cbc, ecb, ofb, cfb and ctr.
14 * Used if there is no special hardware implementations.
16 int cipher_hw_generic_cbc(PROV_CIPHER_CTX *dat, unsigned char *out,
17 const unsigned char *in, size_t len)
20 (*dat->stream.cbc) (in, out, len, dat->ks, dat->iv, dat->enc);
22 CRYPTO_cbc128_encrypt(in, out, len, dat->ks, dat->iv, dat->block);
24 CRYPTO_cbc128_decrypt(in, out, len, dat->ks, dat->iv, dat->block);
29 int cipher_hw_generic_ecb(PROV_CIPHER_CTX *dat, unsigned char *out,
30 const unsigned char *in, size_t len)
32 size_t i, bl = dat->blocksize;
37 for (i = 0, len -= bl; i <= len; i += bl)
38 (*dat->block) (in + i, out + i, dat->ks);
43 int cipher_hw_generic_ofb128(PROV_CIPHER_CTX *dat, unsigned char *out,
44 const unsigned char *in, size_t len)
48 CRYPTO_ofb128_encrypt(in, out, len, dat->ks, dat->iv, &num, dat->block);
54 int cipher_hw_generic_cfb128(PROV_CIPHER_CTX *dat, unsigned char *out,
55 const unsigned char *in, size_t len)
59 CRYPTO_cfb128_encrypt(in, out, len, dat->ks, dat->iv, &num, dat->enc,
66 int cipher_hw_generic_cfb8(PROV_CIPHER_CTX *dat, unsigned char *out,
67 const unsigned char *in, size_t len)
71 CRYPTO_cfb128_8_encrypt(in, out, len, dat->ks, dat->iv, &num, dat->enc,
78 int cipher_hw_generic_cfb1(PROV_CIPHER_CTX *dat, unsigned char *out,
79 const unsigned char *in, size_t len)
83 if ((dat->flags & EVP_CIPH_FLAG_LENGTH_BITS) != 0) {
84 CRYPTO_cfb128_1_encrypt(in, out, len, dat->ks, dat->iv, &num,
85 dat->enc, dat->block);
90 while (len >= MAXBITCHUNK) {
91 CRYPTO_cfb128_1_encrypt(in, out, MAXBITCHUNK * 8, dat->ks,
92 dat->iv, &num, dat->enc, dat->block);
98 CRYPTO_cfb128_1_encrypt(in, out, len * 8, dat->ks, dat->iv, &num,
99 dat->enc, dat->block);
106 int cipher_hw_generic_ctr(PROV_CIPHER_CTX *dat, unsigned char *out,
107 const unsigned char *in, size_t len)
109 unsigned int num = dat->num;
112 CRYPTO_ctr128_encrypt_ctr32(in, out, len, dat->ks, dat->iv, dat->buf,
113 &num, dat->stream.ctr);
115 CRYPTO_ctr128_encrypt(in, out, len, dat->ks, dat->iv, dat->buf,
123 * The chunked cipher functions for cipher modes cbc, ecb, ofb, cfb and ctr.
124 * Used if there is no special hardware implementations.
127 int cipher_hw_chunked_cbc(PROV_CIPHER_CTX *ctx, unsigned char *out,
128 const unsigned char *in, size_t inl)
130 while (inl >= MAXCHUNK) {
131 cipher_hw_generic_cbc(ctx, out, in, MAXCHUNK);
137 cipher_hw_generic_cbc(ctx, out, in, inl);
141 int cipher_hw_chunked_cfb8(PROV_CIPHER_CTX *ctx, unsigned char *out,
142 const unsigned char *in, size_t inl)
144 size_t chunk = MAXCHUNK;
148 while (inl > 0 && inl >= chunk) {
149 cipher_hw_generic_cfb8(ctx, out, in, inl);
159 int cipher_hw_chunked_cfb128(PROV_CIPHER_CTX *ctx, unsigned char *out,
160 const unsigned char *in, size_t inl)
162 size_t chunk = MAXCHUNK;
166 while (inl > 0 && inl >= chunk) {
167 cipher_hw_generic_cfb128(ctx, out, in, inl);
177 int cipher_hw_chunked_ofb128(PROV_CIPHER_CTX *ctx, unsigned char *out,
178 const unsigned char *in, size_t inl)
180 while (inl >= MAXCHUNK) {
181 cipher_hw_generic_ofb128(ctx, out, in, MAXCHUNK);
187 cipher_hw_generic_ofb128(ctx, out, in, inl);