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

mcuxClMacModes example application

mcuxClMacModes example application

/*--------------------------------------------------------------------------*/
/* Copyright 2023-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 <mcuxClResource.h>
#include <mcuxClKey.h>
#include <mcuxClAes.h> // Interface to AES-related definitions and types
#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 <mcuxClBuffer.h>
#include <mcuxClCore_Examples.h>
#include <mcuxClExample_OS.h>
#include <mcuxClExample_Session_Helper.h>
#include <mcuxClExample_RNG_Helper.h>
#include <platform_specific_headers.h> // needed for DMA interrupts
/************************************************************************************/
/* Reference data to process and check results */
/************************************************************************************/
static const uint8_t keyData[16] = {
0x2bu, 0x7eu, 0x15u, 0x16u, 0x28u, 0xaeu, 0xd2u, 0xa6u,
0xabu, 0xf7u, 0x15u, 0x88u, 0x09u, 0xcfu, 0x4fu, 0x3cu
};
/************************************************************************************/
/* Helper code to synchronize example flow with nonBlocking background computation */
/************************************************************************************/
#define MCUXCLMAC_STATUS_CALLBACK_NOT_EXECUTED ((uint32_t) 0xDEADBEEFu)
/* This variable is used to keep track of callbacks triggered by the non-blocking API. */
static volatile uint32_t macStatus_nonBlockingCallback = MCUXCLMAC_STATUS_CALLBACK_NOT_EXECUTED;
/* This function is called after the nonBlocking operation has finished */
static void user_callback(uint32_t status, void * data)
{
(void)data;
macStatus_nonBlockingCallback = status;
}
#define MCUXCLMAC_FLAG_DMA_INTERRUPT_NOT_TRIGGERED ((uint32_t) 0xDEADBEEFu)
/* This variable is a flag to notify the caller that an interrupt happened.
It will contain the DMA channel ID of the respective channel that had an interrupt. */
static volatile uint32_t flag_interruptNumber = MCUXCLMAC_FLAG_DMA_INTERRUPT_NOT_TRIGGERED;
/* Interrupt configuration code */
/**************************************************************************/
/* Global resource context to handle status and session for the available HW resources.
It shall be global and therefor only allocated once, for all sessions.
Note that the examples create a "static" version of this global context, which is done
solely to make sure examples are self-contained and do not conflict with each other. */
static uint32_t resourceContext[MCUXCLRESOURCE_CONTEXT_SIZE/sizeof(uint32_t)];
static mcuxClResource_Context_t * resourceCtxHandle = (mcuxClResource_Context_t *) &resourceContext;
/* This is the Interrupt handler for DMA done and error interrupts on the input channel.
This function only sets the interrupt number as a global flag, the actual handler
mcuxClResource_handle_interrupt needs to be called afterwards to wrap-up the CLib operation. */
static void handleDmaInterrupt_channel0(void)
{
MCUX_CSSL_ANALYSIS_START_PATTERN_SFR_ACCESS()
/* Clear DMA interrupt request status, W1C. Needed for DONE interrupts. */
DMA0->CH[0].CH_INT = 1U;
/* Clear the DMA error interrupt request status, W1C. Needed for ERROR interrupts. */
uint32_t chCsr = DMA0->CH[0].CH_CSR;
/* 1. Unset the DONE bit, to not accidentally perform a W1C. CLib needs this bit for internal checks. */
chCsr &= ~((uint32_t)DMA_CH_CSR_DONE_MASK);
/* 2. Clear the EEI bit. */
chCsr &= ~((uint32_t)DMA_CH_CSR_EEI_MASK);
/* 3. Write to CH_CSR */
DMA0->CH[0].CH_CSR = chCsr;
MCUX_CSSL_ANALYSIS_STOP_PATTERN_SFR_ACCESS()
flag_interruptNumber = GET_DMA_CHX_IRQ_NUMBER(0U);
}
/* Initialize (install, enable) the interrupts */
static void interruptInit(void)
{
/* Enable interrupts for the input channel */
mcuxClExample_OS_Interrupt_Callback_Install(handleDmaInterrupt_channel0, GET_DMA_CHX_IRQ_NUMBER(0U));
/* Enable the interrupts in the controller */
mcuxClExample_OS_Interrupt_Enable(GET_DMA_CHX_IRQ_NUMBER(0U));
}
/* Uninitialize (disable) the interrupts */
static void interruptUninit(void)
{
/* Disable the interrupts in the controller */
mcuxClExample_OS_Interrupt_Disable(GET_DMA_CHX_IRQ_NUMBER(0U));
}
/**************************************************************************/
/* Example for non-blocking CMAC compute */
/* */
/* The example shows which functions need to be called to configure the */
/* non-blocking flow. Its important that the interrupt is triggered on */
/* input channel. To show the non-blocking interrupt flow a polling-loop */
/* is used to wait for the user-callback to be triggered by an interrupt. */
/**************************************************************************/
MCUXCLEXAMPLE_FUNCTION(mcuxClMacModes_Cmac_Aes128_Compute_Dma_NonBlocking_example)
{
/**************************************************************************/
/* General Preparation */
/**************************************************************************/
/* Note: All DMA buffer needs to be on the stack because DMA cannot access ROM */
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
};
const uint8_t cmacReference[16] = {
0xdfu, 0xa6u, 0x67u, 0x47u, 0xdeu, 0x9au, 0xe6u, 0x30u,
0x30u, 0xcau, 0x32u, 0x61u, 0x14u, 0x97u, 0xc8u, 0x27u
};
mcuxClSession_Descriptor_t sessionDesc;
mcuxClSession_Handle_t session = &sessionDesc;
/* Allocate and initialize session */
MCUXCLEXAMPLE_ALLOCATE_AND_INITIALIZE_SESSION_NONBLOCKING(session, MCUXCLEXAMPLE_MAX_WA(MCUXCLMAC_COMPUTE_CPU_WA_BUFFER_SIZE, MCUXCLRANDOM_NCINIT_WACPU_SIZE), 0u);
/* Initialize the PRNG */
MCUXCLEXAMPLE_INITIALIZE_PRNG(session);
/* Initialize the key */
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: */ keyData,
/* uint32_t keyDataLength: */ sizeof(keyData))
);
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/**************************************************************************/
/* Non-Blocking Preparation */
/**************************************************************************/
/* Enable DMA interrupt and set callback */
interruptInit();
/* Configure the DMA channels that should be used. For MAC operations, the two channels will never be used at the same time.
* Use DMA channel 0 for both HW input and output operations. */
mcuxClSession_Channels_t dmaChannels = {
.input = (mcuxClSession_Channel_t) 0u,
.output = (mcuxClSession_Channel_t) 0u
};
/* Set DMA channels and user callback function */
/* mcuxClSession_Handle_t session: */ session,
/* mcuxClSession_Channels_t dmaChannels, */ dmaChannels,
/* mcuxClSession_Callback_t pUserCallback, */ user_callback,
MCUX_CSSL_ANALYSIS_START_SUPPRESS_NULL_POINTER_CONSTANT("NULL is used in code")
/* void * pUserData */ NULL)
MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_NULL_POINTER_CONSTANT()
);
/* Initialize resource context and add it to the session */
if(!mcuxClExample_Session_InitAndSetResourceCtx(session, resourceCtxHandle))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/**************************************************************************/
/* MAC Computation */
/**************************************************************************/
uint32_t macSize = 0u;
uint8_t macData[sizeof(cmacReference)];
MCUXCLBUFFER_INIT_DMA_RO(dataBuf, session, data, sizeof(data));
MCUXCLBUFFER_INIT_DMA(macDataBuf, session, macData, sizeof(macData));
/* mcuxClSession_Handle_t session: */ session,
/* const mcuxClKey_Handle_t key: */ key,
/* const mcuxClMac_Mode_t mode: */ mcuxClMac_Mode_CMAC_NonBlocking,
/* mcuxCl_InputBuffer_t pIn: */ dataBuf,
/* uint32_t inLength: */ sizeof(data),
/* mcuxCl_Buffer_t pMac: */ macDataBuf,
/* uint32_t macLength: */ &macSize)
);
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
{
/* A non-blocking job was started. Wait for the interrupt */
while(MCUXCLMAC_FLAG_DMA_INTERRUPT_NOT_TRIGGERED == flag_interruptNumber) {};
/* Call the resource interrupt handler to finish the non-blocking operation.
* On normal operation flow, this will trigger the user_callback function at the end,
* which sets macStatus_nonBlockingCallback to the status code of the Mac operation.
* On error, mcuxClResource_handle_interrupt returns an ERROR code without triggering
* the user_callback. */
MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(rhi_status, rhi_token, mcuxClResource_handle_interrupt(resourceCtxHandle, flag_interruptNumber));
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
if(MCUXCLMAC_STATUS_JOB_COMPLETED != macStatus_nonBlockingCallback)
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/* Reset polling loop condition and the status code */
flag_interruptNumber = MCUXCLMAC_FLAG_DMA_INTERRUPT_NOT_TRIGGERED;
macStatus_nonBlockingCallback = MCUXCLMAC_STATUS_CALLBACK_NOT_EXECUTED;
}
/**************************************************************************/
/* Destroy the current session and clean-up */
/**************************************************************************/
if(!mcuxClExample_Session_Clean(session))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/* Disable the interrupts */
interruptUninit();
/**************************************************************************/
/* Verification */
/**************************************************************************/
if (macSize != sizeof(cmacReference))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
MCUX_CSSL_ANALYSIS_START_SUPPRESS_ALREADY_INITIALIZED("Initialized by MCUXCLBUFFER_INIT_DMA")
if (!mcuxClCore_assertEqual(macData, cmacReference, sizeof(cmacReference)))
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 mcuxClResource component.
Top-level include file for the mcuxClSession component.
Provides the API for the CSSL flow protection mechanism.
#define MCUXCLRESOURCE_CONTEXT_SIZE
Size (in bytes) of mcuxClResource context.
Definition mcuxClResource_MemoryConsumption.h:24
static const mcuxClKey_Type_t mcuxClKey_Type_Aes128
Key type pointer for AES-128 based keys.
Definition mcuxClAes_KeyTypes.h:51
#define MCUXCLBUFFER_INIT_DMA(name, info, ptr, size)
Initialize an input/output buffer (mcuxCl_Buffer_t) with DMA handling.
Definition mcuxClBuffer.h:157
#define MCUXCLBUFFER_INIT_DMA_RO(name, info, ptr, size)
Initialize an input buffer (mcuxCl_InputBuffer_t) with DMA handling.
Definition mcuxClBuffer.h:120
#define MCUXCLKEY_STATUS_OK
Key operation successful.
Definition mcuxClKey_Constants.h:40
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_Descriptor_t *const mcuxClKey_Handle_t
Key handle type.
Definition mcuxClKey_Types.h:91
#define MCUXCLMAC_STATUS_JOB_COMPLETED
Non-blocking operation finished successfully.
Definition mcuxClMac_Constants.h:39
#define MCUXCLMAC_STATUS_OK
Blocking operation finished successfully.
Definition mcuxClMac_Constants.h:36
#define MCUXCLMAC_STATUS_JOB_STARTED
Non-blocking operation started successfully.
Definition mcuxClMac_Constants.h:38
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_NonBlocking
AES-CMAC mode, non-blocking API, using the DMA for I/O operations.
Definition mcuxClMacModes_Modes.h:71
mcuxClResource_Status_t mcuxClResource_handle_interrupt(const mcuxClResource_Context_t *pResourceCtx, mcuxClResource_Interrupt_t interrupt)
Resource interrupt handler.
struct mcuxClResource_Context mcuxClResource_Context_t
Resource context type.
Definition mcuxClResource_Types.h:47
#define MCUXCLRESOURCE_STATUS_OK
Resource operation successful.
Definition mcuxClResource_Types.h:82
mcuxClSession_Status_t mcuxClSession_configure_job(mcuxClSession_Handle_t session, mcuxClSession_Channels_t dmaChannels, mcuxClSession_Callback_t pUserCallback, void *pUserData)
Configure the parameters for non-blocking operations (jobs) in this session.
#define MCUXCLSESSION_STATUS_OK
Session operation successful.
Definition mcuxClSession_Types.h:39
mcuxClSession_Descriptor_t *const mcuxClSession_Handle_t
Type for mcuxClSession Handle.
Definition mcuxClSession_Types.h:98
uint16_t mcuxClSession_Channel_t
Session channel type.
Definition mcuxClSession_Types.h:105
#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
Session channels type.
Definition mcuxClSession_Types.h:113