mcci-catena / arduino-lmic

LoraWAN-MAC-in-C library, adapted to run under the Arduino environment
https://forum.mcci.io/c/device-software/arduino-lmic/
MIT License
643 stars 210 forks source link

[Feature Request] option to use ESP32 AES hardware encryption #202

Open cyberman54 opened 5 years ago

cyberman54 commented 5 years ago

ESP32 cpu provides AES hardware encryption. The ESP32 SDK has two libraries: aes.o and sha.o providing several functions. Using hwcrypto may speed up the LMIC library and saves storage (flash + ram) on ESP32 systems.

Perhaps could be done by wrapping void lmic_aes_encrypt(unsigned char *Data, unsigned char *Key)

manuelbl commented 5 years ago

In my library for the ESP-IDF framework, the hardware encryption is used. I assume (I never measured it) that it reduces the code sizes. I also believe that it is slower than a good software implementation – the reason being that I've gone for the simplest approach by just implementing lmic_aes_encrypt. So the key needs to be set for each AES block – not super efficient. But then the amount of data that's being encrypted is very small.

If you want to use it as well, you'll need the file mbedtls_aes.c and you will need to modify config.h so it supports the preprocessor define USE_MBEDTLS_AES.

cyberman54 commented 5 years ago

Thanks for your note! So, is it correct that means i need two defines in lmic_config.h ?

#define USE_ORIGINAL_AES
#define USE_MBEDTLS_AES
manuelbl commented 5 years ago

USE_MBEDTLS_AES must be defined, but not USE_ORIGINAL_AES. Furthermore config.h must be modified such that it does not define USE_IDEETRON_AES.

cyberman54 commented 5 years ago

Hmm, i don't want to touch the original library. As far as i see this is not possible with config.h :-(

terrillmoore commented 5 years ago

@cyberman54, unfortunately, there are no good ways to do this with config.h, because it requires some configuration variable to be set. It would be sensible to allow this to be overridden by something external, as in "USE_EXTERNAL_AES" -- that would turn off both of the stock defines, and allow you to do your own thing. Then you could put this in your boards.txt file for the Arduino environment.

cyberman54 commented 5 years ago

@terrillmoore Since Espressif announced the new ESP32-S2 with crypto feature, would be nive to use this feature with it.

https://www.espressif.com/en/news/espressif-announces-%E2%80%A8esp32-s2-secure-wi-fi-mcu

DylanGWork commented 3 years ago

Hi, I'm having trouble getting this to work, getting the error: ../components/src/aes/other.c: In function 'os_aes_cmac': ../components/src/aes/other.c:56:9: error: implicit declaration of function 'lmic_aes_encrypt' [-Werror=implicit-function-declaration] lmic_aes_encrypt(AESaux, AESkey); ^~~~ cc1.exe: some warnings being treated as errors

I see this line but I'm not sure where it should be defined?

// This should be defined elsewhere void lmic_aes_encrypt(u1_t data, u1_t key);

DylanGWork commented 3 years ago

I ended up using the way it was done in this library and have it working: https://github.com/TobleMiner/lmic-esp-idf/tree/master/src/aes

cyberman54 commented 3 years ago

@DylanGWork So, that means you changed code inside of the MCCI LMIC library? Can you make a PR?

DylanGWork commented 3 years ago

Sure did, I've made a few alterations so I'll see if I can backtrack to a working version just using the edit for this AES stuff