Skip to content

Commit 877ffea

Browse files
authored
Merge pull request #748 from danielinux/fixes-20260414
Fixes 20260414
2 parents 58c2e04 + 0809d83 commit 877ffea

35 files changed

+1760
-139
lines changed

.gitignore

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ include/target.h
114114
.wolfboot-offset
115115
.wolfboot-partition-size
116116
.bootloader-partition-size
117+
NVChip
117118
MPLabX/wolfBoot-SAME51.X/.generated_files/
118119
test-dummy-ca/**
119120

@@ -181,6 +182,12 @@ tools/unit-tests/unit-policy-create
181182
tools/unit-tests/unit-sign-encrypted-output
182183
tools/unit-tests/unit-update-flash-delta
183184
tools/unit-tests/unit-update-flash-self-update
185+
tools/unit-tests/unit-loader-tpm-init
186+
tools/unit-tests/unit-update-ram-nofixed
187+
tools/unit-tests/unit-max-space
188+
tools/unit-tests/unit-sdhci-disk-unaligned
189+
190+
184191

185192

186193

@@ -373,4 +380,3 @@ system-default.dtb
373380
test_output/
374381
sdcard.img
375382

376-

config/examples/hifive1.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ V?=0
1414
SPMATH?=1
1515
RAM_CODE?=1
1616
DUALBANK_SWAP?=0
17-
WOLFBOOT_PARTITION_SIZE?=0x80000
17+
WOLFBOOT_PARTITION_SIZE?=0x40000
1818
WOLFBOOT_SECTOR_SIZE?=0x1000
1919
WOLFBOOT_PARTITION_BOOT_ADDRESS?=0x20020000
2020
WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x20060000

hal/library.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ void hal_prepare_boot(void)
8383
return;
8484
}
8585

86+
void WEAKFUNCTION wolfBoot_panic(void)
87+
{
88+
wolfBoot_printf("wolfBoot: PANIC!\n");
89+
exit('P');
90+
}
91+
8692
int do_boot(uint32_t* v)
8793
{
8894
wolfBoot_printf("booting %p"
@@ -143,6 +149,13 @@ int wolfBoot_start(void)
143149

144150
wolfBoot_printf("Firmware Valid\n");
145151

152+
#ifndef WOLFBOOT_SKIP_BOOT_VERIFY
153+
if ((os_image.hdr_ok != 1U) || (os_image.sha_ok != 1U) ||
154+
(os_image.signature_ok != 1U)) {
155+
wolfBoot_panic();
156+
}
157+
PART_SANITY_CHECK(&os_image);
158+
#endif
146159
do_boot((uint32_t*)os_image.fw_base);
147160

148161
exit:

hal/stm32h5.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,14 @@ static int buffer_is_all_value(const uint8_t *buf, size_t len, uint8_t value)
246246
return 1;
247247
}
248248

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+
249257
int hal_uds_derive_key(uint8_t *out, size_t out_len)
250258
{
251259
#if defined(FLASH_OTP_KEYSTORE)
@@ -272,9 +280,11 @@ int hal_uds_derive_key(uint8_t *out, size_t out_len)
272280
copy_len = out_len;
273281
}
274282
memcpy(out, uds, copy_len);
283+
hal_secret_zeroize(uds, sizeof(uds));
275284
return 0;
276285
}
277286
}
287+
hal_secret_zeroize(uds, sizeof(uds));
278288
#endif
279289

280290
#ifdef WOLFBOOT_UDS_UID_FALLBACK_FORTEST

include/MPLAB/target.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,41 @@
9090
#define WOLFBOOT_DTS_BOOT_ADDRESS
9191
#define WOLFBOOT_DTS_UPDATE_ADDRESS
9292

93+
#if !defined(WOLFBOOT_PART_USE_ARCH_OFFSET) && !defined(PULL_LINKER_DEFINES)
94+
/*
95+
* Only compare partitions that share the same internal flash address
96+
* space. External partitions and runtime/linker-provided addresses are
97+
* validated elsewhere.
98+
*/
99+
#if !defined(PART_BOOT_EXT) && !defined(PART_UPDATE_EXT) && \
100+
(WOLFBOOT_PARTITION_UPDATE_ADDRESS != 0) && \
101+
((WOLFBOOT_PARTITION_BOOT_ADDRESS + WOLFBOOT_PARTITION_SIZE) > \
102+
WOLFBOOT_PARTITION_UPDATE_ADDRESS) && \
103+
(WOLFBOOT_PARTITION_BOOT_ADDRESS < \
104+
(WOLFBOOT_PARTITION_UPDATE_ADDRESS + WOLFBOOT_PARTITION_SIZE))
105+
#error "Boot and update partitions overlap"
106+
#endif
107+
108+
#if !defined(PART_BOOT_EXT) && !defined(PART_SWAP_EXT) && \
109+
(WOLFBOOT_PARTITION_SWAP_ADDRESS != 0) && \
110+
((WOLFBOOT_PARTITION_BOOT_ADDRESS + WOLFBOOT_PARTITION_SIZE) > \
111+
WOLFBOOT_PARTITION_SWAP_ADDRESS) && \
112+
(WOLFBOOT_PARTITION_BOOT_ADDRESS < \
113+
(WOLFBOOT_PARTITION_SWAP_ADDRESS + WOLFBOOT_SECTOR_SIZE))
114+
#error "Boot and swap partitions overlap"
115+
#endif
116+
117+
#if !defined(PART_UPDATE_EXT) && !defined(PART_SWAP_EXT) && \
118+
(WOLFBOOT_PARTITION_UPDATE_ADDRESS != 0) && \
119+
(WOLFBOOT_PARTITION_SWAP_ADDRESS != 0) && \
120+
((WOLFBOOT_PARTITION_UPDATE_ADDRESS + WOLFBOOT_PARTITION_SIZE) > \
121+
WOLFBOOT_PARTITION_SWAP_ADDRESS) && \
122+
(WOLFBOOT_PARTITION_UPDATE_ADDRESS < \
123+
(WOLFBOOT_PARTITION_SWAP_ADDRESS + WOLFBOOT_SECTOR_SIZE))
124+
#error "Update and swap partitions overlap"
125+
#endif
126+
#endif
127+
93128
#endif /* WOLFBOOT_FIXED_PARTITIONS */
94129

95130
/* Load address in RAM for staged OS (update_ram only) */

include/image.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1395,7 +1395,7 @@ static inline int wb_flash_write_verify_word(struct wolfBoot_image *img,
13951395
#ifndef EXT_ENCRYPTED
13961396
#define WOLFBOOT_MAX_SPACE (WOLFBOOT_PARTITION_SIZE - \
13971397
(TRAILER_SKIP + sizeof(uint32_t) + \
1398-
(WOLFBOOT_PARTITION_SIZE + 1 / (WOLFBOOT_SECTOR_SIZE * 8))))
1398+
((WOLFBOOT_PARTITION_SIZE + 1) / (WOLFBOOT_SECTOR_SIZE * 8))))
13991399
#else
14001400
#define WOLFBOOT_MAX_SPACE (WOLFBOOT_PARTITION_SIZE - ENCRYPT_TMP_SECRET_OFFSET)
14011401
#endif

include/target.h.in

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,42 @@
110110
#define WOLFBOOT_DTS_BOOT_ADDRESS @WOLFBOOT_DTS_BOOT_ADDRESS@
111111
#define WOLFBOOT_DTS_UPDATE_ADDRESS @WOLFBOOT_DTS_UPDATE_ADDRESS@
112112

113+
#if defined(WOLFBOOT_FIXED_PARTITIONS) && \
114+
!defined(WOLFBOOT_PART_USE_ARCH_OFFSET) && !defined(PULL_LINKER_DEFINES)
115+
/*
116+
* Only compare partitions that share the same internal flash address
117+
* space. External partitions and runtime/linker-provided addresses are
118+
* validated elsewhere.
119+
*/
120+
#if !defined(PART_BOOT_EXT) && !defined(PART_UPDATE_EXT) && \
121+
((WOLFBOOT_PARTITION_UPDATE_ADDRESS + 0) != 0) && \
122+
((WOLFBOOT_PARTITION_BOOT_ADDRESS + 0 + WOLFBOOT_PARTITION_SIZE + 0) > \
123+
(WOLFBOOT_PARTITION_UPDATE_ADDRESS + 0)) && \
124+
((WOLFBOOT_PARTITION_BOOT_ADDRESS + 0) < \
125+
(WOLFBOOT_PARTITION_UPDATE_ADDRESS + 0 + WOLFBOOT_PARTITION_SIZE + 0))
126+
#error "Boot and update partitions overlap"
127+
#endif
128+
129+
#if !defined(PART_BOOT_EXT) && !defined(PART_SWAP_EXT) && \
130+
((WOLFBOOT_PARTITION_SWAP_ADDRESS + 0) != 0) && \
131+
((WOLFBOOT_PARTITION_BOOT_ADDRESS + 0 + WOLFBOOT_PARTITION_SIZE + 0) > \
132+
(WOLFBOOT_PARTITION_SWAP_ADDRESS + 0)) && \
133+
((WOLFBOOT_PARTITION_BOOT_ADDRESS + 0) < \
134+
(WOLFBOOT_PARTITION_SWAP_ADDRESS + 0 + WOLFBOOT_SECTOR_SIZE))
135+
#error "Boot and swap partitions overlap"
136+
#endif
137+
138+
#if !defined(PART_UPDATE_EXT) && !defined(PART_SWAP_EXT) && \
139+
((WOLFBOOT_PARTITION_UPDATE_ADDRESS + 0) != 0) && \
140+
((WOLFBOOT_PARTITION_SWAP_ADDRESS + 0) != 0) && \
141+
((WOLFBOOT_PARTITION_UPDATE_ADDRESS + 0 + WOLFBOOT_PARTITION_SIZE + 0) > \
142+
(WOLFBOOT_PARTITION_SWAP_ADDRESS + 0)) && \
143+
((WOLFBOOT_PARTITION_UPDATE_ADDRESS + 0) < \
144+
(WOLFBOOT_PARTITION_SWAP_ADDRESS + 0 + WOLFBOOT_SECTOR_SIZE))
145+
#error "Update and swap partitions overlap"
146+
#endif
147+
#endif
148+
113149
#endif /* WOLFBOOT_FIXED_PARTITIONS */
114150

