mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-07-30 22:43:08 +03:00
pk: add an alternative function for checking private/public key pairs
Instead of using the legacy mbedtls_ecp_check_pub_priv() function which was based on ECP math, we add a new option named eckey_check_pair_psa() which takes advantage of PSA. Of course, this is available when MBEDTLS_USE_PSA_CRYPTO in enabled. Tests were also fixed accordingly. Signed-off-by: Valerio Setti <valerio.setti@nordicsemi.no>
This commit is contained in:
@ -1296,9 +1296,12 @@ int mbedtls_ecp_write_key(mbedtls_ecp_keypair *key,
|
|||||||
* \return An \c MBEDTLS_ERR_ECP_XXX or an \c MBEDTLS_ERR_MPI_XXX
|
* \return An \c MBEDTLS_ERR_ECP_XXX or an \c MBEDTLS_ERR_MPI_XXX
|
||||||
* error code on calculation failure.
|
* error code on calculation failure.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#if !defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||||
int mbedtls_ecp_check_pub_priv(
|
int mbedtls_ecp_check_pub_priv(
|
||||||
const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv,
|
const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv,
|
||||||
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
|
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
|
||||||
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief This function exports generic key-pair parameters.
|
* \brief This function exports generic key-pair parameters.
|
||||||
|
@ -3316,7 +3316,7 @@ cleanup:
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if !defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||||
/*
|
/*
|
||||||
* Check a public-private key pair
|
* Check a public-private key pair
|
||||||
*/
|
*/
|
||||||
@ -3357,6 +3357,7 @@ cleanup:
|
|||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
#endif /* !MBEDTLS_USE_PSA_CRYPTO */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Export generic key-pair parameters.
|
* Export generic key-pair parameters.
|
||||||
|
@ -1095,13 +1095,92 @@ cleanup:
|
|||||||
}
|
}
|
||||||
#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
|
#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
|
||||||
|
|
||||||
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||||
|
/*
|
||||||
|
* Alternative function used to verify that the EC private/public key pair
|
||||||
|
* is valid using PSA functions instead of ECP ones.
|
||||||
|
* The flow is:
|
||||||
|
* - import the private key "prv" to PSA and export its public part
|
||||||
|
* - write the raw content of public key "pub" to a local buffer
|
||||||
|
* - compare the two buffers
|
||||||
|
*/
|
||||||
|
static int eckey_check_pair_psa(const void *pub, const void *prv)
|
||||||
|
{
|
||||||
|
psa_status_t status;
|
||||||
|
psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
|
||||||
|
mbedtls_ecp_keypair *prv_ctx = (mbedtls_ecp_keypair *) prv;
|
||||||
|
mbedtls_ecp_keypair *pub_ctx = (mbedtls_ecp_keypair *) pub;
|
||||||
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||||
|
uint8_t prv_key_buf[MBEDTLS_PSA_MAX_EC_KEY_PAIR_LENGTH];
|
||||||
|
size_t prv_key_len;
|
||||||
|
uint8_t pub_key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
|
||||||
|
size_t pub_key_len;
|
||||||
|
mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
|
||||||
|
size_t curve_bits;
|
||||||
|
psa_ecc_family_t curve =
|
||||||
|
mbedtls_ecc_group_to_psa(prv_ctx->grp.id, &curve_bits);
|
||||||
|
size_t curve_bytes = PSA_BITS_TO_BYTES(curve_bits);
|
||||||
|
|
||||||
|
psa_set_key_type(&key_attr, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
|
||||||
|
psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_EXPORT);
|
||||||
|
|
||||||
|
ret = mbedtls_mpi_write_binary(&prv_ctx->d, prv_key_buf, curve_bytes);
|
||||||
|
if (ret != 0) {
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
status = psa_import_key(&key_attr, prv_key_buf, curve_bytes, &key_id);
|
||||||
|
if (status != PSA_SUCCESS) {
|
||||||
|
ret = PSA_PK_TO_MBEDTLS_ERR(status);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
mbedtls_platform_zeroize(prv_key_buf, sizeof(prv_key_buf));
|
||||||
|
|
||||||
|
status = psa_export_public_key(key_id, prv_key_buf, sizeof(prv_key_buf),
|
||||||
|
&prv_key_len);
|
||||||
|
if (status != PSA_SUCCESS) {
|
||||||
|
ret = PSA_PK_TO_MBEDTLS_ERR(status);
|
||||||
|
status = psa_destroy_key(key_id);
|
||||||
|
return (status != PSA_SUCCESS) ? PSA_PK_TO_MBEDTLS_ERR(status) : ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
status = psa_destroy_key(key_id);
|
||||||
|
if (status != PSA_SUCCESS) {
|
||||||
|
return PSA_PK_TO_MBEDTLS_ERR(status);
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = mbedtls_ecp_point_write_binary(&pub_ctx->grp, &pub_ctx->Q,
|
||||||
|
MBEDTLS_ECP_PF_UNCOMPRESSED,
|
||||||
|
&pub_key_len, pub_key_buf,
|
||||||
|
sizeof(pub_key_buf));
|
||||||
|
if (ret != 0) {
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (memcmp(prv_key_buf, pub_key_buf, curve_bytes) != 0) {
|
||||||
|
return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||||
|
|
||||||
static int eckey_check_pair(const void *pub, const void *prv,
|
static int eckey_check_pair(const void *pub, const void *prv,
|
||||||
int (*f_rng)(void *, unsigned char *, size_t),
|
int (*f_rng)(void *, unsigned char *, size_t),
|
||||||
void *p_rng)
|
void *p_rng)
|
||||||
{
|
{
|
||||||
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||||
|
(void) f_rng;
|
||||||
|
(void) p_rng;
|
||||||
|
return eckey_check_pair_psa((const mbedtls_ecp_keypair *) pub,
|
||||||
|
(const mbedtls_ecp_keypair *) prv);
|
||||||
|
#else /* MBEDTLS_USE_PSA_CRYPTO */
|
||||||
return mbedtls_ecp_check_pub_priv((const mbedtls_ecp_keypair *) pub,
|
return mbedtls_ecp_check_pub_priv((const mbedtls_ecp_keypair *) pub,
|
||||||
(const mbedtls_ecp_keypair *) prv,
|
(const mbedtls_ecp_keypair *) prv,
|
||||||
f_rng, p_rng);
|
f_rng, p_rng);
|
||||||
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||||
|
return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void *eckey_alloc_wrap(void)
|
static void *eckey_alloc_wrap(void)
|
||||||
|
@ -955,7 +955,7 @@ exit:
|
|||||||
}
|
}
|
||||||
/* END_CASE */
|
/* END_CASE */
|
||||||
|
|
||||||
/* BEGIN_CASE */
|
/* BEGIN_CASE depends_on:!MBEDTLS_USE_PSA_CRYPTO */
|
||||||
void mbedtls_ecp_check_pub_priv(int id_pub, char *Qx_pub, char *Qy_pub,
|
void mbedtls_ecp_check_pub_priv(int id_pub, char *Qx_pub, char *Qy_pub,
|
||||||
int id, char *d, char *Qx, char *Qy,
|
int id, char *d, char *Qx, char *Qy,
|
||||||
int ret)
|
int ret)
|
||||||
|
@ -489,6 +489,15 @@ void mbedtls_pk_check_pair(char *pub_file, char *prv_file, int ret)
|
|||||||
mbedtls_pk_init(&prv);
|
mbedtls_pk_init(&prv);
|
||||||
mbedtls_pk_init(&alt);
|
mbedtls_pk_init(&alt);
|
||||||
|
|
||||||
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||||
|
/* mbedtls_pk_check_pair() returns either PK or ECP error codes depending
|
||||||
|
on MBEDTLS_USE_PSA_CRYPTO so here we dynamically translate between the
|
||||||
|
two */
|
||||||
|
if (ret == MBEDTLS_ERR_ECP_BAD_INPUT_DATA) {
|
||||||
|
ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
TEST_ASSERT(mbedtls_pk_parse_public_keyfile(&pub, pub_file) == 0);
|
TEST_ASSERT(mbedtls_pk_parse_public_keyfile(&pub, pub_file) == 0);
|
||||||
TEST_ASSERT(mbedtls_pk_parse_keyfile(&prv, prv_file, NULL,
|
TEST_ASSERT(mbedtls_pk_parse_keyfile(&prv, prv_file, NULL,
|
||||||
mbedtls_test_rnd_std_rand, NULL)
|
mbedtls_test_rnd_std_rand, NULL)
|
||||||
|
Reference in New Issue
Block a user