Skip to content
Open
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
24 changes: 24 additions & 0 deletions .github/workflows/test-configs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1317,6 +1317,22 @@ jobs:
arch: arm
config-file: ./config/examples/stm32h5-dk-tz.config

stm32h5_dk_pka_hash_test:
uses: ./.github/workflows/test-build-stm32cube.yml
with:
arch: arm
config-file: ./config/examples/stm32h5-dk.config
cube-repo: STMicroelectronics/STM32CubeH5
make-args: PKA=1 STM32_HASH=1 BOOT_BENCHMARK=1 STM32CUBE_H5=$GITHUB_WORKSPACE/STM32Cube

stm32h5_tz_pka_test:
uses: ./.github/workflows/test-build-stm32cube.yml
with:
arch: arm
config-file: ./config/examples/stm32h5-tz.config
cube-repo: STMicroelectronics/STM32CubeH5
make-args: PKA=1 STM32CUBE_H5=$GITHUB_WORKSPACE/STM32Cube

stm32n6_test:
uses: ./.github/workflows/test-build.yml
with:
Expand Down Expand Up @@ -1548,6 +1564,14 @@ jobs:
arch: arm
config-file: ./config/examples/stm32u5.config

stm32u5_pka_hash_test:
uses: ./.github/workflows/test-build-stm32cube.yml
with:
arch: arm
config-file: ./config/examples/stm32u5.config
cube-repo: STMicroelectronics/STM32CubeU5
make-args: PKA=1 STM32_HASH=1 BOOT_BENCHMARK=1 STM32CUBE_U5=$GITHUB_WORKSPACE/STM32Cube

stm32u5_wolfcrypt_tz:
uses: ./.github/workflows/test-build.yml
with:
Expand Down
42 changes: 42 additions & 0 deletions docs/Targets.md
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,9 @@ mon reset init

The STM32U5 is a Cortex-M33 (ARMv8-M).

For PKA signature verification and HASH image digests see
[STM32H5/U5 hardware crypto](#stm32h5u5-hardware-crypto-pka-hash).

Note: We have seen issues with vector table alignment, so the default image header size (IMAGE_HEADER_SIZE) has been increased to 1024 bytes to avoid potential issues.

### Scenario 1: TrustZone enabled, staging non-secure application
Expand Down Expand Up @@ -1523,6 +1526,45 @@ STM32_Programmer_CLI -c port=swd -d trigger_magic.bin 0x0819FFFB -hardRst
The `success` command of the test application confirms the new version. An
update that is not confirmed is rolled back on the next reset.

### STM32H5/U5 hardware crypto (PKA, HASH)

`PKA=1` verifies the ECDSA signature on the PKA peripheral and `STM32_HASH=1`
computes the SHA-256 image digest on the HASH peripheral, both through
wolfCrypt's register-level STM32 port (`WOLFSSL_STM32_BARE`). The port needs
only the CMSIS device headers of an STM32Cube firmware package: the newest
`STM32Cube_FW_H5_V*` or `STM32Cube_FW_U5_V*` under `~/STM32Cube/Repository`
is used unless `STM32CUBE_H5` or `STM32CUBE_U5` is set. `STM32_DEVICE` selects
the device header: `STM32H563xx` by default, `STM32H573xx` with
`STM32H5_BOARD=h573i_dk`, and `STM32U585xx` on the STM32U5.

```sh
cp config/examples/stm32h5-dk.config .config
make PKA=1 STM32_HASH=1
```

- The STM32H563 PKA can only verify, which is all wolfBoot needs.
- With `PKA=1`, `hal_init()` starts HSI48: PKA initialization needs the RNG
kernel clock.
- With `STM32_HASH=1` the image is hashed in 4 KB blocks
(`STM32_HASH_BLOCK_SIZE`), because the HASH engine saves and restores its
context on every update. `WOLFBOOT_IMG_HASH_ONESHOT=1` hashes the image in
one update.
- `STM32_HASH=1` changes the layout of the wolfCrypt hash context and is not
supported with `WOLFTPM`, SPI/QSPI/OCTOSPI flash, `WOLFCRYPT_TZ_PSA` or
`WOLFCRYPT_TZ_WOLFHSM`. `PKA=1` works with all of them.

Integrity check plus signature verification time, ECC256 with SHA-256,
measured with `BOOT_BENCHMARK=1` (DWT cycle counter) over 10 boots:

| Board (config) | SYSCLK | Image | Software | `PKA=1` | `PKA=1 STM32_HASH=1` |
|---|---|---|---|---|---|
| STM32H573I-DK (`stm32h5-dk`) | 250 MHz | 256 KB | 131 + 20 ms | 131 + 12 ms | 38 + 12 ms |
| NUCLEO-H563ZI (`stm32h5-tz`) | 248 MHz | 263 KB | 159 + 17 ms | 147 + 12 ms | 17 + 12 ms |
| B-U585I-IOT02A (`stm32u5-nonsecure-dualbank`) | 160 MHz | 128 KB | 56 + 19 ms | 56 + 19 ms | 27 + 19 ms |

`PKA=1` also removes the software ECC code: wolfBoot for `stm32h5-dk` is 30 KB
with software crypto, 21 KB with `PKA=1` and 17 KB with both options.

### STM32H5 Debugging


Expand Down
38 changes: 38 additions & 0 deletions hal/stm32_hwcrypto.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* stm32_hwcrypto.h
*
* Copyright (C) 2026 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
*/

/* Force-included (-include) into the HAL objects by options.mk with
* STM32_HASH=1 on STM32H5/U5. wolfCrypt's STM32 port includes the CMSIS
* device header, whose register macros clash with the ones in hal/stm32h5.h
* and hal/stm32u5.h. The HAL sources do not use the port, so hide it from
* them. */

#ifndef STM32_HWCRYPTO_H
#define STM32_HWCRYPTO_H

#ifdef WOLFSSL_STM32_BARE
#undef WOLFSSL_STM32_BARE
#undef WOLFSSL_STM32H5
#undef WOLFSSL_STM32U5
#undef WOLFSSL_STM32_PKA
#endif

#endif /* STM32_HWCRYPTO_H */
52 changes: 52 additions & 0 deletions hal/stm32h5.c
Original file line number Diff line number Diff line change
Expand Up @@ -451,33 +451,76 @@ static void clock_pll_off(void)

}

