From b152d4d8b6116746584a2011333295d855635bcc Mon Sep 17 00:00:00 2001 From: mohammad1603 Date: Wed, 11 Apr 2018 23:51:20 -0700 Subject: [PATCH] add test scenarios to decrypt and encrypt input and compare with given output --- library/psa_crypto.c | 2 +- tests/suites/test_suite_psa_crypto.data | 6 +- tests/suites/test_suite_psa_crypto.function | 82 +++++++++++++++++++-- 3 files changed, 82 insertions(+), 8 deletions(-) diff --git a/library/psa_crypto.c b/library/psa_crypto.c index 96d2c0f69..9ad44e7f3 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -1365,7 +1365,7 @@ static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation, operation->key_set = 1; operation->alg = alg; - operation->block_size = PSA_BLOCK_CIPHER_BLOCK_SIZE(key_type); + operation->block_size = PSA_ALG_IS_BLOCK_CIPHER( alg ) ? PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) : 1; if( PSA_ALG_IS_BLOCK_CIPHER( alg ) || ( alg == PSA_ALG_CTR ) ) { operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ); diff --git a/tests/suites/test_suite_psa_crypto.data b/tests/suites/test_suite_psa_crypto.data index d8cab1fd4..3bf93b842 100644 --- a/tests/suites/test_suite_psa_crypto.data +++ b/tests/suites/test_suite_psa_crypto.data @@ -107,7 +107,11 @@ key_lifetime_set_fail:1:PSA_KEY_LIFETIME_PERSISTENT+1:PSA_ERROR_INVALID_ARGUMENT PSA Symmetric encryption: AES-128 depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC -cipher_test_positive:PSA_ALG_CBC_BASE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a" +cipher_test_encrypt:PSA_ALG_CBC_BASE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a":"a076ec9dfbe47d52afc357336f20743b" + +PSA Symmetric encryption: AES-128 +depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC +cipher_test_decrypt:PSA_ALG_CBC_BASE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"396ee84fb75fdbb5c2b13c7fe5a654aa":"49e4e66c89a86b67758df89db9ad6955" PSA Symmetric encryption/decryption: AES-128 depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC diff --git a/tests/suites/test_suite_psa_crypto.function b/tests/suites/test_suite_psa_crypto.function index e3294a79b..eb217f9f9 100644 --- a/tests/suites/test_suite_psa_crypto.function +++ b/tests/suites/test_suite_psa_crypto.function @@ -538,11 +538,10 @@ void cipher_test_positive( int alg_arg, int key_type_arg, unsigned char *key = NULL; size_t key_size; unsigned char iv[16] = {0}; - size_t iv_size = 16; - size_t iv_length = 0; unsigned char *input = NULL; size_t input_size = 0; - unsigned char *output ; + unsigned char *output; + unsigned char *expected_output; size_t output_size = 0; size_t output_length = 0; psa_cipher_operation_t operation; @@ -553,6 +552,11 @@ void cipher_test_positive( int alg_arg, int key_type_arg, input = unhexify_alloc( input_hex, &input_size ); TEST_ASSERT( input != NULL ); + + expected_output = unhexify_alloc( output_hex, &output_size ); + TEST_ASSERT( expected_output != NULL ); + + memset( iv, 0x2a, sizeof( iv ) ); TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); @@ -561,10 +565,9 @@ void cipher_test_positive( int alg_arg, int key_type_arg, TEST_ASSERT( psa_encrypt_setup( &operation, key_slot, alg ) == PSA_SUCCESS ); - TEST_ASSERT( psa_encrypt_generate_iv( &operation, iv, - iv_size, &iv_length) == PSA_SUCCESS ); + TEST_ASSERT( psa_encrypt_set_iv( &operation, iv, + sizeof( iv ) ) == PSA_SUCCESS ); - output_size = input_size; output = mbedtls_calloc(0, output_size); TEST_ASSERT( psa_cipher_update( &operation, input, input_size, @@ -575,6 +578,9 @@ void cipher_test_positive( int alg_arg, int key_type_arg, TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); + TEST_ASSERT( input_size == output_size ); + TEST_ASSERT( memcmp( expected_output, output, output_size ) == 0 ); + exit: mbedtls_free( key ); mbedtls_free( input ); @@ -583,6 +589,70 @@ exit: } /* END_CASE */ + +/* BEGIN_CASE */ +void cipher_test_decrypt( int alg_arg, int key_type_arg, + char *key_hex, + char *input_hex, char *output_hex ) +{ + int key_slot = 1; + psa_key_type_t key_type = key_type_arg; + psa_algorithm_t alg = alg_arg; + unsigned char *key = NULL; + size_t key_size; + unsigned char iv[16] = {0}; + unsigned char *input = NULL; + size_t input_size = 0; + unsigned char *output; + unsigned char *expected_output; + size_t output_size = 0; + size_t output_length = 0; + psa_cipher_operation_t operation; + + + key = unhexify_alloc( key_hex, &key_size ); + TEST_ASSERT( key != NULL ); + + input = unhexify_alloc( input_hex, &input_size ); + TEST_ASSERT( input != NULL ); + + expected_output = unhexify_alloc( output_hex, &output_size ); + TEST_ASSERT( expected_output != NULL ); + + memset( iv, 0x2a, sizeof( iv ) ); + + TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); + + TEST_ASSERT( psa_import_key( key_slot, key_type, + key, key_size ) == PSA_SUCCESS ); + + TEST_ASSERT( psa_decrypt_setup( &operation, key_slot, alg ) == PSA_SUCCESS ); + + TEST_ASSERT( psa_encrypt_set_iv( &operation, iv, + sizeof( iv ) ) == PSA_SUCCESS ); + + output = mbedtls_calloc(0, output_size); + + TEST_ASSERT( psa_cipher_update( &operation, input, input_size, + output, output_size, + &output_length) == PSA_SUCCESS ); + TEST_ASSERT( psa_cipher_finish( &operation, output + output_length, + &output_length) == PSA_SUCCESS ); + + TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); + + TEST_ASSERT( input_size == output_size ); + TEST_ASSERT( memcmp( expected_output, output, output_size ) == 0 ); + +exit: + mbedtls_free( key ); + mbedtls_free( input ); + psa_destroy_key( key_slot ); + mbedtls_psa_crypto_free( ); +} +/* END_CASE */ + + /* BEGIN_CASE */ void cipher_test_verify_output( int alg_arg, int key_type_arg, char *key_hex,