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 08/11] lib/crypto: x86/aes-gcm: Migrate AES-NI optimized code into library
Date: Sun, 29 Nov 2026 12:02:42 -0800 [thread overview]
Message-ID: <20261129200245.146503-9-ebiggers@kernel.org> (raw)
In-Reply-To: <20261129200245.146503-1-ebiggers@kernel.org>
Migrate aes-gcm-aesni-x86_64.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 AES-NI and AES-NI+AVX,
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, since
the ghash_key format is different in that case. The following commits
will migrate the VAES+AVX2 and 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_aesni'.
- Don't use 16-byte SSE memory operands from the key, since
'struct aes_gcm_key' isn't necessarily 16-byte aligned.
- Remove aes_gcm_precompute_aesni*() and aes_gcm_aad_update_aesni*().
The equivalent functionality is now handled in libgf128hash.
- aes_gcm_{enc,dec}_update_*():
- 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_*().
Note: the assembly code is in need of some further cleanups (such as not
mixing VEX-coded and legacy SSE instructions, 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 VAES disabled (QEMU
'-cpu host,-vaes') using aes_gcm_kunit (before = uncombined library
AES-CTR + GHASH fallback):
Length Enc before Enc after Dec before Dec after
------ ---------- --------- ---------- ---------
16 291 MB/s 550 MB/s 281 MB/s 710 MB/s
64 1089 MB/s 1791 MB/s 1092 MB/s 2201 MB/s
128 2058 MB/s 3336 MB/s 2059 MB/s 3720 MB/s
256 3302 MB/s 4884 MB/s 3176 MB/s 5365 MB/s
512 4376 MB/s 6397 MB/s 4291 MB/s 6801 MB/s
1024 5171 MB/s 7526 MB/s 5251 MB/s 7761 MB/s
1420 5157 MB/s 7538 MB/s 5092 MB/s 8028 MB/s
4096 5989 MB/s 8532 MB/s 6126 MB/s 8985 MB/s
16384 6382 MB/s 8532 MB/s 6288 MB/s 8998 MB/s
Signed-off-by: Eric Biggers <ebiggers@kernel.org>
---
arch/x86/crypto/Makefile | 1 -
arch/x86/crypto/aesni-intel_glue.c | 184 +-------
lib/crypto/Makefile | 3 +
.../crypto/x86}/aes-gcm-aesni-x86_64.S | 414 +++++++-----------
lib/crypto/x86/aes.h | 286 ++++++++++--
lib/crypto/x86/gf128hash-cpu.h | 6 +
lib/crypto/x86/gf128hash.h | 23 +
7 files changed, 452 insertions(+), 465 deletions(-)
rename {arch/x86/crypto => lib/crypto/x86}/aes-gcm-aesni-x86_64.S (75%)
diff --git a/arch/x86/crypto/Makefile b/arch/x86/crypto/Makefile
index 09bdea45bc3e7..2749f765c7724 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-aesni-x86_64.o \
aes-gcm-vaes-avx2.o \
aes-gcm-vaes-avx512.o
diff --git a/arch/x86/crypto/aesni-intel_glue.c b/arch/x86/crypto/aesni-intel_glue.c
index 3b58d77d2f5d3..51aa7b1566b3d 100644
--- a/arch/x86/crypto/aesni-intel_glue.c
+++ b/arch/x86/crypto/aesni-intel_glue.c
@@ -46,39 +46,6 @@ struct aes_gcm_key {
u32 rfc4106_nonce;
};
-/* Key struct used by the AES-NI implementations of AES-GCM */
-struct aes_gcm_key_aesni {
- /*
- * Common part of the key. 16-byte alignment is required by the
- * assembly code.
- */
- 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. 16-byte
- * alignment is required by the assembly code.
- */
- u64 h_powers[8][2] __aligned(16);
-
- /*
- * h_powers_xored[i] contains the two 64-bit halves of h_powers[i] XOR'd
- * together. It's used for Karatsuba multiplication. 16-byte alignment
- * is required by the assembly code.
- */
- u64 h_powers_xored[8] __aligned(16);
-
- /*
- * H^1 times x^64 (and also the usual extra factor of x^-1). 16-byte
- * alignment is required by the assembly code.
- */
- u64 h_times_x64[2] __aligned(16);
-};
-#define AES_GCM_KEY_AESNI(key) \
- container_of((key), struct aes_gcm_key_aesni, base)
-#define AES_GCM_KEY_AESNI_SIZE \
- (sizeof(struct aes_gcm_key_aesni) + (15 & ~(CRYPTO_MINALIGN - 1)))
-
/* Key struct used by the VAES + AVX2 implementation of AES-GCM */
struct aes_gcm_key_vaes_avx2 {
/*
@@ -142,7 +109,6 @@ struct aes_gcm_key_vaes_avx512 {
*/
#define FLAG_RFC4106 BIT(0)
#define FLAG_ENC BIT(1)
-#define FLAG_AVX BIT(2)
#define FLAG_VAES_AVX2 BIT(3)
#define FLAG_VAES_AVX512 BIT(4)
@@ -151,16 +117,10 @@ aes_gcm_key_get(struct crypto_aead *tfm, int flags)
{
if (flags & FLAG_VAES_AVX512)
return PTR_ALIGN(crypto_aead_ctx(tfm), 64);
- else if (flags & FLAG_VAES_AVX2)
- return PTR_ALIGN(crypto_aead_ctx(tfm), 32);
else
- return PTR_ALIGN(crypto_aead_ctx(tfm), 16);
+ return PTR_ALIGN(crypto_aead_ctx(tfm), 32);
}
-asmlinkage void
-aes_gcm_precompute_aesni(struct aes_gcm_key_aesni *key);
-asmlinkage void
-aes_gcm_precompute_aesni_avx(struct aes_gcm_key_aesni *key);
asmlinkage void
aes_gcm_precompute_vaes_avx2(struct aes_gcm_key_vaes_avx2 *key);
asmlinkage void
@@ -170,20 +130,10 @@ 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 if (flags & FLAG_VAES_AVX2)
- aes_gcm_precompute_vaes_avx2(AES_GCM_KEY_VAES_AVX2(key));
- else if (flags & FLAG_AVX)
- aes_gcm_precompute_aesni_avx(AES_GCM_KEY_AESNI(key));
else
- aes_gcm_precompute_aesni(AES_GCM_KEY_AESNI(key));
+ aes_gcm_precompute_vaes_avx2(AES_GCM_KEY_VAES_AVX2(key));
}
-asmlinkage void
-aes_gcm_aad_update_aesni(const struct aes_gcm_key_aesni *key,
- u8 ghash_acc[16], const u8 *aad, int aadlen);
-asmlinkage void
-aes_gcm_aad_update_aesni_avx(const struct aes_gcm_key_aesni *key,
- u8 ghash_acc[16], const u8 *aad, int aadlen);
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);
@@ -197,25 +147,11 @@ static void aes_gcm_aad_update(const struct aes_gcm_key *key, u8 ghash_acc[16],
if (flags & FLAG_VAES_AVX512)
aes_gcm_aad_update_vaes_avx512(AES_GCM_KEY_VAES_AVX512(key),
ghash_acc, aad, aadlen);
- else if (flags & FLAG_VAES_AVX2)
+ else
aes_gcm_aad_update_vaes_avx2(AES_GCM_KEY_VAES_AVX2(key),
ghash_acc, aad, aadlen);
- else if (flags & FLAG_AVX)
- aes_gcm_aad_update_aesni_avx(AES_GCM_KEY_AESNI(key), ghash_acc,
- aad, aadlen);
- else
- aes_gcm_aad_update_aesni(AES_GCM_KEY_AESNI(key), ghash_acc,
- aad, aadlen);
}
-asmlinkage void
-aes_gcm_enc_update_aesni(const struct aes_gcm_key_aesni *key,
- const u32 le_ctr[4], u8 ghash_acc[16],
- const u8 *src, u8 *dst, int datalen);
-asmlinkage void
-aes_gcm_enc_update_aesni_avx(const struct aes_gcm_key_aesni *key,
- const u32 le_ctr[4], u8 ghash_acc[16],
- const u8 *src, u8 *dst, int datalen);
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],
@@ -226,14 +162,6 @@ aes_gcm_enc_update_vaes_avx512(const struct aes_gcm_key_vaes_avx512 *key,
const u8 *src, u8 *dst, int datalen);
asmlinkage void
-aes_gcm_dec_update_aesni(const struct aes_gcm_key_aesni *key,
- const u32 le_ctr[4], u8 ghash_acc[16],
- const u8 *src, u8 *dst, int datalen);
-asmlinkage void
-aes_gcm_dec_update_aesni_avx(const struct aes_gcm_key_aesni *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);
@@ -253,45 +181,22 @@ aes_gcm_update(const struct aes_gcm_key *key,
aes_gcm_enc_update_vaes_avx512(AES_GCM_KEY_VAES_AVX512(key),
le_ctr, ghash_acc,
src, dst, datalen);
- else if (flags & FLAG_VAES_AVX2)
+ else
aes_gcm_enc_update_vaes_avx2(AES_GCM_KEY_VAES_AVX2(key),
le_ctr, ghash_acc,
src, dst, datalen);
- else if (flags & FLAG_AVX)
- aes_gcm_enc_update_aesni_avx(AES_GCM_KEY_AESNI(key),
- le_ctr, ghash_acc,
- src, dst, datalen);
- else
- aes_gcm_enc_update_aesni(AES_GCM_KEY_AESNI(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 if (flags & FLAG_VAES_AVX2)
+ else
aes_gcm_dec_update_vaes_avx2(AES_GCM_KEY_VAES_AVX2(key),
le_ctr, ghash_acc,
src, dst, datalen);
- else if (flags & FLAG_AVX)
- aes_gcm_dec_update_aesni_avx(AES_GCM_KEY_AESNI(key),
- le_ctr, ghash_acc,
- src, dst, datalen);
- else
- aes_gcm_dec_update_aesni(AES_GCM_KEY_AESNI(key),
- le_ctr, ghash_acc,
- src, dst, datalen);
}
}
-asmlinkage void
-aes_gcm_enc_final_aesni(const struct aes_gcm_key_aesni *key,
- const u32 le_ctr[4], u8 ghash_acc[16],
- u64 total_aadlen, u64 total_datalen);
-asmlinkage void
-aes_gcm_enc_final_aesni_avx(const struct aes_gcm_key_aesni *key,
- const u32 le_ctr[4], u8 ghash_acc[16],
- u64 total_aadlen, u64 total_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],
@@ -311,30 +216,12 @@ aes_gcm_enc_final(const struct aes_gcm_key *key,
aes_gcm_enc_final_vaes_avx512(AES_GCM_KEY_VAES_AVX512(key),
le_ctr, ghash_acc,
total_aadlen, total_datalen);
- else if (flags & FLAG_VAES_AVX2)
+ else
aes_gcm_enc_final_vaes_avx2(AES_GCM_KEY_VAES_AVX2(key),
le_ctr, ghash_acc,
total_aadlen, total_datalen);
- else if (flags & FLAG_AVX)
- aes_gcm_enc_final_aesni_avx(AES_GCM_KEY_AESNI(key),
- le_ctr, ghash_acc,
- total_aadlen, total_datalen);
- else
- aes_gcm_enc_final_aesni(AES_GCM_KEY_AESNI(key),
- le_ctr, ghash_acc,
- total_aadlen, total_datalen);
}
-asmlinkage bool __must_check
-aes_gcm_dec_final_aesni(const struct aes_gcm_key_aesni *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_aesni_avx(const struct aes_gcm_key_aesni *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_avx2(const struct aes_gcm_key_vaes_avx2 *key,
const u32 le_ctr[4], const u8 ghash_acc[16],
@@ -357,21 +244,11 @@ aes_gcm_dec_final(const struct aes_gcm_key *key, const u32 le_ctr[4],
le_ctr, ghash_acc,
total_aadlen, total_datalen,
tag, taglen);
- else if (flags & FLAG_VAES_AVX2)
+ else
return aes_gcm_dec_final_vaes_avx2(AES_GCM_KEY_VAES_AVX2(key),
le_ctr, ghash_acc,
total_aadlen, total_datalen,
tag, taglen);
- else if (flags & FLAG_AVX)
- return aes_gcm_dec_final_aesni_avx(AES_GCM_KEY_AESNI(key),
- le_ctr, ghash_acc,
- total_aadlen, total_datalen,
- tag, taglen);
- else
- return aes_gcm_dec_final_aesni(AES_GCM_KEY_AESNI(key),
- le_ctr, ghash_acc,
- total_aadlen, total_datalen,
- tag, taglen);
}
/*
@@ -439,11 +316,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_aesni, base.aes_key.len) == 0);
- static_assert(offsetof(struct aes_gcm_key_aesni, base.aes_key.k.rndkeys) == 16);
- static_assert(offsetof(struct aes_gcm_key_aesni, h_powers) == 272);
- static_assert(offsetof(struct aes_gcm_key_aesni, h_powers_xored) == 400);
- static_assert(offsetof(struct aes_gcm_key_aesni, h_times_x64) == 464);
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);
@@ -465,9 +337,6 @@ static int gcm_setkey(struct crypto_aead *tfm, const u8 *raw_key,
static const u8 x_to_the_minus1[16] __aligned(__alignof__(be128)) = {
[0] = 0xc2, [15] = 1
};
- static const u8 x_to_the_63[16] __aligned(__alignof__(be128)) = {
- [7] = 1,
- };
be128 h1 = {};
be128 h;
int i;
@@ -490,7 +359,7 @@ static int gcm_setkey(struct crypto_aead *tfm, const u8 *raw_key,
gf128mul_lle(&h, &h1);
}
memset(k->padding, 0, sizeof(k->padding));
- } else if (flags & FLAG_VAES_AVX2) {
+ } 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 };
@@ -506,19 +375,6 @@ static int gcm_setkey(struct crypto_aead *tfm, const u8 *raw_key,
k->h_powers_xored[i] = k->h_powers[j][0] ^
k->h_powers[j][1];
}
- } else {
- struct aes_gcm_key_aesni *k = AES_GCM_KEY_AESNI(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);
- k->h_powers_xored[i] = k->h_powers[i][0] ^
- k->h_powers[i][1];
- gf128mul_lle(&h, &h1);
- }
- gf128mul_lle(&h1, (const be128 *)x_to_the_63);
- k->h_times_x64[0] = be64_to_cpu(h1.b);
- k->h_times_x64[1] = be64_to_cpu(h1.a);
}
}
return 0;
@@ -764,16 +620,6 @@ static struct aead_alg aes_gcm_algs_##suffix[] = { { \
}, \
} }
-/* aes_gcm_algs_aesni */
-DEFINE_GCM_ALGS(aesni, /* no flags */ 0,
- "generic-gcm-aesni", "rfc4106-gcm-aesni",
- AES_GCM_KEY_AESNI_SIZE, 400);
-
-/* aes_gcm_algs_aesni_avx */
-DEFINE_GCM_ALGS(aesni_avx, FLAG_AVX,
- "generic-gcm-aesni-avx", "rfc4106-gcm-aesni-avx",
- AES_GCM_KEY_AESNI_SIZE, 500);
-
/* aes_gcm_algs_vaes_avx2 */
DEFINE_GCM_ALGS(vaes_avx2, FLAG_VAES_AVX2,
"generic-gcm-vaes-avx2", "rfc4106-gcm-vaes-avx2",
@@ -790,10 +636,6 @@ static int __init register_avx_algs(void)
if (!boot_cpu_has(X86_FEATURE_AVX))
return 0;
- err = crypto_register_aeads(aes_gcm_algs_aesni_avx,
- ARRAY_SIZE(aes_gcm_algs_aesni_avx));
- if (err)
- return err;
/*
* Note: not all the algorithms registered below actually require
* VPCLMULQDQ. But in practice every CPU with VAES also has VPCLMULQDQ.
@@ -836,7 +678,6 @@ static int __init register_avx_algs(void)
static void unregister_avx_algs(void)
{
- unregister_aeads(aes_gcm_algs_aesni_avx);
unregister_aeads(aes_gcm_algs_vaes_avx2);
unregister_aeads(aes_gcm_algs_vaes_avx512);
}
@@ -854,11 +695,6 @@ static int __init aesni_init(void)
if (!x86_match_cpu(aesni_cpu_id))
return -ENODEV;
- err = crypto_register_aeads(aes_gcm_algs_aesni,
- ARRAY_SIZE(aes_gcm_algs_aesni));
- if (err)
- return err;
-
err = register_avx_algs();
if (err)
goto unregister_avx;
@@ -867,15 +703,11 @@ static int __init aesni_init(void)
unregister_avx:
unregister_avx_algs();
- crypto_unregister_aeads(aes_gcm_algs_aesni,
- ARRAY_SIZE(aes_gcm_algs_aesni));
return err;
}
static void __exit aesni_exit(void)
{
- crypto_unregister_aeads(aes_gcm_algs_aesni,
- ARRAY_SIZE(aes_gcm_algs_aesni));
unregister_avx_algs();
}
diff --git a/lib/crypto/Makefile b/lib/crypto/Makefile
index c057c0d896a4f..b813c895cc9da 100644
--- a/lib/crypto/Makefile
+++ b/lib/crypto/Makefile
@@ -95,6 +95,9 @@ libaes-$(CONFIG_X86) += x86/aes-aesni.o
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
+endif
ifneq ($(CONFIG_CRYPTO_LIB_AES_XTS),)
libaes-$(CONFIG_X86_64) += x86/aes-xts-avx-x86_64.o
endif
diff --git a/arch/x86/crypto/aes-gcm-aesni-x86_64.S b/lib/crypto/x86/aes-gcm-aesni-x86_64.S
similarity index 75%
rename from arch/x86/crypto/aes-gcm-aesni-x86_64.S
rename to lib/crypto/x86/aes-gcm-aesni-x86_64.S
index 6b2abb76827e1..a385ca3372e59 100644
--- a/arch/x86/crypto/aes-gcm-aesni-x86_64.S
+++ b/lib/crypto/x86/aes-gcm-aesni-x86_64.S
@@ -134,20 +134,21 @@
.quad 0xc200000000000000
.Lone:
.quad 1
-.Lgfpoly_and_internal_carrybit:
- .octa 0xc2000000000000010000000000000001
// Loading 16 bytes from '.Lzeropad_mask + 16 - len' produces a mask of
// 'len' 0xff bytes and the rest zeroes.
.Lzeropad_mask:
.octa 0xffffffffffffffffffffffffffffffff
.octa 0
+.Lbe_one:
+ .long 0x01000000
-// Offsets in struct aes_gcm_key_aesni
+// Offsets in struct aes_gcm_key
#define OFFSETOF_AESKEYLEN 0
#define OFFSETOF_AESROUNDKEYS 16
-#define OFFSETOF_H_POWERS 272
-#define OFFSETOF_H_POWERS_XORED 400
-#define OFFSETOF_H_TIMES_X64 464
+#define OFFSETOF_H_POWERS_XORED (256 + 0)
+#define OFFSETOF_H_POWERS (256 + 128)
+#define OFFSETOF_H_TIMES_X64 (256 + 304)
+#define OFFSETOF_AUTHTAG_LEN (256 + 320)
.text
@@ -206,6 +207,26 @@
.endif
.endm
+// Do one AES round on \dst using the unaligned round key at \round_key_mem.
+.macro _aesenc_1x round_key_mem, dst, tmp
+.if USE_AVX
+ vaesenc \round_key_mem, \dst, \dst
+.else
+ movdqu \round_key_mem, \tmp
+ aesenc \tmp, \dst
+.endif
+.endm
+
+// Do the final AES round on \dst using the unaligned round key at \round_key_mem.
+.macro _aesenclast_1x round_key_mem, dst, tmp
+.if USE_AVX
+ vaesenclast \round_key_mem, \dst, \dst
+.else
+ movdqu \round_key_mem, \tmp
+ aesenclast \tmp, \dst
+.endif
+.endm
+
// Load 1 <= %ecx <= 15 bytes from the pointer \src into the xmm register \dst
// and zeroize any remaining bytes. Clobbers %rax, %rcx, and \tmp{64,32}.
.macro _load_partial_block src, dst, tmp64, tmp32
@@ -318,14 +339,6 @@
.endif
.endm
-// GHASH-multiply \a by \b and store the reduced product in \b.
-// See _ghash_mul_step for details.
-.macro _ghash_mul a, a_times_x64, b, gfpoly, t0, t1
-.irp i, 0,1,2,3,4,5,6,7,8,9
- _ghash_mul_step \i, \a, \a_times_x64, \b, \gfpoly, \t0, \t1
-.endr
-.endm
-
// GHASH-multiply \a by \b and add the unreduced product to \lo, \mi, and \hi.
// This does Karatsuba multiplication and must be paired with _ghash_reduce. On
// the first call, \lo, \mi, and \hi must be zero. \a_xored must contain the
@@ -399,7 +412,7 @@
xor %eax, %eax
// Load the highest hash key power, H^8.
- movdqa OFFSETOF_H_POWERS(KEY), TMP0
+ movdqu OFFSETOF_H_POWERS(KEY), TMP0
// Load the first ciphertext block and byte-reflect it.
.if \enc
@@ -424,7 +437,8 @@
pclmulqdq $0x11, TMP0, GHASH_ACC
// MI = (a_L + a_H) * (b_L + b_H)
- pclmulqdq $0x00, OFFSETOF_H_POWERS_XORED(KEY), MI
+ movq OFFSETOF_H_POWERS_XORED(KEY), TMP1
+ pclmulqdq $0x00, TMP1, MI
.endm
// Continue the GHASH update of 8 ciphertext blocks as described above by doing
@@ -434,7 +448,7 @@
add $8, %eax
// Load the next lowest key power.
- movdqa OFFSETOF_H_POWERS(KEY,%rax,2), TMP0
+ movdqu OFFSETOF_H_POWERS(KEY,%rax,2), TMP0
// Load the next ciphertext block and byte-reflect it.
.if \enc
@@ -487,145 +501,12 @@
.endif
.endm
-// void aes_gcm_precompute_##suffix(struct aes_gcm_key_aesni *key);
-//
-// Given the expanded AES key, derive the GHASH subkey and initialize the GHASH
-// related fields in the key struct.
-.macro _aes_gcm_precompute
-
- // Function arguments
- .set KEY, %rdi
-
- // Additional local variables.
- // %xmm0-%xmm1 and %rax are used as temporaries.
- .set RNDKEYLAST_PTR, %rsi
- .set H_CUR, %xmm2
- .set H_POW1, %xmm3 // H^1
- .set H_POW1_X64, %xmm4 // H^1 * x^64
- .set GFPOLY, %xmm5
-
- // 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
- movdqa OFFSETOF_AESROUNDKEYS(KEY), H_POW1
- lea OFFSETOF_AESROUNDKEYS+16(KEY), %rax
-1:
- aesenc (%rax), H_POW1
- add $16, %rax
- cmp %rax, RNDKEYLAST_PTR
- jne 1b
- aesenclast (RNDKEYLAST_PTR), H_POW1
-
- // Preprocess the raw hash subkey as needed to operate on GHASH's
- // bit-reflected values directly: reflect its bytes, then multiply it by
- // x^-1 (using the backwards interpretation of polynomial coefficients
- // from the GCM spec) or equivalently x^1 (using the alternative,
- // natural interpretation of polynomial coefficients).
- pshufb .Lbswap_mask(%rip), H_POW1
- movdqa H_POW1, %xmm0
- pshufd $0xd3, %xmm0, %xmm0
- psrad $31, %xmm0
- paddq H_POW1, H_POW1
- pand .Lgfpoly_and_internal_carrybit(%rip), %xmm0
- pxor %xmm0, H_POW1
-
- // Store H^1.
- movdqa H_POW1, OFFSETOF_H_POWERS+7*16(KEY)
-
- // Compute and store H^1 * x^64.
- movq .Lgfpoly(%rip), GFPOLY
- pshufd $0x4e, H_POW1, %xmm0
- _vpclmulqdq $0x00, H_POW1, GFPOLY, H_POW1_X64
- pxor %xmm0, H_POW1_X64
- movdqa H_POW1_X64, OFFSETOF_H_TIMES_X64(KEY)
-
- // Compute and store the halves of H^1 XOR'd together.
- pxor H_POW1, %xmm0
- movq %xmm0, OFFSETOF_H_POWERS_XORED+7*8(KEY)
-
- // Compute and store the remaining key powers H^2 through H^8.
- movdqa H_POW1, H_CUR
- mov $6*8, %eax
-.Lprecompute_next\@:
- // Compute H^i = H^{i-1} * H^1.
- _ghash_mul H_POW1, H_POW1_X64, H_CUR, GFPOLY, %xmm0, %xmm1
- // Store H^i.
- movdqa H_CUR, OFFSETOF_H_POWERS(KEY,%rax,2)
- // Compute and store the halves of H^i XOR'd together.
- pshufd $0x4e, H_CUR, %xmm0
- pxor H_CUR, %xmm0
- movq %xmm0, OFFSETOF_H_POWERS_XORED(KEY,%rax)
- sub $8, %eax
- jge .Lprecompute_next\@
-
- RET
-.endm
-
-// void aes_gcm_aad_update_aesni(const struct aes_gcm_key_aesni *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.
-.macro _aes_gcm_aad_update
-
- // Function arguments
- .set KEY, %rdi
- .set GHASH_ACC_PTR, %rsi
- .set AAD, %rdx
- .set AADLEN, %ecx
- // Note: _load_partial_block relies on AADLEN being in %ecx.
-
- // Additional local variables.
- // %rax, %r10, and %xmm0-%xmm1 are used as temporary registers.
- .set BSWAP_MASK, %xmm2
- .set GHASH_ACC, %xmm3
- .set H_POW1, %xmm4 // H^1
- .set H_POW1_X64, %xmm5 // H^1 * x^64
- .set GFPOLY, %xmm6
-
- movdqa .Lbswap_mask(%rip), BSWAP_MASK
- movdqu (GHASH_ACC_PTR), GHASH_ACC
- movdqa OFFSETOF_H_POWERS+7*16(KEY), H_POW1
- movdqa OFFSETOF_H_TIMES_X64(KEY), H_POW1_X64
- movq .Lgfpoly(%rip), GFPOLY
-
- // Process the AAD one full block at a time.
- sub $16, AADLEN
- jl .Laad_loop_1x_done\@
-.Laad_loop_1x\@:
- movdqu (AAD), %xmm0
- pshufb BSWAP_MASK, %xmm0
- pxor %xmm0, GHASH_ACC
- _ghash_mul H_POW1, H_POW1_X64, GHASH_ACC, GFPOLY, %xmm0, %xmm1
- add $16, AAD
- sub $16, AADLEN
- jge .Laad_loop_1x\@
-.Laad_loop_1x_done\@:
- // Check whether there is a partial block at the end.
- add $16, AADLEN
- jz .Laad_done\@
-
- // Process a partial block of length 1 <= AADLEN <= 15.
- // _load_partial_block assumes that %ecx contains AADLEN.
- _load_partial_block AAD, %xmm0, %r10, %r10d
- pshufb BSWAP_MASK, %xmm0
- pxor %xmm0, GHASH_ACC
- _ghash_mul H_POW1, H_POW1_X64, GHASH_ACC, GFPOLY, %xmm0, %xmm1
-
-.Laad_done\@:
- movdqu GHASH_ACC, (GHASH_ACC_PTR)
- RET
-.endm
-
// Increment LE_CTR eight times to generate eight little-endian counter blocks,
// swap each to big-endian, and store them in AESDATA[0-7]. Also XOR them with
// the zero-th AES round key. Clobbers TMP0 and TMP1.
.macro _ctr_begin_8x
movq .Lone(%rip), TMP0
- movdqa OFFSETOF_AESROUNDKEYS(KEY), TMP1 // zero-th round key
+ movdqu OFFSETOF_AESROUNDKEYS(KEY), TMP1 // zero-th round key
.irp i, 0,1,2,3,4,5,6,7
_vpshufb BSWAP_MASK, LE_CTR, AESDATA\i
pxor TMP1, AESDATA\i
@@ -658,9 +539,11 @@
.endr
.endm
-// void aes_gcm_{enc,dec}_update_##suffix(const struct aes_gcm_key_aesni *key,
-// const u32 le_ctr[4], u8 ghash_acc[16],
-// const u8 *src, u8 *dst, int datalen);
+// void aes_gcm_{enc,dec}_update_##suffix(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).
@@ -674,32 +557,27 @@
// 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. For a new
-// message, the low word of the counter must be 2. 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
+// |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 // Note: overlaps with usage as temp reg
- .set GHASH_ACC_PTR, %rdx
- .set SRC, %rcx
- .set DST, %r8
- .set DATALEN, %r9d
- .set DATALEN64, %r9 // Zero-extend DATALEN before using!
- // Note: the code setting up for _load_partial_block assumes that SRC is
- // in %rcx (and that DATALEN is *not* in %rcx).
+ .set DST, %rdi
+ .set SRC, %rsi
+ .set DATALEN, %rdx
+ .set DATALEN32, %edx
+ .set GHASH_ACC_PTR, %rcx
+ .set BE_CTR_PTR, %r8
+ .set KEY, %r9
+ // Note: the code setting up for _load_partial_block and
+ // _store_partial_block assumes that GHASH_ACC_PTR is in %rcx (and that
+ // DATALEN is *not* in %rcx).
// Additional local variables
-
- // %rax and %rsi are used as temporary registers. Note: %rsi overlaps
- // with LE_CTR_PTR, which is used only at the beginning.
-
- .set AESKEYLEN, %r10d // AES key length in bytes
- .set AESKEYLEN64, %r10
+ .set RNDKEY_PTR, %r10
.set RNDKEYLAST_PTR, %r11 // Pointer to last AES round key
// Put the most frequently used values in %xmm0-%xmm7 to reduce code
@@ -724,10 +602,11 @@
movdqa .Lbswap_mask(%rip), BSWAP_MASK
movdqu (GHASH_ACC_PTR), GHASH_ACC
- movdqu (LE_CTR_PTR), LE_CTR
+ movdqu (BE_CTR_PTR), LE_CTR
+ pshufb BSWAP_MASK, LE_CTR
- movl OFFSETOF_AESKEYLEN(KEY), AESKEYLEN
- lea OFFSETOF_AESROUNDKEYS+6*16(KEY,AESKEYLEN64,4), RNDKEYLAST_PTR
+ movl OFFSETOF_AESKEYLEN(KEY), %eax
+ lea OFFSETOF_AESROUNDKEYS+6*16(KEY,%rax,4), RNDKEYLAST_PTR
// If there are at least 8*16 bytes of data, then continue into the main
// loop, which processes 8*16 bytes of data per iteration.
@@ -746,15 +625,15 @@
.if \enc
// Encrypt the first 8 plaintext blocks.
_ctr_begin_8x
- lea OFFSETOF_AESROUNDKEYS+16(KEY), %rsi
+ lea OFFSETOF_AESROUNDKEYS+16(KEY), RNDKEY_PTR
.p2align 4
1:
- movdqa (%rsi), TMP0
+ movdqu (RNDKEY_PTR), TMP0
_aesenc_8x TMP0
- add $16, %rsi
- cmp %rsi, RNDKEYLAST_PTR
+ add $16, RNDKEY_PTR
+ cmp RNDKEY_PTR, RNDKEYLAST_PTR
jne 1b
- movdqa (%rsi), TMP0
+ movdqu (RNDKEY_PTR), TMP0
_aesenclast_8x TMP0
_xor_data_8x
// Don't increment DST until the ciphertext blocks have been hashed.
@@ -768,12 +647,12 @@
// Generate the next set of 8 counter blocks and start encrypting them.
_ctr_begin_8x
- lea OFFSETOF_AESROUNDKEYS+16(KEY), %rsi
+ lea OFFSETOF_AESROUNDKEYS+16(KEY), RNDKEY_PTR
// Do a round of AES, and start the GHASH update of 8 ciphertext blocks
// by doing the unreduced multiplication for the first ciphertext block.
- movdqa (%rsi), TMP0
- add $16, %rsi
+ movdqu (RNDKEY_PTR), TMP0
+ add $16, RNDKEY_PTR
_aesenc_8x TMP0
_ghash_update_begin_8x \enc
@@ -781,8 +660,8 @@
// unreduced multiplication for the remaining ciphertext blocks.
.p2align 4
1:
- movdqa (%rsi), TMP0
- add $16, %rsi
+ movdqu (RNDKEY_PTR), TMP0
+ add $16, RNDKEY_PTR
_aesenc_8x TMP0
_ghash_update_continue_8x \enc
cmp $7*8, %eax
@@ -791,14 +670,14 @@
// Do the remaining AES rounds.
.p2align 4
1:
- movdqa (%rsi), TMP0
- add $16, %rsi
+ movdqu (RNDKEY_PTR), TMP0
+ add $16, RNDKEY_PTR
_aesenc_8x TMP0
- cmp %rsi, RNDKEYLAST_PTR
+ cmp RNDKEY_PTR, RNDKEYLAST_PTR
jne 1b
// Do the GHASH reduction and the last round of AES.
- movdqa (RNDKEYLAST_PTR), TMP0
+ movdqu (RNDKEYLAST_PTR), TMP0
_ghash_update_end_8x_step 0
_aesenclast_8x TMP0
_ghash_update_end_8x_step 1
@@ -855,7 +734,7 @@
// Set up a block counter %rax to contain 8*(8-n), where n is the number
// of blocks that remain, counting any partial block. This will be used
// to access the key powers H^n through H^1.
- mov DATALEN, %eax
+ mov DATALEN32, %eax
neg %eax
and $~15, %eax
sar $1, %eax
@@ -870,25 +749,25 @@
// Encrypt the next counter block.
_vpshufb BSWAP_MASK, LE_CTR, TMP0
paddd ONE, LE_CTR
- pxor OFFSETOF_AESROUNDKEYS(KEY), TMP0
- lea -6*16(RNDKEYLAST_PTR), %rsi // Reduce code size
- cmp $24, AESKEYLEN
+ _xor_mem_to_reg OFFSETOF_AESROUNDKEYS(KEY), TMP0, tmp=TMP1
+ lea -6*16(RNDKEYLAST_PTR), RNDKEY_PTR // Reduce code size
+ cmpl $24, OFFSETOF_AESKEYLEN(KEY)
jl 128f // AES-128?
je 192f // AES-192?
// AES-256
- aesenc -7*16(%rsi), TMP0
- aesenc -6*16(%rsi), TMP0
+ _aesenc_1x -7*16(RNDKEY_PTR), TMP0, TMP1
+ _aesenc_1x -6*16(RNDKEY_PTR), TMP0, TMP1
192:
- aesenc -5*16(%rsi), TMP0
- aesenc -4*16(%rsi), TMP0
+ _aesenc_1x -5*16(RNDKEY_PTR), TMP0, TMP1
+ _aesenc_1x -4*16(RNDKEY_PTR), TMP0, TMP1
128:
.irp i, -3,-2,-1,0,1,2,3,4,5
- aesenc \i*16(%rsi), TMP0
+ _aesenc_1x \i*16(RNDKEY_PTR), TMP0, TMP1
.endr
- aesenclast (RNDKEYLAST_PTR), TMP0
+ _aesenclast_1x (RNDKEYLAST_PTR), TMP0, TMP1
// Load the next key power H^i.
- movdqa OFFSETOF_H_POWERS(KEY,%rax,2), H_POW
+ movdqu OFFSETOF_H_POWERS(KEY,%rax,2), H_POW
movq OFFSETOF_H_POWERS_XORED(KEY,%rax), H_POW_XORED
// XOR the keystream block that was just generated in TMP0 with the next
@@ -927,37 +806,39 @@
// Encrypt a counter block for the last time.
pshufb BSWAP_MASK, LE_CTR
- pxor OFFSETOF_AESROUNDKEYS(KEY), LE_CTR
- lea OFFSETOF_AESROUNDKEYS+16(KEY), %rsi
+ _xor_mem_to_reg OFFSETOF_AESROUNDKEYS(KEY), LE_CTR, tmp=TMP0
+ lea OFFSETOF_AESROUNDKEYS+16(KEY), RNDKEY_PTR
1:
- aesenc (%rsi), LE_CTR
- add $16, %rsi
- cmp %rsi, RNDKEYLAST_PTR
+ _aesenc_1x (RNDKEY_PTR), LE_CTR, TMP0
+ add $16, RNDKEY_PTR
+ cmp RNDKEY_PTR, RNDKEYLAST_PTR
jne 1b
- aesenclast (RNDKEYLAST_PTR), LE_CTR
+ _aesenclast_1x (RNDKEYLAST_PTR), LE_CTR, TMP0
// Load the lowest key power, H^1.
- movdqa OFFSETOF_H_POWERS(KEY,%rax,2), H_POW
+ movdqu OFFSETOF_H_POWERS(KEY,%rax,2), H_POW
movq OFFSETOF_H_POWERS_XORED(KEY,%rax), H_POW_XORED
- // Load and zero-pad 1 <= DATALEN <= 15 bytes of data from SRC. SRC is
- // in %rcx, but _load_partial_block needs DATALEN in %rcx instead.
- // RNDKEYLAST_PTR is no longer needed, so reuse it for SRC.
- mov SRC, RNDKEYLAST_PTR
- mov DATALEN, %ecx
- _load_partial_block RNDKEYLAST_PTR, TMP0, %rsi, %esi
+ // Load and zero-pad 1 <= DATALEN <= 15 bytes of data from SRC.
+ // GHASH_ACC_PTR is in %rcx, but _load_partial_block and
+ // _store_partial_block need DATALEN in %ecx instead. Save
+ // GHASH_ACC_PTR in RNDKEYLAST_PTR, which is no longer needed.
+ mov GHASH_ACC_PTR, RNDKEYLAST_PTR
+ mov DATALEN32, %ecx
+ _load_partial_block SRC, TMP0, %r10, %r10d
// XOR the keystream block that was just generated in LE_CTR with the
// source data block and store the resulting en/decrypted data to DST.
pxor TMP0, LE_CTR
- mov DATALEN, %ecx
+ mov DATALEN32, %ecx
_store_partial_block LE_CTR, DST
+ mov RNDKEYLAST_PTR, GHASH_ACC_PTR
// If encrypting, zero-pad the final ciphertext block for GHASH. (If
// decrypting, this was already done by _load_partial_block.)
.if \enc
lea .Lzeropad_mask+16(%rip), %rax
- sub DATALEN64, %rax
+ sub DATALEN, %rax
_vpand (%rax), LE_CTR, TMP0
.endif
@@ -977,13 +858,16 @@
RET
.endm
-// void aes_gcm_enc_final_##suffix(const struct aes_gcm_key_aesni *key,
-// const u32 le_ctr[4], u8 ghash_acc[16],
-// u64 total_aadlen, u64 total_datalen);
-// bool aes_gcm_dec_final_##suffix(const struct aes_gcm_key_aesni *key,
-// const u32 le_ctr[4], const u8 ghash_acc[16],
+// void aes_gcm_enc_final_##suffix(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_##suffix(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);
+// 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
@@ -991,41 +875,42 @@
// |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-%xmm2 are used as temporary registers.
- .set AESKEYLEN, %r11d
- .set AESKEYLEN64, %r11
- .set BSWAP_MASK, %xmm3
+ // %rax and %xmm0-%xmm3 are used as temporary registers.
+ .set AESKEYLEN, %r10d
+ .set AESKEYLEN64, %r10
+ .set TAGLEN, %r11d
+ .set TAGLEN64, %r11
.set GHASH_ACC, %xmm4
.set H_POW1, %xmm5 // H^1
.set H_POW1_X64, %xmm6 // H^1 * x^64
.set GFPOLY, %xmm7
+ .set BSWAP_MASK, %xmm8
movdqa .Lbswap_mask(%rip), BSWAP_MASK
movl OFFSETOF_AESKEYLEN(KEY), AESKEYLEN
+ // authtag_len is size_t with value in 4..16, so movl suffices here.
+ movl OFFSETOF_AUTHTAG_LEN(KEY), TAGLEN
- // 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.
- movdqu (LE_CTR_PTR), %xmm0
- mov $1, %eax
- pinsrd $0, %eax, %xmm0
+ // Build J_0 = nonce || be32(1).
+ movq (NONCE_PTR), %xmm0
+ pinsrd $2, 8(NONCE_PTR), %xmm0
+ pinsrd $3, .Lbe_one(%rip), %xmm0
// Build the lengths block and XOR it into the GHASH accumulator.
movq TOTAL_DATALEN, GHASH_ACC
@@ -1033,8 +918,8 @@
psllq $3, GHASH_ACC // Bytes to bits
_xor_mem_to_reg (GHASH_ACC_PTR), GHASH_ACC, %xmm1
- movdqa OFFSETOF_H_POWERS+7*16(KEY), H_POW1
- movdqa OFFSETOF_H_TIMES_X64(KEY), H_POW1_X64
+ movdqu OFFSETOF_H_POWERS+7*16(KEY), H_POW1
+ movdqu OFFSETOF_H_TIMES_X64(KEY), H_POW1_X64
movq .Lgfpoly(%rip), GFPOLY
// Make %rax point to the 6th from last AES round key. (Using signed
@@ -1043,23 +928,23 @@
// AES-encrypt the counter block and also multiply GHASH_ACC by H^1.
// Interleave the AES and GHASH instructions to improve performance.
- pshufb BSWAP_MASK, %xmm0
- pxor OFFSETOF_AESROUNDKEYS(KEY), %xmm0
+ _xor_mem_to_reg OFFSETOF_AESROUNDKEYS(KEY), %xmm0, tmp=%xmm1
+
cmp $24, AESKEYLEN
jl 128f // AES-128?
je 192f // AES-192?
// AES-256
- aesenc -7*16(%rax), %xmm0
- aesenc -6*16(%rax), %xmm0
+ _aesenc_1x -7*16(%rax), %xmm0, %xmm3
+ _aesenc_1x -6*16(%rax), %xmm0, %xmm3
192:
- aesenc -5*16(%rax), %xmm0
- aesenc -4*16(%rax), %xmm0
+ _aesenc_1x -5*16(%rax), %xmm0, %xmm3
+ _aesenc_1x -4*16(%rax), %xmm0, %xmm3
128:
.irp i, 0,1,2,3,4,5,6,7,8
- aesenc (\i-3)*16(%rax), %xmm0
+ _aesenc_1x (\i-3)*16(%rax), %xmm0, %xmm3
_ghash_mul_step \i, H_POW1, H_POW1_X64, GHASH_ACC, GFPOLY, %xmm1, %xmm2
.endr
- aesenclast 6*16(%rax), %xmm0
+ _aesenclast_1x 6*16(%rax), %xmm0, %xmm3
_ghash_mul_step 9, H_POW1, H_POW1_X64, GHASH_ACC, GFPOLY, %xmm1, %xmm2
// Undo the byte reflection of the GHASH accumulator.
@@ -1070,15 +955,30 @@
.if \enc
// Return the computed auth tag.
- movdqu GHASH_ACC, (GHASH_ACC_PTR)
+ cmp $16, TAGLEN
+ jne .Lstore_short_tag\@
+ movdqu GHASH_ACC, (TAG)
+ RET
+.Lstore_short_tag\@:
+ mov TAGLEN, %ecx
+ _store_partial_block GHASH_ACC, TAG
.else
.set ZEROPAD_MASK_PTR, TOTAL_AADLEN // Reusing TOTAL_AADLEN!
// Verify the auth tag in constant time by XOR'ing the transmitted and
// computed auth tags together and using the ptest instruction to check
// whether the first TAGLEN bytes of the result are zero.
+ cmp $16, TAGLEN
+ jne .Lverify_short_tag\@
_xor_mem_to_reg (TAG), GHASH_ACC, tmp=%xmm0
- movl 8(%rsp), TAGLEN
+ xor %eax, %eax
+ ptest GHASH_ACC, GHASH_ACC
+ sete %al
+ RET
+.Lverify_short_tag\@:
+ mov TAGLEN, %ecx
+ _load_partial_block TAG, %xmm0, %r8, %r8d
+ pxor %xmm0, GHASH_ACC
lea .Lzeropad_mask+16(%rip), ZEROPAD_MASK_PTR
sub TAGLEN64, ZEROPAD_MASK_PTR
xor %eax, %eax
@@ -1089,12 +989,6 @@
.endm
.set USE_AVX, 0
-SYM_FUNC_START(aes_gcm_precompute_aesni)
- _aes_gcm_precompute
-SYM_FUNC_END(aes_gcm_precompute_aesni)
-SYM_FUNC_START(aes_gcm_aad_update_aesni)
- _aes_gcm_aad_update
-SYM_FUNC_END(aes_gcm_aad_update_aesni)
SYM_FUNC_START(aes_gcm_enc_update_aesni)
_aes_gcm_update 1
SYM_FUNC_END(aes_gcm_enc_update_aesni)
@@ -1109,12 +1003,6 @@ SYM_FUNC_START(aes_gcm_dec_final_aesni)
SYM_FUNC_END(aes_gcm_dec_final_aesni)
.set USE_AVX, 1
-SYM_FUNC_START(aes_gcm_precompute_aesni_avx)
- _aes_gcm_precompute
-SYM_FUNC_END(aes_gcm_precompute_aesni_avx)
-SYM_FUNC_START(aes_gcm_aad_update_aesni_avx)
- _aes_gcm_aad_update
-SYM_FUNC_END(aes_gcm_aad_update_aesni_avx)
SYM_FUNC_START(aes_gcm_enc_update_aesni_avx)
_aes_gcm_update 1
SYM_FUNC_END(aes_gcm_enc_update_aesni_avx)
diff --git a/lib/crypto/x86/aes.h b/lib/crypto/x86/aes.h
index 01af1a522b429..72222c2895a4f 100644
--- a/lib/crypto/x86/aes.h
+++ b/lib/crypto/x86/aes.h
@@ -6,8 +6,10 @@
*/
#include <asm/fpu/api.h>
+#include "gf128hash-cpu.h"
static __ro_after_init DEFINE_STATIC_KEY_FALSE(have_aesni);
+static __ro_after_init DEFINE_STATIC_KEY_FALSE(have_aesni_pclmul);
static __ro_after_init DEFINE_STATIC_KEY_FALSE(have_aesni_avx);
static __ro_after_init DEFINE_STATIC_KEY_FALSE(have_vaes_avx2);
static __ro_after_init DEFINE_STATIC_KEY_FALSE(have_vaes_avx512);
@@ -406,35 +408,269 @@ static bool aes_xts_decrypt_arch(u8 *dst, const u8 *src, size_t len,
}
#endif /* CONFIG_CRYPTO_LIB_AES_XTS */
+#if IS_ENABLED(CONFIG_CRYPTO_LIB_AES_GCM) && IS_ENABLED(CONFIG_X86_64)
+/* The assembly code assumes the following offsets. */
+static_assert(offsetof(struct gf128hash_key_x86, h_powers_xored) == 0);
+static_assert(offsetof(struct gf128hash_key_x86, h_powers) == 128);
+static_assert(offsetof(struct gf128hash_key_x86, h_times_xm64) == 304);
+static_assert(offsetof(struct gf128hash_key_x86, h_powers[7]) == 240);
+static_assert(offsetof(struct aes_gcm_key, aes) == 0);
+static_assert(offsetof(struct aes_gcm_key, ghash) == 256);
+static_assert(offsetof(struct aes_gcm_key, authtag_len) == 576);
+
+/*
+ * Limit the length of non-preemptible contexts. AES-GCM is fast, so use 32 KiB
+ * as the limit rather than 4 KiB. It must be a multiple of AES_BLOCK_SIZE.
+ */
+#define GCM_CHUNK_SIZE 32768
+
+void aes_gcm_enc_update_aesni(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_aesni(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_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_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_final_aesni(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_aesni(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);
+void aes_gcm_enc_final_aesni_avx(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_aesni_avx(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
+ * nonzero. @len is a multiple of AES_BLOCK_SIZE for incremental AES-GCM, or
+ * any length for one-shot AES-GCM.
+ */
+static __always_inline void
+aes_gcm_update_x86_fpu(u8 *dst, const u8 *src, size_t len,
+ struct polyval_elem *ghash_acc, __be32 ctr32[4],
+ const struct aes_gcm_key *key, bool enc)
+{
+ for (;;) {
+ size_t n = min(len, GCM_CHUNK_SIZE);
+
+ static_assert(GCM_CHUNK_SIZE % AES_BLOCK_SIZE == 0);
+
+ if (static_branch_likely(&have_aesni_avx)) {
+ if (enc)
+ aes_gcm_enc_update_aesni_avx(
+ dst, src, n, ghash_acc, ctr32, key);
+ else
+ aes_gcm_dec_update_aesni_avx(
+ dst, src, n, ghash_acc, ctr32, key);
+ } else {
+ if (enc)
+ aes_gcm_enc_update_aesni(dst, src, n, ghash_acc,
+ ctr32, key);
+ else
+ aes_gcm_dec_update_aesni(dst, src, n, ghash_acc,
+ ctr32, key);
+ }
+ len -= n;
+ if (likely(!len))
+ break;
+ kernel_fpu_end();
+ be32_add_cpu(&ctr32[3], GCM_CHUNK_SIZE / AES_BLOCK_SIZE);
+ dst += GCM_CHUNK_SIZE;
+ src += GCM_CHUNK_SIZE;
+ kernel_fpu_begin_mask(0);
+ }
+}
+
+/* len is always a positive multiple of AES_BLOCK_SIZE here. */
+static __always_inline bool
+aes_gcm_crypt_update_x86(u8 *dst, const u8 *src, size_t len,
+ struct polyval_elem *ghash_acc, const __be32 ctr32[4],
+ const struct aes_gcm_key *key, bool enc)
+{
+ __be32 ctr[4];
+
+ if (!static_branch_likely(&have_aesni_pclmul) ||
+ static_branch_likely(&have_vaes_avx2) ||
+ unlikely(!irq_fpu_usable()))
+ return false;
+
+ memcpy(ctr, ctr32, sizeof(ctr));
+ kernel_fpu_begin_mask(0);
+ aes_gcm_update_x86_fpu(dst, src, len, ghash_acc, ctr, key, enc);
+ kernel_fpu_end();
+ return true;
+}
+
+#define aes_gcm_encrypt_update_arch aes_gcm_encrypt_update_arch
+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_gcm_key *key)
+{
+ return aes_gcm_crypt_update_x86(dst, src, len, ghash_acc, ctr32, key,
+ true);
+}
+
+#define aes_gcm_decrypt_update_arch aes_gcm_decrypt_update_arch
+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_gcm_key *key)
+{
+ return aes_gcm_crypt_update_x86(dst, src, len, ghash_acc, ctr32, key,
+ false);
+}
+
+static void aes_gcm_process_ad_x86(const struct aes_gcm_key *key,
+ struct polyval_elem *ghash_acc, const u8 *ad,
+ size_t ad_len)
+{
+ size_t nblocks = ad_len / GHASH_BLOCK_SIZE;
+ size_t ad_rem = ad_len % GHASH_BLOCK_SIZE;
+
+ while (nblocks) {
+ size_t n = min(nblocks, GCM_CHUNK_SIZE / GHASH_BLOCK_SIZE);
+
+ ghash_blocks_x86_fpu(ghash_acc, &key->ghash.k, ad, n);
+ ad += n * GHASH_BLOCK_SIZE;
+ nblocks -= n;
+ if (likely(!nblocks))
+ break;
+ kernel_fpu_end();
+ kernel_fpu_begin_mask(0);
+ }
+ if (ad_rem) {
+ for (size_t i = 0; i < ad_rem; i++)
+ ghash_acc->bytes[GHASH_BLOCK_SIZE - 1 - i] ^= ad[i];
+ gf128hash_mul_x86_fpu(ghash_acc, &key->ghash.k);
+ }
+}
+
+static __always_inline bool
+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 = {};
+ __be32 ctr32[4];
+
+ if (!static_branch_likely(&have_aesni_pclmul) ||
+ static_branch_likely(&have_vaes_avx2) ||
+ unlikely(!irq_fpu_usable()))
+ return false;
+
+ memcpy(ctr32, nonce, 12);
+ ctr32[3] = cpu_to_be32(2);
+
+ kernel_fpu_begin_mask(0);
+
+ 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,
+ key, enc);
+
+ if (enc) {
+ if (static_branch_likely(&have_aesni_avx))
+ aes_gcm_enc_final_aesni_avx(key, nonce, &ghash_acc,
+ ad_len, data_len, authtag);
+ else
+ aes_gcm_enc_final_aesni(key, nonce, &ghash_acc, ad_len,
+ data_len, authtag);
+ } else {
+ bool valid;
+
+ if (static_branch_likely(&have_aesni_avx))
+ valid = aes_gcm_dec_final_aesni_avx(key, nonce,
+ &ghash_acc, ad_len,
+ data_len, authtag);
+ else
+ valid = aes_gcm_dec_final_aesni(key, nonce, &ghash_acc,
+ ad_len, data_len,
+ authtag);
+ *err = valid ? 0 : -EBADMSG;
+ }
+
+ kernel_fpu_end();
+ memzero_explicit(&ghash_acc, sizeof(ghash_acc));
+ return true;
+}
+
+#define aes_gcm_encrypt_arch 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 aes_gcm_crypt_x86(dst, src, data_len, authtag, ad, ad_len, nonce,
+ key, true, NULL);
+}
+
+#define aes_gcm_decrypt_arch 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 aes_gcm_crypt_x86(dst, src, data_len, (u8 *)authtag, ad, ad_len,
+ nonce, key, false, err);
+}
+#endif /* CONFIG_CRYPTO_LIB_AES_GCM && CONFIG_X86_64 */
+
#define aes_mod_init_arch aes_mod_init_arch
static void aes_mod_init_arch(void)
{
- /* Everything below requires AES-NI. */
- if (!boot_cpu_has(X86_FEATURE_AES))
+ if (!cpu_feature_enabled(X86_FEATURE_AES))
return;
- static_branch_enable(&have_aesni);
- /* Everything below requires AVX and is also 64-bit only. */
- if (!boot_cpu_has(X86_FEATURE_AVX) || !IS_ENABLED(CONFIG_X86_64))
- return;
- static_branch_enable(&have_aesni_avx);
-
- /*
- * Everything below requires VAES, and also sometimes AVX2, VPCLMULQDQ,
- * and PCLMULQDQ. Use a single static key for all of them, since in
- * practice every CPU with VAES also has the others.
- */
- if (!boot_cpu_has(X86_FEATURE_VAES) ||
- !boot_cpu_has(X86_FEATURE_AVX2) ||
- !boot_cpu_has(X86_FEATURE_VPCLMULQDQ) ||
- !boot_cpu_has(X86_FEATURE_PCLMULQDQ))
- return;
- static_branch_enable(&have_vaes_avx2);
+ /* All the AVX functions are 64-bit only. */
+ if (IS_ENABLED(CONFIG_X86_64) && cpu_feature_enabled(X86_FEATURE_AVX)) {
+ static_branch_enable(&have_aesni_avx);
- if (!boot_cpu_has(X86_FEATURE_AVX512BW) ||
- !boot_cpu_has(X86_FEATURE_AVX512VL) ||
- !boot_cpu_has(X86_FEATURE_BMI2) ||
- boot_cpu_has(X86_FEATURE_PREFER_YMM))
- return;
- static_branch_enable(&have_vaes_avx512);
+ /*
+ * The *_vaes_avx2 functions additionally require VAES,
+ * VPCLMULQDQ, and AVX2. (Not all actually need VPCLMULQDQ, but
+ * every CPU with VAES has VPCLMULQDQ too.)
+ *
+ * The *_vaes_avx512 functions require all of that, plus
+ * AVX512BW, AVX512VL, BMI2, and !PREFER_YMM.
+ *
+ * For AES-GCM, the *_vaes_avx2 and *_vaes_avx512 functions
+ * assume the *_vpclmul_avx2 and *_vpclmul_avx512 ghash_key
+ * layouts respectively. The ghash_key layout is selected by
+ * libgf128hash. Therefore, use the gf128hash_*() predicates
+ * (which check for all of these features including VAES) to
+ * ensure the two modules match.
+ */
+ if (gf128hash_use_vpclmul_avx2()) {
+ static_branch_enable(&have_vaes_avx2);
+ if (gf128hash_use_vpclmul_avx512())
+ static_branch_enable(&have_vaes_avx512);
+ }
+ }
+
+ if (gf128hash_use_pclmul() && cpu_feature_enabled(X86_FEATURE_XMM4_1))
+ static_branch_enable(&have_aesni_pclmul);
+
+ static_branch_enable(&have_aesni);
}
diff --git a/lib/crypto/x86/gf128hash-cpu.h b/lib/crypto/x86/gf128hash-cpu.h
index ed7500e665d8d..bad0a5f653851 100644
--- a/lib/crypto/x86/gf128hash-cpu.h
+++ b/lib/crypto/x86/gf128hash-cpu.h
@@ -45,4 +45,10 @@ static inline bool gf128hash_use_vpclmul_avx512(void)
!cpu_feature_enabled(X86_FEATURE_PREFER_YMM);
}
+void ghash_blocks_x86_fpu(struct polyval_elem *acc,
+ const struct gf128hash_key_x86 *key, const u8 *data,
+ size_t nblocks);
+void gf128hash_mul_x86_fpu(struct polyval_elem *acc,
+ const struct gf128hash_key_x86 *key);
+
#endif /* _LIB_CRYPTO_X86_GF128HASH_CPU_H */
diff --git a/lib/crypto/x86/gf128hash.h b/lib/crypto/x86/gf128hash.h
index 9fb826d62e829..b97e52a80da1f 100644
--- a/lib/crypto/x86/gf128hash.h
+++ b/lib/crypto/x86/gf128hash.h
@@ -99,6 +99,29 @@ __ghash_blocks_x86_fpu(struct polyval_elem *acc,
ghash_blocks_pclmul(acc, key, data, nblocks);
}
+/*
+ * The following two functions are exported to libaes for the x86 one-shot
+ * AES-GCM code to efficiently process the associated data. They assume that
+ * the caller is already in a kernel-mode FPU section and has already checked
+ * gf128hash_use_pclmul() (e.g., via a static_key). They also assume that
+ * gf128hash_mod_init_arch() has run already, via the initcall ordering.
+ */
+
+void gf128hash_mul_x86_fpu(struct polyval_elem *acc,
+ const struct gf128hash_key_x86 *key)
+{
+ __gf128hash_mul_x86_fpu(acc, key);
+}
+EXPORT_SYMBOL_FOR_MODULES(gf128hash_mul_x86_fpu, "libaes");
+
+void ghash_blocks_x86_fpu(struct polyval_elem *acc,
+ const struct gf128hash_key_x86 *key, const u8 *data,
+ size_t nblocks)
+{
+ __ghash_blocks_x86_fpu(acc, key, data, nblocks);
+}
+EXPORT_SYMBOL_FOR_MODULES(ghash_blocks_x86_fpu, "libaes");
+
/*
* Compute *out = *in * x^-64 mod G(x), where
* x^-64 = x^64 + x^63 + x^62 + x^57 (mod G(x)).
--
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 ` Eric Biggers [this message]
2026-11-29 20:02 ` [PATCH 09/11] lib/crypto: x86/aes-gcm: Migrate VAES+AVX2 optimized code into library 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-9-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 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.