Skip to content

Commit 85485c7

Browse files
Always gather MBEDTLS_ENTROPY_BLOCK_SIZE bytes of entropy
mbedtls_entropy_func returns up to MBEDTLS_ENTROPY_BLOCK_SIZE bytes. This is the output of a hash function and does not indicate how many bytes of entropy went into the hash computation. Enforce that mbedtls_entropy_func gathers a total of MBEDTLS_ENTROPY_BLOCK_SIZE bytes or more from strong sources. Weak sources don't count for this calculation. This is complementary to the per-source threshold mechanism. In particular, we define system sources with a threshold of 32. But when using SHA-512 for the entropy accumulator, MBEDTLS_ENTROPY_BLOCK_SIZE = 64, so users can expect 64 bytes' worth of entropy. Before, you only got 64 bytes of entropy if there were two sources. Now you get 64 bytes of entropy even with a single source with a threshold of 32.
1 parent 65fc068 commit 85485c7

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

library/entropy.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,8 @@ int mbedtls_entropy_gather( mbedtls_entropy_context *ctx )
325325

326326
int mbedtls_entropy_func( void *data, unsigned char *output, size_t len )
327327
{
328-
int ret, count = 0, i, done;
328+
int ret, count = 0, i, thresholds_reached;
329+
size_t strong_size;
329330
mbedtls_entropy_context *ctx = (mbedtls_entropy_context *) data;
330331
unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
331332

@@ -363,12 +364,17 @@ int mbedtls_entropy_func( void *data, unsigned char *output, size_t len )
363364
if( ( ret = entropy_gather_internal( ctx ) ) != 0 )
364365
goto exit;
365366

366-
done = 1;
367+
thresholds_reached = 1;
368+
strong_size = 0;
367369
for( i = 0; i < ctx->source_count; i++ )
370+
{
368371
if( ctx->source[i].size < ctx->source[i].threshold )
369-
done = 0;
372+
thresholds_reached = 0;
373+
if( ctx->source[i].strong == MBEDTLS_ENTROPY_SOURCE_STRONG )
374+
strong_size += ctx->source[i].size;
375+
}
370376
}
371-
while( ! done );
377+
while( ! thresholds_reached || strong_size < MBEDTLS_ENTROPY_BLOCK_SIZE );
372378

373379
memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
374380

0 commit comments

Comments
 (0)