fix(secure_boot): Fix SB verification failure when sig block and key digest mismatch

- Secure boot V2 verification failed when multiple keys are used to sign the bootloader
  and the application is signed with a key other than the first key that is used to
  sign the bootloader.
- The issue was introduced as a regression from the commit `ff16ce43`.
- Added a QEMU test for recreating the issue.
- Made SECURE_BOOT_FLASH_BOOTLOADER_DEFAULT independent of SECURE_BOOT_BUILD_SIGNED_BINARIES.
This commit is contained in:
harshal.patil
2025-02-27 16:18:47 +05:30
parent afb2154247
commit a6ea9bcd41
12 changed files with 211 additions and 5 deletions

View File

@@ -1,4 +1,4 @@
# SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
# SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD
# SPDX-License-Identifier: Unlicense OR CC0-1.0
import os
import struct
@@ -91,6 +91,36 @@ def test_examples_security_secure_boot(dut: Dut) -> None:
dut.burn_wafer_version()
# Test secure boot flow.
# Correctly signed bootloader + correctly signed app should work
@pytest.mark.host_test
@pytest.mark.qemu
@pytest.mark.esp32c3
@pytest.mark.parametrize(
'qemu_extra_args',
[
f'-drive file={os.path.join(os.path.dirname(__file__), "test", "esp32c3_efuses.bin")},if=none,format=raw,id=efuse '
'-global driver=nvram.esp32c3.efuse,property=drive,value=efuse '
'-global driver=timer.esp32c3.timg,property=wdt_disable,value=true',
],
indirect=True,
)
@pytest.mark.parametrize('config', ['qemu'], indirect=True)
def test_examples_security_secure_boot_qemu(dut: Dut) -> None:
try:
dut.expect('Secure Boot is enabled', timeout=10)
dut.expect('Restarting now.', timeout=10)
dut.expect('Secure Boot is enabled', timeout=10)
finally:
# the above example test burns the efuses, and hence the efuses file which the
# qemu uses to emulate the efuses, "esp32c3_efuses.bin", gets modified.
# Thus, restore the efuses file values back to the default ESP32C3 efuses values.
with open(os.path.join(os.path.dirname(__file__), 'test', 'esp32c3_efuses.bin'), 'wb') as efuse_file:
esp32c3_efuses = '0' * 77 + 'c' + '0' * 1970
efuse_file.write(bytearray.fromhex(esp32c3_efuses))
# Test efuse key index and key block combination.
# Any key index can be written to any key block and should work
@pytest.mark.esp32c3