hmac_drbg: set_entropy_len can now return an error

Make mbedtls_hmac_drbg_set_entropy_len return an error
in case of a too long entropy length setting.
Signed-off-by: Andrzej Kurek <andrzej.kurek@arm.com>
This commit is contained in:
Andrzej Kurek 2020-07-18 06:05:03 -04:00
parent 9167aa96f8
commit 6bc37fa4e2
No known key found for this signature in database
GPG key ID: 89A90840DC388527
3 changed files with 11 additions and 5 deletions

View file

@ -228,9 +228,11 @@ void mbedtls_hmac_drbg_set_prediction_resistance( mbedtls_hmac_drbg_context *ctx
* *
* \param ctx The HMAC_DRBG context. * \param ctx The HMAC_DRBG context.
* \param len The amount of entropy to grab, in bytes. * \param len The amount of entropy to grab, in bytes.
*
* \return \c 0 if \p len is valid, MBEDTLS_HMAC_DRBG_MAX_INPUT otherwise.
*/ */
void mbedtls_hmac_drbg_set_entropy_len( mbedtls_hmac_drbg_context *ctx, int mbedtls_hmac_drbg_set_entropy_len( mbedtls_hmac_drbg_context *ctx,
size_t len ); size_t len );
/** /**
* \brief Set the reseed interval. * \brief Set the reseed interval.

View file

@ -390,9 +390,13 @@ void mbedtls_hmac_drbg_set_prediction_resistance( mbedtls_hmac_drbg_context *ctx
/* /*
* Set entropy length grabbed for seeding * Set entropy length grabbed for seeding
*/ */
void mbedtls_hmac_drbg_set_entropy_len( mbedtls_hmac_drbg_context *ctx, size_t len ) int mbedtls_hmac_drbg_set_entropy_len( mbedtls_hmac_drbg_context *ctx, size_t len )
{ {
if( len > MBEDTLS_HMAC_DRBG_MAX_INPUT )
return( MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG );
ctx->entropy_len = len; ctx->entropy_len = len;
return 0;
} }
/* /*

View file

@ -94,12 +94,12 @@ void hmac_drbg_entropy_usage( int md_alg )
TEST_ASSERT( entropy.len < last_len ); TEST_ASSERT( entropy.len < last_len );
/* Finally, check setting entropy_len */ /* Finally, check setting entropy_len */
mbedtls_hmac_drbg_set_entropy_len( &ctx, 42 ); TEST_ASSERT( mbedtls_hmac_drbg_set_entropy_len( &ctx, 42 ) == 0 );
last_len = entropy.len; last_len = entropy.len;
TEST_ASSERT( mbedtls_hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 ); TEST_ASSERT( mbedtls_hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
TEST_ASSERT( (int) last_len - entropy.len == 42 ); TEST_ASSERT( (int) last_len - entropy.len == 42 );
mbedtls_hmac_drbg_set_entropy_len( &ctx, 13 ); TEST_ASSERT( mbedtls_hmac_drbg_set_entropy_len( &ctx, 13 ) == 0 );
last_len = entropy.len; last_len = entropy.len;
TEST_ASSERT( mbedtls_hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 ); TEST_ASSERT( mbedtls_hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
TEST_ASSERT( (int) last_len - entropy.len == 13 ); TEST_ASSERT( (int) last_len - entropy.len == 13 );