The MCUXpresso SDK provides a peripheral driver for the True Random Number Generator (TRNG) module of MCUXpresso SDK devices.
The True Random Number Generator is a hardware accelerator module that generates a 512-bit entropy as needed by an entropy consuming module or by other post processing functions. A typical entropy consumer is a pseudo random number generator (PRNG) which can be implemented to achieve both true randomness and cryptographic strength random numbers using the TRNG output as its entropy seed. The entropy generated by a TRNG is intended for direct use by functions that generate secret keys, per-message secrets, random challenges, and other similar quantities used in cryptographic algorithms.
TRNG Initialization
-
Define the TRNG user configuration structure. Use TRNG_InitUserConfigDefault() function to set it to default TRNG configuration values.
-
Initialize the TRNG module, call the TRNG_Init() function, and pass the user configuration structure. This function automatically enables the TRNG module and its clock. After that, the TRNG is enabled and the entropy generation starts working.
-
To disable the TRNG module, call the TRNG_Deinit() function.
Get random data from TRNG
-
TRNG_GetRandomData() function gets random data from the TRNG module.
This example code shows how to initialize and get random data from the TRNG driver.
Refer to the driver examples codes located at <SDK_ROOT>/boards/<BOARD>/driver_examples/trng
struct _trng_statistical_check_limit |
uint32_t _trng_statistical_check_limit::maximum |
uint32_t _trng_statistical_check_limit::minimum |
This structure initializes the TRNG by calling the TRNG_Init() function. It contains all TRNG configurations.
bool _trng_user_config::lock |
uint16_t _trng_user_config::entropyDelay |
Defines the length (in system clocks) of each Entropy sample taken.
uint16_t _trng_user_config::sampleSize |
Defines the total number of Entropy samples that will be taken during Entropy generation.
uint16_t _trng_user_config::sparseBitLimit |
This limit is used only for during von Neumann sampling (enabled by TRNG_HAL_SetSampleMode()). Samples are discarded if two consecutive raw samples are both 0 or both 1. If this discarding occurs for a long period of time, it indicates that there is insufficient Entropy.
uint8_t _trng_user_config::retryCount |
It defines the number of times a statistical check may fails during the TRNG Entropy Generation before generating an error.
uint8_t _trng_user_config::longRunMaxLimit |
Current version: 2.0.16
Change log:
- version 2.0.16
- Added support for Dual oscillator mode.
- version 2.0.15
- Changed TRNG_USER_CONFIG_DEFAULT_XXX values according to latest reccomended by design team.
- version 2.0.14
- add support for RW610 and RW612
- version 2.0.13
- After deepsleep it might return error, added clearing bits in TRNG_GetRandomData() and generating new entropy.
- Modified reloading entropy in TRNG_GetRandomData(), for some data length it doesn't reloading entropy correctly.
- version 2.0.12
- For KW34A4_SERIES, KW35A4_SERIES, KW36A4_SERIES set TRNG_USER_CONFIG_DEFAULT_OSC_DIV to kTRNG_RingOscDiv8.
- version 2.0.11
- version 2.0.10
- version 2.0.9
- Fix HIS_CCM metrics issues.
- version 2.0.8
- For K32L2A41A_SERIES set TRNG_USER_CONFIG_DEFAULT_OSC_DIV to kTRNG_RingOscDiv4.
- version 2.0.7
- Fix MISRA 2004 issue rule 12.5.
- version 2.0.6
- For KW35Z4_SERIES set TRNG_USER_CONFIG_DEFAULT_OSC_DIV to kTRNG_RingOscDiv8.
- version 2.0.5
- Add possibility to define default TRNG configuration by device specific preprocessor macros for FRQMIN, FRQMAX and OSCDIV.
- version 2.0.4
- Version 2.0.3
- update TRNG_Init to restart entropy generation
- Version 2.0.2
- Version 2.0.1
- add support for KL8x and KL28Z
- update default OSCDIV for K81 to divide by 2
This structure initializes the TRNG by calling the TRNG_Init() function. It contains all TRNG configurations.
Used by trng_config_t.
Enumerator |
---|
kTRNG_SampleModeVonNeumann |
Use von Neumann data in both Entropy shifter and Statistical Checker.
|
kTRNG_SampleModeRaw |
Use raw data into both Entropy shifter and Statistical Checker.
|
kTRNG_SampleModeVonNeumannRaw |
Use von Neumann data in Entropy shifter.
Use raw data into Statistical Checker.
|
Used by trng_config_t.
Enumerator |
---|
kTRNG_ClockModeRingOscillator |
Ring oscillator is used to operate the TRNG (default).
|
kTRNG_ClockModeSystem |
System clock is used to operate the TRNG.
This is for test use only, and indeterminate results may occur.
|
Used by trng_config_t.
Enumerator |
---|
kTRNG_RingOscDiv0 |
Ring oscillator with no divide.
|
kTRNG_RingOscDiv2 |
Ring oscillator divided-by-2.
|
kTRNG_RingOscDiv4 |
Ring oscillator divided-by-4.
|
kTRNG_RingOscDiv8 |
Ring oscillator divided-by-8.
|
This function initializes the configuration structure to default values. The default values are platform dependent.
- Parameters
-
userConfig | User configuration structure. |
- Returns
- If successful, returns the kStatus_TRNG_Success. Otherwise, it returns an error.
This function initializes the TRNG. When called, the TRNG entropy generation starts immediately.
- Parameters
-
base | TRNG base address |
userConfig | Pointer to the initialization configuration structure. |
- Returns
- If successful, returns the kStatus_TRNG_Success. Otherwise, it returns an error.
void TRNG_Deinit |
( |
TRNG_Type * |
base | ) |
|
This function shuts down the TRNG.
- Parameters
-
status_t TRNG_GetRandomData |
( |
TRNG_Type * |
base, |
|
|
void * |
data, |
|
|
size_t |
dataSize |
|
) |
| |
This function gets random data from the TRNG.
- Parameters
-
base | TRNG base address. |
data | Pointer address used to store random data. |
dataSize | Size of the buffer pointed by the data parameter. |
- Returns
- random data