From 8cc1ceec3e00486e98712f61757c51b563025578 Mon Sep 17 00:00:00 2001 From: mohammad1603 Date: Wed, 28 Mar 2018 01:21:33 +0300 Subject: [PATCH 1/8] Key Policy APIs implementation --- include/psa/crypto.h | 6 ++ library/psa_crypto.c | 79 +++++++++++++++++++++ tests/suites/test_suite_psa_crypto.data | 3 + tests/suites/test_suite_psa_crypto.function | 39 ++++++++++ 4 files changed, 127 insertions(+) diff --git a/include/psa/crypto.h b/include/psa/crypto.h index e8b22e0f5..687a3499f 100644 --- a/include/psa/crypto.h +++ b/include/psa/crypto.h @@ -89,6 +89,8 @@ typedef enum { PSA_ERROR_INVALID_SIGNATURE, /** The decrypted padding is incorrect. */ PSA_ERROR_INVALID_PADDING, + /** The key policy is incorrect. */ + PSA_ERROR_INVALID_KEY_POLICY, /** An error occurred that does not correspond to any defined failure cause. */ PSA_ERROR_UNKNOWN_ERROR, @@ -489,6 +491,10 @@ psa_status_t psa_export_public_key(psa_key_slot_t key, /** \brief Encoding of permitted usage on a key. */ typedef uint32_t psa_key_usage_t; +/** An invalid key usage value. + * */ +#define PSA_KEY_USAGE_NONE ((psa_key_usage_t)0x00000000) + /** Whether the key may be exported. * * A public key or the public part of a key pair may always be exported diff --git a/library/psa_crypto.c b/library/psa_crypto.c index c93da95b9..d53d6ee40 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -96,6 +96,7 @@ static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n ) typedef struct { psa_key_type_t type; + psa_key_policy_t policy; union { struct raw_data { uint8_t *data; @@ -1260,6 +1261,84 @@ psa_status_t psa_asymmetric_sign(psa_key_slot_t key, } +/****************************************************************/ +/* Key Policy */ +/****************************************************************/ + +void psa_key_policy_init(psa_key_policy_t *policy) +{ + mbedtls_zeroize( policy, sizeof( policy ) ); +} + +void psa_key_policy_set_usage(psa_key_policy_t *policy, + psa_key_usage_t usage, + psa_algorithm_t alg) +{ + if( policy != NULL ) + { + policy->usage = usage; + policy->alg = alg; + } +} + +psa_key_usage_t psa_key_policy_get_usage(psa_key_policy_t *policy) +{ + return policy->usage; +} + +psa_algorithm_t psa_key_policy_get_algorithm(psa_key_policy_t *policy) +{ + return policy->alg; +} + +psa_status_t psa_set_key_policy(psa_key_slot_t key, + const psa_key_policy_t *policy) +{ + key_slot_t *slot; + psa_key_usage_t usage = PSA_KEY_USAGE_NONE; + + if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT || policy == NULL ) + return( PSA_ERROR_INVALID_ARGUMENT ); + + slot = &global_data.key_slots[key]; + if( slot->type != PSA_KEY_TYPE_NONE ) + return( PSA_ERROR_OCCUPIED_SLOT ); + + usage |= policy->usage & PSA_KEY_USAGE_EXPORT; + usage |= policy->usage & PSA_KEY_USAGE_ENCRYPT; + usage |= policy->usage & PSA_KEY_USAGE_DECRYPT; + usage |= policy->usage & PSA_KEY_USAGE_SIGN; + usage |= policy->usage & PSA_KEY_USAGE_VERIFY; + + if( usage == PSA_KEY_USAGE_NONE ) + { + return( PSA_ERROR_INVALID_KEY_POLICY ); + } + + //TODO: is there any check over the algorithm before setting the policy? + slot->policy.usage = policy->usage; + slot->policy.alg = policy->alg; + + return( PSA_SUCCESS ); +} + +psa_status_t psa_get_key_policy(psa_key_slot_t key, + psa_key_policy_t *policy) +{ + key_slot_t *slot; + + if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT || policy == NULL ) + return( PSA_ERROR_INVALID_ARGUMENT ); + + slot = &global_data.key_slots[key]; + if( slot->type == PSA_KEY_TYPE_NONE ) + return( PSA_ERROR_EMPTY_SLOT ); + + policy->usage = slot->policy.usage; + policy->alg = slot->policy.alg; + + return( PSA_SUCCESS ); +} /****************************************************************/ /* Module setup */ diff --git a/tests/suites/test_suite_psa_crypto.data b/tests/suites/test_suite_psa_crypto.data index 4f4bef14c..c1261bc12 100644 --- a/tests/suites/test_suite_psa_crypto.data +++ b/tests/suites/test_suite_psa_crypto.data @@ -83,3 +83,6 @@ sign_fail:PSA_KEY_TYPE_RSA_KEYPAIR:"3082025e02010002818100af057d396ee84fb75fdbb5 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(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 diff --git a/tests/suites/test_suite_psa_crypto.function b/tests/suites/test_suite_psa_crypto.function index 93817948c..fc5684f14 100644 --- a/tests/suites/test_suite_psa_crypto.function +++ b/tests/suites/test_suite_psa_crypto.function @@ -360,3 +360,42 @@ exit: 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_ALG_CBC_BASE; + 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 */ + From 06e7920be5fd6a2680fea9f9a8a30c1a4e5c6a9a Mon Sep 17 00:00:00 2001 From: mohammad1603 Date: Wed, 28 Mar 2018 13:17:44 +0300 Subject: [PATCH 2/8] integrate policy key usage in export and asymmetric sign functions --- library/psa_crypto.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/library/psa_crypto.c b/library/psa_crypto.c index d53d6ee40..a12b45400 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -469,6 +469,9 @@ psa_status_t psa_export_key(psa_key_slot_t key, if( slot->type == PSA_KEY_TYPE_NONE ) return( PSA_ERROR_EMPTY_SLOT ); + if( !( slot->policy.usage & PSA_KEY_USAGE_EXPORT ) ) + return( PSA_ERROR_NOT_PERMITTED ); + if( PSA_KEY_TYPE_IS_RAW_BYTES( slot->type ) ) { if( slot->data.raw.bytes > data_size ) @@ -1185,6 +1188,8 @@ psa_status_t psa_asymmetric_sign(psa_key_slot_t key, return( PSA_ERROR_EMPTY_SLOT ); if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) return( PSA_ERROR_INVALID_ARGUMENT ); + if( !( slot->policy.usage & PSA_KEY_USAGE_SIGN ) ) + return( PSA_ERROR_NOT_PERMITTED ); #if defined(MBEDTLS_RSA_C) if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ) From a97cb8c303a5d1e12f4169d4bdbdf137c6874b25 Mon Sep 17 00:00:00 2001 From: mohammad1603 Date: Wed, 28 Mar 2018 03:46:26 -0700 Subject: [PATCH 3/8] Add calls for set policy in export/sign tests Add calls for set policy in export/sign tests --- tests/suites/test_suite_psa_crypto.function | 24 +++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/suites/test_suite_psa_crypto.function b/tests/suites/test_suite_psa_crypto.function index fc5684f14..653467b34 100644 --- a/tests/suites/test_suite_psa_crypto.function +++ b/tests/suites/test_suite_psa_crypto.function @@ -68,6 +68,7 @@ void import_export( char *hex, int type_arg, size_t reexported_length; psa_key_type_t got_type; size_t got_bits; + psa_key_policy_t policy = {0}; data = unhexify_alloc( hex, &data_size ); TEST_ASSERT( data != NULL ); @@ -81,6 +82,13 @@ void import_export( char *hex, int type_arg, } TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); + psa_key_policy_init( &policy ); + + psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, + PSA_ALG_VENDOR_FLAG ); + + TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); + /* Import the key */ TEST_ASSERT( psa_import_key( slot, type, data, data_size ) == PSA_SUCCESS ); @@ -107,6 +115,8 @@ void import_export( char *hex, int type_arg, } else { + TEST_ASSERT( psa_set_key_policy( slot2, &policy ) == PSA_SUCCESS ); + TEST_ASSERT( psa_import_key( slot2, type, exported, export_size ) == PSA_SUCCESS ); @@ -276,6 +286,7 @@ void sign_deterministic( int key_type_arg, char *key_hex, 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 ); @@ -286,6 +297,12 @@ void sign_deterministic( int key_type_arg, char *key_hex, 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, @@ -331,6 +348,7 @@ void sign_fail( int key_type_arg, char *key_hex, 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 ); @@ -341,6 +359,12 @@ void sign_fail( int key_type_arg, char *key_hex, 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 ); From 4eed75790105950feb79779c6214719323711edf Mon Sep 17 00:00:00 2001 From: mohammad1603 Date: Wed, 28 Mar 2018 05:14:59 -0700 Subject: [PATCH 4/8] add new test scenarios --- library/psa_crypto.c | 7 +-- tests/suites/test_suite_psa_crypto.data | 6 +++ tests/suites/test_suite_psa_crypto.function | 54 ++++++++++++++++++++- 3 files changed, 61 insertions(+), 6 deletions(-) diff --git a/library/psa_crypto.c b/library/psa_crypto.c index a12b45400..9fd0a61e2 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -1279,11 +1279,8 @@ void psa_key_policy_set_usage(psa_key_policy_t *policy, psa_key_usage_t usage, psa_algorithm_t alg) { - if( policy != NULL ) - { - policy->usage = usage; - policy->alg = alg; - } + policy->usage = usage; + policy->alg = alg; } psa_key_usage_t psa_key_policy_get_usage(psa_key_policy_t *policy) diff --git a/tests/suites/test_suite_psa_crypto.data b/tests/suites/test_suite_psa_crypto.data index c1261bc12..2c2be2116 100644 --- a/tests/suites/test_suite_psa_crypto.data +++ b/tests/suites/test_suite_psa_crypto.data @@ -86,3 +86,9 @@ sign_fail:PSA_KEY_TYPE_RSA_KEYPAIR:"3082025e02010002818100af057d396ee84fb75fdbb5 PSA Key Policy set and get key_policy:PSA_KEY_USAGE_ENCRYPT:PSA_ALG_CBC_BASE + +PSA Key Policy enforcment - export +key_policy_fail:PSA_KEY_USAGE_EXPORT:PSA_ALG_RSA_PKCS1V15_RAW:PSA_ERROR_NOT_PERMITTED:"3082025e02010002818100af057d396ee84fb75fdbb5c2b13c7fe5a654aa8aa2470b541ee1feb0b12d25c79711531249e1129628042dbbb6c120d1443524ef4c0e6e1d8956eeb2077af12349ddeee54483bc06c2c61948cd02b202e796aebd94d3a7cbf859c2c1819c324cb82b9cd34ede263a2abffe4733f077869e8660f7d6834da53d690ef7985f6bc3020301000102818100874bf0ffc2f2a71d14671ddd0171c954d7fdbf50281e4f6d99ea0e1ebcf82faa58e7b595ffb293d1abe17f110b37c48cc0f36c37e84d876621d327f64bbe08457d3ec4098ba2fa0a319fba411c2841ed7be83196a8cdf9daa5d00694bc335fc4c32217fe0488bce9cb7202e59468b1ead119000477db2ca797fac19eda3f58c1024100e2ab760841bb9d30a81d222de1eb7381d82214407f1b975cbbfe4e1a9467fd98adbd78f607836ca5be1928b9d160d97fd45c12d6b52e2c9871a174c66b488113024100c5ab27602159ae7d6f20c3c2ee851e46dc112e689e28d5fcbbf990a99ef8a90b8bb44fd36467e7fc1789ceb663abda338652c3c73f111774902e840565927091024100b6cdbd354f7df579a63b48b3643e353b84898777b48b15f94e0bfc0567a6ae5911d57ad6409cf7647bf96264e9bd87eb95e263b7110b9a1f9f94acced0fafa4d024071195eec37e8d257decfc672b07ae639f10cbb9b0c739d0c809968d644a94e3fd6ed9287077a14583f379058f76a8aecd43c62dc8c0f41766650d725275ac4a1024100bb32d133edc2e048d463388b7be9cb4be29f4b6250be603e70e3647501c97ddde20a4e71be95fd5e71784e25aca4baf25be5738aae59bbfe1c997781447a2b24" + +PSA Key Policy enforcment - sign +key_policy_fail:PSA_KEY_USAGE_SIGN:PSA_ALG_CBC_BASE:PSA_ERROR_NOT_PERMITTED:"" diff --git a/tests/suites/test_suite_psa_crypto.function b/tests/suites/test_suite_psa_crypto.function index 653467b34..b0cfe20bb 100644 --- a/tests/suites/test_suite_psa_crypto.function +++ b/tests/suites/test_suite_psa_crypto.function @@ -389,7 +389,7 @@ exit: void key_policy( int usage_arg, int alg_arg ) { int key_slot = 1; - psa_key_type_t key_type = PSA_ALG_CBC_BASE; + 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}; @@ -423,3 +423,55 @@ exit: } /* END_CASE */ +/* BEGIN_CASE */ +void key_policy_fail( int usage_arg, int alg_arg, int expected_status, char *key_hex ) +{ + int key_slot = 1; + psa_key_type_t key_type = PSA_KEY_TYPE_AES; + unsigned char key[32] = {0}; + 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; + + memset( key, 0x2a, sizeof( key ) ); + + 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 ); + + switch( usage_arg ) + { + case PSA_KEY_USAGE_EXPORT: + keypair = unhexify_alloc( key_hex, &key_size ); + TEST_ASSERT( keypair != NULL ); + key_type = PSA_KEY_TYPE_RSA_KEYPAIR; + TEST_ASSERT( psa_import_key( key_slot, key_type, + 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 ); + break; + + case PSA_KEY_USAGE_SIGN: + key_type = PSA_KEY_TYPE_AES; + TEST_ASSERT( psa_import_key( key_slot, key_type, + key, sizeof( key ) ) == PSA_SUCCESS ); + actual_status = psa_export_key( key_slot, NULL, 0, NULL ); + break; + default: + break; + } + + TEST_ASSERT( actual_status == expected_status ); + +exit: + psa_destroy_key( key_slot ); + mbedtls_psa_crypto_free( ); +} +/* END_CASE */ From 6df908f23456dcf1f5c5ecb1cdc3df45035cbc15 Mon Sep 17 00:00:00 2001 From: mohammad1603 Date: Mon, 2 Apr 2018 08:34:15 -0700 Subject: [PATCH 5/8] Add static internal MAC finish function add new psa_mac_finish_internal() to be called by psa_mac_finish() and psa_mac_verify() in order to be able to check key usage separatly. --- include/psa/crypto.h | 4 -- include/psa/crypto_struct.h | 2 + library/psa_crypto.c | 56 +++++++++++++-------- tests/suites/test_suite_psa_crypto.data | 6 +-- tests/suites/test_suite_psa_crypto.function | 43 ++++++++-------- 5 files changed, 61 insertions(+), 50 deletions(-) diff --git a/include/psa/crypto.h b/include/psa/crypto.h index 687a3499f..e8bea076d 100644 --- a/include/psa/crypto.h +++ b/include/psa/crypto.h @@ -491,10 +491,6 @@ psa_status_t psa_export_public_key(psa_key_slot_t key, /** \brief Encoding of permitted usage on a key. */ typedef uint32_t psa_key_usage_t; -/** An invalid key usage value. - * */ -#define PSA_KEY_USAGE_NONE ((psa_key_usage_t)0x00000000) - /** Whether the key may be exported. * * A public key or the public part of a key pair may always be exported diff --git a/include/psa/crypto_struct.h b/include/psa/crypto_struct.h index 898784013..eba4862c6 100644 --- a/include/psa/crypto_struct.h +++ b/include/psa/crypto_struct.h @@ -82,6 +82,8 @@ struct psa_mac_operation_s int iv_required : 1; int iv_set : 1; int has_input : 1; + int key_usage_sign : 1; + int key_usage_verify : 1; uint8_t mac_size; union { diff --git a/library/psa_crypto.c b/library/psa_crypto.c index 9fd0a61e2..2391006f0 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -986,6 +986,12 @@ 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 ) + operation->key_usage_sign = 1; + + if ( ( slot->policy.usage & PSA_KEY_USAGE_VERIFY ) != 0 ) + operation->key_usage_verify = 1; + if( ! PSA_ALG_IS_HMAC( alg ) ) { cipher_info = mbedtls_cipher_info_from_psa( alg, key_type, key_bits ); @@ -1084,7 +1090,7 @@ psa_status_t psa_mac_update( psa_mac_operation_t *operation, return( mbedtls_to_psa_error( ret ) ); } -psa_status_t psa_mac_finish( psa_mac_operation_t *operation, +static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation, uint8_t *mac, size_t mac_size, size_t *mac_length ) @@ -1136,6 +1142,17 @@ psa_status_t psa_mac_finish( psa_mac_operation_t *operation, } } +psa_status_t psa_mac_finish( psa_mac_operation_t *operation, + uint8_t *mac, + size_t mac_size, + size_t *mac_length ) +{ + if( !( operation->key_usage_sign ) ) + return( PSA_ERROR_NOT_PERMITTED ); + + return( psa_mac_finish_internal(operation, mac, mac_size, mac_length ) ); +} + #define MBEDTLS_PSA_MAC_MAX_SIZE \ ( MBEDTLS_MD_MAX_SIZE > MBEDTLS_MAX_BLOCK_LENGTH ? \ MBEDTLS_MD_MAX_SIZE : \ @@ -1146,9 +1163,14 @@ psa_status_t psa_mac_verify( psa_mac_operation_t *operation, { uint8_t actual_mac[MBEDTLS_PSA_MAC_MAX_SIZE]; size_t actual_mac_length; - psa_status_t status = psa_mac_finish( operation, - actual_mac, sizeof( actual_mac ), - &actual_mac_length ); + psa_status_t status; + + if( !( operation->key_usage_verify ) ) + return( PSA_ERROR_NOT_PERMITTED ); + + status = psa_mac_finish_internal( operation, + actual_mac, sizeof( actual_mac ), + &actual_mac_length ); if( status != PSA_SUCCESS ) return( status ); if( actual_mac_length != mac_length ) @@ -1272,7 +1294,7 @@ psa_status_t psa_asymmetric_sign(psa_key_slot_t key, void psa_key_policy_init(psa_key_policy_t *policy) { - mbedtls_zeroize( policy, sizeof( policy ) ); + memset( policy, 0, sizeof( psa_key_policy_t ) ); } void psa_key_policy_set_usage(psa_key_policy_t *policy, @@ -1285,19 +1307,18 @@ void psa_key_policy_set_usage(psa_key_policy_t *policy, psa_key_usage_t psa_key_policy_get_usage(psa_key_policy_t *policy) { - return policy->usage; + return( policy->usage ); } psa_algorithm_t psa_key_policy_get_algorithm(psa_key_policy_t *policy) { - return policy->alg; + return( policy->alg ); } psa_status_t psa_set_key_policy(psa_key_slot_t key, const psa_key_policy_t *policy) { key_slot_t *slot; - psa_key_usage_t usage = PSA_KEY_USAGE_NONE; if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT || policy == NULL ) return( PSA_ERROR_INVALID_ARGUMENT ); @@ -1306,20 +1327,12 @@ psa_status_t psa_set_key_policy(psa_key_slot_t key, if( slot->type != PSA_KEY_TYPE_NONE ) return( PSA_ERROR_OCCUPIED_SLOT ); - usage |= policy->usage & PSA_KEY_USAGE_EXPORT; - usage |= policy->usage & PSA_KEY_USAGE_ENCRYPT; - usage |= policy->usage & PSA_KEY_USAGE_DECRYPT; - usage |= policy->usage & PSA_KEY_USAGE_SIGN; - usage |= policy->usage & PSA_KEY_USAGE_VERIFY; - - if( usage == PSA_KEY_USAGE_NONE ) - { + if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_ENCRYPT + | PSA_KEY_USAGE_DECRYPT | PSA_KEY_USAGE_SIGN + | PSA_KEY_USAGE_VERIFY ) ) != 0 ) return( PSA_ERROR_INVALID_KEY_POLICY ); - } - //TODO: is there any check over the algorithm before setting the policy? - slot->policy.usage = policy->usage; - slot->policy.alg = policy->alg; + slot->policy = *policy; return( PSA_SUCCESS ); } @@ -1336,8 +1349,7 @@ psa_status_t psa_get_key_policy(psa_key_slot_t key, if( slot->type == PSA_KEY_TYPE_NONE ) return( PSA_ERROR_EMPTY_SLOT ); - policy->usage = slot->policy.usage; - policy->alg = slot->policy.alg; + *policy = slot->policy; return( PSA_SUCCESS ); } diff --git a/tests/suites/test_suite_psa_crypto.data b/tests/suites/test_suite_psa_crypto.data index 2c2be2116..da01e2381 100644 --- a/tests/suites/test_suite_psa_crypto.data +++ b/tests/suites/test_suite_psa_crypto.data @@ -87,8 +87,8 @@ sign_fail:PSA_KEY_TYPE_RSA_KEYPAIR:"3082025e02010002818100af057d396ee84fb75fdbb5 PSA Key Policy set and get key_policy:PSA_KEY_USAGE_ENCRYPT:PSA_ALG_CBC_BASE -PSA Key Policy enforcment - export +PSA Key Policy enforcement - export key_policy_fail:PSA_KEY_USAGE_EXPORT:PSA_ALG_RSA_PKCS1V15_RAW:PSA_ERROR_NOT_PERMITTED:"3082025e02010002818100af057d396ee84fb75fdbb5c2b13c7fe5a654aa8aa2470b541ee1feb0b12d25c79711531249e1129628042dbbb6c120d1443524ef4c0e6e1d8956eeb2077af12349ddeee54483bc06c2c61948cd02b202e796aebd94d3a7cbf859c2c1819c324cb82b9cd34ede263a2abffe4733f077869e8660f7d6834da53d690ef7985f6bc3020301000102818100874bf0ffc2f2a71d14671ddd0171c954d7fdbf50281e4f6d99ea0e1ebcf82faa58e7b595ffb293d1abe17f110b37c48cc0f36c37e84d876621d327f64bbe08457d3ec4098ba2fa0a319fba411c2841ed7be83196a8cdf9daa5d00694bc335fc4c32217fe0488bce9cb7202e59468b1ead119000477db2ca797fac19eda3f58c1024100e2ab760841bb9d30a81d222de1eb7381d82214407f1b975cbbfe4e1a9467fd98adbd78f607836ca5be1928b9d160d97fd45c12d6b52e2c9871a174c66b488113024100c5ab27602159ae7d6f20c3c2ee851e46dc112e689e28d5fcbbf990a99ef8a90b8bb44fd36467e7fc1789ceb663abda338652c3c73f111774902e840565927091024100b6cdbd354f7df579a63b48b3643e353b84898777b48b15f94e0bfc0567a6ae5911d57ad6409cf7647bf96264e9bd87eb95e263b7110b9a1f9f94acced0fafa4d024071195eec37e8d257decfc672b07ae639f10cbb9b0c739d0c809968d644a94e3fd6ed9287077a14583f379058f76a8aecd43c62dc8c0f41766650d725275ac4a1024100bb32d133edc2e048d463388b7be9cb4be29f4b6250be603e70e3647501c97ddde20a4e71be95fd5e71784e25aca4baf25be5738aae59bbfe1c997781447a2b24" -PSA Key Policy enforcment - sign -key_policy_fail:PSA_KEY_USAGE_SIGN:PSA_ALG_CBC_BASE:PSA_ERROR_NOT_PERMITTED:"" +PSA Key Policy enforcement - sign +key_policy_fail:PSA_KEY_USAGE_SIGN:PSA_ALG_RSA_PKCS1V15_RAW:PSA_ERROR_NOT_PERMITTED:"3082025e02010002818100af057d396ee84fb75fdbb5c2b13c7fe5a654aa8aa2470b541ee1feb0b12d25c79711531249e1129628042dbbb6c120d1443524ef4c0e6e1d8956eeb2077af12349ddeee54483bc06c2c61948cd02b202e796aebd94d3a7cbf859c2c1819c324cb82b9cd34ede263a2abffe4733f077869e8660f7d6834da53d690ef7985f6bc3020301000102818100874bf0ffc2f2a71d14671ddd0171c954d7fdbf50281e4f6d99ea0e1ebcf82faa58e7b595ffb293d1abe17f110b37c48cc0f36c37e84d876621d327f64bbe08457d3ec4098ba2fa0a319fba411c2841ed7be83196a8cdf9daa5d00694bc335fc4c32217fe0488bce9cb7202e59468b1ead119000477db2ca797fac19eda3f58c1024100e2ab760841bb9d30a81d222de1eb7381d82214407f1b975cbbfe4e1a9467fd98adbd78f607836ca5be1928b9d160d97fd45c12d6b52e2c9871a174c66b488113024100c5ab27602159ae7d6f20c3c2ee851e46dc112e689e28d5fcbbf990a99ef8a90b8bb44fd36467e7fc1789ceb663abda338652c3c73f111774902e840565927091024100b6cdbd354f7df579a63b48b3643e353b84898777b48b15f94e0bfc0567a6ae5911d57ad6409cf7647bf96264e9bd87eb95e263b7110b9a1f9f94acced0fafa4d024071195eec37e8d257decfc672b07ae639f10cbb9b0c739d0c809968d644a94e3fd6ed9287077a14583f379058f76a8aecd43c62dc8c0f41766650d725275ac4a1024100bb32d133edc2e048d463388b7be9cb4be29f4b6250be603e70e3647501c97ddde20a4e71be95fd5e71784e25aca4baf25be5738aae59bbfe1c997781447a2b24" diff --git a/tests/suites/test_suite_psa_crypto.function b/tests/suites/test_suite_psa_crypto.function index b0cfe20bb..bda2e7cea 100644 --- a/tests/suites/test_suite_psa_crypto.function +++ b/tests/suites/test_suite_psa_crypto.function @@ -221,6 +221,7 @@ void mac_verify( int key_type_arg, char *key_hex, unsigned char *expected_mac = NULL; size_t expected_mac_size; psa_mac_operation_t operation; + psa_key_policy_t policy; key = unhexify_alloc( key_hex, &key_size ); TEST_ASSERT( key != NULL ); @@ -236,6 +237,12 @@ void mac_verify( int key_type_arg, char *key_hex, TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); + psa_key_policy_init( &policy ); + + psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg_arg ); + + TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS ); + TEST_ASSERT( psa_import_key( key_slot, key_type, key, key_size ) == PSA_SUCCESS ); // TODO: support IV @@ -427,7 +434,6 @@ exit: void key_policy_fail( int usage_arg, int alg_arg, int expected_status, char *key_hex ) { int key_slot = 1; - psa_key_type_t key_type = PSA_KEY_TYPE_AES; unsigned char key[32] = {0}; unsigned char* keypair = NULL; size_t key_size = 0; @@ -445,27 +451,22 @@ void key_policy_fail( int usage_arg, int alg_arg, int expected_status, char *key TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS ); - switch( usage_arg ) + if( usage_arg & PSA_KEY_USAGE_EXPORT ) { - case PSA_KEY_USAGE_EXPORT: - keypair = unhexify_alloc( key_hex, &key_size ); - TEST_ASSERT( keypair != NULL ); - key_type = PSA_KEY_TYPE_RSA_KEYPAIR; - TEST_ASSERT( psa_import_key( key_slot, key_type, - 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 ); - break; - - case PSA_KEY_USAGE_SIGN: - key_type = PSA_KEY_TYPE_AES; - TEST_ASSERT( psa_import_key( key_slot, key_type, - key, sizeof( key ) ) == PSA_SUCCESS ); - actual_status = psa_export_key( key_slot, NULL, 0, NULL ); - break; - default: - break; + 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 ) + { + TEST_ASSERT( psa_import_key( key_slot, PSA_KEY_TYPE_RSA_KEYPAIR, + key, sizeof( key ) ) == PSA_SUCCESS ); + actual_status = psa_export_key( key_slot, NULL, 0, NULL ); } TEST_ASSERT( actual_status == expected_status ); From d926b880852490f791f75e6e161f6ad9c4fbc2bd Mon Sep 17 00:00:00 2001 From: mohammad1603 Date: Mon, 16 Apr 2018 01:53:20 -0700 Subject: [PATCH 6/8] Fix Policy enforcement sign test Fix Policy sign scenario for enforcement test --- tests/suites/test_suite_psa_crypto.function | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/suites/test_suite_psa_crypto.function b/tests/suites/test_suite_psa_crypto.function index bda2e7cea..ae5401a1c 100644 --- a/tests/suites/test_suite_psa_crypto.function +++ b/tests/suites/test_suite_psa_crypto.function @@ -434,14 +434,11 @@ exit: void key_policy_fail( int usage_arg, int alg_arg, int expected_status, char *key_hex ) { int key_slot = 1; - unsigned char key[32] = {0}; 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; - - memset( key, 0x2a, sizeof( key ) ); TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); @@ -464,8 +461,10 @@ void key_policy_fail( int usage_arg, int alg_arg, int expected_status, char *key 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, - key, sizeof( key ) ) == PSA_SUCCESS ); + keypair, key_size ) == PSA_SUCCESS ); actual_status = psa_export_key( key_slot, NULL, 0, NULL ); } @@ -473,6 +472,7 @@ void key_policy_fail( int usage_arg, int alg_arg, int expected_status, char *key exit: psa_destroy_key( key_slot ); + mbedtls_free( keypair ); mbedtls_psa_crypto_free( ); } /* END_CASE */ From 5feda72d7a4c3773833d46c1ea6268a5e00ff8be Mon Sep 17 00:00:00 2001 From: mohammad1603 Date: Mon, 16 Apr 2018 04:38:57 -0700 Subject: [PATCH 7/8] Remove usage of PSA_ERROR_INVALID_KEY_POLICY use PSA_ERROR_INVALID_ARGUMENT instead of INVALID_KEY_POLICY error --- include/psa/crypto.h | 2 -- library/psa_crypto.c | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/include/psa/crypto.h b/include/psa/crypto.h index e8bea076d..e8b22e0f5 100644 --- a/include/psa/crypto.h +++ b/include/psa/crypto.h @@ -89,8 +89,6 @@ typedef enum { PSA_ERROR_INVALID_SIGNATURE, /** The decrypted padding is incorrect. */ PSA_ERROR_INVALID_PADDING, - /** The key policy is incorrect. */ - PSA_ERROR_INVALID_KEY_POLICY, /** An error occurred that does not correspond to any defined failure cause. */ PSA_ERROR_UNKNOWN_ERROR, diff --git a/library/psa_crypto.c b/library/psa_crypto.c index 2391006f0..c516e38af 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -1330,7 +1330,7 @@ psa_status_t psa_set_key_policy(psa_key_slot_t key, if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT | PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY ) ) != 0 ) - return( PSA_ERROR_INVALID_KEY_POLICY ); + return( PSA_ERROR_INVALID_ARGUMENT ); slot->policy = *policy; From 38a622b68ba4734b5b7c5c4da3b6c193c5d7dee4 Mon Sep 17 00:00:00 2001 From: mohammad1603 Date: Tue, 17 Apr 2018 03:27:53 -0700 Subject: [PATCH 8/8] Function psa_get_key_policy() now return policy value for empty slots Function psa_get_key_policy() now return policy value for empty slots --- library/psa_crypto.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/library/psa_crypto.c b/library/psa_crypto.c index c516e38af..a25362224 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -1346,8 +1346,6 @@ psa_status_t psa_get_key_policy(psa_key_slot_t key, return( PSA_ERROR_INVALID_ARGUMENT ); slot = &global_data.key_slots[key]; - if( slot->type == PSA_KEY_TYPE_NONE ) - return( PSA_ERROR_EMPTY_SLOT ); *policy = slot->policy;