12.12. TRNG

12.12.1. Overview

RP2350 contains an Arm IP-based True Random Number Generator block. It supports the following features:

On request, the TRNG block generates a block of 192 entropy bits generated by automatically processing a series of periodic samples from the TRNG block’s internal Ring Oscillator (ROSC).

The TRNG block's ROSC is a free-running oscillator with no direct connection to the system clocks on RP2350. As a result, the ROSC generally runs asynchronously to the system clocks.

After a sufficient number of samples have been collected, the TRNG block completes the generation process and presents the random number in the EHR_DATA[x] result registers.

For more information, see ARM IP - True Random Number Generator

12.12.2. Configuration

The TRNG block contains three different built-in entropy checking mechanisms: At reset, these are all enabled by default and hence do not require explicit enabling.

You can configure the TRNG block in the following ways:

Because the system clock generally runs much faster than the ROSC, the sampling period is expected to be at least a few tens of system clock ticks.

Because the characteristics of the TRNG ROSC and system clock frequency will differ for each implementation of the TRNG IP block, Arm details a TRNG characterisation procedure to determine the most appropriate ROSC chain length and sampling frequency settings on each SoC design. For details about that characterisation procedure, see ARM TrustZone True Number Generator .

Software drivers for the RP2350 TRNG block do not utilise the standard approach (see Section 12.12.4 ). As a result, software does not configure the ROSC length and sample count settings provided by the Arm characterisation procedure.

When configuring the TRNG block, consider the following principles:

For acceptable results with an average generation time of about 2 milliseconds, use ROSC chain length settings of 0 or 1 and sample count settings of 20-25.

Larger sample count settings (e.g. 100) provide proportionately slower average generation times. These settings significantly reduce, but do not eliminate NIST test failures and entropy check failures. Results occasionally take an especially long time to generate.

12.12.3. Operation

To initiate TRNG generation, set the RND_SRC_EN bit in RND_SOURCE_ENABLE . The TRNG will run until:

In either case, you can read the resultant status from RNG_ISR .

To generate TRNG block interrupts, set bits in RNG_IMR . Use RNG_ICR to clear active interrupt status bits.

The EHR_DATA[x] registers read 0 until successful generation has occurred, so the CPU cannot read random number results during generation,

After successful generation, read the last result register, EHR_DATA[5] to clear all of the result registers. If the result fails an entropy check, no results are presented and the EHR_DATA[x] registers all read as 0.

After TRNG generation and when not in use, the RND_SRC_EN bit should be cleared.

12.12.4. Caveats

The generation of random numbers by the TRNG block is not a deterministic process.

Although the modal and mean average times required to generate random numbers are quite similar, the generation process can occasionally take much longer to complete: in excess of 100 times the average. Any run resulting in a failed entropy check discards the result, requiring another generation process.

You can accommodate these unpredictable generation times in your system design. For example, you might generate a small pool of random numbers, initiating subsequent generation whenever space becomes available in the pool.

In the interests of simplicity and timing predictability, alternative approaches were adopted for the RP2350 bootrom and the SDK TRNG block drivers. The methodologies used can be found via the links below. However, nothing in the TRNG block in RP2350 precludes using the block as specified in Arm documentation.

12.12.4.1. Bootrom

The bootrom streams raw TRNG ROSC samples (the TRNG random source) directly into the hardware SHA-256 accelerator. It bypasses all internal checking and conditioning in the TRNG. SHA-256 is a robust hash function which avoids the pitfalls of some of the conditioning logic in the TRNG, most notably the von Neumann decorrelator.

The bootrom has some hard constraints which guide its implementation choices, most notably: the bootrom must boot . It cannot afford to poll the TRNG for an indeterminate amount of time to wait for a random number to appear. Complex error handling is also undesirable.

A link to the bootrom source can be found in Chapter 5 . Consult the source code for the exact implementation of the per-boot random number generation, in varm_boot_path.c.

The A2 bootrom TRNG code is written in assembly due to various implementation constraints, and may not be that illuminating. The following is excerpted from the A1 bootrom source, lightly edited for readability:

// Boot RNG is derived by streaming a large number of TRNG ROSC samples
// into the SHA-256. BOOT_TRNG_SAMPLE_BLOCKS is the number of SHA-256
// blocks to hash, each containing 384 samples from the TRNG ROSC:
const int BOOT_TRNG_SAMPLE_BLOCKS = 25;

// Fixed delay is required after TRNG soft reset
trng_hw->trng_sw_reset = -1u;
(void)trng_hw->trng_sw_reset;
(void)trng_hw->trng_sw_reset;
// Initialise SHA internal state by writing START bit
sha256_hw->csr = SHA256_CSR_RESET | SHA256_CSR_START_BITS;

// Sample one ROSC bit into EHR every cycle, subject to CPU keeping up. More
// temporal resolution to measure ROSC phase noise is better, if we use a
// high quality hash function instead of naive VN decorrelation. (Also more
// metastability events, which are a secondary noise source)
trng_hw->sample_cnt1 = 0;
// Disable checks and bypass decorrelators, to stream raw TRNG ROSC samples:
trng_hw->trng_debug_control = -1u;
// Start ROSC if it is not already started
trng_hw->rnd_source_enable = -1u;
// Clear all interrupts (including EHR_VLD) -- we will check this
// later, after seeding RCP.
trng_hw->rng_icr = -1u;

// Each half-block (192 samples) takes approx 235 cycles, so 470 cycles/block:
for (int half_blocks = 0; half_blocks < 2 * BOOT_TRNG_SAMPLE_BLOCKS; ++half_blocks) {

    // Wait for 192 ROSC samples to fill EHR, this should take constant time:
    while (trng_hw->trng_busy)
;

// Copy 6 EHR words to SHA-256, plus garbage (RND_SOURCE_ENABLE and
// SAMPLE_CNT1) which pads us out to half of a SHA-256 block. This means
// we can avoid checking SHA-256 ready whilst reading EHR, so we restart
// sampling sooner. (SHA-256 becomes non-ready for 57 cycles after each
// 16 words written.)
io_ro_32 *src = &trng_hw->ehr_data[0];
io_wo_32 *dst = &sha256_hw->wdata;
for (int i = 0; i < 8; ++i) {
    *dst = src[i];
}

// TRNG is now sampling again, having started after we read the last EHR
// word. Grab some in-progress SHA bits and use them to modulate the
// chain length, to reduce chance of injection locking:
trng_hw->trng_config = sha256_hw->sum[0];
}

// Wait for SHA result -- if skipped we get the previous block's digest. Note
// this never becomes true if we wrote a number of words % 16 != 0.
while (!(sha256_hw->csr & SHA256_CSR_SUM_VLD_BITS))
    ;

// The per-boot random will change on every core 0 reset (except debugger
// skipping ROM). If this is a problem then the user can sample the
// per-boot random into a preserved variable in main SRAM.
for (int i = 0; i < 4; ++i) {
    bootram->always.boot_random.e[i] = sha256_hw->sum[4 + i];
}

trng_hw->trng_config = 0;
// Stop ROSC as it's a waste of power
trng_hw->rnd_source_enable = 0;

The bootrom resets the SHA-256 and TRNG via RESETS immediately before the above code runs. This code typically runs with clk_sys running from the system ROSC, at its initial boot frequency of approximately 12 MHz. The 256-bit result is available in the SUM0 through SUM7 registers after the code completes.

This code does not represent best programming practice: for example it writes ones into reserved bits in the TRNG_DEBUG_CONTROL register. It was written with close reference to the hardware implementation. The above code listing serves only to document the method the bootrom uses to generate random numbers at boot time, for the once-per-boot random number available via the get_sys_info() ROM API as well as for initialising the RCP salt registers (Section 3.6.3.1).

