Skip to content

Commit a64fa8f

Browse files
committed
Addressed Fenrir's review comments + fixed more regressions
1 parent 00342ac commit a64fa8f

8 files changed

Lines changed: 70 additions & 28 deletions

File tree

hal/stm32h5.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
#elif defined(WOLFBOOT_HASH_SHA3_384)
4343
#include <wolfssl/wolfcrypt/sha3.h>
4444
#endif
45-
#include <wolfssl/wolfcrypt/memory.h>
4645
#endif
4746

4847
#define PLL_SRC_HSE 1
@@ -247,6 +246,14 @@ static int buffer_is_all_value(const uint8_t *buf, size_t len, uint8_t value)
247246
return 1;
248247
}
249248

249+
static NOINLINEFUNCTION void hal_secret_zeroize(void *ptr, size_t len)
250+
{
251+
volatile uint8_t *p = (volatile uint8_t *)ptr;
252+
while (len-- > 0U) {
253+
*p++ = 0U;
254+
}
255+
}
256+
250257
int hal_uds_derive_key(uint8_t *out, size_t out_len)
251258
{
252259
#if defined(FLASH_OTP_KEYSTORE)
@@ -273,11 +280,11 @@ int hal_uds_derive_key(uint8_t *out, size_t out_len)
273280
copy_len = out_len;
274281
}
275282
memcpy(out, uds, copy_len);
276-
wc_ForceZero(uds, sizeof(uds));
283+
hal_secret_zeroize(uds, sizeof(uds));
277284
return 0;
278285
}
279286
}
280-
wc_ForceZero(uds, sizeof(uds));
287+
hal_secret_zeroize(uds, sizeof(uds));
281288
#endif
282289

283290
#ifdef WOLFBOOT_UDS_UID_FALLBACK_FORTEST

src/delta.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,6 @@ int wb_patch(WB_PATCH_CTX *ctx, uint8_t *dst, uint32_t len)
185185
#include <stdio.h>
186186
#include <stdlib.h>
187187
#include <errno.h>
188-
#include <limits.h> /* INT_MAX */
189188
#include <inttypes.h> /* PRIu32 */
190189

191190
static uint32_t wolfboot_sector_size = 0;
@@ -239,11 +238,6 @@ int wb_diff_get_sector_size(void)
239238
sec_sz);
240239
exit(6);
241240
}
242-
if (sec_sz > (uint32_t)INT_MAX) {
243-
fprintf(stderr, "WOLFBOOT_SECTOR_SIZE (%" PRIu32 ") exceeds INT_MAX (%d)\n",
244-
sec_sz, INT_MAX);
245-
exit(6);
246-
}
247241
return (int)sec_sz;
248242
}
249243

src/dice/dice.c

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
#include <wolfssl/wolfcrypt/random.h>
3636
#include <wolfssl/wolfcrypt/sha256.h>
3737
#include <wolfssl/wolfcrypt/integer.h>
38-
#include <wolfssl/wolfcrypt/memory.h>
3938

4039
#if defined(WOLFBOOT_HASH_SHA384)
4140
#include <wolfssl/wolfcrypt/sha512.h>
@@ -68,6 +67,14 @@
6867
#define WOLFBOOT_DICE_ERR_HW -3
6968
#define WOLFBOOT_DICE_ERR_CRYPTO -4
7069

70+
static NOINLINEFUNCTION void wolfboot_dice_zeroize(void *ptr, size_t len)
71+
{
72+
volatile uint8_t *p = (volatile uint8_t *)ptr;
73+
while (len-- > 0U) {
74+
*p++ = 0U;
75+
}
76+
}
77+
7178
#define COSE_LABEL_ALG 1
7279
#define COSE_ALG_ES256 (-7)
7380

@@ -621,7 +628,7 @@ static int wolfboot_dice_derive_attestation_key(ecc_key *key,
621628
goto cleanup;
622629
}
623630
/* CDI is no longer needed once the seed has been derived. */
624-
wc_ForceZero(cdi, sizeof(cdi));
631+
wolfboot_dice_zeroize(cdi, sizeof(cdi));
625632

