1#!/usr/bin/env python 2# SPDX-License-Identifier: BSD-2-Clause 3# 4# Copyright (c) 2015, Linaro Limited 5# All rights reserved. 6# 7 8def get_args(): 9 import argparse 10 11 parser = argparse.ArgumentParser() 12 parser.add_argument('--prefix', required=True, \ 13 help='Prefix for the public key exponent and modulus in c file') 14 15 parser.add_argument('--out', required=True, \ 16 help='Name of c file for the public key') 17 18 parser.add_argument('--key', required=True, help='Name of key file') 19 20 return parser.parse_args() 21 22def main(): 23 import array 24 from Crypto.PublicKey import RSA 25 from Crypto.Util.number import long_to_bytes 26 27 args = get_args(); 28 29 f = open(args.key, 'r') 30 key = RSA.importKey(f.read()) 31 f.close 32 33 f = open(args.out, 'w') 34 35 f.write("#include <stdint.h>\n"); 36 f.write("#include <stddef.h>\n\n"); 37 38 f.write("const uint32_t " + args.prefix + "_exponent = " + 39 str(key.publickey().e) + ";\n\n") 40 41 f.write("const uint8_t " + args.prefix + "_modulus[] = {\n") 42 i = 0; 43 for x in array.array("B", long_to_bytes(key.publickey().n)): 44 f.write("0x" + '{0:02x}'.format(x) + ",") 45 i = i + 1; 46 if i % 8 == 0: 47 f.write("\n"); 48 else: 49 f.write(" "); 50 f.write("};\n"); 51 52 f.write("const size_t " + args.prefix + "_modulus_size = sizeof(" + \ 53 args.prefix + "_modulus);\n") 54 55 f.close() 56 57if __name__ == "__main__": 58 main() 59