From b6891b13f6d4156093a21dadfc2220fdf49f47f2 Mon Sep 17 00:00:00 2001 From: Valerio Setti Date: Mon, 3 Apr 2023 16:01:47 +0200 Subject: [PATCH 1/6] pk: add alternate function for keypair generation using PSA Instead of using the legacy mbedtls_ecp_gen_keypair() which makes use of ECP's math, when USE_PSA_CRYPTO is enabled then the new function pk_genkey_ec() is used in test_suite_pk. Signed-off-by: Valerio Setti --- include/mbedtls/ecp.h | 2 + library/ecp.c | 2 + tests/suites/test_suite_ecp.function | 4 +- tests/suites/test_suite_pk.function | 74 ++++++++++++++++++++++++++-- 4 files changed, 76 insertions(+), 6 deletions(-) diff --git a/include/mbedtls/ecp.h b/include/mbedtls/ecp.h index b6144d9aebdc..d09b5f527f46 100644 --- a/include/mbedtls/ecp.h +++ b/include/mbedtls/ecp.h @@ -1214,6 +1214,7 @@ int mbedtls_ecp_gen_keypair_base(mbedtls_ecp_group *grp, * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code * on failure. */ +#if !defined(MBEDTLS_USE_PSA_CRYPTO) int mbedtls_ecp_gen_keypair(mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q, int (*f_rng)(void *, unsigned char *, size_t), @@ -1235,6 +1236,7 @@ int mbedtls_ecp_gen_keypair(mbedtls_ecp_group *grp, mbedtls_mpi *d, int mbedtls_ecp_gen_key(mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key, int (*f_rng)(void *, unsigned char *, size_t), void *p_rng); +#endif /* !MBEDTLS_USE_PSA_CRYPTO */ /** * \brief This function reads an elliptic curve private key. diff --git a/library/ecp.c b/library/ecp.c index 08fbe86c732a..50058af6ef06 100644 --- a/library/ecp.c +++ b/library/ecp.c @@ -3159,6 +3159,7 @@ int mbedtls_ecp_gen_privkey(const mbedtls_ecp_group *grp, return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; } +#if !defined(MBEDTLS_USE_PSA_CRYPTO) /* * Generate a keypair with configurable base point */ @@ -3200,6 +3201,7 @@ int mbedtls_ecp_gen_key(mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key, return mbedtls_ecp_gen_keypair(&key->grp, &key->d, &key->Q, f_rng, p_rng); } +#endif /* !MBEDTLS_USE_PSA_CRYPTO */ #define ECP_CURVE25519_KEY_SIZE 32 #define ECP_CURVE448_KEY_SIZE 56 diff --git a/tests/suites/test_suite_ecp.function b/tests/suites/test_suite_ecp.function index 4b51a9fb9d39..4a9bc53519f7 100644 --- a/tests/suites/test_suite_ecp.function +++ b/tests/suites/test_suite_ecp.function @@ -987,7 +987,7 @@ exit: } /* END_CASE */ -/* BEGIN_CASE */ +/* BEGIN_CASE depends_on:!MBEDTLS_USE_PSA_CRYPTO */ void mbedtls_ecp_gen_keypair(int id) { mbedtls_ecp_group grp; @@ -1016,7 +1016,7 @@ exit: } /* END_CASE */ -/* BEGIN_CASE */ +/* BEGIN_CASE depends_on:!MBEDTLS_USE_PSA_CRYPTO */ void mbedtls_ecp_gen_key(int id) { mbedtls_ecp_keypair key; diff --git a/tests/suites/test_suite_pk.function b/tests/suites/test_suite_pk.function index de531d32e734..f82eeb8a2254 100644 --- a/tests/suites/test_suite_pk.function +++ b/tests/suites/test_suite_pk.function @@ -17,9 +17,63 @@ * unconditionally (https://github.com/Mbed-TLS/mbedtls/issues/2023). */ #include "psa/crypto.h" +#if defined(MBEDTLS_USE_PSA_CRYPTO) +#include "mbedtls/psa_util.h" +#endif + #define RSA_KEY_SIZE 512 #define RSA_KEY_LEN 64 +static int pk_genkey_ec(mbedtls_ecp_group *grp, + mbedtls_mpi *d, mbedtls_ecp_point *Q) +{ + psa_status_t status; + psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT; + 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(grp->id, + &curve_bits); + unsigned char key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH]; + size_t key_len; + int ret; + + psa_set_key_type(&key_attr, PSA_KEY_TYPE_ECC_KEY_PAIR(curve)); + psa_set_key_bits(&key_attr, curve_bits); + psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_EXPORT); + + status = psa_generate_key(&key_attr, &key_id); + if (status != PSA_SUCCESS) { + return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; + } + + status = psa_export_key(key_id, key_buf, sizeof(key_buf), &key_len); + if (status != PSA_SUCCESS) { + psa_destroy_key(key_id); + return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; + } + + ret = mbedtls_mpi_read_binary(d, key_buf, key_len); + if (ret != 0) { + return ret; + } + + status = psa_export_public_key(key_id, key_buf, sizeof(key_buf), + &key_len); + if (status != PSA_SUCCESS) { + psa_destroy_key(key_id); + return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; + } + + ret = mbedtls_ecp_point_read_binary(grp, Q, key_buf, key_len); + if (ret != 0) { + return ret; + } + + psa_destroy_key(key_id); + + return 0; +} + /** Generate a key of the desired type. * * \param pk The PK object to fill. It must have been initialized @@ -53,12 +107,18 @@ static int pk_genkey(mbedtls_pk_context *pk, int parameter) return ret; } +#if defined(MBEDTLS_USE_PSA_CRYPTO) + return pk_genkey_ec(&mbedtls_pk_ec(*pk)->grp, + &mbedtls_pk_ec(*pk)->d, + &mbedtls_pk_ec(*pk)->Q); +#else /* MBEDTLS_USE_PSA_CRYPTO */ return mbedtls_ecp_gen_keypair(&mbedtls_pk_ec(*pk)->grp, &mbedtls_pk_ec(*pk)->d, &mbedtls_pk_ec(*pk)->Q, mbedtls_test_rnd_std_rand, NULL); +#endif /* MBEDTLS_USE_PSA_CRYPTO */ } -#endif +#endif /* MBEDTLS_ECP_C */ return -1; } @@ -462,6 +522,10 @@ void pk_utils(int type, int parameter, int bitlen, int len, char *name) { mbedtls_pk_context pk; +#if defined(MBEDTLS_USE_PSA_CRYPTO) + PSA_INIT(); +#endif + mbedtls_pk_init(&pk); TEST_ASSERT(mbedtls_pk_setup(&pk, mbedtls_pk_info_from_type(type)) == 0); @@ -475,6 +539,9 @@ void pk_utils(int type, int parameter, int bitlen, int len, char *name) exit: mbedtls_pk_free(&pk); +#if defined(MBEDTLS_USE_PSA_CRYPTO) + PSA_DONE(); +#endif } /* END_CASE */ @@ -1234,9 +1301,8 @@ void pk_psa_sign(int parameter_arg, mbedtls_pk_init(&pk); TEST_ASSERT(mbedtls_pk_setup(&pk, mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY)) == 0); - TEST_ASSERT(mbedtls_ecp_gen_key(grpid, - (mbedtls_ecp_keypair *) pk.pk_ctx, - mbedtls_test_rnd_std_rand, NULL) == 0); + TEST_ASSERT(pk_genkey(&pk, grpid) == 0); + alg_psa = PSA_ALG_ECDSA(PSA_ALG_SHA_256); } else #endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */ From 37194b97f8d61de5d10c24f00613c4260e8dd854 Mon Sep 17 00:00:00 2001 From: Valerio Setti Date: Mon, 3 Apr 2023 16:52:52 +0200 Subject: [PATCH 2/6] gen_key: limit EC key generation to when USE_PSA is disabled Signed-off-by: Valerio Setti --- programs/pkey/gen_key.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/programs/pkey/gen_key.c b/programs/pkey/gen_key.c index 029558d81753..57b7d400092a 100644 --- a/programs/pkey/gen_key.c +++ b/programs/pkey/gen_key.c @@ -321,7 +321,7 @@ int main(int argc, char *argv[]) } } else #endif /* MBEDTLS_RSA_C */ -#if defined(MBEDTLS_ECP_C) +#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_USE_PSA_CRYPTO) if (opt.type == MBEDTLS_PK_ECKEY) { ret = mbedtls_ecp_gen_key((mbedtls_ecp_group_id) opt.ec_curve, mbedtls_pk_ec(key), From 0b304421d8ebd57b8c1a6ae06fb9e5b6b0a23e8b Mon Sep 17 00:00:00 2001 From: Valerio Setti Date: Tue, 4 Apr 2023 11:10:18 +0200 Subject: [PATCH 3/6] ecp: revert changes to ECP module and related tests/programs Signed-off-by: Valerio Setti --- include/mbedtls/ecp.h | 2 -- library/ecp.c | 2 -- programs/pkey/gen_key.c | 2 +- tests/suites/test_suite_ecp.function | 4 ++-- 4 files changed, 3 insertions(+), 7 deletions(-) diff --git a/include/mbedtls/ecp.h b/include/mbedtls/ecp.h index d09b5f527f46..b6144d9aebdc 100644 --- a/include/mbedtls/ecp.h +++ b/include/mbedtls/ecp.h @@ -1214,7 +1214,6 @@ int mbedtls_ecp_gen_keypair_base(mbedtls_ecp_group *grp, * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code * on failure. */ -#if !defined(MBEDTLS_USE_PSA_CRYPTO) int mbedtls_ecp_gen_keypair(mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q, int (*f_rng)(void *, unsigned char *, size_t), @@ -1236,7 +1235,6 @@ int mbedtls_ecp_gen_keypair(mbedtls_ecp_group *grp, mbedtls_mpi *d, int mbedtls_ecp_gen_key(mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key, int (*f_rng)(void *, unsigned char *, size_t), void *p_rng); -#endif /* !MBEDTLS_USE_PSA_CRYPTO */ /** * \brief This function reads an elliptic curve private key. diff --git a/library/ecp.c b/library/ecp.c index 50058af6ef06..08fbe86c732a 100644 --- a/library/ecp.c +++ b/library/ecp.c @@ -3159,7 +3159,6 @@ int mbedtls_ecp_gen_privkey(const mbedtls_ecp_group *grp, return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; } -#if !defined(MBEDTLS_USE_PSA_CRYPTO) /* * Generate a keypair with configurable base point */ @@ -3201,7 +3200,6 @@ int mbedtls_ecp_gen_key(mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key, return mbedtls_ecp_gen_keypair(&key->grp, &key->d, &key->Q, f_rng, p_rng); } -#endif /* !MBEDTLS_USE_PSA_CRYPTO */ #define ECP_CURVE25519_KEY_SIZE 32 #define ECP_CURVE448_KEY_SIZE 56 diff --git a/programs/pkey/gen_key.c b/programs/pkey/gen_key.c index 57b7d400092a..029558d81753 100644 --- a/programs/pkey/gen_key.c +++ b/programs/pkey/gen_key.c @@ -321,7 +321,7 @@ int main(int argc, char *argv[]) } } else #endif /* MBEDTLS_RSA_C */ -#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_USE_PSA_CRYPTO) +#if defined(MBEDTLS_ECP_C) if (opt.type == MBEDTLS_PK_ECKEY) { ret = mbedtls_ecp_gen_key((mbedtls_ecp_group_id) opt.ec_curve, mbedtls_pk_ec(key), diff --git a/tests/suites/test_suite_ecp.function b/tests/suites/test_suite_ecp.function index 4a9bc53519f7..4b51a9fb9d39 100644 --- a/tests/suites/test_suite_ecp.function +++ b/tests/suites/test_suite_ecp.function @@ -987,7 +987,7 @@ exit: } /* END_CASE */ -/* BEGIN_CASE depends_on:!MBEDTLS_USE_PSA_CRYPTO */ +/* BEGIN_CASE */ void mbedtls_ecp_gen_keypair(int id) { mbedtls_ecp_group grp; @@ -1016,7 +1016,7 @@ exit: } /* END_CASE */ -/* BEGIN_CASE depends_on:!MBEDTLS_USE_PSA_CRYPTO */ +/* BEGIN_CASE */ void mbedtls_ecp_gen_key(int id) { mbedtls_ecp_keypair key; From 12a063abb75999605a1f2b3c2aba3eee8cd10559 Mon Sep 17 00:00:00 2001 From: Valerio Setti Date: Tue, 4 Apr 2023 11:13:06 +0200 Subject: [PATCH 4/6] test: use proper macros for PSA init/done Signed-off-by: Valerio Setti --- tests/suites/test_suite_pk.function | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/tests/suites/test_suite_pk.function b/tests/suites/test_suite_pk.function index f82eeb8a2254..f09cc94c424e 100644 --- a/tests/suites/test_suite_pk.function +++ b/tests/suites/test_suite_pk.function @@ -522,10 +522,7 @@ void pk_utils(int type, int parameter, int bitlen, int len, char *name) { mbedtls_pk_context pk; -#if defined(MBEDTLS_USE_PSA_CRYPTO) - PSA_INIT(); -#endif - + USE_PSA_INIT(); mbedtls_pk_init(&pk); TEST_ASSERT(mbedtls_pk_setup(&pk, mbedtls_pk_info_from_type(type)) == 0); @@ -539,9 +536,7 @@ void pk_utils(int type, int parameter, int bitlen, int len, char *name) exit: mbedtls_pk_free(&pk); -#if defined(MBEDTLS_USE_PSA_CRYPTO) - PSA_DONE(); -#endif + USE_PSA_DONE(); } /* END_CASE */ From b3f20da31382568f99a4f5c08239bf00b985c617 Mon Sep 17 00:00:00 2001 From: Valerio Setti Date: Tue, 4 Apr 2023 11:19:33 +0200 Subject: [PATCH 5/6] test: fix error handling in the new pk_genkey_ec() function Signed-off-by: Valerio Setti --- tests/suites/test_suite_pk.function | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/tests/suites/test_suite_pk.function b/tests/suites/test_suite_pk.function index f09cc94c424e..2e374fe85f14 100644 --- a/tests/suites/test_suite_pk.function +++ b/tests/suites/test_suite_pk.function @@ -48,30 +48,28 @@ static int pk_genkey_ec(mbedtls_ecp_group *grp, status = psa_export_key(key_id, key_buf, sizeof(key_buf), &key_len); if (status != PSA_SUCCESS) { - psa_destroy_key(key_id); - return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; + ret = MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; + goto exit; } ret = mbedtls_mpi_read_binary(d, key_buf, key_len); if (ret != 0) { - return ret; + goto exit; } status = psa_export_public_key(key_id, key_buf, sizeof(key_buf), &key_len); if (status != PSA_SUCCESS) { - psa_destroy_key(key_id); - return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; + ret = MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; + goto exit; } ret = mbedtls_ecp_point_read_binary(grp, Q, key_buf, key_len); - if (ret != 0) { - return ret; - } +exit: psa_destroy_key(key_id); - return 0; + return ret; } /** Generate a key of the desired type. From 7816c24f2d0e98db60cadaa47406f3f5d26e1818 Mon Sep 17 00:00:00 2001 From: Valerio Setti Date: Tue, 4 Apr 2023 11:47:55 +0200 Subject: [PATCH 6/6] test: fix guards position in test_suite_pk Signed-off-by: Valerio Setti --- tests/suites/test_suite_pk.function | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/suites/test_suite_pk.function b/tests/suites/test_suite_pk.function index 2e374fe85f14..dc4604ad5bbd 100644 --- a/tests/suites/test_suite_pk.function +++ b/tests/suites/test_suite_pk.function @@ -17,13 +17,13 @@ * unconditionally (https://github.com/Mbed-TLS/mbedtls/issues/2023). */ #include "psa/crypto.h" -#if defined(MBEDTLS_USE_PSA_CRYPTO) +/* Used for properly sizing the key buffer in pk_genkey_ec() */ #include "mbedtls/psa_util.h" -#endif #define RSA_KEY_SIZE 512 #define RSA_KEY_LEN 64 +#if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECP_C) static int pk_genkey_ec(mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q) { @@ -71,6 +71,7 @@ exit: return ret; } +#endif /* MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_ECP_C */ /** Generate a key of the desired type. *