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

Example for the mcuxClRandomModes component.

Example for the mcuxClRandomModes component

/*--------------------------------------------------------------------------*/
/* Copyright 2022-2024 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>
#include <mcuxClBuffer.h>
static const ALIGNED uint8_t randomData[] = { 0x8au,0x76u,0x90u,0xd2u,0xd9u,0x55u,0x3cu,0x93u,
0x03u,0x52u,0x3au,0x3cu,0xbeu,0xe1u,0x39u,0xa4u,
0xefu,0xf1u,0xc4u,0xbbu,0xa3u,0xc7u,0x09u,0xf3u,
0xb7u,0x14u,0x07u,0xb2u,0xd8u,0x98u,0xa0u,0xaeu };
/******************************************************************************
* Local and global function declarations
******************************************************************************/
static mcuxClRandom_Status_t RNG_Patch_function(
uint32_t outLength
)
{
(void)session;
(void)pCustomCtx;
uint32_t indexRandomData = 0u;
for (uint32_t i = 0u; i < outLength; i++)
{
MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(be_status, be_token, mcuxClBuffer_export(pOut, i, (uint8_t const *)&randomData[indexRandomData], 1u));
if((MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClBuffer_export) != be_token) || (MCUXCLBUFFER_STATUS_OK != be_status))
{
}
indexRandomData = (indexRandomData + 1u) % sizeof(randomData);
}
}
MCUXCLEXAMPLE_FUNCTION(mcuxClRandomModes_PatchMode_CtrDrbg_AES256_DRG3_example)
{
/**************************************************************************/
/* Preparation */
/**************************************************************************/
mcuxClSession_Handle_t session = &sessionDesc;
MCUXCLEXAMPLE_ALLOCATE_AND_INITIALIZE_SESSION(session, MCUXCLRANDOMMODES_MAX_CPU_WA_BUFFER_SIZE, 0u);
/* Fill mode descriptor with the relevant data */
uint32_t customModeDescBytes[MCUXCLRANDOMMODES_PATCHMODE_DESCRIPTOR_SIZE_IN_WORDS];
mcuxClRandom_ModeDescriptor_t *mcuxClRandomModes_Mode_Custom = (mcuxClRandom_ModeDescriptor_t *) customModeDescBytes;
/**************************************************************************/
/* RANDOM Patch Mode creation, use custom function RNG_Patch_function */
/**************************************************************************/
MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(cp_status, cp_token, mcuxClRandomModes_createPatchMode(
mcuxClRandomModes_Mode_Custom,
(mcuxClRandomModes_CustomGenerateAlgorithm_t)RNG_Patch_function,
NULL,
256U
));
if((MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClRandomModes_createPatchMode) != cp_token) || (MCUXCLRANDOM_STATUS_OK != cp_status))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/**************************************************************************/
/* patch mode initialization */
/**************************************************************************/
uint32_t* rngContextPatched = NULL;
session,
(mcuxClRandom_Context_t)rngContextPatched,
mcuxClRandomModes_Mode_Custom
));
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/**************************************************************************/
/* Generate several random byte strings */
/**************************************************************************/
/* 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[sizeof(randomData) + 16u];
MCUXCLBUFFER_INIT(drbgBuf2, NULL, &drbg_data2[0], sizeof(randomData) + 16u);
/* Generate random values of smaller amount than the size of prepared random data array. */
session,
drbgBuf1,
sizeof(drbg_data1)));
if((MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClRandom_generate) != generate1_token) || (MCUXCLRANDOM_STATUS_OK != rg1_status))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/* Check if the generated data meets expectation */
if(!mcuxClCore_assertEqual(drbg_data1, randomData, sizeof(drbg_data1)))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/* Generate random values of larger amount than the size of prepared random data array. */
session,
drbgBuf2,
sizeof(drbg_data2)));
if((MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClRandom_generate) != generate2_token) || (MCUXCLRANDOM_STATUS_OK != rg2_status))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/* Check if the generated data meets expectation */
if(!mcuxClCore_assertEqual(drbg_data2, randomData, sizeof(randomData)))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
if(!mcuxClCore_assertEqual(drbg_data2 + sizeof(randomData), randomData, 16u))
{
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;
}
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
Provides the API for the CL buffer types.
mcuxClBuffer_Status_t mcuxClBuffer_export(mcuxCl_Buffer_t bufDst, uint32_t offset, const uint8_t *pSrc, uint32_t byteLength)
Perform a write to the buffer.
Definition of function identifiers for the flow protection mechanism.
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
uint8_t * mcuxCl_Buffer_t
Input/output buffer type.
Definition mcuxClBuffer_Pointer.h:55
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.
uint32_t mcuxClRandom_Status_t
Type for status codes of mcuxClRandom component functions.
Definition mcuxClRandom_Types.h:48
mcuxClRandom_ContextDescriptor_t * mcuxClRandom_Context_t
Random context type.
Definition mcuxClRandom_Types.h:71
struct mcuxClRandom_ModeDescriptor mcuxClRandom_ModeDescriptor_t
Random data generation mode/algorithm descriptor type.
Definition mcuxClRandom_Types.h:87
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_FAULT_ATTACK
Random function returned fault attack.
Definition mcuxClRandom_Constants.h:47
#define MCUXCLRANDOM_STATUS_OK
Random function returned successfully.
Definition mcuxClRandom_Constants.h:46
Structure for mcuxClSession Descriptor.
Definition mcuxClSession_Types.h:121