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 that 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
. 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.
Hot News! : Fast Blowfish Implementation on PIC18F4550
RELATED STUFF
Download:
Blowfish Source Code | v1.0 | v1.1 | v1.2 | v1.3
PIC16F877 Datasheet
Mid Range Reference Manual
References:
Blowfish Cipher
Bruce Schneier




I think blowfish is good encryption algorithm but how can minimize the S-boxes from 4 to 1 with the same entry?
Comment by Kassahun — 5 November 2008 @ 4:19 pm
I guess, you got to learn the behaviour of a large lookup table on PIC16 micros more. In PIC16 micros, a large sbox is fragmented as a group of 256 bytes of cell.
In my application, i concatenated all Blowfish SBOXes as a single large lookup table that consists of 4x(4×256) bytes, or 16 code segments in short. This will approximately takes 4096 bytes. At this phase, the lookup table is supposed to be page aligned, to avoid misinterpretation.
The interpretation of lookup table is decided by the value of pclath and pcl register. The pclath register will point to the segment of lookup table while pcl will point more detail to the cell inside segment that pointed by pclath.
That was the reason why i concatenated all sbox into a single table with a single entry point. If i’m not mistaken, that way works well. Please check out other references that describe about large table implementation. And please inform me if i were wrong.
Good Luck,,
Comment by edipermadi — 5 November 2008 @ 6:22 pm
if i already burned the hex code u given in the pic16f877, how do my PIC encrypt/decrypt the data from i/o port? How do i test whether am i encrypted/decrypted the data?
Comment by Jim — 22 May 2009 @ 2:37 pm
@jim
Hi jim. I coded the software for simulation purpose only and it has nothing to do with I/O, so can observe nothing there. you have to modify the code in order to gain access form hardware.
goodluck.
Comment by edipermadi — 22 May 2009 @ 3:55 pm
Do you know how to modify it? Ermm.. cause i am not familiar with assembly code.. Do you have C Code file for it? Hope you can give me some hint or advices cause i am doing this project and i am stucked!!
Thanks for the Fast reply.
Comment by jim — 23 May 2009 @ 11:26 pm
@Jim.
Hi Jim, i suggested ypu to write additional lines that copy data from somewhat PORT to blowfish buffer. In addition, you also required to create handshaking feature that enable external hardware to ask microcontroller to perform encryption or decryption.
For hardware ciphering machine. I ve constructed an electronic enigma. you may locate it on this blog. The hardware uses serial port for communication. I hope this will be nice.
Good Luck.
Comment by edipermadi — 24 May 2009 @ 1:40 pm
Hi,
May i ask what compiler u using to compile the C code for blowfish? Because i try to compile with microC there exist lots of internal error?
Comment by Jim — 29 May 2009 @ 9:35 pm
i am now try to implementing blowfish with ATmega32 but the problem is the lock up table the inner bits is 2 bits and the outer bits is 6 bits is that right someone help me….?
Comment by mohamed samir — 1 July 2009 @ 11:27 am
@mohamed samir:
Hi, the look up table is basically the implementation of 32 bit table in 8 bit environment. each array element has to splitted into 4 bytes. Therefore you will have 4 look up tables.
Comment by edipermadi — 3 July 2009 @ 12:47 pm
@Jim.
Hi i used MPLAB 8.1.0 to compile that code
Comment by edipermadi — 1 June 2009 @ 9:38 am