From da2c29751ef840ff2fb66a623f93bfebbb63c6a8 Mon Sep 17 00:00:00 2001 From: "harshal.patil" Date: Thu, 11 May 2023 15:28:02 +0530 Subject: [PATCH] hal: combine ecc hal test app into security peripherals test app --- components/hal/.build-test-rules.yml | 9 -- components/hal/test_apps/ecc/CMakeLists.txt | 7 -- components/hal/test_apps/ecc/README.md | 43 --------- .../hal/test_apps/ecc/main/CMakeLists.txt | 6 -- components/hal/test_apps/ecc/main/app_main.c | 13 --- components/hal/test_apps/ecc/pytest_ecc.py | 12 --- .../hal/test_apps/ecc/sdkconfig.defaults | 2 - components/hal/test_apps/security/README.md | 15 ++++ .../test_apps/security/main/CMakeLists.txt | 4 + .../hal/test_apps/security/main/app_main.c | 6 ++ .../main/ecc/ecc_params.h} | 0 .../main => security/main/ecc}/test_ecc.c | 90 ++++++++++++++++--- 12 files changed, 102 insertions(+), 105 deletions(-) delete mode 100644 components/hal/.build-test-rules.yml delete mode 100644 components/hal/test_apps/ecc/CMakeLists.txt delete mode 100644 components/hal/test_apps/ecc/README.md delete mode 100644 components/hal/test_apps/ecc/main/CMakeLists.txt delete mode 100644 components/hal/test_apps/ecc/main/app_main.c delete mode 100644 components/hal/test_apps/ecc/pytest_ecc.py delete mode 100644 components/hal/test_apps/ecc/sdkconfig.defaults rename components/hal/test_apps/{ecc/main/test_params.h => security/main/ecc/ecc_params.h} (100%) rename components/hal/test_apps/{ecc/main => security/main/ecc}/test_ecc.c (81%) diff --git a/components/hal/.build-test-rules.yml b/components/hal/.build-test-rules.yml deleted file mode 100644 index 4e02657514..0000000000 --- a/components/hal/.build-test-rules.yml +++ /dev/null @@ -1,9 +0,0 @@ -# Documentation: .gitlab/ci/README.md#manifest-file-to-control-the-buildtest-apps - -components/hal/test_apps/ecc: - disable: - - if: SOC_ECC_SUPPORTED != 1 - disable_test: - - if: IDF_TARGET == "esp32c2" - temporary: true - reason: C2 ECC peripheral has a bug in ECC point verification, if value of K is zero the verification fails diff --git a/components/hal/test_apps/ecc/CMakeLists.txt b/components/hal/test_apps/ecc/CMakeLists.txt deleted file mode 100644 index 5ca38f39c4..0000000000 --- a/components/hal/test_apps/ecc/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -cmake_minimum_required(VERSION 3.16) - -set(COMPONENTS main) - -include($ENV{IDF_PATH}/tools/cmake/project.cmake) - -project(ecc_test) diff --git a/components/hal/test_apps/ecc/README.md b/components/hal/test_apps/ecc/README.md deleted file mode 100644 index 51c6228f80..0000000000 --- a/components/hal/test_apps/ecc/README.md +++ /dev/null @@ -1,43 +0,0 @@ -| Supported Targets | ESP32-C2 | ESP32-C6 | ESP32-H2 | -| ----------------- | -------- | -------- | -------- | - -## ECC peripheral test - -This application contains basic test cases for the ECC peripheral without using any OS functionality or higher abstraction layer. - -This contains tests for the following features of ECC peripheral: - -- ECC Point multiplication for P192 and P256 curve -- ECC Point verification for P192 and P256 curve -- ECC Point verify and multiply for P192 and P256 curve -- ECC Inverse multiplication for P192 and P256 - -If the hardware supports extended work modes then it also tests: -- ECC Jacobian multiplication for P192 and P256 curve -- ECC Jacobian verification for P192 and P256 curve -- ECC Point verification and Jacobian multiplication for P192 and P256 curve -- ECC Point addition for P192 and P256 curve -- Mod addition -- Mod subtraction -- Mod multiplication - -# Building - -```bash -idf.py set-target -idf.py build -``` - -# Running the app manually - -```bash -idf.py flash monitor -``` - -Enter the test that you want to run locally - -# Running tests - -```bash -pytest --target -``` diff --git a/components/hal/test_apps/ecc/main/CMakeLists.txt b/components/hal/test_apps/ecc/main/CMakeLists.txt deleted file mode 100644 index 668d5be1ed..0000000000 --- a/components/hal/test_apps/ecc/main/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -set(srcs "app_main.c" - "test_ecc.c") - -idf_component_register(SRCS ${srcs} - REQUIRES unity - WHOLE_ARCHIVE) diff --git a/components/hal/test_apps/ecc/main/app_main.c b/components/hal/test_apps/ecc/main/app_main.c deleted file mode 100644 index ecde0a6c5d..0000000000 --- a/components/hal/test_apps/ecc/main/app_main.c +++ /dev/null @@ -1,13 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Unlicense OR CC0-1.0 - */ - -#include "unity.h" -#include "unity_test_runner.h" - -void app_main(void) -{ - unity_run_menu(); -} diff --git a/components/hal/test_apps/ecc/pytest_ecc.py b/components/hal/test_apps/ecc/pytest_ecc.py deleted file mode 100644 index 4210c9aa88..0000000000 --- a/components/hal/test_apps/ecc/pytest_ecc.py +++ /dev/null @@ -1,12 +0,0 @@ -# SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD -# SPDX-License-Identifier: CC0-1.0 - -import pytest -from pytest_embedded import Dut - - -@pytest.mark.esp32c6 -@pytest.mark.esp32h2 -@pytest.mark.generic -def test_ecc(dut: Dut) -> None: - dut.run_all_single_board_cases() diff --git a/components/hal/test_apps/ecc/sdkconfig.defaults b/components/hal/test_apps/ecc/sdkconfig.defaults deleted file mode 100644 index a4ba403514..0000000000 --- a/components/hal/test_apps/ecc/sdkconfig.defaults +++ /dev/null @@ -1,2 +0,0 @@ -CONFIG_ESP_TASK_WDT_EN=y -CONFIG_ESP_TASK_WDT_INIT=n diff --git a/components/hal/test_apps/security/README.md b/components/hal/test_apps/security/README.md index 32319a6e43..f1caaac31c 100644 --- a/components/hal/test_apps/security/README.md +++ b/components/hal/test_apps/security/README.md @@ -12,6 +12,21 @@ This contains tests for the following features of the security peripherals: - MPI Multiplication - MPI Modular Exponentiation +- ECC peripheral + - ECC Point multiplication for P192 and P256 curve + - ECC Point verification for P192 and P256 curve + - ECC Point verify and multiply for P192 and P256 curve + - ECC Inverse multiplication for P192 and P256 + + If the hardware supports extended work modes then it also tests: + - ECC Jacobian multiplication for P192 and P256 curve + - ECC Jacobian verification for P192 and P256 curve + - ECC Point verification and Jacobian multiplication for P192 and P256 curve + - ECC Point addition for P192 and P256 curve + - Mod addition + - Mod subtraction + - Mod multiplication + # Building ```bash diff --git a/components/hal/test_apps/security/main/CMakeLists.txt b/components/hal/test_apps/security/main/CMakeLists.txt index ba1f2bd743..7574a4447b 100644 --- a/components/hal/test_apps/security/main/CMakeLists.txt +++ b/components/hal/test_apps/security/main/CMakeLists.txt @@ -4,6 +4,10 @@ if(CONFIG_SOC_MPI_SUPPORTED) list(APPEND srcs "mpi/test_mpi.c") endif() +if(CONFIG_SOC_ECC_SUPPORTED) + list(APPEND srcs "ecc/test_ecc.c") +endif() + idf_component_register(SRCS ${srcs} REQUIRES test_utils unity diff --git a/components/hal/test_apps/security/main/app_main.c b/components/hal/test_apps/security/main/app_main.c index 1b66c356b0..c42bb7c0c5 100644 --- a/components/hal/test_apps/security/main/app_main.c +++ b/components/hal/test_apps/security/main/app_main.c @@ -9,7 +9,13 @@ static void run_all_tests(void) { +#if CONFIG_SOC_MPI_SUPPORTED RUN_TEST_GROUP(mpi); +#endif + +#if CONFIG_SOC_ECC_SUPPORTED + RUN_TEST_GROUP(ecc); +#endif } void app_main(void) diff --git a/components/hal/test_apps/ecc/main/test_params.h b/components/hal/test_apps/security/main/ecc/ecc_params.h similarity index 100% rename from components/hal/test_apps/ecc/main/test_params.h rename to components/hal/test_apps/security/main/ecc/ecc_params.h diff --git a/components/hal/test_apps/ecc/main/test_ecc.c b/components/hal/test_apps/security/main/ecc/test_ecc.c similarity index 81% rename from components/hal/test_apps/ecc/main/test_ecc.c rename to components/hal/test_apps/security/main/ecc/test_ecc.c index a7ee73abe3..605bd77e50 100644 --- a/components/hal/test_apps/ecc/main/test_ecc.c +++ b/components/hal/test_apps/security/main/ecc/test_ecc.c @@ -9,12 +9,13 @@ #include #include "sdkconfig.h" #include "esp_log.h" -#include "test_params.h" +#include "ecc_params.h" #include "soc/soc_caps.h" #include "hal/ecc_hal.h" #include "hal/clk_gate_ll.h" -#include "unity.h" +#include "memory_checks.h" +#include "unity_fixture.h" #define _DEBUG_ 0 #define SOC_ECC_SUPPORT_POINT_MULT 1 @@ -45,6 +46,21 @@ static void ecc_enable_and_reset(void) periph_ll_enable_clk_clear_rst(PERIPH_ECC_MODULE); } + +TEST_GROUP(ecc); + +TEST_SETUP(ecc) +{ + test_utils_record_free_mem(); + TEST_ESP_OK(test_utils_set_leak_level(0, ESP_LEAK_TYPE_CRITICAL, ESP_COMP_LEAK_GENERAL)); +} + +TEST_TEAR_DOWN(ecc) +{ + test_utils_finish_and_evaluate_leaks(test_utils_get_leak_level(ESP_LEAK_TYPE_WARNING, ESP_COMP_LEAK_ALL), + test_utils_get_leak_level(ESP_LEAK_TYPE_CRITICAL, ESP_COMP_LEAK_ALL)); +} + #if SOC_ECC_SUPPORT_POINT_MULT static void ecc_point_mul(const uint8_t *k_le, const uint8_t *x_le, const uint8_t *y_le, uint8_t len, bool verify_first, uint8_t *res_x_le, uint8_t *res_y_le) @@ -126,7 +142,7 @@ static void test_ecc_point_mul_inner(bool verify_first) TEST_ASSERT_EQUAL_MEMORY_MESSAGE(y_mul_le, y_res_le, 24, "Y coordinate of P192 point multiplication "); } -TEST_CASE("ECC point multiplication on SECP192R1 and SECP256R1", "[ecc][hal]") +TEST(ecc, ecc_point_multiplication_on_SECP192R1_and_SECP256R1) { test_ecc_point_mul_inner(false); } @@ -148,7 +164,7 @@ static int ecc_point_verify(const uint8_t *x_le, const uint8_t *y_le, uint8_t le return ecc_hal_read_verify_result(); } -TEST_CASE("ECC point verification on SECP192R1 and SECP256R1", "[ecc][hal]") +TEST(ecc, ecc_point_verification_on_SECP192R1_and_SECP256R1) { int res; uint8_t x_le[32]; @@ -183,7 +199,7 @@ TEST_CASE("ECC point verification on SECP192R1 and SECP256R1", "[ecc][hal]") #endif #if SOC_ECC_SUPPORT_POINT_MULT && SOC_ECC_SUPPORT_POINT_VERIFY && !defined(SOC_ECC_SUPPORT_POINT_VERIFY_QUIRK) -TEST_CASE("ECC point verification and multiplication on SECP192R1 and SECP256R1", "[ecc][hal]") +TEST(ecc, ecc_point_verification_and_multiplication_on_SECP192R1_and_SECP256R1) { test_ecc_point_mul_inner(true); } @@ -208,7 +224,7 @@ static void ecc_point_inv_mul(const uint8_t *num_le, const uint8_t *deno_le, uin ecc_hal_read_mul_result(zero, res_le, len); } -TEST_CASE("ECC inverse multiplication (or mod division) using SECP192R1 and SECP256R1 order of curve", "[ecc][hal]") +TEST(ecc, ecc_inverse_multiplication_or_mod_division_using_SECP192R1_and_SECP256R1_order_of_curve) { uint8_t res[32] = {0}; ecc_point_inv_mul(ecc256_num, ecc256_den, 32, res); @@ -277,7 +293,7 @@ static void test_ecc_jacob_mul_inner(bool verify_first) TEST_ASSERT_EQUAL_MEMORY_MESSAGE(ecc_p192_jacob_mul_res_z_le, z_res_le, 24, "Z coordinate of P192 point jacobian multiplication "); } -TEST_CASE("ECC jacobian point multiplication on SECP192R1 and SECP256R1", "[ecc][hal]") +TEST(ecc, ecc_jacobian_point_multiplication_on_SECP192R1_and_SECP256R1) { test_ecc_jacob_mul_inner(false); } @@ -301,7 +317,7 @@ static int ecc_jacob_verify(const uint8_t *x_le, const uint8_t *y_le, const uint return ecc_hal_read_verify_result(); } -TEST_CASE("ECC jacobian point verification on SECP192R1 and SECP256R1", "[ecc][hal]") +TEST(ecc, ecc_jacobian_point_verification_on_SECP192R1_and_SECP256R1) { int res; /* P256 */ @@ -315,7 +331,7 @@ TEST_CASE("ECC jacobian point verification on SECP192R1 and SECP256R1", "[ecc][h #endif #if SOC_ECC_SUPPORT_JACOB_POINT_MULT && SOC_ECC_SUPPORT_JACOB_POINT_VERIFY -TEST_CASE("ECC point verification and Jacobian point multiplication on SECP192R1 and SECP256R1", "[ecc][hal]") +TEST(ecc, ecc_point_verification_and_jacobian_point_multiplication_on_SECP192R1_and_SECP256R1) { test_ecc_jacob_mul_inner(true); } @@ -341,7 +357,7 @@ static void ecc_point_addition(uint8_t *px_le, uint8_t *py_le, uint8_t *qx_le, u ecc_hal_read_point_add_result(x_res_le, y_res_le, z_res_le, len, jacob_output); } -TEST_CASE("ECC point addition on SECP192R1 and SECP256R1", "[ecc][hal]") +TEST(ecc, ecc_point_addition_on_SECP192R1_and_SECP256R1) { uint8_t scalar_le[32] = {0}; uint8_t x_le[32] = {0}; @@ -414,7 +430,7 @@ static void ecc_mod_op(ecc_mode_t mode, const uint8_t *a, const uint8_t *b, uint #endif #if SOC_ECC_SUPPORT_MOD_ADD -TEST_CASE("ECC mod addition using SECP192R1 and SECP256R1 order of curve", "[ecc][hal]") +TEST(ecc, ecc_mod_addition_using_SECP192R1_and_SECP256R1_order_of_curve) { uint8_t res[32] = {0}; ecc_mod_op(ECC_MODE_MOD_ADD, ecc256_x, ecc256_y, 32, res); @@ -426,7 +442,7 @@ TEST_CASE("ECC mod addition using SECP192R1 and SECP256R1 order of curve", "[ecc #endif #if SOC_ECC_SUPPORT_MOD_SUB -TEST_CASE("ECC mod subtraction using SECP192R1 and SECP256R1 order of curve", "[ecc][hal]") +TEST(ecc, ecc_mod_subtraction_using_SECP192R1_and_SECP256R1_order_of_curve) { uint8_t res[32] = {0}; ecc_mod_op(ECC_MODE_MOD_SUB, ecc256_x, ecc256_y, 32, res); @@ -438,7 +454,7 @@ TEST_CASE("ECC mod subtraction using SECP192R1 and SECP256R1 order of curve", "[ #endif #if SOC_ECC_SUPPORT_MOD_MUL -TEST_CASE("ECC mod multiplication using SECP192R1 and SECP256R1 order of curve", "[ecc][hal]") +TEST(ecc, ecc_mod_multiplication_using_SECP192R1_and_SECP256R1_order_of_curve) { uint8_t res[32] = {0}; ecc_mod_op(ECC_MODE_MOD_MUL, ecc256_x, ecc256_y, 32, res); @@ -448,3 +464,51 @@ TEST_CASE("ECC mod multiplication using SECP192R1 and SECP256R1 order of curve", TEST_ASSERT_EQUAL_MEMORY_MESSAGE(ecc192_mul_res, res, 24, "P192 mod multiplication"); } #endif + +TEST_GROUP_RUNNER(ecc) +{ +#if SOC_ECC_SUPPORT_POINT_MULT + RUN_TEST_CASE(ecc, ecc_point_multiplication_on_SECP192R1_and_SECP256R1); +#endif + +#if SOC_ECC_SUPPORT_POINT_VERIFY && !defined(SOC_ECC_SUPPORT_POINT_VERIFY_QUIRK) + RUN_TEST_CASE(ecc, ecc_point_verification_on_SECP192R1_and_SECP256R1); +#endif + +#if SOC_ECC_SUPPORT_POINT_MULT && SOC_ECC_SUPPORT_POINT_VERIFY && !defined(SOC_ECC_SUPPORT_POINT_VERIFY_QUIRK) + RUN_TEST_CASE(ecc, ecc_point_verification_and_multiplication_on_SECP192R1_and_SECP256R1); +#endif + +#if SOC_ECC_SUPPORT_POINT_DIVISION + RUN_TEST_CASE(ecc, ecc_inverse_multiplication_or_mod_division_using_SECP192R1_and_SECP256R1_order_of_curve); +#endif + +#if SOC_ECC_SUPPORT_JACOB_POINT_MULT + RUN_TEST_CASE(ecc, ecc_jacobian_point_multiplication_on_SECP192R1_and_SECP256R1); +#endif + +#if SOC_ECC_SUPPORT_JACOB_POINT_VERIFY + RUN_TEST_CASE(ecc, ecc_jacobian_point_verification_on_SECP192R1_and_SECP256R1); +#endif + +#if SOC_ECC_SUPPORT_JACOB_POINT_MULT && SOC_ECC_SUPPORT_JACOB_POINT_VERIFY + RUN_TEST_CASE(ecc, ecc_point_verification_and_jacobian_point_multiplication_on_SECP192R1_and_SECP256R1); +#endif + +#if SOC_ECC_SUPPORT_POINT_ADDITION + RUN_TEST_CASE(ecc, ecc_point_addition_on_SECP192R1_and_SECP256R1); +#endif + +#if SOC_ECC_SUPPORT_MOD_ADD + RUN_TEST_CASE(ecc, ecc_mod_addition_using_SECP192R1_and_SECP256R1_order_of_curve); +#endif + +#if SOC_ECC_SUPPORT_MOD_SUB + RUN_TEST_CASE(ecc, ecc_mod_subtraction_using_SECP192R1_and_SECP256R1_order_of_curve); +#endif + +#if SOC_ECC_SUPPORT_MOD_MUL + RUN_TEST_CASE(ecc, ecc_mod_multiplication_using_SECP192R1_and_SECP256R1_order_of_curve); +#endif + +}