|
1 | 1 | /*
|
2 |
| - * SPDX-FileCopyrightText: 2017-2021 Espressif Systems (Shanghai) CO LTD |
| 2 | + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD |
3 | 3 | *
|
4 | 4 | * SPDX-License-Identifier: Apache-2.0
|
5 | 5 | */
|
6 |
| -#include "bootloader_sha.h" |
| 6 | + |
| 7 | +#include <assert.h> |
7 | 8 | #include <stdbool.h>
|
8 | 9 | #include <string.h>
|
9 |
| -#include <assert.h> |
10 | 10 | #include <sys/param.h>
|
11 | 11 |
|
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 | + |
13 | 56 | #include "soc/dport_reg.h"
|
14 | 57 | #include "soc/hwcrypto_periph.h"
|
15 | 58 |
|
@@ -114,3 +157,46 @@ void bootloader_sha256_finish(bootloader_sha256_handle_t handle, uint8_t *digest
|
114 | 157 | }
|
115 | 158 | asm volatile ("memw");
|
116 | 159 | }
|
| 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) */ |
0 commit comments