[PATCH v2 14/30] crypto: sa2ul - validate AES key sizes before indexing

From: Manorit Chawdhry

Date: Wed Nov 04 2026 - 00:46:20 EST


Short or malformed AES keys can produce a negative MCI table index. In
the AEAD path this access happens before the common late AES-key check,
so reject anything other than the supported AES key lengths before
selecting the mode-control instruction.

Fixes: 7694b6ca649f ("crypto: sa2ul - Add crypto driver")
Fixes: d2c8ac187fc9 ("crypto: sa2ul - Add AEAD algorithm support")
Signed-off-by: Manorit Chawdhry <m-chawdhry@xxxxxx>
---
drivers/crypto/sa2ul.c | 11 +++++++----
1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/drivers/crypto/sa2ul.c b/drivers/crypto/sa2ul.c
index 3c25c98e3e92..30ddefd4c0b5 100644
--- a/drivers/crypto/sa2ul.c
+++ b/drivers/crypto/sa2ul.c
@@ -856,7 +856,7 @@ static int sa_aes_cbc_setkey(struct crypto_skcipher *tfm, const u8 *key,
/* Convert the key size (16/24/32) to the key size index (0/1/2) */
int key_idx = (keylen >> 3) - 2;

- if (key_idx >= 3)
+ if (key_idx < 0 || key_idx >= 3)
return -EINVAL;

ad.mci_enc = mci_cbc_enc_array[key_idx];
@@ -876,7 +876,7 @@ static int sa_aes_ecb_setkey(struct crypto_skcipher *tfm, const u8 *key,
/* Convert the key size (16/24/32) to the key size index (0/1/2) */
int key_idx = (keylen >> 3) - 2;

- if (key_idx >= 3)
+ if (key_idx < 0 || key_idx >= 3)
return -EINVAL;

ad.mci_enc = mci_ecb_enc_array[key_idx];
@@ -1658,10 +1658,13 @@ static int sa_aead_setkey(struct crypto_aead *authenc,
return -EINVAL;

/* Convert the key size (16/24/32) to the key size index (0/1/2) */
- key_idx = (keys.enckeylen >> 3) - 2;
- if (key_idx >= 3)
+ if (keys.enckeylen != AES_KEYSIZE_128 &&
+ keys.enckeylen != AES_KEYSIZE_192 &&
+ keys.enckeylen != AES_KEYSIZE_256)
return -EINVAL;

+ key_idx = (keys.enckeylen >> 3) - 2;
+
ad->enc_eng.eng_id = SA_ENG_ID_EM1;
ad->enc_eng.sc_size = SA_CTX_ENC_TYPE1_SZ;
ad->auth_eng.eng_id = SA_ENG_ID_AM1;

--
2.43.0