TRNG driver header.
The True Random Number Generator (TRNG) module generates numbers of variable lengths from a source of entropy. The output is suitable for applications requiring cryptographically random numbers such as keying material for private or symmetric keys.
Before starting a TRNG operation, the application must do the following:
TRNG_generateEntropy() provides the most basic functionality. Use it to generate random numbers of a specified width without further restrictions. An example use-case would be generating a symmetric key for AES encryption and / or authentication.
To generate an ECC private key, you should use rejection sampling to ensure that the keying material is in the interval [1, n - 1]. The ECDH public key genreation APIs will reject private keys that are outside of this interval. This information may be used to generate keying material until a suitable key is generated. For most curves, it is improbable to generate a random number outside of this interval because n is a large number close to the maximum number that would fit in the k-byte keying material array. An example of how to do this is given below.
After the TRNG operation completes, the application should either start another operation or close the driver by calling TRNG_close().
### Generate symmetric encryption key #
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <ti/drivers/cryptoutils/cryptokey/CryptoKey.h>
Go to the source code of this file.
Data Structures | |
struct | TRNG_Config |
TRNG Global configuration. More... | |
struct | TRNG_Params |
TRNG Parameters. More... | |
Macros | |
#define | TRNG_STATUS_RESERVED (-32) |
#define | TRNG_STATUS_SUCCESS (0) |
Successful status code. More... | |
#define | TRNG_STATUS_ERROR (-1) |
Generic error status code. More... | |
#define | TRNG_STATUS_RESOURCE_UNAVAILABLE (-2) |
An error status code returned if the hardware or software resource is currently unavailable. More... | |
Typedefs | |
typedef struct TRNG_Config * | TRNG_Handle |
A handle that is returned from a TRNG_open() call. More... | |
typedef struct TRNG_Config | TRNG_Config |
TRNG Global configuration. More... | |
typedef void(* | TRNG_CallbackFxn) (TRNG_Handle handle, int_fast16_t returnValue, CryptoKey *entropy) |
The definition of a callback function used by the TRNG driver when used in TRNG_RETURN_BEHAVIOR_CALLBACK. More... | |
Enumerations | |
enum | TRNG_ReturnBehavior { TRNG_RETURN_BEHAVIOR_CALLBACK = 1, TRNG_RETURN_BEHAVIOR_BLOCKING = 2, TRNG_RETURN_BEHAVIOR_POLLING = 4 } |
The way in which TRNG function calls return after generating the requested entropy. More... | |
Functions | |
void | TRNG_init (void) |
This function initializes the TRNG module. More... | |
void | TRNG_Params_init (TRNG_Params *params) |
Function to initialize the TRNG_Params struct to its defaults. More... | |
TRNG_Handle | TRNG_open (uint_least8_t index, TRNG_Params *params) |
This function opens a given TRNG peripheral. More... | |
void | TRNG_close (TRNG_Handle handle) |
Function to close a TRNG peripheral specified by the TRNG handle. More... | |
int_fast16_t | TRNG_generateEntropy (TRNG_Handle handle, CryptoKey *entropy) |
Generate a random number. More... | |
Variables | |
const TRNG_Params | TRNG_defaultParams |
Default TRNG_Params structure. More... | |
#define TRNG_STATUS_RESERVED (-32) |
Common TRNG status code reservation offset. TRNG driver implementations should offset status codes with TRNG_STATUS_RESERVED growing negatively.
Example implementation specific status codes:
#define TRNG_STATUS_SUCCESS (0) |
Successful status code.
Functions return TRNG_STATUS_SUCCESS if the function was executed successfully.
#define TRNG_STATUS_ERROR (-1) |
Generic error status code.
Functions return TRNG_STATUS_ERROR if the function was not executed successfully.
#define TRNG_STATUS_RESOURCE_UNAVAILABLE (-2) |
An error status code returned if the hardware or software resource is currently unavailable.
TRNG driver implementations may have hardware or software limitations on how many clients can simultaneously perform operations. This status code is returned if the mutual exclusion mechanism signals that an operation cannot currently be performed.
typedef struct TRNG_Config* TRNG_Handle |
A handle that is returned from a TRNG_open() call.
typedef struct TRNG_Config TRNG_Config |
TRNG Global configuration.
The TRNG_Config structure contains a set of pointers used to characterize the TRNG driver implementation.
This structure needs to be defined before calling TRNG_init() and it must not be changed thereafter.
typedef void(* TRNG_CallbackFxn) (TRNG_Handle handle, int_fast16_t returnValue, CryptoKey *entropy) |
The definition of a callback function used by the TRNG driver when used in TRNG_RETURN_BEHAVIOR_CALLBACK.
handle | Handle of the client that started the TRNG operation. |
returnValue | Return status code describing the outcome of the operation. |
entropy | The CryptoKey that describes the location the generated entropy will be copied to. |
enum TRNG_ReturnBehavior |
The way in which TRNG function calls return after generating the requested entropy.
Not all TRNG operations exhibit the specified return behavor. Functions that do not require significant computation and cannot offload that computation to a background thread behave like regular functions. Which functions exhibit the specfied return behavior is not implementation dependent. Specifically, a software-backed implementation run on the same CPU as the application will emulate the return behavior while not actually offloading the computation to the background thread.
TRNG functions exhibiting the specified return behavior have restrictions on the context from which they may be called.
Task | Hwi | Swi | |
---|---|---|---|
TRNG_RETURN_BEHAVIOR_CALLBACK | X | X | X |
TRNG_RETURN_BEHAVIOR_BLOCKING | X | ||
TRNG_RETURN_BEHAVIOR_POLLING | X | X | X |
void TRNG_init | ( | void | ) |
This function initializes the TRNG module.
void TRNG_Params_init | ( | TRNG_Params * | params | ) |
Function to initialize the TRNG_Params struct to its defaults.
params | An pointer to TRNG_Params structure for initialization |
Defaults values are: returnBehavior = TRNG_RETURN_BEHAVIOR_BLOCKING callbackFxn = NULL timeout = SemaphoreP_WAIT_FOREVER custom = NULL
TRNG_Handle TRNG_open | ( | uint_least8_t | index, |
TRNG_Params * | params | ||
) |
This function opens a given TRNG peripheral.
index | Logical peripheral number for the TRNG indexed into the TRNG_config table |
params | Pointer to an parameter block, if NULL it will use default values. |
void TRNG_close | ( | TRNG_Handle | handle | ) |
Function to close a TRNG peripheral specified by the TRNG handle.
handle | A TRNG handle returned from TRNG_open() |
int_fast16_t TRNG_generateEntropy | ( | TRNG_Handle | handle, |
CryptoKey * | entropy | ||
) |
Generate a random number.
Generates a random bitstream of the size defined in the entropy
CryptoKey in the range 0 <= entropy
buffer < 2 ^ (entropy length * 8). The entropy will be generated and stored according to the storage requirements defined in the CryptoKey.
handle | A TRNG handle returned from TRNG_open(). |
entropy | A blank, initialized CryptoKey describing the target location the entropy shall be stored in. |
TRNG_STATUS_SUCCESS | The operation succeeded. |
TRNG_STATUS_ERROR | The operation failed. |
TRNG_STATUS_RESOURCE_UNAVAILABLE | The required hardware resource was not available. Try again later. |
const TRNG_Params TRNG_defaultParams |
Default TRNG_Params structure.