[PATCH v2 3/4] crypto: fips - Add back the lost key length check to the hmac(sha256) algorithm
From: Thomas Huth
Date: Thu Dec 03 2026 - 04:33:53 EST
The generic code in hmac_setkey() in crypto/hmac.c rejects keys with
less than 112 bits in FIPS-compliance mode. However, this check got
lost when the hmac(sha224) and hmac(sha256) algorithms were implemented
via the crypto library. Let's restore the same behavior from kernel v6.16
and earlier by adding the key length check to the crypto/sha256.c file,
too.
Cc: stable@xxxxxxxxxxxxxxx # 6.19+, see https://lore.kernel.org/all/20261201202148.GA501764@xxxxxxxxxx/
Fixes: e0cd371691038 ("crypto: sha256 - Wrap library and add HMAC support")
Signed-off-by: Thomas Huth <thuth@xxxxxxxxxx>
---
crypto/sha256.c | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/crypto/sha256.c b/crypto/sha256.c
index fb81defe084c6..a27fc5ca1f92d 100644
--- a/crypto/sha256.c
+++ b/crypto/sha256.c
@@ -10,6 +10,7 @@
*/
#include <crypto/internal/hash.h>
#include <crypto/sha2.h>
+#include <linux/fips.h>
#include <linux/kernel.h>
#include <linux/module.h>
@@ -187,6 +188,9 @@ static int crypto_sha256_import_core(struct shash_desc *desc, const void *in)
static int crypto_hmac_sha224_setkey(struct crypto_shash *tfm,
const u8 *raw_key, unsigned int keylen)
{
+ if (fips_hmac_keylen_disallowed(keylen))
+ return -EINVAL;
+
hmac_sha224_preparekey(HMAC_SHA224_KEY(tfm), raw_key, keylen);
return 0;
}
@@ -254,6 +258,9 @@ static int crypto_hmac_sha224_import_core(struct shash_desc *desc,
static int crypto_hmac_sha256_setkey(struct crypto_shash *tfm,
const u8 *raw_key, unsigned int keylen)
{
+ if (fips_hmac_keylen_disallowed(keylen))
+ return -EINVAL;
+
hmac_sha256_preparekey(HMAC_SHA256_KEY(tfm), raw_key, keylen);
return 0;
}
--
2.56.1