blob: c2d23c91e1be7d2166a5dfac5d4f5e39776defae [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):
43 raise Exception("Key generation program does not exist.")
44 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
69def write_header(macro_name: str):
70 return ("/* This macro was generated from tests/scripts/generate_test_keys.py */\n" +
71 "/* BEGIN FILE string macro {} */\n".format(macro_name))
72
73def write_footer():
74 return "/* END FILE */\n"
75
76def main():
77 # Remove intermediate and output files if already existing.
78 if os.path.exists(OUTPUT_HEADER_FILE):
79 os.remove(OUTPUT_HEADER_FILE)
80 if os.path.exists(TMP_DER_FILE):
81 os.remove(TMP_DER_FILE)
82
83 output_file = open(OUTPUT_HEADER_FILE, 'at')
84
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")
94 output_file.write(write_header(key))
95 c_data = convert_der_to_c(key)
96 output_file.write(c_data)
97 output_file.write(write_footer())
98 # Remove the temporary key file.
99 os.remove(TMP_DER_FILE)
100 add_newline = True
101
102if __name__ == '__main__':
103 sys.exit(main())