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

Example for the mcuxClOsccaRandomModes component.

Example for the mcuxClOsccaRandomModes component

/*--------------------------------------------------------------------------*/
/* Copyright 2022-2024 NXP */
/* */
/* SPDX-License-Identifier: BSD-3-Clause */
/* */
/* Redistribution and use in source and binary forms, with or without */
/* modification, are permitted provided that the following conditions are */
/* met: */
/* */
/* 1. Redistributions of source code must retain the above copyright */
/* notice, this list of conditions and the following disclaimer. */
/* */
/* 2. Redistributions in binary form must reproduce the above copyright */
/* notice, this list of conditions and the following disclaimer in the */
/* documentation and/or other materials provided with the distribution. */
/* */
/* 3. Neither the name of the copyright holder nor the names of its */
/* contributors may be used to endorse or promote products derived from */
/* this software without specific prior written permission. */
/* */
/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS */
/* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED */
/* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A */
/* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
/* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
/* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */
/* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR */
/* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF */
/* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */
/* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
/* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
/*--------------------------------------------------------------------------*/
#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>
static const 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(
uint8_t *pOut,
uint32_t outLength
)
{
(void)session;
(void)pCustomCtx;
uint32_t indexRandomData = 0u;
for (uint32_t i = 0u; i < outLength; i++)
{
pOut[i] = randomData[indexRandomData];
MCUX_CSSL_ANALYSIS_START_SUPPRESS_INTEGER_OVERFLOW("indexRandomData can never be equal to UINT32_MAX because sizeof(randomData) is 32.")
indexRandomData = (indexRandomData + 1u) % sizeof(randomData);
MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_INTEGER_OVERFLOW()
}
}
MCUXCLEXAMPLE_FUNCTION(mcuxClOsccaRandomModes_PatchMode_OsccaRng_example)
{
/**************************************************************************/
/* Preparation */
/**************************************************************************/
if(!mcuxClExample_Els_Init(MCUXCLELS_RESET_DO_NOT_CANCEL))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
mcuxClSession_Descriptor_t sessionDesc;
mcuxClSession_Handle_t session = &sessionDesc;
MCUXCLEXAMPLE_ALLOCATE_AND_INITIALIZE_SESSION(session, sizeof(mcuxClSession_Descriptor_t), 0u);
/* Fill mode descriptor with the relevant data */
uint32_t customModeDescBytes[(MCUXCLRANDOMMODES_PATCHMODE_DESCRIPTOR_SIZE + sizeof(uint32_t) - 1U)/sizeof(uint32_t)];
MCUX_CSSL_ANALYSIS_START_PATTERN_REINTERPRET_MEMORY_OF_OPAQUE_TYPES()
mcuxClRandom_ModeDescriptor_t *mcuxClRandomModes_Mode_Custom = (mcuxClRandom_ModeDescriptor_t *) customModeDescBytes;
MCUX_CSSL_ANALYSIS_STOP_PATTERN_REINTERPRET_MEMORY_OF_OPAQUE_TYPES()
/**************************************************************************/
/* RANDOM Patch Mode creation, use custom function RNG_Patch_function */
/**************************************************************************/
MCUX_CSSL_ANALYSIS_START_SUPPRESS_POINTER_INCOMPATIBLE("mcuxClRandomModes_Mode_Custom has the correct type (mcuxClRandom_ModeDescriptor_t), the cast was valid.")
mcuxClRandomModes_Mode_Custom,
MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_POINTER_INCOMPATIBLE()
(mcuxClRandomModes_CustomGenerateAlgorithm_t)RNG_Patch_function,
MCUX_CSSL_ANALYSIS_START_PATTERN_NULL_POINTER_CONSTANT()
NULL,
MCUX_CSSL_ANALYSIS_STOP_PATTERN_NULL_POINTER_CONSTANT()
128U
));
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/**************************************************************************/
/* patch mode initialization */
/**************************************************************************/
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()
session,
MCUX_CSSL_ANALYSIS_START_SUPPRESS_POINTER_INCOMPATIBLE("rngContextPatched has the correct type (mcuxClRandom_Context_t), the cast was valid.")
rngContextPatched,
MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_POINTER_INCOMPATIBLE()
mcuxClRandomModes_Mode_Custom
));
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/**************************************************************************/
/* Generate several random byte strings */
/**************************************************************************/
/* Buffers to store the generated random values in. */
uint8_t drbg_buffer1[3u];
uint8_t drbg_buffer2[sizeof(randomData) + 16u];
/* Generate random values of smaller amount than the size of prepared random data array. */
MCUX_CSSL_ANALYSIS_START_SUPPRESS_ESCAPING_LOCAL_ADDRESS("Address of drbg_buffer1 is for internal use only and does not escape")
session,
drbg_buffer1,
sizeof(drbg_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;
}
/* Check if the generated data meets expectation */
if(!mcuxClCore_assertEqual(drbg_buffer1, randomData, sizeof(drbg_buffer1)))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/* Generate random values of larger amount than the size of prepared random data array. */
MCUX_CSSL_ANALYSIS_START_SUPPRESS_ESCAPING_LOCAL_ADDRESS("Address of drbg_buffer2 is for internal use only and does not escape")
session,
drbg_buffer2,
sizeof(drbg_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;
}
/* Check if the generated data meets expectation */
if(!mcuxClCore_assertEqual(drbg_buffer2, randomData, sizeof(randomData)))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
if(!mcuxClCore_assertEqual(drbg_buffer2 + 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;
}
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:139
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:68
mcuxClRandom_ContextDescriptor_t * mcuxClRandom_Context_t
Random context type.
Definition mcuxClRandom_Types.h:91
struct mcuxClRandom_ModeDescriptor mcuxClRandom_ModeDescriptor_t
Random data generation mode/algorithm descriptor type.
Definition mcuxClRandom_Types.h:107
mcuxClRandom_Status_t mcuxClRandomModes_createPatchMode(mcuxClRandom_ModeDescriptor_t *patchMode, mcuxClRandomModes_CustomGenerateAlgorithm_t customGenerateAlgorithm, mcuxClRandom_Context_t pCustomCtx, uint32_t securityStrength)
This function creates a PATCH_MODE descriptor.
mcuxClSession_Descriptor_t *const mcuxClSession_Handle_t
Type for mcuxClSession Handle.
Definition mcuxClSession_Types.h:133
#define MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(...)
Call a flow protected function and check the protection token.
Definition mcuxCsslFlowProtection.h:601
#define MCUX_CSSL_FP_FUNCTION_CALLED(...)
Expectation of a called function.
Definition mcuxCsslFlowProtection.h:755
#define MCUX_CSSL_FP_FUNCTION_CALL_END(...)
End a function call section started by MCUX_CSSL_FP_FUNCTION_CALL_BEGIN.
Definition mcuxCsslFlowProtection.h:636
#define MCUXCLRANDOM_STATUS_OK
Random function returned successfully.
Definition mcuxClRandom_Constants.h:66