From 503973bdf33e08ffa99f686ddf52834131d9964a Mon Sep 17 00:00:00 2001 From: mohammad1603 Date: Mon, 12 Mar 2018 15:59:30 +0200 Subject: [PATCH 01/54] initial implementation for PSA symmetric APIs - missing tests and documentations --- include/psa/crypto.h | 11 ++- include/psa/crypto_struct.h | 2 +- library/psa_crypto.c | 146 ++++++++++++++++++++++++++++++++++++ 3 files changed, 154 insertions(+), 5 deletions(-) diff --git a/include/psa/crypto.h b/include/psa/crypto.h index f8b8ceadc3..089484f197 100644 --- a/include/psa/crypto.h +++ b/include/psa/crypto.h @@ -1057,12 +1057,15 @@ psa_status_t psa_encrypt_set_iv(psa_cipher_operation_t *operation, psa_status_t psa_cipher_update(psa_cipher_operation_t *operation, const uint8_t *input, - size_t input_length); + size_t input_length, + unsigned char *output, + size_t output_size, + size_t *output_length); psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation, - uint8_t *mac, - size_t mac_size, - size_t *mac_length); + uint8_t *output, + size_t output_size, + size_t *output_length); psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation); diff --git a/include/psa/crypto_struct.h b/include/psa/crypto_struct.h index eba4862c62..2975bdcb0a 100644 --- a/include/psa/crypto_struct.h +++ b/include/psa/crypto_struct.h @@ -106,7 +106,7 @@ struct psa_cipher_operation_s uint8_t block_size; union { - unsigned dummy; /* Make the union non-empty even with no supported algorithms. */ + mbedtls_cipher_context_t cipher; } ctx; }; diff --git a/library/psa_crypto.c b/library/psa_crypto.c index 7e633a3ce2..428a237dfe 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -1289,6 +1289,152 @@ psa_status_t psa_asymmetric_sign(psa_key_slot_t key, } +/****************************************************************/ +/* Symmetric cryptography */ +/****************************************************************/ + +psa_status_t psa_decrypt_setup(psa_cipher_operation_t *operation, + psa_key_slot_t key, + psa_algorithm_t alg); +{ + int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; + psa_status_t status; + key_slot_t *slot; + psa_key_type_t key_type; + size_t key_bits; + const mbedtls_cipher_info_t *cipher_info = NULL; + + operation->alg = 0; + operation->key_set = 0; + operation->iv_set = 0; + operation->block_size = 0; + operation->iv_size = 0; + + status = psa_get_key_information( key, &key_type, &key_bits ); + if( status != PSA_SUCCESS ) + return( status ); + slot = &global_data.key_slots[key]; + + cipher_info = mbedtls_cipher_info_from_psa( alg, key_type, key_bits ); + if( cipher_info == NULL ) + return( PSA_ERROR_NOT_SUPPORTED ); + + operation->block_size = cipher_info->block_size; + + mbedtls_cipher_init( &operation->ctx.cipher ); + ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info ); + if (ret != 0) + { + psa_cipher_abort( operation ); + return( mbedtls_to_psa_error( ret ) ); + } + + ret = mbedtls_cipher_setkey( &operation->ctx.cipher, slot->data.raw.data, + key_bits, MBEDTLS_DECRYPT ); + if (ret != 0) + { + psa_cipher_abort( operation ); + return( mbedtls_to_psa_error( ret ) ); + } + + operation->key_set = 1; + operation->alg = alg; + + return ( PSA_SUCCESS ); +} + +psa_status_t psa_encrypt_generate_iv(psa_cipher_operation_t *operation, + unsigned char *iv, + size_t iv_size, + size_t *iv_length) +{ + int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; + + ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, iv, iv_size); + if (ret != 0) + { + return( mbedtls_to_psa_error( ret ) ); + } + + *iv_length = iv_size; + retuen ( PSA_SUCCESS ); +} + +psa_status_t psa_encrypt_set_iv(psa_cipher_operation_t *operation, + const unsigned char *iv, + size_t iv_length) +{ + int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; + + ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length ); + if (ret != 0) + { + psa_cipher_abort( operation ); + return( mbedtls_to_psa_error( ret ) ); + } + + operation->iv_set = 1; + operation->iv_size = iv_length; + + return ( PSA_SUCCESS ); +} + +psa_status_t psa_cipher_update(psa_cipher_operation_t *operation, + const uint8_t *input, + size_t input_length, + unsigned char *output, + size_t output_size, + size_t *output_length) +{ + int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; + + ret = mbedtls_cipher_update( &operation->ctx.cipher, input, + input_length, output, output_length ); + if (ret != 0) + { + psa_cipher_abort( operation ); + return( mbedtls_to_psa_error( ret ) ); + } + + return ( PSA_SUCCESS ); +} + +psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation, + uint8_t *output, + size_t output_size, + size_t *output_length) +{ + int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; + + 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, + output_length ); + if (ret != 0) + { + psa_cipher_abort( operation ); + return( mbedtls_to_psa_error( ret ) ); + } + + return ( PSA_SUCCESS ); +} + +psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation) +{ + mbedtls_cipher_free( &operation->ctx.cipher ); + + operation->alg = 0; + operation->key_set = 0; + operation->iv_set = 0; + operation->block_size = 0; + operation->iv_size = 0; + + return ( PSA_SUCCESS ); +} + /****************************************************************/ /* Key Policy */ From 8275961178a62874496bf4c5090775ec8bce09da Mon Sep 17 00:00:00 2001 From: mohammad1603 Date: Mon, 12 Mar 2018 18:16:40 +0200 Subject: [PATCH 02/54] warnings fixes --- include/psa/crypto.h | 3 +-- library/psa_crypto.c | 15 ++++++++++----- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/include/psa/crypto.h b/include/psa/crypto.h index 089484f197..97819b74f2 100644 --- a/include/psa/crypto.h +++ b/include/psa/crypto.h @@ -1046,8 +1046,7 @@ psa_status_t psa_decrypt_setup(psa_cipher_operation_t *operation, psa_key_slot_t key, psa_algorithm_t alg); -psa_status_t psa_encrypt_generate_iv(psa_cipher_operation_t *operation, - unsigned char *iv, +psa_status_t psa_encrypt_generate_iv(unsigned char *iv, size_t iv_size, size_t *iv_length); diff --git a/library/psa_crypto.c b/library/psa_crypto.c index 428a237dfe..d349d19573 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -1295,7 +1295,7 @@ psa_status_t psa_asymmetric_sign(psa_key_slot_t key, psa_status_t psa_decrypt_setup(psa_cipher_operation_t *operation, psa_key_slot_t key, - psa_algorithm_t alg); + psa_algorithm_t alg) { int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; psa_status_t status; @@ -1343,13 +1343,12 @@ psa_status_t psa_decrypt_setup(psa_cipher_operation_t *operation, return ( PSA_SUCCESS ); } -psa_status_t psa_encrypt_generate_iv(psa_cipher_operation_t *operation, - unsigned char *iv, +psa_status_t psa_encrypt_generate_iv(unsigned char *iv, size_t iv_size, size_t *iv_length) { int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; - + ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, iv, iv_size); if (ret != 0) { @@ -1357,7 +1356,7 @@ psa_status_t psa_encrypt_generate_iv(psa_cipher_operation_t *operation, } *iv_length = iv_size; - retuen ( PSA_SUCCESS ); + return ( PSA_SUCCESS ); } psa_status_t psa_encrypt_set_iv(psa_cipher_operation_t *operation, @@ -1388,6 +1387,9 @@ psa_status_t psa_cipher_update(psa_cipher_operation_t *operation, { int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; + if ( output_size < input_length ) + return ( PSA_ERROR_BUFFER_TOO_SMALL ); + ret = mbedtls_cipher_update( &operation->ctx.cipher, input, input_length, output, output_length ); if (ret != 0) @@ -1405,6 +1407,9 @@ 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 ); From d7d7ba5749287334271a559c64fd6379ae932381 Mon Sep 17 00:00:00 2001 From: mohammad1603 Date: Mon, 12 Mar 2018 18:51:53 +0200 Subject: [PATCH 03/54] add positive test scenarios --- tests/suites/test_suite_psa_crypto.data | 9 ++ tests/suites/test_suite_psa_crypto.function | 141 ++++++++++++++++++++ 2 files changed, 150 insertions(+) diff --git a/tests/suites/test_suite_psa_crypto.data b/tests/suites/test_suite_psa_crypto.data index 38f4b80901..533bb71dae 100644 --- a/tests/suites/test_suite_psa_crypto.data +++ b/tests/suites/test_suite_psa_crypto.data @@ -104,3 +104,12 @@ key_lifetime_set_fail:1:PSA_KEY_LIFETIME_WRITE_ONCE:PSA_ERROR_NOT_SUPPORTED PSA Key Lifetime set fail, invalid key lifetime value 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" + +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" + diff --git a/tests/suites/test_suite_psa_crypto.function b/tests/suites/test_suite_psa_crypto.function index de388dbc3e..6f364938ad 100644 --- a/tests/suites/test_suite_psa_crypto.function +++ b/tests/suites/test_suite_psa_crypto.function @@ -526,3 +526,144 @@ exit: mbedtls_psa_crypto_free( ); } /* END_CASE */ + +* BEGIN_CASE */ +void cipher_test_positive( psa_algorithm_t alg_arg, int key_type_arg, + char *key_hex, + char *input_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] = NULL; + size_t iv_size = 16; + size_t iv_length = 0; + unsigned char *input = NULL; + size_t input_size = 0; + unsigned char *output = NULL; + 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 ); + + 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_generate_iv( &operation, iv, + iv_size, &iv_length) == PSA_SUCCESS ); + + TEST_ASSERT( psa_encrypt_set_iv( &operation, iv, + iv_length) == PSA_SUCCESS ); + + 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, + &output_length) == PSA_SUCCESS ); + + TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); + +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( psa_algorithm_t alg_arg, int key_type_arg, + char *key_hex, + char *input_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] = NULL; + size_t iv_size = 16; + size_t iv_length = 0; + unsigned char *input = NULL; + size_t input_size = 0; + unsigned char *output1 = NULL; + size_t output1_size = 0; + size_t output1_length = 0; + unsigned char *output2 = NULL; + size_t output2_size = 0; + size_t output2_length = 0; + size_t tmp_output_length = 0; + psa_cipher_operation_t operation1; + psa_cipher_operation_t operation2; + + key = unhexify_alloc( key_hex, &key_size ); + TEST_ASSERT( key != NULL ); + + input = unhexify_alloc( input_hex, &input_size ); + TEST_ASSERT( input != NULL ); + + 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( &operation1, 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 ); + + 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, + 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 ); + + TEST_ASSERT( psa_encrypt_set_iv( &operation2, iv, + iv_length) == PSA_SUCCESS ); + + TEST_ASSERT( psa_cipher_update( &operation2, output, output_length, + output2, output2_size, &output2_length) == PSA_SUCCESS ); + tmp_output_length = 0; + TEST_ASSERT( psa_cipher_finish( &operation, output2 + output2_length, + output2_size - output2_length, + &tmp_output_length) == PSA_SUCCESS ); + + output2_length += tmp_output_length; + + TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); + + TEST_ASSERT( input_size == output1_length ); + TEST_ASSERT( output1_length == output2_length ); + TEST_ASSERT( memcmp( input, output, input_size ) == 0 ); + +exit: + mbedtls_free( key ); + mbedtls_free( input ); + psa_destroy_key( key_slot ); + mbedtls_psa_crypto_free( ); +} +/* END_CASE */ From e6b67a1e78808da6e750f53a5151a518c23558a8 Mon Sep 17 00:00:00 2001 From: mohammad1603 Date: Mon, 12 Mar 2018 10:38:49 -0700 Subject: [PATCH 04/54] Fix parameters in test suite Fix test function signature in test suite --- tests/suites/test_suite_psa_crypto.function | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/suites/test_suite_psa_crypto.function b/tests/suites/test_suite_psa_crypto.function index 6f364938ad..ce33228b28 100644 --- a/tests/suites/test_suite_psa_crypto.function +++ b/tests/suites/test_suite_psa_crypto.function @@ -528,7 +528,7 @@ exit: /* END_CASE */ * BEGIN_CASE */ -void cipher_test_positive( psa_algorithm_t alg_arg, int key_type_arg, +void cipher_test_positive( int alg_arg, int key_type_arg, char *key_hex, char *input_hex ) { @@ -537,7 +537,7 @@ void cipher_test_positive( psa_algorithm_t alg_arg, int key_type_arg, psa_algorithm_t alg = alg_arg; unsigned char *key = NULL; size_t key_size; - unsigned char *iv[16] = NULL; + unsigned char iv[16] = {0}; size_t iv_size = 16; size_t iv_length = 0; unsigned char *input = NULL; @@ -560,7 +560,7 @@ void cipher_test_positive( psa_algorithm_t alg_arg, int key_type_arg, TEST_ASSERT( psa_decrypt_setup( &operation, key_slot, alg ) == PSA_SUCCESS ); - TEST_ASSERT( psa_encrypt_generate_iv( &operation, iv, + TEST_ASSERT( psa_encrypt_generate_iv( iv, iv_size, &iv_length) == PSA_SUCCESS ); TEST_ASSERT( psa_encrypt_set_iv( &operation, iv, @@ -574,7 +574,7 @@ void cipher_test_positive( psa_algorithm_t alg_arg, int key_type_arg, output_size - output_length, &output_length) == PSA_SUCCESS ); - TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); + TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); exit: mbedtls_free( key ); @@ -585,7 +585,7 @@ exit: /* END_CASE */ /* BEGIN_CASE */ -void cipher_test_verify_output( psa_algorithm_t alg_arg, int key_type_arg, +void cipher_test_verify_output( int alg_arg, int key_type_arg, char *key_hex, char *input_hex ) { @@ -594,7 +594,7 @@ void cipher_test_verify_output( psa_algorithm_t alg_arg, int key_type_arg, psa_algorithm_t alg = alg_arg; unsigned char *key = NULL; size_t key_size; - unsigned char *iv[16] = NULL; + unsigned char iv[16] = {0}; size_t iv_size = 16; size_t iv_length = 0; unsigned char *input = NULL; @@ -622,7 +622,7 @@ void cipher_test_verify_output( psa_algorithm_t alg_arg, int key_type_arg, TEST_ASSERT( psa_decrypt_setup( &operation1, key_slot, alg ) == PSA_SUCCESS ); - TEST_ASSERT( psa_encrypt_generate_iv( &operation1, iv, + TEST_ASSERT( psa_encrypt_generate_iv( iv, iv_size, &iv_length) == PSA_SUCCESS ); TEST_ASSERT( psa_encrypt_set_iv( &operation1, iv, @@ -645,10 +645,10 @@ void cipher_test_verify_output( psa_algorithm_t alg_arg, int key_type_arg, TEST_ASSERT( psa_encrypt_set_iv( &operation2, iv, iv_length) == PSA_SUCCESS ); - TEST_ASSERT( psa_cipher_update( &operation2, output, output_length, + 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( &operation, output2 + output2_length, + TEST_ASSERT( psa_cipher_finish( &operation2, output2 + output2_length, output2_size - output2_length, &tmp_output_length) == PSA_SUCCESS ); @@ -658,7 +658,7 @@ void cipher_test_verify_output( psa_algorithm_t alg_arg, int key_type_arg, TEST_ASSERT( input_size == output1_length ); TEST_ASSERT( output1_length == output2_length ); - TEST_ASSERT( memcmp( input, output, input_size ) == 0 ); + TEST_ASSERT( memcmp( input, output2, input_size ) == 0 ); exit: mbedtls_free( key ); From cdd3be9cfb9b1cd23b2b23d84bd71adaf117b7c0 Mon Sep 17 00:00:00 2001 From: mohammad1603 Date: Tue, 13 Mar 2018 04:38:38 -0700 Subject: [PATCH 05/54] Add psa_crypto test suite to Cmake Add psa_crypto test suite to Cmake --- tests/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 58126bedcf..d8b74f227f 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -110,6 +110,7 @@ add_test_suite(pk) add_test_suite(pkparse) add_test_suite(pkwrite) add_test_suite(poly1305) +add_test_suite(psa_crypto) add_test_suite(shax) add_test_suite(ssl) add_test_suite(timing) From 990a18c2f00a8ff539b7bb3a781ac76e3c2357d7 Mon Sep 17 00:00:00 2001 From: mohammad1603 Date: Wed, 14 Mar 2018 15:15:33 +0200 Subject: [PATCH 06/54] add ecb to cipher algorithms --- include/psa/crypto.h | 1 + library/psa_crypto.c | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/include/psa/crypto.h b/include/psa/crypto.h index 97819b74f2..73cf7bd27b 100644 --- a/include/psa/crypto.h +++ b/include/psa/crypto.h @@ -317,6 +317,7 @@ typedef uint32_t psa_algorithm_t; #define PSA_ALG_CFB_BASE ((psa_algorithm_t)0x04000002) #define PSA_ALG_OFB_BASE ((psa_algorithm_t)0x04000003) #define PSA_ALG_XTS_BASE ((psa_algorithm_t)0x04000004) +#define PSA_ALG_ECB_BASE ((psa_algorithm_t)0x04000005) #define PSA_ALG_STREAM_CIPHER ((psa_algorithm_t)0x04800000) #define PSA_ALG_CTR ((psa_algorithm_t)0x04800001) #define PSA_ALG_ARC4 ((psa_algorithm_t)0x04800002) diff --git a/library/psa_crypto.c b/library/psa_crypto.c index d349d19573..37befc0e5c 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -880,13 +880,14 @@ static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa( if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ) { - if( PSA_ALG_IS_BLOCK_CIPHER( alg ) ) - alg &= ~PSA_ALG_BLOCK_CIPHER_MODE_MASK; switch( alg ) { case PSA_ALG_STREAM_CIPHER: mode = MBEDTLS_MODE_STREAM; break; + case PSA_ALG_ECB_BASE: + mode = MBEDTLS_MODE_ECB; + break; case PSA_ALG_CBC_BASE: mode = MBEDTLS_MODE_CBC; break; From efb0107fbe22de6461aab040d0b3714422587ccf Mon Sep 17 00:00:00 2001 From: mohammad1603 Date: Wed, 14 Mar 2018 17:03:52 +0200 Subject: [PATCH 07/54] CR fix, remove exposing ECB --- include/psa/crypto.h | 1 - library/psa_crypto.c | 3 --- 2 files changed, 4 deletions(-) diff --git a/include/psa/crypto.h b/include/psa/crypto.h index 73cf7bd27b..97819b74f2 100644 --- a/include/psa/crypto.h +++ b/include/psa/crypto.h @@ -317,7 +317,6 @@ typedef uint32_t psa_algorithm_t; #define PSA_ALG_CFB_BASE ((psa_algorithm_t)0x04000002) #define PSA_ALG_OFB_BASE ((psa_algorithm_t)0x04000003) #define PSA_ALG_XTS_BASE ((psa_algorithm_t)0x04000004) -#define PSA_ALG_ECB_BASE ((psa_algorithm_t)0x04000005) #define PSA_ALG_STREAM_CIPHER ((psa_algorithm_t)0x04800000) #define PSA_ALG_CTR ((psa_algorithm_t)0x04800001) #define PSA_ALG_ARC4 ((psa_algorithm_t)0x04800002) diff --git a/library/psa_crypto.c b/library/psa_crypto.c index 37befc0e5c..d170505c3f 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -885,9 +885,6 @@ static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa( case PSA_ALG_STREAM_CIPHER: mode = MBEDTLS_MODE_STREAM; break; - case PSA_ALG_ECB_BASE: - mode = MBEDTLS_MODE_ECB; - break; case PSA_ALG_CBC_BASE: mode = MBEDTLS_MODE_CBC; break; From 8481e74eccdbfdfe101e0e481aada00617bae9d4 Mon Sep 17 00:00:00 2001 From: mohammad1603 Date: Sun, 18 Mar 2018 13:57:31 +0200 Subject: [PATCH 08/54] CR fixes more fixes Compilation fixes Compilation fixes for PSA crypto code and tests --- include/psa/crypto.h | 3 +- library/psa_crypto.c | 123 +++++++++++++++----- tests/suites/test_suite_psa_crypto.function | 4 +- 3 files changed, 99 insertions(+), 31 deletions(-) diff --git a/include/psa/crypto.h b/include/psa/crypto.h index 97819b74f2..089484f197 100644 --- a/include/psa/crypto.h +++ b/include/psa/crypto.h @@ -1046,7 +1046,8 @@ psa_status_t psa_decrypt_setup(psa_cipher_operation_t *operation, psa_key_slot_t key, psa_algorithm_t alg); -psa_status_t psa_encrypt_generate_iv(unsigned char *iv, +psa_status_t psa_encrypt_generate_iv(psa_cipher_operation_t *operation, + unsigned char *iv, size_t iv_size, size_t *iv_length); diff --git a/library/psa_crypto.c b/library/psa_crypto.c index d170505c3f..9a812b8660 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -286,7 +286,40 @@ static psa_status_t mbedtls_to_psa_error( int ret ) } } +static void psa_operation_init(void *operation, + psa_algorithm_t alg) +{ + if( PSA_ALG_IS_MAC(alg) ) + { + if ( ((psa_mac_operation_t*)operation)->alg != 0 ) //restart + { + ((psa_mac_operation_t*)operation)->alg = 0; + ((psa_mac_operation_t*)operation)->iv_required = 0; + } + else + { + ((psa_mac_operation_t*)operation)->alg = alg; + ((psa_mac_operation_t*)operation)->iv_required = 1; + } + ((psa_mac_operation_t*)operation)->key_set = 0; + ((psa_mac_operation_t*)operation)->iv_set = 0; + ((psa_mac_operation_t*)operation)->has_input = 0; + ((psa_mac_operation_t*)operation)->mac_size = 0; + } + else if( PSA_ALG_IS_CIPHER(alg) ) + { + if ( ((psa_cipher_operation_t*)operation)->alg != 0 ) //restart + ((psa_cipher_operation_t*)operation)->alg = 0; + else + ((psa_cipher_operation_t*)operation)->alg = alg; + + ((psa_cipher_operation_t*)operation)->key_set = 0; + ((psa_cipher_operation_t*)operation)->iv_set = 0; + ((psa_cipher_operation_t*)operation)->iv_size = 0; + ((psa_cipher_operation_t*)operation)->block_size = 0; + } +} /****************************************************************/ /* Key management */ @@ -880,6 +913,10 @@ static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa( if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ) { + if( PSA_ALG_IS_BLOCK_CIPHER( alg ) ) + { + alg &= ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK; + } switch( alg ) { case PSA_ALG_STREAM_CIPHER: @@ -955,11 +992,7 @@ psa_status_t psa_mac_abort( psa_mac_operation_t *operation ) #endif /* MBEDTLS_MD_C */ return( PSA_ERROR_NOT_SUPPORTED ); } - operation->alg = 0; - operation->key_set = 0; - operation->iv_set = 0; - operation->iv_required = 0; - operation->has_input = 0; + psa_operation_init(operation, 0); return( PSA_SUCCESS ); } @@ -974,11 +1007,7 @@ psa_status_t psa_mac_start( psa_mac_operation_t *operation, size_t key_bits; const mbedtls_cipher_info_t *cipher_info = NULL; - operation->alg = 0; - operation->key_set = 0; - operation->iv_set = 0; - operation->iv_required = 1; - operation->has_input = 0; + psa_operation_init(operation, alg); status = psa_get_key_information( key, &key_type, &key_bits ); if( status != PSA_SUCCESS ) @@ -1291,9 +1320,9 @@ psa_status_t psa_asymmetric_sign(psa_key_slot_t key, /* Symmetric cryptography */ /****************************************************************/ -psa_status_t psa_decrypt_setup(psa_cipher_operation_t *operation, +static psa_status_t psa_setup(psa_cipher_operation_t *operation, psa_key_slot_t key, - psa_algorithm_t alg) + psa_algorithm_t alg, mbedtls_operation_t cipher_operation) { int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; psa_status_t status; @@ -1301,12 +1330,10 @@ psa_status_t psa_decrypt_setup(psa_cipher_operation_t *operation, psa_key_type_t key_type; size_t key_bits; const mbedtls_cipher_info_t *cipher_info = NULL; + psa_algorithm_t padding_mode = PSA_ALG_BLOCK_CIPHER_PAD_NONE; + mbedtls_cipher_padding_t mode = MBEDTLS_PADDING_NONE; - operation->alg = 0; - operation->key_set = 0; - operation->iv_set = 0; - operation->block_size = 0; - operation->iv_size = 0; + psa_operation_init(operation, alg); status = psa_get_key_information( key, &key_type, &key_bits ); if( status != PSA_SUCCESS ) @@ -1328,33 +1355,78 @@ psa_status_t psa_decrypt_setup(psa_cipher_operation_t *operation, } ret = mbedtls_cipher_setkey( &operation->ctx.cipher, slot->data.raw.data, - key_bits, MBEDTLS_DECRYPT ); + key_bits, cipher_operation ); if (ret != 0) { psa_cipher_abort( operation ); return( mbedtls_to_psa_error( ret ) ); } +#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) + if (( alg & PSA_ALG_CBC_BASE) == PSA_ALG_CBC_BASE) + { + padding_mode = alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK; + + switch (padding_mode) + { + case PSA_ALG_BLOCK_CIPHER_PAD_PKCS7: + mode = MBEDTLS_PADDING_PKCS7; + break; + case PSA_ALG_BLOCK_CIPHER_PAD_NONE: + mode = MBEDTLS_PADDING_NONE; + break; + default: + return ( PSA_ERROR_INVALID_PADDING ); + } + ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, mode ); + if (ret != 0) + return( mbedtls_to_psa_error( ret ) ); + } +#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING + operation->key_set = 1; operation->alg = alg; + operation->block_size = PSA_BLOCK_CIPHER_BLOCK_SIZE(key_type); + if ( PSA_ALG_IS_BLOCK_CIPHER(alg) ) + { + operation->iv_size = operation->block_size; + } return ( PSA_SUCCESS ); } -psa_status_t psa_encrypt_generate_iv(unsigned char *iv, +psa_status_t psa_encrypt_setup(psa_cipher_operation_t *operation, + psa_key_slot_t key, + psa_algorithm_t alg) +{ + return psa_setup(operation, key, alg, MBEDTLS_ENCRYPT); +} + +psa_status_t psa_decrypt_setup(psa_cipher_operation_t *operation, + psa_key_slot_t key, + psa_algorithm_t alg) +{ + return psa_setup(operation, key, alg, MBEDTLS_DECRYPT); +} + +psa_status_t psa_encrypt_generate_iv(psa_cipher_operation_t *operation, + unsigned char *iv, size_t iv_size, size_t *iv_length) { int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; + if (iv_size < operation->iv_size) + return ( PSA_ERROR_BUFFER_TOO_SMALL ); - ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, iv, iv_size); + ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, iv, operation->iv_size); if (ret != 0) { return( mbedtls_to_psa_error( ret ) ); } - *iv_length = iv_size; - return ( PSA_SUCCESS ); + *iv_length = operation->iv_size; + + return psa_encrypt_set_iv( operation, iv, *iv_length); } psa_status_t psa_encrypt_set_iv(psa_cipher_operation_t *operation, @@ -1371,7 +1443,6 @@ psa_status_t psa_encrypt_set_iv(psa_cipher_operation_t *operation, } operation->iv_set = 1; - operation->iv_size = iv_length; return ( PSA_SUCCESS ); } @@ -1429,11 +1500,7 @@ psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation) { mbedtls_cipher_free( &operation->ctx.cipher ); - operation->alg = 0; - operation->key_set = 0; - operation->iv_set = 0; - operation->block_size = 0; - operation->iv_size = 0; + psa_operation_init(operation, 0); return ( PSA_SUCCESS ); } diff --git a/tests/suites/test_suite_psa_crypto.function b/tests/suites/test_suite_psa_crypto.function index ce33228b28..66ab296edb 100644 --- a/tests/suites/test_suite_psa_crypto.function +++ b/tests/suites/test_suite_psa_crypto.function @@ -560,7 +560,7 @@ void cipher_test_positive( int alg_arg, int key_type_arg, TEST_ASSERT( psa_decrypt_setup( &operation, key_slot, alg ) == PSA_SUCCESS ); - TEST_ASSERT( psa_encrypt_generate_iv( iv, + TEST_ASSERT( psa_encrypt_generate_iv( &operation, iv, iv_size, &iv_length) == PSA_SUCCESS ); TEST_ASSERT( psa_encrypt_set_iv( &operation, iv, @@ -622,7 +622,7 @@ void cipher_test_verify_output( int alg_arg, int key_type_arg, TEST_ASSERT( psa_decrypt_setup( &operation1, key_slot, alg ) == PSA_SUCCESS ); - TEST_ASSERT( psa_encrypt_generate_iv( iv, + TEST_ASSERT( psa_encrypt_generate_iv( &operation1, iv, iv_size, &iv_length) == PSA_SUCCESS ); TEST_ASSERT( psa_encrypt_set_iv( &operation1, iv, From 16864af80bae0f3c2717d8291c74ea265e15db25 Mon Sep 17 00:00:00 2001 From: mohammad1603 Date: Mon, 19 Mar 2018 16:22:57 +0200 Subject: [PATCH 09/54] fix static function name --- library/psa_crypto.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/library/psa_crypto.c b/library/psa_crypto.c index 9a812b8660..57847fa1fc 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -1320,7 +1320,7 @@ psa_status_t psa_asymmetric_sign(psa_key_slot_t key, /* Symmetric cryptography */ /****************************************************************/ -static psa_status_t psa_setup(psa_cipher_operation_t *operation, +static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation, psa_key_slot_t key, psa_algorithm_t alg, mbedtls_operation_t cipher_operation) { @@ -1399,14 +1399,14 @@ psa_status_t psa_encrypt_setup(psa_cipher_operation_t *operation, psa_key_slot_t key, psa_algorithm_t alg) { - return psa_setup(operation, key, alg, MBEDTLS_ENCRYPT); + return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT); } psa_status_t psa_decrypt_setup(psa_cipher_operation_t *operation, psa_key_slot_t key, psa_algorithm_t alg) { - return psa_setup(operation, key, alg, MBEDTLS_DECRYPT); + return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT); } psa_status_t psa_encrypt_generate_iv(psa_cipher_operation_t *operation, From 3205a6592ba7b410bceadd49c8f263fcc45ced13 Mon Sep 17 00:00:00 2001 From: Moran Peker Date: Tue, 20 Mar 2018 17:09:59 +0200 Subject: [PATCH 10/54] 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 57847fa1fc..96eea5b9fa 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 533bb71dae..d8cab1fd45 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 66ab296edb..5b4702bef6 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 ); From e1210dcac39c6258b9d6f4435b81fe5be6c1b3c6 Mon Sep 17 00:00:00 2001 From: Moran Peker Date: Sun, 25 Mar 2018 15:44:12 +0300 Subject: [PATCH 11/54] remove unused parameter in psa_cipher_finish. --- include/psa/crypto.h | 1 - library/psa_crypto.c | 1 - tests/suites/test_suite_psa_crypto.function | 3 --- 3 files changed, 5 deletions(-) diff --git a/include/psa/crypto.h b/include/psa/crypto.h index 089484f197..b1c1abb061 100644 --- a/include/psa/crypto.h +++ b/include/psa/crypto.h @@ -1064,7 +1064,6 @@ psa_status_t psa_cipher_update(psa_cipher_operation_t *operation, psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation, uint8_t *output, - size_t output_size, size_t *output_length); psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation); diff --git a/library/psa_crypto.c b/library/psa_crypto.c index 96eea5b9fa..0d309c00bc 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -1471,7 +1471,6 @@ psa_status_t psa_cipher_update(psa_cipher_operation_t *operation, psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation, uint8_t *output, - size_t output_size, size_t *output_length) { int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; diff --git a/tests/suites/test_suite_psa_crypto.function b/tests/suites/test_suite_psa_crypto.function index 5b4702bef6..e3294a79be 100644 --- a/tests/suites/test_suite_psa_crypto.function +++ b/tests/suites/test_suite_psa_crypto.function @@ -571,7 +571,6 @@ void cipher_test_positive( int alg_arg, int key_type_arg, output, output_size, &output_length) == PSA_SUCCESS ); TEST_ASSERT( psa_cipher_finish( &operation, output + output_length, - output_size, &output_length) == PSA_SUCCESS ); TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); @@ -631,7 +630,6 @@ void cipher_test_verify_output( int alg_arg, int key_type_arg, output1, output1_size, &output1_length) == PSA_SUCCESS ); TEST_ASSERT( psa_cipher_finish( &operation1, output1 + output1_length, - output1_size - output1_length, &tmp_output_length) == PSA_SUCCESS ); output1_length += tmp_output_length; @@ -647,7 +645,6 @@ void cipher_test_verify_output( int alg_arg, int key_type_arg, output2, output2_size, &output2_length) == PSA_SUCCESS ); tmp_output_length = 0; TEST_ASSERT( psa_cipher_finish( &operation2, output2 + output2_length, - output2_size - output2_length, &tmp_output_length) == PSA_SUCCESS ); output2_length += tmp_output_length; From 41deec44947a9303fd6fee5e9e9ad3d9f470d167 Mon Sep 17 00:00:00 2001 From: Moran Peker Date: Wed, 4 Apr 2018 15:43:05 +0300 Subject: [PATCH 12/54] partly pr fix --- library/psa_crypto.c | 116 ++++++++++++++++++++----------------------- 1 file changed, 55 insertions(+), 61 deletions(-) diff --git a/library/psa_crypto.c b/library/psa_crypto.c index 0d309c00bc..0761978f79 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -286,41 +286,6 @@ static psa_status_t mbedtls_to_psa_error( int ret ) } } -static void psa_operation_init(void *operation, - psa_algorithm_t alg) -{ - if( PSA_ALG_IS_MAC(alg) ) - { - if ( ((psa_mac_operation_t*)operation)->alg != 0 ) //restart - { - ((psa_mac_operation_t*)operation)->alg = 0; - ((psa_mac_operation_t*)operation)->iv_required = 0; - } - else - { - ((psa_mac_operation_t*)operation)->alg = alg; - ((psa_mac_operation_t*)operation)->iv_required = 1; - } - - ((psa_mac_operation_t*)operation)->key_set = 0; - ((psa_mac_operation_t*)operation)->iv_set = 0; - ((psa_mac_operation_t*)operation)->has_input = 0; - ((psa_mac_operation_t*)operation)->mac_size = 0; - } - else if( PSA_ALG_IS_CIPHER(alg) ) - { - if ( ((psa_cipher_operation_t*)operation)->alg != 0 ) //restart - ((psa_cipher_operation_t*)operation)->alg = 0; - else - ((psa_cipher_operation_t*)operation)->alg = alg; - - ((psa_cipher_operation_t*)operation)->key_set = 0; - ((psa_cipher_operation_t*)operation)->iv_set = 0; - ((psa_cipher_operation_t*)operation)->iv_size = 0; - ((psa_cipher_operation_t*)operation)->block_size = 0; - } -} - /****************************************************************/ /* Key management */ /****************************************************************/ @@ -992,7 +957,13 @@ psa_status_t psa_mac_abort( psa_mac_operation_t *operation ) #endif /* MBEDTLS_MD_C */ return( PSA_ERROR_NOT_SUPPORTED ); } - psa_operation_init(operation, 0); + + operation->alg = 0; + operation->key_set = 0; + operation->iv_set = 0; + operation->iv_required = 0; + operation->has_input = 0; + return( PSA_SUCCESS ); } @@ -1007,7 +978,11 @@ psa_status_t psa_mac_start( psa_mac_operation_t *operation, size_t key_bits; const mbedtls_cipher_info_t *cipher_info = NULL; - psa_operation_init(operation, alg); + operation->alg = 0; + operation->key_set = 0; + operation->iv_set = 0; + operation->iv_required = 1; + operation->has_input = 0; status = psa_get_key_information( key, &key_type, &key_bits ); if( status != PSA_SUCCESS ) @@ -1333,7 +1308,11 @@ static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation, psa_algorithm_t padding_mode = PSA_ALG_BLOCK_CIPHER_PAD_NONE; mbedtls_cipher_padding_t mode = MBEDTLS_PADDING_NONE; - psa_operation_init(operation, alg); + operation->alg = alg; + operation->key_set = 0; + operation->iv_set = 0; + operation->iv_size = 0; + operation->block_size = 0; status = psa_get_key_information( key, &key_type, &key_bits ); if( status != PSA_SUCCESS ) @@ -1348,7 +1327,7 @@ static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation, mbedtls_cipher_init( &operation->ctx.cipher ); ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info ); - if (ret != 0) + if( ret != 0 ) { psa_cipher_abort( operation ); return( mbedtls_to_psa_error( ret ) ); @@ -1356,14 +1335,14 @@ static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation, ret = mbedtls_cipher_setkey( &operation->ctx.cipher, slot->data.raw.data, key_bits, cipher_operation ); - if (ret != 0) + if( ret != 0 ) { psa_cipher_abort( operation ); return( mbedtls_to_psa_error( ret ) ); } #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) - if (( alg & PSA_ALG_CBC_BASE) == PSA_ALG_CBC_BASE) + if( ( alg & PSA_ALG_CBC_BASE) == PSA_ALG_CBC_BASE ) { padding_mode = alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK; @@ -1376,10 +1355,10 @@ static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation, mode = MBEDTLS_PADDING_NONE; break; default: - return ( PSA_ERROR_INVALID_PADDING ); + return ( PSA_ERROR_INVALID_ARGUMENT ); } ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, mode ); - if (ret != 0) + if( ret != 0 ) return( mbedtls_to_psa_error( ret ) ); } #endif //MBEDTLS_CIPHER_MODE_WITH_PADDING @@ -1387,9 +1366,9 @@ 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); - if ( PSA_ALG_IS_BLOCK_CIPHER(alg) ) + if( PSA_ALG_IS_BLOCK_CIPHER( alg ) || ( alg == PSA_ALG_CTR ) ) { - operation->iv_size = operation->block_size; + operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE(key_type); } return ( PSA_SUCCESS ); @@ -1414,28 +1393,39 @@ psa_status_t psa_encrypt_generate_iv(psa_cipher_operation_t *operation, size_t iv_size, size_t *iv_length) { - int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; - if (iv_size < operation->iv_size) - return ( PSA_ERROR_BUFFER_TOO_SMALL ); - - ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, iv, operation->iv_size); - if (ret != 0) + int ret = PSA_SUCCESS; + if( operation->iv_set ) + return( PSA_ERROR_BAD_STATE ); + if( iv_size < operation->iv_size ) { - return( mbedtls_to_psa_error( ret ) ); + ret = PSA_ERROR_BUFFER_TOO_SMALL; + goto exit; + } + ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, iv, operation->iv_size); + if( ret != 0 ) + { + ret = mbedtls_to_psa_error( ret ); + goto exit; } *iv_length = operation->iv_size; - return psa_encrypt_set_iv( operation, iv, *iv_length); + ret = psa_encrypt_set_iv( operation, iv, *iv_length); + + exit: + if( ret != PSA_SUCCESS) + psa_cipher_abort( operation ); + return( ret ); } psa_status_t psa_encrypt_set_iv(psa_cipher_operation_t *operation, const unsigned char *iv, size_t iv_length) { - int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; - + int ret = PSA_SUCCESS; + if( operation->iv_set ) + return( PSA_ERROR_BAD_STATE ); ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length ); - if (ret != 0) + if( ret != 0 ) { psa_cipher_abort( operation ); return( mbedtls_to_psa_error( ret ) ); @@ -1455,12 +1445,12 @@ psa_status_t psa_cipher_update(psa_cipher_operation_t *operation, { int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; - if ( output_size < input_length ) + if( output_size < input_length ) return ( PSA_ERROR_BUFFER_TOO_SMALL ); ret = mbedtls_cipher_update( &operation->ctx.cipher, input, input_length, output, output_length ); - if (ret != 0) + if( ret != 0 ) { psa_cipher_abort( operation ); return( mbedtls_to_psa_error( ret ) ); @@ -1482,7 +1472,7 @@ psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation, ret = mbedtls_cipher_finish( &operation->ctx.cipher, output, output_length ); - if (ret != 0) + if( ret != 0 ) { psa_cipher_abort( operation ); return( mbedtls_to_psa_error( ret ) ); @@ -1495,8 +1485,12 @@ psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation) { mbedtls_cipher_free( &operation->ctx.cipher ); - psa_operation_init(operation, 0); - + operation->alg = 0; + operation->key_set = 0; + operation->iv_set = 0; + operation->iv_size = 0; + operation->block_size = 0; + return ( PSA_SUCCESS ); } From 89e0f468bfec34330f9ac29fe1066b902e9a6656 Mon Sep 17 00:00:00 2001 From: mohammad1603 Date: Thu, 12 Apr 2018 08:48:45 +0300 Subject: [PATCH 13/54] style --- library/psa_crypto.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/library/psa_crypto.c b/library/psa_crypto.c index 0761978f79..96d2c0f69f 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -1368,7 +1368,7 @@ static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation, operation->block_size = PSA_BLOCK_CIPHER_BLOCK_SIZE(key_type); if( PSA_ALG_IS_BLOCK_CIPHER( alg ) || ( alg == PSA_ALG_CTR ) ) { - operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE(key_type); + operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ); } return ( PSA_SUCCESS ); @@ -1401,7 +1401,7 @@ psa_status_t psa_encrypt_generate_iv(psa_cipher_operation_t *operation, ret = PSA_ERROR_BUFFER_TOO_SMALL; goto exit; } - ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, iv, operation->iv_size); + ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, iv, operation->iv_size ); if( ret != 0 ) { ret = mbedtls_to_psa_error( ret ); @@ -1409,10 +1409,10 @@ psa_status_t psa_encrypt_generate_iv(psa_cipher_operation_t *operation, } *iv_length = operation->iv_size; - ret = psa_encrypt_set_iv( operation, iv, *iv_length); + ret = psa_encrypt_set_iv( operation, iv, *iv_length ); exit: - if( ret != PSA_SUCCESS) + if( ret != PSA_SUCCESS ) psa_cipher_abort( operation ); return( ret ); } From b152d4d8b6116746584a2011333295d855635bcc Mon Sep 17 00:00:00 2001 From: mohammad1603 Date: Wed, 11 Apr 2018 23:51:20 -0700 Subject: [PATCH 14/54] 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 96d2c0f69f..9ad44e7f32 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 d8cab1fd45..3bf93b8428 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 e3294a79be..eb217f9f94 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, From 4c80d8331a1cabbad3f61f7d1b16eadbadd3ad2c Mon Sep 17 00:00:00 2001 From: Moran Peker Date: Sun, 22 Apr 2018 20:15:31 +0300 Subject: [PATCH 15/54] adjust indentation per Mbed TLS standards --- library/psa_crypto.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/library/psa_crypto.c b/library/psa_crypto.c index 9ad44e7f32..bb74c26604 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -959,9 +959,9 @@ psa_status_t psa_mac_abort( psa_mac_operation_t *operation ) } operation->alg = 0; - operation->key_set = 0; - operation->iv_set = 0; - operation->iv_required = 0; + operation->key_set = 0; + operation->iv_set = 0; + operation->iv_required = 0; operation->has_input = 0; return( PSA_SUCCESS ); @@ -979,9 +979,9 @@ psa_status_t psa_mac_start( psa_mac_operation_t *operation, const mbedtls_cipher_info_t *cipher_info = NULL; operation->alg = 0; - operation->key_set = 0; - operation->iv_set = 0; - operation->iv_required = 1; + operation->key_set = 0; + operation->iv_set = 0; + operation->iv_required = 1; operation->has_input = 0; status = psa_get_key_information( key, &key_type, &key_bits ); @@ -1358,7 +1358,7 @@ static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation, return ( PSA_ERROR_INVALID_ARGUMENT ); } ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, mode ); - if( ret != 0 ) + if (ret != 0) return( mbedtls_to_psa_error( ret ) ); } #endif //MBEDTLS_CIPHER_MODE_WITH_PADDING @@ -1413,7 +1413,7 @@ psa_status_t psa_encrypt_generate_iv(psa_cipher_operation_t *operation, exit: if( ret != PSA_SUCCESS ) - psa_cipher_abort( operation ); + psa_cipher_abort( operation ); return( ret ); } @@ -1478,7 +1478,7 @@ psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation, return( mbedtls_to_psa_error( ret ) ); } - return ( PSA_SUCCESS ); + return( PSA_SUCCESS ); } psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation) From 0071b873a3609885597e20be5ec6f428ceb22b95 Mon Sep 17 00:00:00 2001 From: Moran Peker Date: Sun, 22 Apr 2018 20:16:58 +0300 Subject: [PATCH 16/54] add missing parameter output_size on psa_cipher_finish --- include/psa/crypto.h | 1 + library/psa_crypto.c | 1 + tests/suites/test_suite_psa_crypto.function | 8 ++++---- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/include/psa/crypto.h b/include/psa/crypto.h index b1c1abb061..089484f197 100644 --- a/include/psa/crypto.h +++ b/include/psa/crypto.h @@ -1064,6 +1064,7 @@ psa_status_t psa_cipher_update(psa_cipher_operation_t *operation, psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation, uint8_t *output, + size_t output_size, size_t *output_length); psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation); diff --git a/library/psa_crypto.c b/library/psa_crypto.c index bb74c26604..fbc5949ddc 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -1461,6 +1461,7 @@ psa_status_t psa_cipher_update(psa_cipher_operation_t *operation, psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation, uint8_t *output, + size_t output_size, size_t *output_length) { int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; diff --git a/tests/suites/test_suite_psa_crypto.function b/tests/suites/test_suite_psa_crypto.function index eb217f9f94..bc46ad215f 100644 --- a/tests/suites/test_suite_psa_crypto.function +++ b/tests/suites/test_suite_psa_crypto.function @@ -574,7 +574,7 @@ void cipher_test_positive( int alg_arg, int key_type_arg, output, output_size, &output_length) == PSA_SUCCESS ); TEST_ASSERT( psa_cipher_finish( &operation, output + output_length, - &output_length) == PSA_SUCCESS ); + output_size, &output_length) == PSA_SUCCESS ); TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); @@ -637,7 +637,7 @@ void cipher_test_decrypt( int alg_arg, int key_type_arg, output, output_size, &output_length) == PSA_SUCCESS ); TEST_ASSERT( psa_cipher_finish( &operation, output + output_length, - &output_length) == PSA_SUCCESS ); + output_size, &output_length) == PSA_SUCCESS ); TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); @@ -700,7 +700,7 @@ void cipher_test_verify_output( int alg_arg, int key_type_arg, output1, output1_size, &output1_length) == PSA_SUCCESS ); TEST_ASSERT( psa_cipher_finish( &operation1, output1 + output1_length, - &tmp_output_length) == PSA_SUCCESS ); + output1_size, &tmp_output_length) == PSA_SUCCESS ); output1_length += tmp_output_length; @@ -715,7 +715,7 @@ void cipher_test_verify_output( int alg_arg, int key_type_arg, output2, output2_size, &output2_length) == PSA_SUCCESS ); tmp_output_length = 0; TEST_ASSERT( psa_cipher_finish( &operation2, output2 + output2_length, - &tmp_output_length) == PSA_SUCCESS ); + output2_size, &tmp_output_length) == PSA_SUCCESS ); output2_length += tmp_output_length; From bed71a2b17b5c7c046c5f4c0c3ae2176aaae200a Mon Sep 17 00:00:00 2001 From: Moran Peker Date: Sun, 22 Apr 2018 20:19:20 +0300 Subject: [PATCH 17/54] fix missing check on output_size in psa_cipher_finish func --- include/psa/crypto.h | 4 ++++ library/psa_crypto.c | 22 ++++++++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/include/psa/crypto.h b/include/psa/crypto.h index 089484f197..31079525bc 100644 --- a/include/psa/crypto.h +++ b/include/psa/crypto.h @@ -321,6 +321,10 @@ typedef uint32_t psa_algorithm_t; #define PSA_ALG_CTR ((psa_algorithm_t)0x04800001) #define PSA_ALG_ARC4 ((psa_algorithm_t)0x04800002) +#define PSA_ALG_IS_STREAM_CIPHER(alg) \ + (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_SUBCATEGORY_MASK)) == \ + PSA_ALG_STREAM_CIPHER) + #define PSA_ALG_CCM ((psa_algorithm_t)0x06000001) #define PSA_ALG_GCM ((psa_algorithm_t)0x06000002) diff --git a/library/psa_crypto.c b/library/psa_crypto.c index fbc5949ddc..2672627213 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -1466,13 +1466,31 @@ psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation, { int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; + uint8_t temp_output_buffer[ MBEDTLS_MAX_BLOCK_LENGTH ]; + if( ! operation->key_set ) return( PSA_ERROR_BAD_STATE ); if( ! operation->iv_set ) return( PSA_ERROR_BAD_STATE ); + if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT ) + { + if (operation->ctx.cipher.unprocessed_len > operation->block_size) + return( PSA_ERROR_INVALID_ARGUMENT ); + if ( ( ( ( operation->alg ) & PSA_ALG_BLOCK_CIPHER_PAD_NONE ) == PSA_ALG_BLOCK_CIPHER_PAD_NONE ) + && ( operation->ctx.cipher.unprocessed_len != 0 ) ) + return(PSA_ERROR_INVALID_ARGUMENT); + if ( ( ( ( operation->alg ) & PSA_ALG_BLOCK_CIPHER_PAD_PKCS7 ) == PSA_ALG_BLOCK_CIPHER_PAD_PKCS7 ) + && ( output_size != operation->block_size ) ) + return(PSA_ERROR_INVALID_ARGUMENT); + } + if ( operation->ctx.cipher.operation == MBEDTLS_DECRYPT ) + if (operation->ctx.cipher.unprocessed_len != 0) + return( PSA_ERROR_INVALID_ARGUMENT ); - ret = mbedtls_cipher_finish( &operation->ctx.cipher, output, - output_length ); + ret = mbedtls_cipher_finish(&operation->ctx.cipher, temp_output_buffer, + output_length); + if ( output_size > *output_length ) + memcpy( temp_output_buffer, output, *output_length ); if( ret != 0 ) { psa_cipher_abort( operation ); From 406008ab4c4a90e133b258ef0234997517876ed6 Mon Sep 17 00:00:00 2001 From: Moran Peker Date: Sun, 22 Apr 2018 20:20:29 +0300 Subject: [PATCH 18/54] add missing check on output_size in psa_cipher_update func --- library/psa_crypto.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/library/psa_crypto.c b/library/psa_crypto.c index 2672627213..0e2d6dafa3 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -1445,7 +1445,8 @@ psa_status_t psa_cipher_update(psa_cipher_operation_t *operation, { int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; - if( output_size < input_length ) + if( ( ( PSA_ALG_IS_STREAM_CIPHER( operation->alg ) ) && ( output_size < input_length ) ) + || ( ( PSA_ALG_IS_BLOCK_CIPHER(operation->alg)) && ( output_size < ((operation->ctx.cipher.unprocessed_len + input_length)/16)*16 ) ) ) return ( PSA_ERROR_BUFFER_TOO_SMALL ); ret = mbedtls_cipher_update( &operation->ctx.cipher, input, From 71f19ae6f8370f7f261f2c6979d0328a63deec27 Mon Sep 17 00:00:00 2001 From: Moran Peker Date: Sun, 22 Apr 2018 20:23:16 +0300 Subject: [PATCH 19/54] add missing call to psa_cipher_abort in cipher_setup func + iv_length check in cipher_set_iv func --- library/psa_crypto.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/library/psa_crypto.c b/library/psa_crypto.c index 0e2d6dafa3..b29b763f62 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -1359,7 +1359,10 @@ static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation, } ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, mode ); if (ret != 0) + { + psa_cipher_abort( operation ); return( mbedtls_to_psa_error( ret ) ); + } } #endif //MBEDTLS_CIPHER_MODE_WITH_PADDING @@ -1424,6 +1427,13 @@ psa_status_t psa_encrypt_set_iv(psa_cipher_operation_t *operation, int ret = PSA_SUCCESS; if( operation->iv_set ) return( PSA_ERROR_BAD_STATE ); + if (iv_length != operation->iv_size) + { + if (((operation->alg) & PSA_ALG_ARC4) == PSA_ALG_ARC4) + return(PSA_ERROR_BAD_STATE); + else + return (PSA_ERROR_INVALID_ARGUMENT); + } ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length ); if( ret != 0 ) { @@ -1466,7 +1476,6 @@ psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation, size_t *output_length) { int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; - uint8_t temp_output_buffer[ MBEDTLS_MAX_BLOCK_LENGTH ]; if( ! operation->key_set ) From ad9d82cc0e675ae2daec00d31d19b123998f2a23 Mon Sep 17 00:00:00 2001 From: Moran Peker Date: Mon, 30 Apr 2018 12:31:04 +0300 Subject: [PATCH 20/54] add iv_required field to psa_cipher_operation_s and fix relevant functions --- include/psa/crypto_struct.h | 1 + library/psa_crypto.c | 21 ++++++++++++--------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/include/psa/crypto_struct.h b/include/psa/crypto_struct.h index 2975bdcb0a..639c15e76b 100644 --- a/include/psa/crypto_struct.h +++ b/include/psa/crypto_struct.h @@ -101,6 +101,7 @@ struct psa_cipher_operation_s { psa_algorithm_t alg; int key_set : 1; + int iv_required : 1; int iv_set : 1; uint8_t iv_size; uint8_t block_size; diff --git a/library/psa_crypto.c b/library/psa_crypto.c index b29b763f62..c5a8456646 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -1309,9 +1309,10 @@ static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation, mbedtls_cipher_padding_t mode = MBEDTLS_PADDING_NONE; operation->alg = alg; - operation->key_set = 0; - operation->iv_set = 0; - operation->iv_size = 0; + operation->key_set = 0; + operation->iv_set = 0; + operation->iv_required = 1; + operation->iv_size = 0; operation->block_size = 0; status = psa_get_key_information( key, &key_type, &key_bits ); @@ -1397,7 +1398,7 @@ psa_status_t psa_encrypt_generate_iv(psa_cipher_operation_t *operation, size_t *iv_length) { int ret = PSA_SUCCESS; - if( operation->iv_set ) + if( operation->iv_set || !( operation->iv_required ) ) return( PSA_ERROR_BAD_STATE ); if( iv_size < operation->iv_size ) { @@ -1425,7 +1426,7 @@ psa_status_t psa_encrypt_set_iv(psa_cipher_operation_t *operation, size_t iv_length) { int ret = PSA_SUCCESS; - if( operation->iv_set ) + if( operation->iv_set || !( operation->iv_required ) ) return( PSA_ERROR_BAD_STATE ); if (iv_length != operation->iv_size) { @@ -1442,6 +1443,7 @@ psa_status_t psa_encrypt_set_iv(psa_cipher_operation_t *operation, } operation->iv_set = 1; + operation->iv_required = 0; return ( PSA_SUCCESS ); } @@ -1480,7 +1482,7 @@ psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation, if( ! operation->key_set ) return( PSA_ERROR_BAD_STATE ); - if( ! operation->iv_set ) + if ( operation->iv_required && ! operation->iv_set ) return( PSA_ERROR_BAD_STATE ); if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT ) { @@ -1515,10 +1517,11 @@ psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation) mbedtls_cipher_free( &operation->ctx.cipher ); operation->alg = 0; - operation->key_set = 0; - operation->iv_set = 0; - operation->iv_size = 0; + operation->key_set = 0; + operation->iv_set = 0; + operation->iv_size = 0; operation->block_size = 0; + operation->iv_required = 0; return ( PSA_SUCCESS ); } From dc38ebc068f0fe023a881785a63a6d465cc21003 Mon Sep 17 00:00:00 2001 From: Moran Peker Date: Mon, 30 Apr 2018 15:45:34 +0300 Subject: [PATCH 21/54] delete decrypt checks + fix memcpy& return value --- library/psa_crypto.c | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/library/psa_crypto.c b/library/psa_crypto.c index c5a8456646..ee45a150f0 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -1484,30 +1484,33 @@ psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation, return( PSA_ERROR_BAD_STATE ); if ( operation->iv_required && ! operation->iv_set ) return( PSA_ERROR_BAD_STATE ); - if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT ) - { - if (operation->ctx.cipher.unprocessed_len > operation->block_size) - return( PSA_ERROR_INVALID_ARGUMENT ); - if ( ( ( ( operation->alg ) & PSA_ALG_BLOCK_CIPHER_PAD_NONE ) == PSA_ALG_BLOCK_CIPHER_PAD_NONE ) - && ( operation->ctx.cipher.unprocessed_len != 0 ) ) - return(PSA_ERROR_INVALID_ARGUMENT); - if ( ( ( ( operation->alg ) & PSA_ALG_BLOCK_CIPHER_PAD_PKCS7 ) == PSA_ALG_BLOCK_CIPHER_PAD_PKCS7 ) - && ( output_size != operation->block_size ) ) - return(PSA_ERROR_INVALID_ARGUMENT); - } - if ( operation->ctx.cipher.operation == MBEDTLS_DECRYPT ) - if (operation->ctx.cipher.unprocessed_len != 0) - return( PSA_ERROR_INVALID_ARGUMENT ); - ret = mbedtls_cipher_finish(&operation->ctx.cipher, temp_output_buffer, - output_length); - if ( output_size > *output_length ) - memcpy( temp_output_buffer, output, *output_length ); + if ( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT ) + { + if( operation->ctx.cipher.unprocessed_len > operation->block_size ) + return( PSA_ERROR_INVALID_ARGUMENT ); + if( ( ( ( operation->alg ) & PSA_ALG_BLOCK_CIPHER_PAD_NONE ) == PSA_ALG_BLOCK_CIPHER_PAD_NONE ) + && ( operation->ctx.cipher.unprocessed_len != 0 ) ) + return( PSA_ERROR_INVALID_ARGUMENT ); + if( ( ( ( operation->alg) & PSA_ALG_BLOCK_CIPHER_PAD_PKCS7 ) == PSA_ALG_BLOCK_CIPHER_PAD_PKCS7 ) + && ( output_size != operation->block_size ) ) + return( PSA_ERROR_INVALID_ARGUMENT ); + } + + ret = mbedtls_cipher_finish( &operation->ctx.cipher, temp_output_buffer, + output_length ); if( ret != 0 ) { psa_cipher_abort( operation ); return( mbedtls_to_psa_error( ret ) ); } + if(output_size >= *output_length) + memcpy( output, temp_output_buffer, *output_length ); + else + { + psa_cipher_abort( operation ); + return( PSA_ERROR_BUFFER_TOO_SMALL ); + } return( PSA_SUCCESS ); } From 2cab25aacf6eff41cd171dcaf57719bd24983fef Mon Sep 17 00:00:00 2001 From: Moran Peker Date: Wed, 2 May 2018 16:43:13 +0300 Subject: [PATCH 22/54] fix conditions in psa_cipher_finish function --- library/psa_crypto.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/library/psa_crypto.c b/library/psa_crypto.c index ee45a150f0..3cf63a9b3c 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -1489,11 +1489,11 @@ psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation, { if( operation->ctx.cipher.unprocessed_len > operation->block_size ) return( PSA_ERROR_INVALID_ARGUMENT ); - if( ( ( ( operation->alg ) & PSA_ALG_BLOCK_CIPHER_PAD_NONE ) == PSA_ALG_BLOCK_CIPHER_PAD_NONE ) + if( ( ( operation->alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK ) == PSA_ALG_BLOCK_CIPHER_PAD_NONE ) && ( operation->ctx.cipher.unprocessed_len != 0 ) ) return( PSA_ERROR_INVALID_ARGUMENT ); - if( ( ( ( operation->alg) & PSA_ALG_BLOCK_CIPHER_PAD_PKCS7 ) == PSA_ALG_BLOCK_CIPHER_PAD_PKCS7 ) - && ( output_size != operation->block_size ) ) + if( ( ( operation->alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK ) == PSA_ALG_BLOCK_CIPHER_PAD_PKCS7 ) + && ( *output_length != operation->block_size ) ) return( PSA_ERROR_INVALID_ARGUMENT ); } From a28258c594ec7752004ede906527f8aec41a8dc1 Mon Sep 17 00:00:00 2001 From: Moran Peker Date: Tue, 29 May 2018 16:25:04 +0300 Subject: [PATCH 23/54] adjust indentation per Mbed TLS standards --- library/psa_crypto.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/library/psa_crypto.c b/library/psa_crypto.c index 3cf63a9b3c..4f02bb2bc2 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -989,10 +989,10 @@ psa_status_t psa_mac_start( psa_mac_operation_t *operation, return( status ); slot = &global_data.key_slots[key]; - if ( ( slot->policy.usage & PSA_KEY_USAGE_SIGN ) != 0 ) + \ ( slot->policy.usage & PSA_KEY_USAGE_SIGN ) != 0 ) operation->key_usage_sign = 1; - if ( ( slot->policy.usage & PSA_KEY_USAGE_VERIFY ) != 0 ) + if( ( slot->policy.usage & PSA_KEY_USAGE_VERIFY ) != 0 ) operation->key_usage_verify = 1; if( ! PSA_ALG_IS_HMAC( alg ) ) @@ -1347,7 +1347,7 @@ static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation, { padding_mode = alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK; - switch (padding_mode) + switch ( padding_mode ) { case PSA_ALG_BLOCK_CIPHER_PAD_PKCS7: mode = MBEDTLS_PADDING_PKCS7; @@ -1359,7 +1359,7 @@ static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation, return ( PSA_ERROR_INVALID_ARGUMENT ); } ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, mode ); - if (ret != 0) + if( ret != 0 ) { psa_cipher_abort( operation ); return( mbedtls_to_psa_error( ret ) ); @@ -1382,14 +1382,14 @@ psa_status_t psa_encrypt_setup(psa_cipher_operation_t *operation, psa_key_slot_t key, psa_algorithm_t alg) { - return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT); + return psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ); } psa_status_t psa_decrypt_setup(psa_cipher_operation_t *operation, psa_key_slot_t key, psa_algorithm_t alg) { - return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT); + return psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ); } psa_status_t psa_encrypt_generate_iv(psa_cipher_operation_t *operation, @@ -1428,12 +1428,12 @@ psa_status_t psa_encrypt_set_iv(psa_cipher_operation_t *operation, int ret = PSA_SUCCESS; if( operation->iv_set || !( operation->iv_required ) ) return( PSA_ERROR_BAD_STATE ); - if (iv_length != operation->iv_size) + if( iv_length != operation->iv_size ) { - if (((operation->alg) & PSA_ALG_ARC4) == PSA_ALG_ARC4) - return(PSA_ERROR_BAD_STATE); + if( ( ( operation->alg ) & PSA_ALG_ARC4 ) == PSA_ALG_ARC4 ) + return( PSA_ERROR_BAD_STATE ); else - return (PSA_ERROR_INVALID_ARGUMENT); + return( PSA_ERROR_INVALID_ARGUMENT ); } ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length ); if( ret != 0 ) @@ -1482,10 +1482,10 @@ psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation, if( ! operation->key_set ) return( PSA_ERROR_BAD_STATE ); - if ( operation->iv_required && ! operation->iv_set ) + if( operation->iv_required && ! operation->iv_set ) return( PSA_ERROR_BAD_STATE ); - if ( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT ) + if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT ) { if( operation->ctx.cipher.unprocessed_len > operation->block_size ) return( PSA_ERROR_INVALID_ARGUMENT ); @@ -1632,7 +1632,7 @@ psa_status_t psa_set_key_lifetime(psa_key_slot_t key, if( slot->type != PSA_KEY_TYPE_NONE ) return( PSA_ERROR_OCCUPIED_SLOT ); - if ( lifetime != PSA_KEY_LIFETIME_VOLATILE ) + if( lifetime != PSA_KEY_LIFETIME_VOLATILE ) return( PSA_ERROR_NOT_SUPPORTED ); slot->lifetime = lifetime; From f55e804e0765249061977d2e3d165175c73560ae Mon Sep 17 00:00:00 2001 From: Moran Peker Date: Tue, 29 May 2018 16:28:28 +0300 Subject: [PATCH 24/54] adjust indentation per Mbed TLS standards --- tests/suites/test_suite_psa_crypto.function | 36 ++++++++++----------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/tests/suites/test_suite_psa_crypto.function b/tests/suites/test_suite_psa_crypto.function index bc46ad215f..081b8d571b 100644 --- a/tests/suites/test_suite_psa_crypto.function +++ b/tests/suites/test_suite_psa_crypto.function @@ -568,12 +568,12 @@ void cipher_test_positive( int alg_arg, int key_type_arg, TEST_ASSERT( psa_encrypt_set_iv( &operation, iv, sizeof( iv ) ) == PSA_SUCCESS ); - output = mbedtls_calloc(0, output_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, + TEST_ASSERT( psa_cipher_finish( &operation, output + output_length, output_size, &output_length) == PSA_SUCCESS ); TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); @@ -631,12 +631,12 @@ void cipher_test_decrypt( int alg_arg, int key_type_arg, TEST_ASSERT( psa_encrypt_set_iv( &operation, iv, sizeof( iv ) ) == PSA_SUCCESS ); - output = mbedtls_calloc(0, output_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, + TEST_ASSERT( psa_cipher_finish( &operation, output + output_length, output_size, &output_length) == PSA_SUCCESS ); TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); @@ -689,35 +689,35 @@ 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_encrypt_setup( &operation1, key_slot, alg ) == PSA_SUCCESS ); - TEST_ASSERT( psa_decrypt_setup( &operation2, 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 ); - output1_size = input_size; - output1 = mbedtls_calloc(0, output1_size); + 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, - output1_size, &tmp_output_length) == PSA_SUCCESS ); + TEST_ASSERT( psa_cipher_finish( &operation1, output1 + output1_length, + output1_size, &tmp_output_length) == PSA_SUCCESS ); output1_length += tmp_output_length; TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); - output2_size = output1_length; - output2 = mbedtls_calloc(0, output2_size); + output2_size = output1_length; + output2 = mbedtls_calloc(0, output2_size); - TEST_ASSERT( psa_encrypt_set_iv( &operation2, iv, - iv_length) == PSA_SUCCESS ); + 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, - output2_size, &tmp_output_length) == PSA_SUCCESS ); - - output2_length += tmp_output_length; + TEST_ASSERT( psa_cipher_finish( &operation2, output2 + output2_length, + output2_size, &tmp_output_length) == PSA_SUCCESS ); + + output2_length += tmp_output_length; TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); From 96cc00a8577e582e2a59bfc7412e73e0695ddf05 Mon Sep 17 00:00:00 2001 From: Moran Peker Date: Thu, 31 May 2018 14:03:56 +0300 Subject: [PATCH 25/54] add missing tests function --- tests/suites/test_suite_psa_crypto.function | 61 +++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/tests/suites/test_suite_psa_crypto.function b/tests/suites/test_suite_psa_crypto.function index 081b8d571b..6a7a2d12a9 100644 --- a/tests/suites/test_suite_psa_crypto.function +++ b/tests/suites/test_suite_psa_crypto.function @@ -589,6 +589,67 @@ exit: } /* END_CASE */ +/* BEGIN_CASE */ +void cipher_test_encrypt( 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_encrypt_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_size, &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_decrypt( int alg_arg, int key_type_arg, From 70531163a9a94d39f85e4b7118f60a391ca0c37f Mon Sep 17 00:00:00 2001 From: Moran Peker Date: Thu, 31 May 2018 14:04:45 +0300 Subject: [PATCH 26/54] fix compilation error - missing if --- library/psa_crypto.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/psa_crypto.c b/library/psa_crypto.c index 4f02bb2bc2..56f3e1d4e4 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -989,7 +989,7 @@ psa_status_t psa_mac_start( psa_mac_operation_t *operation, return( status ); slot = &global_data.key_slots[key]; - \ ( slot->policy.usage & PSA_KEY_USAGE_SIGN ) != 0 ) + if( ( slot->policy.usage & PSA_KEY_USAGE_SIGN ) != 0 ) operation->key_usage_sign = 1; if( ( slot->policy.usage & PSA_KEY_USAGE_VERIFY ) != 0 ) From ae382791fba342d5a630b727f7ad144f053ad03a Mon Sep 17 00:00:00 2001 From: Moran Peker Date: Thu, 31 May 2018 14:06:17 +0300 Subject: [PATCH 27/54] add missing psa_cipher_abort( operation ) --- library/psa_crypto.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/library/psa_crypto.c b/library/psa_crypto.c index 56f3e1d4e4..a85b168453 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -1356,7 +1356,8 @@ static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation, mode = MBEDTLS_PADDING_NONE; break; default: - return ( PSA_ERROR_INVALID_ARGUMENT ); + psa_cipher_abort( operation ); + return( PSA_ERROR_INVALID_ARGUMENT ); } ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, mode ); if( ret != 0 ) @@ -1430,10 +1431,8 @@ psa_status_t psa_encrypt_set_iv(psa_cipher_operation_t *operation, return( PSA_ERROR_BAD_STATE ); if( iv_length != operation->iv_size ) { - if( ( ( operation->alg ) & PSA_ALG_ARC4 ) == PSA_ALG_ARC4 ) - return( PSA_ERROR_BAD_STATE ); - else - return( PSA_ERROR_INVALID_ARGUMENT ); + psa_cipher_abort( operation ); + return( PSA_ERROR_INVALID_ARGUMENT ); } ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length ); if( ret != 0 ) From 395db875e6906ea6e064e742b448ab77e2c74343 Mon Sep 17 00:00:00 2001 From: Moran Peker Date: Thu, 31 May 2018 14:07:14 +0300 Subject: [PATCH 28/54] adjust indentation per Mbed TLS standards --- library/psa_crypto.c | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/library/psa_crypto.c b/library/psa_crypto.c index a85b168453..9f647af842 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -1376,7 +1376,7 @@ static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation, operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ); } - return ( PSA_SUCCESS ); + return( PSA_SUCCESS ); } psa_status_t psa_encrypt_setup(psa_cipher_operation_t *operation, @@ -1416,10 +1416,10 @@ psa_status_t psa_encrypt_generate_iv(psa_cipher_operation_t *operation, *iv_length = operation->iv_size; ret = psa_encrypt_set_iv( operation, iv, *iv_length ); - exit: - if( ret != PSA_SUCCESS ) - psa_cipher_abort( operation ); - return( ret ); +exit: + if( ret != PSA_SUCCESS ) + psa_cipher_abort( operation ); + return( ret ); } psa_status_t psa_encrypt_set_iv(psa_cipher_operation_t *operation, @@ -1444,7 +1444,7 @@ psa_status_t psa_encrypt_set_iv(psa_cipher_operation_t *operation, operation->iv_set = 1; operation->iv_required = 0; - return ( PSA_SUCCESS ); + return( PSA_SUCCESS ); } psa_status_t psa_cipher_update(psa_cipher_operation_t *operation, @@ -1455,10 +1455,12 @@ psa_status_t psa_cipher_update(psa_cipher_operation_t *operation, size_t *output_length) { int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; - - if( ( ( PSA_ALG_IS_STREAM_CIPHER( operation->alg ) ) && ( output_size < input_length ) ) - || ( ( PSA_ALG_IS_BLOCK_CIPHER(operation->alg)) && ( output_size < ((operation->ctx.cipher.unprocessed_len + input_length)/16)*16 ) ) ) - return ( PSA_ERROR_BUFFER_TOO_SMALL ); + size_t expected_output_size = ( ( operation->ctx.cipher.unprocessed_len + input_length )/operation->block_size )*operation->block_size; + if( ( ( PSA_ALG_IS_STREAM_CIPHER( operation->alg ) ) && + ( output_size < input_length ) ) || + ( ( PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) ) && + ( output_size < expected_output_size ) ) ) + return( PSA_ERROR_BUFFER_TOO_SMALL ); ret = mbedtls_cipher_update( &operation->ctx.cipher, input, input_length, output, output_length ); @@ -1468,7 +1470,7 @@ psa_status_t psa_cipher_update(psa_cipher_operation_t *operation, return( mbedtls_to_psa_error( ret ) ); } - return ( PSA_SUCCESS ); + return( PSA_SUCCESS ); } psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation, @@ -1525,7 +1527,7 @@ psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation) operation->block_size = 0; operation->iv_required = 0; - return ( PSA_SUCCESS ); + return( PSA_SUCCESS ); } From 3520c2c4f713c972afb80fce92109c032f491d4b Mon Sep 17 00:00:00 2001 From: Moran Peker Date: Thu, 31 May 2018 14:51:58 +0300 Subject: [PATCH 29/54] unset iv_required to 0 (psa_encrypt_set_iv)and block_size (psa_cipher_setup) --- library/psa_crypto.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/library/psa_crypto.c b/library/psa_crypto.c index 9f647af842..34b5e25306 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -1324,8 +1324,6 @@ static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation, if( cipher_info == NULL ) return( PSA_ERROR_NOT_SUPPORTED ); - operation->block_size = cipher_info->block_size; - mbedtls_cipher_init( &operation->ctx.cipher ); ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info ); if( ret != 0 ) @@ -1442,7 +1440,6 @@ psa_status_t psa_encrypt_set_iv(psa_cipher_operation_t *operation, } operation->iv_set = 1; - operation->iv_required = 0; return( PSA_SUCCESS ); } From 7691fb7b6bd176ff4b0f715bc574af06287b80f8 Mon Sep 17 00:00:00 2001 From: Moran Peker Date: Thu, 31 May 2018 16:15:31 +0300 Subject: [PATCH 30/54] add new test scenario (cipher_test_encrypt_multipart) --- tests/suites/test_suite_psa_crypto.data | 12 ++++ tests/suites/test_suite_psa_crypto.function | 67 +++++++++++++++++++++ 2 files changed, 79 insertions(+) diff --git a/tests/suites/test_suite_psa_crypto.data b/tests/suites/test_suite_psa_crypto.data index 3bf93b8428..3d844d531a 100644 --- a/tests/suites/test_suite_psa_crypto.data +++ b/tests/suites/test_suite_psa_crypto.data @@ -116,3 +116,15 @@ cipher_test_decrypt:PSA_ALG_CBC_BASE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf715880 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":"6bc1bee22e409f96e93d7e117393172a" + +PSA Symmetric encryption multipart: AES-128 +depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC +cipher_test_encrypt_multipart:PSA_ALG_CBC_BASE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a":7:"a076ec9dfbe47d52afc357336f20743b" + +PSA Symmetric encryption multipart: AES-128 +depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC +cipher_test_encrypt_multipart:PSA_ALG_CBC_BASE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a":3:"a076ec9dfbe47d52afc357336f20743b" + +PSA Symmetric encryption multipart: AES-128 +depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC +cipher_test_encrypt_multipart:PSA_ALG_CBC_BASE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a":11:"a076ec9dfbe47d52afc357336f20743b" diff --git a/tests/suites/test_suite_psa_crypto.function b/tests/suites/test_suite_psa_crypto.function index 6a7a2d12a9..e3007716f5 100644 --- a/tests/suites/test_suite_psa_crypto.function +++ b/tests/suites/test_suite_psa_crypto.function @@ -651,6 +651,73 @@ exit: } /* END_CASE */ +/* BEGIN_CASE */ +void cipher_test_encrypt_multipart( int alg_arg, int key_type_arg, + char *key_hex, + char *input_hex, + int first_part_size, 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_encrypt_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( (unsigned int)first_part_size < input_size ); + TEST_ASSERT( psa_cipher_update( &operation, input, first_part_size, + output, output_size, + &output_length) == PSA_SUCCESS ); + TEST_ASSERT( psa_cipher_update( &operation, input + first_part_size, input_size - first_part_size, + output, output_size, + &output_length) == PSA_SUCCESS ); + TEST_ASSERT( psa_cipher_finish( &operation, output + output_length, + output_size, &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_decrypt( int alg_arg, int key_type_arg, char *key_hex, From d8100245d8c5b8a70092ca16f60e63437069441d Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Mon, 4 Jun 2018 16:18:38 +0200 Subject: [PATCH 31/54] Remove cipher_test_positive, duplicated as cipher_test_encrypt cipher_test_positive was never compiled due to a syntax error in the BEGIN_CASE magic comment. It has now been duplicated as cipher_test_encrypt. Remove the copy that was never compiled. --- tests/suites/test_suite_psa_crypto.function | 62 --------------------- 1 file changed, 62 deletions(-) diff --git a/tests/suites/test_suite_psa_crypto.function b/tests/suites/test_suite_psa_crypto.function index e3007716f5..4c62962d8c 100644 --- a/tests/suites/test_suite_psa_crypto.function +++ b/tests/suites/test_suite_psa_crypto.function @@ -527,68 +527,6 @@ exit: } /* END_CASE */ -* BEGIN_CASE */ -void cipher_test_positive( int alg_arg, int key_type_arg, - char *key_hex, - char *input_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_encrypt_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_size, &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_encrypt( int alg_arg, int key_type_arg, char *key_hex, From e553c65cc3da11fa3137b4b0cb4af4cd694325b4 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Mon, 4 Jun 2018 16:22:46 +0200 Subject: [PATCH 32/54] Fix indentation and horizontal whitespace Only whitespace changes in this commit. --- library/psa_crypto.c | 78 ++++++++++++++++++++++---------------------- 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/library/psa_crypto.c b/library/psa_crypto.c index 34b5e25306..ddc007bc8a 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -1295,8 +1295,8 @@ psa_status_t psa_asymmetric_sign(psa_key_slot_t key, /* Symmetric cryptography */ /****************************************************************/ -static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation, - psa_key_slot_t key, +static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation, + psa_key_slot_t key, psa_algorithm_t alg, mbedtls_operation_t cipher_operation) { int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; @@ -1333,7 +1333,7 @@ static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation, } ret = mbedtls_cipher_setkey( &operation->ctx.cipher, slot->data.raw.data, - key_bits, cipher_operation ); + key_bits, cipher_operation ); if( ret != 0 ) { psa_cipher_abort( operation ); @@ -1377,24 +1377,24 @@ static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation, return( PSA_SUCCESS ); } -psa_status_t psa_encrypt_setup(psa_cipher_operation_t *operation, - psa_key_slot_t key, - psa_algorithm_t alg) +psa_status_t psa_encrypt_setup( psa_cipher_operation_t *operation, + psa_key_slot_t key, + psa_algorithm_t alg ) { return psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ); } -psa_status_t psa_decrypt_setup(psa_cipher_operation_t *operation, - psa_key_slot_t key, - psa_algorithm_t alg) +psa_status_t psa_decrypt_setup( psa_cipher_operation_t *operation, + psa_key_slot_t key, + psa_algorithm_t alg ) { return psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ); } -psa_status_t psa_encrypt_generate_iv(psa_cipher_operation_t *operation, - unsigned char *iv, - size_t iv_size, - size_t *iv_length) +psa_status_t psa_encrypt_generate_iv( psa_cipher_operation_t *operation, + unsigned char *iv, + size_t iv_size, + size_t *iv_length ) { int ret = PSA_SUCCESS; if( operation->iv_set || !( operation->iv_required ) ) @@ -1408,9 +1408,9 @@ psa_status_t psa_encrypt_generate_iv(psa_cipher_operation_t *operation, if( ret != 0 ) { ret = mbedtls_to_psa_error( ret ); - goto exit; + goto exit; } - + *iv_length = operation->iv_size; ret = psa_encrypt_set_iv( operation, iv, *iv_length ); @@ -1420,9 +1420,9 @@ exit: return( ret ); } -psa_status_t psa_encrypt_set_iv(psa_cipher_operation_t *operation, - const unsigned char *iv, - size_t iv_length) +psa_status_t psa_encrypt_set_iv( psa_cipher_operation_t *operation, + const unsigned char *iv, + size_t iv_length ) { int ret = PSA_SUCCESS; if( operation->iv_set || !( operation->iv_required ) ) @@ -1444,23 +1444,23 @@ psa_status_t psa_encrypt_set_iv(psa_cipher_operation_t *operation, return( PSA_SUCCESS ); } -psa_status_t psa_cipher_update(psa_cipher_operation_t *operation, - const uint8_t *input, - size_t input_length, - unsigned char *output, - size_t output_size, - size_t *output_length) +psa_status_t psa_cipher_update( psa_cipher_operation_t *operation, + const uint8_t *input, + size_t input_length, + unsigned char *output, + size_t output_size, + size_t *output_length ) { int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; size_t expected_output_size = ( ( operation->ctx.cipher.unprocessed_len + input_length )/operation->block_size )*operation->block_size; - if( ( ( PSA_ALG_IS_STREAM_CIPHER( operation->alg ) ) && - ( output_size < input_length ) ) || - ( ( PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) ) && - ( output_size < expected_output_size ) ) ) + if( ( ( PSA_ALG_IS_STREAM_CIPHER( operation->alg ) ) && + ( output_size < input_length ) ) || + ( ( PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) ) && + ( output_size < expected_output_size ) ) ) return( PSA_ERROR_BUFFER_TOO_SMALL ); ret = mbedtls_cipher_update( &operation->ctx.cipher, input, - input_length, output, output_length ); + input_length, output, output_length ); if( ret != 0 ) { psa_cipher_abort( operation ); @@ -1470,13 +1470,13 @@ psa_status_t psa_cipher_update(psa_cipher_operation_t *operation, return( PSA_SUCCESS ); } -psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation, - uint8_t *output, - size_t output_size, - size_t *output_length) +psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation, + uint8_t *output, + size_t output_size, + size_t *output_length ) { int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; - uint8_t temp_output_buffer[ MBEDTLS_MAX_BLOCK_LENGTH ]; + uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH]; if( ! operation->key_set ) return( PSA_ERROR_BAD_STATE ); @@ -1496,13 +1496,13 @@ psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation, } ret = mbedtls_cipher_finish( &operation->ctx.cipher, temp_output_buffer, - output_length ); + output_length ); if( ret != 0 ) { psa_cipher_abort( operation ); return( mbedtls_to_psa_error( ret ) ); } - if(output_size >= *output_length) + if( output_size >= *output_length ) memcpy( output, temp_output_buffer, *output_length ); else { @@ -1513,10 +1513,10 @@ psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation, return( PSA_SUCCESS ); } -psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation) -{ +psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation ) +{ mbedtls_cipher_free( &operation->ctx.cipher ); - + operation->alg = 0; operation->key_set = 0; operation->iv_set = 0; From 7e9288520f317045a1c5fa63ba1c192d03931db5 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Mon, 4 Jun 2018 16:23:10 +0200 Subject: [PATCH 33/54] Wrap lines to 80 columns --- library/psa_crypto.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/library/psa_crypto.c b/library/psa_crypto.c index ddc007bc8a..621e733efe 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -1297,7 +1297,8 @@ psa_status_t psa_asymmetric_sign(psa_key_slot_t key, static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation, psa_key_slot_t key, - psa_algorithm_t alg, mbedtls_operation_t cipher_operation) + psa_algorithm_t alg, + mbedtls_operation_t cipher_operation ) { int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; psa_status_t status; @@ -1368,7 +1369,9 @@ static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation, operation->key_set = 1; operation->alg = alg; - operation->block_size = PSA_ALG_IS_BLOCK_CIPHER( alg ) ? PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) : 1; + 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 ); @@ -1404,7 +1407,8 @@ psa_status_t psa_encrypt_generate_iv( psa_cipher_operation_t *operation, ret = PSA_ERROR_BUFFER_TOO_SMALL; goto exit; } - ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, iv, operation->iv_size ); + ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, + iv, operation->iv_size ); if( ret != 0 ) { ret = mbedtls_to_psa_error( ret ); @@ -1452,7 +1456,9 @@ psa_status_t psa_cipher_update( psa_cipher_operation_t *operation, size_t *output_length ) { int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; - size_t expected_output_size = ( ( operation->ctx.cipher.unprocessed_len + input_length )/operation->block_size )*operation->block_size; + size_t expected_output_size = + ( ( operation->ctx.cipher.unprocessed_len + input_length ) / + operation->block_size ) * operation->block_size; if( ( ( PSA_ALG_IS_STREAM_CIPHER( operation->alg ) ) && ( output_size < input_length ) ) || ( ( PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) ) && @@ -1487,10 +1493,12 @@ psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation, { if( operation->ctx.cipher.unprocessed_len > operation->block_size ) return( PSA_ERROR_INVALID_ARGUMENT ); - if( ( ( operation->alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK ) == PSA_ALG_BLOCK_CIPHER_PAD_NONE ) + if( ( ( operation->alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK ) + == PSA_ALG_BLOCK_CIPHER_PAD_NONE ) && ( operation->ctx.cipher.unprocessed_len != 0 ) ) return( PSA_ERROR_INVALID_ARGUMENT ); - if( ( ( operation->alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK ) == PSA_ALG_BLOCK_CIPHER_PAD_PKCS7 ) + if( ( ( operation->alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK ) + == PSA_ALG_BLOCK_CIPHER_PAD_PKCS7 ) && ( *output_length != operation->block_size ) ) return( PSA_ERROR_INVALID_ARGUMENT ); } From 89d789c9bc7785e511091dda26b5d946afd9b992 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Mon, 4 Jun 2018 17:17:16 +0200 Subject: [PATCH 34/54] Refactor some argument checks for readability No intended behavior change. --- library/psa_crypto.c | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/library/psa_crypto.c b/library/psa_crypto.c index 621e733efe..4d84ab2522 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -1456,13 +1456,22 @@ psa_status_t psa_cipher_update( psa_cipher_operation_t *operation, size_t *output_length ) { int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; - size_t expected_output_size = - ( ( operation->ctx.cipher.unprocessed_len + input_length ) / - operation->block_size ) * operation->block_size; - if( ( ( PSA_ALG_IS_STREAM_CIPHER( operation->alg ) ) && - ( output_size < input_length ) ) || - ( ( PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) ) && - ( output_size < expected_output_size ) ) ) + size_t expected_output_size; + if( PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) ) + { + /* Take the unprocessed partial block left over from previous + * update calls, if any, plus the input to this call. Remove + * the last partial block, if any. You get the data that will be + * output in this call. */ + expected_output_size = + ( operation->ctx.cipher.unprocessed_len + input_length ) + / operation->block_size * operation->block_size; + } + else + { + expected_output_size = input_length; + } + if( output_size < expected_output_size ) return( PSA_ERROR_BUFFER_TOO_SMALL ); ret = mbedtls_cipher_update( &operation->ctx.cipher, input, @@ -1493,14 +1502,17 @@ psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation, { if( operation->ctx.cipher.unprocessed_len > operation->block_size ) return( PSA_ERROR_INVALID_ARGUMENT ); - if( ( ( operation->alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK ) - == PSA_ALG_BLOCK_CIPHER_PAD_NONE ) - && ( operation->ctx.cipher.unprocessed_len != 0 ) ) - return( PSA_ERROR_INVALID_ARGUMENT ); - if( ( ( operation->alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK ) - == PSA_ALG_BLOCK_CIPHER_PAD_PKCS7 ) - && ( *output_length != operation->block_size ) ) - return( PSA_ERROR_INVALID_ARGUMENT ); + switch( operation->alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK ) + { + case PSA_ALG_BLOCK_CIPHER_PAD_NONE: + if( operation->ctx.cipher.unprocessed_len != 0 ) + return( PSA_ERROR_INVALID_ARGUMENT ); + break; + case PSA_ALG_BLOCK_CIPHER_PAD_PKCS7: + if( *output_length != operation->block_size ) + return( PSA_ERROR_INVALID_ARGUMENT ); + break; + } } ret = mbedtls_cipher_finish( &operation->ctx.cipher, temp_output_buffer, From 5eb6e9ed60d42ebd8c11e167ca014d7502105037 Mon Sep 17 00:00:00 2001 From: Moran Peker Date: Tue, 5 Jun 2018 11:38:39 +0300 Subject: [PATCH 35/54] PSA_ALG_CBC_BASE -> SA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE --- tests/suites/test_suite_psa_crypto.data | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/suites/test_suite_psa_crypto.data b/tests/suites/test_suite_psa_crypto.data index 3d844d531a..3519f11c3b 100644 --- a/tests/suites/test_suite_psa_crypto.data +++ b/tests/suites/test_suite_psa_crypto.data @@ -85,7 +85,7 @@ PSA sign RSA PKCS#1 v1.5 SHA-256, output buffer too small sign_fail:PSA_KEY_TYPE_RSA_KEYPAIR:"3082025e02010002818100af057d396ee84fb75fdbb5c2b13c7fe5a654aa8aa2470b541ee1feb0b12d25c79711531249e1129628042dbbb6c120d1443524ef4c0e6e1d8956eeb2077af12349ddeee54483bc06c2c61948cd02b202e796aebd94d3a7cbf859c2c1819c324cb82b9cd34ede263a2abffe4733f077869e8660f7d6834da53d690ef7985f6bc3020301000102818100874bf0ffc2f2a71d14671ddd0171c954d7fdbf50281e4f6d99ea0e1ebcf82faa58e7b595ffb293d1abe17f110b37c48cc0f36c37e84d876621d327f64bbe08457d3ec4098ba2fa0a319fba411c2841ed7be83196a8cdf9daa5d00694bc335fc4c32217fe0488bce9cb7202e59468b1ead119000477db2ca797fac19eda3f58c1024100e2ab760841bb9d30a81d222de1eb7381d82214407f1b975cbbfe4e1a9467fd98adbd78f607836ca5be1928b9d160d97fd45c12d6b52e2c9871a174c66b488113024100c5ab27602159ae7d6f20c3c2ee851e46dc112e689e28d5fcbbf990a99ef8a90b8bb44fd36467e7fc1789ceb663abda338652c3c73f111774902e840565927091024100b6cdbd354f7df579a63b48b3643e353b84898777b48b15f94e0bfc0567a6ae5911d57ad6409cf7647bf96264e9bd87eb95e263b7110b9a1f9f94acced0fafa4d024071195eec37e8d257decfc672b07ae639f10cbb9b0c739d0c809968d644a94e3fd6ed9287077a14583f379058f76a8aecd43c62dc8c0f41766650d725275ac4a1024100bb32d133edc2e048d463388b7be9cb4be29f4b6250be603e70e3647501c97ddde20a4e71be95fd5e71784e25aca4baf25be5738aae59bbfe1c997781447a2b24":PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_256):"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad":127:PSA_ERROR_BUFFER_TOO_SMALL PSA Key Policy set and get -key_policy:PSA_KEY_USAGE_ENCRYPT:PSA_ALG_CBC_BASE +key_policy:PSA_KEY_USAGE_ENCRYPT:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE PSA Key Policy enforcement - export key_policy_fail:PSA_KEY_USAGE_EXPORT:PSA_ALG_RSA_PKCS1V15_SIGN_RAW:PSA_ERROR_NOT_PERMITTED:"3082025e02010002818100af057d396ee84fb75fdbb5c2b13c7fe5a654aa8aa2470b541ee1feb0b12d25c79711531249e1129628042dbbb6c120d1443524ef4c0e6e1d8956eeb2077af12349ddeee54483bc06c2c61948cd02b202e796aebd94d3a7cbf859c2c1819c324cb82b9cd34ede263a2abffe4733f077869e8660f7d6834da53d690ef7985f6bc3020301000102818100874bf0ffc2f2a71d14671ddd0171c954d7fdbf50281e4f6d99ea0e1ebcf82faa58e7b595ffb293d1abe17f110b37c48cc0f36c37e84d876621d327f64bbe08457d3ec4098ba2fa0a319fba411c2841ed7be83196a8cdf9daa5d00694bc335fc4c32217fe0488bce9cb7202e59468b1ead119000477db2ca797fac19eda3f58c1024100e2ab760841bb9d30a81d222de1eb7381d82214407f1b975cbbfe4e1a9467fd98adbd78f607836ca5be1928b9d160d97fd45c12d6b52e2c9871a174c66b488113024100c5ab27602159ae7d6f20c3c2ee851e46dc112e689e28d5fcbbf990a99ef8a90b8bb44fd36467e7fc1789ceb663abda338652c3c73f111774902e840565927091024100b6cdbd354f7df579a63b48b3643e353b84898777b48b15f94e0bfc0567a6ae5911d57ad6409cf7647bf96264e9bd87eb95e263b7110b9a1f9f94acced0fafa4d024071195eec37e8d257decfc672b07ae639f10cbb9b0c739d0c809968d644a94e3fd6ed9287077a14583f379058f76a8aecd43c62dc8c0f41766650d725275ac4a1024100bb32d133edc2e048d463388b7be9cb4be29f4b6250be603e70e3647501c97ddde20a4e71be95fd5e71784e25aca4baf25be5738aae59bbfe1c997781447a2b24" @@ -107,24 +107,24 @@ 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_encrypt:PSA_ALG_CBC_BASE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a":"a076ec9dfbe47d52afc357336f20743b" +cipher_test_encrypt:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE: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" +cipher_test_decrypt:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"396ee84fb75fdbb5c2b13c7fe5a654aa":"49e4e66c89a86b67758df89db9ad6955" 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":"6bc1bee22e409f96e93d7e117393172a" +cipher_test_verify_output:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a" PSA Symmetric encryption multipart: AES-128 depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC -cipher_test_encrypt_multipart:PSA_ALG_CBC_BASE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a":7:"a076ec9dfbe47d52afc357336f20743b" +cipher_test_encrypt_multipart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a":7:"a076ec9dfbe47d52afc357336f20743b" PSA Symmetric encryption multipart: AES-128 depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC -cipher_test_encrypt_multipart:PSA_ALG_CBC_BASE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a":3:"a076ec9dfbe47d52afc357336f20743b" +cipher_test_encrypt_multipart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a":3:"a076ec9dfbe47d52afc357336f20743b" PSA Symmetric encryption multipart: AES-128 depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC -cipher_test_encrypt_multipart:PSA_ALG_CBC_BASE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a":11:"a076ec9dfbe47d52afc357336f20743b" +cipher_test_encrypt_multipart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a":11:"a076ec9dfbe47d52afc357336f20743b" From 7cb22b8327e97838c4b51d03b74e0f076ad77925 Mon Sep 17 00:00:00 2001 From: Moran Peker Date: Tue, 5 Jun 2018 11:40:02 +0300 Subject: [PATCH 36/54] abort operation before return + fix error checks --- library/psa_crypto.c | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/library/psa_crypto.c b/library/psa_crypto.c index 4d84ab2522..78e0dc4852 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -1342,7 +1342,7 @@ static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation, } #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) - if( ( alg & PSA_ALG_CBC_BASE) == PSA_ALG_CBC_BASE ) + if( ( alg & ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK ) == PSA_ALG_CBC_BASE ) { padding_mode = alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK; @@ -1494,24 +1494,29 @@ psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation, uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH]; if( ! operation->key_set ) - return( PSA_ERROR_BAD_STATE ); - if( operation->iv_required && ! operation->iv_set ) - return( PSA_ERROR_BAD_STATE ); - - if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT ) { - if( operation->ctx.cipher.unprocessed_len > operation->block_size ) - return( PSA_ERROR_INVALID_ARGUMENT ); - switch( operation->alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK ) + psa_cipher_abort( operation ); + return( PSA_ERROR_BAD_STATE ); + } + if( operation->iv_required && ! operation->iv_set ) + { + psa_cipher_abort( operation ); + return( PSA_ERROR_BAD_STATE ); + } + if( ( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT ) && PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) ) + { + if( operation->ctx.cipher.unprocessed_len >= operation->block_size ) { - case PSA_ALG_BLOCK_CIPHER_PAD_NONE: - if( operation->ctx.cipher.unprocessed_len != 0 ) - return( PSA_ERROR_INVALID_ARGUMENT ); - break; - case PSA_ALG_BLOCK_CIPHER_PAD_PKCS7: - if( *output_length != operation->block_size ) - return( PSA_ERROR_INVALID_ARGUMENT ); - break; + psa_cipher_abort( operation ); + return( PSA_ERROR_TAMPERING_DETECTED ); + } + if( ( operation->alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK ) == PSA_ALG_BLOCK_CIPHER_PAD_NONE ) + { + if( operation->ctx.cipher.unprocessed_len != 0 ) + { + psa_cipher_abort( operation ); + return( PSA_ERROR_INVALID_ARGUMENT ); + } } } From 5351420b3e5a4e26c58ddc6341bc026fbea519a4 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Wed, 6 Jun 2018 15:11:46 +0200 Subject: [PATCH 37/54] Use block local variable for padding_mode for readability No intended behavior change. --- library/psa_crypto.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/library/psa_crypto.c b/library/psa_crypto.c index 78e0dc4852..7b59797780 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -1306,8 +1306,6 @@ static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation, psa_key_type_t key_type; size_t key_bits; const mbedtls_cipher_info_t *cipher_info = NULL; - psa_algorithm_t padding_mode = PSA_ALG_BLOCK_CIPHER_PAD_NONE; - mbedtls_cipher_padding_t mode = MBEDTLS_PADDING_NONE; operation->alg = alg; operation->key_set = 0; @@ -1344,7 +1342,8 @@ static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation, #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) if( ( alg & ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK ) == PSA_ALG_CBC_BASE ) { - padding_mode = alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK; + psa_algorithm_t padding_mode = alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK; + mbedtls_cipher_padding_t mode; switch ( padding_mode ) { @@ -1505,12 +1504,14 @@ psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation, } if( ( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT ) && PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) ) { + psa_algorithm_t padding_mode = + operation->alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK; if( operation->ctx.cipher.unprocessed_len >= operation->block_size ) { psa_cipher_abort( operation ); return( PSA_ERROR_TAMPERING_DETECTED ); } - if( ( operation->alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK ) == PSA_ALG_BLOCK_CIPHER_PAD_NONE ) + if( padding_mode == PSA_ALG_BLOCK_CIPHER_PAD_NONE ) { if( operation->ctx.cipher.unprocessed_len != 0 ) { From 2c5219a06d65d3677f90b4a3a5407cbc0d0e3067 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Wed, 6 Jun 2018 15:12:32 +0200 Subject: [PATCH 38/54] Whitespace normalization No semantic change. --- library/psa_crypto.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/library/psa_crypto.c b/library/psa_crypto.c index 7b59797780..dc25dfb4d0 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -1499,23 +1499,24 @@ psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation, } if( operation->iv_required && ! operation->iv_set ) { - psa_cipher_abort( operation ); + psa_cipher_abort( operation ); return( PSA_ERROR_BAD_STATE ); } - if( ( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT ) && PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) ) + if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT && + PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) ) { psa_algorithm_t padding_mode = operation->alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK; if( operation->ctx.cipher.unprocessed_len >= operation->block_size ) { - psa_cipher_abort( operation ); + psa_cipher_abort( operation ); return( PSA_ERROR_TAMPERING_DETECTED ); } if( padding_mode == PSA_ALG_BLOCK_CIPHER_PAD_NONE ) { if( operation->ctx.cipher.unprocessed_len != 0 ) { - psa_cipher_abort( operation ); + psa_cipher_abort( operation ); return( PSA_ERROR_INVALID_ARGUMENT ); } } From 691dfb3e3a3292cc98616916b6dc3d946bf9025e Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Wed, 6 Jun 2018 15:18:02 +0200 Subject: [PATCH 39/54] Whitespce normalization No semantic change. --- tests/suites/test_suite_psa_crypto.function | 47 +++++++++++---------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/tests/suites/test_suite_psa_crypto.function b/tests/suites/test_suite_psa_crypto.function index 4c62962d8c..058c344119 100644 --- a/tests/suites/test_suite_psa_crypto.function +++ b/tests/suites/test_suite_psa_crypto.function @@ -528,9 +528,9 @@ exit: /* END_CASE */ /* BEGIN_CASE */ -void cipher_test_encrypt( int alg_arg, int key_type_arg, - char *key_hex, - char *input_hex, char *output_hex ) +void cipher_test_encrypt( 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; @@ -557,7 +557,7 @@ void cipher_test_encrypt( int alg_arg, int key_type_arg, 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, @@ -571,7 +571,7 @@ void cipher_test_encrypt( int alg_arg, int key_type_arg, output = mbedtls_calloc(0, output_size); TEST_ASSERT( psa_cipher_update( &operation, input, input_size, - output, output_size, + output, output_size, &output_length) == PSA_SUCCESS ); TEST_ASSERT( psa_cipher_finish( &operation, output + output_length, output_size, &output_length) == PSA_SUCCESS ); @@ -590,7 +590,7 @@ exit: /* END_CASE */ /* BEGIN_CASE */ -void cipher_test_encrypt_multipart( int alg_arg, int key_type_arg, +void cipher_test_encrypt_multipart( int alg_arg, int key_type_arg, char *key_hex, char *input_hex, int first_part_size, char *output_hex ) @@ -620,7 +620,7 @@ void cipher_test_encrypt_multipart( int alg_arg, int key_type_arg, 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, @@ -629,16 +629,17 @@ void cipher_test_encrypt_multipart( int alg_arg, int key_type_arg, TEST_ASSERT( psa_encrypt_setup( &operation, key_slot, alg ) == PSA_SUCCESS ); TEST_ASSERT( psa_encrypt_set_iv( &operation, iv, - sizeof( iv ) ) == PSA_SUCCESS ); + sizeof( iv ) ) == PSA_SUCCESS ); output = mbedtls_calloc(0, output_size); TEST_ASSERT( (unsigned int)first_part_size < input_size ); TEST_ASSERT( psa_cipher_update( &operation, input, first_part_size, - output, output_size, + output, output_size, &output_length) == PSA_SUCCESS ); - TEST_ASSERT( psa_cipher_update( &operation, input + first_part_size, input_size - first_part_size, - output, output_size, + TEST_ASSERT( psa_cipher_update( &operation, input + first_part_size, + input_size - first_part_size, + output, output_size, &output_length) == PSA_SUCCESS ); TEST_ASSERT( psa_cipher_finish( &operation, output + output_length, output_size, &output_length) == PSA_SUCCESS ); @@ -657,7 +658,7 @@ exit: /* END_CASE */ /* BEGIN_CASE */ -void cipher_test_decrypt( int alg_arg, int key_type_arg, +void cipher_test_decrypt( int alg_arg, int key_type_arg, char *key_hex, char *input_hex, char *output_hex ) { @@ -686,7 +687,7 @@ void cipher_test_decrypt( int alg_arg, int key_type_arg, 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, @@ -700,7 +701,7 @@ void cipher_test_decrypt( int alg_arg, int key_type_arg, output = mbedtls_calloc(0, output_size); TEST_ASSERT( psa_cipher_update( &operation, input, input_size, - output, output_size, + output, output_size, &output_length) == PSA_SUCCESS ); TEST_ASSERT( psa_cipher_finish( &operation, output + output_length, output_size, &output_length) == PSA_SUCCESS ); @@ -720,7 +721,7 @@ exit: /* BEGIN_CASE */ -void cipher_test_verify_output( int alg_arg, int key_type_arg, +void cipher_test_verify_output( int alg_arg, int key_type_arg, char *key_hex, char *input_hex ) { @@ -749,7 +750,7 @@ void cipher_test_verify_output( int alg_arg, int key_type_arg, input = unhexify_alloc( input_hex, &input_size ); TEST_ASSERT( input != NULL ); - + TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); TEST_ASSERT( psa_import_key( key_slot, key_type, @@ -763,11 +764,11 @@ void cipher_test_verify_output( int alg_arg, int key_type_arg, output1_size = input_size; output1 = mbedtls_calloc(0, output1_size); TEST_ASSERT( psa_cipher_update( &operation1, input, input_size, - output1, output1_size, + output1, output1_size, &output1_length) == PSA_SUCCESS ); TEST_ASSERT( psa_cipher_finish( &operation1, output1 + output1_length, - output1_size, &tmp_output_length) == PSA_SUCCESS ); - + output1_size, &tmp_output_length) == PSA_SUCCESS ); + output1_length += tmp_output_length; TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); @@ -776,15 +777,15 @@ void cipher_test_verify_output( int alg_arg, int key_type_arg, output2 = mbedtls_calloc(0, output2_size); TEST_ASSERT( psa_encrypt_set_iv( &operation2, iv, - iv_length) == PSA_SUCCESS ); - TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length, + 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, - output2_size, &tmp_output_length) == PSA_SUCCESS ); + output2_size, &tmp_output_length) == PSA_SUCCESS ); output2_length += tmp_output_length; - + TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); TEST_ASSERT( input_size == output1_length ); From 7268afc29e5fadfc7ade62a8dfa3dcf54aecef70 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Wed, 6 Jun 2018 15:19:24 +0200 Subject: [PATCH 40/54] Reordered cipher tests to be just after MAC tests --- tests/suites/test_suite_psa_crypto.data | 48 +- tests/suites/test_suite_psa_crypto.function | 530 ++++++++++---------- 2 files changed, 291 insertions(+), 287 deletions(-) diff --git a/tests/suites/test_suite_psa_crypto.data b/tests/suites/test_suite_psa_crypto.data index 3519f11c3b..6fa907d58b 100644 --- a/tests/suites/test_suite_psa_crypto.data +++ b/tests/suites/test_suite_psa_crypto.data @@ -53,6 +53,30 @@ PSA MAC verify: CMAC-AES-128 depends_on:MBEDTLS_CMAC_C:MBEDTLS_AES_C mac_verify:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":PSA_ALG_CMAC:"":"6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411":"dfa66747de9ae63030ca32611497c827" +PSA Symmetric encryption: AES-128 +depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC +cipher_test_encrypt:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE: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_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"396ee84fb75fdbb5c2b13c7fe5a654aa":"49e4e66c89a86b67758df89db9ad6955" + +PSA Symmetric encryption/decryption: AES-128 +depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC +cipher_test_verify_output:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a" + +PSA Symmetric encryption multipart: AES-128 +depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC +cipher_test_encrypt_multipart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a":7:"a076ec9dfbe47d52afc357336f20743b" + +PSA Symmetric encryption multipart: AES-128 +depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC +cipher_test_encrypt_multipart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a":3:"a076ec9dfbe47d52afc357336f20743b" + +PSA Symmetric encryption multipart: AES-128 +depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC +cipher_test_encrypt_multipart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a":11:"a076ec9dfbe47d52afc357336f20743b" + PSA signature size: RSA keypair, 1024 bits, PKCS#1 v1.5 raw signature_size:PSA_KEY_TYPE_RSA_KEYPAIR:1024:PSA_ALG_RSA_PKCS1V15_SIGN_RAW:128 @@ -104,27 +128,3 @@ key_lifetime_set_fail:1:PSA_KEY_LIFETIME_WRITE_ONCE:PSA_ERROR_NOT_SUPPORTED PSA Key Lifetime set fail, invalid key lifetime value 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_encrypt:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE: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_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"396ee84fb75fdbb5c2b13c7fe5a654aa":"49e4e66c89a86b67758df89db9ad6955" - -PSA Symmetric encryption/decryption: AES-128 -depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC -cipher_test_verify_output:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a" - -PSA Symmetric encryption multipart: AES-128 -depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC -cipher_test_encrypt_multipart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a":7:"a076ec9dfbe47d52afc357336f20743b" - -PSA Symmetric encryption multipart: AES-128 -depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC -cipher_test_encrypt_multipart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a":3:"a076ec9dfbe47d52afc357336f20743b" - -PSA Symmetric encryption multipart: AES-128 -depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC -cipher_test_encrypt_multipart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a":11:"a076ec9dfbe47d52afc357336f20743b" diff --git a/tests/suites/test_suite_psa_crypto.function b/tests/suites/test_suite_psa_crypto.function index 058c344119..5ac87214f1 100644 --- a/tests/suites/test_suite_psa_crypto.function +++ b/tests/suites/test_suite_psa_crypto.function @@ -264,269 +264,6 @@ exit: } /* END_CASE */ -/* BEGIN_CASE */ -void signature_size( int type_arg, int bits, int alg_arg, int expected_size_arg ) -{ - psa_key_type_t type = type_arg; - psa_algorithm_t alg = alg_arg; - size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(type, bits, alg); - TEST_ASSERT( actual_size == (size_t) expected_size_arg ); -exit: - ; -} -/* END_CASE */ - -/* BEGIN_CASE */ -void sign_deterministic( int key_type_arg, char *key_hex, - int alg_arg, char *input_hex, char *output_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; - size_t key_bits; - unsigned char *input_data = NULL; - size_t input_size; - unsigned char *output_data = NULL; - size_t output_size; - unsigned char *signature = NULL; - size_t signature_size; - size_t signature_length = 0xdeadbeef; - 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 ); - output_data = unhexify_alloc( output_hex, &output_size ); - TEST_ASSERT( output_data != NULL ); - - TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); - - psa_key_policy_init( &policy ); - - psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg_arg ); - - 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_get_key_information( slot, - NULL, - &key_bits ) == PSA_SUCCESS ); - - signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type, alg, key_bits ); - TEST_ASSERT( signature_size != 0 ); - signature = mbedtls_calloc( 1, signature_size ); - TEST_ASSERT( signature != NULL ); - - TEST_ASSERT( psa_asymmetric_sign( slot, alg, - input_data, input_size, - NULL, 0, - signature, signature_size, - &signature_length ) == PSA_SUCCESS ); - TEST_ASSERT( signature_length == output_size ); - TEST_ASSERT( memcmp( signature, output_data, output_size ) == 0 ); - -exit: - psa_destroy_key( slot ); - mbedtls_free( key_data ); - mbedtls_free( input_data ); - mbedtls_free( output_data ); - mbedtls_free( signature ); - mbedtls_psa_crypto_free( ); -} -/* END_CASE */ - -/* BEGIN_CASE */ -void sign_fail( int key_type_arg, char *key_hex, - int alg_arg, char *input_hex, - int signature_size, int expected_status_arg ) -{ - 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; - psa_status_t actual_status; - psa_status_t expected_status = expected_status_arg; - unsigned char *signature = NULL; - size_t signature_length = 0xdeadbeef; - 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 ); - signature = mbedtls_calloc( 1, signature_size ); - TEST_ASSERT( signature != NULL ); - - TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); - - psa_key_policy_init( &policy ); - - psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg_arg ); - - TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); - - TEST_ASSERT( psa_import_key( slot, key_type, - key_data, key_size ) == PSA_SUCCESS ); - - actual_status = psa_asymmetric_sign( slot, alg, - input_data, input_size, - NULL, 0, - signature, signature_size, - &signature_length ); - TEST_ASSERT( actual_status == expected_status ); - TEST_ASSERT( signature_length == 0 ); - -exit: - psa_destroy_key( slot ); - mbedtls_free( key_data ); - mbedtls_free( input_data ); - mbedtls_free( signature ); - mbedtls_psa_crypto_free( ); -} -/* END_CASE */ - -/* BEGIN_CASE */ -void key_policy( int usage_arg, int alg_arg ) -{ - int key_slot = 1; - psa_key_type_t key_type = PSA_KEY_TYPE_AES; - unsigned char key[32] = {0}; - psa_key_policy_t policy_set = {0}; - psa_key_policy_t policy_get = {0}; - - - memset( key, 0x2a, sizeof( key ) ); - - TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); - - psa_key_policy_init(& policy_set ); - psa_key_policy_init(& policy_get ); - - psa_key_policy_set_usage( &policy_set, usage_arg, alg_arg ); - - TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == ( psa_key_usage_t )usage_arg ); - - TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set) == ( psa_algorithm_t )alg_arg ); - - TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS ); - - TEST_ASSERT( psa_import_key( key_slot, key_type, - key, sizeof( key ) ) == PSA_SUCCESS ); - - TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS ); - - TEST_ASSERT( policy_get.usage == policy_set.usage ); - TEST_ASSERT( policy_get.alg == policy_set.alg ); - - - - -exit: - psa_destroy_key( key_slot ); - mbedtls_psa_crypto_free( ); -} -/* END_CASE */ - -/* BEGIN_CASE */ -void key_policy_fail( int usage_arg, int alg_arg, int expected_status, char *key_hex ) -{ - int key_slot = 1; - unsigned char* keypair = NULL; - size_t key_size = 0; - size_t signature_length = 0; - psa_key_policy_t policy = {0}; - int actual_status = PSA_SUCCESS; - - TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); - - psa_key_policy_init( &policy ); - - psa_key_policy_set_usage( &policy, usage_arg, alg_arg ); - - TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS ); - - if( usage_arg & PSA_KEY_USAGE_EXPORT ) - { - keypair = unhexify_alloc( key_hex, &key_size ); - TEST_ASSERT( keypair != NULL ); - TEST_ASSERT( psa_import_key( key_slot, PSA_KEY_TYPE_RSA_KEYPAIR, - keypair, key_size ) == PSA_SUCCESS ); - actual_status = psa_asymmetric_sign( key_slot, - ( psa_algorithm_t )alg_arg, NULL, 0, NULL, 0, - NULL, 0, &signature_length ); - } - - if( usage_arg & PSA_KEY_USAGE_SIGN ) - { - keypair = unhexify_alloc( key_hex, &key_size ); - TEST_ASSERT( keypair != NULL ); - TEST_ASSERT( psa_import_key( key_slot, PSA_KEY_TYPE_RSA_KEYPAIR, - keypair, key_size ) == PSA_SUCCESS ); - actual_status = psa_export_key( key_slot, NULL, 0, NULL ); - } - - TEST_ASSERT( actual_status == expected_status ); - -exit: - psa_destroy_key( key_slot ); - mbedtls_free( keypair ); - mbedtls_psa_crypto_free( ); -} -/* END_CASE */ - -/* BEGIN_CASE */ -void key_lifetime( int lifetime_arg ) -{ - int key_slot = 1; - psa_key_type_t key_type = PSA_ALG_CBC_BASE; - unsigned char key[32] = {0}; - psa_key_lifetime_t lifetime_set = (psa_key_lifetime_t) lifetime_arg; - psa_key_lifetime_t lifetime_get; - memset( key, 0x2a, sizeof( key ) ); - TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); - TEST_ASSERT( psa_set_key_lifetime( key_slot, - lifetime_set ) == PSA_SUCCESS ); - TEST_ASSERT( psa_import_key( key_slot, key_type, - key, sizeof( key ) ) == PSA_SUCCESS ); - TEST_ASSERT( psa_get_key_lifetime( key_slot, - &lifetime_get ) == PSA_SUCCESS ); - TEST_ASSERT( lifetime_get == lifetime_set ); -exit: - psa_destroy_key( key_slot ); - mbedtls_psa_crypto_free( ); -} -/* END_CASE */ - -/* BEGIN_CASE */ -void key_lifetime_set_fail( int key_slot_arg, int lifetime_arg, int expected_status_arg ) -{ - int key_slot = 1; - psa_key_lifetime_t lifetime_set = (psa_key_lifetime_t) lifetime_arg; - psa_status_t actual_status; - psa_status_t expected_status = expected_status_arg; - - TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); - - actual_status = psa_set_key_lifetime( key_slot_arg, lifetime_set ); - - if( actual_status == PSA_SUCCESS ) - actual_status = psa_set_key_lifetime( key_slot_arg, lifetime_set ); - - TEST_ASSERT( expected_status == actual_status ); - -exit: - psa_destroy_key( key_slot ); - mbedtls_psa_crypto_free( ); -} -/* END_CASE */ - /* BEGIN_CASE */ void cipher_test_encrypt( int alg_arg, int key_type_arg, char *key_hex, @@ -799,3 +536,270 @@ exit: mbedtls_psa_crypto_free( ); } /* END_CASE */ + +/* BEGIN_CASE */ +void signature_size( int type_arg, int bits, int alg_arg, int expected_size_arg ) +{ + psa_key_type_t type = type_arg; + psa_algorithm_t alg = alg_arg; + size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(type, bits, alg); + TEST_ASSERT( actual_size == (size_t) expected_size_arg ); +exit: + ; +} +/* END_CASE */ + +/* BEGIN_CASE */ +void sign_deterministic( int key_type_arg, char *key_hex, + int alg_arg, char *input_hex, char *output_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; + size_t key_bits; + unsigned char *input_data = NULL; + size_t input_size; + unsigned char *output_data = NULL; + size_t output_size; + unsigned char *signature = NULL; + size_t signature_size; + size_t signature_length = 0xdeadbeef; + 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 ); + output_data = unhexify_alloc( output_hex, &output_size ); + TEST_ASSERT( output_data != NULL ); + + TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); + + psa_key_policy_init( &policy ); + + psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg_arg ); + + 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_get_key_information( slot, + NULL, + &key_bits ) == PSA_SUCCESS ); + + signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type, alg, key_bits ); + TEST_ASSERT( signature_size != 0 ); + signature = mbedtls_calloc( 1, signature_size ); + TEST_ASSERT( signature != NULL ); + + TEST_ASSERT( psa_asymmetric_sign( slot, alg, + input_data, input_size, + NULL, 0, + signature, signature_size, + &signature_length ) == PSA_SUCCESS ); + TEST_ASSERT( signature_length == output_size ); + TEST_ASSERT( memcmp( signature, output_data, output_size ) == 0 ); + +exit: + psa_destroy_key( slot ); + mbedtls_free( key_data ); + mbedtls_free( input_data ); + mbedtls_free( output_data ); + mbedtls_free( signature ); + mbedtls_psa_crypto_free( ); +} +/* END_CASE */ + +/* BEGIN_CASE */ +void sign_fail( int key_type_arg, char *key_hex, + int alg_arg, char *input_hex, + int signature_size, int expected_status_arg ) +{ + 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; + psa_status_t actual_status; + psa_status_t expected_status = expected_status_arg; + unsigned char *signature = NULL; + size_t signature_length = 0xdeadbeef; + 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 ); + signature = mbedtls_calloc( 1, signature_size ); + TEST_ASSERT( signature != NULL ); + + TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); + + psa_key_policy_init( &policy ); + + psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg_arg ); + + TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); + + TEST_ASSERT( psa_import_key( slot, key_type, + key_data, key_size ) == PSA_SUCCESS ); + + actual_status = psa_asymmetric_sign( slot, alg, + input_data, input_size, + NULL, 0, + signature, signature_size, + &signature_length ); + TEST_ASSERT( actual_status == expected_status ); + TEST_ASSERT( signature_length == 0 ); + +exit: + psa_destroy_key( slot ); + mbedtls_free( key_data ); + mbedtls_free( input_data ); + mbedtls_free( signature ); + mbedtls_psa_crypto_free( ); +} +/* END_CASE */ + +/* BEGIN_CASE */ +void key_policy( int usage_arg, int alg_arg ) +{ + int key_slot = 1; + psa_key_type_t key_type = PSA_KEY_TYPE_AES; + unsigned char key[32] = {0}; + psa_key_policy_t policy_set = {0}; + psa_key_policy_t policy_get = {0}; + + memset( key, 0x2a, sizeof( key ) ); + + TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); + + psa_key_policy_init(& policy_set ); + psa_key_policy_init(& policy_get ); + + psa_key_policy_set_usage( &policy_set, usage_arg, alg_arg ); + + TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == ( psa_key_usage_t )usage_arg ); + + TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set) == ( psa_algorithm_t )alg_arg ); + + TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS ); + + TEST_ASSERT( psa_import_key( key_slot, key_type, + key, sizeof( key ) ) == PSA_SUCCESS ); + + TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS ); + + TEST_ASSERT( policy_get.usage == policy_set.usage ); + TEST_ASSERT( policy_get.alg == policy_set.alg ); + +exit: + psa_destroy_key( key_slot ); + mbedtls_psa_crypto_free( ); +} +/* END_CASE */ + +/* BEGIN_CASE */ +void key_policy_fail( int usage_arg, int alg_arg, int expected_status, char *key_hex ) +{ + int key_slot = 1; + unsigned char* keypair = NULL; + size_t key_size = 0; + size_t signature_length = 0; + psa_key_policy_t policy = {0}; + int actual_status = PSA_SUCCESS; + + TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); + + psa_key_policy_init( &policy ); + + psa_key_policy_set_usage( &policy, usage_arg, alg_arg ); + + TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS ); + + if( usage_arg & PSA_KEY_USAGE_EXPORT ) + { + keypair = unhexify_alloc( key_hex, &key_size ); + TEST_ASSERT( keypair != NULL ); + TEST_ASSERT( psa_import_key( key_slot, PSA_KEY_TYPE_RSA_KEYPAIR, + keypair, key_size ) == PSA_SUCCESS ); + actual_status = psa_asymmetric_sign( key_slot, + ( psa_algorithm_t )alg_arg, NULL, 0, NULL, 0, + NULL, 0, &signature_length ); + } + + if( usage_arg & PSA_KEY_USAGE_SIGN ) + { + keypair = unhexify_alloc( key_hex, &key_size ); + TEST_ASSERT( keypair != NULL ); + TEST_ASSERT( psa_import_key( key_slot, PSA_KEY_TYPE_RSA_KEYPAIR, + keypair, key_size ) == PSA_SUCCESS ); + actual_status = psa_export_key( key_slot, NULL, 0, NULL ); + } + + TEST_ASSERT( actual_status == expected_status ); + +exit: + psa_destroy_key( key_slot ); + mbedtls_free( keypair ); + mbedtls_psa_crypto_free( ); +} +/* END_CASE */ + +/* BEGIN_CASE */ +void key_lifetime( int lifetime_arg ) +{ + int key_slot = 1; + psa_key_type_t key_type = PSA_ALG_CBC_BASE; + unsigned char key[32] = {0}; + psa_key_lifetime_t lifetime_set = (psa_key_lifetime_t) lifetime_arg; + psa_key_lifetime_t lifetime_get; + + memset( key, 0x2a, sizeof( key ) ); + + TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); + + TEST_ASSERT( psa_set_key_lifetime( key_slot, + lifetime_set ) == PSA_SUCCESS ); + + TEST_ASSERT( psa_import_key( key_slot, key_type, + key, sizeof( key ) ) == PSA_SUCCESS ); + + TEST_ASSERT( psa_get_key_lifetime( key_slot, + &lifetime_get ) == PSA_SUCCESS ); + + TEST_ASSERT( lifetime_get == lifetime_set ); + +exit: + psa_destroy_key( key_slot ); + mbedtls_psa_crypto_free( ); +} +/* END_CASE */ + + +/* BEGIN_CASE */ +void key_lifetime_set_fail( int key_slot_arg, int lifetime_arg, int expected_status_arg ) +{ + int key_slot = 1; + psa_key_lifetime_t lifetime_set = (psa_key_lifetime_t) lifetime_arg; + psa_status_t actual_status; + psa_status_t expected_status = expected_status_arg; + + TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); + + actual_status = psa_set_key_lifetime( key_slot_arg, lifetime_set ); + + if( actual_status == PSA_SUCCESS ) + actual_status = psa_set_key_lifetime( key_slot_arg, lifetime_set ); + + TEST_ASSERT( expected_status == actual_status ); + +exit: + psa_destroy_key( key_slot ); + mbedtls_psa_crypto_free( ); +} +/* END_CASE */ From 5cbb4c8508919f2f0e74ae3426b8cbefad6fc093 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Wed, 6 Jun 2018 15:21:31 +0200 Subject: [PATCH 41/54] Correct some test case descriptions --- tests/suites/test_suite_psa_crypto.data | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/suites/test_suite_psa_crypto.data b/tests/suites/test_suite_psa_crypto.data index 6fa907d58b..2126462c05 100644 --- a/tests/suites/test_suite_psa_crypto.data +++ b/tests/suites/test_suite_psa_crypto.data @@ -57,7 +57,7 @@ PSA Symmetric encryption: AES-128 depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC cipher_test_encrypt:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a":"a076ec9dfbe47d52afc357336f20743b" -PSA Symmetric encryption: AES-128 +PSA Symmetric decryption: AES-128 depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC cipher_test_decrypt:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"396ee84fb75fdbb5c2b13c7fe5a654aa":"49e4e66c89a86b67758df89db9ad6955" @@ -65,15 +65,15 @@ PSA Symmetric encryption/decryption: AES-128 depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC cipher_test_verify_output:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a" -PSA Symmetric encryption multipart: AES-128 +PSA Symmetric encryption multipart: AES-128, 7+9 depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC cipher_test_encrypt_multipart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a":7:"a076ec9dfbe47d52afc357336f20743b" -PSA Symmetric encryption multipart: AES-128 +PSA Symmetric encryption multipart: AES-128, 3+13 depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC cipher_test_encrypt_multipart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a":3:"a076ec9dfbe47d52afc357336f20743b" -PSA Symmetric encryption multipart: AES-128 +PSA Symmetric encryption multipart: AES-128, 11+5 depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC cipher_test_encrypt_multipart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a":11:"a076ec9dfbe47d52afc357336f20743b" From ded844092e7ae866b03f6cdd0d9d4201b6c85371 Mon Sep 17 00:00:00 2001 From: Moran Peker Date: Wed, 6 Jun 2018 16:36:50 +0300 Subject: [PATCH 42/54] fix and add tests case + fix for padding mode --- tests/suites/test_suite_psa_crypto.data | 61 ++++- tests/suites/test_suite_psa_crypto.function | 287 ++++++++++++++++---- 2 files changed, 294 insertions(+), 54 deletions(-) diff --git a/tests/suites/test_suite_psa_crypto.data b/tests/suites/test_suite_psa_crypto.data index 2126462c05..3ebbd59851 100644 --- a/tests/suites/test_suite_psa_crypto.data +++ b/tests/suites/test_suite_psa_crypto.data @@ -55,28 +55,77 @@ mac_verify:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":PSA_ALG_CMAC:"":" PSA Symmetric encryption: AES-128 depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC -cipher_test_encrypt:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a":"a076ec9dfbe47d52afc357336f20743b" +cipher_test_encrypt:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a":"a076ec9dfbe47d52afc357336f20743b":PSA_SUCCESS + +PSA Symmetric encryption: bad, input buffer too small AES-128 +depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC +cipher_test_encrypt:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee223":"6bc1bee223":PSA_ERROR_INVALID_ARGUMENT PSA Symmetric decryption: AES-128 depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC -cipher_test_decrypt:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"396ee84fb75fdbb5c2b13c7fe5a654aa":"49e4e66c89a86b67758df89db9ad6955" +cipher_test_decrypt:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"396ee84fb75fdbb5c2b13c7fe5a654aa":"49e4e66c89a86b67758df89db9ad6955":PSA_SUCCESS + +PSA Symmetric decryption: AES-128 bad, input buffer too small AES-128 +depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC +cipher_test_decrypt:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee223":"6bc1bee223":PSA_ERROR_BAD_STATE + PSA Symmetric encryption/decryption: AES-128 depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC cipher_test_verify_output:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a" -PSA Symmetric encryption multipart: AES-128, 7+9 +PSA Symmetric encryption/decryption: 16 bytes PKC padding +depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC +cipher_test_verify_output:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a" + +PSA Symmetric encryption/decryption: 15 bytes PKC padding +depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC +cipher_test_verify_output:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e11739317" + +PSA Symmetric encryption/decryption CTR alg: AES-128 +depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC +cipher_test_verify_output:PSA_ALG_CTR | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a" + +PSA Symmetric encryption multipart: AES-128 7+9 depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC cipher_test_encrypt_multipart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a":7:"a076ec9dfbe47d52afc357336f20743b" -PSA Symmetric encryption multipart: AES-128, 3+13 +PSA Symmetric encryption multipart: AES-128 3+13 depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC cipher_test_encrypt_multipart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a":3:"a076ec9dfbe47d52afc357336f20743b" -PSA Symmetric encryption multipart: AES-128, 11+5 +PSA Symmetric encryption multipart: AES-128 4+12 +depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC +cipher_test_encrypt_multipart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a":4:"a076ec9dfbe47d52afc357336f20743b" + +PSA Symmetric encryption multipart: AES-128 11+5 depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC cipher_test_encrypt_multipart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a":11:"a076ec9dfbe47d52afc357336f20743b" +PSA Symmetric decryption multipart: AES-128 7+9 +depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC +cipher_test_decrypt_multipart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"a076ec9dfbe47d52afc357336f20743b":7:"6bc1bee22e409f96e93d7e117393172a" + +PSA Symmetric decryption multipart: AES-128 3+12 +depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC +cipher_test_decrypt_multipart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"a076ec9dfbe47d52afc357336f20743b":3:"6bc1bee22e409f96e93d7e117393172a" + +PSA Symmetric decryption multipart: AES-128 11+5 +depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC +cipher_test_decrypt_multipart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"a076ec9dfbe47d52afc357336f20743b":11:"6bc1bee22e409f96e93d7e117393172a" + +PSA Symmetric encryption + decryption multipart: AES-128 11+5 +depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC +cipher_test_verify_output_multpart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"a076ec9dfbe47d52afc357336f20743b":11 + +PSA Symmetric encryption + decryption multipart: AES-128 PKC padding, 4+12 +depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC +cipher_test_verify_output_multpart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"a076ec9dfbe47d52afc357336f20743b":4 + +PSA Symmetric encryption + decryption multipart: AES-128 16+16 +depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC +cipher_test_verify_output_multpart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"a076ec9dfbe47d52afc357336f20743ba076ec9dfbe47d52afc357336f20743b":16 + PSA signature size: RSA keypair, 1024 bits, PKCS#1 v1.5 raw signature_size:PSA_KEY_TYPE_RSA_KEYPAIR:1024:PSA_ALG_RSA_PKCS1V15_SIGN_RAW:128 @@ -128,3 +177,5 @@ key_lifetime_set_fail:1:PSA_KEY_LIFETIME_WRITE_ONCE:PSA_ERROR_NOT_SUPPORTED PSA Key Lifetime set fail, invalid key lifetime value key_lifetime_set_fail:1:PSA_KEY_LIFETIME_PERSISTENT+1:PSA_ERROR_INVALID_ARGUMENT + + diff --git a/tests/suites/test_suite_psa_crypto.function b/tests/suites/test_suite_psa_crypto.function index 5ac87214f1..8d16c6fbf2 100644 --- a/tests/suites/test_suite_psa_crypto.function +++ b/tests/suites/test_suite_psa_crypto.function @@ -265,11 +265,13 @@ exit: /* END_CASE */ /* BEGIN_CASE */ -void cipher_test_encrypt( int alg_arg, int key_type_arg, - char *key_hex, - char *input_hex, char *output_hex ) +void cipher_test_encrypt( int alg_arg, int key_type_arg, + char *key_hex, + char *input_hex, char *output_hex, + int expected_status ) { int key_slot = 1; + psa_status_t status; psa_key_type_t key_type = key_type_arg; psa_algorithm_t alg = alg_arg; unsigned char *key = NULL; @@ -279,7 +281,7 @@ void cipher_test_encrypt( int alg_arg, int key_type_arg, size_t input_size = 0; unsigned char *output; unsigned char *expected_output; - size_t output_size = 0; + size_t output_size, output_size_1 = 0; size_t output_length = 0; psa_cipher_operation_t operation; @@ -294,7 +296,7 @@ void cipher_test_encrypt( int alg_arg, int key_type_arg, 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, @@ -304,20 +306,22 @@ void cipher_test_encrypt( int alg_arg, int key_type_arg, TEST_ASSERT( psa_encrypt_set_iv( &operation, iv, sizeof( iv ) ) == PSA_SUCCESS ); - - output = mbedtls_calloc(0, output_size); + output_size_1 = input_size + operation.block_size; + output = mbedtls_calloc(1, output_size_1); TEST_ASSERT( psa_cipher_update( &operation, input, input_size, - output, output_size, + output, output_size_1, &output_length) == PSA_SUCCESS ); - TEST_ASSERT( psa_cipher_finish( &operation, output + output_length, - output_size, &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 ); + status = psa_cipher_finish( &operation, output + output_length, + output_size_1, &output_length); + TEST_ASSERT( status == (psa_status_t) expected_status ); + if( expected_status == 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 ); @@ -327,7 +331,7 @@ exit: /* END_CASE */ /* BEGIN_CASE */ -void cipher_test_encrypt_multipart( int alg_arg, int key_type_arg, +void cipher_test_encrypt_multipart( int alg_arg, int key_type_arg, char *key_hex, char *input_hex, int first_part_size, char *output_hex ) @@ -342,7 +346,7 @@ void cipher_test_encrypt_multipart( int alg_arg, int key_type_arg, size_t input_size = 0; unsigned char *output; unsigned char *expected_output; - size_t output_size = 0; + size_t output_size, output_size_1 = 0; size_t output_length = 0; psa_cipher_operation_t operation; @@ -357,7 +361,7 @@ void cipher_test_encrypt_multipart( int alg_arg, int key_type_arg, 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, @@ -366,20 +370,19 @@ void cipher_test_encrypt_multipart( int alg_arg, int key_type_arg, TEST_ASSERT( psa_encrypt_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); + sizeof( iv ) ) == PSA_SUCCESS ); + output_size_1 = input_size + operation.block_size; + output = mbedtls_calloc(1, output_size_1); TEST_ASSERT( (unsigned int)first_part_size < input_size ); TEST_ASSERT( psa_cipher_update( &operation, input, first_part_size, - output, output_size, + output, output_size_1, &output_length) == PSA_SUCCESS ); - TEST_ASSERT( psa_cipher_update( &operation, input + first_part_size, - input_size - first_part_size, - output, output_size, + TEST_ASSERT( psa_cipher_update( &operation, input + first_part_size, input_size - first_part_size, + output, output_size_1, &output_length) == PSA_SUCCESS ); TEST_ASSERT( psa_cipher_finish( &operation, output + output_length, - output_size, &output_length) == PSA_SUCCESS ); + output_size_1, &output_length) == PSA_SUCCESS ); TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); @@ -395,11 +398,13 @@ exit: /* END_CASE */ /* BEGIN_CASE */ -void cipher_test_decrypt( int alg_arg, int key_type_arg, +void cipher_test_decrypt_multipart( int alg_arg, int key_type_arg, char *key_hex, - char *input_hex, char *output_hex ) + char *input_hex, + int first_part_size, 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; @@ -409,7 +414,7 @@ void cipher_test_decrypt( int alg_arg, int key_type_arg, size_t input_size = 0; unsigned char *output; unsigned char *expected_output; - size_t output_size = 0; + size_t output_size, output_size_1 = 0; size_t output_length = 0; psa_cipher_operation_t operation; @@ -424,7 +429,7 @@ void cipher_test_decrypt( int alg_arg, int key_type_arg, 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, @@ -435,14 +440,18 @@ void cipher_test_decrypt( int alg_arg, int key_type_arg, TEST_ASSERT( psa_encrypt_set_iv( &operation, iv, sizeof( iv ) ) == PSA_SUCCESS ); - output = mbedtls_calloc(0, output_size); + output_size_1 = input_size + operation.block_size; + output = mbedtls_calloc(1, output_size_1); - TEST_ASSERT( psa_cipher_update( &operation, input, input_size, - output, output_size, + TEST_ASSERT( (unsigned int)first_part_size < input_size ); + TEST_ASSERT( psa_cipher_update( &operation, input, first_part_size, + output, output_size_1, + &output_length) == PSA_SUCCESS ); + TEST_ASSERT( psa_cipher_update( &operation, input + first_part_size, input_size - first_part_size, + output, output_size_1, &output_length) == PSA_SUCCESS ); TEST_ASSERT( psa_cipher_finish( &operation, output + output_length, - output_size, &output_length) == PSA_SUCCESS ); - + output_size_1, &output_length) == PSA_SUCCESS ); TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); TEST_ASSERT( input_size == output_size ); @@ -458,7 +467,78 @@ exit: /* BEGIN_CASE */ -void cipher_test_verify_output( int alg_arg, int key_type_arg, +void cipher_test_decrypt( int alg_arg, int key_type_arg, + char *key_hex, + char *input_hex, char *output_hex, + int expected_status ) +{ + int key_slot = 1; + psa_status_t status; + 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, output_size_1 = 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_size_1 = input_size + operation.block_size; + output = mbedtls_calloc(1, output_size); + + TEST_ASSERT( psa_cipher_update( &operation, input, input_size, + output, output_size_1, + &output_length) == PSA_SUCCESS ); + status = psa_cipher_finish( &operation, output + output_length, + output_size_1, &output_length); + TEST_ASSERT( status == (psa_status_t) expected_status ); + + if( expected_status == 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, char *input_hex ) { @@ -487,7 +567,7 @@ void cipher_test_verify_output( int alg_arg, int key_type_arg, input = unhexify_alloc( input_hex, &input_size ); TEST_ASSERT( input != NULL ); - + TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); TEST_ASSERT( psa_import_key( key_slot, key_type, @@ -498,45 +578,154 @@ void cipher_test_verify_output( int alg_arg, int key_type_arg, TEST_ASSERT( psa_encrypt_generate_iv( &operation1, iv, iv_size, &iv_length) == PSA_SUCCESS ); - output1_size = input_size; - output1 = mbedtls_calloc(0, output1_size); + output1_size = input_size + operation1.block_size; + output1 = mbedtls_calloc(1, output1_size); + TEST_ASSERT( output1 != NULL); + TEST_ASSERT( psa_cipher_update( &operation1, input, input_size, - output1, output1_size, + output1, output1_size, &output1_length) == PSA_SUCCESS ); TEST_ASSERT( psa_cipher_finish( &operation1, output1 + output1_length, - output1_size, &tmp_output_length) == PSA_SUCCESS ); + output1_size, &tmp_output_length) == PSA_SUCCESS ); + + output1_length += tmp_output_length; + + TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); + + output2_size = output1_length; + output2 = mbedtls_calloc(1, output2_size); + + 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, + output2_size, &tmp_output_length) == PSA_SUCCESS ); + + output2_length += tmp_output_length; + + TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); + + TEST_ASSERT( input_size == output2_length ); + TEST_ASSERT( memcmp( input, output2, input_size ) == 0 ); + +exit: + mbedtls_free( key ); + mbedtls_free( input ); + mbedtls_free( output1 ); + mbedtls_free( output2 ); + + psa_destroy_key( key_slot ); + mbedtls_psa_crypto_free( ); +} +/* END_CASE */ + +/* BEGIN_CASE */ +void cipher_test_verify_output_multpart( int alg_arg, + int key_type_arg, + char *key_hex, + char *input_hex, + int first_part_size ) +{ + 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}; + size_t iv_size = 16; + size_t iv_length = 0; + unsigned char *input = NULL; + size_t input_size = 0; + unsigned char *output1; + size_t output1_size = 0; + size_t output1_length = 0; + unsigned char *output2; + size_t output2_size = 0; + size_t output2_length = 0; + size_t tmp_output_length , temp = 0; + psa_cipher_operation_t operation1; + psa_cipher_operation_t operation2; + + key = unhexify_alloc( key_hex, &key_size ); + TEST_ASSERT( key != NULL ); + + input = unhexify_alloc( input_hex, &input_size ); + TEST_ASSERT( input != NULL ); + + TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); + + TEST_ASSERT( psa_import_key( key_slot, key_type, + key, key_size ) == 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 ); + output1_size = input_size + operation1.block_size; + output1 = mbedtls_calloc(1, output1_size); + + TEST_ASSERT( (unsigned int)first_part_size < input_size ); + + TEST_ASSERT( psa_cipher_update( &operation1, input, first_part_size, + output1, output1_size, + &output1_length) == PSA_SUCCESS ); + temp = output1_length ; + + TEST_ASSERT( psa_cipher_update( &operation1, input + first_part_size, input_size - first_part_size, + output1, output1_size, + &output1_length) == PSA_SUCCESS ); + output1_length += temp; + + 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 ); output2_size = output1_length; - output2 = mbedtls_calloc(0, output2_size); + output2 = mbedtls_calloc(1, output2_size); 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 ); + iv_length) == PSA_SUCCESS ); + + TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size, + output2, output2_size, + &output2_length) == PSA_SUCCESS ); + + temp = output2_length ; + + TEST_ASSERT( psa_cipher_update( &operation2, output1 + first_part_size, + output1_length - first_part_size, + output2, output2_size, + &output2_length) == PSA_SUCCESS ); + + output2_length += temp; tmp_output_length = 0; TEST_ASSERT( psa_cipher_finish( &operation2, output2 + output2_length, - output2_size, &tmp_output_length) == PSA_SUCCESS ); + output2_size - output2_length, &tmp_output_length) == PSA_SUCCESS ); output2_length += tmp_output_length; - + TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); - TEST_ASSERT( input_size == output1_length ); - TEST_ASSERT( output1_length == output2_length ); + TEST_ASSERT( input_size == output2_length ); TEST_ASSERT( memcmp( input, output2, input_size ) == 0 ); exit: mbedtls_free( key ); mbedtls_free( input ); + mbedtls_free( output1 ); + mbedtls_free( output2 ); psa_destroy_key( key_slot ); mbedtls_psa_crypto_free( ); } /* END_CASE */ + /* BEGIN_CASE */ void signature_size( int type_arg, int bits, int alg_arg, int expected_size_arg ) { From 7f87850fc41bb5abe4496838827f6e1b2c42ba7e Mon Sep 17 00:00:00 2001 From: Moran Peker Date: Wed, 6 Jun 2018 17:09:40 +0300 Subject: [PATCH 43/54] fix and add tests case + fix for padding mode --- tests/suites/test_suite_psa_crypto.data | 8 +------- tests/suites/test_suite_psa_crypto.function | 7 +------ 2 files changed, 2 insertions(+), 13 deletions(-) diff --git a/tests/suites/test_suite_psa_crypto.data b/tests/suites/test_suite_psa_crypto.data index 3ebbd59851..2958b30ac3 100644 --- a/tests/suites/test_suite_psa_crypto.data +++ b/tests/suites/test_suite_psa_crypto.data @@ -69,7 +69,6 @@ PSA Symmetric decryption: AES-128 bad, input buffer too small AES-128 depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC cipher_test_decrypt:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee223":"6bc1bee223":PSA_ERROR_BAD_STATE - PSA Symmetric encryption/decryption: AES-128 depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC cipher_test_verify_output:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a" @@ -106,7 +105,7 @@ PSA Symmetric decryption multipart: AES-128 7+9 depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC cipher_test_decrypt_multipart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"a076ec9dfbe47d52afc357336f20743b":7:"6bc1bee22e409f96e93d7e117393172a" -PSA Symmetric decryption multipart: AES-128 3+12 +PSA Symmetric decryption multipart: AES-128 3+13 depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC cipher_test_decrypt_multipart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"a076ec9dfbe47d52afc357336f20743b":3:"6bc1bee22e409f96e93d7e117393172a" @@ -122,10 +121,6 @@ PSA Symmetric encryption + decryption multipart: AES-128 PKC padding, 4+12 depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC cipher_test_verify_output_multpart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"a076ec9dfbe47d52afc357336f20743b":4 -PSA Symmetric encryption + decryption multipart: AES-128 16+16 -depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC -cipher_test_verify_output_multpart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"a076ec9dfbe47d52afc357336f20743ba076ec9dfbe47d52afc357336f20743b":16 - PSA signature size: RSA keypair, 1024 bits, PKCS#1 v1.5 raw signature_size:PSA_KEY_TYPE_RSA_KEYPAIR:1024:PSA_ALG_RSA_PKCS1V15_SIGN_RAW:128 @@ -178,4 +173,3 @@ key_lifetime_set_fail:1:PSA_KEY_LIFETIME_WRITE_ONCE:PSA_ERROR_NOT_SUPPORTED PSA Key Lifetime set fail, invalid key lifetime value key_lifetime_set_fail:1:PSA_KEY_LIFETIME_PERSISTENT+1:PSA_ERROR_INVALID_ARGUMENT - diff --git a/tests/suites/test_suite_psa_crypto.function b/tests/suites/test_suite_psa_crypto.function index 8d16c6fbf2..4f1d3d60e4 100644 --- a/tests/suites/test_suite_psa_crypto.function +++ b/tests/suites/test_suite_psa_crypto.function @@ -264,6 +264,7 @@ exit: } /* END_CASE */ + /* BEGIN_CASE */ void cipher_test_encrypt( int alg_arg, int key_type_arg, char *key_hex, @@ -613,9 +614,6 @@ void cipher_test_verify_output( int alg_arg, int key_type_arg, exit: mbedtls_free( key ); mbedtls_free( input ); - mbedtls_free( output1 ); - mbedtls_free( output2 ); - psa_destroy_key( key_slot ); mbedtls_psa_crypto_free( ); } @@ -718,14 +716,11 @@ void cipher_test_verify_output_multpart( int alg_arg, exit: mbedtls_free( key ); mbedtls_free( input ); - mbedtls_free( output1 ); - mbedtls_free( output2 ); psa_destroy_key( key_slot ); mbedtls_psa_crypto_free( ); } /* END_CASE */ - /* BEGIN_CASE */ void signature_size( int type_arg, int bits, int alg_arg, int expected_size_arg ) { From 4ca9c3f9a163185f8e67a46ecb37f37000a4e3c9 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Wed, 6 Jun 2018 18:44:09 +0200 Subject: [PATCH 44/54] Fix whitespace issues Only whitespace changes. * Remove tabs. * Remove trailing whitespace. * Correct some misindented lines. * Normalize whitespace around some punctuation. * Split some lines to avoid going over 80 columns. --- tests/suites/test_suite_psa_crypto.data | 1 - tests/suites/test_suite_psa_crypto.function | 219 +++++++++++--------- 2 files changed, 117 insertions(+), 103 deletions(-) diff --git a/tests/suites/test_suite_psa_crypto.data b/tests/suites/test_suite_psa_crypto.data index 2958b30ac3..16bce06eb5 100644 --- a/tests/suites/test_suite_psa_crypto.data +++ b/tests/suites/test_suite_psa_crypto.data @@ -172,4 +172,3 @@ key_lifetime_set_fail:1:PSA_KEY_LIFETIME_WRITE_ONCE:PSA_ERROR_NOT_SUPPORTED PSA Key Lifetime set fail, invalid key lifetime value key_lifetime_set_fail:1:PSA_KEY_LIFETIME_PERSISTENT+1:PSA_ERROR_INVALID_ARGUMENT - diff --git a/tests/suites/test_suite_psa_crypto.function b/tests/suites/test_suite_psa_crypto.function index 4f1d3d60e4..16b65ac378 100644 --- a/tests/suites/test_suite_psa_crypto.function +++ b/tests/suites/test_suite_psa_crypto.function @@ -266,7 +266,7 @@ exit: /* BEGIN_CASE */ -void cipher_test_encrypt( int alg_arg, int key_type_arg, +void cipher_test_encrypt( int alg_arg, int key_type_arg, char *key_hex, char *input_hex, char *output_hex, int expected_status ) @@ -297,7 +297,7 @@ void cipher_test_encrypt( int alg_arg, int key_type_arg, 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, @@ -305,16 +305,16 @@ void cipher_test_encrypt( int alg_arg, int key_type_arg, TEST_ASSERT( psa_encrypt_setup( &operation, key_slot, alg ) == PSA_SUCCESS ); - TEST_ASSERT( psa_encrypt_set_iv( &operation, iv, - sizeof( iv ) ) == PSA_SUCCESS ); + TEST_ASSERT( psa_encrypt_set_iv( &operation, + iv, sizeof( iv ) ) == PSA_SUCCESS ); output_size_1 = input_size + operation.block_size; - output = mbedtls_calloc(1, output_size_1); + output = mbedtls_calloc( 1, output_size_1 ); TEST_ASSERT( psa_cipher_update( &operation, input, input_size, - output, output_size_1, - &output_length) == PSA_SUCCESS ); + output, output_size_1, + &output_length ) == PSA_SUCCESS ); status = psa_cipher_finish( &operation, output + output_length, - output_size_1, &output_length); + output_size_1, &output_length ); TEST_ASSERT( status == (psa_status_t) expected_status ); if( expected_status == PSA_SUCCESS ) { @@ -332,10 +332,10 @@ exit: /* END_CASE */ /* BEGIN_CASE */ -void cipher_test_encrypt_multipart( int alg_arg, int key_type_arg, - char *key_hex, - char *input_hex, - int first_part_size, char *output_hex ) +void cipher_test_encrypt_multipart( int alg_arg, int key_type_arg, + char *key_hex, + char *input_hex, + int first_part_size, char *output_hex ) { int key_slot = 1; psa_key_type_t key_type = key_type_arg; @@ -362,7 +362,7 @@ void cipher_test_encrypt_multipart( int alg_arg, int key_type_arg, 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, @@ -370,20 +370,22 @@ void cipher_test_encrypt_multipart( int alg_arg, int key_type_arg, TEST_ASSERT( psa_encrypt_setup( &operation, key_slot, alg ) == PSA_SUCCESS ); - TEST_ASSERT( psa_encrypt_set_iv( &operation, iv, - sizeof( iv ) ) == PSA_SUCCESS ); + TEST_ASSERT( psa_encrypt_set_iv( &operation, + iv, sizeof( iv ) ) == PSA_SUCCESS ); output_size_1 = input_size + operation.block_size; - output = mbedtls_calloc(1, output_size_1); + output = mbedtls_calloc( 1, output_size_1 ); - TEST_ASSERT( (unsigned int)first_part_size < input_size ); + TEST_ASSERT( (unsigned int) first_part_size < input_size ); TEST_ASSERT( psa_cipher_update( &operation, input, first_part_size, - output, output_size_1, - &output_length) == PSA_SUCCESS ); - TEST_ASSERT( psa_cipher_update( &operation, input + first_part_size, input_size - first_part_size, - output, output_size_1, - &output_length) == PSA_SUCCESS ); + output, output_size_1, + &output_length ) == PSA_SUCCESS ); + TEST_ASSERT( psa_cipher_update( &operation, + input + first_part_size, + input_size - first_part_size, + output, output_size_1, + &output_length ) == PSA_SUCCESS ); TEST_ASSERT( psa_cipher_finish( &operation, output + output_length, - output_size_1, &output_length) == PSA_SUCCESS ); + output_size_1, &output_length ) == PSA_SUCCESS ); TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); @@ -399,13 +401,13 @@ exit: /* END_CASE */ /* BEGIN_CASE */ -void cipher_test_decrypt_multipart( int alg_arg, int key_type_arg, - char *key_hex, - char *input_hex, - int first_part_size, char *output_hex) +void cipher_test_decrypt_multipart( int alg_arg, int key_type_arg, + char *key_hex, + char *input_hex, + int first_part_size, 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; @@ -430,7 +432,7 @@ void cipher_test_decrypt_multipart( int alg_arg, int key_type_arg, 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, @@ -438,21 +440,23 @@ void cipher_test_decrypt_multipart( int alg_arg, int key_type_arg, TEST_ASSERT( psa_decrypt_setup( &operation, key_slot, alg ) == PSA_SUCCESS ); - TEST_ASSERT( psa_encrypt_set_iv( &operation, iv, - sizeof( iv ) ) == PSA_SUCCESS ); + TEST_ASSERT( psa_encrypt_set_iv( &operation, + iv, sizeof( iv ) ) == PSA_SUCCESS ); output_size_1 = input_size + operation.block_size; - output = mbedtls_calloc(1, output_size_1); + output = mbedtls_calloc( 1, output_size_1 ); - TEST_ASSERT( (unsigned int)first_part_size < input_size ); + TEST_ASSERT( (unsigned int) first_part_size < input_size ); TEST_ASSERT( psa_cipher_update( &operation, input, first_part_size, - output, output_size_1, - &output_length) == PSA_SUCCESS ); - TEST_ASSERT( psa_cipher_update( &operation, input + first_part_size, input_size - first_part_size, - output, output_size_1, - &output_length) == PSA_SUCCESS ); + output, output_size_1, + &output_length ) == PSA_SUCCESS ); + TEST_ASSERT( psa_cipher_update( &operation, + input + first_part_size, + input_size - first_part_size, + output, output_size_1, + &output_length ) == PSA_SUCCESS ); TEST_ASSERT( psa_cipher_finish( &operation, output + output_length, - output_size_1, &output_length) == PSA_SUCCESS ); + output_size_1, &output_length ) == PSA_SUCCESS ); TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); TEST_ASSERT( input_size == output_size ); @@ -468,10 +472,10 @@ exit: /* BEGIN_CASE */ -void cipher_test_decrypt( int alg_arg, int key_type_arg, - char *key_hex, - char *input_hex, char *output_hex, - int expected_status ) +void cipher_test_decrypt( int alg_arg, int key_type_arg, + char *key_hex, + char *input_hex, char *output_hex, + int expected_status ) { int key_slot = 1; psa_status_t status; @@ -499,7 +503,7 @@ void cipher_test_decrypt( int alg_arg, int key_type_arg, 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, @@ -507,17 +511,17 @@ void cipher_test_decrypt( int alg_arg, int key_type_arg, TEST_ASSERT( psa_decrypt_setup( &operation, key_slot, alg ) == PSA_SUCCESS ); - TEST_ASSERT( psa_encrypt_set_iv( &operation, iv, - sizeof( iv ) ) == PSA_SUCCESS ); + TEST_ASSERT( psa_encrypt_set_iv( &operation, + iv, sizeof( iv ) ) == PSA_SUCCESS ); output_size_1 = input_size + operation.block_size; - output = mbedtls_calloc(1, output_size); + output = mbedtls_calloc( 1, output_size ); TEST_ASSERT( psa_cipher_update( &operation, input, input_size, - output, output_size_1, - &output_length) == PSA_SUCCESS ); + output, output_size_1, + &output_length ) == PSA_SUCCESS ); status = psa_cipher_finish( &operation, output + output_length, - output_size_1, &output_length); + output_size_1, &output_length ); TEST_ASSERT( status == (psa_status_t) expected_status ); if( expected_status == PSA_SUCCESS ) @@ -539,9 +543,9 @@ exit: /* BEGIN_CASE */ -void cipher_test_verify_output( int alg_arg, int key_type_arg, - char *key_hex, - char *input_hex ) +void cipher_test_verify_output( int alg_arg, int key_type_arg, + char *key_hex, + char *input_hex ) { int key_slot = 1; psa_key_type_t key_type = key_type_arg; @@ -568,7 +572,7 @@ void cipher_test_verify_output( int alg_arg, int key_type_arg, input = unhexify_alloc( input_hex, &input_size ); TEST_ASSERT( input != NULL ); - + TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); TEST_ASSERT( psa_import_key( key_slot, key_type, @@ -577,35 +581,40 @@ void cipher_test_verify_output( int alg_arg, int key_type_arg, 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_generate_iv( &operation1, + iv, iv_size, + &iv_length ) == PSA_SUCCESS ); output1_size = input_size + operation1.block_size; - output1 = mbedtls_calloc(1, output1_size); - TEST_ASSERT( output1 != NULL); + output1 = mbedtls_calloc( 1, output1_size ); + TEST_ASSERT( output1 != NULL ); 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, - output1_size, &tmp_output_length) == PSA_SUCCESS ); - + output1, output1_size, + &output1_length ) == PSA_SUCCESS ); + TEST_ASSERT( psa_cipher_finish( &operation1, + output1 + output1_length, output1_size, + &tmp_output_length ) == PSA_SUCCESS ); + output1_length += tmp_output_length; TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); output2_size = output1_length; - output2 = mbedtls_calloc(1, output2_size); + output2 = mbedtls_calloc( 1, output2_size ); - 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 ); + 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, - output2_size, &tmp_output_length) == PSA_SUCCESS ); + TEST_ASSERT( psa_cipher_finish( &operation2, + output2 + output2_length, + output2_size, + &tmp_output_length ) == PSA_SUCCESS ); output2_length += tmp_output_length; - + TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); TEST_ASSERT( input_size == output2_length ); @@ -621,10 +630,10 @@ exit: /* BEGIN_CASE */ void cipher_test_verify_output_multpart( int alg_arg, - int key_type_arg, - char *key_hex, - char *input_hex, - int first_part_size ) + int key_type_arg, + char *key_hex, + char *input_hex, + int first_part_size ) { int key_slot = 1; psa_key_type_t key_type = key_type_arg; @@ -642,7 +651,7 @@ void cipher_test_verify_output_multpart( int alg_arg, unsigned char *output2; size_t output2_size = 0; size_t output2_length = 0; - size_t tmp_output_length , temp = 0; + size_t tmp_output_length, temp = 0; psa_cipher_operation_t operation1; psa_cipher_operation_t operation2; @@ -651,7 +660,7 @@ void cipher_test_verify_output_multpart( int alg_arg, input = unhexify_alloc( input_hex, &input_size ); TEST_ASSERT( input != NULL ); - + TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); TEST_ASSERT( psa_import_key( key_slot, key_type, @@ -660,54 +669,60 @@ void cipher_test_verify_output_multpart( int alg_arg, 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_generate_iv( &operation1, + iv, iv_size, + &iv_length ) == PSA_SUCCESS ); output1_size = input_size + operation1.block_size; - output1 = mbedtls_calloc(1, output1_size); + output1 = mbedtls_calloc( 1, output1_size ); + + TEST_ASSERT( (unsigned int) first_part_size < input_size ); - TEST_ASSERT( (unsigned int)first_part_size < input_size ); - TEST_ASSERT( psa_cipher_update( &operation1, input, first_part_size, - output1, output1_size, - &output1_length) == PSA_SUCCESS ); - temp = output1_length ; + output1, output1_size, + &output1_length ) == PSA_SUCCESS ); + temp = output1_length; - TEST_ASSERT( psa_cipher_update( &operation1, input + first_part_size, input_size - first_part_size, - output1, output1_size, - &output1_length) == PSA_SUCCESS ); + TEST_ASSERT( psa_cipher_update( &operation1, + input + first_part_size, + input_size - first_part_size, + output1, output1_size, + &output1_length ) == PSA_SUCCESS ); output1_length += temp; TEST_ASSERT( psa_cipher_finish( &operation1, output1 + output1_length, - output1_size - output1_length, &tmp_output_length) == PSA_SUCCESS ); + output1_size - output1_length, + &tmp_output_length ) == PSA_SUCCESS ); output1_length += tmp_output_length; TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); output2_size = output1_length; - output2 = mbedtls_calloc(1, output2_size); + output2 = mbedtls_calloc( 1, output2_size ); - TEST_ASSERT( psa_encrypt_set_iv( &operation2, iv, - iv_length) == PSA_SUCCESS ); + TEST_ASSERT( psa_encrypt_set_iv( &operation2, + iv, iv_length ) == PSA_SUCCESS ); TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size, - output2, output2_size, - &output2_length) == PSA_SUCCESS ); + output2, output2_size, + &output2_length ) == PSA_SUCCESS ); - temp = output2_length ; + temp = output2_length; - TEST_ASSERT( psa_cipher_update( &operation2, output1 + first_part_size, - output1_length - first_part_size, - output2, output2_size, - &output2_length) == PSA_SUCCESS ); + TEST_ASSERT( psa_cipher_update( &operation2, output1 + first_part_size, + output1_length - first_part_size, + output2, output2_size, + &output2_length ) == PSA_SUCCESS ); output2_length += temp; tmp_output_length = 0; - TEST_ASSERT( psa_cipher_finish( &operation2, output2 + output2_length, - output2_size - output2_length, &tmp_output_length) == PSA_SUCCESS ); + TEST_ASSERT( psa_cipher_finish( &operation2, + output2 + output2_length, + output2_size - output2_length, + &tmp_output_length ) == PSA_SUCCESS ); output2_length += tmp_output_length; - + TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); TEST_ASSERT( input_size == output2_length ); From 9cf78d301d2d839083723b31ef9d893f921ea8ee Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Wed, 6 Jun 2018 18:57:11 +0200 Subject: [PATCH 45/54] Fix some test case dependencies on cipher modes --- tests/suites/test_suite_psa_crypto.data | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/suites/test_suite_psa_crypto.data b/tests/suites/test_suite_psa_crypto.data index 16bce06eb5..88b26d283f 100644 --- a/tests/suites/test_suite_psa_crypto.data +++ b/tests/suites/test_suite_psa_crypto.data @@ -74,15 +74,15 @@ depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC cipher_test_verify_output:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a" PSA Symmetric encryption/decryption: 16 bytes PKC padding -depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC +depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_CIPHER_PADDING_PKCS7 cipher_test_verify_output:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a" PSA Symmetric encryption/decryption: 15 bytes PKC padding -depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC +depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_CIPHER_PADDING_PKCS7 cipher_test_verify_output:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e11739317" PSA Symmetric encryption/decryption CTR alg: AES-128 -depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC +depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CTR cipher_test_verify_output:PSA_ALG_CTR | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a" PSA Symmetric encryption multipart: AES-128 7+9 @@ -118,7 +118,7 @@ depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC cipher_test_verify_output_multpart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"a076ec9dfbe47d52afc357336f20743b":11 PSA Symmetric encryption + decryption multipart: AES-128 PKC padding, 4+12 -depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC +depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_CIPHER_PADDING_PKCS7 cipher_test_verify_output_multpart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"a076ec9dfbe47d52afc357336f20743b":4 PSA signature size: RSA keypair, 1024 bits, PKCS#1 v1.5 raw From 17ddaa27b0ccb95cf7d76af0be38e2882f50e2c8 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Wed, 6 Jun 2018 18:57:49 +0200 Subject: [PATCH 46/54] Correct and improve cipher test case descriptions --- tests/suites/test_suite_psa_crypto.data | 34 ++++++++++++------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/tests/suites/test_suite_psa_crypto.data b/tests/suites/test_suite_psa_crypto.data index 88b26d283f..783148cf19 100644 --- a/tests/suites/test_suite_psa_crypto.data +++ b/tests/suites/test_suite_psa_crypto.data @@ -53,71 +53,71 @@ PSA MAC verify: CMAC-AES-128 depends_on:MBEDTLS_CMAC_C:MBEDTLS_AES_C mac_verify:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":PSA_ALG_CMAC:"":"6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411":"dfa66747de9ae63030ca32611497c827" -PSA Symmetric encryption: AES-128 +PSA Symmetric encryption: AES-CBC-nopad, 16 bytes, good depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC cipher_test_encrypt:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a":"a076ec9dfbe47d52afc357336f20743b":PSA_SUCCESS -PSA Symmetric encryption: bad, input buffer too small AES-128 +PSA Symmetric encryption: AES-CBC-nopad, input too short depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC cipher_test_encrypt:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee223":"6bc1bee223":PSA_ERROR_INVALID_ARGUMENT -PSA Symmetric decryption: AES-128 +PSA Symmetric decryption: AES-CBC-nopad, 16 bytes, good depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC cipher_test_decrypt:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"396ee84fb75fdbb5c2b13c7fe5a654aa":"49e4e66c89a86b67758df89db9ad6955":PSA_SUCCESS -PSA Symmetric decryption: AES-128 bad, input buffer too small AES-128 +PSA Symmetric decryption: AES-CBC-nopad, input too short depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC cipher_test_decrypt:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee223":"6bc1bee223":PSA_ERROR_BAD_STATE -PSA Symmetric encryption/decryption: AES-128 +PSA Symmetric encryption/decryption: AES-CBC-nopad, 16 bytes, good depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC cipher_test_verify_output:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a" -PSA Symmetric encryption/decryption: 16 bytes PKC padding +PSA Symmetric encryption/decryption: AES-CBC-PKCS#7, 16 bytes depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_CIPHER_PADDING_PKCS7 cipher_test_verify_output:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a" -PSA Symmetric encryption/decryption: 15 bytes PKC padding +PSA Symmetric encryption/decryption: AES-CBC-PKCS#7, 15 bytes depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_CIPHER_PADDING_PKCS7 cipher_test_verify_output:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e11739317" -PSA Symmetric encryption/decryption CTR alg: AES-128 +PSA Symmetric encryption/decryption: AES-CTR depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CTR cipher_test_verify_output:PSA_ALG_CTR | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a" -PSA Symmetric encryption multipart: AES-128 7+9 +PSA Symmetric encryption multipart: AES-CBC-nopad, 7+9 bytes depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC cipher_test_encrypt_multipart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a":7:"a076ec9dfbe47d52afc357336f20743b" -PSA Symmetric encryption multipart: AES-128 3+13 +PSA Symmetric encryption multipart: AES-CBC-nopad, 3+13 bytes depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC cipher_test_encrypt_multipart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a":3:"a076ec9dfbe47d52afc357336f20743b" -PSA Symmetric encryption multipart: AES-128 4+12 +PSA Symmetric encryption multipart: AES-CBC-nopad, 4+12 bytes depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC cipher_test_encrypt_multipart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a":4:"a076ec9dfbe47d52afc357336f20743b" -PSA Symmetric encryption multipart: AES-128 11+5 +PSA Symmetric encryption multipart: AES-CBC-nopad, 11+5 bytes depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC cipher_test_encrypt_multipart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a":11:"a076ec9dfbe47d52afc357336f20743b" -PSA Symmetric decryption multipart: AES-128 7+9 +PSA Symmetric decryption multipart: AES-CBC-nopad, 7+9 bytes depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC cipher_test_decrypt_multipart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"a076ec9dfbe47d52afc357336f20743b":7:"6bc1bee22e409f96e93d7e117393172a" -PSA Symmetric decryption multipart: AES-128 3+13 +PSA Symmetric decryption multipart: AES-CBC-nopad, 3+13 bytes depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC cipher_test_decrypt_multipart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"a076ec9dfbe47d52afc357336f20743b":3:"6bc1bee22e409f96e93d7e117393172a" -PSA Symmetric decryption multipart: AES-128 11+5 +PSA Symmetric decryption multipart: AES-CBC-nopad, 11+5 bytes depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC cipher_test_decrypt_multipart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"a076ec9dfbe47d52afc357336f20743b":11:"6bc1bee22e409f96e93d7e117393172a" -PSA Symmetric encryption + decryption multipart: AES-128 11+5 +PSA Symmetric encryption + decryption multipart: AES-CBC-nopad, 11+5 bytes depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC cipher_test_verify_output_multpart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"a076ec9dfbe47d52afc357336f20743b":11 -PSA Symmetric encryption + decryption multipart: AES-128 PKC padding, 4+12 +PSA Symmetric encryption + decryption multipart: AES-CBC-PKCS#7 padding, 4+12 bytes depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_CIPHER_PADDING_PKCS7 cipher_test_verify_output_multpart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"a076ec9dfbe47d52afc357336f20743b":4 From 9e3aa62c137c751eb31dc9dff2d331b3d3bc6bef Mon Sep 17 00:00:00 2001 From: Moran Peker Date: Thu, 7 Jun 2018 12:08:47 +0300 Subject: [PATCH 47/54] change variable naming --- tests/suites/test_suite_psa_crypto.function | 42 ++++++++++----------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/tests/suites/test_suite_psa_crypto.function b/tests/suites/test_suite_psa_crypto.function index 16b65ac378..9061003289 100644 --- a/tests/suites/test_suite_psa_crypto.function +++ b/tests/suites/test_suite_psa_crypto.function @@ -282,7 +282,7 @@ void cipher_test_encrypt( int alg_arg, int key_type_arg, size_t input_size = 0; unsigned char *output; unsigned char *expected_output; - size_t output_size, output_size_1 = 0; + size_t output_size, max_output_size = 0; size_t output_length = 0; psa_cipher_operation_t operation; @@ -307,14 +307,14 @@ void cipher_test_encrypt( int alg_arg, int key_type_arg, TEST_ASSERT( psa_encrypt_set_iv( &operation, iv, sizeof( iv ) ) == PSA_SUCCESS ); - output_size_1 = input_size + operation.block_size; - output = mbedtls_calloc( 1, output_size_1 ); + max_output_size = input_size + operation.block_size; + output = mbedtls_calloc( 1, max_output_size ); TEST_ASSERT( psa_cipher_update( &operation, input, input_size, - output, output_size_1, + output, max_output_size, &output_length ) == PSA_SUCCESS ); status = psa_cipher_finish( &operation, output + output_length, - output_size_1, &output_length ); + max_output_size, &output_length ); TEST_ASSERT( status == (psa_status_t) expected_status ); if( expected_status == PSA_SUCCESS ) { @@ -347,7 +347,7 @@ void cipher_test_encrypt_multipart( int alg_arg, int key_type_arg, size_t input_size = 0; unsigned char *output; unsigned char *expected_output; - size_t output_size, output_size_1 = 0; + size_t output_size, max_output_size = 0; size_t output_length = 0; psa_cipher_operation_t operation; @@ -372,20 +372,20 @@ void cipher_test_encrypt_multipart( int alg_arg, int key_type_arg, TEST_ASSERT( psa_encrypt_set_iv( &operation, iv, sizeof( iv ) ) == PSA_SUCCESS ); - output_size_1 = input_size + operation.block_size; - output = mbedtls_calloc( 1, output_size_1 ); + max_output_size = input_size + operation.block_size; + output = mbedtls_calloc( 1, max_output_size ); TEST_ASSERT( (unsigned int) first_part_size < input_size ); TEST_ASSERT( psa_cipher_update( &operation, input, first_part_size, - output, output_size_1, + output, max_output_size, &output_length ) == PSA_SUCCESS ); TEST_ASSERT( psa_cipher_update( &operation, input + first_part_size, input_size - first_part_size, - output, output_size_1, + output, max_output_size, &output_length ) == PSA_SUCCESS ); TEST_ASSERT( psa_cipher_finish( &operation, output + output_length, - output_size_1, &output_length ) == PSA_SUCCESS ); + max_output_size, &output_length ) == PSA_SUCCESS ); TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); @@ -417,7 +417,7 @@ void cipher_test_decrypt_multipart( int alg_arg, int key_type_arg, size_t input_size = 0; unsigned char *output; unsigned char *expected_output; - size_t output_size, output_size_1 = 0; + size_t output_size, max_output_size = 0; size_t output_length = 0; psa_cipher_operation_t operation; @@ -443,20 +443,20 @@ void cipher_test_decrypt_multipart( int alg_arg, int key_type_arg, TEST_ASSERT( psa_encrypt_set_iv( &operation, iv, sizeof( iv ) ) == PSA_SUCCESS ); - output_size_1 = input_size + operation.block_size; - output = mbedtls_calloc( 1, output_size_1 ); + output_simax_output_sizeze_1 = input_size + operation.block_size; + output = mbedtls_calloc( 1, max_output_size ); TEST_ASSERT( (unsigned int) first_part_size < input_size ); TEST_ASSERT( psa_cipher_update( &operation, input, first_part_size, - output, output_size_1, + output, max_output_size, &output_length ) == PSA_SUCCESS ); TEST_ASSERT( psa_cipher_update( &operation, input + first_part_size, input_size - first_part_size, - output, output_size_1, + output, max_output_size, &output_length ) == PSA_SUCCESS ); TEST_ASSERT( psa_cipher_finish( &operation, output + output_length, - output_size_1, &output_length ) == PSA_SUCCESS ); + max_output_size, &output_length ) == PSA_SUCCESS ); TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); TEST_ASSERT( input_size == output_size ); @@ -488,7 +488,7 @@ void cipher_test_decrypt( int alg_arg, int key_type_arg, size_t input_size = 0; unsigned char *output; unsigned char *expected_output; - size_t output_size, output_size_1 = 0; + size_t output_size, max_output_size = 0; size_t output_length = 0; psa_cipher_operation_t operation; @@ -514,14 +514,14 @@ void cipher_test_decrypt( int alg_arg, int key_type_arg, TEST_ASSERT( psa_encrypt_set_iv( &operation, iv, sizeof( iv ) ) == PSA_SUCCESS ); - output_size_1 = input_size + operation.block_size; + max_output_size = input_size + operation.block_size; output = mbedtls_calloc( 1, output_size ); TEST_ASSERT( psa_cipher_update( &operation, input, input_size, - output, output_size_1, + output, max_output_size, &output_length ) == PSA_SUCCESS ); status = psa_cipher_finish( &operation, output + output_length, - output_size_1, &output_length ); + max_output_size, &output_length ); TEST_ASSERT( status == (psa_status_t) expected_status ); if( expected_status == PSA_SUCCESS ) From a9c3a658becf3486c77f812f575c1a979f654e5a Mon Sep 17 00:00:00 2001 From: Moran Peker Date: Thu, 7 Jun 2018 18:08:58 +0300 Subject: [PATCH 48/54] tests fix + max_output_size --- tests/suites/test_suite_psa_crypto.function | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/tests/suites/test_suite_psa_crypto.function b/tests/suites/test_suite_psa_crypto.function index 9061003289..9f9dd6808f 100644 --- a/tests/suites/test_suite_psa_crypto.function +++ b/tests/suites/test_suite_psa_crypto.function @@ -319,8 +319,6 @@ void cipher_test_encrypt( int alg_arg, int key_type_arg, if( expected_status == 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: @@ -443,7 +441,7 @@ void cipher_test_decrypt_multipart( int alg_arg, int key_type_arg, TEST_ASSERT( psa_encrypt_set_iv( &operation, iv, sizeof( iv ) ) == PSA_SUCCESS ); - output_simax_output_sizeze_1 = input_size + operation.block_size; + max_output_size = input_size + operation.block_size; output = mbedtls_calloc( 1, max_output_size ); TEST_ASSERT( (unsigned int) first_part_size < input_size ); @@ -515,7 +513,7 @@ void cipher_test_decrypt( int alg_arg, int key_type_arg, iv, sizeof( iv ) ) == PSA_SUCCESS ); max_output_size = input_size + operation.block_size; - output = mbedtls_calloc( 1, output_size ); + output = mbedtls_calloc( 1, max_output_size ); TEST_ASSERT( psa_cipher_update( &operation, input, input_size, output, max_output_size, @@ -527,8 +525,6 @@ void cipher_test_decrypt( int alg_arg, int key_type_arg, if( expected_status == 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 ); } From 8172b87a6332120b7d2c1ae12107e7a9072df99e Mon Sep 17 00:00:00 2001 From: Moran Peker Date: Thu, 7 Jun 2018 18:09:18 +0300 Subject: [PATCH 49/54] add tests cases --- tests/suites/test_suite_psa_crypto.data | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/suites/test_suite_psa_crypto.data b/tests/suites/test_suite_psa_crypto.data index 783148cf19..e9f26bc246 100644 --- a/tests/suites/test_suite_psa_crypto.data +++ b/tests/suites/test_suite_psa_crypto.data @@ -57,14 +57,38 @@ PSA Symmetric encryption: AES-CBC-nopad, 16 bytes, good depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC cipher_test_encrypt:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a":"a076ec9dfbe47d52afc357336f20743b":PSA_SUCCESS +PSA Symmetric encryption: AES-CBC-PKCS#7, 16 bytes, good +depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC +cipher_test_encrypt:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a":"a076ec9dfbe47d52afc357336f20743b":PSA_SUCCESS + +PSA Symmetric encryption: AES-CBC-PKCS#7, 15 bytes, good +depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC +cipher_test_encrypt:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e11739317":"6279b49d7f7a8dd87b685175d4276e24":PSA_SUCCESS + PSA Symmetric encryption: AES-CBC-nopad, input too short depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC cipher_test_encrypt:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee223":"6bc1bee223":PSA_ERROR_INVALID_ARGUMENT +PSA Symmetric encryption: AES-CTR, 16 bytes, good +depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC +cipher_test_encrypt:PSA_ALG_CTR | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a":"8f9408fe80a81d3e813da3c7b0b2bd32":PSA_SUCCESS + +PSA Symmetric encryption: AES-CTR, 15 bytes, good +depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC +cipher_test_encrypt:PSA_ALG_CTR | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e11739317":"8f9408fe80a81d3e813da3c7b0b2bd00":PSA_SUCCESS + PSA Symmetric decryption: AES-CBC-nopad, 16 bytes, good depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC cipher_test_decrypt:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"396ee84fb75fdbb5c2b13c7fe5a654aa":"49e4e66c89a86b67758df89db9ad6955":PSA_SUCCESS +PSA Symmetric decryption: AES-CBC-PKCS#7, 15 bytes, bad - cipher full block expected +depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC +cipher_test_decrypt:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e11739317":"49e4e66c89a86b67758df89db9ad6955":PSA_ERROR_BAD_STATE + +PSA Symmetric decryption: AES-CTR, 16 bytes, good +depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC +cipher_test_decrypt:PSA_ALG_CTR | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"396ee84fb75fdbb5c2b13c7fe5a654aa":"dd3b5e5319b7591daab1e1a92687feb2":PSA_SUCCESS + PSA Symmetric decryption: AES-CBC-nopad, input too short depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC cipher_test_decrypt:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee223":"6bc1bee223":PSA_ERROR_BAD_STATE From 048b7f080203519c2ca8d258dcd84ef62d4e2c2e Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Fri, 8 Jun 2018 14:20:49 +0200 Subject: [PATCH 50/54] Rename some variables to make the code easier to read In cipher_test_verify_output_multpart, tweak the ways chunk sizes are added in order to get rid of the variable temp. In other functions, this commit does not change the logic at all. --- tests/suites/test_suite_psa_crypto.function | 178 +++++++++++--------- 1 file changed, 95 insertions(+), 83 deletions(-) diff --git a/tests/suites/test_suite_psa_crypto.function b/tests/suites/test_suite_psa_crypto.function index 9f9dd6808f..29f233b555 100644 --- a/tests/suites/test_suite_psa_crypto.function +++ b/tests/suites/test_suite_psa_crypto.function @@ -282,8 +282,9 @@ void cipher_test_encrypt( int alg_arg, int key_type_arg, size_t input_size = 0; unsigned char *output; unsigned char *expected_output; - size_t output_size, max_output_size = 0; - size_t output_length = 0; + size_t expected_output_size; + size_t output_buffer_size = 0; + size_t function_output_length = 0; psa_cipher_operation_t operation; @@ -293,7 +294,7 @@ void cipher_test_encrypt( 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 ); + expected_output = unhexify_alloc( output_hex, &expected_output_size ); TEST_ASSERT( expected_output != NULL ); memset( iv, 0x2a, sizeof( iv ) ); @@ -307,19 +308,22 @@ void cipher_test_encrypt( int alg_arg, int key_type_arg, TEST_ASSERT( psa_encrypt_set_iv( &operation, iv, sizeof( iv ) ) == PSA_SUCCESS ); - max_output_size = input_size + operation.block_size; - output = mbedtls_calloc( 1, max_output_size ); + output_buffer_size = input_size + operation.block_size; + output = mbedtls_calloc( 1, output_buffer_size ); TEST_ASSERT( psa_cipher_update( &operation, input, input_size, - output, max_output_size, - &output_length ) == PSA_SUCCESS ); - status = psa_cipher_finish( &operation, output + output_length, - max_output_size, &output_length ); + output, output_buffer_size, + &function_output_length ) == PSA_SUCCESS ); + status = psa_cipher_finish( &operation, + output + function_output_length, + output_buffer_size, + &function_output_length ); TEST_ASSERT( status == (psa_status_t) expected_status ); if( expected_status == PSA_SUCCESS ) { TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); - TEST_ASSERT( memcmp( expected_output, output, output_size ) == 0 ); + TEST_ASSERT( memcmp( expected_output, output, + expected_output_size ) == 0 ); } exit: mbedtls_free( key ); @@ -345,8 +349,9 @@ void cipher_test_encrypt_multipart( int alg_arg, int key_type_arg, size_t input_size = 0; unsigned char *output; unsigned char *expected_output; - size_t output_size, max_output_size = 0; - size_t output_length = 0; + size_t expected_output_size; + size_t output_buffer_size = 0; + size_t function_output_length = 0; psa_cipher_operation_t operation; @@ -356,7 +361,7 @@ void cipher_test_encrypt_multipart( 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 ); + expected_output = unhexify_alloc( output_hex, &expected_output_size ); TEST_ASSERT( expected_output != NULL ); memset( iv, 0x2a, sizeof( iv ) ); @@ -370,25 +375,27 @@ void cipher_test_encrypt_multipart( int alg_arg, int key_type_arg, TEST_ASSERT( psa_encrypt_set_iv( &operation, iv, sizeof( iv ) ) == PSA_SUCCESS ); - max_output_size = input_size + operation.block_size; - output = mbedtls_calloc( 1, max_output_size ); + output_buffer_size = input_size + operation.block_size; + output = mbedtls_calloc( 1, output_buffer_size ); TEST_ASSERT( (unsigned int) first_part_size < input_size ); TEST_ASSERT( psa_cipher_update( &operation, input, first_part_size, - output, max_output_size, - &output_length ) == PSA_SUCCESS ); + output, output_buffer_size, + &function_output_length ) == PSA_SUCCESS ); TEST_ASSERT( psa_cipher_update( &operation, input + first_part_size, input_size - first_part_size, - output, max_output_size, - &output_length ) == PSA_SUCCESS ); - TEST_ASSERT( psa_cipher_finish( &operation, output + output_length, - max_output_size, &output_length ) == PSA_SUCCESS ); + output, output_buffer_size, + &function_output_length ) == PSA_SUCCESS ); + TEST_ASSERT( psa_cipher_finish( &operation, + output + function_output_length, + output_buffer_size, + &function_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 ); + TEST_ASSERT( input_size == expected_output_size ); + TEST_ASSERT( memcmp( expected_output, output, expected_output_size ) == 0 ); exit: mbedtls_free( key ); @@ -415,8 +422,9 @@ void cipher_test_decrypt_multipart( int alg_arg, int key_type_arg, size_t input_size = 0; unsigned char *output; unsigned char *expected_output; - size_t output_size, max_output_size = 0; - size_t output_length = 0; + size_t expected_output_size; + size_t output_buffer_size = 0; + size_t function_output_length = 0; psa_cipher_operation_t operation; @@ -426,7 +434,7 @@ void cipher_test_decrypt_multipart( 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 ); + expected_output = unhexify_alloc( output_hex, &expected_output_size ); TEST_ASSERT( expected_output != NULL ); memset( iv, 0x2a, sizeof( iv ) ); @@ -441,24 +449,26 @@ void cipher_test_decrypt_multipart( int alg_arg, int key_type_arg, TEST_ASSERT( psa_encrypt_set_iv( &operation, iv, sizeof( iv ) ) == PSA_SUCCESS ); - max_output_size = input_size + operation.block_size; - output = mbedtls_calloc( 1, max_output_size ); + output_buffer_size = input_size + operation.block_size; + output = mbedtls_calloc( 1, output_buffer_size ); TEST_ASSERT( (unsigned int) first_part_size < input_size ); TEST_ASSERT( psa_cipher_update( &operation, input, first_part_size, - output, max_output_size, - &output_length ) == PSA_SUCCESS ); + output, output_buffer_size, + &function_output_length ) == PSA_SUCCESS ); TEST_ASSERT( psa_cipher_update( &operation, input + first_part_size, input_size - first_part_size, - output, max_output_size, - &output_length ) == PSA_SUCCESS ); - TEST_ASSERT( psa_cipher_finish( &operation, output + output_length, - max_output_size, &output_length ) == PSA_SUCCESS ); + output, output_buffer_size, + &function_output_length ) == PSA_SUCCESS ); + TEST_ASSERT( psa_cipher_finish( &operation, + output + function_output_length, + output_buffer_size, + &function_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 ); + TEST_ASSERT( input_size == expected_output_size ); + TEST_ASSERT( memcmp( expected_output, output, expected_output_size ) == 0 ); exit: mbedtls_free( key ); @@ -486,8 +496,9 @@ void cipher_test_decrypt( int alg_arg, int key_type_arg, size_t input_size = 0; unsigned char *output; unsigned char *expected_output; - size_t output_size, max_output_size = 0; - size_t output_length = 0; + size_t expected_output_size; + size_t output_buffer_size = 0; + size_t function_output_length = 0; psa_cipher_operation_t operation; @@ -497,7 +508,7 @@ void cipher_test_decrypt( 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 ); + expected_output = unhexify_alloc( output_hex, &expected_output_size ); TEST_ASSERT( expected_output != NULL ); memset( iv, 0x2a, sizeof( iv ) ); @@ -512,20 +523,23 @@ void cipher_test_decrypt( int alg_arg, int key_type_arg, TEST_ASSERT( psa_encrypt_set_iv( &operation, iv, sizeof( iv ) ) == PSA_SUCCESS ); - max_output_size = input_size + operation.block_size; - output = mbedtls_calloc( 1, max_output_size ); + output_buffer_size = input_size + operation.block_size; + output = mbedtls_calloc( 1, output_buffer_size ); TEST_ASSERT( psa_cipher_update( &operation, input, input_size, - output, max_output_size, - &output_length ) == PSA_SUCCESS ); - status = psa_cipher_finish( &operation, output + output_length, - max_output_size, &output_length ); + output, output_buffer_size, + &function_output_length ) == PSA_SUCCESS ); + status = psa_cipher_finish( &operation, + output + function_output_length, + output_buffer_size, + &function_output_length ); TEST_ASSERT( status == (psa_status_t) expected_status ); if( expected_status == PSA_SUCCESS ) { TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); - TEST_ASSERT( memcmp( expected_output, output, output_size ) == 0 ); + TEST_ASSERT( memcmp( expected_output, output, + expected_output_size ) == 0 ); } @@ -559,7 +573,7 @@ void cipher_test_verify_output( int alg_arg, int key_type_arg, unsigned char *output2; size_t output2_size = 0; size_t output2_length = 0; - size_t tmp_output_length = 0; + size_t function_output_length = 0; psa_cipher_operation_t operation1; psa_cipher_operation_t operation2; @@ -589,9 +603,9 @@ void cipher_test_verify_output( int alg_arg, int key_type_arg, &output1_length ) == PSA_SUCCESS ); TEST_ASSERT( psa_cipher_finish( &operation1, output1 + output1_length, output1_size, - &tmp_output_length ) == PSA_SUCCESS ); + &function_output_length ) == PSA_SUCCESS ); - output1_length += tmp_output_length; + output1_length += function_output_length; TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); @@ -603,13 +617,13 @@ void cipher_test_verify_output( int alg_arg, int key_type_arg, TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length, output2, output2_size, &output2_length ) == PSA_SUCCESS ); - tmp_output_length = 0; + function_output_length = 0; TEST_ASSERT( psa_cipher_finish( &operation2, output2 + output2_length, output2_size, - &tmp_output_length ) == PSA_SUCCESS ); + &function_output_length ) == PSA_SUCCESS ); - output2_length += tmp_output_length; + output2_length += function_output_length; TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); @@ -642,12 +656,12 @@ void cipher_test_verify_output_multpart( int alg_arg, unsigned char *input = NULL; size_t input_size = 0; unsigned char *output1; - size_t output1_size = 0; + size_t output1_buffer_size = 0; size_t output1_length = 0; unsigned char *output2; - size_t output2_size = 0; + size_t output2_buffer_size = 0; size_t output2_length = 0; - size_t tmp_output_length, temp = 0; + size_t function_output_length; psa_cipher_operation_t operation1; psa_cipher_operation_t operation2; @@ -668,56 +682,54 @@ void cipher_test_verify_output_multpart( int alg_arg, TEST_ASSERT( psa_encrypt_generate_iv( &operation1, iv, iv_size, &iv_length ) == PSA_SUCCESS ); - output1_size = input_size + operation1.block_size; - output1 = mbedtls_calloc( 1, output1_size ); + output1_buffer_size = input_size + operation1.block_size; + output1 = mbedtls_calloc( 1, output1_buffer_size ); TEST_ASSERT( (unsigned int) first_part_size < input_size ); TEST_ASSERT( psa_cipher_update( &operation1, input, first_part_size, - output1, output1_size, - &output1_length ) == PSA_SUCCESS ); - temp = output1_length; + output1, output1_buffer_size, + &function_output_length ) == PSA_SUCCESS ); + output1_length += function_output_length; TEST_ASSERT( psa_cipher_update( &operation1, input + first_part_size, input_size - first_part_size, - output1, output1_size, - &output1_length ) == PSA_SUCCESS ); - output1_length += temp; + output1, output1_buffer_size, + &function_output_length ) == PSA_SUCCESS ); + output1_length += function_output_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_finish( &operation1, + output1 + output1_length, + output1_buffer_size - output1_length, + &function_output_length ) == PSA_SUCCESS ); + output1_length += function_output_length; TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); - output2_size = output1_length; - output2 = mbedtls_calloc( 1, output2_size ); + output2_buffer_size = output1_length; + output2 = mbedtls_calloc( 1, output2_buffer_size ); TEST_ASSERT( psa_encrypt_set_iv( &operation2, iv, iv_length ) == PSA_SUCCESS ); TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size, - output2, output2_size, - &output2_length ) == PSA_SUCCESS ); + output2, output2_buffer_size, + &function_output_length ) == PSA_SUCCESS ); + output2_length += function_output_length; - temp = output2_length; - - TEST_ASSERT( psa_cipher_update( &operation2, output1 + first_part_size, + TEST_ASSERT( psa_cipher_update( &operation2, + output1 + first_part_size, output1_length - first_part_size, - output2, output2_size, - &output2_length ) == PSA_SUCCESS ); + output2, output2_buffer_size, + &function_output_length ) == PSA_SUCCESS ); + output2_length += function_output_length; - output2_length += temp; - tmp_output_length = 0; TEST_ASSERT( psa_cipher_finish( &operation2, output2 + output2_length, - output2_size - output2_length, - &tmp_output_length ) == PSA_SUCCESS ); - - output2_length += tmp_output_length; + output2_buffer_size - output2_length, + &function_output_length ) == PSA_SUCCESS ); + output2_length += function_output_length; TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); From 50e586b691cb81ad3dcf5f43ede156feb22fb8d2 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Fri, 8 Jun 2018 14:28:46 +0200 Subject: [PATCH 51/54] We don't need _test_ in test function names Also fix typo multpart -> multipart --- tests/suites/test_suite_psa_crypto.data | 46 +- tests/suites/test_suite_psa_crypto.function | 588 ++++++++++---------- 2 files changed, 317 insertions(+), 317 deletions(-) diff --git a/tests/suites/test_suite_psa_crypto.data b/tests/suites/test_suite_psa_crypto.data index e9f26bc246..14eb73477f 100644 --- a/tests/suites/test_suite_psa_crypto.data +++ b/tests/suites/test_suite_psa_crypto.data @@ -55,95 +55,95 @@ mac_verify:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":PSA_ALG_CMAC:"":" PSA Symmetric encryption: AES-CBC-nopad, 16 bytes, good depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC -cipher_test_encrypt:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a":"a076ec9dfbe47d52afc357336f20743b":PSA_SUCCESS +cipher_encrypt:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a":"a076ec9dfbe47d52afc357336f20743b":PSA_SUCCESS PSA Symmetric encryption: AES-CBC-PKCS#7, 16 bytes, good depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC -cipher_test_encrypt:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a":"a076ec9dfbe47d52afc357336f20743b":PSA_SUCCESS +cipher_encrypt:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a":"a076ec9dfbe47d52afc357336f20743b":PSA_SUCCESS PSA Symmetric encryption: AES-CBC-PKCS#7, 15 bytes, good depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC -cipher_test_encrypt:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e11739317":"6279b49d7f7a8dd87b685175d4276e24":PSA_SUCCESS +cipher_encrypt:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e11739317":"6279b49d7f7a8dd87b685175d4276e24":PSA_SUCCESS PSA Symmetric encryption: AES-CBC-nopad, input too short depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC -cipher_test_encrypt:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee223":"6bc1bee223":PSA_ERROR_INVALID_ARGUMENT +cipher_encrypt:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee223":"6bc1bee223":PSA_ERROR_INVALID_ARGUMENT PSA Symmetric encryption: AES-CTR, 16 bytes, good depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC -cipher_test_encrypt:PSA_ALG_CTR | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a":"8f9408fe80a81d3e813da3c7b0b2bd32":PSA_SUCCESS +cipher_encrypt:PSA_ALG_CTR | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a":"8f9408fe80a81d3e813da3c7b0b2bd32":PSA_SUCCESS PSA Symmetric encryption: AES-CTR, 15 bytes, good depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC -cipher_test_encrypt:PSA_ALG_CTR | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e11739317":"8f9408fe80a81d3e813da3c7b0b2bd00":PSA_SUCCESS +cipher_encrypt:PSA_ALG_CTR | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e11739317":"8f9408fe80a81d3e813da3c7b0b2bd00":PSA_SUCCESS PSA Symmetric decryption: AES-CBC-nopad, 16 bytes, good depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC -cipher_test_decrypt:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"396ee84fb75fdbb5c2b13c7fe5a654aa":"49e4e66c89a86b67758df89db9ad6955":PSA_SUCCESS +cipher_decrypt:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"396ee84fb75fdbb5c2b13c7fe5a654aa":"49e4e66c89a86b67758df89db9ad6955":PSA_SUCCESS PSA Symmetric decryption: AES-CBC-PKCS#7, 15 bytes, bad - cipher full block expected depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC -cipher_test_decrypt:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e11739317":"49e4e66c89a86b67758df89db9ad6955":PSA_ERROR_BAD_STATE +cipher_decrypt:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e11739317":"49e4e66c89a86b67758df89db9ad6955":PSA_ERROR_BAD_STATE PSA Symmetric decryption: AES-CTR, 16 bytes, good depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC -cipher_test_decrypt:PSA_ALG_CTR | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"396ee84fb75fdbb5c2b13c7fe5a654aa":"dd3b5e5319b7591daab1e1a92687feb2":PSA_SUCCESS +cipher_decrypt:PSA_ALG_CTR | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"396ee84fb75fdbb5c2b13c7fe5a654aa":"dd3b5e5319b7591daab1e1a92687feb2":PSA_SUCCESS PSA Symmetric decryption: AES-CBC-nopad, input too short depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC -cipher_test_decrypt:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee223":"6bc1bee223":PSA_ERROR_BAD_STATE +cipher_decrypt:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee223":"6bc1bee223":PSA_ERROR_BAD_STATE PSA Symmetric encryption/decryption: AES-CBC-nopad, 16 bytes, good depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC -cipher_test_verify_output:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a" +cipher_verify_output:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a" PSA Symmetric encryption/decryption: AES-CBC-PKCS#7, 16 bytes depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_CIPHER_PADDING_PKCS7 -cipher_test_verify_output:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a" +cipher_verify_output:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a" PSA Symmetric encryption/decryption: AES-CBC-PKCS#7, 15 bytes depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_CIPHER_PADDING_PKCS7 -cipher_test_verify_output:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e11739317" +cipher_verify_output:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e11739317" PSA Symmetric encryption/decryption: AES-CTR depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CTR -cipher_test_verify_output:PSA_ALG_CTR | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a" +cipher_verify_output:PSA_ALG_CTR | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a" PSA Symmetric encryption multipart: AES-CBC-nopad, 7+9 bytes depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC -cipher_test_encrypt_multipart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a":7:"a076ec9dfbe47d52afc357336f20743b" +cipher_encrypt_multipart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a":7:"a076ec9dfbe47d52afc357336f20743b" PSA Symmetric encryption multipart: AES-CBC-nopad, 3+13 bytes depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC -cipher_test_encrypt_multipart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a":3:"a076ec9dfbe47d52afc357336f20743b" +cipher_encrypt_multipart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a":3:"a076ec9dfbe47d52afc357336f20743b" PSA Symmetric encryption multipart: AES-CBC-nopad, 4+12 bytes depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC -cipher_test_encrypt_multipart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a":4:"a076ec9dfbe47d52afc357336f20743b" +cipher_encrypt_multipart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a":4:"a076ec9dfbe47d52afc357336f20743b" PSA Symmetric encryption multipart: AES-CBC-nopad, 11+5 bytes depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC -cipher_test_encrypt_multipart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a":11:"a076ec9dfbe47d52afc357336f20743b" +cipher_encrypt_multipart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a":11:"a076ec9dfbe47d52afc357336f20743b" PSA Symmetric decryption multipart: AES-CBC-nopad, 7+9 bytes depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC -cipher_test_decrypt_multipart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"a076ec9dfbe47d52afc357336f20743b":7:"6bc1bee22e409f96e93d7e117393172a" +cipher_decrypt_multipart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"a076ec9dfbe47d52afc357336f20743b":7:"6bc1bee22e409f96e93d7e117393172a" PSA Symmetric decryption multipart: AES-CBC-nopad, 3+13 bytes depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC -cipher_test_decrypt_multipart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"a076ec9dfbe47d52afc357336f20743b":3:"6bc1bee22e409f96e93d7e117393172a" +cipher_decrypt_multipart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"a076ec9dfbe47d52afc357336f20743b":3:"6bc1bee22e409f96e93d7e117393172a" PSA Symmetric decryption multipart: AES-CBC-nopad, 11+5 bytes depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC -cipher_test_decrypt_multipart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"a076ec9dfbe47d52afc357336f20743b":11:"6bc1bee22e409f96e93d7e117393172a" +cipher_decrypt_multipart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"a076ec9dfbe47d52afc357336f20743b":11:"6bc1bee22e409f96e93d7e117393172a" PSA Symmetric encryption + decryption multipart: AES-CBC-nopad, 11+5 bytes depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC -cipher_test_verify_output_multpart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"a076ec9dfbe47d52afc357336f20743b":11 +cipher_verify_output_multipart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"a076ec9dfbe47d52afc357336f20743b":11 PSA Symmetric encryption + decryption multipart: AES-CBC-PKCS#7 padding, 4+12 bytes depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_CIPHER_PADDING_PKCS7 -cipher_test_verify_output_multpart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"a076ec9dfbe47d52afc357336f20743b":4 +cipher_verify_output_multipart:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"a076ec9dfbe47d52afc357336f20743b":4 PSA signature size: RSA keypair, 1024 bits, PKCS#1 v1.5 raw signature_size:PSA_KEY_TYPE_RSA_KEYPAIR:1024:PSA_ALG_RSA_PKCS1V15_SIGN_RAW:128 diff --git a/tests/suites/test_suite_psa_crypto.function b/tests/suites/test_suite_psa_crypto.function index 29f233b555..bee64ef6c6 100644 --- a/tests/suites/test_suite_psa_crypto.function +++ b/tests/suites/test_suite_psa_crypto.function @@ -266,296 +266,296 @@ exit: /* BEGIN_CASE */ -void cipher_test_encrypt( int alg_arg, int key_type_arg, +void cipher_encrypt( int alg_arg, int key_type_arg, + char *key_hex, + char *input_hex, char *output_hex, + int expected_status ) +{ + int key_slot = 1; + psa_status_t status; + 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 expected_output_size; + size_t output_buffer_size = 0; + size_t function_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, &expected_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_encrypt_setup( &operation, key_slot, alg ) == PSA_SUCCESS ); + + TEST_ASSERT( psa_encrypt_set_iv( &operation, + iv, sizeof( iv ) ) == PSA_SUCCESS ); + output_buffer_size = input_size + operation.block_size; + output = mbedtls_calloc( 1, output_buffer_size ); + + TEST_ASSERT( psa_cipher_update( &operation, input, input_size, + output, output_buffer_size, + &function_output_length ) == PSA_SUCCESS ); + status = psa_cipher_finish( &operation, + output + function_output_length, + output_buffer_size, + &function_output_length ); + TEST_ASSERT( status == (psa_status_t) expected_status ); + if( expected_status == PSA_SUCCESS ) + { + TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); + TEST_ASSERT( memcmp( expected_output, output, + expected_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_encrypt_multipart( int alg_arg, int key_type_arg, + char *key_hex, + char *input_hex, + int first_part_size, 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 expected_output_size; + size_t output_buffer_size = 0; + size_t function_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, &expected_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_encrypt_setup( &operation, key_slot, alg ) == PSA_SUCCESS ); + + TEST_ASSERT( psa_encrypt_set_iv( &operation, + iv, sizeof( iv ) ) == PSA_SUCCESS ); + output_buffer_size = input_size + operation.block_size; + output = mbedtls_calloc( 1, output_buffer_size ); + + TEST_ASSERT( (unsigned int) first_part_size < input_size ); + TEST_ASSERT( psa_cipher_update( &operation, input, first_part_size, + output, output_buffer_size, + &function_output_length ) == PSA_SUCCESS ); + TEST_ASSERT( psa_cipher_update( &operation, + input + first_part_size, + input_size - first_part_size, + output, output_buffer_size, + &function_output_length ) == PSA_SUCCESS ); + TEST_ASSERT( psa_cipher_finish( &operation, + output + function_output_length, + output_buffer_size, + &function_output_length ) == PSA_SUCCESS ); + + TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); + + TEST_ASSERT( input_size == expected_output_size ); + TEST_ASSERT( memcmp( expected_output, output, expected_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_decrypt_multipart( int alg_arg, int key_type_arg, + char *key_hex, + char *input_hex, + int first_part_size, 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 expected_output_size; + size_t output_buffer_size = 0; + size_t function_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, &expected_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_buffer_size = input_size + operation.block_size; + output = mbedtls_calloc( 1, output_buffer_size ); + + TEST_ASSERT( (unsigned int) first_part_size < input_size ); + TEST_ASSERT( psa_cipher_update( &operation, input, first_part_size, + output, output_buffer_size, + &function_output_length ) == PSA_SUCCESS ); + TEST_ASSERT( psa_cipher_update( &operation, + input + first_part_size, + input_size - first_part_size, + output, output_buffer_size, + &function_output_length ) == PSA_SUCCESS ); + TEST_ASSERT( psa_cipher_finish( &operation, + output + function_output_length, + output_buffer_size, + &function_output_length ) == PSA_SUCCESS ); + TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); + + TEST_ASSERT( input_size == expected_output_size ); + TEST_ASSERT( memcmp( expected_output, output, expected_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_decrypt( int alg_arg, int key_type_arg, + char *key_hex, + char *input_hex, char *output_hex, + int expected_status ) +{ + int key_slot = 1; + psa_status_t status; + 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 expected_output_size; + size_t output_buffer_size = 0; + size_t function_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, &expected_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_buffer_size = input_size + operation.block_size; + output = mbedtls_calloc( 1, output_buffer_size ); + + TEST_ASSERT( psa_cipher_update( &operation, input, input_size, + output, output_buffer_size, + &function_output_length ) == PSA_SUCCESS ); + status = psa_cipher_finish( &operation, + output + function_output_length, + output_buffer_size, + &function_output_length ); + TEST_ASSERT( status == (psa_status_t) expected_status ); + + if( expected_status == PSA_SUCCESS ) + { + TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); + TEST_ASSERT( memcmp( expected_output, output, + expected_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_verify_output( int alg_arg, int key_type_arg, char *key_hex, - char *input_hex, char *output_hex, - int expected_status ) -{ - int key_slot = 1; - psa_status_t status; - 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 expected_output_size; - size_t output_buffer_size = 0; - size_t function_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, &expected_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_encrypt_setup( &operation, key_slot, alg ) == PSA_SUCCESS ); - - TEST_ASSERT( psa_encrypt_set_iv( &operation, - iv, sizeof( iv ) ) == PSA_SUCCESS ); - output_buffer_size = input_size + operation.block_size; - output = mbedtls_calloc( 1, output_buffer_size ); - - TEST_ASSERT( psa_cipher_update( &operation, input, input_size, - output, output_buffer_size, - &function_output_length ) == PSA_SUCCESS ); - status = psa_cipher_finish( &operation, - output + function_output_length, - output_buffer_size, - &function_output_length ); - TEST_ASSERT( status == (psa_status_t) expected_status ); - if( expected_status == PSA_SUCCESS ) - { - TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); - TEST_ASSERT( memcmp( expected_output, output, - expected_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_encrypt_multipart( int alg_arg, int key_type_arg, - char *key_hex, - char *input_hex, - int first_part_size, 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 expected_output_size; - size_t output_buffer_size = 0; - size_t function_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, &expected_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_encrypt_setup( &operation, key_slot, alg ) == PSA_SUCCESS ); - - TEST_ASSERT( psa_encrypt_set_iv( &operation, - iv, sizeof( iv ) ) == PSA_SUCCESS ); - output_buffer_size = input_size + operation.block_size; - output = mbedtls_calloc( 1, output_buffer_size ); - - TEST_ASSERT( (unsigned int) first_part_size < input_size ); - TEST_ASSERT( psa_cipher_update( &operation, input, first_part_size, - output, output_buffer_size, - &function_output_length ) == PSA_SUCCESS ); - TEST_ASSERT( psa_cipher_update( &operation, - input + first_part_size, - input_size - first_part_size, - output, output_buffer_size, - &function_output_length ) == PSA_SUCCESS ); - TEST_ASSERT( psa_cipher_finish( &operation, - output + function_output_length, - output_buffer_size, - &function_output_length ) == PSA_SUCCESS ); - - TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); - - TEST_ASSERT( input_size == expected_output_size ); - TEST_ASSERT( memcmp( expected_output, output, expected_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_decrypt_multipart( int alg_arg, int key_type_arg, - char *key_hex, - char *input_hex, - int first_part_size, 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 expected_output_size; - size_t output_buffer_size = 0; - size_t function_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, &expected_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_buffer_size = input_size + operation.block_size; - output = mbedtls_calloc( 1, output_buffer_size ); - - TEST_ASSERT( (unsigned int) first_part_size < input_size ); - TEST_ASSERT( psa_cipher_update( &operation, input, first_part_size, - output, output_buffer_size, - &function_output_length ) == PSA_SUCCESS ); - TEST_ASSERT( psa_cipher_update( &operation, - input + first_part_size, - input_size - first_part_size, - output, output_buffer_size, - &function_output_length ) == PSA_SUCCESS ); - TEST_ASSERT( psa_cipher_finish( &operation, - output + function_output_length, - output_buffer_size, - &function_output_length ) == PSA_SUCCESS ); - TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); - - TEST_ASSERT( input_size == expected_output_size ); - TEST_ASSERT( memcmp( expected_output, output, expected_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_decrypt( int alg_arg, int key_type_arg, - char *key_hex, - char *input_hex, char *output_hex, - int expected_status ) -{ - int key_slot = 1; - psa_status_t status; - 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 expected_output_size; - size_t output_buffer_size = 0; - size_t function_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, &expected_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_buffer_size = input_size + operation.block_size; - output = mbedtls_calloc( 1, output_buffer_size ); - - TEST_ASSERT( psa_cipher_update( &operation, input, input_size, - output, output_buffer_size, - &function_output_length ) == PSA_SUCCESS ); - status = psa_cipher_finish( &operation, - output + function_output_length, - output_buffer_size, - &function_output_length ); - TEST_ASSERT( status == (psa_status_t) expected_status ); - - if( expected_status == PSA_SUCCESS ) - { - TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); - TEST_ASSERT( memcmp( expected_output, output, - expected_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, - char *input_hex ) + char *input_hex ) { int key_slot = 1; psa_key_type_t key_type = key_type_arg; @@ -639,11 +639,11 @@ exit: /* END_CASE */ /* BEGIN_CASE */ -void cipher_test_verify_output_multpart( int alg_arg, - int key_type_arg, - char *key_hex, - char *input_hex, - int first_part_size ) +void cipher_verify_output_multipart( int alg_arg, + int key_type_arg, + char *key_hex, + char *input_hex, + int first_part_size ) { int key_slot = 1; psa_key_type_t key_type = key_type_arg; From a7ec95f1ea08139290184dcaf2f5b450da51a4af Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Fri, 8 Jun 2018 14:40:59 +0200 Subject: [PATCH 52/54] Cipher tests: calculate and verify the actual output size --- tests/suites/test_suite_psa_crypto.function | 25 ++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/tests/suites/test_suite_psa_crypto.function b/tests/suites/test_suite_psa_crypto.function index bee64ef6c6..6be41c3504 100644 --- a/tests/suites/test_suite_psa_crypto.function +++ b/tests/suites/test_suite_psa_crypto.function @@ -285,6 +285,7 @@ void cipher_encrypt( int alg_arg, int key_type_arg, size_t expected_output_size; size_t output_buffer_size = 0; size_t function_output_length = 0; + size_t total_output_length = 0; psa_cipher_operation_t operation; @@ -314,17 +315,22 @@ void cipher_encrypt( int alg_arg, int key_type_arg, TEST_ASSERT( psa_cipher_update( &operation, input, input_size, output, output_buffer_size, &function_output_length ) == PSA_SUCCESS ); + total_output_length += function_output_length; status = psa_cipher_finish( &operation, output + function_output_length, output_buffer_size, &function_output_length ); + total_output_length += function_output_length; + TEST_ASSERT( status == (psa_status_t) expected_status ); if( expected_status == PSA_SUCCESS ) { TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); + TEST_ASSERT( total_output_length == expected_output_size ); TEST_ASSERT( memcmp( expected_output, output, expected_output_size ) == 0 ); } + exit: mbedtls_free( key ); mbedtls_free( input ); @@ -352,9 +358,9 @@ void cipher_encrypt_multipart( int alg_arg, int key_type_arg, size_t expected_output_size; size_t output_buffer_size = 0; size_t function_output_length = 0; + size_t total_output_length = 0; psa_cipher_operation_t operation; - key = unhexify_alloc( key_hex, &key_size ); TEST_ASSERT( key != NULL ); @@ -382,19 +388,21 @@ void cipher_encrypt_multipart( int alg_arg, int key_type_arg, TEST_ASSERT( psa_cipher_update( &operation, input, first_part_size, output, output_buffer_size, &function_output_length ) == PSA_SUCCESS ); + total_output_length += function_output_length; TEST_ASSERT( psa_cipher_update( &operation, input + first_part_size, input_size - first_part_size, output, output_buffer_size, &function_output_length ) == PSA_SUCCESS ); + total_output_length += function_output_length; TEST_ASSERT( psa_cipher_finish( &operation, output + function_output_length, output_buffer_size, &function_output_length ) == PSA_SUCCESS ); - + total_output_length += function_output_length; TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); - TEST_ASSERT( input_size == expected_output_size ); + TEST_ASSERT( total_output_length == expected_output_size ); TEST_ASSERT( memcmp( expected_output, output, expected_output_size ) == 0 ); exit: @@ -425,9 +433,9 @@ void cipher_decrypt_multipart( int alg_arg, int key_type_arg, size_t expected_output_size; size_t output_buffer_size = 0; size_t function_output_length = 0; + size_t total_output_length = 0; psa_cipher_operation_t operation; - key = unhexify_alloc( key_hex, &key_size ); TEST_ASSERT( key != NULL ); @@ -456,18 +464,21 @@ void cipher_decrypt_multipart( int alg_arg, int key_type_arg, TEST_ASSERT( psa_cipher_update( &operation, input, first_part_size, output, output_buffer_size, &function_output_length ) == PSA_SUCCESS ); + total_output_length += function_output_length; TEST_ASSERT( psa_cipher_update( &operation, input + first_part_size, input_size - first_part_size, output, output_buffer_size, &function_output_length ) == PSA_SUCCESS ); + total_output_length += function_output_length; TEST_ASSERT( psa_cipher_finish( &operation, output + function_output_length, output_buffer_size, &function_output_length ) == PSA_SUCCESS ); + total_output_length += function_output_length; TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); - TEST_ASSERT( input_size == expected_output_size ); + TEST_ASSERT( total_output_length == expected_output_size ); TEST_ASSERT( memcmp( expected_output, output, expected_output_size ) == 0 ); exit: @@ -499,6 +510,7 @@ void cipher_decrypt( int alg_arg, int key_type_arg, size_t expected_output_size; size_t output_buffer_size = 0; size_t function_output_length = 0; + size_t total_output_length = 0; psa_cipher_operation_t operation; @@ -529,15 +541,18 @@ void cipher_decrypt( int alg_arg, int key_type_arg, TEST_ASSERT( psa_cipher_update( &operation, input, input_size, output, output_buffer_size, &function_output_length ) == PSA_SUCCESS ); + total_output_length += function_output_length; status = psa_cipher_finish( &operation, output + function_output_length, output_buffer_size, &function_output_length ); + total_output_length += function_output_length; TEST_ASSERT( status == (psa_status_t) expected_status ); if( expected_status == PSA_SUCCESS ) { TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); + TEST_ASSERT( total_output_length == expected_output_size ); TEST_ASSERT( memcmp( expected_output, output, expected_output_size ) == 0 ); } From 42b8aec7923d03f909649f2234d773d2ab8cc3b0 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Fri, 8 Jun 2018 14:41:49 +0200 Subject: [PATCH 53/54] Correct some bad test data * PKCS#7 padding always adds at least one byte of padding, so test data with plaintext length = ciphertext length could not have been correct. * CTR has plaintext length = ciphertext length, so test data with differing lengths could not have been correct. --- tests/suites/test_suite_psa_crypto.data | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/suites/test_suite_psa_crypto.data b/tests/suites/test_suite_psa_crypto.data index 14eb73477f..e2fea70353 100644 --- a/tests/suites/test_suite_psa_crypto.data +++ b/tests/suites/test_suite_psa_crypto.data @@ -59,7 +59,7 @@ cipher_encrypt:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES PSA Symmetric encryption: AES-CBC-PKCS#7, 16 bytes, good depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC -cipher_encrypt:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a":"a076ec9dfbe47d52afc357336f20743b":PSA_SUCCESS +cipher_encrypt:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e117393172a":"a076ec9dfbe47d52afc357336f20743bca7e8a15dc3c776436314293031cd4f3":PSA_SUCCESS PSA Symmetric encryption: AES-CBC-PKCS#7, 15 bytes, good depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC @@ -75,7 +75,7 @@ cipher_encrypt:PSA_ALG_CTR | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7 PSA Symmetric encryption: AES-CTR, 15 bytes, good depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC -cipher_encrypt:PSA_ALG_CTR | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e11739317":"8f9408fe80a81d3e813da3c7b0b2bd00":PSA_SUCCESS +cipher_encrypt:PSA_ALG_CTR | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e11739317":"8f9408fe80a81d3e813da3c7b0b2bd":PSA_SUCCESS PSA Symmetric decryption: AES-CBC-nopad, 16 bytes, good depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC From 5809ce7bd602202e6b4a703a5987344f339665af Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Fri, 8 Jun 2018 14:42:50 +0200 Subject: [PATCH 54/54] Add PKCS#7 good decryption test cases --- tests/suites/test_suite_psa_crypto.data | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/suites/test_suite_psa_crypto.data b/tests/suites/test_suite_psa_crypto.data index e2fea70353..2d2431577e 100644 --- a/tests/suites/test_suite_psa_crypto.data +++ b/tests/suites/test_suite_psa_crypto.data @@ -81,6 +81,14 @@ PSA Symmetric decryption: AES-CBC-nopad, 16 bytes, good depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC cipher_decrypt:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_NONE:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"396ee84fb75fdbb5c2b13c7fe5a654aa":"49e4e66c89a86b67758df89db9ad6955":PSA_SUCCESS +PSA Symmetric decryption: AES-CBC-PKCS#7, 16 bytes, good +depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC +cipher_decrypt:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"a076ec9dfbe47d52afc357336f20743bca7e8a15dc3c776436314293031cd4f3":"6bc1bee22e409f96e93d7e117393172a":PSA_SUCCESS + +PSA Symmetric decryption: AES-CBC-PKCS#7, 15 bytes, good +depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC +cipher_decrypt:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6279b49d7f7a8dd87b685175d4276e24":"6bc1bee22e409f96e93d7e11739317":PSA_SUCCESS + PSA Symmetric decryption: AES-CBC-PKCS#7, 15 bytes, bad - cipher full block expected depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC cipher_decrypt:PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:PSA_KEY_TYPE_AES:"2b7e151628aed2a6abf7158809cf4f3c":"6bc1bee22e409f96e93d7e11739317":"49e4e66c89a86b67758df89db9ad6955":PSA_ERROR_BAD_STATE