Simplify the chunk loop in psa_generate_random

Make the code slightly more readable and slightly smaller.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
This commit is contained in:
Gilles Peskine 2021-01-04 21:01:07 +01:00
parent 88fa5c463e
commit 71ddab9154

View file

@ -6394,21 +6394,18 @@ psa_status_t psa_generate_random( uint8_t *output,
#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; int ret = 0;
while( output_size > 0 )
while( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST )
{ {
ret = mbedtls_psa_get_random( size_t request_size =
MBEDTLS_PSA_RANDOM_STATE, ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
output, MBEDTLS_PSA_RANDOM_MAX_REQUEST ); MBEDTLS_PSA_RANDOM_MAX_REQUEST :
if( ret != 0 ) output_size );
return( mbedtls_to_psa_error( ret ) ); ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
output += MBEDTLS_PSA_RANDOM_MAX_REQUEST; output, request_size );
output_size -= MBEDTLS_PSA_RANDOM_MAX_REQUEST; output_size -= request_size;
output += request_size;
} }
ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
output, output_size );
return( mbedtls_to_psa_error( ret ) ); return( mbedtls_to_psa_error( ret ) );
#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
} }