Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 16 of 16

Thread: AES Encryption for Teensy 3.0

  1. #1
    Senior Member
    Join Date
    Nov 2012
    Location
    Chipping Norton, UK
    Posts
    278

    AES Encryption for Teensy 3.0

    Can anyone point me in the direction of an AES encryption library that would be compatible with Teensy 3.0 and Teensyduino ?

    I have been using this one with the Teensy 2.0 and Teensy 2.0 ++:

    https://github.com/DavyLandman/AESLib
    Last edited by Experimentalist; 12-13-2012 at 03:21 PM.

  2. #2
    Senior Member
    Join Date
    Nov 2012
    Location
    Chipping Norton, UK
    Posts
    278
    I will post some more here soon, but it's late and my brain hurts but I believe I have nailed it. I am successfully encrypting and decrypting data in 16 byte chunks with AES 256 bit encryption.

    I have adapted the code found at the following URL to work with my project:

    http://www.efgh.com/software/rijndael.htm

  3. #3
    Hi there

    I'm very interested in encryption for Teensy devices but also very much a novice.

    Please could post more details about your AES implementation that you managed to get working?

    I'm trying to implement RSA however AES would be a good start.

    Many thanks

    Andy

  4. #4
    Senior Member
    Join Date
    Nov 2012
    Location
    Chipping Norton, UK
    Posts
    278
    Hi

    For some reason I decided to make cosmetic, not functional, changes to the library referenced previously replacing all occurrences of rijndael with AES. Apart from that the library is essentially unchanged.

    It is not my work and I credit the author:

    Philip J. Erdelsky
    pje@efgh.com
    http://www.alumni.caltech.edu/~pje/

    and reference the source:
    http://www.efgh.com/software/rijndael.htm

    Anyway I have knocked up a demo sketch to get you on your way, I am also a novice so don't be too hard on me. I use the hardware serial output of the Teensy 3 for debugging so you may need to edit that to see some output but the code is self explanatory. Please post back any advances with your work as I am interested in hardening my encryption. I have zipped the sketch and library which was all tested on Arduino 1.0.5 with Teensyduino 1.16 and attached it, hope it helps you or others.

    Copy the AES folder to \arduino-1.0.5\libraries folder, copy the AESDemo folder to your sketch folder and run it up

    Have fun

    Ex
    Attached Files Attached Files

  5. #5
    Hi Ex

    Thank you very much for taking the time to package your work. I'm very keen to have a look and see it working unfortunately I'm still waiting for my Teensy in the post so I have to be patient and wait. I'm good with modern programming and encryption concepts but a complete novice in C and C++ and especially in packaging these sketches. So you creating a starting sketch all packaged is great because I can then work on understanding it and can make tweaks if I can spot anything.

    I'll give an update when I have the chance to use it :-)

    Thanks

    Andy

  6. #6
    Hi Ex

    I've had a quick look at the library and it certainly looks like it should do the trick. What was the performance of the library like? How long did encryption and decryption take?

    Thanks

    Andy

  7. #7
    Senior Member
    Join Date
    Nov 2012
    Location
    Chipping Norton, UK
    Posts
    278
    Hi

    I too am a complete novice with C and C++. I am a really a hobbyist programmer and write mainly in C# using the .NET framework so C and C++ are quite alien to me. I must admit I have never bench marked the performance as yet. I am encrypting small keyboard macros for saving to SD card and decrypting them when they are read back; the data volume is small so performance has not been an issue to date. I am writing a client server type application which is constantly evolving as I go so lots of debug code and little concern about performance as yet .

    With regard to taking the time to package it up I did this as others on this forum have spent their time helping me and have been very generous so I thought I should give something back.

    If you are waiting for the Teensy and wish to use the sketch I sent as is you will need a TTL to USB converter or similar to connect the Teensy hardware serial to the PC. I prefer to use the hardware serial for debug output as you do not have to keep reconnecting every time you download. I am also using the software serial for my application so it is not available.

    Anyway if you are interested this is what I use to connect the hardware serial to the PC:
    http://www.amazon.co.uk/gp/product/B008AGDTA4

    Cheap as chips and works great. As you can see from the link I am in the UK, not sure where you are. If you do get that cable you need to link the green cable to Pin0 and the white to Pin1 on the T3. If you are using the same USB hub for the Teensy and the TTL converter you don't need to connect the red or black wires. Find what serial port the TTL converter is on and connect to it with your a PC terminal emulator, set the baud to 38400, 8 databits, no parity and 1 stop bit

    Hope that helps
    Ex.
    Last edited by Experimentalist; 10-10-2013 at 10:11 AM. Reason: Fix URL link

  8. #8
    Hi Ex

    I'm in the UK too. This device you mention, is my understanding correct that you are using that because it makes it easier to work with the Teensy 3.0 than connecting the micro USB cable? However I don't have to have it? Sounds like a useful purchase, just trying to get my head round it.

    That performance sounds fine, I would only be looking at encrypting a small packet of information which it sounds like could be done very quickly. My preference is RSA encryption but due to performance I'm not sure how secure it will be because of the concessions I need to make. So at least this AES should definitely work and maybe I could combine it with my potentially weak RSA. I would probably look to add some kind of initialisation vector to the AES code if there isn't one already.

    One of the scenario I'm catering for is that the device itself has been compromised. With AES the private key could be swiped. With a public key it wouldn't matter.

    Cheers

    Andy

  9. #9
    Senior Member
    Join Date
    Nov 2012
    Location
    Chipping Norton, UK
    Posts
    278
    Hi

    The device is used to allow a permanent connection to the Teensy for debugging. Most users will use the software serial for this job, which in Windows will require a driver to be installed. However when new code is downloaded to the T3 the connection to the software serial is lost and must be reopened. By using hardware serial for debug output I can just leave my terminal window open on the PC and as soon as the T3 boots it can output serial debugging messages and I see them without worrying about timing issues. If using software serial you cannot connect to the T3 until after it boots and has opened the USB serial port. I use the software serial for communication between my PC application and the T3 and so it is not an option for me for debugging. The device is a couple of quid and well worth having even if you do not intend to use it yet. It does not replace the micro USB connection it is in addition.

    To get over the private key issue I use a keypad and a PIN. I format the 2048 bytes of EEPROM with random data and use an algorithm to retrieve 16 bytes from the 2048 for the private key based on the PIN entered. If the PIN is entered 3 times incorrectly the EEPROM is wiped and the SD card data is effectively rendered garbage. It works for me at the moment. Had considered some kind of RFID or similar enhancement later.

    Ex

  10. #10
    Senior Member
    Join Date
    Jun 2013
    Location
    So. Calif
    Posts
    2,828
    Quote Originally Posted by Experimentalist View Post
    Hi

    I too am a complete novice with C and C++. I am a really a hobbyist programmer and write mainly in C# using the .NET framework so C and C++ are quite alien to me. I must admit I have never bench marked the performance as yet.
    In Microsoft's .net for C#, I believe it's correct to say that all programs written from that environment compile to bytecode for the virtual machine interpreter (MS has their own names for these, e.g., common runtime environment). But my experience (limited it be) with this bytecode interpreter scheme is that C/C++ code written to run on the CPU rather than via the interpreter, is a 100 times faster, as is the case for most all bytecode interpreters, including, say, the one for Java on windows or linux.
    Whenever I run a C program that doesn't need the interpreter, and does not use the windows GUI/graphics, I am ever dumbfounded how fast this kind of code runs on today's fast CPUs in PCs. When burdened with MS's interpreter, and the layer upon layer of abstractions in object-oriented-overdone, we see the lackluster response time in GUI programs. If only...

  11. #11
    Senior Member
    Join Date
    Nov 2012
    Location
    Chipping Norton, UK
    Posts
    278
    We were actually referring to the performance of the AES library on the T3

  12. #12
    Senior Member
    Join Date
    Nov 2012
    Posts
    412
    @Experimentalist - AES expert

    Along the encryption lines ... this is a "tad off topic" but have you ever worked with 1-wire Atmel ATSHA204 on the Teensy 3?
    I am trying to make this chip work on the T3 but due to timing it seems to not function.

    https://www.sparkfun.com/products/11551
    Last edited by t3andy; 10-11-2013 at 09:04 PM.

  13. #13
    You should be able to use LibTomCrypt, which is written in ANSI C. It supports many ciphers, including AES and RSA.

    Don't forget to use a chaining mode (I recommend CBC mode) and an IV (e.g. all zero's with CBC mode) when you use a block-cipher like AES.

    Also, make sure you have sufficient entropy (from mouse movements or a noise diode, for example) to seed the CSPRNG (cryptographically secure pseudo-random number generator) which you use to generate encryption keys!
    Last edited by bigpilot; 10-14-2013 at 03:42 PM.

  14. #14
    Senior Member
    Join Date
    Nov 2012
    Location
    Chipping Norton, UK
    Posts
    278
    Sorry for the slow response, somehow unsubscribed from the thread. In answer to your question no I have not worked with any hardware based solutions and I really am no expert, just trying to help others as best I can in return for what others have done for me on this forum :0)

  15. #15
    Senior Member
    Join Date
    Nov 2012
    Location
    Chipping Norton, UK
    Posts
    278
    Quote Originally Posted by bigpilot View Post
    You should be able to use LibTomCrypt
    Thanks for the pointer looks like a great piece of work and well worth investigating further. It also looks like I need to spend some time reading up on chaining modes and CSPRNG. It seems like the thread is turning into a general encryption on the Teensy thread which can only be a good thing. I originally went for AES as it was reasonably light weight but I was really just looking for options of protecting data on my SD card and a mechanism for rendering it useless if the project was stolen. I have recently restarted work on this project and any improvements can only be a good thing.

  16. #16
    Senior Member
    Join Date
    Nov 2012
    Location
    Chipping Norton, UK
    Posts
    278
    I particularly like the licence mechanism for the libtom projects :0)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •