diff --git a/components/app_update/esp_ota_ops.c b/components/app_update/esp_ota_ops.c index 920e49aa7c..80a07af5fa 100644 --- a/components/app_update/esp_ota_ops.c +++ b/components/app_update/esp_ota_ops.c @@ -43,6 +43,8 @@ #include "esp32c2/rom/secure_boot.h" #elif CONFIG_IDF_TARGET_ESP32C6 #include "esp32c6/rom/secure_boot.h" +#elif CONFIG_IDF_TARGET_ESP32H2 +#include "esp32h2/rom/secure_boot.h" #endif #define SUB_TYPE_ID(i) (i & 0x0F) diff --git a/components/bootloader_support/src/esp32h2/bootloader_sha.c b/components/bootloader_support/src/esp32h2/bootloader_sha.c index c439307823..212345ca81 100644 --- a/components/bootloader_support/src/esp32h2/bootloader_sha.c +++ b/components/bootloader_support/src/esp32h2/bootloader_sha.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -24,7 +24,11 @@ bootloader_sha256_handle_t bootloader_sha256_start() void bootloader_sha256_data(bootloader_sha256_handle_t handle, const void *data, size_t data_len) { assert(handle != NULL); - assert(data_len % 4 == 0); + /* H2 secure boot key field consists of 1 byte of curve identifier and 64 bytes of ECDSA public key. + * While verifying the signature block, we need to calculate the SHA of this key field which is of 65 bytes. + * ets_sha_update handles it cleanly so we can safely remove the check: + * assert(data_len % 4) == 0 + */ ets_sha_update(&ctx, data, data_len, false); } diff --git a/components/bootloader_support/src/secure_boot_v2/secure_boot.c b/components/bootloader_support/src/secure_boot_v2/secure_boot.c index 74f4893384..5c90ce6f41 100644 --- a/components/bootloader_support/src/secure_boot_v2/secure_boot.c +++ b/components/bootloader_support/src/secure_boot_v2/secure_boot.c @@ -14,22 +14,8 @@ #include "esp_image_format.h" #include "esp_efuse.h" #include "esp_efuse_table.h" +#include "secure_boot_signature_priv.h" -#if CONFIG_IDF_TARGET_ESP32 -#include "esp32/rom/secure_boot.h" -#elif CONFIG_IDF_TARGET_ESP32S2 -#include "esp32s2/rom/secure_boot.h" -#elif CONFIG_IDF_TARGET_ESP32C3 -#include "esp32c3/rom/secure_boot.h" -#elif CONFIG_IDF_TARGET_ESP32S3 -#include "esp32s3/rom/secure_boot.h" -#elif CONFIG_IDF_TARGET_ESP32H4 -#include "esp32h4/rom/secure_boot.h" -#elif CONFIG_IDF_TARGET_ESP32C2 -#include "esp32c2/rom/secure_boot.h" -#elif CONFIG_IDF_TARGET_ESP32C6 -#include "esp32c6/rom/secure_boot.h" -#endif /* The following API implementations are used only when called * from the bootloader code. diff --git a/components/bootloader_support/src/secure_boot_v2/secure_boot_signature_priv.h b/components/bootloader_support/src/secure_boot_v2/secure_boot_signature_priv.h index 792ad37bc8..540b18aebf 100644 --- a/components/bootloader_support/src/secure_boot_v2/secure_boot_signature_priv.h +++ b/components/bootloader_support/src/secure_boot_v2/secure_boot_signature_priv.h @@ -19,6 +19,8 @@ #include "esp32c2/rom/secure_boot.h" #elif CONFIG_IDF_TARGET_ESP32C6 #include "esp32c6/rom/secure_boot.h" +#elif CONFIG_IDF_TARGET_ESP32H2 +#include "esp32h2/rom/secure_boot.h" #endif esp_err_t verify_ecdsa_signature_block(const ets_secure_boot_signature_t *sig_block, const uint8_t *image_digest, const ets_secure_boot_sig_block_t *trusted_block); diff --git a/components/esp_rom/include/esp32h2/rom/ecdsa.h b/components/esp_rom/include/esp32h2/rom/ecdsa.h new file mode 100644 index 0000000000..79cbb05c0b --- /dev/null +++ b/components/esp_rom/include/esp32h2/rom/ecdsa.h @@ -0,0 +1,26 @@ +/* + * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#define ETS_DIGEST_LEN 32 /* SHA-256, bytes */ + +typedef enum { + ECDSA_CURVE_P192 = 1, + ECDSA_CURVE_P256 = 2 +} ECDSA_CURVE; + +int ets_ecdsa_verify(const uint8_t *key, const uint8_t *sig, ECDSA_CURVE curve_id, const uint8_t *digest, uint8_t *verified_digest); + +#ifdef __cplusplus +} +#endif diff --git a/components/esp_rom/include/esp32h2/rom/secure_boot.h b/components/esp_rom/include/esp32h2/rom/secure_boot.h index 6c1e95974b..cb7df5db60 100644 --- a/components/esp_rom/include/esp32h2/rom/secure_boot.h +++ b/components/esp_rom/include/esp32h2/rom/secure_boot.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -9,6 +9,7 @@ #include #include #include "ets_sys.h" +#include "ecdsa.h" #include "rsa_pss.h" #include "esp_assert.h" @@ -16,6 +17,8 @@ extern "C" { #endif +#if CONFIG_SECURE_BOOT_V2_ENABLED || CONFIG_SECURE_SIGNED_APPS_NO_SECURE_BOOT + typedef struct ets_secure_boot_sig_block ets_secure_boot_sig_block_t; typedef struct ets_secure_boot_signature ets_secure_boot_signature_t; typedef struct ets_secure_boot_key_digests ets_secure_boot_key_digests_t; @@ -69,6 +72,8 @@ void ets_secure_boot_revoke_public_key_digest(int index); (Up to 3 in a signature sector are appended to the image) */ +#if CONFIG_SECURE_SIGNED_APPS_RSA_SCHEME + struct ets_secure_boot_sig_block { uint8_t magic_byte; uint8_t version; @@ -81,6 +86,27 @@ struct ets_secure_boot_sig_block { uint8_t _padding[16]; }; +#elif CONFIG_SECURE_SIGNED_APPS_ECDSA_V2_SCHEME + +struct __attribute((packed)) ets_secure_boot_sig_block { + uint8_t magic_byte; + uint8_t version; + uint8_t _reserved1; + uint8_t _reserved2; + uint8_t image_digest[32]; + struct { + struct { + uint8_t curve_id; /* ETS_ECDSA_CURVE_P192 / ETS_ECDSA_CURVE_P256 */ + uint8_t point[64]; /* X followed by Y (both little-endian), plus zero bytes if P192 */ + } key; + uint8_t signature[64]; /* r followed by s (both little-endian) */ + uint8_t padding[1031]; + } ecdsa; + uint32_t block_crc; /* note: crc covers all bytes in the structure before it, regardless of version field */ + uint8_t _padding[16]; +}; +#endif + ESP_STATIC_ASSERT(sizeof(ets_secure_boot_sig_block_t) == 1216, "invalid sig block size"); #define SECURE_BOOT_NUM_BLOCKS 3 @@ -100,6 +126,8 @@ struct ets_secure_boot_key_digests { bool allow_key_revoke; }; +#endif /* CONFIG_SECURE_BOOT_V2_ENABLED || CONFIG_SECURE_SIGNED_APPS_NO_SECURE_BOOT */ + #ifdef __cplusplus } #endif diff --git a/components/soc/esp32h2/include/soc/Kconfig.soc_caps.in b/components/soc/esp32h2/include/soc/Kconfig.soc_caps.in index 0ef496b85e..40910c74dd 100644 --- a/components/soc/esp32h2/include/soc/Kconfig.soc_caps.in +++ b/components/soc/esp32h2/include/soc/Kconfig.soc_caps.in @@ -59,6 +59,10 @@ config SOC_TEMP_SENSOR_SUPPORTED bool default y +config SOC_SUPPORTS_SECURE_DL_MODE + bool + default y + config SOC_EFUSE_KEY_PURPOSE_FIELD bool default y @@ -131,6 +135,10 @@ config SOC_FLASH_ENC_SUPPORTED bool default y +config SOC_SECURE_BOOT_SUPPORTED + bool + default y + config SOC_BOD_SUPPORTED bool default y diff --git a/components/soc/esp32h2/include/soc/soc_caps.h b/components/soc/esp32h2/include/soc/soc_caps.h index 35124418bf..05e230dd21 100644 --- a/components/soc/esp32h2/include/soc/soc_caps.h +++ b/components/soc/esp32h2/include/soc/soc_caps.h @@ -39,7 +39,7 @@ #define SOC_IEEE802154_BLE_ONLY 1 #define SOC_USB_SERIAL_JTAG_SUPPORTED 1 #define SOC_TEMP_SENSOR_SUPPORTED 1 -// #define SOC_SUPPORTS_SECURE_DL_MODE 1 // TODO: IDF-6281 +#define SOC_SUPPORTS_SECURE_DL_MODE 1 //#define SOC_RISCV_COPROC_SUPPORTED 1 // TODO: IDF-6272 #define SOC_EFUSE_KEY_PURPOSE_FIELD 1 #define SOC_RTC_FAST_MEM_SUPPORTED 1 @@ -60,7 +60,7 @@ #define SOC_HMAC_SUPPORTED 1 #define SOC_DIG_SIGN_SUPPORTED 1 #define SOC_FLASH_ENC_SUPPORTED 1 -// #define SOC_SECURE_BOOT_SUPPORTED 1 // TODO: IDF-6281 +#define SOC_SECURE_BOOT_SUPPORTED 1 #define SOC_BOD_SUPPORTED 1 #define SOC_APM_SUPPORTED 1 #define SOC_PMU_SUPPORTED 1 @@ -397,7 +397,6 @@ #define SOC_EFUSE_DIS_DIRECT_BOOT 1 #define SOC_EFUSE_SOFT_DIS_JTAG 1 -// TODO: IDF-6281 (Copy from esp32c6, need check) /*-------------------------- Secure Boot CAPS----------------------------*/ #define SOC_SECURE_BOOT_V2_RSA 1 #define SOC_SECURE_BOOT_V2_ECC 1 diff --git a/docs/docs_not_updated/esp32h2.txt b/docs/docs_not_updated/esp32h2.txt index f1e9ca781f..1be9bd7095 100644 --- a/docs/docs_not_updated/esp32h2.txt +++ b/docs/docs_not_updated/esp32h2.txt @@ -118,9 +118,6 @@ api-reference/protocols/mdns api-reference/protocols/index api-reference/protocols/asio security/esp32h2_log.inc -security/security -security/secure-boot-v2 -security/secure-boot-v1 about resources migration-guides/release-5.x/5.1/index diff --git a/docs/en/security/secure-boot-v2.rst b/docs/en/security/secure-boot-v2.rst index 5356f3104a..b81383e6cf 100644 --- a/docs/en/security/secure-boot-v2.rst +++ b/docs/en/security/secure-boot-v2.rst @@ -3,19 +3,19 @@ Secure Boot V2 ============== -{IDF_TARGET_SBV2_SCHEME:default="RSA-PSS", esp32c2="ECDSA", esp32c6="RSA-PSS or ECDSA"} +{IDF_TARGET_SBV2_SCHEME:default="RSA-PSS", esp32c2="ECDSA", esp32c6 or esp32h2="RSA-PSS or ECDSA"} -{IDF_TARGET_SBV2_KEY:default="RSA-3072", esp32c2="ECDSA-256 or ECDSA-192", esp32c6="RSA-3072, ECDSA-256, or ECDSA-192"} +{IDF_TARGET_SBV2_KEY:default="RSA-3072", esp32c2="ECDSA-256 or ECDSA-192", esp32c6 or esp32h2="RSA-3072, ECDSA-256, or ECDSA-192"} -{IDF_TARGET_SECURE_BOOT_OPTION_TEXT:default="", esp32c6="RSA is recommended because of faster verification time. You can choose between RSA and ECDSA scheme from the menu."} +{IDF_TARGET_SECURE_BOOT_OPTION_TEXT:default="", esp32c6 or esp32h2="RSA is recommended because of faster verification time. You can choose between RSA and ECDSA scheme from the menu."} {IDF_TARGET_ECO_VERSION:default="", esp32="(ECO 3 onwards)", esp32c3="(ECO 3 onwards)"} -{IDF_TARGET_RSA_TIME:default="", esp32c6="~2.7 ms"} +{IDF_TARGET_RSA_TIME:default="", esp32c6="~2.7 ms", esp32h2="~4.5 ms"} -{IDF_TARGET_ECDSA_TIME:default="", esp32c6="~21.5 ms"} +{IDF_TARGET_ECDSA_TIME:default="", esp32c6="~21.5 ms", esp32h2="~36 ms"} -{IDF_TARGET_CPU_FREQ:default="", esp32c6="160 MHz"} +{IDF_TARGET_CPU_FREQ:default="", esp32c6="160 MHz", esp32h2="96 MHz"} {IDF_TARGET_SBV2_DEFAULT_SCHEME:default="RSA", esp32c2="ECDSA (V2)"}