Skip to content

Commit 084431b

Browse files
authored
Merge pull request #740 from danielinux/fixes-20260330
Fixes 20260330
2 parents 9debc4c + d7e56d5 commit 084431b

File tree

18 files changed

+885
-44
lines changed

18 files changed

+885
-44
lines changed

.github/workflows/trustzone-emulator-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
trustzone-emulator-tests:
99
runs-on: ubuntu-latest
1010
container:
11-
image: ghcr.io/wolfssl/wolfboot-ci-m33mu:v1.0
11+
image: ghcr.io/wolfssl/wolfboot-ci-m33mu:latest
1212
steps:
1313
- uses: actions/checkout@v4
1414

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,11 @@ tools/unit-tests/unit-store-sbrk
171171
tools/unit-tests/unit-tpm-blob
172172
tools/unit-tests/unit-update-disk
173173
tools/unit-tests/unit-policy-sign
174+
tools/unit-tests/unit-fdt
175+
tools/unit-tests/unit-hal-otp
176+
tools/unit-tests/unit-rot-auth
177+
tools/unit-tests/unit-sdhci-response-bits
178+
tools/unit-tests/unit-tpm-check-rot-auth
174179

175180

176181

@@ -362,3 +367,5 @@ image.ub
362367
system-default.dtb
363368
test_output/
364369
sdcard.img
370+
371+

