The Hardware Side of Cryptography

25 January 2008

Implementing Blowfish using PIC16F877

Filed under: encryption — Tags: , , , — edipermadi @ 8:30 am

Again for crypto nutters :) , i’ve implemented blowfish encryption using PIC16F877. Blowfish was pretty simple to implement, it employs 32 bit addtion, bitwise XOR and substitution only. But wait, it uses 4096 bytes to implement SBOX Table Lookup, so you have to choose microcontroller with memory capacity greater than 4 kbytes.

Blowfish is one of symetric block cipher. It employs feistel structure with 32 bit wide on each side. It is royalty free and require no license. It’s faster than DES and IDEA. Yeah, its good for everyone. Its Free!

This implementation contains both of encryption and decryption routine. You can download and redistribute the code, but take your own risk and don’t forget to mention the source :D . So, If you are looking for an example of Blowfish implementation on hardware, you got the right page.

As a benchmark, the program takes 1216 cycles (1.2 ms) to do key setup. To encrypt one block of plain text, it takes 5100 cycles (5.1 ms) also, to decrypt one block of cipher text also takes 5100 cycles (5.1 ms). By this implementation, you can encrypt and decrypt 1.568 kbyte/s. I knew i was fairly slow, i keep trying to modify the code till the most effective one. Hopefully, someday i”ll have a chance to implement this on FPGA or CPLD.

Bruce Scheneier is the author of Blowfish Encryption/Decryption Algorithm. He own the algorithm. Big thanks to him. Check out his website here.

RELATED STUFF

Download:
Source Code v1.0
Source Code v1.1
Source Code v1.2
PIC16F877 Datasheet
Mid Range Reference Manual

References:
 Blowfish Cipher
 Bruce Schneier

Links:
 Encryption
 PIC16F877
 PIC16F877 Programmer
 PIC16F877 Development Board

Official Website
Microchip
Blowfish

21 January 2008

Implementing AES using PIC16F84

Filed under: encryption — Tags: , , , , — edipermadi @ 5:18 am

Rindael is one of many ciphers that belong to Advanced Encryption Stantard (AES). Generally, AES term refers to rijndael algorithm, although there are many other candidates of AES cipher. Rijndael algorithm was developed by Vincent Rijmen and Joan Daemen.

Unlike blowfish, Rijndael or AES takes litle memory. This is true since AES only has two 256 byte of SBOX tables. However, blowfish has eight 32 bit of sbox tables. So you can easily implement it on any platform of microcontroller, like what i’ve done here. AES has several version, depends on how long the key was. There are AES-128, AES-192 and AES-256. Here, to make life easier, i just code the first one (AES-128). Later on you can even coded the crazier one :) .

I’ve decided to choose PIC16F84 to run the implementation of AES on microcontroller. If you need an example on how to implement AES using a microcontroller such PIC16F84, you get the right page. You can grab, copy and paste this page but dont forget to mention the source :) . Later on, you can translate it this code into other microcontroller platform, hopefully it will increase the speed.

I encrypted “abcdefghijklmnop” string with key “0f1571c9 47d9e859 0cb7add6 af7f6798″ i get “110aaff3 f2d56c9e 691a95a5 2e1928eb” as the result which is the same as an “AES Example” that i found from http://people.eku.edu/styere/Encrypt/JS-AES.html . Open that link, it shows you how AES was done, step by step.

As a benchmark first, all of encrypting routine and sbox tables takes 835 lines of program memory. The program tooks 11.62 ms to cipher one block of plain text. I’ll keep modifying the code until it reach the fastest one.

Here is the screenshot, I took it from MPLAB 5.20

 Test Result

Finally, i’ve added decryption routine into the code. I also simplified the algorithm to reduce memory usage. You know that speed and memory usage are trading off. Surely the speed was decreased, but i think it’s fair enough comparing to reducing memory usage. You can find the latest implementation (version 1.1), which is more complete than before.

This code are freely distributed under GPL license.

RELATED STUFFS

Downloads:
Source Code v1.0
Source Code v1.1
Source Code v1.2
PIC16F84 Datasheet
Mid-Range Reference Manual
 AES Publication (fips-197)

References:
Wikipedia Advanced Encryption Standard
Wikipedia Vincent Rijmen
Wikipedia Joan Daemen

Links:
Google Encryption
 PIC16F84
 PIC16F84 Programmer
AES (Rijndael) Simulator
 PIC16F84 Development Board

Official Website:
- Microchip
- Advanced Encryption Standard

15 January 2008

Implementing Simplified DES using PIC16F84

Filed under: encryption — Tags: , , , — edipermadi @ 5:56 am

