mirror of
https://github.com/espressif/esp-idf.git
synced 2026-10-01 10:40:47 +03:00
feat(coredump): Migrated coredump sha256 implementation to PSA
Added test case to test the sha256 implementation of coredump
This commit is contained in:
committed by
Ashish Sharma
parent
eb5e92063f
commit
72a2f0b9aa
@@ -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;
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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 <string.h>
|
||||
#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);
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
|
||||
CONFIG_ESP_COREDUMP_ENABLE_TO_UART=y
|
||||
CONFIG_ESP_COREDUMP_CHECKSUM_SHA256=y
|
||||
CONFIG_ESP_COREDUMP_ENABLE_TO_UART=y
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user