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

Example of SHA2-256 hashing using sha direct mode of ELS (CLNS component mcuxClEls).

Example of SHA2-256 hashing using sha direct mode of ELS (CLNS component mcuxClEls)

/*--------------------------------------------------------------------------*/
/* Copyright 2020, 2022-2023 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 <mcuxClEls.h> // Interface to the entire mcuxClEls component
#include <mcuxClCore_FunctionIdentifiers.h> // Code flow protection
#include <mcuxClCore_Examples.h>
#include <mcuxClExample_ELS_Helper.h>
static uint8_t const sha256_padded_input[MCUXCLELS_HASH_BLOCK_SIZE_SHA_256] = {0x61U, 0x62U, 0x63U, 0x80U,
0x00U, 0x00U, 0x00U, 0x00U,
0x00U, 0x00U, 0x00U, 0x00U,
0x00U, 0x00U, 0x00U, 0x00U,
0x00U, 0x00U, 0x00U, 0x00U,
0x00U, 0x00U, 0x00U, 0x00U,
0x00U, 0x00U, 0x00U, 0x00U,
0x00U, 0x00U, 0x00u, 0x00U,
0x00U, 0x00U, 0x00U, 0x00U,
0x00U, 0x00U, 0x00U, 0x00U,
0x00U, 0x00U, 0x00U, 0x00U,
0x00U, 0x00U, 0x00U, 0x00U,
0x00U, 0x00U, 0x00U, 0x00U,
0x00U, 0x00U, 0x00U, 0x00U,
0x00U, 0x00U, 0x00U, 0x00U,
0x00U, 0x00U, 0x00U, 0x18U};
static uint8_t sha256_reference_digest[MCUXCLELS_HASH_OUTPUT_SIZE_SHA_256] = {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(mcuxClEls_Hash_Sha256_Sha_Direct_One_Block_example)
{
if(!mcuxClExample_Els_Init(MCUXCLELS_RESET_DO_NOT_CANCEL))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(result, token, mcuxClEls_ShaDirect_Enable()); // Enable the Sha direct mode of ELS
if((MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClEls_ShaDirect_Enable) != token) || (MCUXCLELS_STATUS_OK != result))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
mcuxClEls_HashOption_t hash_options = {0U}; // Initialize a new configuration for the planned mcuxClEls_Hash_ShaDirect operation.
hash_options.bits.hashini = MCUXCLELS_HASH_INIT_ENABLE; // Configure that the mcuxClEls_Hash_ShaDirect operation shall initialized with the standard IV (Initialization Vector).
hash_options.bits.hashoe = MCUXCLELS_HASH_OUTPUT_ENABLE; // Configure the mcuxClEls_Hash_ShaDirect operation so that the hash digest is moved into memory at the end of the operation.
hash_options.bits.hashmd = MCUXCLELS_HASH_MODE_SHA_256; // Configure the mcuxClEls_Hash_Async operation so that the Sha2-256 algorithm is used.
MCUX_CSSL_ANALYSIS_START_PATTERN_NULL_POINTER_CONSTANT()
MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(result, token, mcuxClEls_Hash_ShaDirect( // Perform the hashing.
hash_options, // Set the prepared configuration.
sha256_padded_input, sizeof(sha256_padded_input), // Set the data to be hashed. Note that this data's length is a multiple of the block length, so no padding is required
sha2_256_digest, // Output buffer, which the operation will write the hash digest to.
NULL, // A callback function can be provided to perform the data loading into the Sha core. If not set, a CPU copy will be used.
NULL // An pointer can be provided which will be forwarded to the callback function.
));
MCUX_CSSL_ANALYSIS_STOP_PATTERN_NULL_POINTER_CONSTANT()
// mcuxClEls_Hash_ShaDirect is a flow-protected function: Check the protection token and the return value
if ((MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClEls_Hash_ShaDirect) != token) || (MCUXCLELS_STATUS_OK != result))
{
return MCUXCLEXAMPLE_STATUS_ERROR; // Expect that no error occurred, meaning that the mcuxClEls_Hash_ShaDirect operation was started.
}
MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(result, token, mcuxClEls_ShaDirect_Disable()); // Disable the Sha direct mode of ELS
if((MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClEls_ShaDirect_Disable) != token) || (MCUXCLELS_STATUS_OK != result))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
for (size_t i = 0; i < sizeof(sha256_reference_digest); i++)
{
{
return MCUXCLEXAMPLE_STATUS_ERROR; // Expect that the resulting hash digest matches our expected output
}
}
if(!mcuxClExample_Els_Disable())
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
return MCUXCLEXAMPLE_STATUS_OK;
}
static ALIGNED uint8_t sha256_reference_digest[MCUXCLELS_HASH_OUTPUT_SIZE_SHA_256]
Expected hash value.
Definition mcuxClEls_Hash_Sha256_One_Block_example.c:68
static ALIGNED uint8_t const sha256_padded_input[MCUXCLELS_HASH_BLOCK_SIZE_SHA_256]
Data input for SHA2-256 hashing.
Definition mcuxClEls_Hash_Sha256_One_Block_example.c:50
static ALIGNED uint8_t sha2_256_digest[MCUXCLELS_HASH_STATE_SIZE_SHA_256]
Destination buffer to receive the hash output of the SHA2-256 hashing.
Definition mcuxClEls_Hash_Sha256_One_Block_example.c:78
Definition of function identifiers for the flow protection mechanism.
Top-level include file for the ELS driver.
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
#define MCUXCLELS_HASH_INIT_ENABLE
Set this option at mcuxClEls_HashOption_t.hashini to initialize the hash.
Definition mcuxClEls_Hash.h:80
#define MCUXCLELS_HASH_OUTPUT_ENABLE
Set this option at mcuxClEls_HashOption_t.hashoe to output the hash to pDigest.
Definition mcuxClEls_Hash.h:86
#define MCUXCLELS_HASH_MODE_SHA_256
Set this option at mcuxClEls_HashOption_t.hashmd to use the hash algorithm SHA-256.
Definition mcuxClEls_Hash.h:96
#define MCUXCLELS_HASH_BLOCK_SIZE_SHA_256
SHA-256 output size: 512 bit (64 bytes).
Definition mcuxClEls_Hash.h:118
#define MCUXCLELS_HASH_STATE_SIZE_SHA_256
SHA-256 state size: 256 bit (32 bytes).
Definition mcuxClEls_Hash.h:132
#define MCUXCLELS_HASH_OUTPUT_SIZE_SHA_256
SHA-256 output size: 256 bit (32 bytes).
Definition mcuxClEls_Hash.h:146
#define MCUXCLELS_STATUS_OK
No error occurred.
Definition mcuxClEls_Types.h:191
#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
Command option bit field for mcuxClEls_Hash_Async and mcuxClEls_Hash_ShaDirect.
Definition mcuxClEls_Hash.h:181
uint32_t hashoe
Defines if the hash digest shall be moved to the output buffer.
Definition mcuxClEls_Hash.h:192
uint32_t hashmd
Defines which hash algorithm shall be used.
Definition mcuxClEls_Hash.h:191
struct mcuxClEls_HashOption_t::@106367111014206316214074042350035065154005170132 bits
Access mcuxClEls_HashOption_t bit-wise.
uint32_t hashini
Defines if the hash engine shall be initialized.
Definition mcuxClEls_Hash.h:189