blob: 8cd104430f49b8a81ce3e8c371aadabdf18c243b [file] [log] [blame]
Jens Wiklanderbc420742015-05-05 14:59:15 +02001#!/usr/bin/env python
Jerome Forissier1bb92982017-12-15 14:27:02 +01002# SPDX-License-Identifier: BSD-2-Clause
Jens Wiklanderbc420742015-05-05 14:59:15 +02003#
4# Copyright (c) 2015, Linaro Limited
Jens Wiklanderbc420742015-05-05 14:59:15 +02005
Jerome Forissier049aefa2018-11-14 11:02:49 +01006
Jens Wiklanderbc420742015-05-05 14:59:15 +02007def get_args():
Jerome Forissier049aefa2018-11-14 11:02:49 +01008 import argparse
Jens Wiklanderbc420742015-05-05 14:59:15 +02009
Jerome Forissier049aefa2018-11-14 11:02:49 +010010 parser = argparse.ArgumentParser()
11 parser.add_argument(
Markus S. Wamser1718b6c2019-04-30 12:06:14 +020012 '--prefix', required=True,
Jerome Forissier049aefa2018-11-14 11:02:49 +010013 help='Prefix for the public key exponent and modulus in c file')
Markus S. Wamser1718b6c2019-04-30 12:06:14 +020014 parser.add_argument(
15 '--out', required=True,
16 help='Name of c file for the public key')
Jerome Forissier049aefa2018-11-14 11:02:49 +010017 parser.add_argument('--key', required=True, help='Name of key file')
Jens Wiklanderbc420742015-05-05 14:59:15 +020018
Jerome Forissier049aefa2018-11-14 11:02:49 +010019 return parser.parse_args()
20
Jens Wiklanderbc420742015-05-05 14:59:15 +020021
22def main():
Jerome Forissier049aefa2018-11-14 11:02:49 +010023 import array
24 from Crypto.PublicKey import RSA
25 from Crypto.Util.number import long_to_bytes
Jens Wiklanderbc420742015-05-05 14:59:15 +020026
Jerome Forissier049aefa2018-11-14 11:02:49 +010027 args = get_args()
Jens Wiklanderbc420742015-05-05 14:59:15 +020028
Markus S. Wamser1718b6c2019-04-30 12:06:14 +020029 with open(args.key, 'r') as f:
30 key = RSA.importKey(f.read())
Jens Wiklanderbc420742015-05-05 14:59:15 +020031
Markus S. Wamser0a6f2bc2019-03-26 11:29:44 +010032 # Refuse public exponent with more than 32 bits. Otherwise the C
33 # compiler may simply truncate the value and proceed.
34 # This will lead to TAs seemingly having invalid signatures with a
35 # possible security issue for any e = k*2^32 + 1 (for any integer k).
36 if key.publickey().e > 0xffffffff:
37 raise ValueError(
38 'Unsupported large public exponent detected. ' +
39 'OP-TEE handles only public exponents up to 2^32 - 1.')
40
Markus S. Wamser1718b6c2019-04-30 12:06:14 +020041 with open(args.out, 'w') as f:
42 f.write("#include <stdint.h>\n")
43 f.write("#include <stddef.h>\n\n")
44 f.write("const uint32_t " + args.prefix + "_exponent = " +
45 str(key.publickey().e) + ";\n\n")
46 f.write("const uint8_t " + args.prefix + "_modulus[] = {\n")
47 i = 0
48 for x in array.array("B", long_to_bytes(key.publickey().n)):
49 f.write("0x" + '{0:02x}'.format(x) + ",")
50 i = i + 1
51 if i % 8 == 0:
52 f.write("\n")
53 else:
54 f.write(" ")
55 f.write("};\n")
56 f.write("const size_t " + args.prefix + "_modulus_size = sizeof(" +
57 args.prefix + "_modulus);\n")
Jerome Forissier049aefa2018-11-14 11:02:49 +010058
Jens Wiklanderbc420742015-05-05 14:59:15 +020059
60if __name__ == "__main__":
Jerome Forissier049aefa2018-11-14 11:02:49 +010061 main()