| 1 |
// Copyright (c) 2017, Google Inc. |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
#include <openssl/cipher.h> |
| 5 |
|
| 6 |
#include <string.h> |
| 7 |
|
| 8 |
#include <openssl/aes.h> |
| 9 |
#include <openssl/obj.h> |
| 10 |
|
| 11 |
#include "../../internal.h" |
| 12 |
#include "../../fipsmodule/cipher/internal.h" |
| 13 |
|
| 14 |
// MAXBITCHUNK is used in |aes_cfb1_cipher| to avoid overflow because |
| 15 |
// |AES_cfb1_encrypt| operates data on bit level. |
| 16 |
#define MAXBITCHUNK ((size_t)1<<(sizeof(size_t)*8-4)) |
| 17 |
|
| 18 |
typedef struct { |
| 19 |
AES_KEY ks; |
| 20 |
} EVP_CFB_CTX; |
| 21 |
|
| 22 |
static int aes_cfb_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key, |
| 23 |
const uint8_t *iv, int enc) { |
| 24 |
if (key) { |
| 25 |
EVP_CFB_CTX *cfb_ctx = (EVP_CFB_CTX *)ctx->cipher_data; |
| 26 |
AES_set_encrypt_key(key, ctx->key_len * 8, &cfb_ctx->ks); |
| 27 |
} |
| 28 |
|
| 29 |
return 1; |
| 30 |
} |
| 31 |
|
| 32 |
static int aes_cfb1_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, |
| 33 |
const uint8_t *in, size_t len) { |
| 34 |
if (!out || !in) { |
| 35 |
return 0; |
| 36 |
} |
| 37 |
|
| 38 |
EVP_CFB_CTX *cfb_ctx = (EVP_CFB_CTX *)ctx->cipher_data; |
| 39 |
if (ctx->flags & EVP_CIPH_FLAG_LENGTH_BITS) { |
| 40 |
int num = ctx->num; |
| 41 |
AES_cfb1_encrypt(in, out, len, &cfb_ctx->ks, ctx->iv, &num, |
| 42 |
ctx->encrypt ? AES_ENCRYPT : AES_DECRYPT); |
| 43 |
ctx->num = num; |
| 44 |
return 1; |
| 45 |
} |
| 46 |
|
| 47 |
while (len >= MAXBITCHUNK) { |
| 48 |
int num = ctx->num; |
| 49 |
AES_cfb1_encrypt(in, out, MAXBITCHUNK * 8, &cfb_ctx->ks, ctx->iv, &num, |
| 50 |
ctx->encrypt ? AES_ENCRYPT : AES_DECRYPT); |
| 51 |
ctx->num = num; |
| 52 |
len -= MAXBITCHUNK; |
| 53 |
out += MAXBITCHUNK; |
| 54 |
in += MAXBITCHUNK; |
| 55 |
} |
| 56 |
if (len) { |
| 57 |
int num = ctx->num; |
| 58 |
AES_cfb1_encrypt(in, out, len * 8, &cfb_ctx->ks, ctx->iv, &num, |
| 59 |
ctx->encrypt ? AES_ENCRYPT : AES_DECRYPT); |
| 60 |
ctx->num = num; |
| 61 |
} |
| 62 |
|
| 63 |
return 1; |
| 64 |
} |
| 65 |
|
| 66 |
static int aes_cfb8_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, |
| 67 |
const uint8_t *in, size_t len) { |
| 68 |
if (!out || !in) { |
| 69 |
return 0; |
| 70 |
} |
| 71 |
|
| 72 |
EVP_CFB_CTX *cfb_ctx = (EVP_CFB_CTX *)ctx->cipher_data; |
| 73 |
int num = ctx->num; |
| 74 |
AES_cfb8_encrypt(in, out, len, &cfb_ctx->ks, ctx->iv, &num, |
| 75 |
ctx->encrypt ? AES_ENCRYPT : AES_DECRYPT); |
| 76 |
ctx->num = num; |
| 77 |
|
| 78 |
return 1; |
| 79 |
} |
| 80 |
|
| 81 |
static int aes_cfb128_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, |
| 82 |
const uint8_t *in, size_t len) { |
| 83 |
if (!out || !in) { |
| 84 |
return 0; |
| 85 |
} |
| 86 |
|
| 87 |
EVP_CFB_CTX *cfb_ctx = (EVP_CFB_CTX *)ctx->cipher_data; |
| 88 |
int num = ctx->num; |
| 89 |
AES_cfb128_encrypt(in, out, len, &cfb_ctx->ks, ctx->iv, &num, |
| 90 |
ctx->encrypt ? AES_ENCRYPT : AES_DECRYPT); |
| 91 |
ctx->num = num; |
| 92 |
|
| 93 |
return 1; |
| 94 |
} |
| 95 |
|
| 96 |
static const EVP_CIPHER aes_128_cfb1 = { |
| 97 |
NID_aes_128_cfb1, 1 /* block_size */, 16 /* key_size */, |
| 98 |
16 /* iv_len */, sizeof(EVP_CFB_CTX), EVP_CIPH_CFB_MODE, |
| 99 |
aes_cfb_init_key, aes_cfb1_cipher, NULL /* cleanup */, |
| 100 |
NULL /* ctrl */, |
| 101 |
}; |
| 102 |
|
| 103 |
static const EVP_CIPHER aes_128_cfb8 = { |
| 104 |
NID_aes_128_cfb8, 1 /* block_size */, 16 /* key_size */, |
| 105 |
16 /* iv_len */, sizeof(EVP_CFB_CTX), EVP_CIPH_CFB_MODE, |
| 106 |
aes_cfb_init_key, aes_cfb8_cipher, NULL /* cleanup */, |
| 107 |
NULL /* ctrl */, |
| 108 |
}; |
| 109 |
|
| 110 |
static const EVP_CIPHER aes_128_cfb128 = { |
| 111 |
NID_aes_128_cfb128, 1 /* block_size */, 16 /* key_size */, |
| 112 |
16 /* iv_len */, sizeof(EVP_CFB_CTX), EVP_CIPH_CFB_MODE, |
| 113 |
aes_cfb_init_key, aes_cfb128_cipher, NULL /* cleanup */, |
| 114 |
NULL /* ctrl */, |
| 115 |
}; |
| 116 |
|
| 117 |
static const EVP_CIPHER aes_192_cfb1 = { |
| 118 |
NID_aes_192_cfb1, 1 /* block_size */, 24 /* key_size */, |
| 119 |
16 /* iv_len */, sizeof(EVP_CFB_CTX), EVP_CIPH_CFB_MODE, |
| 120 |
aes_cfb_init_key, aes_cfb1_cipher, NULL /* cleanup */, |
| 121 |
NULL /* ctrl */, |
| 122 |
}; |
| 123 |
|
| 124 |
static const EVP_CIPHER aes_192_cfb8 = { |
| 125 |
NID_aes_192_cfb8, 1 /* block_size */, 24 /* key_size */, |
| 126 |
16 /* iv_len */, sizeof(EVP_CFB_CTX), EVP_CIPH_CFB_MODE, |
| 127 |
aes_cfb_init_key, aes_cfb8_cipher, NULL /* cleanup */, |
| 128 |
NULL /* ctrl */, |
| 129 |
}; |
| 130 |
|
| 131 |
static const EVP_CIPHER aes_192_cfb128 = { |
| 132 |
NID_aes_192_cfb128, 1 /* block_size */, 24 /* key_size */, |
| 133 |
16 /* iv_len */, sizeof(EVP_CFB_CTX), EVP_CIPH_CFB_MODE, |
| 134 |
aes_cfb_init_key, aes_cfb128_cipher, NULL /* cleanup */, |
| 135 |
NULL /* ctrl */, |
| 136 |
}; |
| 137 |
|
| 138 |
static const EVP_CIPHER aes_256_cfb1 = { |
| 139 |
NID_aes_256_cfb1, 1 /* block_size */, 32 /* key_size */, |
| 140 |
16 /* iv_len */, sizeof(EVP_CFB_CTX), EVP_CIPH_CFB_MODE, |
| 141 |
aes_cfb_init_key, aes_cfb1_cipher, NULL /* cleanup */, |
| 142 |
NULL /* ctrl */, |
| 143 |
}; |
| 144 |
|
| 145 |
static const EVP_CIPHER aes_256_cfb8 = { |
| 146 |
NID_aes_256_cfb8, 1 /* block_size */, 32 /* key_size */, |
| 147 |
16 /* iv_len */, sizeof(EVP_CFB_CTX), EVP_CIPH_CFB_MODE, |
| 148 |
aes_cfb_init_key, aes_cfb8_cipher, NULL /* cleanup */, |
| 149 |
NULL /* ctrl */, |
| 150 |
}; |
| 151 |
|
| 152 |
static const EVP_CIPHER aes_256_cfb128 = { |
| 153 |
NID_aes_256_cfb128, 1 /* block_size */, 32 /* key_size */, |
| 154 |
16 /* iv_len */, sizeof(EVP_CFB_CTX), EVP_CIPH_CFB_MODE, |
| 155 |
aes_cfb_init_key, aes_cfb128_cipher, NULL /* cleanup */, |
| 156 |
NULL /* ctrl */, |
| 157 |
}; |
| 158 |
|
| 159 |
const EVP_CIPHER *EVP_aes_128_cfb1(void) { return &aes_128_cfb1; } |
| 160 |
const EVP_CIPHER *EVP_aes_128_cfb8(void) { return &aes_128_cfb8; } |
| 161 |
const EVP_CIPHER *EVP_aes_128_cfb128(void) { return &aes_128_cfb128; } |
| 162 |
const EVP_CIPHER *EVP_aes_128_cfb(void) { return &aes_128_cfb128; } |
| 163 |
|
| 164 |
const EVP_CIPHER *EVP_aes_192_cfb1(void) { return &aes_192_cfb1; } |
| 165 |
const EVP_CIPHER *EVP_aes_192_cfb8(void) { return &aes_192_cfb8; } |
| 166 |
const EVP_CIPHER *EVP_aes_192_cfb128(void) { return &aes_192_cfb128; } |
| 167 |
const EVP_CIPHER *EVP_aes_192_cfb(void) { return &aes_192_cfb128; } |
| 168 |
|
| 169 |
const EVP_CIPHER *EVP_aes_256_cfb1(void) { return &aes_256_cfb1; } |
| 170 |
const EVP_CIPHER *EVP_aes_256_cfb8(void) { return &aes_256_cfb8; } |
| 171 |
const EVP_CIPHER *EVP_aes_256_cfb128(void) { return &aes_256_cfb128; } |
| 172 |
const EVP_CIPHER *EVP_aes_256_cfb(void) { return &aes_256_cfb128; } |