Flash encryption and secure boot are useful security features for ESP32S3 chip. While not perfect, they definitely make it harder to extract the secrets in the chip.
However, it is tricky to enable both features at the same time. The topic is actually discussed in the official documentation:
Especially, the second one mentioned it is recommended to enable flash encryption before secure boot. But I still find the documentation confusing.
In the end I was able to successfully enable both, here's my findings.
My Understanding
After my adventure, here's what I think could have worked. WARNING, this is untested.
- Follow Security Features Enablement Workflows:
- Burn all the keys, as long as their purpose eFuses and read/write protections
- Burn other security eFuses, but DO NOT burn ENABLE_SECURITY_DOWNLOAD in the middle, which is mentined at the end of the instruction for both flash encryption and secure boot.
- Burn SPI_BOOT_CRYPT_CNT (all 3 bits) and SECURE_BOOT_EN
- Sign bootloader and application
- Encrypt signed bootloader, partition table and signed application
- Flash three encrypted pieces into correct offsets.
My Actual Adventure
I had thought the official bootloader was necessary to enable the features, so I actually downloaded ESP-IDF and flashed the bootloader as the last step of enabling flash encryption. Later I found that ENABLE_SECURITY_DOWNLOAD had been automatically burned by the bootloader, because I used the recommended "secure download mode" option in `idf.py menuconfig`.
This is not the end of the world, it just meant I could no longer use `espefuse.py`. I could actually use the C API to burn the eFuses. The API is actually quite user-friendly, and the high-level functions (e.g. burn key, purpose and read/write protection at the same time) do check common errors. I just had to read the documentation to understand the differences between blocks, bits and registers.
One more thing about the bootloader: A recent version (5.3-ish) of ESP-IDF changed something about application image header, which is no longer compatible with esp-hal before this change, this took me quite a while to figure out.
Comments