Linux cryptographic layer development
 help / color / mirror / Atom feed
* [PATCH] lib/crypto: aes-cmac: Hide selection mask value from compilers
@ 2026-10-31  5:20 Eric Biggers
  0 siblings, 0 replies; only message in thread
From: Eric Biggers @ 2026-10-31  5:20 UTC (permalink / raw)
  To: linux-crypto
  Cc: Ard Biesheuvel, Jason A . Donenfeld, Herbert Xu, Eric Biggers

Hide selection mask values from compilers, so that they cannot see that
they are used to select between two values and potentially generate a
secret-dependent branch.  This is not known to be happening here, but we
might as well be extra careful.

Since the selected values are only 0x87 and 0 here, change the type of
the mask to 'unsigned long' so that the compiler is not forced to
materialize it in a register pair on 32-bit architectures.

Signed-off-by: Eric Biggers <ebiggers@kernel.org>
---
 lib/crypto/aes.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/lib/crypto/aes.c b/lib/crypto/aes.c
index e1c191551736..1d26fd8be028 100644
--- a/lib/crypto/aes.c
+++ b/lib/crypto/aes.c
@@ -578,7 +578,7 @@ static void aes_cbcmac_blocks(u8 h[AES_BLOCK_SIZE],
 int aes_cmac_preparekey(struct aes_cmac_key *key, const u8 *in_key,
 			size_t key_len)
 {
-	u64 hi, lo, mask;
+	u64 hi, lo;
 	int err;
 
 	/* Prepare the AES key. */
@@ -596,9 +596,11 @@ int aes_cmac_preparekey(struct aes_cmac_key *key, const u8 *in_key,
 	hi = be64_to_cpu(key->k_final[0].w[0]);
 	lo = be64_to_cpu(key->k_final[0].w[1]);
 	for (int i = 0; i < 2; i++) {
-		mask = ((s64)hi >> 63) & 0x87;
+		unsigned long mask = (s64)hi >> 63;
+
+		OPTIMIZER_HIDE_VAR(mask);
 		hi = (hi << 1) ^ (lo >> 63);
-		lo = (lo << 1) ^ mask;
+		lo = (lo << 1) ^ (mask & 0x87);
 		key->k_final[i].w[0] = cpu_to_be64(hi);
 		key->k_final[i].w[1] = cpu_to_be64(lo);
 	}

base-commit: 2a500a04d6f969318c05f46aedb6e73fc1963a00
-- 
2.56.0


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-10-31  5:21 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-31  5:20 [PATCH] lib/crypto: aes-cmac: Hide selection mask value from compilers Eric Biggers

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox