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

Example of SHA2-512 hashing using the ELS (CLNS component mcuxClEls)

Example of SHA2-512 hashing using the ELS (CLNS component mcuxClEls)

/*--------------------------------------------------------------------------*/
/* Copyright 2020, 2022-2023 NXP */
/* */
/* NXP Confidential. 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 <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 ALIGNED uint8_t const sha512_padded_input[MCUXCLELS_HASH_BLOCK_SIZE_SHA_512] = {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, 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, 0x00U, 0x00U,
0x00U, 0x00U, 0x00U, 0x18U};
static ALIGNED uint8_t sha512_reference_digest[MCUXCLELS_HASH_OUTPUT_SIZE_SHA_512] = {0xddU, 0xafU, 0x35U, 0xa1U,
0x93U, 0x61U, 0x7aU, 0xbaU,
0xccU, 0x41U, 0x73U, 0x49U,
0xaeU, 0x20U, 0x41U, 0x31U,
0x12U, 0xe6U, 0xfaU, 0x4eU,
0x89U, 0xa9U, 0x7eU, 0xa2U,
0x0aU, 0x9eU, 0xeeU, 0xe6U,
0x4bU, 0x55U, 0xd3U, 0x9aU,
0x21U, 0x92U, 0x99U, 0x2aU,
0x27U, 0x4fU, 0xc1U, 0xa8U,
0x36U, 0xbaU, 0x3cU, 0x23U,
0xa3U, 0xfeU, 0xebU, 0xbdU,
0x45U, 0x4dU, 0x44U, 0x23U,
0x64U, 0x3cU, 0xe8U, 0x0eU,
0x2aU, 0x9aU, 0xc9U, 0x4fU,
0xa5U, 0x4cU, 0xa4U, 0x9fU};
static ALIGNED uint8_t sha2_512_digest[MCUXCLELS_HASH_STATE_SIZE_SHA_512]; // MCUXCLELS_HASH_STATE_SIZE_SHA_512 has to be used as the mcuxClEls_Hash_Async do not perform the truncation of the hash state.
MCUXCLEXAMPLE_FUNCTION(mcuxClEls_Hash_Sha512_One_Block_example)
{
if(!mcuxClExample_Els_Init(MCUXCLELS_RESET_DO_NOT_CANCEL))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
mcuxClEls_HashOption_t hash_options = {0U}; // Initialize a new configuration for the planned mcuxClEls_Hash_Async operation.
hash_options.bits.hashini = MCUXCLELS_HASH_INIT_ENABLE; // Configure that the mcuxClEls_Hash_Async operation shall initialized with the standard IV (Initialization Vector).
hash_options.bits.hashoe = MCUXCLELS_HASH_OUTPUT_ENABLE; // Configure the mcuxClEls_Hash_Async operation so that the hash digest is moved into memory at the end of the operation.
hash_options.bits.hashmd = MCUXCLELS_HASH_MODE_SHA_512; // Configure the mcuxClEls_Hash_Async operation so that the Sha2-512 algorithm is used.
MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(result, token, mcuxClEls_Hash_Async( // Perform the hashing.
hash_options, // Set the prepared configuration.
sha512_padded_input, sizeof(sha512_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_512_digest // Output buffer, which the operation will write the hash digest to.
));
// mcuxClEls_Hash_Async is a flow-protected function: Check the protection token and the return value
{
return MCUXCLEXAMPLE_STATUS_ERROR; // Expect that no error occurred, meaning that the mcuxClEls_Hash_Async operation was started.
}
MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(result, token, mcuxClEls_WaitForOperation(MCUXCLELS_ERROR_FLAGS_CLEAR)); // Wait for the mcuxClEls_Hash_Async operation to complete.
// mcuxClEls_WaitForOperation is a flow-protected function: Check the protection token and the return value
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
for (size_t i = 0; i < sizeof(sha512_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 sha2_512_digest[MCUXCLELS_HASH_STATE_SIZE_SHA_512]
Destination buffer to receive the hash output of the SHA2-512 hashing.
Definition mcuxClEls_Hash_Sha512_One_Block_example.c:82
static ALIGNED uint8_t sha512_reference_digest[MCUXCLELS_HASH_OUTPUT_SIZE_SHA_512]
Expected hash value.
Definition mcuxClEls_Hash_Sha512_One_Block_example.c:64
static ALIGNED uint8_t const sha512_padded_input[MCUXCLELS_HASH_BLOCK_SIZE_SHA_512]
Data input for SHA2-512 hashing.
Definition mcuxClEls_Hash_Sha512_One_Block_example.c:30
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:33
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_ERROR_FLAGS_CLEAR
Set this option at mcuxClEls_ErrorHandling_t to clear all ELS error flags.
Definition mcuxClEls_Common.h:110
#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:119
MCUXCLELS_API mcuxClEls_Status_t mcuxClEls_WaitForOperation(mcuxClEls_ErrorHandling_t errorHandling)
Wait for an ELS operation and optionally clear the error status.
#define MCUXCLELS_HASH_INIT_ENABLE
Set this option at mcuxClEls_HashOption_t.hashini to initialize the hash.
Definition mcuxClEls_Hash.h:60
#define MCUXCLELS_HASH_OUTPUT_ENABLE
Set this option at mcuxClEls_HashOption_t.hashoe to output the hash to pDigest.
Definition mcuxClEls_Hash.h:66
#define MCUXCLELS_HASH_MODE_SHA_512
Set this option at mcuxClEls_HashOption_t.hashmd to use the hash algorithm SHA-512.
Definition mcuxClEls_Hash.h:78
#define MCUXCLELS_HASH_BLOCK_SIZE_SHA_512
SHA-512 output size: 1024 bit (128 bytes)
Definition mcuxClEls_Hash.h:100
#define MCUXCLELS_HASH_STATE_SIZE_SHA_512
SHA-512 state size: 512 bit (64 bytes)
Definition mcuxClEls_Hash.h:114
#define MCUXCLELS_HASH_OUTPUT_SIZE_SHA_512
SHA-512 output size: 512 bit (64 bytes)
Definition mcuxClEls_Hash.h:128
MCUXCLELS_API mcuxClEls_Status_t mcuxClEls_Hash_Async(mcuxClEls_HashOption_t options, uint8_t const *pInput, size_t inputLength, uint8_t *pDigest)
Computes the hash of a message.
#define MCUXCLELS_STATUS_OK
No error occurred.
Definition mcuxClEls_Types.h:171
#define MCUXCLELS_STATUS_OK_WAIT
An _Async function successfully started an ELS command. Call mcuxClEls_WaitForOperation to complete i...
Definition mcuxClEls_Types.h:172
#define MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(...)
Call a flow protected function and check the protection token.
Definition mcuxCsslFlowProtection.h:581
#define MCUX_CSSL_FP_FUNCTION_CALLED(...)
Expectation of a called function.
Definition mcuxCsslFlowProtection.h:735
#define MCUX_CSSL_FP_FUNCTION_CALL_END(...)
End a function call section started by MCUX_CSSL_FP_FUNCTION_CALL_BEGIN.
Definition mcuxCsslFlowProtection.h:616
Command option bit field for mcuxClEls_Hash_Async and #mcuxClEls_Hash_ShaDirect.
Definition mcuxClEls_Hash.h:161
struct mcuxClEls_HashOption_t::@29 bits
Access mcuxClEls_HashOption_t bit-wise.
uint32_t hashoe
Defines if the hash digest shall be moved to the output buffer.
Definition mcuxClEls_Hash.h:172
uint32_t hashmd
Defines which hash algorithm shall be used.
Definition mcuxClEls_Hash.h:171
uint32_t hashini
Defines if the hash engine shall be initialized.
Definition mcuxClEls_Hash.h:169