[PATCH v2 1/4] crypto: fips - Add a wrapper function for checking the HMAC key length

From: Thomas Huth

Date: Thu Dec 03 2026 - 04:33:38 EST


We are going to need this check in multiple locations. To avoid having
to update each location separately if the FIPS requirements change,
introduce a common helper function first..

Cc: stable@xxxxxxxxxxxxxxx # 6.19+, prerequisite for the following patches
Signed-off-by: Thomas Huth <thuth@xxxxxxxxxx>
---
Note: Please don't backport this patch to any kernel before 6.19, see:
https://lore.kernel.org/linux-crypto/20261201202148.GA501764@xxxxxxxxxx/

crypto/hmac.c | 4 ++--
include/linux/fips.h | 7 +++++++
2 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/crypto/hmac.c b/crypto/hmac.c
index 807e08b252c55..bfa5225a79990 100644
--- a/crypto/hmac.c
+++ b/crypto/hmac.c
@@ -45,7 +45,7 @@ static int hmac_setkey(struct crypto_shash *parent,
SHASH_DESC_ON_STACK(shash, hash);
int err, i;

- if (fips_enabled && (keylen < 112 / 8))
+ if (fips_hmac_keylen_disallowed(keylen))
return -EINVAL;

shash->tfm = hash;
@@ -246,7 +246,7 @@ static int hmac_setkey_ahash(struct crypto_ahash *parent,
u8 *ipad = &tctx->pads[0];
int err, i;

- if (fips_enabled && (keylen < 112 / 8))
+ if (fips_hmac_keylen_disallowed(keylen))
return -EINVAL;

ahash_request_set_callback(req, 0, NULL, NULL);
diff --git a/include/linux/fips.h b/include/linux/fips.h
index c6961e932fef3..17e7ae747c485 100644
--- a/include/linux/fips.h
+++ b/include/linux/fips.h
@@ -2,6 +2,8 @@
#ifndef _FIPS_H
#define _FIPS_H

+#include <linux/types.h>
+
#ifdef CONFIG_CRYPTO_FIPS
extern int fips_enabled;
extern struct atomic_notifier_head fips_fail_notif_chain;
@@ -15,4 +17,9 @@ static inline void fips_fail_notify(void) {}

#endif

+static inline bool fips_hmac_keylen_disallowed(unsigned int keylen)
+{
+ return fips_enabled && keylen < 112 / 8;
+}
+
#endif
--
2.56.1