From c1fc136b14cf6366c5db66d8bbb121b32d01470e Mon Sep 17 00:00:00 2001 From: Waleed Elmelegy Date: Tue, 3 Dec 2024 13:45:47 +0000 Subject: [PATCH 01/14] Add Header for mbedtls_psa_ecp_export_public_key_iop_complete() Signed-off-by: Waleed Elmelegy --- .../drivers/builtin/src/psa_crypto_ecp.h | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tf-psa-crypto/drivers/builtin/src/psa_crypto_ecp.h b/tf-psa-crypto/drivers/builtin/src/psa_crypto_ecp.h index 261b873897..42116597ee 100644 --- a/tf-psa-crypto/drivers/builtin/src/psa_crypto_ecp.h +++ b/tf-psa-crypto/drivers/builtin/src/psa_crypto_ecp.h @@ -147,6 +147,31 @@ psa_status_t mbedtls_psa_ecp_export_public_key_iop_setup( size_t private_key_len, const psa_key_attributes_t *private_key_attributes); + +/** + * \brief Continue and eventually complete an export public-key operation. + * + * \param[in] operation The \c mbedtls_psa_export_public_key_iop_operation_t to use. + * This must be initialized first and + * had \c mbedtls_psa_ecp_export_public_key_iop_setup() + * called successfully. + * \param[out] pub_key Buffer where the public key data is to be written. + * \param[in] pub_key_size Size of the \p pub_key buffer in bytes. + * \param[out] pub_key_len On success, the number of bytes that make up the public key data. + * + * \retval #PSA_SUCCESS + * The key was exported successfully. + * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription + * \retval #PSA_ERROR_BUFFER_TOO_SMALL \emptydescription + * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription + * + */ +psa_status_t mbedtls_psa_ecp_export_public_key_iop_complete( + mbedtls_psa_export_public_key_iop_operation_t *operation, + uint8_t *pub_key, + size_t pub_key_size, + size_t *pub_key_len); + /** * \brief Abort an interruptible export public-key operation. * From af2595b4a7064d509adf494745dc22294af7dcaf Mon Sep 17 00:00:00 2001 From: Waleed Elmelegy Date: Tue, 3 Dec 2024 13:46:23 +0000 Subject: [PATCH 02/14] Add implementation for mbedtls_psa_ecp_export_public_key_iop_complete() Signed-off-by: Waleed Elmelegy --- .../drivers/builtin/src/psa_crypto_ecp.c | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tf-psa-crypto/drivers/builtin/src/psa_crypto_ecp.c b/tf-psa-crypto/drivers/builtin/src/psa_crypto_ecp.c index f90274ed13..f92be5d06a 100644 --- a/tf-psa-crypto/drivers/builtin/src/psa_crypto_ecp.c +++ b/tf-psa-crypto/drivers/builtin/src/psa_crypto_ecp.c @@ -689,6 +689,33 @@ exit: return status; } +psa_status_t mbedtls_psa_ecp_export_public_key_iop_complete( + mbedtls_psa_export_public_key_iop_operation_t *operation, + uint8_t *pub_key, + size_t pub_key_size, + size_t *pub_key_len) +{ + int status = 0; + + if (mbedtls_ecp_is_zero(&operation->key->Q)) { + mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops()); + + status = mbedtls_ecp_mul_restartable(&operation->key->grp, &operation->key->Q, + &operation->key->d, &operation->key->grp.G, + mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE, + &operation->restart_ctx); + operation->num_ops += operation->restart_ctx.ops_done; + } + + if (status == 0) { + status = mbedtls_ecp_write_public_key((const mbedtls_ecp_keypair *) operation->key, + MBEDTLS_ECP_PF_UNCOMPRESSED, pub_key_len, + pub_key, pub_key_size); + } + + return mbedtls_to_psa_error(status); +} + psa_status_t mbedtls_psa_ecp_export_public_key_iop_abort( mbedtls_psa_export_public_key_iop_operation_t *operation) { From 2cfce63fe6a952e70d3da3b101bdd61fd0aebf77 Mon Sep 17 00:00:00 2001 From: Waleed Elmelegy Date: Tue, 3 Dec 2024 13:49:27 +0000 Subject: [PATCH 03/14] Fix status variable type in mbedtls_psa_ecp_export_public_key_iop_setup() Signed-off-by: Waleed Elmelegy --- tf-psa-crypto/drivers/builtin/src/psa_crypto_ecp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tf-psa-crypto/drivers/builtin/src/psa_crypto_ecp.c b/tf-psa-crypto/drivers/builtin/src/psa_crypto_ecp.c index f92be5d06a..c5cd45d325 100644 --- a/tf-psa-crypto/drivers/builtin/src/psa_crypto_ecp.c +++ b/tf-psa-crypto/drivers/builtin/src/psa_crypto_ecp.c @@ -670,7 +670,7 @@ psa_status_t mbedtls_psa_ecp_export_public_key_iop_setup( size_t private_key_len, const psa_key_attributes_t *private_key_attributes) { - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; + int status = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; status = mbedtls_psa_ecp_load_representation( psa_get_key_type(private_key_attributes), From 4cffd5d4f39e1d8797ef4e6c14fb8a600f8ac6f0 Mon Sep 17 00:00:00 2001 From: Waleed Elmelegy Date: Tue, 3 Dec 2024 13:50:56 +0000 Subject: [PATCH 04/14] Add implementaion for psa_export_public_key_iop_complete() Signed-off-by: Waleed Elmelegy --- tf-psa-crypto/core/psa_crypto.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/tf-psa-crypto/core/psa_crypto.c b/tf-psa-crypto/core/psa_crypto.c index 359d622728..13ab40a3b6 100644 --- a/tf-psa-crypto/core/psa_crypto.c +++ b/tf-psa-crypto/core/psa_crypto.c @@ -1757,12 +1757,33 @@ psa_status_t psa_export_public_key_iop_complete(psa_export_public_key_iop_t *ope size_t data_size, size_t *data_length) { +#if defined(MBEDTLS_ECP_RESTARTABLE) + psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; + + if (operation->id == 0 || operation->error_occurred) { + return PSA_ERROR_BAD_STATE; + } + + status = mbedtls_psa_ecp_export_public_key_iop_complete(&operation->ctx, data, data_size, + data_length); + + if (status != PSA_OPERATION_INCOMPLETE) { + psa_export_public_key_iop_abort_internal(operation); + + if (status != PSA_SUCCESS) { + operation->error_occurred = 1; + } + } + + return status; +#else (void) operation; (void) data; (void) data_size; (void) data_length; - return PSA_ERROR_NOT_SUPPORTED; + return PSA_ERROR_BAD_STATE; +#endif } psa_status_t psa_export_public_key_iop_abort(psa_export_public_key_iop_t *operation) From f466a284c12b8843f7d488029886976f68d332b2 Mon Sep 17 00:00:00 2001 From: Waleed Elmelegy Date: Tue, 3 Dec 2024 13:52:28 +0000 Subject: [PATCH 05/14] Fix checks for key type in psa_export_public_key_iop_setup() Key type must be a key pair or public-key if not we return PSA_ERROR_INVALID_ARGUMENT. The key type must be ECC key as this is what we support for now otherwise we return PSA_ERROR_NOT_SUPPORTED. Signed-off-by: Waleed Elmelegy --- tf-psa-crypto/core/psa_crypto.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tf-psa-crypto/core/psa_crypto.c b/tf-psa-crypto/core/psa_crypto.c index 13ab40a3b6..a298fcc7ae 100644 --- a/tf-psa-crypto/core/psa_crypto.c +++ b/tf-psa-crypto/core/psa_crypto.c @@ -1717,12 +1717,14 @@ psa_status_t psa_export_public_key_iop_setup(psa_export_public_key_iop_t *operat private_key_type = psa_get_key_type(&private_key_attributes); - if (!PSA_KEY_TYPE_IS_KEY_PAIR(private_key_type)) { + if (!PSA_KEY_TYPE_IS_KEY_PAIR(private_key_type) && + !PSA_KEY_TYPE_IS_PUBLIC_KEY(private_key_type)) { status = PSA_ERROR_INVALID_ARGUMENT; goto exit; } - if (!PSA_KEY_TYPE_IS_ECC_KEY_PAIR(private_key_type)) { + if (!PSA_KEY_TYPE_IS_ECC_KEY_PAIR(private_key_type) && + !PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(private_key_type)) { status = PSA_ERROR_NOT_SUPPORTED; goto exit; } From 54ba963575b0c0d34bf6507c52d09929f3bd8df7 Mon Sep 17 00:00:00 2001 From: Waleed Elmelegy Date: Tue, 3 Dec 2024 14:17:25 +0000 Subject: [PATCH 06/14] Add interuptible export public-key testing to invalid key tests Signed-off-by: Waleed Elmelegy --- tf-psa-crypto/tests/suites/test_suite_psa_crypto.function | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tf-psa-crypto/tests/suites/test_suite_psa_crypto.function b/tf-psa-crypto/tests/suites/test_suite_psa_crypto.function index 00b935be54..c72511b2f9 100644 --- a/tf-psa-crypto/tests/suites/test_suite_psa_crypto.function +++ b/tf-psa-crypto/tests/suites/test_suite_psa_crypto.function @@ -222,6 +222,9 @@ static int test_operations_on_invalid_key(mbedtls_svc_key_id_t key) { psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 0x6964); +#if defined(MBEDTLS_ECP_RESTARTABLE) + psa_export_public_key_iop_t export_key_iop = PSA_EXPORT_PUBLIC_KEY_IOP_INIT; +#endif uint8_t buffer[1]; size_t length; int ok = 0; @@ -248,6 +251,11 @@ static int test_operations_on_invalid_key(mbedtls_svc_key_id_t key) buffer, sizeof(buffer), &length), PSA_ERROR_INVALID_HANDLE); +#if defined(MBEDTLS_ECP_RESTARTABLE) + TEST_EQUAL(psa_export_public_key_iop_setup(&export_key_iop, key), + PSA_ERROR_INVALID_HANDLE); +#endif + ok = 1; exit: From 81a525849c917ba4cc53d003ff839e810d748d43 Mon Sep 17 00:00:00 2001 From: Waleed Elmelegy Date: Tue, 3 Dec 2024 14:24:40 +0000 Subject: [PATCH 07/14] Add interuptible export public-key to current export public-key tests Signed-off-by: Waleed Elmelegy --- .../suites/test_suite_psa_crypto.function | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/tf-psa-crypto/tests/suites/test_suite_psa_crypto.function b/tf-psa-crypto/tests/suites/test_suite_psa_crypto.function index c72511b2f9..c7ff9ad432 100644 --- a/tf-psa-crypto/tests/suites/test_suite_psa_crypto.function +++ b/tf-psa-crypto/tests/suites/test_suite_psa_crypto.function @@ -1894,6 +1894,8 @@ void import_export_public_key(data_t *data, size_t export_size = expected_public_key->len + export_size_delta; size_t exported_length = INVALID_EXPORT_LENGTH; psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; + psa_export_public_key_iop_t export_key_operation = PSA_EXPORT_PUBLIC_KEY_IOP_INIT; + PSA_ASSERT(psa_crypto_init()); @@ -1925,6 +1927,55 @@ void import_export_public_key(data_t *data, TEST_MEMORY_COMPARE(expected_public_key->x, expected_public_key->len, exported, exported_length); } + + /* Adjust expected_status for interruptible export public-key. + * Interruptible export public-key is only supported for ECC key pairs and even + * for those only when MBEDTLS_ECP_RESTARTABLE is on. + */ + if ((PSA_KEY_TYPE_IS_KEY_PAIR(type) || PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) && + !PSA_KEY_TYPE_IS_ECC(type)) { + expected_export_status = PSA_ERROR_NOT_SUPPORTED; + } + +#if !defined(MBEDTLS_ECP_RESTARTABLE) + expected_export_status = PSA_ERROR_NOT_SUPPORTED; +#endif + + if (PSA_KEY_LIFETIME_GET_LOCATION(psa_get_key_lifetime(&attributes)) != + PSA_KEY_LOCATION_LOCAL_STORAGE) { + expected_export_status = PSA_ERROR_NOT_SUPPORTED; + } + + status = psa_export_public_key_iop_setup(&export_key_operation, key); + TEST_EQUAL(status, expected_export_status); + + if (status != PSA_SUCCESS) { + expected_export_status = PSA_ERROR_BAD_STATE; + } + + do { + status = psa_export_public_key_iop_complete(&export_key_operation, + exported, + export_size, + &exported_length); + } while (status == PSA_OPERATION_INCOMPLETE); + TEST_EQUAL(status, expected_export_status); + + if (status == PSA_SUCCESS) { + psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type); + size_t bits; + PSA_ASSERT(psa_get_key_attributes(key, &attributes)); + bits = psa_get_key_bits(&attributes); + TEST_LE_U(expected_public_key->len, + PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits)); + TEST_LE_U(expected_public_key->len, + PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits)); + TEST_LE_U(expected_public_key->len, + PSA_EXPORT_PUBLIC_KEY_MAX_SIZE); + TEST_MEMORY_COMPARE(expected_public_key->x, expected_public_key->len, + exported, exported_length); + } + exit: /* * Key attributes may have been returned by psa_get_key_attributes() From a04e88adf00e381a10a186efa34ca3f429820926 Mon Sep 17 00:00:00 2001 From: Waleed Elmelegy Date: Tue, 3 Dec 2024 14:25:57 +0000 Subject: [PATCH 08/14] Fix export public-key opaque key test paramters The test is marked as opaque but the parameter was set to PSA_KEY_LIFETIME_VOLATILE. Signed-off-by: Waleed Elmelegy --- tf-psa-crypto/tests/suites/test_suite_psa_crypto.data | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tf-psa-crypto/tests/suites/test_suite_psa_crypto.data b/tf-psa-crypto/tests/suites/test_suite_psa_crypto.data index 35073af206..26bc55e0ed 100644 --- a/tf-psa-crypto/tests/suites/test_suite_psa_crypto.data +++ b/tf-psa-crypto/tests/suites/test_suite_psa_crypto.data @@ -422,7 +422,7 @@ import_export:"372c9778f69f726cbca3f4a268f16b4d617d10280d79a6a029cd51879fe101293 PSA import/export-public EC brainpool512r1: good, opaque depends_on:PSA_WANT_ALG_ECDSA:PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_BASIC:PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_IMPORT:PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_EXPORT:PSA_WANT_ECC_BRAINPOOL_P_R1_512:PSA_CRYPTO_DRIVER_TEST -import_export_public_key:"372c9778f69f726cbca3f4a268f16b4d617d10280d79a6a029cd51879fe1012934dfe5395455337df6906dc7d6d2eea4dbb2065c0228f73b3ed716480e7d71d2":PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_BRAINPOOL_P_R1):PSA_ALG_ECDSA_ANY:0:0:PSA_SUCCESS:"0438b7ec92b61c5c6c7fbc28a4ec759d48fcd4e2e374defd5c4968a54dbef7510e517886fbfc38ea39aa529359d70a7156c35d3cbac7ce776bdb251dd64bce71234424ee7049eed072f0dbc4d79996e175d557e263763ae97095c081e73e7db2e38adc3d4c9a0487b1ede876dc1fca61c902e9a1d8722b8612928f18a24845591a" +import_export_public_key:"372c9778f69f726cbca3f4a268f16b4d617d10280d79a6a029cd51879fe1012934dfe5395455337df6906dc7d6d2eea4dbb2065c0228f73b3ed716480e7d71d2":PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_BRAINPOOL_P_R1):PSA_ALG_ECDSA_ANY:PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION( PSA_KEY_PERSISTENCE_VOLATILE, TEST_DRIVER_LOCATION ):0:PSA_SUCCESS:"0438b7ec92b61c5c6c7fbc28a4ec759d48fcd4e2e374defd5c4968a54dbef7510e517886fbfc38ea39aa529359d70a7156c35d3cbac7ce776bdb251dd64bce71234424ee7049eed072f0dbc4d79996e175d557e263763ae97095c081e73e7db2e38adc3d4c9a0487b1ede876dc1fca61c902e9a1d8722b8612928f18a24845591a" PSA import/export EC curve25519 key pair: good (already properly masked), opaque depends_on:PSA_WANT_ALG_ECDH:PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_BASIC:PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_IMPORT:PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_EXPORT:PSA_WANT_ECC_MONTGOMERY_255:PSA_CRYPTO_DRIVER_TEST From e283ed9e2057f84ac408a9e50f3955fe47dfd5bc Mon Sep 17 00:00:00 2001 From: Waleed Elmelegy Date: Tue, 3 Dec 2024 14:28:50 +0000 Subject: [PATCH 09/14] Add testing of complete API of interruptible export public-key Signed-off-by: Waleed Elmelegy --- .../suites/test_suite_psa_crypto.function | 60 ++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/tf-psa-crypto/tests/suites/test_suite_psa_crypto.function b/tf-psa-crypto/tests/suites/test_suite_psa_crypto.function index c7ff9ad432..a793404ece 100644 --- a/tf-psa-crypto/tests/suites/test_suite_psa_crypto.function +++ b/tf-psa-crypto/tests/suites/test_suite_psa_crypto.function @@ -10449,6 +10449,11 @@ void iop_export_public_key( psa_status_t expected_status = expected_status_arg; psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; psa_export_public_key_iop_t export_key_operation = PSA_EXPORT_PUBLIC_KEY_IOP_INIT; + uint8_t output[PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)] = { 0 }; + size_t output_len = 0; + uint8_t refrence_output[PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)] = + { 0 }; + size_t refrence_output_len = 0; psa_status_t status; PSA_ASSERT(psa_crypto_init()); @@ -10465,6 +10470,15 @@ void iop_export_public_key( status = psa_generate_key(&attributes, &iop_key); TEST_EQUAL(status, PSA_SUCCESS); + /* Test calling complete() without calling setup() will fail. */ + status = psa_export_public_key_iop_complete(&export_key_operation, + output, + sizeof(output), + &output_len); + TEST_EQUAL(status, PSA_ERROR_BAD_STATE); + + PSA_ASSERT(psa_export_public_key_iop_abort(&export_key_operation)); + status = psa_export_public_key_iop_setup(&export_key_operation, iop_key); TEST_EQUAL(status, expected_status); @@ -10474,12 +10488,56 @@ void iop_export_public_key( TEST_EQUAL(status, PSA_ERROR_BAD_STATE); #endif - TEST_EQUAL(psa_export_public_key_iop_abort(&export_key_operation), PSA_SUCCESS); + PSA_ASSERT(psa_export_public_key_iop_abort(&export_key_operation)); /* Test that after calling abort operation is reset to it's fresh state */ status = psa_export_public_key_iop_setup(&export_key_operation, iop_key); TEST_EQUAL(status, expected_status); + if (expected_status != PSA_SUCCESS) { + expected_status = PSA_ERROR_BAD_STATE; + } + + do { + status = psa_export_public_key_iop_complete(&export_key_operation, + output, + sizeof(output), + &output_len); + } while (status == PSA_OPERATION_INCOMPLETE); + TEST_EQUAL(status, expected_status); + + /* Test calling complete() 2 times consecutively will fail. */ + status = psa_export_public_key_iop_complete(&export_key_operation, + output, + sizeof(output), + &output_len); + TEST_EQUAL(status, PSA_ERROR_BAD_STATE); + + if (expected_status == PSA_SUCCESS) { + status = psa_export_public_key(iop_key, + refrence_output, + sizeof(refrence_output), + &refrence_output_len); + TEST_EQUAL(status, PSA_SUCCESS); + + TEST_MEMORY_COMPARE(refrence_output, refrence_output_len, output, output_len); + + /* Test psa_export_public_key_iop_complete() returns right error code when + output buffer is not enough. */ + PSA_ASSERT(psa_export_public_key_iop_abort(&export_key_operation)); + + status = psa_export_public_key_iop_setup(&export_key_operation, iop_key); + TEST_EQUAL(status, PSA_SUCCESS); + + do { + status = psa_export_public_key_iop_complete(&export_key_operation, + output, + refrence_output_len-1, + &output_len); + } while (status == PSA_OPERATION_INCOMPLETE); + TEST_EQUAL(status, PSA_ERROR_BUFFER_TOO_SMALL); + } + exit: psa_export_public_key_iop_abort(&export_key_operation); psa_destroy_key(iop_key); From 3c46535ff9fee7d86825cefefeeee354571c0a10 Mon Sep 17 00:00:00 2001 From: Waleed Elmelegy Date: Tue, 3 Dec 2024 14:34:35 +0000 Subject: [PATCH 10/14] Rename mbedtls_psa_export_public_key_iop_operation_t Rename it to mbedtls_psa_export_public_key_iop_t as iop stands for "interuptible operation" already. Signed-off-by: Waleed Elmelegy --- tf-psa-crypto/drivers/builtin/src/psa_crypto_ecp.c | 6 +++--- tf-psa-crypto/drivers/builtin/src/psa_crypto_ecp.h | 12 ++++++------ .../include/psa/crypto_builtin_composites.h | 2 +- tf-psa-crypto/include/psa/crypto_struct.h | 2 +- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/tf-psa-crypto/drivers/builtin/src/psa_crypto_ecp.c b/tf-psa-crypto/drivers/builtin/src/psa_crypto_ecp.c index c5cd45d325..1221f61860 100644 --- a/tf-psa-crypto/drivers/builtin/src/psa_crypto_ecp.c +++ b/tf-psa-crypto/drivers/builtin/src/psa_crypto_ecp.c @@ -665,7 +665,7 @@ psa_status_t mbedtls_psa_ecp_generate_key_iop_abort( } psa_status_t mbedtls_psa_ecp_export_public_key_iop_setup( - mbedtls_psa_export_public_key_iop_operation_t *operation, + mbedtls_psa_export_public_key_iop_t *operation, uint8_t *private_key, size_t private_key_len, const psa_key_attributes_t *private_key_attributes) @@ -690,7 +690,7 @@ exit: } psa_status_t mbedtls_psa_ecp_export_public_key_iop_complete( - mbedtls_psa_export_public_key_iop_operation_t *operation, + mbedtls_psa_export_public_key_iop_t *operation, uint8_t *pub_key, size_t pub_key_size, size_t *pub_key_len) @@ -717,7 +717,7 @@ psa_status_t mbedtls_psa_ecp_export_public_key_iop_complete( } psa_status_t mbedtls_psa_ecp_export_public_key_iop_abort( - mbedtls_psa_export_public_key_iop_operation_t *operation) + mbedtls_psa_export_public_key_iop_t *operation) { mbedtls_ecp_keypair_free(operation->key); mbedtls_free(operation->key); diff --git a/tf-psa-crypto/drivers/builtin/src/psa_crypto_ecp.h b/tf-psa-crypto/drivers/builtin/src/psa_crypto_ecp.h index 42116597ee..c220e82713 100644 --- a/tf-psa-crypto/drivers/builtin/src/psa_crypto_ecp.h +++ b/tf-psa-crypto/drivers/builtin/src/psa_crypto_ecp.h @@ -123,7 +123,7 @@ psa_status_t mbedtls_psa_ecp_export_public_key( /** * \brief Setup a new interruptible export public-key operation. * - * \param[in] operation The \c mbedtls_psa_export_public_key_iop_operation_t to use. + * \param[in] operation The \c mbedtls_psa_export_public_key_iop_t to use. * This must be initialized first. * \param[in] private_key pointer to private key. * \param[in] private_key_len size of \p private_key in bytes. @@ -142,7 +142,7 @@ psa_status_t mbedtls_psa_ecp_export_public_key( * */ psa_status_t mbedtls_psa_ecp_export_public_key_iop_setup( - mbedtls_psa_export_public_key_iop_operation_t *operation, + mbedtls_psa_export_public_key_iop_t *operation, uint8_t *private_key, size_t private_key_len, const psa_key_attributes_t *private_key_attributes); @@ -151,7 +151,7 @@ psa_status_t mbedtls_psa_ecp_export_public_key_iop_setup( /** * \brief Continue and eventually complete an export public-key operation. * - * \param[in] operation The \c mbedtls_psa_export_public_key_iop_operation_t to use. + * \param[in] operation The \c mbedtls_psa_export_public_key_iop_t to use. * This must be initialized first and * had \c mbedtls_psa_ecp_export_public_key_iop_setup() * called successfully. @@ -167,7 +167,7 @@ psa_status_t mbedtls_psa_ecp_export_public_key_iop_setup( * */ psa_status_t mbedtls_psa_ecp_export_public_key_iop_complete( - mbedtls_psa_export_public_key_iop_operation_t *operation, + mbedtls_psa_export_public_key_iop_t *operation, uint8_t *pub_key, size_t pub_key_size, size_t *pub_key_len); @@ -175,13 +175,13 @@ psa_status_t mbedtls_psa_ecp_export_public_key_iop_complete( /** * \brief Abort an interruptible export public-key operation. * - * \param[in] operation The \c mbedtls_psa_export_public_key_iop_operation_t to abort. + * \param[in] operation The \c mbedtls_psa_export_public_key_iop_t to abort. * * \retval #PSA_SUCCESS * The operation was aborted successfully. */ psa_status_t mbedtls_psa_ecp_export_public_key_iop_abort( - mbedtls_psa_export_public_key_iop_operation_t *operation); + mbedtls_psa_export_public_key_iop_t *operation); /** * \brief Generate an ECP key. diff --git a/tf-psa-crypto/include/psa/crypto_builtin_composites.h b/tf-psa-crypto/include/psa/crypto_builtin_composites.h index 9bd58f9d9e..ba5375fdde 100644 --- a/tf-psa-crypto/include/psa/crypto_builtin_composites.h +++ b/tf-psa-crypto/include/psa/crypto_builtin_composites.h @@ -258,7 +258,7 @@ typedef struct { /* Make the struct non-empty if algs not supported. */ unsigned MBEDTLS_PRIVATE(dummy); #endif -} mbedtls_psa_export_public_key_iop_operation_t; +} mbedtls_psa_export_public_key_iop_t; #if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_ECP_RESTARTABLE) #define MBEDTLS_PSA_EXPORT_PUBLIC_KEY_IOP_INIT { NULL, MBEDTLS_ECP_RESTART_INIT, 0 } diff --git a/tf-psa-crypto/include/psa/crypto_struct.h b/tf-psa-crypto/include/psa/crypto_struct.h index d0300da004..ffaf6c2b66 100644 --- a/tf-psa-crypto/include/psa/crypto_struct.h +++ b/tf-psa-crypto/include/psa/crypto_struct.h @@ -584,7 +584,7 @@ struct psa_export_public_key_iop_s { * any driver (i.e. none of the driver contexts are active). */ unsigned int MBEDTLS_PRIVATE(id); - mbedtls_psa_export_public_key_iop_operation_t MBEDTLS_PRIVATE(ctx); + mbedtls_psa_export_public_key_iop_t MBEDTLS_PRIVATE(ctx); unsigned int MBEDTLS_PRIVATE(error_occurred) : 1; uint32_t MBEDTLS_PRIVATE(num_ops); #endif From 1daabc113b4442a5568c159e6a6d1ffc08ba4b81 Mon Sep 17 00:00:00 2001 From: Waleed Elmelegy Date: Mon, 9 Dec 2024 17:59:42 +0000 Subject: [PATCH 11/14] Refactor and improve iop export public-key setup and abort APIs Signed-off-by: Waleed Elmelegy --- tf-psa-crypto/core/psa_crypto.c | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/tf-psa-crypto/core/psa_crypto.c b/tf-psa-crypto/core/psa_crypto.c index a298fcc7ae..e823966ccb 100644 --- a/tf-psa-crypto/core/psa_crypto.c +++ b/tf-psa-crypto/core/psa_crypto.c @@ -1676,6 +1676,8 @@ static psa_status_t psa_export_public_key_iop_abort_internal(psa_export_public_k status = mbedtls_psa_ecp_export_public_key_iop_abort(&operation->ctx); + memset(&operation->ctx, 0, sizeof(operation->ctx)); + operation->id = 0; return status; @@ -1694,9 +1696,8 @@ psa_status_t psa_export_public_key_iop_setup(psa_export_public_key_iop_t *operat #if defined(MBEDTLS_ECP_RESTARTABLE) psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; - size_t key_size = 0; - psa_key_attributes_t private_key_attributes; - psa_key_type_t private_key_type; + psa_key_attributes_t key_attributes; + psa_key_type_t key_type; psa_key_slot_t *slot = NULL; if (operation->id != 0 || operation->error_occurred) { @@ -1713,31 +1714,22 @@ psa_status_t psa_export_public_key_iop_setup(psa_export_public_key_iop_t *operat goto exit; } - private_key_attributes = slot->attr; + key_attributes = slot->attr; - private_key_type = psa_get_key_type(&private_key_attributes); + key_type = psa_get_key_type(&key_attributes); - if (!PSA_KEY_TYPE_IS_KEY_PAIR(private_key_type) && - !PSA_KEY_TYPE_IS_PUBLIC_KEY(private_key_type)) { + if (!PSA_KEY_TYPE_IS_ASYMMETRIC(key_type)) { status = PSA_ERROR_INVALID_ARGUMENT; goto exit; } - if (!PSA_KEY_TYPE_IS_ECC_KEY_PAIR(private_key_type) && - !PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(private_key_type)) { - status = PSA_ERROR_NOT_SUPPORTED; - goto exit; - } - - key_size = PSA_EXPORT_KEY_OUTPUT_SIZE(private_key_type, - psa_get_key_bits(&private_key_attributes)); - if (key_size == 0) { + if (!PSA_KEY_TYPE_IS_ECC(key_type)) { status = PSA_ERROR_NOT_SUPPORTED; goto exit; } status = mbedtls_psa_ecp_export_public_key_iop_setup(&operation->ctx, slot->key.data, - slot->key.bytes, &private_key_attributes); + slot->key.bytes, &key_attributes); exit: unlock_status = psa_unregister_read_under_mutex(slot); From c66147df7275558e1c63dde893c1216fba7cad8e Mon Sep 17 00:00:00 2001 From: Waleed Elmelegy Date: Mon, 9 Dec 2024 18:00:45 +0000 Subject: [PATCH 12/14] Refactor & improve internal iop export public-key setup and complete APIs Signed-off-by: Waleed Elmelegy --- .../drivers/builtin/src/psa_crypto_ecp.c | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/tf-psa-crypto/drivers/builtin/src/psa_crypto_ecp.c b/tf-psa-crypto/drivers/builtin/src/psa_crypto_ecp.c index 1221f61860..3ca28fa984 100644 --- a/tf-psa-crypto/drivers/builtin/src/psa_crypto_ecp.c +++ b/tf-psa-crypto/drivers/builtin/src/psa_crypto_ecp.c @@ -666,17 +666,17 @@ psa_status_t mbedtls_psa_ecp_generate_key_iop_abort( psa_status_t mbedtls_psa_ecp_export_public_key_iop_setup( mbedtls_psa_export_public_key_iop_t *operation, - uint8_t *private_key, - size_t private_key_len, - const psa_key_attributes_t *private_key_attributes) + uint8_t *key, + size_t key_len, + const psa_key_attributes_t *key_attributes) { - int status = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; + psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; status = mbedtls_psa_ecp_load_representation( - psa_get_key_type(private_key_attributes), - psa_get_key_bits(private_key_attributes), - private_key, - private_key_len, + psa_get_key_type(key_attributes), + psa_get_key_bits(key_attributes), + key, + key_len, &operation->key); if (status != PSA_SUCCESS) { goto exit; @@ -695,25 +695,25 @@ psa_status_t mbedtls_psa_ecp_export_public_key_iop_complete( size_t pub_key_size, size_t *pub_key_len) { - int status = 0; + int ret = 0; if (mbedtls_ecp_is_zero(&operation->key->Q)) { mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops()); - status = mbedtls_ecp_mul_restartable(&operation->key->grp, &operation->key->Q, - &operation->key->d, &operation->key->grp.G, - mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE, - &operation->restart_ctx); + ret = mbedtls_ecp_mul_restartable(&operation->key->grp, &operation->key->Q, + &operation->key->d, &operation->key->grp.G, + mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE, + &operation->restart_ctx); operation->num_ops += operation->restart_ctx.ops_done; } - if (status == 0) { - status = mbedtls_ecp_write_public_key((const mbedtls_ecp_keypair *) operation->key, - MBEDTLS_ECP_PF_UNCOMPRESSED, pub_key_len, - pub_key, pub_key_size); + if (ret == 0) { + ret = mbedtls_ecp_write_public_key(operation->key, + MBEDTLS_ECP_PF_UNCOMPRESSED, pub_key_len, + pub_key, pub_key_size); } - return mbedtls_to_psa_error(status); + return mbedtls_to_psa_error(ret); } psa_status_t mbedtls_psa_ecp_export_public_key_iop_abort( From 0843214dee703328f49f82e95785491a30fe0659 Mon Sep 17 00:00:00 2001 From: Waleed Elmelegy Date: Mon, 9 Dec 2024 18:01:51 +0000 Subject: [PATCH 13/14] Remove Invalid import/export key test The test is supposed to be an opaque key test but the testing function does not support specifying an opaque driver. Signed-off-by: Waleed Elmelegy --- tf-psa-crypto/tests/suites/test_suite_psa_crypto.data | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tf-psa-crypto/tests/suites/test_suite_psa_crypto.data b/tf-psa-crypto/tests/suites/test_suite_psa_crypto.data index 26bc55e0ed..f042a13a26 100644 --- a/tf-psa-crypto/tests/suites/test_suite_psa_crypto.data +++ b/tf-psa-crypto/tests/suites/test_suite_psa_crypto.data @@ -156,10 +156,6 @@ PSA import/export RSA keypair: export buffer too small, opaque depends_on:PSA_WANT_ALG_RSA_PKCS1V15_SIGN:PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC:PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_IMPORT:PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_EXPORT:PSA_CRYPTO_DRIVER_TEST import_export:"3082025e02010002818100af057d396ee84fb75fdbb5c2b13c7fe5a654aa8aa2470b541ee1feb0b12d25c79711531249e1129628042dbbb6c120d1443524ef4c0e6e1d8956eeb2077af12349ddeee54483bc06c2c61948cd02b202e796aebd94d3a7cbf859c2c1819c324cb82b9cd34ede263a2abffe4733f077869e8660f7d6834da53d690ef7985f6bc3020301000102818100874bf0ffc2f2a71d14671ddd0171c954d7fdbf50281e4f6d99ea0e1ebcf82faa58e7b595ffb293d1abe17f110b37c48cc0f36c37e84d876621d327f64bbe08457d3ec4098ba2fa0a319fba411c2841ed7be83196a8cdf9daa5d00694bc335fc4c32217fe0488bce9cb7202e59468b1ead119000477db2ca797fac19eda3f58c1024100e2ab760841bb9d30a81d222de1eb7381d82214407f1b975cbbfe4e1a9467fd98adbd78f607836ca5be1928b9d160d97fd45c12d6b52e2c9871a174c66b488113024100c5ab27602159ae7d6f20c3c2ee851e46dc112e689e28d5fcbbf990a99ef8a90b8bb44fd36467e7fc1789ceb663abda338652c3c73f111774902e840565927091024100b6cdbd354f7df579a63b48b3643e353b84898777b48b15f94e0bfc0567a6ae5911d57ad6409cf7647bf96264e9bd87eb95e263b7110b9a1f9f94acced0fafa4d024071195eec37e8d257decfc672b07ae639f10cbb9b0c739d0c809968d644a94e3fd6ed9287077a14583f379058f76a8aecd43c62dc8c0f41766650d725275ac4a1024100bb32d133edc2e048d463388b7be9cb4be29f4b6250be603e70e3647501c97ddde20a4e71be95fd5e71784e25aca4baf25be5738aae59bbfe1c997781447a2b24":PSA_KEY_TYPE_RSA_KEY_PAIR:PSA_KEY_USAGE_EXPORT:PSA_ALG_RSA_PKCS1V15_SIGN_RAW:PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION( PSA_KEY_PERSISTENCE_VOLATILE, TEST_DRIVER_LOCATION ):1024:-1:PSA_ERROR_BUFFER_TOO_SMALL:1 -PSA import/export RSA keypair: trailing garbage rejected, opaque -depends_on:PSA_WANT_ALG_RSA_PKCS1V15_SIGN:PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC:PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_IMPORT:PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_EXPORT:PSA_CRYPTO_DRIVER_TEST -import_with_data:"3082025e02010002818100af057d396ee84fb75fdbb5c2b13c7fe5a654aa8aa2470b541ee1feb0b12d25c79711531249e1129628042dbbb6c120d1443524ef4c0e6e1d8956eeb2077af12349ddeee54483bc06c2c61948cd02b202e796aebd94d3a7cbf859c2c1819c324cb82b9cd34ede263a2abffe4733f077869e8660f7d6834da53d690ef7985f6bc3020301000102818100874bf0ffc2f2a71d14671ddd0171c954d7fdbf50281e4f6d99ea0e1ebcf82faa58e7b595ffb293d1abe17f110b37c48cc0f36c37e84d876621d327f64bbe08457d3ec4098ba2fa0a319fba411c2841ed7be83196a8cdf9daa5d00694bc335fc4c32217fe0488bce9cb7202e59468b1ead119000477db2ca797fac19eda3f58c1024100e2ab760841bb9d30a81d222de1eb7381d82214407f1b975cbbfe4e1a9467fd98adbd78f607836ca5be1928b9d160d97fd45c12d6b52e2c9871a174c66b488113024100c5ab27602159ae7d6f20c3c2ee851e46dc112e689e28d5fcbbf990a99ef8a90b8bb44fd36467e7fc1789ceb663abda338652c3c73f111774902e840565927091024100b6cdbd354f7df579a63b48b3643e353b84898777b48b15f94e0bfc0567a6ae5911d57ad6409cf7647bf96264e9bd87eb95e263b7110b9a1f9f94acced0fafa4d024071195eec37e8d257decfc672b07ae639f10cbb9b0c739d0c809968d644a94e3fd6ed9287077a14583f379058f76a8aecd43c62dc8c0f41766650d725275ac4a1024100bb32d133edc2e048d463388b7be9cb4be29f4b6250be603e70e3647501c97ddde20a4e71be95fd5e71784e25aca4baf25be5738aae59bbfe1c997781447a2b2400":PSA_KEY_TYPE_RSA_KEY_PAIR:1024:PSA_ERROR_INVALID_ARGUMENT - PSA import RSA keypair: truncated depends_on:PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC:PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_IMPORT import_with_data:"3082025e02010002818100af057d396ee84fb75fdbb5c2b13c7fe5a654aa8aa2470b541ee1feb0b12d25c79711531249e1129628042dbbb6c120d1443524ef4c0e6e1d8956eeb2077af12349ddeee54483bc06c2c61948cd02b202e796aebd94d3a7cbf859c2c1819c324cb82b9cd34ede263a2abffe4733f077869e8660f7d6834da53d690ef7985f6bc3020301000102818100874bf0ffc2f2a71d14671ddd0171c954d7fdbf50281e4f6d99ea0e1ebcf82faa58e7b595ffb293d1abe17f110b37c48cc0f36c37e84d876621d327f64bbe08457d3ec4098ba2fa0a319fba411c2841ed7be83196a8cdf9daa5d00694bc335fc4c32217fe0488bce9cb7202e59468b1ead119000477db2ca797fac19eda3f58c1024100e2ab760841bb9d30a81d222de1eb7381d82214407f1b975cbbfe4e1a9467fd98adbd78f607836ca5be1928b9d160d97fd45c12d6b52e2c9871a174c66b488113024100c5ab27602159ae7d6f20c3c2ee851e46dc112e689e28d5fcbbf990a99ef8a90b8bb44fd36467e7fc1789ceb663abda338652c3c73f111774902e840565927091024100b6cdbd354f7df579a63b48b3643e353b84898777b48b15f94e0bfc0567a6ae5911d57ad6409cf7647bf96264e9bd87eb95e263b7110b9a1f9f94acced0fafa4d024071195eec37e8d257decfc672b07ae639f10cbb9b0c739d0c809968d644a94e3fd6ed9287077a14583f379058f76a8aecd43c62dc8c0f41766650d725275ac4a1024100bb32d133edc2e048d463388b7be9cb4be29f4b6250be603e70e3647501c97ddde20a4e71be95fd5e71784e25aca4baf25be5738aae59bbfe1c997781447a2b":PSA_KEY_TYPE_RSA_KEY_PAIR:0:PSA_ERROR_INVALID_ARGUMENT From e330e58bd7efca051bd7f2536efba59771c50653 Mon Sep 17 00:00:00 2001 From: Waleed Elmelegy Date: Mon, 9 Dec 2024 18:04:05 +0000 Subject: [PATCH 14/14] Improve iop export public-key testing * Improve wording of comments. * Zeroize buffer before doing iop testing to avoid comparing with previous values in case they are not overwritten. * Remove redundant testing. Signed-off-by: Waleed Elmelegy --- .../tests/suites/test_suite_psa_crypto.function | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/tf-psa-crypto/tests/suites/test_suite_psa_crypto.function b/tf-psa-crypto/tests/suites/test_suite_psa_crypto.function index a793404ece..6be0f60c11 100644 --- a/tf-psa-crypto/tests/suites/test_suite_psa_crypto.function +++ b/tf-psa-crypto/tests/suites/test_suite_psa_crypto.function @@ -1929,7 +1929,7 @@ void import_export_public_key(data_t *data, } /* Adjust expected_status for interruptible export public-key. - * Interruptible export public-key is only supported for ECC key pairs and even + * Interruptible export public-key is only supported for ECC keys and even * for those only when MBEDTLS_ECP_RESTARTABLE is on. */ if ((PSA_KEY_TYPE_IS_KEY_PAIR(type) || PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) && @@ -1953,6 +1953,8 @@ void import_export_public_key(data_t *data, expected_export_status = PSA_ERROR_BAD_STATE; } + memset(exported, 0, export_size); + do { status = psa_export_public_key_iop_complete(&export_key_operation, exported, @@ -1962,16 +1964,6 @@ void import_export_public_key(data_t *data, TEST_EQUAL(status, expected_export_status); if (status == PSA_SUCCESS) { - psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type); - size_t bits; - PSA_ASSERT(psa_get_key_attributes(key, &attributes)); - bits = psa_get_key_bits(&attributes); - TEST_LE_U(expected_public_key->len, - PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits)); - TEST_LE_U(expected_public_key->len, - PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits)); - TEST_LE_U(expected_public_key->len, - PSA_EXPORT_PUBLIC_KEY_MAX_SIZE); TEST_MEMORY_COMPARE(expected_public_key->x, expected_public_key->len, exported, exported_length); }