emLib AES

What is AES?

The Advanced Encryption Standard, short AES, is a symmetric-key algorithm used for encryption and decryption of data. It was established by the U.S. National Institute of Standards and Technology (NIST) and is the standard for encrypting electronic data since 2001. AES supersedes the Data Encryption Standard (DES).

AES is a substitution-permutation network block cipher using a fixed block size of 128 bits and a key size of 128, 192 or 256 bits. The data block is stored in a 4-row matrix with a cell size of 8 bits. Based on the key length, these blocks are transformed using parts of the key in a number of rounds.

AES128 uses 10 rounds, AES256 14. Therefore encryption with AES256 is ~40% slower than AES128. In each round a round key is derived from the original key. Afterwards each byte is non-linear substituted according to a lookup table, the rows of the data matrix are shifted cyclically and mixed.

emLib AES uses a key of 128 or 256 bits to encrypt a block of 16 bytes of data at a time. To optimize the performance of the algorithms the generation of the round keys can be done before the actual encryption or decryption and used more than one time.

For the substitution and mixing steps, emLib can be built with pre-calculated lookup tables, to increase the speed performance. emLib can also be built without these tables, to save memory.

AES can also be used in cipher block chaining (CBC) mode to process a multiple of 16 bytes. In CBC mode every chunk of 16 bytes is XOR linked with the result of the previous encryption (the cipher text), before being encrypted.To decrypt one block, all previous blocks have to be known.

For the encryption of the first block an initialization vector which will be linked with the block, can be used to make sure the first block cannot be brute-force decrypted by comparing it to common first data blocks.

Using emLib AES

The emLib AES module has a simple yet powerful API. It can be easily integrated into an existing application. The code is completely written in ANSI-C.

All functionality can be verified with standard test patterns using the Validation API functions. The functions for generating the tables used for higher optimization levels are also included for full transparency.

The module can be built with configurable optimizations to fit any requirement of high speed or low memory usage. To simply encrypt or decrypt data the application will only need to call one function.

If more than one block needs to be processed with the same key, a context containing the round keys calculated from the key can be prepared and directly used by the encryption and decryption functions. For more than one call of these functions this method results in a slightly higher processing speed.

Performance and Memory Footprint

The following table shows the en- and decryption speed of emLib AES128 as tested on an STM32F4:

ConfigurationSpeed [MB/s]ROM usage [KB]
Fastest speed2.1511.37
Default1.156.36
Smallest size0.502.86

AES Configuration

emLib AES aims for portability and is designed to fit speed and size requirements for different targets. It includes configurable defines to switch between speed and size optimizations. The values can be changed in AES_Config.h.

#defineValuesDescription
OPTIMIZE_MIX_SUBST0 (Smallest)
1* (Fastest)
Use a 32-bit tablesaw to perform “MixColumns” and “SubBytes” at the same time.
OPTIMIZE_MIX_COLUMNS0* (Smallest)
1
2 (Fastest)
Use tables for matrix multiplication.
COPY_CONST_TO_RAM0* (Off – Least RAM)
1 (On – Fastest)
Copy timing critical constant data to RAM, which yields higher performance on systems where const are in (slower) flash memory.

Options marked with * are default.

AES API Functions

The table below lists the available AES API functions.

FunctionDescription
AES128_CBC_Decrypt()Decrypts data with AES 128 Bit using CBC.
AES128_CBC_Encrypt()Encrypts data with AES 128 Bit using CBC.
AES128_Decrypt()Decrypts 16 Bytes with AES 128 Bit.
AES128_Encrypt()Encrypts 16 Bytes with AES 128 Bit.
AES128_Prepare()Prepares the context for de-/encryption.
AES256_CBC_Decrypt()Decrypts data with AES 256 Bit using CBC.
AES256_CBC_Encrypt()Encrypts data with AES 256 Bit using CBC.
AES256_Decrypt()Decrypts 16 Bytes with AES 256 Bit.
AES256_Encrypt()Encrypts 16 Bytes with AES 256 Bit.
AES256_Prepare()Prepares the context for de-/encryption.
AES_Validate()Test function for validation of AES.

Detailed descriptions of all functions can be found in the emLib user manual.

Example Code

AES 128bit en-/decryption of 32 Bytes using CBC

This sample shows how to encrypt and afterwards decrypt 32 bytes of data with AES and a 128 bit key using Cipher Block Chaining.

Example Code AES 128bit

Sample Applications

emLib includes some sample applications to show the modules functionality and provide an easy to use starting point for your application. The application’s source code is included within the module. The following applications are included in emLib AES:

Application nameTarget platformDescription
AESCrypt.exeWindowsCommandline tool to en-/decrypt a file using AES 256.
AESSpeedtest.exeWindowsConsole application testing the speed of emLib AES.
AESValidate.exeWindowsConsole application validating emLib AES with standard test patterns.

AESCrypt

AESCrypt is a windows application, encrypting and decrypting a file with the given keyword. The tool can be used to easily keep files secured.

Usage: AESCrypt [option]

The password can contain any character and does not have a fixed required length. The output file after encryption will have the extension “.enc”. If present, the original file will be renamed to .orig, when decrypting a file with the same name.

AEScrypt 1
AEScrypt 2
ParameterDescription
<sourcefile>Path to the file, which has to be en-/decrypted
<password>Password used for en-/decryption
[option](optional) “-en”: Force encryption of the source file. “-de”: Force decryption of the source file. If no option is given, operation depends on source file extension.

AESSpeedtest

AESSpeedtest is a windows application, testing the performance of the emLib AES algorithms.

AESSpeedtest

AESValidate

AESValidate is a Windows application used to test and validate the implementation of the AES algorithms. The application uses the Validation API and compares the results of encryption and decryption with the expected results. AESValidate will show an error message, if a validation test fails.