|
24 | 24 | import sys,os |
25 | 25 | from wolfcrypt import ciphers |
26 | 26 |
|
| 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 | + |
27 | 38 | Cfile_Banner="/* Public-key file for wolfBoot, automatically generated. Do not edit. */\n"+ \ |
28 | 39 | "/*\n" + \ |
29 | 40 | " * This file has been generated and contains the public key which is\n"+ \ |
|
38 | 49 |
|
39 | 50 | sign="ed25519" |
40 | 51 |
|
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 |
47 | 89 |
|
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] |
56 | 90 |
|
57 | 91 | if pubkey_cfile[-2:] != '.c': |
58 | 92 | print("** Warning: generated public key cfile does not have a '.c' extension") |
|
67 | 101 | if (sign == "ed25519"): |
68 | 102 | ed = ciphers.Ed25519Private.make_key(32) |
69 | 103 | priv,pub = ed.encode_key() |
70 | | - if os.path.exists(key_file): |
| 104 | + if os.path.exists(key_file) and not force: |
71 | 105 | choice = input("** Warning: key file already exist! Are you sure you want to "+ |
72 | 106 | "generate a new key and overwrite the existing key? [Type 'Yes, I am sure!']: ") |
73 | 107 | if (choice != "Yes, I am sure!"): |
|
98 | 132 | if (sign == "ecc256"): |
99 | 133 | ec = ciphers.EccPrivate.make_key(32) |
100 | 134 | qx,qy,d = ec.encode_key_raw() |
101 | | - if os.path.exists(key_file): |
| 135 | + if os.path.exists(key_file) and not force: |
102 | 136 | choice = input("** Warning: key file already exist! Are you sure you want to "+ |
103 | 137 | "generate a new key and overwrite the existing key? [Type 'Yes, I am sure!']: ") |
104 | 138 | if (choice != "Yes, I am sure!"): |
|
134 | 168 |
|
135 | 169 | if (sign == "rsa2048"): |
136 | 170 | rsa = ciphers.RsaPrivate.make_key(2048) |
137 | | - if os.path.exists(key_file): |
| 171 | + if os.path.exists(key_file) and not force: |
138 | 172 | choice = input("** Warning: key file already exist! Are you sure you want to "+ |
139 | 173 | "generate a new key and overwrite the existing key? [Type 'Yes, I am sure!']: ") |
140 | 174 | if (choice != "Yes, I am sure!"): |
|
162 | 196 |
|
163 | 197 | if (sign == "rsa4096"): |
164 | 198 | rsa = ciphers.RsaPrivate.make_key(4096) |
165 | | - if os.path.exists(key_file): |
| 199 | + if os.path.exists(key_file) and not force: |
166 | 200 | choice = input("** Warning: key file already exist! Are you sure you want to "+ |
167 | 201 | "generate a new key and overwrite the existing key? [Type 'Yes, I am sure!']: ") |
168 | 202 | if (choice != "Yes, I am sure!"): |
|
0 commit comments