Flash Encryption¶
This is a quick start guide to ESP32-C3’s flash encryption feature. Using an application code example, it demonstrates how to test and verify flash encryption operations during development and production.
Introduction¶
Flash encryption is intended for encrypting the contents of the ESP32-C3’s off-chip flash memory. Once this feature is enabled, firmware is flashed as plaintext, and then the data is encrypted in place on the first boot. As a result, physical readout of flash will not be sufficient to recover most flash contents.
With flash encryption enabled, the following types of data are encrypted by default:
Firmware bootloader
Partition Table
All “app” type partitions
Other types of data can be encrypted conditionally:
Any partition marked with the
encrypted
flag in the partition table. For details, see Encrypted Partition Flag.Secure Boot bootloader digest if Secure Boot is enabled (see below).
Important
For production use, flash encryption should be enabled in the “Release” mode only.
Important
Enabling flash encryption limits the options for further updates of ESP32-C3. Before using this feature, read the document and make sure to understand the implications.
Relevant eFuses¶
The flash encryption operation is controlled by various eFuses available on ESP32-C3. The list of eFuses and their descriptions is given in the table below. The names in eFuse column are also used by espefuse.py tool. For usage in the eFuse API, modify the name by adding ESP_EFUSE_
, for example: esp_efuse_read_field_bit(ESP_EFUSE_**DISABLE_DL_ENCRYPT**).
eFuse |
Description |
Bit Depth |
R/W Access Control Available |
Default Value |
|
AES key storage. N is between 0 and 5. |
256 |
Yes |
x |
|
Controls the purpose of eFuse block |
4 |
Yes |
0 |
|
If set, disables flash encryption when in download bootmodes. |
1 |
Yes |
0 |
|
Enables encryption and decryption, when an SPI boot mode is set. Feature is enabled if 1 or 3 bits are set in the eFuse, disabled otherwise. |
3 |
Yes |
0 |
Read and write access to eFuse bits is controlled by appropriate fields in the registers WR_DIS
and RD_DIS
. For more information on ESP32-C3 eFuses, see eFuse manager. To change protection bits of eFuse field using espefuse.py, use these two commands: read_protect_efuse and write_protect_efuse. Example espefuse.py write_protect_efuse DISABLE_DL_ENCRYPT
.
Flash Encryption Process¶
Assuming that the eFuse values are in their default states and the firmware bootloader is compiled to support flash encryption, the flash encryption process executes as shown below:
On the first power-on reset, all data in flash is un-encrypted (plaintext). The ROM bootloader loads the firmware bootloader.
Firmware bootloader reads the
SPI_BOOT_CRYPT_CNT
eFuse value (0b000
). Since the value is0
(even number of bits set), it configures and enables the flash encryption block. For more information on the flash encryption block, see ESP32-C3 Technical Reference Manual.Firmware bootloader uses RNG (random) module to generate an 256 bit key and then writes it into BLOCK_KEYN eFuse. The software also updates the
KEY_PURPOSE_N
for the block where the key were stored. The key cannot be accessed via software as the write and read protection bits for BLOCK_KEYN eFuse are set.KEY_PURPOSE_N
field is write-protected as well. The flash encryption operations happen entirely by hardware, and the key cannot be accessed via software.Flash encryption block encrypts the flash contents - the firmware bootloader, applications and partitions marked as
encrypted
. Encrypting in-place can take time, up to a minute for large partitions.Firmware bootloader sets the first available bit in
SPI_BOOT_CRYPT_CNT
(0b001) to mark the flash contents as encrypted. Odd number of bits is set.For Development Mode, the firmware bootloader allows the UART bootloader to re-flash encrypted binaries. Also, the
SPI_BOOT_CRYPT_CNT
eFuse bits are NOT write-protected. In addition, the firmware bootloader by default sets the eFuse bitsDIS_DOWNLOAD_ICACHE
,DIS_PAD_JTAG
,DIS_USB_JTAG
andDIS_LEGACY_SPI_BOOT
.For Release Mode, the firmware bootloader sets all the eFuse bits set under development mode as well as
DIS_DOWNLOAD_MANUAL_ENCRYPT
. It also write-protects theSPI_BOOT_CRYPT_CNT
eFuse bits. To modify this behavior, see Enabling UART Bootloader Encryption/Decryption.The device is then rebooted to start executing the encrypted image. The firmware bootloader calls the flash decryption block to decrypt the flash contents and then loads the decrypted contents into IRAM.
During the development stage, there is a frequent need to program different plaintext flash images and test the flash encryption process. This requires that Firmware Download mode is able to load new plaintext images as many times as it might be needed. However, during manufacturing or production stages, Firmware Download mode should not be allowed to access flash contents for security reasons.
Hence, two different flash encryption configurations were created: for development and for production. For details on these configurations, see Section Flash Encryption Configuration.
Flash Encryption Configuration¶
The following flash encryption modes are available:
Development Mode - recommended for use ONLY DURING DEVELOPMENT, as it does not prevent modification and possible readout of encrypted flash contents.
Release Mode - recommended for manufacturing and production to prevent physical readout of encrypted flash contents.
This section provides information on the mentioned flash encryption modes and step by step instructions on how to use them.
Development Mode¶
During development, you can encrypt flash using either an ESP32-C3 generated key or external host-generated key.
Using ESP32-C3 Generated Key¶
Development mode allows you to download multiple plaintext images using Firmware Download mode.
To test flash encryption process, take the following steps:
Ensure that you have an ESP32-C3 device with default flash encryption eFuse settings as shown in Relevant eFuses.
See how to check ESP32-C3 Flash Encryption Status.
In Project Configuration Menu, do the following:
Select encryption mode (Development mode by default)
- ref
Select UART ROM download mode <CONFIG_SECURE_UART_ROM_DL_MODE> (enabled by default.)
Save the configuration and exit.
Enabling flash encryption will increase the size of bootloader, which might require updating partition table offset. See Bootloader Size.
Run the command given below to build and flash the complete images.
idf.py flash monitorNote
This command does not include any user files which should be written to the partitions on the flash memory. Please write them manually before running this command otherwise the files should be encrypted separately before writing.
This command will write to flash memory unencrypted images: the firmware bootloader, the partition table and applications. Once the flashing is complete, ESP32-C3 will reset. On the next boot, the firmware bootloader encrypts: the firmware bootloader, application partitions and partitions marked as
encrypted
then resets. Encrypting in-place can take time, up to a minute for large partitions. After that, the application is decrypted at runtime and executed.
A sample output of the first ESP32-C3 boot after enabling flash encryption is given below:
rst:0x1 (POWERON),boot:0xf (SPI_FAST_FLASH_BOOT)
SPIWP:0xee
mode:DIO, clock div:2
load:0x3fcd6260,len:0x1b8
load:0x3fcd6418,len:0x2538
load:0x403ce000,len:0x704
load:0x403d0000,len:0x34f0
entry 0x403ce03e
I (12) boot: ESP-IDF qa-test-v4.3-20201113-766-g870d 2nd stage bootloader
I (13) boot: compile time 12:10:57
I (13) boot: chip revision: 0
I (16) boot.esp32c3: SPI Speed : 40MHz
I (17) boot.esp32c3: SPI Mode : DIO
I (19) boot.esp32c3: SPI Flash Size : 2MB
I (22) boot: Enabling RNG early entropy source...
I (28) boot: Partition Table:
I (30) boot: ## Label Usage Type ST Offset Length
I (33) boot: 0 nvs WiFi data 01 02 0000a000 00006000
I (37) boot: 1 storage Unknown data 01 ff 00010000 00001000
I (41) boot: 2 factory factory app 00 00 00020000 00100000
I (44) boot: End of partition table
I (46) esp_image: segment 0: paddr=0x00020020 vaddr=0x3c020020 size=0x05508 ( 21768) map
I (61) esp_image: segment 1: paddr=0x00025530 vaddr=0x3fc88780 size=0x014cc ( 5324) load
I (64) esp_image: segment 2: paddr=0x00026a04 vaddr=0x40380000 size=0x08780 ( 34688) load
0x40380000: _vector_table at ??:?
I (81) esp_image: segment 3: paddr=0x0002f18c vaddr=0x00000000 size=0x00e8c ( 3724)
I (84) esp_image: segment 4: paddr=0x00030020 vaddr=0x42000020 size=0x171a8 ( 94632) map
0x42000020: esp_ota_get_app_description at /home/marius/clean/esp-idf_2/components/app_update/esp_app_desc.c:63
I (132) boot: Loaded app from partition at offset 0x20000
I (133) boot: Checking flash encryption...
I (137) efuse: Batch mode of writing fields is enabled
I (140) flash_encrypt: Generating new flash encryption key...
I (144) efuse: Writing EFUSE_BLK_KEY0 with purpose 4
W (148) flash_encrypt: Not disabling UART bootloader encryption
I (152) flash_encrypt: Disable UART bootloader cache...
I (155) flash_encrypt: Disable JTAG...
I (161) efuse: Batch mode. Prepared fields are committed
I (162) esp_image: segment 0: paddr=0x00000020 vaddr=0x3fcd6260 size=0x001b8 ( 440)
I (164) esp_image: segment 1: paddr=0x000001e0 vaddr=0x3fcd6418 size=0x02538 ( 9528)
I (173) esp_image: segment 2: paddr=0x00002720 vaddr=0x403ce000 size=0x00704 ( 1796)
I (174) esp_image: segment 3: paddr=0x00002e2c vaddr=0x403d0000 size=0x034f0 ( 13552)
I (571) flash_encrypt: bootloader encrypted successfully
I (627) flash_encrypt: partition table encrypted and loaded successfully
I (628) flash_encrypt: Encrypting partition 1 at offset 0x10000 (length 0x1000)...
I (685) flash_encrypt: Done encrypting
I (686) esp_image: segment 0: paddr=0x00020020 vaddr=0x3c020020 size=0x05508 ( 21768) map
I (696) esp_image: segment 1: paddr=0x00025530 vaddr=0x3fc88780 size=0x014cc ( 5324)
I (699) esp_image: segment 2: paddr=0x00026a04 vaddr=0x40380000 size=0x08780 ( 34688)
0x40380000: _vector_table at ??:?
I (715) esp_image: segment 3: paddr=0x0002f18c vaddr=0x00000000 size=0x00e8c ( 3724)
I (717) esp_image: segment 4: paddr=0x00030020 vaddr=0x42000020 size=0x171a8 ( 94632) map
0x42000020: esp_ota_get_app_description at /home/marius/clean/esp-idf_2/components/app_update/esp_app_desc.c:63
I (760) flash_encrypt: Encrypting partition 2 at offset 0x20000 (length 0x100000)...
I (14797) flash_encrypt: Done encrypting
I (14801) flash_encrypt: Flash encryption completed
I (14802) boot: Resetting with flash encryption enabled...
A sample output of subsequent ESP32-C3 boots just mentions that flash encryption is already enabled:
rst:0x3 (RTC_SW_SYS_RST),boot:0xf (SPI_FAST_FLASH_BOOT)
Saved PC:0x403d0dde
SPIWP:0xee
mode:DIO, clock div:2
load:0x3fcd6260,len:0x1b8
load:0x3fcd6418,len:0x2538
load:0x403ce000,len:0x704
load:0x403d0000,len:0x34f0
entry 0x403ce03e
I (15) boot: ESP-IDF qa-test-v4.3-20201113-766-g870d 2nd stage bootloader
I (15) boot: compile time 12:10:57
I (16) boot: chip revision: 0
I (19) boot.esp32c3: SPI Speed : 40MHz
I (19) boot.esp32c3: SPI Mode : DIO
I (22) boot.esp32c3: SPI Flash Size : 2MB
I (24) boot: Enabling RNG early entropy source...
I (30) boot: Partition Table:
I (32) boot: ## Label Usage Type ST Offset Length
I (36) boot: 0 nvs WiFi data 01 02 0000a000 00006000
I (39) boot: 1 storage Unknown data 01 ff 00010000 00001000
I (43) boot: 2 factory factory app 00 00 00020000 00100000
I (47) boot: End of partition table
I (49) esp_image: segment 0: paddr=0x00020020 vaddr=0x3c020020 size=0x05508 ( 21768) map
I (64) esp_image: segment 1: paddr=0x00025530 vaddr=0x3fc88780 size=0x014cc ( 5324) load
I (67) esp_image: segment 2: paddr=0x00026a04 vaddr=0x40380000 size=0x08780 ( 34688) load
0x40380000: _vector_table at ??:?
I (86) esp_image: segment 3: paddr=0x0002f18c vaddr=0x00000000 size=0x00e8c ( 3724)
I (88) esp_image: segment 4: paddr=0x00030020 vaddr=0x42000020 size=0x171a8 ( 94632) map
0x42000020: esp_ota_get_app_description at /home/marius/clean/esp-idf_2/components/app_update/esp_app_desc.c:63
I (139) boot: Loaded app from partition at offset 0x20000
I (139) boot: Checking flash encryption...
I (144) flash_encrypt: flash encryption is enabled (1 plaintext flashes left)
I (148) boot: Disabling RNG early entropy source...
I (160) cpu_start: Pro cpu start user code
I (160) cpu_start: cpu freq: 40000000
I (161) cpu_start: Application information:
I (161) cpu_start: Project name: flash_encryption
I (164) cpu_start: App version: qa-test-v4.3-20201113-766-g870d
I (168) cpu_start: Compile time: Dec 21 2020 12:10:55
I (171) cpu_start: ELF file SHA256: 209e8947c2e6a6a6...
I (174) cpu_start: ESP-IDF: qa-test-v4.3-20201113-766-g870d
I (178) heap_init: Initializing. RAM available for dynamic allocation:
I (181) heap_init: At 3FC8A9F0 len 00035610 (213 KiB): D/IRAM
I (184) heap_init: At 3FCC0000 len 0001F260 (124 KiB): STACK/DRAM
I (188) heap_init: At 50000000 len 00002000 (8 KiB): FAKEDRAM
W (192) flash_encrypt: Flash encryption mode is DEVELOPMENT (not secure)
I (195) spi_flash: detected chip: gd
I (197) spi_flash: flash io: dio
W (199) spi_flash: Detected size(4096k) larger than the size in the binary image header(2048k). Using the size in the binary image header.
I (207) cpu_start: Starting scheduler.
Example to check Flash Encryption status
This is esp32c3 chip with 1 CPU core(s), WiFi/BLE, silicon revision 0, 2MB external flash
FLASH_CRYPT_CNT eFuse value is 1
Flash encryption feature is enabled in DEVELOPMENT mode
At this stage, if you need to update and re-flash binaries, see Re-flashing Updated Partitions.
Using Host Generated Key¶
It is possible to pre-generate a flash encryption key on the host computer and burn it into the eFuse. This allows you to pre-encrypt data on the host and flash already encrypted data without needing a plaintext flash update. This feature can be used in both Development Mode and Release Mode. Without a pre-generated key, data is flashed in plaintext and then ESP32-C3 encrypts the data in-place.
Note
This option is not recommended for production, unless a separate key is generated for each individual device.
To use a host generated key, take the following steps:
Ensure that you have an ESP32-C3 device with default flash encryption eFuse settings as shown in Relevant eFuses.
See how to check ESP32-C3 Flash Encryption Status.
Generate a random key by running:
espsecure.py generate_flash_encryption_key my_flash_encryption_key.bin
Before the first encrypted boot, burn the key into your device’s eFuse using the command below. This action can be done only once.
espefuse.py --port PORT burn_key BLOCK my_flash_encryption_key.bin XTS_AES_128_KEY
where BLOCK is a free keyblock between BLOCK_KEY0 and BLOCK_KEY5.
If the key is not burned and the device is started after enabling flash encryption, the ESP32-C3 will generate a random key that software cannot access or modify.
In Project Configuration Menu, do the following:
Select encryption mode (Development mode by default)
Save the configuration and exit.
Enabling flash encryption will increase the size of bootloader, which might require updating partition table offset. See Bootloader Size.
Run the command given below to build and flash the complete images.
idf.py flash monitorNote
This command does not include any user files which should be written to the partitions on the flash memory. Please write them manually before running this command otherwise the files should be encrypted separately before writing.
This command will write to flash memory unencrypted images: the firmware bootloader, the partition table and applications. Once the flashing is complete, ESP32-C3 will reset. On the next boot, the firmware bootloader encrypts: the firmware bootloader, application partitions and partitions marked as
encrypted
then resets. Encrypting in-place can take time, up to a minute for large partitions. After that, the application is decrypted at runtime and executed.
At this stage, if you need to update and re-flash binaries, see Re-flashing Updated Partitions.
Re-flashing Updated Partitions¶
If you update your application code (done in plaintext) and want to re-flash it, you will need to encrypt it before flashing. To encrypt the application and flash it in one step, run:
idf.py encrypted-app-flash monitor
If all partitions needs to be updated in encrypted format, run:
idf.py encrypted-flash monitor
Release Mode¶
In Release mode, UART bootloader cannot perform flash encryption operations. New plaintext images can ONLY be downloaded using the over-the-air (OTA) scheme which will encrypt the plaintext image before writing to flash.
To use this mode, take the following steps:
Ensure that you have an ESP32-C3 device with default flash encryption eFuse settings as shown in Relevant eFuses.
See how to check ESP32-C3 Flash Encryption Status.
In Project Configuration Menu, do the following:
Select Release mode (Note that once Release mode is selected, the
EFUSE_DIS_DOWNLOAD_MANUAL_ENCRYPT
eFuse bit will be burned to disable UART bootloader access to flash contents)Select UART ROM download mode (By default the option to permanently switch to UART ROM Secure download mode is selected)
Save the configuration and exit.
Enabling flash encryption will increase the size of bootloader, which might require updating partition table offset. See Bootloader Size.
Run the command given below to build and flash the complete images.
idf.py flash monitorNote
This command does not include any user files which should be written to the partitions on the flash memory. Please write them manually before running this command otherwise the files should be encrypted separately before writing.
This command will write to flash memory unencrypted images: the firmware bootloader, the partition table and applications. Once the flashing is complete, ESP32-C3 will reset. On the next boot, the firmware bootloader encrypts: the firmware bootloader, application partitions and partitions marked as
encrypted
then resets. Encrypting in-place can take time, up to a minute for large partitions. After that, the application is decrypted at runtime and executed.
Once the flash encryption is enabled in Release mode, the bootloader will write-protect the SPI_BOOT_CRYPT_CNT
eFuse.
For subsequent plaintext field updates, use OTA scheme.
. _flash-encrypt-best-practices:
Best Practices¶
When using Flash Encryption in production:
Do not reuse the same flash encryption key between multiple devices. This means that an attacker who copies encrypted data from one device cannot transfer it to a second device.
The UART ROM Download Mode should be disabled entirely if it is not needed, or permanently set to “Secure Download Mode” otherwise. Secure Download Mode permanently limits the available commands to basic flash read and write only. The default behaviour is to set Secure Download Mode on first boot in Release mode. To disable Download Mode entirely select select the CONFIG_SECURE_UART_ROM_DL_MODE to “Permanently disable ROM Download Mode (recommended)” or call
esp_efuse_disable_rom_download_mode()
at runtime.Enable Secure Boot as an extra layer of protection, and to prevent an attacker from selectively corrupting any part of the flash before boot.
Possible Failures¶
Once flash encryption is enabled, the SPI_BOOT_CRYPT_CNT
eFuse value will have an odd number of bits set. It means that all the partitions marked with the encryption flag are expected to contain encrypted ciphertext. Below are the three typical failure cases if the ESP32-C3 is erroneously loaded with plaintext data:
If the bootloader partition is re-flashed with a plaintext firmware bootloader image, the ROM bootloader will fail to load the firmware bootloader resulting in the following failure:
rst:0x3 (SW_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
invalid header: 0xb414f76b
invalid header: 0xb414f76b
invalid header: 0xb414f76b
invalid header: 0xb414f76b
invalid header: 0xb414f76b
invalid header: 0xb414f76b
invalid header: 0xb414f76b
Note
The value of invalid header will be different for every application.
Note
This error also appears if the flash contents are erased or corrupted.
If the firmware bootloader is encrypted, but the partition table is re-flashed with a plaintext partition table image, the bootloader will fail to read the partition table resulting in the following failure:
rst:0x3 (SW_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT) configsip: 0, SPIWP:0xee clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00 mode:DIO, clock div:2 load:0x3fff0018,len:4 load:0x3fff001c,len:10464 ho 0 tail 12 room 4 load:0x40078000,len:19168 load:0x40080400,len:6664 entry 0x40080764 I (60) boot: ESP-IDF v4.0-dev-763-g2c55fae6c-dirty 2nd stage bootloader I (60) boot: compile time 19:15:54 I (62) boot: Enabling RNG early entropy source... I (67) boot: SPI Speed : 40MHz I (72) boot: SPI Mode : DIO I (76) boot: SPI Flash Size : 4MB E (80) flash_parts: partition 0 invalid magic number 0x94f6 E (86) boot: Failed to verify partition table E (91) boot: load partition table error!
If the bootloader and partition table are encrypted, but the application is re-flashed with a plaintext application image, the bootloader will fail to load the application resulting in the following failure:
rst:0x3 (SW_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT) configsip: 0, SPIWP:0xee clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00 mode:DIO, clock div:2 load:0x3fff0018,len:4 load:0x3fff001c,len:8452 load:0x40078000,len:13616 load:0x40080400,len:6664 entry 0x40080764 I (56) boot: ESP-IDF v4.0-dev-850-gc4447462d-dirty 2nd stage bootloader I (56) boot: compile time 15:37:14 I (58) boot: Enabling RNG early entropy source... I (64) boot: SPI Speed : 40MHz I (68) boot: SPI Mode : DIO I (72) boot: SPI Flash Size : 4MB I (76) boot: Partition Table: I (79) boot: ## Label Usage Type ST Offset Length I (87) boot: 0 nvs WiFi data 01 02 0000a000 00006000 I (94) boot: 1 phy_init RF data 01 01 00010000 00001000 I (102) boot: 2 factory factory app 00 00 00020000 00100000 I (109) boot: End of partition table E (113) esp_image: image at 0x20000 has invalid magic byte W (120) esp_image: image at 0x20000 has invalid SPI mode 108 W (126) esp_image: image at 0x20000 has invalid SPI size 11 E (132) boot: Factory app partition is not bootable E (138) boot: No bootable app partitions in the partition table
ESP32-C3 Flash Encryption Status¶
Ensure that you have an ESP32-C3 device with default flash encryption eFuse settings as shown in Relevant eFuses.
To check if flash encryption on your ESP32-C3 device is enabled, do one of the following:
flash the application example security/flash_encryption onto your device. This application prints the
SPI_BOOT_CRYPT_CNT
eFuse value and if flash encryption is enabled or disabled.Find the serial port name under which your ESP32-C3 device is connected, replace
PORT
with your port name in the following command, and run it:espefuse.py -p PORT summary
Reading and Writing Data in Encrypted Flash¶
ESP32-C3 application code can check if flash encryption is currently enabled by calling esp_flash_encryption_enabled()
. Also, a device can identify the flash encryption mode by calling esp_get_flash_encryption_mode()
.
Once flash encryption is enabled, be more careful with accessing flash contents from code.
Scope of Flash Encryption¶
Whenever the SPI_BOOT_CRYPT_CNT
eFuse is set to a value with an odd number of bits, all flash content accessed via the MMU’s flash cache is transparently decrypted. It includes:
Executable application code in flash (IROM).
All read-only data stored in flash (DROM).
Any data accessed via
spi_flash_mmap()
.The firmware bootloader image when it is read by the ROM bootloader.
Important
The MMU flash cache unconditionally decrypts all existing data. Data which is stored unencrypted in flash memory will also be “transparently decrypted” via the flash cache and will appear to software as random garbage.
Reading from Encrypted Flash¶
To read data without using a flash cache MMU mapping, you can use the partition read function esp_partition_read()
. This function will only decrypt data when it is read from an encrypted partition. Data read from unencrypted partitions will not be decrypted. In this way, software can access encrypted and non-encrypted flash in the same way.
You can also use the following SPI flash API functions:
esp_flash_read()
to read raw (encrypted) data which will not be decryptedesp_flash_read_encrypted()
to read and decrypt data
The ROM function SPIRead()
can read data without decryption, however, this function is not supported in esp-idf applications.
Data stored using the Non-Volatile Storage (NVS) API is always stored and read decrypted from the perspective of flash encryption. It is up to the library to provide encryption feature if required. Refer to NVS Encryption for more details.
Writing to Encrypted Flash¶
It is recommended to use the partition write function esp_partition_write()
. This function will only encrypt data when it is written to an encrypted partition. Data written to unencrypted partitions will not be encrypted. In this way, software can access encrypted and non-encrypted flash in the same way.
You can also pre-encrypt and write data using the function esp_flash_write_encrypted()
Also, the following ROM function exist but not supported in esp-idf applications:
esp_rom_spiflash_write_encrypted
pre-encrypts and writes data to flashSPIWrite
writes unencrypted data to flash
Since data is encrypted in blocks, the minimum write size for encrypted data is 16 bytes and the alignment is also 16 bytes.
Updating Encrypted Flash¶
OTA Updates¶
OTA updates to encrypted partitions will automatically write encrypted data if the function esp_partition_write()
is used.
Before building the application image for OTA updating of an already encrypted device, enable the option Enable flash encryption on boot in project configuration menu.
For general information about ESP-IDF OTA updates, please refer to OTA
Disabling Flash Encryption¶
If flash encryption was enabled accidentally, flashing of plaintext data will soft-brick the ESP32-C3. The device will reboot continuously, printing the error flash read err, 1000
or invalid header: 0xXXXXXX
.
For flash encryption in Development mode, encryption can be disabled by burning the SPI_BOOT_CRYPT_CNT
eFuse. It can only be done one time per chip by taking the following steps:
In Project Configuration Menu, disable Enable flash encryption on boot, then save and exit.
Open project configuration menu again and double-check that you have disabled this option! If this option is left enabled, the bootloader will immediately re-enable encryption when it boots.
With flash encryption disabled, build and flash the new bootloader and application by running
idf.py flash
.Use
espefuse.py
(incomponents/esptool_py/esptool
) to disable theSPI_BOOT_CRYPT_CNT
by running:
espefuse.py burn_efuse SPI_BOOT_CRYPT_CNT
Reset the ESP32-C3. Flash encryption will be disabled, and the bootloader will boot as usual.
Key Points About Flash Encryption¶
Flash memory contents is encrypted using XTS-AES-128. The flash encryption key is 256 bits and stored one``BLOCK_KEYN`` eFuse internal to the chip and, by default, is protected from software access.
Flash access is transparent via the flash cache mapping feature of ESP32-C3 - any flash regions which are mapped to the address space will be transparently decrypted when read.
Some data partitions might need to remain unencrypted for ease of access or might require the use of flash-friendly update algorithms which are ineffective if the data is encrypted. NVS partitions for non-volatile storage cannot be encrypted since the NVS library is not directly compatible with flash encryption. For details, refer to NVS Encryption.
If flash encryption might be used in future, the programmer must keep it in mind and take certain precautions when writing code that uses encrypted flash.
If secure boot is enabled, re-flashing the bootloader of an encrypted device requires a “Re-flashable” secure boot digest (see Flash Encryption and Secure Boot).
Enabling flash encryption will increase the size of bootloader, which might require updating partition table offset. See Bootloader Size.
Important
Do not interrupt power to the ESP32-C3 while the first boot encryption pass is running. If power is interrupted, the flash contents will be corrupted and will require flashing with unencrypted data again. In this case, re-flashing will not count towards the flashing limit.
Limitations of Flash Encryption¶
Flash encryption protects firmware against unauthorised readout and modification. It is important to understand the limitations of the flash encryption feature:
Flash encryption is only as strong as the key. For this reason, we recommend keys are generated on the device during first boot (default behaviour). If generating keys off-device, ensure proper procedure is followed and don’t share the same key between all production devices.
Not all data is stored encrypted. If storing data on flash, check if the method you are using (library, API, etc.) supports flash encryption.
Flash encryption does not prevent an attacker from understanding the high-level layout of the flash. This is because the same AES key is used for every pair of adjacent 16 byte AES blocks. When these adjacent 16 byte blocks contain identical content (such as empty or padding areas), these blocks will encrypt to produce matching pairs of encrypted blocks. This may allow an attacker to make high-level comparisons between encrypted devices (i.e. to tell if two devices are probably running the same firmware version).
Flash encryption alone may not prevent an attacker from modifying the firmware of the device. To prevent unauthorised firmware from running on the device, use flash encryption in combination with Secure Boot.
Flash Encryption and Secure Boot¶
It is recommended to use flash encryption in combination with Secure Boot. However, if Secure Boot is enabled, additional restrictions apply to device re-flashing:
OTA Updates are not restricted, provided that the new app is signed correctly with the Secure Boot signing key.
Advanced Features¶
The following section covers advanced features of flash encryption.
Encrypted Partition Flag¶
Some partitions are encrypted by default. Other partitions can be marked in the partition table description as requiring encryption by adding the flag encrypted
to the partitions’ flag field. As a result, data in these marked partitions will be treated as encrypted in the same manner as an app partition.
# Name, Type, SubType, Offset, Size, Flags
nvs, data, nvs, 0x9000, 0x6000
phy_init, data, phy, 0xf000, 0x1000
factory, app, factory, 0x10000, 1M
secret_data, 0x40, 0x01, 0x20000, 256K, encrypted
For details on partition table description, see partition table.
Further information about encryption of partitions:
Default partition tables do not include any encrypted data partitions.
With flash encryption enabled, the
app
partition is always treated as encrypted and does not require marking.If flash encryption is not enabled, the flag “encrypted” has no effect.
You can also consider protecting
phy_init
data from physical access, readout, or modification, by marking the optionalphy
partition with the flagencrypted
.The
nvs
partition cannot be encrypted, because the NVS library is not directly compatible with flash encryption.
Enabling UART Bootloader Encryption/Decryption¶
On the first boot, the flash encryption process burns by default the following eFuses:
DIS_DOWNLOAD_MANUAL_ENCRYPT
which disables flash encryption operation when running in UART bootloader boot mode.DIS_DOWNLOAD_ICACHE
which disables the entire MMU flash cache when running in UART bootloader mode.DIS_PAD_JTAG
andDIS_USB_JTAG
which disables JTAG.DIS_LEGACY_SPI_BOOT
which disables Legacy SPI boot mode
However, before the first boot you can choose to keep any of these features enabled by burning only selected eFuses and write-protect the rest of eFuses with unset value 0. For example:
espefuse.py --port PORT burn_efuse DIS_DOWNLOAD_MANUAL_ENCRYPT
espefuse.py --port PORT write_protect_efuse DIS_DOWNLOAD_MANUAL_ENCRYPT
Note
Set all appropriate bits before write-protecting!
Write protection of all the three eFuses is controlled by one bit. It means that write-protecting one eFuse bit will inevitably write-protect all unset eFuse bits.
Write protecting these eFuses to keep them unset is not currently very useful, as esptool.py
does not support reading encrypted flash.
JTAG Debugging¶
By default, when Flash Encryption is enabled (in either Development or Release mode) then JTAG debugging is disabled via eFuse. The bootloader does this on first boot, at the same time it enables flash encryption.
See JTAG with Flash Encryption or Secure Boot for more information about using JTAG Debugging with Flash Encryption.
Technical Details¶
The following sections provide some reference information about the operation of flash encryption.
Flash Encryption Algorithm¶
ESP32-C3 use the XTS-AES block chiper mode with 256 bit size for flash encryption.
XTS-AES is a block chiper mode specifically designed for disc encryption and addresses the weaknesses other potential modes (e.g. AES-CTR) have for this use case. A detailed description of the XTS-AES algorithm can be found in IEEE Std 1619-2007.
The flash encryption key is stored in one
BLOCK_KEYN
eFuse and, by default, is protected from further writes or software readout.To see the full flash encryption algorithm implemented in Python, refer to the _flash_encryption_operation() function in the
espsecure.py
source code.