MCUX CLNS
MCUX Crypto Library Normal Secure
mcuxClRandomModes_ELS_example.c

Example for the mcuxClRandomModes component

/*--------------------------------------------------------------------------*/
/* Copyright 2021-2023 NXP */
/* */
/* NXP Confidential. This software is owned or controlled by NXP and may */
/* only be used strictly in accordance with the applicable license terms. */
/* By expressly accepting such terms or by downloading, installing, */
/* activating and/or otherwise using the software, you are agreeing that */
/* you have read, and that you agree to comply with and are bound by, such */
/* license terms. If you do not agree to be bound by the applicable license */
/* terms, then you may not retain, install, activate or otherwise use the */
/* software. */
/*--------------------------------------------------------------------------*/
#include <mcuxClRandom.h>
#include <mcuxClSession.h>
#include <mcuxClCore_FunctionIdentifiers.h> // Code flow protection
#include <mcuxClExample_Session_Helper.h>
#include <mcuxClCore_Examples.h> // Defines and assertions for examples
#include <mcuxClEls.h> // Interface to the entire mcuxClEls component
#include <mcuxClExample_ELS_Helper.h>
MCUXCLEXAMPLE_FUNCTION(mcuxClRandomModes_ELS_example)
{
/**************************************************************************/
/* Preparation */
/**************************************************************************/
if(!mcuxClExample_Els_Init(MCUXCLELS_RESET_DO_NOT_CANCEL))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/* Buffers to store the generated random values in. */
uint8_t prng_buffer[10u];
uint8_t drbg_buffer1[3u];
uint8_t drbg_buffer2[4u];
uint8_t drbg_buffer3[5u];
mcuxClSession_Handle_t session = &sessionDesc;
//Allocate and initialize session
MCUXCLEXAMPLE_ALLOCATE_AND_INITIALIZE_SESSION(session, 0u, 0u);
/* We don't need a context for ELS Rng. */
mcuxClRandom_Context_t context = NULL;
/**************************************************************************/
/* Random init */
/**************************************************************************/
/* Initialize the Random session with ELS mode. */
session,
context,
mcuxClRandomModes_Mode_ELS_Drbg));
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/**************************************************************************/
/* Generate random values. */
/**************************************************************************/
/* Generate random values of smaller amount than one word size. */
session,
drbg_buffer1,
sizeof(drbg_buffer1)));
if((MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClRandom_generate) != token) || (MCUXCLRANDOM_STATUS_OK != randomGenerateresult1))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/* Generate random values of multiple of word size. */
session,
drbg_buffer2,
sizeof(drbg_buffer2)));
if((MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClRandom_generate) != token) || (MCUXCLRANDOM_STATUS_OK != randomGenerateresult2))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/* Generate random values of larger amount than but not multiple of one word size. */
session,
drbg_buffer3,
sizeof(drbg_buffer3)));
if((MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClRandom_generate) != token) || (MCUXCLRANDOM_STATUS_OK != randomGenerateresult3))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/**************************************************************************/
/* Generate non-cryptographic random values. */
/**************************************************************************/
/* Initialize non-cryptographic Rng. */
MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(randomNcInitresult, token, mcuxClRandom_ncInit(session));
if((MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClRandom_ncInit) != token) || (MCUXCLRANDOM_STATUS_OK != randomNcInitresult))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/* Generate random values. */
session,
prng_buffer,
sizeof(prng_buffer)));
if((MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClRandom_ncGenerate) != token) || (MCUXCLRANDOM_STATUS_OK != randomNcGenerateresult))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/**************************************************************************/
/* Cleanup */
/**************************************************************************/
/* Random uninit. */
MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(randomUninitresult, token, mcuxClRandom_uninit(session));
if((MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClRandom_uninit) != token) || (MCUXCLRANDOM_STATUS_OK != randomUninitresult))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
if(!mcuxClExample_Session_Clean(session))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
if(!mcuxClExample_Els_Disable())
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
return MCUXCLEXAMPLE_STATUS_OK;
}