7.3.1 Crypto_Rng_Generate

crypto_Rng_Status_E Crypto_Rng_Generate(
    crypto_HandlerType_E rngHandlerType_en,
    uint8_t *ptr_rngData, 
    uint32_t rngLen, 
    uint8_t *ptr_nonce, 
    uint32_t nonceLen, 
    uint32_t sessionID
    );

Description

This API is used to generate random numbers in blocks of bytes.

The provided PRNG entropy source is declared by the following function:

__attribute__((weak)) int Crypto_Rng_Wc_Prng_EntropySource(void);

This function is generic to all boards. As a result, the entropy can be significantly improved by overriding the __attribute__((weak)) definition with a custom function that better suits the board being developed for.

Max PRNG size : 0x1000 (~65kB) per buffer.

Parameters

No.Argument TypeArgument NameTypeDescription
1crypto_HandlerType_ErngHandlerType_enInputEnum for crypto operation handler i.e., SW, HW.
2uint8_t*ptr_rngDataOutputPointer which holds generated random numbers in bytes.
3uint32_trngLenInputLength of the random numbers in bytes.
4uint8_t*ptr_nonceInputPointer Holds of nonce in bytes; It is optional. Pass it NULL when not required.
5uint32_tnonceLenInputLength of nonce length in bytes. It is optional pass it 0 when not required.
6uint32_tsessionIDInputIt defines the session ID, must be more than zero.

Returns

Return TypeDescription
crypto_Rng_Status_EFunction returns the status of the API.

Prerequisites

  • To use the HW handler, the algorithm must be enabled in Crypto v4 in MPLAB® Code Configurator.
  • To use the SW handler, the algorithm must be enabled in wolfCrypt, and linked to Crypto V4 in MPLAB® Code Configurator.
  • TRNG Peripheral must be enabled in MPLAB® Code Configurator when using the HW handler.

Example

#define sessionID 1
#define DATA_SIZE     32
crypto_Rng_Status_E status;
crypto_HandlerType_E handlerType_en = CRYPTO_HANDLER_HW_INTERNAL;

uint8_t rngData[32]; 


status = Crypto_Rng_Generate(
    handlerType_en,
    rngData,
    DATA_SIZE,
    NULL,       // ptr_nonce
    0,          // sizeof(nonce)
    sessionID
    );