Skip to content

Commit cd209e9

Browse files
committed
Added --force option to keygen
1 parent 5d80dcf commit cd209e9

1 file changed

Lines changed: 52 additions & 18 deletions

File tree

tools/keytools/keygen.py

Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@
2424
import sys,os
2525
from wolfcrypt import ciphers
2626

27+
def usage():
28+
print("Usage: %s [--ed25519 | --ecc256 | --rsa2048 | --rsa4096] [ --force ] pub_key_file.c\n" % sys.argv[0])
29+
parser.print_help()
30+
sys.exit(1)
31+
32+
def dupsign():
33+
print("")
34+
print("Error: only one algorithm must be specified.")
35+
print("")
36+
usage()
37+
2738
Cfile_Banner="/* Public-key file for wolfBoot, automatically generated. Do not edit. */\n"+ \
2839
"/*\n" + \
2940
" * This file has been generated and contains the public key which is\n"+ \
@@ -38,21 +49,44 @@
3849

3950
sign="ed25519"
4051

41-
argc = len(sys.argv)
42-
argv = sys.argv
43-
44-
if (argc < 2) or (argc > 3):
45-
print("Usage: %s [--ed25519 | --ecc256 | --rsa2048 | --rsa4096 ] pub_key_file.c\n" % sys.argv[0])
46-
sys.exit(1)
52+
import argparse as ap
53+
54+
parser = ap.ArgumentParser(prog='keygen.py', description='wolfBoot key generation tool')
55+
parser.add_argument('--ed25519', dest='ed25519', action='store_true')
56+
parser.add_argument('--ecc256', dest='ecc256', action='store_true')
57+
parser.add_argument('--rsa2048', dest='rsa2048', action='store_true')
58+
parser.add_argument('--rsa4096', dest='rsa4096', action='store_true')
59+
parser.add_argument('--force', dest='force', action='store_true')
60+
parser.add_argument('cfile')
61+
62+
args=parser.parse_args()
63+
64+
#print(args.ecc256)
65+
#sys.exit(0) #test
66+
67+
pubkey_cfile = args.cfile
68+
sign=None
69+
force=False
70+
if (args.ed25519):
71+
sign='ed25519'
72+
if (args.ecc256):
73+
if sign is not None:
74+
dupsign()
75+
sign='ecc256'
76+
if (args.rsa2048):
77+
if sign is not None:
78+
dupsign()
79+
sign='rsa2048'
80+
if (args.rsa4096):
81+
if sign is not None:
82+
dupsign()
83+
sign='rsa4096'
84+
85+
if sign is None:
86+
usage()
87+
88+
force = args.force
4789

48-
if argc == 3:
49-
if argv[1] != '--ed25519' and argv[1] != '--ecc256' and argv[1] != '--rsa2048' and argv[1] != '--rsa4096':
50-
print("Usage: %s [--ed25519 | --ecc256 | --rsa2048 | --rsa4096] pub_key_file.c\n" % sys.argv[0])
51-
sys.exit(1)
52-
sign=argv[1][2:]
53-
pubkey_cfile = argv[2]
54-
else:
55-
pubkey_cfile = argv[1]
5690

5791
if pubkey_cfile[-2:] != '.c':
5892
print("** Warning: generated public key cfile does not have a '.c' extension")
@@ -67,7 +101,7 @@
67101
if (sign == "ed25519"):
68102
ed = ciphers.Ed25519Private.make_key(32)
69103
priv,pub = ed.encode_key()
70-
if os.path.exists(key_file):
104+
if os.path.exists(key_file) and not force:
71105
choice = input("** Warning: key file already exist! Are you sure you want to "+
72106
"generate a new key and overwrite the existing key? [Type 'Yes, I am sure!']: ")
73107
if (choice != "Yes, I am sure!"):
@@ -98,7 +132,7 @@
98132
if (sign == "ecc256"):
99133
ec = ciphers.EccPrivate.make_key(32)
100134
qx,qy,d = ec.encode_key_raw()
101-
if os.path.exists(key_file):
135+
if os.path.exists(key_file) and not force:
102136
choice = input("** Warning: key file already exist! Are you sure you want to "+
103137
"generate a new key and overwrite the existing key? [Type 'Yes, I am sure!']: ")
104138
if (choice != "Yes, I am sure!"):
@@ -134,7 +168,7 @@
134168

135169
if (sign == "rsa2048"):
136170
rsa = ciphers.RsaPrivate.make_key(2048)
137-
if os.path.exists(key_file):
171+
if os.path.exists(key_file) and not force:
138172
choice = input("** Warning: key file already exist! Are you sure you want to "+
139173
"generate a new key and overwrite the existing key? [Type 'Yes, I am sure!']: ")
140174
if (choice != "Yes, I am sure!"):
@@ -162,7 +196,7 @@
162196

163197
if (sign == "rsa4096"):
164198
rsa = ciphers.RsaPrivate.make_key(4096)
165-
if os.path.exists(key_file):
199+
if os.path.exists(key_file) and not force:
166200
choice = input("** Warning: key file already exist! Are you sure you want to "+
167201
"generate a new key and overwrite the existing key? [Type 'Yes, I am sure!']: ")
168202
if (choice != "Yes, I am sure!"):

0 commit comments

Comments
 (0)