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

Example for the mcuxClRandom component.

Example for the mcuxClRandom 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 <mcuxClBuffer.h>
#include <mcuxClCore_FunctionIdentifiers.h> // Code flow protection
#include <mcuxClExample_Session_Helper.h>
#include <mcuxClCore_Examples.h> // Defines and assertions for examples
#include <mcuxClExample_ELS_Helper.h>
/* Initialize index to be used as custom context. */
static uint32_t indexRandomData = 0u;
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};
static mcuxClRandom_Status_t prngPatchFunction(
void *pCustomState,
uint32_t outLength
)
{
uint32_t *pIndexRandomData = (uint32_t *)pCustomState;
for (uint32_t i = 0u; i < outLength; i++)
{
/* Ideally mcuxClBuffer_export should be used on larger chunks of data. Using it on individual bytes is just used to keep the example simple. */
MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(be_status, be_token, mcuxClBuffer_export(pOut, i, (uint8_t const *)&randomData[*pIndexRandomData], 1u));
if((MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClBuffer_export) != be_token) || (MCUXCLBUFFER_STATUS_OK != be_status))
{
}
*pIndexRandomData = (*pIndexRandomData + 1u) % sizeof(randomData);
}
}
MCUXCLEXAMPLE_FUNCTION(mcuxClRandom_PRNG_Patch_example)
{
/**************************************************************************/
/* Preparation */
/**************************************************************************/
mcuxClSession_Handle_t session = &sessionDesc;
MCUXCLEXAMPLE_ALLOCATE_AND_INITIALIZE_SESSION(session, 0u, 0u);
if(!mcuxClExample_Els_Init(MCUXCLELS_RESET_DO_NOT_CANCEL))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/* Initialize PRNG. This initializes PRNG in normal / unpatched mode */
session
));
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/**************************************************************************/
/* Start with PRNG in normal mode */
/**************************************************************************/
uint8_t pPrngData[16u];
MCUXCLBUFFER_INIT(pPrngBuffer, session, pPrngData, sizeof(pPrngData));
/* Generate non cryptographic random values. */
session,
pPrngBuffer,
sizeof(pPrngData)
));
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/* Check whether unpatched PRNG indeed outputs unexpected data */
bool outputAsExpected = mcuxClCore_assertEqual((const uint8_t *) pPrngData, (const uint8_t*) randomData, sizeof(pPrngData));
/* Return error if buffers are equal */
if(outputAsExpected)
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/**************************************************************************/
/* Put PRNG in patch mode */
/**************************************************************************/
session,
&indexRandomData
));
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/* Generate patched random values. */
session,
pPrngBuffer,
sizeof(pPrngData)
));
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/* Check whether patched PRNG indeed outputs expected data */
outputAsExpected = mcuxClCore_assertEqual((const uint8_t *) pPrngData, (const uint8_t*) randomData, sizeof(pPrngData));
/* Return error if buffers are unequal */
if(!outputAsExpected)
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/**************************************************************************/
/* Return to PRNG in normal mode */
/**************************************************************************/
MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(unpatch_status, unpatch_token, mcuxClRandom_ncInit(
session
));
if((MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClRandom_ncInit) != unpatch_token) || (MCUXCLRANDOM_STATUS_OK != unpatch_status))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/* Generate non cryptographic random values. */
session,
pPrngBuffer,
sizeof(pPrngData)
));
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/* Check whether unpatched PRNG indeed outputs unexpected data */
outputAsExpected = mcuxClCore_assertEqual((const uint8_t *) pPrngData, (const uint8_t*) randomData, sizeof(pPrngData));
/* Return error if buffers are equal */
if(outputAsExpected)
{
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 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
#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_ncGenerate(mcuxClSession_Handle_t pSession, mcuxCl_Buffer_t pOut, uint32_t outLength)
Non-cryptographic PRNG data generation function.
mcuxClRandom_Status_t mcuxClRandom_ncPatch(mcuxClSession_Handle_t pSession, mcuxClRandom_CustomNcGenerateAlgorithm_t prngPatchFunction, void *pCustomPrngState)
Function to enable the PRNG patch mode.
mcuxClRandom_Status_t(* mcuxClRandom_CustomNcGenerateAlgorithm_t)(void *pCustomPrngState, mcuxCl_Buffer_t pOut, uint32_t outLength)
Interface definition for custom PRNG functions to be used by PRNG patch mode.
Definition mcuxClRandom_Types.h:99
uint32_t mcuxClRandom_Status_t
Type for status codes of mcuxClRandom component functions.
Definition mcuxClRandom_Types.h:48
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