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

Example AES-128 ECB encryption using the ELS (CLNS component mcuxClEls)

Example AES-128 ECB encryption using the ELS (CLNS component mcuxClEls)

/*--------------------------------------------------------------------------*/
/* Copyright 2020, 2022-2023 NXP */
/* */
/* NXP Confidential. 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 <mcuxClEls.h> // Interface to the entire mcuxClEls component
#include <mcuxClCore_FunctionIdentifiers.h> // Code flow protection
#include <mcuxClCore_Examples.h>
#include <mcuxClExample_ELS_Helper.h>
static ALIGNED uint8_t const aes128_input[MCUXCLELS_CIPHER_BLOCK_SIZE_AES] = {0x6BU, 0xC1U, 0xBEU, 0xE2U,
0x2EU, 0x40U, 0x9FU, 0x96U,
0xE9U, 0x3DU, 0x7EU, 0x11U,
0x73U, 0x93U, 0x17U, 0x2AU};
static ALIGNED uint8_t const aes128_expected_output[MCUXCLELS_CIPHER_BLOCK_SIZE_AES] = {0x3AU, 0xD7U, 0x7BU, 0xB4U,
0x0DU, 0x7AU, 0x36U, 0x60U,
0xA8U, 0x9EU, 0xCAU, 0xF3U,
0x24U, 0x66U, 0xEFU, 0x97U};
static uint32_t const aes128_key[MCUXCLELS_CIPHER_KEY_SIZE_AES_128 / sizeof(uint32_t)] = {0x16157E2BU, 0xA6D2AE28U, 0x8815F7ABU, 0x3C4FCF09U};
MCUXCLEXAMPLE_FUNCTION(mcuxClEls_Cipher_Aes128_Ecb_Encrypt_example)
{
if(!mcuxClExample_Els_Init(MCUXCLELS_RESET_DO_NOT_CANCEL))
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
mcuxClEls_CipherOption_t cipher_options = {0U}; // Initialize a new configuration for the planned mcuxClEls_Cipher_Async operation.
cipher_options.bits.cphmde = MCUXCLELS_CIPHERPARAM_ALGORITHM_AES_ECB; // Configure the AES block cipher mode of operation to be the ECB (Electronic Codebook) mode.
cipher_options.bits.dcrpt = MCUXCLELS_CIPHER_ENCRYPT; // Configure that the mcuxClEls_Cipher_Async operation shall perform encryption.
cipher_options.bits.extkey = MCUXCLELS_CIPHER_EXTERNAL_KEY; // Configure that an external key should be used.
MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(result, token, mcuxClEls_Cipher_Async( // Perform the encryption.
cipher_options, // Set the prepared configuration.
(mcuxClEls_KeyIndex_t) 0U, // This parameter (keyIdx) is ignored, since an external key is used.
(const uint8_t *) aes128_key, MCUXCLELS_CIPHER_KEY_SIZE_AES_128, // The AES key for the encryption (external key).
aes128_input, sizeof(aes128_input), // The plaintext to encrypt. Note that this plaintext's length is a multiple of the block length, so no padding is required.
NULL, // This parameter (pIV) is ignored, since the ECB mode is used.
aes128_output // Output buffer, which the operation will write the ciphertext to.
));
// mcuxClEls_Cipher_Async is a flow-protected function: Check the protection token and the return value
{
return MCUXCLEXAMPLE_STATUS_ERROR; // Expect that no error occurred, meaning that the mcuxClEls_Cipher_Async operation was started.
}
MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(result, token, mcuxClEls_WaitForOperation(MCUXCLELS_ERROR_FLAGS_CLEAR)); // Wait for the mcuxClEls_Cipher_Async operation to complete.
// mcuxClEls_WaitForOperation is a flow-protected function: Check the protection token and the return value
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
for (size_t i = 0U; i < sizeof(aes128_output); i++)
{
{
return MCUXCLEXAMPLE_STATUS_ERROR; // Expect that the resulting ciphertext matches our expected output
}
}
if(!mcuxClExample_Els_Disable())
{
return MCUXCLEXAMPLE_STATUS_ERROR;
}
return MCUXCLEXAMPLE_STATUS_OK;
}
static ALIGNED uint8_t const aes128_expected_output[MCUXCLELS_CIPHER_BLOCK_SIZE_AES]
Expected ciphertext output of the AES encryption.
Definition mcuxClEls_Cipher_Aes128_Ecb_Encrypt_example.c:36
static ALIGNED uint8_t const aes128_input[MCUXCLELS_CIPHER_BLOCK_SIZE_AES]
Plaintext input for the AES encryption.
Definition mcuxClEls_Cipher_Aes128_Ecb_Encrypt_example.c:30
static ALIGNED uint8_t aes128_output[MCUXCLELS_CIPHER_BLOCK_SIZE_AES]
Destination buffer to receive the ciphertext output of the AES encryption.
Definition mcuxClEls_Cipher_Aes128_Ecb_Encrypt_example.c:45
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:42
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:33
Definition of function identifiers for the flow protection mechanism.
Top-level include file for the ELS driver.
Provides the API for the CSSL flow protection mechanism.
#define MCUXCLELS_CIPHER_BLOCK_SIZE_AES
Definition mcuxClEls_Cipher.h:87
#define MCUXCLELS_CIPHERPARAM_ALGORITHM_AES_ECB
Set this option at mcuxClEls_CipherOption_t.cphmde to use AES engine in Electornic Code Book (ECB) mo...
Definition mcuxClEls_Cipher.h:76
#define MCUXCLELS_CIPHER_ENCRYPT
Set this option at mcuxClEls_CipherOption_t.dcrpt to perform an encryption.
Definition mcuxClEls_Cipher.h:62
#define MCUXCLELS_CIPHER_EXTERNAL_KEY
Set this option at mcuxClEls_CipherOption_t.extkey to use a key located in CPU memory provided by pKe...
Definition mcuxClEls_Cipher.h:73
#define MCUXCLELS_CIPHER_KEY_SIZE_AES_128
Size of an AES128 key: 128 bit (16 bytes)
Definition mcuxClEls_Cipher.h:96
MCUXCLELS_API mcuxClEls_Status_t mcuxClEls_Cipher_Async(mcuxClEls_CipherOption_t options, mcuxClEls_KeyIndex_t keyIdx, uint8_t const *pKey, size_t keyLength, uint8_t const *pInput, size_t inputLength, uint8_t *pIV, uint8_t *pOutput)
Performs AES encryption/decryption.
#define MCUXCLELS_ERROR_FLAGS_CLEAR
Set this option at mcuxClEls_ErrorHandling_t to clear all ELS error flags.
Definition mcuxClEls_Common.h:110
#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:119
MCUXCLELS_API mcuxClEls_Status_t mcuxClEls_WaitForOperation(mcuxClEls_ErrorHandling_t errorHandling)
Wait for an ELS operation and optionally clear the error status.
#define MCUXCLELS_STATUS_OK
No error occurred.
Definition mcuxClEls_Types.h:171
#define MCUXCLELS_STATUS_OK_WAIT
An _Async function successfully started an ELS command. Call mcuxClEls_WaitForOperation to complete i...
Definition mcuxClEls_Types.h:172
uint32_t mcuxClEls_KeyIndex_t
Type for ELS keystore indices.
Definition mcuxClEls_Types.h:222
#define MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(...)
Call a flow protected function and check the protection token.
Definition mcuxCsslFlowProtection.h:581
#define MCUX_CSSL_FP_FUNCTION_CALLED(...)
Expectation of a called function.
Definition mcuxCsslFlowProtection.h:735
#define MCUX_CSSL_FP_FUNCTION_CALL_END(...)
End a function call section started by MCUX_CSSL_FP_FUNCTION_CALL_BEGIN.
Definition mcuxCsslFlowProtection.h:616
Command option bit field for mcuxClEls_Cipher_Async.
Definition mcuxClEls_Cipher.h:121
struct mcuxClEls_CipherOption_t::@3 bits
Access mcuxClEls_CipherOption_t bit-wise.
uint32_t cphmde
Define cipher mode.
Definition mcuxClEls_Cipher.h:130
uint32_t extkey
Define whether an external key from memory or ELS internal key should be used.
Definition mcuxClEls_Cipher.h:138
uint32_t dcrpt
Define operation mode.
Definition mcuxClEls_Cipher.h:129