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

mcuxClHashModes example application

mcuxClHashModes example application

/*--------------------------------------------------------------------------*/
/* Copyright 2021-2025 NXP */
/* */
/* NXP Confidential and Proprietary. 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 <mcuxClSession.h> // Interface to the entire mcuxClSession component
#include <mcuxClHash.h> // Interface to the entire mcuxClHash component
#include <mcuxClCore_FunctionIdentifiers.h> // Code flow protection
#include <mcuxClToolchain.h> // memory segment definitions
#include <mcuxClExample_Session_Helper.h>
#include <mcuxClCore_Examples.h>
#include <mcuxClExample_RNG_Helper.h>
static const ALIGNED uint8_t data1[7] = {
0x65u, 0x78u, 0x61u, 0x6du, 0x70u, 0x6cu, 0x65u //example
};
static const ALIGNED uint8_t data2[4] = {
0x68u, 0x61u, 0x73u, 0x68u //hash
};
static const ALIGNED uint8_t data3[9] = {
0x73u, 0x74u, 0x72u, 0x65u, 0x61u, 0x6du, 0x69u, 0x6eu, 0x67u //streaming
};
static const ALIGNED uint8_t hashExpected[32] = {
0xb3u, 0xdcu, 0xe3u, 0x33u, 0x68u, 0x24u, 0x6du, 0x98u,
0x04u, 0x6bu, 0xd4u, 0x52u, 0x6cu, 0x69u, 0xc1u, 0xd0u,
0x37u, 0x01u, 0x57u, 0x60u, 0x95u, 0xbau, 0x74u, 0x74u,
0xc6u, 0xcbu, 0xf2u, 0x5eu, 0x3fu, 0xffu, 0xe8u, 0xc4u
};
MCUXCLEXAMPLE_FUNCTION(mcuxClHashModes_sha256_streaming_example)
{
/**************************************************************************/
/* Preparation */
/**************************************************************************/
/* Initialize session */
mcuxClSession_Descriptor_t sessionDesc;
mcuxClSession_Handle_t session = &sessionDesc;
/* Allocate and initialize session */
MCUXCLEXAMPLE_ALLOCATE_AND_INITIALIZE_SESSION(session, MCUXCLEXAMPLE_MAX_WA(MCUXCLHASH_MAX_CPU_WA_BUFFER_SIZE, MCUXCLRANDOM_NCINIT_WACPU_SIZE), 0u);
/* Initialize the PRNG */
MCUXCLEXAMPLE_INITIALIZE_PRNG(session);
/**************************************************************************/
/* Hash computation */
/**************************************************************************/
uint32_t context[MCUXCLHASH_CONTEXT_SIZE_SHA2_256_IN_WORDS];
MCUXCLBUFFER_INIT_RO(data1Buf, session, data1, sizeof(data1));
MCUXCLBUFFER_INIT_RO(data2Buf, session, data2, sizeof(data2));
MCUXCLBUFFER_INIT_RO(data3Buf, session, data3, sizeof(data3));
/* mcuxCLSession_Handle_t session: */ session,
/* mcuxClHash_Context_t context: */ (mcuxClHash_Context_t) context,
/* mcuxClHash_Algo_t algorithm: */ mcuxClHash_Algorithm_Sha256
));
// mcuxClHash_init is a flow-protected function: Check the protection token and the return value
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/* mcuxCLSession_Handle_t session: */ session,
MCUX_CSSL_ANALYSIS_START_SUPPRESS_ALREADY_INITIALIZED("Initialized by mcuxClHash_init")
/* mcuxClHash_Context_t context: */ (mcuxClHash_Context_t) context,
MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_ALREADY_INITIALIZED()
/* mcuxCl_InputBuffer_t in: */ data1Buf,
/* uint32_t inSize: */ sizeof(data1)
));
// mcuxClHash_process is a flow-protected function: Check the protection token and the return value
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/* mcuxCLSession_Handle_t session: */ session,
MCUX_CSSL_ANALYSIS_START_SUPPRESS_ALREADY_INITIALIZED("Initialized by mcuxClHash_init")
/* mcuxClHash_Context_t context: */ (mcuxClHash_Context_t) context,
MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_ALREADY_INITIALIZED()
/* mcuxCl_InputBuffer_t in: */ data2Buf,
/* uint32_t inSize: */ sizeof(data2)
));
// mcuxClHash_process is a flow-protected function: Check the protection token and the return value
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/* mcuxCLSession_Handle_t session: */ session,
MCUX_CSSL_ANALYSIS_START_SUPPRESS_ALREADY_INITIALIZED("Initialized by mcuxClHash_init")
/* mcuxClHash_Context_t context: */ (mcuxClHash_Context_t) context,
MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_ALREADY_INITIALIZED()
/* mcuxCl_InputBuffer_t in: */ data3Buf,
/* uint32_t inSize: */ sizeof(data3)
));
// mcuxClHash_process is a flow-protected function: Check the protection token and the return value
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
ALIGNED uint8_t hash[MCUXCLHASH_OUTPUT_SIZE_SHA_256];
MCUXCLBUFFER_INIT_RW(hashBuf, session, hash, sizeof(hash));
uint32_t hashOutputSize = 0u;
/* mcuxCLSession_Handle_t session: */ session,
MCUX_CSSL_ANALYSIS_START_SUPPRESS_ALREADY_INITIALIZED("Initialized by mcuxClHash_init")
/* mcuxClHash_Context_t context: */ (mcuxClHash_Context_t) context,
MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_ALREADY_INITIALIZED()
/* mcuxCl_Buffer_t pOut */ hashBuf,
/* uint32_t *const pOutSize, */ &hashOutputSize
));
// mcuxClHash_finish is a flow-protected function: Check the protection token and the return value
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
if(sizeof(hash) != hashOutputSize)
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/**************************************************************************/
/* Verification */
/**************************************************************************/
uint8_t hashDifferent = 0u;
for(size_t i = 0u; i < sizeof(hash); i++)
{
if(hashExpected[i] != hash[i])
{
hashDifferent |= 1u;
}
}
if(0u != hashDifferent)
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/**************************************************************************/
/* Session clean-up */
/**************************************************************************/
if(!mcuxClExample_Session_Clean(session))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
return MCUXCLEXAMPLE_STATUS_OK;
}
Definition of function identifiers for the flow protection mechanism.
Top-level include file for the mcuxClHash component.
Top-level include file for the mcuxClHashModes component.
Top-level include file for the mcuxClSession component.
Provides the API for the CSSL flow protection mechanism.
#define MCUXCLHASH_MAX_CPU_WA_BUFFER_SIZE
Defines the max workarea size required for this component.
Definition mcuxClHash_MemoryConsumption.h:27
#define MCUXCLBUFFER_INIT_RW(name, info, ptr, size)
Initialize an input/output buffer (mcuxCl_Buffer_t) with plain CPU handling.
Definition mcuxClBuffer.h:101
#define MCUXCLBUFFER_INIT_RO(name, info, ptr, size)
Initialize an input buffer (mcuxCl_InputBuffer_t) with plain CPU handling.
Definition mcuxClBuffer.h:84
#define MCUXCLHASH_STATUS_OK
Hash operation successful.
Definition mcuxClHash_Constants.h:35
mcuxClHash_Status_t mcuxClHash_process(mcuxClSession_Handle_t session, mcuxClHash_Context_t pContext, mcuxCl_InputBuffer_t pIn, uint32_t inSize)
Multi-part Hash processing function.
mcuxClHash_Status_t mcuxClHash_init(mcuxClSession_Handle_t session, mcuxClHash_Context_t pContext, mcuxClHash_Algo_t algorithm)
Multi-part Hash initialization function.
mcuxClHash_Status_t mcuxClHash_finish(mcuxClSession_Handle_t session, mcuxClHash_Context_t pContext, mcuxCl_Buffer_t pOut, uint32_t *const pOutSize)
Multi-part Hash computation finalization function.
mcuxClHash_ContextDescriptor_t * mcuxClHash_Context_t
Hash Context type.
Definition mcuxClHash_Types.h:72
static mcuxClHash_Algo_t mcuxClHash_Algorithm_Sha256
Sha-256 algorithm descriptor Sha-256 hash calculation using the Hash functionality SGI.
Definition mcuxClHashModes_Algorithms.h:96
#define MCUXCLHASH_OUTPUT_SIZE_SHA_256
SHA-256 output size: 256 bit (32 bytes).
Definition mcuxClHashModes_Constants.h:38
mcuxClSession_Descriptor_t *const mcuxClSession_Handle_t
Type for mcuxClSession Handle.
Definition mcuxClSession_Types.h:98
#define MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(...)
Call a flow protected function and check the protection token.
Definition mcuxCsslFlowProtection.h:623
#define MCUX_CSSL_FP_FUNCTION_CALLED(...)
Expectation of a called function.
Definition mcuxCsslFlowProtection.h:777
#define MCUX_CSSL_FP_FUNCTION_CALL_END(...)
End a function call section started by MCUX_CSSL_FP_FUNCTION_CALL_BEGIN.
Definition mcuxCsslFlowProtection.h:658