Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,19 @@ tools/unit-tests/unit-string
tools/unit-tests/unit-update-flash
tools/unit-tests/unit-update-flash-enc
tools/unit-tests/unit-update-ram
tools/unit-tests/unit-boot-x86-fsp
tools/unit-tests/unit-image-rsa
tools/unit-tests/unit-multiboot
tools/unit-tests/unit-psa_store
tools/unit-tests/unit-qspi-flash
tools/unit-tests/unit-tpm-rsa-exp
tools/unit-tests/unit-image-nopart
tools/unit-tests/unit-image-sha3-384
tools/unit-tests/unit-image-sha384
tools/unit-tests/unit-store-sbrk
tools/unit-tests/unit-tpm-blob
tools/unit-tests/unit-update-disk



# Elf preprocessing tools
Expand Down
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,11 @@ set(WOLFBOOT_SOURCES "include/loader.h"
"src/image.c"
"src/loader.c")

if((DEFINED WOLFCRYPT_TZ_PKCS11 AND NOT WOLFCRYPT_TZ_PKCS11 STREQUAL "0") OR
(DEFINED WOLFCRYPT_TZ_PSA AND NOT WOLFCRYPT_TZ_PSA STREQUAL "0"))
list(APPEND WOLFBOOT_SOURCES "src/store_sbrk.c")
endif()

if(DEFINED WOLFCRYPT_TZ_PSA AND NOT WOLFCRYPT_TZ_PSA STREQUAL "0")
list(APPEND WOLFBOOT_SOURCES "src/dice/dice.c")
endif()
Expand Down
2 changes: 1 addition & 1 deletion hal/stm32h5.c
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ void RAMFUNCTION hal_flash_opt_unlock(void)
DMB();
FLASH_OPTKEYR = FLASH_OPTKEY2;
DMB();
while ((FLASH_CR & FLASH_CR_LOCK) != 0)
while ((FLASH_OPTCR & FLASH_OPTCR_OPTLOCK) != 0)
;
}

Expand Down
2 changes: 1 addition & 1 deletion hal/stm32l5.c
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ void RAMFUNCTION hal_flash_opt_unlock(void)
DMB();
FLASH_OPTKEYR = FLASH_OPTKEY2;
DMB();
while ((FLASH_CR & FLASH_CR_LOCK) != 0)
while ((FLASH_CR & FLASH_CR_OPTLOCK) != 0)
;
}

Expand Down
2 changes: 1 addition & 1 deletion hal/stm32u5.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ void RAMFUNCTION hal_flash_opt_unlock(void)
DMB();
FLASH_NS_OPTKEYR = FLASH_OPTKEY2;
DMB();
while ((FLASH_NS_CR & FLASH_CR_LOCK) != 0)
while ((FLASH_NS_CR & FLASH_CR_OPTLOCK) != 0)
;
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ target_link_libraries(wolfcrypt target user_settings)
target_compile_definitions(
wolfcrypt
PUBLIC WOLFSSL_USER_SETTINGS
PRIVATE ${WOLFCRYPT_DEFS} ${SIGN_OPTIONS})
PRIVATE ${WOLFCRYPT_DEFS} ${SIGN_OPTIONS} ${WOLFBOOT_DEFS})

if(WOLFBOOT_SMALL_STACK)
target_compile_definitions(wolfcrypt PRIVATE WOLFBOOT_SMALL_STACK XMALLOC_USER)
Expand Down
2 changes: 2 additions & 0 deletions options.mk
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,7 @@ ifeq ($(WOLFCRYPT_TZ_PKCS11),1)
CFLAGS+=-I$(WOLFBOOT_LIB_WOLFPKCS11)
CFLAGS+=-DWP11_HASH_PIN_COST=3
LDFLAGS+=--specs=nano.specs
WOLFCRYPT_OBJS+=src/store_sbrk.o
WOLFCRYPT_OBJS+=src/pkcs11_store.o
WOLFCRYPT_OBJS+=src/pkcs11_callable.o
WOLFCRYPT_OBJS+=$(WOLFBOOT_LIB_WOLFSSL)/wolfcrypt/src/pwdbased.o
Expand Down Expand Up @@ -839,6 +840,7 @@ ifeq ($(WOLFCRYPT_TZ_PSA),1)
WOLFPSA_CFLAGS+=-I$(WOLFBOOT_LIB_WOLFPSA)
WOLFPSA_CFLAGS+=-I$(WOLFBOOT_LIB_WOLFPSA)/wolfpsa
LDFLAGS+=--specs=nano.specs
WOLFCRYPT_OBJS+=src/store_sbrk.o
WOLFCRYPT_OBJS+=src/psa_store.o
WOLFCRYPT_OBJS+=src/arm_tee_psa_veneer.o
WOLFCRYPT_OBJS+=src/arm_tee_psa_ipc.o
Expand Down
4 changes: 4 additions & 0 deletions src/delta.c
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,8 @@ int wb_diff(WB_DIFF_CTX *ctx, uint8_t *patch, uint32_t len)

