Linux cryptographic layer development
 help / color / mirror / Atom feed
From: Eric Biggers <ebiggers@kernel.org>
To: linux-crypto@vger.kernel.org
Cc: Ard Biesheuvel <ardb@kernel.org>,
	"Jason A . Donenfeld" <Jason@zx2c4.com>,
	Herbert Xu <herbert@gondor.apana.org.au>,
	x86@kernel.org, Antonio Quartulli <antonio@mandelbit.com>,
	Eric Biggers <ebiggers@kernel.org>
Subject: [PATCH 03/11] lib/crypto: aes-gcm: Add one-shot AES-GCM arch hooks
Date: Sun, 29 Nov 2026 12:02:37 -0800	[thread overview]
Message-ID: <20261129200245.146503-4-ebiggers@kernel.org> (raw)
In-Reply-To: <20261129200245.146503-1-ebiggers@kernel.org>

The incremental AES-GCM API has a fair bit of per-message overhead due
to multiple SIMD/FPU sections, separate J_0 encryption and GHASH calls,
tag copying, etc.

Therefore, add hooks to the one-shot AES-GCM en/decryption functions to
allow architectures to optimize the full AES-GCM.

This will be useful in two cases:

- Direct calls to aes_gcm_encrypt() and aes_gcm_decrypt()

- Calls to aes_gcm_encrypt() and aes_gcm_decrypt() via crypto_aead for
  linear requests, once crypto/aes.c is updated to make those calls.

The latter is needed to avoid regressing "gcm(aes)" crypto_aead
performance on short messages once the "gcm(aes)" crypto_aead
implementations in arch/x86/crypto/ (which have similar short-message
optimizations) are superseded by the library-based code.

For efficiency, pass a pointer to 'struct aes_gcm_key' rather than its
embedded 'struct aes_enckey' and 'struct ghash_key' separately.  Update
aes_gcm_{en,de}crypt_update_arch() to use this convention too, which
conveniently makes all their arguments fit into registers on x86_64.

Signed-off-by: Eric Biggers <ebiggers@kernel.org>
---
 lib/crypto/aes.c | 97 +++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 83 insertions(+), 14 deletions(-)

diff --git a/lib/crypto/aes.c b/lib/crypto/aes.c
index 13d934ba712c3..7b0486953b4df 100644
--- a/lib/crypto/aes.c
+++ b/lib/crypto/aes.c
@@ -1483,8 +1483,7 @@ static inline void aes_xts_fips_test(void)
 static bool aes_gcm_encrypt_update_arch(u8 *dst, const u8 *src, size_t len,
 					struct polyval_elem *ghash_acc,
 					const __be32 ctr32[4],
-					const struct aes_enckey *aes_key,
-					const struct ghash_key *ghash_key)
+					const struct aes_gcm_key *key)
 {
 	return false;
 }
@@ -1493,8 +1492,35 @@ static bool aes_gcm_encrypt_update_arch(u8 *dst, const u8 *src, size_t len,
 static bool aes_gcm_decrypt_update_arch(u8 *dst, const u8 *src, size_t len,
 					struct polyval_elem *ghash_acc,
 					const __be32 ctr32[4],
-					const struct aes_enckey *aes_key,
-					const struct ghash_key *ghash_key)
+					const struct aes_gcm_key *key)
+{
+	return false;
+}
+#endif
+
+/*
+ * Hooks for optimized one-shot AES-GCM implementations, overridable by the
+ * architecture to avoid the overhead of the incremental API.
+ *
+ * Returning false causes the incremental implementation to be used instead.
+ *
+ * The decryption hook (if it returns true) must check the auth tag and store
+ * the status (0 or -EBADMSG) at *err.
+ */
+#ifndef aes_gcm_encrypt_arch
+static bool aes_gcm_encrypt_arch(u8 *dst, const u8 *src, size_t data_len,
+				 u8 *authtag, const u8 *ad, size_t ad_len,
+				 const u8 nonce[12],
+				 const struct aes_gcm_key *key)
+{
+	return false;
+}
+#endif
+#ifndef aes_gcm_decrypt_arch
+static bool aes_gcm_decrypt_arch(u8 *dst, const u8 *src, size_t data_len,
+				 const u8 *authtag, const u8 *ad, size_t ad_len,
+				 const u8 nonce[12],
+				 const struct aes_gcm_key *key, int *err)
 {
 	return false;
 }
