Skip to content

Commit 2168518

Browse files
committed
Fix broken selection of primary algorithm
1 parent 438c097 commit 2168518

File tree

6 files changed

+39
-36
lines changed

6 files changed

+39
-36
lines changed

Makefile

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,11 @@ endif
143143

144144
ifneq ($(SIGN_SECONDARY),)
145145
SECONDARY_PRIVATE_KEY=wolfboot_signing_second_private_key.der
146-
MAIN_TARGET+=$(SECONDARY_PRIVATE_KEY)
147146
endif
148147

149148
ASFLAGS:=$(CFLAGS)
150149

151-
all: $(MAIN_TARGET)
150+
all: $(SECONDARY_PRIVATE_KEY) $(MAIN_TARGET)
152151

153152
stage1: stage1/loader_stage1.bin
154153
stage1/loader_stage1.bin: wolfboot.elf
@@ -208,12 +207,14 @@ $(PRIVATE_KEY):
208207
$(Q)(test $(SIGN) = NONE) && (echo "// SIGN=NONE" > src/keystore.c) || true
209208
$(Q)(test "$(FLASH_OTP_KEYSTORE)" = "1") && (make -C tools/keytools/otp) || true
210209

211-
$(SECONDARY_PRIVATE_KEY):
210+
$(SECONDARY_PRIVATE_KEY): $(PRIVATE_KEY)
212211
$(Q)$(MAKE) keytools_check
213212
$(Q)rm -f src/keystore.c
213+
$(Q)mv $(PRIVATE_KEY) primary.$(PRIVATE_KEY)
214214
$(Q)(test $(SIGN_SECONDARY) = NONE) || ("$(KEYGEN_TOOL)" \
215-
$(KEYGEN_OPTIONS) -i $(PRIVATE_KEY) $(SECONDARY_KEYGEN_OPTIONS) \
215+
$(KEYGEN_OPTIONS) -i primary.$(PRIVATE_KEY) $(SECONDARY_KEYGEN_OPTIONS) \
216216
-g $(SECONDARY_PRIVATE_KEY)) || true
217+
$(Q)mv primary.$(PRIVATE_KEY) $(PRIVATE_KEY)
217218
$(Q)(test "$(FLASH_OTP_KEYSTORE)" = "1") && (make -C tools/keytools/otp) || true
218219

219220
keytools: include/target.h

include/image.h

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,20 @@ int wolfBot_get_dts_size(void *dts_addr);
7979
#define SECT_FLAG_UPDATED 0x0f
8080
#endif
8181

82-
#define WOLFBOOT_SIGN_PRIMARY_ML_DSA
83-
8482
#ifdef WOLFBOOT_SIGN_PRIMARY_ED25519
8583
#define wolfBoot_verify_signature wolfBoot_verify_signature_ed25519
8684
#endif
8785
#ifdef WOLFBOOT_SIGN_PRIMARY_ED448
8886
#define wolfBoot_verify_signature wolfBoot_verify_signature_ed448
8987
#endif
90-
#ifdef WOLFBOOT_SIGN_PRIMARY_RSA
88+
#if defined (WOLFBOOT_SIGN_PRIMARY_RSA2048) || \
89+
defined (WOLFBOOT_SIGN_PRIMARY_RSA3072) || \
90+
defined (WOLFBOOT_SIGN_PRIMARY_RSA4096)
9191
#define wolfBoot_verify_signature wolfBoot_verify_signature_rsa
9292
#endif
93-
#ifdef WOLFBOOT_SIGN_PRIMARY_ECC
93+
#if defined (WOLFBOOT_SIGN_PRIMARY_ECC256) || \
94+
defined (WOLFBOOT_SIGN_PRIMARY_ECC384) || \
95+
defined (WOLFBOOT_SIGN_PRIMARY_ECC521)
9496
#define wolfBoot_verify_signature wolfBoot_verify_signature_ecc
9597
#endif
9698
#ifdef WOLFBOOT_SIGN_PRIMARY_LMS
@@ -560,16 +562,6 @@ struct wolfBoot_image {
560562
};
561563

