summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/evp
diff options
context:
space:
mode:
authorjsing <>2025-06-27 17:10:45 +0000
committerjsing <>2025-06-27 17:10:45 +0000
commitabb03e21a8d0fc7f97a871f5aee5a8084176540f (patch)
tree8acdb3ab7caf1e6f49b7bf3d7e6a066ca52920a2 /src/lib/libcrypto/evp
parentc5c4895f860c5e071b09ef5f94bcfae0a51b148e (diff)
downloadopenbsd-abb03e21a8d0fc7f97a871f5aee5a8084176540f.tar.gz
openbsd-abb03e21a8d0fc7f97a871f5aee5a8084176540f.tar.bz2
openbsd-abb03e21a8d0fc7f97a871f5aee5a8084176540f.zip
Move AES-NI from EVP to AES for CTR mode.
The mode implementation for CTR has two variants - one takes the block function, while the other takes a "ctr32" function. The latter is expected to handle the lower 32 bits of the IV/counter, but is not expected to handle overflow. The AES-NI implementation for CTR currently uses the second variant. Provide aes_ctr32_encrypt_internal() as a function that can be replaced on a machine dependent basis, along with an aes_ctr32_encrypt_generic() function that provides the default implementation and can be used as a fallback. Wire up the AES-NI version for amd64 and i386, change AES_ctr128_encrypt() to use CRYPTO_ctr128_encrypt_ctr32() (which calls aes_ctr32_encrypt_internal()) and remove the various AES-NI specific EVP_CIPHER methods for CTR. Callers of AES_ctr128_encrypt() will now use AES-NI, if available. ok tb@
Diffstat (limited to 'src/lib/libcrypto/evp')
-rw-r--r--src/lib/libcrypto/evp/e_aes.c68
1 files changed, 1 insertions, 67 deletions
diff --git a/src/lib/libcrypto/evp/e_aes.c b/src/lib/libcrypto/evp/e_aes.c
index 34f5513300..b00eb048e8 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.75 2025/06/16 14:50:30 jsing Exp $ */ 1/* $OpenBSD: e_aes.c,v 1.76 2025/06/27 17:10:45 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 *
@@ -160,21 +160,6 @@ void aesni_ccm64_decrypt_blocks (const unsigned char *in, unsigned char *out,
160 unsigned char cmac[16]); 160 unsigned char cmac[16]);
161 161
162static int 162static int
163aesni_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
164 const unsigned char *in, size_t len)
165{
166 EVP_AES_KEY *eak = ctx->cipher_data;
167 unsigned int num = ctx->num;
168
169 CRYPTO_ctr128_encrypt_ctr32(in, out, len, &eak->ks, ctx->iv, ctx->buf,
170 &num, aesni_ctr32_encrypt_blocks);
171
172 ctx->num = (size_t)num;
173
174 return 1;
175}
176
177static int
178aesni_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, 163aesni_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
179 const unsigned char *in, size_t len) 164 const unsigned char *in, size_t len)
180{ 165{
@@ -562,19 +547,6 @@ EVP_aes_128_cfb8(void)
562} 547}
563LCRYPTO_ALIAS(EVP_aes_128_cfb8); 548LCRYPTO_ALIAS(EVP_aes_128_cfb8);
564 549
565#ifdef AESNI_CAPABLE
566static const EVP_CIPHER aesni_128_ctr = {
567 .nid = NID_aes_128_ctr,
568 .block_size = 1,
569 .key_len = 16,
570 .iv_len = 16,
571 .flags = EVP_CIPH_CTR_MODE,
572 .init = aes_init_key,
573 .do_cipher = aesni_ctr_cipher,
574 .ctx_size = sizeof(EVP_AES_KEY),
575};
576#endif
577
578static const EVP_CIPHER aes_128_ctr = { 550static const EVP_CIPHER aes_128_ctr = {
579 .nid = NID_aes_128_ctr, 551 .nid = NID_aes_128_ctr,
580 .block_size = 1, 552 .block_size = 1,
@@ -589,11 +561,7 @@ static const EVP_CIPHER aes_128_ctr = {
589const EVP_CIPHER * 561const EVP_CIPHER *
590EVP_aes_128_ctr(void) 562EVP_aes_128_ctr(void)
591{ 563{
592#ifdef AESNI_CAPABLE
593 return AESNI_CAPABLE ? &aesni_128_ctr : &aes_128_ctr;
594#else
595 return &aes_128_ctr; 564 return &aes_128_ctr;
596#endif
597} 565}
598LCRYPTO_ALIAS(EVP_aes_128_ctr); 566LCRYPTO_ALIAS(EVP_aes_128_ctr);
599 567
@@ -722,19 +690,6 @@ EVP_aes_192_cfb8(void)
722} 690}
723LCRYPTO_ALIAS(EVP_aes_192_cfb8); 691LCRYPTO_ALIAS(EVP_aes_192_cfb8);
724 692
725#ifdef AESNI_CAPABLE
726static const EVP_CIPHER aesni_192_ctr = {
727 .nid = NID_aes_192_ctr,
728 .block_size = 1,
729 .key_len = 24,
730 .iv_len = 16,
731 .flags = EVP_CIPH_CTR_MODE,
732 .init = aes_init_key,
733 .do_cipher = aesni_ctr_cipher,
734 .ctx_size = sizeof(EVP_AES_KEY),
735};
736#endif
737
738static const EVP_CIPHER aes_192_ctr = { 693static const EVP_CIPHER aes_192_ctr = {
739 .nid = NID_aes_192_ctr, 694 .nid = NID_aes_192_ctr,
740 .block_size = 1, 695 .block_size = 1,
@@ -749,11 +704,7 @@ static const EVP_CIPHER aes_192_ctr = {
749const EVP_CIPHER * 704const EVP_CIPHER *
750EVP_aes_192_ctr(void) 705EVP_aes_192_ctr(void)
751{ 706{
752#ifdef AESNI_CAPABLE
753 return AESNI_CAPABLE ? &aesni_192_ctr : &aes_192_ctr;
754#else
755 return &aes_192_ctr; 707 return &aes_192_ctr;
756#endif
757} 708}
758LCRYPTO_ALIAS(EVP_aes_192_ctr); 709LCRYPTO_ALIAS(EVP_aes_192_ctr);
759 710
@@ -882,19 +833,6 @@ EVP_aes_256_cfb8(void)
882} 833}
883LCRYPTO_ALIAS(EVP_aes_256_cfb8); 834LCRYPTO_ALIAS(EVP_aes_256_cfb8);
884 835
885#ifdef AESNI_CAPABLE
886static const EVP_CIPHER aesni_256_ctr = {
887 .nid = NID_aes_256_ctr,
888 .block_size = 1,
889 .key_len = 32,
890 .iv_len = 16,
891 .flags = EVP_CIPH_CTR_MODE,
892 .init = aes_init_key,
893 .do_cipher = aesni_ctr_cipher,
894 .ctx_size = sizeof(EVP_AES_KEY),
895};
896#endif
897
898static const EVP_CIPHER aes_256_ctr = { 836static const EVP_CIPHER aes_256_ctr = {
899 .nid = NID_aes_256_ctr, 837 .nid = NID_aes_256_ctr,
900 .block_size = 1, 838 .block_size = 1,
@@ -909,11 +847,7 @@ static const EVP_CIPHER aes_256_ctr = {
909const EVP_CIPHER * 847const EVP_CIPHER *
910EVP_aes_256_ctr(void) 848EVP_aes_256_ctr(void)
911{ 849{
912#ifdef AESNI_CAPABLE
913 return AESNI_CAPABLE ? &aesni_256_ctr : &aes_256_ctr;
914#else
915 return &aes_256_ctr; 850 return &aes_256_ctr;
916#endif
917} 851}
918LCRYPTO_ALIAS(EVP_aes_256_ctr); 852LCRYPTO_ALIAS(EVP_aes_256_ctr);
919 853