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=1795982662; b=D8LSEdLsjWjbuPtMCmrm1fmJrprrvnCF+sehQEPtN7t9iRe8dC90bhWDWx204oj8h0ku IgnjLIuqWeARhXtLnZixnFBJhzuluPrzFwi+MStRqiE8pF5B+s0Yn9Dt7Rntfclu3xbuQ +N5sM7ZHtHz2ry3eanKVLcHmrm9pliFYG8CvB1JFxb8fr4KfGNgsJJ6XccrzjtH1vih2P Pm1Z3BUv3QW9ARe8DafFuhSXPyMXPMMnxPZThVzp16k7Rin7q7pPBN7y0/tKgnvY3oZNd yLOl74CB2ohUUwP3FQLCjhAh2Uw9HsjjRqVIc2EF8axrybf/91i9UsUc/kCZ7u2bakA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795982662; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=Jd/zBssY+jO4jGzjz591UMmeKYzA4PS1jgjKcrtYCZA=; b=mwQCuEVn0d+/E2UVY1Y5x6ids6OrOfy8GXLBiscgU1rY+VvbuVww3m4pfrmTyHaApWqF OtJuklH8SMUGvSoFgzRTI13JPmWnYcb1hmttDD1FIwj+4cfCWo774NDpvQgjj9T5x66cY ZwCdTNsqjmn8rc9wPf85kE28dlBwabyWzYHnsVDVj/NilD4R2A9PEJb2LBvVLEU+TygNr Wyre0u1rR01EJxHOH/jXXbgNCoiYPWFkUh2c9APucutgS1tQ00Eka8oq/vhiI92df0ndJ ycXcixlCkevrkpIV5V4rAMpSYMPFvWuXKsICzMYyGwPRZQK51cSwF1QPgzT5gQSBi3g== 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=CacQSFO3; 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 B9B9B606A325 for ; Sun, 29 Nov 2026 20:04:22 +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=CacQSFO3 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5049D1F00896; 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=Jd/zBssY+jO4jGzjz591UMmeKYzA4PS1jgjKcrtYCZA=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=CacQSFO3J5U+cSWrqFumYNA3EEyhxx/hqstsbdotaJwtWZqmwj0aRMcSRvEMGi8xq lxJIxgAxTWmJ7OQ0lVnmSL2IfA/mBOLAL0dwv6+E9fZi89Zva2PBtI5reIDL4uRskx o6lTw2TxVaO4IdoVDVn8ZxTNxqZ+AnB/o/YktGPgE3nnn7Id86cQL3NpTsRExnYIR6 H7K7D8b3c424m2yipuecxcf3C2j656B1EN/Ixi8lZA1Xle/6Of+3YiZVpekXlof9JE OKVIR/Ed0v+whLSeG3WWojWQR1BotEkd4DqU58PmaS8YQfTK6sGqpD0wxA4eIT8bjV QceJZ+wv2U7qA== 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 03/11] lib/crypto: aes-gcm: Add one-shot AES-GCM arch hooks Date: Sun, 29 Nov 2026 12:02:37 -0800 Message-ID: <20261129200245.146503-4-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 incremental AES-GCM API has a fair bit of per-message overhead due to multiple SIMD/FPU sections, separate J_0 encryption and GHASH calls, tag copying, etc. Therefore, add hooks to the one-shot AES-GCM en/decryption functions to allow architectures to optimize the full AES-GCM. This will be useful in two cases: - Direct calls to aes_gcm_encrypt() and aes_gcm_decrypt() - Calls to aes_gcm_encrypt() and aes_gcm_decrypt() via crypto_aead for linear requests, once crypto/aes.c is updated to make those calls. The latter is needed to avoid regressing "gcm(aes)" crypto_aead performance on short messages once the "gcm(aes)" crypto_aead implementations in arch/x86/crypto/ (which have similar short-message optimizations) are superseded by the library-based code. For efficiency, pass a pointer to 'struct aes_gcm_key' rather than its embedded 'struct aes_enckey' and 'struct ghash_key' separately. Update aes_gcm_{en,de}crypt_update_arch() to use this convention too, which conveniently makes all their arguments fit into registers on x86_64. Signed-off-by: Eric Biggers --- lib/crypto/aes.c | 97 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 83 insertions(+), 14 deletions(-) diff --git a/lib/crypto/aes.c b/lib/crypto/aes.c index 13d934ba712c3..7b0486953b4df 100644 --- a/lib/crypto/aes.c +++ b/lib/crypto/aes.c @@ -1483,8 +1483,7 @@ static inline void aes_xts_fips_test(void) 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_enckey *aes_key, - const struct ghash_key *ghash_key) + const struct aes_gcm_key *key) { return false; } @@ -1493,8 +1492,35 @@ static bool aes_gcm_encrypt_update_arch(u8 *dst, const u8 *src, size_t len, 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_enckey *aes_key, - const struct ghash_key *ghash_key) + const struct aes_gcm_key *key) +{ + return false; +} +#endif + +/* + * Hooks for optimized one-shot AES-GCM implementations, overridable by the + * architecture to avoid the overhead of the incremental API. + * + * Returning false causes the incremental implementation to be used instead. + * + * The decryption hook (if it returns true) must check the auth tag and store + * the status (0 or -EBADMSG) at *err. + */ +#ifndef 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 false; +} +#endif +#ifndef 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 false; } @@ -1598,7 +1624,7 @@ static __always_inline void aes_gcm_crypt_update(struct aes_gcm_ctx *ctx, if (enc) { if (likely(aes_gcm_encrypt_update_arch( dst, src, n, &ctx->ghash.acc, ctx->ctr32, - &ctx->key->aes, &ctx->key->ghash))) { + ctx->key))) { be32_add_cpu(&ctx->ctr32[3], n / AES_BLOCK_SIZE); } else { @@ -1608,7 +1634,7 @@ static __always_inline void aes_gcm_crypt_update(struct aes_gcm_ctx *ctx, } else { if (likely(aes_gcm_decrypt_update_arch( dst, src, n, &ctx->ghash.acc, ctx->ctr32, - &ctx->key->aes, &ctx->key->ghash))) { + ctx->key))) { be32_add_cpu(&ctx->ctr32[3], n / AES_BLOCK_SIZE); } else { @@ -1706,9 +1732,15 @@ int aes_gcm_decrypt_final(struct aes_gcm_ctx *ctx, const u8 *authtag) } EXPORT_SYMBOL_GPL(aes_gcm_decrypt_final); -void aes_gcm_encrypt(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) +#ifdef aes_gcm_encrypt_arch +/* Keep struct aes_gcm_ctx out of the fast path stack frame. */ +static noinline void +#else +static inline void +#endif +aes_gcm_encrypt_fallback(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) { struct aes_gcm_ctx ctx; @@ -1717,19 +1749,56 @@ void aes_gcm_encrypt(u8 *dst, const u8 *src, size_t data_len, u8 *authtag, aes_gcm_encrypt_update(&ctx, dst, src, data_len); aes_gcm_encrypt_final(&ctx, authtag); } + +void aes_gcm_encrypt(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) +{ + WARN_ON_ONCE((u64)ad_len > AES_GCM_MAX_AD_LEN); + WARN_ON_ONCE((u64)data_len > AES_GCM_MAX_DATA_LEN); + + if (likely(aes_gcm_encrypt_arch(dst, src, data_len, authtag, ad, ad_len, + nonce, key))) + return; + aes_gcm_encrypt_fallback(dst, src, data_len, authtag, ad, ad_len, nonce, + key); +} EXPORT_SYMBOL_GPL(aes_gcm_encrypt); -int aes_gcm_decrypt(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) +#ifdef aes_gcm_decrypt_arch +/* Keep struct aes_gcm_ctx out of the fast path stack frame. */ +static noinline int +#else +static inline int +#endif +aes_gcm_decrypt_fallback(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) { struct aes_gcm_ctx ctx; - int err; aes_gcm_init(&ctx, nonce, key); aes_gcm_auth_update(&ctx, ad, ad_len); aes_gcm_decrypt_update(&ctx, dst, src, data_len); - err = aes_gcm_decrypt_final(&ctx, authtag); + return aes_gcm_decrypt_final(&ctx, authtag); +} + +int aes_gcm_decrypt(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; + + if (WARN_ON_ONCE((u64)ad_len > AES_GCM_MAX_AD_LEN) || + WARN_ON_ONCE((u64)data_len > AES_GCM_MAX_DATA_LEN)) + return -EBADMSG; + + if (likely(aes_gcm_decrypt_arch(dst, src, data_len, authtag, ad, ad_len, + nonce, key, &err))) + goto out; + err = aes_gcm_decrypt_fallback(dst, src, data_len, authtag, ad, ad_len, + nonce, key); +out: if (unlikely(err) && data_len) { /* * Clear the inauthentic decrypted data so that callers won't -- 2.56.1