imgtool: Add rust pubkey generation

Add a `--rust` flag to the getpub subcommand to output the public key in
Rust format rather than C.

Signed-off-by: David Brown <david.brown@linaro.org>
This commit is contained in:
David Brown 2017-09-01 09:33:00 -06:00 committed by David Brown
parent 27648b8344
commit d36e91acc1
2 changed files with 25 additions and 3 deletions

View File

@ -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')

View File

@ -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: