diff --git a/components/hal/test_apps/crypto/README.md b/components/hal/test_apps/crypto/README.md index 789f9ada71..e405032663 100644 --- a/components/hal/test_apps/crypto/README.md +++ b/components/hal/test_apps/crypto/README.md @@ -41,6 +41,12 @@ This contains tests for the following features of the crypto peripherals: - Digital Signature Blocking Operation - Digital Signature Invalid Data +- ECDSA peripheral + - ECDSA P192 signature generation + - ECDSA P256 signature generation + - ECDSA P192 signature verification + - ECDSA P256 signature verification + > **_NOTE:_** The verification tests for the HMAC and Digital Signature peripherals would get exercised in only in an FPGA environment. # Burning the HMAC key @@ -64,6 +70,16 @@ espefuse.py -p $ESPPORT burn_key BLOCK_KEY2 ds_key2.bin HMAC_DOWN_DIGITAL_SIGNAT espefuse.py -p $ESPPORT burn_key BLOCK_KEY3 ds_key3.bin HMAC_DOWN_DIGITAL_SIGNATURE --no-read-protect --no-write-protect --do-not-confirm ``` +# Burning the ECDSA keys + +The ECDSA tests need some ECDSA keys to be burned in the `BLOCK_KEY1` and `BLOCK_KEY2` of the efuses. As this verification application is independent of the efuse component, the user needs to manually burn the keys and their key purposes using `espefuse.py`. + +```bash +espefuse.py -p $ESPPORT burn_key BLOCK_KEY1 ecdsa192_priv_key.pem ECDSA_KEY --no-read-protect --no-write-protect --do-not-confirm + +espefuse.py -p $ESPPORT burn_key BLOCK_KEY2 ecdsa256_priv_key.pem ECDSA_KEY --no-read-protect --no-write-protect --do-not-confirm +``` + # Building ```bash diff --git a/components/hal/test_apps/crypto/main/CMakeLists.txt b/components/hal/test_apps/crypto/main/CMakeLists.txt index 0138accf63..fe81012989 100644 --- a/components/hal/test_apps/crypto/main/CMakeLists.txt +++ b/components/hal/test_apps/crypto/main/CMakeLists.txt @@ -16,6 +16,10 @@ if(CONFIG_SOC_DIG_SIGN_SUPPORTED) list(APPEND srcs "ds/test_ds.c") endif() +if(CONFIG_SOC_ECDSA_SUPPORTED) + list(APPEND srcs "ecdsa/test_ecdsa.c") +endif() + idf_component_register(SRCS ${srcs} REQUIRES test_utils unity WHOLE_ARCHIVE) diff --git a/components/hal/test_apps/crypto/main/app_main.c b/components/hal/test_apps/crypto/main/app_main.c index df7d535588..7779b0db2f 100644 --- a/components/hal/test_apps/crypto/main/app_main.c +++ b/components/hal/test_apps/crypto/main/app_main.c @@ -27,6 +27,10 @@ static void run_all_tests(void) RUN_TEST_GROUP(ds); #endif +#if CONFIG_SOC_ECDSA_SUPPORTED + RUN_TEST_GROUP(ecdsa) +#endif + #endif /* CONFIG_IDF_ENV_FPGA */ } diff --git a/components/hal/test_apps/crypto/main/ecdsa/ecdsa192_priv_key.pem b/components/hal/test_apps/crypto/main/ecdsa/ecdsa192_priv_key.pem new file mode 100644 index 0000000000..7062ad62ff --- /dev/null +++ b/components/hal/test_apps/crypto/main/ecdsa/ecdsa192_priv_key.pem @@ -0,0 +1,5 @@ +-----BEGIN EC PRIVATE KEY----- +MF8CAQEEGPswTe6IsRRpS/xbXdWj+HbzKV79UPNuFqAKBggqhkjOPQMBAaE0AzIA +BNA/b+ddqvTAHmN7gqsjMzR0WVZdIRCcsYX8dstlvMS+dAn983Tcwt5+SyOtRlyH +wg== +-----END EC PRIVATE KEY----- diff --git a/components/hal/test_apps/crypto/main/ecdsa/ecdsa256_priv_key.pem b/components/hal/test_apps/crypto/main/ecdsa/ecdsa256_priv_key.pem new file mode 100644 index 0000000000..7e1a876895 --- /dev/null +++ b/components/hal/test_apps/crypto/main/ecdsa/ecdsa256_priv_key.pem @@ -0,0 +1,5 @@ +-----BEGIN EC PRIVATE KEY----- +MHcCAQEEIKLY3NrF5q4bw/TqTE0a4MGI70bitMzz2AoLIIcMeHi8oAoGCCqGSM49 +AwEHoUQDQgAETd9kH9hu1IunygTHEbhF2gz/X3rOWhH5lVUIJoWI6agksE3Mv86a +bqCKthpA8nFqUKj9qiyAoLxzoejsrywlNA== +-----END EC PRIVATE KEY----- diff --git a/components/hal/test_apps/crypto/main/ecdsa/ecdsa_params.h b/components/hal/test_apps/crypto/main/ecdsa/ecdsa_params.h new file mode 100644 index 0000000000..17f18c64b1 --- /dev/null +++ b/components/hal/test_apps/crypto/main/ecdsa/ecdsa_params.h @@ -0,0 +1,88 @@ +/* + * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + */ +#pragma once + +#include + +const uint8_t ecc_p256_k[] = { + 0xB2, 0xC5, 0x9E, 0x92, 0x64, 0xCD, 0x5F, 0x66, + 0x9E, 0xC8, 0x83, 0x6D, 0x99, 0x61, 0x18, 0x72, + 0xC8, 0x60, 0x83, 0x1E, 0xE5, 0x79, 0xCC, 0x73, + 0xA9, 0xB4, 0x74, 0x85, 0x70, 0x11, 0x2D, 0xA2, +}; + +const uint8_t ecc_p192_k[] = { + 0x6f, 0x18, 0x34, 0xeb, 0x16, 0xb7, 0xac, 0x9f, + 0x3c, 0x77, 0x71, 0xb3, 0x02, 0x30, 0x70, 0x48, + 0x75, 0x87, 0xbb, 0x6f, 0x80, 0x34, 0x8d, 0x5e +}; + +uint8_t sha[] = { + 0x0c, 0xaa, 0x08, 0xb4, 0xf0, 0x89, 0xd3, 0x45, + 0xbb, 0x55, 0x98, 0xd9, 0xc2, 0xe9, 0x65, 0x5d, + 0x7e, 0xa3, 0xa9, 0xc3, 0xcd, 0x69, 0xb1, 0xcf, + 0x91, 0xbe, 0x58, 0x10, 0xfe, 0x80, 0x65, 0x6e +}; + +/* Little endian */ +uint8_t ecdsa256_r[] = { + 0xff, 0x94, 0xf7, 0x5a, 0xce, 0x81, 0x05, 0xfc, + 0x98, 0x17, 0xd5, 0x0a, 0x94, 0x53, 0xab, 0x54, + 0xa8, 0x21, 0x8b, 0xe6, 0xf2, 0x6e, 0xa0, 0x7b, + 0x20, 0x36, 0x27, 0xd2, 0x60, 0x5d, 0xa3, 0x2e +}; + +/* Little endian */ +uint8_t ecdsa256_s[] = { + 0x50, 0x78, 0x0e, 0x41, 0xe3, 0x03, 0xd2, 0x5b, + 0xaf, 0x74, 0x31, 0xb8, 0x74, 0xc9, 0xb3, 0x89, + 0x8d, 0xb5, 0x40, 0x3e, 0x5b, 0x2a, 0x4b, 0xe2, + 0x5e, 0xf8, 0x96, 0xd4, 0xf9, 0x22, 0xf9, 0xb4 +}; + +/* Little endian */ +uint8_t ecdsa256_pub_x[] = { + 0xa8, 0xe9, 0x88, 0x85, 0x26, 0x08, 0x55, 0x95, + 0xf9, 0x11, 0x5a, 0xce, 0x7a, 0x5f, 0xff, 0x0c, + 0xda, 0x45, 0xb8, 0x11, 0xc7, 0x04, 0xca, 0xa7, + 0x8b, 0xd4, 0x6e, 0xd8, 0x1f, 0x64, 0xdf, 0x4d +}; + +/* Little endian */ +uint8_t ecdsa256_pub_y[] = { + 0x34, 0x25, 0x2c, 0xaf, 0xec, 0xe8, 0xa1, 0x73, + 0xbc, 0xa0, 0x80, 0x2c, 0xaa, 0xfd, 0xa8, 0x50, + 0x6a, 0x71, 0xf2, 0x40, 0x1a, 0xb6, 0x8a, 0xa0, + 0x6e, 0x9a, 0xce, 0xbf, 0xcc, 0x4d, 0xb0, 0x24 +}; + +/* Little endian */ +uint8_t ecdsa192_r[] = { + 0x9f, 0xee, 0xb7, 0x4f, 0x09, 0xd5, 0xc8, 0x42, + 0x2c, 0x74, 0xe7, 0xaa, 0x6d, 0xe2, 0xe1, 0x1c, + 0xb7, 0x26, 0x75, 0xb2, 0x2f, 0x18, 0x8a, 0x2b +}; + +/* Little endian */ +uint8_t ecdsa192_s[] = { + 0x12, 0x5b, 0x30, 0x24, 0x59, 0x24, 0xeb, 0xf6, + 0x2f, 0x06, 0x60, 0xa8, 0xff, 0xa5, 0xed, 0xce, + 0xb6, 0xa5, 0x28, 0xf4, 0x05, 0xb4, 0x74, 0x1a +}; + +/* Little endian */ +uint8_t ecdsa192_pub_x[] = { + 0xb1, 0x9c, 0x10, 0x21, 0x5d, 0x56, 0x59, 0x74, + 0x34, 0x33, 0x23, 0xab, 0x82, 0x7b, 0x63, 0x1e, + 0xc0, 0xf4, 0xaa, 0x5d, 0xe7, 0x6f, 0x3f, 0xd0 +}; + +/* Little endian */ +uint8_t ecdsa192_pub_y[] = { + 0xc2, 0x87, 0x5c, 0x46, 0xad, 0x23, 0x4b, 0x7e, + 0xde, 0xc2, 0xdc, 0x74, 0xf3, 0xfd, 0x09, 0x74, + 0xbe, 0xc4, 0xbc, 0x65, 0xcb, 0x76, 0xfc, 0x85 +}; diff --git a/components/hal/test_apps/crypto/main/ecdsa/test_ecdsa.c b/components/hal/test_apps/crypto/main/ecdsa/test_ecdsa.c new file mode 100644 index 0000000000..fef0354f66 --- /dev/null +++ b/components/hal/test_apps/crypto/main/ecdsa/test_ecdsa.c @@ -0,0 +1,210 @@ +/* + * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: CC0-1.0 + */ + +#include +#include +#include + +#include "esp_private/periph_ctrl.h" +#include "esp_random.h" +#include "hal/clk_gate_ll.h" +#include "hal/ecdsa_hal.h" +#include "hal/ecdsa_types.h" + +#include "memory_checks.h" +#include "unity_fixture.h" + +#include "ecdsa_params.h" + + +static void ecdsa_enable_and_reset(void) +{ + periph_ll_enable_clk_clear_rst(PERIPH_ECDSA_MODULE); +} + +static void ecdsa_disable_and_reset(void) +{ + periph_ll_disable_clk_set_rst(PERIPH_ECDSA_MODULE); +} + +static void ecc_be_to_le(const uint8_t* be_point, uint8_t *le_point, uint8_t len) +{ + memset(le_point, 0x0, 32); + for (int i = 0; i < len; i++) { + le_point[i] = be_point[len - i - 1]; + } +} + +static int test_ecdsa_verify(bool is_p256, uint8_t* sha, uint8_t* r_le, uint8_t* s_le, uint8_t *pub_x, uint8_t *pub_y) +{ + uint16_t len; + uint8_t sha_le[32]; + + ecdsa_hal_config_t conf = { + .mode = ECDSA_MODE_SIGN_VERIFY, + .k_mode = ECDSA_K_USE_TRNG, + .sha_mode = ECDSA_Z_USER_PROVIDED, + }; + + if (is_p256) { + conf.curve = ECDSA_CURVE_SECP256R1; + len = 32; + } else { + conf.curve = ECDSA_CURVE_SECP192R1; + len = 24; + } + + /* Set HASH */ + ecc_be_to_le(sha, sha_le, len); + + ecdsa_enable_and_reset(); + int ret = ecdsa_hal_verify_signature(&conf, sha_le, r_le, s_le, pub_x, pub_y, len); + ecdsa_disable_and_reset(); + return ret; +} + +static void test_ecdsa_corrupt_data(bool is_p256, uint8_t* sha, uint8_t* r_le, uint8_t* s_le, uint8_t *pub_x, uint8_t *pub_y) +{ + int len; + + if (is_p256) { + len = 32; + } else { + len = 24; + } + + // Randomly select a bit and corrupt its correpsonding value + uint16_t r_bit = esp_random() % len * 8; + + printf("Corrupting SHA bit %d...\n", r_bit); + sha[r_bit / 8] ^= 1 << (r_bit % 8); + TEST_ASSERT_EQUAL(-1, test_ecdsa_verify(1, sha, r_le, s_le, pub_x, pub_y)); + sha[r_bit / 8] ^= 1 << (r_bit % 8); + + printf("Corrupting R bit %d...\n", r_bit); + r_le[r_bit / 8] ^= 1 << (r_bit % 8); + TEST_ASSERT_EQUAL(-1, test_ecdsa_verify(1, sha, r_le, s_le, pub_x, pub_y)); + r_le[r_bit / 8] ^= 1 << (r_bit % 8); + + printf("Corrupting S bit %d...\n", r_bit); + s_le[r_bit / 8] ^= 1 << (r_bit % 8); + TEST_ASSERT_EQUAL(-1, test_ecdsa_verify(1, sha, r_le, s_le, pub_x, pub_y)); + s_le[r_bit / 8] ^= 1 << (r_bit % 8); + + printf("Corrupting pub_x bit %d...\n", r_bit); + pub_x[r_bit / 8] ^= 1 << (r_bit % 8); + TEST_ASSERT_EQUAL(-1, test_ecdsa_verify(1, sha, r_le, s_le, pub_x, pub_y)); + pub_x[r_bit / 8] ^= 1 << (r_bit % 8); + + printf("Corrupting pub_y bit %d...\n", r_bit); + pub_y[r_bit / 8] ^= 1 << (r_bit % 8); + TEST_ASSERT_EQUAL(-1, test_ecdsa_verify(1, sha, r_le, s_le, pub_x, pub_y)); + pub_y[r_bit / 8] ^= 1 << (r_bit % 8); + +} + +static void test_ecdsa_sign(bool is_p256, uint8_t* sha, uint8_t* r_le, uint8_t* s_le) +{ + uint8_t sha_le[32] = {0}; + uint8_t zeroes[32] = {0}; + uint16_t len; + + ecdsa_hal_config_t conf = { + .mode = ECDSA_MODE_SIGN_GEN, + .k_mode = ECDSA_K_USE_TRNG, + .sha_mode = ECDSA_Z_USER_PROVIDED, + }; + + if (is_p256) { + conf.curve = ECDSA_CURVE_SECP256R1; + conf.efuse_key_blk = 5; + len = 32; + } else { + conf.curve = ECDSA_CURVE_SECP192R1; + conf.efuse_key_blk = 6; + len = 24; + } + + /* Set HASH */ + ecc_be_to_le(sha, sha_le, len); + + ecdsa_enable_and_reset(); + + do { + ecdsa_hal_gen_signature(&conf, NULL, sha_le, r_le, s_le, len); + } while(!memcmp(r_le, zeroes, len) || !memcmp(s_le, zeroes, len)); + + ecdsa_disable_and_reset(); +} + +static void test_ecdsa_sign_and_verify(bool is_p256, uint8_t* sha, uint8_t* pub_x, uint8_t* pub_y) +{ + uint8_t r_le[32] = {0}; + uint8_t s_le[32] = {0}; + + test_ecdsa_sign(is_p256, sha, r_le, s_le); + + TEST_ASSERT_EQUAL(0, test_ecdsa_verify(is_p256, sha, r_le, s_le, pub_x, pub_y)); +} + +TEST_GROUP(ecdsa); + +TEST_SETUP(ecdsa) +{ + 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(ecdsa) +{ + 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)); +} + +TEST(ecdsa, ecdsa_SECP192R1_signature_verification) +{ + TEST_ASSERT_EQUAL(0, test_ecdsa_verify(0, sha, ecdsa192_r, ecdsa192_s, ecdsa192_pub_x, ecdsa192_pub_y)); +} + + +TEST(ecdsa, ecdsa_SECP192R1_sign_and_verify) +{ + test_ecdsa_sign_and_verify(0, sha, ecdsa192_pub_x, ecdsa192_pub_y); +} + + +TEST(ecdsa, ecdsa_SECP192R1_corrupt_signature) +{ + test_ecdsa_corrupt_data(0, sha, ecdsa192_r, ecdsa192_s, ecdsa192_pub_x, ecdsa192_pub_y); +} + + +TEST(ecdsa, ecdsa_SECP256R1_signature_verification) +{ + TEST_ASSERT_EQUAL(0, test_ecdsa_verify(1, sha, ecdsa256_r, ecdsa256_s, ecdsa256_pub_x, ecdsa256_pub_y)); +} + + +TEST(ecdsa, ecdsa_SECP256R1_sign_and_verify) +{ + test_ecdsa_sign_and_verify(1, sha, ecdsa256_pub_x, ecdsa256_pub_y); +} + + +TEST(ecdsa, ecdsa_SECP256R1_corrupt_signature) +{ + test_ecdsa_corrupt_data(1, sha, ecdsa256_r, ecdsa256_s, ecdsa256_pub_x, ecdsa256_pub_y); +} + +TEST_GROUP_RUNNER(ecdsa) +{ + RUN_TEST_CASE(ecdsa, ecdsa_SECP192R1_signature_verification) + RUN_TEST_CASE(ecdsa, ecdsa_SECP192R1_sign_and_verify) + RUN_TEST_CASE(ecdsa, ecdsa_SECP192R1_corrupt_signature) + RUN_TEST_CASE(ecdsa, ecdsa_SECP256R1_signature_verification) + RUN_TEST_CASE(ecdsa, ecdsa_SECP256R1_sign_and_verify) + RUN_TEST_CASE(ecdsa, ecdsa_SECP256R1_corrupt_signature) +}