Skip to content

Commit 8f45af9

Browse files
author
Eric Biggers
committed
lib/crypto: aesgcm: Don't disable IRQs during AES block encryption
aes_encrypt() now uses AES instructions when available instead of always using table-based code. AES instructions are constant-time and don't benefit from disabling IRQs as a constant-time hardening measure. In fact, on two architectures (arm and riscv) disabling IRQs is counterproductive because it prevents the AES instructions from being used. (See the may_use_simd() implementation on those architectures.) Therefore, let's remove the IRQ disabling/enabling and leave the choice of constant-time hardening measures to the AES library code. Note that currently the arm table-based AES code (which runs on arm kernels that don't have ARMv8 CE) disables IRQs, while the generic table-based AES code does not. So this does technically regress in constant-time hardening when that generic code is used. But as discussed in commit a22fd0e ("lib/crypto: aes: Introduce improved AES library") I think just leaving IRQs enabled is the right choice. Disabling them is slow and can cause problems, and AES instructions (which modern CPUs have) solve the problem in a much better way anyway. Link: https://lore.kernel.org/r/20260331024430.51755-1-ebiggers@kernel.org Signed-off-by: Eric Biggers <ebiggers@kernel.org>
1 parent 1aa82df commit 8f45af9

1 file changed

Lines changed: 3 additions & 22 deletions

File tree

lib/crypto/aesgcm.c

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,6 @@
99
#include <crypto/utils.h>
1010
#include <linux/export.h>
1111
#include <linux/module.h>
12-
#include <asm/irqflags.h>
13-
14-
static void aesgcm_encrypt_block(const struct aes_enckey *key, void *dst,
15-
const void *src)
16-
{
17-
unsigned long flags;
18-
19-
/*
20-
* In AES-GCM, both the GHASH key derivation and the CTR mode
21-
* encryption operate on known plaintext, making them susceptible to
22-
* timing attacks on the encryption key. The AES library already
23-
* mitigates this risk to some extent by pulling the entire S-box into
24-
* the caches before doing any substitutions, but this strategy is more
25-
* effective when running with interrupts disabled.
26-
*/
27-
local_irq_save(flags);
28-
aes_encrypt(key, dst, src);
29-
local_irq_restore(flags);
30-
}
3112

3213
/**
3314
* aesgcm_expandkey - Expands the AES and GHASH keys for the AES-GCM key
@@ -53,7 +34,7 @@ int aesgcm_expandkey(struct aesgcm_ctx *ctx, const u8 *key,
5334
return ret;
5435

5536
ctx->authsize = authsize;
56-
aesgcm_encrypt_block(&ctx->aes_key, h, h);
37+
aes_encrypt(&ctx->aes_key, h, h);
5738
ghash_preparekey(&ctx->ghash_key, h);
5839
memzero_explicit(h, sizeof(h));
5940
return 0;
@@ -98,7 +79,7 @@ static void aesgcm_mac(const struct aesgcm_ctx *ctx, const u8 *src, int src_len,
9879
ghash_final(&ghash, ghash_out);
9980

10081
ctr[3] = cpu_to_be32(1);
101-
aesgcm_encrypt_block(&ctx->aes_key, enc_ctr, ctr);
82+
aes_encrypt(&ctx->aes_key, enc_ctr, (const u8 *)ctr);
10283
crypto_xor_cpy(authtag, ghash_out, enc_ctr, ctx->authsize);
10384

10485
memzero_explicit(ghash_out, sizeof(ghash_out));
@@ -120,7 +101,7 @@ static void aesgcm_crypt(const struct aesgcm_ctx *ctx, u8 *dst, const u8 *src,
120101
* len', this cannot happen, so no explicit test is necessary.
121102
*/
122103
ctr[3] = cpu_to_be32(n++);
123-
aesgcm_encrypt_block(&ctx->aes_key, buf, ctr);
104+
aes_encrypt(&ctx->aes_key, buf, (const u8 *)ctr);
124105
crypto_xor_cpy(dst, src, buf, min(len, AES_BLOCK_SIZE));
125106

126107
dst += AES_BLOCK_SIZE;

0 commit comments

Comments
 (0)