From d610191ad65ea114a0f42991a9479da7298f3447 Mon Sep 17 00:00:00 2001 From: Thomas Daubney Date: Fri, 21 Jan 2022 15:07:22 +0000 Subject: [PATCH] Adds driver dispatch test for M-AEAD encryption setup This commit adds a test called aead_encrypt_setup() which tests that the relevant drivers get called the correct amount of times when running the multipart AEAD encrypt setup API. Signed-off-by: Thomas Daubney --- ...test_suite_psa_crypto_driver_wrappers.data | 2 +- ..._suite_psa_crypto_driver_wrappers.function | 120 ++++++++++++++++++ 2 files changed, 121 insertions(+), 1 deletion(-) diff --git a/tests/suites/test_suite_psa_crypto_driver_wrappers.data b/tests/suites/test_suite_psa_crypto_driver_wrappers.data index 210ab65b50..2531120d00 100644 --- a/tests/suites/test_suite_psa_crypto_driver_wrappers.data +++ b/tests/suites/test_suite_psa_crypto_driver_wrappers.data @@ -779,4 +779,4 @@ asymmetric_encrypt:PSA_ALG_RSA_OAEP(PSA_ALG_SHA_256):"30818902818100af057d396ee8 PSA encrypt transparent driver: fallback not available RSA OAEP-SHA-256 depends_on:!MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP -asymmetric_encrypt:PSA_ALG_RSA_OAEP(PSA_ALG_SHA_256):"30818902818100af057d396ee84fb75fdbb5c2b13c7fe5a654aa8aa2470b541ee1feb0b12d25c79711531249e1129628042dbbb6c120d1443524ef4c0e6e1d8956eeb2077af12349ddeee54483bc06c2c61948cd02b202e796aebd94d3a7cbf859c2c1819c324cb82b9cd34ede263a2abffe4733f077869e8660f7d6834da53d690ef7985f6bc30203010001":"af057d396ee84fb75fdbb5c2b13c7fe5a654aa8aa2470b541ee1feb0b12d25c79711531249e1129628042dbbb6c120d1443524ef4c0e6e1d8956eeb2077af12349ddeee54483bc06c2c61948cd02b202e796aebd94d3a7cbf859c2c1819c324cb82b9cd34ede263a2abffe4733f077869e8660f7d6834da53d690ef7985f6bc3":"874bf0ffc2f2a71d14671ddd0171c954d7fdbf50281e4f6d99ea0e1ebcf82faa58e7b595ffb293d1abe17f110b37c48cc0f36c37e84d876621d327f64bbe08457d3ec4098ba2fa0a319fba411c2841ed7be83196a8cdf9daa5d00694bc335fc4c32217fe0488bce9cb7202e59468b1ead119000477db2ca797fac19eda3f58c1":"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad":"":"":PSA_ERROR_NOT_SUPPORTED:PSA_ERROR_NOT_SUPPORTED +asymmetric_encrypt:PSA_ALG_RSA_OAEP(PSA_ALG_SHA_256):"30818902818100af057d396ee84fb75fdbb5c2b13c7fe5a654aa8aa2470b541ee1feb0b12d25c79711531249e1129628042dbbb6c120d1443524ef4c0e6e1d8956eeb2077af12349ddeee54483bc06c2c61948cd02b202e796aebd94d3a7cbf859c2c1819c324cb82b9cd34ede263a2abffe4733f077869e8660f7d6834da53d690ef7985f6bc30203010001":"af057d396ee84fb75fdbb5c2b13c7fe5a654aa8aa2470b541ee1feb0b12d25c79711531249e1129628042dbbb6c120d1443524ef4c0e6e1d8956eeb2077af12349ddeee54483bc06c2c61948cd02b202e796aebd94d3a7cbf859c2c1819c324cb82b9cd34ede263a2abffe4733f077869e8660f7d6834da53d690ef7985f6bc3":"874bf0ffc2f2a71d14671ddd0171c954d7fdbf50281e4f6d99ea0e1ebcf82faa58e7b595ffb293d1abe17f110b37c48cc0f36c37e84d876621d327f64bbe08457d3ec4098ba2fa0a319fba411c2841ed7be83196a8cdf9daa5d00694bc335fc4c32217fe0488bce9cb7202e59468b1ead119000477db2ca797fac19eda3f58c1":"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad":"":"":PSA_ERROR_NOT_SUPPORTED:PSA_ERROR_NOT_SUPPORTED \ No newline at end of file diff --git a/tests/suites/test_suite_psa_crypto_driver_wrappers.function b/tests/suites/test_suite_psa_crypto_driver_wrappers.function index 406432f8d0..9e433bcd9d 100644 --- a/tests/suites/test_suite_psa_crypto_driver_wrappers.function +++ b/tests/suites/test_suite_psa_crypto_driver_wrappers.function @@ -2328,3 +2328,123 @@ exit: PSA_DONE( ); } /* END_CASE */ + +/* BEGIN_CASE */ +void aead_encrypt_setup( int key_type_arg, data_t *key_data, + int alg_arg, + data_t *nonce, + data_t *additional_data, + data_t *input_data, + data_t *expected_ciphertext, + data_t *expected_tag, + int forced_status_arg, + int expected_status_arg ) +{ + mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; + psa_key_type_t key_type = key_type_arg; + psa_algorithm_t alg = alg_arg; + size_t key_bits; + psa_status_t forced_status = forced_status_arg; + psa_status_t expected_status = expected_status_arg; + uint8_t *output_data = NULL; + size_t output_size = 0; + size_t output_length = 0; + size_t finish_output_length = 0; + psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; + psa_status_t status = PSA_ERROR_GENERIC_ERROR; + size_t tag_length = 0; + uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; + + psa_aead_operation_t operation = psa_aead_operation_init(); + + mbedtls_test_driver_aead_hooks = mbedtls_test_driver_aead_hooks_init(); + + PSA_INIT( ); + + mbedtls_test_driver_aead_hooks.forced_status = forced_status; + + psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); + psa_set_key_algorithm( &attributes, alg ); + psa_set_key_type( &attributes, key_type ); + + PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, + &key ) ); + PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); + key_bits = psa_get_key_bits( &attributes ); + + output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits, + alg ); + + /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE + * should be exact. */ + TEST_EQUAL( output_size, + PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); + TEST_ASSERT( output_size <= + PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); + ASSERT_ALLOC( output_data, output_size ); + + status = psa_aead_encrypt_setup( &operation, key, alg ); + + TEST_EQUAL( status, expected_status ); + TEST_EQUAL( mbedtls_test_driver_aead_hooks.hits_encrypt_setup, 1 ); + + if( status == PSA_SUCCESS ) + { + /* Set the nonce. */ + PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); + + TEST_EQUAL( mbedtls_test_driver_aead_hooks.hits_set_nonce, + forced_status == PSA_SUCCESS ? 1 : 0 ); + + /* Check hooks hits and + * set length (additional data and data to encrypt) */ + PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, + input_data->len ) ); + + TEST_EQUAL( mbedtls_test_driver_aead_hooks.hits_set_lengths, + forced_status == PSA_SUCCESS ? 1 : 0 ); + + /* Pass the additional data */ + PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, + additional_data->len ) ); + + TEST_EQUAL( mbedtls_test_driver_aead_hooks.hits_update_ad, + forced_status == PSA_SUCCESS ? 1 : 0 ); + + /* Pass the data to encrypt */ + PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len, + output_data, output_size, &output_length ) ); + + TEST_EQUAL( mbedtls_test_driver_aead_hooks.hits_update, + forced_status == PSA_SUCCESS ? 1 : 0 ); + + /* Finish the encryption operation */ + PSA_ASSERT( psa_aead_finish( &operation, output_data + output_length, + output_size - output_length, + &finish_output_length, tag_buffer, + PSA_AEAD_TAG_MAX_SIZE, &tag_length ) ); + + TEST_EQUAL( mbedtls_test_driver_aead_hooks.hits_finish, + forced_status == PSA_SUCCESS ? 1 : 0 ); + + TEST_EQUAL( mbedtls_test_driver_aead_hooks.hits_abort, + forced_status == PSA_SUCCESS ? 1 : 0 ); + + /* Compare output_data and expected_ciphertext */ + ASSERT_COMPARE( expected_ciphertext->x, expected_ciphertext->len, + output_data, output_length ); + + TEST_EQUAL( output_length + finish_output_length, expected_ciphertext->len ); + + /* Compare tag and expected_tag */ + ASSERT_COMPARE( expected_tag->x, expected_tag->len, tag_buffer, tag_length ); + } + +exit: + /* Cleanup */ + PSA_ASSERT( psa_destroy_key( key ) ); + mbedtls_free( output_data ); + PSA_DONE( ); + mbedtls_test_driver_aead_hooks = mbedtls_test_driver_aead_hooks_init(); +} +/* END_CASE */