The Hardware Side of Cryptography

31 July 2008

Enigma Railway RS232 Security Dongle using PIC16F84

Filed under: encryption — Tags: , , , , , , — edipermadi @ 12:10 pm

Security dongle is an example of implementation of cryptography. There are many ways to connect dongle to your PC, whether you use USB, paralel port, serial port or even PCI or ISA port. Here, i just extend my previous code about Enigma Railway into projects called “Enigma Railway RS232 Security Dongle”.

The concept is actually easy. First, a PIC16F84 that works at 4MHz polls data form RS232 serial prot at 9600 bps. When data received, it will automatically determine wheter the data is gonna be encoded normally using Enigma Railway Algorithm or just interpreted as an ordinary command. Commands here are a set of instruction to view or modify inner setting of Enigma Railway Security Dongle. There are basically 13 commands exist, listed below:

Enigma Railway Security Dongle Commands
[`] View Inner Setting
[1] Change Left Rotor Type
[2] Change Middle Rotor Type
[3] Change Right Rotor Type
[4] Increase Reflector Setting
[5] Increase Left Rotor Setting
[6] Increase Middle Rotor Setting
[7] Increase Right Rotor Setting
[8] Increase Reflector Offset
[9] Increase Left Rotor Setting
[0] Increase Middle Rotor Setting
[-] Increase Right Rotor Setting
[=] Reset Enigma

The uppercase alphabetical characters (A to Z) will be treated as ordinary data and it will be encoded as an enigma Railway Message. Other undefined characters are ignored.

The code here was written using MPLAB v8.0, simulated and tested using PIC simulator IDE and compared to Enigma Railway Simulator with satisfying result. Both three methods were showing the same results. Have a look those three screenshots below:

Simulated using Oshon Soft PIC Simulator IDE

Captured RS232 result using Oshon Soft PIC Simulator IDE

Simulated using Enigma Railway Simulator

I’ve tested this code using PIC16F84 development board from Innovative Electronics at 4MHz clock and 9600bps transmission speed. Where TX bit was RA0 and RX bit was RA1. The schematic is basicaly easy, so i dont need to mention it here :) .

Last, this code is released freely under GNU Public License v3.0 without any warranty. So take your own risk and leave some comments, so i can hear your voice :)

RELATED STUFF

Download:
Enigma Railway RS232 Security Dongle Source Code | v1.0 | v1.1
PIC16F84 Datasheet
Mid-Range Reference Manual
MPLAB v8.0
Enigma Railway Simulator

Reference:
Enigma Machine
Enigma Rotor Details
Crypt Analysis of Enigma

Official Website
- Microchip
- Oshon Soft
- Innovative Electronics

24 July 2008

Tweaked Khazad Block Cipher Implementation on PIC18F4550

Filed under: encryption — Tags: , , , , — edipermadi @ 1:23 pm

Khazad is a Non Feistel Block Cipher scheme designed by Paulo S. L. M. Barreto and Vincent Rijmen. This cipher consists of 8 rounds of simple operation such XOR, shift, and substitution. Khazad block cipher has capability of encrypting and decrypting 64-bit block of data with 128-bit keys supplied. The authors said that khazad cipher is free to use and never be patented.

The cipher was easy, straightforward and easy to understand. Here, i provided you an example of Tweaked Khazad Block cipher implementation on PIC18F4550 microcontroller. The implementation here is based on the original C code Khazad implemementation on 64-bit microprocessor platform, therefore this way of implementation employs large lookup table approximately 16kbytes.

Basically, Khazad runs fast on PIC18F4550. It requires 9903 cyles for key setup and 3872 cycles for encryption and  decryption. Since Khazad was an involutional block cipher, you may notice that encrypting and decrypting speed was the same as well as its strength. The only thing that differ is about subkeys.

The source code here was developed and simulated on MPLAB v8.0 and released under the term of GNU Public License 3.0. Feel free to use it by your own risk :) . See screenshots below.

Khazad Plain text (Blue = key, Orange = Plain Text)

Khazad Cipher Text (Blue = Cipher Text)

Khazad Generated Subkeys (Blue = Encrypting Subkeys, Orange = Decrypting Subkeys)

I realized that this implementation hoggs memory much and i may not fit on real implementation such smart cards. I will scale down this implementatio so that it fit on smaller microcontroller such PIC16F84 and PIC16F877. Keep visiting this blog for more updates. Cheers :D

RELATED STUFF

Download
Source Code | v1.0 | v1.1 | v1.2
PIC18F4550 Datasheet
Khazad Tweak Specification
MPLAB v8.0

Reference
Khazad Cipher

Official Website
- Khazad Block Cipher

21 July 2008

2048-bit Fibonacci Sequence Number Generator using PIC18F4550

Filed under: number generator — Tags: , , , , — edipermadi @ 12:56 pm

Previously, i wrote a simple program to compute 128-bit Fibonacci Number using PIC16F84 microcontroller. In addition i extended that post to perform 2048-bit Fibonacci Number computation on PIC18F4550.

So far, i found that the largest Fibonacci number that fit on 2048-bit binary system was the 2947-th fibonacci number. The program was written and simulated on MPLAB v8.0. MPLAB v8.0 denoted that the time required to compute the 2947-th Fibonacci Number was 15182071 cycles.

Screenshot

RELATED STUFF

Download:
Source Code v1.0
PIC18F4550 Datasheet
MPLAB v8.0

Reference:
Fibonacci Number

20 July 2008

Implementing 128-bit Fibonacci Number Generator on PIC16F84

Filed under: number generator — Tags: , , , , — edipermadi @ 2:21 pm

Fibonacci was a commonly known sequence on numbers. Its sequence behaves recursively as F(n) = F(n-1) + F(n-2). In addition fibonacci number grows massively as its index increments, therefore we need such large binary representation in order to compute high order fibonacci number. Below are examples of some fibonacci numbers.

Example : 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765,…

Here, i wrote a code that able to generate any fibonacci numbers that fit into 128-bit binary system. So far, i found that the largest fibonacci number that fit on 128-bit binary representation was 3.328511 x 1038 which actually the 186-th fibonacci number. The code here was developed and simulated using MPLAB v8.0 and it works well. Next time, i’ll try to develop 2048-bits or perhaps 4096-bits fibonacci number generator hopefully :) .

For more detail explanation, please consider Fibonacci Number article on Wikipedia.

Screenshot

You may freely use this code under the GNU Public License v3.0. Cheers .

RELATED STUFF

Download:
Source Code v1.0
PIC16F84 Datasheet
Mid-Range Reference Manual
MPLAB v8.0

Referrence:
Fibonacci Number

15 July 2008

Implementing Enigma M3 Cipher Machine on PIC16F84

Filed under: encryption — Tags: , , , , , , — edipermadi @ 10:01 am

Enigma was a machine used to encrypt and decrypt messages during World War II. Enigma M3 or Funkschlüssel M is one of Engima Machine derivation used by Germany Army.
Enigma M3 has 5 type of rotors and 2 type of reflectors. Each of those rotors and reflectors characteristics are shown below.

Enigma M3 Rotors

Enigma M3 Reflectors

Enigma Machine

In addition, i wrote code for emulating enigma M3 using PIC16F84 microcontroller. The code was written and simulated on MPLAB v8.0. Not only that, I’ve tested this implementation against test vectors and compared the results to Enigma Simulator written by Geoff Sullivan. Guess What, i got exactly the same results. That was nice :) .

Example:
- Rotor offset : AAA
- Rotor config : UKW-B/I/II/III
- Rotor Setting : AAA
- Plain Text : AAAAAAAAAAAAAAAA
- Cipher Text : BDZGOWCXLTKSBTMC

Enigma M3 Simulator Screenshot

Enigma M3 Simulator Inner Settings

Enigma M3 Plain Text

Enigma M3 Cipher Text

The Implementation of Enigma M3 here was divided into two versions. Version 1.x denotes Basic Enigma M3 (3 slots, 5 rotors) and version 2.x denotes enhanced Enigma M3(3 slots, 8 Rotors)

RELATED STUFF

Download:
Source Code Enigma M3 (Basic) | v1.0 | v1.1 | v1.2
PIC16F84 Datasheet
Mid-Range Reference Manual
MPLAB v8.0
Enigma M3 Simulator

Reference:
Enigma Machine
Enigma Rotor Details
Crypt Analysis of Enigma

8 July 2008

1200 bps AES-128 Serial Encryptor

Filed under: encryption — Tags: , , , , , , , , — edipermadi @ 2:07 pm

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

Mathematically speaking,

IV1[0] = IV1
IV2[0] = 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)

RS232 Cable

Cheap Adaptor

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.

RELATED STUFF

Download
AES-128 Serial Encryptor Source Code | v1.0 | v1.1
PIC16F84 Datasheet
Mid-Range Reference Manual
AES Publication (fips-197)
MPLAB v8.0

References:
Wikipedia Advanced Encryption Standard
Wikipedia Vincent Rijmen
Wikipedia Joan Daemen
- Various TTL to RS232 Connection

Official Website:
- Microchip
- Advanced Encryption Standard
- Innovative Electronics
- Oshon Soft

Simulator:
AES (Rijndael) Simulator

7 July 2008

Fast blowfish block cipher implementation on PIC18F4550

Filed under: encryption — Tags: , , — edipermadi @ 10:00 am

i’ve ported blowfish source code that originally written for PIC16F877 microcontroller. It works twice faster than the original one. It can encrypt and decrypt data just in 2310 cycles which is fast. This cipher was invented by the “Security Guru” Bruce Scheneier. The core of blowfish cipher is made from addition, substitution and XOR processes and organized inside 18 rounds feistel structure. For example if plain text and key all contain zeros, the output of blowfish cipher would be “0×706d9fcc1792d23a”. Check out two screenshots below.

Plain Text

Cipher Text

I’ve tested this implementation against several test vector and it works the same as specification. However, this code is released for free under the term of GNU Public License v3.0 and comes with absolutely no guarantee at all. So, take your own risk while using this code. The implementation here is intended for performance oriented application, where throughput is everything :) . I’ll keep modifying this code till it reach the fastest one. Keep visiting this blog and happy coding :D ! .

RELATED STUFF

Download:
source code v1.0
PIC18F4550 datasheet
MPLAB v8.0

Reference:
Blowfish Cipher
Bruce Schneier

Official Website:
- Bruce Schneier
- Blowfish Cipher

4 July 2008

Implementing RSA Public Key Cipher on PIC18F4550

Filed under: encryption — Tags: , , , , — edipermadi @ 11:21 am

Last night, i was coding RSA Public Key Cipher for PIC18F4550 microcontroller till 1.30 AM :) , phew . Actually, this is out of my plan. I am supposed to finisih coding Anubis and RC5 cipher but i suddenly wondering how RSA works in microcontroller Hmm…   :?: . See, sometimes commiting plan is hard to me, i better work spontaneously. Yeah, regularity will bored me in a few nanoseconds. But nothing useless, RSA is a powerfull cipher compared to others. This cipher was invented by the threesome cool and damn genius Guys : Ron Rivest, Adi Shamir, and Leonard Adleman. Despite of its high security, RSA algorithm runs sluggy slow on PIC18F4550. For higher keylength, it also hogs PIC18F4550 data memory. I could not imagine what if code it on PIC16F84 or PIC16F877, Hmm :evil: . For sure, i’ll code RSA cipher for PIC16F84 and PIC16F877, too see how slow it could be :mrgreen: .

I’ve tested the code against several test vecor and it shows exactly the same result. That was a nice news to hear. For example 887 mod 187 will result 11. I believe that this code is compatible with the RSA specification. However this is a free open source software that comes with no guarantee, so take your own risk if yiu wish to use this code.

In addition, i divided the RSA implementation into 7 version. Each version has its own characteristics listed below.

  • Version 1.x for 32-bit RSA implementation
  • Version 2.x for 64-bit RSA implementation
  • Version 3.x for 128-bit RSA implementation
  • Version 4.x for 256-bit RSA implementation
  • Version 5.x for 512-bit RSA implementation
  • Version 6.x for 1024-bit RSA implementation
  • Version 7.x for 2048-bit RSA implementation

See this screenshots below. First boxed row denote RSA input, second boxed row denote exponentiaion constant, third boxed row denote mod operation constant and fourth boxed row denote the result of RSA algorithm.

Encrypting Screenshot

Decrypting Screenshot

So far, the code only contains the core of RSA cipher, it has no capability of doing key scheduling i.e. key generating. I’ll code and upgrade all of that version gradually, so keep visiting this blog and please mail me your suggestion or even question.

RELATED STUFF

Download:
RSA 32-bit | v1.0
RSA 64-bit | v2.0
PIC18F4550 Datasheet
MPLAB v8.0

Reference:
RSA
Ron Rivest
Adi Shamir
Leonard Adleman

Official Website:
- microchip
- RSA Security

Blog at WordPress.com.