mirror of
https://github.com/espressif/esp-idf.git
synced 2025-09-20 00:22:39 +00:00
feat: add ecdsa-p384 testcases and relative support for ESP32C5 ECO2
This commit adds testcases in crypto/hal and mbedtls testapps.
This commit is contained in:
@@ -27,6 +27,7 @@
|
||||
#include "hal/ecdsa_ll.h"
|
||||
#include "hal/ecdsa_hal.h"
|
||||
#include "esp_efuse.h"
|
||||
#include "esp_efuse_chip.h"
|
||||
#endif
|
||||
#if SOC_ECC_SUPPORTED
|
||||
#include "hal/ecc_ll.h"
|
||||
@@ -37,8 +38,24 @@
|
||||
|
||||
#define ECDSA_KEY_MAGIC (short) 0xECD5A
|
||||
#define ECDSA_KEY_MAGIC_TEE (short) 0xA5DCE
|
||||
#define ECDSA_SHA_LEN 32
|
||||
|
||||
/* Key lengths for different ECDSA curves */
|
||||
#define ECDSA_KEY_LEN_P192 24
|
||||
#define ECDSA_KEY_LEN_P256 32
|
||||
#define ECDSA_KEY_LEN_P384 48
|
||||
|
||||
#if SOC_ECDSA_SUPPORT_CURVE_P384
|
||||
#define MAX_ECDSA_COMPONENT_LEN 48
|
||||
#define MAX_ECDSA_SHA_LEN 48
|
||||
#else
|
||||
#define MAX_ECDSA_COMPONENT_LEN 32
|
||||
#define MAX_ECDSA_SHA_LEN 32
|
||||
#endif
|
||||
|
||||
#define ECDSA_SHA_LEN 32
|
||||
#if SOC_ECDSA_SUPPORT_CURVE_P384
|
||||
#define ECDSA_SHA_LEN_P384 48
|
||||
#endif /* SOC_ECDSA_SUPPORT_CURVE_P384 */
|
||||
|
||||
#if CONFIG_MBEDTLS_HARDWARE_ECDSA_SIGN_CONSTANT_TIME_CM
|
||||
#include "esp_timer.h"
|
||||
@@ -67,6 +84,19 @@
|
||||
__attribute__((unused)) static const char *TAG = "ecdsa_alt";
|
||||
|
||||
#if SOC_ECDSA_SUPPORTED
|
||||
/**
|
||||
* @brief Check if the extracted efuse blocks are valid
|
||||
*
|
||||
* @param high_blk High efuse block number
|
||||
* @param low_blk Low efuse block number
|
||||
* @return true if both blocks are valid, false otherwise
|
||||
*/
|
||||
static inline bool is_efuse_blk_valid(int high_blk, int low_blk)
|
||||
{
|
||||
return (high_blk >= EFUSE_BLK0 && high_blk < EFUSE_BLK_MAX &&
|
||||
low_blk >= EFUSE_BLK0 && low_blk < EFUSE_BLK_MAX);
|
||||
}
|
||||
|
||||
static void esp_ecdsa_acquire_hardware(void)
|
||||
{
|
||||
esp_crypto_ecdsa_lock_acquire();
|
||||
@@ -97,10 +127,90 @@ static void esp_ecdsa_release_hardware(void)
|
||||
}
|
||||
#endif /* SOC_ECDSA_SUPPORTED */
|
||||
|
||||
#if SOC_ECDSA_SUPPORT_EXPORT_PUBKEY || CONFIG_MBEDTLS_HARDWARE_ECDSA_SIGN
|
||||
/**
|
||||
* @brief Validate if the efuse block(s) have the appropriate ECDSA key purpose for the given curve
|
||||
*
|
||||
* This function validates that the provided efuse block(s) have been programmed with the appropriate
|
||||
* ECDSA key purpose for the specified curve type. It handles both curve-specific key purposes
|
||||
* (when SOC_ECDSA_SUPPORT_CURVE_SPECIFIC_KEY_PURPOSES is defined) and generic ECDSA key purpose.
|
||||
*
|
||||
* For SECP384R1 curve, it checks for both high and low key blocks when supported.
|
||||
* For SECP192R1 and SECP256R1 curves, it validates the single block.
|
||||
*
|
||||
* @param[in] grp_id The ECP group ID (curve type) to validate the key purpose for
|
||||
* @param[in] efuse_blk The efuse block(s) to validate (can be combined for 384-bit keys)
|
||||
*
|
||||
* @return
|
||||
* - 0 on success (block(s) have correct purpose)
|
||||
* - MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input parameters are invalid
|
||||
* - MBEDTLS_ERR_ECP_INVALID_KEY if block(s) don't have appropriate key purpose
|
||||
*/
|
||||
static int esp_ecdsa_validate_efuse_block(mbedtls_ecp_group_id grp_id, int efuse_blk)
|
||||
{
|
||||
#if SOC_ECDSA_SUPPORT_CURVE_SPECIFIC_KEY_PURPOSES
|
||||
esp_efuse_purpose_t expected_purpose;
|
||||
esp_efuse_purpose_t actual_purpose;
|
||||
|
||||
switch (grp_id) {
|
||||
case MBEDTLS_ECP_DP_SECP192R1:
|
||||
expected_purpose = ESP_EFUSE_KEY_PURPOSE_ECDSA_KEY_P192;
|
||||
actual_purpose = esp_efuse_get_key_purpose((esp_efuse_block_t)efuse_blk);
|
||||
if (actual_purpose != expected_purpose) {
|
||||
ESP_LOGE(TAG, "Efuse block %d has purpose %d, expected %d", efuse_blk, actual_purpose, expected_purpose);
|
||||
return MBEDTLS_ERR_ECP_INVALID_KEY;
|
||||
}
|
||||
break;
|
||||
case MBEDTLS_ECP_DP_SECP256R1:
|
||||
expected_purpose = ESP_EFUSE_KEY_PURPOSE_ECDSA_KEY_P256;
|
||||
actual_purpose = esp_efuse_get_key_purpose((esp_efuse_block_t)efuse_blk);
|
||||
if (actual_purpose != expected_purpose) {
|
||||
ESP_LOGE(TAG, "Efuse block %d has purpose %d, expected %d", efuse_blk, actual_purpose, expected_purpose);
|
||||
return MBEDTLS_ERR_ECP_INVALID_KEY;
|
||||
}
|
||||
break;
|
||||
#if SOC_ECDSA_SUPPORT_CURVE_P384
|
||||
case MBEDTLS_ECP_DP_SECP384R1:
|
||||
int high_blk, low_blk;
|
||||
MBEDTLS_ECDSA_EXTRACT_KEY_BLOCKS(efuse_blk, high_blk, low_blk);
|
||||
// For P384, we need to check both blocks
|
||||
esp_efuse_purpose_t high_purpose = esp_efuse_get_key_purpose((esp_efuse_block_t)high_blk);
|
||||
esp_efuse_purpose_t low_purpose = esp_efuse_get_key_purpose((esp_efuse_block_t)low_blk);
|
||||
|
||||
if (high_purpose != ESP_EFUSE_KEY_PURPOSE_ECDSA_KEY_P384_H) {
|
||||
ESP_LOGE(TAG, "Efuse block %d has purpose %d, expected P384_H (%d)",
|
||||
high_blk, high_purpose, ESP_EFUSE_KEY_PURPOSE_ECDSA_KEY_P384_H);
|
||||
return MBEDTLS_ERR_ECP_INVALID_KEY;
|
||||
}
|
||||
if (low_purpose != ESP_EFUSE_KEY_PURPOSE_ECDSA_KEY_P384_L) {
|
||||
ESP_LOGE(TAG, "Efuse block %d has purpose %d, expected P384_L (%d)",
|
||||
low_blk, low_purpose, ESP_EFUSE_KEY_PURPOSE_ECDSA_KEY_P384_L);
|
||||
return MBEDTLS_ERR_ECP_INVALID_KEY;
|
||||
}
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
ESP_LOGE(TAG, "Invalid ECDSA curve id");
|
||||
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
||||
}
|
||||
#else /* SOC_ECDSA_SUPPORT_CURVE_SPECIFIC_KEY_PURPOSES */
|
||||
// For generic ECDSA key purpose, validate the single block (efuse_blk)
|
||||
esp_efuse_purpose_t actual_purpose = esp_efuse_get_key_purpose((esp_efuse_block_t)efuse_blk);
|
||||
if (actual_purpose != ESP_EFUSE_KEY_PURPOSE_ECDSA_KEY) {
|
||||
ESP_LOGE(TAG, "Efuse block %d has purpose %d, expected ECDSA_KEY (%d)",
|
||||
efuse_blk, actual_purpose, ESP_EFUSE_KEY_PURPOSE_ECDSA_KEY);
|
||||
return MBEDTLS_ERR_ECP_INVALID_KEY;
|
||||
}
|
||||
#endif /* !SOC_ECDSA_SUPPORT_CURVE_SPECIFIC_KEY_PURPOSES */
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif /* SOC_ECDSA_SUPPORT_EXPORT_PUBKEY || CONFIG_MBEDTLS_HARDWARE_ECDSA_SIGN */
|
||||
|
||||
static void __attribute__((unused)) ecdsa_be_to_le(const uint8_t* be_point, uint8_t *le_point, uint8_t len)
|
||||
{
|
||||
/* When the size is 24 bytes, it should be padded with 0 bytes*/
|
||||
memset(le_point, 0x0, 32);
|
||||
memset(le_point, 0x0, len);
|
||||
|
||||
for(int i = 0; i < len; i++) {
|
||||
le_point[i] = be_point[len - i - 1];
|
||||
@@ -113,14 +223,16 @@ int esp_ecdsa_load_pubkey(mbedtls_ecp_keypair *keypair, int efuse_blk)
|
||||
int ret = -1;
|
||||
bool use_km_key = (efuse_blk == USE_ECDSA_KEY_FROM_KEY_MANAGER)? true: false;
|
||||
if (!use_km_key) {
|
||||
if (efuse_blk < EFUSE_BLK_KEY0 || efuse_blk >= EFUSE_BLK_KEY_MAX) {
|
||||
int high_blk, low_blk;
|
||||
MBEDTLS_ECDSA_EXTRACT_KEY_BLOCKS(efuse_blk, high_blk, low_blk);
|
||||
|
||||
if (!is_efuse_blk_valid(high_blk, low_blk)) {
|
||||
ESP_LOGE(TAG, "Invalid efuse block selected");
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
ecdsa_curve_t curve;
|
||||
esp_efuse_block_t blk;
|
||||
uint16_t len;
|
||||
uint8_t zeroes[MAX_ECDSA_COMPONENT_LEN] = {0};
|
||||
uint8_t qx_le[MAX_ECDSA_COMPONENT_LEN];
|
||||
@@ -128,18 +240,25 @@ int esp_ecdsa_load_pubkey(mbedtls_ecp_keypair *keypair, int efuse_blk)
|
||||
|
||||
if (keypair->MBEDTLS_PRIVATE(grp).id == MBEDTLS_ECP_DP_SECP192R1) {
|
||||
curve = ECDSA_CURVE_SECP192R1;
|
||||
len = 24;
|
||||
len = ECDSA_KEY_LEN_P192;
|
||||
} else if (keypair->MBEDTLS_PRIVATE(grp).id == MBEDTLS_ECP_DP_SECP256R1) {
|
||||
curve = ECDSA_CURVE_SECP256R1;
|
||||
len = 32;
|
||||
} else {
|
||||
len = ECDSA_KEY_LEN_P256;
|
||||
}
|
||||
#if SOC_ECDSA_SUPPORT_CURVE_P384
|
||||
else if (keypair->MBEDTLS_PRIVATE(grp).id == MBEDTLS_ECP_DP_SECP384R1) {
|
||||
curve = ECDSA_CURVE_SECP384R1;
|
||||
len = ECDSA_KEY_LEN_P384;
|
||||
}
|
||||
#endif
|
||||
else {
|
||||
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
||||
}
|
||||
|
||||
if (!use_km_key) {
|
||||
if (!esp_efuse_find_purpose(ESP_EFUSE_KEY_PURPOSE_ECDSA_KEY, &blk)) {
|
||||
ESP_LOGE(TAG, "No efuse block with purpose ECDSA_KEY found");
|
||||
return MBEDTLS_ERR_ECP_INVALID_KEY;
|
||||
ret = esp_ecdsa_validate_efuse_block(keypair->MBEDTLS_PRIVATE(grp).id, efuse_blk);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -196,7 +315,11 @@ static int validate_ecdsa_pk_input(mbedtls_pk_context *key_ctx, esp_ecdsa_pk_con
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (conf->grp_id != MBEDTLS_ECP_DP_SECP192R1 && conf->grp_id != MBEDTLS_ECP_DP_SECP256R1) {
|
||||
if (conf->grp_id != MBEDTLS_ECP_DP_SECP192R1 && conf->grp_id != MBEDTLS_ECP_DP_SECP256R1
|
||||
#if SOC_ECDSA_SUPPORT_CURVE_P384
|
||||
&& conf->grp_id != MBEDTLS_ECP_DP_SECP384R1
|
||||
#endif
|
||||
) {
|
||||
ESP_LOGE(TAG, "Invalid EC curve group id mentioned in esp_ecdsa_pk_conf_t");
|
||||
return ret;
|
||||
}
|
||||
@@ -215,8 +338,11 @@ int esp_ecdsa_privkey_load_mpi(mbedtls_mpi *key, int efuse_blk)
|
||||
|
||||
bool use_km_key = (efuse_blk == USE_ECDSA_KEY_FROM_KEY_MANAGER)? true: false;
|
||||
if (!use_km_key) {
|
||||
if (efuse_blk < EFUSE_BLK_KEY0 || efuse_blk >= EFUSE_BLK_KEY_MAX) {
|
||||
ESP_LOGE(TAG, "Invalid efuse block");
|
||||
int high_blk, low_blk;
|
||||
MBEDTLS_ECDSA_EXTRACT_KEY_BLOCKS(efuse_blk, high_blk, low_blk);
|
||||
|
||||
if (!is_efuse_blk_valid(high_blk, low_blk)) {
|
||||
ESP_LOGE(TAG, "Invalid efuse block selected");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@@ -253,8 +379,11 @@ int esp_ecdsa_privkey_load_pk_context(mbedtls_pk_context *key_ctx, int efuse_blk
|
||||
|
||||
bool use_km_key = (efuse_blk == USE_ECDSA_KEY_FROM_KEY_MANAGER)? true: false;
|
||||
if (!use_km_key) {
|
||||
if (efuse_blk < EFUSE_BLK_KEY0 || efuse_blk >= EFUSE_BLK_KEY_MAX) {
|
||||
ESP_LOGE(TAG, "Invalid efuse block");
|
||||
int high_blk, low_blk;
|
||||
MBEDTLS_ECDSA_EXTRACT_KEY_BLOCKS(efuse_blk, high_blk, low_blk);
|
||||
|
||||
if (!is_efuse_blk_valid(high_blk, low_blk)) {
|
||||
ESP_LOGE(TAG, "Invalid efuse block selected");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@@ -285,7 +414,6 @@ int esp_ecdsa_set_pk_context(mbedtls_pk_context *key_ctx, esp_ecdsa_pk_conf_t *c
|
||||
efuse_key_block = conf->efuse_block;
|
||||
}
|
||||
|
||||
|
||||
if ((ret = esp_ecdsa_privkey_load_pk_context(key_ctx, efuse_key_block)) != 0) {
|
||||
ESP_LOGE(TAG, "Loading private key context failed, esp_ecdsa_privkey_load_pk_context() returned %d", ret);
|
||||
return ret;
|
||||
@@ -314,10 +442,9 @@ static int esp_ecdsa_sign(mbedtls_ecp_group *grp, mbedtls_mpi* r, mbedtls_mpi* s
|
||||
ecdsa_sign_type_t k_type)
|
||||
{
|
||||
ecdsa_curve_t curve;
|
||||
esp_efuse_block_t blk;
|
||||
uint16_t len;
|
||||
uint8_t zeroes[MAX_ECDSA_COMPONENT_LEN] = {0};
|
||||
uint8_t sha_le[ECDSA_SHA_LEN];
|
||||
uint8_t sha_le[MAX_ECDSA_SHA_LEN];
|
||||
uint8_t r_le[MAX_ECDSA_COMPONENT_LEN];
|
||||
uint8_t s_le[MAX_ECDSA_COMPONENT_LEN];
|
||||
|
||||
@@ -325,17 +452,29 @@ static int esp_ecdsa_sign(mbedtls_ecp_group *grp, mbedtls_mpi* r, mbedtls_mpi* s
|
||||
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
||||
}
|
||||
|
||||
if (msg_len != ECDSA_SHA_LEN) {
|
||||
if ((grp->id == MBEDTLS_ECP_DP_SECP192R1 && msg_len != ECDSA_SHA_LEN) ||
|
||||
(grp->id == MBEDTLS_ECP_DP_SECP256R1 && msg_len != ECDSA_SHA_LEN)
|
||||
#if SOC_ECDSA_SUPPORT_CURVE_P384
|
||||
|| (grp->id == MBEDTLS_ECP_DP_SECP384R1 && msg_len != ECDSA_SHA_LEN_P384)
|
||||
#endif
|
||||
) {
|
||||
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
||||
}
|
||||
|
||||
if (grp->id == MBEDTLS_ECP_DP_SECP192R1) {
|
||||
curve = ECDSA_CURVE_SECP192R1;
|
||||
len = 24;
|
||||
len = ECDSA_KEY_LEN_P192;
|
||||
} else if (grp->id == MBEDTLS_ECP_DP_SECP256R1) {
|
||||
curve = ECDSA_CURVE_SECP256R1;
|
||||
len = 32;
|
||||
} else {
|
||||
len = ECDSA_KEY_LEN_P256;
|
||||
}
|
||||
#if SOC_ECDSA_SUPPORT_CURVE_P384
|
||||
else if (grp->id == MBEDTLS_ECP_DP_SECP384R1) {
|
||||
curve = ECDSA_CURVE_SECP384R1;
|
||||
len = ECDSA_KEY_LEN_P384;
|
||||
}
|
||||
#endif
|
||||
else {
|
||||
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
||||
}
|
||||
|
||||
@@ -343,10 +482,11 @@ static int esp_ecdsa_sign(mbedtls_ecp_group *grp, mbedtls_mpi* r, mbedtls_mpi* s
|
||||
if (d->MBEDTLS_PRIVATE(n) == (unsigned short) USE_ECDSA_KEY_FROM_KEY_MANAGER) {
|
||||
use_km_key = true;
|
||||
}
|
||||
|
||||
if (!use_km_key) {
|
||||
if (!esp_efuse_find_purpose(ESP_EFUSE_KEY_PURPOSE_ECDSA_KEY, &blk)) {
|
||||
ESP_LOGE(TAG, "No efuse block with purpose ECDSA_KEY found");
|
||||
return MBEDTLS_ERR_ECP_INVALID_KEY;
|
||||
int ret = esp_ecdsa_validate_efuse_block(grp->id, d->MBEDTLS_PRIVATE(n));
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -422,10 +562,10 @@ int esp_ecdsa_tee_load_pubkey(mbedtls_ecp_keypair *keypair, const char *tee_key_
|
||||
esp_tee_sec_storage_type_t key_type;
|
||||
|
||||
if (keypair->MBEDTLS_PRIVATE(grp).id == MBEDTLS_ECP_DP_SECP256R1) {
|
||||
len = 32;
|
||||
len = ECDSA_KEY_LEN_P256;
|
||||
key_type = ESP_SEC_STG_KEY_ECDSA_SECP256R1;
|
||||
} else if (keypair->MBEDTLS_PRIVATE(grp).id == MBEDTLS_ECP_DP_SECP192R1) {
|
||||
len = 24;
|
||||
len = ECDSA_KEY_LEN_P192;
|
||||
key_type = ESP_SEC_STG_KEY_ECDSA_SECP192R1;
|
||||
} else {
|
||||
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
||||
@@ -522,17 +662,29 @@ static int esp_ecdsa_tee_sign(mbedtls_ecp_group *grp, mbedtls_mpi* r, mbedtls_mp
|
||||
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
||||
}
|
||||
|
||||
if (msg_len != ECDSA_SHA_LEN) {
|
||||
if ((grp->id == MBEDTLS_ECP_DP_SECP192R1 && msg_len != ECDSA_SHA_LEN) ||
|
||||
(grp->id == MBEDTLS_ECP_DP_SECP256R1 && msg_len != ECDSA_SHA_LEN)
|
||||
#if SOC_ECDSA_SUPPORT_CURVE_P384
|
||||
|| (grp->id == MBEDTLS_ECP_DP_SECP384R1 && msg_len != ECDSA_SHA_LEN_P384)
|
||||
#endif
|
||||
) {
|
||||
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
||||
}
|
||||
|
||||
if (grp->id == MBEDTLS_ECP_DP_SECP256R1) {
|
||||
len = 32;
|
||||
len = ECDSA_KEY_LEN_P256;
|
||||
key_type = ESP_SEC_STG_KEY_ECDSA_SECP256R1;
|
||||
} else if (grp->id == MBEDTLS_ECP_DP_SECP192R1) {
|
||||
len = 24;
|
||||
len = ECDSA_KEY_LEN_P192;
|
||||
key_type = ESP_SEC_STG_KEY_ECDSA_SECP192R1;
|
||||
} else {
|
||||
}
|
||||
#if SOC_ECDSA_SUPPORT_CURVE_P384
|
||||
else if (grp->id == MBEDTLS_ECP_DP_SECP384R1) {
|
||||
len = ECDSA_KEY_LEN_P384;
|
||||
key_type = ESP_SEC_STG_KEY_ECDSA_SECP384R1;
|
||||
}
|
||||
#endif
|
||||
else {
|
||||
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
||||
}
|
||||
|
||||
@@ -877,23 +1029,35 @@ static int esp_ecdsa_verify(mbedtls_ecp_group *grp,
|
||||
uint8_t s_le[MAX_ECDSA_COMPONENT_LEN];
|
||||
uint8_t qx_le[MAX_ECDSA_COMPONENT_LEN];
|
||||
uint8_t qy_le[MAX_ECDSA_COMPONENT_LEN];
|
||||
uint8_t sha_le[ECDSA_SHA_LEN];
|
||||
uint8_t sha_le[MAX_ECDSA_SHA_LEN];
|
||||
|
||||
if (!grp || !buf || !Q || !r || !s) {
|
||||
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
||||
}
|
||||
|
||||
if (blen != ECDSA_SHA_LEN) {
|
||||
if ((grp->id == MBEDTLS_ECP_DP_SECP192R1 && blen != ECDSA_SHA_LEN) ||
|
||||
(grp->id == MBEDTLS_ECP_DP_SECP256R1 && blen != ECDSA_SHA_LEN)
|
||||
#if SOC_ECDSA_SUPPORT_CURVE_P384
|
||||
|| (grp->id == MBEDTLS_ECP_DP_SECP384R1 && blen != ECDSA_SHA_LEN_P384)
|
||||
#endif
|
||||
) {
|
||||
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
||||
}
|
||||
|
||||
if (grp->id == MBEDTLS_ECP_DP_SECP192R1) {
|
||||
curve = ECDSA_CURVE_SECP192R1;
|
||||
len = 24;
|
||||
len = ECDSA_KEY_LEN_P192;
|
||||
} else if (grp->id == MBEDTLS_ECP_DP_SECP256R1) {
|
||||
curve = ECDSA_CURVE_SECP256R1;
|
||||
len = 32;
|
||||
} else {
|
||||
len = ECDSA_KEY_LEN_P256;
|
||||
}
|
||||
#if SOC_ECDSA_SUPPORT_CURVE_P384
|
||||
else if (grp->id == MBEDTLS_ECP_DP_SECP384R1) {
|
||||
curve = ECDSA_CURVE_SECP384R1;
|
||||
len = ECDSA_KEY_LEN_P384;
|
||||
}
|
||||
#endif
|
||||
else {
|
||||
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
||||
}
|
||||
|
||||
@@ -953,9 +1117,12 @@ int __wrap_mbedtls_ecdsa_verify_restartable(mbedtls_ecp_group *grp,
|
||||
const mbedtls_mpi *s,
|
||||
mbedtls_ecdsa_restart_ctx *rs_ctx)
|
||||
{
|
||||
if (((grp->id == MBEDTLS_ECP_DP_SECP192R1 && esp_efuse_is_ecdsa_p192_curve_supported())
|
||||
|| (grp->id == MBEDTLS_ECP_DP_SECP256R1 && esp_efuse_is_ecdsa_p256_curve_supported()))
|
||||
&& blen == ECDSA_SHA_LEN) {
|
||||
if ((grp->id == MBEDTLS_ECP_DP_SECP192R1 && blen == ECDSA_SHA_LEN && esp_efuse_is_ecdsa_p192_curve_supported()) ||
|
||||
(grp->id == MBEDTLS_ECP_DP_SECP256R1 && blen == ECDSA_SHA_LEN && esp_efuse_is_ecdsa_p256_curve_supported())
|
||||
#if SOC_ECDSA_SUPPORT_CURVE_P384
|
||||
|| (grp->id == MBEDTLS_ECP_DP_SECP384R1 && blen == ECDSA_SHA_LEN_P384)
|
||||
#endif
|
||||
) {
|
||||
return esp_ecdsa_verify(grp, buf, blen, Q, r, s);
|
||||
} else {
|
||||
return __real_mbedtls_ecdsa_verify_restartable(grp, buf, blen, Q, r, s, rs_ctx);
|
||||
|
@@ -12,6 +12,7 @@
|
||||
#include "mbedtls/pk.h"
|
||||
#include "sdkconfig.h"
|
||||
#include "soc/soc_caps.h"
|
||||
#include "hal/ecdsa_types.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
@@ -19,6 +20,22 @@ extern "C" {
|
||||
|
||||
#define USE_ECDSA_KEY_FROM_KEY_MANAGER INT_MAX
|
||||
|
||||
/* ECDSA key block combination macros - aliases to HAL macros */
|
||||
|
||||
/**
|
||||
* @brief Macro to combine two key blocks into a single integer
|
||||
* @note Least significant 4 bits stores block number of the low key block, and the next 4 more significant bits store the high key block number.
|
||||
* @note example: HAL_ECDSA_COMBINE_KEY_BLOCKS(blk_high, blk_low)
|
||||
*/
|
||||
#define MBEDTLS_ECDSA_COMBINE_KEY_BLOCKS HAL_ECDSA_COMBINE_KEY_BLOCKS
|
||||
|
||||
/**
|
||||
* @brief Macro to extract high and low key block numbers from a combined integer
|
||||
* @note Extracts high block from bits 4-7 and low block from bits 0-3
|
||||
* @note example: HAL_ECDSA_EXTRACT_KEY_BLOCKS(combined_blk, blk_high, blk_low)
|
||||
*/
|
||||
#define MBEDTLS_ECDSA_EXTRACT_KEY_BLOCKS HAL_ECDSA_EXTRACT_KEY_BLOCKS
|
||||
|
||||
/**
|
||||
* @brief ECDSA private key context initialization config structure
|
||||
* @note Contains configuration information like the efuse key block that should be used as the private key,
|
||||
@@ -50,7 +67,7 @@ typedef struct {
|
||||
*
|
||||
* @param keypair The mbedtls ECP key-pair structure
|
||||
* @param efuse_blk The efuse key block that should be used as the private key.
|
||||
* The key purpose of this block must be ECDSA_KEY
|
||||
* The efuse block where ECDSA key is stored. If two blocks are used to store the key, then the macro MBEDTLS_ECDSA_COMBINE_KEY_BLOCKS() can be used to combine them. The macro is defined in this header file.
|
||||
* @return - 0 if successful
|
||||
* - MBEDTLS_ERR_ECP_BAD_INPUT_DATA if invalid ecp group id specified
|
||||
* - MBEDTLS_ERR_ECP_INVALID_KEY if efuse block with purpose ECDSA_KEY is not found
|
||||
@@ -70,7 +87,7 @@ int esp_ecdsa_load_pubkey(mbedtls_ecp_keypair *keypair, int efuse_blk);
|
||||
* @param key The MPI in which this functions stores the hardware context.
|
||||
* This must be uninitialized
|
||||
* @param efuse_blk The efuse key block that should be used as the private key.
|
||||
* The key purpose of this block must be ECDSA_KEY
|
||||
* The efuse block where ECDSA key is stored. If two blocks are used to store the key, then the macro MBEDTLS_ECDSA_COMBINE_KEY_BLOCKS() can be used to combine them. The macro is defined in this header file.
|
||||
*
|
||||
* @return - 0 if successful
|
||||
* - -1 otherwise
|
||||
@@ -86,7 +103,7 @@ int esp_ecdsa_privkey_load_mpi(mbedtls_mpi *key, int efuse_blk);
|
||||
* @param key_ctx The context in which this functions stores the hardware context.
|
||||
* This must be uninitialized
|
||||
* @param efuse_blk The efuse key block that should be used as the private key.
|
||||
* The key purpose of this block must be ECDSA_KEY
|
||||
* The efuse block where ECDSA key is stored. If two blocks are used to store the key, then the macro MBEDTLS_ECDSA_COMBINE_KEY_BLOCKS() can be used to combine them. The macro is defined in this header file.
|
||||
*
|
||||
* @return - 0 if successful
|
||||
* - -1 otherwise
|
||||
|
6
components/mbedtls/test_apps/ecdsa_key_p384.pem
Normal file
6
components/mbedtls/test_apps/ecdsa_key_p384.pem
Normal file
@@ -0,0 +1,6 @@
|
||||
-----BEGIN EC PRIVATE KEY-----
|
||||
MIGkAgEBBDAlJNNxsYfk5Ljs1S+0A+F0bsbCBOYvEkGgHLe2GX8BZWvF/UcBQ/MC
|
||||
qw2m/lFnDx+gBwYFK4EEACKhZANiAASuwiuzzbIJKY3lHflvXsUZx1txuxUGfudz
|
||||
LZ6X0dM0DAfhk37MxXHli3veGelbkUX37qyRhGShPfNCznxwgfHRTBzEus0gR0hM
|
||||
/KCjgiryPZfgI0t41sLOf4bJ1q6tvyk=
|
||||
-----END EC PRIVATE KEY-----
|
@@ -44,6 +44,22 @@
|
||||
#define NEWLIB_NANO_COMPAT_CAST(int64_t_var) int64_t_var
|
||||
#endif
|
||||
|
||||
#if SOC_ECDSA_SUPPORT_CURVE_P384
|
||||
#define MAX_ECDSA_COMPONENT_LEN 48
|
||||
#define MAX_HASH_LEN 48
|
||||
#else /* !SOC_ECDSA_SUPPORT_CURVE_P384 */
|
||||
#define MAX_ECDSA_COMPONENT_LEN 32
|
||||
#define MAX_HASH_LEN 32
|
||||
#endif /* SOC_ECDSA_SUPPORT_CURVE_P384 */
|
||||
|
||||
#define HASH_LEN 32
|
||||
#if SOC_ECDSA_SUPPORT_CURVE_P384
|
||||
#define HASH_LEN_P384 48
|
||||
#endif /* SOC_ECDSA_SUPPORT_CURVE_P384 */
|
||||
|
||||
#define ECDSA_P192_HASH_COMPONENT_LEN 24
|
||||
#define ECDSA_P256_HASH_COMPONENT_LEN 32
|
||||
|
||||
__attribute__((unused)) static const char * TAG = "mbedtls_test";
|
||||
|
||||
/*
|
||||
@@ -51,52 +67,94 @@ __attribute__((unused)) static const char * TAG = "mbedtls_test";
|
||||
*/
|
||||
|
||||
const 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
|
||||
0x98, 0xca, 0xea, 0x85, 0x7b, 0x03, 0x5e, 0xc0,
|
||||
0xe3, 0xc3, 0x39, 0x29, 0xef, 0xf1, 0xf1, 0x25,
|
||||
0x00, 0x19, 0xe7, 0x11, 0xc3, 0x3d, 0x84, 0x42,
|
||||
0x38, 0x79, 0x10, 0xef, 0xb2, 0x9b, 0xd2, 0x63,
|
||||
0xed, 0xfe, 0x04, 0xce, 0x66, 0x89, 0xd0, 0xa4,
|
||||
0xb2, 0x60, 0xb2, 0x38, 0x93, 0xa6, 0x27, 0x14
|
||||
};
|
||||
|
||||
#if CONFIG_MBEDTLS_HARDWARE_ECC || CONFIG_MBEDTLS_HARDWARE_ECDSA_VERIFY
|
||||
|
||||
/* Big endian */
|
||||
uint8_t ecdsa384_r[] = {
|
||||
0x1a, 0x24, 0xe3, 0xaf, 0x08, 0x08, 0xb6, 0x81,
|
||||
0xe6, 0x18, 0xc6, 0xf8, 0x14, 0x75, 0x7f, 0x04,
|
||||
0x6b, 0x2d, 0x2e, 0xd7, 0xa9, 0xf0, 0x6f, 0xbe,
|
||||
0x9d, 0x3a, 0x20, 0x4a, 0xf0, 0x4b, 0x6a, 0x35,
|
||||
0xfb, 0x5f, 0xa7, 0x22, 0x82, 0x89, 0x0a, 0x59,
|
||||
0xc5, 0x31, 0x36, 0x09, 0x35, 0x80, 0x6e, 0xbb,
|
||||
};
|
||||
|
||||
/* Big endian */
|
||||
uint8_t ecdsa384_s[] = {
|
||||
0x38, 0x81, 0x30, 0x8c, 0x41, 0x94, 0x62, 0xe4,
|
||||
0x6e, 0x36, 0xf7, 0x4e, 0x20, 0xe4, 0xa7, 0x5f,
|
||||
0x47, 0x4e, 0x5f, 0x94, 0xa2, 0x89, 0x7c, 0x71,
|
||||
0x87, 0x90, 0xc2, 0xdc, 0xb9, 0x34, 0x5b, 0x85,
|
||||
0xe9, 0x9b, 0xa9, 0x29, 0x33, 0x3e, 0x12, 0xd8,
|
||||
0x23, 0x3c, 0x7a, 0xcd, 0xa8, 0xf9, 0x61, 0x94,
|
||||
};
|
||||
/* Big endian */
|
||||
const uint8_t ecdsa384_pub_x[] = {
|
||||
0xae, 0xc2, 0x2b, 0xb3, 0xcd, 0xb2, 0x09, 0x29,
|
||||
0x8d, 0xe5, 0x1d, 0xf9, 0x6f, 0x5e, 0xc5, 0x19,
|
||||
0xc7, 0x5b, 0x71, 0xbb, 0x15, 0x06, 0x7e, 0xe7,
|
||||
0x73, 0x2d, 0x9e, 0x97, 0xd1, 0xd3, 0x34, 0x0c,
|
||||
0x07, 0xe1, 0x93, 0x7e, 0xcc, 0xc5, 0x71, 0xe5,
|
||||
0x8b, 0x7b, 0xde, 0x19, 0xe9, 0x5b, 0x91, 0x45,
|
||||
};
|
||||
|
||||
/* Big endian */
|
||||
const uint8_t ecdsa384_pub_y[] = {
|
||||
0xf7, 0xee, 0xac, 0x91, 0x84, 0x64, 0xa1, 0x3d,
|
||||
0xf3, 0x42, 0xce, 0x7c, 0x70, 0x81, 0xf1, 0xd1,
|
||||
0x4c, 0x1c, 0xc4, 0xba, 0xcd, 0x20, 0x47, 0x48,
|
||||
0x4c, 0xfc, 0xa0, 0xa3, 0x82, 0x2a, 0xf2, 0x3d,
|
||||
0x97, 0xe0, 0x23, 0x4b, 0x78, 0xd6, 0xc2, 0xce,
|
||||
0x7f, 0x86, 0xc9, 0xd6, 0xae, 0xad, 0xbf, 0x29,
|
||||
};
|
||||
|
||||
const uint8_t ecdsa256_r[] = {
|
||||
0x26, 0x1a, 0x0f, 0xbd, 0xa5, 0xe5, 0x1e, 0xe7,
|
||||
0xb3, 0xc3, 0xb7, 0x09, 0xd1, 0x4a, 0x7a, 0x2a,
|
||||
0x16, 0x69, 0x4b, 0xaf, 0x76, 0x5c, 0xd4, 0x0e,
|
||||
0x93, 0x57, 0xb8, 0x67, 0xf9, 0xa1, 0xe5, 0xe8
|
||||
0x97, 0x3e, 0xe1, 0x0f, 0x64, 0x24, 0x6c, 0x46,
|
||||
0xb4, 0xe3, 0x27, 0x0b, 0x64, 0x9e, 0x58, 0xfc,
|
||||
0xde, 0x87, 0x8b, 0x28, 0x53, 0xa0, 0xa4, 0x10,
|
||||
0x30, 0xf4, 0x17, 0x49, 0xe5, 0x94, 0xc0, 0x23
|
||||
};
|
||||
|
||||
const uint8_t ecdsa256_s[] = {
|
||||
0x63, 0x59, 0xc0, 0x3b, 0x6a, 0xc2, 0xc4, 0xc4,
|
||||
0xaf, 0x47, 0x5c, 0xe6, 0x6d, 0x43, 0x3b, 0xa7,
|
||||
0x91, 0x51, 0x15, 0x62, 0x7e, 0x46, 0x0e, 0x68,
|
||||
0x84, 0xce, 0x72, 0xa0, 0xd8, 0x8b, 0x69, 0xd5
|
||||
0xb1, 0xae, 0x1b, 0xd5, 0xab, 0x3b, 0x6f, 0x55,
|
||||
0xec, 0x8d, 0xe3, 0x5a, 0x0c, 0x05, 0xf3, 0x86,
|
||||
0x67, 0x98, 0xc9, 0x71, 0x78, 0x75, 0xcb, 0x3b,
|
||||
0x8f, 0x8c, 0xf1, 0x8a, 0x9a, 0xf0, 0x16, 0x98
|
||||
};
|
||||
|
||||
const uint8_t ecdsa256_pub_x[] = {
|
||||
0xcb, 0x59, 0xde, 0x9c, 0xbb, 0x28, 0xaa, 0xac,
|
||||
0x72, 0x06, 0xc3, 0x43, 0x2a, 0x65, 0x82, 0xcc,
|
||||
0x68, 0x01, 0x76, 0x68, 0xfc, 0xec, 0xf5, 0x91,
|
||||
0xd1, 0x9e, 0xbf, 0xcf, 0x67, 0x7d, 0x7d, 0xbe
|
||||
0x4d, 0xdf, 0x64, 0x1f, 0xd8, 0x6e, 0xd4, 0x8b,
|
||||
0xa7, 0xca, 0x04, 0xc7, 0x11, 0xb8, 0x45, 0xda,
|
||||
0x0c, 0xff, 0x5f, 0x7a, 0xce, 0x5a, 0x11, 0xf9,
|
||||
0x95, 0x55, 0x08, 0x26, 0x85, 0x88, 0xe9, 0xa8
|
||||
};
|
||||
|
||||
const uint8_t ecdsa256_pub_y[] = {
|
||||
0x00, 0x66, 0x14, 0x74, 0xe0, 0x06, 0x44, 0x66,
|
||||
0x6f, 0x3b, 0x8c, 0x3b, 0x2d, 0x05, 0xf6, 0xd5,
|
||||
0xb2, 0x5d, 0xe4, 0x85, 0x6c, 0x61, 0x38, 0xc5,
|
||||
0xb1, 0x21, 0xde, 0x2b, 0x44, 0xf5, 0x13, 0x62
|
||||
0x24, 0xb0, 0x4d, 0xcc, 0xbf, 0xce, 0x9a, 0x6e,
|
||||
0xa0, 0x8a, 0xb6, 0x1a, 0x40, 0xf2, 0x71, 0x6a,
|
||||
0x50, 0xa8, 0xfd, 0xaa, 0x2c, 0x80, 0xa0, 0xbc,
|
||||
0x73, 0xa1, 0xe8, 0xec, 0xaf, 0x2c, 0x25, 0x34
|
||||
};
|
||||
|
||||
|
||||
const uint8_t ecdsa192_r[] = {
|
||||
0x2b, 0x8a, 0x18, 0x2f, 0xb2, 0x75, 0x26, 0xb7,
|
||||
0x1c, 0xe1, 0xe2, 0x6d, 0xaa, 0xe7, 0x74, 0x2c,
|
||||
0x42, 0xc8, 0xd5, 0x09, 0x4f, 0xb7, 0xee, 0x9f
|
||||
0x90, 0xc8, 0x29, 0x95, 0x68, 0x29, 0x81, 0xa2,
|
||||
0xef, 0x46, 0x2d, 0x2f, 0x07, 0x20, 0xfc, 0x00,
|
||||
0x87, 0x7e, 0x77, 0xbe, 0x89, 0xbc, 0xd0, 0x9c
|
||||
};
|
||||
|
||||
const uint8_t ecdsa192_s[] = {
|
||||
0x1a, 0x74, 0xb4, 0x5, 0xf4, 0x28, 0xa5, 0xb6,
|
||||
0xce, 0xed, 0xa5, 0xff, 0xa8, 0x60, 0x06, 0x2f,
|
||||
0xf6, 0xeb, 0x24, 0x59, 0x24, 0x30, 0x5b, 0x12
|
||||
0x1e, 0xb1, 0x84, 0xc5, 0xb6, 0x5b, 0x8a, 0x11,
|
||||
0x3b, 0xc1, 0x76, 0xbd, 0xea, 0xb2, 0xd6, 0x57,
|
||||
0x74, 0xb2, 0x0e, 0x9e, 0xfa, 0x2b, 0x46, 0x46
|
||||
};
|
||||
|
||||
const uint8_t ecdsa192_pub_x[] = {
|
||||
@@ -114,6 +172,7 @@ const uint8_t ecdsa192_pub_y[] = {
|
||||
void test_ecdsa_verify(mbedtls_ecp_group_id id, const uint8_t *hash, const uint8_t *r_comp, const uint8_t *s_comp,
|
||||
const uint8_t *pub_x, const uint8_t *pub_y)
|
||||
{
|
||||
size_t hash_len = HASH_LEN;
|
||||
int64_t elapsed_time;
|
||||
mbedtls_mpi r, s;
|
||||
|
||||
@@ -127,15 +186,23 @@ void test_ecdsa_verify(mbedtls_ecp_group_id id, const uint8_t *hash, const uint8
|
||||
size_t plen = mbedtls_mpi_size(&ecdsa_context.MBEDTLS_PRIVATE(grp).P);
|
||||
|
||||
TEST_ASSERT_MBEDTLS_OK(mbedtls_mpi_read_binary(&r, r_comp, plen));
|
||||
|
||||
TEST_ASSERT_MBEDTLS_OK(mbedtls_mpi_read_binary(&s, s_comp, plen));
|
||||
|
||||
TEST_ASSERT_MBEDTLS_OK(mbedtls_mpi_read_binary(&ecdsa_context.MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(X), pub_x, plen));
|
||||
TEST_ASSERT_MBEDTLS_OK(mbedtls_mpi_read_binary(&ecdsa_context.MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(Y), pub_y, plen));
|
||||
TEST_ASSERT_MBEDTLS_OK(mbedtls_mpi_lset(&ecdsa_context.MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(Z), 1));
|
||||
|
||||
if (id == MBEDTLS_ECP_DP_SECP192R1 || id == MBEDTLS_ECP_DP_SECP256R1) {
|
||||
hash_len = HASH_LEN;
|
||||
}
|
||||
#if SOC_ECDSA_SUPPORT_CURVE_P384
|
||||
else if (id == MBEDTLS_ECP_DP_SECP384R1) {
|
||||
hash_len = HASH_LEN_P384;
|
||||
}
|
||||
#endif /* SOC_ECDSA_SUPPORT_CURVE_P384 */
|
||||
|
||||
ccomp_timer_start();
|
||||
TEST_ASSERT_MBEDTLS_OK(mbedtls_ecdsa_verify(&ecdsa_context.MBEDTLS_PRIVATE(grp), hash, 32, &ecdsa_context.MBEDTLS_PRIVATE(Q), &r, &s));
|
||||
TEST_ASSERT_MBEDTLS_OK(mbedtls_ecdsa_verify(&ecdsa_context.MBEDTLS_PRIVATE(grp), hash, hash_len, &ecdsa_context.MBEDTLS_PRIVATE(Q), &r, &s));
|
||||
elapsed_time = ccomp_timer_stop();
|
||||
|
||||
if (id == MBEDTLS_ECP_DP_SECP192R1) {
|
||||
@@ -143,6 +210,11 @@ void test_ecdsa_verify(mbedtls_ecp_group_id id, const uint8_t *hash, const uint8
|
||||
} else if (id == MBEDTLS_ECP_DP_SECP256R1) {
|
||||
TEST_PERFORMANCE_CCOMP_LESS_THAN(ECDSA_P256_VERIFY_OP, "%" NEWLIB_NANO_COMPAT_FORMAT" us", NEWLIB_NANO_COMPAT_CAST(elapsed_time));
|
||||
}
|
||||
#if SOC_ECDSA_SUPPORT_CURVE_P384
|
||||
else if (id == MBEDTLS_ECP_DP_SECP384R1) {
|
||||
TEST_PERFORMANCE_CCOMP_LESS_THAN(ECDSA_P384_VERIFY_OP, "%" NEWLIB_NANO_COMPAT_FORMAT" us", NEWLIB_NANO_COMPAT_CAST(elapsed_time));
|
||||
}
|
||||
#endif
|
||||
|
||||
mbedtls_mpi_free(&r);
|
||||
mbedtls_mpi_free(&s);
|
||||
@@ -161,6 +233,14 @@ TEST_CASE("mbedtls ECDSA signature verification performance on SECP256R1", "[mbe
|
||||
ecdsa256_pub_x, ecdsa256_pub_y);
|
||||
}
|
||||
|
||||
#ifdef SOC_ECDSA_SUPPORT_CURVE_P384
|
||||
TEST_CASE("mbedtls ECDSA signature verification performance on SECP384R1", "[mbedtls]")
|
||||
{
|
||||
test_ecdsa_verify(MBEDTLS_ECP_DP_SECP384R1, sha, ecdsa384_r, ecdsa384_s,
|
||||
ecdsa384_pub_x, ecdsa384_pub_y);
|
||||
}
|
||||
#endif /* SOC_ECDSA_SUPPORT_CURVE_P384 */
|
||||
|
||||
#endif /* CONFIG_MBEDTLS_HARDWARE_ECC */
|
||||
|
||||
#if CONFIG_MBEDTLS_HARDWARE_ECDSA_SIGN
|
||||
@@ -170,12 +250,12 @@ TEST_CASE("mbedtls ECDSA signature verification performance on SECP256R1", "[mbe
|
||||
*
|
||||
* ecdsa_key_p192.pem must be burnt in efuse block 4
|
||||
* ecdsa_key_p256.pem must be burnt in efuse block 5
|
||||
* ecdsa_key_p384.pem must be burnt in efuse block 6 and 7
|
||||
*/
|
||||
#define SECP192R1_EFUSE_BLOCK 4 // EFUSE_BLK_KEY0
|
||||
#define SECP256R1_EFUSE_BLOCK 5 // EFUSE_BLK_KEY1
|
||||
|
||||
#define MAX_ECDSA_COMPONENT_LEN 32
|
||||
#define HASH_LEN 32
|
||||
#define SECP384R1_EFUSE_BLOCK_HIGH 6 // EFUSE_BLK_KEY2
|
||||
#define SECP384R1_EFUSE_BLOCK_LOW 7 // EFUSE_BLK_KEY3
|
||||
|
||||
const uint8_t ecdsa256_sign_pub_x[] = {
|
||||
0xa2, 0x8f, 0x52, 0x60, 0x20, 0x9b, 0x54, 0x3c,
|
||||
@@ -223,6 +303,7 @@ const uint8_t k1_ecdsa192_encrypt[] = {
|
||||
|
||||
void test_ecdsa_sign(mbedtls_ecp_group_id id, const uint8_t *hash, const uint8_t *pub_x, const uint8_t *pub_y, bool is_deterministic, int efuse_key_block)
|
||||
{
|
||||
size_t hash_len = HASH_LEN;
|
||||
uint8_t r_be[MAX_ECDSA_COMPONENT_LEN] = {0};
|
||||
uint8_t s_be[MAX_ECDSA_COMPONENT_LEN] = {0};
|
||||
|
||||
@@ -240,12 +321,26 @@ void test_ecdsa_sign(mbedtls_ecp_group_id id, const uint8_t *hash, const uint8_t
|
||||
} else if (id == MBEDTLS_ECP_DP_SECP256R1) {
|
||||
mbedtls_ecp_group_load(&ecdsa_context.MBEDTLS_PRIVATE(grp), MBEDTLS_ECP_DP_SECP256R1);
|
||||
}
|
||||
#if SOC_ECDSA_SUPPORT_CURVE_P384
|
||||
else if (id == MBEDTLS_ECP_DP_SECP384R1) {
|
||||
mbedtls_ecp_group_load(&ecdsa_context.MBEDTLS_PRIVATE(grp), MBEDTLS_ECP_DP_SECP384R1);
|
||||
}
|
||||
#endif
|
||||
esp_ecdsa_privkey_load_mpi(&key_mpi, efuse_key_block);
|
||||
|
||||
if (id == MBEDTLS_ECP_DP_SECP192R1 || id == MBEDTLS_ECP_DP_SECP256R1) {
|
||||
hash_len = HASH_LEN;
|
||||
}
|
||||
#if SOC_ECDSA_SUPPORT_CURVE_P384
|
||||
else if (id == MBEDTLS_ECP_DP_SECP384R1) {
|
||||
hash_len = HASH_LEN_P384;
|
||||
}
|
||||
#endif /* SOC_ECDSA_SUPPORT_CURVE_P384 */
|
||||
|
||||
if (is_deterministic) {
|
||||
mbedtls_ecdsa_sign_det_ext(&ecdsa_context.MBEDTLS_PRIVATE(grp), &r, &s, &key_mpi, sha, HASH_LEN, 0, NULL, NULL);
|
||||
mbedtls_ecdsa_sign_det_ext(&ecdsa_context.MBEDTLS_PRIVATE(grp), &r, &s, &key_mpi, sha, hash_len, 0, NULL, NULL);
|
||||
} else {
|
||||
mbedtls_ecdsa_sign(&ecdsa_context.MBEDTLS_PRIVATE(grp), &r, &s, &key_mpi, sha, HASH_LEN, NULL, NULL);
|
||||
mbedtls_ecdsa_sign(&ecdsa_context.MBEDTLS_PRIVATE(grp), &r, &s, &key_mpi, sha, hash_len, NULL, NULL);
|
||||
}
|
||||
|
||||
mbedtls_mpi_write_binary(&r, r_be, MAX_ECDSA_COMPONENT_LEN);
|
||||
@@ -253,10 +348,15 @@ void test_ecdsa_sign(mbedtls_ecp_group_id id, const uint8_t *hash, const uint8_t
|
||||
|
||||
if (id == MBEDTLS_ECP_DP_SECP192R1) {
|
||||
// Skip the initial zeroes
|
||||
test_ecdsa_verify(id, sha, &r_be[8], &s_be[8], pub_x, pub_y);
|
||||
test_ecdsa_verify(id, sha, &r_be[MAX_HASH_LEN - ECDSA_P192_HASH_COMPONENT_LEN], &s_be[MAX_HASH_LEN - ECDSA_P192_HASH_COMPONENT_LEN], pub_x, pub_y);
|
||||
} else if (id == MBEDTLS_ECP_DP_SECP256R1) {
|
||||
test_ecdsa_verify(id, sha, &r_be[MAX_HASH_LEN - ECDSA_P256_HASH_COMPONENT_LEN], &s_be[MAX_HASH_LEN - ECDSA_P256_HASH_COMPONENT_LEN], pub_x, pub_y);
|
||||
}
|
||||
#if SOC_ECDSA_SUPPORT_CURVE_P384
|
||||
else if (id == MBEDTLS_ECP_DP_SECP384R1) {
|
||||
test_ecdsa_verify(id, sha, r_be, s_be, pub_x, pub_y);
|
||||
}
|
||||
#endif
|
||||
|
||||
mbedtls_mpi_free(&r);
|
||||
mbedtls_mpi_free(&s);
|
||||
@@ -272,6 +372,15 @@ TEST_CASE("mbedtls ECDSA signature generation on SECP256R1", "[mbedtls][efuse_ke
|
||||
{
|
||||
test_ecdsa_sign(MBEDTLS_ECP_DP_SECP256R1, sha, ecdsa256_sign_pub_x, ecdsa256_sign_pub_y, false, SECP256R1_EFUSE_BLOCK);
|
||||
}
|
||||
#ifdef SOC_ECDSA_SUPPORT_CURVE_P384
|
||||
TEST_CASE("mbedtls ECDSA signature generation on SECP384R1", "[mbedtls][efuse_key]")
|
||||
{
|
||||
uint8_t efuse_key_block = MBEDTLS_ECDSA_COMBINE_KEY_BLOCKS(SECP384R1_EFUSE_BLOCK_HIGH, SECP384R1_EFUSE_BLOCK_LOW);
|
||||
test_ecdsa_sign(MBEDTLS_ECP_DP_SECP384R1, sha, ecdsa384_pub_x, ecdsa384_pub_y, false, efuse_key_block);
|
||||
}
|
||||
#endif /* SOC_ECDSA_SUPPORT_CURVE_P384 */
|
||||
|
||||
|
||||
|
||||
#if SOC_KEY_MANAGER_SUPPORTED
|
||||
static void deploy_key_in_key_manager(const uint8_t *k1_encrypted, esp_key_mgr_key_type_t key_type) {
|
||||
@@ -332,6 +441,14 @@ TEST_CASE("mbedtls ECDSA deterministic signature generation on SECP256R1", "[mbe
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef SOC_ECDSA_SUPPORT_CURVE_P384
|
||||
TEST_CASE("mbedtls ECDSA deterministic signature generation on SECP384R1", "[mbedtls][efuse_key]")
|
||||
{
|
||||
uint8_t efuse_key_block = MBEDTLS_ECDSA_COMBINE_KEY_BLOCKS(SECP384R1_EFUSE_BLOCK_HIGH, SECP384R1_EFUSE_BLOCK_LOW);
|
||||
test_ecdsa_sign(MBEDTLS_ECP_DP_SECP384R1, sha, ecdsa384_pub_x, ecdsa384_pub_y, true, efuse_key_block);
|
||||
}
|
||||
#endif /* SOC_ECDSA_SUPPORT_CURVE_P384 */
|
||||
|
||||
#if SOC_KEY_MANAGER_SUPPORTED
|
||||
TEST_CASE("mbedtls ECDSA deterministic signature generation on SECP192R1", "[mbedtls][key_manager_key]")
|
||||
{
|
||||
@@ -360,8 +477,8 @@ TEST_CASE("mbedtls ECDSA deterministic signature generation on SECP256R1", "[mbe
|
||||
#ifdef SOC_ECDSA_SUPPORT_EXPORT_PUBKEY
|
||||
void test_ecdsa_export_pubkey(mbedtls_ecp_group_id id, const uint8_t *pub_x, const uint8_t *pub_y, int efuse_key_block)
|
||||
{
|
||||
uint8_t export_pub_x[32] = {0};
|
||||
uint8_t export_pub_y[32] = {0};
|
||||
uint8_t export_pub_x[48] = {0};
|
||||
uint8_t export_pub_y[48] = {0};
|
||||
int len = 0;
|
||||
|
||||
esp_ecdsa_pk_conf_t pk_conf = {
|
||||
@@ -380,6 +497,11 @@ void test_ecdsa_export_pubkey(mbedtls_ecp_group_id id, const uint8_t *pub_x, con
|
||||
} else if (id == MBEDTLS_ECP_DP_SECP256R1) {
|
||||
len = 32;
|
||||
}
|
||||
#if SOC_ECDSA_SUPPORT_CURVE_P384
|
||||
else if (id == MBEDTLS_ECP_DP_SECP384R1) {
|
||||
len = 48;
|
||||
}
|
||||
#endif
|
||||
|
||||
mbedtls_pk_context key_ctx;
|
||||
|
||||
@@ -407,6 +529,14 @@ TEST_CASE("mbedtls ECDSA export public key on SECP256R1", "[mbedtls][efuse_key]"
|
||||
test_ecdsa_export_pubkey(MBEDTLS_ECP_DP_SECP256R1, ecdsa256_sign_pub_x, ecdsa256_sign_pub_y, SECP256R1_EFUSE_BLOCK);
|
||||
}
|
||||
|
||||
#ifdef SOC_ECDSA_SUPPORT_CURVE_P384
|
||||
TEST_CASE("mbedtls ECDSA export public key on SECP384R1", "[mbedtls][efuse_key]")
|
||||
{
|
||||
uint8_t efuse_key_block = MBEDTLS_ECDSA_COMBINE_KEY_BLOCKS(SECP384R1_EFUSE_BLOCK_HIGH, SECP384R1_EFUSE_BLOCK_LOW);
|
||||
test_ecdsa_export_pubkey(MBEDTLS_ECP_DP_SECP384R1, ecdsa384_pub_x, ecdsa384_pub_y, efuse_key_block);
|
||||
}
|
||||
#endif /* SOC_ECDSA_SUPPORT_CURVE_P384 */
|
||||
|
||||
#if SOC_KEY_MANAGER_SUPPORTED
|
||||
TEST_CASE("mbedtls ECDSA export public key on SECP192R1", "[mbedtls][key_manager_key]")
|
||||
{
|
||||
|
Reference in New Issue
Block a user