-
Notifications
You must be signed in to change notification settings - Fork 7.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ESP32-Arduino Sketch and Encryption with ESP-idf #5645
Comments
What bootloader do you flash with encryption enabled? |
bootloader from ESP-idf after enabling encryption |
Did you try this scenario:
As far as i remember esp32 should encrypt binary on first run. This is only theory i remember from other issues and topics on forum. |
Arduino itself is not geared towards encryption. You need to use it as IDF component if you want such features. |
I have Done so but have same error: |
I have found the problem: |
Hi, can you explain in brief your steps to make encryption work for projects which use Arduino? |
1- Using a simple project from ESP-IDF (for example Hello-World) 2- adding Arduino Components as following: https://docs.espressif.com/projects/arduino-esp32/en/latest/esp-idf_component.html 3- adding suitable sdkconfig from Arduino folder to the project folder (from ..\components\arduino\tools\sdk\esp32 for ESP32) 4- adding customized partition.csv as follow to the project: Name, Type, SubType, Offset, Size, Flagsnvs, data, nvs, 0x9000, 0x5000, 5- enabling the encryption and customized partition table by idf.py menuconfig 6- building the projekt by: idf.py build 7- copying the built partition-table.bin (from ..\build\partition_table) , the built bootloader.bin (from ..\build\bootloader) and ota_data_initial.bin (from ..\build) to the esptool folder (..\esp-idf\components\esptool_py\esptool) 8- convert the Arduino sketch to binary by: Arduino IDE-> Sketch-> Export compiled binary (rename it as main.bin) 9- copying the main.bin to the esptool folder 10- creating own key espsecure.py generate_flash_encryption_key my_flash_encryption_key.bin 11 - write the key in the module espefuse.py --port PORT burn_key flash_encryption my_flash_encryption_key.bin 12- write all the files to the module: esptool.py -p PORT -b 921600 --before default_reset --after no_reset --chip esp32 write_flash --flash_mode dio --flash_size detect --flash_freq 40m 0x1000 bootloader.bin 0x8000 partition-table.bin 0xe000 ota_data_initial.bin 0x10000 main.bin 0x290000 spiffs.bin 13- it will encrypt the flash and restart to rewrite the module for development, use the development mode in step 5 and encrypt the main.bin file: espsecure.py encrypt_flash_data --keyfile my_flash_encryption_key.bin --address 0x10000 --output main_en.bin main.bin and write this part only: esptool.py -p PORT -b 921600 write_flash --flash_mode dio 0x10000 main_en.bin for me it works perfectly |
Very nice exmplanation @Mos135 , you really are making thing easy. Could you kindly exapand a bit this two very generic points, which versions did you use? At the moment seems to me that everything I try to use arduino as IDF component produce errors.
thank you |
After many efforts I've succesfully built the Arduino as ESP32 component (see guide here) I had also to install python2 (see guide) to generate the key.bin file to avoid an error ocurring using python3 (see Git thread)
Up to here everything was fine. I did everything as sketchily exmpained by @Mos135 and I managed to connect to ESP32 and write the key and the bins but analyzing the serial monitor on the COM I got the following error at the very end
Unfortunately in FINAL SOLUTION
I attach to this post a bootloader set fot 16MB of flash and Flash Frequency of 40MHz. |
Hello @Mos135, can we close this as solved? |
Hello, as there was no answer in more than 14 days, I'm closing the issue as expired to keep our backlog manageable. If it's still needed, please reopen the issue. Thanks for understanding. |
Hi.. I need i bootloader for my specs.. HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash Name, Type, SubType, Offset, Size, Flagsnvs, data, nvs, 0x9000, 0x5000, is it the procedure complicated? Can you guide me ? Thanks a lot for your help |
Basically with esp Idf, I suggest you to use ESP-IDF 5.0 CMD console you just need to create an encrypted project and copy the bootloader programmed to suit your needs If you do not much about esp |
Hi @pedros89 .. Thanks a lot |
Sorry, from my experience only esp app partition will be encrypted with the randomly generated key by the bootloader. I am not sure about NVS partition, but I think it will not. I'm still using the old esp Virtual EEPROM library and the 4KB allocated are not encrypted in my case. Also SPIFFs partition is not encrypted with this method. I don't know how to encrypt those. If you want you can save sensible information (text or numbers) as encrypted with RC4 or similar methods and use an unique key you have in flash to decrypt when you retrieve the info, in this way is like having it encrypted. |
Hi @pedros89 .. thanks for your suggestion.. when i create the custom bootloader with the menuconfig do I hvae also to change the partion table to be used ? I need a special one which is set in this file .. Name, Type, SubType, Offset, Size, Flagsnvs, data, nvs, 0x9000, 0x5000, |
yes of course if you have a custom partition you have to modify the .csv |
Hi again @pedros89 .. Thanks a lot for your support |
Hi all! I'm trying to repeat the encryption procedure for the Lolin S2 mini board.
Before that, I uploaded the flash files that the Arduino uses to a my directory and wrote a cmd file for the firmware (use parameters from arduino log window), checked that the board was being flashed and the program was working. esptool.exe --chip esp32s2 --port COM%PORT% --baud 921600 --before default_reset --after hard_reset write_flash -z --flash_mode dio --flash_freq 80m --flash_size 4MB 0x1000 bootloader.bin 0x8000 partitions.bin 0xe000 boot_app0.bin 0x10000 myfile.bin I replaced the bootloader with the one generated above in idf tools. It is flashed, I perform a reset (automatically impossible for this board) and wait, up to 10 minutes. Nothing changes, rebooting the board does not launch the program. What could I have missed? Arduino settings: menuconfig |
If you find 128 with many question marks as key, as in the picture it means the chip is encrypted
By the way it is weird that commas at the end of each line were removed in the partition .csv as default partition schemes have it. |
Thanks for the answer! I don’t think it’s a directory issue, I copied the same files that would interfere with the Arduino theme and used the command line from their Arduino log when flashing the firmware. The process is completely identical only The sketch does not compile, it was exported earlier. Everything works with the Arduino bootloader. I can't view the bootloader messages because The port is USB (not uart/usbbrigde) and disappears after flashing and resetting. No signs of work. The firmware lights up the LED at startup. This doesn't happen. I can flash the original bootloader since I did not exclude this possibility when setting up a new bootloader and chose development. I'll try to replace the bootloader in the Arduino, ok. But I don't think this will help. The busy ones had to be deleted because esptools showed a csv partitions file error. |
My problem is solved in this checkbox. We need to take it off. Addition: |
why write not sign files? it dont need! where set fuse for encrypt mechanism? it not full description! and with mistakes! |
Good that you got it sorted. I did not remember about that checkbox, infact, I did not touch anything, I left everything on default in I confirm that if you have the bootloader with the encryption in developer mode, before uploading a new firmware via USB you need to burn an efuse with the following command On the other hand if you are working in release mode only OTA updates are possible to update the firmware, no more USB At the end of the encryption process you can also try to read the flash and see if it is encrypted with this command Read Falsh |
i think, 'SPI_BOOT_CRYPT_CNT' need for esp32s2? |
I set SPI_BOOT_CRYPT_CNT (0b001 -> 0b011) and it worked! As I understand it, I have one attempt left, but I can already try to work via OTA (I have provided for it in the firmware). I just would like to return this particular board to Arduino, if possible) I also compared the downloaded firmware and my own, and they are clearly different, that is, the downloaded one is encrypted. In mine, when I open the words from the F() macros with notepad, the downloaded file is complete garbage :) UPD OTA work perfectly ;) Thanks! Im add two bootloaders for ESP32 S2 (lolin s2 mini) for development with usb (tested) and release without usb/uart (not tested!) to this message |
where do you find the sdkconfig in Arduino for the project you want to build? @pedros89 maybe do you know? |
Are you looking for ? |
Hi
I have written a complicated project with Arduino-IDE for ESP32, GPS and Display modules and it was working perfect.
After that I have decided to enable encryption and protection, to protect my program. but I find out that it is not possible to enable it through Arduino-IDE. I read many forums and have tried to use ESP-idf
but the problem was, I got many errors from other module's libraries, while I didn't have the same problem with Arduino-IDE.
then I have tried to compile my code with Arduino-IDE and only write the encrypted flash in the app partition and I got the error that it can not initialize EEPROM and SPIFF.
then I have tried to find out what the problem is. I write a simple code to use SPIFF and EEPROM. when I compile it in ESP-idf and then encrypt it and write it to the flash, it works perfectly and I get this message:
I (0) cpu_start: App cpu up.
I (145) cpu_start: Pro cpu start user code
I (145) cpu_start: cpu freq: 160000000
I (145) cpu_start: Application information:
I (150) cpu_start: Project name: main
I (154) cpu_start: App version: 1
I (159) cpu_start: Compile time: Aug 27 2021 14:32:23
I (165) cpu_start: ELF file SHA256: c81c9b73a2a94ecd...
I (171) cpu_start: ESP-IDF: v4.3-dirty
I (176) heap_init: Initializing. RAM available for dynamic allocation:
I (183) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (189) heap_init: At 3FFB3840 len 0002C7C0 (177 KiB): DRAM
I (196) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (202) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (208) heap_init: At 4008BB88 len 00014478 (81 KiB): IRAM
I (216) spi_flash: detected chip: generic
I (219) spi_flash: flash io: dio
W (224) flash_encrypt: Flash encryption mode is DEVELOPMENT (not secure)
I (233) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
I (261) nvs: NVS partition "nvs" is encrypted.
�EEPROM is Empty
EEPROM was initialised
SPIFFS mounted
Flash encryption enabled
but when I compile it with Arduino-IDE and then encrypt it and write it to the app partition, I got this error.
entry 0x400805e8
�failed to initialise EEPROM
EEPROM is Empty
failed to initialise EEPROM
EEPROM is Empty
E (4004) SPIFFS: spiffs partition could not be found
An Error has occurred while mounting SPIFFS
Flash encryption enabled
can anyone help about this issue?
please note that when I disable encryption, in both situation code works perfectly. also for my original project.
The text was updated successfully, but these errors were encountered: