lib/crypto: fips: Split fips.h into fips-aes.h and fips-sha.h

In preparation for adding FIPS self-tests for AES encryption modes,
split fips.h into separate files for the AES and SHA test vectors.

They are still generated by the same script, but this keeps things a bit
more organized.

Reviewed-by: Ard Biesheuvel <ardb@kernel.org>
Link: https://patch.msgid.link/20260802222408.91757-2-ebiggers@kernel.org
Signed-off-by: Eric Biggers <ebiggers@kernel.org>
This commit is contained in:
Eric Biggers 2026-08-02 15:24:06 -07:00
parent adbc4db2c0
commit 60e9a0f5be
8 changed files with 83 additions and 36 deletions

View File

@ -19,7 +19,7 @@
#include <linux/export.h>
#include <linux/module.h>
#include <linux/unaligned.h>
#include "fips.h"
#include "fips-aes.h"
static const u8 ____cacheline_aligned aes_sbox[] = {
0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5,

20
lib/crypto/fips-aes.h Normal file
View File

@ -0,0 +1,20 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/* This file was generated by: gen-fips-testvecs.py */
/* clang-format off */
#include <linux/fips.h>
static const u8 fips_test_data[] __initconst __maybe_unused = {
0x66, 0x69, 0x70, 0x73, 0x20, 0x74, 0x65, 0x73,
0x74, 0x20, 0x64, 0x61, 0x74, 0x61, 0x00, 0x00,
};
static const u8 fips_test_key[] __initconst __maybe_unused = {
0x66, 0x69, 0x70, 0x73, 0x20, 0x74, 0x65, 0x73,
0x74, 0x20, 0x6b, 0x65, 0x79, 0x00, 0x00, 0x00,
};
static const u8 fips_test_aes_cmac_value[] __initconst __maybe_unused = {
0xc5, 0x88, 0x28, 0x55, 0xd7, 0x2c, 0x00, 0xb6,
0x6a, 0xa7, 0xfc, 0x82, 0x90, 0x81, 0xcf, 0x18,
};

View File

@ -1,5 +1,6 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/* This file was generated by: gen-fips-testvecs.py */
/* clang-format off */
#include <linux/fips.h>
@ -43,8 +44,3 @@ static const u8 fips_test_sha3_256_value[] __initconst __maybe_unused = {
0xba, 0x9b, 0xb6, 0xaa, 0x32, 0xa7, 0x97, 0x00,
0x98, 0xdb, 0xff, 0xe7, 0xc6, 0xde, 0xb5, 0x82,
};
static const u8 fips_test_aes_cmac_value[] __initconst __maybe_unused = {
0xc5, 0x88, 0x28, 0x55, 0xd7, 0x2c, 0x00, 0xb6,
0x6a, 0xa7, 0xfc, 0x82, 0x90, 0x81, 0xcf, 0x18,
};

View File

@ -12,7 +12,7 @@
#include <linux/string.h>
#include <linux/unaligned.h>
#include <linux/wordpart.h>
#include "fips.h"
#include "fips-sha.h"
static const struct sha1_block_state sha1_iv = {
.h = { SHA1_H0, SHA1_H1, SHA1_H2, SHA1_H3, SHA1_H4 },

View File

@ -17,7 +17,7 @@
#include <linux/string.h>
#include <linux/unaligned.h>
#include <linux/wordpart.h>
#include "fips.h"
#include "fips-sha.h"
static const struct sha256_block_state sha224_iv = {
.h = {

View File

@ -17,7 +17,7 @@
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/unaligned.h>
#include "fips.h"
#include "fips-sha.h"
/*
* On some 32-bit architectures, such as h8300, GCC ends up using over 1 KB of

View File

@ -17,7 +17,7 @@
#include <linux/string.h>
#include <linux/unaligned.h>
#include <linux/wordpart.h>
#include "fips.h"
#include "fips-sha.h"
static const struct sha512_block_state sha384_iv = {
.h = {

View File

@ -1,7 +1,7 @@
#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-2.0-or-later
#
# Script that generates lib/crypto/fips.h
# Script that generates lib/crypto/fips-aes.h and lib/crypto/fips-sha.h
#
# Requires that python-cryptography be installed.
#
@ -12,35 +12,66 @@ import cryptography.hazmat.primitives.cmac
import hashlib
import hmac
fips_test_data = b"fips test data\0\0"
fips_test_key = b"fips test key\0\0\0"
def print_static_u8_array_definition(name, value):
print('')
print(f'static const u8 {name}[] __initconst __maybe_unused = {{')
def print_static_u8_array_definition(file, name, value):
print("", file=file)
print(f"static const u8 {name}[] __initconst __maybe_unused = {{", file=file)
for i in range(0, len(value), 8):
line = '\t' + ''.join(f'0x{b:02x}, ' for b in value[i:i+8])
print(f'{line.rstrip()}')
print('};')
line = "\t" + "".join(f"0x{b:02x}, " for b in value[i : i + 8])
print(f"{line.rstrip()}", file=file)
print("};", file=file)
print('/* SPDX-License-Identifier: GPL-2.0-or-later */')
print(f'/* This file was generated by: gen-fips-testvecs.py */')
print()
print('#include <linux/fips.h>')
print_static_u8_array_definition("fips_test_data", fips_test_data)
print_static_u8_array_definition("fips_test_key", fips_test_key)
def print_header(file):
print("/* SPDX-License-Identifier: GPL-2.0-or-later */", file=file)
print("/* This file was generated by: gen-fips-testvecs.py */", file=file)
print("/* clang-format off */", file=file)
print("", file=file)
print("#include <linux/fips.h>", file=file)
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())
def gen_aes_test_data(file):
fips_test_data = b"fips test data\0\0"
fips_test_key = b"fips test key\0\0\0"
aes = cryptography.hazmat.primitives.ciphers.algorithms.AES(fips_test_key)
aes_cmac = cryptography.hazmat.primitives.cmac.CMAC(aes)
aes_cmac.update(fips_test_data)
print_static_u8_array_definition('fips_test_aes_cmac_value',
aes_cmac.finalize())
print_header(file)
print_static_u8_array_definition(file, "fips_test_data", fips_test_data)
print_static_u8_array_definition(file, "fips_test_key", fips_test_key)
aes = cryptography.hazmat.primitives.ciphers.algorithms.AES(fips_test_key)
aes_cmac = cryptography.hazmat.primitives.cmac.CMAC(aes)
aes_cmac.update(fips_test_data)
print_static_u8_array_definition(
file, "fips_test_aes_cmac_value", aes_cmac.finalize()
)
def gen_sha_test_data(file):
fips_test_data = b"fips test data\0\0"
fips_test_key = b"fips test key\0\0\0"
print_header(file)
print_static_u8_array_definition(file, "fips_test_data", fips_test_data)
print_static_u8_array_definition(file, "fips_test_key", fips_test_key)
for alg in "sha1", "sha256", "sha512":
ctx = hmac.new(fips_test_key, digestmod=alg)
ctx.update(fips_test_data)
print_static_u8_array_definition(
file, f"fips_test_hmac_{alg}_value", ctx.digest()
)
print_static_u8_array_definition(
file, "fips_test_sha3_256_value", hashlib.sha3_256(fips_test_data).digest()
)
filename = "lib/crypto/fips-aes.h"
with open(filename, "w") as file:
print(f"Generating {filename}")
gen_aes_test_data(file)
filename = "lib/crypto/fips-sha.h"
with open(filename, "w") as file:
print(f"Generating {filename}")
gen_sha_test_data(file)