gen-keys.py (1400B)
1 # This Source Code Form is subject to the terms of the Mozilla Public 2 # License, v. 2.0. If a copy of the MPL was not distributed with this 3 # file, You can obtain one at http://mozilla.org/MPL/2.0/. 4 5 import sys 6 7 from pyasn1.codec.der import decoder 8 from pyasn1.type import univ 9 from pyasn1_modules import pem 10 11 12 def public_key_to_string(file, name): 13 out = "static const unsigned char " + name + "[65] = { " 14 with open(file) as f: 15 substrate = pem.readPemFromFile( 16 f, "-----BEGIN PUBLIC KEY-----", "-----END PUBLIC KEY-----" 17 ) 18 key = decoder.decode(substrate) 19 ident = key[0][0] 20 assert ident[0] == univ.ObjectIdentifier("1.2.840.10045.2.1"), ( 21 "should be an ECPublicKey" 22 ) 23 assert ident[1] == univ.ObjectIdentifier("1.2.840.10045.3.1.7"), ( 24 "should be a EcdsaP256 key" 25 ) 26 bits = key[0][1] 27 assert isinstance(bits, univ.BitString), "Should be a bit string" 28 assert len(bits) == 520, "Should be 520 bits (65 bytes)" 29 for byte in bits.asOctets(): 30 out += hex(byte) + ", " 31 out += "};" 32 return out 33 34 35 def generate(output, test_key, prod_key): 36 output.write(public_key_to_string(test_key, "kTestKey")) 37 output.write("\n\n") 38 output.write(public_key_to_string(prod_key, "kProdKey")) 39 40 41 if __name__ == "__main__": 42 generate(sys.stdout, *sys.argv[1:])