The Hardware Side of Cryptography

9 February 2008

An AES implementation on PIC16F877

Filed under: encryption — Tags: , , , — edipermadi @ 2:16 am

Yup, previously i’ve implemented AES (Rijndael) 128 bit using PIC16F84. But you knew that the performance was not so good. Since program size and speed are trade off and limitation of PIC16F84 memory space, i decided to decrease the the performance to fit PIC16F84 memory limit.

Here, i ported the code into PIC16F877 which has larger memory 8) . Of course to speed up the application. By using PIC16F877, i felt much more flexible to rock the microcontroller. As a benchmark, porting the code from PIC16F84 into PIC16F877 will speed up the encryption process by 56% as well as speeding up decryption by 37%.

Speed Benchmark:

  • Encryption
    PIC16F877 : 3834 cycles
    PIC16F84 : 7157 cycles
  • Decryption
    PIC16F877 : 5250 cycles
    PIC16F84 : 28504 cycles

Today, i woke up till 3 AM just to make a significant progress of this cipher implementation. The new version which is v1.7 has a giant step distance from its predecessor. The latest version (v1.7) requires less memory program and it’s able to do inverse mix column twice faster than the previous version (v1.6). Nice progress huh!😀 .

I will keep modify both of codes to rock the microcontroller and speed up the whole process. If necessary, i will also port the code into higher microcontroller platform such PIC18x. However, the implementation of AES (Rijndael) cipher in this post is intended for performance oriented application. Now, I’m currently developing another version of AES (Rijndael) cipher on PIC16F877 which are AES-192 and AES-256. Keep visiting this blog to get the latest release. Have a nice day😀 !

Hot News! : Fast AES cipher on PIC18F4550


Source Code AES 128-bit | v1.0 | v1.1 | v1.2 | v1.3 | v1.4 | v1.5 | v1.6 | v1.7
Source Code AES 256-bit | v3.0
PIC16F877 Datasheet
Mid Range Reference Manual
AES Publication (fips-197)
Practical Implementation of Rijndael S-Box Using Combinational Logic

Advanced Encryption Standard
Vincent Rijmen
Joan Daemen

AES (Rijndael) Simulator

Official Website
– Microchip
– Advanced Encryption Standard


  1. Hello, Thnaks alot for proving the asm codes for implementation of AES on PIC18f4550. Presently this code runs in MPLAB using C18 compiler under simulation mode. when I am trying to debug, I am not able to put a break point anywhere in the code. I am trying to modify the user key and plain text. Can u please help?

    Comment by Bappaditya Mandal — 3 September 2008 @ 10:12 am

  2. if i want to change the key, how to reinitialize the “subkey buffer”?…..which part of the code has that?. Thanks….wud be grateful if anyone answers this.

    Comment by Bappaditya Mandal — 3 September 2008 @ 11:28 am

  3. AES implementation on PIC16F877 has no subkey scheduling routine. Scheduling routine was done on the fly, ir runs concurently while encrypting/ decrypting

    to initialize subkey, please fill k0 – k15 registers and invoke “encr” routine. Once you invoke that function, you need to reinitialize subkey buffer again, if you wish to use the same key.

    Good Luck,,

    Comment by edipermadi — 3 September 2008 @ 12:42 pm

  4. Kindly tell me the speed you have mentioned is either clock cycles or instruction cycles?

    Comment by Muhammad Naveed — 16 February 2009 @ 2:18 am

  5. Also tell me that this speed is for one round or whole encryption of one block?

    Comment by Muhammad Naveed — 16 February 2009 @ 2:37 am

    • @ Muhammad Naveed : dude, i measured the speed based on the number of cycles. The speed are listed below, assuming that the crystal frequency is 4MHz:
      128-bit version (v1.7):
      – enc : 3834 cycles = 3834 us/block = 3.834 ms/block = 260.83 block/s = 4173.28 bytes/s = 4.2 kByte/s
      – dec : 5250 cycles = 5250 us/block = 5.250 ms/block = 190.48 block/s = 3047.68 bytes/s = 3 kByte/s
      256-bit version (v3.0):
      – enc : 5373 cycles = 5373 us/block = 5.373 ms/block = 186.12 block/s = 5955.84 bytes/s = 5.9 kByte/s
      – dec : 7400 cycles = 7400 us/block = 7.400 ms/block = 135.14 block/s = 4324.48 bytes/s = 4.3 kByte/s

      Comment by edipermadi — 16 February 2009 @ 8:13 am

  6. Thank you very much.

    Comment by Muhammad Naveed — 17 February 2009 @ 2:51 am

  7. thanks for your open source of aes encrypt😉

    I translate it for a 16F193x ,

    the encrypt and decrypt function work well if I use first encrypt function and after decrypt function
    call encr
    call decr

    but I would only use decrypt function (and not call encrypt)
    call lastk
    call decr

    I don’t understand the part which talk about ‘lastk’ …

    how can I use lastk routine?


    Comment by diabolo — 8 October 2011 @ 3:10 am

    • Hi.

      Lastk is actually function to generate subkeys

      Comment by edipermadi — 10 October 2011 @ 1:16 pm

      • thanks for this information

        but when I must call “lastk” function for use only decrypt routine (I tested before, it doesn’t work) ?
        I don’t want to implant a crypt function and lost ~4000 cycles😀


        Comment by diabolo — 10 October 2011 @ 2:48 pm

  8. I don’t get it too!!
    How do I decrypt DIRECTLY without using encryption routine?
    Just key and ciphertext!

    call lastk
    call decr

    Does NOT work!

    Comment by Borg — 13 August 2013 @ 12:00 am

  9. Decrypt doesn’t work, any idea?

    Comment by Alexandre Marques — 8 July 2015 @ 9:05 am

RSS feed for comments on this post. TrackBack URI

Leave a Reply

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

You are commenting using your 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

%d bloggers like this: