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
48 changes: 48 additions & 0 deletions .github/workflows/stm32-wolfcrypt-test-f417.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: STM32Sim wolfCrypt test (F417)

on:
push:
branches: [main]
pull_request:
branches: ['**']
workflow_dispatch:

jobs:
wolfcrypt-test:
name: wolfCrypt + RNG fault tests on STM32Sim F417
runs-on: ubuntu-24.04
steps:
- name: Checkout simulator-stm32
uses: actions/checkout@v4
with:
path: simulator-stm32

- name: Checkout wolfSSL
uses: actions/checkout@v4
with:
repository: wolfSSL/wolfssl
ref: master
path: wolfssl

- name: Build stm32sim-wolfcrypt image
run: |
docker build \
-t stm32sim-wolfcrypt:ci \
-f simulator-stm32/STM32Sim/Dockerfile.wolfcrypt \
simulator-stm32/STM32Sim

# CRYP (DES/TDES, AES ECB/CBC/CTR), HASH (SHA-1/MD5) and RNG through
# the STM32CubeF4 HAL.
- name: Run wolfCrypt test on stm32-sim (F417)
run: |
docker run --rm \
-v "${{ github.workspace }}/wolfssl:/opt/wolfssl:ro" \
stm32sim-wolfcrypt:ci run-wolfcrypt-f417.sh

# wc_GenerateSeed() must return, not hang, when the RNG raises a
# seed or clock error mid-request (StdPeriph and NOLIB arms).
- name: Run RNG fault test on stm32-sim (F417)
run: |
docker run --rm \
-v "${{ github.workspace }}/wolfssl:/opt/wolfssl:ro" \
stm32sim-wolfcrypt:ci run-rng-fault-f417.sh
11 changes: 11 additions & 0 deletions PIC32MZSim/firmware/common/user_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@
#define MICROCHIP_MPLAB_HARMONY
#define MICROCHIP_PIC32
#define WOLFSSL_HAVE_MCHP_HW_CRYPTO_HARMONY
/* Seed from the PIC32MZ RNG block, as the direct port does (there it comes
* with WOLFSSL_MICROCHIP_PIC32MZ). Without it wolfSSL falls back to rand()
* seeded from the core timer, which it rejects as weak entropy. */
#define WOLFSSL_PIC32MZ_RNG
#endif

/* The PIC32MZ EC has no TRNG: wolfSSL seeds its RNG block from the core
* timer, which it only accepts as a test configuration. This is a
* simulator, so accept it on EC; EF seeds from its TRNG. */
#if defined(__PIC32_FEATURE_SET1) && (__PIC32_FEATURE_SET1 == 'C')
#define WOLFSSL_ACCEPT_WEAK_ENTROPY
#endif

/* ---- No OS dependencies ---- */
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ supported targets:
- **STM32MP135** (Cortex-A7 with MMU, runs out of DDR with no
internal flash; CRYP1/HASH1/RNG1/PKA modelled, including SHA3 and
SHAKE on HASH1)
- **STM32F417 / F407** (Cortex-M4; F417 CRYP with DES/TDES and AES
ECB/CBC/CTR, HASH with SHA-1/MD5, RNG with seed/clock fault
injection)

## PIC32MZSim

Expand Down
37 changes: 26 additions & 11 deletions STM32Sim/Dockerfile.wolfcrypt
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,21 @@
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.

# Builds the wolfCrypt-on-STM32 firmwares (H753, U585, MP135) that
# today run under Renode CI, then runs them through stm32-sim
# instead. The wolfSSL source tree is expected to be mounted at
# /opt/wolfssl at runtime (the GitHub workflow does
# Builds the wolfCrypt-on-STM32 firmwares (H753, U585, MP135, F417) and
# runs them through stm32-sim. The wolfSSL source tree is expected to be
# mounted at /opt/wolfssl at runtime (the GitHub workflow does
# `docker run -v $(pwd):/opt/wolfssl ...`). Default CMD runs the H7
# firmware; override with `run-wolfcrypt-u5.sh` or
# `run-wolfcrypt-mp135.sh` for the other targets.
# firmware; override with `run-wolfcrypt-u5.sh`, `run-wolfcrypt-mp135.sh`,
# `run-wolfcrypt-f417.sh` or `run-rng-fault-f417.sh` for the others.
# Image contents:
# - arm-none-eabi-gcc cross toolchain
# - CMSIS_5, cmsis-device-h7, STM32CubeH7 v1.11.2 (vendored under /opt)
# - cmsis-device-u5, STM32CubeU5 (vendored under /opt)
# - STM32CubeMP13 (vendored under /opt for the MP135 build)
# - cmsis-device-f4, stm32f4xx-hal-driver (vendored under /opt for F417)
# - stm32-sim runner binary (built from this same repo)
# - run-wolfcrypt-{h7,u5,mp135}.sh entrypoints
# - run-wolfcrypt-{h7,u5,mp135,f417}.sh and run-rng-fault-f417.sh
# entrypoints

# =============================================================================
# Stage 1: build stm32-sim (Rust)
Expand Down Expand Up @@ -59,6 +60,12 @@ RUN git clone --depth 1 \
&& git clone --depth 1 \
https://github.com/STMicroelectronics/cmsis-device-u5.git \
/opt/cmsis-device-u5 \
&& git clone --depth 1 \
https://github.com/STMicroelectronics/cmsis-device-f4.git \
/opt/cmsis-device-f4 \
&& git clone --depth 1 \
https://github.com/STMicroelectronics/stm32f4xx-hal-driver.git \
/opt/stm32f4xx-hal-driver \
&& git clone --depth 1 \
https://github.com/ARM-software/CMSIS_5.git \
/opt/CMSIS_5 \
Expand Down Expand Up @@ -87,7 +94,8 @@ RUN git clone --depth 1 \
&& cd /opt/STM32CubeMP13 \
&& git submodule update --init --recursive --depth 1)) \
&& find /opt/STM32CubeH7 /opt/STM32CubeU5 /opt/STM32CubeMP13 -name '.git' -prune -exec rm -rf {} + \
&& rm -rf /opt/cmsis-device-h7/.git /opt/cmsis-device-u5/.git /opt/CMSIS_5/.git
&& rm -rf /opt/cmsis-device-h7/.git /opt/cmsis-device-u5/.git /opt/CMSIS_5/.git \
/opt/cmsis-device-f4/.git /opt/stm32f4xx-hal-driver/.git

COPY --from=sim-builder /app/stm32-sim/target/release/stm32-sim /usr/local/bin/stm32-sim

Expand All @@ -99,17 +107,24 @@ COPY --from=sim-builder /app/stm32-sim/target/release/stm32-sim /usr/local/bin/s
COPY firmware/wolfcrypt-test-h7/ /opt/firmware-h7/
COPY firmware/wolfcrypt-test-u5/ /opt/firmware-u5/
COPY firmware/wolfcrypt-test-mp135/ /opt/firmware-mp135/
COPY firmware/wolfcrypt-test-f417/ /opt/firmware-f417/
COPY firmware/rng-fault-f4/ /opt/firmware-rng-fault-f4/

COPY scripts/run-wolfcrypt-h7.sh /usr/local/bin/run-wolfcrypt-h7.sh
COPY scripts/run-wolfcrypt-u5.sh /usr/local/bin/run-wolfcrypt-u5.sh
COPY scripts/run-wolfcrypt-mp135.sh /usr/local/bin/run-wolfcrypt-mp135.sh
COPY scripts/run-wolfcrypt-f417.sh /usr/local/bin/run-wolfcrypt-f417.sh
COPY scripts/run-rng-fault-f417.sh /usr/local/bin/run-rng-fault-f417.sh
RUN chmod +x /usr/local/bin/run-wolfcrypt-h7.sh \
/usr/local/bin/run-wolfcrypt-u5.sh \
/usr/local/bin/run-wolfcrypt-mp135.sh
/usr/local/bin/run-wolfcrypt-mp135.sh \
/usr/local/bin/run-wolfcrypt-f417.sh \
/usr/local/bin/run-rng-fault-f417.sh

ENV WOLFSSL_ROOT=/opt/wolfssl

# Default entrypoint runs the H7 wolfCrypt test. Override by passing
# `run-wolfcrypt-u5.sh` (U585) or `run-wolfcrypt-mp135.sh` (MP135) as
# the command for the other targets.
# `run-wolfcrypt-u5.sh` (U585), `run-wolfcrypt-mp135.sh` (MP135),
# `run-wolfcrypt-f417.sh` (F417) or `run-rng-fault-f417.sh` (F4 RNG fault
# regression) as the command for the other targets.
CMD ["run-wolfcrypt-h7.sh"]
61 changes: 46 additions & 15 deletions STM32Sim/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ Cargo workspace under [`stm32-sim/`](stm32-sim):
stm32-sim/
core/ CPU + MMIO bus + ELF loader + Runner
peripherals/ USART, RCC, RNG, CRYP, HASH, PKA
chips/ STM32H753 / STM32U575 / STM32U585 / STM32MP135 chip
configurations (memory map + peripheral wiring)
chips/ STM32F407 / STM32F417 / STM32H753 / STM32U575 /
STM32U585 / STM32MP135 chip configurations (memory
map + peripheral wiring)
runner-bin/ `stm32-sim` CLI binary
```

Expand All @@ -52,19 +53,25 @@ even though three chips might present three different DIN/HR layouts.
## Status

**STM32H753** (Cortex-M7, HAL v1, no PKA), **STM32U575/U585**
(Cortex-M33, HAL v2, PKA v2), and **STM32MP135** (Cortex-A7,
HAL v2 with the H7-style CRYP block, PKA v2) chip targets all boot,
run firmware, and drive their on-chip cryptographic peripherals end-
to-end:

| Peripheral | H7 (v1) | U5 (v2) | MP135 |
|------------|----------------------------------|--------------------|---------------------------------------|
| USART | OK | OK | OK (UART4) |
| RCC | stub | stub | stub |
| RNG | OK | OK | OK (RNG1) |
| CRYP/AES | ECB/CBC/CTR/GCM (HAL-driven) | ECB/CBC/CTR/GCM | ECB/CBC/CTR/GCM (CRYP1, aliased CRYP) |
| HASH | SHA-1/224/256, MD5 | SHA-1/224/256, MD5 | SHA-1/224/256, MD5, SHA-384/512, SHA3-224/256/384/512, SHAKE-128/256 (HASH1) |
| PKA | n/a | ECC mul (P-256/P-384), RSA modexp, mod arithmetic | same as U5 |
(Cortex-M33, HAL v2, PKA v2), **STM32MP135** (Cortex-A7, HAL v2 with
the H7-style CRYP block, PKA v2) and **STM32F417** (Cortex-M4, the F4
CRYP/HASH/RNG block) chip targets all boot, run firmware, and drive
their on-chip cryptographic peripherals end-to-end. **STM32F407** is
the F417 without CRYP and HASH (the STM32F4DISCOVERY part).

| Peripheral | H7 (v1) | U5 (v2) | MP135 | F417 |
|------------|----------------------------------|--------------------|---------------------------------------|-------------------------------|
| USART | OK | OK | OK (UART4) | OK (USART1/2/3, F4 layout) |
| RCC | stub | stub | stub | stub |
| RNG | OK | OK | OK (RNG1) | OK |
| CRYP/AES | ECB/CBC/CTR/GCM (HAL-driven) | ECB/CBC/CTR/GCM | ECB/CBC/CTR/GCM (CRYP1, aliased CRYP) | DES/TDES ECB/CBC, AES ECB/CBC/CTR (no GCM/CCM on this part) |
| HASH | SHA-1/224/256, MD5 | SHA-1/224/256, MD5 | SHA-1/224/256, MD5, SHA-384/512, SHA3-224/256/384/512, SHAKE-128/256 (HASH1) | SHA-1, MD5 |
| PKA | n/a | ECC mul (P-256/P-384), RSA modexp, mod arithmetic | same as U5 | n/a |

The F417 CRYP and HASH reject what the silicon lacks the same way the
silicon does: the GCM/CCM mode bits and the HASH SHA-224/256 ALGO bit
are reserved and dropped, so firmware that tries to use them gets the
wrong answer rather than a silently working model.

The MP135 is bare-metal Cortex-A7 with no internal flash. The firmware
links at the DDR base (0xC0000000); the simulator maps DDR as plain
Expand Down Expand Up @@ -115,6 +122,30 @@ make -C firmware/smoke-test-mp135

Expected output ends with `=== smoke test passed ===` and the binary
exits 0 when the firmware sets `test_result = 0` and `test_complete = 1`.
A run that hits `--timeout` exits 3 and logs the PC the core was at, so
a hung firmware says where it hung.

### RNG fault injection

`--rng-fault <seed|clock>:<reads>[:sticky]` makes the chip's RNG raise
an error after `<reads>` RNG_DR reads (with `0`, the error is there
before the first status check), following the F2/F4 reference manual
error management (RM0033 / RM0090):

- `seed`: SECS and SEIS latch and DRDY stays low until the firmware
clears SEIS and cycles RNGEN.
- `clock`: CECS and CEIS latch and DRDY stays low until the kernel clock
"recovers" a few dozen RNG_SR polls later; CEIS stays set until
cleared.
- `:sticky`: the error never clears (a dead entropy source, a clock
that never comes back).

[`firmware/rng-fault-f4/`](firmware/rng-fault-f4) uses this to check
that wolfSSL's `wc_GenerateSeed()` returns rather than hangs when the
F4 RNG fails mid-request, for both the Standard Peripheral Library and
`WOLFSSL_STM32_RNG_NOLIB` arms (`scripts/run-rng-fault-f417.sh`). The
same firmware also builds for a real STM32F4DISCOVERY, where it stops
the RNG kernel clock itself; see that directory's README.

## Tests

Expand Down
92 changes: 92 additions & 0 deletions STM32Sim/firmware/rng-fault-f4/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# STM32F4 RNG fault test for wolfSSL's wc_GenerateSeed().
#
# make [TARGET=sim|board] [ARM=stdperiph|nolib]
#
# TARGET=sim (default) STM32F417 under stm32-sim; the RNG fault comes from
# the simulator's --rng-fault option.
# TARGET=board STM32F407 (STM32F4DISCOVERY) silicon; the firmware stops the
# RNG kernel clock itself mid-fill. `make TARGET=board flash`
# programs it with OpenOCD.
# ARM=stdperiph (default) selects the Standard Peripheral Library arm of
# wc_GenerateSeed(); ARM=nolib selects WOLFSSL_STM32_RNG_NOLIB.

WOLFSSL_ROOT ?= /opt/wolfssl
CMSIS_CORE ?= /opt/CMSIS_5/CMSIS/Core/Include
CMSIS_DEVICE ?= /opt/cmsis-device-f4/Include
TARGET ?= sim
ARM ?= stdperiph

CC = arm-none-eabi-gcc

ifeq ($(TARGET),board)
DEVICE ?= STM32F407xx
DEFS += -DRNG_FAULT_BOARD
else ifeq ($(TARGET),sim)
DEVICE ?= STM32F417xx
else
$(error TARGET must be sim or board)
endif

ifeq ($(ARM),nolib)
DEFS += -DRNG_ARM_NOLIB
else ifneq ($(ARM),stdperiph)
$(error ARM must be stdperiph or nolib)
endif

# TARGET=board only: GLITCH_MS=<n> restores the RNG clock n ms after
# removing it (a transient clock glitch rather than a dead clock).
ifneq ($(GLITCH_MS),)
DEFS += -DRNG_FAULT_GLITCH_MS=$(GLITCH_MS)
SUFFIX = -glitch$(GLITCH_MS)
endif

BUILD = build/$(ARM)-$(TARGET)$(SUFFIX)
ELF = rng-fault-$(ARM)-$(TARGET)$(SUFFIX).elf

WOLFCRYPT_SRCS = \
$(WOLFSSL_ROOT)/wolfcrypt/src/random.c \
$(WOLFSSL_ROOT)/wolfcrypt/src/sha256.c \
$(WOLFSSL_ROOT)/wolfcrypt/src/memory.c \
$(WOLFSSL_ROOT)/wolfcrypt/src/wc_port.c \
$(WOLFSSL_ROOT)/wolfcrypt/src/logging.c \
$(WOLFSSL_ROOT)/wolfcrypt/src/error.c

LOCAL_SRCS = main.c startup_stm32f4.c stdperiph_shim.c
HEADERS = user_settings.h stdperiph/stm32f4xx.h stdperiph/stdperiph_shim.h

OBJS = $(addprefix $(BUILD)/,$(LOCAL_SRCS:.c=.o)) \
$(addprefix $(BUILD)/wolfcrypt/,$(notdir $(WOLFCRYPT_SRCS:.c=.o)))

ARCH = -mcpu=cortex-m4 -mthumb -mfloat-abi=soft
# stdperiph/ comes before the CMSIS device directory so its StdPeriph-style
# stm32f4xx.h wrapper shadows the CMSIS one (and #include_next's it).
CPPFLAGS = -DWOLFSSL_USER_SETTINGS -D$(DEVICE) $(DEFS) \
-I. -Istdperiph -I$(CMSIS_DEVICE) -I$(CMSIS_CORE) -I$(WOLFSSL_ROOT)
CFLAGS = $(ARCH) -Os -g -ffunction-sections -fdata-sections -Wall
# --wrap lets main.c account for every crypto hardware mutex lock/unlock.
LDFLAGS = $(ARCH) -Tstm32f4.ld -nostartfiles -Wl,--gc-sections \
-Wl,--wrap=wolfSSL_CryptHwMutexLock \
-Wl,--wrap=wolfSSL_CryptHwMutexUnLock \
-specs=nano.specs -specs=nosys.specs -Wl,-Map=$(BUILD)/rng-fault.map

all: $(ELF)

$(ELF): $(OBJS)
$(CC) $(LDFLAGS) -o $@ $^

$(BUILD)/%.o: %.c $(HEADERS)
@mkdir -p $(dir $@)
$(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $<

$(BUILD)/wolfcrypt/%.o: $(WOLFSSL_ROOT)/wolfcrypt/src/%.c $(HEADERS)
@mkdir -p $(dir $@)
$(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $<

flash: $(ELF)
openocd -f interface/stlink.cfg -f target/stm32f4x.cfg \
-c "program $(ELF) verify reset exit"

clean:
rm -rf build rng-fault-*.elf

.PHONY: all flash clean
Loading
Loading