From 371a6e4067fb0ddd1582caf9f22a20db8f20ac89 Mon Sep 17 00:00:00 2001 From: mohammad1603 Date: Mon, 4 Jun 2018 15:11:08 +0300 Subject: [PATCH] add decrypt tests for CCM --- tests/suites/test_suite_psa_crypto.data | 14 ++-- tests/suites/test_suite_psa_crypto.function | 74 +++++++++++++++++++++ 2 files changed, 84 insertions(+), 4 deletions(-) diff --git a/tests/suites/test_suite_psa_crypto.data b/tests/suites/test_suite_psa_crypto.data index c0c5f92bc..4aafeffaa 100755 --- a/tests/suites/test_suite_psa_crypto.data +++ b/tests/suites/test_suite_psa_crypto.data @@ -139,8 +139,14 @@ aead_encrypt_decrypt:PSA_KEY_TYPE_DES:"D7828D13B2B0BDC325A76236DF93CC6B":PSA_ALG PSA AEAD Encrypt-Decrypt, Fail Scenario - Invalid algorithm aead_encrypt_decrypt:PSA_KEY_TYPE_AES:"D7828D13B2B0BDC325A76236DF93CC6B":PSA_ALG_CTR:"B96B49E21D621741632875DB7F6C9243D2D7C2":"EC46BB63B02520C33C49FD70":PSA_ERROR_NOT_SUPPORTED -#PSA AEAD Encrypt, AES CCM - scenario 1 -#aead_encrypt:PSA_KEY_TYPE_AES:"D7828D13B2B0BDC325A76236DF93CC6B":PSA_ALG_CCM:"08E8CF97D820EA258460E96AD9CF5289054D895CEAC47C":"0BE1A88BACE018B1":"00412B4EA9CDBE3C9696766CFA":"4CB97F86A2A4689A877947AB8091EF5386A6FFBDD080F8120333D1FCB691F3406CBF531F83A4D8" +PSA AEAD Encrypt, AES CCM - scenario 1 +aead_encrypt:PSA_KEY_TYPE_AES:"D7828D13B2B0BDC325A76236DF93CC6B":PSA_ALG_CCM:"08E8CF97D820EA258460E96AD9CF5289054D895CEAC47C":"0BE1A88BACE018B1":"00412B4EA9CDBE3C9696766CFA":"4CB97F86A2A4689A877947AB8091EF5386A6FFBDD080F8120333D1FCB691F3406CBF531F83A4D8" -#PSA AEAD Encrypt, AES CCM - scenario 2 -#aead_encrypt:PSA_KEY_TYPE_AES:"4189351B5CAEA375A0299E81C621BF43":PSA_ALG_CCM:"4535d12b4377928a7c0a61c9f825a48671ea05910748c8ef":"40a27c1d1e23ea3dbe8056b2774861a4a201cce49f19997d19206d8c8a343951":"48c0906930561e0ab0ef4cd972":"26c56961c035a7e452cce61bc6ee220d77b3f94d18fd10b6d80e8bf80f4a46cab06d4313f0db9be9" +PSA AEAD Encrypt, AES CCM - scenario 2 +aead_encrypt:PSA_KEY_TYPE_AES:"4189351B5CAEA375A0299E81C621BF43":PSA_ALG_CCM:"4535d12b4377928a7c0a61c9f825a48671ea05910748c8ef":"40a27c1d1e23ea3dbe8056b2774861a4a201cce49f19997d19206d8c8a343951":"48c0906930561e0ab0ef4cd972":"26c56961c035a7e452cce61bc6ee220d77b3f94d18fd10b6d80e8bf80f4a46cab06d4313f0db9be9" + +PSA AEAD Decrypt, AES CCM - scenario 1 +aead_decrypt:PSA_KEY_TYPE_AES:"D7828D13B2B0BDC325A76236DF93CC6B":PSA_ALG_CCM:"4CB97F86A2A4689A877947AB8091EF5386A6FFBDD080F8120333D1FCB691F3406CBF531F83A4D8":"0BE1A88BACE018B1":"00412B4EA9CDBE3C9696766CFA":"08E8CF97D820EA258460E96AD9CF5289054D895CEAC47C" + +PSA AEAD Decrypt, AES CCM - scenario 2 +aead_decrypt:PSA_KEY_TYPE_AES:"4189351B5CAEA375A0299E81C621BF43":PSA_ALG_CCM:"26c56961c035a7e452cce61bc6ee220d77b3f94d18fd10b6d80e8bf80f4a46cab06d4313f0db9be9":"40a27c1d1e23ea3dbe8056b2774861a4a201cce49f19997d19206d8c8a343951":"48c0906930561e0ab0ef4cd972":"4535d12b4377928a7c0a61c9f825a48671ea05910748c8ef" diff --git a/tests/suites/test_suite_psa_crypto.function b/tests/suites/test_suite_psa_crypto.function index 16577dd91..f276bee87 100755 --- a/tests/suites/test_suite_psa_crypto.function +++ b/tests/suites/test_suite_psa_crypto.function @@ -736,3 +736,77 @@ exit: mbedtls_psa_crypto_free( ); } /* END_CASE */ + +/* BEGIN_CASE */ +void aead_decrypt( int key_type_arg, char * key_hex, + int alg_arg, char * input_hex, + char * add_data, char * nonce_hex, + char * expected_result_hex ) +{ + int slot = 1; + psa_key_type_t key_type = key_type_arg; + psa_algorithm_t alg = alg_arg; + unsigned char *key_data = NULL; + size_t key_size; + unsigned char *input_data = NULL; + size_t input_size; + unsigned char *output_data = NULL; + size_t output_size = 0; + size_t output_length = 0; + unsigned char *expected_result = NULL; + size_t expected_result_length = 0; + uint8_t* nonce = NULL; + size_t nonce_length = 0; + size_t tag_length = 16; + unsigned char *additional_data = NULL; + size_t additional_data_length = 0; + psa_key_policy_t policy = {0}; + + + key_data = unhexify_alloc( key_hex, &key_size ); + TEST_ASSERT( key_data != NULL ); + input_data = unhexify_alloc( input_hex, &input_size ); + TEST_ASSERT( input_data != NULL ); + additional_data = unhexify_alloc( add_data, &additional_data_length ); + TEST_ASSERT( input_data != NULL ); + output_size = input_size + tag_length; + output_data = mbedtls_calloc( 1, output_size ); + TEST_ASSERT( output_data != NULL ); + nonce = unhexify_alloc( nonce_hex, &nonce_length ); + TEST_ASSERT( nonce != NULL ); + expected_result = unhexify_alloc( expected_result_hex, &expected_result_length ); + TEST_ASSERT( expected_result != NULL ); + + TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); + + psa_key_policy_init( &policy ); + + psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg ); + + TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); + + TEST_ASSERT( psa_import_key( slot, key_type, + key_data, key_size ) == PSA_SUCCESS ); + + TEST_ASSERT( psa_aead_decrypt( slot, alg, + nonce, nonce_length, + additional_data, additional_data_length, + input_data, input_size, output_data, + output_size, &output_length ) == PSA_SUCCESS ); + + + TEST_ASSERT( memcmp( output_data, expected_result, + output_length ) == 0 ); + + +exit: + psa_destroy_key( slot ); + mbedtls_free( key_data ); + mbedtls_free( input_data ); + mbedtls_free( additional_data ); + mbedtls_free( output_data ); + mbedtls_free( nonce ); + mbedtls_free( expected_result ); + mbedtls_psa_crypto_free( ); +} +/* END_CASE */