From edd7c041b0bb5cf016b0de4ef57a99e9612cd4b1 Mon Sep 17 00:00:00 2001 From: David Garske Date: Wed, 2 Dec 2026 16:55:55 -0800 Subject: [PATCH] stm32h5, stm32u5: verify images with the PKA and HASH peripherals --- .github/workflows/test-configs.yml | 24 +++++++++++++ docs/Targets.md | 42 +++++++++++++++++++++++ hal/stm32_hwcrypto.h | 38 +++++++++++++++++++++ hal/stm32h5.c | 52 ++++++++++++++++++++++++++++ hal/stm32u5.c | 46 +++++++++++++++++++++++++ lib/wolfssl | 2 +- options.mk | 54 ++++++++++++++++++++++++++++++ tools/config.mk | 4 +++ 8 files changed, 261 insertions(+), 1 deletion(-) create mode 100644 hal/stm32_hwcrypto.h diff --git a/.github/workflows/test-configs.yml b/.github/workflows/test-configs.yml index 3314396828..159ea90be7 100644 --- a/.github/workflows/test-configs.yml +++ b/.github/workflows/test-configs.yml @@ -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: @@ -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: diff --git a/docs/Targets.md b/docs/Targets.md index 27a9153df7..0462efd5ce 100644 --- a/docs/Targets.md +++ b/docs/Targets.md @@ -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 @@ -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 diff --git a/hal/stm32_hwcrypto.h b/hal/stm32_hwcrypto.h new file mode 100644 index 0000000000..6e0781f9d8 --- /dev/null +++ b/hal/stm32_hwcrypto.h @@ -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 */ diff --git a/hal/stm32h5.c b/hal/stm32h5.c index ae3fa3f422..ee19c86cb0 100644 --- a/hal/stm32h5.c +++ b/hal/stm32h5.c @@ -451,6 +451,40 @@ 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. */ @@ -458,12 +492,15 @@ 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 @@ -471,6 +508,7 @@ static void clock_pll_on(void) pllq = 5; /* 100Mhz */ pllr = 2; #else + src_mhz = 4; pllm = 4; plln = 31; pllp = 2; @@ -478,6 +516,11 @@ static void clock_pll_on(void) 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); @@ -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); diff --git a/hal/stm32u5.c b/hal/stm32u5.c index 1c467a9530..4913c54296 100644 --- a/hal/stm32u5.c +++ b/hal/stm32u5.c @@ -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) @@ -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; @@ -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); diff --git a/lib/wolfssl b/lib/wolfssl index c81176ab76..969637a71f 160000 --- a/lib/wolfssl +++ b/lib/wolfssl @@ -1 +1 @@ -Subproject commit c81176ab76f02538ea8caaa414bb4390adaa7c7e +Subproject commit 969637a71fa58d4f8847b1c7b25e72d9b9cabb5c diff --git a/options.mk b/options.mk index d29b117cac..840d2c74bc 100644 --- a/options.mk +++ b/options.mk @@ -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) diff --git a/tools/config.mk b/tools/config.mk index ead717fbd7..98f5398af5 100644 --- a/tools/config.mk +++ b/tools/config.mk @@ -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 @@ -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 \