psa: mac: Re-organize psa_mac_setup() internal function

Re-organize psa_mac_setup() to prepare the move
to a dedicated function of the additional checks
on the algorithm and the key attributes done by
this function. We want to move those checks in
a dedicated function to be able to do them
without duplicating them in psa_mac_compute().

Signed-off-by: Ronald Cron <ronald.cron@arm.com>
This commit is contained in:
Ronald Cron 2021-06-17 16:10:24 +02:00
parent a93e423739
commit 48f875e809

View file

@ -2308,14 +2308,12 @@ static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
psa_key_slot_t *slot; psa_key_slot_t *slot;
uint8_t mac_size;
/* A context must be freshly initialized before it can be set up. */ /* A context must be freshly initialized before it can be set up. */
if( operation->id != 0 ) if( operation->id != 0 )
return( PSA_ERROR_BAD_STATE ); return( PSA_ERROR_BAD_STATE );
if( ! PSA_ALG_IS_MAC( alg ) )
return( PSA_ERROR_INVALID_ARGUMENT );
status = psa_get_and_lock_key_slot_with_policy( status = psa_get_and_lock_key_slot_with_policy(
key, key,
&slot, &slot,
@ -2324,24 +2322,28 @@ static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
if( status != PSA_SUCCESS ) if( status != PSA_SUCCESS )
return( status ); return( status );
psa_key_attributes_t attributes = { psa_key_attributes_t key_attributes = {
.core = slot->attr .core = slot->attr
}; };
psa_key_attributes_t *attributes = &key_attributes;
if( ! PSA_ALG_IS_MAC( alg ) )
{
status = PSA_ERROR_INVALID_ARGUMENT;
goto exit;
}
/* Validate the combination of key type and algorithm */ /* Validate the combination of key type and algorithm */
status = psa_mac_key_can_do( alg, psa_get_key_type( &attributes ) ); status = psa_mac_key_can_do( alg, psa_get_key_type( attributes ) );
if( status != PSA_SUCCESS ) if( status != PSA_SUCCESS )
goto exit; goto exit;
operation->is_sign = is_sign;
/* Get the output length for the algorithm and key combination */ /* Get the output length for the algorithm and key combination */
operation->mac_size = PSA_MAC_LENGTH( mac_size = PSA_MAC_LENGTH( psa_get_key_type( attributes ),
psa_get_key_type( &attributes ), psa_get_key_bits( attributes ),
psa_get_key_bits( &attributes ), alg );
alg );
if( operation->mac_size < 4 ) if( mac_size < 4 )
{ {
/* A very short MAC is too short for security since it can be /* A very short MAC is too short for security since it can be
* brute-forced. Ancient protocols with 32-bit MACs do exist, * brute-forced. Ancient protocols with 32-bit MACs do exist,
@ -2351,9 +2353,9 @@ static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
goto exit; goto exit;
} }
if( operation->mac_size > PSA_MAC_LENGTH( psa_get_key_type( &attributes ), if( mac_size > PSA_MAC_LENGTH( psa_get_key_type( attributes ),
psa_get_key_bits( &attributes ), psa_get_key_bits( attributes ),
PSA_ALG_FULL_LENGTH_MAC( alg ) ) ) PSA_ALG_FULL_LENGTH_MAC( alg ) ) )
{ {
/* It's impossible to "truncate" to a larger length than the full length /* It's impossible to "truncate" to a larger length than the full length
* of the algorithm. */ * of the algorithm. */
@ -2361,11 +2363,14 @@ static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
goto exit; goto exit;
} }
operation->is_sign = is_sign;
operation->mac_size = mac_size;
/* Dispatch the MAC setup call with validated input */ /* Dispatch the MAC setup call with validated input */
if( is_sign ) if( is_sign )
{ {
status = psa_driver_wrapper_mac_sign_setup( operation, status = psa_driver_wrapper_mac_sign_setup( operation,
&attributes, &key_attributes,
slot->key.data, slot->key.data,
slot->key.bytes, slot->key.bytes,
alg ); alg );
@ -2373,7 +2378,7 @@ static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
else else
{ {
status = psa_driver_wrapper_mac_verify_setup( operation, status = psa_driver_wrapper_mac_verify_setup( operation,
&attributes, &key_attributes,
slot->key.data, slot->key.data,
slot->key.bytes, slot->key.bytes,
alg ); alg );