Skip to content

Commit 50ed86b

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(). This removes the need for the test-only function mbedtls_ctr_drbg_seed_entropy_len(). Just call mbedtls_ctr_drbg_set_entropy_len() followed by mbedtls_ctr_drbg_seed(), it works now.
1 parent 8bf5613 commit 50ed86b

File tree

3 files changed

+25
-41
lines changed

3 files changed

+25
-41
lines changed

include/mbedtls/ctr_drbg.h

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,8 @@ void mbedtls_ctr_drbg_init( mbedtls_ctr_drbg_context *ctx );
214214
* with mbedtls_entropy_init() (which registers the platform's default
215215
* entropy sources).
216216
*
217-
* \p f_entropy is always called with a buffer size equal to the entropy
218-
* length. The entropy length is initially #MBEDTLS_CTR_DRBG_ENTROPY_LEN
219-
* and this value is always used for the initial seeding. You can change
220-
* the entropy length for subsequent seeding by calling
221-
* mbedtls_ctr_drbg_set_entropy_len() after this function.
217+
* The entropy length is #MBEDTLS_CTR_DRBG_ENTROPY_LEN by default.
218+
* You can override it by calling mbedtls_ctr_drbg_set_entropy_len().
222219
*
223220
* You can provide a personalization string in addition to the
224221
* entropy source, to make this instantiation as unique as possible.
@@ -255,6 +252,8 @@ void mbedtls_ctr_drbg_init( mbedtls_ctr_drbg_context *ctx );
255252
* \param f_entropy The entropy callback, taking as arguments the
256253
* \p p_entropy context, the buffer to fill, and the
257254
* length of the buffer.
255+
* \p f_entropy is always called with a buffer size
256+
* equal to the entropy length.
258257
* \param p_entropy The entropy context to pass to \p f_entropy.
259258
* \param custom The personalization string.
260259
* This can be \c NULL, in which case the personalization
@@ -298,7 +297,7 @@ void mbedtls_ctr_drbg_set_prediction_resistance( mbedtls_ctr_drbg_context *ctx,
298297

299298
/**
300299
* \brief This function sets the amount of entropy grabbed on each
301-
* subsequent reseed.
300+
* seed or reseed.
302301
*
303302
* The default value is #MBEDTLS_CTR_DRBG_ENTROPY_LEN.
304303
*
@@ -499,11 +498,6 @@ int mbedtls_ctr_drbg_self_test( int verbose );
499498

500499
#endif /* MBEDTLS_SELF_TEST */
501500

502-
/* Internal functions (do not call directly) */
503-
int mbedtls_ctr_drbg_seed_entropy_len( mbedtls_ctr_drbg_context *,
504-
int (*)(void *, unsigned char *, size_t), void *,
505-
const unsigned char *, size_t, size_t );
506-
507501
#ifdef __cplusplus
508502
}
509503
#endif

library/ctr_drbg.c

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -381,10 +381,6 @@ int mbedtls_ctr_drbg_reseed( mbedtls_ctr_drbg_context *ctx,
381381
return( ret );
382382
}
383383

384-
/*
385-
* Non-public function wrapped by mbedtls_ctr_drbg_seed(). Necessary to allow
386-
* NIST tests to succeed (which require known length fixed entropy)
387-
*/
388384
/* CTR_DRBG_Instantiate with derivation function (SP 800-90A §10.2.1.3.2)
389385
* mbedtls_ctr_drbg_seed_entropy_len(ctx, f_entropy, p_entropy,
390386
* custom, len, entropy_len)
@@ -397,13 +393,11 @@ int mbedtls_ctr_drbg_reseed( mbedtls_ctr_drbg_context *ctx,
397393
* and with outputs
398394
* ctx = initial_working_state
399395
*/
400-
int mbedtls_ctr_drbg_seed_entropy_len(
401-
mbedtls_ctr_drbg_context *ctx,
402-
int (*f_entropy)(void *, unsigned char *, size_t),
403-
void *p_entropy,
404-
const unsigned char *custom,
405-
size_t len,
406-
size_t entropy_len )
396+
int mbedtls_ctr_drbg_seed( mbedtls_ctr_drbg_context *ctx,
397+
int (*f_entropy)(void *, unsigned char *, size_t),
398+
void *p_entropy,
399+
const unsigned char *custom,
400+
size_t len )
407401
{
408402
int ret;
409403
unsigned char key[MBEDTLS_CTR_DRBG_KEYSIZE];
@@ -415,7 +409,8 @@ int mbedtls_ctr_drbg_seed_entropy_len(
415409
ctx->f_entropy = f_entropy;
416410
ctx->p_entropy = p_entropy;
417411

418-
ctx->entropy_len = entropy_len;
412+
if( ctx->entropy_len == 0 )
413+
ctx->entropy_len = MBEDTLS_CTR_DRBG_ENTROPY_LEN;
419414
ctx->reseed_interval = MBEDTLS_CTR_DRBG_RESEED_INTERVAL;
420415

421416
/*
@@ -434,17 +429,6 @@ int mbedtls_ctr_drbg_seed_entropy_len(
434429
return( 0 );
435430
}
436431

437-
int mbedtls_ctr_drbg_seed( mbedtls_ctr_drbg_context *ctx,
438-
int (*f_entropy)(void *, unsigned char *, size_t),
439-
void *p_entropy,
440-
const unsigned char *custom,
441-
size_t len )
442-
{
443-
return( mbedtls_ctr_drbg_seed_entropy_len( ctx, f_entropy, p_entropy,
444-
custom, len,
445-
MBEDTLS_CTR_DRBG_ENTROPY_LEN ) );
446-
}
447-
448432
/* CTR_DRBG_Generate with derivation function (SP 800-90A §10.2.1.5.2)
449433
* mbedtls_ctr_drbg_random_with_add(ctx, output, output_len, additional, add_len)
450434
* implements
@@ -708,8 +692,11 @@ int mbedtls_ctr_drbg_self_test( int verbose )
708692
mbedtls_printf( " CTR_DRBG (PR = TRUE) : " );
709693

710694
test_offset = 0;
711-
CHK( mbedtls_ctr_drbg_seed_entropy_len( &ctx, ctr_drbg_self_test_entropy,
712-
(void *) entropy_source_pr, nonce_pers_pr, 16, 32 ) );
695+
mbedtls_ctr_drbg_set_entropy_len( &ctx, 32 );
696+
CHK( mbedtls_ctr_drbg_seed( &ctx,
697+
ctr_drbg_self_test_entropy,
698+
(void *) entropy_source_pr,
699+
nonce_pers_pr, 16 ) );
713700
mbedtls_ctr_drbg_set_prediction_resistance( &ctx, MBEDTLS_CTR_DRBG_PR_ON );
714701
CHK( mbedtls_ctr_drbg_random( &ctx, buf, MBEDTLS_CTR_DRBG_BLOCKSIZE ) );
715702
CHK( mbedtls_ctr_drbg_random( &ctx, buf, MBEDTLS_CTR_DRBG_BLOCKSIZE ) );
@@ -729,8 +716,11 @@ int mbedtls_ctr_drbg_self_test( int verbose )
729716
mbedtls_ctr_drbg_init( &ctx );
730717

731718
test_offset = 0;
732-
CHK( mbedtls_ctr_drbg_seed_entropy_len( &ctx, ctr_drbg_self_test_entropy,
733-
(void *) entropy_source_nopr, nonce_pers_nopr, 16, 32 ) );
719+
mbedtls_ctr_drbg_set_entropy_len( &ctx, 32 );
720+
CHK( mbedtls_ctr_drbg_seed( &ctx,
721+
ctr_drbg_self_test_entropy,
722+
(void *) entropy_source_nopr,
723+
nonce_pers_nopr, 16 ) );
734724
CHK( mbedtls_ctr_drbg_random( &ctx, buf, 16 ) );
735725
CHK( mbedtls_ctr_drbg_reseed( &ctx, NULL, 0 ) );
736726
CHK( mbedtls_ctr_drbg_random( &ctx, buf, 16 ) );

tests/suites/test_suite_ctr_drbg.function

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ static void ctr_drbg_validate_internal( int reseed_mode, data_t * nonce,
4444

4545
/* CTR_DRBG_Instantiate(entropy[:entropy->len], nonce, perso, <ignored>)
4646
* where nonce||perso = nonce[nonce->len] */
47-
TEST_ASSERT( mbedtls_ctr_drbg_seed_entropy_len(
47+
mbedtls_ctr_drbg_set_entropy_len( &ctx, entropy_chunk_len );
48+
TEST_ASSERT( mbedtls_ctr_drbg_seed(
4849
&ctx,
4950
mbedtls_test_entropy_func, entropy->x,
50-
nonce->x, nonce->len,
51-
entropy_chunk_len ) == 0 );
51+
nonce->x, nonce->len ) == 0 );
5252
if( reseed_mode == RESEED_ALWAYS )
5353
mbedtls_ctr_drbg_set_prediction_resistance(
5454
&ctx,

0 commit comments

Comments
 (0)