mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-04-07 13:22:46 +00:00
Merge pull request #8920 from valeriosetti/issue8919
Generalize some PK functions from MBEDTLS_PSA_CRYPTO_C to MBEDTLS_PSA_CRYPTO_CLIENT
This commit is contained in:
commit
93071cfeec
@ -390,77 +390,6 @@ int mbedtls_pk_setup_opaque(mbedtls_pk_context *ctx,
|
||||
const mbedtls_svc_key_id_t key);
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
#if defined(MBEDTLS_PSA_CRYPTO_C)
|
||||
/**
|
||||
* \brief Create a PK context starting from a key stored in PSA.
|
||||
* This key:
|
||||
* - must be exportable and
|
||||
* - must be an RSA or EC key pair or public key (FFDH is not supported in PK).
|
||||
*
|
||||
* The resulting PK object will be a transparent type:
|
||||
* - #MBEDTLS_PK_RSA for RSA keys or
|
||||
* - #MBEDTLS_PK_ECKEY for EC keys.
|
||||
*
|
||||
* Once this functions returns the PK object will be completely
|
||||
* independent from the original PSA key that it was generated
|
||||
* from.
|
||||
* Calling mbedtls_pk_sign(), mbedtls_pk_verify(),
|
||||
* mbedtls_pk_encrypt(), mbedtls_pk_decrypt() on the resulting
|
||||
* PK context will perform the corresponding algorithm for that
|
||||
* PK context type.
|
||||
* * For ECDSA, the choice of deterministic vs randomized will
|
||||
* be based on the compile-time setting #MBEDTLS_ECDSA_DETERMINISTIC.
|
||||
* * For an RSA key, the output PK context will allow both
|
||||
* encrypt/decrypt and sign/verify regardless of the original
|
||||
* key's policy.
|
||||
* The original key's policy determines the output key's padding
|
||||
* mode: PCKS1 v2.1 is set if the PSA key policy is OAEP or PSS,
|
||||
* otherwise PKCS1 v1.5 is set.
|
||||
*
|
||||
* \param key_id The key identifier of the key stored in PSA.
|
||||
* \param pk The PK context that will be filled. It must be initialized,
|
||||
* but not set up.
|
||||
*
|
||||
* \return 0 on success.
|
||||
* \return #MBEDTLS_ERR_PK_BAD_INPUT_DATA in case the provided input
|
||||
* parameters are not correct.
|
||||
*/
|
||||
int mbedtls_pk_copy_from_psa(mbedtls_svc_key_id_t key_id, mbedtls_pk_context *pk);
|
||||
|
||||
/**
|
||||
* \brief Create a PK context for the public key of a PSA key.
|
||||
*
|
||||
* The key must be an RSA or ECC key. It can be either a
|
||||
* public key or a key pair, and only the public key is copied.
|
||||
* The resulting PK object will be a transparent type:
|
||||
* - #MBEDTLS_PK_RSA for RSA keys or
|
||||
* - #MBEDTLS_PK_ECKEY for EC keys.
|
||||
*
|
||||
* Once this functions returns the PK object will be completely
|
||||
* independent from the original PSA key that it was generated
|
||||
* from.
|
||||
* Calling mbedtls_pk_verify() or
|
||||
* mbedtls_pk_encrypt() on the resulting
|
||||
* PK context will perform the corresponding algorithm for that
|
||||
* PK context type.
|
||||
*
|
||||
* For an RSA key, the output PK context will allow both
|
||||
* encrypt and verify regardless of the original key's policy.
|
||||
* The original key's policy determines the output key's padding
|
||||
* mode: PCKS1 v2.1 is set if the PSA key policy is OAEP or PSS,
|
||||
* otherwise PKCS1 v1.5 is set.
|
||||
*
|
||||
* \param key_id The key identifier of the key stored in PSA.
|
||||
* \param pk The PK context that will be filled. It must be initialized,
|
||||
* but not set up.
|
||||
*
|
||||
* \return 0 on success.
|
||||
* \return MBEDTLS_ERR_PK_BAD_INPUT_DATA in case the provided input
|
||||
* parameters are not correct.
|
||||
*/
|
||||
int mbedtls_pk_copy_public_from_psa(mbedtls_svc_key_id_t key_id, mbedtls_pk_context *pk);
|
||||
#endif /* MBEDTLS_PSA_CRYPTO_C */
|
||||
|
||||
#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
|
||||
/**
|
||||
* \brief Initialize an RSA-alt context
|
||||
@ -529,7 +458,7 @@ int mbedtls_pk_can_do(const mbedtls_pk_context *ctx, mbedtls_pk_type_t type);
|
||||
* PSA_ALG_RSA_PKCS1V15_CRYPT,
|
||||
* PSA_ALG_ECDSA(hash),
|
||||
* PSA_ALG_ECDH, where hash is a specific hash.
|
||||
* \param usage PSA usage flag to check against, must be composed of:
|
||||
* \param usage PSA usage flag to check against, must be composed of:
|
||||
* PSA_KEY_USAGE_SIGN_HASH
|
||||
* PSA_KEY_USAGE_DECRYPT
|
||||
* PSA_KEY_USAGE_DERIVE.
|
||||
@ -550,7 +479,7 @@ int mbedtls_pk_can_do_ext(const mbedtls_pk_context *ctx, psa_algorithm_t alg,
|
||||
psa_key_usage_t usage);
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
#if defined(MBEDTLS_PSA_CRYPTO_C)
|
||||
#if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
|
||||
/**
|
||||
* \brief Determine valid PSA attributes that can be used to
|
||||
* import a key into PSA.
|
||||
@ -710,7 +639,76 @@ int mbedtls_pk_get_psa_attributes(const mbedtls_pk_context *pk,
|
||||
int mbedtls_pk_import_into_psa(const mbedtls_pk_context *pk,
|
||||
const psa_key_attributes_t *attributes,
|
||||
mbedtls_svc_key_id_t *key_id);
|
||||
#endif /* MBEDTLS_PSA_CRYPTO_C */
|
||||
|
||||
/**
|
||||
* \brief Create a PK context starting from a key stored in PSA.
|
||||
* This key:
|
||||
* - must be exportable and
|
||||
* - must be an RSA or EC key pair or public key (FFDH is not supported in PK).
|
||||
*
|
||||
* The resulting PK object will be a transparent type:
|
||||
* - #MBEDTLS_PK_RSA for RSA keys or
|
||||
* - #MBEDTLS_PK_ECKEY for EC keys.
|
||||
*
|
||||
* Once this functions returns the PK object will be completely
|
||||
* independent from the original PSA key that it was generated
|
||||
* from.
|
||||
* Calling mbedtls_pk_sign(), mbedtls_pk_verify(),
|
||||
* mbedtls_pk_encrypt(), mbedtls_pk_decrypt() on the resulting
|
||||
* PK context will perform the corresponding algorithm for that
|
||||
* PK context type.
|
||||
* * For ECDSA, the choice of deterministic vs randomized will
|
||||
* be based on the compile-time setting #MBEDTLS_ECDSA_DETERMINISTIC.
|
||||
* * For an RSA key, the output PK context will allow both
|
||||
* encrypt/decrypt and sign/verify regardless of the original
|
||||
* key's policy.
|
||||
* The original key's policy determines the output key's padding
|
||||
* mode: PCKS1 v2.1 is set if the PSA key policy is OAEP or PSS,
|
||||
* otherwise PKCS1 v1.5 is set.
|
||||
*
|
||||
* \param key_id The key identifier of the key stored in PSA.
|
||||
* \param pk The PK context that will be filled. It must be initialized,
|
||||
* but not set up.
|
||||
*
|
||||
* \return 0 on success.
|
||||
* \return #MBEDTLS_ERR_PK_BAD_INPUT_DATA in case the provided input
|
||||
* parameters are not correct.
|
||||
*/
|
||||
int mbedtls_pk_copy_from_psa(mbedtls_svc_key_id_t key_id, mbedtls_pk_context *pk);
|
||||
|
||||
/**
|
||||
* \brief Create a PK context for the public key of a PSA key.
|
||||
*
|
||||
* The key must be an RSA or ECC key. It can be either a
|
||||
* public key or a key pair, and only the public key is copied.
|
||||
* The resulting PK object will be a transparent type:
|
||||
* - #MBEDTLS_PK_RSA for RSA keys or
|
||||
* - #MBEDTLS_PK_ECKEY for EC keys.
|
||||
*
|
||||
* Once this functions returns the PK object will be completely
|
||||
* independent from the original PSA key that it was generated
|
||||
* from.
|
||||
* Calling mbedtls_pk_verify() or
|
||||
* mbedtls_pk_encrypt() on the resulting
|
||||
* PK context will perform the corresponding algorithm for that
|
||||
* PK context type.
|
||||
*
|
||||
* For an RSA key, the output PK context will allow both
|
||||
* encrypt and verify regardless of the original key's policy.
|
||||
* The original key's policy determines the output key's padding
|
||||
* mode: PCKS1 v2.1 is set if the PSA key policy is OAEP or PSS,
|
||||
* otherwise PKCS1 v1.5 is set.
|
||||
*
|
||||
* \param key_id The key identifier of the key stored in PSA.
|
||||
* \param pk The PK context that will be filled. It must be initialized,
|
||||
* but not set up.
|
||||
*
|
||||
* \return 0 on success.
|
||||
* \return MBEDTLS_ERR_PK_BAD_INPUT_DATA in case the provided input
|
||||
* parameters are not correct.
|
||||
*/
|
||||
int mbedtls_pk_copy_public_from_psa(mbedtls_svc_key_id_t key_id, mbedtls_pk_context *pk);
|
||||
#endif /* MBEDTLS_PSA_CRYPTO_CLIENT */
|
||||
|
||||
/**
|
||||
* \brief Verify signature (including padding if relevant).
|
||||
|
308
library/pk.c
308
library/pk.c
@ -324,14 +324,14 @@ int mbedtls_pk_can_do_ext(const mbedtls_pk_context *ctx, psa_algorithm_t alg,
|
||||
}
|
||||
|
||||
psa_algorithm_t key_alg = psa_get_key_algorithm(&attributes);
|
||||
/* Key's enrollment is available only when MBEDTLS_PSA_CRYPTO_CLIENT is
|
||||
* defined, i.e. when the Mbed TLS implementation of PSA Crypto is being used.
|
||||
/* Key's enrollment is available only when an Mbed TLS implementation of PSA
|
||||
* Crypto is being used, i.e. when MBEDTLS_PSA_CRYPTO_C is defined.
|
||||
* Even though we don't officially support using other implementations of PSA
|
||||
* Crypto with TLS and X.509 (yet), we're still trying to simplify the life of
|
||||
* people who would like to try it before it's officially supported. */
|
||||
#if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
|
||||
* Crypto with TLS and X.509 (yet), we try to keep vendor's customizations
|
||||
* separated. */
|
||||
#if defined(MBEDTLS_PSA_CRYPTO_C)
|
||||
psa_algorithm_t key_alg2 = psa_get_key_enrollment_algorithm(&attributes);
|
||||
#endif /* MBEDTLS_PSA_CRYPTO_CLIENT */
|
||||
#endif /* MBEDTLS_PSA_CRYPTO_C */
|
||||
key_usage = psa_get_key_usage_flags(&attributes);
|
||||
psa_reset_key_attributes(&attributes);
|
||||
|
||||
@ -349,11 +349,11 @@ int mbedtls_pk_can_do_ext(const mbedtls_pk_context *ctx, psa_algorithm_t alg,
|
||||
if (alg == key_alg) {
|
||||
return 1;
|
||||
}
|
||||
#if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
|
||||
#if defined(MBEDTLS_PSA_CRYPTO_C)
|
||||
if (alg == key_alg2) {
|
||||
return 1;
|
||||
}
|
||||
#endif /* MBEDTLS_PSA_CRYPTO_CLIENT */
|
||||
#endif /* MBEDTLS_PSA_CRYPTO_C */
|
||||
|
||||
/*
|
||||
* If key_alg [or key_alg2] is a hash-and-sign with a wildcard for the hash,
|
||||
@ -361,26 +361,25 @@ int mbedtls_pk_can_do_ext(const mbedtls_pk_context *ctx, psa_algorithm_t alg,
|
||||
* then alg is compliant with this key alg
|
||||
*/
|
||||
if (PSA_ALG_IS_SIGN_HASH(alg)) {
|
||||
|
||||
if (PSA_ALG_IS_SIGN_HASH(key_alg) &&
|
||||
PSA_ALG_SIGN_GET_HASH(key_alg) == PSA_ALG_ANY_HASH &&
|
||||
(alg & ~PSA_ALG_HASH_MASK) == (key_alg & ~PSA_ALG_HASH_MASK)) {
|
||||
return 1;
|
||||
}
|
||||
#if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
|
||||
#if defined(MBEDTLS_PSA_CRYPTO_C)
|
||||
if (PSA_ALG_IS_SIGN_HASH(key_alg2) &&
|
||||
PSA_ALG_SIGN_GET_HASH(key_alg2) == PSA_ALG_ANY_HASH &&
|
||||
(alg & ~PSA_ALG_HASH_MASK) == (key_alg2 & ~PSA_ALG_HASH_MASK)) {
|
||||
return 1;
|
||||
}
|
||||
#endif /* MBEDTLS_PSA_CRYPTO_CLIENT */
|
||||
#endif /* MBEDTLS_PSA_CRYPTO_C */
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
#if defined(MBEDTLS_PSA_CRYPTO_C)
|
||||
#if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
|
||||
#if defined(MBEDTLS_RSA_C)
|
||||
static psa_algorithm_t psa_algorithm_for_rsa(const mbedtls_rsa_context *rsa,
|
||||
int want_crypt)
|
||||
@ -577,7 +576,14 @@ int mbedtls_pk_get_psa_attributes(const mbedtls_pk_context *pk,
|
||||
}
|
||||
|
||||
psa_set_key_usage_flags(attributes, more_usage);
|
||||
/* Key's enrollment is available only when an Mbed TLS implementation of PSA
|
||||
* Crypto is being used, i.e. when MBEDTLS_PSA_CRYPTO_C is defined.
|
||||
* Even though we don't officially support using other implementations of PSA
|
||||
* Crypto with TLS and X.509 (yet), we try to keep vendor's customizations
|
||||
* separated. */
|
||||
#if defined(MBEDTLS_PSA_CRYPTO_C)
|
||||
psa_set_key_enrollment_algorithm(attributes, PSA_ALG_NONE);
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -854,7 +860,136 @@ int mbedtls_pk_import_into_psa(const mbedtls_pk_context *pk,
|
||||
return import_pair_into_psa(pk, attributes, key_id);
|
||||
}
|
||||
}
|
||||
#endif /* MBEDTLS_PSA_CRYPTO_C */
|
||||
|
||||
static int copy_from_psa(mbedtls_svc_key_id_t key_id,
|
||||
mbedtls_pk_context *pk,
|
||||
int public_only)
|
||||
{
|
||||
psa_status_t status;
|
||||
psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
|
||||
psa_key_type_t key_type;
|
||||
psa_algorithm_t alg_type;
|
||||
size_t key_bits;
|
||||
/* Use a buffer size large enough to contain either a key pair or public key. */
|
||||
unsigned char exp_key[PSA_EXPORT_KEY_PAIR_OR_PUBLIC_MAX_SIZE];
|
||||
size_t exp_key_len;
|
||||
int ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
|
||||
|
||||
if (pk == NULL) {
|
||||
return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
|
||||
}
|
||||
|
||||
status = psa_get_key_attributes(key_id, &key_attr);
|
||||
if (status != PSA_SUCCESS) {
|
||||
return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
|
||||
}
|
||||
|
||||
if (public_only) {
|
||||
status = psa_export_public_key(key_id, exp_key, sizeof(exp_key), &exp_key_len);
|
||||
} else {
|
||||
status = psa_export_key(key_id, exp_key, sizeof(exp_key), &exp_key_len);
|
||||
}
|
||||
if (status != PSA_SUCCESS) {
|
||||
ret = PSA_PK_TO_MBEDTLS_ERR(status);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
key_type = psa_get_key_type(&key_attr);
|
||||
if (public_only) {
|
||||
key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(key_type);
|
||||
}
|
||||
key_bits = psa_get_key_bits(&key_attr);
|
||||
alg_type = psa_get_key_algorithm(&key_attr);
|
||||
|
||||
#if defined(MBEDTLS_RSA_C)
|
||||
if ((key_type == PSA_KEY_TYPE_RSA_KEY_PAIR) ||
|
||||
(key_type == PSA_KEY_TYPE_RSA_PUBLIC_KEY)) {
|
||||
|
||||
ret = mbedtls_pk_setup(pk, mbedtls_pk_info_from_type(MBEDTLS_PK_RSA));
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if (key_type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
|
||||
ret = mbedtls_rsa_parse_key(mbedtls_pk_rsa(*pk), exp_key, exp_key_len);
|
||||
} else {
|
||||
ret = mbedtls_rsa_parse_pubkey(mbedtls_pk_rsa(*pk), exp_key, exp_key_len);
|
||||
}
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_md_type_t md_type = MBEDTLS_MD_NONE;
|
||||
if (PSA_ALG_GET_HASH(alg_type) != PSA_ALG_ANY_HASH) {
|
||||
md_type = mbedtls_md_type_from_psa_alg(alg_type);
|
||||
}
|
||||
|
||||
if (PSA_ALG_IS_RSA_OAEP(alg_type) || PSA_ALG_IS_RSA_PSS(alg_type)) {
|
||||
ret = mbedtls_rsa_set_padding(mbedtls_pk_rsa(*pk), MBEDTLS_RSA_PKCS_V21, md_type);
|
||||
} else if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg_type) ||
|
||||
alg_type == PSA_ALG_RSA_PKCS1V15_CRYPT) {
|
||||
ret = mbedtls_rsa_set_padding(mbedtls_pk_rsa(*pk), MBEDTLS_RSA_PKCS_V15, md_type);
|
||||
}
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
} else
|
||||
#endif /* MBEDTLS_RSA_C */
|
||||
#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
|
||||
if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ||
|
||||
PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(key_type)) {
|
||||
mbedtls_ecp_group_id grp_id;
|
||||
|
||||
ret = mbedtls_pk_setup(pk, mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY));
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
grp_id = mbedtls_ecc_group_from_psa(PSA_KEY_TYPE_ECC_GET_FAMILY(key_type), key_bits);
|
||||
ret = mbedtls_pk_ecc_set_group(pk, grp_id);
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type)) {
|
||||
ret = mbedtls_pk_ecc_set_key(pk, exp_key, exp_key_len);
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
ret = mbedtls_pk_ecc_set_pubkey_from_prv(pk, exp_key, exp_key_len,
|
||||
mbedtls_psa_get_random,
|
||||
MBEDTLS_PSA_RANDOM_STATE);
|
||||
} else {
|
||||
ret = mbedtls_pk_ecc_set_pubkey(pk, exp_key, exp_key_len);
|
||||
}
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
} else
|
||||
#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
|
||||
{
|
||||
return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
|
||||
}
|
||||
|
||||
exit:
|
||||
psa_reset_key_attributes(&key_attr);
|
||||
mbedtls_platform_zeroize(exp_key, sizeof(exp_key));
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int mbedtls_pk_copy_from_psa(mbedtls_svc_key_id_t key_id,
|
||||
mbedtls_pk_context *pk)
|
||||
{
|
||||
return copy_from_psa(key_id, pk, 0);
|
||||
}
|
||||
|
||||
int mbedtls_pk_copy_public_from_psa(mbedtls_svc_key_id_t key_id,
|
||||
mbedtls_pk_context *pk)
|
||||
{
|
||||
return copy_from_psa(key_id, pk, 1);
|
||||
}
|
||||
#endif /* MBEDTLS_PSA_CRYPTO_CLIENT */
|
||||
|
||||
/*
|
||||
* Helper for mbedtls_pk_sign and mbedtls_pk_verify
|
||||
@ -1187,7 +1322,10 @@ int mbedtls_pk_sign_ext(mbedtls_pk_type_t pk_type,
|
||||
|
||||
if (mbedtls_pk_get_type(ctx) == MBEDTLS_PK_OPAQUE) {
|
||||
psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
|
||||
psa_algorithm_t psa_alg, psa_enrollment_alg, sign_alg;
|
||||
psa_algorithm_t psa_alg, sign_alg;
|
||||
#if defined(MBEDTLS_PSA_CRYPTO_C)
|
||||
psa_algorithm_t psa_enrollment_alg;
|
||||
#endif /* MBEDTLS_PSA_CRYPTO_C */
|
||||
psa_status_t status;
|
||||
|
||||
status = psa_get_key_attributes(ctx->priv_id, &key_attr);
|
||||
@ -1195,16 +1333,22 @@ int mbedtls_pk_sign_ext(mbedtls_pk_type_t pk_type,
|
||||
return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
|
||||
}
|
||||
psa_alg = psa_get_key_algorithm(&key_attr);
|
||||
#if defined(MBEDTLS_PSA_CRYPTO_C)
|
||||
psa_enrollment_alg = psa_get_key_enrollment_algorithm(&key_attr);
|
||||
#endif /* MBEDTLS_PSA_CRYPTO_C */
|
||||
psa_reset_key_attributes(&key_attr);
|
||||
|
||||
/* Since we're PK type is MBEDTLS_PK_RSASSA_PSS at least one between
|
||||
* alg and enrollment alg should be of type RSA_PSS. */
|
||||
if (PSA_ALG_IS_RSA_PSS(psa_alg)) {
|
||||
sign_alg = psa_alg;
|
||||
} else if (PSA_ALG_IS_RSA_PSS(psa_enrollment_alg)) {
|
||||
}
|
||||
#if defined(MBEDTLS_PSA_CRYPTO_C)
|
||||
else if (PSA_ALG_IS_RSA_PSS(psa_enrollment_alg)) {
|
||||
sign_alg = psa_enrollment_alg;
|
||||
} else {
|
||||
}
|
||||
#endif /* MBEDTLS_PSA_CRYPTO_C */
|
||||
else {
|
||||
/* The opaque key has no RSA PSS algorithm associated. */
|
||||
return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
|
||||
}
|
||||
@ -1378,136 +1522,4 @@ mbedtls_pk_type_t mbedtls_pk_get_type(const mbedtls_pk_context *ctx)
|
||||
return ctx->pk_info->type;
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_PSA_CRYPTO_C)
|
||||
static int copy_from_psa(mbedtls_svc_key_id_t key_id,
|
||||
mbedtls_pk_context *pk,
|
||||
int public_only)
|
||||
{
|
||||
psa_status_t status;
|
||||
psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
|
||||
psa_key_type_t key_type;
|
||||
psa_algorithm_t alg_type;
|
||||
size_t key_bits;
|
||||
/* Use a buffer size large enough to contain either a key pair or public key. */
|
||||
unsigned char exp_key[PSA_EXPORT_KEY_PAIR_OR_PUBLIC_MAX_SIZE];
|
||||
size_t exp_key_len;
|
||||
int ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
|
||||
|
||||
if (pk == NULL) {
|
||||
return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
|
||||
}
|
||||
|
||||
status = psa_get_key_attributes(key_id, &key_attr);
|
||||
if (status != PSA_SUCCESS) {
|
||||
return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
|
||||
}
|
||||
|
||||
if (public_only) {
|
||||
status = psa_export_public_key(key_id, exp_key, sizeof(exp_key), &exp_key_len);
|
||||
} else {
|
||||
status = psa_export_key(key_id, exp_key, sizeof(exp_key), &exp_key_len);
|
||||
}
|
||||
if (status != PSA_SUCCESS) {
|
||||
ret = PSA_PK_TO_MBEDTLS_ERR(status);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
key_type = psa_get_key_type(&key_attr);
|
||||
if (public_only) {
|
||||
key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(key_type);
|
||||
}
|
||||
key_bits = psa_get_key_bits(&key_attr);
|
||||
alg_type = psa_get_key_algorithm(&key_attr);
|
||||
|
||||
#if defined(MBEDTLS_RSA_C)
|
||||
if ((key_type == PSA_KEY_TYPE_RSA_KEY_PAIR) ||
|
||||
(key_type == PSA_KEY_TYPE_RSA_PUBLIC_KEY)) {
|
||||
|
||||
ret = mbedtls_pk_setup(pk, mbedtls_pk_info_from_type(MBEDTLS_PK_RSA));
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if (key_type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
|
||||
ret = mbedtls_rsa_parse_key(mbedtls_pk_rsa(*pk), exp_key, exp_key_len);
|
||||
} else {
|
||||
ret = mbedtls_rsa_parse_pubkey(mbedtls_pk_rsa(*pk), exp_key, exp_key_len);
|
||||
}
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_md_type_t md_type = MBEDTLS_MD_NONE;
|
||||
if (PSA_ALG_GET_HASH(alg_type) != PSA_ALG_ANY_HASH) {
|
||||
md_type = mbedtls_md_type_from_psa_alg(alg_type);
|
||||
}
|
||||
|
||||
if (PSA_ALG_IS_RSA_OAEP(alg_type) || PSA_ALG_IS_RSA_PSS(alg_type)) {
|
||||
ret = mbedtls_rsa_set_padding(mbedtls_pk_rsa(*pk), MBEDTLS_RSA_PKCS_V21, md_type);
|
||||
} else if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg_type) ||
|
||||
alg_type == PSA_ALG_RSA_PKCS1V15_CRYPT) {
|
||||
ret = mbedtls_rsa_set_padding(mbedtls_pk_rsa(*pk), MBEDTLS_RSA_PKCS_V15, md_type);
|
||||
}
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
} else
|
||||
#endif /* MBEDTLS_RSA_C */
|
||||
#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
|
||||
if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ||
|
||||
PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(key_type)) {
|
||||
mbedtls_ecp_group_id grp_id;
|
||||
|
||||
ret = mbedtls_pk_setup(pk, mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY));
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
grp_id = mbedtls_ecc_group_from_psa(PSA_KEY_TYPE_ECC_GET_FAMILY(key_type), key_bits);
|
||||
ret = mbedtls_pk_ecc_set_group(pk, grp_id);
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type)) {
|
||||
ret = mbedtls_pk_ecc_set_key(pk, exp_key, exp_key_len);
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
ret = mbedtls_pk_ecc_set_pubkey_from_prv(pk, exp_key, exp_key_len,
|
||||
mbedtls_psa_get_random,
|
||||
MBEDTLS_PSA_RANDOM_STATE);
|
||||
} else {
|
||||
ret = mbedtls_pk_ecc_set_pubkey(pk, exp_key, exp_key_len);
|
||||
}
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
} else
|
||||
#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
|
||||
{
|
||||
return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
|
||||
}
|
||||
|
||||
exit:
|
||||
psa_reset_key_attributes(&key_attr);
|
||||
mbedtls_platform_zeroize(exp_key, sizeof(exp_key));
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
int mbedtls_pk_copy_from_psa(mbedtls_svc_key_id_t key_id,
|
||||
mbedtls_pk_context *pk)
|
||||
{
|
||||
return copy_from_psa(key_id, pk, 0);
|
||||
}
|
||||
|
||||
int mbedtls_pk_copy_public_from_psa(mbedtls_svc_key_id_t key_id,
|
||||
mbedtls_pk_context *pk)
|
||||
{
|
||||
return copy_from_psa(key_id, pk, 1);
|
||||
}
|
||||
#endif /* MBEDTLS_PSA_CRYPTO_C */
|
||||
|
||||
#endif /* MBEDTLS_PK_C */
|
||||
|
@ -1285,19 +1285,67 @@ component_build_psa_crypto_spm () {
|
||||
check_renamed_symbols tests/include/spe/crypto_spe.h library/libmbedcrypto.a
|
||||
}
|
||||
|
||||
component_test_psa_crypto_client () {
|
||||
msg "build: default config - PSA_CRYPTO_C + PSA_CRYPTO_CLIENT, make"
|
||||
# Get a list of library-wise undefined symbols and ensure that they only
|
||||
# belong to psa_xxx() functions and not to mbedtls_yyy() ones.
|
||||
# This function is a common helper used by both:
|
||||
# - component_test_default_psa_crypto_client_without_crypto_provider
|
||||
# - component_build_full_psa_crypto_client_without_crypto_provider.
|
||||
common_check_mbedtls_missing_symbols() {
|
||||
nm library/libmbedcrypto.a | grep ' [TRrDC] ' | grep -Eo '(mbedtls_|psa_).*' | sort -u > sym_def.txt
|
||||
nm library/libmbedcrypto.a | grep ' U ' | grep -Eo '(mbedtls_|psa_).*' | sort -u > sym_undef.txt
|
||||
comm sym_def.txt sym_undef.txt -13 > linking_errors.txt
|
||||
not grep mbedtls_ linking_errors.txt
|
||||
|
||||
rm sym_def.txt sym_undef.txt linking_errors.txt
|
||||
}
|
||||
|
||||
component_test_default_psa_crypto_client_without_crypto_provider () {
|
||||
msg "build: default config - PSA_CRYPTO_C + PSA_CRYPTO_CLIENT"
|
||||
|
||||
scripts/config.py unset MBEDTLS_PSA_CRYPTO_C
|
||||
scripts/config.py unset MBEDTLS_PSA_CRYPTO_STORAGE_C
|
||||
scripts/config.py unset MBEDTLS_PSA_ITS_FILE_C
|
||||
scripts/config.py set MBEDTLS_PSA_CRYPTO_CLIENT
|
||||
scripts/config.py unset MBEDTLS_LMS_C
|
||||
scripts/config.py unset MBEDTLS_LMS_PRIVATE
|
||||
|
||||
make
|
||||
|
||||
msg "test: default config - PSA_CRYPTO_C + PSA_CRYPTO_CLIENT, make"
|
||||
msg "check missing symbols: default config - PSA_CRYPTO_C + PSA_CRYPTO_CLIENT"
|
||||
common_check_mbedtls_missing_symbols
|
||||
|
||||
msg "test: default config - PSA_CRYPTO_C + PSA_CRYPTO_CLIENT"
|
||||
make test
|
||||
}
|
||||
|
||||
component_build_full_psa_crypto_client_without_crypto_provider () {
|
||||
msg "build: full config - PSA_CRYPTO_C"
|
||||
|
||||
# Use full config which includes USE_PSA and CRYPTO_CLIENT.
|
||||
scripts/config.py full
|
||||
|
||||
scripts/config.py unset MBEDTLS_PSA_CRYPTO_C
|
||||
scripts/config.py unset MBEDTLS_PSA_CRYPTO_STORAGE_C
|
||||
# Dynamic secure element support is a deprecated feature and it is not
|
||||
# available when CRYPTO_C and PSA_CRYPTO_STORAGE_C are disabled.
|
||||
scripts/config.py unset MBEDTLS_PSA_CRYPTO_SE_C
|
||||
|
||||
# Since there is no crypto provider in this build it is not possible to
|
||||
# build all the test executables and progrems due to missing PSA functions
|
||||
# at link time. Therefore we will just build libraries and we'll check
|
||||
# that symbols of interest are there.
|
||||
make lib
|
||||
|
||||
msg "check missing symbols: full config - PSA_CRYPTO_C"
|
||||
|
||||
common_check_mbedtls_missing_symbols
|
||||
|
||||
# Ensure that desired functions are included into the build (extend the
|
||||
# following list as required).
|
||||
grep mbedtls_pk_get_psa_attributes library/libmbedcrypto.a
|
||||
grep mbedtls_pk_import_into_psa library/libmbedcrypto.a
|
||||
grep mbedtls_pk_copy_from_psa library/libmbedcrypto.a
|
||||
}
|
||||
|
||||
component_test_psa_crypto_rsa_no_genprime() {
|
||||
msg "build: default config minus MBEDTLS_GENPRIME"
|
||||
scripts/config.py unset MBEDTLS_GENPRIME
|
||||
|
@ -22,4 +22,54 @@ psa_status_t psa_generate_random(uint8_t *output,
|
||||
return PSA_ERROR_COMMUNICATION_FAILURE;
|
||||
}
|
||||
|
||||
psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
|
||||
uint8_t *data,
|
||||
size_t data_size,
|
||||
size_t *data_length)
|
||||
{
|
||||
(void) key;
|
||||
(void) data;
|
||||
(void) data_size;
|
||||
(void) data_length;
|
||||
return PSA_ERROR_COMMUNICATION_FAILURE;
|
||||
}
|
||||
|
||||
psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
|
||||
uint8_t *data,
|
||||
size_t data_size,
|
||||
size_t *data_length)
|
||||
{
|
||||
(void) key;
|
||||
(void) data;
|
||||
(void) data_size;
|
||||
(void) data_length;
|
||||
return PSA_ERROR_COMMUNICATION_FAILURE;
|
||||
}
|
||||
|
||||
psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
|
||||
psa_key_attributes_t *attributes)
|
||||
{
|
||||
(void) key;
|
||||
(void) attributes;
|
||||
return PSA_ERROR_COMMUNICATION_FAILURE;
|
||||
}
|
||||
|
||||
psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
|
||||
{
|
||||
(void) operation;
|
||||
return PSA_ERROR_COMMUNICATION_FAILURE;
|
||||
}
|
||||
|
||||
psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
|
||||
const uint8_t *data,
|
||||
size_t data_length,
|
||||
mbedtls_svc_key_id_t *key)
|
||||
{
|
||||
(void) attributes;
|
||||
(void) data;
|
||||
(void) data_length;
|
||||
(void) key;
|
||||
return PSA_ERROR_COMMUNICATION_FAILURE;
|
||||
}
|
||||
|
||||
#endif /* MBEDTLS_PSA_CRYPTO_CLIENT && !MBEDTLS_PSA_CRYPTO_C */
|
||||
|
Loading…
x
Reference in New Issue
Block a user