From abb03e21a8d0fc7f97a871f5aee5a8084176540f Mon Sep 17 00:00:00 2001 From: jsing <> Date: Fri, 27 Jun 2025 17:10:45 +0000 Subject: 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@ --- src/lib/libcrypto/evp/e_aes.c | 68 +------------------------------------------ 1 file changed, 1 insertion(+), 67 deletions(-) (limited to 'src/lib/libcrypto/evp') 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 @@ -/* $OpenBSD: e_aes.c,v 1.75 2025/06/16 14:50:30 jsing Exp $ */ +/* $OpenBSD: e_aes.c,v 1.76 2025/06/27 17:10:45 jsing Exp $ */ /* ==================================================================== * Copyright (c) 2001-2011 The OpenSSL Project. All rights reserved. * @@ -159,21 +159,6 @@ void aesni_ccm64_decrypt_blocks (const unsigned char *in, unsigned char *out, size_t blocks, const void *key, const unsigned char ivec[16], unsigned char cmac[16]); -static int -aesni_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, - const unsigned char *in, size_t len) -{ - EVP_AES_KEY *eak = ctx->cipher_data; - unsigned int num = ctx->num; - - CRYPTO_ctr128_encrypt_ctr32(in, out, len, &eak->ks, ctx->iv, ctx->buf, - &num, aesni_ctr32_encrypt_blocks); - - ctx->num = (size_t)num; - - return 1; -} - static int aesni_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t len) @@ -562,19 +547,6 @@ EVP_aes_128_cfb8(void) } LCRYPTO_ALIAS(EVP_aes_128_cfb8); -#ifdef AESNI_CAPABLE -static const EVP_CIPHER aesni_128_ctr = { - .nid = NID_aes_128_ctr, - .block_size = 1, - .key_len = 16, - .iv_len = 16, - .flags = EVP_CIPH_CTR_MODE, - .init = aes_init_key, - .do_cipher = aesni_ctr_cipher, - .ctx_size = sizeof(EVP_AES_KEY), -}; -#endif - static const EVP_CIPHER aes_128_ctr = { .nid = NID_aes_128_ctr, .block_size = 1, @@ -589,11 +561,7 @@ static const EVP_CIPHER aes_128_ctr = { const EVP_CIPHER * EVP_aes_128_ctr(void) { -#ifdef AESNI_CAPABLE - return AESNI_CAPABLE ? &aesni_128_ctr : &aes_128_ctr; -#else return &aes_128_ctr; -#endif } LCRYPTO_ALIAS(EVP_aes_128_ctr); @@ -722,19 +690,6 @@ EVP_aes_192_cfb8(void) } LCRYPTO_ALIAS(EVP_aes_192_cfb8); -#ifdef AESNI_CAPABLE -static const EVP_CIPHER aesni_192_ctr = { - .nid = NID_aes_192_ctr, - .block_size = 1, - .key_len = 24, - .iv_len = 16, - .flags = EVP_CIPH_CTR_MODE, - .init = aes_init_key, - .do_cipher = aesni_ctr_cipher, - .ctx_size = sizeof(EVP_AES_KEY), -}; -#endif - static const EVP_CIPHER aes_192_ctr = { .nid = NID_aes_192_ctr, .block_size = 1, @@ -749,11 +704,7 @@ static const EVP_CIPHER aes_192_ctr = { const EVP_CIPHER * EVP_aes_192_ctr(void) { -#ifdef AESNI_CAPABLE - return AESNI_CAPABLE ? &aesni_192_ctr : &aes_192_ctr; -#else return &aes_192_ctr; -#endif } LCRYPTO_ALIAS(EVP_aes_192_ctr); @@ -882,19 +833,6 @@ EVP_aes_256_cfb8(void) } LCRYPTO_ALIAS(EVP_aes_256_cfb8); -#ifdef AESNI_CAPABLE -static const EVP_CIPHER aesni_256_ctr = { - .nid = NID_aes_256_ctr, - .block_size = 1, - .key_len = 32, - .iv_len = 16, - .flags = EVP_CIPH_CTR_MODE, - .init = aes_init_key, - .do_cipher = aesni_ctr_cipher, - .ctx_size = sizeof(EVP_AES_KEY), -}; -#endif - static const EVP_CIPHER aes_256_ctr = { .nid = NID_aes_256_ctr, .block_size = 1, @@ -909,11 +847,7 @@ static const EVP_CIPHER aes_256_ctr = { const EVP_CIPHER * EVP_aes_256_ctr(void) { -#ifdef AESNI_CAPABLE - return AESNI_CAPABLE ? &aesni_256_ctr : &aes_256_ctr; -#else return &aes_256_ctr; -#endif } LCRYPTO_ALIAS(EVP_aes_256_ctr); -- cgit v1.2.3-55-g6feb