The Hardware Side of Cryptography

16 January 2009

Tiny Encryption Algorithm (TEA) on PIC18F4550

Tiny Encryption Algorithm (TEA) was a fast and simple block cipher designed by Roger Needham and David Wheeler. This cipher uses simple 32-bit operations such XORs, Additions, Subtractions and Shifts. Initially, those instructions were not applicable directyly to PIC18F4550. However those 32-bit instructions are availabe under emulation. Here is an example of Tiny Encryption Algorithm (TEA) implementation on PIC18F4550, hopefully you enjoy it . The program are available under GNU Public License v3.0. Please feel free to use it without any warranty implied. Leave your comment below, so i can hear your voice. Happy coding

.

Below is test the result of TEA test vector and it has satisfactory results, the same  as specification ,

Test Vector Result:
Plain Text : 0x0123456789abcdef
Cipher Key : 0x00112233445566778899aabbccddeeff
Cipher Text : 0x126c6b92c0653a3e

Plain Text

Note : Cipher Key is marked red

Cipher Text

Note : Cipher Key is marked red

RELATED  STUFF

Download:
Source Code | v1.0
PIC18F4550 Datasheet
TEA Specification
TEA PIC18F4550 Documentation
MPLAB v8.10

Reference:
Tiny Encryption Algorithm
Roger Needham
David Wheeler

32 Comments

  1. Bonjour

    j’ai besoin d’aide,afin de trouver une solution et de comprendre ce cryptage.

    Y a-t-il un moyen de décrypter ces données ?

    J’ai une carte électronique qui crypte les données avant de les expédies au PC et un logiciel qui me sert a réceptionné les donnes.

    Mais je les envoies a un ami par mail et lui peut les décryptées, mais c’est lui qui
    A c réer ces programmes.

    Mais j’ai le texte en claire et je peux le créer mais pour la sauvegarde c’est le pic16f876
    Qui crypte avant de les ecrires dans la mémoire externe.

    cinqtrois@yahoo.fr

    Merci pour votre aide

    TEXTE CLAIRE 9876543210 ET HORLOGE A ZERO (00:00) ET DATE 1 JANVIER 2000

    —————————–TEXTE CODER———————————

    ªõñƒ-WŠ_ªõQ€R•+¸æä++dÒüŠ¨ì델æöҚ…ÌîúǛÔòîΎ‹¯äÀÒ𶱩îÎÚø¾»¿ôÖÄà ¡¿èÐÀ½êÈÜþ¸¹§èÈØú¼¹
    ¡îÈÜ侧¿òÐÎ⦧¿òÜÀè®U³&4ꬫM&4TSE,>X]E*8BE€_

    õQ€C*8^]A(:BEA2VUK.<^[C4$@A[.F[C4″DnxJnzXLh|XJC[0″ RIJ> R0pXJAJ<~^JNn R<$RN_

    õQ€n RMUlzXOYBn.ROJnzDG_0 DC[2″JC[0$þ

    —————————–SANS LES CARACTERES—————————

    d U 4 M 4 T S E X E 8 B E C
    8 A B E A 2 V U K C 4 A J O
    W F C 4 D n x J n z X L h X
    J C 0 0 R I J R 0 p X J A J
    J N n R R N n R M U l z X O
    Y B n R O J n z D G 0 D C 2
    J C 0

    ——————————CHIFFRE—————————————

    4 4 8 8 2 4 4 0 0 0 2 0

    4=4 fois 8=2 fois
    2=2 fois 0=4 fois

    manque le: 1 3 5 6 7 9
    ——————————MINUSCULE————————————-

    d n x n z h p n n l z n n z

    d=1 fois n=6 fois x=1 fois
    z=2 fois h=1 fois p=1 fois
    l=1 fois

    d n x z h p l

    ——————————MAJUSCULE————————————-

    U M T S E X E B E C A B E A V
    U K C A J O W F C D J X L X J
    C R I J R X J A J J N R R N R
    M U X O Y B R O J D G D C J C

    U=3 fois M=2 fois T=1 fois S=1 fois E=4 fois X=5 fois
    B=2 fois C=6 fois A=4 fois V=1 fois K=1 fois J=9 fois
    O=2 fois W=1 fois F=1 fois D=3 fois L=1 fois R=5 fois
    I=1 fois N=2 fois Y=1 fois G=1 fois

    U M T S E X B C A V K J O W F D L R I N Y G

    manque le: H P Q Z
    —————————————————————————-

    F5 51 00 80 52 95 2B 01 B8 E6 E4 2B 2B 64 D2 FC
    8A A8 EC EB 8D B8 E6 F6 D2 9A 9D 85 CC EE FA C4
    82 87 9B D4 F2 EE CE 8E 8B AF E4 C0 D2 F0 B6 B1
    A9 EE CE DA F8 BE BB BF F4 D6 C4 E0 A0 A1 BF E8
    D0 C0 EE AA A1 BD EA C8 DC FE B8 B9 A7 E8 C8 D8
    FA BC B9 A1 EE C8 DC E4 BE A7 BF F2 D0 CE E2 A6
    A7 BF F2 DC C0 E8 AE 55 B3 06 26 34 EA AC AB 4D
    06 26 34 12 54 53 45 0C 2C 3E 1C 58 5D 45 0E 2A
    38 04 42 45 80 5F

    F5 51 01 80 43 16 2A 38 18 5E 5D 41 0C 28 3A 04
    42 45 41 0A 20 32 16 56 55 4B 02 2E 3C 18 5E 5B
    43 16 34 24 06 40 41 5B 10 3C 2E 0E 4A 4F 57 1C
    3E 2E 02 46 5B 43 0A 34 22 0C 44 13 03 18 6E 78
    0A 18 19 01 4A 6E 7A 58 1E 1F 01 4C 68 7C 58 4A
    43 5B 12 30 22 06 5C 11 05 10 30 20 52 1E 49 05
    4A 3E 20 52 1E 1D 07 10 30 70 58 4A 41 0B 4A 3C
    7E 5E 4A 13 07 4E 6E 20 52 18 1F 07 1E 3C 24 52
    18 1D 07 4E 0F 5F

    F5 51 02 80 6E 20 52 1A 4D 55 12 6C 7A 58 1E 4F
    59 42 6E 2E 52 1E 4F 0B 4A 6E 7A 0C 44 47 5F 10
    30 20 02 44 43 5B 16 32 22 06 4A 43 5B 10 30 24
    FE 47 5F
    ——————————————————————————-

    Comment by PAUL — 22 February 2009 @ 8:27 pm

    • @paul : sorry to say dude, i have no capability to answer your question. your question seems to be solved with cryptanalysis skill, which i have not learned yet. again, sorry to say, your questions about “what type of encryption” and “how to decrypt” with respect to data you supplied is hard to answer.

      However, if you supply me the complete specification of those cipher algorithm. i’m probably able to help you.

      Thank you for visiting my blog. Good Luck

      Comment by edipermadi — 23 February 2009 @ 9:35 am

  2. Hello

    This crypt has an electronic card-based and pic16f876 eeprom
    24lc512.

    The microcontroller saves 40 bytes of data and only the maximum
    Numeric characters. Of 0123456789.

    But before it encrypts the data and that I am reading with a terminal
    The data are received crypts.

    And I do not know how?
    I read your messages you have experience compared moi.Vous talk about
    Encryption microchip microcontroller.

    TEA Encryption on MEL PICBASIC Forum

    Here are links where you can download programs and screenshots.
    Thank you for your help

    http://dl.free.fr/pZZOtQt48

    http://dl.free.fr/akZIJYI3k

    Comment by PAUL — 24 February 2009 @ 5:20 pm

  3. Bonjour

    Merci de ta reponse mais je n’y connais rien et je cherche sur divers forums et
    Personne ne peut me dire de quel cryptage ?
    .
    Ce texte crypté vient d’une carte électronique à base de pic16f876 et une eeprom
    24lc512.

    Le microcontrôleur sauvegardes de données de 40 octets maximum et seulement des
    caractères numériques. 0123456789.

    Mais avant il crypte ses données et des que je fais une relecture avec un terminal
    les données sont reçus cryptes.

    Comme le cryptage est brouillé par des caractères, chiffres, lettres d’alphabet

    J’ai fait des testes avec l’heure a 00 :00 et la date 01/01 /2000 et j’ai fait une
    sauvegarde a chaque minute, sauvegarde a 00:00, 00:01,
    00:02 , 00:03, 00:04, 00:05, 00:06, 00:07, 00:08, 00:09, 00:10
    A fin de comprendre comment le cryptage se fait, donc + 1 a chaque fois
    Et même avec très peu de chiffres ce n’est pas évident.

    Et je ne sais pas comment faire ?

    Voici des liens ou vous pouvez télécharger les programmes et les captures d’écran.
    Merci de votre aide

    http://dl.free.fr/pZZOtQt48

    http://dl.free.fr/akZIJYI3k

    Comment by paul — 25 February 2009 @ 6:42 pm

  4. Hello

    Thank you for your answer but I do not know anything and I’m looking on various forums and
    Nobody can tell me what encryption?
    .
    This text comes from an encrypted electronic card-based and pic16f876 eeprom
    24lc512.

    The microcontroller saves 40 bytes of data and only the maximum
    numeric characters. 0123456789.

    But before it encrypts the data and that I am reading with a terminal
    the data are received crypts.

    As encryption is obscured by characters, figures, letters of alphabet

    I did tests with a time 00: 00 and the date 01/01 / 2000 and I made a
    every minute backup, backup 00:00, 00:01,
    00:02, 00:03, 00:04, 00:05, 00:06, 00:07, 00:08, 00:09, 00:10
    In order to understand how the encryption is done, so + 1 each time
    Even with very little data that is not obvious.

    And I do not know how?

    Here are links where you can download programs and screenshots.
    Thank you for your help

    http://dl.free.fr/pZZOtQt48

    http://dl.free.fr/akZIJYI3k

    Comment by paul — 25 February 2009 @ 6:44 pm

  5. @paul:
    OK’ i got your file. let me try first.

    Comment by edipermadi — 26 February 2009 @ 9:34 am

  6. Hello

    You have my email address and I let you think

    Comment by paul — 26 February 2009 @ 4:48 pm

  7. Hello

    I do not think encryption but data compression.
    On that website he talks about compression eeprom 512k and it’s like
    for my problem

    Good courage
    Thank you
    http://www.picbasic.co.uk/forum/showthread.php?t=6521&highlight=compression
    http://www.picbasic.co.uk/forum/showthread.php?t=2813&highlight=compression

    Comment by PAUL — 1 March 2009 @ 8:00 pm

    • @paul : sounds unfamiliar to me. please try to ask other people which master compression.

      Comment by edipermadi — 5 March 2009 @ 10:58 am

  8. Hello

    I do not know, I guess it’s quality of compression, because I do not know
    Not how it is encrypted.

    Encryption reduces the bytes and is the opposite.
    Do you know the encryption microcontroller and data from the eeprom 512k.

    Thank you for your help

    Comment by paul — 5 March 2009 @ 5:18 pm

  9. edipermadi:
    I have a question, i have written vhdl code for TEA and verified that it works by using your results…however, i tried varying the number of rounds say for example to 40,64 or any such value and it doesn’t work anymore! do i need to change the delta value or something, because according to the author, TEA has *variable* rounds..? please help!!! thanks

    Comment by Shirley G — 10 March 2009 @ 10:56 am

    • @ Shirley G :
      Hmmm… let me try first…
      Or you can try to emulate your code in another easy laguage such as PHP. It will help you to debug your VHDL code.

      Comment by edipermadi — 10 March 2009 @ 6:06 pm

  10. Hello

    I too do not know how and I do not understand what
    you ask me (the delta change).

    thank you

    Comment by paul — 10 March 2009 @ 6:01 pm

  11. @edipermadi:
    I don’t know php, and I am trying to implement this design on an FPGA (hence i need it in vhdl) or C..thank you for responding..please let me know if you get variable rounds to work

    @paul:
    I meant, does the delta value need to be changed (from the constant value 0x9e3779b9) in accordance with the number of rounds..This is just a guess..because if i just change number of rounds to say 40 or something like a random number if i encrypt and decrypt it using that random value, i do not get the original plaintext…it works perfectly only if the number of rounds is 32..i hope this explains my question more clearly….

    Comment by Shirley G — 11 March 2009 @ 12:27 am

    • Shirley G :

      I think the problem comes from the delta constant. The delta constant in your encryption and decryption routine were fixed to 32 rounds.

      For Example (Normal Situation 32 rounds)
      In Encryption : delta = 0x9e3779b9
      In decryption : delta = (0x9e3779b9 x 32)% 0xffffffff = 0xc6ef3720

      For example, if you want to change it to 64 rounds, then
      In encryption : delta = 0x9e3779b9
      In decryption : delta = (0x9e3779b9 x 64)% 0xffffffff = 0x8dde6e40

      it hope it helps. please tell me whether this tweak works or not.

      Comment by edipermadi — 11 March 2009 @ 10:13 am

  12. Bonjour

    Je ne sais pas moi-même comment faire pour décrypter
    Ni comment il a était crypte.
    Je vous serai très reconnaissant si vous trouvez.
    Peut être qu’au niveau des logiciels est une fois
    Désassemble il y a la routine de décryptage.
    Merci de votre aide

    Hello

    I do not know myself how to decrypt
    Or how it was a crypt.
    I shall be very grateful if you find.
    May be that level of software is once
    Disassembler there is the decryption routine.
    Thank you for your help

    Comment by paul — 11 March 2009 @ 4:56 pm

    • @paul :
      i have spent my time to guess what is going on actually in your application. that was hard. sorry to say, i cannot give you any solution. perhaps, reversing from the code would be easier. i really appologize for this.

      Comment by edipermadi — 12 March 2009 @ 9:38 am

  13. Hello

    And thank you again for your help, I think he has a bit of encryption
    And generate random characters to scramble and make decoding difficult.

    But you’ve written a routine encryption?
    The I work on a program for Pic 162628 and I looking for a routine
    in basic or assembler which I will encrypt the data before stored in eeprom.

    Thank you

    Bonjour

    Et encore merci de votre aide, moi je pense qu’il a un peut de cryptage
    Et des caractères génèrent aléatoirement pour brouiller et rendre difficile le décodage.

    Mais vous vous avez écris une routine de cryptage ?
    La je travail sur un programme pour un Pic 162628 et je cherche une routine
    en basic ou assembleur qui va me crypter les données avant de stockée dans l’eeprom.

    Merci

    Comment by paul — 12 March 2009 @ 4:51 pm

  14. @edipermadi:

    It works!! I tried what you said and it can work for any number of rounds..:) Many many thanks!!!

    Comment by Shirley G — 13 March 2009 @ 12:05 am

  15. @Shirley G :
    Congratulation,,🙂

    @paul :
    i guess, decrypting something is a matter of algorithm. in the other words, we should use decryption routine which is the inverse of encrypting routine. the next problem is to guess how the data was encrypted. and that is the reason why decryping an unknown algorithm is a tough question. this problem must be solved by cryptanalysis.

    Comment by edipermadi — 13 March 2009 @ 9:40 am

  16. Hello

    I know this is not easy to find it so he was
    Encrypted.
    And you even know write programs for microcontrollers?
    If yes what language.

    Good luck

    Bonjour

    Je sais que ce n’est pas facile de trouver de qu’elle manière il a était
    Cryptée.
    Et vous-même savez écrire des programmes pour microcontrôleurs ?
    Si oui avec quel langage.

    Bon courage

    Comment by paul — 13 March 2009 @ 5:13 pm

    • @paul :
      dude, sorry to say. i tried once again to reveal the algorithm you supplied, but i have no idea at all. it probably written in C, assembly of basic. but all language are resulting the same thing which is native code for microcontroller anyway. so i cannot guarantee which language is used in that case.

      Comment by edipermadi — 19 March 2009 @ 9:51 am

  17. Hello

    Thank you for your help, you know your own schedule pics?
    Because I need a routine for my encryption program on a pic
    16f628A I go to basic language pro.
    http://www.microengineeringlabs.com/

    so if you know using this compiler?

    Thank you

    Comment by paul — 19 March 2009 @ 5:18 pm

    • @paul:
      i coded several cipher algorithm. all of them were coded in assembly language. most most them were focused on PIC16 while a small part belong to PIC18. You can check it out on http://cryptonutter.googlecode.com

      i’m not familiar to basic compiler ;p

      basically basic programming is easy you could use it without any problem. OK, good luck

      Comment by edipermadi — 23 March 2009 @ 9:52 am

  18. hello…can you explain how the TEA algorithm works and diagram above pls?We use the TEA algorithm on our thesis title “a file encryption and decryption system”…We are having hard time to defend it,and we will redefend it one more time on Oct.26,2009…It will be our last chance or else we will failed our thesis 1.They are asking what is the green box,the red circle,the shifts,the delta,and what is the explanation for k(0) to k(3)and why it has left and right?how it process step by step?Please Please help us…God bless…

    Comment by Ysa — 20 October 2009 @ 11:44 am

  19. hello…can you explain how the TEA algorithm works and diagram above pls?We use the TEA algorithm on our thesis title “a file encryption and decryption system”…We are having hard time to defend it,and we will redefend it one more time on Oct.26,2009…It will be our last chance or else we will failed our thesis 1.They are asking what is the green box,the red circle,the shifts,the delta,and what is the explanation for k(0) to k(3)and why it has left and right?how it process step by step?Please Please help us…God bless…thanks for the help in advance…

    Comment by Ysa — 20 October 2009 @ 12:26 pm

    • @Ysa
      hello..Ysa, i’ll send you the explanation about TEA through your yahoo email. i hope i could mail it tomorrow.

      Comment by edipermadi — 20 October 2009 @ 5:00 pm

      • Dear Edipermadi,

        Thank you so much for the email that you send me about the TEA Algorithm..I will study it and hopefully I can explain it well to our panelist…

        Again,thank you for the help…

        Comment by Ysa — 23 October 2009 @ 9:17 pm

  20. BIG BIG THANKS FOR THE BIG BIG HELP

    Comment by Ysa — 23 October 2009 @ 9:20 pm

  21. i need a assembly langauge code for rsa algorithm..please semd me as early as possible..

    Comment by navya — 2 April 2010 @ 12:17 am

    • @nayva:
      sorry friend, i dont have it yet.

      Comment by edipermadi — 6 April 2010 @ 9:51 am

  22. can u teach me how to do the VHDL TEA coding for 32 bit… i really weak in programming.

    Comment by rezurret — 1 August 2010 @ 3:05 am


RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

Blog at WordPress.com.

%d bloggers like this: