Implementation of AES in C

This algorithm implementation is a part of my cryptography implementations project. The full project, along with licensing information and more detail, is hosted on GitHub.

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.

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.
  • 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.

Code Usage

  1. 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.
  2. 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).
  3. 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

Notes

I've only taken basic steps towards optimizing this, so the implementation is fairly slow.

This implementation has not been designed to resist side-channel attacks.

There are a couple extra simple functions included in this code, printstate(), print_rnd_key(), and print_arry(), that are strictly 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 someone try to alter the code. I used them excessively when originally writing this implementation.