Skip to content

Commit 069fb0e

Browse files
committed
Merge remote-tracking branch 'origin/pr/2860' into mbedtls-2.16
* origin/pr/2860: (26 commits) config.pl full: exclude MBEDTLS_CTR_DRBG_USE_128_BIT_KEY mbedtls_hmac_drbg_set_entropy_len() only matters when reseeding mbedtls_ctr_drbg_set_entropy_len() only matters when reseeding mbedtls_ctr_drbg_seed: correct maximum for len Add a note about CTR_DRBG security strength to config.h Move MBEDTLS_CTR_DRBG_USE_128_BIT_KEY to the correct section CTR_DRBG: more consistent formatting and wording CTR_DRBG documentation: further wording improvements CTR_DRBG: Improve the explanation of security strength CTR_DRBG: make it easier to understand the security strength HMAC_DRBG: note that the initial seeding grabs entropy for the nonce Use standard terminology to describe the personalization string Do note that xxx_drbg_random functions reseed with PR enabled Consistently use \c NULL and \c 0 Also mention HMAC_DRBG in the changelog entry HMAC_DRBG: improve the documentation of the entropy length HMAC_DRBG documentation improvements clarifications More CTR_DRBG documentation improvements and clarifications Fix wording Remove warning that the previous expanded discussion has obsoleted ...
2 parents b9fc079 + 16ee3c1 commit 069fb0e

File tree

7 files changed

+403
-167
lines changed

7 files changed

+403
-167
lines changed

ChangeLog

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ Bugfix
1010
Changes
1111
* Add unit tests for AES-GCM when called through mbedtls_cipher_auth_xxx()
1212
from the cipher abstraction layer. Fixes #2198.
13+
* Clarify how the interface of the CTR_DRBG and HMAC modules relates to
14+
NIST SP 800-90A. In particular CTR_DRBG requires an explicit nonce
15+
to achieve a 256-bit strength if MBEDTLS_ENTROPY_FORCE_SHA256 is set.
1316

1417
= mbed TLS 2.16.3 branch released 2019-09-06
1518

include/mbedtls/config.h

+12-2
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,13 @@
689689
#define MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN
690690
#define MBEDTLS_CIPHER_PADDING_ZEROS
691691

692+
/** \def MBEDTLS_CTR_DRBG_USE_128_BIT_KEY
693+
*
694+
* Uncomment this macro to use a 128-bit key in the CTR_DRBG module.
695+
* By default, CTR_DRBG uses a 256-bit key.
696+
*/
697+
//#define MBEDTLS_CTR_DRBG_USE_128_BIT_KEY
698+
692699
/**
693700
* \def MBEDTLS_ENABLE_WEAK_CIPHERSUITES
694701
*
@@ -2149,7 +2156,11 @@
21492156
*
21502157
* Enable the CTR_DRBG AES-based random generator.
21512158
* The CTR_DRBG generator uses AES-256 by default.
2152-
* To use AES-128 instead, enable MBEDTLS_CTR_DRBG_USE_128_BIT_KEY below.
2159+
* To use AES-128 instead, enable \c MBEDTLS_CTR_DRBG_USE_128_BIT_KEY above.
2160+
*
2161+
* \note To achieve a 256-bit security strength with CTR_DRBG,
2162+
* you must use AES-256 *and* use sufficient entropy.
2163+
* See ctr_drbg.h for more details.
21532164
*
21542165
* Module: library/ctr_drbg.c
21552166
* Caller:
@@ -3041,7 +3052,6 @@
30413052
//#define MBEDTLS_CTR_DRBG_MAX_INPUT 256 /**< Maximum number of additional input bytes */
30423053
//#define MBEDTLS_CTR_DRBG_MAX_REQUEST 1024 /**< Maximum number of requested bytes per call */
30433054
//#define MBEDTLS_CTR_DRBG_MAX_SEED_INPUT 384 /**< Maximum size of (re)seed buffer */
3044-
//#define MBEDTLS_CTR_DRBG_USE_128_BIT_KEY /**< Use 128-bit key for CTR_DRBG - may reduce security (see ctr_drbg.h) */
30453055

30463056
/* HMAC_DRBG options */
30473057
//#define MBEDTLS_HMAC_DRBG_RESEED_INTERVAL 10000 /**< Interval before reseed is performed by default */

include/mbedtls/ctr_drbg.h

+174-43
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,44 @@
11
/**
22
* \file ctr_drbg.h
33
*
4-
* \brief This file contains CTR_DRBG definitions and functions.
4+
* \brief This file contains definitions and functions for the
5+
* CTR_DRBG pseudorandom generator.
56
*
67
* CTR_DRBG is a standardized way of building a PRNG from a block-cipher
78
* in counter mode operation, as defined in <em>NIST SP 800-90A:
89
* Recommendation for Random Number Generation Using Deterministic Random
910
* Bit Generators</em>.
1011
*
1112
* The Mbed TLS implementation of CTR_DRBG uses AES-256 (default) or AES-128
12-
* as the underlying block cipher.
13-
*
14-
* \warning Using 128-bit keys for CTR_DRBG limits the security of generated
15-
* keys and operations that use random values generated to 128-bit security.
13+
* (if \c MBEDTLS_CTR_DRBG_USE_128_BIT_KEY is enabled at compile time)
14+
* as the underlying block cipher, with a derivation function.
15+
* The initial seeding grabs #MBEDTLS_CTR_DRBG_ENTROPY_LEN bytes of entropy.
16+
* See the documentation of mbedtls_ctr_drbg_seed() for more details.
17+
*
18+
* Based on NIST SP 800-90A §10.2.1 table 3 and NIST SP 800-57 part 1 table 2,
19+
* here are the security strengths achieved in typical configuration:
20+
* - 256 bits under the default configuration of the library, with AES-256
21+
* and with #MBEDTLS_CTR_DRBG_ENTROPY_LEN set to 48 or more.
22+
* - 256 bits if AES-256 is used, #MBEDTLS_CTR_DRBG_ENTROPY_LEN is set
23+
* to 32 or more, and the DRBG is initialized with an explicit
24+
* nonce in the \c custom parameter to mbedtls_ctr_drbg_seed().
25+
* - 128 bits if AES-256 is used but #MBEDTLS_CTR_DRBG_ENTROPY_LEN is
26+
* between 24 and 47 and the DRBG is not initialized with an explicit
27+
* nonce (see mbedtls_ctr_drbg_seed()).
28+
* - 128 bits if AES-128 is used (\c MBEDTLS_CTR_DRBG_USE_128_BIT_KEY enabled)
29+
* and #MBEDTLS_CTR_DRBG_ENTROPY_LEN is set to 24 or more (which is
30+
* always the case unless it is explicitly set to a different value
31+
* in config.h).
32+
*
33+
* Note that the value of #MBEDTLS_CTR_DRBG_ENTROPY_LEN defaults to:
34+
* - \c 48 if the module \c MBEDTLS_SHA512_C is enabled and the symbol
35+
* \c MBEDTLS_ENTROPY_FORCE_SHA256 is disabled at compile time.
36+
* This is the default configuration of the library.
37+
* - \c 32 if the module \c MBEDTLS_SHA512_C is disabled at compile time.
38+
* - \c 32 if \c MBEDTLS_ENTROPY_FORCE_SHA256 is enabled at compile time.
1639
*/
1740
/*
18-
* Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
41+
* Copyright (C) 2006-2019, Arm Limited (or its affiliates), All Rights Reserved
1942
* SPDX-License-Identifier: Apache-2.0
2043
*
2144
* Licensed under the Apache License, Version 2.0 (the "License"); you may
@@ -56,9 +79,19 @@
5679
#define MBEDTLS_CTR_DRBG_BLOCKSIZE 16 /**< The block size used by the cipher. */
5780

5881
#if defined(MBEDTLS_CTR_DRBG_USE_128_BIT_KEY)
59-
#define MBEDTLS_CTR_DRBG_KEYSIZE 16 /**< The key size used by the cipher (compile-time choice: 128 bits). */
82+
#define MBEDTLS_CTR_DRBG_KEYSIZE 16
83+
/**< The key size in bytes used by the cipher.
84+
*
85+
* Compile-time choice: 16 bytes (128 bits)
86+
* because #MBEDTLS_CTR_DRBG_USE_128_BIT_KEY is enabled.
87+
*/
6088
#else
61-
#define MBEDTLS_CTR_DRBG_KEYSIZE 32 /**< The key size used by the cipher (compile-time choice: 256 bits). */
89+
#define MBEDTLS_CTR_DRBG_KEYSIZE 32
90+
/**< The key size in bytes used by the cipher.
91+
*
92+
* Compile-time choice: 32 bytes (256 bits)
93+
* because \c MBEDTLS_CTR_DRBG_USE_128_BIT_KEY is disabled.
94+
*/
6295
#endif
6396

6497
#define MBEDTLS_CTR_DRBG_KEYBITS ( MBEDTLS_CTR_DRBG_KEYSIZE * 8 ) /**< The key size for the DRBG operation, in bits. */
@@ -73,21 +106,31 @@
73106
* \{
74107
*/
75108

109+
/** \def MBEDTLS_CTR_DRBG_ENTROPY_LEN
110+
*
111+
* \brief The amount of entropy used per seed by default, in bytes.
112+
*/
76113
#if !defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN)
77114
#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256)
115+
/** This is 48 bytes because the entropy module uses SHA-512
116+
* (\c MBEDTLS_ENTROPY_FORCE_SHA256 is disabled).
117+
*/
78118
#define MBEDTLS_CTR_DRBG_ENTROPY_LEN 48
79-
/**< The amount of entropy used per seed by default:
80-
* <ul><li>48 with SHA-512.</li>
81-
* <li>32 with SHA-256.</li></ul>
119+
120+
#else /* defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256) */
121+
122+
/** This is 32 bytes because the entropy module uses SHA-256
123+
* (the SHA512 module is disabled or
124+
* \c MBEDTLS_ENTROPY_FORCE_SHA256 is enabled).
82125
*/
83-
#else
84-
#define MBEDTLS_CTR_DRBG_ENTROPY_LEN 32
85-
/**< Amount of entropy used per seed by default:
86-
* <ul><li>48 with SHA-512.</li>
87-
* <li>32 with SHA-256.</li></ul>
126+
#if !defined(MBEDTLS_CTR_DRBG_USE_128_BIT_KEY)
127+
/** \warning To achieve a 256-bit security strength, you must pass a nonce
128+
* to mbedtls_ctr_drbg_seed().
88129
*/
89-
#endif
90-
#endif
130+
#endif /* !defined(MBEDTLS_CTR_DRBG_USE_128_BIT_KEY) */
131+
#define MBEDTLS_CTR_DRBG_ENTROPY_LEN 32
132+
#endif /* defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256) */
133+
#endif /* !defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN) */
91134

92135
#if !defined(MBEDTLS_CTR_DRBG_RESEED_INTERVAL)
93136
#define MBEDTLS_CTR_DRBG_RESEED_INTERVAL 10000
@@ -106,7 +149,7 @@
106149

107150
#if !defined(MBEDTLS_CTR_DRBG_MAX_SEED_INPUT)
108151
#define MBEDTLS_CTR_DRBG_MAX_SEED_INPUT 384
109-
/**< The maximum size of seed or reseed buffer. */
152+
/**< The maximum size of seed or reseed buffer in bytes. */
110153
#endif
111154

112155
/* \} name SECTION: Module settings */
@@ -164,17 +207,62 @@ void mbedtls_ctr_drbg_init( mbedtls_ctr_drbg_context *ctx );
164207
* \brief This function seeds and sets up the CTR_DRBG
165208
* entropy source for future reseeds.
166209
*
167-
* \note Personalization data can be provided in addition to the more generic
168-
* entropy source, to make this instantiation as unique as possible.
169-
*
210+
* A typical choice for the \p f_entropy and \p p_entropy parameters is
211+
* to use the entropy module:
212+
* - \p f_entropy is mbedtls_entropy_func();
213+
* - \p p_entropy is an instance of ::mbedtls_entropy_context initialized
214+
* with mbedtls_entropy_init() (which registers the platform's default
215+
* entropy sources).
216+
*
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.
222+
*
223+
* You can provide a personalization string in addition to the
224+
* entropy source, to make this instantiation as unique as possible.
225+
*
226+
* \note The _seed_material_ value passed to the derivation
227+
* function in the CTR_DRBG Instantiate Process
228+
* described in NIST SP 800-90A §10.2.1.3.2
229+
* is the concatenation of the string obtained from
230+
* calling \p f_entropy and the \p custom string.
231+
* The origin of the nonce depends on the value of
232+
* the entropy length relative to the security strength.
233+
* - If the entropy length is at least 1.5 times the
234+
* security strength then the nonce is taken from the
235+
* string obtained with \p f_entropy.
236+
* - If the entropy length is less than the security
237+
* strength, then the nonce is taken from \p custom.
238+
* In this case, for compliance with SP 800-90A,
239+
* you must pass a unique value of \p custom at
240+
* each invocation. See SP 800-90A §8.6.7 for more
241+
* details.
242+
*/
243+
#if MBEDTLS_CTR_DRBG_ENTROPY_LEN < MBEDTLS_CTR_DRBG_KEYSIZE * 3 / 2
244+
/** \warning When #MBEDTLS_CTR_DRBG_ENTROPY_LEN is less than
245+
* #MBEDTLS_CTR_DRBG_KEYSIZE * 3 / 2, to achieve the
246+
* maximum security strength permitted by CTR_DRBG,
247+
* you must pass a value of \p custom that is a nonce:
248+
* this value must never be repeated in subsequent
249+
* runs of the same application or on a different
250+
* device.
251+
*/
252+
#endif
253+
/**
170254
* \param ctx The CTR_DRBG context to seed.
171255
* \param f_entropy The entropy callback, taking as arguments the
172256
* \p p_entropy context, the buffer to fill, and the
173-
length of the buffer.
174-
* \param p_entropy The entropy context.
175-
* \param custom Personalization data, that is device-specific
176-
identifiers. Can be NULL.
177-
* \param len The length of the personalization data.
257+
* length of the buffer.
258+
* \param p_entropy The entropy context to pass to \p f_entropy.
259+
* \param custom The personalization string.
260+
* This can be \c NULL, in which case the personalization
261+
* string is empty regardless of the value of \p len.
262+
* \param len The length of the personalization string.
263+
* This must be at most
264+
* #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT
265+
* - #MBEDTLS_CTR_DRBG_ENTROPY_LEN.
178266
*
179267
* \return \c 0 on success.
180268
* \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED on failure.
@@ -197,7 +285,8 @@ void mbedtls_ctr_drbg_free( mbedtls_ctr_drbg_context *ctx );
197285
* The default value is off.
198286
*
199287
* \note If enabled, entropy is gathered at the beginning of
200-
* every call to mbedtls_ctr_drbg_random_with_add().
288+
* every call to mbedtls_ctr_drbg_random_with_add()
289+
* or mbedtls_ctr_drbg_random().
201290
* Only use this if your entropy source has sufficient
202291
* throughput.
203292
*
@@ -209,18 +298,42 @@ void mbedtls_ctr_drbg_set_prediction_resistance( mbedtls_ctr_drbg_context *ctx,
209298

210299
/**
211300
* \brief This function sets the amount of entropy grabbed on each
212-
* seed or reseed. The default value is
213-
* #MBEDTLS_CTR_DRBG_ENTROPY_LEN.
301+
* subsequent reseed.
302+
*
303+
* The default value is #MBEDTLS_CTR_DRBG_ENTROPY_LEN.
304+
*
305+
* \note mbedtls_ctr_drbg_seed() always sets the entropy length
306+
* to #MBEDTLS_CTR_DRBG_ENTROPY_LEN, so this function
307+
* only has an effect when it is called after
308+
* mbedtls_ctr_drbg_seed().
309+
*
310+
* \note The security strength of CTR_DRBG is bounded by the
311+
* entropy length. Thus:
312+
* - When using AES-256
313+
* (\c MBEDTLS_CTR_DRBG_USE_128_BIT_KEY is disabled,
314+
* which is the default),
315+
* \p len must be at least 32 (in bytes)
316+
* to achieve a 256-bit strength.
317+
* - When using AES-128
318+
* (\c MBEDTLS_CTR_DRBG_USE_128_BIT_KEY is enabled)
319+
* \p len must be at least 16 (in bytes)
320+
* to achieve a 128-bit strength.
214321
*
215322
* \param ctx The CTR_DRBG context.
216-
* \param len The amount of entropy to grab.
323+
* \param len The amount of entropy to grab, in bytes.
324+
* This must be at most #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT.
217325
*/
218326
void mbedtls_ctr_drbg_set_entropy_len( mbedtls_ctr_drbg_context *ctx,
219327
size_t len );
220328

221329
/**
222330
* \brief This function sets the reseed interval.
223-
* The default value is #MBEDTLS_CTR_DRBG_RESEED_INTERVAL.
331+
*
332+
* The reseed interval is the number of calls to mbedtls_ctr_drbg_random()
333+
* or mbedtls_ctr_drbg_random_with_add() after which the entropy function
334+
* is called again.
335+
*
336+
* The default value is #MBEDTLS_CTR_DRBG_RESEED_INTERVAL.
224337
*
225338
* \param ctx The CTR_DRBG context.
226339
* \param interval The reseed interval.
@@ -233,8 +346,12 @@ void mbedtls_ctr_drbg_set_reseed_interval( mbedtls_ctr_drbg_context *ctx,
233346
* extracts data from the entropy source.
234347
*
235348
* \param ctx The CTR_DRBG context.
236-
* \param additional Additional data to add to the state. Can be NULL.
349+
* \param additional Additional data to add to the state. Can be \c NULL.
237350
* \param len The length of the additional data.
351+
* This must be less than
352+
* #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT - \c entropy_len
353+
* where \c entropy_len is the entropy length
354+
* configured for the context.
238355
*
239356
* \return \c 0 on success.
240357
* \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED on failure.
@@ -246,7 +363,8 @@ int mbedtls_ctr_drbg_reseed( mbedtls_ctr_drbg_context *ctx,
246363
* \brief This function updates the state of the CTR_DRBG context.
247364
*
248365
* \param ctx The CTR_DRBG context.
249-
* \param additional The data to update the state with.
366+
* \param additional The data to update the state with. This must not be
367+
* \c NULL unless \p add_len is \c 0.
250368
* \param add_len Length of \p additional in bytes. This must be at
251369
* most #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT.
252370
*
@@ -264,14 +382,23 @@ int mbedtls_ctr_drbg_update_ret( mbedtls_ctr_drbg_context *ctx,
264382
* \brief This function updates a CTR_DRBG instance with additional
265383
* data and uses it to generate random data.
266384
*
267-
* \note The function automatically reseeds if the reseed counter is exceeded.
385+
* This function automatically reseeds if the reseed counter is exceeded
386+
* or prediction resistance is enabled.
268387
*
269388
* \param p_rng The CTR_DRBG context. This must be a pointer to a
270389
* #mbedtls_ctr_drbg_context structure.
271390
* \param output The buffer to fill.
272-
* \param output_len The length of the buffer.
273-
* \param additional Additional data to update. Can be NULL.
274-
* \param add_len The length of the additional data.
391+
* \param output_len The length of the buffer in bytes.
392+
* \param additional Additional data to update. Can be \c NULL, in which
393+
* case the additional data is empty regardless of
394+
* the value of \p add_len.
395+
* \param add_len The length of the additional data
396+
* if \p additional is not \c NULL.
397+
* This must be less than #MBEDTLS_CTR_DRBG_MAX_INPUT
398+
* and less than
399+
* #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT - \c entropy_len
400+
* where \c entropy_len is the entropy length
401+
* configured for the context.
275402
*
276403
* \return \c 0 on success.
277404
* \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or
@@ -284,12 +411,14 @@ int mbedtls_ctr_drbg_random_with_add( void *p_rng,
284411
/**
285412
* \brief This function uses CTR_DRBG to generate random data.
286413
*
287-
* \note The function automatically reseeds if the reseed counter is exceeded.
414+
* This function automatically reseeds if the reseed counter is exceeded
415+
* or prediction resistance is enabled.
416+
*
288417
*
289418
* \param p_rng The CTR_DRBG context. This must be a pointer to a
290419
* #mbedtls_ctr_drbg_context structure.
291420
* \param output The buffer to fill.
292-
* \param output_len The length of the buffer.
421+
* \param output_len The length of the buffer in bytes.
293422
*
294423
* \return \c 0 on success.
295424
* \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or
@@ -336,7 +465,7 @@ MBEDTLS_DEPRECATED void mbedtls_ctr_drbg_update(
336465
*
337466
* \return \c 0 on success.
338467
* \return #MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR on file error.
339-
* \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED on
468+
* \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED on reseed
340469
* failure.
341470
*/
342471
int mbedtls_ctr_drbg_write_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
@@ -350,8 +479,10 @@ int mbedtls_ctr_drbg_write_seed_file( mbedtls_ctr_drbg_context *ctx, const char
350479
*
351480
* \return \c 0 on success.
352481
* \return #MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR on file error.
353-
* \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or
354-
* #MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG on failure.
482+
* \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED on
483+
* reseed failure.
484+
* \return #MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG if the existing
485+
* seed file is too large.
355486
*/
356487
int mbedtls_ctr_drbg_update_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
357488
#endif /* MBEDTLS_FS_IO */

0 commit comments

Comments
 (0)