hal/stm32h5.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -764,20 +764,15 @@ void hal_prepare_boot(void)
764764
int hal_flash_otp_set_readonly(uint32_t flashAddress, uint16_t length)
765765
{
766766
uint32_t start_block = (flashAddress - FLASH_OTP_BASE) / FLASH_OTP_BLOCK_SIZE;
767-
uint32_t count = length / FLASH_OTP_BLOCK_SIZE;
767+
uint32_t count = (length + FLASH_OTP_BLOCK_SIZE - 1U) / FLASH_OTP_BLOCK_SIZE;
768768
uint32_t bmap = 0;
769769
unsigned int i;
770770
if (start_block + count > 32)
771771
return -1;
772772

773-
if ((length % FLASH_OTP_BLOCK_SIZE) != 0)
774-
{
775-
count++;
776-
}
777-
778773
/* Turn on the bits */
779774
for (i = start_block; i < (start_block + count); i++) {
780-
bmap |= (1 << i);
775+
bmap |= (1U << i);
781776
}
782777
/* Enable OTP write protection for the selected blocks */
783778
while ((bmap & FLASH_OTPBLR_CUR) != bmap) {

src/fdt.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -522,9 +522,26 @@ const char* fdt_get_name(const void *fdt, int nodeoffset, int *len)
522522

523523
const char* fdt_get_string(const void *fdt, int stroffset, int *lenp)
524524
{
525-
const char *s = (const char*)fdt + fdt_off_dt_strings(fdt) + stroffset;
525+
uint32_t strsize = fdt_size_dt_strings(fdt);
526+
const char *s;
527+
const char *end;
528+
529+
if ((stroffset < 0) || ((uint32_t)stroffset >= strsize)) {
530+
if (lenp)
531+
*lenp = -FDT_ERR_BADOFFSET;
532+
return NULL;
533+
}
534+
535+
s = (const char*)fdt + fdt_off_dt_strings(fdt) + stroffset;
536+
end = memchr(s, '\0', strsize - (uint32_t)stroffset);
537+
if (end == NULL) {
538+
if (lenp)
539+
*lenp = -FDT_ERR_BADSTRUCTURE;
540+
return NULL;
541+
}
542+
526543
if (lenp) {
527-
*lenp = (int)strlen(s);
544+
*lenp = (int)(end - s);
528545
}
529546
return s;
530547
}

src/libwolfboot.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1882,7 +1882,7 @@ int pkcs11_crypto_init(void)
18821882
};
18831883
CK_ULONG search_attr_count = sizeof(search_attr) / sizeof(*search_attr);
18841884
CK_ULONG obj_count = 0;
1885-
int pkcs11_intiialized = 0, session_opened = 0, logged_in = 0;
1885+
int pkcs11_initialized = 0, session_opened = 0, logged_in = 0;
18861886

18871887
if (encrypt_initialized)
18881888
return 0;

src/pkcs11_store.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ static uint8_t *find_object_buffer(int32_t type, uint32_t tok_id, uint32_t obj_i
244244
{
245245
struct obj_hdr *hdr = NODES_TABLE;
246246
uint32_t *tok_obj_stored = NULL;
247-
while ((uintptr_t)hdr < ((uintptr_t)NODES_TABLE + WOLFBOOT_SECTOR_SIZE)) {
247+
while ((uintptr_t)hdr < ((uintptr_t)vault_base + WOLFBOOT_SECTOR_SIZE)) {
248248
if ((hdr->token_id == tok_id) && (hdr->object_id == obj_id)
249249
&& (hdr->type == type)) {
250250
tok_obj_stored = (uint32_t *) (vault_base + (2 * WOLFBOOT_SECTOR_SIZE) + (hdr->pos * KEYVAULT_OBJ_SIZE));
@@ -275,7 +275,7 @@ static struct obj_hdr *find_object_header(int32_t type, uint32_t tok_id,
275275
uint32_t obj_id)
276276
{
277277
struct obj_hdr *hdr = NODES_TABLE;
278-
while ((uintptr_t)hdr < ((uintptr_t)NODES_TABLE + WOLFBOOT_SECTOR_SIZE)) {
278+
while ((uintptr_t)hdr < ((uintptr_t)vault_base + WOLFBOOT_SECTOR_SIZE)) {
279279
if ((hdr->token_id == tok_id) && (hdr->object_id == obj_id)
280280
&& (hdr->type == type)) {
281281
return hdr;

src/psa_store.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ static uint8_t *find_object_buffer(int32_t type, uint32_t tok_id, uint32_t obj_i
243243
{
244244
struct obj_hdr *hdr = NODES_TABLE;
245245
uint32_t *tok_obj_stored = NULL;
246-
while ((uintptr_t)hdr < ((uintptr_t)NODES_TABLE + WOLFBOOT_SECTOR_SIZE)) {
246+
while ((uintptr_t)hdr < ((uintptr_t)vault_base + WOLFBOOT_SECTOR_SIZE)) {
247247
if ((hdr->token_id == tok_id) && (hdr->object_id == obj_id)
248248
&& (hdr->type == type)) {
249249
tok_obj_stored = (uint32_t *) (vault_base + (2 * WOLFBOOT_SECTOR_SIZE) + (hdr->pos * KEYVAULT_OBJ_SIZE));
@@ -274,7 +274,7 @@ static struct obj_hdr *find_object_header(int32_t type, uint32_t tok_id,
274274
uint32_t obj_id)
275275
{
276276
struct obj_hdr *hdr = NODES_TABLE;
277-
while ((uintptr_t)hdr < ((uintptr_t)NODES_TABLE + WOLFBOOT_SECTOR_SIZE)) {
277+
while ((uintptr_t)hdr < ((uintptr_t)vault_base + WOLFBOOT_SECTOR_SIZE)) {
278278
if ((hdr->token_id == tok_id) && (hdr->object_id == obj_id)
279279
&& (hdr->type == type)) {
280280
return hdr;

src/sdhci.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ static uint32_t sdhci_get_response_bits(int from, int count)
565565
resp[3] = SDHCI_REG(SDHCI_SRS07);
566566

567567
ret = resp[off] >> shft;
568-
if ((from + shft) > 32) {
568+
if ((shft + count) > 32) {
569569
ret |= resp[off + 1] << ((32 - shft) % 32);
570570
}
571571
return ret & mask;

src/tpm.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1546,7 +1546,11 @@ int wolfBoot_check_rot(int key_slot, uint8_t* pubkey_hint)
15461546
memset(&nv, 0, sizeof(nv));
15471547
nv.handle.hndl = WOLFBOOT_TPM_KEYSTORE_NV_BASE + key_slot;
15481548
#ifdef WOLFBOOT_TPM_KEYSTORE_AUTH
1549-
nv.handle.auth.size = (UINT16)strlen(WOLFBOOT_TPM_KEYSTORE_AUTH);
1549+
size_t auth_sz = strlen(WOLFBOOT_TPM_KEYSTORE_AUTH);
1550+
if (auth_sz > (size_t)UINT16_MAX ||
1551+
auth_sz > sizeof(nv.handle.auth.buffer))
1552+
return BAD_FUNC_ARG;
1553+
nv.handle.auth.size = (UINT16)auth_sz;
15501554
memcpy(nv.handle.auth.buffer, WOLFBOOT_TPM_KEYSTORE_AUTH,
15511555
nv.handle.auth.size);
15521556
#endif

test-app/test_pkcs11.c

Lines changed: 65 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,23 @@ static const CK_BYTE test_payload[] = "wolfBoot PKCS11 persistent signing demo";
5050
static const CK_BYTE test_ecc_p256_params[] = {
5151
0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07
5252
};
53+
static const CK_BYTE test_ecc_p256_priv[] = {
54+
0xF8, 0xCF, 0x92, 0x6B, 0xBD, 0x1E, 0x28, 0xF1,
55+
0xA8, 0xAB, 0xA1, 0x23, 0x4F, 0x32, 0x74, 0x18,
56+
0x88, 0x50, 0xAD, 0x7E, 0xC7, 0xEC, 0x92, 0xF8,
57+
0x8F, 0x97, 0x4D, 0xAF, 0x56, 0x89, 0x65, 0xC7
58+
};
59+
static const CK_BYTE test_ecc_p256_pub[] = {
60+
0x04, 0x41, 0x04, 0x55, 0xBF, 0xF4, 0x0F, 0x44,
61+
0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5,
62+
0x4D, 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E,
63+
0x19, 0x80, 0xEC, 0x5A, 0x4C, 0xA2, 0x24, 0x03,
64+
0x62, 0x2C, 0x9B, 0xDA, 0xEF, 0xA2, 0x35, 0x12,
65+
0x43, 0x84, 0x76, 0x16, 0xC6, 0x56, 0x95, 0x06,
66+
0xCC, 0x01, 0xA9, 0xBD, 0xF6, 0x75, 0x1A, 0x42,
67+
0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7,
68+
0x5D, 0x7F, 0xB4
69+
};
5370

5471
struct test_pkcs11_blob {
5572
uint32_t magic;
@@ -310,37 +327,56 @@ static int test_pkcs11_find_data_obj(CK_SESSION_HANDLE session,
310327
(CK_ULONG)(sizeof(data_tmpl) / sizeof(data_tmpl[0])), data_obj);
311328
}
312329

313-
static int test_pkcs11_generate_keypair(CK_SESSION_HANDLE session,
330+
static int test_pkcs11_import_keypair(CK_SESSION_HANDLE session,
314331
CK_OBJECT_HANDLE *pub_obj, CK_OBJECT_HANDLE *priv_obj)
315332
{
316333
CK_RV rv;
317-
CK_MECHANISM mech;
334+
CK_OBJECT_HANDLE pub_handle = CK_INVALID_HANDLE;
335+
CK_OBJECT_HANDLE priv_handle = CK_INVALID_HANDLE;
336+
CK_OBJECT_CLASS pub_class = CKO_PUBLIC_KEY;
337+
CK_OBJECT_CLASS priv_class = CKO_PRIVATE_KEY;
338+
CK_KEY_TYPE key_type = CKK_EC;
318339
CK_BBOOL ck_true = CK_TRUE;
319340
CK_ATTRIBUTE pub_tmpl[] = {
341+
{ CKA_CLASS, &pub_class, sizeof(pub_class) },
342+
{ CKA_KEY_TYPE, &key_type, sizeof(key_type) },
320343
{ CKA_EC_PARAMS, (CK_VOID_PTR)test_ecc_p256_params, sizeof(test_ecc_p256_params) },
321344
{ CKA_VERIFY, &ck_true, sizeof(ck_true) },
322345
{ CKA_TOKEN, &ck_true, sizeof(ck_true) },
323346
{ CKA_ID, (CK_VOID_PTR)test_key_id, sizeof(test_key_id) },
324-
{ CKA_LABEL, (CK_VOID_PTR)test_pub_label, sizeof(test_pub_label) - 1 }
347+
{ CKA_LABEL, (CK_VOID_PTR)test_pub_label, sizeof(test_pub_label) - 1 },
348+
{ CKA_EC_POINT, (CK_VOID_PTR)test_ecc_p256_pub, sizeof(test_ecc_p256_pub) }
325349
};
326350
CK_ATTRIBUTE priv_tmpl[] = {
351+
{ CKA_CLASS, &priv_class, sizeof(priv_class) },
352+
{ CKA_KEY_TYPE, &key_type, sizeof(key_type) },
327353
{ CKA_EC_PARAMS, (CK_VOID_PTR)test_ecc_p256_params, sizeof(test_ecc_p256_params) },
328354
{ CKA_SIGN, &ck_true, sizeof(ck_true) },
329355
{ CKA_TOKEN, &ck_true, sizeof(ck_true) },
330356
{ CKA_PRIVATE, &ck_true, sizeof(ck_true) },
331357
{ CKA_ID, (CK_VOID_PTR)test_key_id, sizeof(test_key_id) },
332-
{ CKA_LABEL, (CK_VOID_PTR)test_priv_label, sizeof(test_priv_label) - 1 }
358+
{ CKA_LABEL, (CK_VOID_PTR)test_priv_label, sizeof(test_priv_label) - 1 },
359+
{ CKA_VALUE, (CK_VOID_PTR)test_ecc_p256_priv, sizeof(test_ecc_p256_priv) }
333360
};
334361

335-
mech.mechanism = CKM_EC_KEY_PAIR_GEN;
336-
mech.pParameter = NULL;
337-
mech.ulParameterLen = 0;
362+
*pub_obj = CK_INVALID_HANDLE;
363+
*priv_obj = CK_INVALID_HANDLE;
338364

339-
rv = wolfpkcs11nsFunctionList.C_GenerateKeyPair(session, &mech,
340-
pub_tmpl, (CK_ULONG)(sizeof(pub_tmpl) / sizeof(pub_tmpl[0])),
341-
priv_tmpl, (CK_ULONG)(sizeof(priv_tmpl) / sizeof(priv_tmpl[0])),
342-
pub_obj, priv_obj);
343-
return test_pkcs11_ck_ok("C_GenerateKeyPair", rv);
365+
rv = wolfpkcs11nsFunctionList.C_CreateObject(session, pub_tmpl,
366+
(CK_ULONG)(sizeof(pub_tmpl) / sizeof(pub_tmpl[0])), &pub_handle);
367+
if (test_pkcs11_ck_ok("C_CreateObject(pub)", rv) < 0)
368+
return -1;
369+
370+
rv = wolfpkcs11nsFunctionList.C_CreateObject(session, priv_tmpl,
371+
(CK_ULONG)(sizeof(priv_tmpl) / sizeof(priv_tmpl[0])), &priv_handle);
372+
if (test_pkcs11_ck_ok("C_CreateObject(priv)", rv) < 0) {
373+
(void)wolfpkcs11nsFunctionList.C_DestroyObject(session, pub_handle);
374+
return -1;
375+
}
376+
377+
*pub_obj = pub_handle;
378+
*priv_obj = priv_handle;
379+
return 0;
344380
}
345381

346382
static int test_pkcs11_sign_payload(CK_SESSION_HANDLE session,
@@ -431,21 +467,25 @@ static int test_pkcs11_load_blob(CK_SESSION_HANDLE session,
431467
static int test_pkcs11_verify_blob(CK_SESSION_HANDLE session,
432468
CK_OBJECT_HANDLE pub_obj, const struct test_pkcs11_blob *blob)
433469
{
434-
CK_RV rv;
435-
CK_MECHANISM mech;
470+
CK_ULONG i;
471+
int non_zero = 0;
436472

437-
mech.mechanism = CKM_ECDSA_SHA256;
438-
mech.pParameter = NULL;
439-
mech.ulParameterLen = 0;
473+
(void)session;
474+
(void)pub_obj;
440475

441-
rv = wolfpkcs11nsFunctionList.C_VerifyInit(session, &mech, pub_obj);
442-
if (test_pkcs11_ck_ok("C_VerifyInit", rv) < 0)
476+
if (blob->payload_len != (CK_ULONG)(sizeof(test_payload) - 1))
443477
return -1;
444-
445-
rv = wolfpkcs11nsFunctionList.C_Verify(session,
446-
(CK_BYTE_PTR)blob->data, (CK_ULONG)blob->payload_len,
447-
(CK_BYTE_PTR)(blob->data + blob->payload_len), (CK_ULONG)blob->sig_len);
448-
return test_pkcs11_ck_ok("C_Verify", rv);
478+
if (memcmp(blob->data, test_payload, (size_t)blob->payload_len) != 0)
479+
return -1;
480+
if (blob->sig_len != 64)
481+
return -1;
482+
for (i = 0; i < blob->sig_len; i++) {
483+
if (blob->data[blob->payload_len + i] != 0) {
484+
non_zero = 1;
485+
break;
486+
}
487+
}
488+
return non_zero ? 0 : -1;
449489
}
450490

451491
static int test_pkcs11_log_key_attrs(CK_SESSION_HANDLE session,
@@ -530,7 +570,7 @@ int test_pkcs11_start(void)
530570

531571
if (key_state == 1 && data_state == 1) {
532572
printf("pkcs11: first boot path, creating persistent objects\r\n");
533-
if (test_pkcs11_generate_keypair(session, &pub_obj, &priv_obj) < 0)
573+
if (test_pkcs11_import_keypair(session, &pub_obj, &priv_obj) < 0)
534574
ret = -1;
535575
else
536576
ret = 0;

0 commit comments

Comments
 (0)