All the mail mirrored from lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] crypto: x86/aes-gcm - simplify GCM hash subkey derivation
@ 2024-04-20  6:00 Eric Biggers
  2024-04-20 18:19 ` Eric Biggers
  0 siblings, 1 reply; 3+ messages in thread
From: Eric Biggers @ 2024-04-20  6:00 UTC (permalink / raw
  To: linux-crypto; +Cc: x86

From: Eric Biggers <ebiggers@google.com>

Remove a redundant expansion of the AES key, and utilize the zero page.
Also rename rfc4106_set_hash_subkey() to aes_gcm_derive_hash_subkey()
because it's used for both versions of AES-GCM, not just RFC4106.

Signed-off-by: Eric Biggers <ebiggers@google.com>
---
 arch/x86/crypto/aesni-intel_glue.c | 26 +++++++-------------------
 1 file changed, 7 insertions(+), 19 deletions(-)

diff --git a/arch/x86/crypto/aesni-intel_glue.c b/arch/x86/crypto/aesni-intel_glue.c
index 110b3282a1f2..b4058c3d410d 100644
--- a/arch/x86/crypto/aesni-intel_glue.c
+++ b/arch/x86/crypto/aesni-intel_glue.c
@@ -38,11 +38,10 @@
 
 
 #define AESNI_ALIGN	16
 #define AESNI_ALIGN_ATTR __attribute__ ((__aligned__(AESNI_ALIGN)))
 #define AES_BLOCK_MASK	(~(AES_BLOCK_SIZE - 1))
-#define RFC4106_HASH_SUBKEY_SIZE 16
 #define AESNI_ALIGN_EXTRA ((AESNI_ALIGN - 1) & ~(CRYPTO_MINALIGN - 1))
 #define CRYPTO_AES_CTX_SIZE (sizeof(struct crypto_aes_ctx) + AESNI_ALIGN_EXTRA)
 #define XTS_AES_CTX_SIZE (sizeof(struct aesni_xts_ctx) + AESNI_ALIGN_EXTRA)
 
 /* This data is stored at the end of the crypto_tfm struct.
@@ -588,27 +587,14 @@ static int xctr_crypt(struct skcipher_request *req)
 		err = skcipher_walk_done(&walk, nbytes);
 	}
 	return err;
 }
 
-static int
-rfc4106_set_hash_subkey(u8 *hash_subkey, const u8 *key, unsigned int key_len)
+static int aes_gcm_derive_hash_subkey(const struct crypto_aes_ctx *aes_key,
+				      u8 hash_subkey[AES_BLOCK_SIZE])
 {
-	struct crypto_aes_ctx ctx;
-	int ret;
-
-	ret = aes_expandkey(&ctx, key, key_len);
-	if (ret)
-		return ret;
-
-	/* Clear the data in the hash sub key container to zero.*/
-	/* We want to cipher all zeros to create the hash sub key. */
-	memset(hash_subkey, 0, RFC4106_HASH_SUBKEY_SIZE);
-
-	aes_encrypt(&ctx, hash_subkey, hash_subkey);
-
-	memzero_explicit(&ctx, sizeof(ctx));
+	aes_encrypt(aes_key, hash_subkey, page_address(ZERO_PAGE(0)));
 	return 0;
 }
 
 static int common_rfc4106_set_key(struct crypto_aead *aead, const u8 *key,
 				  unsigned int key_len)
@@ -622,11 +608,12 @@ static int common_rfc4106_set_key(struct crypto_aead *aead, const u8 *key,
 	key_len -= 4;
 
 	memcpy(ctx->nonce, key + key_len, sizeof(ctx->nonce));
 
 	return aes_set_key_common(&ctx->aes_key_expanded, key, key_len) ?:
-	       rfc4106_set_hash_subkey(ctx->hash_subkey, key, key_len);
+	       aes_gcm_derive_hash_subkey(&ctx->aes_key_expanded,
+					  ctx->hash_subkey);
 }
 
 /* This is the Integrity Check Value (aka the authentication tag) length and can
  * be 8, 12 or 16 bytes long. */
 static int common_rfc4106_set_authsize(struct crypto_aead *aead,
@@ -1328,11 +1315,12 @@ static int generic_gcmaes_set_key(struct crypto_aead *aead, const u8 *key,
 				  unsigned int key_len)
 {
 	struct generic_gcmaes_ctx *ctx = generic_gcmaes_ctx_get(aead);
 
 	return aes_set_key_common(&ctx->aes_key_expanded, key, key_len) ?:
-	       rfc4106_set_hash_subkey(ctx->hash_subkey, key, key_len);
+	       aes_gcm_derive_hash_subkey(&ctx->aes_key_expanded,
+					  ctx->hash_subkey);
 }
 
 static int generic_gcmaes_encrypt(struct aead_request *req)
 {
 	struct crypto_aead *tfm = crypto_aead_reqtfm(req);

base-commit: 543ea178fbfadeaf79e15766ac989f3351349f02
-- 
2.44.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] crypto: x86/aes-gcm - simplify GCM hash subkey derivation
  2024-04-20  6:00 [PATCH] crypto: x86/aes-gcm - simplify GCM hash subkey derivation Eric Biggers
@ 2024-04-20 18:19 ` Eric Biggers
  2024-04-26  9:39   ` Herbert Xu
  0 siblings, 1 reply; 3+ messages in thread
From: Eric Biggers @ 2024-04-20 18:19 UTC (permalink / raw
  To: linux-crypto; +Cc: x86

On Fri, Apr 19, 2024 at 11:00:37PM -0700, Eric Biggers wrote:
> +	aes_encrypt(aes_key, hash_subkey, page_address(ZERO_PAGE(0)));

Actually, page_address(ZERO_PAGE(0)) expands into a surprisingly large number of
instructions.  Using empty_zero_page directly would avoid this, but there's
little precedent for doing that.  For now, I think just using something like
'static const u8 zeroes[16]' is the way to go for small buffers like this.

- Eric

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] crypto: x86/aes-gcm - simplify GCM hash subkey derivation
  2024-04-20 18:19 ` Eric Biggers
@ 2024-04-26  9:39   ` Herbert Xu
  0 siblings, 0 replies; 3+ messages in thread
From: Herbert Xu @ 2024-04-26  9:39 UTC (permalink / raw
  To: Eric Biggers; +Cc: linux-crypto, x86

Eric Biggers <ebiggers@kernel.org> wrote:
> On Fri, Apr 19, 2024 at 11:00:37PM -0700, Eric Biggers wrote:
>> +     aes_encrypt(aes_key, hash_subkey, page_address(ZERO_PAGE(0)));
> 
> Actually, page_address(ZERO_PAGE(0)) expands into a surprisingly large number of
> instructions.  Using empty_zero_page directly would avoid this, but there's
> little precedent for doing that.  For now, I think just using something like
> 'static const u8 zeroes[16]' is the way to go for small buffers like this.

Yes it's not worth the effort given the small size.  But still it
looks like lib/raid6 could benefit from using a shared zero page
too.

Cheers,
-- 
Email: Herbert Xu <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-04-26  9:39 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-04-20  6:00 [PATCH] crypto: x86/aes-gcm - simplify GCM hash subkey derivation Eric Biggers
2024-04-20 18:19 ` Eric Biggers
2024-04-26  9:39   ` Herbert Xu

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.