diff --git a/scripts/imgtool.py b/scripts/imgtool.py index 6f0fb4e3..ea289349 100755 --- a/scripts/imgtool.py +++ b/scripts/imgtool.py @@ -26,7 +26,13 @@ def do_keygen(args): def do_getpub(args): key = keys.load(args.key) - key.emit_c() + if args.lang == 'c': + key.emit_c() + elif args.lang == 'rust': + key.emit_rust() + else: + msg = "Unsupported language, valid are: c, or rust" + raise argparse.ArgumentTypeError(msg) def do_sign(args): if args.rsa_pkcs1_15: @@ -73,6 +79,7 @@ def args(): getpub = subs.add_parser('getpub', help='Get public key from keypair') getpub.add_argument('-k', '--key', metavar='filename', required=True) + getpub.add_argument('-l', '--lang', metavar='lang', default='c') sign = subs.add_parser('sign', help='Sign an image with a private key') sign.add_argument('-k', '--key', metavar='filename') diff --git a/scripts/imgtool/keys.py b/scripts/imgtool/keys.py index e81f6f64..fddfec9b 100644 --- a/scripts/imgtool/keys.py +++ b/scripts/imgtool/keys.py @@ -34,13 +34,16 @@ class RSA2048(): with open(path, 'wb') as f: f.write(self.key.exportKey('PEM')) - def emit_c(self): + def get_public_bytes(self): node = RSAPublicKey() node['modulus'] = self.key.n node['publicExponent'] = self.key.e + return bytearray(encode(node)) + + def emit_c(self): print(AUTOGEN_MESSAGE) print("const unsigned char rsa_pub_key[] = {", end='') - encoded = bytearray(encode(node)) + encoded = self.get_public_bytes() for count, b in enumerate(encoded): if count % 8 == 0: print("\n\t", end='') @@ -50,6 +53,18 @@ class RSA2048(): print("\n};") print("const unsigned int rsa_pub_key_len = {};".format(len(encoded))) + def emit_rust(self): + print(AUTOGEN_MESSAGE) + print("static RSA_PUB_KEY: &'static [u8] = &[", end='') + encoded = self.get_public_bytes() + for count, b in enumerate(encoded): + if count % 8 == 0: + print("\n ", end='') + else: + print(" ", end='') + print("0x{:02x},".format(b), end='') + print("\n];") + def sig_type(self): """Return the type of this signature (as a string)""" if sign_rsa_pss: