Skip to content

STM32Sim: add STM32F417/F407 targets with RNG fault injection - #27

Open
LinuxJedi wants to merge 4 commits into
wolfSSL:mainfrom
LinuxJedi:stm32sim-f417
Open

LinuxJedi wants to merge 4 commits into
wolfSSL:mainfrom
LinuxJedi:stm32sim-f417

Conversation

@LinuxJedi

@LinuxJedi LinuxJedi commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Adds an STM32F417 (and F407) target to STM32Sim, so wolfSSL's F4 hardware crypto and its STM32 RNG seed code can be tested in CI. It started from a wolfSSL wc_GenerateSeed() that hung forever when the F4 RNG raised an error mid-request. That was reproduced on an STM32F4DISCOVERY first, and the same firmware now runs here.

Also fixes the PIC32MZ CI (last commit), which wolfSSL master broke for every simulators PR; see the PIC32MZSim section below.

Merge order: the new workflow tests wolfSSL master and needs these merged first:

How it was validated

  • Silicon first. firmware/rng-fault-f4 was run on an STM32F4DISCOVERY (STM32F407), where it stops the RNG kernel clock from SysTick part way through a seed request. RNG_SR reads 0x22 (CECS | CEIS). The unfixed StdPeriph arm hangs, and the fixed one returns. The simulator's fault model follows what the chip did: DRDY stays low during the error, CECS clears when the clock returns, and CEIS stays latched until cleared.
  • scripts/run-rng-fault-f417.sh passes all 10 scenarios against wolfssl#12265. Against master it fails, reporting the PC the StdPeriph arm hung at (RNG_GetFlagStatus).
  • scripts/run-wolfcrypt-f417.sh passes the full wolfCrypt test on --chip stm32f417: DES/3DES (CBC and ECB) and AES ECB/CBC/CTR on the CRYP, MD5/SHA-1 on the HASH, the HAL RNG, AES-GCM through wolfSSL's software GHASH fallback, RSA and ECC. Tracing MMIO confirms the CRYP and HASH are heavily used.
  • Image and regressions. Both scripts were run in a locally built Dockerfile.wolfcrypt image. The existing H7, U5 and MP135 wolfCrypt runs still pass in that image, since the RNG and CRYP v1 models are shared. cargo test passes, including the smoke firmware.

Changes

  • Chips (chips/src/stm32f417.rs): stm32f417 (CRYP, HASH, RNG, USART1/2/3, RCC, flash interface) and stm32f407 (no CRYP/HASH), with the RM0090 memory map. ChipOptions carries run-time knobs to every chip.
  • CRYP v1:
    • DES/TDES ECB/CBC (des crate).
    • CrypV1::new_f4() drops the GCM/CCM mode bits, as F405/F415/F417 silicon does, so firmware that tries GCM gets wrong output instead of a model that silently works.
    • The IV registers track the chaining value (CBC: last ciphertext block, CTR: counter). wolfSSL's STM32_HAL_V2 DES path reads them back, which RM0090's context-swap procedure supports.
  • HASH v1: HashV1::new_f4(), SHA-1/MD5 only. ALGO bit 18 is reserved, and the extended HR window is absent.
  • USART: the F1/F2/F4 register layout (Usart::new_legacy). RCC: F4 ready bits.
  • RNG fault injection: --rng-fault seed|clock:<reads>[:sticky].
    • A seed error latches SECS/SEIS and clears once firmware clears SEIS and cycles RNGEN.
    • A clock error latches CECS/CEIS and clears after a short glitch.
    • :sticky never clears.
    • It works on every chip; the recovery rules are the F2/F4 ones.
  • Runner: a run that hits --timeout logs the PC it stopped at.
  • Firmware:
    • rng-fault-f4: the StdPeriph and NOLIB arms, for the sim or the Discovery board (board-run.sh flashes it and reads the result back with OpenOCD). ST's StdPeriph library isn't vendored; a small shim implements the calls the seed code makes.
    • wolfcrypt-test-f417: the STM32CubeF4 HAL (stm32f4xx-hal-driver, STM32_HAL_V2).
  • CI and docs: Dockerfile.wolfcrypt adds cmsis-device-f4 and stm32f4xx-hal-driver; there is a new stm32-wolfcrypt-test-f417 workflow; READMEs are updated.

Found along the way

  • wolfSSL's STM32 DES ECB couldn't link and ECB decrypt actually encrypted. That is wolfssl#12266.
  • The wolfSSL direct-register (NOLIB) RNG arm gave up on a transient clock glitch, on silicon as well as here. That is the second commit of wolfssl#12265.

PIC32MZSim CI fix

wolfSSL master 76aed2f9d4 ("Use available entropy sources in embedded configurations") turned weak PIC32 seed sources into #errors. That broke three of the four PIC32MZ wolfCrypt jobs on every PR:

  • EC direct: the EC has no TRNG, so wolfSSL seeds its RNG block from the core timer, which now needs WOLFSSL_ACCEPT_WEAK_ENTROPY.
  • EC and EF Harmony: the Harmony build never defined WOLFSSL_PIC32MZ_RNG (the direct port gets it with WOLFSSL_MICROCHIP_PIC32MZ), so wolfSSL fell back to rand() seeded from the core timer. That is now an #error too, even on the EF, which has a TRNG.

PIC32MZSim/firmware/common/user_settings.h now defines WOLFSSL_PIC32MZ_RNG for the Harmony tier, so it seeds from the RNG block like the direct port and the EF uses its TRNG. It also defines WOLFSSL_ACCEPT_WEAK_ENTROPY on EC parts only, keyed on __PIC32_FEATURE_SET1 the same way wolfSSL's own check is. All four tiers (EC/EF, direct/Harmony) pass against wolfSSL master in the CI images.

Adds the F4 crypto block to STM32Sim so wolfSSL's F4 hardware crypto
and STM32 RNG seed code can be tested in CI, prompted by a wolfSSL
wc_GenerateSeed() that hung forever when the F4 RNG raised an error
mid-request (reproduced on an STM32F4DISCOVERY first).

Simulator:
- stm32f417 chip (CRYP, HASH, RNG, USART1/2/3, RCC) and stm32f407 (no
  CRYP/HASH), with the RM0090 memory map.
- CRYP v1: DES/TDES ECB/CBC; an F4 variant that drops the GCM/CCM mode
  bits as F405/F415/F417 silicon does; the IV registers track the
  chaining value, as wolfSSL's HAL_V2 DES path reads them back.
- HASH v1: an F4 layout with SHA-1/MD5 only (ALGO bit 18 reserved, no
  extended HR window).
- USART: the F1/F2/F4 register layout. RCC: F4 ready bits.
- RNG: --rng-fault seed|clock:<reads>[:sticky] injects a seed or clock
  error after a number of RNG_DR reads, with the RM0090 recovery rules
  (seed error clears on SEIS clear + RNGEN cycle; clock error clears
  after a short glitch; sticky never clears). Works on every chip.
- A run that hits --timeout logs the PC it stopped at.

Firmware and CI:
- firmware/rng-fault-f4: wc_GenerateSeed() fault test for the
  StdPeriph and WOLFSSL_STM32_RNG_NOLIB arms. Builds for stm32-sim or
  for a real STM32F4DISCOVERY, where it stops the RNG kernel clock from
  SysTick (board-run.sh flashes and reads it back over OpenOCD).
  ST's StdPeriph library is not vendored; a small shim stands in.
- firmware/wolfcrypt-test-f417: the wolfCrypt test through the
  STM32CubeF4 HAL (CRYP for DES/TDES and AES ECB/CBC/CTR, HASH for
  SHA-1/MD5, RNG), including the software GCM fallback and DES ECB.
- scripts/run-wolfcrypt-f417.sh, scripts/run-rng-fault-f417.sh, the
  Docker image gains cmsis-device-f4 and stm32f4xx-hal-driver, and a new
  stm32-wolfcrypt-test-f417 workflow runs both.

The workflow tests wolfSSL master, so it needs the wolfSSL fixes for the
StdPeriph RNG hang, the NOLIB clock-error handling and the STM32 DES
ECB build merged first.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Zero-read RNG faults are not latched before the first data read, and both CI scripts can report success despite simulator failure after expected output appears.

3 open issues
What changed in this PR

Adds STM32F417 and STM32F407 support to STM32Sim so CI can exercise wolfSSL’s F4 hardware crypto and reproduce RNG error-handling hangs observed on hardware.

Changes:

  • Adds F4 chip configurations, legacy USART support, and F4-specific CRYP/HASH behavior, including DES/TDES.
  • Adds configurable RNG fault injection and program-counter reporting on timeout.
  • Adds wolfCrypt and RNG regression firmware, run scripts, Docker support, and an F417 CI workflow.
File Description
STM32Sim/​stm32-sim/​runner-bin/​src/​main.rs Exposes RNG fault injection and timeout diagnostics.
STM32Sim/​stm32-sim/​peripherals/​src/​usart.rs Adds the legacy F4 USART layout and tests.
STM32Sim/​stm32-sim/​peripherals/​src/​rng.rs Models seed and clock faults with recovery rules.
STM32Sim/​stm32-sim/​peripherals/​src/​rcc.rs Adds F4 clock-ready bits.
STM32Sim/​stm32-sim/​peripherals/​src/​lib.rs Exports fault and USART layout types.
STM32Sim/​stm32-sim/​peripherals/​src/​hash/​v1.rs Adds F4 SHA-1/MD5-only behavior.
STM32Sim/​stm32-sim/​peripherals/​src/​cryp/​v1.rs Adds F4 modes, DES/TDES wiring, and IV readback.
STM32Sim/​stm32-sim/​peripherals/​src/​cryp/​mod.rs Implements shared DES/TDES block processing.
STM32Sim/​stm32-sim/​peripherals/​Cargo.toml Enables the DES dependency.
STM32Sim/​stm32-sim/​core/​src/​runner.rs Records the program counter on timeout.
STM32Sim/​stm32-sim/​chips/​src/​stm32u575.rs Passes RNG fault options to U5 targets.
STM32Sim/​stm32-sim/​chips/​src/​stm32mp135.rs Passes RNG fault options to MP135.
STM32Sim/​stm32-sim/​chips/​src/​stm32h753.rs Passes RNG fault options to H753.
STM32Sim/​stm32-sim/​chips/​src/​stm32f417.rs Defines F417/F407 memory and peripheral maps.
STM32Sim/​stm32-sim/​chips/​src/​lib.rs Registers F4 targets and shared chip options.
STM32Sim/​stm32-sim/​Cargo.toml Declares the workspace DES dependency.
STM32Sim/​scripts/​run-wolfcrypt-f417.sh Builds and runs F417 wolfCrypt firmware.
STM32Sim/​scripts/​run-rng-fault-f417.sh Runs ten RNG fault regression scenarios.
STM32Sim/​README.md Documents F4 support and fault injection.
STM32Sim/​firmware/​wolfcrypt-test-f417/​user_settings.h Configures wolfSSL’s F4 crypto paths.
STM32Sim/​firmware/​wolfcrypt-test-f417/​toolchain-arm-none-eabi.cmake Configures Cortex-M4 compilation and vendor headers.
STM32Sim/​firmware/​wolfcrypt-test-f417/​stm32f4xx_hal_conf.h Enables required F4 HAL modules.
STM32Sim/​firmware/​wolfcrypt-test-f417/​stm32f417.ld Maps firmware, heap, and CCM stack.
STM32Sim/​firmware/​wolfcrypt-test-f417/​startup_stm32f417.c Provides startup and exception vectors.
STM32Sim/​firmware/​wolfcrypt-test-f417/​main.c Runs wolfCrypt and reports completion.
STM32Sim/​firmware/​wolfcrypt-test-f417/​CMakeLists.txt Links wolfSSL, HAL, and test firmware.
STM32Sim/​firmware/​rng-fault-f4/​user_settings.h Selects the two RNG implementation paths.
STM32Sim/​firmware/​rng-fault-f4/​stm32f4.ld Defines the RNG firmware memory layout.
STM32Sim/​firmware/​rng-fault-f4/​stdperiph/​stm32f4xx.h Wraps CMSIS with StdPeriph declarations.
STM32Sim/​firmware/​rng-fault-f4/​stdperiph/​stdperiph_shim.h Declares the RNG/RCC compatibility interface.
STM32Sim/​firmware/​rng-fault-f4/​stdperiph_shim.c Implements register-level StdPeriph calls.
STM32Sim/​firmware/​rng-fault-f4/​startup_stm32f4.c Provides minimal RNG firmware startup.
STM32Sim/​firmware/​rng-fault-f4/​README.md Documents simulator and board fault tests.
STM32Sim/​firmware/​rng-fault-f4/​Makefile Builds simulator and board variants.
STM32Sim/​firmware/​rng-fault-f4/​main.c Exercises seed requests and board clock faults.
STM32Sim/​firmware/​rng-fault-f4/​board-run.sh Flashes hardware and reads diagnostics.
STM32Sim/​Dockerfile.wolfcrypt Packages F4 dependencies, firmware, and scripts.
README.md Lists F417/F407 support.
.github/​workflows/​stm32-wolfcrypt-test-f417.yml Runs F417 crypto and RNG tests against wolfSSL master.

Review effort: Balanced

Comment thread STM32Sim/scripts/run-rng-fault-f417.sh Resolved
Comment thread STM32Sim/scripts/run-wolfcrypt-f417.sh Resolved
wc_GenerateSeed() must release the crypto hardware mutex on every return
path, including when it gives up with RNG_FAILURE_E. In a SINGLE_THREADED
build the mutex itself is a no-op, so a leak would go unnoticed. The
rng-fault-f4 firmware now links with ld --wrap for
wolfSSL_CryptHwMutexLock/UnLock, tracks the lock depth, and reports
"mutex=ok" or "mutex=bad" after each call (also g_hw_mutex_errors for
board runs). run-rng-fault-f417.sh fails a scenario whose mutex is not
released.

With a wolfSSL copy whose StdPeriph retry-exhaustion path skips the
unlock, the seed:200:sticky and clock:200:sticky scenarios now fail with
"mutex='bad'"; the fixed tree passes all ten.

The runner also cleans the copied firmware directory before building,
so build output left in a local checkout can't stand in for a fresh
build against WOLFSSL_ROOT.
- run-wolfcrypt-f417.sh: the firmware prints its pass line before it
  sets test_complete, so a simulator timeout or fault after that line
  still exited 0. Pass now needs the marker and a zero simulator exit
  status, and a run with neither exits non-zero.
- run-rng-fault-f417.sh: likewise, a scenario whose RNG_FAULT line
  matches now also needs the simulator to exit cleanly.
- rng: a fault armed with zero reads latched only after the first
  RNG_DR read had returned fresh data, so seed:0 behaved like seed:1 and
  the firmware's first status check saw a healthy RNG. It now latches
  when armed, so the error is there from the start.
…ropy

wolfSSL master (76aed2f9d4, "Use available entropy sources in embedded
configurations") turned weak PIC32 seed sources into #errors, which
broke three of the four PIC32MZ wolfCrypt tiers:

- EC direct: the EC has no TRNG, so wolfSSL seeds the PIC32MZ RNG block
  from the core timer and now requires WOLFSSL_ACCEPT_WEAK_ENTROPY.
- EC and EF Harmony: the Harmony build never defined
  WOLFSSL_PIC32MZ_RNG (the direct port gets it with
  WOLFSSL_MICROCHIP_PIC32MZ), so wolfSSL fell back to rand() seeded from
  the core timer, which is now an #error too, even on the EF with its
  TRNG.

Define WOLFSSL_PIC32MZ_RNG for the Harmony tier, so it seeds from the
RNG block like the direct port (the EF from its TRNG), and define
WOLFSSL_ACCEPT_WEAK_ENTROPY on EC parts only, keyed on
__PIC32_FEATURE_SET1 as wolfSSL's own check is. All four tiers pass
against wolfSSL master.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants