24. Random number generator (RNG)

This section applies to the whole STM32F4xx family, unless otherwise specified.

24.1 RNG introduction

The RNG processor is a random number generator, based on a continuous analog noise, that provides a random 32-bit value to the host when read.

The RNG passed the FIPS PUB 140-2 (2001 October 10) tests with a success ratio of 99%.

24.2 RNG main features

24.3 RNG functional description

Figure 232 shows the RNG block diagram.

Figure 232. Block diagram

Block diagram of the Random Number Generator (RNG) showing the internal components and their connections.

The block diagram illustrates the internal structure of the Random Number Generator (RNG). At the top, a horizontal double-headed arrow represents the '32-bit AHB bus'. Below this bus, on the left, is the 'Control register' (RNG_CR), connected to the bus via a vertical double-headed arrow. On the right, the 'data register' (RNG_DR) is also connected to the bus via a vertical double-headed arrow. Below the RNG_DR is the 'LFSR' (Linear Feedback Shift Register) block. An arrow points from the LFSR up to the RNG_DR. To the left of the LFSR, the 'RNG_CLK' clock signal is shown. This signal branches: one path goes directly to the LFSR, and another path goes down to a 'Clock checker & fault detector' block. The output of the 'Clock checker & fault detector' block points left to the 'Status register' (RNG_SR). Below the 'Clock checker & fault detector' is the 'Analog seed' block. An arrow points from the 'Analog seed' up to the 'Clock checker & fault detector', and another arrow points from the 'Clock checker & fault detector' up to the 'LFSR'. The text 'feed a Linear Feedback Shift Register' is placed between the 'Clock checker & fault detector' and the 'LFSR'. The identifier 'ai16080' is located in the bottom right corner of the diagram area.

Block diagram of the Random Number Generator (RNG) showing the internal components and their connections.
  1. 1. For more details about RNG clock (RNG_CLK) source, refer to Section 6: Reset and clock control for STM32F42xxx and STM32F43xxx (RCC) and Section 7: Reset and clock control for STM32F405xx/07xx and STM32F415xx/17xx(RCC).

The random number generator implements an analog circuit. This circuit generates seeds that feed a linear feedback shift register (RNG_LFSR) in order to produce 32-bit random numbers.

The analog circuit is made of several ring oscillators whose outputs are XORed to generate the seeds. The RNG_LFSR is clocked by a dedicated clock (RNG_CLK) at a constant frequency, so that the quality of the random number is independent of the HCLK frequency. The contents of the RNG_LFSR are transferred into the data register (RNG_DR) when a significant number of seeds have been introduced into the RNG_LFSR.

In parallel, the analog seed and the dedicated RNG_CLK clock are monitored. Status bits (in the RNG_SR register) indicate when an abnormal sequence occurs on the seed or when the frequency of the RNG_CLK clock is too low. An interrupt can be generated when an error is detected.

24.3.1 Operation

To run the RNG, follow the steps below:

  1. 1. Enable the interrupt if needed (to do so, set the IE bit in the RNG_CR register). An interrupt is generated when a random number is ready or when an error occurs.
  2. 2. Enable the random number generation by setting the RNGEN bit in the RNG_CR register. This activates the analog part, the RNG_LFSR and the error detector.
  3. 3. At each interrupt, check that no error occurred (the SEIS and CEIS bits should be '0' in the RNG_SR register) and that a random number is ready (the DRDY bit is '1' in the RNG_SR register). The contents of the RNG_DR register can then be read.

As required by the FIPS PUB (Federal Information Processing Standard Publication) 140-2, the first random number generated after setting the RNGEN bit should not be used, but saved for comparison with the next generated random number. Each subsequent generated random number has to be compared with the previously generated number. The test fails if any two compared numbers are equal (continuous random number generator test).

24.3.2 Error management

If the CEIS bit is read as '1' (clock error)

In the case of a clock, the RNG is no more able to generate random numbers because the RNG_CLK clock is not correct. Check that the clock controller is correctly configured to provide the RNG clock and clear the CEIS bit. The RNG can work when the CEIS bit is '0'. The clock error has no impact on the previously generated random numbers, and the RNG_DR register contents can be used.

If the SEIS bit is read as '1' (seed error)

In the case of a seed error, the generation of random numbers is interrupted for as long as the SEIS bit is '1'. If a number is available in the RNG_DR register, it must not be used because it may not have enough entropy.

What you should do is clear the SEIS bit, then clear and set the RNGEN bit to reinitialize and restart the RNG.

24.4 RNG registers

The RNG is associated with a control register, a data register and a status register. They have to be accessed by words (32 bits).

24.4.1 RNG control register (RNG_CR)

Address offset: 0x00

Reset value: 0x0000 0000

31302928272625242322212019181716
Reserved
1514131211109876543210
ReservedIERNGENReserved
rwrw

Bits 31:4 Reserved, must be kept at reset value

Bit 3 IE: Interrupt enable

0: RNG Interrupt is disabled

1: RNG Interrupt is enabled. An interrupt is pending as soon as DRDY=1 or SEIS=1 or CEIS=1 in the RNG_SR register.

Bit 2 RNGEN: Random number generator enable

0: Random number generator is disabled

1: random Number Generator is enabled.

Bits 1:0 Reserved, must be kept at reset value

24.4.2 RNG status register (RNG_SR)

Address offset: 0x04

Reset value: 0x0000 0000

31302928272625242322212019181716
Reserved
1514131211109876543210
ReservedSEISCEISReservedSECSCECSDRDY
rc_w0rc_w0rrr

Bits 31:3 Reserved, must be kept at reset value

Bit 6 SEIS: Seed error interrupt status

This bit is set at the same time as SECS, it is cleared by writing it to 0.

0: No faulty sequence detected

1: One of the following faulty sequences has been detected:

– More than 64 consecutive bits at the same value (0 or 1)

– More than 32 consecutive alternances of 0 and 1 (0101010101...01)

An interrupt is pending if IE = 1 in the RNG_CR register.

Bit 5 CEIS: Clock error interrupt status

This bit is set at the same time as CECS, it is cleared by writing it to 0.

0: The RNG_CLK clock was correctly detected

1: The RNG_CLK was not correctly detected (\(f_{RNG\_CLK} < f_{HCLK}/16\))

An interrupt is pending if IE = 1 in the RNG_CR register.

Bits 4:3 Reserved, must be kept at reset value

Bit 2 SECS: Seed error current status

0: No faulty sequence has currently been detected. If the SEIS bit is set, this means that a faulty sequence was detected and the situation has been recovered.

1: One of the following faulty sequences has been detected:

Bit 1 CECS: Clock error current status

0: The RNG_CLK clock has been correctly detected. If the CEIS bit is set, this means that a clock error was detected and the situation has been recovered

1: The RNG_CLK was not correctly detected (\(f_{\text{RNG\_CLK}} < f_{\text{HCLK}}/16\)).

Bit 0 DRDY: Data ready

0: The RNG_DR register is not yet valid, no random data is available

1: The RNG_DR register contains valid random data

Note: An interrupt is pending if IE = 1 in the RNG_CR register.

Once the RNG_DR register has been read, this bit returns to 0 until a new valid value is computed.

24.4.3 RNG data register (RNG_DR)

Address offset: 0x08

Reset value: 0x0000 0000

The RNG_DR register is a read-only register that delivers a 32-bit random value when read. After being read, this register delivers a new random value after a maximum time of 40 periods of the RNG_CLK clock. The software must check that the DRDY bit is set before reading the RNDATA value.

31302928272625242322212019181716
RNDATA
rrrrrrrrrrrrrrrr
1514131211109876543210
RNDATA
rrrrrrrrrrrrrrrr

Bits 31:0 RNDATA: Random data
32-bit random data.

24.4.4 RNG register map

Table 116 gives the RNG register map and reset values.

Table 116. RNG register map and reset map

OffsetRegister name
reset value
Register size
313029282726252423222120191817161514131211109876543210
0x00RNG_CR
0x0000000
ReservedIERNGENReserved
0x04RNG_SR
0x0000000
ReservedSEISCEISReservedSECSCECSDRDY
0x08RNG_DR
0x0000000
RNDATA[31:0]