Guys, if you are looking for an implementation of Simplified DES (SDES) using microcontroller then you are lucky if you see this page. Last night, i have tried to implement a simple encryption/decryption algorithm called Simplified DES (SDES) using PIC16F84 microcontroller. Its just a piece of cake, you can implement all of this while cutting nail :) . Its only 384 bytes of code.

Both of encrypting and decrypting process takes approximately 130 cycles of clock. But if you want to get faster, you just need to convert permutation process into lookup. It will takes larger memory but require less time. For example, you can can convert e/p permutation into 16 cells of lookup table. On the other hand Initial Permutation (IP) and Inverse Initial Permutation can also be done by converting each of them into 256 cells of lookup table. Surely, your application becoming memory hogg, but it going faster :)

Here are some screenshoot, i took it from My Own SDES Simulator and MPLAB 5.20

MPLAB 5.20

MPLAB Screenshot

SDES Simulator

SDES Simulator ScreenShot

To test the code, i supplied “0×39″ as plain text and “0×2f2″ as the key. Both of MPLAB and SDES simulator gives the same cipher text result which is “0xb5″.

Links:
Download Source Code
 Download SDES Simulator
 Download PIC16F84 Datasheet

13 January 2008

Implementing MD5 using PIC16F84

Filed under: Hash — Tags: , , , — edipermadi @ 7:51 am

i’ve search around google to find acticles on how to implement MD5 hash function in a microcontroller such PIC16F84 but results nothing. Just for for the sake of curiosity, suddenly i code MD5 algorithm into PIC16F84 assembly language. During debugging process. i dealt much with “big endian” and “little endian” format which is confusing :) . Finally i’ve done everything properly. I compared hash of “abc” string using DAMN Hash Calculator and using MPLAB simulator and got the same hash. It was amazing. I also simulate the code using Oshon Soft “PIC Simulator IDE” which gives me the same result as well. Big thanks to oshon soft, your shareware program helps me much.

To implement all of this i used approximately 900 bytes of PIC16F84 program memory. I have not optimize this code, please let me know if you have any other algorithm which is faster than the original one. Now I am currently trying to implement SHA1 or SHA160 using AT89S52 microcontroller. Hopefully someday i will implement all of hashing function on microcontroller.

I have modified the original code of this. The original code requires 20.45 ms to compute hash, however the modified one just takes 15.99 ms which is 4.4 ms faster.

Here some screenshot, i took it from damn hash calculator, MPLAB and PIC Simulator IDE.

  Damn Hash Calculator

Damn Hash Calculator Screenshot

 

MPLAB Simulator 5.20

 MPLAB 5.20 MD5 Screenshot

 

Oshon Soft PIC Simulator IDE

 Oshon PIC IDE Simulator Screenshot

RELATED STUFF

Downloads:
Source Code v1.0
Source Code v1.1
 PIC16F84 Datasheet
 Mid-Range Reference Manual

References:
 MD5
 Ronald Rivest
RFC1321 

Links:
 Hash
 PIC16F84
 PIC16F84 Programmer
 PIC16F84 Development Board

Official Website:
Microchip
Ronald Rivest
RSA Security
PIC IDE Simulator

12 January 2008

Implementing Simplified DES using EEPROM

Filed under: encryption — Tags: , — edipermadi @ 7:35 am

Just another SDES stuffs. I ‘ve generated Simplified DES Encryption Lookup Table by mapping both of key and plain text input becoming cipher text output. Since Plaintext was 8 bit width and key was 10 bit width, so you will have 2^18 combinantion or 262144 exactly. I put key as leftmost input and plain text as rightmost input.

If you name key as k1:k10 and Plain text as p1:p8, you can name the this process as a function f(k1:k10,p1:p8). The same way as decryption, you can name it as g(k1:k10;c1:c8). Both of encryption and decryption are just lookup table. you can implement it using EEPROM for example. It is fast, cheap and easy to implement. Have a look at both two picture below, it describes you how this process works.

Encryption Mode
EEPROM SDES Encrypt

Decryption Mode

EEPROM SDES Decrypt

Click links below to download lookup Table:
Download Encryption Lookup Table
Download Decryption Lookup Table

Simplified DES Simulator

Filed under: encryption — Tags: , — edipermadi @ 7:31 am

Yet another stuff about Simplified DES, i’ve coded simple program to simulate Simplified DES. I released this program as freeware. You can redistribute it as much as you want, without altering the program. You can use the program for educational use, personal use, commercial use or whatever you want.

Yeah, i think it much better than scretching on bit mapping your paper. It drives you crazy. I hope this program simplifiy your job or your homework perhaps :) .

