Skip to content

Commit a45d713

Browse files
committed
Merge branch 'refactor/unify_bootloader_sha' into 'master'
Unify the bootloader_sha layer Closes IDF-12704 See merge request espressif/esp-idf!37927
2 parents 85a88f3 + a02dec0 commit a45d713

File tree

14 files changed

+93
-536
lines changed

14 files changed

+93
-536
lines changed

components/bootloader_support/CMakeLists.txt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ if(esp_tee_build)
1111
"bootloader_flash/include")
1212

1313
set(tee_srcs "src/flash_partitions.c"
14-
"src/${IDF_TARGET}/bootloader_sha.c"
14+
"src/bootloader_sha.c"
1515
"src/bootloader_common_loader.c"
1616
"src/esp_image_format.c"
1717
"src/bootloader_utility.c"
@@ -64,6 +64,8 @@ if(CONFIG_APP_BUILD_TYPE_APP_2NDBOOT)
6464
)
6565
endif()
6666

67+
list(APPEND srcs "src/bootloader_sha.c")
68+
6769
if(BOOTLOADER_BUILD OR CONFIG_APP_BUILD_TYPE_RAM)
6870
set(include_dirs "include" "bootloader_flash/include"
6971
"private_include")
@@ -73,7 +75,6 @@ if(BOOTLOADER_BUILD OR CONFIG_APP_BUILD_TYPE_RAM)
7375
"src/bootloader_clock_loader.c"
7476
"src/bootloader_console.c"
7577
"src/bootloader_console_loader.c"
76-
"src/${IDF_TARGET}/bootloader_sha.c"
7778
"src/${IDF_TARGET}/bootloader_soc.c"
7879
"src/${IDF_TARGET}/bootloader_${IDF_TARGET}.c"
7980
)
@@ -86,8 +87,6 @@ if(BOOTLOADER_BUILD OR CONFIG_APP_BUILD_TYPE_RAM)
8687
"src/${IDF_TARGET}/bootloader_ecdsa.c")
8788
endif()
8889
else()
89-
list(APPEND srcs
90-
"src/idf/bootloader_sha.c")
9190
set(include_dirs "include" "bootloader_flash/include")
9291
set(priv_include_dirs "private_include")
9392
# heap is required for `heap_memory_layout.h` header

components/bootloader_support/src/esp32/bootloader_sha.c renamed to components/bootloader_support/src/bootloader_sha.c

Lines changed: 90 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,58 @@
11
/*
2-
* SPDX-FileCopyrightText: 2017-2021 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
6-
#include "bootloader_sha.h"
6+
7+
#include <assert.h>
78
#include <stdbool.h>
89
#include <string.h>
9-
#include <assert.h>
1010
#include <sys/param.h>
1111

12-
#include "esp32/rom/sha.h"
12+
#include "bootloader_sha.h"
13+
#include "soc/soc_caps.h"
14+
#include "rom/sha.h"
15+
#include "sdkconfig.h"
16+
17+
#if NON_OS_BUILD || CONFIG_APP_BUILD_TYPE_RAM
18+
#if !CONFIG_IDF_TARGET_ESP32
19+
static SHA_CTX ctx;
20+
21+
bootloader_sha256_handle_t bootloader_sha256_start()
22+
{
23+
// Enable SHA hardware
24+
ets_sha_enable();
25+
ets_sha_init(&ctx, SHA2_256);
26+
return &ctx; // Meaningless non-NULL value
27+
}
28+
29+
void bootloader_sha256_data(bootloader_sha256_handle_t handle, const void *data, size_t data_len)
30+
{
31+
assert(handle != NULL);
32+
33+
#if !SOC_SECURE_BOOT_V2_ECC
34+
/* For secure boot, the key field consists of 1 byte of curve identifier and 64 bytes of ECDSA public key.
35+
* While verifying the signature block, we need to calculate the SHA of this key field which is of 65 bytes.
36+
* ets_sha_update handles it cleanly so we can safely remove the check:
37+
*/
38+
assert(data_len % 4 == 0);
39+
#endif /* SOC_SECURE_BOOT_V2_ECC */
40+
41+
ets_sha_update(&ctx, data, data_len, false);
42+
}
43+
44+
void bootloader_sha256_finish(bootloader_sha256_handle_t handle, uint8_t *digest)
45+
{
46+
assert(handle != NULL);
47+
48+
if (digest == NULL) {
49+
bzero(&ctx, sizeof(ctx));
50+
return;
51+
}
52+
ets_sha_finish(&ctx, digest);
53+
}
54+
#else /* !CONFIG_IDF_TARGET_ESP32 */
55+
1356
#include "soc/dport_reg.h"
1457
#include "soc/hwcrypto_periph.h"
1558

@@ -114,3 +157,46 @@ void bootloader_sha256_finish(bootloader_sha256_handle_t handle, uint8_t *digest
114157
}
115158
asm volatile ("memw");
116159
}
160+
#endif /* CONFIG_IDF_TARGET_ESP32 */
161+
#else /* NON_OS_BUILD || CONFIG_APP_BUILD_TYPE_RAM */
162+
163+
#include "bootloader_flash_priv.h"
164+
#include <mbedtls/sha256.h>
165+
166+
bootloader_sha256_handle_t bootloader_sha256_start(void)
167+
{
168+
mbedtls_sha256_context *ctx = (mbedtls_sha256_context *)malloc(sizeof(mbedtls_sha256_context));
169+
if (!ctx) {
170+
return NULL;
171+
}
172+
mbedtls_sha256_init(ctx);
173+
int ret = mbedtls_sha256_starts(ctx, false);
174+
if (ret != 0) {
175+
return NULL;
176+
}
177+
return ctx;
178+
}
179+
180+
void bootloader_sha256_data(bootloader_sha256_handle_t handle, const void *data, size_t data_len)
181+
{
182+
assert(handle != NULL);
183+
mbedtls_sha256_context *ctx = (mbedtls_sha256_context *)handle;
184+
int ret = mbedtls_sha256_update(ctx, data, data_len);
185+
assert(ret == 0);
186+
(void)ret;
187+
}
188+
189+
void bootloader_sha256_finish(bootloader_sha256_handle_t handle, uint8_t *digest)
190+
{
191+
assert(handle != NULL);
192+
mbedtls_sha256_context *ctx = (mbedtls_sha256_context *)handle;
193+
if (digest != NULL) {
194+
int ret = mbedtls_sha256_finish(ctx, digest);
195+
assert(ret == 0);
196+
(void)ret;
197+
}
198+
mbedtls_sha256_free(ctx);
199+
free(handle);
200+
handle = NULL;
201+
}
202+
#endif /* !(NON_OS_BUILD || CONFIG_APP_BUILD_TYPE_RAM) */

components/bootloader_support/src/esp32c2/bootloader_sha.c

Lines changed: 0 additions & 44 deletions
This file was deleted.

components/bootloader_support/src/esp32c3/bootloader_sha.c

Lines changed: 0 additions & 40 deletions
This file was deleted.

components/bootloader_support/src/esp32c5/bootloader_sha.c

Lines changed: 0 additions & 44 deletions
This file was deleted.

components/bootloader_support/src/esp32c6/bootloader_sha.c

Lines changed: 0 additions & 44 deletions
This file was deleted.

components/bootloader_support/src/esp32c61/bootloader_sha.c

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)