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

Example for the mcuxClAeadModes component.

Example for the mcuxClAeadModes 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 <mcuxClBuffer.h>
#include <mcuxClCore_Examples.h>
#include <mcuxClExample_Session_Helper.h>
#include <mcuxClSession.h>
#include <mcuxClKey.h>
#include <mcuxClAead.h>
#include <mcuxClCore_FunctionIdentifiers.h> // Code flow protection
#include <mcuxClAes.h>
#include <mcuxClExample_RNG_Helper.h>
//Values from: The Galois/Counter mode of operation (GCM) by David A. McGrew and John Viega
//https://github.com/nsc/SwiftTLS/blob/master/RFCs/gcm-revised-spec.pdf
static const uint8_t plain[] = {
0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U,
0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U
};
static const uint8_t nonce[] = {
0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U,
0x00U, 0x00U, 0x00U, 0x00U
};
static const uint8_t keyBytes[] = {
0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U,
0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U
};
static const uint8_t tagReference[] = {
0xabU, 0x6eU, 0x47U, 0xd4U, 0x2cU, 0xecU, 0x13U, 0xbdU,
0xf5U, 0x3aU, 0x67U, 0xb2U, 0x12U, 0x57U, 0xbdU, 0xdfU
};
static const uint8_t encryptedReference[] = {
0x03U, 0x88U, 0xdaU, 0xceU, 0x60U, 0xb6U, 0xa3U, 0x92U,
0xf3U, 0x28U, 0xc2U, 0xb9U, 0x71U, 0xb2U, 0xfeU, 0x78U
};
MCUXCLEXAMPLE_FUNCTION(mcuxClAeadModes_Gcm_Aes128_Oneshot_example)
{
/**************************************************************************/
/* Preparation */
/**************************************************************************/
mcuxClSession_Descriptor_t sessionDesc;
mcuxClSession_Handle_t session = &sessionDesc;
MCUXCLBUFFER_INIT_RO(plainBuf, session, plain, sizeof(plain));
MCUXCLBUFFER_INIT_RO(adataBuf, session, NULL, 0);
MCUXCLBUFFER_INIT_RO(nonceBuf, session, nonce, sizeof(nonce));
#define maxBufferSize (((MCUXCLAEAD_ENCRYPT_CPU_WA_BUFFER_SIZE > MCUXCLAEAD_DECRYPT_CPU_WA_BUFFER_SIZE) ? \
MCUXCLAEAD_ENCRYPT_CPU_WA_BUFFER_SIZE : MCUXCLAEAD_DECRYPT_CPU_WA_BUFFER_SIZE) + MCUXCLRANDOM_NCINIT_WACPU_SIZE)
/* Allocate and initialize session */
MCUXCLEXAMPLE_ALLOCATE_AND_INITIALIZE_SESSION(session, maxBufferSize, 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;
}
/**************************************************************************/
/* One-shot Encryption */
/**************************************************************************/
uint32_t encryptedOneShotSize = 0U;
uint8_t encryptedOneshotData[sizeof(encryptedReference)];
MCUXCLBUFFER_INIT(encryptedOneshotDataBuf, session, encryptedOneshotData, sizeof(encryptedOneshotData));
uint8_t tagOneshotData[sizeof(tagReference)];
MCUXCLBUFFER_INIT(tagOneshotDataBuf, session, tagOneshotData, sizeof(tagOneshotData));
/* mcuxClSession_Handle_t session: */ session,
/* const mcuxClKey_Handle_t key: */ key,
/* const mcuxClAead_Mode_t * const mode: */ mcuxClAead_Mode_GCM,
/* mcuxCl_InputBuffer_t nonce */ nonceBuf,
/* uint32_t nonceSize, */ sizeof(nonce),
/* mcuxCl_InputBuffer_t in */ plainBuf,
/* uint32_t inSize, */ sizeof(plain),
/* mcuxCl_InputBuffer_t adata */ adataBuf,
/* uint32_t adataSize, */ 0,
/* mcuxCl_Buffer_t out, */ encryptedOneshotDataBuf,
/* uint32_t * const outSize */ &encryptedOneShotSize,
/* mcuxCl_Buffer_t tag, */ tagOneshotDataBuf,
/* const uint32_t tagSize, */ sizeof(tagReference))
);
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/**************************************************************************/
/* One-shot Decryption */
/**************************************************************************/
uint32_t decryptedOneshotSize = 0U;
uint8_t decryptedOneshotData[sizeof(plain)];
MCUXCLBUFFER_INIT(decryptedOneshotDataBuf, session, decryptedOneshotData, sizeof(decryptedOneshotData));
/* mcuxClSession_Handle_t session: */ session,
/* const mcuxClKey_Handle_t key: */ key,
/* const mcuxClAead_Mode_t * const mode: */ mcuxClAead_Mode_GCM,
/* mcuxCl_InputBuffer_t nonce, */ nonceBuf,
/* const uint32_t nonceSize, */ sizeof(nonce),
/* mcuxCl_InputBuffer_t in */ encryptedOneshotDataBuf,
/* uint32_t inSize, */ encryptedOneShotSize,
/* mcuxCl_InputBuffer_t adata */ adataBuf,
/* const uint32_t adataSize, */ 0,
/* mcuxCl_Buffer_t tag, */ tagOneshotDataBuf,
/* const uint32_t tagSize, */ sizeof(tagReference),
/* mcuxCl_Buffer_t out, */ decryptedOneshotDataBuf,
/* uint32_t * const outSize */ &decryptedOneshotSize)
);
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/**************************************************************************/
/* Destroy the current session */
/**************************************************************************/
if(!mcuxClExample_Session_Clean(session))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
/**************************************************************************/
/* Verification */
/**************************************************************************/
MCUX_CSSL_ANALYSIS_START_SUPPRESS_ALREADY_INITIALIZED("Initialized by MCUXCLBUFFER_INIT")
if (!mcuxClCore_assertEqual(encryptedOneshotData, encryptedReference, sizeof(encryptedReference)))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_ALREADY_INITIALIZED()
MCUX_CSSL_ANALYSIS_START_SUPPRESS_ALREADY_INITIALIZED("Initialized by MCUXCLBUFFER_INIT")
if (!mcuxClCore_assertEqual(tagOneshotData, tagReference, sizeof(tagReference)))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_ALREADY_INITIALIZED()
if (sizeof(encryptedReference) != encryptedOneShotSize)
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
if (!mcuxClCore_assertEqual(plain, decryptedOneshotData, sizeof(plain)))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
if (sizeof(plain) != decryptedOneshotSize)
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
return MCUXCLEXAMPLE_STATUS_OK;
}
Top-level include file for the mcuxClAead component.
Top-level include file for the mcuxClAeadModes component.
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 mcuxClSession component.
Provides the API for the CSSL flow protection mechanism.
#define MCUXCLAEAD_STATUS_OK
Aead function returned successfully.
Definition mcuxClAead_Constants.h:32
mcuxClAead_Status_t mcuxClAead_encrypt(mcuxClSession_Handle_t session, mcuxClKey_Handle_t key, mcuxClAead_Mode_t mode, mcuxCl_InputBuffer_t pNonce, uint32_t nonceLength, mcuxCl_InputBuffer_t pIn, uint32_t inLength, mcuxCl_InputBuffer_t pAdata, uint32_t adataLength, mcuxCl_Buffer_t pOut, uint32_t *const pOutLength, mcuxCl_Buffer_t pTag, uint32_t tagLength)
One-shot authenticated encryption function.
mcuxClAead_Status_t mcuxClAead_decrypt(mcuxClSession_Handle_t session, mcuxClKey_Handle_t key, mcuxClAead_Mode_t mode, mcuxCl_InputBuffer_t pNonce, uint32_t nonceLength, mcuxCl_InputBuffer_t pIn, uint32_t inLength, mcuxCl_InputBuffer_t pAdata, uint32_t adataLength, mcuxCl_InputBuffer_t pTag, uint32_t tagLength, mcuxCl_Buffer_t pOut, uint32_t *const pOutLength)
One-shot authenticated decryption function.
static mcuxClAead_Mode_t mcuxClAead_Mode_GCM
GCM mode.
Definition mcuxClAeadModes_Modes.h:68
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
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