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.
This commit is contained in:
mohammad1603 2018-04-02 08:34:15 -07:00 committed by itayzafrir
parent 4eed757901
commit 6df908f234
5 changed files with 61 additions and 50 deletions

View File

@ -491,10 +491,6 @@ psa_status_t psa_export_public_key(psa_key_slot_t key,
/** \brief Encoding of permitted usage on a key. */ /** \brief Encoding of permitted usage on a key. */
typedef uint32_t psa_key_usage_t; 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. /** Whether the key may be exported.
* *
* A public key or the public part of a key pair may always be exported * A public key or the public part of a key pair may always be exported

View File

@ -82,6 +82,8 @@ struct psa_mac_operation_s
int iv_required : 1; int iv_required : 1;
int iv_set : 1; int iv_set : 1;
int has_input : 1; int has_input : 1;
int key_usage_sign : 1;
int key_usage_verify : 1;
uint8_t mac_size; uint8_t mac_size;
union union
{ {

View File

@ -986,6 +986,12 @@ psa_status_t psa_mac_start( psa_mac_operation_t *operation,
return( status ); return( status );
slot = &global_data.key_slots[key]; 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 ) ) if( ! PSA_ALG_IS_HMAC( alg ) )
{ {
cipher_info = mbedtls_cipher_info_from_psa( alg, key_type, key_bits ); 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 ) ); 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, uint8_t *mac,
size_t mac_size, size_t mac_size,
size_t *mac_length ) 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 \ #define MBEDTLS_PSA_MAC_MAX_SIZE \
( MBEDTLS_MD_MAX_SIZE > MBEDTLS_MAX_BLOCK_LENGTH ? \ ( MBEDTLS_MD_MAX_SIZE > MBEDTLS_MAX_BLOCK_LENGTH ? \
MBEDTLS_MD_MAX_SIZE : \ 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]; uint8_t actual_mac[MBEDTLS_PSA_MAC_MAX_SIZE];
size_t actual_mac_length; size_t actual_mac_length;
psa_status_t status = psa_mac_finish( operation, psa_status_t status;
actual_mac, sizeof( actual_mac ),
&actual_mac_length ); 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 ) if( status != PSA_SUCCESS )
return( status ); return( status );
if( actual_mac_length != mac_length ) 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) 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, 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) 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) 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, psa_status_t psa_set_key_policy(psa_key_slot_t key,
const psa_key_policy_t *policy) const psa_key_policy_t *policy)
{ {
key_slot_t *slot; key_slot_t *slot;
psa_key_usage_t usage = PSA_KEY_USAGE_NONE;
if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT || policy == NULL ) if( key == 0 || key > MBEDTLS_PSA_KEY_SLOT_COUNT || policy == NULL )
return( PSA_ERROR_INVALID_ARGUMENT ); 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 ) if( slot->type != PSA_KEY_TYPE_NONE )
return( PSA_ERROR_OCCUPIED_SLOT ); return( PSA_ERROR_OCCUPIED_SLOT );
usage |= policy->usage & PSA_KEY_USAGE_EXPORT; if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_ENCRYPT
usage |= policy->usage & PSA_KEY_USAGE_ENCRYPT; | PSA_KEY_USAGE_DECRYPT | PSA_KEY_USAGE_SIGN
usage |= policy->usage & PSA_KEY_USAGE_DECRYPT; | PSA_KEY_USAGE_VERIFY ) ) != 0 )
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 ); return( PSA_ERROR_INVALID_KEY_POLICY );
}
//TODO: is there any check over the algorithm before setting the policy? slot->policy = *policy;
slot->policy.usage = policy->usage;
slot->policy.alg = policy->alg;
return( PSA_SUCCESS ); 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 ) if( slot->type == PSA_KEY_TYPE_NONE )
return( PSA_ERROR_EMPTY_SLOT ); return( PSA_ERROR_EMPTY_SLOT );
policy->usage = slot->policy.usage; *policy = slot->policy;
policy->alg = slot->policy.alg;
return( PSA_SUCCESS ); return( PSA_SUCCESS );
} }

View File

@ -87,8 +87,8 @@ sign_fail:PSA_KEY_TYPE_RSA_KEYPAIR:"3082025e02010002818100af057d396ee84fb75fdbb5
PSA Key Policy set and get 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 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" key_policy_fail:PSA_KEY_USAGE_EXPORT:PSA_ALG_RSA_PKCS1V15_RAW:PSA_ERROR_NOT_PERMITTED:"3082025e02010002818100af057d396ee84fb75fdbb5c2b13c7fe5a654aa8aa2470b541ee1feb0b12d25c79711531249e1129628042dbbb6c120d1443524ef4c0e6e1d8956eeb2077af12349ddeee54483bc06c2c61948cd02b202e796aebd94d3a7cbf859c2c1819c324cb82b9cd34ede263a2abffe4733f077869e8660f7d6834da53d690ef7985f6bc3020301000102818100874bf0ffc2f2a71d14671ddd0171c954d7fdbf50281e4f6d99ea0e1ebcf82faa58e7b595ffb293d1abe17f110b37c48cc0f36c37e84d876621d327f64bbe08457d3ec4098ba2fa0a319fba411c2841ed7be83196a8cdf9daa5d00694bc335fc4c32217fe0488bce9cb7202e59468b1ead119000477db2ca797fac19eda3f58c1024100e2ab760841bb9d30a81d222de1eb7381d82214407f1b975cbbfe4e1a9467fd98adbd78f607836ca5be1928b9d160d97fd45c12d6b52e2c9871a174c66b488113024100c5ab27602159ae7d6f20c3c2ee851e46dc112e689e28d5fcbbf990a99ef8a90b8bb44fd36467e7fc1789ceb663abda338652c3c73f111774902e840565927091024100b6cdbd354f7df579a63b48b3643e353b84898777b48b15f94e0bfc0567a6ae5911d57ad6409cf7647bf96264e9bd87eb95e263b7110b9a1f9f94acced0fafa4d024071195eec37e8d257decfc672b07ae639f10cbb9b0c739d0c809968d644a94e3fd6ed9287077a14583f379058f76a8aecd43c62dc8c0f41766650d725275ac4a1024100bb32d133edc2e048d463388b7be9cb4be29f4b6250be603e70e3647501c97ddde20a4e71be95fd5e71784e25aca4baf25be5738aae59bbfe1c997781447a2b24"
PSA Key Policy enforcment - sign PSA Key Policy enforcement - sign
key_policy_fail:PSA_KEY_USAGE_SIGN:PSA_ALG_CBC_BASE:PSA_ERROR_NOT_PERMITTED:"" key_policy_fail:PSA_KEY_USAGE_SIGN:PSA_ALG_RSA_PKCS1V15_RAW:PSA_ERROR_NOT_PERMITTED:"3082025e02010002818100af057d396ee84fb75fdbb5c2b13c7fe5a654aa8aa2470b541ee1feb0b12d25c79711531249e1129628042dbbb6c120d1443524ef4c0e6e1d8956eeb2077af12349ddeee54483bc06c2c61948cd02b202e796aebd94d3a7cbf859c2c1819c324cb82b9cd34ede263a2abffe4733f077869e8660f7d6834da53d690ef7985f6bc3020301000102818100874bf0ffc2f2a71d14671ddd0171c954d7fdbf50281e4f6d99ea0e1ebcf82faa58e7b595ffb293d1abe17f110b37c48cc0f36c37e84d876621d327f64bbe08457d3ec4098ba2fa0a319fba411c2841ed7be83196a8cdf9daa5d00694bc335fc4c32217fe0488bce9cb7202e59468b1ead119000477db2ca797fac19eda3f58c1024100e2ab760841bb9d30a81d222de1eb7381d82214407f1b975cbbfe4e1a9467fd98adbd78f607836ca5be1928b9d160d97fd45c12d6b52e2c9871a174c66b488113024100c5ab27602159ae7d6f20c3c2ee851e46dc112e689e28d5fcbbf990a99ef8a90b8bb44fd36467e7fc1789ceb663abda338652c3c73f111774902e840565927091024100b6cdbd354f7df579a63b48b3643e353b84898777b48b15f94e0bfc0567a6ae5911d57ad6409cf7647bf96264e9bd87eb95e263b7110b9a1f9f94acced0fafa4d024071195eec37e8d257decfc672b07ae639f10cbb9b0c739d0c809968d644a94e3fd6ed9287077a14583f379058f76a8aecd43c62dc8c0f41766650d725275ac4a1024100bb32d133edc2e048d463388b7be9cb4be29f4b6250be603e70e3647501c97ddde20a4e71be95fd5e71784e25aca4baf25be5738aae59bbfe1c997781447a2b24"

View File

@ -221,6 +221,7 @@ void mac_verify( int key_type_arg, char *key_hex,
unsigned char *expected_mac = NULL; unsigned char *expected_mac = NULL;
size_t expected_mac_size; size_t expected_mac_size;
psa_mac_operation_t operation; psa_mac_operation_t operation;
psa_key_policy_t policy;
key = unhexify_alloc( key_hex, &key_size ); key = unhexify_alloc( key_hex, &key_size );
TEST_ASSERT( key != NULL ); 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 ); 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, TEST_ASSERT( psa_import_key( key_slot, key_type,
key, key_size ) == PSA_SUCCESS ); key, key_size ) == PSA_SUCCESS );
// TODO: support IV // TODO: support IV
@ -427,7 +434,6 @@ exit:
void key_policy_fail( int usage_arg, int alg_arg, int expected_status, char *key_hex ) void key_policy_fail( int usage_arg, int alg_arg, int expected_status, char *key_hex )
{ {
int key_slot = 1; int key_slot = 1;
psa_key_type_t key_type = PSA_KEY_TYPE_AES;
unsigned char key[32] = {0}; unsigned char key[32] = {0};
unsigned char* keypair = NULL; unsigned char* keypair = NULL;
size_t key_size = 0; 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 ); 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 );
keypair = unhexify_alloc( key_hex, &key_size ); TEST_ASSERT( keypair != NULL );
TEST_ASSERT( keypair != NULL ); TEST_ASSERT( psa_import_key( key_slot, PSA_KEY_TYPE_RSA_KEYPAIR,
key_type = PSA_KEY_TYPE_RSA_KEYPAIR; keypair, key_size ) == PSA_SUCCESS );
TEST_ASSERT( psa_import_key( key_slot, key_type, actual_status = psa_asymmetric_sign( key_slot,
keypair, key_size ) == PSA_SUCCESS ); ( psa_algorithm_t )alg_arg, NULL, 0, NULL, 0,
actual_status = psa_asymmetric_sign( key_slot, NULL, 0, &signature_length );
( psa_algorithm_t )alg_arg, NULL, 0, NULL, 0, }
NULL, 0, &signature_length );
break; if( usage_arg & PSA_KEY_USAGE_SIGN )
{
case PSA_KEY_USAGE_SIGN: TEST_ASSERT( psa_import_key( key_slot, PSA_KEY_TYPE_RSA_KEYPAIR,
key_type = PSA_KEY_TYPE_AES; key, sizeof( key ) ) == PSA_SUCCESS );
TEST_ASSERT( psa_import_key( key_slot, key_type, actual_status = psa_export_key( key_slot, NULL, 0, NULL );
key, sizeof( key ) ) == PSA_SUCCESS );
actual_status = psa_export_key( key_slot, NULL, 0, NULL );
break;
default:
break;
} }
TEST_ASSERT( actual_status == expected_status ); TEST_ASSERT( actual_status == expected_status );