crypto: sa2ul - Fix HMAC key preparation
Subsystems (2)
PATCH 1/1 crypto: sa2ul - Fix HMAC key preparation
commit 780cd10c8e78f97ed768be229acff0ffd5483477 upstream.
sa_prepare_iopads() has multiple bugs, as discussed at
https://lore.kernel.org/linux-crypto/[email protected]/T/#u
- Commit 3bf533787910 ("crypto: sha256 - Use the partial block API") introduced a 1-byte buffer overflow in the SHA-256 case by making the crypto_shash_export() write 1 more than sizeof(struct sha256_state).
- It never worked for HMAC keys longer than 64 bytes in the first place.
Fix both of these bugs by just using the crypto library to prepare the key, like what the chelsio and qat drivers do. Also remove the pointless use of an indirect call.
--- drivers/crypto/sa2ul.c | 121 +++++++++-------------------------------- 1 file changed, 27 insertions(+), 94 deletions(-)
drivers/crypto/sa2ul.c +27 -94
@@ -125,9 +125,7 @@ struct sa_cmdl_cfg {* @mci_enc: Mode Control Instruction for Encryption algorithm * @mci_dec: Mode Control Instruction for Decryption * @inv_key: Whether the encryption algorithm demands key inversion -* @ctx: Pointer to the algorithm context * @keyed_mac: Whether the authentication algorithm has key -* @prep_iopad: Function pointer to generate intermediate ipad/opad */ struct algo_data {struct sa_eng_info enc_eng;
@@ -141,10 +139,7 @@ struct algo_data {u8 *mci_enc;u8 *mci_dec;bool inv_key; -struct sa_tfm_ctx *ctx;bool keyed_mac; -void (*prep_iopad)(struct algo_data *algo,const u8 *key, -u16 key_sz,__be32 *ipad,__be32 *opad); };/**
@@ -381,86 +376,36 @@ static void sa_swiz_128(u8 *in, u16 len) } } -/* Prepare the ipad and opad from key as per SHA algorithm step 1*/ -static void prepare_kipad (u8 *k_ipad,const u8 *key,u16 key_sz) +static void sa_prepare_iopads (struct algo_data *data,const u8 *key,u16 key_sz, +__be32 *ipad,__be32 *opad) { -int i; - -for (i =0 ; i < key_sz; i++) - k_ipad[i] = key[i] ^0x36 ; - -/* Instead of XOR with 0 */ -for (; i <SHA1_BLOCK_SIZE ; i++) - k_ipad[i] =0x36 ; -} - -static void prepare_kopad (u8 *k_opad,const u8 *key,u16 key_sz) -{ -int i; - -for (i =0 ; i < key_sz; i++) - k_opad[i] = key[i] ^0x5c ; - -/* Instead of XOR with 0 */ -for (; i <SHA1_BLOCK_SIZE ; i++) - k_opad[i] =0x5c ; -} - -static void sa_export_shash (void *state,struct shash_desc *hash, -int digest_size,__be32 *out) -{ -struct sha1_state *sha1; -struct sha256_state *sha256; -u32 *result; - -switch (digest_size) { -case SHA1_DIGEST_SIZE : - sha1 = state; - result = sha1->state; +union { +struct hmac_sha1_key sha1; +struct hmac_sha256_key sha256; + } k; + +switch (data->aalg_id) { +case SA_AALG_ID_HMAC_SHA1 : +hmac_sha1_preparekey (&k.sha1, key, key_sz); +cpu_to_be32_array (ipad, k.sha1.istate.h, +ARRAY_SIZE (k.sha1.istate.h)); +cpu_to_be32_array (opad, k.sha1.ostate.h, +ARRAY_SIZE (k.sha1.ostate.h));break ; -case SHA256_DIGEST_SIZE : - sha256 = state; - result = sha256->state; +case SA_AALG_ID_HMAC_SHA2_256 : +hmac_sha256_preparekey (&k.sha256, key, key_sz); +cpu_to_be32_array (ipad, k.sha256.key.istate.h, +ARRAY_SIZE (k.sha256.key.istate.h)); +cpu_to_be32_array (opad, k.sha256.key.ostate.h, +ARRAY_SIZE (k.sha256.key.ostate.h));break ;default : -dev_err (sa_k3_dev,"%s: bad digest_size=%d\n" , __func__, - digest_size); +dev_err (sa_k3_dev,"%s: bad aalg_id=%d\n" , __func__, + data->aalg_id);return ; } -crypto_shash_export (hash, state); - -cpu_to_be32_array (out, result, digest_size /4 ); -} - -static void sa_prepare_iopads (struct algo_data *data,const u8 *key, -u16 key_sz,__be32 *ipad,__be32 *opad) -{ -SHASH_DESC_ON_STACK (shash, data->ctx->shash); -int block_size =crypto_shash_blocksize (data->ctx->shash); -int digest_size =crypto_shash_digestsize (data->ctx->shash); -union { -struct sha1_state sha1; -struct sha256_state sha256; -u8 k_pad[SHA1_BLOCK_SIZE ]; - } sha; - - shash->tfm = data->ctx->shash; - -prepare_kipad (sha.k_pad, key, key_sz); - -crypto_shash_init (shash); -crypto_shash_update (shash, sha.k_pad, block_size); -sa_export_shash (&sha, shash, digest_size, ipad); - -prepare_kopad (sha.k_pad, key, key_sz); - -crypto_shash_init (shash); -crypto_shash_update (shash, sha.k_pad, block_size); - -sa_export_shash (&sha, shash, digest_size, opad); - -memzero_explicit (&sha,sizeof (sha)); +memzero_explicit (&k,sizeof (k)); }/* Derive the inverse key used in AES-CBC decryption operation */
@@ -544,7 +489,7 @@ static void sa_set_sc_auth(struct algo_data *ad, const u8 *key, u16 key_sz,/* Copy the keys or ipad/opad */ if (ad->keyed_mac) - ad->prep_iopad (ad, key, key_sz, ipad, opad); +sa_prepare_iopads (ad, key, key_sz, ipad, opad);else {/* basic hash */ sc_buf[1 ] |=SA_BASIC_HASH ;
@@ -1697,8 +1642,7 @@ static void sa_aead_dma_in_callback(void *data)aead_request_complete (req, err); } -static int sa_cra_init_aead (struct crypto_aead *tfm,const char *hash, -const char *fallback) +static int sa_cra_init_aead (struct crypto_aead *tfm,const char *fallback) {struct sa_tfm_ctx *ctx =crypto_aead_ctx (tfm);struct sa_crypto_data *data =dev_get_drvdata (sa_k3_dev);
@@ -1707,12 +1651,6 @@ static int sa_cra_init_aead(struct crypto_aead *tfm, const char *hash,memzero_explicit (ctx,sizeof (*ctx)); ctx->dev_data = data; - ctx->shash =crypto_alloc_shash (hash,0 ,CRYPTO_ALG_NEED_FALLBACK ); -if (IS_ERR (ctx->shash)) { -dev_err (sa_k3_dev,"base driver %s couldn't be loaded\n" , hash); -return PTR_ERR (ctx->shash); - } - ctx->fallback.aead =crypto_alloc_aead (fallback,0 ,CRYPTO_ALG_NEED_FALLBACK );
@@ -1744,14 +1682,12 @@ static int sa_cra_init_aead(struct crypto_aead *tfm, const char *hash,static int sa_cra_init_aead_sha1 (struct crypto_aead *tfm) { -return sa_cra_init_aead (tfm,"sha1" , -"authenc(hmac(sha1),cbc(aes))" ); +return sa_cra_init_aead (tfm,"authenc(hmac(sha1),cbc(aes))" ); }static int sa_cra_init_aead_sha256 (struct crypto_aead *tfm) { -return sa_cra_init_aead (tfm,"sha256" , -"authenc(hmac(sha256),cbc(aes))" ); +return sa_cra_init_aead (tfm,"authenc(hmac(sha256),cbc(aes))" ); }static void sa_exit_tfm_aead (struct crypto_aead *tfm)
@@ -1759,7 +1695,6 @@ static void sa_exit_tfm_aead(struct crypto_aead *tfm)struct sa_tfm_ctx *ctx =crypto_aead_ctx (tfm);struct sa_crypto_data *data =dev_get_drvdata (sa_k3_dev); -crypto_free_shash (ctx->shash);crypto_free_aead (ctx->fallback.aead);sa_free_ctx_info (&ctx->enc, data);
@@ -1785,7 +1720,6 @@ static int sa_aead_setkey(struct crypto_aead *authenc,if (key_idx >=3 )return -EINVAL ; - ad->ctx = ctx; 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 ;
@@ -1795,7 +1729,6 @@ static int sa_aead_setkey(struct crypto_aead *authenc, ad->inv_key = true; ad->keyed_mac = true; ad->ealg_id =SA_EALG_ID_AES_CBC ; - ad->prep_iopad = sa_prepare_iopads;memset (&cfg,0 ,sizeof (cfg)); cfg.enc = true;
base-commit: c3f63d70f236bb7685e6d11d1d9b5b351552e600
Signature · 1 line
2.56.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce: - [Critical] crypto: sa2ul: negative index…
Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [Critical] crypto: sa2ul: negative index out-of-bounds read in sa_aead_setkey() [1]
--
commit 44d71286f88be3b8e50cd6afb3ed5b5d01ae3f54
Author: Eric Biggers <[email protected]>
crypto: sa2ul - Fix HMAC key preparation
This commit replaces the manual shash-based HMAC ipad/opad derivation in sa_prepare_iopads() with the crypto library helpers hmac_sha1_preparekey() and hmac_sha256_preparekey(), and removes the unused shash allocation and indirect call.
Quoted 4 lines
drivers/crypto/sa2ul.c +0
-0
[ ... ]
drivers/crypto/sa2ul.c static int sa_aead_setkey(struct crypto_aead *authenc, · line 1725
Quoted 8 lines
drivers/crypto/sa2ul.c +0
-1@@ -1785,7 +1720,6 @@ static int sa_aead_setkey(struct crypto_aead *authenc,if (key_idx >=3 )return -EINVAL ; - ad->ctx = ctx; 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 ;
[Severity: Critical]
This problem wasn't introduced by this patch, but can a negative key_idx cause an out-of-bounds read on mci_cbc_enc_no_iv_array[] and mci_cbc_dec_no_iv_array[] [1]?
Unlike skcipher, the AEAD framework does not enforce a minimum encryption key size before calling ->setkey(). When crypto_authenc_extractkeys() extracts an encryption key with keys.enckeylen < 16, key_idx = (keys.enckeylen >> 3) - 2 is negative, bypassing the signed key_idx >= 3 check:
drivers/crypto/sa2ul.c:sa_aead_setkey() {
...
key_idx = (keys.enckeylen >> 3) - 2;
if (key_idx >= 3)
return -EINVAL;
...
ad->mci_enc = mci_cbc_enc_no_iv_array[key_idx];
ad->mci_dec = mci_cbc_dec_no_iv_array[key_idx];
...
}This loads out-of-bounds memory into the hardware mode control instruction pointers mci_enc and mci_dec. Note that non-multiples of 8 (such as enckeylen == 17) also pass this check, so validating keys.enckeylen with aes_check_keylen() before computing key_idx would catch both.
[1] https://sashiko.dev/#/bug/linux-3f578a84-38a7-46fe-bc7b-117d71d9d394
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 5 lines · On Sat, Nov 07, 2026 at 07:34:55PM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [Critical] crypto: sa2ul: negative index out-of-bounds read insa_aead_setkey()[1]
drivers/crypto/ is buggy. No surprise there.
- Eric