12.12.4.2. SDK

The pico_rand library uses the TRNG as one of its entropy sources. It streams raw ROSC samples from the TRNG ROSC in a similar manner to the bootrom. It uses the xoroshiro128** and splitmix64() PRNG functions to condition the output.

12.12.5. List of registers

The TRNG control registers start at a base address of 0x400f0000(defined as TRNG_BASE in the SDK).

Table 1261. List of TRNG registers

OffsetNameInfo
0x100RNG_IMRInterrupt masking.
OffsetNameInfo
0x104RNG_ISRRNG status register. If corresponding RNG_IMR bit is unmasked, an interrupt will be generated.
0x108RNG_ICRInterrupt/status bit clear Register.
0x10cTRNG_CONFIGSelecting the inverter-chain length.
0x110TRNG_VALID192 bit collection indication.
0x114EHR_DATA0RNG collected bits.
0x118EHR_DATA1RNG collected bits.
0x11cEHR_DATA2RNG collected bits.
0x120EHR_DATA3RNG collected bits.
0x124EHR_DATA4RNG collected bits.
0x128EHR_DATA5RNG collected bits.
0x12cRND_SOURCE_ENABLEEnable signal for the random source.
0x130SAMPLE_CNT1Counts clocks between sampling of random bit.
0x134AUTOCORR_STATISTICStatistics about autocorrelation test activations.
0x138TRNG_DEBUG_CONTROLDebug register.
0x140TRNG_SW_RESETGenerate internal SW reset within the RNG block.
0x1b4RNG_DEBUG_EN_INPUTEnable the RNG debug mode
0x1b8TRNG_BUSYRNG Busy indication.
0x1bcRST_BITS_COUNTERReset the counter of collected bits in the RNG.
0x1c0RNG_VERSIONDisplays the version settings of the TRNG.
0x1e0RNG_BIST_CNTR_0Collected BIST results.
0x1e4RNG_BIST_CNTR_1Collected BIST results.
0x1e8RNG_BIST_CNTR_2Collected BIST results.

TRNG: RNG_IMR Register

Offset: 0x100

Description

Interrupt masking.

Table 1262. RNG_IMR Register

BitsDescriptionTypeReset
31:4Reserved.--
3VN_ERR_INT_MASK: Set to 1 to mask (disable) this interrupt: no interrupt will be generated. See RNG_ISR for an explanation on this interrupt.RW0x1
2CRNGT_ERR_INT_MASK: Set to 1 to mask (disable) this interrupt: no interrupt will be generated. See RNG_ISR for an explanation on this interrupt.RW0x1
1AUTOCORR_ERR_INT_MASK: Set to 1 to mask (disable) this interrupt: no interrupt will be generated. See RNG_ISR for an explanation on this interrupt.RW0x1
0EHR_VALID_INT_MASK: Set to 1 to mask (disable) this interrupt: no interrupt will be generated. See RNG_ISR for an explanation on this interrupt.RW0x1

TRNG: RNG_ISR Register

Offset: 0x104

Description

RNG status register. If corresponding RNG_IMR bit is unmasked, an interrupt will be generated.

Table 1263. RNG_ISR Register

Bits 14:10 9:5 4:0 BitsDescription SHIFT DescriptionMASK_MSB : The most-significant bit allowed to pass by the mask (inclusive) MASK_LSB : The least-significant bit allowed to pass by the mask (inclusive) : Right-rotate applied to accumulator before masking. By appropriatelyType RW RW RW TypeReset 0x00 0x00 0x00 Reset
31:4Reserved.--
3VN_ERR: 1 indicates von Neumann error. Error in von Neumann occurs if 32RO0x0
2CRNGT_ERRconsecutive collected bits are identical, ZERO or ONE. : 1 indicates CRNGT in the RNG test failed. Failure occurs whenRO0x0
1AUTOCORR_ERRtwo consecutive blocks of 16 collected bits are equal. : 1 indicates Autocorrelation test failed four times in a row.RO0x0
0EHR_VALIDWhen set, RNG ceases functioning until next reset. : 1 indicates that 192 bits have been collected in the RNG, and are ready to be read.RO0x0
BitsDescriptionTypeReset
31:4Reserved.--
3VN_ERR: Write 1 to clear corresponding bit in RNG_ISR.RW0x0
2CRNGT_ERR: Write 1 to clear corresponding bit in RNG_ISR.RW0x0
1AUTOCORR_ERR: Cannot be cleared by SW! Only RNG reset clears this bit.RW0x0
0EHR_VALID: Write 1 - clear corresponding bit in RNG_ISR.RW0x0
BitsDescriptionTypeReset
Register 31:2Reserved.--
1:0RND_SRC_SEL: Selects the number of inverters (out of four possible selections) in the ring oscillator (the entropy source). Higher values selectRW0x0

TRNG: RNG_ICR Register

Offset: 0x108

Description

Interrupt/status bit clear Register.

Table 1264. RNG_ICR Register

TRNG: TRNG_CONFIG Register

Offset: 0x10c

Description

Selecting the inverter-chain length.

Table 1265. TRNG_CONFIG Register

TRNG: TRNG_VALID Register

Offset: 0x110

Description

192 bit collection indication.

Table 1266.
TRNG_VALID Register

BitsDescriptionTypeReset
31:1Reserved.--
0EHR_VALID : 1 indicates that collection of bits in the RNG is completed, and data can be read from EHR_DATA register.RO0x0

TRNG: EHR_DATA0, EHR_DATA1, ..., EHR_DATA4, EHR_DATA5 Registers

Offsets: 0x114, 0x118, ..., 0x124, 0x128

Description

RNG collected bits.

Table 1267.
EHR_DATA0,
EHR_DATA1, ...,
EHR_DATA4,
EHR_DATA5 Registers

BitsDescriptionTypeReset
31:0Bits \( [(32*(i+1))-1:(32*i)] \) of Entropy Holding Register.RO0x00000000

TRNG: RND_SOURCE_ENABLE Register

Offset: 0x12c

Description

Enable signal for the random source.

Table 1268.
RND_SOURCE_ENABLE
Register

BitsDescriptionTypeReset
31:1Reserved.--
0RND_SRC_EN : * 1 - entropy source is enabled.

* 0 - entropy source is disabled
RW0x0

TRNG: SAMPLE_CNT1 Register

Offset: 0x130

Description

Counts clocks between sampling of random bit.

Table 1269.
SAMPLE_CNT1
Register

BitsDescriptionTypeReset
31:0SAMPLE_CNTR1 : Sets the number of rng_clk cycles between two consecutive ring oscillator samples.

Note: If the von Neumann decorrelator is bypassed, the minimum value for sample counter must not be less than seventeen
RW0x0000ffff

TRNG: AUTOCORR_STATISTIC Register

Offset: 0x134

Description

Statistics about autocorrelation test activations.

Table 1270.
AUTOCORR_STATISTI
C Register

BitsDescriptionTypeReset
31:22Reserved.--
21:14AUTOCORR_FAILS : Count each time an autocorrelation test fails. Any write to the register reset the counter. Stop collecting statistic if one of the counters reached the limit.RW0x00
Bits 2 1 0 BitsDescription RESET PUSH DATA Description: Reset (before sending a new key)Type RW RW RW TypeReset 0x0 0x0 0x0 Reset
OL Register 31:4Reserved.--
3AUTO_CORRELATE_BYPASS: When set, the autocorrelation test in the TRNGRW0x0
2TRNG_CRNGT_BYPASSmodule is bypassed. : When set, the CRNGT test in the RNG is bypassed.RW0x0
1VNC_BYPASS: When set, the Von-Neuman balancer is bypassed (including the 32 consecutive bits test).RW0x0
0N/A Reserved.--
BitsDescriptionTypeReset
Register 31:1Reserved.--
0TRNG_SW_RESET: Writing 1 to this register causes an internal RNG reset.RW0x0
BitsDescriptionTypeReset
T Register 31:1Reserved.--
0RNG_DEBUG_EN: * 1 - debug mode is enabled. * 0 - debug mode is disabledRW0x0

