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 09/11] lib/crypto: x86/aes-gcm: Migrate VAES+AVX2 optimized code into library
Date: Sun, 29 Nov 2026 12:02:43 -0800 [thread overview]
Message-ID: <20261129200245.146503-10-ebiggers@kernel.org> (raw)
In-Reply-To: <20261129200245.146503-1-ebiggers@kernel.org>
Migrate aes-gcm-vaes-avx2.S into lib/crypto/, and wire it up to the
AES-GCM library functions. Remove the superseded crypto_aead
implementations from aesni-intel_glue.c.
This makes the AES-GCM library be optimized with VAES+AVX2, while the
same AES-GCM optimizations still remain accessible via crypto_aead via
gcm-aes-lib and rfc4106-gcm-aes-lib. It's also simpler, since it
eliminates the crypto_aead boilerplate.
Note: the new code is not yet enabled on CPUs that support VAES+AVX512,
since the ghash_key format is different in that case. The following
commit will migrate the VAES+AVX512 optimized AES-GCM code.
Some changes to the assembly code were required to match what the
library expects:
- Take 'struct aes_gcm_key' instead of 'struct aes_gcm_key_vaes_avx2'.
- Remove aes_gcm_precompute_vaes_avx2() and
aes_gcm_aad_update_vaes_avx2(). The equivalent functionality is now
handled in libgf128hash.
- aes_gcm_{enc,dec}_update_vaes_avx2():
- Take the counter as big-endian instead of little-endian.
- Widen datalen from int to size_t.
- Reorder the arguments to match what the library hooks expect.
- Load and store the exact tag length in
aes_gcm_{enc,dec}_final_vaes_avx2().
Note: the assembly code is in need of some further cleanups (such as
updating some names and comments, and further optimizations). Those are
left for a later commit, as this one is already quite large.
Benchmark results on AMD Ryzen 9 9950X (Zen 5) with AVX-512VL disabled
(QEMU '-cpu host,-avx512vl') using aes_gcm_kunit (before = uncombined
library AES-CTR + GHASH fallback):
Length Enc before Enc after Dec before Dec after
------ ---------- --------- ---------- ---------
16 278 MB/s 511 MB/s 274 MB/s 670 MB/s
64 1101 MB/s 2046 MB/s 1052 MB/s 2633 MB/s
128 2079 MB/s 3613 MB/s 2088 MB/s 4537 MB/s
256 3776 MB/s 6121 MB/s 3948 MB/s 6989 MB/s
512 5949 MB/s 8547 MB/s 6030 MB/s 9715 MB/s
1024 7847 MB/s 10685 MB/s 8010 MB/s 11668 MB/s
1420 7876 MB/s 10436 MB/s 7440 MB/s 12326 MB/s
4096 10393 MB/s 12954 MB/s 10410 MB/s 14075 MB/s
16384 11251 MB/s 13359 MB/s 11402 MB/s 14488 MB/s
Signed-off-by: Eric Biggers <ebiggers@kernel.org>
---
arch/x86/crypto/Makefile | 1 -
arch/x86/crypto/aesni-intel_glue.c | 176 ++------
lib/crypto/Makefile | 3 +-
.../crypto/x86}/aes-gcm-vaes-avx2.S | 419 ++++--------------
lib/crypto/x86/aes.h | 42 +-
5 files changed, 158 insertions(+), 483 deletions(-)
rename {arch/x86/crypto => lib/crypto/x86}/aes-gcm-vaes-avx2.S (69%)
diff --git a/arch/x86/crypto/Makefile b/arch/x86/crypto/Makefile
index 2749f765c7724..b0e3660980f2f 100644
--- a/arch/x86/crypto/Makefile
+++ b/arch/x86/crypto/Makefile
@@ -41,7 +41,6 @@ aegis128-aesni-y := aegis128-aesni-asm.o aegis128-aesni-glue.o
obj-$(CONFIG_CRYPTO_AES_NI_INTEL) += aesni-intel.o
aesni-intel-y := aesni-intel_glue.o \
- aes-gcm-vaes-avx2.o \
aes-gcm-vaes-avx512.o
obj-$(CONFIG_CRYPTO_SM4_AESNI_AVX_X86_64) += sm4-aesni-avx-x86_64.o
diff --git a/arch/x86/crypto/aesni-intel_glue.c b/arch/x86/crypto/aesni-intel_glue.c
index 51aa7b1566b3d..8d969dfa8f896 100644
--- a/arch/x86/crypto/aesni-intel_glue.c
+++ b/arch/x86/crypto/aesni-intel_glue.c
@@ -46,35 +46,6 @@ struct aes_gcm_key {
u32 rfc4106_nonce;
};
-/* Key struct used by the VAES + AVX2 implementation of AES-GCM */
-struct aes_gcm_key_vaes_avx2 {
- /*
- * Common part of the key. The assembly code prefers 16-byte alignment
- * for this.
- */
- struct aes_gcm_key base __aligned(16);
-
- /*
- * Powers of the hash key H^8 through H^1. These are 128-bit values.
- * They all have an extra factor of x^-1 and are byte-reversed.
- * The assembly code prefers 32-byte alignment for this.
- */
- u64 h_powers[8][2] __aligned(32);
-
- /*
- * Each entry in this array contains the two halves of an entry of
- * h_powers XOR'd together, in the following order:
- * H^8,H^6,H^7,H^5,H^4,H^2,H^3,H^1 i.e. indices 0,2,1,3,4,6,5,7.
- * This is used for Karatsuba multiplication.
- */
- u64 h_powers_xored[8];
-};
-
-#define AES_GCM_KEY_VAES_AVX2(key) \
- container_of((key), struct aes_gcm_key_vaes_avx2, base)
-#define AES_GCM_KEY_VAES_AVX2_SIZE \
- (sizeof(struct aes_gcm_key_vaes_avx2) + (31 & ~(CRYPTO_MINALIGN - 1)))
-
/* Key struct used by the VAES + AVX512 implementation of AES-GCM */
struct aes_gcm_key_vaes_avx512 {
/*
@@ -109,34 +80,21 @@ struct aes_gcm_key_vaes_avx512 {
*/
#define FLAG_RFC4106 BIT(0)
#define FLAG_ENC BIT(1)
-#define FLAG_VAES_AVX2 BIT(3)
#define FLAG_VAES_AVX512 BIT(4)
static inline struct aes_gcm_key *
aes_gcm_key_get(struct crypto_aead *tfm, int flags)
{
- if (flags & FLAG_VAES_AVX512)
- return PTR_ALIGN(crypto_aead_ctx(tfm), 64);
- else
- return PTR_ALIGN(crypto_aead_ctx(tfm), 32);
+ return PTR_ALIGN(crypto_aead_ctx(tfm), 64);
}
-asmlinkage void
-aes_gcm_precompute_vaes_avx2(struct aes_gcm_key_vaes_avx2 *key);
asmlinkage void
aes_gcm_precompute_vaes_avx512(struct aes_gcm_key_vaes_avx512 *key);
static void aes_gcm_precompute(struct aes_gcm_key *key, int flags)
{
- if (flags & FLAG_VAES_AVX512)
- aes_gcm_precompute_vaes_avx512(AES_GCM_KEY_VAES_AVX512(key));
- else
- aes_gcm_precompute_vaes_avx2(AES_GCM_KEY_VAES_AVX2(key));
+ aes_gcm_precompute_vaes_avx512(AES_GCM_KEY_VAES_AVX512(key));
}
-
-asmlinkage void
-aes_gcm_aad_update_vaes_avx2(const struct aes_gcm_key_vaes_avx2 *key,
- u8 ghash_acc[16], const u8 *aad, int aadlen);
asmlinkage void
aes_gcm_aad_update_vaes_avx512(const struct aes_gcm_key_vaes_avx512 *key,
u8 ghash_acc[16], const u8 *aad, int aadlen);
@@ -144,28 +102,15 @@ aes_gcm_aad_update_vaes_avx512(const struct aes_gcm_key_vaes_avx512 *key,
static void aes_gcm_aad_update(const struct aes_gcm_key *key, u8 ghash_acc[16],
const u8 *aad, int aadlen, int flags)
{
- if (flags & FLAG_VAES_AVX512)
- aes_gcm_aad_update_vaes_avx512(AES_GCM_KEY_VAES_AVX512(key),
- ghash_acc, aad, aadlen);
- else
- aes_gcm_aad_update_vaes_avx2(AES_GCM_KEY_VAES_AVX2(key),
- ghash_acc, aad, aadlen);
+ aes_gcm_aad_update_vaes_avx512(AES_GCM_KEY_VAES_AVX512(key), ghash_acc,
+ aad, aadlen);
}
-
-asmlinkage void
-aes_gcm_enc_update_vaes_avx2(const struct aes_gcm_key_vaes_avx2 *key,
- const u32 le_ctr[4], u8 ghash_acc[16],
- const u8 *src, u8 *dst, int datalen);
asmlinkage void
aes_gcm_enc_update_vaes_avx512(const struct aes_gcm_key_vaes_avx512 *key,
const u32 le_ctr[4], u8 ghash_acc[16],
const u8 *src, u8 *dst, int datalen);
asmlinkage void
-aes_gcm_dec_update_vaes_avx2(const struct aes_gcm_key_vaes_avx2 *key,
- const u32 le_ctr[4], u8 ghash_acc[16],
- const u8 *src, u8 *dst, int datalen);
-asmlinkage void
aes_gcm_dec_update_vaes_avx512(const struct aes_gcm_key_vaes_avx512 *key,
const u32 le_ctr[4], u8 ghash_acc[16],
const u8 *src, u8 *dst, int datalen);
@@ -177,30 +122,16 @@ aes_gcm_update(const struct aes_gcm_key *key,
const u8 *src, u8 *dst, int datalen, int flags)
{
if (flags & FLAG_ENC) {
- if (flags & FLAG_VAES_AVX512)
- aes_gcm_enc_update_vaes_avx512(AES_GCM_KEY_VAES_AVX512(key),
- le_ctr, ghash_acc,
- src, dst, datalen);
- else
- aes_gcm_enc_update_vaes_avx2(AES_GCM_KEY_VAES_AVX2(key),
- le_ctr, ghash_acc,
- src, dst, datalen);
+ aes_gcm_enc_update_vaes_avx512(AES_GCM_KEY_VAES_AVX512(key),
+ le_ctr, ghash_acc, src, dst,
+ datalen);
} else {
- if (flags & FLAG_VAES_AVX512)
- aes_gcm_dec_update_vaes_avx512(AES_GCM_KEY_VAES_AVX512(key),
- le_ctr, ghash_acc,
- src, dst, datalen);
- else
- aes_gcm_dec_update_vaes_avx2(AES_GCM_KEY_VAES_AVX2(key),
- le_ctr, ghash_acc,
- src, dst, datalen);
+ aes_gcm_dec_update_vaes_avx512(AES_GCM_KEY_VAES_AVX512(key),
+ le_ctr, ghash_acc, src, dst,
+ datalen);
}
}
-asmlinkage void
-aes_gcm_enc_final_vaes_avx2(const struct aes_gcm_key_vaes_avx2 *key,
- const u32 le_ctr[4], u8 ghash_acc[16],
- u64 total_aadlen, u64 total_datalen);
asmlinkage void
aes_gcm_enc_final_vaes_avx512(const struct aes_gcm_key_vaes_avx512 *key,
const u32 le_ctr[4], u8 ghash_acc[16],
@@ -212,21 +143,9 @@ aes_gcm_enc_final(const struct aes_gcm_key *key,
const u32 le_ctr[4], u8 ghash_acc[16],
u64 total_aadlen, u64 total_datalen, int flags)
{
- if (flags & FLAG_VAES_AVX512)
- aes_gcm_enc_final_vaes_avx512(AES_GCM_KEY_VAES_AVX512(key),
- le_ctr, ghash_acc,
- total_aadlen, total_datalen);
- else
- aes_gcm_enc_final_vaes_avx2(AES_GCM_KEY_VAES_AVX2(key),
- le_ctr, ghash_acc,
- total_aadlen, total_datalen);
+ aes_gcm_enc_final_vaes_avx512(AES_GCM_KEY_VAES_AVX512(key), le_ctr,
+ ghash_acc, total_aadlen, total_datalen);
}
-
-asmlinkage bool __must_check
-aes_gcm_dec_final_vaes_avx2(const struct aes_gcm_key_vaes_avx2 *key,
- const u32 le_ctr[4], const u8 ghash_acc[16],
- u64 total_aadlen, u64 total_datalen,
- const u8 tag[16], int taglen);
asmlinkage bool __must_check
aes_gcm_dec_final_vaes_avx512(const struct aes_gcm_key_vaes_avx512 *key,
const u32 le_ctr[4], const u8 ghash_acc[16],
@@ -239,16 +158,9 @@ aes_gcm_dec_final(const struct aes_gcm_key *key, const u32 le_ctr[4],
u8 ghash_acc[16], u64 total_aadlen, u64 total_datalen,
u8 tag[16], int taglen, int flags)
{
- if (flags & FLAG_VAES_AVX512)
- return aes_gcm_dec_final_vaes_avx512(AES_GCM_KEY_VAES_AVX512(key),
- le_ctr, ghash_acc,
- total_aadlen, total_datalen,
- tag, taglen);
- else
- return aes_gcm_dec_final_vaes_avx2(AES_GCM_KEY_VAES_AVX2(key),
- le_ctr, ghash_acc,
- total_aadlen, total_datalen,
- tag, taglen);
+ return aes_gcm_dec_final_vaes_avx512(AES_GCM_KEY_VAES_AVX512(key),
+ le_ctr, ghash_acc, total_aadlen,
+ total_datalen, tag, taglen);
}
/*
@@ -316,10 +228,6 @@ static int gcm_setkey(struct crypto_aead *tfm, const u8 *raw_key,
}
/* The assembly code assumes the following offsets. */
- static_assert(offsetof(struct aes_gcm_key_vaes_avx2, base.aes_key.len) == 0);
- static_assert(offsetof(struct aes_gcm_key_vaes_avx2, base.aes_key.k.rndkeys) == 16);
- static_assert(offsetof(struct aes_gcm_key_vaes_avx2, h_powers) == 288);
- static_assert(offsetof(struct aes_gcm_key_vaes_avx2, h_powers_xored) == 416);
static_assert(offsetof(struct aes_gcm_key_vaes_avx512, base.aes_key.len) == 0);
static_assert(offsetof(struct aes_gcm_key_vaes_avx512, base.aes_key.k.rndkeys) == 16);
static_assert(offsetof(struct aes_gcm_key_vaes_avx512, h_powers) == 320);
@@ -349,33 +257,15 @@ static int gcm_setkey(struct crypto_aead *tfm, const u8 *raw_key,
gf128mul_lle(&h, (const be128 *)x_to_the_minus1);
/* Compute the needed key powers */
- if (flags & FLAG_VAES_AVX512) {
- struct aes_gcm_key_vaes_avx512 *k =
- AES_GCM_KEY_VAES_AVX512(key);
-
- for (i = ARRAY_SIZE(k->h_powers) - 1; i >= 0; i--) {
- k->h_powers[i][0] = be64_to_cpu(h.b);
- k->h_powers[i][1] = be64_to_cpu(h.a);
- gf128mul_lle(&h, &h1);
- }
- memset(k->padding, 0, sizeof(k->padding));
- } else {
- struct aes_gcm_key_vaes_avx2 *k =
- AES_GCM_KEY_VAES_AVX2(key);
- static const u8 indices[8] = { 0, 2, 1, 3, 4, 6, 5, 7 };
-
- for (i = ARRAY_SIZE(k->h_powers) - 1; i >= 0; i--) {
- k->h_powers[i][0] = be64_to_cpu(h.b);
- k->h_powers[i][1] = be64_to_cpu(h.a);
- gf128mul_lle(&h, &h1);
- }
- for (i = 0; i < ARRAY_SIZE(k->h_powers_xored); i++) {
- int j = indices[i];
-
- k->h_powers_xored[i] = k->h_powers[j][0] ^
- k->h_powers[j][1];
- }
+ struct aes_gcm_key_vaes_avx512 *k =
+ AES_GCM_KEY_VAES_AVX512(key);
+
+ for (i = ARRAY_SIZE(k->h_powers) - 1; i >= 0; i--) {
+ k->h_powers[i][0] = be64_to_cpu(h.b);
+ k->h_powers[i][1] = be64_to_cpu(h.a);
+ gf128mul_lle(&h, &h1);
}
+ memset(k->padding, 0, sizeof(k->padding));
}
return 0;
}
@@ -620,11 +510,6 @@ static struct aead_alg aes_gcm_algs_##suffix[] = { { \
}, \
} }
-/* aes_gcm_algs_vaes_avx2 */
-DEFINE_GCM_ALGS(vaes_avx2, FLAG_VAES_AVX2,
- "generic-gcm-vaes-avx2", "rfc4106-gcm-vaes-avx2",
- AES_GCM_KEY_VAES_AVX2_SIZE, 600);
-
/* aes_gcm_algs_vaes_avx512 */
DEFINE_GCM_ALGS(vaes_avx512, FLAG_VAES_AVX512,
"generic-gcm-vaes-avx512", "rfc4106-gcm-vaes-avx512",
@@ -632,8 +517,6 @@ DEFINE_GCM_ALGS(vaes_avx512, FLAG_VAES_AVX512,
static int __init register_avx_algs(void)
{
- int err;
-
if (!boot_cpu_has(X86_FEATURE_AVX))
return 0;
/*
@@ -647,10 +530,6 @@ static int __init register_avx_algs(void)
!boot_cpu_has(X86_FEATURE_VPCLMULQDQ) ||
!boot_cpu_has(X86_FEATURE_PCLMULQDQ))
return 0;
- err = crypto_register_aeads(aes_gcm_algs_vaes_avx2,
- ARRAY_SIZE(aes_gcm_algs_vaes_avx2));
- if (err)
- return err;
if (!boot_cpu_has(X86_FEATURE_AVX512BW) ||
!boot_cpu_has(X86_FEATURE_AVX512VL) ||
@@ -664,12 +543,8 @@ static int __init register_avx_algs(void)
aes_gcm_algs_vaes_avx512[i].base.cra_priority = 1;
}
- err = crypto_register_aeads(aes_gcm_algs_vaes_avx512,
- ARRAY_SIZE(aes_gcm_algs_vaes_avx512));
- if (err)
- return err;
-
- return 0;
+ return crypto_register_aeads(aes_gcm_algs_vaes_avx512,
+ ARRAY_SIZE(aes_gcm_algs_vaes_avx512));
}
#define unregister_aeads(A) \
@@ -678,7 +553,6 @@ static int __init register_avx_algs(void)
static void unregister_avx_algs(void)
{
- unregister_aeads(aes_gcm_algs_vaes_avx2);
unregister_aeads(aes_gcm_algs_vaes_avx512);
}
diff --git a/lib/crypto/Makefile b/lib/crypto/Makefile
index b813c895cc9da..6e2d264f12492 100644
--- a/lib/crypto/Makefile
+++ b/lib/crypto/Makefile
@@ -96,7 +96,8 @@ ifneq ($(CONFIG_CRYPTO_LIB_AES_CTR),)
libaes-$(CONFIG_X86_64) += x86/aes-ctr-avx-x86_64.o
endif
ifneq ($(CONFIG_CRYPTO_LIB_AES_GCM),)
-libaes-$(CONFIG_X86_64) += x86/aes-gcm-aesni-x86_64.o
+libaes-$(CONFIG_X86_64) += x86/aes-gcm-aesni-x86_64.o \
+ x86/aes-gcm-vaes-avx2.o
endif
ifneq ($(CONFIG_CRYPTO_LIB_AES_XTS),)
libaes-$(CONFIG_X86_64) += x86/aes-xts-avx-x86_64.o
diff --git a/arch/x86/crypto/aes-gcm-vaes-avx2.S b/lib/crypto/x86/aes-gcm-vaes-avx2.S
similarity index 69%
rename from arch/x86/crypto/aes-gcm-vaes-avx2.S
rename to lib/crypto/x86/aes-gcm-vaes-avx2.S
index 9cc387957fa93..2adcb36c08e37 100644
--- a/arch/x86/crypto/aes-gcm-vaes-avx2.S
+++ b/lib/crypto/x86/aes-gcm-vaes-avx2.S
@@ -108,10 +108,6 @@
.Lgfpoly:
.octa 0xc2000000000000000000000000000001
- // Same as above, but with the (1 << 64) bit set.
-.Lgfpoly_and_internal_carrybit:
- .octa 0xc2000000000000010000000000000001
-
// Values needed to prepare the initial vector of counter blocks.
.Lctr_pattern:
.octa 0
@@ -121,13 +117,17 @@
.Linc_2blocks:
.octa 2
-// Offsets in struct aes_gcm_key_vaes_avx2
+.Lbe_one:
+ .long 0x01000000
+
+// Offsets in struct aes_gcm_key
#define OFFSETOF_AESKEYLEN 0
#define OFFSETOF_AESROUNDKEYS 16
-#define OFFSETOF_H_POWERS 288
+#define OFFSETOF_H_POWERS_XORED (256 + 0)
+#define OFFSETOF_H_POWERS (256 + 128)
#define NUM_H_POWERS 8
#define OFFSETOFEND_H_POWERS (OFFSETOF_H_POWERS + (NUM_H_POWERS * 16))
-#define OFFSETOF_H_POWERS_XORED OFFSETOFEND_H_POWERS
+#define OFFSETOF_AUTHTAG_LEN (256 + 320)
.text
@@ -161,14 +161,6 @@
.endif
.endm
-// GHASH-multiply the 128-bit lanes of \a by the 128-bit lanes of \b and store
-// the reduced products in \dst. See _ghash_mul_step for full explanation.
-.macro _ghash_mul a, b, dst, gfpoly, t0, t1, t2
-.irp i, 0,1,2,3,4,5,6,7,8,9
- _ghash_mul_step \i, \a, \b, \dst, \gfpoly, \t0, \t1, \t2
-.endr
-.endm
-
// GHASH-multiply the 128-bit lanes of \a by the 128-bit lanes of \b and add the
// *unreduced* products to \lo, \mi, and \hi.
.macro _ghash_mul_noreduce a, b, lo, mi, hi, t0
@@ -195,122 +187,12 @@
vpxor \t0, \hi, \hi
.endm
-// This is a specialized version of _ghash_mul that computes \a * \a, i.e. it
-// squares \a. It skips computing MI = (a_L * a_H) + (a_H * a_L) = 0.
-.macro _ghash_square a, dst, gfpoly, t0, t1
- vpclmulqdq $0x00, \a, \a, \t0 // LO = a_L * a_L
- vpclmulqdq $0x11, \a, \a, \dst // HI = a_H * a_H
- vpclmulqdq $0x01, \t0, \gfpoly, \t1 // LO_L*(x^63 + x^62 + x^57)
- vpshufd $0x4e, \t0, \t0 // Swap halves of LO
- vpxor \t0, \t1, \t1 // Fold LO into MI
- vpclmulqdq $0x01, \t1, \gfpoly, \t0 // MI_L*(x^63 + x^62 + x^57)
- vpshufd $0x4e, \t1, \t1 // Swap halves of MI
- vpxor \t1, \dst, \dst // Fold MI into HI (part 1)
- vpxor \t0, \dst, \dst // Fold MI into HI (part 2)
-.endm
-
-// void aes_gcm_precompute_vaes_avx2(struct aes_gcm_key_vaes_avx2 *key);
-//
-// Given the expanded AES key |key->base.aes_key|, derive the GHASH subkey and
-// initialize |key->h_powers| and |key->h_powers_xored|.
-//
-// We use h_powers[0..7] to store H^8 through H^1, and h_powers_xored[0..7] to
-// store the 64-bit halves of the key powers XOR'd together (for Karatsuba
-// multiplication) in the order 8,6,7,5,4,2,3,1.
-SYM_FUNC_START(aes_gcm_precompute_vaes_avx2)
-
- // Function arguments
- .set KEY, %rdi
-
- // Additional local variables
- .set POWERS_PTR, %rsi
- .set RNDKEYLAST_PTR, %rdx
- .set TMP0, %ymm0
- .set TMP0_XMM, %xmm0
- .set TMP1, %ymm1
- .set TMP1_XMM, %xmm1
- .set TMP2, %ymm2
- .set TMP2_XMM, %xmm2
- .set H_CUR, %ymm3
- .set H_CUR_XMM, %xmm3
- .set H_CUR2, %ymm4
- .set H_INC, %ymm5
- .set H_INC_XMM, %xmm5
- .set GFPOLY, %ymm6
- .set GFPOLY_XMM, %xmm6
-
- // Encrypt an all-zeroes block to get the raw hash subkey.
- movl OFFSETOF_AESKEYLEN(KEY), %eax
- lea OFFSETOF_AESROUNDKEYS+6*16(KEY,%rax,4), RNDKEYLAST_PTR
- vmovdqu OFFSETOF_AESROUNDKEYS(KEY), H_CUR_XMM
- lea OFFSETOF_AESROUNDKEYS+16(KEY), %rax
-1:
- vaesenc (%rax), H_CUR_XMM, H_CUR_XMM
- add $16, %rax
- cmp %rax, RNDKEYLAST_PTR
- jne 1b
- vaesenclast (RNDKEYLAST_PTR), H_CUR_XMM, H_CUR_XMM
-
- // Reflect the bytes of the raw hash subkey.
- vpshufb .Lbswap_mask(%rip), H_CUR_XMM, H_CUR_XMM
-
- // Finish preprocessing the byte-reflected hash subkey by multiplying it
- // by x^-1 ("standard" interpretation of polynomial coefficients) or
- // equivalently x^1 (natural interpretation). This gets the key into a
- // format that avoids having to bit-reflect the data blocks later.
- vpshufd $0xd3, H_CUR_XMM, TMP0_XMM
- vpsrad $31, TMP0_XMM, TMP0_XMM
- vpaddq H_CUR_XMM, H_CUR_XMM, H_CUR_XMM
- vpand .Lgfpoly_and_internal_carrybit(%rip), TMP0_XMM, TMP0_XMM
- vpxor TMP0_XMM, H_CUR_XMM, H_CUR_XMM
-
- // Load the gfpoly constant.
- vbroadcasti128 .Lgfpoly(%rip), GFPOLY
-
- // Square H^1 to get H^2.
- _ghash_square H_CUR_XMM, H_INC_XMM, GFPOLY_XMM, TMP0_XMM, TMP1_XMM
-
- // Create H_CUR = [H^2, H^1] and H_INC = [H^2, H^2].
- vinserti128 $1, H_CUR_XMM, H_INC, H_CUR
- vinserti128 $1, H_INC_XMM, H_INC, H_INC
-
- // Compute H_CUR2 = [H^4, H^3].
- _ghash_mul H_INC, H_CUR, H_CUR2, GFPOLY, TMP0, TMP1, TMP2
-
- // Store [H^2, H^1] and [H^4, H^3].
- vmovdqu H_CUR, OFFSETOF_H_POWERS+3*32(KEY)
- vmovdqu H_CUR2, OFFSETOF_H_POWERS+2*32(KEY)
-
- // For Karatsuba multiplication: compute and store the two 64-bit halves
- // of each key power XOR'd together. Order is 4,2,3,1.
- vpunpcklqdq H_CUR, H_CUR2, TMP0
- vpunpckhqdq H_CUR, H_CUR2, TMP1
- vpxor TMP1, TMP0, TMP0
- vmovdqu TMP0, OFFSETOF_H_POWERS_XORED+32(KEY)
-
- // Compute and store H_CUR = [H^6, H^5] and H_CUR2 = [H^8, H^7].
- _ghash_mul H_INC, H_CUR2, H_CUR, GFPOLY, TMP0, TMP1, TMP2
- _ghash_mul H_INC, H_CUR, H_CUR2, GFPOLY, TMP0, TMP1, TMP2
- vmovdqu H_CUR, OFFSETOF_H_POWERS+1*32(KEY)
- vmovdqu H_CUR2, OFFSETOF_H_POWERS+0*32(KEY)
-
- // Again, compute and store the two 64-bit halves of each key power
- // XOR'd together. Order is 8,6,7,5.
- vpunpcklqdq H_CUR, H_CUR2, TMP0
- vpunpckhqdq H_CUR, H_CUR2, TMP1
- vpxor TMP1, TMP0, TMP0
- vmovdqu TMP0, OFFSETOF_H_POWERS_XORED(KEY)
-
- vzeroupper
- RET
-SYM_FUNC_END(aes_gcm_precompute_vaes_avx2)
-
// Do one step of the GHASH update of four vectors of data blocks.
// \i: the step to do, 0 through 9
-// \ghashdata_ptr: pointer to the data blocks (ciphertext or AAD)
-// KEY: pointer to struct aes_gcm_key_vaes_avx2
+// \ghashdata_ptr: pointer to the data blocks (ciphertext)
+// KEY: pointer to struct aes_gcm_key
// BSWAP_MASK: mask for reflecting the bytes of blocks
-// H_POW[2-1]_XORED: cached values from KEY->h_powers_xored
+// H_POW[2-1]_XORED: cached values from KEY->ghash.k.h_powers_xored
// TMP[0-2]: temporary registers. TMP[1-2] must be preserved across steps.
// LO, MI: working state for this macro that must be preserved across steps
// GHASH_ACC: the GHASH accumulator (input/output)
@@ -480,136 +362,6 @@ SYM_FUNC_END(aes_gcm_precompute_vaes_avx2)
.Ldone\@:
.endm
-// void aes_gcm_aad_update_vaes_avx2(const struct aes_gcm_key_vaes_avx2 *key,
-// u8 ghash_acc[16],
-// const u8 *aad, int aadlen);
-//
-// This function processes the AAD (Additional Authenticated Data) in GCM.
-// Using the key |key|, it updates the GHASH accumulator |ghash_acc| with the
-// data given by |aad| and |aadlen|. On the first call, |ghash_acc| must be all
-// zeroes. |aadlen| must be a multiple of 16, except on the last call where it
-// can be any length. The caller must do any buffering needed to ensure this.
-//
-// This handles large amounts of AAD efficiently, while also keeping overhead
-// low for small amounts which is the common case. TLS and IPsec use less than
-// one block of AAD, but (uncommonly) other use cases may use much more.
-SYM_FUNC_START(aes_gcm_aad_update_vaes_avx2)
-
- // Function arguments
- .set KEY, %rdi
- .set GHASH_ACC_PTR, %rsi
- .set AAD, %rdx
- .set AADLEN, %ecx // Must be %ecx for _load_partial_block
- .set AADLEN64, %rcx // Zero-extend AADLEN before using!
-
- // Additional local variables.
- // %rax and %r8 are used as temporary registers.
- .set TMP0, %ymm0
- .set TMP0_XMM, %xmm0
- .set TMP1, %ymm1
- .set TMP1_XMM, %xmm1
- .set TMP2, %ymm2
- .set TMP2_XMM, %xmm2
- .set LO, %ymm3
- .set LO_XMM, %xmm3
- .set MI, %ymm4
- .set MI_XMM, %xmm4
- .set GHASH_ACC, %ymm5
- .set GHASH_ACC_XMM, %xmm5
- .set BSWAP_MASK, %ymm6
- .set BSWAP_MASK_XMM, %xmm6
- .set GFPOLY, %ymm7
- .set GFPOLY_XMM, %xmm7
- .set H_POW2_XORED, %ymm8
- .set H_POW1_XORED, %ymm9
-
- // Load the bswap_mask and gfpoly constants. Since AADLEN is usually
- // small, usually only 128-bit vectors will be used. So as an
- // optimization, don't broadcast these constants to both 128-bit lanes
- // quite yet.
- vmovdqu .Lbswap_mask(%rip), BSWAP_MASK_XMM
- vmovdqu .Lgfpoly(%rip), GFPOLY_XMM
-
- // Load the GHASH accumulator.
- vmovdqu (GHASH_ACC_PTR), GHASH_ACC_XMM
-
- // Check for the common case of AADLEN <= 16, as well as AADLEN == 0.
- test AADLEN, AADLEN
- jz .Laad_done
- cmp $16, AADLEN
- jle .Laad_lastblock
-
- // AADLEN > 16, so we'll operate on full vectors. Broadcast bswap_mask
- // and gfpoly to both 128-bit lanes.
- vinserti128 $1, BSWAP_MASK_XMM, BSWAP_MASK, BSWAP_MASK
- vinserti128 $1, GFPOLY_XMM, GFPOLY, GFPOLY
-
- // If AADLEN >= 128, update GHASH with 128 bytes of AAD at a time.
- add $-128, AADLEN // 128 is 4 bytes, -128 is 1 byte
- jl .Laad_loop_4x_done
- vmovdqu OFFSETOF_H_POWERS_XORED(KEY), H_POW2_XORED
- vmovdqu OFFSETOF_H_POWERS_XORED+32(KEY), H_POW1_XORED
-.Laad_loop_4x:
- _ghash_4x AAD
- sub $-128, AAD
- add $-128, AADLEN
- jge .Laad_loop_4x
-.Laad_loop_4x_done:
-
- // If AADLEN >= 32, update GHASH with 32 bytes of AAD at a time.
- add $96, AADLEN
- jl .Laad_loop_1x_done
-.Laad_loop_1x:
- vmovdqu (AAD), TMP0
- vpshufb BSWAP_MASK, TMP0, TMP0
- vpxor TMP0, GHASH_ACC, GHASH_ACC
- vmovdqu OFFSETOFEND_H_POWERS-32(KEY), TMP0
- _ghash_mul TMP0, GHASH_ACC, GHASH_ACC, GFPOLY, TMP1, TMP2, LO
- vextracti128 $1, GHASH_ACC, TMP0_XMM
- vpxor TMP0_XMM, GHASH_ACC_XMM, GHASH_ACC_XMM
- add $32, AAD
- sub $32, AADLEN
- jge .Laad_loop_1x
-.Laad_loop_1x_done:
- add $32, AADLEN
- // Now 0 <= AADLEN < 32.
-
- jz .Laad_done
- cmp $16, AADLEN
- jle .Laad_lastblock
-
- // Update GHASH with the remaining 17 <= AADLEN <= 31 bytes of AAD.
- mov AADLEN, AADLEN // Zero-extend AADLEN to AADLEN64.
- vmovdqu (AAD), TMP0_XMM
- vmovdqu -16(AAD, AADLEN64), TMP1_XMM
- vpshufb BSWAP_MASK_XMM, TMP0_XMM, TMP0_XMM
- vpxor TMP0_XMM, GHASH_ACC_XMM, GHASH_ACC_XMM
- lea .Lrshift_and_bswap_table(%rip), %rax
- vpshufb -16(%rax, AADLEN64), TMP1_XMM, TMP1_XMM
- vinserti128 $1, TMP1_XMM, GHASH_ACC, GHASH_ACC
- vmovdqu OFFSETOFEND_H_POWERS-32(KEY), TMP0
- _ghash_mul TMP0, GHASH_ACC, GHASH_ACC, GFPOLY, TMP1, TMP2, LO
- vextracti128 $1, GHASH_ACC, TMP0_XMM
- vpxor TMP0_XMM, GHASH_ACC_XMM, GHASH_ACC_XMM
- jmp .Laad_done
-
-.Laad_lastblock:
- // Update GHASH with the remaining 1 <= AADLEN <= 16 bytes of AAD.
- _load_partial_block AAD, TMP0_XMM, %r8, %r8d
- vpshufb BSWAP_MASK_XMM, TMP0_XMM, TMP0_XMM
- vpxor TMP0_XMM, GHASH_ACC_XMM, GHASH_ACC_XMM
- vmovdqu OFFSETOFEND_H_POWERS-16(KEY), TMP0_XMM
- _ghash_mul TMP0_XMM, GHASH_ACC_XMM, GHASH_ACC_XMM, GFPOLY_XMM, \
- TMP1_XMM, TMP2_XMM, LO_XMM
-
-.Laad_done:
- // Store the updated GHASH accumulator back to memory.
- vmovdqu GHASH_ACC_XMM, (GHASH_ACC_PTR)
-
- vzeroupper
- RET
-SYM_FUNC_END(aes_gcm_aad_update_vaes_avx2)
-
// Do one non-last round of AES encryption on the blocks in the given AESDATA
// vectors using the round key that has been broadcast to all 128-bit lanes of
// \round_key.
@@ -659,9 +411,11 @@ SYM_FUNC_END(aes_gcm_aad_update_vaes_avx2)
.endr
.endm
-// void aes_gcm_{enc,dec}_update_vaes_avx2(const struct aes_gcm_key_vaes_avx2 *key,
-// const u32 le_ctr[4], u8 ghash_acc[16],
-// const u8 *src, u8 *dst, int datalen);
+// void aes_gcm_{enc,dec}_update_vaes_avx2(u8 *dst, const u8 *src,
+// size_t datalen,
+// struct polyval_elem *ghash_acc,
+// const __be32 ctr32[4],
+// const struct aes_gcm_key *key);
//
// This macro generates a GCM encryption or decryption update function with the
// above prototype (with \enc selecting which one). The function computes the
@@ -674,28 +428,25 @@ SYM_FUNC_END(aes_gcm_aad_update_vaes_avx2)
// any length. The caller must do any buffering needed to ensure this. Both
// in-place and out-of-place en/decryption are supported.
//
-// |le_ctr| must give the current counter in little-endian format. This
-// function loads the counter from |le_ctr| and increments the loaded counter as
-// needed, but it does *not* store the updated counter back to |le_ctr|. The
-// caller must update |le_ctr| if any more data segments follow. Internally,
-// only the low 32-bit word of the counter is incremented, following the GCM
-// standard.
+// |ctr32| must give the current counter in big-endian format. This function
+// loads the counter from |ctr32| and increments the loaded counter as needed,
+// but it does *not* store the updated counter back to |ctr32|. The caller must
+// update |ctr32| if any more data segments follow. Internally, only the low
+// 32-bit word of the counter is incremented, following the GCM standard.
.macro _aes_gcm_update enc
// Function arguments
- .set KEY, %rdi
- .set LE_CTR_PTR, %rsi
- .set LE_CTR_PTR32, %esi
- .set GHASH_ACC_PTR, %rdx
- .set SRC, %rcx // Assumed to be %rcx.
+ .set DST, %rdi
+ .set SRC, %rsi
+ .set DATALEN, %rdx
+ .set DATALEN32, %edx
+ .set GHASH_ACC_PTR, %rcx // Assumed to be %rcx.
// See .Ltail_xor_and_ghash_1to16bytes
- .set DST, %r8
- .set DATALEN, %r9d
- .set DATALEN64, %r9 // Zero-extend DATALEN before using!
-
- // Additional local variables
+ .set BE_CTR_PTR, %r8
+ .set BE_CTR_PTR32, %r8d
+ .set KEY, %r9
- // %rax is used as a temporary register. LE_CTR_PTR is also available
+ // %rax is used as a temporary register. BE_CTR_PTR is also available
// as a temporary register after the counter is loaded.
// AES key length in bytes
@@ -730,8 +481,8 @@ SYM_FUNC_END(aes_gcm_aad_update_vaes_avx2)
.set MI, %ymm6
.set MI_XMM, %xmm6
- // H_POW[2-1]_XORED contain cached values from KEY->h_powers_xored. The
- // descending numbering reflects the order of the key powers.
+ // H_POW[2-1]_XORED contain cached values from KEY->ghash.k.h_powers_xored.
+ // The descending numbering reflects the order of the key powers.
.set H_POW2_XORED, %ymm7
.set H_POW2_XORED_XMM, %xmm7
.set H_POW1_XORED, %ymm8
@@ -761,7 +512,8 @@ SYM_FUNC_END(aes_gcm_aad_update_vaes_avx2)
// Load the GHASH accumulator and the starting counter.
vmovdqu (GHASH_ACC_PTR), GHASH_ACC_XMM
- vbroadcasti128 (LE_CTR_PTR), LE_CTR
+ vbroadcasti128 (BE_CTR_PTR), LE_CTR
+ vpshufb BSWAP_MASK, LE_CTR, LE_CTR
// Load the AES key length in bytes.
movl OFFSETOF_AESKEYLEN(KEY), AESKEYLEN
@@ -857,9 +609,9 @@ SYM_FUNC_END(aes_gcm_aad_update_vaes_avx2)
// Make POWERS_PTR point to the key powers [H^N, H^(N-1), ...] where N
// is the number of blocks that remain.
- .set POWERS_PTR, LE_CTR_PTR // LE_CTR_PTR is free to be reused.
- .set POWERS_PTR32, LE_CTR_PTR32
- mov DATALEN, %eax
+ .set POWERS_PTR, BE_CTR_PTR // BE_CTR_PTR is free to be reused.
+ .set POWERS_PTR32, BE_CTR_PTR32
+ mov DATALEN32, %eax
neg %rax
and $~15, %rax // -round_up(DATALEN, 16)
lea OFFSETOFEND_H_POWERS(KEY,%rax), POWERS_PTR
@@ -924,7 +676,7 @@ SYM_FUNC_END(aes_gcm_aad_update_vaes_avx2)
// Load a vpshufb mask that will right-shift by '32 - DATALEN' bytes
// (shifting in zeroes), then reflect all 16 bytes.
lea .Lrshift_and_bswap_table(%rip), %rax
- vmovdqu -16(%rax, DATALEN64), TMP2_XMM
+ vmovdqu -16(%rax, DATALEN), TMP2_XMM
// Move the second keystream block to its own register and left-align it
vextracti128 $1, AESDATA0, AESDATA1_XMM
@@ -936,18 +688,18 @@ SYM_FUNC_END(aes_gcm_aad_update_vaes_avx2)
// input data: the full ciphertext block and the zero-padded partial
// ciphertext block, both byte-reflected, in AESDATA0.
.if \enc
- vpxor -16(SRC, DATALEN64), AESDATA1_XMM, AESDATA1_XMM
+ vpxor -16(SRC, DATALEN), AESDATA1_XMM, AESDATA1_XMM
vpxor (SRC), AESDATA0_XMM, AESDATA0_XMM
- vmovdqu AESDATA1_XMM, -16(DST, DATALEN64)
+ vmovdqu AESDATA1_XMM, -16(DST, DATALEN)
vmovdqu AESDATA0_XMM, (DST)
vpshufb TMP2_XMM, AESDATA1_XMM, AESDATA1_XMM
vpshufb BSWAP_MASK_XMM, AESDATA0_XMM, AESDATA0_XMM
.else
- vmovdqu -16(SRC, DATALEN64), TMP1_XMM
+ vmovdqu -16(SRC, DATALEN), TMP1_XMM
vmovdqu (SRC), TMP0_XMM
vpxor TMP1_XMM, AESDATA1_XMM, AESDATA1_XMM
vpxor TMP0_XMM, AESDATA0_XMM, AESDATA0_XMM
- vmovdqu AESDATA1_XMM, -16(DST, DATALEN64)
+ vmovdqu AESDATA1_XMM, -16(DST, DATALEN)
vmovdqu AESDATA0_XMM, (DST)
vpshufb TMP2_XMM, TMP1_XMM, AESDATA1_XMM
vpshufb BSWAP_MASK_XMM, TMP0_XMM, AESDATA0_XMM
@@ -961,16 +713,19 @@ SYM_FUNC_END(aes_gcm_aad_update_vaes_avx2)
// Handle 1 <= DATALEN <= 16. Carefully load and store the
// possibly-partial block, which we mustn't access out of bounds.
vmovdqu (POWERS_PTR), TMP2_XMM
- mov SRC, KEY // Free up %rcx, assuming SRC == %rcx
- mov DATALEN, %ecx
- _load_partial_block KEY, TMP0_XMM, POWERS_PTR, POWERS_PTR32
+ // Free up %rcx (assuming GHASH_ACC_PTR == %rcx) by saving it in KEY,
+ // which is dead after this point; it is restored below.
+ mov GHASH_ACC_PTR, KEY
+ mov DATALEN32, %ecx
+ _load_partial_block SRC, TMP0_XMM, POWERS_PTR, POWERS_PTR32
vpxor TMP0_XMM, AESDATA0_XMM, AESDATA0_XMM
- mov DATALEN, %ecx
+ mov DATALEN32, %ecx
_store_partial_block AESDATA0_XMM, DST, POWERS_PTR, POWERS_PTR32
+ mov KEY, GHASH_ACC_PTR
.if \enc
lea .Lselect_high_bytes_table(%rip), %rax
vpshufb BSWAP_MASK_XMM, AESDATA0_XMM, AESDATA0_XMM
- vpand (%rax, DATALEN64), AESDATA0_XMM, AESDATA0_XMM
+ vpand (%rax, DATALEN), AESDATA0_XMM, AESDATA0_XMM
.else
vpshufb BSWAP_MASK_XMM, TMP0_XMM, AESDATA0_XMM
.endif
@@ -997,13 +752,16 @@ SYM_FUNC_END(aes_gcm_aad_update_vaes_avx2)
RET
.endm
-// void aes_gcm_enc_final_vaes_avx2(const struct aes_gcm_key_vaes_avx2 *key,
-// const u32 le_ctr[4], u8 ghash_acc[16],
-// u64 total_aadlen, u64 total_datalen);
-// bool aes_gcm_dec_final_vaes_avx2(const struct aes_gcm_key_vaes_avx2 *key,
-// const u32 le_ctr[4], const u8 ghash_acc[16],
+// void aes_gcm_enc_final_vaes_avx2(const struct aes_gcm_key *key,
+// const u8 nonce[12],
+// const struct polyval_elem *ghash_acc,
// u64 total_aadlen, u64 total_datalen,
-// const u8 tag[16], int taglen);
+// u8 *authtag);
+// bool aes_gcm_dec_final_vaes_avx2(const struct aes_gcm_key *key,
+// const u8 nonce[12],
+// const struct polyval_elem *ghash_acc,
+// u64 total_aadlen, u64 total_datalen,
+// const u8 *authtag);
//
// This macro generates one of the above two functions (with \enc selecting
// which one). Both functions finish computing the GCM authentication tag by
@@ -1011,44 +769,40 @@ SYM_FUNC_END(aes_gcm_aad_update_vaes_avx2)
// |total_aadlen| and |total_datalen| must be the total length of the additional
// authenticated data and the en/decrypted data in bytes, respectively.
//
-// The encryption function then stores the full-length (16-byte) computed
-// authentication tag to |ghash_acc|. The decryption function instead loads the
-// expected authentication tag (the one that was transmitted) from the 16-byte
-// buffer |tag|, compares the first 4 <= |taglen| <= 16 bytes of it to the
+// The encryption function then stores the computed authentication tag to
+// |authtag|. The decryption function instead loads the expected authentication
+// tag (the one that was transmitted) from |authtag|, compares it to the
// computed tag in constant time, and returns true if and only if they match.
+// In both cases the length of the tag is |key->authtag_len| bytes.
.macro _aes_gcm_final enc
// Function arguments
.set KEY, %rdi
- .set LE_CTR_PTR, %rsi
+ .set NONCE_PTR, %rsi
.set GHASH_ACC_PTR, %rdx
.set TOTAL_AADLEN, %rcx
.set TOTAL_DATALEN, %r8
.set TAG, %r9
- .set TAGLEN, %r10d // Originally at 8(%rsp)
- .set TAGLEN64, %r10
// Additional local variables.
// %rax and %xmm0-%xmm3 are used as temporary registers.
+ .set TAGLEN, %r10d
+ .set TAGLEN64, %r10
.set AESKEYLEN, %r11d
.set AESKEYLEN64, %r11
.set GFPOLY, %xmm4
.set BSWAP_MASK, %xmm5
- .set LE_CTR, %xmm6
- .set GHASH_ACC, %xmm7
- .set H_POW1, %xmm8
+ .set GHASH_ACC, %xmm6
+ .set H_POW1, %xmm7
// Load some constants.
vmovdqa .Lgfpoly(%rip), GFPOLY
vmovdqa .Lbswap_mask(%rip), BSWAP_MASK
- // Load the AES key length in bytes.
+ // Load the AES key length and auth tag length in bytes.
+ // authtag_len is size_t with value in 4..16, so movl suffices here.
movl OFFSETOF_AESKEYLEN(KEY), AESKEYLEN
-
- // Set up a counter block with 1 in the low 32-bit word. This is the
- // counter that produces the ciphertext needed to encrypt the auth tag.
- // GFPOLY has 1 in the low word, so grab the 1 from there using a blend.
- vpblendd $0xe, (LE_CTR_PTR), GFPOLY, LE_CTR
+ movl OFFSETOF_AUTHTAG_LEN(KEY), TAGLEN
// Build the lengths block and XOR it with the GHASH accumulator.
// Although the lengths block is defined as the AAD length followed by
@@ -1064,17 +818,14 @@ SYM_FUNC_END(aes_gcm_aad_update_vaes_avx2)
// Load the first hash key power (H^1), which is stored last.
vmovdqu OFFSETOFEND_H_POWERS-16(KEY), H_POW1
- // Load TAGLEN if decrypting.
-.if !\enc
- movl 8(%rsp), TAGLEN
-.endif
-
// Make %rax point to the last AES round key for the chosen AES variant.
lea OFFSETOF_AESROUNDKEYS+6*16(KEY,AESKEYLEN64,4), %rax
- // Start the AES encryption of the counter block by swapping the counter
- // block to big-endian and XOR-ing it with the zero-th AES round key.
- vpshufb BSWAP_MASK, LE_CTR, %xmm0
+ // Build J_0 = nonce || be32(1) and XOR it with the zero-th AES round
+ // key to start the AES encryption of the counter block.
+ vmovq (NONCE_PTR), %xmm0
+ vpinsrd $2, 8(NONCE_PTR), %xmm0, %xmm0
+ vpinsrd $3, .Lbe_one(%rip), %xmm0, %xmm0
vpxor OFFSETOF_AESROUNDKEYS(KEY), %xmm0, %xmm0
// Complete the AES encryption and multiply GHASH_ACC by H^1.
@@ -1116,11 +867,29 @@ SYM_FUNC_END(aes_gcm_aad_update_vaes_avx2)
.if \enc
vpxor (%rax), GHASH_ACC, %xmm1
vaesenclast %xmm1, %xmm0, GHASH_ACC
- vmovdqu GHASH_ACC, (GHASH_ACC_PTR)
+ cmp $16, TAGLEN
+ jne .Lstore_short_tag\@
+ vmovdqu GHASH_ACC, (TAG)
+ RET
+.Lstore_short_tag\@:
+ mov TAGLEN, %ecx
+ _store_partial_block GHASH_ACC, TAG, %r8, %r8d
.else
+ cmp $16, TAGLEN
+ jne .Lverify_short_tag\@
vpxor (TAG), GHASH_ACC, GHASH_ACC
vpxor (%rax), GHASH_ACC, GHASH_ACC
vaesenclast GHASH_ACC, %xmm0, %xmm0
+ xor %eax, %eax
+ vptest %xmm0, %xmm0
+ sete %al
+ RET
+.Lverify_short_tag\@:
+ vpxor (%rax), GHASH_ACC, GHASH_ACC
+ mov TAGLEN, %ecx
+ _load_partial_block TAG, %xmm1, %r8, %r8d
+ vpxor %xmm1, GHASH_ACC, GHASH_ACC
+ vaesenclast GHASH_ACC, %xmm0, %xmm0
lea .Lselect_high_bytes_table(%rip), %rax
vmovdqu (%rax, TAGLEN64), %xmm1
vpshufb BSWAP_MASK, %xmm1, %xmm1 // select low bytes, not high
diff --git a/lib/crypto/x86/aes.h b/lib/crypto/x86/aes.h
index 72222c2895a4f..97035b783ec4a 100644
--- a/lib/crypto/x86/aes.h
+++ b/lib/crypto/x86/aes.h
@@ -440,6 +440,14 @@ void aes_gcm_dec_update_aesni_avx(u8 *dst, const u8 *src, size_t len,
struct polyval_elem *ghash_acc,
const __be32 ctr32[4],
const struct aes_gcm_key *key);
+void aes_gcm_enc_update_vaes_avx2(u8 *dst, const u8 *src, size_t len,
+ struct polyval_elem *ghash_acc,
+ const __be32 ctr32[4],
+ const struct aes_gcm_key *key);
+void aes_gcm_dec_update_vaes_avx2(u8 *dst, const u8 *src, size_t len,
+ struct polyval_elem *ghash_acc,
+ const __be32 ctr32[4],
+ const struct aes_gcm_key *key);
void aes_gcm_enc_final_aesni(const struct aes_gcm_key *key, const u8 nonce[12],
const struct polyval_elem *ghash_acc,
@@ -458,6 +466,16 @@ bool aes_gcm_dec_final_aesni_avx(const struct aes_gcm_key *key,
const struct polyval_elem *ghash_acc,
u64 total_aadlen, u64 total_datalen,
const u8 *authtag);
+void aes_gcm_enc_final_vaes_avx2(const struct aes_gcm_key *key,
+ const u8 nonce[12],
+ const struct polyval_elem *ghash_acc,
+ u64 total_aadlen, u64 total_datalen,
+ u8 *authtag);
+bool aes_gcm_dec_final_vaes_avx2(const struct aes_gcm_key *key,
+ const u8 nonce[12],
+ const struct polyval_elem *ghash_acc,
+ u64 total_aadlen, u64 total_datalen,
+ const u8 *authtag);
/*
* En/decrypt and GHASH @len bytes inside an active FPU section. @len must be
@@ -474,7 +492,14 @@ aes_gcm_update_x86_fpu(u8 *dst, const u8 *src, size_t len,
static_assert(GCM_CHUNK_SIZE % AES_BLOCK_SIZE == 0);
- if (static_branch_likely(&have_aesni_avx)) {
+ if (static_branch_likely(&have_vaes_avx2)) {
+ if (enc)
+ aes_gcm_enc_update_vaes_avx2(
+ dst, src, n, ghash_acc, ctr32, key);
+ else
+ aes_gcm_dec_update_vaes_avx2(
+ dst, src, n, ghash_acc, ctr32, key);
+ } else if (static_branch_likely(&have_aesni_avx)) {
if (enc)
aes_gcm_enc_update_aesni_avx(
dst, src, n, ghash_acc, ctr32, key);
@@ -509,7 +534,7 @@ aes_gcm_crypt_update_x86(u8 *dst, const u8 *src, size_t len,
__be32 ctr[4];
if (!static_branch_likely(&have_aesni_pclmul) ||
- static_branch_likely(&have_vaes_avx2) ||
+ static_branch_likely(&have_vaes_avx512) ||
unlikely(!irq_fpu_usable()))
return false;
@@ -574,7 +599,7 @@ aes_gcm_crypt_x86(u8 *dst, const u8 *src, size_t data_len, u8 *authtag,
__be32 ctr32[4];
if (!static_branch_likely(&have_aesni_pclmul) ||
- static_branch_likely(&have_vaes_avx2) ||
+ static_branch_likely(&have_vaes_avx512) ||
unlikely(!irq_fpu_usable()))
return false;
@@ -591,7 +616,10 @@ aes_gcm_crypt_x86(u8 *dst, const u8 *src, size_t data_len, u8 *authtag,
key, enc);
if (enc) {
- if (static_branch_likely(&have_aesni_avx))
+ if (static_branch_likely(&have_vaes_avx2))
+ aes_gcm_enc_final_vaes_avx2(key, nonce, &ghash_acc,
+ ad_len, data_len, authtag);
+ else if (static_branch_likely(&have_aesni_avx))
aes_gcm_enc_final_aesni_avx(key, nonce, &ghash_acc,
ad_len, data_len, authtag);
else
@@ -600,7 +628,11 @@ aes_gcm_crypt_x86(u8 *dst, const u8 *src, size_t data_len, u8 *authtag,
} else {
bool valid;
- if (static_branch_likely(&have_aesni_avx))
+ if (static_branch_likely(&have_vaes_avx2))
+ valid = aes_gcm_dec_final_vaes_avx2(key, nonce,
+ &ghash_acc, ad_len,
+ data_len, authtag);
+ else if (static_branch_likely(&have_aesni_avx))
valid = aes_gcm_dec_final_aesni_avx(key, nonce,
&ghash_acc, ad_len,
data_len, authtag);
--
2.56.1
next prev 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 ` [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 ` Eric Biggers [this message]
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-10-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