Skip to content

Commit 151a35e

Browse files
committed
sign.c: Fixed parsing size of public key when in 'manual-sign' or 'sha-only'
mode.
1 parent 2b6d093 commit 151a35e

1 file changed

Lines changed: 33 additions & 13 deletions

File tree

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. */

0 commit comments

Comments
 (0)