Skip to content

Commit d763951

Browse files
authored
Merge pull request #58 from wolfSSL/keytools-fix-pubkey-parser
Keytools fix pubkey parser
2 parents f2ba779 + e367cd1 commit d763951

3 files changed

Lines changed: 53 additions & 19 deletions

File tree

tools/keytools/keygen.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ int main(int argc, char** argv)
301301
fclose(f);
302302
printf("** Warning: key file already exist! Are you sure you want to generate a new key and overwrite the existing key? [Type 'Yes, I am sure!']: ");
303303
fflush(stdout);
304-
gets(reply);
304+
scanf("%s", reply);
305305
printf("Reply is [%s]\n", reply);
306306
if (strcmp(reply, "Yes, I am sure!") != 0) {
307307
printf("Operation aborted by user.");

tools/keytools/sign.c

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ static void header_append_u16(uint8_t* header, uint32_t* idx, uint16_t tmp16)
103103
memcpy(&header[*idx], &tmp16, sizeof(tmp16));
104104
*idx += sizeof(tmp16);
105105
}
106-
static void header_append_tag(uint8_t* header, uint32_t* idx, uint16_t tag,
106+
static void header_append_tag(uint8_t* header, uint32_t* idx, uint16_t tag,
107107
uint16_t len, void* data)
108108
{
109109
header_append_u16(header, idx, tag);
@@ -227,7 +227,7 @@ int main(int argc, char** argv)
227227
if (tmpstr) {
228228
*tmpstr = '\0'; /* null terminate at last "." */
229229
}
230-
snprintf(output_image_file, sizeof(output_image_file), "%s_v%s_%s.bin",
230+
snprintf(output_image_file, sizeof(output_image_file), "%s_v%s_%s.bin",
231231
(char*)buf, fw_version, sha_only ? "digest" : "signed");
232232

233233
printf("Update type: %s\n", self_update ? "wolfBoot" : "Firmware");
@@ -256,14 +256,34 @@ int main(int argc, char** argv)
256256
}
257257

258258
/* key type "auto" selection */
259-
if (key_buffer_sz == 64) {
260-
if (sign == SIGN_ECC256) {
261-
printf("Error: key size does not match the cipher selected\n");
259+
if (key_buffer_sz == 32) {
260+
if ((sign != SIGN_ED25519) && !manual_sign && !sha_only ) {
261+
printf("Error: key too short for cipher\n");
262262
goto exit;
263263
}
264-
if (sign == SIGN_AUTO) {
264+
if (sign == SIGN_AUTO && (manual_sign || sha_only)) {
265+
printf("ed25519 public key autodetected\n");
265266
sign = SIGN_ED25519;
266-
printf("ed25519 key autodetected\n");
267+
}
268+
269+
}
270+
else if (key_buffer_sz == 64) {
271+
if (sign == SIGN_ECC256) {
272+
if (!manual_sign && !sha_only) {
273+
printf("Error: key size does not match the cipher selected\n");
274+
goto exit;
275+
} else {
276+
printf("ECC256 public key detected\n");
277+
}
278+
}
279+
if (sign == SIGN_AUTO) {
280+
if (!manual_sign && !sha_only) {
281+
sign = SIGN_ED25519;
282+
printf("ed25519 key autodetected\n");
283+
} else {
284+
sign = SIGN_ECC256;
285+
printf("ecc256 public key autodetected\n");
286+
}
267287
}
268288
}
269289
else if (key_buffer_sz == 96) {
@@ -298,7 +318,7 @@ int main(int argc, char** argv)
298318
}
299319

300320
/* get header and signature sizes */
301-
if (sign == SIGN_ED25519) {
321+
if (sign == SIGN_ED25519) {
302322
header_sz = 256;
303323
signature_sz = 64;
304324
}
@@ -323,7 +343,7 @@ int main(int argc, char** argv)
323343
if (!sha_only && !manual_sign) {
324344
/* import (decode) private key for signing */
325345
if (sign == SIGN_ED25519) {
326-
#ifdef HAVE_ED25519
346+
#ifdef HAVE_ED25519
327347
ret = wc_ed25519_init(&key.ed);
328348
if (ret == 0) {
329349
pubkey = key_buffer + ED25519_KEY_SIZE;
@@ -336,7 +356,7 @@ int main(int argc, char** argv)
336356
#ifdef HAVE_ECC
337357
ret = wc_ecc_init(&key.ecc);
338358
if (ret == 0) {
339-
ret = wc_ecc_import_unsigned(&key.ecc, &key_buffer[0], &key_buffer[32],
359+
ret = wc_ecc_import_unsigned(&key.ecc, &key_buffer[0], &key_buffer[32],
340360
&key_buffer[64], ECC_SECP256R1);
341361
if (ret == 0) {
342362
pubkey = key_buffer; /* first 64 bytes is public porition */
@@ -405,22 +425,22 @@ int main(int argc, char** argv)
405425

406426
/* Append Version field */
407427
fw_version32 = strtol(fw_version, NULL, 10);
408-
header_append_tag(header, &header_idx, HDR_VERSION, HDR_VERSION_LEN,
428+
header_append_tag(header, &header_idx, HDR_VERSION, HDR_VERSION_LEN,
409429
&fw_version32);
410430

411431
/* Append Four pad bytes, so timestamp is aligned */
412432
header_idx += 4; /* memset 0xFF above handles value */
413433

414434
/* Append Timestamp field */
415435
stat(image_file, &attrib);
416-
header_append_tag(header, &header_idx, HDR_TIMESTAMP, HDR_TIMESTAMP_LEN,
436+
header_append_tag(header, &header_idx, HDR_TIMESTAMP, HDR_TIMESTAMP_LEN,
417437
&attrib.st_ctime);
418438

419439
/* Append Image type field */
420440
image_type = (uint16_t)sign;
421441
if (!self_update)
422442
image_type |= HDR_IMG_TYPE_APP;
423-
header_append_tag(header, &header_idx, HDR_IMG_TYPE, HDR_IMG_TYPE_LEN,
443+
header_append_tag(header, &header_idx, HDR_IMG_TYPE, HDR_IMG_TYPE_LEN,
424444
&image_type);
425445

426446
/* Six pad bytes, Sha-3 requires 8-byte alignment. */

tools/keytools/sign.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,27 @@
135135
kf = open(key_file, "rb")
136136
wolfboot_key_buffer = kf.read(4096)
137137
wolfboot_key_buffer_len = len(wolfboot_key_buffer)
138-
if wolfboot_key_buffer_len == 64:
139-
if (sign == 'ecc256'):
140-
print("Error: key size does not match the cipher selected")
138+
if wolfboot_key_buffer_len == 32:
139+
if (sign != 'ed25519' and not manual_sign and not sha_only):
140+
print("Error: key too short for cipher")
141141
sys.exit(1)
142-
if sign == 'auto':
142+
elif sign == 'auto' and (manual_sign or sha_only):
143143
sign = 'ed25519'
144-
print("'ed25519' key autodetected.")
144+
print("'ed25519' public key autodetected.")
145+
elif wolfboot_key_buffer_len == 64:
146+
if (sign == 'ecc256'):
147+
if not manual_sign and not sha_only:
148+
print("Error: key size does not match the cipher selected")
149+
sys.exit(1)
150+
else:
151+
print("Ecc256 public key detected")
152+
if sign == 'auto':
153+
if (manual_sign or sha_only):
154+
sign = 'ecc256'
155+
print("'ecc256' public key autodetected.")
156+
else:
157+
sign = 'ed25519'
158+
print("'ed25519' key autodetected.")
145159
elif wolfboot_key_buffer_len == 96:
146160
if (sign == 'ed25519'):
147161
print("Error: key size does not match the cipher selected")

0 commit comments

Comments
 (0)