562564
/* do not warn if this is not used */
563-
#if !defined(__CCRX__)
564-
static void __attribute__ ((unused)) wolfBoot_image_confirm_signature_ok(
565-
struct wolfBoot_image *img)
566-
{
567-
}
568-
static void __attribute__ ((unused)) wolfBoot_image_clear_signature_ok(
569-
struct wolfBoot_image *img)
570-
{
571-
}
572-
#else
573565
static void wolfBoot_image_confirm_signature_ok(struct wolfBoot_image *img)
574566
{
575567
img->signature_ok = 1;
@@ -578,7 +570,6 @@ static void wolfBoot_image_clear_signature_ok(struct wolfBoot_image *img)
578570
{
579571
img->signature_ok = 0;
580572
}
581-
#endif
582573

583574
#define likely(x) (x)
584575
#define unlikely(x) (x)

include/keystore.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ extern "C" {
3232
#ifndef KEYSTORE_PUBKEY_SIZE
3333
/* allow building version for external API use */
3434
#define KEYSTORE_ANY
35-
#ifdef ML_DSA_LEVEL
35+
#ifdef WOLFBOOT_SIGN_ML_DSA
3636
#define KEYSTORE_PUBKEY_SIZE KEYSTORE_PUBKEY_SIZE_ML_DSA
3737
#else
3838
#define KEYSTORE_PUBKEY_SIZE KEYSTORE_PUBKEY_SIZE_RSA4096

options.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,8 @@ ifneq ($(SIGN_SECONDARY),)
515515
CFLAGS+=-DSIGN_SECONDARY_$(SIGN_SECONDARY)
516516
endif
517517

518+
CFLAGS+=-DWOLFBOOT_SIGN_PRIMARY_$(SIGN)
519+
518520
ifeq ($(RAM_CODE),1)
519521
CFLAGS+= -D"RAM_CODE"
520522
endif

tools/keytools/keygen.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ static int saveAsDer = 0;
122122
static WC_RNG rng;
123123

124124
#ifndef KEYSLOT_MAX_PUBKEY_SIZE
125-
#if defined(WOLFSSL_WC_DILITHIUM)
125+
#if defined(KEYSTORE_PUBKEY_SIZE_ML_DSA)
126126
/* ML-DSA pub keys are big. */
127127
#define KEYSLOT_MAX_PUBKEY_SIZE KEYSTORE_PUBKEY_SIZE_ML_DSA
128128
#else
@@ -388,9 +388,11 @@ static uint32_t get_pubkey_size(uint32_t keyType)
388388
case KEYGEN_XMSS:
389389
size = KEYSTORE_PUBKEY_SIZE_XMSS;
390390
break;
391+
#ifdef KEYSTORE_PUBKEY_SIZE_ML_DSA
391392
case KEYGEN_ML_DSA:
392393
size = KEYSTORE_PUBKEY_SIZE_ML_DSA;
393394
break;
395+
#endif
394396
default:
395397
size = 0;
396398
}
@@ -1113,6 +1115,7 @@ int main(int argc, char** argv)
11131115
#ifdef DEBUG_SIGNTOOL
11141116
wolfSSL_Debugging_ON();
11151117
#endif
1118+
printf("Keystore size: %lu\n", sizeof(struct keystore_slot));
11161119

11171120
/* Check arguments and print usage */
11181121
if (argc < 2)

tools/keytools/sign.c

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,6 @@ static struct cmd_options CMD = {
309309

310310
};
311311

312-
313312
static int load_key_ecc(int sign_type, uint32_t curve_sz, int curve_id,
314313
int header_sz,
315314
uint8_t **key_buffer, uint32_t *key_buffer_sz,
@@ -323,11 +322,7 @@ static int load_key_ecc(int sign_type, uint32_t curve_sz, int curve_id,
323322

324323
*pubkey_sz = curve_sz * 2;
325324
*pubkey = malloc(*pubkey_sz); /* assume malloc works */
326-
327-
printf("Load key: %s", secondary?"secondary":"primary");
328-
printf(" Size: %d\n", *pubkey_sz);
329325
initRet = ret = wc_ecc_init(&key.ecc);
330-
331326
if (CMD.manual_sign || CMD.sha_only) {
332327
/* raw (public x + public y) */
333328
if (*key_buffer_sz == (curve_sz * 2)) {
@@ -520,7 +515,7 @@ static uint8_t *load_key(uint8_t **key_buffer, uint32_t *key_buffer_sz,
520515

521516
f = fopen(key_file, "rb");
522517
if (f == NULL) {
523-
printf("Open key file %s failed\n", CMD.key_file);
518+
printf("Open key file %s failed\n", key_file);
524519
goto failure;
525520
}
526521
fseek(f, 0, SEEK_END);
@@ -889,11 +884,12 @@ static uint8_t *load_key(uint8_t **key_buffer, uint32_t *key_buffer_sz,
889884
/* Sign the digest */
890885
static int sign_digest(int sign, int hash_algo,
891886
uint8_t* signature, uint32_t* signature_sz,
892-
uint8_t* digest, uint32_t digest_sz)
887+
uint8_t* digest, uint32_t digest_sz, int secondary)
893888
{
894889
int ret;
895890
WC_RNG rng;
896891
printf("Sign: %02x\n", sign >> 8);
892+
(void)secondary;
897893

898894
if ((ret = wc_InitRng(&rng)) != 0) {
899895
return ret;
@@ -976,6 +972,10 @@ static int sign_digest(int sign, int hash_algo,
976972
#endif
977973
#ifdef WOLFSSL_HAVE_LMS
978974
if (sign == SIGN_LMS) {
975+
const char *key_file = CMD.key_file;
976+
if (secondary) {
977+
key_file = CMD.secondary_key_file;
978+
}
979979
/* Set the callbacks, so LMS can update the private key while signing */
980980
ret = wc_LmsKey_SetWriteCb(&key.lms, lms_write_key);
981981
if (ret == 0) {
@@ -999,6 +999,10 @@ static int sign_digest(int sign, int hash_algo,
999999
#endif /* WOLFSSL_HAVE_LMS */
10001000
#ifdef WOLFSSL_HAVE_XMSS
10011001
if (sign == SIGN_XMSS) {
1002+
const char *key_file = CMD.key_file;
1003+
if (secondary) {
1004+
key_file = CMD.secondary_key_file;
1005+
}
10021006
ret = wc_XmssKey_Init(&key.xmss, NULL, INVALID_DEVID);
10031007
/* Set the callbacks, so XMSS can update the private key while signing */
10041008
if (ret == 0) {
@@ -1200,6 +1204,7 @@ static int make_header_ex(int is_diff, uint8_t *pubkey, uint32_t pubkey_sz,
12001204
if (ret == 0) {
12011205
ret = wc_InitSha256_ex(&sha, NULL, INVALID_DEVID);
12021206
if (ret == 0) {
1207+
printf("Hashing primary pubkey, size: %d\n", pubkey_sz);
12031208
ret = wc_Sha256Update(&sha, pubkey, pubkey_sz);
12041209
if (ret == 0)
12051210
wc_Sha256Final(&sha, buf);
@@ -1209,6 +1214,7 @@ static int make_header_ex(int is_diff, uint8_t *pubkey, uint32_t pubkey_sz,
12091214
/* secondary public key in hybrid mode */
12101215
if (ret == 0 && secondary_key_sz > 0) {
12111216
ret = wc_InitSha256_ex(&sha, NULL, INVALID_DEVID);
1217+
printf("Hashing secondary pubkey, size: %d\n", secondary_key_sz);
12121218
if (ret == 0) {
12131219
ret = wc_Sha256Update(&sha, secondary_key, secondary_key_sz);
12141220
if (ret == 0)
@@ -1387,7 +1393,7 @@ static int make_header_ex(int is_diff, uint8_t *pubkey, uint32_t pubkey_sz,
13871393
/* Sign the digest */
13881394
printf("CMD.sign == %02x\n", CMD.sign);
13891395
ret = sign_digest(CMD.sign, CMD.hash_algo,
1390-
signature, &CMD.signature_sz, digest, digest_sz);
1396+
signature, &CMD.signature_sz, digest, digest_sz, 0);
13911397
if (ret != 0) {
13921398
printf("Signing error %d\n", ret);
13931399
goto failure;
@@ -1419,7 +1425,7 @@ static int make_header_ex(int is_diff, uint8_t *pubkey, uint32_t pubkey_sz,
14191425
}
14201426
memset(secondary_signature, 0, CMD.secondary_signature_sz);
14211427
ret = sign_digest(CMD.secondary_sign, CMD.hash_algo,
1422-
secondary_signature, &CMD.secondary_signature_sz, digest, digest_sz);
1428+
secondary_signature, &CMD.secondary_signature_sz, digest, digest_sz, 1);
14231429
if (ret != 0) {
14241430
printf("Secondary Signing error %d\n", ret);
14251431
goto failure;
@@ -1471,7 +1477,7 @@ static int make_header_ex(int is_diff, uint8_t *pubkey, uint32_t pubkey_sz,
14711477
/* Policy is always SHA2-256 */
14721478
ret = sign_digest(CMD.sign, HASH_SHA256,
14731479
policy + sizeof(uint32_t), &CMD.policy_sz,
1474-
digest, digest_sz);
1480+
digest, digest_sz, 0);
14751481
if (ret != 0) {
14761482
printf("Signing policy error %d\n", ret);
14771483
goto failure;
@@ -2047,7 +2053,7 @@ static void set_signature_sizes(int secondary)
20472053
if (lms_ret != 0) {
20482054
fprintf(stderr, "error: wc_LmsKey_SetParameters(%d, %d, %d)" \
20492055
" returned %d\n", LMS_LEVELS, LMS_HEIGHT,
2050-
LMS_WINTERNITZ, ret);
2056+
LMS_WINTERNITZ, lms_ret);
20512057
exit(1);
20522058
}
20532059

@@ -2146,8 +2152,8 @@ int main(int argc, char** argv)
21462152
uint8_t buf[PATH_MAX-32]; /* leave room to avoid "directive output may be truncated" */
21472153
uint8_t *pubkey = NULL;
21482154
uint32_t pubkey_sz = 0;
2149-
uint8_t *kbuf=NULL, *key_buffer;
2150-
uint32_t key_buffer_sz;
2155+
uint8_t *kbuf=NULL, *key_buffer, *key_buffer2;
2156+
uint32_t key_buffer_sz, key_buffer_sz2;
21512157

21522158
#ifdef DEBUG_SIGNTOOL
21532159
wolfSSL_Debugging_ON();
@@ -2625,10 +2631,10 @@ int main(int argc, char** argv)
26252631

26262632
if (CMD.hybrid) {
26272633
uint8_t *kbuf2 = NULL;
2628-
uint32_t key_buffer_sz2;
26292634
uint8_t *pubkey2 = NULL;
26302635
uint32_t pubkey_sz2;
2631-
kbuf2 = load_key(&key_buffer, &key_buffer_sz2, &pubkey2, &pubkey_sz2, 1);
2636+
printf("Loading secondary key\n");
2637+
kbuf2 = load_key(&key_buffer2, &key_buffer_sz2, &pubkey2, &pubkey_sz2, 1);
26322638
printf("Creating hybrid signature\n");
26332639
make_hybrid_header(pubkey, pubkey_sz, CMD.image_file, CMD.output_image_file,
26342640
pubkey2, pubkey_sz2);

0 commit comments

Comments
 (0)