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

mcuxClHashModes example application

mcuxClHashModes example application

/*--------------------------------------------------------------------------*/
/* Copyright 2020, 2022-2025 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 <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 data[3] = {
0x61u, 0x62u, 0x63u
};
static const ALIGNED uint8_t hashExpected[32] = {
0xbau, 0x78u, 0x16u, 0xbfu, 0x8fu, 0x01u, 0xcfu, 0xeau,
0x41u, 0x41u, 0x40u, 0xdeu, 0x5du, 0xaeu, 0x22u, 0x23u,
0xb0u, 0x03u, 0x61u, 0xa3u, 0x96u, 0x17u, 0x7au, 0x9cu,
0xb4u, 0x10u, 0xffu, 0x61u, 0xf2u, 0x00u, 0x15u, 0xadu
};
MCUXCLEXAMPLE_FUNCTION(mcuxClHashModes_sha256_oneshot_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 */
/**************************************************************************/
ALIGNED uint8_t hash[MCUXCLHASH_OUTPUT_SIZE_SHA_256];
MCUXCLBUFFER_INIT_RW(hashBuf, session, hash, sizeof(hash));
MCUXCLBUFFER_INIT_RO(dataBuf, session, data, sizeof(data));
uint32_t hashOutputSize = 0u;
/* mcuxClSession_Handle_t session: */ session,
/* mcuxClHash_Algo_t algorithm: */ mcuxClHash_Algorithm_Sha256,
/* mcuxCl_InputBuffer_t pIn: */ dataBuf,
/* uint32_t inSize: */ sizeof(data),
/* mcuxCl_Buffer_t pOut */ hashBuf,
/* uint32_t *const pOutSize, */ &hashOutputSize
));
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
if(sizeof(hash) != hashOutputSize)
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/**************************************************************************/
/* Verification */
/**************************************************************************/
for(size_t i = 0U; i < sizeof(hash); i++)
{
MCUX_CSSL_ANALYSIS_START_SUPPRESS_ALREADY_INITIALIZED("Initialized by MCUXCLBUFFER_INIT_RW")
if(hashExpected[i] != hash[i]) // Expect that the resulting hash matches our expected output
MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_ALREADY_INITIALIZED()
{
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:47
#define MCUXCLBUFFER_INIT_RW(name, info, ptr, size)
Initialize an input/output buffer (mcuxCl_Buffer_t) with plain CPU handling.
Definition mcuxClBuffer.h:121
#define MCUXCLBUFFER_INIT_RO(name, info, ptr, size)
Initialize an input buffer (mcuxCl_InputBuffer_t) with plain CPU handling.
Definition mcuxClBuffer.h:104
#define MCUXCLHASH_STATUS_OK
Hash operation successful.
Definition mcuxClHash_Constants.h:55
mcuxClHash_Status_t mcuxClHash_compute(mcuxClSession_Handle_t session, mcuxClHash_Algo_t algorithm, mcuxCl_InputBuffer_t pIn, uint32_t inSize, mcuxCl_Buffer_t pOut, uint32_t *const pOutSize)
One-shot Hash computation function.
static mcuxClHash_Algo_t mcuxClHash_Algorithm_Sha256
Sha-256 algorithm descriptor Sha-256 hash calculation using the Hash functionality SGI.
Definition mcuxClHashModes_Algorithms.h:116
#define MCUXCLHASH_OUTPUT_SIZE_SHA_256
SHA-256 output size: 256 bit (32 bytes).
Definition mcuxClHashModes_Constants.h:58
mcuxClSession_Descriptor_t *const mcuxClSession_Handle_t
Type for mcuxClSession Handle.
Definition mcuxClSession_Types.h:118
#define MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(...)
Call a flow protected function and check the protection token.
Definition mcuxCsslFlowProtection.h:643
#define MCUX_CSSL_FP_FUNCTION_CALLED(...)
Expectation of a called function.
Definition mcuxCsslFlowProtection.h:797
#define MCUX_CSSL_FP_FUNCTION_CALL_END(...)
End a function call section started by MCUX_CSSL_FP_FUNCTION_CALL_BEGIN.
Definition mcuxCsslFlowProtection.h:678