Skip to content

STM32 RNG: fix the StdPeriph seed hang and ride out clock errors in the NOLIB arm - #12265

Open
LinuxJedi wants to merge 4 commits into
wolfSSL:masterfrom
LinuxJedi:fenrir-14646-stm32-stdperiph-rng
Open

LinuxJedi wants to merge 4 commits into
wolfSSL:masterfrom
LinuxJedi:fenrir-14646-stm32-stdperiph-rng

Conversation

@LinuxJedi

@LinuxJedi LinuxJedi commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Description

Two fixes to the STM32 hardware RNG arms of wc_GenerateSeed() in random.c, in two commits. Based on master.

StdPeriph arm hangs on an RNG error (F-14646)

The Standard Peripheral Library arm (WOLFSSL_STM32F4 etc. with neither WOLFSSL_STM32_CUBEMX nor WOLFSSL_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:

  • Seed error (SECS): clear SEIS, disable and re-enable the RNG, and discard the first word after the restart.
  • Clock error (CECS): data already generated stays valid and DRDY returns when the clock recovers, so the error is waited out within the poll budget. If it outlasts the budget, the call fails straight away, because a restart can't fix the clock.
  • No data within the poll budget: also restarts the RNG. After STM32_BARE_RNG_MAX_RETRIES restarts for one byte the call returns RNG_FAILURE_E.

STM32_BARE_RNG_BYTE_TIMEOUT / STM32_BARE_RNG_MAX_RETRIES 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, 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_RNG arm 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_DR is 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.

Arm Clock never returns Clock returns after 50 ms
StdPeriph, master hangs in RNG_GetFlagStatus(), still there 8 s later 0
StdPeriph, this PR RNG_FAILURE_E about 0.6 s after the fault 0
NOLIB, master RNG_FAILURE_E at once RNG_FAILURE_E at once
NOLIB, this PR RNG_FAILURE_E after about 3.5 s (16 MHz SYSCLK) 0

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.c warnings under -Wall -Wextra -Wdeclaration-after-statement -Wshadow (arm-none-eabi-gcc 15.3, STM32F407xx/STM32F417xx).

Checklist

  • added tests
  • updated/added doxygen
  • updated appropriate READMEs
  • Updated manual and documentation

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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 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

Comment thread wolfcrypt/src/random.c Resolved
@github-actions

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

zephyr-frdm_rw612

  • FLASH: datas +16 B, log_const_area -8 B, rodata +108 B, text +13,832 B (+4.9%, 296,412 B / 67,108,864 B, total: 0% used)
  • RAM: bss +49 B, datas +16 B (+0.1%, 58,423 B / 983,040 B, total: 6% used)

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

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.
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