Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

lib/crypto: sha3: Add FIPS cryptographic algorithm self-test

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>

+27 -1
+7
lib/crypto/fips.h
··· 36 36 0x57, 0x0b, 0x15, 0x38, 0x95, 0xd8, 0xa3, 0x81, 37 37 0xba, 0xb3, 0x15, 0x37, 0x5c, 0x6d, 0x57, 0x2b, 38 38 }; 39 + 40 + static const u8 fips_test_sha3_256_value[] __initconst __maybe_unused = { 41 + 0x77, 0xc4, 0x8b, 0x69, 0x70, 0x5f, 0x0a, 0xb1, 42 + 0xb1, 0xa5, 0x82, 0x0a, 0x22, 0x2b, 0x49, 0x31, 43 + 0xba, 0x9b, 0xb6, 0xaa, 0x32, 0xa7, 0x97, 0x00, 44 + 0x98, 0xdb, 0xff, 0xe7, 0xc6, 0xde, 0xb5, 0x82, 45 + };
+16 -1
lib/crypto/sha3.c
··· 17 17 #include <linux/kernel.h> 18 18 #include <linux/module.h> 19 19 #include <linux/unaligned.h> 20 + #include "fips.h" 20 21 21 22 /* 22 23 * On some 32-bit architectures, such as h8300, GCC ends up using over 1 KB of ··· 342 341 } 343 342 EXPORT_SYMBOL_GPL(shake256); 344 343 345 - #ifdef sha3_mod_init_arch 344 + #if defined(sha3_mod_init_arch) || defined(CONFIG_CRYPTO_FIPS) 346 345 static int __init sha3_mod_init(void) 347 346 { 347 + #ifdef sha3_mod_init_arch 348 348 sha3_mod_init_arch(); 349 + #endif 350 + if (fips_enabled) { 351 + /* 352 + * FIPS cryptographic algorithm self-test. As per the FIPS 353 + * Implementation Guidance, testing any SHA-3 algorithm 354 + * satisfies the test requirement for all of them. 355 + */ 356 + u8 hash[SHA3_256_DIGEST_SIZE]; 357 + 358 + sha3_256(fips_test_data, sizeof(fips_test_data), hash); 359 + if (memcmp(fips_test_sha3_256_value, hash, sizeof(hash)) != 0) 360 + panic("sha3: FIPS self-test failed\n"); 361 + } 349 362 return 0; 350 363 } 351 364 subsys_initcall(sha3_mod_init);
+4
scripts/crypto/gen-fips-testvecs.py
··· 5 5 # 6 6 # Copyright 2025 Google LLC 7 7 8 + import hashlib 8 9 import hmac 9 10 10 11 fips_test_data = b"fips test data\0\0" ··· 31 30 ctx = hmac.new(fips_test_key, digestmod=alg) 32 31 ctx.update(fips_test_data) 33 32 print_static_u8_array_definition(f'fips_test_hmac_{alg}_value', ctx.digest()) 33 + 34 + print_static_u8_array_definition(f'fips_test_sha3_256_value', 35 + hashlib.sha3_256(fips_test_data).digest())