AN4230 · Application noteIntroduction to random number generation validation using the NIST statistical test suite for STM32 MCUs and MPUs
Download .pdfOpen in the full document

RNG overview

RNG overview

Random number generators (RNGs) used for cryptographic applications typically produce sequences made of random 0’s and 1’s bits.

There are two basic classes of random number generators:
  • Deterministic RNG or pseudo RNG (PRNG)

    A deterministic RNG consists of an algorithm that produces a sequence of bits from an initial value called a seed. To ensure forward unpredictability, care must be taken in obtaining seeds. The values produced by a PRNG are completely predictable if the seed and generation algorithm are known. Since in many cases the generation algorithm is publicly available, the seed must be kept secret and generated from a TRNG.

  • Non-deterministic RNG or true RNG (TRNG)

    A non-deterministic RNG produces randomness that depends on some unpredictable physical source (the entropy source) outside of any human control.

The RNG hardware peripheral implemented in some STM32 MCUs is a true random number generator.

STM32 MCU implementation description

The table below lists the STM32 Arm® Cortex® core-based MCUs that embed the RNG peripheral.

Note: Arm and Cortex are registered trademarks of Arm Limited (or its subsidiaries or affiliates) in the US and/or elsewhere.

The Arm word and logo are trademarks of Arm Limited (or its subsidiaries) in the US and/or elsewhere. All rights reserved.

Table 2. STM32 lines embedding the RNG hardware peripheral

SeriesSTM32 lines
STM32F2 seriesAll lines
STM32F4 seriesSTM32F405/415, STM32F407/417, STM32F410, STM32F412, STM32F413/423, STM32F427/437, STM32F429/439, STM32F469/479
STM32F7 seriesAll lines
STM32L0 seriesSTM32L05x, STM32L06x, STM32L072/073
STM32L4 seriesAll lines
STM32L4+ seriesAll lines
STM32H5 seriesAll lines
STM32H7 seriesAll lines(1)
STM32L5 seriesAll lines
STM32U0 seriesAll lines
STM32U5 seriesAll lines
STM32G0 seriesSTM32G081xB, STM32G041x6, STM32G041x8, STM32G061x6, STM32G061x8, STM32G0C1xC, STM32G0C1xE
STM32G4 seriesAll lines
STM32MP1 seriesAll lines
STM32WBA seriesAll lines(2)
STM32WB seriesAll lines
STM32WL seriesAll lines
STM32MP2 seriesAll lines
STM32N6 seriesAll lines
STM32U3 seriesAll lines
STM32C5 seriesAll lines

The true RNG implemented in the STM32 MCUs is based on an analog circuit. This circuit generates a continuous analog noise that is used in the RNG processing to produce a 32-bit random number. The analog circuit is made of several ring oscillators whose outputs are XORed.

The RNG processing is clocked by a dedicated clock at a constant frequency and, for a subset of microcontrollers, the RNG-dedicated clock can be reduced using the divider inside the RNG peripheral.

For more details about the RNG peripherals, refer to the STM32 reference manuals.

The figures below show a simplified view of a true RNG in STM32 microcontrollers.
Figure 1. STM32 true RNG block diagram of the products verified with the SP800-22rev1a


Figure 2. STM32 true RNG block diagram of the products verified with the SP800-90b


1 Except the following part numbers, which don't embed RNG peripherals: STM32H745BG, STM32H745XG, STM32H745ZG, STM32H747AG, STM32H747AI, STM32H747BG, STM32H747IG, STM32H747II, STM32H747XG, STM32H757AI, STM32H757II.
2 Except STM32WBA55HG and STM32WBA55UG.

NIST SP800 22rev1a overview

The NIST SP800-22rev1a statistical test suite is used to probe the quality of RNGs for cryptographic applications. A comprehensive description of the suite is presented in the NIST document entitled A Statistical Test Suite for the Validation of Random Number Generators and Pseudo Random Number Generators for Cryptographic Applications.

NIST SP800-22rev1a test suite description

The NIST SP800-22rev1a statistical test suite “sts-2.1.1” is a software package developed by NSIT that can be downloaded from the NIST web site (search for download the NIST Statistical Test Suite at csrc.nist.gov).

