HAL RNG How to Use

group RNG_How_To_Use

The RNG main features:

The true random number generator (RNG) is a NIST SP 800-90B compliant entropy source that delivers 32-bit random numbers. It has an AMBA AHB slave peripheral, accessible through 32-bit word single accesses only. It can be disabled to reduce power consumption, or enabled with an automatic low power mode (default configuration). The RNG has been pre-certified to NIST SP800-90B, and it has also been tested with the German BSI statistical tests of AIS-31 (T0 to T8).

How to use the HAL RNG driver

The HAL RNG driver can be used as follows:

  • Initialize the RNG handle by calling the HAL_RNG_Init() API. This performs these operations:

    • Associate the instance with the handle.

    • Enable the RNG clock interface when the USE_HAL_RNG_CLK_ENABLE_MODEL compilation flag is set to HAL_CLK_ENABLE_PERIPH_ONLY or HAL_CLK_ENABLE_PERIPH_PWR_SYSTEM in the stm32c5xx_hal_conf.h module.

    • Initialize the handle state to HAL_RNG_STATE_IDLE.

  • Configure the RNG peripheral with one of the following configurations:

    • Custom configuration:

Candidate NIST compliant configuration:

Candidate German BSI compliant configuration:

Additional health test configuration:

Call HAL_RNG_LockConfig() to protect the peripheral from configuration changes. When locked, do not apply any new configuration. Apply configuration only after a system reset or an RNG peripheral reset through RCC.

The maximum timeout for random number words to be generated.

  1. When a seed error occurs, call HAL_RNG_RecoverSeedError(). (Recovery is not guaranteed through this function due to hardware constraints.)

Interrupt mode operation:

  1. Call HAL_RNG_GenerateRandomNumber_IT() and specify:

    • The number of words to be generated.

    • The application buffer where the data will be stored.

  2. Call HAL_RNG_IRQHandler() to handle RNG interrupts and store the generated number words in the specified user buffer.

  3. When all random numbers specified by the user are generated, HAL_RNG_GenerationCpltCallback() executes.

  4. When a seed error occurs during the generation process, HAL_RNG_ErrorCallback() executes. Call HAL_RNG_RecoverSeedError().

To deinitialize the RNG peripheral, call HAL_RNG_DeInit().