#if defined(BOOT_BENCHMARK) && defined(__WOLFBOOT)
#define DEMCR (*(volatile uint32_t *)(0xE000EDFCUL))
#define DEMCR_TRCENA (1UL << 24)
#define DWT_CTRL (*(volatile uint32_t *)(0xE0001000UL))
#define DWT_CYCCNTENA (1UL << 0)
#define DWT_CYCCNT (*(volatile uint32_t *)(0xE0001004UL))

static uint32_t sysclk_mhz;

/* DWT cycle counter scaled by SYSCLK. CYCCNT wraps every ~17 s at 250 MHz;
* the elapsed cycles are accumulated here, so callers must read more often
* than that. */
uint64_t hal_get_timer_us(void)
{
static uint32_t last;
static uint64_t cycles;
static int started;
uint32_t now;

/* DWT_CTRL survives a system reset but DEMCR.TRCENA does not, so
* CYCCNTENA alone does not mean the counter runs */
if (!started) {
DEMCR |= DEMCR_TRCENA;
DWT_CTRL |= DWT_CYCCNTENA;
last = DWT_CYCCNT;
started = 1;
}
now = DWT_CYCCNT;
cycles += (uint32_t)(now - last);
last = now;
return cycles / sysclk_mhz;
}
#endif

/* If PLL_SRC_HSE is set then HSE (8MHz) is used otherwise HSI 64 MHz is used
* and system clock is 250MHz. The STM32H573I-DK HSE is 25MHz. */

static void clock_pll_on(void)
{
uint32_t reg32;
uint32_t plln, pllm, pllq, pllp, pllr, hpre, apb1pre, apb2pre, apb3pre, flash_waitstates;
uint32_t src_mhz;

#if PLL_SRC_HSE
#ifdef STM32H573I_DK
src_mhz = 25;
pllm = 5;
plln = 100;
#else
src_mhz = 8;
pllm = 1;
plln = 62;
#endif
pllp = 2; /* 250Mhz */
pllq = 5; /* 100Mhz */
pllr = 2;
#else
src_mhz = 4;
pllm = 4;
plln = 31;
pllp = 2;
pllq = 5;
pllr = 2;
#endif
flash_waitstates = 5;
#if defined(BOOT_BENCHMARK) && defined(__WOLFBOOT)
sysclk_mhz = (src_mhz * plln) / (pllm * pllp);
#else
(void)src_mhz;
#endif

/* Set voltage scaler */
reg32 = PWR_VOSCR & (~PWR_VOS_MASK);
Expand Down Expand Up @@ -809,6 +852,15 @@ static void fork_bootloader(void)
void hal_init(void)
{
clock_pll_on();
#ifdef STM32_PKA_HSI48
{
/* bounded: without HSI48 the PKA fails to init and verify fails */
uint32_t timeout = 1000000;
RCC_CR |= RCC_CR_HSI48ON;
while (((RCC_CR & RCC_CR_HSI48RDY) == 0) && (--timeout != 0))
;
}
#endif

#ifdef DEBUG_UART
uart_init(115200, 8, 'N', 1);
Expand Down
46 changes: 46 additions & 0 deletions hal/stm32u5.c
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,40 @@ static void clock_pll_off(void)
DMB();
}

#if defined(BOOT_BENCHMARK) && defined(__WOLFBOOT)
#define DEMCR (*(volatile uint32_t *)(0xE000EDFCUL))
#define DEMCR_TRCENA (1UL << 24)
#define DWT_CTRL (*(volatile uint32_t *)(0xE0001000UL))
#define DWT_CYCCNTENA (1UL << 0)
#define DWT_CYCCNT (*(volatile uint32_t *)(0xE0001004UL))

static uint32_t sysclk_mhz;

/* DWT cycle counter scaled by SYSCLK. CYCCNT wraps every ~26 s at 160 MHz;
* the elapsed cycles are accumulated here, so callers must read more often
* than that. */
uint64_t hal_get_timer_us(void)
{
static uint32_t last;
static uint64_t cycles;
static int started;
uint32_t now;

/* DWT_CTRL survives a system reset but DEMCR.TRCENA does not, so
* CYCCNTENA alone does not mean the counter runs */
if (!started) {
DEMCR |= DEMCR_TRCENA;
DWT_CTRL |= DWT_CYCCNTENA;
last = DWT_CYCCNT;
started = 1;
}
now = DWT_CYCCNT;
cycles += (uint32_t)(now - last);
last = now;
return cycles / sysclk_mhz;
}
#endif

/* This implementation will setup MSI 48 MHz as PLL Source Mux, PLLCLK as
* System Clock Source */
static void clock_pll_on(int powersave)
Expand Down Expand Up @@ -378,6 +412,9 @@ static void clock_pll_on(int powersave)
pll1q = 2;
pll1r = 1;
pll1fracn = 0;
#if defined(BOOT_BENCHMARK) && defined(__WOLFBOOT)
sysclk_mhz = (48 * pll1n) / (pll1m * pll1r); /* MSIS range 0: 48 MHz */
#endif
pll1rge = RCC_PLL1VCIRANGE_1;
hpre = RCC_AHB_PRESCALER_DIV_NONE;
apb1pre = RCC_APB_PRESCALER_DIV_NONE;
Expand Down Expand Up @@ -617,6 +654,15 @@ void hal_init(void)
fork_bootloader();
#endif
clock_pll_on(0);
#ifdef STM32_PKA_HSI48
{
/* bounded: without HSI48 the PKA fails to init and verify fails */
uint32_t timeout = 1000000;
RCC_CR |= RCC_CR_HSI48ON;
while (((RCC_CR & RCC_CR_HSI48RDY) == 0) && (--timeout != 0))
;
}
#endif

#ifdef DEBUG_UART
uart_init(115200, 8, 'N', 1);
Expand Down
2 changes: 1 addition & 1 deletion lib/wolfssl
Submodule wolfssl updated 448 files
54 changes: 54 additions & 0 deletions options.mk
Original file line number Diff line number Diff line change
Expand Up @@ -1474,6 +1474,60 @@ ifeq ($(VTOR),0)
CFLAGS+=-D"NO_VTOR"
endif

# PKA=1 (ECDSA verify) and STM32_HASH=1 (image hash) run on the STM32H5/U5
# peripherals through wolfCrypt's register-level port, which needs only the
# CMSIS device header from an STM32Cube tree. wolfBoot only, not the test-app.
ifeq ($(WOLFBOOT_TARGET_BUILD),1)
ifneq ($(filter stm32h5 stm32u5,$(TARGET)),)
ifneq ($(filter 1,$(PKA) $(STM32_HASH)),)
ifeq ($(TARGET),stm32h5)
STM32CUBE_H5?=$(shell ls -d $(HOME)/STM32Cube/Repository/STM32Cube_FW_H5_V* 2>/dev/null | sort -V | tail -n 1)
STM32CUBE_FW=$(STM32CUBE_H5)
STM32_CMSIS_DEVICE=$(STM32CUBE_FW)/Drivers/CMSIS/Device/ST/STM32H5xx/Include
ifeq ($(STM32H5_BOARD),h573i_dk)
STM32_DEVICE?=STM32H573xx
else
STM32_DEVICE?=STM32H563xx
endif
STM32_FAMILY=WOLFSSL_STM32H5
else
STM32CUBE_U5?=$(shell ls -d $(HOME)/STM32Cube/Repository/STM32Cube_FW_U5_V* 2>/dev/null | sort -V | tail -n 1)
STM32CUBE_FW=$(STM32CUBE_U5)
STM32_CMSIS_DEVICE=$(STM32CUBE_FW)/Drivers/CMSIS/Device/ST/STM32U5xx/Include
STM32_DEVICE?=STM32U585xx
STM32_FAMILY=WOLFSSL_STM32U5
endif
ifeq ($(wildcard $(STM32_CMSIS_DEVICE)),)
$(error PKA=1/STM32_HASH=1 need the STM32Cube CMSIS headers: set STM32CUBE_H5 or STM32CUBE_U5)
endif
OBJS+=$(WOLFBOOT_LIB_WOLFSSL)/wolfcrypt/src/port/st/stm32.o
STM32_PORT_CFLAGS:=-DWOLFSSL_STM32_BARE -D$(STM32_FAMILY) -D$(STM32_DEVICE) \
-DNO_STM32_RNG -DNO_STM32_CRYPTO -DNO_STM32_HMAC \
-I$(STM32_CMSIS_DEVICE) -I$(STM32CUBE_FW)/Drivers/CMSIS/Include
ifeq ($(PKA),1)
STM32_PORT_CFLAGS+=-DWOLFSSL_STM32_PKA
# PKA init needs the RNG kernel clock, which hal_init starts
CFLAGS+=-DSTM32_PKA_HSI48
endif
ifeq ($(STM32_HASH),1)
ifneq ($(filter 1,$(WOLFTPM) $(SPI_FLASH) $(QSPI_FLASH) $(OCTOSPI_FLASH) \
$(WOLFCRYPT_TZ_PSA) $(WOLFCRYPT_TZ_WOLFHSM)),)
$(error STM32_HASH=1 is not supported with WOLFTPM, SPI/QSPI/OCTOSPI flash, WOLFCRYPT_TZ_PSA or WOLFCRYPT_TZ_WOLFHSM)
endif
# The hash context layout changes, so every object sees the port;
# the register-level HAL objects hide it again.
STM32_HASH_BLOCK_SIZE?=4096
CFLAGS+=$(STM32_PORT_CFLAGS) -DWOLFBOOT_SHA_BLOCK_SIZE=$(STM32_HASH_BLOCK_SIZE)
hal/%.o src/flash_otp_keystore.o: CFLAGS+=-include hal/stm32_hwcrypto.h
else
# The PKA leaves the public wolfCrypt types unchanged, so only
# wolfCrypt itself needs the port.
$(WOLFBOOT_LIB_WOLFSSL)/wolfcrypt/src/%.o: CFLAGS+=$(STM32_PORT_CFLAGS) -DNO_STM32_HASH
endif
endif
endif
endif

ifeq ($(PKA),1)
OBJS += $(PKA_EXTRA_OBJS)
CFLAGS+=$(PKA_EXTRA_CFLAGS)
Expand Down
4 changes: 4 additions & 0 deletions tools/config.mk
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ ifeq ($(ARCH),)
RAM_CODE?=0
DUALBANK_SWAP?=0
IMAGE_HEADER_SIZE?=256
ifneq ($(filter stm32h5 stm32u5,$(TARGET)),)
PKA?=0
endif
PKA?=1
HWPUF?=0
WOLFBOOT_HWPUF_PROVISION?=0
Expand Down Expand Up @@ -126,6 +129,7 @@ CONFIG_VARS:= ARCH TARGET SIGN HASH MCUXSDK MCUXPRESSO MCUXPRESSO_CPU MCUXPRESSO
FLASH_WRITE_UNIT FLASH_ERASE_UNIT \
DISABLE_BACKUP WOLFBOOT_VERSION V NO_MPU ENCRYPT FLAGS_HOME FLAGS_INVERT NO_SWAP_EXT \
SPMATH SPMATHALL RAM_CODE DUALBANK_SWAP IMAGE_HEADER_SIZE PKA TZEN PSOC6_CRYPTO PSOC6_BOARD PSOC6_CM4_APP STM32H5_BOARD \
STM32_HASH STM32CUBE_H5 STM32CUBE_U5 STM32_DEVICE STM32_HASH_BLOCK_SIZE \
WOLFTPM WOLFBOOT_TPM_VERIFY MEASURED_BOOT WOLFBOOT_TPM_SEAL WOLFBOOT_TPM_KEYSTORE WOLFBOOT_SECURE_APP WOLFBOOT_SECURE_HANDOFF_ADDRESS \
WOLFBOOT_TPM_MFG_AUTH_DERIVE \
WOLFBOOT_ATTESTATION_IAK \
Expand Down
Loading