1 /**
2  * \file ctr_drbg.h
3  *
4  * \brief    This file contains definitions and functions for the
5  *           CTR_DRBG pseudorandom generator.
6  *
7  * CTR_DRBG is a standardized way of building a PRNG from a block-cipher
8  * in counter mode operation, as defined in <em>NIST SP 800-90A:
9  * Recommendation for Random Number Generation Using Deterministic Random
10  * Bit Generators</em>.
11  *
12  * The Mbed TLS implementation of CTR_DRBG uses AES-256 (default) or AES-128
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  *
16  * The security strength as defined in NIST SP 800-90A is
17  * 128 bits when AES-128 is used (\c MBEDTLS_CTR_DRBG_USE_128_BIT_KEY enabled)
18  * and 256 bits otherwise, provided that #MBEDTLS_CTR_DRBG_ENTROPY_LEN is
19  * kept at its default value (and not overridden in mbedtls_config.h) and that the
20  * DRBG instance is set up with default parameters.
21  * See the documentation of mbedtls_ctr_drbg_seed() for more
22  * information.
23  */
24 /*
25  *  Copyright The Mbed TLS Contributors
26  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
27  */
28 
29 #ifndef MBEDTLS_CTR_DRBG_H
30 #define MBEDTLS_CTR_DRBG_H
31 #include "mbedtls/private_access.h"
32 
33 #include "mbedtls/build_info.h"
34 
35 #include "mbedtls/aes.h"
36 #include "entropy.h"
37 
38 #if defined(MBEDTLS_THREADING_C)
39 #include "mbedtls/threading.h"
40 #endif
41 
42 /** The entropy source failed. */
43 #define MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED        -0x0034
44 /** The requested random buffer length is too big. */
45 #define MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG              -0x0036
46 /** The input (entropy + additional data) is too large. */
47 #define MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG                -0x0038
48 /** Read or write error in file. */
49 #define MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR                -0x003A
50 
51 #define MBEDTLS_CTR_DRBG_BLOCKSIZE          16 /**< The block size used by the cipher. */
52 
53 #if defined(MBEDTLS_CTR_DRBG_USE_128_BIT_KEY)
54 #define MBEDTLS_CTR_DRBG_KEYSIZE            16
55 /**< The key size in bytes used by the cipher.
56  *
57  * Compile-time choice: 16 bytes (128 bits)
58  * because #MBEDTLS_CTR_DRBG_USE_128_BIT_KEY is enabled.
59  */
60 #else
61 #define MBEDTLS_CTR_DRBG_KEYSIZE            32
62 /**< The key size in bytes used by the cipher.
63  *
64  * Compile-time choice: 32 bytes (256 bits)
65  * because \c MBEDTLS_CTR_DRBG_USE_128_BIT_KEY is disabled.
66  */
67 #endif
68 
69 #define MBEDTLS_CTR_DRBG_KEYBITS            (MBEDTLS_CTR_DRBG_KEYSIZE * 8)   /**< The key size for the DRBG operation, in bits. */
70 #define MBEDTLS_CTR_DRBG_SEEDLEN            (MBEDTLS_CTR_DRBG_KEYSIZE + MBEDTLS_CTR_DRBG_BLOCKSIZE)   /**< The seed length, calculated as (counter + AES key). */
71 
72 /**
73  * \name SECTION: Module settings
74  *
75  * The configuration options you can set for this module are in this section.
76  * Either change them in mbedtls_config.h or define them using the compiler command
77  * line.
78  * \{
79  */
80 
81 /** \def MBEDTLS_CTR_DRBG_ENTROPY_LEN
82  *
83  * \brief The amount of entropy used per seed by default, in bytes.
84  */
85 #if !defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN)
86 #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
87 /** This is 48 bytes because the entropy module uses SHA-512.
88  */
89 #define MBEDTLS_CTR_DRBG_ENTROPY_LEN        48
90 
91 #else /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
92 
93 /** This is 32 bytes because the entropy module uses SHA-256.
94  */
95 #if !defined(MBEDTLS_CTR_DRBG_USE_128_BIT_KEY)
96 /** \warning To achieve a 256-bit security strength, you must pass a nonce
97  *           to mbedtls_ctr_drbg_seed().
98  */
99 #endif /* !defined(MBEDTLS_CTR_DRBG_USE_128_BIT_KEY) */
100 #define MBEDTLS_CTR_DRBG_ENTROPY_LEN        32
101 #endif /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
102 #endif /* !defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN) */
103 
104 #if !defined(MBEDTLS_CTR_DRBG_RESEED_INTERVAL)
105 #define MBEDTLS_CTR_DRBG_RESEED_INTERVAL    10000
106 /**< The interval before reseed is performed by default. */
107 #endif
108 
109 #if !defined(MBEDTLS_CTR_DRBG_MAX_INPUT)
110 #define MBEDTLS_CTR_DRBG_MAX_INPUT          256
111 /**< The maximum number of additional input Bytes. */
112 #endif
113 
114 #if !defined(MBEDTLS_CTR_DRBG_MAX_REQUEST)
115 #define MBEDTLS_CTR_DRBG_MAX_REQUEST        1024
116 /**< The maximum number of requested Bytes per call. */
117 #endif
118 
119 #if !defined(MBEDTLS_CTR_DRBG_MAX_SEED_INPUT)
120 #define MBEDTLS_CTR_DRBG_MAX_SEED_INPUT     384
121 /**< The maximum size of seed or reseed buffer in bytes. */
122 #endif
123 
124 /** \} name SECTION: Module settings */
125 
126 #define MBEDTLS_CTR_DRBG_PR_OFF             0
127 /**< Prediction resistance is disabled. */
128 #define MBEDTLS_CTR_DRBG_PR_ON              1
129 /**< Prediction resistance is enabled. */
130 
131 #ifdef __cplusplus
132 extern "C" {
133 #endif
134 
135 #if MBEDTLS_CTR_DRBG_ENTROPY_LEN >= MBEDTLS_CTR_DRBG_KEYSIZE * 3 / 2
136 /** The default length of the nonce read from the entropy source.
137  *
138  * This is \c 0 because a single read from the entropy source is sufficient
139  * to include a nonce.
140  * See the documentation of mbedtls_ctr_drbg_seed() for more information.
141  */
142 #define MBEDTLS_CTR_DRBG_ENTROPY_NONCE_LEN 0
143 #else
144 /** The default length of the nonce read from the entropy source.
145  *
146  * This is half of the default entropy length because a single read from
147  * the entropy source does not provide enough material to form a nonce.
148  * See the documentation of mbedtls_ctr_drbg_seed() for more information.
149  */
150 #define MBEDTLS_CTR_DRBG_ENTROPY_NONCE_LEN (MBEDTLS_CTR_DRBG_ENTROPY_LEN + 1) / 2
151 #endif
152 
153 /**
154  * \brief          The CTR_DRBG context structure.
155  */
156 typedef struct mbedtls_ctr_drbg_context {
157     unsigned char MBEDTLS_PRIVATE(counter)[16];  /*!< The counter (V). */
158     int MBEDTLS_PRIVATE(reseed_counter);         /*!< The reseed counter.
159                                                   * This is the number of requests that have
160                                                   * been made since the last (re)seeding,
161                                                   * minus one.
162                                                   * Before the initial seeding, this field
163                                                   * contains the amount of entropy in bytes
164                                                   * to use as a nonce for the initial seeding,
165                                                   * or -1 if no nonce length has been explicitly
166                                                   * set (see mbedtls_ctr_drbg_set_nonce_len()).
167                                                   */
168     int MBEDTLS_PRIVATE(prediction_resistance);  /*!< This determines whether prediction
169                                                     resistance is enabled, that is
170                                                     whether to systematically reseed before
171                                                     each random generation. */
172     size_t MBEDTLS_PRIVATE(entropy_len);         /*!< The amount of entropy grabbed on each
173                                                     seed or reseed operation, in bytes. */
174     int MBEDTLS_PRIVATE(reseed_interval);        /*!< The reseed interval.
175                                                   * This is the maximum number of requests
176                                                   * that can be made between reseedings. */
177 
178     mbedtls_aes_context MBEDTLS_PRIVATE(aes_ctx);        /*!< The AES context. */
179 
180     /*
181      * Callbacks (Entropy)
182      */
183     int(*MBEDTLS_PRIVATE(f_entropy))(void *, unsigned char *, size_t);
184     /*!< The entropy callback function. */
185 
186     void *MBEDTLS_PRIVATE(p_entropy);            /*!< The context for the entropy function. */
187 
188 #if defined(MBEDTLS_THREADING_C)
189     /* Invariant: the mutex is initialized if and only if f_entropy != NULL.
190      * This means that the mutex is initialized during the initial seeding
191      * in mbedtls_ctr_drbg_seed() and freed in mbedtls_ctr_drbg_free().
192      *
193      * Note that this invariant may change without notice. Do not rely on it
194      * and do not access the mutex directly in application code.
195      */
196     mbedtls_threading_mutex_t MBEDTLS_PRIVATE(mutex);
197 #endif
198 }
199 mbedtls_ctr_drbg_context;
200 
201 /**
202  * \brief               This function initializes the CTR_DRBG context,
203  *                      and prepares it for mbedtls_ctr_drbg_seed()
204  *                      or mbedtls_ctr_drbg_free().
205  *
206  * \note                The reseed interval is
207  *                      #MBEDTLS_CTR_DRBG_RESEED_INTERVAL by default.
208  *                      You can override it by calling
209  *                      mbedtls_ctr_drbg_set_reseed_interval().
210  *
211  * \param ctx           The CTR_DRBG context to initialize.
212  */
213 void mbedtls_ctr_drbg_init(mbedtls_ctr_drbg_context *ctx);
214 
215 /**
216  * \brief               This function seeds and sets up the CTR_DRBG
217  *                      entropy source for future reseeds.
218  *
219  * A typical choice for the \p f_entropy and \p p_entropy parameters is
220  * to use the entropy module:
221  * - \p f_entropy is mbedtls_entropy_func();
222  * - \p p_entropy is an instance of ::mbedtls_entropy_context initialized
223  *   with mbedtls_entropy_init() (which registers the platform's default
224  *   entropy sources).
225  *
226  * The entropy length is #MBEDTLS_CTR_DRBG_ENTROPY_LEN by default.
227  * You can override it by calling mbedtls_ctr_drbg_set_entropy_len().
228  *
229  * The entropy nonce length is:
230  * - \c 0 if the entropy length is at least 3/2 times the entropy length,
231  *   which guarantees that the security strength is the maximum permitted
232  *   by the key size and entropy length according to NIST SP 800-90A §10.2.1;
233  * - Half the entropy length otherwise.
234  * You can override it by calling mbedtls_ctr_drbg_set_nonce_len().
235  * With the default entropy length, the entropy nonce length is
236  * #MBEDTLS_CTR_DRBG_ENTROPY_NONCE_LEN.
237  *
238  * You can provide a nonce and personalization string in addition to the
239  * entropy source, to make this instantiation as unique as possible.
240  * See SP 800-90A §8.6.7 for more details about nonces.
241  *
242  * The _seed_material_ value passed to the derivation function in
243  * the CTR_DRBG Instantiate Process described in NIST SP 800-90A §10.2.1.3.2
244  * is the concatenation of the following strings:
245  * - A string obtained by calling \p f_entropy function for the entropy
246  *   length.
247  */
248 #if MBEDTLS_CTR_DRBG_ENTROPY_NONCE_LEN == 0
249 /**
250  * - If mbedtls_ctr_drbg_set_nonce_len() has been called, a string
251  *   obtained by calling \p f_entropy function for the specified length.
252  */
253 #else
254 /**
255  * - A string obtained by calling \p f_entropy function for the entropy nonce
256  *   length. If the entropy nonce length is \c 0, this function does not
257  *   make a second call to \p f_entropy.
258  */
259 #endif
260 #if defined(MBEDTLS_THREADING_C)
261 /**
262  * \note                When Mbed TLS is built with threading support,
263  *                      after this function returns successfully,
264  *                      it is safe to call mbedtls_ctr_drbg_random()
265  *                      from multiple threads. Other operations, including
266  *                      reseeding, are not thread-safe.
267  */
268 #endif /* MBEDTLS_THREADING_C */
269 /**
270  * - The \p custom string.
271  *
272  * \note                To achieve the nominal security strength permitted
273  *                      by CTR_DRBG, the entropy length must be:
274  *                      - at least 16 bytes for a 128-bit strength
275  *                      (maximum achievable strength when using AES-128);
276  *                      - at least 32 bytes for a 256-bit strength
277  *                      (maximum achievable strength when using AES-256).
278  *
279  *                      In addition, if you do not pass a nonce in \p custom,
280  *                      the sum of the entropy length
281  *                      and the entropy nonce length must be:
282  *                      - at least 24 bytes for a 128-bit strength
283  *                      (maximum achievable strength when using AES-128);
284  *                      - at least 48 bytes for a 256-bit strength
285  *                      (maximum achievable strength when using AES-256).
286  *
287  * \param ctx           The CTR_DRBG context to seed.
288  *                      It must have been initialized with
289  *                      mbedtls_ctr_drbg_init().
290  *                      After a successful call to mbedtls_ctr_drbg_seed(),
291  *                      you may not call mbedtls_ctr_drbg_seed() again on
292  *                      the same context unless you call
293  *                      mbedtls_ctr_drbg_free() and mbedtls_ctr_drbg_init()
294  *                      again first.
295  *                      After a failed call to mbedtls_ctr_drbg_seed(),
296  *                      you must call mbedtls_ctr_drbg_free().
297  * \param f_entropy     The entropy callback, taking as arguments the
298  *                      \p p_entropy context, the buffer to fill, and the
299  *                      length of the buffer.
300  *                      \p f_entropy is always called with a buffer size
301  *                      less than or equal to the entropy length.
302  * \param p_entropy     The entropy context to pass to \p f_entropy.
303  * \param custom        The personalization string.
304  *                      This can be \c NULL, in which case the personalization
305  *                      string is empty regardless of the value of \p len.
306  * \param len           The length of the personalization string.
307  *                      This must be at most
308  *                      #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT
309  *                      - #MBEDTLS_CTR_DRBG_ENTROPY_LEN.
310  *
311  * \return              \c 0 on success.
312  * \return              #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED on failure.
313  */
314 int mbedtls_ctr_drbg_seed(mbedtls_ctr_drbg_context *ctx,
315                           int (*f_entropy)(void *, unsigned char *, size_t),
316                           void *p_entropy,
317                           const unsigned char *custom,
318                           size_t len);
319 
320 /**
321  * \brief               This function resets CTR_DRBG context to the state immediately
322  *                      after initial call of mbedtls_ctr_drbg_init().
323  *
324  * \param ctx           The CTR_DRBG context to clear.
325  */
326 void mbedtls_ctr_drbg_free(mbedtls_ctr_drbg_context *ctx);
327 
328 /**
329  * \brief               This function turns prediction resistance on or off.
330  *                      The default value is off.
331  *
332  * \note                If enabled, entropy is gathered at the beginning of
333  *                      every call to mbedtls_ctr_drbg_random_with_add()
334  *                      or mbedtls_ctr_drbg_random().
335  *                      Only use this if your entropy source has sufficient
336  *                      throughput.
337  *
338  * \param ctx           The CTR_DRBG context.
339  * \param resistance    #MBEDTLS_CTR_DRBG_PR_ON or #MBEDTLS_CTR_DRBG_PR_OFF.
340  */
341 void mbedtls_ctr_drbg_set_prediction_resistance(mbedtls_ctr_drbg_context *ctx,
342                                                 int resistance);
343 
344 /**
345  * \brief               This function sets the amount of entropy grabbed on each
346  *                      seed or reseed.
347  *
348  * The default value is #MBEDTLS_CTR_DRBG_ENTROPY_LEN.
349  *
350  * \note                The security strength of CTR_DRBG is bounded by the
351  *                      entropy length. Thus:
352  *                      - When using AES-256
353  *                        (\c MBEDTLS_CTR_DRBG_USE_128_BIT_KEY is disabled,
354  *                        which is the default),
355  *                        \p len must be at least 32 (in bytes)
356  *                        to achieve a 256-bit strength.
357  *                      - When using AES-128
358  *                        (\c MBEDTLS_CTR_DRBG_USE_128_BIT_KEY is enabled)
359  *                        \p len must be at least 16 (in bytes)
360  *                        to achieve a 128-bit strength.
361  *
362  * \param ctx           The CTR_DRBG context.
363  * \param len           The amount of entropy to grab, in bytes.
364  *                      This must be at most #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT
365  *                      and at most the maximum length accepted by the
366  *                      entropy function that is set in the context.
367  */
368 void mbedtls_ctr_drbg_set_entropy_len(mbedtls_ctr_drbg_context *ctx,
369                                       size_t len);
370 
371 /**
372  * \brief               This function sets the amount of entropy grabbed
373  *                      as a nonce for the initial seeding.
374  *
375  * Call this function before calling mbedtls_ctr_drbg_seed() to read
376  * a nonce from the entropy source during the initial seeding.
377  *
378  * \param ctx           The CTR_DRBG context.
379  * \param len           The amount of entropy to grab for the nonce, in bytes.
380  *                      This must be at most #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT
381  *                      and at most the maximum length accepted by the
382  *                      entropy function that is set in the context.
383  *
384  * \return              \c 0 on success.
385  * \return              #MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG if \p len is
386  *                      more than #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT.
387  * \return              #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED
388  *                      if the initial seeding has already taken place.
389  */
390 int mbedtls_ctr_drbg_set_nonce_len(mbedtls_ctr_drbg_context *ctx,
391                                    size_t len);
392 
393 /**
394  * \brief               This function sets the reseed interval.
395  *
396  * The reseed interval is the number of calls to mbedtls_ctr_drbg_random()
397  * or mbedtls_ctr_drbg_random_with_add() after which the entropy function
398  * is called again.
399  *
400  * The default value is #MBEDTLS_CTR_DRBG_RESEED_INTERVAL.
401  *
402  * \param ctx           The CTR_DRBG context.
403  * \param interval      The reseed interval.
404  */
405 void mbedtls_ctr_drbg_set_reseed_interval(mbedtls_ctr_drbg_context *ctx,
406                                           int interval);
407 
408 /**
409  * \brief               This function reseeds the CTR_DRBG context, that is
410  *                      extracts data from the entropy source.
411  *
412  * \note                This function is not thread-safe. It is not safe
413  *                      to call this function if another thread might be
414  *                      concurrently obtaining random numbers from the same
415  *                      context or updating or reseeding the same context.
416  *
417  * \param ctx           The CTR_DRBG context.
418  * \param additional    Additional data to add to the state. Can be \c NULL.
419  * \param len           The length of the additional data.
420  *                      This must be less than
421  *                      #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT - \c entropy_len
422  *                      where \c entropy_len is the entropy length
423  *                      configured for the context.
424  *
425  * \return              \c 0 on success.
426  * \return              #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED on failure.
427  */
428 int mbedtls_ctr_drbg_reseed(mbedtls_ctr_drbg_context *ctx,
429                             const unsigned char *additional, size_t len);
430 
431 /**
432  * \brief              This function updates the state of the CTR_DRBG context.
433  *
434  * \note                This function is not thread-safe. It is not safe
435  *                      to call this function if another thread might be
436  *                      concurrently obtaining random numbers from the same
437  *                      context or updating or reseeding the same context.
438  *
439  * \param ctx          The CTR_DRBG context.
440  * \param additional   The data to update the state with. This must not be
441  *                     \c NULL unless \p add_len is \c 0.
442  * \param add_len      Length of \p additional in bytes. This must be at
443  *                     most #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT.
444  *
445  * \return             \c 0 on success.
446  * \return             #MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG if
447  *                     \p add_len is more than
448  *                     #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT.
449  * \return             An error from the underlying AES cipher on failure.
450  */
451 int mbedtls_ctr_drbg_update(mbedtls_ctr_drbg_context *ctx,
452                             const unsigned char *additional,
453                             size_t add_len);
454 
455 /**
456  * \brief   This function updates a CTR_DRBG instance with additional
457  *          data and uses it to generate random data.
458  *
459  * This function automatically reseeds if the reseed counter is exceeded
460  * or prediction resistance is enabled.
461  *
462  * \note                This function is not thread-safe. It is not safe
463  *                      to call this function if another thread might be
464  *                      concurrently obtaining random numbers from the same
465  *                      context or updating or reseeding the same context.
466  *
467  * \param p_rng         The CTR_DRBG context. This must be a pointer to a
468  *                      #mbedtls_ctr_drbg_context structure.
469  * \param output        The buffer to fill.
470  * \param output_len    The length of the buffer in bytes.
471  * \param additional    Additional data to update. Can be \c NULL, in which
472  *                      case the additional data is empty regardless of
473  *                      the value of \p add_len.
474  * \param add_len       The length of the additional data
475  *                      if \p additional is not \c NULL.
476  *                      This must be less than #MBEDTLS_CTR_DRBG_MAX_INPUT
477  *                      and less than
478  *                      #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT - \c entropy_len
479  *                      where \c entropy_len is the entropy length
480  *                      configured for the context.
481  *
482  * \return    \c 0 on success.
483  * \return    #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or
484  *            #MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG on failure.
485  */
486 int mbedtls_ctr_drbg_random_with_add(void *p_rng,
487                                      unsigned char *output, size_t output_len,
488                                      const unsigned char *additional, size_t add_len);
489 
490 /**
491  * \brief   This function uses CTR_DRBG to generate random data.
492  *
493  * This function automatically reseeds if the reseed counter is exceeded
494  * or prediction resistance is enabled.
495  */
496 #if defined(MBEDTLS_THREADING_C)
497 /**
498  * \note                When Mbed TLS is built with threading support,
499  *                      it is safe to call mbedtls_ctr_drbg_random()
500  *                      from multiple threads. Other operations, including
501  *                      reseeding, are not thread-safe.
502  */
503 #endif /* MBEDTLS_THREADING_C */
504 /**
505  * \param p_rng         The CTR_DRBG context. This must be a pointer to a
506  *                      #mbedtls_ctr_drbg_context structure.
507  * \param output        The buffer to fill.
508  * \param output_len    The length of the buffer in bytes.
509  *
510  * \return              \c 0 on success.
511  * \return              #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or
512  *                      #MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG on failure.
513  */
514 int mbedtls_ctr_drbg_random(void *p_rng,
515                             unsigned char *output, size_t output_len);
516 
517 #if defined(MBEDTLS_FS_IO)
518 /**
519  * \brief               This function writes a seed file.
520  *
521  * \param ctx           The CTR_DRBG context.
522  * \param path          The name of the file.
523  *
524  * \return              \c 0 on success.
525  * \return              #MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR on file error.
526  * \return              #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED on reseed
527  *                      failure.
528  */
529 int mbedtls_ctr_drbg_write_seed_file(mbedtls_ctr_drbg_context *ctx, const char *path);
530 
531 /**
532  * \brief               This function reads and updates a seed file. The seed
533  *                      is added to this instance.
534  *
535  * \param ctx           The CTR_DRBG context.
536  * \param path          The name of the file.
537  *
538  * \return              \c 0 on success.
539  * \return              #MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR on file error.
540  * \return              #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED on
541  *                      reseed failure.
542  * \return              #MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG if the existing
543  *                      seed file is too large.
544  */
545 int mbedtls_ctr_drbg_update_seed_file(mbedtls_ctr_drbg_context *ctx, const char *path);
546 #endif /* MBEDTLS_FS_IO */
547 
548 #if defined(MBEDTLS_SELF_TEST)
549 
550 /**
551  * \brief               The CTR_DRBG checkup routine.
552  *
553  * \return              \c 0 on success.
554  * \return              \c 1 on failure.
555  */
556 int mbedtls_ctr_drbg_self_test(int verbose);
557 
558 #endif /* MBEDTLS_SELF_TEST */
559 
560 #ifdef __cplusplus
561 }
562 #endif
563 
564 #endif /* ctr_drbg.h */
565