Microchip® Advanced Software Framework

Quick start guide for XMEGA AES driver

This is the quick start guide for the AES Driver, with step-by-step instructions on how to configure and use the driver for specific use cases.

The section described below can be compiled into e.g. the main application loop or any other function that might use the AES functionality.

Basic use case of the AES driver

In our basic use case, the AES driver is used to manually encrypt a block of code with AES encryption using a predefined key, and subsequently decrypt it using the same key.

Prerequisites

The System Clock Management module is required to enable the clock to the AES module.

Setup

When the System Clock Management module has been included, it must be initialized:

Subsequently, the clock to the AES module must be started:

Note
The example code below assumes that this setup has been done.

Use case

Example code

0x30, 0x70, 0x97, 0x1A, 0xB7, 0xCE, 0x45, 0x06,
0x3F, 0xD2, 0x57, 0x3F, 0x49, 0xF5, 0x42, 0x0D
};
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
};
t_data encrypted_data;
t_data decrypted_data;
void encrypt_decrypt() {
aes_set_key(encryption_key);
aes_write_inputdata(encryption_data);
do {
// Wait until AES is finished or an error occurs.
} while (aes_is_busy());
aes_read_outputdata(encrypted_data);
aes_write_inputdata(encrypted_data);
do {
// Wait until AES is finished or an error occurs.
} while (aes_is_busy());
aes_read_outputdata(decrypted_data);
}

Workflow

We first define our encryption key, and some encryption data, along with variables to hold encrypted and decrypted data.

t_key encryption_key = {
0x30, 0x70, 0x97, 0x1A, 0xB7, 0xCE, 0x45, 0x06,
0x3F, 0xD2, 0x57, 0x3F, 0x49, 0xF5, 0x42, 0x0D
};
t_data encryption_data = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
};
t_data encrypted_data;
t_data decrypted_data;
Note
These variables are of type t_key and t_data, which are defined as an AES_KEY_SIZE and AES_DATA_SIZE large block of unsigned 8 bit integers.

Inside our function, we first do a call to aes_software_reset(), to make sure that the AES driver is ready for use:

We configure the driver for manual encryption, with no XOR-ing:

We tell the driver where the key and the encryption data is located:

aes_set_key(encryption_key);
aes_write_inputdata(encryption_data);

We are now ready to start encryption, and since we have selected manual triggering, we trigger the module to start by calling aes_start():

We then wait until the module is finished, by checking the status register:

do {
// Wait until AES is finished or an error occurs.
} while (aes_is_busy());

When it is done, we can read out our result using aes_read_outputdata():

aes_read_outputdata(encrypted_data);

Our encrypted data is now stored in the variable encrypted_data.

We will now decrypt the data, and the procedure is very similar. We first set up the driver to do decryption, manually triggered with no XOR-ing:

We tell it where our encrypted data is located:

aes_write_inputdata(encrypted_data);
Note
As we have not called a software reset, the key is still stored in the module, so we do not need to do it again.

And tell it to start decryption:

And wait until it is finished:

do {
// Wait until AES is finished or an error occurs.
} while (aes_is_busy());

We read the output:

aes_read_outputdata(decrypted_data);

The decrypted data is now stored in the variable decrypted_data, and is identical to that stored in the variable encryption_data.