The source code has been written in ANSI C. The NIST statistical test suite consists of 15 tests that verify the randomness of a binary sequence. These tests focus on various types of non-randomness that can exist in a sequence.

These test can be classified as follows:

  • Frequency tests
    • Frequency (Monobit) test

      To measure the distribution of 0’s and 1’s in a sequence and to check if the result is similar to the one expected for a truly random sequence.

    • Frequency test within a block

      To check whether the frequency of 1’s in an Mbit block is approximately M/2, as expected from the theory of randomness.

    • Run tests

      To assess if the expected total number of runs of 1’s and 0’s of various lengths is as expected for a random sequence.

    • Test of the longest run of 1’s in a block

      To examine the long runs of 1’s in a sequence. Test of linearity

  • Test of linearity
    • Binary matrix rank test

      To assess the distribution of the rank for 32 x 32 binary matrices.

    • Linear complexity test

      To determine the linear complexity of a finite sequence.

  • Test of correlation (by means of Fourier transform)
    • Discrete Fourier transform (spectral) test

      To assess the spectral frequency of a bit string via the spectral test based on the discrete Fourier transform. It is sensitive to the periodicity in the sequence.

  • Test of finding some special strings
    • Non-overlapping template matching test

      To assess the frequency of Mbit non-periodic patterns.

    • Overlapping template matching test

      To assess the frequency of Mbit periodic templates.

  • Entropy tests
    • Maurer’s “Universal Statistical” test

      To assess the compressibility of a binary sequence of L-bit blocks.

    • Serial test

      To assess the distribution of all 2m Mbit blocks.

Note: For m = 1, the serial test is equivalent to the frequency test.
  • Approximate entropy test
  • To assess the entropy of a bit string, comparing the frequency of all Mbit patterns against all (m+1)-bit patterns.
  • Random walk tests
    • Cumulative sums (Cusums) test

      To assess that the sum of partial sequences is not too large or too small; it is indicative of too many 0’s or 1’s.

    • Random excursion test

      To assess the distribution of states within a cycle of random walk.

    • To assess that the sum of partial sequences is not too large or too small; it is indicative of too many 0’s or 1’s.

    • Random excursion variant test

      To detect deviations from the expected number of visits to different states of the random walk.

Each of the above tests is based on a calculated test statistic value that is a function of the testing sequence.

The test statistic is used to calculate a Pvalue, which is the probability that a perfect random number generator generated a sequence less random than the sequence that was tested.

For more details about the NIST statistical test suite, refer to the following NIST document available on the NIST web site: A Statistical Test Suite for Random and Pseudorandom Number Generators for Cryptographic Applications” Special Publication 800-22 Revision 1a.

STM32MCU side

The firmware package is provided upon request. For more details, contact the local ST sales representative.

This program allows random numbers generation, using the STM32 RNG peripheral. It also retrieves these numbers on a workstation for testing with the NIST statistical test suite.

Each firmware program is used to generate 10 64-Kbyte blocks of random numbers. The output file contains 5,120,000 random bits to be tested with the NIST statistical test.

As recommended by the NIST statistical test suite, the output file format can be one of the followings:

  • a sequence of ASCII 0's and 1's if the FILE_ASCII_FORMAT Private define is uncommented in the main.c file
  • A binary file of random bytes if the FILE_BINARY_FORMAT Private define is uncommented in the main.c file.

Form more details about the program description and settings, refer to the readme file inside the firmware package.

Note:
The USART configuration can be changed via the SendToWorkstation() function in the main.c file.

The output values can be changed by modifying the Private define in the main.c file as follows:

#define NUMBER_OF_RANDOM_BITS_TO_GENERATE 512000

#define BLOCK_NUMBER 10

On the NIST SP800-22rev1a test suite side

Downloaded on a workstation, the NIST statistical test suite package sts-2.1.1 verifies the randomness of the output file of the STM32 RNG peripheral.

The generator file to be analyzed must be stored under the data folder (sts-2.1.1\data).

For more details about how the NIST statistical tests work, refer to section ‘How to get started in the NIST document A Statistical Test Suite for Random and Pseudorandom Number Generators for Cryptographic Applications.