626633
if (wolfboot_dice_hkdf(seed, sizeof(seed),
627634
(const uint8_t *)"WOLFBOOT-IAK", 12,
@@ -630,7 +637,7 @@ static int wolfboot_dice_derive_attestation_key(ecc_key *key,
630637
goto cleanup;
631638
}
632639
/* Seed is no longer needed once the private key material is derived. */
633-
wc_ForceZero(seed, sizeof(seed));
640+
wolfboot_dice_zeroize(seed, sizeof(seed));
634641

635642
if (wolfboot_dice_fixup_priv(priv, sizeof(priv)) != 0) {
636643
goto cleanup;
@@ -644,9 +651,9 @@ static int wolfboot_dice_derive_attestation_key(ecc_key *key,
644651
ret = 0;
645652

646653
cleanup:
647-
wc_ForceZero(priv, sizeof(priv));
648-
wc_ForceZero(seed, sizeof(seed));
649-
wc_ForceZero(cdi, sizeof(cdi));
654+
wolfboot_dice_zeroize(priv, sizeof(priv));
655+
wolfboot_dice_zeroize(seed, sizeof(seed));
656+
wolfboot_dice_zeroize(cdi, sizeof(cdi));
650657
return ret;
651658
}
652659

@@ -675,7 +682,7 @@ static int wolfboot_attest_get_private_key(ecc_key *key,
675682
ret = 0;
676683

677684
cleanup:
678-
wc_ForceZero(priv, sizeof(priv));
685+
wolfboot_dice_zeroize(priv, sizeof(priv));
679686
return ret;
680687
}
681688
#else
@@ -684,7 +691,7 @@ static int wolfboot_attest_get_private_key(ecc_key *key,
684691
if (hal_uds_derive_key(uds, uds_len) == 0) {
685692
ret = wolfboot_dice_derive_attestation_key(key, uds, uds_len, claims);
686693
}
687-
wc_ForceZero(uds, sizeof(uds));
694+
wolfboot_dice_zeroize(uds, sizeof(uds));
688695
return ret;
689696
#endif
690697
}
@@ -870,10 +877,10 @@ static int wolfboot_dice_sign_tbs(const uint8_t *tbs,
870877
}
871878
if (key_inited) {
872879
wc_ecc_free(&key);
873-
wc_ForceZero(&key, sizeof(key));
880+
wolfboot_dice_zeroize(&key, sizeof(key));
874881
}
875-
wc_ForceZero(hash, sizeof(hash));
876-
wc_ForceZero(der_sig, sizeof(der_sig));
882+
wolfboot_dice_zeroize(hash, sizeof(hash));
883+
wolfboot_dice_zeroize(der_sig, sizeof(der_sig));
877884
return ret;
878885
}
879886

src/tpm.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,17 @@ WOLFTPM2_KEY wolftpm_srk;
4444
#endif
4545

4646
#if defined(WOLFBOOT_TPM_SEAL) || defined(WOLFBOOT_TPM_KEYSTORE)
47-
int wolfBoot_constant_compare(const uint8_t* a, const uint8_t* b,
47+
int NOINLINEFUNCTION wolfBoot_constant_compare(const uint8_t* a, const uint8_t* b,
4848
uint32_t len)
4949
{
5050
uint32_t i;
51-
volatile uint8_t diff = 0;
51+
volatile uint32_t diff = 0U;
5252

5353
for (i = 0; i < len; i++) {
54-
diff |= a[i] ^ b[i];
54+
diff |= (uint32_t)(a[i] ^ b[i]);
5555
}
5656

57-
return diff;
57+
return (int)diff;
5858
}
5959

6060
void wolfBoot_print_hexstr(const unsigned char* bin, unsigned long sz,

src/update_flash.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ static int RAMFUNCTION wolfBoot_swap_and_final_erase(int resume)
567567
# define DELTA_BLOCK_SIZE 1024
568568
#endif
569569

570-
static inline uint32_t im2n(uint32_t val)
570+
static inline uint32_t wb_delta_im2n(uint32_t val)
571571
{
572572
#ifdef BIG_ENDIAN_ORDER
573573
return (((val & 0x000000FFU) << 24) |
@@ -631,9 +631,9 @@ static int wolfBoot_delta_update(struct wolfBoot_image *boot,
631631
&delta_base_hash, &delta_base_hash_sz) < 0) {
632632
return -1;
633633
}
634-
delta_img_size = im2n(*img_size);
634+
delta_img_size = wb_delta_im2n(*img_size);
635635
if (inverse)
636-
delta_img_offset = im2n(*img_offset);
636+
delta_img_offset = wb_delta_im2n(*img_offset);
637637
cur_v = wolfBoot_current_firmware_version();
638638
upd_v = wolfBoot_update_firmware_version();
639639
delta_base_v = wolfBoot_get_diffbase_version(PART_UPDATE);

tools/keytools/otp/Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ CFLAGS+=-I. -I../../../ -I../../../include -I$(WOLFBOOT_LIB_WOLFSSL) \
1919
CFLAGS+=-I./wcs
2020
CFLAGS+=-DFLASH_OTP_KEYSTORE -D__FLASH_OTP_PRIMER -DWOLFSSL_USER_SETTINGS -DWOLFCRYPT_SECURE_MODE
2121
PRI_KS_OBJS+=startup.o otp-keystore-primer.o ../../../src/keystore.o
22-
PRI_KS_OBJS+=$(WOLFBOOT_LIB_WOLFSSL)/wolfcrypt/src/memory.o
2322

2423
ifeq ($(HASH),SHA256)
2524
PRI_KS_OBJS+=$(WOLFBOOT_LIB_WOLFSSL)/wolfcrypt/src/sha256.o

tools/keytools/otp/target.ld

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ SECTIONS
1515
*(.fini)
1616
*(.text*)
1717
*(.rodata*)
18+
KEEP(*(.keystore*))
1819
. = ALIGN(4);
1920
_end_text = .;
2021
} > FLASH

tools/unit-tests/unit-update-flash.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,38 @@ START_TEST (test_empty_panic)
573573
}
574574
END_TEST
575575

576+
START_TEST (test_part_sanity_check_panics_on_sha_mismatch)
577+
{
578+
struct wolfBoot_image img;
579+
580+
memset(&img, 0, sizeof(img));
581+
img.hdr_ok = 1;
582+
img.sha_ok = 0;
583+
img.signature_ok = 1;
584+
wolfBoot_panicked = 0;
585+
586+
PART_SANITY_CHECK(&img);
587+
ck_assert_int_eq(wolfBoot_panicked, 1);
588+
wolfBoot_panicked = 0;
589+
}
590+
END_TEST
591+
592+
START_TEST (test_part_sanity_check_panics_on_signature_mismatch)
593+
{
594+
struct wolfBoot_image img;
595+
596+
memset(&img, 0, sizeof(img));
597+
img.hdr_ok = 1;
598+
img.sha_ok = 1;
599+
img.signature_ok = 0;
600+
wolfBoot_panicked = 0;
601+
602+
PART_SANITY_CHECK(&img);
603+
ck_assert_int_eq(wolfBoot_panicked, 1);
604+
wolfBoot_panicked = 0;
605+
}
606+
END_TEST
607+
576608
#ifdef EXT_ENCRYPTED
577609
START_TEST (test_fallback_image_verification_rejects_corruption)
578610
{
@@ -1297,6 +1329,8 @@ Suite *wolfboot_suite(void)
12971329
return s;
12981330
#else
12991331
tcase_add_test(empty_panic, test_empty_panic);
1332+
tcase_add_test(empty_panic, test_part_sanity_check_panics_on_sha_mismatch);
1333+
tcase_add_test(empty_panic, test_part_sanity_check_panics_on_signature_mismatch);
13001334
tcase_add_test(sunnyday_noupdate, test_sunnyday_noupdate);
13011335
tcase_add_test(forward_update_samesize, test_forward_update_samesize);
13021336
tcase_add_test(forward_update_tolarger, test_forward_update_tolarger);

0 commit comments

Comments
 (0)