Skip to content
Merged
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
22 changes: 18 additions & 4 deletions .github/workflows/stm32-sim.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,25 @@ permissions:
# hardware. Replaces the previous Renode-based STM32H753 workflow and adds
# U5/PKA + MP135 (SHA3/SHAKE on HASH1) coverage.
#
# The F417-RNG-fault entry runs the STM32F4 RNG fault test: the StdPeriph
# and WOLFSSL_STM32_RNG_NOLIB arms of wc_GenerateSeed() on an emulated
# STM32F417 whose RNG raises recoverable and persistent seed and clock
# errors mid-request. Each call must return the expected code (0 after
# recovery, RNG_FAILURE_E once the retries run out) instead of hanging,
# and must leave the crypto hardware mutex released.
#
# Dockerfile.wolfcrypt reads wolfSSL from /opt/wolfssl at runtime via a
# bind mount, so unlike se050-sim.yml / stsafe-a120-sim.yml no Dockerfile
# patching is required - we just mount the PR checkout.
#
# The simulators repo is pinned via SIMULATORS_REF so the MP135 SHAKE-
# enabling sed patch below has a known anchor in user_settings.h. Bump
# the pin when simulators changes are needed.
# the pin when simulators changes are needed. The commit is fetched by
# SHA, so the pin may also name a commit still under review in a
# simulators pull request.

env:
SIMULATORS_REF: 840da2f4a28a9e3027c127da38d758ded902d926
SIMULATORS_REF: 02351c8ca4d11fa19b52c5c8b14a7b9901d4b250

jobs:
stm32_sim:
Expand All @@ -55,6 +64,8 @@ jobs:
script: run-wolfcrypt-u5.sh
- chip_label: MP135
script: run-wolfcrypt-mp135.sh
- chip_label: F417-RNG-fault
script: run-rng-fault-f417.sh
steps:
- name: Checkout wolfSSL (PR source)
uses: actions/checkout@v5
Expand All @@ -63,8 +74,11 @@ jobs:

- name: Clone STM32 simulator
run: |
git clone https://github.com/wolfSSL/simulators simulators
cd simulators && git checkout "$SIMULATORS_REF"
git init -q simulators
cd simulators
git remote add origin https://github.com/wolfSSL/simulators
git fetch --depth 1 origin "$SIMULATORS_REF"
git checkout -q FETCH_HEAD

# The MP135 firmware in the simulators repo currently disables SHAKE
# in user_settings.h with a comment pointing at the wolfSSL build
Expand Down
137 changes: 107 additions & 30 deletions wolfcrypt/src/random.c
Original file line number Diff line number Diff line change
Expand Up @@ -10012,6 +10012,17 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
#include <stdio.h>
#endif

/* Bounds on the RNG_SR polling in the direct-register and Standard
* Peripheral Library arms below: STM32_BARE_RNG_BYTE_TIMEOUT polls per
* output byte before the RNG is restarted, and at most
* STM32_BARE_RNG_MAX_RETRIES restarts per byte before wc_GenerateSeed()
* gives up with RNG_FAILURE_E. */
#ifndef STM32_BARE_RNG_BYTE_TIMEOUT
#define STM32_BARE_RNG_BYTE_TIMEOUT 0x40000
#endif
#ifndef STM32_BARE_RNG_MAX_RETRIES
#define STM32_BARE_RNG_MAX_RETRIES 8
#endif

#ifdef WOLFSSL_STM32_CUBEMX
int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
Expand Down Expand Up @@ -10085,15 +10096,13 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
* forever on chips where the RNG kernel clock is unstable
* (e.g. WL55 with RNGSEL = MSI under sustained ECDSA-key-gen
* load), because once the IP latches a Seed-error or Clock-
* error condition it stops asserting DRDY. Per the STM32 RM
* recovery sequence: clear SEIS/CEIS, toggle RNGEN, discard the
* stale words sitting in the RNG output, then retry. */
#ifndef STM32_BARE_RNG_BYTE_TIMEOUT
#define STM32_BARE_RNG_BYTE_TIMEOUT 0x40000
#endif
#ifndef STM32_BARE_RNG_MAX_RETRIES
#define STM32_BARE_RNG_MAX_RETRIES 8
#endif
* error condition it stops asserting DRDY. A clock error is first
* ridden out within the poll budget (DRDY returns when the clock
* recovers); a seed error, or no data by the end of the budget, runs
* the STM32 RM recovery sequence: clear SEIS/CEIS, toggle RNGEN,
* discard the stale words sitting in the RNG output, then retry. The
* bounds are STM32_BARE_RNG_BYTE_TIMEOUT / STM32_BARE_RNG_MAX_RETRIES
* (above). */

/* Bring the direct-register RNG to a producing state: clock enable, the
* new-gen (C5) NIST conditioning under CONDRST on the first call, and
Expand Down Expand Up @@ -10203,6 +10212,7 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
word32 guard;
word32 retries;
word32 sr;
int clkErr;
(void)os;

ret = wolfSSL_CryptHwMutexLock();
Expand Down Expand Up @@ -10238,25 +10248,37 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
* happy-path / error checks have a defined value even
* if STM32_BARE_RNG_BYTE_TIMEOUT is configured to 0. */
sr = WC_RNG_SR;
/* Bounded DRDY poll -- breaks on either DRDY or any
* error indication (SECS/CECS). */
clkErr = 0;
/* Bounded DRDY poll -- breaks on DRDY or a seed error
* (SECS). A clock error (CECS) is ridden out here: data
* already generated stays valid and DRDY returns once the
* kernel clock recovers, so restarting the RNG at the first
* sign of one would only burn the retry budget on a
* transient glitch. */
while (t < STM32_BARE_RNG_BYTE_TIMEOUT) {
sr = WC_RNG_SR;
if ((sr & (RNG_SR_DRDY | RNG_SR_SECS |
RNG_SR_CECS)) != 0U) {
if ((sr & (RNG_SR_DRDY | RNG_SR_SECS)) != 0U) {
break;
}
if ((sr & RNG_SR_CECS) != 0U) {
clkErr = 1;
}
t++;
}

/* Happy path: data ready and no error. */
if ((sr & RNG_SR_DRDY) != 0U &&
(sr & (RNG_SR_SECS | RNG_SR_CECS)) == 0U) {
/* Happy path: data ready and no seed error. */
if ((sr & RNG_SR_DRDY) != 0U && (sr & RNG_SR_SECS) == 0U) {
output[i] = WC_RNG_DR;
if (clkErr || (sr & RNG_SR_CECS) != 0U) {
/* Acknowledge the clock error (clears CEIS; see
* the SR write note in the recovery below). */
WC_RNG_SR = 0;
}
break;
}

/* Either timed out or an error latched. Recover. */
/* Seed error, or no data within the poll budget (which
* includes a clock error that outlasted it). Recover. */
if (++retries > STM32_BARE_RNG_MAX_RETRIES) {
#ifdef WC_STM32_RNG_DIAG
printf("[RNG] retry max byte=%lu sr=%08lx CR=%08lx\n",
Expand Down Expand Up @@ -10302,10 +10324,10 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
(void)WC_RNG_DR;
t++;
}
else if ((WC_RNG_SR &
(RNG_SR_SECS | RNG_SR_CECS)) != 0U) {
/* Clock-error during recovery -- bail and
* let the outer retry handle it. */
else if ((WC_RNG_SR & RNG_SR_SECS) != 0U) {
/* Seed error during recovery -- bail and let
* the outer retry handle it. A clock error is
* ridden out within 'guard', as above. */
break;
}
}
Expand All @@ -10319,11 +10341,30 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)

#else

/* Generate a RNG seed using the STM32 Standard Peripheral Library */
/* Generate a RNG seed using the STM32 Standard Peripheral Library.
*
* Every wait is bounded, so a failing RNG returns RNG_FAILURE_E instead
* of hanging with the crypto hardware mutex held. Errors are handled as
* the F2/F4 reference manuals (RM0033 / RM0090, RNG "Error management")
* describe:
* - Seed error (SECS): generation stops while SECS is set and any word
* in RNG_DR must not be used. Clear SEIS, then disable and re-enable
* the RNG to restart it.
* - Clock error (CECS): the RNG kernel clock is too slow. Data already
* generated stays valid and DRDY returns once the clock recovers, so
* a clock error is waited out within the poll budget; one that
* outlasts it fails straight away, as a restart cannot cure it.
* A byte still not ready after STM32_BARE_RNG_BYTE_TIMEOUT polls, or a
* seed error, restarts the RNG. The first word after the RNG is enabled
* or restarted is discarded. After STM32_BARE_RNG_MAX_RETRIES restarts
* for one byte the function gives up. */
int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
{
int ret;
word32 i;
word32 t;
word32 retries;
int discard;
(void)os;

ret = wolfSSL_CryptHwMutexLock();
Expand All @@ -10340,15 +10381,51 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
/* enable RNG peripheral */
RNG_Cmd(ENABLE);

/* verify no errors with RNG_CLK or Seed */
if (RNG_GetFlagStatus(RNG_FLAG_SECS | RNG_FLAG_CECS) != RESET) {
wolfSSL_CryptHwMutexUnLock();
return RNG_FAILURE_E;
}

/* An error already showing here is handled by the loop below like
* one raised later, so it is not checked separately. */
for (i = 0; i < sz; i++) {
/* wait until RNG number is ready */
while (RNG_GetFlagStatus(RNG_FLAG_DRDY) == RESET) { }
retries = 0;
/* the first word after RNG_Cmd(ENABLE) above is not used */
discard = (i == 0);
t = 0;
for (;;) {
if (RNG_GetFlagStatus(RNG_FLAG_SECS) == RESET) {
if (RNG_GetFlagStatus(RNG_FLAG_DRDY) != RESET) {
Comment thread
LinuxJedi marked this conversation as resolved.
Resolved
if (!discard) {
break;
}
/* first word after enabling or restarting the RNG */
(void)RNG_GetRandomNumber();
discard = 0;
t = 0;
continue;
}
/* No data yet (this is also where a clock error is
* ridden out). */
if (++t < STM32_BARE_RNG_BYTE_TIMEOUT) {
continue;
}
/* A clock error that outlasted the whole poll budget
* will not be cured by restarting the RNG. */
if (RNG_GetFlagStatus(RNG_FLAG_CECS) != RESET) {
retries = STM32_BARE_RNG_MAX_RETRIES;
}
}

/* Seed error, or no data within the poll budget: restart
* the RNG. */
if (++retries > STM32_BARE_RNG_MAX_RETRIES) {
wolfSSL_CryptHwMutexUnLock();
return RNG_FAILURE_E;
}
/* one flag per call: StdPeriph asserts a single flag */
RNG_ClearFlag(RNG_FLAG_SECS);
RNG_ClearFlag(RNG_FLAG_CECS);
RNG_Cmd(DISABLE);
RNG_Cmd(ENABLE);
discard = 1;
t = 0;
}

/* get value */
output[i] = RNG_GetRandomNumber();
Expand Down
Loading