xref: /linux/scripts/crypto/gen-fips-testvecs.py (revision 04cadb4fe0341304741ef60a297366b553f0ce36)
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 hmac
9
10fips_test_data = b"fips test data\0\0"
11fips_test_key = b"fips test key\0\0\0"
12
13def print_static_u8_array_definition(name, value):
14    print('')
15    print(f'static const u8 {name}[] __initconst __maybe_unused = {{')
16    for i in range(0, len(value), 8):
17        line = '\t' + ''.join(f'0x{b:02x}, ' for b in value[i:i+8])
18        print(f'{line.rstrip()}')
19    print('};')
20
21print('/* SPDX-License-Identifier: GPL-2.0-or-later */')
22print(f'/* This file was generated by: gen-fips-testvecs.py */')
23print()
24print('#include <linux/fips.h>')
25
26print_static_u8_array_definition("fips_test_data", fips_test_data)
27print_static_u8_array_definition("fips_test_key", fips_test_key)
28
29for alg in 'sha1', 'sha256', 'sha512':
30    ctx = hmac.new(fips_test_key, digestmod=alg)
31    ctx.update(fips_test_data)
32    print_static_u8_array_definition(f'fips_test_hmac_{alg}_value', ctx.digest())
33