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

Example constant-time memory compare (CSSL component mcuxCsslMemory).

Example constant-time memory compare (CSSL component mcuxCsslMemory)

/*--------------------------------------------------------------------------*/
/* Copyright 2020-2021, 2025-2026 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 <mcuxCsslMemory.h>
#include <mcuxCsslMemory_Examples.h>
MCUXCSSL_MEMORY_EX_FUNCTION(mcuxCsslMemory_Compare_example)
{
/* Define data arrays */
ALIGNED uint8_t arr_1[] = {0xe4u, 0xf9u, 0x26u, 0x4cu, 0x65u, 0xe2u, 0x13u, 0xa3u,
0x9au, 0x40u, 0xd7u, 0x87u, 0xccu, 0x0bu, 0x31u, 0x18u,
0xacu, 0x55u, 0xb5u, 0x7du, 0x06u, 0x7fu, 0xceu, 0xe4u,
0xb2u, 0x7eu, 0xd5u, 0xaau, 0x90u, 0x9au, 0x42u, 0x56u,
0x76u};
ALIGNED uint8_t arr_2[] = {0xe4u, 0xf9u, 0x26u, 0x4cu, 0x65u, 0xe2u, 0x13u, 0xa3u,
0x9au, 0x40u, 0xd7u, 0x87u, 0xccu, 0x0bu, 0x31u, 0x18u,
0xacu, 0x55u, 0xb5u, 0x7du, 0x06u, 0x7fu, 0xceu, 0xe4u,
0xb2u, 0x7eu, 0xd5u, 0xaau, 0x90u, 0x9au, 0x42u, 0x56u,
0x76u};
ALIGNED uint8_t arr_3[] = {0x00u, 0xf9u, 0x26u, 0x4cu, 0x65u, 0xe2u, 0x13u, 0xa3u,
0x9au, 0x40u, 0xd7u, 0x87u, 0xccu, 0x0bu, 0x31u, 0x18u,
0xacu, 0x55u, 0xb5u, 0x7du, 0x06u, 0x7fu, 0xceu, 0xe4u,
0xb2u, 0x7eu, 0xd5u, 0xaau, 0x90u, 0x9au, 0x42u, 0x56u,
0x76u};
/* Pass length as zero => Should result in the zero length return code. */
MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(compareResultZeroLength, compareTokenZeroLength, mcuxCsslMemory_Compare(
/* mcuxCsslParamIntegrity_Checksum_t chk,*/ MCUX_CSSL_PI_PROTECT(arr_1, arr_2, 0U),
/* void const * lhs, */ arr_1,
/* void const * rhs, */ arr_2,
/* uint32_t length */ 0U
));
/* Check the return code of mcuxCsslMemory_Compare */
if((MCUX_CSSL_FP_FUNCTION_CALLED(mcuxCsslMemory_Compare) != compareTokenZeroLength)
|| (MCUXCSSLMEMORY_STATUS_ZERO_LENGTH != compareResultZeroLength))
{
return MCUXCSSLMEMORY_EX_ERROR;
}
/* Compare arr_1 with arr_2 => Should be true */
MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(compareResultEq, compareTokenEq, mcuxCsslMemory_Compare(
/* mcuxCsslParamIntegrity_Checksum_t chk,*/ MCUX_CSSL_PI_PROTECT(arr_1, arr_2, sizeof(arr_1)),
/* void const * lhs, */ arr_1,
/* void const * rhs, */ arr_2,
/* uint32_t length */ sizeof(arr_1)));
/* Check the return code of mcuxCsslMemory_Compare */
|| (MCUXCSSLMEMORY_STATUS_EQUAL != compareResultEq))
{
return MCUXCSSLMEMORY_EX_ERROR;
}
/* Compare arr_1 with arr_3 => Should be false */
MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(compareResultNotEq, compareTokenNotEq, mcuxCsslMemory_Compare(
/* mcuxCsslParamIntegrity_Checksum_t chk,*/ MCUX_CSSL_PI_PROTECT(arr_1, arr_3, sizeof(arr_1)),
/* void const * lhs, */ arr_1,
/* void const * rhs, */ arr_3,
/* uint32_t length */ sizeof(arr_1)));
/* Check the return code of mcuxCsslMemory_Compare */
|| (MCUXCSSLMEMORY_STATUS_NOT_EQUAL != compareResultNotEq))
{
return MCUXCSSLMEMORY_EX_ERROR;
}
/* No error occurred during execution, exit with MCUXCSSLMEMORY_EX_OK */
return MCUXCSSLMEMORY_EX_OK;
}
Provides the API for the CSSL flow protection mechanism.
Definition of function identifiers for the flow protection mechanism.
Top-level include file for the CSSL memory functions.
Top-level include file for the parameter integrity protection mechanism.
#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
mcuxCsslMemory_Status_t mcuxCsslMemory_Compare(mcuxCsslParamIntegrity_Checksum_t chk, void const *pLhs, void const *pRhs, uint32_t length)
Compares the two memory regions lhs and rhs.
#define MCUXCSSLMEMORY_STATUS_NOT_EQUAL
The two contents of the Memory Compare are not equal.
Definition mcuxCsslMemory_Constants.h:71
#define MCUXCSSLMEMORY_STATUS_ZERO_LENGTH
The length passed to Memory Compare is zero.
Definition mcuxCsslMemory_Constants.h:70
#define MCUXCSSLMEMORY_STATUS_EQUAL
The two contents of the Memory Compare are equal.
Definition mcuxCsslMemory_Constants.h:69