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 04/11] crypto: aes - Add linear fast path for AES-GCM
Date: Sun, 29 Nov 2026 12:02:38 -0800 [thread overview]
Message-ID: <20261129200245.146503-5-ebiggers@kernel.org> (raw)
In-Reply-To: <20261129200245.146503-1-ebiggers@kernel.org>
The "gcm(aes)" crypto_aead implementations in arch/x86/crypto/ currently
have several optimizations that reduce overhead for short messages, such
as using a single kernel-mode FPU section in most cases, and comparing
authentication tags in assembly code.
gcm-aes-lib currently executes each aead_request using the incremental
AES-GCM API, which is less efficient.
Therefore, add a "fast path" similar to what the xts-aes-lib has: if
!CONFIG_HIGHMEM, check if the associated data, source data, destination
data, and authentication tag are each linear, and if so, call
aes_gcm_encrypt() or aes_gcm_decrypt().
This allows the library to dispatch the operation to
aes_gcm_encrypt_arch() and aes_gcm_decrypt_arch().
Signed-off-by: Eric Biggers <ebiggers@kernel.org>
---
crypto/aes.c | 192 ++++++++++++++++++++++++++++++++++++++++++++++++---
1 file changed, 184 insertions(+), 8 deletions(-)
diff --git a/crypto/aes.c b/crypto/aes.c
index c4541f5bc6e32..e360756385cdf 100644
--- a/crypto/aes.c
+++ b/crypto/aes.c
@@ -787,9 +787,97 @@ static void aes_gcm_decrypt_update_helper(u8 *dst, const u8 *src,
aes_gcm_decrypt_update(ctx, dst, src, len);
}
-static int crypto_aes_gcm_encrypt_common(struct aead_request *req,
- const struct aes_gcm_key *key,
- u8 iv[12], unsigned int assoclen)
+/*
+ * Given a scatterlist @sg containing @assoclen bytes of associated data,
+ * @data_len bytes of en/decrypted data, and @tag_len bytes of authentication
+ * tag, check whether each of those regions (when non-empty) is contiguous in
+ * low memory. Each region may share a scatterlist element with the previous
+ * region or begin in the immediately following scatterlist element.
+ *
+ * This enables a fast path for the common case where an AEAD request's regions
+ * are linear in low memory, allowing crypto_aes_gcm_{en,de}crypt_common() to
+ * call the one-shot aes_gcm_encrypt() / aes_gcm_decrypt() library functions
+ * directly, avoiding the overhead of incremental processing.
+ */
+static __always_inline bool
+aead_sg_get_linear(struct scatterlist *sg, unsigned int assoclen,
+ unsigned int data_len, unsigned int tag_len,
+ const u8 **ad_out, u8 **data_out,
+ struct scatterlist **data_sg_out, unsigned int *data_off_out,
+ u8 **tag_out, struct scatterlist **tag_sg_out,
+ unsigned int *tag_off_out)
+{
+ unsigned int off = 0;
+ u8 *virt = NULL;
+
+ if (IS_ENABLED(CONFIG_HIGHMEM))
+ return false;
+
+ if (assoclen) {
+ if (!sg || sg->length < assoclen)
+ return false;
+ if (ad_out) {
+ virt = sg_virt(sg);
+ *ad_out = virt;
+ }
+ off = assoclen;
+ } else if (ad_out) {
+ *ad_out = NULL;
+ }
+
+ if (data_len) {
+ if (off && sg->length == off) {
+ sg = sg_next(sg);
+ off = 0;
+ virt = NULL;
+ }
+ if (!sg || sg->length < (u64)off + data_len)
+ return false;
+ if (!virt)
+ virt = sg_virt(sg);
+ *data_out = virt + off;
+ if (data_sg_out) {
+ *data_sg_out = sg;
+ *data_off_out = off;
+ }
+ off += data_len;
+ } else {
+ *data_out = NULL;
+ if (data_sg_out) {
+ *data_sg_out = NULL;
+ *data_off_out = 0;
+ }
+ }
+
+ if (tag_len) {
+ if (off && sg->length == off) {
+ sg = sg_next(sg);
+ off = 0;
+ virt = NULL;
+ }
+ if (!sg || sg->length < (u64)off + tag_len)
+ return false;
+ if (!virt)
+ virt = sg_virt(sg);
+ *tag_out = virt + off;
+ if (tag_sg_out) {
+ *tag_sg_out = sg;
+ *tag_off_out = off;
+ }
+ }
+
+ return true;
+}
+
+/*
+ * This handles AES-GCM encryption requests that use a nonlinear scatterlist
+ * layout or where HIGHMEM is enabled. It is explicitly 'noinline' to keep the
+ * on-stack buffers out of the stack frame of the fast path.
+ */
+static noinline int
+crypto_aes_gcm_encrypt_nonlinear(struct aead_request *req,
+ const struct aes_gcm_key *key, u8 iv[12],
+ unsigned int assoclen)
{
struct aes_gcm_ctx ctx;
u8 authtag[16];
@@ -805,20 +893,69 @@ static int crypto_aes_gcm_encrypt_common(struct aead_request *req,
return 0;
}
-static int crypto_aes_gcm_decrypt_common(struct aead_request *req,
+/*
+ * Common AES-GCM encryption helper for gcm(aes) and rfc4106(gcm(aes)).
+ * @assoclen is the number of bytes of associated data to authenticate, which is
+ * req->assoclen for gcm(aes) and req->assoclen - 8 for rfc4106(gcm(aes)) (where
+ * the 8-byte explicit IV sits between the AAD and the data in the scatterlist).
+ */
+static int crypto_aes_gcm_encrypt_common(struct aead_request *req,
const struct aes_gcm_key *key,
u8 iv[12], unsigned int assoclen)
+{
+ struct scatterlist *dst_sg, *tag_sg;
+ unsigned int dst_off, tag_off;
+ const u8 *ad;
+ u8 *src, *dst, *tag;
+
+ if (req->src == req->dst) {
+ if (unlikely(!aead_sg_get_linear(
+ req->src, req->assoclen, req->cryptlen,
+ key->authtag_len, &ad, &dst, &dst_sg, &dst_off,
+ &tag, &tag_sg, &tag_off)))
+ return crypto_aes_gcm_encrypt_nonlinear(req, key, iv,
+ assoclen);
+ src = dst;
+ } else {
+ if (unlikely(!aead_sg_get_linear(
+ req->src, req->assoclen, req->cryptlen, 0,
+ &ad, &src, NULL, NULL, NULL, NULL, NULL) ||
+ !aead_sg_get_linear(
+ req->dst, req->assoclen, req->cryptlen,
+ key->authtag_len, NULL, &dst, &dst_sg,
+ &dst_off, &tag, &tag_sg, &tag_off)))
+ return crypto_aes_gcm_encrypt_nonlinear(req, key, iv,
+ assoclen);
+ }
+
+ aes_gcm_encrypt(dst, src, req->cryptlen, tag, ad, assoclen, iv, key);
+ if (dst_sg == tag_sg) {
+ sg_flush_dcache(dst_sg, dst_off,
+ req->cryptlen + key->authtag_len);
+ } else {
+ if (dst_sg)
+ sg_flush_dcache(dst_sg, dst_off, req->cryptlen);
+ sg_flush_dcache(tag_sg, tag_off, key->authtag_len);
+ }
+ return 0;
+}
+
+/*
+ * This handles AES-GCM decryption requests that use a nonlinear scatterlist
+ * layout or where HIGHMEM is enabled. It is explicitly 'noinline' to keep the
+ * on-stack buffers out of the stack frame of the fast path.
+ */
+static noinline int
+crypto_aes_gcm_decrypt_nonlinear(struct aead_request *req,
+ const struct aes_gcm_key *key, u8 iv[12],
+ unsigned int assoclen, unsigned int data_len)
{
struct aes_gcm_ctx ctx;
- unsigned int data_len;
u8 authtag[16];
int err;
aes_gcm_init(&ctx, iv, key);
crypto_aes_gcm_auth_update(&ctx, req->src, assoclen);
-
- /* crypto_aead_decrypt() already checked cryptlen >= authtag_len. */
- data_len = req->cryptlen - key->authtag_len;
AES_CRYPT_SG(aes_gcm_decrypt_update_helper, req->dst, req->src,
data_len, req->assoclen, &ctx);
@@ -829,6 +966,45 @@ static int crypto_aes_gcm_decrypt_common(struct aead_request *req,
return err;
}
+/*
+ * Common AES-GCM decryption helper; see crypto_aes_gcm_encrypt_common() for
+ * @assoclen.
+ */
+static int crypto_aes_gcm_decrypt_common(struct aead_request *req,
+ const struct aes_gcm_key *key,
+ u8 iv[12], unsigned int assoclen)
+{
+ /* crypto_aead_decrypt() already checked cryptlen >= authtag_len. */
+ unsigned int data_len = req->cryptlen - key->authtag_len;
+ struct scatterlist *src_sg, *dst_sg;
+ unsigned int src_off, dst_off;
+ const u8 *ad;
+ u8 *src, *dst, *tag;
+ int err;
+
+ if (unlikely(!aead_sg_get_linear(req->src, req->assoclen, data_len,
+ key->authtag_len, &ad, &src, &src_sg,
+ &src_off, &tag, NULL, NULL)))
+ return crypto_aes_gcm_decrypt_nonlinear(req, key, iv, assoclen,
+ data_len);
+ if (req->src == req->dst || !data_len) {
+ dst_sg = src_sg;
+ dst_off = src_off;
+ dst = src;
+ } else {
+ if (unlikely(!aead_sg_get_linear(
+ req->dst, req->assoclen, data_len, 0, NULL, &dst,
+ &dst_sg, &dst_off, NULL, NULL, NULL)))
+ return crypto_aes_gcm_decrypt_nonlinear(
+ req, key, iv, assoclen, data_len);
+ }
+
+ err = aes_gcm_decrypt(dst, src, data_len, tag, ad, assoclen, iv, key);
+ if (data_len)
+ sg_flush_dcache(dst_sg, dst_off, data_len);
+ return err;
+}
+
static __maybe_unused int crypto_aes_gcm_encrypt(struct aead_request *req)
{
struct crypto_aead *tfm = crypto_aead_reqtfm(req);
--
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 ` Eric Biggers [this message]
2026-11-29 20:02 ` [PATCH 05/11] lib/crypto: x86/gf128hash: Optimize GHASH and POLYVAL with PCLMULQDQ and AVX Eric Biggers
2026-11-29 20:02 ` [PATCH 06/11] lib/crypto: x86/gf128hash: Optimize GHASH and POLYVAL with VPCLMULQDQ and AVX2 Eric Biggers
2026-11-29 20:02 ` [PATCH 07/11] lib/crypto: x86/gf128hash: Optimize GHASH and POLYVAL with VPCLMULQDQ and AVX512 Eric Biggers
2026-11-29 20:02 ` [PATCH 08/11] lib/crypto: x86/aes-gcm: Migrate AES-NI optimized code into library Eric Biggers
2026-11-29 20:02 ` [PATCH 09/11] lib/crypto: x86/aes-gcm: Migrate VAES+AVX2 " Eric Biggers
2026-11-29 20:02 ` [PATCH 10/11] lib/crypto: x86/aes-gcm: Migrate VAES+AVX512 " Eric Biggers
2026-11-29 20:02 ` [PATCH 11/11] crypto: x86/aesni-intel - Remove empty module Eric Biggers
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261129200245.146503-5-ebiggers@kernel.org \
--to=ebiggers@kernel.org \
--cc=Jason@zx2c4.com \
--cc=antonio@mandelbit.com \
--cc=ardb@kernel.org \
--cc=herbert@gondor.apana.org.au \
--cc=linux-crypto@vger.kernel.org \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox