Skip to content

nRF54Crypto: configure the TRNG the way nrfx does - #14

Open
cvaldess wants to merge 1 commit into
meshtastic:masterfrom
cvaldess:pr/trng-nrfx-config
Open

cvaldess wants to merge 1 commit into
meshtastic:masterfrom
cvaldess:pr/trng-nrfx-config

Conversation

@cvaldess

@cvaldess cvaldess commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

cracen_rng_reset() soft-resets the TRNG and enables it with one 128-bit block for conditioning, leaving everything else at its reset value. trng_init() in nrfx_cracen.c also programs the off timer, TRNG_CLK_DIV and a start-up wait of 512, and uses four blocks.

Since #6 every draw starts with that reset, so every draw pays for a TRNG start:

Reset values (today) Configured as nrfx
nRF54L15: first word 530 µs 20 µs
nRF54L15: polling iterations ~2,200 69
nRF54LM20: first word 32.9 ms 18–46 µs
nRF54LM20: polling iterations ~167,700 66–202

On the nRF54L15 this is only waiting: INITWAITVAL resets to 0xFFFF.

On the nRF54LM20 it is a failure. SAMPLINGPERIOD resets to 0xFFF where nrfx uses 1, and ~167,700 iterations run past the 100,000 of RNG_TIMEOUT, so every draw fails. seed_softdevice_rng() then fails, Bluefruit.begin() returns false and there is no BLE. The nRF54LM20 is not in this repo yet, but anyone taking it to 0.5.x will hit this. I did, while rebasing an nRF54LM20-DK onto v0.5.1.

One thing to look at: this also takes number_128_blocks from 1 to 4. I took nrfx's value because I can't judge the entropy of a single block, and that way the whole configuration is Nordic's. If the 1 was deliberate, I can drop that line; I have not timed that combination precisely.

Testing

Figures from temporary counters around rng_draw() (polling iterations, and µs from the GRTC), not part of this PR.

  • nRF54L15-DK, Meshtastic 2.8.3 on platform v0.5.0, with this PR and Bluefruit: don't send commands to the connection LED timer before it exists #13: 4 draws at boot (SoftDevice seed and LESC key generation), 0 failures. BLE advertises, telemetry goes out. It ran for 3 h 24 min with no reset. At 3 h 20 min: 5 draws, 0 failures, never more than 69 iterations for a first word. The fifth appeared once a phone connected, and is the size of a SoftDevice seed.
  • nRF54LM20-DK, a local line based on v0.5.1 with the same change: 5 draws at boot, 0 failures. BLE comes up, a phone reconnected to its existing bond by itself, and the serial API works. It ran for 3 h 24 min with no reset. Before this change: 3 draws out of 3 failed at boot, each after 19.6 ms.
  • nRF54L15-DK, same image: a phone with an existing LESC bond connected. The link reports security mode 1 level 4, and the app listed the nodes, ran a traceroute that came back and changed the TX power, which took effect without a reboot. After a reset the phone reconnected by itself at the same level.
  • nRF54L15-DK, same image, new pairing: the phone forgot the board and paired again with the passkey. The stored bond has a new LTK with its LESC flag set, and the link is at security mode 1 level 4. Pairing added two draws, 0 failures: one seed-sized, and one that by its timing fits the 32 bytes of the ECDH's random Z (not traced individually). That one took 91 µs rather than about 27: 50 iterations, but 84 µs for the first word, which I put down to preemption by the SoftDevice without having traced it. After another reset the phone reconnected by itself on the new bond, with no further pairing.

Not tested: entropy quality beyond the TRNG's own start-up tests, and the nRF54L05, nRF54L10 and XIAO boards.

Summary by CodeRabbit

  • Bug Fixes
    • Updated random number generator configuration to use a longer initialization wait and four 128-bit blocks.

cracen_rng_reset() soft-resets the TRNG and enables it, leaving the sampling period and the start-up wait
at their reset values, and uses one 128-bit block for conditioning. trng_init() in nrfx_cracen.c programs
TRNG_CLK_DIV and a wait of 512, and uses four blocks.

The reset values differ between parts. An nRF54LM20 resets to a sampling period of 0xFFF, where nrfx uses 1:
since each draw starts from a soft reset, every draw took 33 ms there, more than the 100000 iterations of
RNG_TIMEOUT last, so all of them failed. An nRF54L15 resets to a start-up wait of 0xFFFF, which is the 0.5 ms
a draw takes on it today.
@coderabbitai

coderabbitai Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The TRNG initialization wait value is set to 512, and the configured block count is increased to four. The reset function also sets the clock divider and, when supported, sets the idle timer to 0.

Changes: TRNG Reset Configuration

Layer / File(s) Summary
Set TRNG reset parameters
libraries/nRF54Crypto/src/nRF54Crypto.cpp
The reset function configures the idle timer when supported, sets the TRNG clock divider and initialization wait value, and enables four 128-bit blocks.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Possibly related PRs

Suggested reviewers: caveman99

Merge Risk: Minimal · up to 7b13e

No merge-blocking risk was identified in the TRNG reset configuration.

Pre-merge checks: Passed 5
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: configuring the nRF54 TRNG to match nrfx behavior.
Docstring Coverage Passed No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 0 functions across 1 files.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

A rabbit checks the timer’s place,
Four blocks begin their steady pace.
The clock divider joins the queue,
Five hundred twelve waits in view.
The TRNG starts with settings new.

Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 0d008a49-6229-4633-8acc-80c73c0c33b4
Commits

Reviewing files that changed from the base of the PR and between b7e729c and 7b13e45.

Files selected for processing: 1
  • libraries/nRF54Crypto/src/nRF54Crypto.cpp

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@cvaldess

cvaldess commented Nov 4, 2026

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 4, 2026 •

Copy link
Copy Markdown
Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

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.

1 participant