From 1ac41dec09aebc2640f899a3e5d5d741337db416 Mon Sep 17 00:00:00 2001 From: Aditya Deshpande Date: Mon, 28 Nov 2022 14:46:30 +0000 Subject: [PATCH] Add test function for opaque driver (simply returns PSA_ERROR_NOT_SUPPORTED), and address other review comments. Signed-off-by: Aditya Deshpande --- library/psa_crypto.c | 24 ++++++++-------- .../psa_crypto_driver_wrappers.c.jinja | 28 +++++++++++-------- tests/include/test/drivers/key_agreement.h | 20 ++++++------- tests/src/drivers/test_driver_key_agreement.c | 27 ++++++++++++++++-- ...test_suite_psa_crypto_driver_wrappers.data | 2 +- ..._suite_psa_crypto_driver_wrappers.function | 1 - 6 files changed, 65 insertions(+), 37 deletions(-) diff --git a/library/psa_crypto.c b/library/psa_crypto.c index b41307e94..fe0eaa6f4 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -5738,11 +5738,11 @@ psa_status_t psa_key_agreement_raw_builtin( const psa_key_attributes_t *attribut #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) case PSA_ALG_ECDH: return( mbedtls_psa_key_agreement_ecdh( attributes, key_buffer, - key_buffer_size, alg, - peer_key, peer_key_length, - shared_secret, - shared_secret_size, - shared_secret_length ) ); + key_buffer_size, alg, + peer_key, peer_key_length, + shared_secret, + shared_secret_size, + shared_secret_length ) ); #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */ default: (void) attributes; @@ -5771,18 +5771,20 @@ static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg, size_t shared_secret_size, size_t *shared_secret_length ) { - if( !PSA_ALG_IS_RAW_KEY_AGREEMENT(alg) ) + if( !PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) ) return( PSA_ERROR_NOT_SUPPORTED ); psa_key_attributes_t attributes = { .core = private_key->attr }; - return( psa_driver_wrapper_key_agreement( &attributes, private_key->key.data, - private_key->key.bytes, - alg, peer_key, peer_key_length, - shared_secret, shared_secret_size, - shared_secret_length ) ); + return( psa_driver_wrapper_key_agreement( &attributes, + private_key->key.data, + private_key->key.bytes, alg, + peer_key, peer_key_length, + shared_secret, + shared_secret_size, + shared_secret_length ) ); } /* Note that if this function fails, you must call psa_key_derivation_abort() diff --git a/scripts/data_files/driver_templates/psa_crypto_driver_wrappers.c.jinja b/scripts/data_files/driver_templates/psa_crypto_driver_wrappers.c.jinja index 75ac6aa4d..3ad92aaef 100644 --- a/scripts/data_files/driver_templates/psa_crypto_driver_wrappers.c.jinja +++ b/scripts/data_files/driver_templates/psa_crypto_driver_wrappers.c.jinja @@ -2497,22 +2497,17 @@ psa_status_t psa_driver_wrapper_key_agreement( case PSA_KEY_LOCATION_LOCAL_STORAGE: /* Key is stored in the slot in export representation, so * cycle through all known transparent accelerators */ - #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) - #if defined(PSA_CRYPTO_DRIVER_TEST) +#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) +#if defined(PSA_CRYPTO_DRIVER_TEST) status = mbedtls_test_transparent_key_agreement( attributes, - key_buffer, - key_buffer_size, - alg, - peer_key, - peer_key_length, - shared_secret, - shared_secret_size, - shared_secret_length ); + key_buffer, key_buffer_size, alg, peer_key, + peer_key_length, shared_secret, shared_secret_size, + shared_secret_length ); if( status != PSA_ERROR_NOT_SUPPORTED ) return( status ); - #endif /* PSA_CRYPTO_DRIVER_TEST */ - #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ +#endif /* PSA_CRYPTO_DRIVER_TEST */ +#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ /* Software Fallback */ status = psa_key_agreement_raw_builtin( attributes, @@ -2525,6 +2520,15 @@ psa_status_t psa_driver_wrapper_key_agreement( shared_secret_size, shared_secret_length ); return( status ); +#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) +#if defined(PSA_CRYPTO_DRIVER_TEST) + case PSA_CRYPTO_TEST_DRIVER_LOCATION: + return( mbedtls_test_opaque_key_agreement( attributes, + key_buffer, key_buffer_size, alg, peer_key, + peer_key_length, shared_secret, shared_secret_size, + shared_secret_length ) ); +#endif /* PSA_CRYPTO_DRIVER_TEST */ +#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ default: (void) attributes; diff --git a/tests/include/test/drivers/key_agreement.h b/tests/include/test/drivers/key_agreement.h index ec6515982..8f28cefda 100644 --- a/tests/include/test/drivers/key_agreement.h +++ b/tests/include/test/drivers/key_agreement.h @@ -59,16 +59,16 @@ psa_status_t mbedtls_test_transparent_key_agreement( size_t shared_secret_size, size_t *shared_secret_length ); -// psa_status_t mbedtls_test_opaque_key_agreement( -// const psa_key_attributes_t *attributes, -// const uint8_t *key_buffer, -// size_t key_buffer_size, -// psa_algorithm_t alg, -// const uint8_t *peer_key, -// size_t peer_key_length, -// uint8_t *shared_secret, -// size_t shared_secret_size, -// size_t *shared_secret_length ); +psa_status_t mbedtls_test_opaque_key_agreement( + const psa_key_attributes_t *attributes, + const uint8_t *key_buffer, + size_t key_buffer_size, + psa_algorithm_t alg, + const uint8_t *peer_key, + size_t peer_key_length, + uint8_t *shared_secret, + size_t shared_secret_size, + size_t *shared_secret_length ); #endif /*PSA_CRYPTO_DRIVER_TEST */ #endif /* PSA_CRYPTO_TEST_DRIVERS_KEY_AGREEMENT_H */ diff --git a/tests/src/drivers/test_driver_key_agreement.c b/tests/src/drivers/test_driver_key_agreement.c index 51301f8f0..3552f48f7 100644 --- a/tests/src/drivers/test_driver_key_agreement.c +++ b/tests/src/drivers/test_driver_key_agreement.c @@ -69,8 +69,8 @@ psa_status_t mbedtls_test_transparent_key_agreement( if( PSA_ALG_IS_ECDH(alg) ) { -#if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \ - (LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_ALG_ECDH) +#if (defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \ + defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_ALG_ECDH)) return( libtestdriver1_mbedtls_psa_key_agreement_ecdh( (const libtestdriver1_psa_key_attributes_t *) attributes, key_buffer, key_buffer_size, @@ -103,4 +103,27 @@ psa_status_t mbedtls_test_transparent_key_agreement( } +psa_status_t mbedtls_test_opaque_key_agreement( + const psa_key_attributes_t *attributes, + const uint8_t *key_buffer, + size_t key_buffer_size, + psa_algorithm_t alg, + const uint8_t *peer_key, + size_t peer_key_length, + uint8_t *shared_secret, + size_t shared_secret_size, + size_t *shared_secret_length ) +{ + (void) attributes; + (void) key_buffer; + (void) key_buffer_size; + (void) alg; + (void) peer_key; + (void) peer_key_length; + (void) shared_secret; + (void) shared_secret_size; + (void) shared_secret_length; + return( PSA_ERROR_NOT_SUPPORTED ); +} + #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */ diff --git a/tests/suites/test_suite_psa_crypto_driver_wrappers.data b/tests/suites/test_suite_psa_crypto_driver_wrappers.data index 74b74da92..6069a696c 100644 --- a/tests/suites/test_suite_psa_crypto_driver_wrappers.data +++ b/tests/suites/test_suite_psa_crypto_driver_wrappers.data @@ -308,7 +308,7 @@ depends_on:PSA_WANT_ALG_ECDH:PSA_WANT_KEY_TYPE_ECC_KEY_PAIR:MBEDTLS_PK_PARSE_C:P key_agreement:PSA_ALG_ECDH:PSA_SUCCESS:PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1):"c88f01f510d9ac3f70a292daa2316de544e9aab8afe84049c62a9c57862d1433":"04d12dfb5289c8d4f81208b70270398c342296970a0bccb74c736fc7554494bf6356fbf3ca366cc23e8157854c13c58d6aac23f046ada30f8353e74f33039872ab":"d6840f6b42f6edafd13116e0e12565202fef8e9ece7dce03812464d04b9442de":"":PSA_SUCCESS raw key agreement through driver: fallback -depends_on:PSA_WANT_ALG_ECDH:PSA_WANT_KEY_TYPE_ECC_KEY_PAIR:MBEDTLS_PK_PARSE_C:PSA_WANT_ECC_SECP_R1_256:MBEDLTS_PSA_BUILTIN_ALG_ECDH +depends_on:PSA_WANT_ALG_ECDH:PSA_WANT_KEY_TYPE_ECC_KEY_PAIR:MBEDTLS_PK_PARSE_C:PSA_WANT_ECC_SECP_R1_256:MBEDTLS_PSA_BUILTIN_ALG_ECDH key_agreement:PSA_ALG_ECDH:PSA_ERROR_NOT_SUPPORTED:PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1):"c88f01f510d9ac3f70a292daa2316de544e9aab8afe84049c62a9c57862d1433":"04d12dfb5289c8d4f81208b70270398c342296970a0bccb74c736fc7554494bf6356fbf3ca366cc23e8157854c13c58d6aac23f046ada30f8353e74f33039872ab":"d6840f6b42f6edafd13116e0e12565202fef8e9ece7dce03812464d04b9442de":"":PSA_SUCCESS raw key agreement through driver: error diff --git a/tests/suites/test_suite_psa_crypto_driver_wrappers.function b/tests/suites/test_suite_psa_crypto_driver_wrappers.function index 7fa3c947e..5f38614e8 100644 --- a/tests/suites/test_suite_psa_crypto_driver_wrappers.function +++ b/tests/suites/test_suite_psa_crypto_driver_wrappers.function @@ -626,7 +626,6 @@ exit: PSA_DONE( ); mbedtls_test_driver_key_agreement_hooks = mbedtls_test_driver_key_agreement_hooks_init(); - } /* END_CASE */