xref: /linux/scripts/crypto/gen-fips-testvecs.py (revision 7fc2cd2e4b398c57c9cf961cfea05eadbf34c05c)
1#!/usr/bin/env python3
2# SPDX-License-Identifier: GPL-2.0-or-later
3#
4# Script that generates lib/crypto/fips.h
5#
6# Copyright 2025 Google LLC
7
8import hashlib
9import hmac
10
11fips_test_data = b"fips test data\0\0"
12fips_test_key = b"fips test key\0\0\0"
13
14def print_static_u8_array_definition(name, value):
15    print('')
16    print(f'static const u8 {name}[] __initconst __maybe_unused = {{')
17    for i in range(0, len(value), 8):
18        line = '\t' + ''.join(f'0x{b:02x}, ' for b in value[i:i+8])
19        print(f'{line.rstrip()}')
20    print('};')
21
22print('/* SPDX-License-Identifier: GPL-2.0-or-later */')
23print(f'/* This file was generated by: gen-fips-testvecs.py */')
24print()
25print('#include <linux/fips.h>')
26
27print_static_u8_array_definition("fips_test_data", fips_test_data)
28print_static_u8_array_definition("fips_test_key", fips_test_key)
29
30for alg in 'sha1', 'sha256', 'sha512':
31    ctx = hmac.new(fips_test_key, digestmod=alg)
32    ctx.update(fips_test_data)
33    print_static_u8_array_definition(f'fips_test_hmac_{alg}_value', ctx.digest())
34
35print_static_u8_array_definition(f'fips_test_sha3_256_value',
36                                 hashlib.sha3_256(fips_test_data).digest())
37