Algorithm
This is an implementation of the AES block cipher in 128, 192, and 256 bit modes, where the bit size corresponds to the key length. The algorithm encrypts plaintext in block sizes of 128 bits.
Code Documentation
- KeySchedule(unsigned char key[], unsigned int w[])
This function takes the generated key (128, 192, or 256 bits in length) and generates a key schedule to use in encryption.- unsigned char key[]
Must contain 16, 24, or 32 bytes of data to be used as the key. - unsigned int w[]
Must be 60 32-bit integers in size. This will be the outputted key schedule. - int keysize
Must be the size of the key in bits, limited to the values 128, 192, or 256.
- unsigned char key[]
- aes_encrypt(unsigned char in, unsigned char out, unsigned int key[], int keysize)
aes_decrypt(unsigned char in, unsigned char out, unsigned int key[], int keysize)
These functions encrypt and decrypt data, respectively.- unsigned char in[]
The plaintext to be encrypted. The array must be 16 bytes in size. - unsigned char out[]
The output ciphertext. The array must be 16 bytes in size. - unsigned int key[]
The “w” array key schedule from the KeySchedule() function. - int keysize
Ensure it’s the same value as the “keysize” value in KeySchedule() function.
- unsigned char in[]
Code Usage
- Call the KeyExpantion() function with the encryption key you want to use, the key schedule array, and the number of bits in length of the key.
- To decrypt data, call the aes_encrypt() function with the input plaintext array, an array to hold the output ciphertext, and the size of the original key in bits (same as the value passed to the KeyExpantion() function).
- To decrypt data, call the aes_decrypt() function with the input ciphertext array, an array to hold the output plaintext, and the size of the original key in bits (same as the value passed to the KeyExpantion() function).
Code
• Source Code • Sample Driver Program
Notes
This implementation is designed for little endian processors, but I think should work for big endian processors as well.
I’ve taken some steps towards optimizing it, (basic things like unrolling loops), but the implementation is fairly slow. I hope to be able to optimize it more in the future.
Also note that this is not a hardened implementation of AES. It has benchmarked to be fairly slow and has not been designed to be resistant to side-channel attacks. Optimizing it and attack-proofing it are on my to-do list.
There are a few extra functions included in this code, printstate() and print_rnd_key(), and print_arry(), that fall under the heading of “Debugging Functions”. These are not a part of the AES implementation and may be removed, they only exist to aid in debugging the code should the implementor seek to optimize it or tinker with it in any way. I used them excessively when originally implementing the code, and figured they might come in handy to someone else as well. They’re not complex, but useful.