@@ -1598,7 +1624,7 @@ static __always_inline void aes_gcm_crypt_update(struct aes_gcm_ctx *ctx,
 		if (enc) {
 			if (likely(aes_gcm_encrypt_update_arch(
 				    dst, src, n, &ctx->ghash.acc, ctx->ctr32,
-				    &ctx->key->aes, &ctx->key->ghash))) {
+				    ctx->key))) {
 				be32_add_cpu(&ctx->ctr32[3],
 					     n / AES_BLOCK_SIZE);
 			} else {
@@ -1608,7 +1634,7 @@ static __always_inline void aes_gcm_crypt_update(struct aes_gcm_ctx *ctx,
 		} else {
 			if (likely(aes_gcm_decrypt_update_arch(
 				    dst, src, n, &ctx->ghash.acc, ctx->ctr32,
-				    &ctx->key->aes, &ctx->key->ghash))) {
+				    ctx->key))) {
 				be32_add_cpu(&ctx->ctr32[3],
 					     n / AES_BLOCK_SIZE);
 			} else {
@@ -1706,9 +1732,15 @@ int aes_gcm_decrypt_final(struct aes_gcm_ctx *ctx, const u8 *authtag)
 }
 EXPORT_SYMBOL_GPL(aes_gcm_decrypt_final);
 
-void aes_gcm_encrypt(u8 *dst, const u8 *src, size_t data_len, u8 *authtag,
-		     const u8 *ad, size_t ad_len, const u8 nonce[12],
-		     const struct aes_gcm_key *key)
+#ifdef aes_gcm_encrypt_arch
+/* Keep struct aes_gcm_ctx out of the fast path stack frame. */
+static noinline void
+#else
+static inline void
+#endif
+aes_gcm_encrypt_fallback(u8 *dst, const u8 *src, size_t data_len, u8 *authtag,
+			 const u8 *ad, size_t ad_len, const u8 nonce[12],
+			 const struct aes_gcm_key *key)
 {
 	struct aes_gcm_ctx ctx;
 
@@ -1717,19 +1749,56 @@ void aes_gcm_encrypt(u8 *dst, const u8 *src, size_t data_len, u8 *authtag,
 	aes_gcm_encrypt_update(&ctx, dst, src, data_len);
 	aes_gcm_encrypt_final(&ctx, authtag);
 }
+
+void aes_gcm_encrypt(u8 *dst, const u8 *src, size_t data_len, u8 *authtag,
+		     const u8 *ad, size_t ad_len, const u8 nonce[12],
+		     const struct aes_gcm_key *key)
+{
+	WARN_ON_ONCE((u64)ad_len > AES_GCM_MAX_AD_LEN);
+	WARN_ON_ONCE((u64)data_len > AES_GCM_MAX_DATA_LEN);
+
+	if (likely(aes_gcm_encrypt_arch(dst, src, data_len, authtag, ad, ad_len,
+					nonce, key)))
+		return;
+	aes_gcm_encrypt_fallback(dst, src, data_len, authtag, ad, ad_len, nonce,
+				 key);
+}
 EXPORT_SYMBOL_GPL(aes_gcm_encrypt);
 
-int aes_gcm_decrypt(u8 *dst, const u8 *src, size_t data_len, const u8 *authtag,
-		    const u8 *ad, size_t ad_len, const u8 nonce[12],
-		    const struct aes_gcm_key *key)
+#ifdef aes_gcm_decrypt_arch
+/* Keep struct aes_gcm_ctx out of the fast path stack frame. */
+static noinline int
+#else
+static inline int
+#endif
+aes_gcm_decrypt_fallback(u8 *dst, const u8 *src, size_t data_len,
+			 const u8 *authtag, const u8 *ad, size_t ad_len,
+			 const u8 nonce[12], const struct aes_gcm_key *key)
 {
 	struct aes_gcm_ctx ctx;
-	int err;
 
 	aes_gcm_init(&ctx, nonce, key);
 	aes_gcm_auth_update(&ctx, ad, ad_len);
 	aes_gcm_decrypt_update(&ctx, dst, src, data_len);
-	err = aes_gcm_decrypt_final(&ctx, authtag);
+	return aes_gcm_decrypt_final(&ctx, authtag);
+}
+
+int aes_gcm_decrypt(u8 *dst, const u8 *src, size_t data_len, const u8 *authtag,
+		    const u8 *ad, size_t ad_len, const u8 nonce[12],
+		    const struct aes_gcm_key *key)
+{
+	int err;
+
+	if (WARN_ON_ONCE((u64)ad_len > AES_GCM_MAX_AD_LEN) ||
+	    WARN_ON_ONCE((u64)data_len > AES_GCM_MAX_DATA_LEN))
+		return -EBADMSG;
+
+	if (likely(aes_gcm_decrypt_arch(dst, src, data_len, authtag, ad, ad_len,
+					nonce, key, &err)))
+		goto out;
+	err = aes_gcm_decrypt_fallback(dst, src, data_len, authtag, ad, ad_len,
+				       nonce, key);
+out:
 	if (unlikely(err) && data_len) {
 		/*
 		 * Clear the inauthentic decrypted data so that callers won't
-- 
2.56.1


  parent reply	other threads:[~2026-11-29 20:04 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-29 20:02 [PATCH 00/11] Migrate x86_64 AES-GCM code into library Eric Biggers
2026-11-29 20:02 ` [PATCH 01/11] crypto: aes - Flush dcache in CBC-CTS and XTS linear fast paths Eric Biggers
2026-11-29 20:02 ` [PATCH 02/11] lib/crypto: gf128hash: Link libgf128hash before libaes Eric Biggers
2026-11-29 20:02 ` Eric Biggers [this message]
2026-11-29 20:02 ` [PATCH 04/11] crypto: aes - Add linear fast path for AES-GCM Eric Biggers
2026-11-29 20:02 ` [PATCH 05/11] lib/crypto: x86/gf128hash: Optimize GHASH and POLYVAL with PCLMULQDQ and AVX Eric Biggers
2026-11-29 20:02 ` [PATCH 06/11] lib/crypto: x86/gf128hash: Optimize GHASH and POLYVAL with VPCLMULQDQ and AVX2 Eric Biggers
2026-11-29 20:02 ` [PATCH 07/11] lib/crypto: x86/gf128hash: Optimize GHASH and POLYVAL with VPCLMULQDQ and AVX512 Eric Biggers
2026-11-29 20:02 ` [PATCH 08/11] lib/crypto: x86/aes-gcm: Migrate AES-NI optimized code into library Eric Biggers
2026-11-29 20:02 ` [PATCH 09/11] lib/crypto: x86/aes-gcm: Migrate VAES+AVX2 " Eric Biggers
2026-11-29 20:02 ` [PATCH 10/11] lib/crypto: x86/aes-gcm: Migrate VAES+AVX512 " Eric Biggers
2026-11-29 20:02 ` [PATCH 11/11] crypto: x86/aesni-intel - Remove empty module Eric Biggers

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261129200245.146503-4-ebiggers@kernel.org \
    --to=ebiggers@kernel.org \
    --cc=Jason@zx2c4.com \
    --cc=antonio@mandelbit.com \
    --cc=ardb@kernel.org \
    --cc=herbert@gondor.apana.org.au \
    --cc=linux-crypto@vger.kernel.org \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox