From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795982663; b=SUlvMr/7iVPog4NGtuo+q8/nbW4qKO5V9gJ7DY9n3bsnCHCpg+g9qlcGbSZzun3hR/aV OFNr744NuidQNsQT0cb7o01AxHmzDEdr6LKEM7rA+oq2zjcrCdck0XPS5yPDgRg9HHV7j ybaCbPixRA9U0vgWSp1yREPk0KHzVn8R6iR2IH4b/IIOHqCHx/7jSitnWLPWjUYlSddCb zuVcWP6HPva+uP5B71y1NYSdlI5T5QBs8uoR9RvKQ3TzUFQk6pehWPfBmXxzLTE55jz1s lqv+GiTNS+e9tlLHKYXLwGsoIjXZK2sZkmrzfwFW0zUFinzNa4tuji+M4K8ZiiuUDqw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795982663; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=VpUAr0db2cTJQT2WqwLhtLdUGcJYxol2u+9UnioG/14=; b=F5/3Afzw3FQxk633mskoLphOaaId4cyKkAHI/M1jRh/sX7mhfh4XuXALDxUJKVQWd2xY IkZOhaeyUAkvqofSrRtfVsccOVygO56UdnKSlciWOS+PJ31wKY/hhSZ1UqQfr6h+8DPdI 9eS89sw/a2NnWgaknY/2/gU+B9fsbBySo5Xm7SlGxvpqnnns90egTTZhp8eNfNSXlt4XD kvGv5bpyviFLBGofEkNy4ZEehx0TSQa5GyA8rU6oROGFH1FbPzzVwCwEjYPY6v/Zlyv2i PVKa6kBTD/v5QjfM7OHX9U+qmWwctZRUk4m9GMB7hB0qakoJG+VsfijdkYS5GXKF/ag== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=PiRvSNT3; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7D4CC606A325 for ; Sun, 29 Nov 2026 20:04:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=PiRvSNT3 Received: by smtp.kernel.org (Postfix) with ESMTPSA id A6F0B1F00898; Sun, 29 Nov 2026 20:04:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795982662; bh=VpUAr0db2cTJQT2WqwLhtLdUGcJYxol2u+9UnioG/14=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=PiRvSNT3xWM7gX3pXUAeHJLsGoFfyiS2WSBE19uIPMSWrSq0PNJpf4Uo5F+kSl8wX nENh08GIZArRv9g0wNryvw8shJFeQ2BatQYHMIj0ywnJKumMLh/uvnsYt2EPSTlBbj ylCraeUwngX5hPHc8O9j3DZbi76xQrPX2+kYX1Md2FHwTJcRmwxNNeBTLoDe0M9zyg IyRXWurpGXAuYC5FogcjZJVNuF+VPfPi4rgwQKXsKY9Sez1RT89oIaqVaPeK5Mr+Le AyBp/Jdf0saxqn3f/n03jkESSEf/9Asbf7OS4ZcXBeBkFNQ9Z+bQw3vvEcUQe88lsg j/I2GtDo9ApeA== From: Eric Biggers To: linux-crypto@vger.kernel.org Cc: Ard Biesheuvel , "Jason A . Donenfeld" , Herbert Xu , x86@kernel.org, Antonio Quartulli , Eric Biggers Subject: [PATCH 04/11] crypto: aes - Add linear fast path for AES-GCM Date: Sun, 29 Nov 2026 12:02:38 -0800 Message-ID: <20261129200245.146503-5-ebiggers@kernel.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261129200245.146503-1-ebiggers@kernel.org> References: <20261129200245.146503-1-ebiggers@kernel.org> X-Mailing-List: linux-crypto@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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