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 if (dat->stream.ecb) {
38 (*dat->stream.ecb) (in, out, len, dat->ks, dat->enc);
41 for (i = 0, len -= bl; i <= len; i += bl)
42 (*dat->block) (in + i, out + i, dat->ks);
48 int cipher_hw_generic_ofb128(PROV_CIPHER_CTX *dat, unsigned char *out,
49 const unsigned char *in, size_t len)
53 CRYPTO_ofb128_encrypt(in, out, len, dat->ks, dat->iv, &num, dat->block);
59 int cipher_hw_generic_cfb128(PROV_CIPHER_CTX *dat, unsigned char *out,
60 const unsigned char *in, size_t len)
64 CRYPTO_cfb128_encrypt(in, out, len, dat->ks, dat->iv, &num, dat->enc,
71 int cipher_hw_generic_cfb8(PROV_CIPHER_CTX *dat, unsigned char *out,
72 const unsigned char *in, size_t len)
76 CRYPTO_cfb128_8_encrypt(in, out, len, dat->ks, dat->iv, &num, dat->enc,
83 int cipher_hw_generic_cfb1(PROV_CIPHER_CTX *dat, unsigned char *out,
84 const unsigned char *in, size_t len)
88 if ((dat->flags & EVP_CIPH_FLAG_LENGTH_BITS) != 0) {
89 CRYPTO_cfb128_1_encrypt(in, out, len, dat->ks, dat->iv, &num,
90 dat->enc, dat->block);
95 while (len >= MAXBITCHUNK) {
96 CRYPTO_cfb128_1_encrypt(in, out, MAXBITCHUNK * 8, dat->ks,
97 dat->iv, &num, dat->enc, dat->block);
103 CRYPTO_cfb128_1_encrypt(in, out, len * 8, dat->ks, dat->iv, &num,
104 dat->enc, dat->block);
111 int cipher_hw_generic_ctr(PROV_CIPHER_CTX *dat, unsigned char *out,
112 const unsigned char *in, size_t len)
114 unsigned int num = dat->num;
117 CRYPTO_ctr128_encrypt_ctr32(in, out, len, dat->ks, dat->iv, dat->buf,
118 &num, dat->stream.ctr);
120 CRYPTO_ctr128_encrypt(in, out, len, dat->ks, dat->iv, dat->buf,
128 * The chunked cipher functions for cipher modes cbc, ecb, ofb, cfb and ctr.
129 * Used if there is no special hardware implementations.
132 int cipher_hw_chunked_cbc(PROV_CIPHER_CTX *ctx, unsigned char *out,
133 const unsigned char *in, size_t inl)
135 while (inl >= MAXCHUNK) {
136 cipher_hw_generic_cbc(ctx, out, in, MAXCHUNK);
142 cipher_hw_generic_cbc(ctx, out, in, inl);
146 int cipher_hw_chunked_cfb8(PROV_CIPHER_CTX *ctx, unsigned char *out,
147 const unsigned char *in, size_t inl)
149 size_t chunk = MAXCHUNK;
153 while (inl > 0 && inl >= chunk) {
154 cipher_hw_generic_cfb8(ctx, out, in, inl);
164 int cipher_hw_chunked_cfb128(PROV_CIPHER_CTX *ctx, unsigned char *out,
165 const unsigned char *in, size_t inl)
167 size_t chunk = MAXCHUNK;
171 while (inl > 0 && inl >= chunk) {
172 cipher_hw_generic_cfb128(ctx, out, in, inl);
182 int cipher_hw_chunked_ofb128(PROV_CIPHER_CTX *ctx, unsigned char *out,
183 const unsigned char *in, size_t inl)
185 while (inl >= MAXCHUNK) {
186 cipher_hw_generic_ofb128(ctx, out, in, MAXCHUNK);
192 cipher_hw_generic_ofb128(ctx, out, in, inl);