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

Example CMAC computation using functions of the mcuxClKey and mcuxClMac component.

Example CMAC computation using functions of the mcuxClKey and mcuxClMac component

/*--------------------------------------------------------------------------*/
/* Copyright 2021-2024 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 <mcuxClCore_Examples.h> // Defines and assertions for examples
#include <mcuxClEls.h> // Interface to the entire mcuxClEls component
#include <mcuxClKey.h>
#include <mcuxClAes.h> // Interface to AES-related definitions and types
#include <mcuxClSession.h>
#include <mcuxClCore_FunctionIdentifiers.h> // Code flow protection
#include <mcuxClMac.h> // Interface to the entire mcuxClMac component
#include <mcuxClMacModes.h> // Interface to the entire mcuxClMacModes component
#include <mcuxClExample_ELS_Helper.h>
#include <mcuxClExample_Key_Helper.h>
#include <mcuxClExample_Session_Helper.h>
#include <mcuxClExample_RNG_Helper.h>
MCUXCLEXAMPLE_FUNCTION(mcuxClMacModes_Els_Cmac_Aes128_Oneshot_example)
{
/* Example AES-128 key. */
0x7c, 0x0b, 0x7d, 0xb9,
0x81, 0x1f, 0x10, 0xd0,
0x0e, 0x47, 0x6c, 0x7a,
0x0d, 0x92, 0xf6, 0xe0
};
/* Example input size. */
size_t cmacInputSize = 32u;
/* Example input to the CMAC function. */
static ALIGNED uint8_t cmacInput16[] = {
0x1eu, 0xe0u, 0xecu, 0x46u,
0x6du, 0x46u, 0xfdu, 0x84u,
0x9bu, 0x40u, 0xc0u, 0x66u,
0xb4u, 0xfbu, 0xbdu, 0x22u,
0xa2u, 0x0au, 0x4du, 0x80u,
0xa0u, 0x08u, 0xacu, 0x9au,
0xf1u, 0x7eu, 0x4fu, 0xdfu,
0xd1u, 0x06u, 0x78u, 0x5eu
};
/* Example reference CMAC. */
static ALIGNED uint8_t cmacOutputReference16[MCUXCLELS_CMAC_OUT_SIZE] = {
0xbau, 0xecu, 0xdcu, 0x91u,
0xe9u, 0xa1u, 0xfcu, 0x35u,
0x72u, 0xadu, 0xf1u, 0xe4u,
0x23u, 0x2au, 0xe2u, 0x85u
};
/**************************************************************************/
/* Preparation */
/**************************************************************************/
if(!mcuxClExample_Els_Init(MCUXCLELS_RESET_DO_NOT_CANCEL))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/* Key buffer for the key in memory. */
uint32_t key_buffer[MCUXCLAES_AES128_KEY_SIZE_IN_WORDS];
/* Output buffer for the computed MAC. */
static ALIGNED uint8_t result[MCUXCLELS_CMAC_OUT_SIZE];
mcuxClSession_Descriptor_t sessionDesc;
mcuxClSession_Handle_t session = &sessionDesc;
/* Allocate and initialize session */
MCUXCLEXAMPLE_ALLOCATE_AND_INITIALIZE_SESSION(session, MCUXCLEXAMPLE_MAX_WA(MCUXCLMAC_MAX_CPU_WA_BUFFER_SIZE, MCUXCLRANDOMMODES_NCINIT_WACPU_SIZE), 0u);
/* Initialize the PRNG */
MCUXCLEXAMPLE_INITIALIZE_PRNG(session);
/**************************************************************************/
/* Key setup */
/**************************************************************************/
/* Create and initialize mcuxClKey_Descriptor_t structure. */
uint32_t keyDesc[MCUXCLKEY_DESCRIPTOR_SIZE_IN_WORDS];
MCUX_CSSL_ANALYSIS_START_PATTERN_REINTERPRET_MEMORY_OF_OPAQUE_TYPES()
MCUX_CSSL_ANALYSIS_STOP_PATTERN_REINTERPRET_MEMORY_OF_OPAQUE_TYPES()
/* Set key properties. */
mcuxClEls_KeyProp_t cmac_key_properties;
cmac_key_properties.word.value = 0u;
MCUX_CSSL_ANALYSIS_START_PATTERN_0U_1U_ARE_UNSIGNED()
MCUX_CSSL_ANALYSIS_STOP_PATTERN_0U_1U_ARE_UNSIGNED()
//Initializes a key handle, Set key properties and Load key.
if(!mcuxClExample_Key_Init_And_Load(session,
MCUX_CSSL_ANALYSIS_START_SUPPRESS_POINTER_INCOMPATIBLE("The pointer key points to an object of the right type, the cast was valid.")
key,
MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_POINTER_INCOMPATIBLE()
sizeof(aes128_key),
&cmac_key_properties,
key_buffer, MCUXCLEXAMPLE_CONST_EXTERNAL_KEY))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/**************************************************************************/
/* MAC computation */
/**************************************************************************/
MCUXCLBUFFER_INIT_RO(cmacInput16Buf, session, cmacInput16, sizeof(cmacInput16));
MCUXCLBUFFER_INIT(resultBuf, session, result, sizeof(result));
/* Call the mcuxClMac_compute function to compute a CMAC in one shot. */
uint32_t result_size = 0u;
/* mcuxClSession_Handle_t session: */ session,
/* const mcuxClKey_Handle_t key: */ key,
/* const mcuxClMac_Mode_t mode: */ mcuxClMac_Mode_CMAC,
/* mcuxCl_InputBuffer_t pIn: */ cmacInput16Buf,
/* uint32_t inLength: */ cmacInputSize,
/* mcuxCl_Buffer_t pMac: */ resultBuf,
/* uint32_t * const pMacLength: */ &result_size
));
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/**************************************************************************/
/* Verification */
/**************************************************************************/
/* Compare the output size with the expected MAC size */
if(sizeof(cmacOutputReference16) != result_size)
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/* Compare the result to the reference value. */
if(!mcuxClCore_assertEqual(cmacOutputReference16, result, sizeof(cmacOutputReference16)))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/**************************************************************************/
/* Cleanup */
/**************************************************************************/
/* Flush the key. */
key));
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
if(!mcuxClExample_Session_Clean(session))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
if(!mcuxClExample_Els_Disable())
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
return MCUXCLEXAMPLE_STATUS_OK;
}
static uint32_t const aes128_key[MCUXCLELS_CIPHER_KEY_SIZE_AES_128/sizeof(uint32_t)]
Key for the AES encryption.
Definition mcuxClEls_Cipher_Aes128_Ecb_Encrypt_example.c:62
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:53
Top-level include file for the mcuxClAes component.
Definition of function identifiers for the flow protection mechanism.
Top-level include file for the ELS driver.
Top-level include file for the mcuxClKey component.
Top-level include file for the mcuxClMac component.
Top-level include file for the mcuxClMacModes component.
Top-level include file for the mcuxClSession component.
Provides the API for the CSSL flow protection mechanism.
#define MCUXCLAES_AES128_KEY_SIZE_IN_WORDS
AES-128 key size in words.
Definition mcuxClAes_Constants.h:62
#define MCUXCLAES_AES128_KEY_SIZE
AES-128 key size in bytes.
Definition mcuxClAes_Constants.h:61
static const mcuxClKey_Type_t mcuxClKey_Type_Aes128
Key type pointer for AES-128 based keys.
Definition mcuxClAes_KeyTypes.h:70
#define MCUXCLBUFFER_INIT(name, info, ptr, size)
Initialize an input/output buffer (mcuxCl_Buffer_t).
Definition mcuxClBuffer.h:88
#define MCUXCLBUFFER_INIT_RO(name, info, ptr, size)
Initialize an input buffer (mcuxCl_InputBuffer_t) with plain CPU handling.
Definition mcuxClBuffer.h:105
#define MCUXCLELS_CMAC_OUT_SIZE
Size of CMAC output: 128 bit (16 bytes).
Definition mcuxClEls_Cmac.h:93
#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_KEYPROPERTY_CMAC_TRUE
This value of mcuxClEls_KeyProp_t.ucmac indicates that the key can be used for CMAC.
Definition mcuxClEls_Types.h:142
#define MCUXCLELS_KEYPROPERTY_KEY_SIZE_128
This value of mcuxClEls_KeyProp_t.ksize indicates a 128 bit key.
Definition mcuxClEls_Types.h:121
#define MCUXCLELS_KEYPROPERTY_ACTIVE_TRUE
This value of mcuxClEls_KeyProp_t.kactv indicates that the slot contains an active key.
Definition mcuxClEls_Types.h:126
#define MCUXCLKEY_STATUS_OK
Key operation successful.
Definition mcuxClKey_Constants.h:59
mcuxClKey_Status_t mcuxClKey_flush(mcuxClSession_Handle_t pSession, mcuxClKey_Handle_t key)
Flush key from destination which can be a key slot of coprocessor or memory buffer.
mcuxClKey_Descriptor_t *const mcuxClKey_Handle_t
Key handle type.
Definition mcuxClKey_Types.h:108
#define MCUXCLMAC_STATUS_OK
Blocking operation finished successfully.
Definition mcuxClMac_Constants.h:55
mcuxClMac_Status_t mcuxClMac_compute(mcuxClSession_Handle_t session, mcuxClKey_Handle_t key, mcuxClMac_Mode_t mode, mcuxCl_InputBuffer_t pIn, uint32_t inLength, mcuxCl_Buffer_t pMac, uint32_t *const pMacLength)
One-shot message authentication code (MAC) computation function.
static mcuxClMac_Mode_t mcuxClMac_Mode_CMAC
CMAC mode.
Definition mcuxClMacModes_Modes.h:65
mcuxClSession_Descriptor_t *const mcuxClSession_Handle_t
Type for mcuxClSession Handle.
Definition mcuxClSession_Types.h:133
#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
Type for ELS key store key properties.
Definition mcuxClEls_Types.h:246
uint32_t ksize
Key size.
Definition mcuxClEls_Types.h:257
struct mcuxClEls_KeyProp_t::@027006156274216044314047241254162244310261250270 bits
Access mcuxClEls_KeyProp_t bit-wise.
struct mcuxClEls_KeyProp_t::@275171221237375205140351133234315312172366173335 word
Access mcuxClEls_KeyProp_t word-wise.
uint32_t ucmac
Usage permission for CMAC.
Definition mcuxClEls_Types.h:272
uint32_t kactv
Status flag to indicate whether the key slot contains an active key or not.
Definition mcuxClEls_Types.h:260
uint32_t value
Accesses the bit field as a full word; initialize with a combination of constants from MCUXCLELS_KEYP...
Definition mcuxClEls_Types.h:249