Skip to content

Commit 5ad685f

Browse files
CTR_DRBG: support set_entropy_len() before seed()
mbedtls_ctr_drbg_seed() always set the entropy length to the default, so a call to mbedtls_ctr_drbg_set_entropy_len() before seed() had no effect. Change this to the more intuitive behavior that set_entropy_len() sets the entropy length and seed() respects that and only uses the default entropy length if there was no call to set_entropy_len(). The former test-only function mbedtls_ctr_drbg_seed_entropy_len() is no longer used, but keep it for strict ABI compatibility.
1 parent ef25599 commit 5ad685f

File tree

3 files changed

+38
-31
lines changed

3 files changed

+38
-31
lines changed

include/mbedtls/ctr_drbg.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,8 @@ void mbedtls_ctr_drbg_init( mbedtls_ctr_drbg_context *ctx );
190190
* with mbedtls_entropy_init() (which registers the platform's default
191191
* entropy sources).
192192
*
193-
* \p f_entropy is always called with a buffer size equal to the entropy
194-
* length. The entropy length is initially #MBEDTLS_CTR_DRBG_ENTROPY_LEN
195-
* and this value is always used for the initial seeding. You can change
196-
* the entropy length for subsequent seeding by calling
197-
* mbedtls_ctr_drbg_set_entropy_len() after this function.
193+
* The entropy length is #MBEDTLS_CTR_DRBG_ENTROPY_LEN by default.
194+
* You can override it by calling mbedtls_ctr_drbg_set_entropy_len().
198195
*
199196
* You can provide a personalization string in addition to the
200197
* entropy source, to make this instantiation as unique as possible.
@@ -230,6 +227,8 @@ void mbedtls_ctr_drbg_init( mbedtls_ctr_drbg_context *ctx );
230227
* \param f_entropy The entropy callback, taking as arguments the
231228
* \p p_entropy context, the buffer to fill, and the
232229
* length of the buffer.
230+
* \p f_entropy is always called with a buffer size
231+
* equal to the entropy length.
233232
* \param p_entropy The entropy context to pass to \p f_entropy.
234233
* \param custom The personalization string.
235234
* This can be \c NULL, in which case the personalization
@@ -273,7 +272,7 @@ void mbedtls_ctr_drbg_set_prediction_resistance( mbedtls_ctr_drbg_context *ctx,
273272

274273
/**
275274
* \brief This function sets the amount of entropy grabbed on each
276-
* subsequent reseed.
275+
* seed or reseed.
277276
*
278277
* The default value is #MBEDTLS_CTR_DRBG_ENTROPY_LEN.
279278
*

library/ctr_drbg.c

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -336,17 +336,11 @@ int mbedtls_ctr_drbg_reseed( mbedtls_ctr_drbg_context *ctx,
336336
return( ret );
337337
}
338338

339-
/*
340-
* Non-public function wrapped by mbedtls_ctr_drbg_seed(). Necessary to allow
341-
* NIST tests to succeed (which require known length fixed entropy)
342-
*/
343-
int mbedtls_ctr_drbg_seed_entropy_len(
344-
mbedtls_ctr_drbg_context *ctx,
345-
int (*f_entropy)(void *, unsigned char *, size_t),
346-
void *p_entropy,
347-
const unsigned char *custom,
348-
size_t len,
349-
size_t entropy_len )
339+
int mbedtls_ctr_drbg_seed( mbedtls_ctr_drbg_context *ctx,
340+
int (*f_entropy)(void *, unsigned char *, size_t),
341+
void *p_entropy,
342+
const unsigned char *custom,
343+
size_t len )
350344
{
351345
int ret;
352346
unsigned char key[MBEDTLS_CTR_DRBG_KEYSIZE];
@@ -358,7 +352,8 @@ int mbedtls_ctr_drbg_seed_entropy_len(
358352
ctx->f_entropy = f_entropy;
359353
ctx->p_entropy = p_entropy;
360354

361-
ctx->entropy_len = entropy_len;
355+
if( ctx->entropy_len == 0 )
356+
ctx->entropy_len = MBEDTLS_CTR_DRBG_ENTROPY_LEN;
362357
ctx->reseed_interval = MBEDTLS_CTR_DRBG_RESEED_INTERVAL;
363358

364359
/*
@@ -376,14 +371,15 @@ int mbedtls_ctr_drbg_seed_entropy_len(
376371
return( 0 );
377372
}
378373

379-
int mbedtls_ctr_drbg_seed( mbedtls_ctr_drbg_context *ctx,
380-
int (*f_entropy)(void *, unsigned char *, size_t),
381-
void *p_entropy,
382-
const unsigned char *custom,
383-
size_t len )
374+
/* Backward compatibility wrapper */
375+
int mbedtls_ctr_drbg_seed_entropy_len(
376+
mbedtls_ctr_drbg_context *ctx,
377+
int (*f_entropy)(void *, unsigned char *, size_t), void *p_entropy,
378+
const unsigned char *custom, size_t len,
379+
size_t entropy_len )
384380
{
385-
return( mbedtls_ctr_drbg_seed_entropy_len( ctx, f_entropy, p_entropy, custom, len,
386-
MBEDTLS_CTR_DRBG_ENTROPY_LEN ) );
381+
mbedtls_ctr_drbg_set_entropy_len( ctx, entropy_len );
382+
return( mbedtls_ctr_drbg_seed( ctx, f_entropy, p_entropy, custom, len ) );
387383
}
388384

389385
int mbedtls_ctr_drbg_random_with_add( void *p_rng,
@@ -617,8 +613,11 @@ int mbedtls_ctr_drbg_self_test( int verbose )
617613
mbedtls_printf( " CTR_DRBG (PR = TRUE) : " );
618614

619615
test_offset = 0;
620-
CHK( mbedtls_ctr_drbg_seed_entropy_len( &ctx, ctr_drbg_self_test_entropy,
621-
(void *) entropy_source_pr, nonce_pers_pr, 16, 32 ) );
616+
mbedtls_ctr_drbg_set_entropy_len( &ctx, 32 );
617+
CHK( mbedtls_ctr_drbg_seed( &ctx,
618+
ctr_drbg_self_test_entropy,
619+
(void *) entropy_source_pr,
620+
nonce_pers_pr, 16 ) );
622621
mbedtls_ctr_drbg_set_prediction_resistance( &ctx, MBEDTLS_CTR_DRBG_PR_ON );
623622
CHK( mbedtls_ctr_drbg_random( &ctx, buf, MBEDTLS_CTR_DRBG_BLOCKSIZE ) );
624623
CHK( mbedtls_ctr_drbg_random( &ctx, buf, MBEDTLS_CTR_DRBG_BLOCKSIZE ) );
@@ -638,8 +637,11 @@ int mbedtls_ctr_drbg_self_test( int verbose )
638637
mbedtls_ctr_drbg_init( &ctx );
639638

640639
test_offset = 0;
641-
CHK( mbedtls_ctr_drbg_seed_entropy_len( &ctx, ctr_drbg_self_test_entropy,
642-
(void *) entropy_source_nopr, nonce_pers_nopr, 16, 32 ) );
640+
mbedtls_ctr_drbg_set_entropy_len( &ctx, 32 );
641+
CHK( mbedtls_ctr_drbg_seed( &ctx,
642+
ctr_drbg_self_test_entropy,
643+
(void *) entropy_source_nopr,
644+
nonce_pers_nopr, 16 ) );
643645
CHK( mbedtls_ctr_drbg_random( &ctx, buf, 16 ) );
644646
CHK( mbedtls_ctr_drbg_reseed( &ctx, NULL, 0 ) );
645647
CHK( mbedtls_ctr_drbg_random( &ctx, buf, 16 ) );

tests/suites/test_suite_ctr_drbg.function

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ void ctr_drbg_validate_pr( char *add_init_string, char *entropy_string,
7272
add2_len = unhexify( add2, add2_string );
7373

7474
test_offset_idx = 0;
75-
TEST_ASSERT( mbedtls_ctr_drbg_seed_entropy_len( &ctx, mbedtls_test_entropy_func, entropy, add_init, add_init_len, 32 ) == 0 );
75+
mbedtls_ctr_drbg_set_entropy_len( &ctx, 32 );
76+
TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctx,
77+
mbedtls_test_entropy_func, entropy,
78+
add_init, add_init_len ) == 0 );
7679
mbedtls_ctr_drbg_set_prediction_resistance( &ctx, MBEDTLS_CTR_DRBG_PR_ON );
7780

7881
TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, buf, 16, add1, add1_len ) == 0 );
@@ -110,7 +113,10 @@ void ctr_drbg_validate_nopr( char *add_init_string, char *entropy_string,
110113
add2_len = unhexify( add2, add2_string );
111114

112115
test_offset_idx = 0;
113-
TEST_ASSERT( mbedtls_ctr_drbg_seed_entropy_len( &ctx, mbedtls_test_entropy_func, entropy, add_init, add_init_len, 32 ) == 0 );
116+
mbedtls_ctr_drbg_set_entropy_len( &ctx, 32 );
117+
TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctx,
118+
mbedtls_test_entropy_func, entropy,
119+
add_init, add_init_len ) == 0 );
114120

115121
TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, buf, 16, add1, add1_len ) == 0 );
116122
TEST_ASSERT( mbedtls_ctr_drbg_reseed( &ctx, add_reseed, add_reseed_len ) == 0 );

0 commit comments

Comments
 (0)