TRNG: TRNG_DEBUG_CONTROL Register

Offset: 0x138

Description

Debug register.

Table 1271.
TRNG_DEBUG_CONTR
OL Register

TRNG: TRNG_SW_RESET Register

Offset: 0x140

Description

Generate internal SW reset within the RNG block.

Table 1272.
TRNG_SW_RESET
Register

TRNG: RNG_DEBUG_EN_INPUT Register

Offset: 0x1b4

Description

Enable the RNG debug mode

Table 1273.
RNG_DEBUG_EN_INPU
T Register

TRNG: TRNG_BUSY Register

Offset: 0x1b8

Description

RNG Busy indication.

Table 1274.
TRNG_BUSY Register

Bits 2 1 0 BitsDescription RESET PUSH DATA Description: Reset (before sending a new key)Type RW RW RW TypeReset 0x0 0x0 0x0 Reset
31:1Reserved.--
0TRNG_BUSY : 0x1bc: Reflects rng_busy status. RST_BITS_COUNTER RegisterRO0x0
BitsDescriptionTypeReset
Register 31:1Reserved.--
0RST_BITS_COUNTER: Writing any value to this address will reset the bits in order for the reset to take place. RNG_VERSION RegisterRW0x0
BitsDescriptionTypeReset
Register 31:8Reserved.--
7RNG_USE_5_SBOXES: * 1 - 5 SBOX AES. * 0 - 20 SBOX AESRO0x0
6RESEEDING_EXISTS: * 1 - Exists. * 0 - Does not existRO0x0
5KAT_EXISTS: * 1 - Exists. * 0 - Does not existRO0x0
4PRNG_EXISTS: * 1 - Exists. * 0 - Does not existRO0x0
3TRNG_TESTS_BYPASS_EN: * 1 - Exists. * 0 - Does not existRO0x0
2AUTOCORR_EXISTS: * 1 - Exists. * 0 - Does not existRO0x0
1CRNGT_EXISTS: * 1 - Exists. * 0 - Does not existRO0x0
0EHR_WIDTH_192 * 0 - 128-bit EHR: * 1 - 192-bit EHR.RO0x0

TRNG: RST_BITS_COUNTER Register

Offset: 0x1bc

Description

Reset the counter of collected bits in the RNG.

Table 1275.
RST_BITS_COUNTER
Register

TRNG: RNG_VERSION Register

Offset: 0x1c0

Description

Displays the version settings of the TRNG.

Table 1276.
RNG_VERSION
Register

TRNG: RNG_BIST_CNTR_0 Register

Offset: 0x1e0

Description

Collected BIST results.

Table 1277.
RNG_BIST_CNTR_0
Register

BitsDescriptionTypeReset
31:22Reserved.--
21:0ROSC_CNTR_VAL: Reflects the results of RNG BIST counter.RO0x000000

TRNG: RNG_BIST_CNTR_1 Register

Offset: 0x1e4

Description

Collected BIST results.

Table 1278.
RNG_BIST_CNTR_1
Register

BitsDescriptionTypeReset
31:22Reserved.--
21:0ROSC_CNTR_VAL: Reflects the results of RNG BIST counter.RO0x000000

TRNG: RNG_BIST_CNTR_2 Register

Offset: 0x1e8

Description

Collected BIST results.

Table 1279.
RNG_BIST_CNTR_2
Register

BitsDescriptionTypeReset
31:22Reserved.--
21:0ROSC_CNTR_VAL: Reflects the results of RNG BIST counter.RO0x000000