Recover key from flash encryption

Rafael Albarello
Posts: 4
Joined: Wed Feb 13, 2019 5:22 pm

Recover key from flash encryption

Postby Rafael Albarello » Fri May 24, 2019 8:39 pm

I'm having trouble with re-flashing with flash_encryption enabled, so I've burnt the BLOCK1 keys without read protection, here's the output:

BLK1 Flash encryption key
= 1a b2 bd d9 01 16 bd 91 63 ee 1a 81 3d da c8 29 6f 4f 1d 16 c1 dd b0 f0 eb 56 65 d4 e2 fe e2 87 R/W

I've tried a lot of combinations to get the key to use with "espsecure.py encrypt_flash_data ..", but none of them seems to work, as I get this error on monitor:

Code: Select all

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
flash read err, 1000
ets_main.c 371 
ets Jun  8 2016 00:22:57

rst:0x10 (RTCWDT_RTC_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
flash read err, 1000
ets_main.c 371 
ets Jun  8 2016 00:22:57
What am I missing?

esp_Hemal
Posts: 8
Joined: Thu Feb 14, 2019 9:44 am

Re: Recover key from flash encryption

Postby esp_Hemal » Sat May 25, 2019 5:17 pm

Hi,

When you say you are having trouble with re-flashing with flash encryption enabled does that mean that the first plaintext flashed got encrypted properly after flash encryption was enabled with pre-generated key and ESP32 boots correctly?

Could you provide details of all the steps you followed?

Also, you can provide the efuse dump using 'espefuse summary' command

WiFive
Posts: 2618
Joined: Tue Dec 01, 2015 7:35 am

Re: Recover key from flash encryption

Postby WiFive » Sat May 25, 2019 5:51 pm

Did you set the key tweak?

Rafael Albarello
Posts: 4
Joined: Wed Feb 13, 2019 5:22 pm

Re: Recover key from flash encryption

Postby Rafael Albarello » Mon May 27, 2019 12:53 pm

Yes, first flash encryption with FLASH_CRYPT_CNT on even number with "make flash" works just fine, here's what I did:

I need to burn a key without having the key in plaintext on a file, so I adapted the burn_key function from espefuse.py to consume a variable instead of a file.
After burning, i've saved the key in a separate file (for debugging), but when I use it with "espsecure.py encrypt_flash_data ..", the output of monitor is the one above. I'm sure I used the right addresses in encrypting and flashing. I'm encrypting and flashing the bootloader, partition table, factory and nvs_keys.

The base64 of my key.bin is: h+L+4tRlVuvwsN3BFh1PbynI2j2BGu5jkb0WAdm9sho=

I can't see what i'm missing, if could help me, I'd be thankful.


Output of efuse summary:

Code: Select all

EFUSE_NAME             Description = [Meaningful Value] [Readable/Writeable] (Hex Value)
----------------------------------------------------------------------------------------
Security fuses:
FLASH_CRYPT_CNT        Flash encryption mode counter                     = 7 R/W (0x7)
FLASH_CRYPT_CONFIG     Flash encryption config (key tweak bits)          = 0 R/W (0x0)
CONSOLE_DEBUG_DISABLE  Disable ROM BASIC interpreter fallback            = 1 R/W (0x1)
ABS_DONE_0             secure boot enabled for bootloader                = 0 R/W (0x0)
ABS_DONE_1             secure boot abstract 1 locked                     = 0 R/W (0x0)
JTAG_DISABLE           Disable JTAG                                      = 0 R/W (0x0)
DISABLE_DL_ENCRYPT     Disable flash encryption in UART bootloader       = 0 R/W (0x0)
DISABLE_DL_DECRYPT     Disable flash decryption in UART bootloader       = 0 R/W (0x0)
DISABLE_DL_CACHE       Disable flash cache in UART bootloader            = 0 R/W (0x0)
BLK1                   Flash encryption key                              
  = 1a b2 bd d9 01 16 bd 91 63 ee 1a 81 3d da c8 29 6f 4f 1d 16 c1 dd b0 f0 eb 56 65 d4 e2 fe e2 87 R/W 
BLK2                   Secure boot key                                   
  = 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 R/W 
BLK3                   Variable Block 3                                  
  = 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 R/W 

Efuse fuses:
WR_DIS                 Efuse write disable mask                          = 0 R/W (0x0)
RD_DIS                 Efuse read disablemask                            = 0 R/W (0x0)
CODING_SCHEME          Efuse variable block length scheme                = 0 R/W (0x0)
KEY_STATUS             Usage of efuse block 3 (reserved)                 = 0 R/W (0x0)

Config fuses:
XPD_SDIO_FORCE         Ignore MTDI pin (GPIO12) for VDD_SDIO on reset    = 0 R/W (0x0)
XPD_SDIO_REG           If XPD_SDIO_FORCE, enable VDD_SDIO reg on reset   = 0 R/W (0x0)
XPD_SDIO_TIEH          If XPD_SDIO_FORCE & XPD_SDIO_REG, 1=3.3V 0=1.8V   = 0 R/W (0x0)
SPI_PAD_CONFIG_CLK     Override SD_CLK pad (GPIO6/SPICLK)                = 0 R/W (0x0)
SPI_PAD_CONFIG_Q       Override SD_DATA_0 pad (GPIO7/SPIQ)               = 0 R/W (0x0)
SPI_PAD_CONFIG_D       Override SD_DATA_1 pad (GPIO8/SPID)               = 0 R/W (0x0)
SPI_PAD_CONFIG_HD      Override SD_DATA_2 pad (GPIO9/SPIHD)              = 0 R/W (0x0)
SPI_PAD_CONFIG_CS0     Override SD_CMD pad (GPIO11/SPICS0)               = 0 R/W (0x0)
DISABLE_SDIO_HOST      Disable SDIO host                                 = 0 R/W (0x0)

Identity fuses:
MAC                    Factory MAC Address                               
  = (censored) (CRC bf OK) R/W 
CHIP_VER_REV1          Silicon Revision 1                                = 1 R/W (0x1)
CHIP_VERSION           Reserved for future chip versions                 = 2 R/W (0x2)
CHIP_PACKAGE           Chip package identifier                           = 0 R/W (0x0)

Calibration fuses:
BLK3_PART_RESERVE      BLOCK3 partially served for ADC calibration data  = 0 R/W (0x0)
ADC_VREF               Voltage reference calibration                     = 1107 R/W (0x1)

WiFive
Posts: 2618
Joined: Tue Dec 01, 2015 7:35 am

Re: Recover key from flash encryption

Postby WiFive » Mon May 27, 2019 11:15 pm

FLASH_CRYPT_CONFIG Flash encryption config (key tweak bits) = 0 R/W (0x0)
https://docs.espressif.com/projects/esp ... ypt-config

ESP_Angus
Posts: 1793
Joined: Sun May 08, 2016 4:11 am

Re: Recover key from flash encryption

Postby ESP_Angus » Tue May 28, 2019 12:17 am

To expand on WiFive's answer, you can set this via:

Code: Select all

espefuse.py burn_efuse FLASH_CRYPT_CONF 0xF
Also, there are a number of other efuses which need to be burned to make the device configuration secure. You can burn these manually, but the recommended approach is to have the device do all of this on first boot when it self-encrypts.

Rafael Albarello
Posts: 4
Joined: Wed Feb 13, 2019 5:22 pm

Re: Recover key from flash encryption

Postby Rafael Albarello » Tue May 28, 2019 1:43 pm

Thanks Hemal, WiFive and Angus, that was the problem.

Who is online

Users browsing this forum: No registered users and 23 guests