Skip to content

Commit 8f7921e

Browse files
HMAC_DRBG: support set_entropy_len() before seed()
mbedtls_hmac_drbg_seed() always set the entropy length to the default, so a call to mbedtls_hmac_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().
1 parent 3cdb3da commit 8f7921e

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

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/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
{

0 commit comments

Comments
 (0)