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

Example for the mcuxClMacModes component.

Example for the mcuxClMacModes component

/*--------------------------------------------------------------------------*/
/* Copyright 2024-2025 NXP */
/* */
/* NXP Confidential and Proprietary. 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 <mcuxClSession.h>
#include <mcuxClKey.h>
#include <mcuxClAes.h>
#include <mcuxClBuffer.h>
#include <mcuxClMac.h> // Interface to the entire mcuxClMac component
#include <mcuxClMacModes.h> // Interface to the entire mcuxClMacModes component
#include <mcuxClCore_FunctionIdentifiers.h> // Code flow protection
#include <mcuxClCore_Examples.h>
#include <mcuxClExample_Session_Helper.h>
#include <mcuxClExample_RNG_Helper.h>
static const uint8_t data[40] = {
0x6BU, 0xC1U, 0xBEU, 0xE2U, 0x2EU, 0x40U, 0x9FU, 0x96U,
0xE9U, 0x3DU, 0x7EU, 0x11U, 0x73U, 0x93U, 0x17U, 0x2AU,
0xAEU, 0x2DU, 0x8AU, 0x57U, 0x1EU, 0x03U, 0xACU, 0x9CU,
0x9EU, 0xB7U, 0x6FU, 0xACU, 0x45U, 0xAFU, 0x8EU, 0x51U,
0x30U, 0xC8U, 0x1CU, 0x46U, 0xA3U, 0x5CU, 0xE4U, 0x11U
};
static const uint8_t keyDataAes128[16] = {
0x2BU, 0x7EU, 0x15U, 0x16U, 0x28U, 0xAEU, 0xD2U, 0xA6U,
0xABU, 0xF7U, 0x15U, 0x88U, 0x09U, 0xCFU, 0x4FU, 0x3CU
};
static const uint8_t cmacReferenceAes128[16] = {
0xDFU, 0xA6U, 0x67U, 0x47U, 0xDEU, 0x9AU, 0xE6U, 0x30U,
0x30U, 0xCAU, 0x32U, 0x61U, 0x14U, 0x97U, 0xC8U, 0x27U
};
MCUXCLEXAMPLE_FUNCTION(mcuxClMacModes_Cmac_Aes128_Compute_PreloadedKey_example)
{
/**************************************************************************/
/* Preparation */
/**************************************************************************/
mcuxClSession_Descriptor_t sessionDesc;
mcuxClSession_Handle_t session = &sessionDesc;
/* Allocate and initialize session */
#define MCUXCLMACMODES_EXAMPLE_MAX_CPU_WA_SIZE MCUXCLEXAMPLE_MAX_WA(MCUXCLKEY_LOADCOPRO_CPU_WA_SIZE, \
MCUXCLEXAMPLE_MAX_WA(MCUXCLMAC_COMPUTE_CPU_WA_BUFFER_SIZE, \
MCUXCLRANDOM_NCINIT_WACPU_SIZE))
MCUXCLEXAMPLE_ALLOCATE_AND_INITIALIZE_SESSION(session, MCUXCLMACMODES_EXAMPLE_MAX_CPU_WA_SIZE, 0U);
/* Initialize the PRNG */
MCUXCLEXAMPLE_INITIALIZE_PRNG(session);
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()
/* mcuxClSession_Handle_t session */ session,
/* mcuxClKey_Handle_t key */ key,
/* mcuxClKey_Type_t type */ mcuxClKey_Type_Aes128,
/* uint8_t * pKeyData */ keyDataAes128,
/* uint32_t keyDataLength */ sizeof(keyDataAes128))
);
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/**************************************************************************/
/* Key Load */
/* This preloads the key into an SGI key register. */
/* The key will stay in the SGI until it is explicitly flushed. */
/**************************************************************************/
/* mcuxClSession_Handle_t session: */ session,
/* mcuxClKey_Handle_t key: */ key,
/* uint32_t options: */ MCUXCLKEY_LOADOPTION_SLOT_SGI_KEY_2)
);
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/**************************************************************************/
/* MAC Computation */
/**************************************************************************/
uint32_t macSize = 0U;
uint8_t macData[sizeof(cmacReferenceAes128)];
MCUXCLBUFFER_INIT_RO(dataBuf, session, data, sizeof(data));
MCUXCLBUFFER_INIT(macDataBuf, session, macData, sizeof(macData));
/* mcuxClSession_Handle_t session: */ session,
/* const mcuxClKey_Handle_t key: */ key,
/* const mcuxClMac_Mode_t mode: */ mcuxClMac_Mode_CMAC,
/* mcuxCl_InputBuffer_t pIn: */ dataBuf,
/* uint32_t inLength: */ sizeof(data),
/* mcuxCl_Buffer_t pMac: */ macDataBuf,
/* uint32_t * const pMacLength: */ &macSize)
);
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/**************************************************************************/
/* Key Flush */
/**************************************************************************/
/* mcuxClSession_Handle_t session: */ session,
/* mcuxClKey_Handle_t key: */ key)
);
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/**************************************************************************/
/* Destroy the current session */
/**************************************************************************/
if(!mcuxClExample_Session_Clean(session))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/**************************************************************************/
/* Verification */
/**************************************************************************/
if (macSize != sizeof(cmacReferenceAes128))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
MCUX_CSSL_ANALYSIS_START_SUPPRESS_ALREADY_INITIALIZED("Initialized by MCUXCLBUFFER_INIT")
if (!mcuxClCore_assertEqual(macData, cmacReferenceAes128, sizeof(cmacReferenceAes128)))
MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_ALREADY_INITIALIZED()
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
return MCUXCLEXAMPLE_STATUS_OK;
}
Top-level include file for the mcuxClAes component.
Top-level include file for the mcuxClBuffer component.
Definition of function identifiers for the flow protection mechanism.
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.
static const mcuxClKey_Type_t mcuxClKey_Type_Aes128
Key type pointer for AES-128 based keys.
Definition mcuxClAes_KeyTypes.h:51
#define MCUXCLBUFFER_INIT(name, info, ptr, size)
Initialize an input/output buffer (mcuxCl_Buffer_t).
Definition mcuxClBuffer.h:67
#define MCUXCLBUFFER_INIT_RO(name, info, ptr, size)
Initialize an input buffer (mcuxCl_InputBuffer_t) with plain CPU handling.
Definition mcuxClBuffer.h:84
#define MCUXCLKEY_STATUS_OK
Key operation successful.
Definition mcuxClKey_Constants.h:40
#define MCUXCLKEY_LOADOPTION_SLOT_SGI_KEY_2
SGI key slot 2.
Definition mcuxClKey_Constants.h:174
mcuxClKey_Status_t mcuxClKey_init(mcuxClSession_Handle_t session, mcuxClKey_Handle_t key, mcuxClKey_Type_t type, const uint8_t *pKeyData, uint32_t keyDataLength)
Initializes a key handle.
mcuxClKey_Status_t mcuxClKey_flush(mcuxClSession_Handle_t session, mcuxClKey_Handle_t key)
Flush key from destination which can be a key slot of coprocessor or memory buffer.
mcuxClKey_Status_t mcuxClKey_loadCopro(mcuxClSession_Handle_t session, mcuxClKey_Handle_t key, uint32_t loadOptions)
Load key into destination key slot of a coprocessor.
mcuxClKey_Descriptor_t *const mcuxClKey_Handle_t
Key handle type.
Definition mcuxClKey_Types.h:91
#define MCUXCLMAC_STATUS_OK
Blocking operation finished successfully.
Definition mcuxClMac_Constants.h:36
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
AES-CMAC mode.
Definition mcuxClMacModes_Modes.h:48
mcuxClSession_Descriptor_t *const mcuxClSession_Handle_t
Type for mcuxClSession Handle.
Definition mcuxClSession_Types.h:98
#define MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(...)
Call a flow protected function and check the protection token.
Definition mcuxCsslFlowProtection.h:623
#define MCUX_CSSL_FP_FUNCTION_CALLED(...)
Expectation of a called function.
Definition mcuxCsslFlowProtection.h:777
#define MCUX_CSSL_FP_FUNCTION_CALL_END(...)
End a function call section started by MCUX_CSSL_FP_FUNCTION_CALL_BEGIN.
Definition mcuxCsslFlowProtection.h:658