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

Example for the mcuxClRandomModes component.

Example for the mcuxClRandomModes component

/*--------------------------------------------------------------------------*/
/* Copyright 2023-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 <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 <mcuxClEls.h> // Interface to the entire mcuxClEls component
#include <mcuxClExample_ELS_Helper.h>
/* CAVP test vectors */
static const uint32_t entropyAndNonceInputInit[MCUXCLCORE_NUM_OF_CPUWORDS_CEIL(MCUXCLRANDOMMODES_TESTMODE_CTR_DRBG_AES256_INIT_ENTROPY_SIZE)] =
{
0x5d97e604u, 0x45bf8250u, 0x93fdc193u, 0x240602c2u, 0x667688eeu, 0xc7fec3ceu, 0x37cd6b3du, 0x0f3fba6cu,
0x7e7cc018u, 0x143a77f6u, 0x929e7f5au, 0x2ccdb36cu, 0xb366cc42u, 0x1cec520au, 0x4796757au, 0x85399312u,
0x2db4e8f5u, 0x007f04au
};
/* Last byte is not used, because reseed entropy size is 55 bytes */
static const uint32_t entropyInputReseed[MCUXCLCORE_NUM_OF_CPUWORDS_CEIL(MCUXCLRANDOMMODES_TESTMODE_CTR_DRBG_AES256_RESEED_ENTROPY_SIZE)] =
{
0x20cfe741u, 0xd987b4e5u, 0x7aed81d9u, 0x2d878601u, 0x0b614e77u, 0x5a6c244eu, 0xf4a19d89u, 0x058c53a0u,
0x973bd4c6u, 0x60555726u, 0x11c4a6d3u, 0xa6cb397fu, 0x65ef9ebau, 0x009d46a8u
};
static const uint32_t refOutput[64u / sizeof(uint32_t)] =
{
0x8199928eu, 0x61249be5u, 0x163bc982u, 0x00790f1au, 0xff5ba6b1u, 0x3dab7965u, 0x04ac13bfu, 0x96b79e0eu,
0x7ee1234cu, 0xe6d553ceu, 0x46aedc8au, 0x6fb0fec9u, 0x1f60484du, 0xcedb8334u, 0xaa14c399u, 0x925fa977u
};
MCUXCLEXAMPLE_FUNCTION(mcuxClRandomModes_TestMode_CtrDrbg_AES256_DRG4_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, MCUXCLRANDOMMODES_MAX_CPU_WA_BUFFER_SIZE, 0u);
/* Allocate space for a test mode descriptor for an AES-256 CTR_DRBG DRG4. */
uint32_t testModeDescBytes[MCUXCLRANDOMMODES_TESTMODE_DESCRIPTOR_SIZE_IN_WORDS];
mcuxClRandom_ModeDescriptor_t *pTestModeDesc = (mcuxClRandom_ModeDescriptor_t *) testModeDescBytes;
/**************************************************************************/
/* Test mode creation for an AES-256 CTR_DRBG DRG4 and preparation of */
/* known entropy and nonce input for later DRBG instantiation */
/**************************************************************************/
MCUX_CSSL_ANALYSIS_START_SUPPRESS_POINTER_INCOMPATIBLE("pTestModeDesc has compatible type and cast was valid")
pTestModeDesc,
MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_POINTER_INCOMPATIBLE()
mcuxClRandomModes_Mode_CtrDrbg_AES256_DRG4,
entropyAndNonceInputInit
));
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/**************************************************************************/
/* Test mode initialization with known entropy and nonce input */
/**************************************************************************/
uint32_t context[MCUXCLRANDOMMODES_CTR_DRBG_AES256_CONTEXT_SIZE_IN_WORDS] = {0};
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("pRngCtx has the correct type (mcuxClRandom_Context_t), the cast was valid.")
pRng_ctx,
MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_POINTER_INCOMPATIBLE()
pTestModeDesc
));
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/**************************************************************************/
/* Reseed the DRBG with a known entropy, and generate two random bytes */
/* strings */
/**************************************************************************/
/* Buffers to store the generated random values in. */
ALIGNED uint8_t drbg_data1[64u] = {0u};
MCUXCLBUFFER_INIT(drbgBuf1, NULL, &drbg_data1[0], 64u);
ALIGNED uint8_t drbg_data2[64u] = {0u};
MCUXCLBUFFER_INIT(drbgBuf2, NULL, &drbg_data2[0], 64u);
/* Update entropy input to be taken for the upcoming reseeding */
MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(ue_status, ue_token, mcuxClRandomModes_updateEntropyInput(pTestModeDesc, entropyInputReseed));
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/* Reseed the DRBG with known entropy input */
MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(rr_status, reseed_token, mcuxClRandom_reseed(session));
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/* Generate random values of 512 bits */
session,
drbgBuf1,
sizeof(drbg_data1)));
if((MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClRandom_generate) != generate1_token) || (MCUXCLRANDOM_STATUS_OK != rg1_status))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/* Generate random values of 512 bits */
session,
drbgBuf2,
sizeof(drbg_data2)));
if((MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClRandom_generate) != generate2_token) || (MCUXCLRANDOM_STATUS_OK != rg2_status))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/**************************************************************************/
/* Compare the last generated random output to the expected output */
/**************************************************************************/
bool outputIsExpected = mcuxClCore_assertEqual((const uint8_t*)drbg_data2, (const uint8_t*)refOutput, sizeof(drbg_data2));
/* Return error if buffers are unequal */
if(!outputIsExpected)
{
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;
}
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:53
Definition of function identifiers for the flow protection mechanism.
Definition of macros.
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:88
#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_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.
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_createTestFromNormalMode(mcuxClRandom_ModeDescriptor_t *pTestMode, mcuxClRandom_Mode_t normalMode, const uint32_t *const pCustomSeed)
This function creates a TEST_MODE descriptor from an existing NORMAL_MODE one.
mcuxClRandom_Status_t mcuxClRandomModes_updateEntropyInput(mcuxClRandom_ModeDescriptor_t *pTestMode, const uint32_t *const pCustomSeed)
This function updates the custom seed pointer in a TEST_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