The supported board list:
- SAM L21 Xplained Pro
- SAM L22 Xplained Pro
This example demonstrates how to use the AES driver to perform:
Upon startup, the program uses the USART driver to display application output message from which ECB DMA encryption modes can be tested.
Quick Start
Prerequisites
There are no prerequisites for this use case.
Code
Add to the main application source file, outside of any functions:
#define AES_EXAMPLE_REFBUF_SIZE 4
uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = {
0xe2bec16b,
0x969f402e,
0x117e3de9,
0x2a179373
};
uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = {
0xb47bd73a,
0x60367a0d,
0xf3ca9ea8,
0x97ef6624
};
const uint32_t key128[4] = {
0x16157e2b,
0xa6d2ae28,
0x8815f7ab,
0x3c4fcf09
};
Add to the main application source file, outside of any functions:
static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE];
volatile bool state = false;
struct aes_module aes_instance;
Copy-paste the following setup code to your user application:
static void configure_usart(void)
{
config_usart.baudrate = 38400;
config_usart.mux_setting = EDBG_CDC_SERCOM_MUX_SETTING;
config_usart.pinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0;
config_usart.pinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1;
config_usart.pinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2;
config_usart.pinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3;
stdio_serial_init(&usart_instance, EDBG_CDC_MODULE, &config_usart);
}
static void ecb_mode_test_dma(void)
{
printf("\r\n-----------------------------------\r\n");
printf("- 128bit cryptographic key\r\n");
printf("- ECB cipher mode\r\n");
printf("- DMA mode\r\n");
printf("- 4 32bit words with DMA\r\n");
printf("-----------------------------------\r\n");
state = false;
g_aes_cfg.lod = false;
while (false == state) {
}
}
state = false;
while (false == state) {
}
if ((ref_cipher_text_ecb[0] != output_data[0]) ||
(ref_cipher_text_ecb[1] != output_data[1]) ||
(ref_cipher_text_ecb[2] != output_data[2]) ||
(ref_cipher_text_ecb[3] != output_data[3])) {
printf("\r\nKO!!!\r\n");
} else {
printf("\r\nOK!!!\r\n");
}
}
static void transfer_tx_rx_done(
struct dma_resource*
const resource )
{
state = true;
}
static void configure_dma_aes_wr(void)
{
tx_config.peripheral_trigger = AES_DMAC_ID_WR;
tx_descriptor_config.dst_increment_enable = false;
tx_descriptor_config.block_transfer_count = AES_EXAMPLE_REFBUF_SIZE;
tx_descriptor_config.source_address = (uint32_t)ref_plain_text + sizeof(ref_plain_text);
tx_descriptor_config.destination_address =(uint32_t) &(AES->INDATA);
}
static void configure_dma_aes_rd(void)
{
rx_config.peripheral_trigger = AES_DMAC_ID_RD;
rx_descriptor_config.src_increment_enable = false;
rx_descriptor_config.block_transfer_count = AES_EXAMPLE_REFBUF_SIZE;
rx_descriptor_config.source_address = (uint32_t)&(AES->INDATA);
rx_descriptor_config.destination_address =
(uint32_t)output_data + sizeof(output_data);
}
Add to user application initialization (typically the start of main()
):
configure_usart();
configure_dma_aes_wr();
configure_dma_aes_rd();
aes_init(&aes_instance,AES, &g_aes_cfg);
Workflow
- Define sample data from NIST-800-38A appendix F for ECB mode.
#define AES_EXAMPLE_REFBUF_SIZE 4
uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = {
0xe2bec16b,
0x969f402e,
0x117e3de9,
0x2a179373
};
uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = {
0xb47bd73a,
0x60367a0d,
0xf3ca9ea8,
0x97ef6624
};
const uint32_t key128[4] = {
0x16157e2b,
0xa6d2ae28,
0x8815f7ab,
0x3c4fcf09
};
- Create related module variable and software instance structure.
static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE];
volatile bool state = false;
struct aes_module aes_instance;
- Create DMA resource struct and descriptor.
DmacDescriptor example_descriptor_tx SECTION_DMAC_DESCRIPTOR;
DmacDescriptor example_descriptor_rx SECTION_DMAC_DESCRIPTOR;
- Configure, initialize, and enable AES module.
- Configuration AES DMA module, which can be used for AES.
configure_dma_aes_wr();
configure_dma_aes_rd();
- Configuration AES struct, which can be filled out to adjust the configuration of a physical AES peripheral.
- Initialize the AES configuration struct with the module's default values.
aes_init(&aes_instance,AES, &g_aes_cfg);
- Enable the AES module.
Use Case
Code
Copy-paste the following code to your user application:
Workflow
- Configure ECB mode encryption with DMA and run test.
state = false;
g_aes_cfg.lod = false;
while (false == state) {
}
}
state = false;
while (false == state) {
}
if ((ref_cipher_text_ecb[0] != output_data[0]) ||
(ref_cipher_text_ecb[1] != output_data[1]) ||
(ref_cipher_text_ecb[2] != output_data[2]) ||
(ref_cipher_text_ecb[3] != output_data[3])) {
printf("\r\nKO!!!\r\n");
} else {
printf("\r\nOK!!!\r\n");
}