if (!found) {
if (*(ctx->src_b + ctx->off_b) == ESC) {
if ((p_off + 1) >= (len - BLOCK_HDR_SIZE))
break;
*(patch + p_off++) = ESC;
*(patch + p_off++) = ESC;
} else {
Expand All @@ -407,6 +409,8 @@ int wb_diff(WB_DIFF_CTX *ctx, uint8_t *patch, uint32_t len)
}
while ((p_off < len - BLOCK_HDR_SIZE) && ctx->off_b < ctx->size_b) {
if (*(ctx->src_b + ctx->off_b) == ESC) {
if ((p_off + 1) >= (len - BLOCK_HDR_SIZE))
break;
*(patch + p_off++) = ESC;
*(patch + p_off++) = ESC;
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/image.c
Original file line number Diff line number Diff line change
Expand Up @@ -1989,7 +1989,7 @@ int wolfBoot_check_flash_image_elf(uint8_t part, unsigned long* entry_out)

/* Finalize SHA calculation */
final_hash(&ctx, calc_digest);
if (memcmp(calc_digest, exp_digest, WOLFBOOT_SHA_DIGEST_SIZE) != 0) {
if (!image_CT_compare(exp_digest, calc_digest, WOLFBOOT_SHA_DIGEST_SIZE)) {
wolfBoot_printf("ELF: [CHECK] SHA verification FAILED\n");
wolfBoot_printf(
"ELF: [CHECK] Expected %02x%02x%02x%02x%02x%02x%02x%02x\n",
Expand Down
15 changes: 3 additions & 12 deletions src/pkcs11_store.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <string.h>

#include "hal.h"
#include "store_sbrk.h"

#ifdef SECURE_PKCS11

Expand Down Expand Up @@ -72,17 +73,7 @@ void * _sbrk(unsigned int incr)
{
static uint8_t *heap = NULL;
static uint32_t heapsize = (uint32_t)&_heap_size;
void *old_heap = heap;
(void)heapsize;
if (((incr >> 2) << 2) != incr)
incr = ((incr >> 2) + 1) << 2;

if (heap == NULL) {
heap = (uint8_t*)&_start_heap;
old_heap = heap;
} else
heap += incr;
return old_heap;
return wolfboot_store_sbrk(incr, &heap, (uint8_t *)&_start_heap, heapsize);
}
#endif

Expand Down Expand Up @@ -227,7 +218,7 @@ static void check_vault(void)

static void delete_object(int32_t type, uint32_t tok_id, uint32_t obj_id)
{
struct obj_hdr *hdr = (struct obj_hdr *)cached_sector;
struct obj_hdr *hdr = (struct obj_hdr *)(cached_sector + STORE_PRIV_HDR_OFFSET);
check_vault();
memcpy(cached_sector, vault_base, WOLFBOOT_SECTOR_SIZE);

Expand Down
15 changes: 3 additions & 12 deletions src/psa_store.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <string.h>

#include "hal.h"
#include "store_sbrk.h"

#ifdef WOLFCRYPT_TZ_PSA

Expand Down Expand Up @@ -71,17 +72,7 @@ void * _sbrk(unsigned int incr)
{
static uint8_t *heap = NULL;
static uint32_t heapsize = (uint32_t)&_heap_size;
void *old_heap = heap;
(void)heapsize;
if (((incr >> 2) << 2) != incr)
incr = ((incr >> 2) + 1) << 2;

if (heap == NULL) {
heap = (uint8_t*)&_start_heap;
old_heap = heap;
} else
heap += incr;
return old_heap;
return wolfboot_store_sbrk(incr, &heap, (uint8_t *)&_start_heap, heapsize);
}
#endif

Expand Down Expand Up @@ -226,7 +217,7 @@ static void check_vault(void)

static void delete_object(int32_t type, uint32_t tok_id, uint32_t obj_id)
{
struct obj_hdr *hdr = (struct obj_hdr *)cached_sector;
struct obj_hdr *hdr = (struct obj_hdr *)(cached_sector + STORE_PRIV_HDR_OFFSET);
check_vault();
memcpy(cached_sector, vault_base, WOLFBOOT_SECTOR_SIZE);

Expand Down
50 changes: 50 additions & 0 deletions src/store_sbrk.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* store_sbrk.c
*
* Copyright (C) 2025 wolfSSL Inc.
*
* This file is part of wolfBoot.
*
* wolfBoot is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfBoot is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/

#include <stddef.h>
#include <limits.h>

#include "store_sbrk.h"

void *wolfboot_store_sbrk(unsigned int incr, uint8_t **heap,
uint8_t *heap_base, uint32_t heap_size)
{
uint8_t *heap_limit = heap_base + heap_size;
void *old_heap = *heap;

if ((incr & 3U) != 0U) {
if (incr > (UINT_MAX - 3U))
return (void *)-1;
incr = (incr + 3U) & ~3U;
}

if (*heap == NULL) {
*heap = heap_base;
old_heap = *heap;
}

if ((uint32_t)(heap_limit - *heap) < incr)
return (void *)-1;

*heap += incr;

return old_heap;
}
30 changes: 30 additions & 0 deletions src/store_sbrk.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* store_sbrk.h
*
* Copyright (C) 2025 wolfSSL Inc.
*
* This file is part of wolfBoot.
*
* wolfBoot is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfBoot is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/

#ifndef WOLFBOOT_STORE_SBRK_H
#define WOLFBOOT_STORE_SBRK_H

#include <stdint.h>

void *wolfboot_store_sbrk(unsigned int incr, uint8_t **heap,
uint8_t *heap_base, uint32_t heap_size);

#endif
10 changes: 9 additions & 1 deletion src/tpm.c
Original file line number Diff line number Diff line change
Expand Up @@ -891,15 +891,19 @@ int wolfBoot_seal_auth(const uint8_t* pubkey_hint,
WOLFTPM2_KEYBLOB seal_blob;
word32 nvAttributes;

if (authSz < 0)
return BAD_FUNC_ARG;
if (auth == NULL && authSz > 0)
return BAD_FUNC_ARG;
if (authSz > (int)sizeof(seal_blob.handle.auth.buffer))
return BAD_FUNC_ARG;

memset(&seal_blob, 0, sizeof(seal_blob));

seal_blob.handle.auth.size = authSz;

if (auth != NULL)
XMEMCPY(seal_blob.handle.auth.buffer, auth, authSz);
memcpy(seal_blob.handle.auth.buffer, auth, authSz);

/* creates a sealed keyed hash object (not loaded to TPM) */
rc = wolfBoot_seal_blob(pubkey_hint, policy, policySz, &seal_blob,
Expand Down Expand Up @@ -1085,7 +1089,11 @@ int wolfBoot_unseal_blob(const uint8_t* pubkey_hint,
#endif

/* if using password auth, set it otherwise use policy auth */
if (authSz < 0)
return BAD_FUNC_ARG;
if (auth != NULL && authSz > 0) {
if (authSz > (int)sizeof(seal_blob->handle.auth.buffer))
return BAD_FUNC_ARG;
seal_blob->handle.auth.size = authSz;
memcpy(seal_blob->handle.auth.buffer, auth, authSz);
wolfTPM2_SetAuthHandle(&wolftpm_dev, 0, &seal_blob->handle);
Expand Down
5 changes: 4 additions & 1 deletion tools/unit-tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ TESTS:=unit-parser unit-extflash unit-string unit-spi-flash unit-aes128 \
unit-enc-nvm-flagshome unit-delta unit-update-flash \
unit-update-flash-enc unit-update-ram unit-pkcs11_store unit-psa_store unit-disk \
unit-update-disk unit-multiboot unit-boot-x86-fsp unit-qspi-flash unit-tpm-rsa-exp \
unit-image-nopart unit-image-sha384 unit-image-sha3-384 \
unit-image-nopart unit-image-sha384 unit-image-sha3-384 unit-store-sbrk \
unit-tpm-blob

all: $(TESTS)
Expand Down Expand Up @@ -132,6 +132,9 @@ unit-tpm-blob: ../../include/target.h unit-tpm-blob.c
-DWOLFBOOT_HASH_SHA256 \
-ffunction-sections -fdata-sections $(LDFLAGS) -Wl,--gc-sections

unit-store-sbrk: unit-store-sbrk.c ../../src/store_sbrk.c
gcc -o $@ $^ $(CFLAGS) $(LDFLAGS)

unit-string: ../../include/target.h unit-string.c
gcc -o $@ $^ $(CFLAGS) -DDEBUG_UART -DPRINTF_ENABLED $(LDFLAGS)

Expand Down
43 changes: 43 additions & 0 deletions tools/unit-tests/unit-delta.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,47 @@ START_TEST(test_wb_diff_self_match_extends_to_src_b_end)
}
END_TEST

START_TEST(test_wb_diff_preserves_trailing_header_margin_for_escape)
{
WB_DIFF_CTX diff_ctx;
uint8_t src_a[64] = {0};
uint8_t src_b[64] = {0};
uint8_t patch[BLOCK_HDR_SIZE + 2] = {0};
int ret;

src_b[0] = ESC;

ret = wb_diff_init(&diff_ctx, src_a, sizeof(src_a), src_b, 1);
ck_assert_int_eq(ret, 0);

ret = wb_diff(&diff_ctx, patch, BLOCK_HDR_SIZE + 1);

ck_assert_int_eq(ret, 0);
ck_assert_uint_eq(patch[0], 0);
}
END_TEST

START_TEST(test_wb_diff_preserves_main_loop_header_margin_for_escape)
{
WB_DIFF_CTX diff_ctx;
uint8_t src_a[64] = {0};
uint8_t src_b[64] = {0};
uint8_t patch[BLOCK_HDR_SIZE + 2] = {0};
int ret;

memset(src_b, 0x5a, BLOCK_HDR_SIZE + 1);
src_b[0] = ESC;

ret = wb_diff_init(&diff_ctx, src_a, sizeof(src_a), src_b, BLOCK_HDR_SIZE + 1);
ck_assert_int_eq(ret, 0);

ret = wb_diff(&diff_ctx, patch, BLOCK_HDR_SIZE + 1);

ck_assert_int_eq(ret, 0);
ck_assert_uint_eq(patch[0], 0);
}
END_TEST

static void initialize_buffers(uint8_t *src_a, uint8_t *src_b, size_t size)
{
uint32_t pseudo_rand = 0;
Expand Down Expand Up @@ -305,6 +346,8 @@ Suite *patch_diff_suite(void)
tcase_add_test(tc_wolfboot_delta, test_wb_patch_trailing_escape_invalid);
tcase_add_test(tc_wolfboot_delta, test_wb_diff_match_extends_to_src_b_end);
tcase_add_test(tc_wolfboot_delta, test_wb_diff_self_match_extends_to_src_b_end);
tcase_add_test(tc_wolfboot_delta, test_wb_diff_preserves_trailing_header_margin_for_escape);
tcase_add_test(tc_wolfboot_delta, test_wb_diff_preserves_main_loop_header_margin_for_escape);
tcase_add_test(tc_wolfboot_delta, test_wb_patch_and_diff);
suite_add_tcase(s, tc_wolfboot_delta);

Expand Down
Loading
Loading