Skip to content

Commit b1c7197

Browse files
authored
Merge pull request #299 from gilles-peskine-arm/drbg-set_entropy_len
Allow xxx_drbg_set_entropy_len before xxx_drbg_seed
2 parents 150d577 + 50ed86b commit b1c7197

File tree

5 files changed

+83
-98
lines changed

5 files changed

+83
-98
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

include/mbedtls/hmac_drbg.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,9 @@ void mbedtls_hmac_drbg_init( mbedtls_hmac_drbg_context *ctx );
141141
* entropy length is set with
142142
* mbedtls_hmac_drbg_set_entropy_len() afterwards.
143143
*
144-
* \note The entropy length for the initial seeding is
145-
* the security strength (converted from bits to bytes).
146-
* You can set a different entropy length for subsequent
147-
* seeding by calling mbedtls_hmac_drbg_set_entropy_len()
148-
* after this function.
144+
* \note The default entropy length is the security strength
145+
* (converted from bits to bytes). You can override
146+
* it by calling mbedtls_hmac_drbg_set_entropy_len().
149147
*
150148
* \note During the initial seeding, this function calls
151149
* the entropy source to obtain a nonce

library/ctr_drbg.c

Lines changed: 58 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -62,70 +62,6 @@ void mbedtls_ctr_drbg_init( mbedtls_ctr_drbg_context *ctx )
6262
#endif
6363
}
6464

65-
/*
66-
* Non-public function wrapped by mbedtls_ctr_drbg_seed(). Necessary to allow
67-
* NIST tests to succeed (which require known length fixed entropy)
68-
*/
69-
/* CTR_DRBG_Instantiate with derivation function (SP 800-90A §10.2.1.3.2)
70-
* mbedtls_ctr_drbg_seed_entropy_len(ctx, f_entropy, p_entropy,
71-
* custom, len, entropy_len)
72-
* implements
73-
* CTR_DRBG_Instantiate(entropy_input, nonce, personalization_string,
74-
* security_strength) -> initial_working_state
75-
* with inputs
76-
* custom[:len] = nonce || personalization_string
77-
* where entropy_input comes from f_entropy for entropy_len bytes
78-
* and with outputs
79-
* ctx = initial_working_state
80-
*/
81-
int mbedtls_ctr_drbg_seed_entropy_len(
82-
mbedtls_ctr_drbg_context *ctx,
83-
int (*f_entropy)(void *, unsigned char *, size_t),
84-
void *p_entropy,
85-
const unsigned char *custom,
86-
size_t len,
87-
size_t entropy_len )
88-
{
89-
int ret;
90-
unsigned char key[MBEDTLS_CTR_DRBG_KEYSIZE];
91-
92-
memset( key, 0, MBEDTLS_CTR_DRBG_KEYSIZE );
93-
94-
mbedtls_aes_init( &ctx->aes_ctx );
95-
96-
ctx->f_entropy = f_entropy;
97-
ctx->p_entropy = p_entropy;
98-
99-
ctx->entropy_len = entropy_len;
100-
ctx->reseed_interval = MBEDTLS_CTR_DRBG_RESEED_INTERVAL;
101-
102-
/*
103-
* Initialize with an empty key
104-
*/
105-
if( ( ret = mbedtls_aes_setkey_enc( &ctx->aes_ctx, key,
106-
MBEDTLS_CTR_DRBG_KEYBITS ) ) != 0 )
107-
{
108-
return( ret );
109-
}
110-
111-
if( ( ret = mbedtls_ctr_drbg_reseed( ctx, custom, len ) ) != 0 )
112-
{
113-
return( ret );
114-
}
115-
return( 0 );
116-
}
117-
118-
int mbedtls_ctr_drbg_seed( mbedtls_ctr_drbg_context *ctx,
119-
int (*f_entropy)(void *, unsigned char *, size_t),
120-
void *p_entropy,
121-
const unsigned char *custom,
122-
size_t len )
123-
{
124-
return( mbedtls_ctr_drbg_seed_entropy_len( ctx, f_entropy, p_entropy,
125-
custom, len,
126-
MBEDTLS_CTR_DRBG_ENTROPY_LEN ) );
127-
}
128-
12965
void mbedtls_ctr_drbg_free( mbedtls_ctr_drbg_context *ctx )
13066
{
13167
if( ctx == NULL )
@@ -445,6 +381,54 @@ int mbedtls_ctr_drbg_reseed( mbedtls_ctr_drbg_context *ctx,
445381
return( ret );
446382
}
447383

384+
/* CTR_DRBG_Instantiate with derivation function (SP 800-90A §10.2.1.3.2)
385+
* mbedtls_ctr_drbg_seed_entropy_len(ctx, f_entropy, p_entropy,
386+
* custom, len, entropy_len)
387+
* implements
388+
* CTR_DRBG_Instantiate(entropy_input, nonce, personalization_string,
389+
* security_strength) -> initial_working_state
390+
* with inputs
391+
* custom[:len] = nonce || personalization_string
392+
* where entropy_input comes from f_entropy for entropy_len bytes
393+
* and with outputs
394+
* ctx = initial_working_state
395+
*/
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 )
401+
{
402+
int ret;
403+
unsigned char key[MBEDTLS_CTR_DRBG_KEYSIZE];
404+
405+
memset( key, 0, MBEDTLS_CTR_DRBG_KEYSIZE );
406+
407+
mbedtls_aes_init( &ctx->aes_ctx );
408+
409+
ctx->f_entropy = f_entropy;
410+
ctx->p_entropy = p_entropy;
411+
412+
if( ctx->entropy_len == 0 )
413+
ctx->entropy_len = MBEDTLS_CTR_DRBG_ENTROPY_LEN;
414+
ctx->reseed_interval = MBEDTLS_CTR_DRBG_RESEED_INTERVAL;
415+
416+
/*
417+
* Initialize with an empty key
418+
*/
419+
if( ( ret = mbedtls_aes_setkey_enc( &ctx->aes_ctx, key,
420+
MBEDTLS_CTR_DRBG_KEYBITS ) ) != 0 )
421+
{
422+
return( ret );
423+
}
424+
425+
if( ( ret = mbedtls_ctr_drbg_reseed( ctx, custom, len ) ) != 0 )
426+
{
427+
return( ret );
428+
}
429+
return( 0 );
430+
}
431+
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 ) );

library/hmac_drbg.c

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -273,16 +273,19 @@ int mbedtls_hmac_drbg_seed( mbedtls_hmac_drbg_context *ctx,
273273

274274
ctx->reseed_interval = MBEDTLS_HMAC_DRBG_RESEED_INTERVAL;
275275

276-
/*
277-
* See SP800-57 5.6.1 (p. 65-66) for the security strength provided by
278-
* each hash function, then according to SP800-90A rev1 10.1 table 2,
279-
* min_entropy_len (in bits) is security_strength.
280-
*
281-
* (This also matches the sizes used in the NIST test vectors.)
282-
*/
283-
ctx->entropy_len = md_size <= 20 ? 16 : /* 160-bits hash -> 128 bits */
284-
md_size <= 28 ? 24 : /* 224-bits hash -> 192 bits */
285-
32; /* better (256+) -> 256 bits */
276+
if( ctx->entropy_len == 0 )
277+
{
278+
/*
279+
* See SP800-57 5.6.1 (p. 65-66) for the security strength provided by
280+
* each hash function, then according to SP800-90A rev1 10.1 table 2,
281+
* min_entropy_len (in bits) is security_strength.
282+
*
283+
* (This also matches the sizes used in the NIST test vectors.)
284+
*/
285+
ctx->entropy_len = md_size <= 20 ? 16 : /* 160-bits hash -> 128 bits */
286+
md_size <= 28 ? 24 : /* 224-bits hash -> 192 bits */
287+
32; /* better (256+) -> 256 bits */
288+
}
286289

287290
if( ( ret = hmac_drbg_reseed_core( ctx, custom, len,
288291
1 /* add nonce */ ) ) != 0 )
@@ -303,7 +306,7 @@ void mbedtls_hmac_drbg_set_prediction_resistance( mbedtls_hmac_drbg_context *ctx
303306
}
304307

305308
/*
306-
* Set entropy length grabbed for reseeds
309+
* Set entropy length grabbed for seeding
307310
*/
308311
void mbedtls_hmac_drbg_set_entropy_len( mbedtls_hmac_drbg_context *ctx, size_t len )
309312
{

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)