diff options
| author | jsing <> | 2025-07-13 06:01:33 +0000 |
|---|---|---|
| committer | jsing <> | 2025-07-13 06:01:33 +0000 |
| commit | f0234f5a33ecf3b2784f3e73bdf1e937abe56599 (patch) | |
| tree | a43688f8969e5bd862faf101152f51b1560e7731 /src/lib/libcrypto/aes | |
| parent | 417b1213b262bbe6d34c708537dff4b062920bfa (diff) | |
| download | openbsd-f0234f5a33ecf3b2784f3e73bdf1e937abe56599.tar.gz openbsd-f0234f5a33ecf3b2784f3e73bdf1e937abe56599.tar.bz2 openbsd-f0234f5a33ecf3b2784f3e73bdf1e937abe56599.zip | |
Simplify AES-XTS implementation and remove AES-NI specific code from EVP.
Provide aes_xts_encrypt_internal() and call that from aes_xts_cipher().
Have amd64 and i386 provide their own versions that dispatch to
aesni_xts_encrypt()/aesni_xts_decrypt() as appropriate. The
AESNI_CAPABLE code and methods can then be removed.
ok tb@
Diffstat (limited to 'src/lib/libcrypto/aes')
| -rw-r--r-- | src/lib/libcrypto/aes/aes.c | 32 | ||||
| -rw-r--r-- | src/lib/libcrypto/aes/aes_amd64.c | 31 | ||||
| -rw-r--r-- | src/lib/libcrypto/aes/aes_i386.c | 31 | ||||
| -rw-r--r-- | src/lib/libcrypto/aes/aes_local.h | 6 |
4 files changed, 96 insertions, 4 deletions
diff --git a/src/lib/libcrypto/aes/aes.c b/src/lib/libcrypto/aes/aes.c index e9dbe975e3..45b7a3b109 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.10 2025/06/27 17:10:45 jsing Exp $ */ | 1 | /* $OpenBSD: aes.c,v 1.11 2025/07/13 06:01:33 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 | * |
| @@ -57,6 +57,7 @@ | |||
| 57 | 57 | ||
| 58 | #include "crypto_arch.h" | 58 | #include "crypto_arch.h" |
| 59 | #include "crypto_internal.h" | 59 | #include "crypto_internal.h" |
| 60 | #include "modes_local.h" | ||
| 60 | 61 | ||
| 61 | static const unsigned char aes_wrap_default_iv[] = { | 62 | static const unsigned char aes_wrap_default_iv[] = { |
| 62 | 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, | 63 | 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, |
| @@ -322,6 +323,35 @@ AES_ofb128_encrypt(const unsigned char *in, unsigned char *out, size_t length, | |||
| 322 | } | 323 | } |
| 323 | LCRYPTO_ALIAS(AES_ofb128_encrypt); | 324 | LCRYPTO_ALIAS(AES_ofb128_encrypt); |
| 324 | 325 | ||
| 326 | void | ||
| 327 | aes_xts_encrypt_generic(const unsigned char *in, unsigned char *out, size_t len, | ||
| 328 | const AES_KEY *key1, const AES_KEY *key2, const unsigned char iv[16], | ||
| 329 | int encrypt) | ||
| 330 | { | ||
| 331 | XTS128_CONTEXT xctx; | ||
| 332 | |||
| 333 | if (encrypt) | ||
| 334 | xctx.block1 = aes_encrypt_block128; | ||
| 335 | else | ||
| 336 | xctx.block1 = aes_decrypt_block128; | ||
| 337 | |||
| 338 | xctx.block2 = aes_encrypt_block128; | ||
| 339 | xctx.key1 = key1; | ||
| 340 | xctx.key2 = key2; | ||
| 341 | |||
| 342 | CRYPTO_xts128_encrypt(&xctx, iv, in, out, len, encrypt); | ||
| 343 | } | ||
| 344 | |||
| 345 | #ifndef HAVE_AES_XTS_ENCRYPT_INTERNAL | ||
| 346 | void | ||
| 347 | aes_xts_encrypt_internal(const unsigned char *in, unsigned char *out, size_t len, | ||
| 348 | const AES_KEY *key1, const AES_KEY *key2, const unsigned char iv[16], | ||
| 349 | int encrypt) | ||
| 350 | { | ||
| 351 | aes_xts_encrypt_generic(in, out, len, key1, key2, iv, encrypt); | ||
| 352 | } | ||
| 353 | #endif | ||
| 354 | |||
| 325 | int | 355 | int |
| 326 | AES_wrap_key(AES_KEY *key, const unsigned char *iv, unsigned char *out, | 356 | AES_wrap_key(AES_KEY *key, const unsigned char *iv, unsigned char *out, |
| 327 | const unsigned char *in, unsigned int inlen) | 357 | const unsigned char *in, unsigned int inlen) |
diff --git a/src/lib/libcrypto/aes/aes_amd64.c b/src/lib/libcrypto/aes/aes_amd64.c index 456409d186..5a40274675 100644 --- a/src/lib/libcrypto/aes/aes_amd64.c +++ b/src/lib/libcrypto/aes/aes_amd64.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: aes_amd64.c,v 1.2 2025/06/27 17:10:45 jsing Exp $ */ | 1 | /* $OpenBSD: aes_amd64.c,v 1.3 2025/07/13 06:01:33 jsing Exp $ */ |
| 2 | /* | 2 | /* |
| 3 | * Copyright (c) 2025 Joel Sing <jsing@openbsd.org> | 3 | * Copyright (c) 2025 Joel Sing <jsing@openbsd.org> |
| 4 | * | 4 | * |
| @@ -18,6 +18,7 @@ | |||
| 18 | #include <openssl/aes.h> | 18 | #include <openssl/aes.h> |
| 19 | 19 | ||
| 20 | #include "crypto_arch.h" | 20 | #include "crypto_arch.h" |
| 21 | #include "modes_local.h" | ||
| 21 | 22 | ||
| 22 | int aes_set_encrypt_key_generic(const unsigned char *userKey, const int bits, | 23 | int aes_set_encrypt_key_generic(const unsigned char *userKey, const int bits, |
| 23 | AES_KEY *key); | 24 | AES_KEY *key); |
| @@ -35,6 +36,10 @@ void aes_cbc_encrypt_generic(const unsigned char *in, unsigned char *out, | |||
| 35 | void aes_ctr32_encrypt_generic(const unsigned char *in, unsigned char *out, | 36 | void aes_ctr32_encrypt_generic(const unsigned char *in, unsigned char *out, |
| 36 | size_t blocks, const AES_KEY *key, const unsigned char ivec[AES_BLOCK_SIZE]); | 37 | size_t blocks, const AES_KEY *key, const unsigned char ivec[AES_BLOCK_SIZE]); |
| 37 | 38 | ||
| 39 | void aes_xts_encrypt_generic(const unsigned char *in, unsigned char *out, | ||
| 40 | size_t len, const AES_KEY *key1, const AES_KEY *key2, | ||
| 41 | const unsigned char iv[16], int encrypt); | ||
| 42 | |||
| 38 | int aesni_set_encrypt_key(const unsigned char *userKey, int bits, | 43 | int aesni_set_encrypt_key(const unsigned char *userKey, int bits, |
| 39 | AES_KEY *key); | 44 | AES_KEY *key); |
| 40 | int aesni_set_decrypt_key(const unsigned char *userKey, int bits, | 45 | int aesni_set_decrypt_key(const unsigned char *userKey, int bits, |
| @@ -51,6 +56,14 @@ void aesni_cbc_encrypt(const unsigned char *in, unsigned char *out, | |||
| 51 | void aesni_ctr32_encrypt_blocks(const unsigned char *in, unsigned char *out, | 56 | void aesni_ctr32_encrypt_blocks(const unsigned char *in, unsigned char *out, |
| 52 | size_t blocks, const void *key, const unsigned char *ivec); | 57 | size_t blocks, const void *key, const unsigned char *ivec); |
| 53 | 58 | ||
| 59 | void aesni_xts_encrypt(const unsigned char *in, unsigned char *out, | ||
| 60 | size_t length, const AES_KEY *key1, const AES_KEY *key2, | ||
| 61 | const unsigned char iv[16]); | ||
| 62 | |||
| 63 | void aesni_xts_decrypt(const unsigned char *in, unsigned char *out, | ||
| 64 | size_t length, const AES_KEY *key1, const AES_KEY *key2, | ||
| 65 | const unsigned char iv[16]); | ||
| 66 | |||
| 54 | int | 67 | int |
| 55 | aes_set_encrypt_key_internal(const unsigned char *userKey, const int bits, | 68 | aes_set_encrypt_key_internal(const unsigned char *userKey, const int bits, |
| 56 | AES_KEY *key) | 69 | AES_KEY *key) |
| @@ -118,3 +131,19 @@ aes_ctr32_encrypt_internal(const unsigned char *in, unsigned char *out, | |||
| 118 | 131 | ||
| 119 | aes_ctr32_encrypt_generic(in, out, blocks, key, ivec); | 132 | aes_ctr32_encrypt_generic(in, out, blocks, key, ivec); |
| 120 | } | 133 | } |
| 134 | |||
| 135 | void | ||
| 136 | aes_xts_encrypt_internal(const unsigned char *in, unsigned char *out, | ||
| 137 | size_t len, const AES_KEY *key1, const AES_KEY *key2, | ||
| 138 | const unsigned char iv[16], int encrypt) | ||
| 139 | { | ||
| 140 | if ((crypto_cpu_caps_amd64 & CRYPTO_CPU_CAPS_AMD64_AES) != 0) { | ||
| 141 | if (encrypt) | ||
| 142 | aesni_xts_encrypt(in, out, len, key1, key2, iv); | ||
| 143 | else | ||
| 144 | aesni_xts_decrypt(in, out, len, key1, key2, iv); | ||
| 145 | return; | ||
| 146 | } | ||
| 147 | |||
| 148 | aes_xts_encrypt_generic(in, out, len, key1, key2, iv, encrypt); | ||
| 149 | } | ||
diff --git a/src/lib/libcrypto/aes/aes_i386.c b/src/lib/libcrypto/aes/aes_i386.c index 2da02a8d35..73b75d28f5 100644 --- a/src/lib/libcrypto/aes/aes_i386.c +++ b/src/lib/libcrypto/aes/aes_i386.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: aes_i386.c,v 1.2 2025/06/27 17:10:45 jsing Exp $ */ | 1 | /* $OpenBSD: aes_i386.c,v 1.3 2025/07/13 06:01:33 jsing Exp $ */ |
| 2 | /* | 2 | /* |
| 3 | * Copyright (c) 2025 Joel Sing <jsing@openbsd.org> | 3 | * Copyright (c) 2025 Joel Sing <jsing@openbsd.org> |
| 4 | * | 4 | * |
| @@ -18,6 +18,7 @@ | |||
| 18 | #include <openssl/aes.h> | 18 | #include <openssl/aes.h> |
| 19 | 19 | ||
| 20 | #include "crypto_arch.h" | 20 | #include "crypto_arch.h" |
| 21 | #include "modes_local.h" | ||
| 21 | 22 | ||
| 22 | int aes_set_encrypt_key_generic(const unsigned char *userKey, const int bits, | 23 | int aes_set_encrypt_key_generic(const unsigned char *userKey, const int bits, |
| 23 | AES_KEY *key); | 24 | AES_KEY *key); |
| @@ -35,6 +36,10 @@ void aes_cbc_encrypt_generic(const unsigned char *in, unsigned char *out, | |||
| 35 | void aes_ctr32_encrypt_generic(const unsigned char *in, unsigned char *out, | 36 | void aes_ctr32_encrypt_generic(const unsigned char *in, unsigned char *out, |
| 36 | size_t blocks, const AES_KEY *key, const unsigned char ivec[AES_BLOCK_SIZE]); | 37 | size_t blocks, const AES_KEY *key, const unsigned char ivec[AES_BLOCK_SIZE]); |
| 37 | 38 | ||
| 39 | void aes_xts_encrypt_generic(const unsigned char *in, unsigned char *out, | ||
| 40 | size_t len, const AES_KEY *key1, const AES_KEY *key2, | ||
| 41 | const unsigned char iv[16], int encrypt); | ||
| 42 | |||
| 38 | int aesni_set_encrypt_key(const unsigned char *userKey, int bits, | 43 | int aesni_set_encrypt_key(const unsigned char *userKey, int bits, |
| 39 | AES_KEY *key); | 44 | AES_KEY *key); |
| 40 | int aesni_set_decrypt_key(const unsigned char *userKey, int bits, | 45 | int aesni_set_decrypt_key(const unsigned char *userKey, int bits, |
| @@ -51,6 +56,14 @@ void aesni_cbc_encrypt(const unsigned char *in, unsigned char *out, | |||
| 51 | void aesni_ctr32_encrypt_blocks(const unsigned char *in, unsigned char *out, | 56 | void aesni_ctr32_encrypt_blocks(const unsigned char *in, unsigned char *out, |
| 52 | size_t blocks, const void *key, const unsigned char *ivec); | 57 | size_t blocks, const void *key, const unsigned char *ivec); |
| 53 | 58 | ||
| 59 | void aesni_xts_encrypt(const unsigned char *in, unsigned char *out, | ||
| 60 | size_t length, const AES_KEY *key1, const AES_KEY *key2, | ||
| 61 | const unsigned char iv[16]); | ||
| 62 | |||
| 63 | void aesni_xts_decrypt(const unsigned char *in, unsigned char *out, | ||
| 64 | size_t length, const AES_KEY *key1, const AES_KEY *key2, | ||
| 65 | const unsigned char iv[16]); | ||
| 66 | |||
| 54 | int | 67 | int |
| 55 | aes_set_encrypt_key_internal(const unsigned char *userKey, const int bits, | 68 | aes_set_encrypt_key_internal(const unsigned char *userKey, const int bits, |
| 56 | AES_KEY *key) | 69 | AES_KEY *key) |
| @@ -118,3 +131,19 @@ aes_ctr32_encrypt_internal(const unsigned char *in, unsigned char *out, | |||
| 118 | 131 | ||
| 119 | aes_ctr32_encrypt_generic(in, out, blocks, key, ivec); | 132 | aes_ctr32_encrypt_generic(in, out, blocks, key, ivec); |
| 120 | } | 133 | } |
| 134 | |||
| 135 | void | ||
| 136 | aes_xts_encrypt_internal(const unsigned char *in, unsigned char *out, | ||
| 137 | size_t len, const AES_KEY *key1, const AES_KEY *key2, | ||
| 138 | const unsigned char iv[16], int encrypt) | ||
| 139 | { | ||
| 140 | if ((crypto_cpu_caps_i386 & CRYPTO_CPU_CAPS_I386_AES) != 0) { | ||
| 141 | if (encrypt) | ||
| 142 | aesni_xts_encrypt(in, out, len, key1, key2, iv); | ||
| 143 | else | ||
| 144 | aesni_xts_decrypt(in, out, len, key1, key2, iv); | ||
| 145 | return; | ||
| 146 | } | ||
| 147 | |||
| 148 | aes_xts_encrypt_generic(in, out, len, key1, key2, iv, encrypt); | ||
| 149 | } | ||
diff --git a/src/lib/libcrypto/aes/aes_local.h b/src/lib/libcrypto/aes/aes_local.h index 5052cf9e70..f68d4624e7 100644 --- a/src/lib/libcrypto/aes/aes_local.h +++ b/src/lib/libcrypto/aes/aes_local.h | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: aes_local.h,v 1.8 2025/07/06 15:37:33 jsing Exp $ */ | 1 | /* $OpenBSD: aes_local.h,v 1.9 2025/07/13 06:01:33 jsing Exp $ */ |
| 2 | /* ==================================================================== | 2 | /* ==================================================================== |
| 3 | * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved. | 3 | * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved. |
| 4 | * | 4 | * |
| @@ -69,6 +69,10 @@ void aes_ctr32_encrypt_ctr128f(const unsigned char *in, unsigned char *out, | |||
| 69 | void aes_ecb_encrypt_internal(const unsigned char *in, unsigned char *out, | 69 | void aes_ecb_encrypt_internal(const unsigned char *in, unsigned char *out, |
| 70 | size_t len, const AES_KEY *key, int encrypt); | 70 | size_t len, const AES_KEY *key, int encrypt); |
| 71 | 71 | ||
| 72 | void aes_xts_encrypt_internal(const char unsigned *in, char unsigned *out, | ||
| 73 | size_t len, const AES_KEY *key1, const AES_KEY *key2, | ||
| 74 | const unsigned char iv[16], int encrypt); | ||
| 75 | |||
| 72 | __END_HIDDEN_DECLS | 76 | __END_HIDDEN_DECLS |
| 73 | 77 | ||
| 74 | #endif /* !HEADER_AES_LOCAL_H */ | 78 | #endif /* !HEADER_AES_LOCAL_H */ |
