12.12. TRNG
12.12.1. Overview
RP2350 contains an Arm IP-based True Random Number Generator block. It supports the following features:
- • Compliance with FIPS Publication 140-2, BSI AIS-31, and NIST SP 800-90B
- • Produces approximately 7.5 kb/s of entropy when the core runs at 150 MHz
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:
- • Configure the frequency of the ROSC by selecting of one of four ROSC chain lengths , see TRNG_CONFIG .
- • Configure the ROSC sampling period in terms of system clock ticks, see SAMPLE_CNT1 .
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:
- • As average generation time increases, result quality increases and failed entropy checks decrease.
- • A low sample count decreases average generation time, but increases the chance of NIST test-failing results and failed entropy checks.
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:
- • It has successfully completed the generation of a random number.
- • One, or more, of the internal entropy checking mechanisms indicates a failed run.
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
| Offset | Name | Info |
|---|---|---|
| 0x100 | RNG_IMR | Interrupt masking. |
| Offset | Name | Info |
|---|---|---|
| 0x104 | RNG_ISR | RNG status register. If corresponding RNG_IMR bit is unmasked, an interrupt will be generated. |
| 0x108 | RNG_ICR | Interrupt/status bit clear Register. |
| 0x10c | TRNG_CONFIG | Selecting the inverter-chain length. |
| 0x110 | TRNG_VALID | 192 bit collection indication. |
| 0x114 | EHR_DATA0 | RNG collected bits. |
| 0x118 | EHR_DATA1 | RNG collected bits. |
| 0x11c | EHR_DATA2 | RNG collected bits. |
| 0x120 | EHR_DATA3 | RNG collected bits. |
| 0x124 | EHR_DATA4 | RNG collected bits. |
| 0x128 | EHR_DATA5 | RNG collected bits. |
| 0x12c | RND_SOURCE_ENABLE | Enable signal for the random source. |
| 0x130 | SAMPLE_CNT1 | Counts clocks between sampling of random bit. |
| 0x134 | AUTOCORR_STATISTIC | Statistics about autocorrelation test activations. |
| 0x138 | TRNG_DEBUG_CONTROL | Debug register. |
| 0x140 | TRNG_SW_RESET | Generate internal SW reset within the RNG block. |
| 0x1b4 | RNG_DEBUG_EN_INPUT | Enable the RNG debug mode |
| 0x1b8 | TRNG_BUSY | RNG Busy indication. |
| 0x1bc | RST_BITS_COUNTER | Reset the counter of collected bits in the RNG. |
| 0x1c0 | RNG_VERSION | Displays the version settings of the TRNG. |
| 0x1e0 | RNG_BIST_CNTR_0 | Collected BIST results. |
| 0x1e4 | RNG_BIST_CNTR_1 | Collected BIST results. |
| 0x1e8 | RNG_BIST_CNTR_2 | Collected BIST results. |
TRNG: RNG_IMR Register
Offset: 0x100
Description
Interrupt masking.
Table 1262. RNG_IMR Register
| Bits | Description | Type | Reset |
|---|---|---|---|
| 31:4 | Reserved. | - | - |
| 3 | VN_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. | RW | 0x1 |
| 2 | CRNGT_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. | RW | 0x1 |
| 1 | AUTOCORR_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. | RW | 0x1 |
| 0 | EHR_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. | RW | 0x1 |
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 Bits | Description SHIFT Description | MASK_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 appropriately | Type RW RW RW Type | Reset 0x00 0x00 0x00 Reset |
|---|---|---|---|---|
| 31:4 | Reserved. | - | - | |
| 3 | VN_ERR | : 1 indicates von Neumann error. Error in von Neumann occurs if 32 | RO | 0x0 |
| 2 | CRNGT_ERR | consecutive collected bits are identical, ZERO or ONE. : 1 indicates CRNGT in the RNG test failed. Failure occurs when | RO | 0x0 |
| 1 | AUTOCORR_ERR | two consecutive blocks of 16 collected bits are equal. : 1 indicates Autocorrelation test failed four times in a row. | RO | 0x0 |
| 0 | EHR_VALID | When set, RNG ceases functioning until next reset. : 1 indicates that 192 bits have been collected in the RNG, and are ready to be read. | RO | 0x0 |
| Bits | Description | Type | Reset | |
| 31:4 | Reserved. | - | - | |
| 3 | VN_ERR | : Write 1 to clear corresponding bit in RNG_ISR. | RW | 0x0 |
| 2 | CRNGT_ERR | : Write 1 to clear corresponding bit in RNG_ISR. | RW | 0x0 |
| 1 | AUTOCORR_ERR | : Cannot be cleared by SW! Only RNG reset clears this bit. | RW | 0x0 |
| 0 | EHR_VALID | : Write 1 - clear corresponding bit in RNG_ISR. | RW | 0x0 |
| Bits | Description | Type | Reset | |
| Register 31:2 | Reserved. | - | - | |
| 1:0 | RND_SRC_SEL | : Selects the number of inverters (out of four possible selections) in the ring oscillator (the entropy source). Higher values select | RW | 0x0 |
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
| Bits | Description | Type | Reset |
|---|---|---|---|
| 31:1 | Reserved. | - | - |
| 0 | EHR_VALID : 1 indicates that collection of bits in the RNG is completed, and data can be read from EHR_DATA register. | RO | 0x0 |
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
| Bits | Description | Type | Reset |
|---|---|---|---|
| 31:0 | Bits \( [(32*(i+1))-1:(32*i)] \) of Entropy Holding Register. | RO | 0x00000000 |
TRNG: RND_SOURCE_ENABLE Register
Offset: 0x12c
Description
Enable signal for the random source.
Table 1268.
RND_SOURCE_ENABLE
Register
| Bits | Description | Type | Reset |
|---|---|---|---|
| 31:1 | Reserved. | - | - |
| 0 | RND_SRC_EN
: * 1 - entropy source is enabled. * 0 - entropy source is disabled | RW | 0x0 |
TRNG: SAMPLE_CNT1 Register
Offset: 0x130
Description
Counts clocks between sampling of random bit.
Table 1269.
SAMPLE_CNT1
Register
| Bits | Description | Type | Reset |
|---|---|---|---|
| 31:0 | SAMPLE_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 | RW | 0x0000ffff |
TRNG: AUTOCORR_STATISTIC Register
Offset: 0x134
Description
Statistics about autocorrelation test activations.
Table 1270.
AUTOCORR_STATISTI
C Register
| Bits | Description | Type | Reset |
|---|---|---|---|
| 31:22 | Reserved. | - | - |
| 21:14 | AUTOCORR_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. | RW | 0x00 |
| Bits 2 1 0 Bits | Description RESET PUSH DATA Description | : Reset (before sending a new key) | Type RW RW RW Type | Reset 0x0 0x0 0x0 Reset |
|---|---|---|---|---|
| OL Register 31:4 | Reserved. | - | - | |
| 3 | AUTO_CORRELATE_BYPASS | : When set, the autocorrelation test in the TRNG | RW | 0x0 |
| 2 | TRNG_CRNGT_BYPASS | module is bypassed. : When set, the CRNGT test in the RNG is bypassed. | RW | 0x0 |
| 1 | VNC_BYPASS | : When set, the Von-Neuman balancer is bypassed (including the 32 consecutive bits test). | RW | 0x0 |
| 0 | N/A Reserved. | - | - | |
| Bits | Description | Type | Reset | |
| Register 31:1 | Reserved. | - | - | |
| 0 | TRNG_SW_RESET | : Writing 1 to this register causes an internal RNG reset. | RW | 0x0 |
| Bits | Description | Type | Reset | |
| T Register 31:1 | Reserved. | - | - | |
| 0 | RNG_DEBUG_EN | : * 1 - debug mode is enabled. * 0 - debug mode is disabled | RW | 0x0 |
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 Bits | Description RESET PUSH DATA Description | : Reset (before sending a new key) | Type RW RW RW Type | Reset 0x0 0x0 0x0 Reset |
|---|---|---|---|---|
| 31:1 | Reserved. | - | - | |
| 0 | TRNG_BUSY : 0x1bc | : Reflects rng_busy status. RST_BITS_COUNTER Register | RO | 0x0 |
| Bits | Description | Type | Reset | |
| Register 31:1 | Reserved. | - | - | |
| 0 | RST_BITS_COUNTER | : Writing any value to this address will reset the bits in order for the reset to take place. RNG_VERSION Register | RW | 0x0 |
| Bits | Description | Type | Reset | |
| Register 31:8 | Reserved. | - | - | |
| 7 | RNG_USE_5_SBOXES | : * 1 - 5 SBOX AES. * 0 - 20 SBOX AES | RO | 0x0 |
| 6 | RESEEDING_EXISTS | : * 1 - Exists. * 0 - Does not exist | RO | 0x0 |
| 5 | KAT_EXISTS | : * 1 - Exists. * 0 - Does not exist | RO | 0x0 |
| 4 | PRNG_EXISTS | : * 1 - Exists. * 0 - Does not exist | RO | 0x0 |
| 3 | TRNG_TESTS_BYPASS_EN | : * 1 - Exists. * 0 - Does not exist | RO | 0x0 |
| 2 | AUTOCORR_EXISTS | : * 1 - Exists. * 0 - Does not exist | RO | 0x0 |
| 1 | CRNGT_EXISTS | : * 1 - Exists. * 0 - Does not exist | RO | 0x0 |
| 0 | EHR_WIDTH_192 * 0 - 128-bit EHR | : * 1 - 192-bit EHR. | RO | 0x0 |
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
| Bits | Description | Type | Reset |
|---|---|---|---|
| 31:22 | Reserved. | - | - |
| 21:0 | ROSC_CNTR_VAL: Reflects the results of RNG BIST counter. | RO | 0x000000 |
TRNG: RNG_BIST_CNTR_1 Register
Offset: 0x1e4
Description
Collected BIST results.
Table 1278.
RNG_BIST_CNTR_1
Register
| Bits | Description | Type | Reset |
|---|---|---|---|
| 31:22 | Reserved. | - | - |
| 21:0 | ROSC_CNTR_VAL: Reflects the results of RNG BIST counter. | RO | 0x000000 |
TRNG: RNG_BIST_CNTR_2 Register
Offset: 0x1e8
Description
Collected BIST results.
Table 1279.
RNG_BIST_CNTR_2
Register
| Bits | Description | Type | Reset |
|---|---|---|---|
| 31:22 | Reserved. | - | - |
| 21:0 | ROSC_CNTR_VAL: Reflects the results of RNG BIST counter. | RO | 0x000000 |