For one of the Matasano crypto challenges, I had to decrypt the text which was encrypted using AES in ECB mode. Everything about AES is actually documented by the National Institute of Standards and Technology. You can get all the algorithms behind AES encryption. It is probably not a good idea to implement it from scratch. Openssl has a well tested and widely used library which works.

This Openssl library page gives a complete example of how to use them. There are a few preparatory steps before you can use the instructions though. These instructions are for Ubuntu like Linux distributions. These worked well on my Raspberry Pi too.

Installing Openssl library

Following command installs all the C libraries needed to use Openssl with your C code.

sudo apt-get install libssl-dev

For example, you will want to include the following header files:

#include <openssl/evp.h>
#include <openssl/ssl.h>
#include <openssl/rsa.h>
#include <openssl/x509.h>

Compiling your C program with the Openssl library

Next, you can follow the instructions from the Openssl crypto library page to create your C program. I have an example program in my Crytopals Github repository. While linking the program you need to provide the ssl and crypto library names. Following command should do it:

gcc yourprogram.c -lssl -lcrypto

A few pointers on the do_crypt function

  • If you are going to use the do_crypt function for decrypting a text encrypted using electronic code book (ECB) mode, you should remove the following assert line since there is no Initialization Vector for ECB.
OPENSSL_assert(EVP_CIPHER_CTX_iv_length(&ctx) == 16);
  • The example code operates on the raw data. So, if you are trying to decrypt the data which is base64 encoded, your first step should be to convert it into raw data.