Repository navigation
STM32 RNG: fix the StdPeriph seed hang and ride out clock errors in the NOLIB arm - #12265
Conversation
The Standard Peripheral Library arm of the STM32_RNG wc_GenerateSeed() checked SECS/CECS once after enabling the RNG and then polled DRDY for each output byte with no bound. A seed or clock error raised during the request keeps DRDY low, so the call never returned and held the crypto hardware mutex, blocking every other hardware crypto user. This was reproduced on an STM32F407 by switching off the PLL that feeds the RNG kernel clock part way through a seed request: RNG_SR reads 0x22 (CECS | CEIS) and the old code spins in RNG_GetFlagStatus() for good. Bound every wait and handle the errors as the F2/F4 reference manuals describe: - Seed error: clear SEIS, disable and re-enable the RNG, and discard the first word after the restart. - Clock error: data already generated stays valid and DRDY returns when the clock recovers, so wait it out within the poll budget; if it outlasts the budget, fail straight away, as a restart cannot cure it. - No data within STM32_BARE_RNG_BYTE_TIMEOUT polls also restarts the RNG, and after STM32_BARE_RNG_MAX_RETRIES restarts for one byte the function returns RNG_FAILURE_E. The two tunables move above the STM32 arms so the direct-register and StdPeriph arms share them. SEIS and CEIS are cleared with one RNG_ClearFlag() call each, as the StdPeriph library asserts a single flag. On the STM32F407 the fixed code returns RNG_FAILURE_E about 0.6 s after the RNG clock dies (16 MHz SYSCLK) and returns 0 through a 50 ms clock glitch.
The direct-register STM32_RNG arm of wc_GenerateSeed() (WOLFSSL_STM32F427_RNG / WOLFSSL_STM32_RNG_NOLIB / STM32_NUTTX_RNG) broke out of its DRDY poll as soon as CECS was set and then ran the restart sequence, whose discard loop also stopped on CECS. A transient RNG clock error therefore used up all STM32_BARE_RNG_MAX_RETRIES restarts within microseconds and the seed request failed with RNG_FAILURE_E, even though the clock was about to come back. On an STM32F407 with the RNG kernel clock removed for 50 ms part way through a request, this arm returned RNG_FAILURE_E while the StdPeriph arm returned 0. The reference manuals say data already generated stays valid during a clock error and DRDY returns once the clock recovers. Poll for DRDY or a seed error only, so a clock error is ridden out within the poll budget; use RNG_DR whenever DRDY is set without SECS, and acknowledge a clock error seen while waiting by clearing CEIS. The discard loop after a restart likewise only bails out on a seed error. Seed errors, and running out of poll budget (including a clock error that outlasts it), still go through the existing restart sequence and retry limit. On the STM32F407 this arm now returns 0 through the 50 ms glitch and still returns RNG_FAILURE_E when the clock never comes back.
There was a problem hiding this comment.
🟡 Changes recommended
The RNG polling and recovery loop lacks automated fault-injection regression coverage, and the described F417 fault runner is not wired into CI to verify recovery, retry exhaustion, and mutex release on failure.
1 open issue
What changed in this PR
This PR improves STM32 hardware seed generation in wolfCrypt by preventing StdPeriph RNG hangs and allowing the direct-register implementation to tolerate transient clock errors.
Changes:
- Shares polling and retry limits between the two STM32 implementations.
- Adds bounded StdPeriph polling, seed-error recovery, and mutex release on failure.
- Updates direct-register polling to wait through clock glitches while preserving seed-error recovery.
| File | Description |
|---|---|
wolfcrypt/src/random.c |
Bounds STM32 RNG waits and improves seed- and clock-error handling. |
Review effort: Balanced
|
The StdPeriph hang fix and the direct-register clock-error change in wc_GenerateSeed() had no CI coverage: test_wc_GenerateSeed() cannot inject hardware faults, and stm32-sim.yml ran only the H753, U585 and MP135 wolfCrypt tests. Bump SIMULATORS_REF to the STM32Sim revision that adds an STM32F417 target with RNG fault injection, and add its run-rng-fault-f417.sh entry point to the matrix. It runs both the StdPeriph and WOLFSSL_STM32_RNG_NOLIB arms through recoverable and persistent seed and clock errors and checks each call returns the expected code (0 after recovery, RNG_FAILURE_E once the retries run out) rather than hanging, and that the crypto hardware mutex is released on every return path. On master the StdPeriph arm times out in three of the scenarios. The simulator is now fetched by SHA, which also works for a commit that is still under review in a simulators pull request.
wolfSSL/simulators does not serve 2bb0193 yet: GitHub has not picked up that push to the simulators pull request, so fetching it by SHA fails with "not our ref" and every stm32-sim job stopped at the clone step. Pin 02351c8 instead, the commit wolfSSL/simulators currently serves for that pull request. It already has the STM32F417 target, the RNG fault runner and the crypto mutex check; the newer commit only tightens the simulators repo's own CI scripts and how a zero-read fault latches, neither of which this workflow relies on.
SparkiDev
left a comment
There was a problem hiding this comment.
The check just above (line 10385, RNG_GetFlagStatus(RNG_FLAG_SECS | RNG_FLAG_CECS)) has two problems now. First, it passes two flags together, and StdPeriph's IS_RNG_GET_FLAG assert only accepts one. That's the same reason the new code clears SECS and CECS with separate RNG_ClearFlag() calls, so it fails under USE_FULL_ASSERT. Second, it returns RNG_FAILURE_E straight away on a clock error that's already showing at entry, while the loop below would wait it out. The loop handles both SECS and CECS now, so I think the check can be removed. Otherwise, split it into two single-flag calls.
The check after RNG_Cmd(ENABLE) passed RNG_FLAG_SECS | RNG_FLAG_CECS to RNG_GetFlagStatus(), which the Standard Peripheral Library's IS_RNG_GET_FLAG() assert rejects, and it failed straight away on a clock error that the loop below would ride out. The loop handles seed and clock errors itself, so the check is removed. The first word after the initial RNG_Cmd(ENABLE) is now discarded, like the first word after a restart.

Description
Two fixes to the STM32 hardware RNG arms of
wc_GenerateSeed()inrandom.c, in two commits. Based on master.StdPeriph arm hangs on an RNG error (F-14646)
The Standard Peripheral Library arm (
WOLFSSL_STM32F4etc. with neitherWOLFSSL_STM32_CUBEMXnorWOLFSSL_STM32_BARE) checked SECS/CECS once after enabling the RNG, then polled DRDY for every output byte with no bound. A seed or clock error raised during the request keeps DRDY low, so the call never returned, and it held the crypto hardware mutex, blocking every other hardware crypto user.Every wait is now bounded, and errors are handled the way the F2/F4 reference manuals (RM0033 / RM0090, RNG "Error management") describe:
STM32_BARE_RNG_MAX_RETRIESrestarts for one byte the call returnsRNG_FAILURE_E.STM32_BARE_RNG_BYTE_TIMEOUT/STM32_BARE_RNG_MAX_RETRIESmove above the STM32 arms, so the direct-register and StdPeriph arms share them. SEIS and CEIS are cleared with oneRNG_ClearFlag()call each, because the StdPeriph library asserts a single flag.Direct-register arm gives up on a transient clock error
The
WOLFSSL_STM32F427_RNG/WOLFSSL_STM32_RNG_NOLIB/STM32_NUTTX_RNGarm doesn't hang, but it broke out of its DRDY poll as soon as CECS was set and went straight into the restart sequence, whose discard loop also stopped on CECS. A short clock glitch therefore used up all the restarts within microseconds and the seed request failed. The poll now breaks only on DRDY or SECS, so a clock error is ridden out within the budget.RNG_DRis used whenever DRDY is set without SECS, and CEIS is cleared once the data has been read. Seed errors, and running out of poll budget (including a clock error that lasts longer than the budget), still go through the existing restart sequence and retry limit.Testing
STM32F407 (STM32F4DISCOVERY), real silicon. The test firmware runs SYSCLK from the 16 MHz HSI and uses the main PLL only for the 48 MHz RNG kernel clock (PLL48CLK). A SysTick interrupt switches the PLL off part way through a 32 KB seed request. The silicon reports a genuine clock error: RNG_SR reads
0x22(CECS | CEIS) and DRDY stops. The StdPeriph calls go through a small shim, because the ST library isn't redistributable; everything else is the real wolfSSL code.RNG_GetFlagStatus(), still there 8 s laterRNG_FAILURE_Eabout 0.6 s after the faultRNG_FAILURE_Eat onceRNG_FAILURE_Eat onceRNG_FAILURE_Eafter about 3.5 s (16 MHz SYSCLK)STM32Sim. A new STM32F417 target with RNG seed/clock fault injection runs the same firmware for both arms under recoverable and sticky seed and clock errors. Every scenario returns the expected code with this PR. On master, the StdPeriph arm times out in
RNG_GetFlagStatus()for the seed errors and the dead clock. The simulator PR, wolfSSL/simulators#27, adds this as a CI job; it should merge after this one.All variants build with no
random.cwarnings under-Wall -Wextra -Wdeclaration-after-statement -Wshadow(arm-none-eabi-gcc 15.3,STM32F407xx/STM32F417xx).Checklist