I’ve verified the program and found no error. If you found something funny, wrong, stupid and unusual, please mail me on edipermadi at gmail dot com.

But, i totally take no care about innacuracy, mistakes, or even damages caused by this program. So please take your own risk and have fun. If you download and use the program, means you are ready to take your own risk.

This is the screenshoot, i made it simple and visually understandable:

SDES Simulator ScreenShot

How To Use It:
- first, select working mode. Initially it set to encryption
- enter 10 bit key, use only “0″ and “1″.
- if you are going to encrypt, enter text into textbox labelled plaintext.
- if you are going to decrypt, enter cipher text into textbox labelled ciphertext.
- Have fun

Download Simulator:
SDES Simulator

10 January 2008

Simplifying Simplified DES

Filed under: encryption — Tags: , , — edipermadi @ 9:15 am

1. SDES Overview
SDES Stands for “Simlified DES” or “Simplified Data Encryption Standard”. This cipher was developed by Professor Edward Schaefer of Santa Clara University. He developed SDES cipher for educational purpose. SDES has much similiar properties compared to real DES. It is usefull for anyone who wants to learn DES. By learning SDES you will gain more understanding on DES.

2. Detailed Step of SDES
SDES is a symetric cipher. It was able to encrypt 8 bit plain text with 10 bit of key. SDES cipher consisted of two round processes and adopt feistel structure, means each block has divided into left side process and right side process. Each side contains 4 bit.

At first, 8 bit plaintext will propagate through (IP) Initial Permutation. The upper nibble of Initial Permutation’s result, which are 4 left most bits, will go to left side process as well as lower nibble, which are 4 right most bits, will enter right process. On the right side process, the lower nibble of IP will be expanded into 8 bit by Expansion Process named “E/P”. This expanded lower nibble of IP then XOR-ed by K1 that comes out from key scheduling process which will explained later. The 8 bits of the result of XORing between K1 and expanded lower nibble of IP then splitted again into 2 nibbles. This two nibbles of XORing result will be used for SBOX input, the higher nibble for SBOX0 and the lower nibble for SBOX1. Since each SBOX results two bits, then total results of SBOX0 and SBOX1 will be 4 bits. These 4 bits then enter another permutation named “P4″ becoming another 4 bit. These another 4 bits will be used to XOR higher nibble of Initial Permutation that we had before. Thats all for first round.

On the second round, the result of XOR between previous complex function and higher nibble of IP will goes to Right side Process. On the other hand the original lower nibble of IP will goes to Left Side Process. The nibble that enter right process of second round will be expanded into 8 bit by expansion process named “E/P”. These expanded 8 bits then XORed by K2 that comes out form key scheduling. The result of that XORing process will be divided into 2 nibble as SBOX inputs. The higher nibble will enter SBOX0 and the lower niblle will enter SBOX1. Again, since each SBOX results2 bits then the total result of SBOX0 and SBOX1 will be 4 bits. These 4 bits then enter another permutation named “P4″. The result of P4 permutation will be used to XOR Left side process of second round.

At Last, the result of XORing process between complex function of senond round and left side process of second round will recombined again with right process of second round becoming 8 bits of data. These 8 bits data the enter last permutation named “Inverse of Initial Permutation” or (IP’). The result of IP’ then called ciphered text.

Moreover, key scheduling process will goes as follows: Key, which 10 bits will enter “P10″ permutation. The 5 left most bits of P10 and the 4 bits rightmost of P10 then experience Circular Left Shift and enter P8 permutation becoming K1. For K2, the each of the result of Circular Left Shift Process will experince another Left Circular Shift by 2 and enter “P8″ and results K2.

For Decrypting process, the step goes along provious process but with switched subkey. Means first round uses K2 and second round uses K1, the result of this process become original plaintext.

(more…)

9 January 2008

Decomposing Blowfish SBOX

Filed under: encryption — Tags: — edipermadi @ 7:30 am

Blowfish is a kind symetric encryption/decryption. It uses feistel structure and employs 4 SBOXes. Each SBOXes has 8 bit input and 32 bit output. Generally, we can assume that each input combination will mapped into unique output combination, although it can’t be guaranteed.

Since blowfish uses feistel structure, each step of encryption and decription was splitted into two 32 bit process. Which are left side process and right side process. Here, one of the side, which is 32 bit will be divided into 4 times 8 bit as SBOX input. The left most 8 bit for SBOX0 and the rightmost 8 bit for SBOX3. Lets see the picture below, hopefully it helps you to understand the process.

Well… Today, i’ll try to decompose blowfish SBOX becoming boolean algebra :)

Initially you will see SBOX0, SBOX1, SBOX2, SBOX3 Like this:

(more…)

Blog at WordPress.com.