Merge pull request #126 from ARMmbed/multi-op-abort-on-error

Multipart operations don't always call abort on error - FIX
This commit is contained in:
Gilles Peskine 2018-08-06 19:45:59 +02:00 committed by Jaeden Amero
commit 39f0c22c97

View file

@ -1110,6 +1110,7 @@ psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
size_t hash_size, size_t hash_size,
size_t *hash_length ) size_t *hash_length )
{ {
psa_status_t status;
int ret; int ret;
size_t actual_hash_length = PSA_HASH_SIZE( operation->alg ); size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
@ -1123,7 +1124,10 @@ psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
memset( hash, '!', hash_size ); memset( hash, '!', hash_size );
if( hash_size < actual_hash_length ) if( hash_size < actual_hash_length )
return( PSA_ERROR_BUFFER_TOO_SMALL ); {
status = PSA_ERROR_BUFFER_TOO_SMALL;
goto exit;
}
switch( operation->alg ) switch( operation->alg )
{ {
@ -1168,8 +1172,10 @@ psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA; ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
break; break;
} }
status = mbedtls_to_psa_error( ret );
if( ret == 0 ) exit:
if( status == PSA_SUCCESS )
{ {
*hash_length = actual_hash_length; *hash_length = actual_hash_length;
return( psa_hash_abort( operation ) ); return( psa_hash_abort( operation ) );
@ -1177,7 +1183,7 @@ psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
else else
{ {
psa_hash_abort( operation ); psa_hash_abort( operation );
return( mbedtls_to_psa_error( ret ) ); return( status );
} }
} }
@ -2472,53 +2478,59 @@ psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
size_t iv_size, size_t iv_size,
size_t *iv_length ) size_t *iv_length )
{ {
int ret = PSA_SUCCESS; psa_status_t status;
int ret;
if( operation->iv_set || ! operation->iv_required ) if( operation->iv_set || ! operation->iv_required )
return( PSA_ERROR_BAD_STATE ); {
status = PSA_ERROR_BAD_STATE;
goto exit;
}
if( iv_size < operation->iv_size ) if( iv_size < operation->iv_size )
{ {
ret = PSA_ERROR_BUFFER_TOO_SMALL; status = PSA_ERROR_BUFFER_TOO_SMALL;
goto exit; goto exit;
} }
ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
iv, operation->iv_size ); iv, operation->iv_size );
if( ret != 0 ) if( ret != 0 )
{ {
ret = mbedtls_to_psa_error( ret ); status = mbedtls_to_psa_error( ret );
goto exit; goto exit;
} }
*iv_length = operation->iv_size; *iv_length = operation->iv_size;
ret = psa_cipher_set_iv( operation, iv, *iv_length ); status = psa_cipher_set_iv( operation, iv, *iv_length );
exit: exit:
if( ret != PSA_SUCCESS ) if( status != PSA_SUCCESS )
psa_cipher_abort( operation ); psa_cipher_abort( operation );
return( ret ); return( status );
} }
psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation, psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
const unsigned char *iv, const unsigned char *iv,
size_t iv_length ) size_t iv_length )
{ {
int ret = PSA_SUCCESS; psa_status_t status;
int ret;
if( operation->iv_set || ! operation->iv_required ) if( operation->iv_set || ! operation->iv_required )
return( PSA_ERROR_BAD_STATE ); {
status = PSA_ERROR_BAD_STATE;
goto exit;
}
if( iv_length != operation->iv_size ) if( iv_length != operation->iv_size )
{ {
psa_cipher_abort( operation ); status = PSA_ERROR_INVALID_ARGUMENT;
return( PSA_ERROR_INVALID_ARGUMENT ); goto exit;
} }
ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length ); ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
if( ret != 0 ) status = mbedtls_to_psa_error( ret );
{ exit:
psa_cipher_abort( operation ); if( status == PSA_SUCCESS )
return( mbedtls_to_psa_error( ret ) );
}
operation->iv_set = 1; operation->iv_set = 1;
else
return( PSA_SUCCESS ); psa_cipher_abort( operation );
return( status );
} }
psa_status_t psa_cipher_update( psa_cipher_operation_t *operation, psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
@ -2528,7 +2540,8 @@ psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
size_t output_size, size_t output_size,
size_t *output_length ) size_t *output_length )
{ {
int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; psa_status_t status;
int ret;
size_t expected_output_size; size_t expected_output_size;
if( PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) ) if( PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
{ {
@ -2544,18 +2557,20 @@ psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
{ {
expected_output_size = input_length; expected_output_size = input_length;
} }
if( output_size < expected_output_size ) if( output_size < expected_output_size )
return( PSA_ERROR_BUFFER_TOO_SMALL ); {
status = PSA_ERROR_BUFFER_TOO_SMALL;
goto exit;
}
ret = mbedtls_cipher_update( &operation->ctx.cipher, input, ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
input_length, output, output_length ); input_length, output, output_length );
if( ret != 0 ) status = mbedtls_to_psa_error( ret );
{ exit:
if( status != PSA_SUCCESS )
psa_cipher_abort( operation ); psa_cipher_abort( operation );
return( mbedtls_to_psa_error( ret ) ); return( status );
}
return( PSA_SUCCESS );
} }
psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation, psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,