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 10/11] lib/crypto: x86/aes-gcm: Migrate VAES+AVX512 optimized code into library
Date: Sun, 29 Nov 2026 12:02:44 -0800 [thread overview]
Message-ID: <20261129200245.146503-11-ebiggers@kernel.org> (raw)
In-Reply-To: <20261129200245.146503-1-ebiggers@kernel.org>
Migrate aes-gcm-vaes-avx512.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, leaving aesni-intel as an empty
module to be removed in the next commit.
This makes the AES-GCM library be optimized with VAES+AVX512, 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.
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_avx512'.
- Remove aes_gcm_precompute_vaes_avx512() and
aes_gcm_aad_update_vaes_avx512(). The equivalent functionality is now
handled in libgf128hash.
- aes_gcm_{enc,dec}_update_vaes_avx512():
- 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_avx512() using masked vmovdqu8, reading
key->authtag_len from the key struct.
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.
Finally, now that all x86 AES-GCM implementations have been migrated
into the library, increase the priority of "gcm-aes-lib" and
"rfc4106-gcm-aes-lib" to 500 on x86 to match the other x86 AES modes.
Benchmark results on AMD Ryzen 9 9950X (Zen 5) using aes_gcm_kunit
(before = uncombined library AES-CTR + GHASH fallback):
Length Enc before Enc after Dec before Dec after
------ ---------- --------- ---------- ---------
16 276 MB/s 465 MB/s 272 MB/s 614 MB/s
64 1062 MB/s 1935 MB/s 1094 MB/s 2403 MB/s
128 2116 MB/s 3866 MB/s 2121 MB/s 4948 MB/s
256 4226 MB/s 7621 MB/s 4159 MB/s 8977 MB/s
512 7342 MB/s 12609 MB/s 7849 MB/s 13496 MB/s
1024 11717 MB/s 17542 MB/s 11675 MB/s 18838 MB/s
1420 10548 MB/s 17082 MB/s 10360 MB/s 19754 MB/s
4096 17896 MB/s 24722 MB/s 17626 MB/s 26789 MB/s
16384 19560 MB/s 26937 MB/s 20104 MB/s 29057 MB/s
Signed-off-by: Eric Biggers <ebiggers@kernel.org>
---
arch/x86/crypto/Makefile | 3 +-
arch/x86/crypto/aesni-intel_glue.c | 548 ------------------
crypto/aes.c | 4 +-
lib/crypto/Makefile | 3 +-
.../crypto/x86}/aes-gcm-vaes-avx512.S | 374 ++----------
lib/crypto/x86/aes.h | 44 +-
6 files changed, 100 insertions(+), 876 deletions(-)
rename {arch/x86/crypto => lib/crypto/x86}/aes-gcm-vaes-avx512.S (74%)
diff --git a/arch/x86/crypto/Makefile b/arch/x86/crypto/Makefile
index b0e3660980f2f..7a443aa3ce4d7 100644
--- a/arch/x86/crypto/Makefile
+++ b/arch/x86/crypto/Makefile
@@ -40,8 +40,7 @@ obj-$(CONFIG_CRYPTO_AEGIS128_AESNI_SSE2) += aegis128-aesni.o
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-avx512.o
+aesni-intel-y := aesni-intel_glue.o
obj-$(CONFIG_CRYPTO_SM4_AESNI_AVX_X86_64) += sm4-aesni-avx-x86_64.o
sm4-aesni-avx-x86_64-y := sm4-aesni-avx-asm_64.o sm4_aesni_avx_glue.o
diff --git a/arch/x86/crypto/aesni-intel_glue.c b/arch/x86/crypto/aesni-intel_glue.c
index 8d969dfa8f896..93718bb95092a 100644
--- a/arch/x86/crypto/aesni-intel_glue.c
+++ b/arch/x86/crypto/aesni-intel_glue.c
@@ -17,544 +17,9 @@
* Copyright 2024 Google LLC
*/
-#include <linux/hardirq.h>
-#include <linux/types.h>
#include <linux/module.h>
#include <linux/err.h>
-#include <crypto/algapi.h>
-#include <crypto/aes.h>
-#include <crypto/b128ops.h>
-#include <crypto/gcm.h>
-#include <crypto/gf128mul.h>
#include <asm/cpu_device_id.h>
-#include <asm/simd.h>
-#include <crypto/scatterwalk.h>
-#include <crypto/internal/aead.h>
-#include <crypto/internal/simd.h>
-#include <crypto/internal/skcipher.h>
-#include <linux/jump_label.h>
-#include <linux/workqueue.h>
-#include <linux/spinlock.h>
-#include <linux/static_call.h>
-
-/* The common part of the x86_64 AES-GCM key struct */
-struct aes_gcm_key {
- /* Expanded AES key and the AES key length in bytes */
- struct aes_enckey aes_key;
-
- /* RFC4106 nonce (used only by the rfc4106 algorithms) */
- u32 rfc4106_nonce;
-};
-
-/* Key struct used by the VAES + AVX512 implementation of AES-GCM */
-struct aes_gcm_key_vaes_avx512 {
- /*
- * 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^16 through H^1. These are 128-bit values.
- * They all have an extra factor of x^-1 and are byte-reversed. This
- * array is aligned to a 64-byte boundary to make it naturally aligned
- * for 512-bit loads, which can improve performance. (The assembly code
- * doesn't *need* the alignment; this is just an optimization.)
- */
- u64 h_powers[16][2] __aligned(64);
-
- /* Three padding blocks required by the assembly code */
- u64 padding[3][2];
-};
-#define AES_GCM_KEY_VAES_AVX512(key) \
- container_of((key), struct aes_gcm_key_vaes_avx512, base)
-#define AES_GCM_KEY_VAES_AVX512_SIZE \
- (sizeof(struct aes_gcm_key_vaes_avx512) + (63 & ~(CRYPTO_MINALIGN - 1)))
-
-/*
- * These flags are passed to the AES-GCM helper functions to specify the
- * specific version of AES-GCM (RFC4106 or not), whether it's encryption or
- * decryption, and which assembly functions should be called. Assembly
- * functions are selected using flags instead of function pointers to avoid
- * indirect calls (which are very expensive on x86) regardless of inlining.
- */
-#define FLAG_RFC4106 BIT(0)
-#define FLAG_ENC BIT(1)
-#define FLAG_VAES_AVX512 BIT(4)
-
-static inline struct aes_gcm_key *
-aes_gcm_key_get(struct crypto_aead *tfm, int flags)
-{
- return PTR_ALIGN(crypto_aead_ctx(tfm), 64);
-}
-
-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)
-{
- aes_gcm_precompute_vaes_avx512(AES_GCM_KEY_VAES_AVX512(key));
-}
-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);
-
-static void aes_gcm_aad_update(const struct aes_gcm_key *key, u8 ghash_acc[16],
- const u8 *aad, int aadlen, int flags)
-{
- aes_gcm_aad_update_vaes_avx512(AES_GCM_KEY_VAES_AVX512(key), ghash_acc,
- aad, aadlen);
-}
-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_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);
-
-/* __always_inline to optimize out the branches based on @flags */
-static __always_inline void
-aes_gcm_update(const struct aes_gcm_key *key,
- const u32 le_ctr[4], u8 ghash_acc[16],
- const u8 *src, u8 *dst, int datalen, int flags)
-{
- if (flags & FLAG_ENC) {
- aes_gcm_enc_update_vaes_avx512(AES_GCM_KEY_VAES_AVX512(key),
- le_ctr, ghash_acc, src, dst,
- datalen);
- } else {
- 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_avx512(const struct aes_gcm_key_vaes_avx512 *key,
- const u32 le_ctr[4], u8 ghash_acc[16],
- u64 total_aadlen, u64 total_datalen);
-
-/* __always_inline to optimize out the branches based on @flags */
-static __always_inline void
-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)
-{
- 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_avx512(const struct aes_gcm_key_vaes_avx512 *key,
- const u32 le_ctr[4], const u8 ghash_acc[16],
- u64 total_aadlen, u64 total_datalen,
- const u8 tag[16], int taglen);
-
-/* __always_inline to optimize out the branches based on @flags */
-static __always_inline bool __must_check
-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)
-{
- return aes_gcm_dec_final_vaes_avx512(AES_GCM_KEY_VAES_AVX512(key),
- le_ctr, ghash_acc, total_aadlen,
- total_datalen, tag, taglen);
-}
-
-/*
- * This is the Integrity Check Value (aka the authentication tag) length and can
- * be 8, 12 or 16 bytes long.
- */
-static int common_rfc4106_set_authsize(struct crypto_aead *aead,
- unsigned int authsize)
-{
- switch (authsize) {
- case 8:
- case 12:
- case 16:
- break;
- default:
- return -EINVAL;
- }
-
- return 0;
-}
-
-static int generic_gcmaes_set_authsize(struct crypto_aead *tfm,
- unsigned int authsize)
-{
- switch (authsize) {
- case 4:
- case 8:
- case 12:
- case 13:
- case 14:
- case 15:
- case 16:
- break;
- default:
- return -EINVAL;
- }
-
- return 0;
-}
-
-/*
- * This is the setkey function for the x86_64 implementations of AES-GCM. It
- * saves the RFC4106 nonce if applicable, expands the AES key, and precomputes
- * powers of the hash key.
- *
- * To comply with the crypto_aead API, this has to be usable in no-SIMD context.
- * For that reason, this function includes a portable C implementation of the
- * needed logic. However, the portable C implementation is very slow, taking
- * about the same time as encrypting 37 KB of data. To be ready for users that
- * may set a key even somewhat frequently, we therefore also include a SIMD
- * assembly implementation, expanding the AES key using AES-NI and precomputing
- * the hash key powers using PCLMULQDQ or VPCLMULQDQ.
- */
-static int gcm_setkey(struct crypto_aead *tfm, const u8 *raw_key,
- unsigned int keylen, int flags)
-{
- struct aes_gcm_key *key = aes_gcm_key_get(tfm, flags);
- int err;
-
- if (flags & FLAG_RFC4106) {
- if (keylen < 4)
- return -EINVAL;
- keylen -= 4;
- key->rfc4106_nonce = get_unaligned_be32(raw_key + keylen);
- }
-
- /* The assembly code assumes the following offsets. */
- 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);
- static_assert(offsetof(struct aes_gcm_key_vaes_avx512, padding) == 576);
-
- err = aes_prepareenckey(&key->aes_key, raw_key, keylen);
- if (err)
- return err;
-
- if (likely(crypto_simd_usable())) {
- kernel_fpu_begin_mask(0);
- aes_gcm_precompute(key, flags);
- kernel_fpu_end();
- } else {
- static const u8 x_to_the_minus1[16] __aligned(__alignof__(be128)) = {
- [0] = 0xc2, [15] = 1
- };
- be128 h1 = {};
- be128 h;
- int i;
-
- /* Encrypt the all-zeroes block to get the hash key H^1 */
- aes_encrypt(&key->aes_key, (u8 *)&h1, (u8 *)&h1);
-
- /* Compute H^1 * x^-1 */
- h = h1;
- gf128mul_lle(&h, (const be128 *)x_to_the_minus1);
-
- /* Compute the needed key powers */
- 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;
-}
-
-/*
- * Initialize @ghash_acc, then pass all @assoclen bytes of associated data
- * (a.k.a. additional authenticated data) from @sg_src through the GHASH update
- * assembly function. The caller must be in a kernel-mode FPU section.
- */
-static void gcm_process_assoc(const struct aes_gcm_key *key, u8 ghash_acc[16],
- struct scatterlist *sg_src, unsigned int assoclen,
- int flags)
-{
- struct scatter_walk walk;
- /*
- * The assembly function requires that the length of any non-last
- * segment of associated data be a multiple of 16 bytes, so this
- * function does the buffering needed to achieve that.
- */
- unsigned int pos = 0;
- u8 buf[16];
-
- memset(ghash_acc, 0, 16);
- scatterwalk_start(&walk, sg_src);
-
- while (assoclen) {
- unsigned int orig_len_this_step = scatterwalk_next(
- &walk, assoclen);
- unsigned int len_this_step = orig_len_this_step;
- unsigned int len;
- const u8 *src = walk.addr;
-
- assoclen -= orig_len_this_step;
- if (unlikely(pos)) {
- len = min(len_this_step, 16 - pos);
- memcpy(&buf[pos], src, len);
- pos += len;
- src += len;
- len_this_step -= len;
- if (pos < 16)
- goto next;
- aes_gcm_aad_update(key, ghash_acc, buf, 16, flags);
- pos = 0;
- }
- len = len_this_step;
- if (unlikely(assoclen)) /* Not the last segment yet? */
- len = round_down(len, 16);
- aes_gcm_aad_update(key, ghash_acc, src, len, flags);
- src += len;
- len_this_step -= len;
- if (unlikely(len_this_step)) {
- memcpy(buf, src, len_this_step);
- pos = len_this_step;
- }
-next:
- scatterwalk_done_src(&walk, orig_len_this_step);
- if (need_resched()) {
- kernel_fpu_end();
- kernel_fpu_begin_mask(0);
- }
- }
- if (unlikely(pos))
- aes_gcm_aad_update(key, ghash_acc, buf, pos, flags);
-}
-
-
-/* __always_inline to optimize out the branches based on @flags */
-static __always_inline int
-gcm_crypt(struct aead_request *req, int flags)
-{
- struct crypto_aead *tfm = crypto_aead_reqtfm(req);
- const struct aes_gcm_key *key = aes_gcm_key_get(tfm, flags);
- unsigned int assoclen = req->assoclen;
- struct skcipher_walk walk;
- unsigned int nbytes;
- u8 ghash_acc[16]; /* GHASH accumulator */
- u32 le_ctr[4]; /* Counter in little-endian format */
- int taglen;
- int err;
-
- /* Initialize the counter and determine the associated data length. */
- le_ctr[0] = 2;
- if (flags & FLAG_RFC4106) {
- if (unlikely(assoclen != 16 && assoclen != 20))
- return -EINVAL;
- assoclen -= 8;
- le_ctr[1] = get_unaligned_be32(req->iv + 4);
- le_ctr[2] = get_unaligned_be32(req->iv + 0);
- le_ctr[3] = key->rfc4106_nonce; /* already byte-swapped */
- } else {
- le_ctr[1] = get_unaligned_be32(req->iv + 8);
- le_ctr[2] = get_unaligned_be32(req->iv + 4);
- le_ctr[3] = get_unaligned_be32(req->iv + 0);
- }
-
- /* Begin walking through the plaintext or ciphertext. */
- if (flags & FLAG_ENC)
- err = skcipher_walk_aead_encrypt(&walk, req, false);
- else
- err = skcipher_walk_aead_decrypt(&walk, req, false);
- if (err)
- return err;
-
- /*
- * Since the AES-GCM assembly code requires that at least three assembly
- * functions be called to process any message (this is needed to support
- * incremental updates cleanly), to reduce overhead we try to do all
- * three calls in the same kernel FPU section if possible. We close the
- * section and start a new one if there are multiple data segments or if
- * rescheduling is needed while processing the associated data.
- */
- kernel_fpu_begin_mask(0);
-
- /* Pass the associated data through GHASH. */
- gcm_process_assoc(key, ghash_acc, req->src, assoclen, flags);
-
- /* En/decrypt the data and pass the ciphertext through GHASH. */
- while (unlikely((nbytes = walk.nbytes) < walk.total)) {
- /*
- * Non-last segment. In this case, the assembly function
- * requires that the length be a multiple of 16 (AES_BLOCK_SIZE)
- * bytes. The needed buffering of up to 16 bytes is handled by
- * the skcipher_walk. Here we just need to round down to a
- * multiple of 16.
- */
- nbytes = round_down(nbytes, AES_BLOCK_SIZE);
- aes_gcm_update(key, le_ctr, ghash_acc, walk.src.virt.addr,
- walk.dst.virt.addr, nbytes, flags);
- le_ctr[0] += nbytes / AES_BLOCK_SIZE;
- kernel_fpu_end();
- err = skcipher_walk_done(&walk, walk.nbytes - nbytes);
- if (err)
- return err;
- kernel_fpu_begin_mask(0);
- }
- /* Last segment: process all remaining data. */
- aes_gcm_update(key, le_ctr, ghash_acc, walk.src.virt.addr,
- walk.dst.virt.addr, nbytes, flags);
- /*
- * The low word of the counter isn't used by the finalize, so there's no
- * need to increment it here.
- */
-
- /* Finalize */
- taglen = crypto_aead_authsize(tfm);
- if (flags & FLAG_ENC) {
- /* Finish computing the auth tag. */
- aes_gcm_enc_final(key, le_ctr, ghash_acc, assoclen,
- req->cryptlen, flags);
-
- /* Store the computed auth tag in the dst scatterlist. */
- scatterwalk_map_and_copy(ghash_acc, req->dst, req->assoclen +
- req->cryptlen, taglen, 1);
- } else {
- unsigned int datalen = req->cryptlen - taglen;
- u8 tag[16];
-
- /* Get the transmitted auth tag from the src scatterlist. */
- scatterwalk_map_and_copy(tag, req->src, req->assoclen + datalen,
- taglen, 0);
- /*
- * Finish computing the auth tag and compare it to the
- * transmitted one. The assembly function does the actual tag
- * comparison. Here, just check the boolean result.
- */
- if (!aes_gcm_dec_final(key, le_ctr, ghash_acc, assoclen,
- datalen, tag, taglen, flags))
- err = -EBADMSG;
- }
- kernel_fpu_end();
- if (nbytes)
- skcipher_walk_done(&walk, 0);
- return err;
-}
-
-#define DEFINE_GCM_ALGS(suffix, flags, generic_driver_name, rfc_driver_name, \
- ctxsize, priority) \
- \
-static int gcm_setkey_##suffix(struct crypto_aead *tfm, const u8 *raw_key, \
- unsigned int keylen) \
-{ \
- return gcm_setkey(tfm, raw_key, keylen, (flags)); \
-} \
- \
-static int gcm_encrypt_##suffix(struct aead_request *req) \
-{ \
- return gcm_crypt(req, (flags) | FLAG_ENC); \
-} \
- \
-static int gcm_decrypt_##suffix(struct aead_request *req) \
-{ \
- return gcm_crypt(req, (flags)); \
-} \
- \
-static int rfc4106_setkey_##suffix(struct crypto_aead *tfm, const u8 *raw_key, \
- unsigned int keylen) \
-{ \
- return gcm_setkey(tfm, raw_key, keylen, (flags) | FLAG_RFC4106); \
-} \
- \
-static int rfc4106_encrypt_##suffix(struct aead_request *req) \
-{ \
- return gcm_crypt(req, (flags) | FLAG_RFC4106 | FLAG_ENC); \
-} \
- \
-static int rfc4106_decrypt_##suffix(struct aead_request *req) \
-{ \
- return gcm_crypt(req, (flags) | FLAG_RFC4106); \
-} \
- \
-static struct aead_alg aes_gcm_algs_##suffix[] = { { \
- .setkey = gcm_setkey_##suffix, \
- .setauthsize = generic_gcmaes_set_authsize, \
- .encrypt = gcm_encrypt_##suffix, \
- .decrypt = gcm_decrypt_##suffix, \
- .ivsize = GCM_AES_IV_SIZE, \
- .chunksize = AES_BLOCK_SIZE, \
- .maxauthsize = 16, \
- .base = { \
- .cra_name = "gcm(aes)", \
- .cra_driver_name = generic_driver_name, \
- .cra_priority = (priority), \
- .cra_blocksize = 1, \
- .cra_ctxsize = (ctxsize), \
- .cra_module = THIS_MODULE, \
- }, \
-}, { \
- .setkey = rfc4106_setkey_##suffix, \
- .setauthsize = common_rfc4106_set_authsize, \
- .encrypt = rfc4106_encrypt_##suffix, \
- .decrypt = rfc4106_decrypt_##suffix, \
- .ivsize = GCM_RFC4106_IV_SIZE, \
- .chunksize = AES_BLOCK_SIZE, \
- .maxauthsize = 16, \
- .base = { \
- .cra_name = "rfc4106(gcm(aes))", \
- .cra_driver_name = rfc_driver_name, \
- .cra_priority = (priority), \
- .cra_blocksize = 1, \
- .cra_ctxsize = (ctxsize), \
- .cra_module = THIS_MODULE, \
- }, \
-} }
-
-/* aes_gcm_algs_vaes_avx512 */
-DEFINE_GCM_ALGS(vaes_avx512, FLAG_VAES_AVX512,
- "generic-gcm-vaes-avx512", "rfc4106-gcm-vaes-avx512",
- AES_GCM_KEY_VAES_AVX512_SIZE, 800);
-
-static int __init register_avx_algs(void)
-{
- if (!boot_cpu_has(X86_FEATURE_AVX))
- return 0;
- /*
- * Note: not all the algorithms registered below actually require
- * VPCLMULQDQ. But in practice every CPU with VAES also has VPCLMULQDQ.
- * Similarly, the assembler support was added at about the same time.
- * For simplicity, just always check for VAES and VPCLMULQDQ together.
- */
- if (!boot_cpu_has(X86_FEATURE_AVX2) ||
- !boot_cpu_has(X86_FEATURE_VAES) ||
- !boot_cpu_has(X86_FEATURE_VPCLMULQDQ) ||
- !boot_cpu_has(X86_FEATURE_PCLMULQDQ))
- return 0;
-
- if (!boot_cpu_has(X86_FEATURE_AVX512BW) ||
- !boot_cpu_has(X86_FEATURE_AVX512VL) ||
- !boot_cpu_has(X86_FEATURE_BMI2))
- return 0;
-
- if (boot_cpu_has(X86_FEATURE_PREFER_YMM)) {
- int i;
-
- for (i = 0; i < ARRAY_SIZE(aes_gcm_algs_vaes_avx512); i++)
- aes_gcm_algs_vaes_avx512[i].base.cra_priority = 1;
- }
-
- return crypto_register_aeads(aes_gcm_algs_vaes_avx512,
- ARRAY_SIZE(aes_gcm_algs_vaes_avx512));
-}
-
-#define unregister_aeads(A) \
- if (refcount_read(&(A)[0].base.cra_refcnt) != 0) \
- crypto_unregister_aeads((A), ARRAY_SIZE(A))
-
-static void unregister_avx_algs(void)
-{
- unregister_aeads(aes_gcm_algs_vaes_avx512);
-}
static const struct x86_cpu_id aesni_cpu_id[] = {
X86_MATCH_FEATURE(X86_FEATURE_AES, NULL),
@@ -564,25 +29,14 @@ MODULE_DEVICE_TABLE(x86cpu, aesni_cpu_id);
static int __init aesni_init(void)
{
- int err;
-
if (!x86_match_cpu(aesni_cpu_id))
return -ENODEV;
- err = register_avx_algs();
- if (err)
- goto unregister_avx;
-
return 0;
-
-unregister_avx:
- unregister_avx_algs();
- return err;
}
static void __exit aesni_exit(void)
{
- unregister_avx_algs();
}
module_init(aesni_init);
@@ -590,5 +44,3 @@ module_exit(aesni_exit);
MODULE_DESCRIPTION("AES-GCM, optimized with AES-NI or VAES instructions");
MODULE_LICENSE("GPL");
-MODULE_ALIAS_CRYPTO("gcm(aes)");
-MODULE_ALIAS_CRYPTO("rfc4106(gcm(aes))");
diff --git a/crypto/aes.c b/crypto/aes.c
index e360756385cdf..08ac2faa6cb71 100644
--- a/crypto/aes.c
+++ b/crypto/aes.c
@@ -1208,7 +1208,7 @@ static struct aead_alg aead_algs[] = {
{
.base.cra_name = "gcm(aes)",
.base.cra_driver_name = "gcm-aes-lib",
- .base.cra_priority = 110,
+ .base.cra_priority = IS_ENABLED(CONFIG_X86) ? 500 : 110,
.base.cra_blocksize = 1,
.base.cra_ctxsize = sizeof(struct aes_gcm_key),
.base.cra_module = THIS_MODULE,
@@ -1223,7 +1223,7 @@ static struct aead_alg aead_algs[] = {
{
.base.cra_name = "rfc4106(gcm(aes))",
.base.cra_driver_name = "rfc4106-gcm-aes-lib",
- .base.cra_priority = 110,
+ .base.cra_priority = IS_ENABLED(CONFIG_X86) ? 500 : 110,
.base.cra_blocksize = 1,
.base.cra_ctxsize = sizeof(struct aes_rfc4106_key),
.base.cra_module = THIS_MODULE,
diff --git a/lib/crypto/Makefile b/lib/crypto/Makefile
index 6e2d264f12492..ccd702f58f3d6 100644
--- a/lib/crypto/Makefile
+++ b/lib/crypto/Makefile
@@ -97,7 +97,8 @@ 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 \
- x86/aes-gcm-vaes-avx2.o
+ x86/aes-gcm-vaes-avx2.o \
+ x86/aes-gcm-vaes-avx512.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-avx512.S b/lib/crypto/x86/aes-gcm-vaes-avx512.S
similarity index 74%
rename from arch/x86/crypto/aes-gcm-vaes-avx512.S
rename to lib/crypto/x86/aes-gcm-vaes-avx512.S
index 516747db46592..70b9adc02b41c 100644
--- a/arch/x86/crypto/aes-gcm-vaes-avx512.S
+++ b/lib/crypto/x86/aes-gcm-vaes-avx512.S
@@ -66,10 +66,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
@@ -81,6 +77,10 @@
.Linc_4blocks:
.octa 4
+ // Big-endian 1, for constructing J_0 = nonce || be32(1).
+.Lbe_one:
+ .long 0x01000000
+
// Number of powers of the hash key stored in the key struct. The powers are
// stored from highest (H^NUM_H_POWERS) to lowest (H^1).
#define NUM_H_POWERS 16
@@ -92,7 +92,7 @@
#define OFFSETOF_AESROUNDKEYS 16
// Offset to start of hash key powers array in the key struct
-#define OFFSETOF_H_POWERS 320
+#define OFFSETOF_H_POWERS 256
// Offset to end of hash key powers array in the key struct.
//
@@ -101,6 +101,7 @@
// blocks remain, we load the 4 values [H^2, H^1, 0, 0]. The most padding
// blocks needed is 3, which occurs if [H^1, 0, 0, 0] is loaded.
#define OFFSETOFEND_H_POWERS (OFFSETOF_H_POWERS + (NUM_H_POWERS * 16))
+#define OFFSETOF_AUTHTAG_LEN (OFFSETOF_H_POWERS + 320)
.text
@@ -232,14 +233,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, t1, t2, t3
@@ -263,126 +256,6 @@
vpternlogd $0x96, \t0, \mi, \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
- vpxord \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
- vpternlogd $0x96, \t0, \t1, \dst // Fold MI into HI
-.endm
-
-// void aes_gcm_precompute_vaes_avx512(struct aes_gcm_key_vaes_avx512 *key);
-//
-// Given the expanded AES key |key->base.aes_key|, derive the GHASH subkey and
-// initialize |key->h_powers| and |key->padding|.
-SYM_FUNC_START(aes_gcm_precompute_vaes_avx512)
-
- // Function arguments
- .set KEY, %rdi
-
- // Additional local variables.
- // %zmm[0-2] and %rax are used as temporaries.
- .set POWERS_PTR, %rsi
- .set RNDKEYLAST_PTR, %rdx
- .set H_CUR, %zmm3
- .set H_CUR_YMM, %ymm3
- .set H_CUR_XMM, %xmm3
- .set H_INC, %zmm4
- .set H_INC_YMM, %ymm4
- .set H_INC_XMM, %xmm4
- .set GFPOLY, %zmm5
- .set GFPOLY_YMM, %ymm5
- .set GFPOLY_XMM, %xmm5
-
- // Get pointer to lowest set of key powers (located at end of array).
- lea OFFSETOFEND_H_POWERS-64(KEY), POWERS_PTR
-
- // 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), %xmm0
- add $OFFSETOF_AESROUNDKEYS+16, KEY
-1:
- vaesenc (KEY), %xmm0, %xmm0
- add $16, KEY
- cmp KEY, RNDKEYLAST_PTR
- jne 1b
- vaesenclast (RNDKEYLAST_PTR), %xmm0, %xmm0
-
- // Reflect the bytes of the raw hash subkey.
- vpshufb .Lbswap_mask(%rip), %xmm0, H_CUR_XMM
-
- // Zeroize the padding blocks.
- vpxor %xmm0, %xmm0, %xmm0
- vmovdqu %ymm0, 64(POWERS_PTR)
- vmovdqu %xmm0, 64+2*16(POWERS_PTR)
-
- // Finish preprocessing the first key power, H^1. Since this GHASH
- // implementation operates directly on values with the backwards bit
- // order specified by the GCM standard, it's necessary to preprocess the
- // raw key as follows. First, reflect its bytes. Second, multiply it
- // by x^-1 mod x^128 + x^7 + x^2 + x + 1 (if using the backwards
- // interpretation of polynomial coefficients), which can also be
- // interpreted as multiplication by x mod x^128 + x^127 + x^126 + x^121
- // + 1 using the alternative, natural interpretation of polynomial
- // coefficients. For details, see the comment above _ghash_mul_step.
- //
- // Either way, for the multiplication the concrete operation performed
- // is a left shift of the 128-bit value by 1 bit, then an XOR with (0xc2
- // << 120) | 1 if a 1 bit was carried out. However, there's no 128-bit
- // wide shift instruction, so instead double each of the two 64-bit
- // halves and incorporate the internal carry bit into the value XOR'd.
- vpshufd $0xd3, H_CUR_XMM, %xmm0
- vpsrad $31, %xmm0, %xmm0
- vpaddq H_CUR_XMM, H_CUR_XMM, H_CUR_XMM
- // H_CUR_XMM ^= xmm0 & gfpoly_and_internal_carrybit
- vpternlogd $0x78, .Lgfpoly_and_internal_carrybit(%rip), %xmm0, H_CUR_XMM
-
- // Load the gfpoly constant.
- vbroadcasti32x4 .Lgfpoly(%rip), GFPOLY
-
- // Square H^1 to get H^2.
- //
- // Note that as with H^1, all higher key powers also need an extra
- // factor of x^-1 (or x using the natural interpretation). Nothing
- // special needs to be done to make this happen, though: H^1 * H^1 would
- // end up with two factors of x^-1, but the multiplication consumes one.
- // So the product H^2 ends up with the desired one factor of x^-1.
- _ghash_square H_CUR_XMM, H_INC_XMM, GFPOLY_XMM, %xmm0, %xmm1
-
- // Create H_CUR_YMM = [H^2, H^1] and H_INC_YMM = [H^2, H^2].
- vinserti128 $1, H_CUR_XMM, H_INC_YMM, H_CUR_YMM
- vinserti128 $1, H_INC_XMM, H_INC_YMM, H_INC_YMM
-
- // Create H_CUR = [H^4, H^3, H^2, H^1] and H_INC = [H^4, H^4, H^4, H^4].
- _ghash_mul H_INC_YMM, H_CUR_YMM, H_INC_YMM, GFPOLY_YMM, \
- %ymm0, %ymm1, %ymm2
- vinserti64x4 $1, H_CUR_YMM, H_INC, H_CUR
- vshufi64x2 $0, H_INC, H_INC, H_INC
-
- // Store the lowest set of key powers.
- vmovdqu8 H_CUR, (POWERS_PTR)
-
- // Compute and store the remaining key powers.
- // Repeatedly multiply [H^(i+3), H^(i+2), H^(i+1), H^i] by
- // [H^4, H^4, H^4, H^4] to get [H^(i+7), H^(i+6), H^(i+5), H^(i+4)].
- mov $3, %eax
-.Lprecompute_next:
- sub $64, POWERS_PTR
- _ghash_mul H_INC, H_CUR, H_CUR, GFPOLY, %zmm0, %zmm1, %zmm2
- vmovdqu8 H_CUR, (POWERS_PTR)
- dec %eax
- jnz .Lprecompute_next
-
- vzeroupper // This is needed after using ymm or zmm registers.
- RET
-SYM_FUNC_END(aes_gcm_precompute_vaes_avx512)
-
// XOR together the 128-bit lanes of \src (whose low lane is \src_xmm) and store
// the result in \dst_xmm. This implicitly zeroizes the other lanes of dst.
.macro _horizontal_xor src, src_xmm, dst_xmm, t0_xmm, t1_xmm, t2_xmm
@@ -482,139 +355,6 @@ SYM_FUNC_END(aes_gcm_precompute_vaes_avx512)
.endr
.endm
-// void aes_gcm_aad_update_vaes_avx512(const struct aes_gcm_key_vaes_avx512 *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_avx512)
-
- // Function arguments
- .set KEY, %rdi
- .set GHASH_ACC_PTR, %rsi
- .set AAD, %rdx
- .set AADLEN, %ecx
- .set AADLEN64, %rcx // Zero-extend AADLEN before using!
-
- // Additional local variables.
- // %rax and %k1 are used as temporary registers.
- .set GHASHDATA0, %zmm0
- .set GHASHDATA0_XMM, %xmm0
- .set GHASHDATA1, %zmm1
- .set GHASHDATA1_XMM, %xmm1
- .set GHASHDATA2, %zmm2
- .set GHASHDATA2_XMM, %xmm2
- .set GHASHDATA3, %zmm3
- .set BSWAP_MASK, %zmm4
- .set BSWAP_MASK_XMM, %xmm4
- .set GHASH_ACC, %zmm5
- .set GHASH_ACC_XMM, %xmm5
- .set H_POW4, %zmm6
- .set H_POW3, %zmm7
- .set H_POW2, %zmm8
- .set H_POW1, %zmm9
- .set H_POW1_XMM, %xmm9
- .set GFPOLY, %zmm10
- .set GFPOLY_XMM, %xmm10
- .set GHASHTMP0, %zmm11
- .set GHASHTMP1, %zmm12
- .set GHASHTMP2, %zmm13
-
- // Load the GHASH accumulator.
- vmovdqu (GHASH_ACC_PTR), GHASH_ACC_XMM
-
- // Check for the common case of AADLEN <= 16, as well as AADLEN == 0.
- cmp $16, AADLEN
- jg .Laad_more_than_16bytes
- test AADLEN, AADLEN
- jz .Laad_done
-
- // Fast path: update GHASH with 1 <= AADLEN <= 16 bytes of AAD.
- vmovdqu .Lbswap_mask(%rip), BSWAP_MASK_XMM
- vmovdqu .Lgfpoly(%rip), GFPOLY_XMM
- mov $-1, %eax
- bzhi AADLEN, %eax, %eax
- kmovd %eax, %k1
- vmovdqu8 (AAD), GHASHDATA0_XMM{%k1}{z}
- vmovdqu OFFSETOFEND_H_POWERS-16(KEY), H_POW1_XMM
- vpshufb BSWAP_MASK_XMM, GHASHDATA0_XMM, GHASHDATA0_XMM
- vpxor GHASHDATA0_XMM, GHASH_ACC_XMM, GHASH_ACC_XMM
- _ghash_mul H_POW1_XMM, GHASH_ACC_XMM, GHASH_ACC_XMM, GFPOLY_XMM, \
- GHASHDATA0_XMM, GHASHDATA1_XMM, GHASHDATA2_XMM
- jmp .Laad_done
-
-.Laad_more_than_16bytes:
- vbroadcasti32x4 .Lbswap_mask(%rip), BSWAP_MASK
- vbroadcasti32x4 .Lgfpoly(%rip), GFPOLY
-
- // If AADLEN >= 256, update GHASH with 256 bytes of AAD at a time.
- sub $256, AADLEN
- jl .Laad_loop_4x_done
- vmovdqu8 OFFSETOFEND_H_POWERS-4*64(KEY), H_POW4
- vmovdqu8 OFFSETOFEND_H_POWERS-3*64(KEY), H_POW3
- vmovdqu8 OFFSETOFEND_H_POWERS-2*64(KEY), H_POW2
- vmovdqu8 OFFSETOFEND_H_POWERS-1*64(KEY), H_POW1
-.Laad_loop_4x:
- vmovdqu8 0*64(AAD), GHASHDATA0
- vmovdqu8 1*64(AAD), GHASHDATA1
- vmovdqu8 2*64(AAD), GHASHDATA2
- vmovdqu8 3*64(AAD), GHASHDATA3
- _ghash_4x
- add $256, AAD
- sub $256, AADLEN
- jge .Laad_loop_4x
-.Laad_loop_4x_done:
-
- // If AADLEN >= 64, update GHASH with 64 bytes of AAD at a time.
- add $192, AADLEN
- jl .Laad_loop_1x_done
- vmovdqu8 OFFSETOFEND_H_POWERS-1*64(KEY), H_POW1
-.Laad_loop_1x:
- vmovdqu8 (AAD), GHASHDATA0
- vpshufb BSWAP_MASK, GHASHDATA0, GHASHDATA0
- vpxord GHASHDATA0, GHASH_ACC, GHASH_ACC
- _ghash_mul H_POW1, GHASH_ACC, GHASH_ACC, GFPOLY, \
- GHASHDATA0, GHASHDATA1, GHASHDATA2
- _horizontal_xor GHASH_ACC, GHASH_ACC_XMM, GHASH_ACC_XMM, \
- GHASHDATA0_XMM, GHASHDATA1_XMM, GHASHDATA2_XMM
- add $64, AAD
- sub $64, AADLEN
- jge .Laad_loop_1x
-.Laad_loop_1x_done:
-
- // Update GHASH with the remaining 0 <= AADLEN < 64 bytes of AAD.
- add $64, AADLEN
- jz .Laad_done
- mov $-1, %rax
- bzhi AADLEN64, %rax, %rax
- kmovq %rax, %k1
- vmovdqu8 (AAD), GHASHDATA0{%k1}{z}
- neg AADLEN64
- and $~15, AADLEN64 // -round_up(AADLEN, 16)
- vmovdqu8 OFFSETOFEND_H_POWERS(KEY,AADLEN64), H_POW1
- vpshufb BSWAP_MASK, GHASHDATA0, GHASHDATA0
- vpxord GHASHDATA0, GHASH_ACC, GHASH_ACC
- _ghash_mul H_POW1, GHASH_ACC, GHASH_ACC, GFPOLY, \
- GHASHDATA0, GHASHDATA1, GHASHDATA2
- _horizontal_xor GHASH_ACC, GHASH_ACC_XMM, GHASH_ACC_XMM, \
- GHASHDATA0_XMM, GHASHDATA1_XMM, GHASHDATA2_XMM
-
-.Laad_done:
- // Store the updated GHASH accumulator back to memory.
- vmovdqu GHASH_ACC_XMM, (GHASH_ACC_PTR)
-
- vzeroupper // This is needed after using ymm or zmm registers.
- RET
-SYM_FUNC_END(aes_gcm_aad_update_vaes_avx512)
-
// Do one non-last round of AES encryption on the blocks in %zmm[0-3] using the
// round key that has been broadcast to all 128-bit lanes of \round_key.
.macro _vaesenc_4x round_key
@@ -671,9 +411,11 @@ SYM_FUNC_END(aes_gcm_aad_update_vaes_avx512)
vmovdqu8 GHASHDATA3, 3*64(DST)
.endm
-// void aes_gcm_{enc,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);
+// void aes_gcm_{enc,dec}_update_vaes_avx512(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
@@ -686,26 +428,25 @@ SYM_FUNC_END(aes_gcm_aad_update_vaes_avx512)
// 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 GHASH_ACC_PTR, %rdx
- .set SRC, %rcx
- .set DST, %r8
- .set DATALEN, %r9d
- .set DATALEN64, %r9 // Zero-extend DATALEN before using!
+ .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
// Additional local variables
- // %rax and %k1 are used as temporary registers. LE_CTR_PTR is also
+ // %rax and %k1 are used as temporary registers. BE_CTR_PTR is also
// available as a temporary register after the counter is loaded.
// AES key length in bytes
@@ -785,7 +526,8 @@ SYM_FUNC_END(aes_gcm_aad_update_vaes_avx512)
// Load the GHASH accumulator and the starting counter.
vmovdqu (GHASH_ACC_PTR), GHASH_ACC_XMM
- vbroadcasti32x4 (LE_CTR_PTR), LE_CTR
+ vbroadcasti32x4 (BE_CTR_PTR), LE_CTR
+ vpshufb BSWAP_MASK, LE_CTR, LE_CTR
// Load the AES key length in bytes.
movl OFFSETOF_AESKEYLEN(KEY), AESKEYLEN
@@ -807,8 +549,7 @@ SYM_FUNC_END(aes_gcm_aad_update_vaes_avx512)
// that processes 256 bytes of data at a time. Otherwise skip it.
//
// Pre-subtracting 256 from DATALEN saves an instruction from the main
- // loop and also ensures that at least one write always occurs to
- // DATALEN, zero-extending it and allowing DATALEN64 to be used later.
+ // loop.
sub $256, DATALEN
jl .Lcrypt_loop_4x_done\@
@@ -930,8 +671,8 @@ SYM_FUNC_END(aes_gcm_aad_update_vaes_avx512)
// 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.
- mov DATALEN, %eax
+ .set POWERS_PTR, BE_CTR_PTR // BE_CTR_PTR is free to be reused.
+ mov DATALEN32, %eax
neg %rax
and $~15, %rax // -round_up(DATALEN, 16)
lea OFFSETOFEND_H_POWERS(KEY,%rax), POWERS_PTR
@@ -953,7 +694,7 @@ SYM_FUNC_END(aes_gcm_aad_update_vaes_avx512)
// DATALEN >= 64, otherwise DATALEN 1's. Do this branchlessly using the
// bzhi instruction from BMI2. (This relies on DATALEN <= 255.)
mov $-1, %rax
- bzhi DATALEN64, %rax, %rax
+ bzhi DATALEN, %rax, %rax
kmovq %rax, %k1
// Encrypt a vector of counter blocks. This does not need to be masked.
@@ -1014,14 +755,16 @@ SYM_FUNC_END(aes_gcm_aad_update_vaes_avx512)
RET
.endm
-// void aes_gcm_enc_final_vaes_avx512(const struct aes_gcm_key_vaes_avx512 *key,
-// const u32 le_ctr[4], u8 ghash_acc[16],
-// u64 total_aadlen, u64 total_datalen);
-// bool aes_gcm_dec_final_vaes_avx512(const struct aes_gcm_key_vaes_avx512 *key,
-// const u32 le_ctr[4],
-// const u8 ghash_acc[16],
+// void aes_gcm_enc_final_vaes_avx512(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_avx512(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
@@ -1029,31 +772,30 @@ SYM_FUNC_END(aes_gcm_aad_update_vaes_avx512)
// |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)
// Additional local variables.
// %rax, %xmm0-%xmm3, and %k1 are used as temporary registers.
+ .set TAGLEN, %r10d
.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
@@ -1062,11 +804,6 @@ SYM_FUNC_END(aes_gcm_aad_update_vaes_avx512)
// Load the AES key length in bytes.
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
-
// Build the lengths block and XOR it with the GHASH accumulator.
// Although the lengths block is defined as the AAD length followed by
// the en/decrypted data length, both in big-endian byte order, a byte
@@ -1081,20 +818,21 @@ SYM_FUNC_END(aes_gcm_aad_update_vaes_avx512)
// Load the first hash key power (H^1), which is stored last.
vmovdqu8 OFFSETOFEND_H_POWERS-16(KEY), H_POW1
-.if !\enc
// Prepare a mask of TAGLEN one bits.
- movl 8(%rsp), TAGLEN
+ // authtag_len is size_t with value in 4..16, so movl suffices here.
+ movl OFFSETOF_AUTHTAG_LEN(KEY), TAGLEN
mov $-1, %eax
bzhi TAGLEN, %eax, %eax
kmovd %eax, %k1
-.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.
@@ -1137,9 +875,9 @@ SYM_FUNC_END(aes_gcm_aad_update_vaes_avx512)
.if \enc
vpxor (%rax), GHASH_ACC, %xmm1
vaesenclast %xmm1, %xmm0, GHASH_ACC
- vmovdqu GHASH_ACC, (GHASH_ACC_PTR)
+ vmovdqu8 GHASH_ACC, (TAG){%k1}
.else
- vmovdqu (TAG), %xmm1
+ vmovdqu8 (TAG), %xmm1{%k1}{z}
vpternlogd $0x96, (%rax), GHASH_ACC, %xmm1
vaesenclast %xmm1, %xmm0, %xmm0
xor %eax, %eax
diff --git a/lib/crypto/x86/aes.h b/lib/crypto/x86/aes.h
index 97035b783ec4a..e0c65904eef09 100644
--- a/lib/crypto/x86/aes.h
+++ b/lib/crypto/x86/aes.h
@@ -412,6 +412,8 @@ static bool aes_xts_decrypt_arch(u8 *dst, const u8 *src, size_t len,
/* 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_powers16) == 0);
+static_assert(offsetof(struct gf128hash_key_x86, padding) == 256);
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);
@@ -448,6 +450,14 @@ 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_update_vaes_avx512(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_avx512(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,
@@ -476,6 +486,16 @@ bool aes_gcm_dec_final_vaes_avx2(const struct aes_gcm_key *key,
const struct polyval_elem *ghash_acc,
u64 total_aadlen, u64 total_datalen,
const u8 *authtag);
+void aes_gcm_enc_final_vaes_avx512(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_avx512(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
@@ -492,7 +512,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_vaes_avx2)) {
+ if (static_branch_likely(&have_vaes_avx512)) {
+ if (enc)
+ aes_gcm_enc_update_vaes_avx512(
+ dst, src, n, ghash_acc, ctr32, key);
+ else
+ aes_gcm_dec_update_vaes_avx512(
+ dst, src, n, ghash_acc, ctr32, key);
+ } else if (static_branch_likely(&have_vaes_avx2)) {
if (enc)
aes_gcm_enc_update_vaes_avx2(
dst, src, n, ghash_acc, ctr32, key);
@@ -534,7 +561,6 @@ 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_avx512) ||
unlikely(!irq_fpu_usable()))
return false;
@@ -599,7 +625,6 @@ 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_avx512) ||
unlikely(!irq_fpu_usable()))
return false;
@@ -616,7 +641,11 @@ aes_gcm_crypt_x86(u8 *dst, const u8 *src, size_t data_len, u8 *authtag,
key, enc);
if (enc) {
- if (static_branch_likely(&have_vaes_avx2))
+ if (static_branch_likely(&have_vaes_avx512))
+ aes_gcm_enc_final_vaes_avx512(key, nonce, &ghash_acc,
+ ad_len, data_len,
+ authtag);
+ else 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))
@@ -628,7 +657,12 @@ aes_gcm_crypt_x86(u8 *dst, const u8 *src, size_t data_len, u8 *authtag,
} else {
bool valid;
- if (static_branch_likely(&have_vaes_avx2))
+ if (static_branch_likely(&have_vaes_avx512))
+ valid = aes_gcm_dec_final_vaes_avx512(key, nonce,
+ &ghash_acc,
+ ad_len, data_len,
+ authtag);
+ else if (static_branch_likely(&have_vaes_avx2))
valid = aes_gcm_dec_final_vaes_avx2(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 ` [PATCH 09/11] lib/crypto: x86/aes-gcm: Migrate VAES+AVX2 " Eric Biggers
2026-11-29 20:02 ` Eric Biggers [this message]
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-11-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.