mirror of
https://github.com/espressif/esp-idf.git
synced 2026-10-02 03:00:34 +03:00
feat(mbedtls): migrates ESP-TEE with PSA APIs
This commit is contained in:
@@ -230,7 +230,7 @@ static void init_ota_sem(void)
|
||||
static int create_ota_task(const char *url, const char *task_name, void (*ota_task)(void *))
|
||||
{
|
||||
init_ota_sem();
|
||||
if (xTaskCreate(ota_task, task_name, configMINIMAL_STACK_SIZE * 3, (void *)url, 5, NULL) != pdPASS) {
|
||||
if (xTaskCreate(ota_task, task_name, configMINIMAL_STACK_SIZE * 4, (void *)url, 5, NULL) != pdPASS) {
|
||||
ESP_LOGE(TAG, "Task creation failed for %s", task_name);
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
@@ -14,9 +14,11 @@
|
||||
#include "esp_console.h"
|
||||
#include "argtable3/argtable3.h"
|
||||
|
||||
#include "mbedtls/ecp.h"
|
||||
#include "mbedtls/ecdsa.h"
|
||||
#include "mbedtls/sha256.h"
|
||||
#define MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS
|
||||
// #include "mbedtls/ecp.h"
|
||||
// #include "mbedtls/ecdsa.h"
|
||||
// #include "mbedtls/sha256.h"
|
||||
#include "psa/crypto.h"
|
||||
|
||||
#include "esp_tee_sec_storage.h"
|
||||
#include "example_tee_srv.h"
|
||||
@@ -91,57 +93,33 @@ static esp_err_t verify_ecdsa_secp256r1_sign(const uint8_t *digest, size_t len,
|
||||
|
||||
esp_err_t err = ESP_FAIL;
|
||||
|
||||
mbedtls_mpi r, s;
|
||||
mbedtls_mpi_init(&r);
|
||||
mbedtls_mpi_init(&s);
|
||||
psa_key_id_t key_id = 0;
|
||||
psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
|
||||
psa_set_key_type(&key_attributes, PSA_KEY_TYPE_ECC_PUBLIC_KEY(PSA_ECC_FAMILY_SECP_R1));
|
||||
psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_VERIFY_HASH);
|
||||
psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDSA(PSA_ALG_SHA_256));
|
||||
|
||||
mbedtls_ecdsa_context ecdsa_context;
|
||||
mbedtls_ecdsa_init(&ecdsa_context);
|
||||
uint8_t pub_key[2 * ECDSA_SECP256R1_KEY_LEN + 1];
|
||||
pub_key[0] = 0x04;
|
||||
memcpy(pub_key + 1, pubkey->pub_x, ECDSA_SECP256R1_KEY_LEN);
|
||||
memcpy(pub_key + 1 + ECDSA_SECP256R1_KEY_LEN, pubkey->pub_y, ECDSA_SECP256R1_KEY_LEN);
|
||||
|
||||
int ret = mbedtls_ecp_group_load(&ecdsa_context.MBEDTLS_PRIVATE(grp), MBEDTLS_ECP_DP_SECP256R1);
|
||||
if (ret != 0) {
|
||||
psa_status_t status = psa_import_key(&key_attributes, pub_key, sizeof(pub_key), &key_id);
|
||||
if (status != PSA_SUCCESS) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
size_t plen = mbedtls_mpi_size(&ecdsa_context.MBEDTLS_PRIVATE(grp).P);
|
||||
|
||||
ret = mbedtls_mpi_read_binary(&r, sign->sign_r, plen);
|
||||
if (ret != 0) {
|
||||
status = psa_verify_hash(key_id, PSA_ALG_ECDSA(PSA_ALG_SHA_256), digest, len, sign->signature, sizeof(sign->signature));
|
||||
if (status != PSA_SUCCESS) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
ret = mbedtls_mpi_read_binary(&s, sign->sign_s, plen);
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
ret = mbedtls_mpi_read_binary(&ecdsa_context.MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(X), pubkey->pub_x, plen);
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
ret = mbedtls_mpi_read_binary(&ecdsa_context.MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(Y), pubkey->pub_y, plen);
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
ret = mbedtls_mpi_lset(&ecdsa_context.MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(Z), 1);
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
ret = mbedtls_ecdsa_verify(&ecdsa_context.MBEDTLS_PRIVATE(grp), digest, len, &ecdsa_context.MBEDTLS_PRIVATE(Q), &r, &s);
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
psa_destroy_key(key_id);
|
||||
psa_reset_key_attributes(&key_attributes);
|
||||
|
||||
err = ESP_OK;
|
||||
|
||||
exit:
|
||||
mbedtls_mpi_free(&r);
|
||||
mbedtls_mpi_free(&s);
|
||||
mbedtls_ecdsa_free(&ecdsa_context);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
@@ -161,8 +139,10 @@ static int get_msg_sha256(int argc, char **argv)
|
||||
const char *msg = (const char *)cmd_get_msg_sha256_args.msg->sval[0];
|
||||
|
||||
uint8_t msg_digest[SHA256_DIGEST_SZ];
|
||||
int ret = mbedtls_sha256((const unsigned char *)msg, strlen(msg), msg_digest, false);
|
||||
if (ret != 0) {
|
||||
size_t msg_len = strlen(msg);
|
||||
size_t digest_len = 0;
|
||||
psa_status_t status = psa_hash_compute(PSA_ALG_SHA_256, (const uint8_t *)msg, msg_len, msg_digest, sizeof(msg_digest), &digest_len);
|
||||
if (status != PSA_SUCCESS) {
|
||||
ESP_LOGE(TAG, "Failed to calculate message hash!");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
@@ -5,8 +5,8 @@ CONFIG_SECURE_TEE_SEC_STG_EFUSE_HMAC_KEY_ID=5
|
||||
# Reducing TEE I/DRAM sizes
|
||||
# 24KB
|
||||
CONFIG_SECURE_TEE_IRAM_SIZE=0x6000
|
||||
# 12KB
|
||||
CONFIG_SECURE_TEE_DRAM_SIZE=0x3000
|
||||
# 16KB
|
||||
CONFIG_SECURE_TEE_DRAM_SIZE=0x4000
|
||||
|
||||
# Disable TEE logs (also disable all panic logs)
|
||||
CONFIG_SECURE_TEE_DEBUG_MODE=n
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
# Reducing TEE I/DRAM sizes
|
||||
# 28KB
|
||||
CONFIG_SECURE_TEE_IRAM_SIZE=0x7000
|
||||
# 16KB
|
||||
CONFIG_SECURE_TEE_DRAM_SIZE=0x5000
|
||||
|
||||
# TEE Secure Storage: Release mode
|
||||
CONFIG_SECURE_TEE_SEC_STG_MODE_RELEASE=y
|
||||
|
||||
@@ -18,4 +18,4 @@ CONFIG_SECURE_TEE_SEC_STG_EFUSE_HMAC_KEY_ID=5
|
||||
|
||||
# Increasing TEE DRAM size
|
||||
# 18KB
|
||||
CONFIG_SECURE_TEE_DRAM_SIZE=0x4800
|
||||
CONFIG_SECURE_TEE_DRAM_SIZE=0x5000
|
||||
|
||||
@@ -22,25 +22,21 @@ endif()
|
||||
set(mbedtls_test_srcs_dir "${idf_path}/components/mbedtls/test_apps/main")
|
||||
|
||||
# AES
|
||||
if(CONFIG_SOC_AES_SUPPORTED)
|
||||
list(APPEND srcs "${mbedtls_test_srcs_dir}/test_aes.c"
|
||||
"${mbedtls_test_srcs_dir}/test_aes_gcm.c"
|
||||
"${mbedtls_test_srcs_dir}/test_aes_perf.c")
|
||||
endif()
|
||||
# SHA
|
||||
if(CONFIG_SOC_SHA_SUPPORTED)
|
||||
list(APPEND srcs "${mbedtls_test_srcs_dir}/test_mbedtls_sha.c"
|
||||
"${mbedtls_test_srcs_dir}/test_sha.c"
|
||||
"${mbedtls_test_srcs_dir}/test_sha_perf.c")
|
||||
endif()
|
||||
# list(APPEND srcs "${mbedtls_test_srcs_dir}/test_aes.c"
|
||||
# "${mbedtls_test_srcs_dir}/test_aes_gcm.c"
|
||||
# "${mbedtls_test_srcs_dir}/test_aes_perf.c")
|
||||
# # SHA
|
||||
list(APPEND srcs "${mbedtls_test_srcs_dir}/test_mbedtls_sha.c"
|
||||
"${mbedtls_test_srcs_dir}/test_sha.c"
|
||||
"${mbedtls_test_srcs_dir}/test_sha_perf.c")
|
||||
|
||||
# Mixed
|
||||
if(CONFIG_SOC_AES_SUPPORTED AND CONFIG_SOC_SHA_SUPPORTED)
|
||||
list(APPEND srcs "${mbedtls_test_srcs_dir}/test_aes_sha_parallel.c")
|
||||
endif()
|
||||
# ECC
|
||||
if(CONFIG_SOC_ECC_SUPPORTED)
|
||||
list(APPEND srcs "${mbedtls_test_srcs_dir}/test_ecp.c")
|
||||
endif()
|
||||
# list(APPEND srcs "${mbedtls_test_srcs_dir}/test_ecp.c")
|
||||
|
||||
# Utility
|
||||
list(APPEND srcs "${mbedtls_test_srcs_dir}/test_apb_dport_access.c"
|
||||
"${mbedtls_test_srcs_dir}/test_mbedtls_utils.c")
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
|
||||
* SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
@@ -9,10 +9,64 @@
|
||||
#include "nvs_flash.h"
|
||||
#include "unity.h"
|
||||
#include "memory_checks.h"
|
||||
#include "psa/crypto.h"
|
||||
#if SOC_SHA_SUPPORT_PARALLEL_ENG
|
||||
#include "sha/sha_parallel_engine.h"
|
||||
#else
|
||||
#include "sha/sha_core.h"
|
||||
#endif
|
||||
#include "bignum_impl.h"
|
||||
|
||||
/* setUp runs before every test */
|
||||
void setUp(void)
|
||||
{
|
||||
#if SOC_SHA_SUPPORTED
|
||||
// Execute esp_sha operation to allocate internal SHA semaphore (in case of ESP32)
|
||||
// and initial DMA setup memory which is considered as leaked otherwise
|
||||
const uint8_t input_buffer[64] = {0};
|
||||
uint8_t output_buffer[64];
|
||||
#if SOC_SHA_SUPPORT_SHA1
|
||||
esp_sha(SHA1, input_buffer, sizeof(input_buffer), output_buffer);
|
||||
#endif // SOC_SHA_SUPPORT_SHA1
|
||||
#if SOC_SHA_SUPPORT_SHA256
|
||||
esp_sha(SHA2_256, input_buffer, sizeof(input_buffer), output_buffer);
|
||||
#endif // SOC_SHA_SUPPORT_SHA256
|
||||
#if SOC_SHA_SUPPORT_SHA512
|
||||
esp_sha(SHA2_512, input_buffer, sizeof(input_buffer), output_buffer);
|
||||
#endif // SOC_SHA_SUPPORT_SHA512
|
||||
#endif // SOC_SHA_SUPPORTED
|
||||
|
||||
#if defined(CONFIG_MBEDTLS_HARDWARE_MPI)
|
||||
esp_mpi_enable_hardware_hw_op();
|
||||
esp_mpi_disable_hardware_hw_op();
|
||||
#endif // CONFIG_MBEDTLS_HARDWARE_MPI
|
||||
|
||||
#if SOC_AES_SUPPORTED
|
||||
// Execute mbedtls_aes_init operation to allocate AES interrupt
|
||||
// allocation memory which is considered as leak otherwise
|
||||
const uint8_t plaintext[16] = {0};
|
||||
uint8_t ciphertext[32];
|
||||
const uint8_t key[16] = { 0 };
|
||||
psa_status_t status;
|
||||
psa_key_id_t key_id = 0;
|
||||
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
||||
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
|
||||
psa_set_key_algorithm(&attributes, PSA_ALG_CBC_NO_PADDING);
|
||||
psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
|
||||
psa_set_key_bits(&attributes, 128);
|
||||
status = psa_import_key(&attributes, key, sizeof(key), &key_id);
|
||||
TEST_ASSERT_EQUAL(PSA_SUCCESS, status);
|
||||
size_t output_len = 0;
|
||||
status = psa_cipher_encrypt(key_id, PSA_ALG_CBC_NO_PADDING, plaintext, sizeof(plaintext), ciphertext, sizeof(ciphertext), &output_len);
|
||||
TEST_ASSERT_EQUAL(PSA_SUCCESS, status);
|
||||
|
||||
const uint8_t plaintext_long[256] = {0};
|
||||
uint8_t ciphertext_long[272];
|
||||
output_len = 0;
|
||||
status = psa_cipher_encrypt(key_id, PSA_ALG_CBC_NO_PADDING, plaintext_long, sizeof(plaintext_long), ciphertext_long, sizeof(ciphertext_long), &output_len);
|
||||
TEST_ASSERT_EQUAL(PSA_SUCCESS, status);
|
||||
psa_destroy_key(key_id);
|
||||
#endif // SOC_AES_SUPPORTED
|
||||
test_utils_record_free_mem();
|
||||
test_utils_set_leak_level(CONFIG_UNITY_CRITICAL_LEAK_LEVEL_GENERAL, ESP_LEAK_TYPE_CRITICAL, ESP_COMP_LEAK_GENERAL);
|
||||
test_utils_set_leak_level(CONFIG_UNITY_WARN_LEAK_LEVEL_GENERAL, ESP_LEAK_TYPE_WARNING, ESP_COMP_LEAK_GENERAL);
|
||||
|
||||
@@ -7,10 +7,11 @@
|
||||
|
||||
#include "esp_log.h"
|
||||
#include "esp_heap_caps.h"
|
||||
|
||||
#include "mbedtls/ecp.h"
|
||||
#include "mbedtls/ecdsa.h"
|
||||
#include "mbedtls/sha256.h"
|
||||
#define MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS
|
||||
// #include "mbedtls/ecp.h"
|
||||
// #include "mbedtls/ecdsa.h"
|
||||
// #include "mbedtls/sha256.h"
|
||||
#include "psa/crypto.h"
|
||||
|
||||
#include "esp_tee.h"
|
||||
#include "esp_tee_attestation.h"
|
||||
@@ -24,6 +25,7 @@
|
||||
/* Note: negative value here so that assert message prints a grep-able
|
||||
error hex value (mbedTLS uses -N for error codes) */
|
||||
#define TEST_ASSERT_MBEDTLS_OK(X) TEST_ASSERT_EQUAL_HEX32(0, -(X))
|
||||
#define TEST_ASSERT_PSA_OK(X) TEST_ASSERT_EQUAL_HEX32(0, -(X))
|
||||
|
||||
#define SHA256_DIGEST_SZ (32)
|
||||
#define ECDSA_SECP256R1_KEY_LEN (32)
|
||||
@@ -165,19 +167,13 @@ static void prehash_token_data(const char *token_json, uint8_t *digest, size_t l
|
||||
char *eat_str = cJSON_PrintUnformatted(eat);
|
||||
char *public_key_str = cJSON_PrintUnformatted(public_key);
|
||||
|
||||
mbedtls_sha256_context sha256_ctx;
|
||||
|
||||
mbedtls_sha256_init(&sha256_ctx);
|
||||
|
||||
TEST_ASSERT_MBEDTLS_OK(mbedtls_sha256_starts(&sha256_ctx, false));
|
||||
|
||||
TEST_ASSERT_MBEDTLS_OK(mbedtls_sha256_update(&sha256_ctx, (const unsigned char *)header_str, strlen(header_str)));
|
||||
TEST_ASSERT_MBEDTLS_OK(mbedtls_sha256_update(&sha256_ctx, (const unsigned char *)eat_str, strlen(eat_str)));
|
||||
TEST_ASSERT_MBEDTLS_OK(mbedtls_sha256_update(&sha256_ctx, (const unsigned char *)public_key_str, strlen(public_key_str)));
|
||||
|
||||
TEST_ASSERT_MBEDTLS_OK(mbedtls_sha256_finish(&sha256_ctx, digest));
|
||||
|
||||
mbedtls_sha256_free(&sha256_ctx);
|
||||
psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
|
||||
TEST_ASSERT_PSA_OK(psa_hash_setup(&operation, PSA_ALG_SHA_256));
|
||||
size_t digest_len = 0;
|
||||
TEST_ASSERT_PSA_OK(psa_hash_update(&operation, (const unsigned char *)header_str, strlen(header_str)));
|
||||
TEST_ASSERT_PSA_OK(psa_hash_update(&operation, (const unsigned char *)eat_str, strlen(eat_str)));
|
||||
TEST_ASSERT_PSA_OK(psa_hash_update(&operation, (const unsigned char *)public_key_str, strlen(public_key_str)));
|
||||
TEST_ASSERT_PSA_OK(psa_hash_finish(&operation, digest, SHA256_DIGEST_SZ, &digest_len));
|
||||
|
||||
free(public_key_str);
|
||||
free(eat_str);
|
||||
@@ -239,13 +235,13 @@ static void fetch_signature(const char *token_json, esp_tee_sec_storage_ecdsa_si
|
||||
uint8_t *sign_r_buf = NULL;
|
||||
size_t sign_r_buf_sz = 0;
|
||||
hexstr_to_bytes(sign_r->valuestring, &sign_r_buf, &sign_r_buf_sz);
|
||||
memcpy(sign_ctx->sign_r, sign_r_buf, sign_r_buf_sz);
|
||||
memcpy(sign_ctx->signature, sign_r_buf, sign_r_buf_sz);
|
||||
free(sign_r_buf);
|
||||
|
||||
uint8_t *sign_s_buf = NULL;
|
||||
size_t sign_s_buf_sz = 0;
|
||||
hexstr_to_bytes(sign_s->valuestring, &sign_s_buf, &sign_s_buf_sz);
|
||||
memcpy(sign_ctx->sign_s, sign_s_buf, sign_s_buf_sz);
|
||||
memcpy(sign_ctx->signature + sign_r_buf_sz, sign_s_buf, sign_s_buf_sz);
|
||||
free(sign_s_buf);
|
||||
|
||||
cJSON_Delete(root);
|
||||
|
||||
@@ -8,10 +8,10 @@
|
||||
#include "esp_log.h"
|
||||
#include "esp_heap_caps.h"
|
||||
#include "esp_partition.h"
|
||||
|
||||
#include "mbedtls/ecp.h"
|
||||
#include "mbedtls/ecdsa.h"
|
||||
#include "mbedtls/sha256.h"
|
||||
#define MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS
|
||||
// #include "mbedtls/ecp.h"
|
||||
// #include "mbedtls/ecdsa.h"
|
||||
// #include "mbedtls/sha256.h"
|
||||
#include "ecdsa/ecdsa_alt.h"
|
||||
|
||||
#include "esp_tee.h"
|
||||
@@ -25,6 +25,7 @@
|
||||
#include "nvs.h"
|
||||
#include "unity.h"
|
||||
#include "sdkconfig.h"
|
||||
#include "ecdsa/ecdsa_alt.h"
|
||||
|
||||
/* Note: negative value here so that assert message prints a grep-able
|
||||
error hex value (mbedTLS uses -N for error codes) */
|
||||
@@ -52,33 +53,52 @@ int verify_ecdsa_sign(const uint8_t *digest, size_t len, const esp_tee_sec_stora
|
||||
TEST_ASSERT_NOT_NULL(sign);
|
||||
TEST_ASSERT_NOT_EQUAL(0, len);
|
||||
|
||||
mbedtls_mpi r, s;
|
||||
mbedtls_mpi_init(&r);
|
||||
mbedtls_mpi_init(&s);
|
||||
int err = ESP_FAIL;
|
||||
|
||||
mbedtls_ecdsa_context ecdsa_context;
|
||||
mbedtls_ecdsa_init(&ecdsa_context);
|
||||
psa_key_id_t key_id = 0;
|
||||
psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
|
||||
psa_set_key_type(&key_attributes, PSA_KEY_TYPE_ECC_PUBLIC_KEY(PSA_ECC_FAMILY_SECP_R1));
|
||||
psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_VERIFY_HASH);
|
||||
psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDSA(PSA_ALG_SHA_256));
|
||||
|
||||
mbedtls_ecp_group_id curve_id = MBEDTLS_ECP_DP_SECP256R1;
|
||||
size_t pub_key_len;
|
||||
size_t signature_size;
|
||||
if (is_crv_p192) {
|
||||
curve_id = MBEDTLS_ECP_DP_SECP192R1;
|
||||
psa_set_key_bits(&key_attributes, ECDSA_SECP192R1_KEY_LEN * 8);
|
||||
pub_key_len = ECDSA_SECP192R1_KEY_LEN;
|
||||
signature_size = ECDSA_SECP192R1_KEY_LEN * 2;
|
||||
} else {
|
||||
psa_set_key_bits(&key_attributes, ECDSA_SECP256R1_KEY_LEN * 8);
|
||||
pub_key_len = ECDSA_SECP256R1_KEY_LEN;
|
||||
signature_size = ECDSA_SECP256R1_KEY_LEN * 2;
|
||||
}
|
||||
|
||||
TEST_ASSERT_MBEDTLS_OK(mbedtls_ecp_group_load(&ecdsa_context.MBEDTLS_PRIVATE(grp), curve_id));
|
||||
size_t plen = mbedtls_mpi_size(&ecdsa_context.MBEDTLS_PRIVATE(grp).P);
|
||||
uint8_t pub_key[2 * pub_key_len + 1];
|
||||
pub_key[0] = 0x04;
|
||||
memcpy(pub_key + 1, pubkey->pub_x, pub_key_len);
|
||||
memcpy(pub_key + 1 + pub_key_len, pubkey->pub_y, pub_key_len);
|
||||
|
||||
TEST_ASSERT_MBEDTLS_OK(mbedtls_mpi_read_binary(&r, sign->sign_r, plen));
|
||||
TEST_ASSERT_MBEDTLS_OK(mbedtls_mpi_read_binary(&s, sign->sign_s, plen));
|
||||
TEST_ASSERT_MBEDTLS_OK(mbedtls_mpi_read_binary(&ecdsa_context.MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(X), pubkey->pub_x, plen));
|
||||
TEST_ASSERT_MBEDTLS_OK(mbedtls_mpi_read_binary(&ecdsa_context.MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(Y), pubkey->pub_y, plen));
|
||||
TEST_ASSERT_MBEDTLS_OK(mbedtls_mpi_lset(&ecdsa_context.MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(Z), 1));
|
||||
TEST_ASSERT_MBEDTLS_OK(mbedtls_ecdsa_verify(&ecdsa_context.MBEDTLS_PRIVATE(grp), digest, len, &ecdsa_context.MBEDTLS_PRIVATE(Q), &r, &s));
|
||||
psa_status_t status = psa_import_key(&key_attributes, pub_key, sizeof(pub_key), &key_id);
|
||||
if (status != PSA_SUCCESS) {
|
||||
ESP_LOGE(TAG, "Failed to import ECDSA public key: %ld", status);
|
||||
err = ESP_ERR_INVALID_ARG;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_mpi_free(&r);
|
||||
mbedtls_mpi_free(&s);
|
||||
mbedtls_ecdsa_free(&ecdsa_context);
|
||||
status = psa_verify_hash(key_id, PSA_ALG_ECDSA(PSA_ALG_SHA_256), digest, len, sign->signature, signature_size);
|
||||
if (status != PSA_SUCCESS) {
|
||||
ESP_LOGE(TAG, "Failed to verify ECDSA signature: %ld", status);
|
||||
err = ESP_ERR_INVALID_ARG;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
return 0;
|
||||
psa_destroy_key(key_id);
|
||||
psa_reset_key_attributes(&key_attributes);
|
||||
|
||||
err = ESP_OK;
|
||||
|
||||
exit:
|
||||
return err;
|
||||
}
|
||||
|
||||
TEST_CASE("Test TEE Secure Storage - Sign-verify (ecdsa_secp256r1)", "[sec_storage]")
|
||||
@@ -90,7 +110,9 @@ TEST_CASE("Test TEE Secure Storage - Sign-verify (ecdsa_secp256r1)", "[sec_stora
|
||||
esp_fill_random(message, buf_sz);
|
||||
|
||||
uint8_t msg_digest[SHA256_DIGEST_SZ];
|
||||
TEST_ASSERT_MBEDTLS_OK(mbedtls_sha256(message, buf_sz, msg_digest, false));
|
||||
size_t msg_digest_len = 0;
|
||||
psa_status_t status = psa_hash_compute(PSA_ALG_SHA_256, message, buf_sz, msg_digest, sizeof(msg_digest), &msg_digest_len);
|
||||
TEST_ASSERT_EQUAL(PSA_SUCCESS, status);
|
||||
free(message);
|
||||
|
||||
esp_tee_sec_storage_key_cfg_t key_cfg = {
|
||||
@@ -108,12 +130,12 @@ TEST_CASE("Test TEE Secure Storage - Sign-verify (ecdsa_secp256r1)", "[sec_stora
|
||||
TEST_ESP_OK(esp_tee_sec_storage_gen_key(&key_cfg));
|
||||
|
||||
esp_tee_sec_storage_ecdsa_sign_t sign = {};
|
||||
TEST_ESP_OK(esp_tee_sec_storage_ecdsa_sign(&key_cfg, msg_digest, sizeof(msg_digest), &sign));
|
||||
TEST_ESP_OK(esp_tee_sec_storage_ecdsa_sign(&key_cfg, msg_digest, msg_digest_len, &sign));
|
||||
|
||||
esp_tee_sec_storage_ecdsa_pubkey_t pubkey = {};
|
||||
TEST_ESP_OK(esp_tee_sec_storage_ecdsa_get_pubkey(&key_cfg, &pubkey));
|
||||
|
||||
TEST_ESP_OK(verify_ecdsa_sign(msg_digest, sizeof(msg_digest), &pubkey, &sign, false));
|
||||
TEST_ESP_OK(verify_ecdsa_sign(msg_digest, msg_digest_len, &pubkey, &sign, false));
|
||||
|
||||
TEST_ESP_OK(esp_tee_sec_storage_clear_key(key_cfg.id));
|
||||
}
|
||||
@@ -129,7 +151,10 @@ TEST_CASE("Test TEE Secure Storage - Sign-verify (ecdsa_secp192r1)", "[sec_stora
|
||||
esp_fill_random(message, buf_sz);
|
||||
|
||||
uint8_t msg_digest[SHA256_DIGEST_SZ];
|
||||
TEST_ASSERT_MBEDTLS_OK(mbedtls_sha256(message, buf_sz, msg_digest, false));
|
||||
size_t msg_digest_len = 0;
|
||||
psa_status_t status = psa_hash_compute(PSA_ALG_SHA_256, message, buf_sz, msg_digest, sizeof(msg_digest), &msg_digest_len);
|
||||
(void)msg_digest_len;
|
||||
TEST_ASSERT_EQUAL(PSA_SUCCESS, status);
|
||||
free(message);
|
||||
|
||||
esp_tee_sec_storage_key_cfg_t key_cfg = {
|
||||
@@ -222,7 +247,9 @@ TEST_CASE("Test TEE Secure Storage - Operations with invalid/non-existent keys",
|
||||
TEST_ASSERT_NOT_NULL(message);
|
||||
esp_fill_random(message, SZ);
|
||||
uint8_t msg_digest[SHA256_DIGEST_SZ];
|
||||
TEST_ASSERT_MBEDTLS_OK(mbedtls_sha256(message, SZ, msg_digest, false));
|
||||
size_t msg_digest_len = 0;
|
||||
psa_status_t status = psa_hash_compute(PSA_ALG_SHA_256, message, SZ, msg_digest, sizeof(msg_digest), &msg_digest_len);
|
||||
TEST_ASSERT_EQUAL(PSA_SUCCESS, status);
|
||||
free(message);
|
||||
|
||||
const char *key_id = "key_id_misc";
|
||||
@@ -483,18 +510,18 @@ static void test_ecdsa_sign(mbedtls_ecp_group_id gid)
|
||||
};
|
||||
TEST_ASSERT_EQUAL(0, esp_ecdsa_tee_set_pk_context(&key_ctx, &conf));
|
||||
|
||||
mbedtls_ecp_keypair *keypair = mbedtls_pk_ec(key_ctx);
|
||||
mbedtls_ecp_keypair *keypair = key_ctx.MBEDTLS_PRIVATE(pk_ctx); //mbedtls_pk_ec(key_ctx);
|
||||
mbedtls_mpi key_mpi = keypair->MBEDTLS_PRIVATE(d);
|
||||
|
||||
TEST_ASSERT_MBEDTLS_OK(mbedtls_ecdsa_sign(&ecdsa_context.MBEDTLS_PRIVATE(grp), &r, &s, &key_mpi, sha, SHA256_DIGEST_SZ, NULL, NULL));
|
||||
|
||||
esp_tee_sec_storage_ecdsa_sign_t sign = {};
|
||||
TEST_ASSERT_MBEDTLS_OK(mbedtls_mpi_write_binary(&r, sign.sign_r, key_len));
|
||||
TEST_ASSERT_MBEDTLS_OK(mbedtls_mpi_write_binary(&s, sign.sign_s, key_len));
|
||||
TEST_ASSERT_MBEDTLS_OK(mbedtls_mpi_write_binary(&r, sign.signature, key_len));
|
||||
TEST_ASSERT_MBEDTLS_OK(mbedtls_mpi_write_binary(&s, sign.signature + key_len, key_len));
|
||||
|
||||
TEST_ESP_OK(verify_ecdsa_sign(sha, sizeof(sha), &pubkey, &sign, is_crv_p192));
|
||||
|
||||
mbedtls_pk_free(&key_ctx);
|
||||
esp_ecdsa_free_pk_context(&key_ctx);
|
||||
mbedtls_ecdsa_free(&ecdsa_context);
|
||||
mbedtls_mpi_free(&r);
|
||||
mbedtls_mpi_free(&s);
|
||||
|
||||
@@ -11,3 +11,6 @@ CONFIG_SECURE_TEE_TEST_MODE=y
|
||||
# Setting partition table
|
||||
CONFIG_PARTITION_TABLE_SINGLE_APP_TEE=y
|
||||
CONFIG_PARTITION_TABLE_OFFSET=0xF000
|
||||
|
||||
# TEE IRAM size
|
||||
CONFIG_SECURE_TEE_IRAM_SIZE=0xC400
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
©œ_¶Ý“òc©/!û¨Ž¹²º�Ʋm YÃSÌ+)vÅ—¤␍רƒeS›
|
||||
Reference in New Issue
Block a user