Linux cryptographic layer development
 help / color / mirror / Atom feed
From: Antonio Quartulli <antonio@mandelbit.com>
To: Eric Biggers <ebiggers@kernel.org>, linux-crypto@vger.kernel.org
Cc: Antonio Quartulli <antonio@mandelbit.com>,
	Ard Biesheuvel <ardb@kernel.org>,
	"Jason A . Donenfeld" <Jason@zx2c4.com>,
	Herbert Xu <herbert@gondor.apana.org.au>,
	x86@kernel.org
Subject: [RFC PATCH] lib/crypto: x86/aes-gcm: Process short associated data in one asm call
Date: Sat,  5 Dec 2026 20:51:06 +0100	[thread overview]
Message-ID: <20261205195107.181324-1-antonio@mandelbit.com> (raw)
In-Reply-To: <20261129200245.146503-1-ebiggers@kernel.org>

The one-shot AES-GCM path XORs associated data shorter than one block
byte by byte into the zeroed GHASH accumulator in memory and then calls
the GHASH multiplication, which reloads it.  Network protocols commonly
use 8 to 13 bytes of associated data, so this runs once per packet.

Add aes_gcm_ad_short_vaes_avx512(), which loads 1 to 15 bytes of
associated data with a masked load straight from the caller's buffer,
multiplies the zero-padded block by H in registers and stores the
accumulator once, without reading it.  Use it from aes_gcm_crypt_x86()
on the VAES+AVX512 tier, and zero the accumulator only on the other
paths.

Signed-off-by: Antonio Quartulli <antonio@mandelbit.com>
---
Hi Eric,

this patch applies on top of your series (v1).

I converted ovpn to aes_gcm_encrypt()/aes_gcm_decrypt() for linear
packets (8 bytes of associated data per record) and compared it with
what we had been testing before: the pre-migration VAES+AVX512 code called
directly from ovpn in one FPU section, as mentioned in one of my earlier
mails.

The library was about 2% slower on the receiver. Most of the difference
was the sub-block associated data path, which this patch replaces on the
VAES+AVX512 tier.

Single ovpn UDP tunnel, AMD Ryzen 9 9950X, 100G mlx5, receiver-bound,
3 rounds each; library vs the direct calls, on the same boot:

                        your series     + this patch
  UDP inside tunnel       -2.1%            +0.2%
  TCP inside tunnel       -2.3%            -2.0%

Receiver AES-GCM cost per packet (receiver cycles with AES-GCM minus
AEGIS on the same variant), library minus direct calls:

  UDP inside tunnel      +30 cycles      +5 cycles
  TCP inside tunnel      +25 cycles     +15 cycles

TCP is noisier; what is left there is mostly the decrypt residual below.
For records of 32 to 128 bytes (e.g. the TCP ACKs) the library is
within a few cycles of the direct calls.

Per operation in a pinned VM (AES-256, 1420 bytes of data, 8 bytes of
associated data; TSC ticks, library vs direct calls):

                        your series     + this patch
  encrypt                 410 vs 400      406 vs 403
  decrypt                 417 vs 398      409 vs 400

The remaining decrypt difference sits in the C glue around the asm
calls (about 40 more instructions per operation); the asm itself is not
slower.

Only the VAES+AVX512 tier is covered, since masked loads make this a
very small function. The VAES+AVX2 tier could do the same with a
partial-block load; I can follow up with that if you think it is worth
it.....
If it makes sense to you, feel free to fold this into patch 10.

Tested with the aes_gcm, ghash and polyval KUnit suites, both on
VAES+AVX512 and booted with clearcpuid=avx512bw (VAES+AVX2 tier), and by
cross-checking encryption, decryption and tag rejection against the
pre-migration code for every associated data length from 0 to 40 bytes
combined with data lengths 0, 1, 15, 16, 17, 255 and 1420.

**Only tested on AMD Zen 5**

Thanks!
Antonio

 lib/crypto/x86/aes-gcm-vaes-avx512.S | 39 ++++++++++++++++++++++++++++
 lib/crypto/x86/aes.h                 | 19 +++++++++++---
 2 files changed, 55 insertions(+), 3 deletions(-)

diff --git a/lib/crypto/x86/aes-gcm-vaes-avx512.S b/lib/crypto/x86/aes-gcm-vaes-avx512.S
index 70b9adc02b41..085cff5fdfef 100644
--- a/lib/crypto/x86/aes-gcm-vaes-avx512.S
+++ b/lib/crypto/x86/aes-gcm-vaes-avx512.S
@@ -896,6 +896,45 @@ SYM_FUNC_START(aes_gcm_dec_update_vaes_avx512)
 	_aes_gcm_update	0
 SYM_FUNC_END(aes_gcm_dec_update_vaes_avx512)
 
+// void aes_gcm_ad_short_vaes_avx512(const struct aes_gcm_key *key,
+//				     struct polyval_elem *ghash_acc,
+//				     const u8 *ad, size_t ad_len);
+//
+// Process associated data that is shorter than one block and is the whole
+// associated data of a message: 1 <= |ad_len| <= 15.  The GHASH accumulator
+// would be zero, so it is not read: *|ghash_acc| is set to the zero-padded
+// block multiplied by H.  The block is loaded with a masked load straight from
+// |ad|, reading exactly |ad_len| bytes.  This is the common case for network
+// protocols (8 to 13 bytes), and doing it in one short leaf function avoids
+// the generic path's byte-wise update of the accumulator in memory followed by
+// a vector load of it.
+SYM_FUNC_START(aes_gcm_ad_short_vaes_avx512)
+	// Function arguments
+	.set	KEY,		%rdi
+	.set	GHASH_ACC_PTR,	%rsi
+	.set	AD,		%rdx
+	.set	ADLEN,		%ecx
+	// %eax, %xmm1-%xmm3, and %k1 are used as temporary registers.
+	.set	GFPOLY,		%xmm4
+	.set	GHASH_ACC,	%xmm6
+	.set	H_POW1,		%xmm7
+
+	vmovdqa		.Lgfpoly(%rip), GFPOLY
+	mov		$-1, %eax
+	bzhi		ADLEN, %eax, %eax
+	kmovd		%eax, %k1
+	vmovdqu8	(AD), GHASH_ACC{%k1}{z}
+	vpshufb		.Lbswap_mask(%rip), GHASH_ACC, GHASH_ACC
+	vmovdqu8	OFFSETOFEND_H_POWERS-16(KEY), H_POW1
+.irp i, 0,1,2,3,4,5,6,7,8,9
+	_ghash_mul_step	\i, H_POW1, GHASH_ACC, GHASH_ACC, GFPOLY, \
+			%xmm1, %xmm2, %xmm3
+.endr
+	vmovdqu		GHASH_ACC, (GHASH_ACC_PTR)
+	// No need for vzeroupper, since only xmm registers were used.
+	RET
+SYM_FUNC_END(aes_gcm_ad_short_vaes_avx512)
+
 SYM_FUNC_START(aes_gcm_enc_final_vaes_avx512)
 	_aes_gcm_final	1
 SYM_FUNC_END(aes_gcm_enc_final_vaes_avx512)
diff --git a/lib/crypto/x86/aes.h b/lib/crypto/x86/aes.h
index e0c65904eef0..5076cfd16fa4 100644
--- a/lib/crypto/x86/aes.h
+++ b/lib/crypto/x86/aes.h
@@ -486,6 +486,9 @@ bool aes_gcm_dec_final_vaes_avx2(const struct aes_gcm_key *key,
 				 const struct polyval_elem *ghash_acc,
 				 u64 total_aadlen, u64 total_datalen,
 				 const u8 *authtag);
+void aes_gcm_ad_short_vaes_avx512(const struct aes_gcm_key *key,
+				  struct polyval_elem *ghash_acc,
+				  const u8 *ad, size_t ad_len);
 void aes_gcm_enc_final_vaes_avx512(const struct aes_gcm_key *key,
 				   const u8 nonce[12],
 				   const struct polyval_elem *ghash_acc,
@@ -621,7 +624,7 @@ aes_gcm_crypt_x86(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, bool enc, int *err)
 {
-	struct polyval_elem ghash_acc = {};
+	struct polyval_elem ghash_acc;
 	__be32 ctr32[4];
 
 	if (!static_branch_likely(&have_aesni_pclmul) ||
@@ -633,8 +636,18 @@ aes_gcm_crypt_x86(u8 *dst, const u8 *src, size_t data_len, u8 *authtag,
 
 	kernel_fpu_begin_mask(0);
 
-	if (ad_len)
-		aes_gcm_process_ad_x86(key, &ghash_acc, ad, ad_len);
+	/*
+	 * Associated data shorter than one block (common for network protocols)
+	 * initializes the accumulator directly, without reading it.
+	 */
+	if (ad_len && ad_len < GHASH_BLOCK_SIZE &&
+	    static_branch_likely(&have_vaes_avx512)) {
+		aes_gcm_ad_short_vaes_avx512(key, &ghash_acc, ad, ad_len);
+	} else {
+		memset(&ghash_acc, 0, sizeof(ghash_acc));
+		if (ad_len)
+			aes_gcm_process_ad_x86(key, &ghash_acc, ad, ad_len);
+	}
 
 	if (likely(data_len))
 		aes_gcm_update_x86_fpu(dst, src, data_len, &ghash_acc, ctr32,
-- 
2.56.1


  parent reply	other threads:[~2026-12-05 19:51 UTC|newest]

Thread overview: 15+ 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 ` [PATCH 03/11] lib/crypto: aes-gcm: Add one-shot AES-GCM arch hooks Eric Biggers
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
2026-12-05 19:51 ` Antonio Quartulli [this message]
2026-12-06  2:27   ` [RFC PATCH] lib/crypto: x86/aes-gcm: Process short associated data in one asm call Eric Biggers
2026-12-06  2:09 ` [PATCH 00/11] Migrate x86_64 AES-GCM code into library 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=20261205195107.181324-1-antonio@mandelbit.com \
    --to=antonio@mandelbit.com \
    --cc=Jason@zx2c4.com \
    --cc=ardb@kernel.org \
    --cc=ebiggers@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