MCUX CLNS
MCUX Crypto Library Normal Secure
 
Loading...
Searching...
No Matches
mcuxClOsccaRandomModes_TrngMode_OsccaRng_example.c

Example for the mcuxClOsccaRandomModes component.

Example for the mcuxClOsccaRandomModes component

/*--------------------------------------------------------------------------*/
/* Copyright 2023-2024 NXP */
/* */
/* NXP Proprietary. 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>
#include <mcuxClExample_ELS_Helper.h>
MCUXCLEXAMPLE_FUNCTION(mcuxClOsccaRandomModes_TrngMode_OsccaRng_example)
{
/**************************************************************************/
/* Preparation */
/**************************************************************************/
if(!mcuxClExample_Els_Init(MCUXCLELS_RESET_DO_NOT_CANCEL))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
mcuxClSession_Handle_t session = &sessionDesc;
MCUXCLEXAMPLE_ALLOCATE_AND_INITIALIZE_SESSION(session, MCUXCLRANDOMMODES_NCINIT_WACPU_SIZE, 0u);
uint32_t ctx[MCUXCLOSCCARANDOMMODES_OSCCARNG_CONTEXT_SIZE_IN_WORDS];
MCUX_CSSL_ANALYSIS_START_PATTERN_REINTERPRET_MEMORY_OF_OPAQUE_TYPES()
MCUX_CSSL_ANALYSIS_STOP_PATTERN_REINTERPRET_MEMORY_OF_OPAQUE_TYPES()
/* Initialize a DRBG in OSCCA TRNG mode. */
session,
MCUX_CSSL_ANALYSIS_START_SUPPRESS_POINTER_INCOMPATIBLE("pCtx has the correct type (mcuxClRandom_Context_t), the cast was valid.")
pCtx,
MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_POINTER_INCOMPATIBLE()
mcuxClOsccaRandomModes_Mode_TRNG));
if((MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClRandom_init) != InitToken) || (MCUXCLRANDOM_STATUS_OK != randomInitRet))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/* Initialize the PRNG */
MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(prngInitRet, prngInitToken, mcuxClRandom_ncInit(session));
if((MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClRandom_ncInit) != prngInitToken) || (MCUXCLRANDOM_STATUS_OK != prngInitRet))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/**************************************************************************/
/* Generate several random byte strings */
/**************************************************************************/
/* Buffers to store the generated random values in. */
uint8_t random_buffer1[3u];
uint8_t random_buffer2[16u];
uint8_t random_buffer3[31u];
/* Generate random values of smaller amount than one CPU word size. */
MCUX_CSSL_ANALYSIS_START_SUPPRESS_ESCAPING_LOCAL_ADDRESS("Address of random_buffer1 is for internal use only and does not escape")
session,
random_buffer1,
sizeof(random_buffer1)));
MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_ESCAPING_LOCAL_ADDRESS()
if((MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClRandom_generate) != generate1_token) || (MCUXCLRANDOM_STATUS_OK != rg1_status))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/* Generate random values of multiple of CPU word size. */
MCUX_CSSL_ANALYSIS_START_SUPPRESS_ESCAPING_LOCAL_ADDRESS("Address of random_buffer2 is for internal use only and does not escape")
session,
random_buffer2,
sizeof(random_buffer2)));
MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_ESCAPING_LOCAL_ADDRESS()
if((MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClRandom_generate) != generate2_token) || (MCUXCLRANDOM_STATUS_OK != rg2_status))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/* Generate random values of larger amount than but not multiple of one CPU word size. */
MCUX_CSSL_ANALYSIS_START_SUPPRESS_ESCAPING_LOCAL_ADDRESS("Address of random_buffer3 is for internal use only and does not escape")
session,
random_buffer3,
sizeof(random_buffer3)));
MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_ESCAPING_LOCAL_ADDRESS()
if((MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClRandom_generate) != generate3_token) || (MCUXCLRANDOM_STATUS_OK != rg3_status))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/**************************************************************************/
/* Cleanup */
/**************************************************************************/
/* Random uninit. */
MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(ru_status, uninit_token, mcuxClRandom_uninit(session));
{
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;
}
Definition of function identifiers for the flow protection mechanism.
Top level header of mcuxClOsccaRandomModes component.
Top level header of mcuxClRandom component.
Top level header of mcuxClRandomModes component.
Top-level include file for the mcuxClSession component.
Provides the API for the CSSL flow protection mechanism.
#define MCUXCLELS_RESET_DO_NOT_CANCEL
Set this option at mcuxClEls_ResetOption_t to abort the requested command if another ELS operation is...
Definition mcuxClEls_Common.h:119
mcuxClRandom_Status_t mcuxClRandom_ncInit(mcuxClSession_Handle_t pSession)
Non-cryptographic PRNG initialization function.
mcuxClRandom_Status_t mcuxClRandom_generate(mcuxClSession_Handle_t pSession, mcuxCl_Buffer_t pOut, uint32_t outLength)
Random data generation function.
mcuxClRandom_Status_t mcuxClRandom_uninit(mcuxClSession_Handle_t pSession)
Random data generator uninitialization function.
mcuxClRandom_Status_t mcuxClRandom_init(mcuxClSession_Handle_t pSession, mcuxClRandom_Context_t pContext, mcuxClRandom_Mode_t mode)
Random data generator initialization function.
mcuxClRandom_ContextDescriptor_t * mcuxClRandom_Context_t
Random context type.
Definition mcuxClRandom_Types.h:71
mcuxClSession_Descriptor_t *const mcuxClSession_Handle_t
Type for mcuxClSession Handle.
Definition mcuxClSession_Types.h:113
#define MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(...)
Call a flow protected function and check the protection token.
Definition mcuxCsslFlowProtection.h:581
#define MCUX_CSSL_FP_FUNCTION_CALLED(...)
Expectation of a called function.
Definition mcuxCsslFlowProtection.h:735
#define MCUX_CSSL_FP_FUNCTION_CALL_END(...)
End a function call section started by MCUX_CSSL_FP_FUNCTION_CALL_BEGIN.
Definition mcuxCsslFlowProtection.h:616
#define MCUXCLRANDOM_STATUS_OK
Random function returned successfully.
Definition mcuxClRandom_Constants.h:46
Structure for mcuxClSession Descriptor.
Definition mcuxClSession_Types.h:121