Skip to content

Commit 91cacce

Browse files
committed
Added python keygen tool
1 parent 8852c01 commit 91cacce

2 files changed

Lines changed: 101 additions & 0 deletions

File tree

tools/keytools/keygen.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/usr/bin/python3
2+
3+
import sys,os
4+
from wolfcrypt import ciphers
5+
6+
Cfile_Banner="/* Public-key file for wolfBoot, automatically generated. Do not edit. */\n"+ \
7+
"/*\n" + \
8+
" * This file has been generated and contains the public key which is\n"+ \
9+
" * used by wolfBoot to verify the updates.\n"+ \
10+
" */" \
11+
"\n#include <stdint.h>\n\n" + \
12+
"const uint8_t ed25519_pub_key[32] = {\n"
13+
14+
sign="ed25519"
15+
16+
argc = len(sys.argv)
17+
argv = sys.argv
18+
19+
if (argc < 2) or (argc > 3):
20+
print("Usage: %s [--ed25519 | --ecc256 ] pub_key_file.c\n" % sys.argv[0])
21+
sys.exit(1)
22+
23+
if argc == 3:
24+
if argv[1] != '--ed25519' and argv[1] != '--ecc256':
25+
print("Usage: %s [--ed25519 | --ecc256 ] pub_key_file.c\n" % sys.argv[0])
26+
sys.exit(1)
27+
sign=argv[1][2:]
28+
pubkey_cfile = argv[2]
29+
else:
30+
pubkey_cfile = argv[1]
31+
32+
if pubkey_cfile[-2:] != '.c':
33+
print("** Warning: generated public key cfile does not have a '.c' extension")
34+
35+
key_file=sign+".der"
36+
37+
print ("Selected cipher: " + sign)
38+
print ("Output Private key: " + key_file)
39+
print ("Output C file: " + pubkey_cfile)
40+
41+
if (sign == "ed25519"):
42+
ed = ciphers.Ed25519Private.make_key(32)
43+
priv,pub = ed.encode_key()
44+
if os.path.exists(key_file):
45+
choice = input("** Warning: key file already exist! Are you sure you want to "+
46+
"generate a new key and overwrite the existing key? [Type 'Yes, I am sure!']: ")
47+
if (choice != "Yes, I am sure!"):
48+
print("Operation canceled.")
49+
sys.exit(2)
50+
51+
print()
52+
print("Creating file " + key_file)
53+
with open(key_file, "wb") as f:
54+
f.write(priv)
55+
f.write(pub)
56+
f.close()
57+
print("Creating file " + pubkey_cfile)
58+
with open(pubkey_cfile, "w") as f:
59+
f.write(Cfile_Banner)
60+
i = 0
61+
for c in bytes(pub[0:-1]):
62+
f.write("0x%02X, " % c)
63+
i += 1
64+
if (i % 8 == 0):
65+
f.write('\n')
66+
f.write("0x%02X" % pub[-1])
67+
f.write("\n};\n")
68+
f.write("const uint32_t ed25519_pub_key_len = 32;\n")
69+
f.close()
70+
71+
if (sign == "ecc256"):
72+
ec = ciphers.EccPrivate.make_key(64)
73+
priv = ec.encode_key()
74+
if os.path.exists(key_file):
75+
choice = input("** Warning: key file already exist! Are you sure you want to "+
76+
"generate a new key and overwrite the existing key? [Type 'Yes, I am sure!']: ")
77+
if (choice != "Yes, I am sure!"):
78+
print("Operation canceled.")
79+
sys.exit(2)
80+
81+
print()
82+
print("Creating file " + key_file)
83+
with open(key_file, "wb") as f:
84+
f.write(priv)
85+
f.close()
86+
87+

tools/keytools/test.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* Public-key file for wolfBoot, automatically generated. Do not edit. */
2+
/*
3+
* This file has been generated and contains the public key which is
4+
* used by wolfBoot to verify the updates.
5+
*/
6+
#include <stdint.h>
7+
8+
const uint8_t ed25519_pub_key[32] = {
9+
0xB3, 0xAE, 0xD5, 0xEC, 0x20, 0xB0, 0x59, 0x99,
10+
0x2C, 0x0D, 0x76, 0x67, 0xA7, 0x29, 0x0A, 0x6D,
11+
0x34, 0xD0, 0xDA, 0xBC, 0x7B, 0xDB, 0x2C, 0x6D,
12+
0x2F, 0x2A, 0xD5, 0xAE, 0xE1, 0x41, 0x02, 0x46
13+
};
14+
const uint32_t ed25519_pub_key_len = 32;

0 commit comments

Comments
 (0)