diff options
author | jsing <> | 2025-05-19 04:32:52 +0000 |
---|---|---|
committer | jsing <> | 2025-05-19 04:32:52 +0000 |
commit | c2782d1f52bceb30584b71c11631e00eacebcc4e (patch) | |
tree | 933fe7cf5ca670d95a283dd28b6cd3dbbcfcf0c5 /src/lib | |
parent | 79868e04e6efa26035e7e479e6094da677341390 (diff) | |
download | openbsd-c2782d1f52bceb30584b71c11631e00eacebcc4e.tar.gz openbsd-c2782d1f52bceb30584b71c11631e00eacebcc4e.tar.bz2 openbsd-c2782d1f52bceb30584b71c11631e00eacebcc4e.zip |
Simplify EVP AES code for ECB.
AES_ecb_encrypt() does not really do ECB - provide an
aes_ecb_encrypt_internal that actually does multiple blocks and call this
from aes_ecb_cipher(). Provide ECB with its own key initialisation
function, which allows aes_init_key() to be simplified considerably.
The block function pointer is now unused, so mop this up.
ok joshua@ tb@
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/libcrypto/aes/aes.c | 14 | ||||
-rw-r--r-- | src/lib/libcrypto/evp/e_aes.c | 65 |
2 files changed, 46 insertions, 33 deletions
diff --git a/src/lib/libcrypto/aes/aes.c b/src/lib/libcrypto/aes/aes.c index 712168e9fa..1c1c61a7a9 100644 --- a/src/lib/libcrypto/aes/aes.c +++ b/src/lib/libcrypto/aes/aes.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: aes.c,v 1.6 2025/05/19 04:01:07 jsing Exp $ */ | 1 | /* $OpenBSD: aes.c,v 1.7 2025/05/19 04:32:51 jsing Exp $ */ |
2 | /* ==================================================================== | 2 | /* ==================================================================== |
3 | * Copyright (c) 2002-2006 The OpenSSL Project. All rights reserved. | 3 | * Copyright (c) 2002-2006 The OpenSSL Project. All rights reserved. |
4 | * | 4 | * |
@@ -191,6 +191,18 @@ AES_ecb_encrypt(const unsigned char *in, unsigned char *out, | |||
191 | LCRYPTO_ALIAS(AES_ecb_encrypt); | 191 | LCRYPTO_ALIAS(AES_ecb_encrypt); |
192 | 192 | ||
193 | void | 193 | void |
194 | aes_ecb_encrypt_internal(const unsigned char *in, unsigned char *out, | ||
195 | size_t len, const AES_KEY *key, int encrypt) | ||
196 | { | ||
197 | while (len >= AES_BLOCK_SIZE) { | ||
198 | AES_ecb_encrypt(in, out, key, encrypt); | ||
199 | in += AES_BLOCK_SIZE; | ||
200 | out += AES_BLOCK_SIZE; | ||
201 | len -= AES_BLOCK_SIZE; | ||
202 | } | ||
203 | } | ||
204 | |||
205 | void | ||
194 | AES_ofb128_encrypt(const unsigned char *in, unsigned char *out, size_t length, | 206 | AES_ofb128_encrypt(const unsigned char *in, unsigned char *out, size_t length, |
195 | const AES_KEY *key, unsigned char *ivec, int *num) | 207 | const AES_KEY *key, unsigned char *ivec, int *num) |
196 | { | 208 | { |
diff --git a/src/lib/libcrypto/evp/e_aes.c b/src/lib/libcrypto/evp/e_aes.c index d0bcb2b3dd..bfdfed8172 100644 --- a/src/lib/libcrypto/evp/e_aes.c +++ b/src/lib/libcrypto/evp/e_aes.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: e_aes.c,v 1.67 2025/05/19 03:55:09 jsing Exp $ */ | 1 | /* $OpenBSD: e_aes.c,v 1.68 2025/05/19 04:32:52 jsing Exp $ */ |
2 | /* ==================================================================== | 2 | /* ==================================================================== |
3 | * Copyright (c) 2001-2011 The OpenSSL Project. All rights reserved. | 3 | * Copyright (c) 2001-2011 The OpenSSL Project. All rights reserved. |
4 | * | 4 | * |
@@ -67,7 +67,6 @@ | |||
67 | 67 | ||
68 | typedef struct { | 68 | typedef struct { |
69 | AES_KEY ks; | 69 | AES_KEY ks; |
70 | block128_f block; | ||
71 | } EVP_AES_KEY; | 70 | } EVP_AES_KEY; |
72 | 71 | ||
73 | typedef struct { | 72 | typedef struct { |
@@ -104,6 +103,9 @@ typedef struct { | |||
104 | 103 | ||
105 | #define MAXBITCHUNK ((size_t)1<<(sizeof(size_t)*8-4)) | 104 | #define MAXBITCHUNK ((size_t)1<<(sizeof(size_t)*8-4)) |
106 | 105 | ||
106 | void aes_ecb_encrypt_internal(const unsigned char *in, unsigned char *out, | ||
107 | size_t len, const AES_KEY *key, int encrypt); | ||
108 | |||
107 | #ifdef AES_XTS_ASM | 109 | #ifdef AES_XTS_ASM |
108 | void AES_xts_encrypt(const char *inp, char *out, size_t len, | 110 | void AES_xts_encrypt(const char *inp, char *out, size_t len, |
109 | const AES_KEY *key1, const AES_KEY *key2, const unsigned char iv[16]); | 111 | const AES_KEY *key1, const AES_KEY *key2, const unsigned char iv[16]); |
@@ -164,7 +166,6 @@ aesni_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | |||
164 | const unsigned char *iv, int enc) | 166 | const unsigned char *iv, int enc) |
165 | { | 167 | { |
166 | int ret, mode; | 168 | int ret, mode; |
167 | EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; | ||
168 | 169 | ||
169 | mode = ctx->cipher->flags & EVP_CIPH_MODE; | 170 | mode = ctx->cipher->flags & EVP_CIPH_MODE; |
170 | 171 | ||
@@ -172,11 +173,9 @@ aesni_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | |||
172 | !enc) { | 173 | !enc) { |
173 | ret = aesni_set_decrypt_key(key, ctx->key_len * 8, | 174 | ret = aesni_set_decrypt_key(key, ctx->key_len * 8, |
174 | ctx->cipher_data); | 175 | ctx->cipher_data); |
175 | dat->block = (block128_f)aesni_decrypt; | ||
176 | } else { | 176 | } else { |
177 | ret = aesni_set_encrypt_key(key, ctx->key_len * 8, | 177 | ret = aesni_set_encrypt_key(key, ctx->key_len * 8, |
178 | ctx->cipher_data); | 178 | ctx->cipher_data); |
179 | dat->block = (block128_f)aesni_encrypt; | ||
180 | } | 179 | } |
181 | 180 | ||
182 | if (ret < 0) { | 181 | if (ret < 0) { |
@@ -267,9 +266,7 @@ static int | |||
267 | aesni_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | 266 | aesni_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
268 | const unsigned char *in, size_t len) | 267 | const unsigned char *in, size_t len) |
269 | { | 268 | { |
270 | size_t bl = ctx->cipher->block_size; | 269 | if (len < ctx->cipher->block_size) |
271 | |||
272 | if (len < bl) | ||
273 | return 1; | 270 | return 1; |
274 | 271 | ||
275 | aesni_ecb_encrypt(in, out, len, ctx->cipher_data, ctx->encrypt); | 272 | aesni_ecb_encrypt(in, out, len, ctx->cipher_data, ctx->encrypt); |
@@ -390,20 +387,9 @@ static int | |||
390 | aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | 387 | aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
391 | const unsigned char *iv, int enc) | 388 | const unsigned char *iv, int enc) |
392 | { | 389 | { |
393 | int ret, mode; | 390 | EVP_AES_KEY *eak = ctx->cipher_data; |
394 | EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; | ||
395 | |||
396 | mode = ctx->cipher->flags & EVP_CIPH_MODE; | ||
397 | |||
398 | if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE) && !enc) { | ||
399 | ret = AES_set_decrypt_key(key, ctx->key_len * 8, &dat->ks); | ||
400 | dat->block = (block128_f)AES_decrypt; | ||
401 | } else { | ||
402 | ret = AES_set_encrypt_key(key, ctx->key_len * 8, &dat->ks); | ||
403 | dat->block = (block128_f)AES_encrypt; | ||
404 | } | ||
405 | 391 | ||
406 | if (ret < 0) { | 392 | if (AES_set_encrypt_key(key, ctx->key_len * 8, &eak->ks) < 0) { |
407 | EVPerror(EVP_R_AES_KEY_SETUP_FAILED); | 393 | EVPerror(EVP_R_AES_KEY_SETUP_FAILED); |
408 | return 0; | 394 | return 0; |
409 | } | 395 | } |
@@ -444,18 +430,33 @@ aes_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | |||
444 | } | 430 | } |
445 | 431 | ||
446 | static int | 432 | static int |
433 | aes_ecb_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | ||
434 | const unsigned char *iv, int encrypt) | ||
435 | { | ||
436 | EVP_AES_KEY *eak = ctx->cipher_data; | ||
437 | |||
438 | if (encrypt) { | ||
439 | if (AES_set_encrypt_key(key, ctx->key_len * 8, &eak->ks) < 0) { | ||
440 | EVPerror(EVP_R_AES_KEY_SETUP_FAILED); | ||
441 | return 0; | ||
442 | } | ||
443 | } else { | ||
444 | if (AES_set_decrypt_key(key, ctx->key_len * 8, &eak->ks) < 0) { | ||
445 | EVPerror(EVP_R_AES_KEY_SETUP_FAILED); | ||
446 | return 0; | ||
447 | } | ||
448 | } | ||
449 | |||
450 | return 1; | ||
451 | } | ||
452 | |||
453 | static int | ||
447 | aes_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | 454 | aes_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
448 | const unsigned char *in, size_t len) | 455 | const unsigned char *in, size_t len) |
449 | { | 456 | { |
450 | size_t bl = ctx->cipher->block_size; | 457 | EVP_AES_KEY *eak = ctx->cipher_data; |
451 | size_t i; | ||
452 | EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; | ||
453 | |||
454 | if (len < bl) | ||
455 | return 1; | ||
456 | 458 | ||
457 | for (i = 0, len -= bl; i <= len; i += bl) | 459 | aes_ecb_encrypt_internal(in, out, len, &eak->ks, ctx->encrypt); |
458 | (*dat->block)(in + i, out + i, &dat->ks); | ||
459 | 460 | ||
460 | return 1; | 461 | return 1; |
461 | } | 462 | } |
@@ -590,7 +591,7 @@ static const EVP_CIPHER aes_128_ecb = { | |||
590 | .key_len = 16, | 591 | .key_len = 16, |
591 | .iv_len = 0, | 592 | .iv_len = 0, |
592 | .flags = EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_ECB_MODE, | 593 | .flags = EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_ECB_MODE, |
593 | .init = aes_init_key, | 594 | .init = aes_ecb_init_key, |
594 | .do_cipher = aes_ecb_cipher, | 595 | .do_cipher = aes_ecb_cipher, |
595 | .ctx_size = sizeof(EVP_AES_KEY), | 596 | .ctx_size = sizeof(EVP_AES_KEY), |
596 | }; | 597 | }; |
@@ -836,7 +837,7 @@ static const EVP_CIPHER aes_192_ecb = { | |||
836 | .key_len = 24, | 837 | .key_len = 24, |
837 | .iv_len = 0, | 838 | .iv_len = 0, |
838 | .flags = EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_ECB_MODE, | 839 | .flags = EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_ECB_MODE, |
839 | .init = aes_init_key, | 840 | .init = aes_ecb_init_key, |
840 | .do_cipher = aes_ecb_cipher, | 841 | .do_cipher = aes_ecb_cipher, |
841 | .ctx_size = sizeof(EVP_AES_KEY), | 842 | .ctx_size = sizeof(EVP_AES_KEY), |
842 | }; | 843 | }; |
@@ -1082,7 +1083,7 @@ static const EVP_CIPHER aes_256_ecb = { | |||
1082 | .key_len = 32, | 1083 | .key_len = 32, |
1083 | .iv_len = 0, | 1084 | .iv_len = 0, |
1084 | .flags = EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_ECB_MODE, | 1085 | .flags = EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_ECB_MODE, |
1085 | .init = aes_init_key, | 1086 | .init = aes_ecb_init_key, |
1086 | .do_cipher = aes_ecb_cipher, | 1087 | .do_cipher = aes_ecb_cipher, |
1087 | .ctx_size = sizeof(EVP_AES_KEY), | 1088 | .ctx_size = sizeof(EVP_AES_KEY), |
1088 | }; | 1089 | }; |