The Hardware Side of Cryptography

25 January 2008

Implementing Blowfish using PIC16F877

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

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

Official Website
– Microchip
– Blowfish

19 Comments »

  1. 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

  2. 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

  3. 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

  4. 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

      • @Jim.
        Hi i used MPLAB 8.1.0 to compile that code

        Comment by edipermadi — 1 June 2009 @ 9:38 am

  5. 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

  6. Hi !
    I was looking at your code, also 18f4550 version. Where do you get the test vectors ? I don’t see where is the slow, complicated key schedule (it says encryption routine should be invoked 521 times) … do you need to do the key schedule externally and “hardcode” (in Flash-memory) the resulting sboxes and p-array ?

    Comment by Steffen — 8 May 2011 @ 3:47 am

    • i derived the test code, from the C implementation. please inform me if i made it wrong. the implementation i made is basically optimized for lookup table based implementation. things that are convertible to table were stored as tables

      Comment by edipermadi — 9 May 2011 @ 8:12 pm

      • Hi !
        I guess the encryption routine itself is ok … I mean the key schedule should take much longer. Wikipedia (I hope they’re right, but I guess so) says all the tables (4 kilobyte s-box, and the p-array) are key-dependent. For a new key, these tables have to be constructed iteratively, whereby the encryption routine runs 521 times. This means key setup is very slow, compared to other algorithms, and also that, normally, all tables should end up in RAM (or maybe can be written to Flash, although then frequent key changes won’t be nice for the flash …). Did you try the test vectors (from official Blowfish website), also for keys other than zero ? Even the first test vector (with key zeros, and data zeros) doesn’t seem to match …

        Comment by Steffen — 10 May 2011 @ 4:37 pm

  7. hii..
    how about media library java for blowfish?

    Comment by ayyank — 19 May 2011 @ 4:35 pm

  8. hi..can any one help me with c code for blowfish algorithm…

    Comment by sowmya — 27 February 2012 @ 11:28 pm

  9. Magnificent beat ! I would like to apprentice while you amend your web site, how could i subscribe for a
    blog website? The account aided me a acceptable deal.
    I had been a little bit acquainted of this your broadcast offered bright clear idea

    Comment by http://mycreditrestoration.org — 28 April 2013 @ 9:48 am

  10. I blog quite often and I seriously thank you for your information.
    The article has truly peaked my interest. I
    am going to bookmark your site and keep checking for new information
    about once per week. I opted in for your RSS feed as well.

    Comment by http://gregorybaskin.com/turtle-fighting-turtle-fighting-style-turtle-fighting-games/ — 22 May 2013 @ 11:42 pm

  11. Hello! Do you know if they make any plugins to assist with Search Engine Optimization?
    I’m trying to get my blog to rank for some targeted keywords but I’m not seeing
    very good results. If you know of any please share. Cheers!

    Comment by www.reedbeta.com — 3 June 2013 @ 11:38 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: