1
1
/**
2
2
* \file ctr_drbg.h
3
3
*
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.
5
6
*
6
7
* CTR_DRBG is a standardized way of building a PRNG from a block-cipher
7
8
* in counter mode operation, as defined in <em>NIST SP 800-90A:
8
9
* Recommendation for Random Number Generation Using Deterministic Random
9
10
* Bit Generators</em>.
10
11
*
11
12
* 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.
16
39
*/
17
40
/*
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
19
42
* SPDX-License-Identifier: Apache-2.0
20
43
*
21
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may
56
79
#define MBEDTLS_CTR_DRBG_BLOCKSIZE 16 /**< The block size used by the cipher. */
57
80
58
81
#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
+ */
60
88
#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
+ */
62
95
#endif
63
96
64
97
#define MBEDTLS_CTR_DRBG_KEYBITS ( MBEDTLS_CTR_DRBG_KEYSIZE * 8 ) /**< The key size for the DRBG operation, in bits. */
73
106
* \{
74
107
*/
75
108
109
+ /** \def MBEDTLS_CTR_DRBG_ENTROPY_LEN
110
+ *
111
+ * \brief The amount of entropy used per seed by default, in bytes.
112
+ */
76
113
#if !defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN )
77
114
#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
+ */
78
118
#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).
82
125
*/
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().
88
129
*/
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) */
91
134
92
135
#if !defined(MBEDTLS_CTR_DRBG_RESEED_INTERVAL )
93
136
#define MBEDTLS_CTR_DRBG_RESEED_INTERVAL 10000
106
149
107
150
#if !defined(MBEDTLS_CTR_DRBG_MAX_SEED_INPUT )
108
151
#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 . */
110
153
#endif
111
154
112
155
/* \} name SECTION: Module settings */
@@ -164,17 +207,62 @@ void mbedtls_ctr_drbg_init( mbedtls_ctr_drbg_context *ctx );
164
207
* \brief This function seeds and sets up the CTR_DRBG
165
208
* entropy source for future reseeds.
166
209
*
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
+ /**
170
254
* \param ctx The CTR_DRBG context to seed.
171
255
* \param f_entropy The entropy callback, taking as arguments the
172
256
* \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.
178
266
*
179
267
* \return \c 0 on success.
180
268
* \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED on failure.
@@ -197,7 +285,8 @@ void mbedtls_ctr_drbg_free( mbedtls_ctr_drbg_context *ctx );
197
285
* The default value is off.
198
286
*
199
287
* \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().
201
290
* Only use this if your entropy source has sufficient
202
291
* throughput.
203
292
*
@@ -209,18 +298,42 @@ void mbedtls_ctr_drbg_set_prediction_resistance( mbedtls_ctr_drbg_context *ctx,
209
298
210
299
/**
211
300
* \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.
214
321
*
215
322
* \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.
217
325
*/
218
326
void mbedtls_ctr_drbg_set_entropy_len ( mbedtls_ctr_drbg_context * ctx ,
219
327
size_t len );
220
328
221
329
/**
222
330
* \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.
224
337
*
225
338
* \param ctx The CTR_DRBG context.
226
339
* \param interval The reseed interval.
@@ -233,8 +346,12 @@ void mbedtls_ctr_drbg_set_reseed_interval( mbedtls_ctr_drbg_context *ctx,
233
346
* extracts data from the entropy source.
234
347
*
235
348
* \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.
237
350
* \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.
238
355
*
239
356
* \return \c 0 on success.
240
357
* \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED on failure.
@@ -246,7 +363,8 @@ int mbedtls_ctr_drbg_reseed( mbedtls_ctr_drbg_context *ctx,
246
363
* \brief This function updates the state of the CTR_DRBG context.
247
364
*
248
365
* \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.
250
368
* \param add_len Length of \p additional in bytes. This must be at
251
369
* most #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT.
252
370
*
@@ -264,14 +382,23 @@ int mbedtls_ctr_drbg_update_ret( mbedtls_ctr_drbg_context *ctx,
264
382
* \brief This function updates a CTR_DRBG instance with additional
265
383
* data and uses it to generate random data.
266
384
*
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.
268
387
*
269
388
* \param p_rng The CTR_DRBG context. This must be a pointer to a
270
389
* #mbedtls_ctr_drbg_context structure.
271
390
* \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.
275
402
*
276
403
* \return \c 0 on success.
277
404
* \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or
@@ -284,12 +411,14 @@ int mbedtls_ctr_drbg_random_with_add( void *p_rng,
284
411
/**
285
412
* \brief This function uses CTR_DRBG to generate random data.
286
413
*
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
+ *
288
417
*
289
418
* \param p_rng The CTR_DRBG context. This must be a pointer to a
290
419
* #mbedtls_ctr_drbg_context structure.
291
420
* \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 .
293
422
*
294
423
* \return \c 0 on success.
295
424
* \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or
@@ -336,7 +465,7 @@ MBEDTLS_DEPRECATED void mbedtls_ctr_drbg_update(
336
465
*
337
466
* \return \c 0 on success.
338
467
* \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
340
469
* failure.
341
470
*/
342
471
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
350
479
*
351
480
* \return \c 0 on success.
352
481
* \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.
355
486
*/
356
487
int mbedtls_ctr_drbg_update_seed_file ( mbedtls_ctr_drbg_context * ctx , const char * path );
357
488
#endif /* MBEDTLS_FS_IO */
0 commit comments