Secure boot problem

vlad2891
Posts: 8
Joined: Wed Mar 08, 2017 2:27 pm

Secure boot problem

Postby vlad2891 » Wed Apr 05, 2017 8:38 am

Hello guys,

Lately I'we been having trouble getting secure boot working on the ESP32 device ( been using adafruit devboards). So what I do basically is :
1. have a private key /secure_boot_signing_key.pem
2. extract public key by doing

Code: Select all

python espsecure.py extract_public_key --keyfile <project_path>/secure_boot_signing_key.pem <project_path>/signature_verification_key.bin
and I get signature_verification_key.bin
3.

Code: Select all

 make bootloader
then gives me

Code: Select all

python esptool.py --chip esp32 --port COM5 --baud 115200 --before default_reset --after no_reset write_flash -z --flash_mode dio --flash_freq 40m --flash_size detect 0x1000 <project_path>/build/bootloader/bootloader.bin
.
4. After I run the previous command I do a

Code: Select all

make flash
5. after rebooting I get the following

Code: Select all

rst:0x10 (RTCWDT_RTC_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0x00
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0008,len:8
load:0x3fff0010,len:3844
load:0x40078000,len:12912
ho 0 tail 12 room 4
load:0x40080000,len:252
entry 0x40080034
E (47) secure_boot: bootloader image appears invalid! error 258
E (47) boot: Bootloader digest generation failed (258). SECURE BOOT IS NOT ENABLED.
I (1204) cpu_start: Pro cpu up.
I (1205) cpu_start: Single core mode
I (1206) heap_alloc_caps: Initializing. RAM available for dynamic allocation:
I (1219) heap_alloc_caps: At 3FFAE2A0 len 00001D60 (7 KiB): DRAM
I (1239) heap_alloc_caps: At 3FFBA600 len 00025A00 (150 KiB): DRAM
I (1260) heap_alloc_caps: At 3FFE0440 len 00003BC0 (14 KiB): D/IRAM
I (1282) heap_alloc_caps: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (1303) heap_alloc_caps: At 4009B6C0 len 00004940 (18 KiB): IRAM
I (1324) cpu_start: Pro cpu start user code
I (1385) cpu_start: Starting scheduler on PRO CPU.
I (1390) [MAIN]: Initialization started
I (1390) [MAIN]: Software version: 0.1
I (1393) [MAIN]: IDF version: v2.0-rc1-302-g57486a1
The problem being here

Code: Select all

E (47) secure_boot: bootloader image appears invalid! error 258
E (47) boot: Bootloader digest generation failed (258). SECURE BOOT IS NOT ENABLED.
You can check the sdkconfig.

I'we been using this as a guide and at this point I don't get what I'm missing.

If you need any further info please let me know.

Any help will be appreciated. Thank you,
Vlad

vlad2891
Posts: 8
Joined: Wed Mar 08, 2017 2:27 pm

Re: Secure boot problem

Postby vlad2891 » Thu Apr 13, 2017 7:52 am

Hello guys,

After taking a peak through the esp-idf itself, I found that the error is first detected here in esp_image_format.c:111

Code: Select all

if (src_addr % SPI_FLASH_MMU_PAGE_SIZE != 0) {
        /* Image must start on a 64KB boundary

           (This is not a technical limitation, only the flash mapped regions need to be 64KB aligned.  But the most
           consistent way to do this is to have all the offsets internal to the image correctly 64KB aligned, and then
           start the image on a 64KB boundary also.)
         */
        return ESP_ERR_INVALID_ARG;
    }
Can anyone give me a hint on what this means and what am I doing wrong?

Thank you,
Vlad

vlad2891
Posts: 8
Joined: Wed Mar 08, 2017 2:27 pm

Re: Secure boot problem

Postby vlad2891 » Fri Apr 14, 2017 7:13 am

Hello guys,

So, digging around more I found this scenario:
1. On boot, if secure boot enabled, secured boot enabled is called (bootloader_start.c#L357)

Code: Select all

err = esp_secure_boot_permanently_enable();
2. this leads to secure_boot.c file that calls image verify (secure_boot.c#L114)

Code: Select all

 err = esp_image_basic_verify(0x1000, true, &image_len);
here the src_image is hardcoded to 0x1000

3. in the esp_image_format.c we reach the above check (esp_image_format.c#L111)

Code: Select all

if (src_addr % SPI_FLASH_MMU_PAGE_SIZE != 0) { ...
4. the value for SPI_FLASH_MMU_PAGE_SIZE comes from (esp_spi_flash.h#L34) where is defined as 0x10000.

Code: Select all

#define SPI_FLASH_MMU_PAGE_SIZE 0x10000 /**< Flash cache MMU mapping page size */
Here is the tricky part, from 2,3 and 4, replacing the values we get

Code: Select all

if ( 0x1000 %  0x10000 != 0) { ...
This will always return true( 0x1000 % 0x10000 = 0x1000 != 0), thus leaving the secure boot disabled.

Is there a reason this is disabled or is it a bug? Let me know what you guys think.

Regards,
Vlad

ESP_igrr
Posts: 2072
Joined: Tue Dec 01, 2015 8:37 am

Re: Secure boot problem

Postby ESP_igrr » Fri Apr 14, 2017 8:04 am

Thanks for reporting this, seems to be a bug.

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

Re: Secure boot problem

Postby ESP_Angus » Fri Apr 28, 2017 12:01 am

Sorry for the delay in replying.

This has been fixed in esp-idf master branch as of commit 32fc0a62b9e20dbf6499f8ebf4a5026fe05e28ce, and will be in the forthcoming v2.1 release.

smeedy
Posts: 15
Joined: Sun Jan 22, 2017 10:35 pm

Re: Secure boot problem

Postby smeedy » Fri Apr 28, 2017 7:10 am

Hi Angus,

Nice! We've been waiting for this to be fixed. Thanks for the effort.

regards,
Martijn

Who is online

Users browsing this forum: nullbert and 112 guests