From 3205a6592ba7b410bceadd49c8f263fcc45ced13 Mon Sep 17 00:00:00 2001 From: Moran Peker Date: Tue, 20 Mar 2018 17:09:59 +0200 Subject: [PATCH] tests fix --- library/psa_crypto.c | 6 +-- tests/suites/test_suite_psa_crypto.data | 5 +-- tests/suites/test_suite_psa_crypto.function | 44 ++++++++++----------- 3 files changed, 24 insertions(+), 31 deletions(-) diff --git a/library/psa_crypto.c b/library/psa_crypto.c index 57847fa1f..96eea5b9f 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -1425,7 +1425,6 @@ psa_status_t psa_encrypt_generate_iv(psa_cipher_operation_t *operation, } *iv_length = operation->iv_size; - return psa_encrypt_set_iv( operation, iv, *iv_length); } @@ -1476,16 +1475,13 @@ psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation, size_t *output_length) { int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; - - if ( output_size < operation->block_size ) - return ( PSA_ERROR_BUFFER_TOO_SMALL ); if( ! operation->key_set ) return( PSA_ERROR_BAD_STATE ); if( ! operation->iv_set ) return( PSA_ERROR_BAD_STATE ); - ret = mbedtls_cipher_finish( &operation->ctx.cipher, output, + ret = mbedtls_cipher_finish( &operation->ctx.cipher, output, output_length ); if (ret != 0) { diff --git a/tests/suites/test_suite_psa_crypto.data b/tests/suites/test_suite_psa_crypto.data index 533bb71da..d8cab1fd4 100644 --- a/tests/suites/test_suite_psa_crypto.data +++ b/tests/suites/test_suite_psa_crypto.data @@ -107,9 +107,8 @@ 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":"6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411" +cipher_test_positive:PSA_ALG_CBC_BASE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a" PSA Symmetric encryption/decryption: AES-128 depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC -cipher_test_verify_output:PSA_ALG_CBC_BASE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411" - +cipher_test_verify_output:PSA_ALG_CBC_BASE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a" diff --git a/tests/suites/test_suite_psa_crypto.function b/tests/suites/test_suite_psa_crypto.function index 66ab296ed..5b4702bef 100644 --- a/tests/suites/test_suite_psa_crypto.function +++ b/tests/suites/test_suite_psa_crypto.function @@ -542,11 +542,12 @@ void cipher_test_positive( int alg_arg, int key_type_arg, size_t iv_length = 0; unsigned char *input = NULL; size_t input_size = 0; - unsigned char *output = NULL; + unsigned char *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 ); @@ -558,20 +559,19 @@ void cipher_test_positive( int alg_arg, int key_type_arg, 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_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, - iv_length) == PSA_SUCCESS ); + output_size = input_size; + 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_size - output_length, + TEST_ASSERT( psa_cipher_finish( &operation, output + output_length, + output_size, &output_length) == PSA_SUCCESS ); TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); @@ -599,10 +599,10 @@ void cipher_test_verify_output( int alg_arg, int key_type_arg, size_t iv_length = 0; unsigned char *input = NULL; size_t input_size = 0; - unsigned char *output1 = NULL; + unsigned char *output1; size_t output1_size = 0; size_t output1_length = 0; - unsigned char *output2 = NULL; + unsigned char *output2; size_t output2_size = 0; size_t output2_length = 0; size_t tmp_output_length = 0; @@ -620,39 +620,37 @@ void cipher_test_verify_output( int alg_arg, int key_type_arg, TEST_ASSERT( psa_import_key( key_slot, key_type, key, key_size ) == PSA_SUCCESS ); - TEST_ASSERT( psa_decrypt_setup( &operation1, key_slot, alg ) == PSA_SUCCESS ); + TEST_ASSERT( psa_encrypt_setup( &operation1, key_slot, alg ) == PSA_SUCCESS ); + TEST_ASSERT( psa_decrypt_setup( &operation2, key_slot, alg ) == PSA_SUCCESS ); TEST_ASSERT( psa_encrypt_generate_iv( &operation1, iv, iv_size, &iv_length) == PSA_SUCCESS ); - - TEST_ASSERT( psa_encrypt_set_iv( &operation1, iv, - iv_length) == PSA_SUCCESS ); - + output1_size = input_size; + output1 = mbedtls_calloc(0, output1_size); TEST_ASSERT( psa_cipher_update( &operation1, input, input_size, output1, output1_size, &output1_length) == PSA_SUCCESS ); - - TEST_ASSERT( psa_cipher_finish( &operation1, output1 + output1_length, + TEST_ASSERT( psa_cipher_finish( &operation1, output1 + output1_length, output1_size - output1_length, &tmp_output_length) == PSA_SUCCESS ); - + output1_length += tmp_output_length; TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); - TEST_ASSERT( psa_decrypt_setup( &operation2, key_slot, alg ) == PSA_SUCCESS ); + output2_size = output1_length; + output2 = mbedtls_calloc(0, output2_size); - TEST_ASSERT( psa_encrypt_set_iv( &operation2, iv, + TEST_ASSERT( psa_encrypt_set_iv( &operation2, iv, iv_length) == PSA_SUCCESS ); - TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length, output2, output2_size, &output2_length) == PSA_SUCCESS ); tmp_output_length = 0; - TEST_ASSERT( psa_cipher_finish( &operation2, output2 + output2_length, + TEST_ASSERT( psa_cipher_finish( &operation2, output2 + output2_length, output2_size - output2_length, &tmp_output_length) == PSA_SUCCESS ); - - output2_length += tmp_output_length; + + output2_length += tmp_output_length; TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );