fix: use ROM APIs for espcoredump SHA operations

This commit is contained in:
Ashish Sharma
2025-12-19 07:29:08 +08:00
parent 98afd06c8f
commit efee84f929
5 changed files with 39 additions and 103 deletions
@@ -89,8 +89,10 @@ extern "C" {
typedef uint32_t core_dump_crc_t;
#if CONFIG_IDF_TARGET_ESP32
#include "psa/crypto.h"
typedef psa_hash_operation_t sha256_ctx_t;
/* Use ESP32 ROM SHA context directly to avoid malloc during panic handler.
* ESP32 ROM SHA has a different context structure than other targets. */
#include "rom/sha.h"
typedef SHA_CTX sha256_ctx_t;
#else
#include "hal/sha_types.h" /* SHA_CTX */
typedef SHA_CTX sha256_ctx_t;
+16 -10
View File
@@ -19,26 +19,32 @@ uint32_t esp_core_dump_elf_version(void) __attribute__((alias("core_dump_sha_ver
#if CONFIG_IDF_TARGET_ESP32
#include "rom/sha.h"
/* Use ESP32 ROM SHA hardware directly to avoid malloc during panic handler.
* PSA driver allocates memory which is unsafe during panic handling.
* ESP32 ROM SHA API uses bits instead of bytes and has a different context structure. */
static void core_dump_sha256_start(core_dump_sha_ctx_t *sha_ctx)
{
sha_ctx->ctx = psa_hash_operation_init();
psa_hash_setup(&sha_ctx->ctx, PSA_ALG_SHA_256);
SHA_CTX *ctx = (SHA_CTX *)&sha_ctx->ctx;
/* Initialize context to zero */
memset(ctx, 0, sizeof(SHA_CTX));
ets_sha_enable();
ets_sha_init(ctx);
}
static void core_dump_sha256_update(core_dump_sha_ctx_t *sha_ctx, const void *data, size_t data_len)
{
#if ((CONFIG_MBEDTLS_HARDWARE_SHA) && (MBEDTLS_MAJOR_VERSION < 4))
mbedtls_psa_hash_operation_t *op = &sha_ctx->ctx.MBEDTLS_PRIVATE(ctx).mbedtls_ctx;
mbedtls_sha256_context *ctx = &op->MBEDTLS_PRIVATE(ctx).sha256;
ctx->mode = ESP_MBEDTLS_SHA256_SOFTWARE;
#endif /* (CONFIG_MBEDTLS_HARDWARE_SHA) */
psa_hash_update(&sha_ctx->ctx, data, data_len);
SHA_CTX *ctx = (SHA_CTX *)&sha_ctx->ctx;
/* ESP32 ROM SHA update takes input_bits, not bytes */
ets_sha_update(ctx, SHA2_256, (const uint8_t *)data, data_len * 8);
}
static void core_dump_sha256_finish(core_dump_sha_ctx_t *sha_ctx)
{
size_t hash_len;
psa_hash_finish(&sha_ctx->ctx, sha_ctx->result, COREDUMP_SHA256_LEN, &hash_len);
SHA_CTX *ctx = (SHA_CTX *)&sha_ctx->ctx;
ets_sha_finish(ctx, SHA2_256, sha_ctx->result);
ets_sha_disable();
}
#else