blob: 23bc9a19e8a640552890da8b3e7fa7a76c63f439 [file] [log] [blame]
Valerio Setti7126ba52024-03-29 16:59:40 +01001#!/usr/bin/env python3
2
3# Copyright The Mbed TLS Contributors
4# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
5
6"""Module generating EC and RSA keys to be used in test_suite_pk instead of
7generating the required key at run time. This helps speeding up testing."""
8
9import os
10import sys
11import subprocess
12
13KEY_GEN = "./programs/pkey/gen_key"
14TMP_DER_FILE = "tmp_key.der"
15OUTPUT_HEADER_FILE = "./tests/src/test_keys.h"
16BYTES_PER_LINE = 12
17
18KEYS = {
19 # RSA keys
20 'test_rsa_1024': ['rsa', '1024'],
21 'test_rsa_1026': ['rsa', '1026'],
22 'test_rsa_1028': ['rsa', '1028'],
23 'test_rsa_1030': ['rsa', '1030'],
24 'test_rsa_2048': ['rsa', '2048'],
25 'test_rsa_4096': ['rsa', '4096'],
26 # EC keys
27 'test_ec_secp192r1': ['ec', 'secp192r1'],
28 'test_ec_secp224r1': ['ec', 'secp224r1'],
29 'test_ec_secp256r1': ['ec', 'secp256r1'],
30 'test_ec_secp384r1': ['ec', 'secp384r1'],
31 'test_ec_secp521r1': ['ec', 'secp521r1'],
32 'test_ec_bp256r1': ['ec', 'brainpoolP256r1'],
33 'test_ec_bp384r1': ['ec', 'brainpoolP384r1'],
34 'test_ec_bp512r1': ['ec', 'brainpoolP512r1'],
35 'test_ec_curve25519': ['ec', 'x25519'],
36 'test_ec_secp192k1': ['ec', 'secp192k1'],
37 'test_ec_secp256k1': ['ec', 'secp256k1'],
38 'test_ec_curve448': ['ec', 'x448'],
39}
40
41def generate_der_file(curve_type: str, curve_or_bits: str):
42 if not os.path.exists(KEY_GEN):
Valerio Setti3e22bf22024-04-03 13:42:20 +020043 raise Exception(KEY_GEN + " does not exist. Please build it before running this script.")
Valerio Setti7126ba52024-03-29 16:59:40 +010044 if curve_type == 'ec':
45 cob_param = 'ec_curve=' + curve_or_bits
46 else:
47 cob_param = 'rsa_keysize=' + curve_or_bits
48
49 subprocess.run([KEY_GEN, 'type=' + curve_type, cob_param,
50 'format=der', 'filename=' + TMP_DER_FILE], check=True)
51
52def convert_der_to_c(array_name: str) -> str:
53 """Convert a DER file content to a C array. The name of such array is
54 provided as input parameter. The file to be converted is the temporary
55 TMP_DER_FILE."""
56 output_text = "const unsigned char {}[] = {{\n".format(array_name)
57
58 with open(TMP_DER_FILE, 'rb') as input_file:
59 data_block = input_file.read(BYTES_PER_LINE)
60 while data_block:
61 new_line = ' ' + ', '.join(['{:#04x}'.format(b) for b in data_block])
62 output_text = output_text + new_line + ",\n"
63 data_block = input_file.read(BYTES_PER_LINE)
64
65 output_text = output_text + "};\n"
66
67 return output_text
68
Valerio Setti7126ba52024-03-29 16:59:40 +010069def main():
70 # Remove intermediate and output files if already existing.
71 if os.path.exists(OUTPUT_HEADER_FILE):
72 os.remove(OUTPUT_HEADER_FILE)
73 if os.path.exists(TMP_DER_FILE):
74 os.remove(TMP_DER_FILE)
75
76 output_file = open(OUTPUT_HEADER_FILE, 'at')
Valerio Setti3e22bf22024-04-03 13:42:20 +020077 output_file.write(
78 "/*********************************************************************************\n" +
79 " * This file was automatically generated from tests/scripts/generate_test_keys.py.\n" +
80 " * Please do not edit it manually.\n" +
81 " *********************************************************************************/\n" +
82 "\n"
83 )
Valerio Setti7126ba52024-03-29 16:59:40 +010084
85 add_newline = False
86 for key in KEYS:
87 # Use gen_key tool to generate the desired key (in DER format) and save
88 # it into a temporary file.
89 generate_der_file(KEYS[key][0], KEYS[key][1])
90 # Convert the key from binary format to a C array and append the result
91 # to the output header file.
92 if add_newline:
93 output_file.write("\n")
Valerio Setti7126ba52024-03-29 16:59:40 +010094 c_data = convert_der_to_c(key)
95 output_file.write(c_data)
Valerio Setti7126ba52024-03-29 16:59:40 +010096 # Remove the temporary key file.
97 os.remove(TMP_DER_FILE)
98 add_newline = True
99
100if __name__ == '__main__':
101 sys.exit(main())