CryptoAccel library use with T4.1 for encryption/decryption

EcodroneSRL

Member
Hello everyone!

I'm currently developing a code, running on Teensy4.1, that stores some variables in the SD card. Before the storing, we want to encrypt the plaintext and we tried to use the CryptoAccel library. We tried to run this test code to see if the library works well:

Code:
#include "CryptoAccel.h"

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  delay(1000);
  Serial.println("Starting :)");
  unsigned int i;
  unsigned char aeskey[16] = "Password";
  
  unsigned char keysched[4*44];
  mmcau_aes_set_key(aeskey, 128, keysched);
  unsigned char in[16];
  for(i = 0; i < sizeof(in); i++)
  {
    in[i] = 49+i;
  }
  for(i = 0; i < sizeof(in); i++)
  {
    Serial.write(in[i]);
  }
  Serial.println();
  unsigned char out[16];
  mmcau_aes_encrypt(in, keysched, 10, out);
  for(i = 0; i < sizeof(out); i++) 
  {
    Serial.write(out[i]);
  }
  Serial.println();
  unsigned char iv[16];
  mmcau_aes_decrypt(out, keysched, 10, iv);
  for(i = 0; i < sizeof(iv); i++) 
  {
    Serial.write(iv[i]);
  }
  Serial.println();
}

void loop() {
  // put your main code here, to run repeatedly:

}

Now, the result of the operations are shown in the image.

teensy_enc_dec.png

The strange thing is that the Serial.write(iv) instruction prints the AES key instead of the decrypted plaintext. Can someone tell us why this happens? We are newbies of this type of features of Teensy boards, and we read also posts on this forum about the compatibility of the CryptoAccel library with Teensy 4.1 and we have a lot of doubts, so thank in advance to everyone for the support!
 
Code works as expected on T3.6.

As you found T4 and T4.1 compile with no errors but not even close to working. Looks like assembly language for 3.x only.
 
Thank you for the clarification!

UPDATE:
In these days we found the dcptst.ino code by manitou:

https://github.com/manitou48/teensy4/blob/master/dcptst.ino

and relative thread on this link:

https://forum.pjrc.com/threads/70922-AES-Encryption-amp-Decryption-on-Teensy-4-x?p=312337#post312337

The code seems to work and is "approved" by Paul Stoffregen too, except for the topic about the security of the secret key storage. So we started to port the functions on the sketch in a library and we're going to use these waiting for a T4.x compatible version of the CryptoAccel library. We are glad to share the files with whoever wants to try it, with the disclaimer that is not a professional work.

Thank you!
 
Ecodrone, I have also written a very basic and incomplete "library" based on the dcptst.ino code by Manitou. So far it seems to work when I use the same settings as the example function do_aes(); When I tried to explore some other options, like using a different kind of key such as OTP key or Unique key I had no success. Seems like the Teensy 4.1 hardware has more limitations than Teensy 3.5 (which I used before) when using native hardware accelerated functions, such as AES and CRC32 computation.

If you need my help with implementing this library, I can help. I just didn't post my code on GitHub or something because the modifications I made were pretty minor and I didn't have time to document or organize anything, and I don't know about the rights to share that code.
 
How did it go with this? I am also looking to use AES to encrypt on the Teensy 4.1. Would appreciate an update on this thread. Thanks!
 
Back
Top