diff --git a/components/espcoredump/include_core_dump/esp_core_dump_types.h b/components/espcoredump/include_core_dump/esp_core_dump_types.h index 13b5c49bbd3..dc2c1e804a8 100644 --- a/components/espcoredump/include_core_dump/esp_core_dump_types.h +++ b/components/espcoredump/include_core_dump/esp_core_dump_types.h @@ -89,8 +89,8 @@ extern "C" { typedef uint32_t core_dump_crc_t; #if CONFIG_IDF_TARGET_ESP32 -#include "mbedtls/sha256.h" /* mbedtls_sha256_context */ -typedef mbedtls_sha256_context sha256_ctx_t; +#include "psa/crypto.h" +typedef psa_hash_operation_t sha256_ctx_t; #else #include "hal/sha_types.h" /* SHA_CTX */ typedef SHA_CTX sha256_ctx_t; diff --git a/components/espcoredump/src/core_dump_sha.c b/components/espcoredump/src/core_dump_sha.c index e77fd44745c..015a8cd766f 100644 --- a/components/espcoredump/src/core_dump_sha.c +++ b/components/espcoredump/src/core_dump_sha.c @@ -21,23 +21,19 @@ uint32_t esp_core_dump_elf_version(void) __attribute__((alias("core_dump_sha_ver static void core_dump_sha256_start(core_dump_sha_ctx_t *sha_ctx) { - mbedtls_sha256_init(&sha_ctx->ctx); - mbedtls_sha256_starts(&sha_ctx->ctx, false); + psa_hash_operation_init(sha_ctx->ctx); + psa_hash_setup(&sha_ctx->ctx, PSA_ALG_SHA_256); } static void core_dump_sha256_update(core_dump_sha_ctx_t *sha_ctx, const void *data, size_t data_len) { - // set software mode of SHA calculation -#if CONFIG_MBEDTLS_HARDWARE_SHA - sha_ctx->ctx.mode = ESP_MBEDTLS_SHA256_SOFTWARE; -#endif - mbedtls_sha256_update(&sha_ctx->ctx, data, data_len); + psa_hash_update(&sha_ctx->ctx, data, data_len); } static void core_dump_sha256_finish(core_dump_sha_ctx_t *sha_ctx) { - mbedtls_sha256_finish(&sha_ctx->ctx, sha_ctx->result); - mbedtls_sha256_free(&sha_ctx->ctx); + size_t hash_len; + psa_hash_finish(&sha_ctx->ctx, sha_ctx->result, COREDUMP_SHA256_LEN, &hash_len); } #else diff --git a/components/espcoredump/test_apps/main/CMakeLists.txt b/components/espcoredump/test_apps/main/CMakeLists.txt index 565a14b80d8..7f8382a3b78 100644 --- a/components/espcoredump/test_apps/main/CMakeLists.txt +++ b/components/espcoredump/test_apps/main/CMakeLists.txt @@ -1,5 +1,11 @@ -idf_component_register(SRCS "test_coredump_main.c" - "test_sections.c" +set(priv_includes "") +set(SRCS "test_coredump_main.c" + "test_sections.c") +idf_component_get_property(espcoredump_dir espcoredump COMPONENT_DIR) +list(APPEND priv_includes "${espcoredump_dir}/include_core_dump") +# list(APPEND SRCS "${espcoredump_dir}/src/core_dump_sha.c") +idf_component_register(SRCS ${SRCS} INCLUDE_DIRS "." PRIV_REQUIRES unity espcoredump + PRIV_INCLUDE_DIRS ${priv_includes} WHOLE_ARCHIVE) diff --git a/components/espcoredump/test_apps/main/test_sections.c b/components/espcoredump/test_apps/main/test_sections.c index 445b4328edb..5a70caa0428 100644 --- a/components/espcoredump/test_apps/main/test_sections.c +++ b/components/espcoredump/test_apps/main/test_sections.c @@ -1,11 +1,13 @@ /* - * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ #include #include "unity.h" #include "esp_attr.h" +#include "esp_core_dump_types.h" +#include "core_dump_checksum.h" /* Global variables that should be part of the coredump */ COREDUMP_IRAM_DATA_ATTR uint32_t var_iram = 0x42; @@ -64,3 +66,49 @@ TEST_CASE("test variables presence in core dump sections", "[espcoredump]") TEST_ASSERT(is_addr_in_region(&var_rtcfast, (uint8_t *) section_start, section_size)); #endif // SOC_RTC_MEM_SUPPORTED } + +/* + * This section tests the SHA256 checksum functionality for the espcoredump component + */ +TEST_CASE("espcoredump SHA256 checksum API", "[espcoredump]") +{ +#define SHA256_RESULT_LEN 32 + ESP_LOGI("espcoredump", "Testing SHA256 checksum API"); + + /* Known test vector for SHA-256 */ + static const char *test_str = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"; + static const uint8_t expected_sha[SHA256_RESULT_LEN] = { + 0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8, + 0xe5, 0xc0, 0x26, 0x93, 0x0c, 0x3e, 0x60, 0x39, + 0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67, + 0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1 + }; + + // Verify size and version + TEST_ASSERT_EQUAL(SHA256_RESULT_LEN, esp_core_dump_checksum_size()); + TEST_ASSERT_EQUAL(COREDUMP_VERSION_ELF_SHA256, esp_core_dump_elf_version()); + + // Test both single update and multiple updates with the same input + checksum_ctx_t sha_ctx1, sha_ctx2; + core_dump_checksum_bytes checksum1, checksum2; + + // Test 1: Multiple updates (split string) + const char *part1 = "abcdbc"; + const char *part2 = "decdefdefgefgh"; + const char *part3 = "fghighijhijkijkljklmklmnlmnomnopnopq"; + + esp_core_dump_checksum_init(&sha_ctx1); + esp_core_dump_checksum_update(&sha_ctx1, (void*)part1, strlen(part1)); + esp_core_dump_checksum_update(&sha_ctx1, (void*)part2, strlen(part2)); + esp_core_dump_checksum_update(&sha_ctx1, (void*)part3, strlen(part3)); + esp_core_dump_checksum_finish(&sha_ctx1, &checksum1); + + // Test 2: Single update (whole string) + esp_core_dump_checksum_init(&sha_ctx2); + esp_core_dump_checksum_update(&sha_ctx2, (void*)test_str, strlen(test_str)); + esp_core_dump_checksum_finish(&sha_ctx2, &checksum2); + + // Check against known vector and ensure both methods match + TEST_ASSERT_EQUAL_MEMORY(expected_sha, checksum1, SHA256_RESULT_LEN); + TEST_ASSERT_EQUAL_MEMORY(checksum1, checksum2, SHA256_RESULT_LEN); +} diff --git a/components/espcoredump/test_apps/pytest_coredump.py b/components/espcoredump/test_apps/pytest_coredump.py index 8685495c35c..38ba25f978e 100644 --- a/components/espcoredump/test_apps/pytest_coredump.py +++ b/components/espcoredump/test_apps/pytest_coredump.py @@ -9,3 +9,9 @@ from pytest_embedded_idf.utils import idf_parametrize @idf_parametrize('target', ['esp32', 'esp32c3', 'esp32c2'], indirect=['target']) def test_coredump(dut: Dut) -> None: dut.run_all_single_board_cases() + + +@pytest.mark.generic +@pytest.mark.parametrize('config', ['checksum_sha256',], indirect=True) +def test_coredump_sha(dut: Dut) -> None: + dut.run_all_single_board_cases() diff --git a/components/espcoredump/test_apps/sdkconfig.ci.checksum_sha256 b/components/espcoredump/test_apps/sdkconfig.ci.checksum_sha256 new file mode 100644 index 00000000000..1c351cf836f --- /dev/null +++ b/components/espcoredump/test_apps/sdkconfig.ci.checksum_sha256 @@ -0,0 +1,4 @@ + +CONFIG_ESP_COREDUMP_ENABLE_TO_UART=y +CONFIG_ESP_COREDUMP_CHECKSUM_SHA256=y +CONFIG_ESP_COREDUMP_ENABLE_TO_UART=y diff --git a/components/espcoredump/test_apps/sdkconfig.defaults b/components/espcoredump/test_apps/sdkconfig.defaults index 247d7f79158..87cbee6cd87 100644 --- a/components/espcoredump/test_apps/sdkconfig.defaults +++ b/components/espcoredump/test_apps/sdkconfig.defaults @@ -1,2 +1,2 @@ CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0=n -CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH=y +CONFIG_ESP_COREDUMP_ENABLE_TO_UART=y