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

Example for the mcuxClCipherModes component.

Example for the mcuxClCipherModes component

/*--------------------------------------------------------------------------*/
/* Copyright 2020-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 <mcuxClToolchain.h>
#include <mcuxClSession.h>
#include <mcuxClKey.h>
#include <mcuxClAes.h> // Interface to AES-related definitions and types
#include <mcuxClCipher.h> // Interface to the entire mcuxClCipher component
#include <mcuxClCipherModes.h> // Interface to the entire mcuxClCipherModes component
#include <mcuxClCore_FunctionIdentifiers.h> // Code flow protection
#include <mcuxClCore_Examples.h>
#include <mcuxClBuffer.h>
#include <mcuxClExample_Session_Helper.h>
#include <mcuxClExample_RNG_Helper.h>
static const uint8_t plain[62] = {
0x61u, 0x62u, 0x63u, 0x64u, 0x65u, 0x66u, 0x67u, 0x68u,
0x69u, 0x6Au, 0x6Bu, 0x6Cu, 0x6Du, 0x6Eu, 0x6Fu, 0x70u,
0x62u, 0x63u, 0x64u, 0x65u, 0x66u, 0x67u, 0x68u, 0x69u,
0x6Au, 0x6Bu, 0x6Cu, 0x6Du, 0x6Eu, 0x6Fu, 0x70u, 0x71u,
0x63u, 0x64u, 0x65u, 0x66u, 0x67u, 0x68u, 0x69u, 0x6Au,
0x6Bu, 0x6Cu, 0x6Du, 0x6Eu, 0x6Fu, 0x70u, 0x71u, 0x72u,
0x64u, 0x65u, 0x66u, 0x67u, 0x68u, 0x69u, 0x6Au, 0x6Bu,
0x6Cu, 0x6Du, 0x6Eu, 0x6Fu, 0x70u, 0x71u
};
/* CBC encrypted data */
static const uint8_t encryptedRef[64] = {
0xe6u, 0xd4u, 0xe7u, 0xdau, 0x9eu, 0x63u, 0xc6u, 0x67u,
0xfbu, 0xa0u, 0x43u, 0x2au, 0xadu, 0x45u, 0x4bu, 0x7du,
0xf2u, 0xb7u, 0x91u, 0x4eu, 0x89u, 0xe1u, 0x07u, 0xe1u,
0xa0u, 0x0eu, 0xe2u, 0x52u, 0xccu, 0xbau, 0xbbu, 0x1fu,
0x69u, 0x4au, 0x00u, 0xe4u, 0x2eu, 0x89u, 0xfbu, 0x43u,
0x79u, 0x8bu, 0x28u, 0x38u, 0x6bu, 0x7cu, 0xecu, 0x7fu,
0xd2u, 0x83u, 0xd6u, 0xabu, 0x01u, 0x38u, 0xe4u, 0xd0u,
0xc8u, 0x41u, 0x99u, 0x55u, 0x5au, 0x1bu, 0xccu, 0x51u
};
/* Decrypted zero padded data */
static const uint8_t decryptedRef[64] = {
0x61u, 0x62u, 0x63u, 0x64u, 0x65u, 0x66u, 0x67u, 0x68u,
0x69u, 0x6Au, 0x6Bu, 0x6Cu, 0x6Du, 0x6Eu, 0x6Fu, 0x70u,
0x62u, 0x63u, 0x64u, 0x65u, 0x66u, 0x67u, 0x68u, 0x69u,
0x6Au, 0x6Bu, 0x6Cu, 0x6Du, 0x6Eu, 0x6Fu, 0x70u, 0x71u,
0x63u, 0x64u, 0x65u, 0x66u, 0x67u, 0x68u, 0x69u, 0x6Au,
0x6Bu, 0x6Cu, 0x6Du, 0x6Eu, 0x6Fu, 0x70u, 0x71u, 0x72u,
0x64u, 0x65u, 0x66u, 0x67u, 0x68u, 0x69u, 0x6Au, 0x6Bu,
0x6Cu, 0x6Du, 0x6Eu, 0x6Fu, 0x70u, 0x71u, 0x00u, 0x00u
};
static const uint8_t iv[16] = {
0x7Au, 0x79u, 0x78u, 0x77u, 0x76u, 0x75u, 0x74u, 0x73u,
0x72u, 0x71u, 0x70u, 0x6Fu, 0x6Eu, 0x6Du, 0x6Cu, 0x6Bu,
};
static const uint8_t keyBytes[16] = {
0x6Bu, 0x6Cu, 0x6Du, 0x6Eu, 0x6Fu, 0x70u, 0x71u, 0x72u,
0x73u, 0x74u, 0x75u, 0x76u, 0x77u, 0x78u, 0x79u, 0x7Au,
};
MCUXCLEXAMPLE_FUNCTION(mcuxClCipherModes_Cbc_Aes128_Multipart_PaddingZero_example)
{
/**************************************************************************/
/* Preparation */
/**************************************************************************/
mcuxClSession_Descriptor_t sessionDesc;
mcuxClSession_Handle_t session = &sessionDesc;
#define MCUXCLCIPHERMODES_CPU_WA MCUXCLCIPHER_AES_PROCESS_CPU_WA_BUFFER_SIZE
#define MCUXCLCIPHERMODES_CONTEXT_SIZE MCUXCLCIPHER_AES_CONTEXT_SIZE
/* Allocate and initialize session */
MCUXCLEXAMPLE_ALLOCATE_AND_INITIALIZE_SESSION(session, MCUXCLEXAMPLE_MAX_WA(MCUXCLCIPHERMODES_CPU_WA, MCUXCLRANDOM_NCINIT_WACPU_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: */ keyBytes,
/* uint32_t keyDataLength: */ sizeof(keyBytes))
);
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/**************************************************************************/
/* Encryption */
/**************************************************************************/
uint32_t outLength = 0u;
uint32_t encryptedSize = 0u;
uint8_t encryptedData[sizeof(encryptedRef)];
/* Create a buffer for the context */
ALIGNED uint8_t ctxBuf[MCUXCLCIPHERMODES_CONTEXT_SIZE];
MCUX_CSSL_ANALYSIS_START_PATTERN_REINTERPRET_MEMORY_OF_OPAQUE_TYPES()
MCUX_CSSL_ANALYSIS_STOP_PATTERN_REINTERPRET_MEMORY_OF_OPAQUE_TYPES()
MCUXCLBUFFER_INIT_RO(ivBuf, session, iv, sizeof(iv));
/* mcuxClSession_Handle_t session: */ session,
/* mcuxClCipher_Context_t * const pContext:*/ ctx,
/* const mcuxClKey_Handle_t key: */ key,
/* mcuxClCipher_Mode_t mode: */ mcuxClCipher_Mode_AES_CBC_PaddingISO9797_1_Method1,
/* mcuxCl_InputBuffer_t pIv: */ ivBuf,
/* uint32_t ivLength: */ sizeof(iv))
);
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
MCUXCLBUFFER_INIT_RO(plainBuf, session, plain, sizeof(plain));
MCUXCLBUFFER_INIT(encryptedDataBuf, session, encryptedData, sizeof(encryptedData));
/* mcuxClSession_Handle_t session: */ session,
MCUX_CSSL_ANALYSIS_START_SUPPRESS_ALREADY_INITIALIZED("Initialized by mcuxClCipher_init_encrypt")
/* mcuxClCipher_Context_t * const pContext:*/ ctx,
MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_ALREADY_INITIALIZED()
/* mcuxCl_InputBuffer_t pIn: */ plainBuf,
/* uint32_t inLength: */ sizeof(plain) / 2u,
/* mcuxCl_Buffer_t pOut: */ encryptedDataBuf,
/* uint32_t * const outLength: */ &outLength)
);
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
MCUX_CSSL_ANALYSIS_START_SUPPRESS_INTEGER_OVERFLOW("Calculation does not overflow")
encryptedSize += outLength;
MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_INTEGER_OVERFLOW()
MCUXCLBUFFER_UPDATE(plainBuf, sizeof(plain)/2u);
MCUXCLBUFFER_UPDATE(encryptedDataBuf, encryptedSize);
/* mcuxClSession_Handle_t session: */ session,
MCUX_CSSL_ANALYSIS_START_SUPPRESS_ALREADY_INITIALIZED("Initialized by mcuxClCipher_init_encrypt")
/* mcuxClCipher_Context_t * const pContext:*/ ctx,
MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_ALREADY_INITIALIZED()
/* mcuxCl_InputBuffer_t pIn: */ plainBuf,
/* uint32_t inLength: */ sizeof(plain) - sizeof(plain) / 2u,
/* mcuxCl_Buffer_t pOut: */ encryptedDataBuf,
/* uint32_t * const outLength: */ &outLength)
);
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
MCUX_CSSL_ANALYSIS_START_SUPPRESS_INTEGER_OVERFLOW("Calculation does not overflow")
encryptedSize += outLength;
MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_INTEGER_OVERFLOW()
/* Using MCUXCLBUFFER_SET instead of MCUXCLBUFFER_UPDATE is needed to properly advance the buffer to the correct offset */
MCUXCLBUFFER_SET(encryptedDataBuf, &encryptedData[encryptedSize], sizeof(encryptedData) /* unused */);
/* mcuxClSession_Handle_t session: */ session,
MCUX_CSSL_ANALYSIS_START_SUPPRESS_ALREADY_INITIALIZED("Initialized by mcuxClCipher_init_encrypt")
/* mcuxClCipher_Context_t * const pContext:*/ ctx,
MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_ALREADY_INITIALIZED()
/* mcuxCl_Buffer_t pOut: */ encryptedDataBuf,
/* uint32_t * const outLength: */ &outLength)
);
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
MCUX_CSSL_ANALYSIS_START_SUPPRESS_INTEGER_OVERFLOW("Calculation does not overflow")
encryptedSize += outLength;
MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_INTEGER_OVERFLOW()
/**************************************************************************/
/* Decryption */
/**************************************************************************/
uint32_t decryptedSize = 0u;
uint8_t decryptedData[sizeof(decryptedRef)];
/* mcuxClSession_Handle_t session: */ session,
/* mcuxClCipher_Context_t * const pContext:*/ ctx,
/* const mcuxClKey_Handle_t key: */ key,
/* mcuxClCipher_Mode_t mode: */ mcuxClCipher_Mode_AES_CBC_PaddingISO9797_1_Method1,
/* mcuxCl_InputBuffer_t pIv: */ ivBuf,
/* uint32_t ivLength: */ sizeof(iv))
);
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/* Process again from the beginning of encryptedDataBuf */
MCUXCLBUFFER_SET(encryptedDataBuf, encryptedData, sizeof(encryptedData) /* unused */);
MCUXCLBUFFER_INIT(decryptedDataBuf, session, decryptedData, sizeof(decryptedData));
/* mcuxClSession_Handle_t session: */ session,
MCUX_CSSL_ANALYSIS_START_SUPPRESS_ALREADY_INITIALIZED("Initialized by mcuxClCipher_init_decrypt")
/* mcuxClCipher_Context_t * const pContext:*/ ctx,
MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_ALREADY_INITIALIZED()
/* mcuxCl_InputBuffer_t pIn: */ (mcuxCl_InputBuffer_t) encryptedDataBuf,
/* uint32_t inLength: */ encryptedSize / 3u,
/* mcuxCl_Buffer_t pOut: */ decryptedDataBuf,
/* uint32_t * const outLength: */ &outLength)
);
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
MCUX_CSSL_ANALYSIS_START_SUPPRESS_INTEGER_OVERFLOW("Calculation does not overflow")
decryptedSize += outLength;
MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_INTEGER_OVERFLOW()
MCUXCLBUFFER_UPDATE(encryptedDataBuf, encryptedSize / 3u);
MCUXCLBUFFER_UPDATE(decryptedDataBuf, decryptedSize);
/* mcuxClSession_Handle_t session: */ session,
MCUX_CSSL_ANALYSIS_START_SUPPRESS_ALREADY_INITIALIZED("Initialized by mcuxClCipher_init_decrypt")
/* mcuxClCipher_Context_t * const pContext:*/ ctx,
MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_ALREADY_INITIALIZED()
/* mcuxCl_InputBuffer_t pIn: */ (mcuxCl_InputBuffer_t) encryptedDataBuf,
MCUX_CSSL_ANALYSIS_START_SUPPRESS_INTEGER_OVERFLOW("Calculation does not wrap")
/* uint32_t inLength: */ encryptedSize - encryptedSize / 3u,
MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_INTEGER_OVERFLOW()
/* mcuxCl_Buffer_t pOut: */ decryptedDataBuf,
/* uint32_t * const outLength: */ &outLength)
);
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
MCUX_CSSL_ANALYSIS_START_SUPPRESS_INTEGER_OVERFLOW("Calculation does not overflow")
decryptedSize += outLength;
MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_INTEGER_OVERFLOW()
/* Using MCUXCLBUFFER_SET instead of MCUXCLBUFFER_UPDATE is needed to properly advance the buffer to the correct offset */
MCUXCLBUFFER_SET(decryptedDataBuf, &decryptedData[decryptedSize], sizeof(decryptedData) /* unused */);
/* mcuxClSession_Handle_t session: */ session,
MCUX_CSSL_ANALYSIS_START_SUPPRESS_ALREADY_INITIALIZED("Initialized by mcuxClCipher_init_decrypt")
/* mcuxClCipher_Context_t * const pContext:*/ ctx,
MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_ALREADY_INITIALIZED()
/* mcuxCl_Buffer_t pOut: */ decryptedDataBuf,
/* uint32_t * const outLength: */ &outLength)
);
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
MCUX_CSSL_ANALYSIS_START_SUPPRESS_INTEGER_OVERFLOW("Calculation does not overflow")
decryptedSize += outLength;
MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_INTEGER_OVERFLOW()
/**************************************************************************/
/* Destroy the current session */
/**************************************************************************/
if(!mcuxClExample_Session_Clean(session))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/**************************************************************************/
/* Verification */
/**************************************************************************/
if(sizeof(encryptedRef) != encryptedSize)
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
if(!mcuxClCore_assertEqual(encryptedRef, encryptedData, sizeof(encryptedRef)))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
if(sizeof(decryptedRef) != decryptedSize)
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
if(!mcuxClCore_assertEqual(decryptedRef, decryptedData, sizeof(decryptedRef)))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
return MCUXCLEXAMPLE_STATUS_OK;
}
Top-level include file for the mcuxClAes component.
Top-level include file for the mcuxClBuffer component.
Top-level include file for the mcuxClCipher component.
Top-level include file for the mcuxClCipherModes component.
Definition of function identifiers for the flow protection mechanism.
Top-level include file for the mcuxClKey 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 MCUXCLBUFFER_UPDATE(name, offset)
Update the buffer pointer with the given offset.
Definition mcuxClBuffer.h:201
#define MCUXCLBUFFER_SET(name, ptr, size)
Update the buffer pointer the a new memory location.
Definition mcuxClBuffer.h:240
const uint8_t * mcuxCl_InputBuffer_t
Input buffer type.
Definition mcuxClBuffer_Pointer.h:38
#define MCUXCLCIPHER_STATUS_OK
Blocking operation finished successfully.
Definition mcuxClCipher_Constants.h:35
mcuxClCipher_Status_t mcuxClCipher_process(mcuxClSession_Handle_t session, mcuxClCipher_Context_t *const pContext, mcuxCl_InputBuffer_t pIn, uint32_t inLength, mcuxCl_Buffer_t pOut, uint32_t *const pOutLength)
Multi-part encryption/decryption processing function.
mcuxClCipher_Status_t mcuxClCipher_init_encrypt(mcuxClSession_Handle_t session, mcuxClCipher_Context_t *const pContext, mcuxClKey_Handle_t key, mcuxClCipher_Mode_t mode, mcuxCl_InputBuffer_t pIv, uint32_t ivLength)
Multi-part encryption initialization function.
mcuxClCipher_Status_t mcuxClCipher_finish(mcuxClSession_Handle_t session, mcuxClCipher_Context_t *const pContext, mcuxCl_Buffer_t pOut, uint32_t *const pOutLength)
Multi-part encryption/decryption finalization function.
mcuxClCipher_Status_t mcuxClCipher_init_decrypt(mcuxClSession_Handle_t session, mcuxClCipher_Context_t *const pContext, mcuxClKey_Handle_t key, mcuxClCipher_Mode_t mode, mcuxCl_InputBuffer_t pIv, uint32_t ivLength)
Multi-part decryption initialization function.
struct mcuxClCipher_Context mcuxClCipher_Context_t
Cipher context type.
Definition mcuxClCipher_Types.h:100
static mcuxClCipher_Mode_t mcuxClCipher_Mode_AES_CBC_PaddingISO9797_1_Method1
AES-CBC mode with ISO/IEC 9797-1 padding method 1.
Definition mcuxClCipherModes_Modes.h:214
#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
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