115151
#if !defined(WOLFBOOT_NO_LOAD_ADDRESS)

src/delta.c

Lines changed: 4 additions & 4 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;
@@ -233,9 +232,10 @@ int wb_diff_get_sector_size(void)
233232
fprintf(stderr, "WOLFBOOT_SECTOR_SIZE cannot be 0\n");
234233
exit(6);
235234
}
236-
if (sec_sz > (uint32_t)INT_MAX) {
237-
fprintf(stderr, "WOLFBOOT_SECTOR_SIZE (%" PRIu32 ") exceeds INT_MAX (%d)\n",
238-
sec_sz, INT_MAX);
235+
if (sec_sz > 0xFFFFU) {
236+
fprintf(stderr,
237+
"WOLFBOOT_SECTOR_SIZE (%" PRIu32 ") exceeds delta encoding limit (65535)\n",
238+
sec_sz);
239239
exit(6);
240240
}
241241
return (int)sec_sz;

src/dice/dice.c

Lines changed: 58 additions & 29 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

@@ -660,24 +667,32 @@ static int wolfboot_attest_get_private_key(ecc_key *key,
660667
{
661668
uint8_t priv[WOLFBOOT_DICE_KEY_LEN];
662669
size_t priv_len = sizeof(priv);
670+
int ret = -1;
663671

664672
if (hal_attestation_get_iak_private_key(priv, &priv_len) != 0) {
665-
return -1;
673+
goto cleanup;
666674
}
667675
if (priv_len != WOLFBOOT_DICE_KEY_LEN) {
668-
return -1;
676+
goto cleanup;
669677
}
670678
if (wc_ecc_import_private_key_ex(priv, (word32)priv_len, NULL, 0,
671679
key, ECC_SECP256R1) != 0) {
672-
return -1;
680+
goto cleanup;
673681
}
674-
return 0;
682+
ret = 0;
683+
684+
cleanup:
685+
wolfboot_dice_zeroize(priv, sizeof(priv));
686+
return ret;
675687
}
676688
#else
677-
if (hal_uds_derive_key(uds, uds_len) != 0) {
678-
return -1;
689+
int ret = -1;
690+
691+
if (hal_uds_derive_key(uds, uds_len) == 0) {
692+
ret = wolfboot_dice_derive_attestation_key(key, uds, uds_len, claims);
679693
}
680-
return wolfboot_dice_derive_attestation_key(key, uds, uds_len, claims);
694+
wolfboot_dice_zeroize(uds, sizeof(uds));
695+
return ret;
681696
#endif
682697
}
683698

@@ -801,7 +816,10 @@ static int wolfboot_dice_sign_tbs(const uint8_t *tbs,
801816
{
802817
ecc_key key;
803818
WC_RNG rng;
804-
int ret;
819+
int ret = WOLFBOOT_DICE_ERR_CRYPTO;
820+
int wc_ret;
821+
int key_inited = 0;
822+
int rng_inited = 0;
805823
uint8_t hash[SHA256_DIGEST_SIZE];
806824
uint8_t der_sig[128];
807825
word32 der_sig_len = sizeof(der_sig);
@@ -815,16 +833,18 @@ static int wolfboot_dice_sign_tbs(const uint8_t *tbs,
815833
}
816834

817835
wc_ecc_init(&key);
836+
key_inited = 1;
818837
if (wolfboot_attest_get_private_key(&key, claims) != 0) {
819-
wc_ecc_free(&key);
820-
return WOLFBOOT_DICE_ERR_HW;
838+
ret = WOLFBOOT_DICE_ERR_HW;
839+
goto cleanup;
821840
}
822841

823842
(void)wc_ecc_set_deterministic(&key, 1);
824843
if (wc_InitRng(&rng) != 0) {
825-
wc_ecc_free(&key);
826-
return WOLFBOOT_DICE_ERR_HW;
844+
ret = WOLFBOOT_DICE_ERR_HW;
845+
goto cleanup;
827846
}
847+
rng_inited = 1;
828848

829849
{
830850
wc_Sha256 sha;
@@ -833,26 +853,35 @@ static int wolfboot_dice_sign_tbs(const uint8_t *tbs,
833853
wc_Sha256Final(&sha, hash);
834854
}
835855

836-
ret = wc_ecc_sign_hash(hash, sizeof(hash), der_sig, &der_sig_len, &rng, &key);
837-
wc_FreeRng(&rng);
838-
if (ret != 0) {
839-
wc_ecc_free(&key);
840-
return WOLFBOOT_DICE_ERR_CRYPTO;
856+
wc_ret = wc_ecc_sign_hash(hash, sizeof(hash), der_sig, &der_sig_len, &rng, &key);
857+
if (wc_ret != 0) {
858+
ret = WOLFBOOT_DICE_ERR_CRYPTO;
859+
goto cleanup;
841860
}
842861

843-
ret = wc_ecc_sig_to_rs(der_sig, der_sig_len, r, &r_len, s, &s_len);
844-
if (ret != 0 || r_len > sizeof(r) || s_len > sizeof(s)) {
845-
wc_ecc_free(&key);
846-
return WOLFBOOT_DICE_ERR_CRYPTO;
862+
wc_ret = wc_ecc_sig_to_rs(der_sig, der_sig_len, r, &r_len, s, &s_len);
863+
if (wc_ret != 0 || r_len > sizeof(r) || s_len > sizeof(s)) {
864+
ret = WOLFBOOT_DICE_ERR_CRYPTO;
865+
goto cleanup;
847866
}
848867

849868
XMEMSET(sig, 0, WOLFBOOT_DICE_SIG_LEN);
850869
XMEMCPY(sig + (sizeof(r) - r_len), r, r_len);
851870
XMEMCPY(sig + sizeof(r) + (sizeof(s) - s_len), s, s_len);
852871
*sig_len = WOLFBOOT_DICE_SIG_LEN;
872+
ret = WOLFBOOT_DICE_SUCCESS;
853873

854-
wc_ecc_free(&key);
855-
return WOLFBOOT_DICE_SUCCESS;
874+
cleanup:
875+
if (rng_inited) {
876+
wc_FreeRng(&rng);
877+
}
878+
if (key_inited) {
879+
wc_ecc_free(&key);
880+
wolfboot_dice_zeroize(&key, sizeof(key));
881+
}
882+
wolfboot_dice_zeroize(hash, sizeof(hash));
883+
wolfboot_dice_zeroize(der_sig, sizeof(der_sig));
884+
return ret;
856885
}
857886

858887
static int wolfboot_dice_build_token(uint8_t *token_buf,

0 commit comments

Comments
 (0)