AES-128 is a common block cipher used in cryptography. AES-128 encrypt 128-bit plain text becoming 128-bit cipher text with 128-bit key supplied. AES-128 consists of 10 rounds non-feistel cipher. Each of that rounds are being supplied by subkeys generated from cipherkey.
Here, as my first embedded cryptography experiment, I’m trying to implement simple RS232 serial encryptor using AES-128 block cipher on PIC16F84 microcontroller flatform. The goal is about creating secured serial communication. Here, i’ve got couple things to do. First, is about modifying AES-128 block cipher becoming stream cipher and the second one is about embedding the cryptographic function itself.
This project is originally designed for 1200 bps serial transmission. See, I just tought that 1200 bps is really slow, so i decided to speed it up into 9600 bps.
See this picture below, it describes more than words.
The picture above is the concept of modifying AES-128 block cipher into stream cipher. But wait, why we have to do such this thing? The answer is that RS232 transmit everything byte by byte therefore we need byte stream encryptor, but how can we do this? The answer is easy. Here i just use standard AES-128 encryption routine to generate 128-bit random vector. The random vector is just the cipher text of IV1 and IV2. IV1 acts as a plain text and IV2 as cipher key. So here we have couple IV values to initialize.
Next, the last subkey generated from previous step is propagated to the current random vector generating process as its IV2 and its IV1 was taken from previous random vector. Each generated random vector will encrypt 16 bytes of plain text byte by byte. Here we can conclude that random vector will generated after 16 bytes of data were encrypted.
Since generating random vector takes 3850 cycles (approximately 3850us when we use 4MHz crystal), we need such delay between 16 bytes of data to resynchronize the encryptor.
[16 bytes of data] [delay] [16 bytes of data] [delay]
After i finished modyfying AES, i tried to wrote the code for microcntroller itself. I wrote the code on MPLAB v8.0 and simulate it using Oshon Soft PIC Simulator IDE and it works. See this screenshot below.
The screenshot above simulates this setting:
IV1 = 00000000000000000000000000000000
IV2 = 00000000000000000000000000000000
pt0 = 00000000000000000000000000000000
ct0 = 66e94bd4ef8a2c3b884cfa59ca342b2e
pt1 = 00000000000000000000000000000000
ct1 = 40527d2f2d1fafb4d326b0f8f93a58e1
pt2 = 00000000000000000000000000000000
ct2 = ae08e8d98ee3cb0e595e64f8cfe7f609
pt3 = 00000000000000000000000000000000
ct3 = 8e4ddc08cb5d0ac7a4ac041cc9819610
IV1 = IV1
IV2 = IV2
IV1[n] = RV[n-1]
IV2[n] = Last Subkey of IV2[n-1]
RV[n] = AES_128_Encrypt(IV1[n],IV2[n])
CT[n] = PT[n] XOR RV[n]
After code verified, i downloaded the code into PIC16F84 microcontroller using ICProg, PIC ICSP Programmer and PIC16F84 development board from Innovative Electronics bought couple months ago. I operated my PIC16F84 microcontroller at 4MHz and 9600 bps serial transmission. The schematic is basically easy. You jusy use RA0 as RS232 Tranmitting port (TX) and RA1 as receiving port (RX). You may use any TTL to RS232 converter, here i just used the simplest one🙂 . See Pictures Below
PIC ICSP Programmer
PIC ICSP Cable
PIC16F84 Development Board (Front)
PIC16F84 Development Board (Back)
After everything plugged properly and RS232 cable connected from microcontroller to PC, i tested the circuit using RS232 Terminal Viewer integrated on Oshon PIC Simulator IDE and it works the same as simulation. To use this encryptor, first you have to send 32 bytes of data to initialize the microcontroller. The first 16 bytes is used to initialize IV1 and second one to initialize IV2. After that, you may dump data as much as you want and dont forget to give somewhat 50 ms delay on each 16 bytes transmission. Nice huh! see the screenshot below.
Encrypted RS232 Result Screenshot (taken form RS232 COM1 terminal)
Last, I realized that 50 ms delay requirement on each 16 bytes of data is somewhat annoying and sucks. If you want to eliminate such this delay, please rock the microcontroller up to 20 MHz and down the speed to 1200 bps, so that the required delay is equal to one bit propagation (approximately 1/1200) which is OK. Hopefully that way removes problem from your application and removes headache from yourself🙂 .
Codes here are released for free under the term of GNU Public License v3.0. This program comes with no guarantee at all. Take you own Risk and please write some comments or mail me for some feedbacks.
AES (Rijndael) Simulator