Contiki-NG
Files | Macros | Typedefs | Functions
CC13xx/CC26xx Random Number Generator

Driver for the CC13xx/CC26xx Random Number Generator. More...

Files

file  random.c
 This file overrides os/lib/random.c and calls SoC-specific RNG functions.
 
file  soc-trng.c
 Implementation of the CC13xx/CC26xx RNG driver.
 
file  soc-trng.h
 Header file for the CC13xx/CC26xx TRNG driver.
 

Macros

#define SOC_TRNG_CACHE_LEN   4
 Size of the random number cache. More...
 
#define SOC_TRNG_RAND_ASYNC_REQUEST_ERROR   0
 Async request rejected.
 
#define SOC_TRNG_RAND_ASYNC_REQUEST_OK   1
 Async request accepted.
 

Typedefs

typedef void(* soc_trng_callback_t) (uint64_t rand)
 Pointer to a callback to be provided as an argument to soc_trng_rand_asynchronous()
 

Functions

unsigned short random_rand (void)
 Generates a new random number using the hardware TRNG. More...
 
void random_init (unsigned short seed)
 Function required by the API. More...
 

TRNG functions

uint64_t soc_trng_rand_synchronous ()
 Returns a minimum entropy random number. More...
 
uint8_t soc_trng_rand_asynchronous (uint32_t samples, soc_trng_callback_t cb)
 Request a 64-bit, configurable-entropy random number. More...
 
void soc_trng_init (void)
 Initialise the CC13xx/CC26xx TRNG driver.
 

Detailed Description

Driver for the CC13xx/CC26xx Random Number Generator.

Macro Definition Documentation

◆ SOC_TRNG_CACHE_LEN

#define SOC_TRNG_CACHE_LEN   4

Size of the random number cache.

Each slot holds 4 16-bit numbers

Definition at line 54 of file soc-trng.c.

Function Documentation

◆ random_init()

void random_init ( unsigned short  seed)

Function required by the API.

Seed the cc2538 random number generator.

Parameters
seedIgnored.

Definition at line 58 of file random.c.

References soc_trng_init().

◆ random_rand()

unsigned short random_rand ( void  )

Generates a new random number using the hardware TRNG.

Generates a new random number using the cc2538 RNG.

Returns
The random number.

Definition at line 48 of file random.c.

References soc_trng_rand_synchronous().

◆ soc_trng_rand_asynchronous()

uint8_t soc_trng_rand_asynchronous ( uint32_t  samples,
soc_trng_callback_t  cb 
)

Request a 64-bit, configurable-entropy random number.

Parameters
samplesControls the entropy generated for the random number
cbA callback function to be called when the generation is complete
Return values
SOC_TRNG_RAND_ASYNC_REQUEST_ERRORThere was an error adding request.
SOC_TRNG_RAND_ASYNC_REQUEST_OKRequest successfully registered

This function is asynchronous, it will start generation of a random number and will return. The caller must provide a callback that will be called when the generation is complete. This callback must either use the random number immediately or store it, since it will not be possible to retrieve it again later form the soc-trng module.

Only one generation can be active at any given point in time. If this function gets called when a generation is already in progress, it will return SOC_TRNG_RAND_ASYNC_REQUEST_ERROR.

The function will configure the TRNG to generate entropy by sampling the FROs for a number clock cycles controlled by the samples argument. The 8 LS bits of this argument will be truncated by CC13xxware/CC26xxware and the resulting number of clock cycles will be (samples >> 8) * 2^8. Increasing the value of this argument increases entropy, but it also increases latency. Maximum entropy can be generated by passing SOC_TRNG_REFILL_CYCLES_MAX, but this will take approximately 350ms. Consult the chip's technical reference manual for advice on what would constitute sufficient entropy for random numbers meant to be used for crypto.

While this function is executing, calls to soc_trng_rand_synchronous() will return cached random numbers.

This function is not re-entrant and must not be called from an interrupt context.

Definition at line 191 of file soc-trng.c.

◆ soc_trng_rand_synchronous()

uint64_t soc_trng_rand_synchronous ( )

Returns a minimum entropy random number.

Returns
The random number

This function is synchronous. This function will make sure the PERIPH PD is powered and the TRNG is clocked. The function will then configure the TRNG to generate a random number of minimum entropy. These numbers are not suitable for cryptographic usage, but their generation is very fast.

If a high-entropy random number is currently being generated, this function will return a cached random number. The cache is of configurable size and can hold a maximum SOC_TRNG_CONF_CACHE_LEN numbers. If the cache gets emptied while high-entropy generation is in progress (e.g. because a function requested many random numbers in a row), this function will return 0. Care must therefore be taken when the return value is 0, which can also be a valid random number.

This function can be safely called from within an interrupt context.

Definition at line 145 of file soc-trng.c.

Referenced by random_rand().