# drivers/aes/aes.c File Reference

Asynchronous Advanced Encryption Standard (AES) driver. More...

`#include <assert.h>`
`#include <debug.h>`
`#include <malloc.h>`
`#include <status-codes.h>`
`#include <string.h>`
`#include <interrupt.h>`
`#include <chip/memory-map.h>`
`#include <aes.h>`
`#include "aes_regs.h"`

Include dependency graph for aes.c:

Go to the source code of this file.

## Functions
void | aes_init (struct aes_module *module, uint32_t mode)
| Initialize AES module.
void | aes_free (struct aes_module *module)
| Free AES module.
void | aes_set_cipher (struct aes_module *module, uint32_t mode)
| Change AES cipher mode.
uint32_t | aes_get_cipher (struct aes_module *module)
| Get AES cipher mode.
void | aes_load_init_vector (struct aes_module *module, const uint32_t *vector)
| Load initialization vector.
void | aes_load_key (struct aes_module *module, const uint32_t *key)
| Load AES key.
void | aes_free_duplicate_buffers (struct slist *buf_list)
| Free a duplicated buffer list.
int | aes_duplicate_buffer_refs (struct slist *copy, struct slist *original)
| Create duplicate references to the data in a buffer list.
void | aes_submit_request (struct aes_request *req)
| Submit AES request.
struct aes_request * | aes_alloc_request (struct aes_module *module)
| Allocate AES request.
void | aes_free_request (struct aes_request *req)
| Free AES request.
void | aes_prepare_request (struct aes_request *req, struct slist *buf_list)
| Prepare an AES request.

## Detailed Description

Asynchronous Advanced Encryption Standard (AES) driver.

This is a fully asynchronous driver for the AES module present on AT32UC3A3S. Data must be provided as a single linked list of buffers, and as long as no reconfiguration of the AES module is required, request queuing is supported.

- Compiler: IAR EWAVR32 and GNU GCC for AVR32
- Supported devices: All devices
- AppNote:

Author:
  Atmel Corporation: http://www.atmel.com
  Support and FAQ: http://support.atmel.no/

Definition in file aes.c.

## Function Documentation

struct aes_request* aes_alloc_request | ( | struct aes_module * | module | ) | `[read]`

Allocate AES request.

This function will allocate memory for one AES request.

Parameters:

  | module | Pointer to AES module struct

Returns:
  Pointer to newly allocated AES request struct

Definition at line 356 of file aes.c.

References assert, aes_request::module, and aes_module::port.

int aes_duplicate_buffer_refs | ( | struct slist * | copy,
|  | struct slist * | original |
| ) |  |  |

Create duplicate references to the data in a buffer list.

This function will create a duplicate buffer list, mirroring the buffers in the original buffer list. The new buffers will refer to the data stored in the original buffers. If the two buffer lists are submitted as tx and rx buffers to the AES driver, the encryption will be performed in-place.

Parameters:

  | copy | Duplicated buffer list
  | original | The original buffer list

Returns:
  0 Successfull duplication

  -1 Duplication failed, no new buffers allocated

Definition at line 228 of file aes.c.

References buffer::addr, aes_free_duplicate_buffers(), assert, buffer::len, buffer::node, buffer_addr_t::phys, buffer_addr_t::ptr, slist_for_each, slist_insert_tail(), and unlikely.

Referenced by aes_prepare_request().

Here is the call graph for this function:

void aes_free | ( | struct aes_module * | module | ) |

Free AES module.

This function will free the DMA channels allocated by aes_init(), which afterwards will have to be run again in order to use the AES module.

Parameters:

  | module | Pointer to AES module struct

Definition at line 92 of file aes.c.

References assert, aes_module::dma_rx_channel, aes_module::dma_tx_channel, and aes_module::port.

void aes_free_duplicate_buffers | ( | struct slist * | buf_list | ) |

Free a duplicated buffer list.

This function will free all the buffers allocated by aes_duplicate_buffer_refs().

Parameters:

  | buf_list | The buffer list to free

Definition at line 201 of file aes.c.

References assert, buffer::node, slist_is_empty(), and slist_pop_head.

Referenced by aes_duplicate_buffer_refs().

Here is the call graph for this function:

void aes_free_request | ( | struct aes_request * | req | ) |

Free AES request.

This function will free the memory allocate by an AES request.

Parameters:

  | req | Pointer to AES request struct

Definition at line 378 of file aes.c.

References assert, aes_request::rx_buf_list, slist_is_empty(), and aes_request::tx_buf_list.

Here is the call graph for this function:

uint32_t aes_get_cipher | ( | struct aes_module * | module | ) |

Get AES cipher mode.

This function will return the current AES cipher mode. It can be used to make sure the correct mode is enabled before submitting requests.

Parameters:

  | module | Pointer to AES module struct

Definition at line 129 of file aes.c.

References assert, and aes_module::mode.

void aes_init | ( | struct aes_module * | module,
|  | uint32_t | mode |
| ) |  |  |

Initialize AES module.

This function will initialize the AES hardware module and create the DMA channels needed for it to operate. The operation mode can be set by the mode bit field.

Parameters:

  | module | Pointer to AES module struct
  | mode | AES mode bit field

Definition at line 68 of file aes.c.

References assert, aes_module::counter, aes_module::dma_rx_channel, aes_module::dma_tx_channel, aes_module::mode, and aes_module::port.

void aes_load_init_vector | ( | struct aes_module * | module,
|  | const uint32_t * | vector |
| ) |  |  |

Load initialization vector.

This function will load an initialization vector to the AES module. This vector is needed for when performing block chained encryption.

Parameters:

  | module | Pointer to AES module struct
  | key | Initialization vector

Definition at line 145 of file aes.c.

References assert, and aes_module::port.

void aes_load_key | ( | struct aes_module * | module,
|  | const uint32_t * | key |
| ) |  |  |

Load AES key.

This function will load the key the AES module will use for encryption and decryption into the hardware. aes_init() must be run first and the key provided here must correspond to the keysize selected when the module was initialized.

Parameters:

  | module | Pointer to AES module struct
  | key | AES key

Definition at line 167 of file aes.c.

References AES_KEYSIZE_128, AES_KEYSIZE_192, AES_KEYSIZE_256, AES_KEYSIZE_MASK, assert, aes_module::mode, aes_module::port, and unhandled_case.

void aes_prepare_request | ( | struct aes_request * | req,
|  | struct slist * | buf_list |
| ) |  |  |

Prepare an AES request.

This function will prepare an AES request for submittion. The buffers in buf_list is copied to an inernal list in the request. A new bufer list mirroring the input buffer list is created and will be used to hold the AES output (encrypted data when encrypting). When the operation is done, the input buffers are thrown away and a pointer to the output buffer list is returned to the callback function.

Parameters:

  | req | Pointer to AES request struct
  | buf_list | Buffer list containing input data.

Definition at line 399 of file aes.c.

References aes_duplicate_buffer_refs(), assert, aes_request::rx_buf_list, slist_init(), slist_is_empty(), slist_move_to_tail(), and aes_request::tx_buf_list.

Here is the call graph for this function:

void aes_set_cipher | ( | struct aes_module * | module,
|  | uint32_t | mode |
| ) |  |  |

Change AES cipher mode.

This function will change the current AES cipher mode to encrypt or decrypt, depending on the mode paramater.

Parameters:

  | module | Pointer to AES module struct
  | mode | AES cipher mode mask

Definition at line 110 of file aes.c.

References assert, aes_module::mode, and aes_module::port.

void aes_submit_request | ( | struct aes_request * | req | ) |

Submit AES request.

This function will submit one AES request for encryption/decryption by the AES hardware module. It will allocate two DMA requests for transfer to and from the AES module and submit these to their repsective DMA channels. The memory allocated for DMA requests will automatically be freed prior to calling the callback.

Parameters:

  | req | Pointer to AES request struct

Definition at line 320 of file aes.c.

References assert, dmac_request::buf_list, DMA_FROM_DEVICE, aes_module::dma_rx_channel, DMA_TO_DEVICE, aes_module::dma_tx_channel, dmac_chan_submit_request(), aes_request::done_counter, aes_request::module, aes_request::rx_buf_list, aes_request::rx_req, slist_is_empty(), slist_move_to_tail(), aes_request::tx_buf_list, and aes_request::tx_req.

Here is the call graph for this function: