]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
lib/crypto: sha3: Add FIPS cryptographic algorithm self-test
authorEric Biggers <ebiggers@kernel.org>
Sun, 26 Oct 2025 05:50:24 +0000 (22:50 -0700)
committerEric Biggers <ebiggers@kernel.org>
Thu, 6 Nov 2025 04:02:35 +0000 (20:02 -0800)
Since the SHA-3 algorithms are FIPS-approved, add the boot-time
self-test which is apparently required.  This closely follows the
corresponding SHA-1, SHA-256, and SHA-512 tests.

Tested-by: Harald Freudenberger <freude@linux.ibm.com>
Reviewed-by: Ard Biesheuvel <ardb@kernel.org>
Link: https://lore.kernel.org/r/20251026055032.1413733-8-ebiggers@kernel.org
Signed-off-by: Eric Biggers <ebiggers@kernel.org>
lib/crypto/fips.h
lib/crypto/sha3.c
scripts/crypto/gen-fips-testvecs.py

index 78a1bdd33a15130ae5e40c5798923673b8b2ac3f..023410c2e0dbc54f8632be895f7815943c7a40f6 100644 (file)
@@ -36,3 +36,10 @@ static const u8 fips_test_hmac_sha512_value[] __initconst __maybe_unused = {
        0x57, 0x0b, 0x15, 0x38, 0x95, 0xd8, 0xa3, 0x81,
        0xba, 0xb3, 0x15, 0x37, 0x5c, 0x6d, 0x57, 0x2b,
 };
+
+static const u8 fips_test_sha3_256_value[] __initconst __maybe_unused = {
+       0x77, 0xc4, 0x8b, 0x69, 0x70, 0x5f, 0x0a, 0xb1,
+       0xb1, 0xa5, 0x82, 0x0a, 0x22, 0x2b, 0x49, 0x31,
+       0xba, 0x9b, 0xb6, 0xaa, 0x32, 0xa7, 0x97, 0x00,
+       0x98, 0xdb, 0xff, 0xe7, 0xc6, 0xde, 0xb5, 0x82,
+};
index 2102c2ecac96980bd0398a3da987405967c1aea4..8434f9bff13858346ea89563a604e7945abb97ea 100644 (file)
@@ -17,6 +17,7 @@
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/unaligned.h>
+#include "fips.h"
 
 /*
  * On some 32-bit architectures, such as h8300, GCC ends up using over 1 KB of
@@ -341,10 +342,24 @@ void shake256(const u8 *in, size_t in_len, u8 *out, size_t out_len)
 }
 EXPORT_SYMBOL_GPL(shake256);
 
-#ifdef sha3_mod_init_arch
+#if defined(sha3_mod_init_arch) || defined(CONFIG_CRYPTO_FIPS)
 static int __init sha3_mod_init(void)
 {
+#ifdef sha3_mod_init_arch
        sha3_mod_init_arch();
+#endif
+       if (fips_enabled) {
+               /*
+                * FIPS cryptographic algorithm self-test.  As per the FIPS
+                * Implementation Guidance, testing any SHA-3 algorithm
+                * satisfies the test requirement for all of them.
+                */
+               u8 hash[SHA3_256_DIGEST_SIZE];
+
+               sha3_256(fips_test_data, sizeof(fips_test_data), hash);
+               if (memcmp(fips_test_sha3_256_value, hash, sizeof(hash)) != 0)
+                       panic("sha3: FIPS self-test failed\n");
+       }
        return 0;
 }
 subsys_initcall(sha3_mod_init);
index 2956f88b764aea1845c7a1f1357cc6c570c95453..db873f88619ac0616020b4ef3052d3bf8cf6c4f5 100755 (executable)
@@ -5,6 +5,7 @@
 #
 # Copyright 2025 Google LLC
 
+import hashlib
 import hmac
 
 fips_test_data = b"fips test data\0\0"
@@ -30,3 +31,6 @@ for alg in 'sha1', 'sha256', 'sha512':
     ctx = hmac.new(fips_test_key, digestmod=alg)
     ctx.update(fips_test_data)
     print_static_u8_array_definition(f'fips_test_hmac_{alg}_value', ctx.digest())
+
+print_static_u8_array_definition(f'fips_test_sha3_256_value',
+                                 hashlib.sha3_256(fips_test_data).digest())