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

Example for the mcuxClRandomModes component.

Example for the mcuxClRandomModes component

/*--------------------------------------------------------------------------*/
/* Copyright 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 <mcuxClToolchain.h>
#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_Different_Sessions_example)
{
/**************************************************************************/
/* Preparation */
/**************************************************************************/
if(!mcuxClExample_Els_Init(MCUXCLELS_RESET_DO_NOT_CANCEL))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
uint32_t context[MCUXCLRANDOMMODES_CTR_DRBG_AES256_CONTEXT_SIZE_IN_WORDS] = {0};
mcuxClRandom_Mode_t mcuxClRandomModes_Mode = mcuxClRandomModes_Mode_CtrDrbg_AES256_DRG3 ;
/* Buffers to store the generated random values in. */
ALIGNED uint8_t drbg_data1[3u];
MCUXCLBUFFER_INIT(drbgBuf1, NULL, &drbg_data1[0], 3u);
ALIGNED uint8_t drbg_data2[16u];
MCUXCLBUFFER_INIT(drbgBuf2, NULL, &drbg_data2[0], 16u);
ALIGNED uint8_t drbg_data3[31u];
MCUXCLBUFFER_INIT(drbgBuf3, NULL, &drbg_data3[0], 31u);
{ /* session_0 Scope */
mcuxClSession_Handle_t session_0 = &sessionDesc_0;
MCUXCLEXAMPLE_ALLOCATE_AND_INITIALIZE_SESSION(session_0, MCUXCLRANDOMMODES_MAX_CPU_WA_BUFFER_SIZE, 0u);
/**************************************************************************/
/* DRBG selftest. */
/**************************************************************************/
session_0,
mcuxClRandomModes_Mode
));
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/**************************************************************************/
/* DRBG initialization */
/**************************************************************************/
/* Initialize an AES-128 CTR_DRBG DRG.3 */
session_0,
mcuxClRandomModes_Mode
));
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/**************************************************************************/
/* Generate several random byte strings and reseed the DRBG in between. */
/**************************************************************************/
/* Generate random values of smaller amount than one word size. */
session_0,
drbgBuf1,
sizeof(drbg_data1)));
if((MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClRandom_generate) != generate1_token) || (MCUXCLRANDOM_STATUS_OK != rg1_status))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
if(!mcuxClExample_Session_Clean(session_0))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
}
{ /* session_1 Scope */
mcuxClSession_Handle_t session_1 = &sessionDesc_1;
MCUXCLEXAMPLE_ALLOCATE_AND_INITIALIZE_SESSION(session_1, MCUXCLRANDOMMODES_MAX_CPU_WA_BUFFER_SIZE, 0u);
/* Set random context that was previously assigned to session_0 */
MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(setRandom_status, setRandom_token, mcuxClSession_setRandom(
session_1,
mcuxClRandomModes_Mode,
pContext));
if((MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClSession_setRandom) != setRandom_token) || (MCUXCLSESSION_STATUS_OK != setRandom_status))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/* Generate random values of multiple of word size. */
session_1,
drbgBuf2,
sizeof(drbg_data2)));
if((MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClRandom_generate) != generate2_token) || (MCUXCLRANDOM_STATUS_OK != rg2_status))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/* Reseed */
MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(rr_status, reseed_token, mcuxClRandom_reseed(session_1));
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/* Generate random values of larger amount than but not multiple of one word size. */
session_1,
drbgBuf3,
sizeof(drbg_data3)));
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_1));
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
if(!mcuxClExample_Session_Clean(session_1))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
}
if(!mcuxClExample_Els_Disable())
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
return MCUXCLEXAMPLE_STATUS_OK;
}
static mcuxClEls_EccByte_t ecc_public_key_client[MCUXCLELS_ECC_PUBLICKEY_SIZE] ALIGNED
Destination buffer to receive the public key of the mcuxClEls_EccKeyGen_Async operation.
Definition mcuxClEls_Tls_Master_Key_Session_Keys_example.c:33
Definition of function identifiers for the flow protection mechanism.
Top-level include file for the ELS driver.
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 MCUXCLBUFFER_INIT(name, info, ptr, size)
Initialize an input/output buffer (mcuxCl_Buffer_t).
Definition mcuxClBuffer.h:71
#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_selftest(mcuxClSession_Handle_t pSession, mcuxClRandom_Mode_t mode)
Random data generator self-test function.
mcuxClRandom_Status_t mcuxClRandom_reseed(mcuxClSession_Handle_t pSession)
Random data generator reseed 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.
const mcuxClRandom_ModeDescriptor_t * mcuxClRandom_Mode_t
Random data generation mode/algorithm type.
Definition mcuxClRandom_Types.h:94
mcuxClRandom_ContextDescriptor_t * mcuxClRandom_Context_t
Random context type.
Definition mcuxClRandom_Types.h:71
mcuxClSession_Status_t mcuxClSession_setRandom(mcuxClSession_Handle_t session, mcuxClRandom_Mode_t randomMode, mcuxClRandom_Context_t randomCtx)
Function to switch to another random configuration.
#define MCUXCLSESSION_STATUS_OK
Session operation successful.
Definition mcuxClSession_Types.h:42
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