blob: 33fe63f482b64007ddc2e9d6b73764ae8a13a5c2 [file] [log] [blame]
Paul Bakker0e04d0e2011-11-27 14:46:59 +00001/**
2 * \file ctr_drbg.h
3 *
Gilles Peskine25e19452019-10-02 18:54:20 +02004 * \brief The CTR_DRBG pseudorandom generator.
5 *
6 * CTR_DRBG is a standardized way of building a PRNG from a block-cipher
7 * in counter mode operation, as defined in <em>NIST SP 800-90A:
8 * Recommendation for Random Number Generation Using Deterministic Random
9 * Bit Generators</em>.
Rose Zadik2f8163d2018-01-25 21:55:14 +000010 *
Gilles Peskineeb99c102019-10-02 18:56:17 +020011 * The Mbed TLS implementation of CTR_DRBG uses AES-256
Gilles Peskinedff36822019-10-03 15:10:21 +020012 * as the underlying block cipher, with a derivation function.
13 * The initial seeding grabs #MBEDTLS_CTR_DRBG_ENTROPY_LEN bytes of entropy.
14 * See the documentation of mbedtls_ctr_drbg_seed() for more details.
15 *
16 * Based on NIST SP 800-90A §10.2.1 table 3 and NIST SP 800-57 part 1 table 2,
17 * here are the security strengths achieved in typical configuration:
Gilles Peskineeb99c102019-10-02 18:56:17 +020018 * - 256 bits under the default configuration of the library,
19 * with #MBEDTLS_CTR_DRBG_ENTROPY_LEN set to 48 or more.
20 * - 256 bits if #MBEDTLS_CTR_DRBG_ENTROPY_LEN is set
21 * to 32 or more and the DRBG is initialized with an explicit
22 * nonce in the \c custom parameter to mbedtls_ctr_drbg_seed().
23 * - 128 bits if #MBEDTLS_CTR_DRBG_ENTROPY_LEN is
24 * between 24 and 47 and the DRBG is not initialized with an explicit
25 * nonce (see mbedtls_ctr_drbg_seed()).
26 *
27 * Note that the value of #MBEDTLS_CTR_DRBG_ENTROPY_LEN defaults to:
Gilles Peskinef0b3dcb2019-10-03 14:28:17 +020028 * - \c 48 if the module \c MBEDTLS_SHA512_C is enabled and the symbol
29 * \c MBEDTLS_ENTROPY_FORCE_SHA256 is not enabled at compile time.
Gilles Peskineeb99c102019-10-02 18:56:17 +020030 * This is the default configuration of the library.
Gilles Peskinef0b3dcb2019-10-03 14:28:17 +020031 * - \c 32 if the module \c MBEDTLS_SHA512_C is disabled at compile time.
32 * - \c 32 if \c MBEDTLS_ENTROPY_FORCE_SHA256 is enabled at compile time.
Darryl Greena40a1012018-01-05 15:33:17 +000033 */
34/*
Bence Szépkúti44bfbe32020-08-19 16:54:51 +020035 * Copyright The Mbed TLS Contributors
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020036 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
37 *
38 * This file is provided under the Apache License 2.0, or the
39 * GNU General Public License v2.0 or later.
40 *
41 * **********
42 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020043 *
44 * Licensed under the Apache License, Version 2.0 (the "License"); you may
45 * not use this file except in compliance with the License.
46 * You may obtain a copy of the License at
47 *
48 * http://www.apache.org/licenses/LICENSE-2.0
49 *
50 * Unless required by applicable law or agreed to in writing, software
51 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
52 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
53 * See the License for the specific language governing permissions and
54 * limitations under the License.
Paul Bakker0e04d0e2011-11-27 14:46:59 +000055 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020056 * **********
57 *
58 * **********
59 * GNU General Public License v2.0 or later:
60 *
61 * This program is free software; you can redistribute it and/or modify
62 * it under the terms of the GNU General Public License as published by
63 * the Free Software Foundation; either version 2 of the License, or
64 * (at your option) any later version.
65 *
66 * This program is distributed in the hope that it will be useful,
67 * but WITHOUT ANY WARRANTY; without even the implied warranty of
68 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
69 * GNU General Public License for more details.
70 *
71 * You should have received a copy of the GNU General Public License along
72 * with this program; if not, write to the Free Software Foundation, Inc.,
73 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
74 *
75 * **********
Paul Bakker0e04d0e2011-11-27 14:46:59 +000076 */
Rose Zadik2f8163d2018-01-25 21:55:14 +000077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078#ifndef MBEDTLS_CTR_DRBG_H
79#define MBEDTLS_CTR_DRBG_H
Paul Bakker0e04d0e2011-11-27 14:46:59 +000080
Ron Eldor0559c662018-02-14 16:02:41 +020081#if !defined(MBEDTLS_CONFIG_FILE)
82#include "config.h"
83#else
84#include MBEDTLS_CONFIG_FILE
85#endif
86
Paul Bakker0e04d0e2011-11-27 14:46:59 +000087#include "aes.h"
88
Manuel Pégourié-Gonnard0a4fb092015-05-07 12:50:31 +010089#if defined(MBEDTLS_THREADING_C)
Ron Eldordf9b93e2017-05-14 16:17:33 +030090#include "threading.h"
Manuel Pégourié-Gonnard0a4fb092015-05-07 12:50:31 +010091#endif
92
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093#define MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED -0x0034 /**< The entropy source failed. */
Rose Zadik2f8163d2018-01-25 21:55:14 +000094#define MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG -0x0036 /**< The requested random buffer length is too big. */
95#define MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG -0x0038 /**< The input (entropy + additional data) is too large. */
96#define MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR -0x003A /**< Read or write error in file. */
Paul Bakker0e04d0e2011-11-27 14:46:59 +000097
Rose Zadik2f8163d2018-01-25 21:55:14 +000098#define MBEDTLS_CTR_DRBG_BLOCKSIZE 16 /**< The block size used by the cipher. */
99#define MBEDTLS_CTR_DRBG_KEYSIZE 32 /**< The key size used by the cipher. */
100#define MBEDTLS_CTR_DRBG_KEYBITS ( MBEDTLS_CTR_DRBG_KEYSIZE * 8 ) /**< The key size for the DRBG operation, in bits. */
101#define MBEDTLS_CTR_DRBG_SEEDLEN ( MBEDTLS_CTR_DRBG_KEYSIZE + MBEDTLS_CTR_DRBG_BLOCKSIZE ) /**< The seed length, calculated as (counter + AES key). */
Paul Bakker9bcf16c2013-06-24 19:31:17 +0200102
Paul Bakker088c5c52014-04-25 11:11:10 +0200103/**
104 * \name SECTION: Module settings
105 *
106 * The configuration options you can set for this module are in this section.
Rose Zadik2f8163d2018-01-25 21:55:14 +0000107 * Either change them in config.h or define them using the compiler command
108 * line.
Paul Bakker088c5c52014-04-25 11:11:10 +0200109 * \{
110 */
111
Gilles Peskinedff36822019-10-03 15:10:21 +0200112/** \def MBEDTLS_CTR_DRBG_ENTROPY_LEN
113 *
114 * \brief The amount of entropy used per seed by default, in bytes.
115 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116#if !defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN)
117#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256)
Gilles Peskinedff36822019-10-03 15:10:21 +0200118/** This is 48 bytes because the entropy module uses SHA-512
Gilles Peskinef0b3dcb2019-10-03 14:28:17 +0200119 * (\c MBEDTLS_ENTROPY_FORCE_SHA256 is not set).
Gilles Peskineeb99c102019-10-02 18:56:17 +0200120 */
Rose Zadik2f8163d2018-01-25 21:55:14 +0000121#define MBEDTLS_CTR_DRBG_ENTROPY_LEN 48
Gilles Peskinedff36822019-10-03 15:10:21 +0200122
123#else /* defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256) */
124
125/** This is 32 bytes because the entropy module uses SHA-256
Gilles Peskinef0b3dcb2019-10-03 14:28:17 +0200126 * (the SHA512 module is disabled or
127 * \c MBEDTLS_ENTROPY_FORCE_SHA256 is enabled).
Gilles Peskineeb99c102019-10-02 18:56:17 +0200128 *
Gilles Peskinedff36822019-10-03 15:10:21 +0200129 * \warning To achieve a 256-bit security strength, you must pass a nonce
130 * to mbedtls_ctr_drbg_seed().
Rose Zadik2f8163d2018-01-25 21:55:14 +0000131 */
Gilles Peskineeb99c102019-10-02 18:56:17 +0200132#define MBEDTLS_CTR_DRBG_ENTROPY_LEN 32
Gilles Peskinedff36822019-10-03 15:10:21 +0200133#endif /* defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256) */
134#endif /* !defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN) */
Paul Bakker088c5c52014-04-25 11:11:10 +0200135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136#if !defined(MBEDTLS_CTR_DRBG_RESEED_INTERVAL)
Rose Zadik2f8163d2018-01-25 21:55:14 +0000137#define MBEDTLS_CTR_DRBG_RESEED_INTERVAL 10000
138/**< The interval before reseed is performed by default. */
Paul Bakker088c5c52014-04-25 11:11:10 +0200139#endif
140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141#if !defined(MBEDTLS_CTR_DRBG_MAX_INPUT)
Rose Zadik2f8163d2018-01-25 21:55:14 +0000142#define MBEDTLS_CTR_DRBG_MAX_INPUT 256
143/**< The maximum number of additional input Bytes. */
Paul Bakker088c5c52014-04-25 11:11:10 +0200144#endif
145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146#if !defined(MBEDTLS_CTR_DRBG_MAX_REQUEST)
Rose Zadik2f8163d2018-01-25 21:55:14 +0000147#define MBEDTLS_CTR_DRBG_MAX_REQUEST 1024
148/**< The maximum number of requested Bytes per call. */
Paul Bakker088c5c52014-04-25 11:11:10 +0200149#endif
150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151#if !defined(MBEDTLS_CTR_DRBG_MAX_SEED_INPUT)
Rose Zadik2f8163d2018-01-25 21:55:14 +0000152#define MBEDTLS_CTR_DRBG_MAX_SEED_INPUT 384
Gilles Peskine25e19452019-10-02 18:54:20 +0200153/**< The maximum size of seed or reseed buffer in bytes. */
Paul Bakker088c5c52014-04-25 11:11:10 +0200154#endif
155
156/* \} name SECTION: Module settings */
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000157
Rose Zadik2f8163d2018-01-25 21:55:14 +0000158#define MBEDTLS_CTR_DRBG_PR_OFF 0
159/**< Prediction resistance is disabled. */
160#define MBEDTLS_CTR_DRBG_PR_ON 1
161/**< Prediction resistance is enabled. */
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000162
163#ifdef __cplusplus
164extern "C" {
165#endif
166
167/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000168 * \brief The CTR_DRBG context structure.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000169 */
170typedef struct
171{
Rose Zadik2f8163d2018-01-25 21:55:14 +0000172 unsigned char counter[16]; /*!< The counter (V). */
173 int reseed_counter; /*!< The reseed counter. */
174 int prediction_resistance; /*!< This determines whether prediction
175 resistance is enabled, that is
176 whether to systematically reseed before
177 each random generation. */
178 size_t entropy_len; /*!< The amount of entropy grabbed on each
179 seed or reseed operation. */
180 int reseed_interval; /*!< The reseed interval. */
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000181
Rose Zadik2f8163d2018-01-25 21:55:14 +0000182 mbedtls_aes_context aes_ctx; /*!< The AES context. */
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000183
184 /*
185 * Callbacks (Entropy)
186 */
187 int (*f_entropy)(void *, unsigned char *, size_t);
Rose Zadik2f8163d2018-01-25 21:55:14 +0000188 /*!< The entropy callback function. */
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000189
Rose Zadik2f8163d2018-01-25 21:55:14 +0000190 void *p_entropy; /*!< The context for the entropy function. */
Manuel Pégourié-Gonnard0a4fb092015-05-07 12:50:31 +0100191
192#if defined(MBEDTLS_THREADING_C)
Gilles Peskine6e2cf252021-02-09 18:44:02 +0100193 /* Invariant: the mutex is initialized if and only if f_entropy != NULL.
194 * This means that the mutex is initialized during the initial seeding
195 * in mbedtls_ctr_drbg_seed() and freed in mbedtls_ctr_drbg_free().
196 *
197 * Note that this invariant may change without notice. Do not rely on it
198 * and do not access the mutex directly in application code.
199 */
Manuel Pégourié-Gonnard0a4fb092015-05-07 12:50:31 +0100200 mbedtls_threading_mutex_t mutex;
201#endif
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000202}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203mbedtls_ctr_drbg_context;
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000204
205/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000206 * \brief This function initializes the CTR_DRBG context,
207 * and prepares it for mbedtls_ctr_drbg_seed()
208 * or mbedtls_ctr_drbg_free().
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200209 *
Gavin Acquroffceb99902020-03-01 17:06:11 -0800210 * \note The reseed interval is
211 * #MBEDTLS_CTR_DRBG_RESEED_INTERVAL by default.
212 * You can override it by calling
213 * mbedtls_ctr_drbg_set_reseed_interval().
214 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000215 * \param ctx The CTR_DRBG context to initialize.
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200216 */
217void mbedtls_ctr_drbg_init( mbedtls_ctr_drbg_context *ctx );
218
219/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000220 * \brief This function seeds and sets up the CTR_DRBG
221 * entropy source for future reseeds.
Paul Bakker9af723c2014-05-01 13:03:14 +0200222 *
Gilles Peskineb9cfe582019-10-02 19:00:57 +0200223 * A typical choice for the \p f_entropy and \p p_entropy parameters is
224 * to use the entropy module:
225 * - \p f_entropy is mbedtls_entropy_func();
226 * - \p p_entropy is an instance of ::mbedtls_entropy_context initialized
227 * with mbedtls_entropy_init() (which registers the platform's default
228 * entropy sources).
229 *
Gilles Peskineb729e1b2019-10-04 12:15:55 +0200230 * The entropy length is #MBEDTLS_CTR_DRBG_ENTROPY_LEN by default.
231 * You can override it by calling mbedtls_ctr_drbg_set_entropy_len().
Gilles Peskineb9cfe582019-10-02 19:00:57 +0200232 *
Gilles Peskine25e19452019-10-02 18:54:20 +0200233 * You can provide a personalization string in addition to the
234 * entropy source, to make this instantiation as unique as possible.
235 *
Gilles Peskineeb99c102019-10-02 18:56:17 +0200236 * \note The _seed_material_ value passed to the derivation
237 * function in the CTR_DRBG Instantiate Process
238 * described in NIST SP 800-90A §10.2.1.3.2
239 * is the concatenation of the string obtained from
240 * calling \p f_entropy and the \p custom string.
241 * The origin of the nonce depends on the value of
242 * the entropy length relative to the security strength.
Gilles Peskineeb99c102019-10-02 18:56:17 +0200243 * - If the entropy length is at least 1.5 times the
244 * security strength then the nonce is taken from the
245 * string obtained with \p f_entropy.
246 * - If the entropy length is less than the security
247 * strength, then the nonce is taken from \p custom.
248 * In this case, for compliance with SP 800-90A,
249 * you must pass a unique value of \p custom at
250 * each invocation. See SP 800-90A §8.6.7 for more
251 * details.
Gilles Peskinedff36822019-10-03 15:10:21 +0200252 */
253#if MBEDTLS_CTR_DRBG_ENTROPY_LEN < MBEDTLS_CTR_DRBG_KEYSIZE * 3 / 2
254/** \warning When #MBEDTLS_CTR_DRBG_ENTROPY_LEN is less than
255 * 48, to achieve a 256-bit security strength,
256 * you must pass a value of \p custom that is a nonce:
257 * this value must never be repeated in subsequent
258 * runs of the same application or on a different
259 * device.
260 */
261#endif
Gilles Peskine275598d2021-02-09 18:44:18 +0100262#if defined(MBEDTLS_THREADING_C)
263/**
264 * \note When Mbed TLS is built with threading support,
265 * after this function returns successfully,
266 * it is safe to call mbedtls_ctr_drbg_random()
267 * from multiple threads. Other operations, including
268 * reseeding, are not thread-safe.
269 */
270#endif /* MBEDTLS_THREADING_C */
Gilles Peskinedff36822019-10-03 15:10:21 +0200271/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000272 * \param ctx The CTR_DRBG context to seed.
Gilles Peskine4c575c02019-10-28 17:33:07 +0100273 * It must have been initialized with
274 * mbedtls_ctr_drbg_init().
275 * After a successful call to mbedtls_ctr_drbg_seed(),
276 * you may not call mbedtls_ctr_drbg_seed() again on
277 * the same context unless you call
278 * mbedtls_ctr_drbg_free() and mbedtls_ctr_drbg_init()
279 * again first.
Gilles Peskine275598d2021-02-09 18:44:18 +0100280 * After a failed call to mbedtls_ctr_drbg_seed(),
281 * you must call mbedtls_ctr_drbg_free().
Rose Zadik2f8163d2018-01-25 21:55:14 +0000282 * \param f_entropy The entropy callback, taking as arguments the
283 * \p p_entropy context, the buffer to fill, and the
Gilles Peskine25e19452019-10-02 18:54:20 +0200284 * length of the buffer.
Gilles Peskineb729e1b2019-10-04 12:15:55 +0200285 * \p f_entropy is always called with a buffer size
286 * equal to the entropy length.
Gilles Peskine25e19452019-10-02 18:54:20 +0200287 * \param p_entropy The entropy context to pass to \p f_entropy.
288 * \param custom The personalization string.
289 * This can be \c NULL, in which case the personalization
290 * string is empty regardless of the value of \p len.
291 * \param len The length of the personalization string.
292 * This must be at most
Gilles Peskine2abefef2019-10-03 15:13:08 +0200293 * #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT
294 * - #MBEDTLS_CTR_DRBG_ENTROPY_LEN.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000295 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000296 * \return \c 0 on success, or
297 * #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED on failure.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000298 */
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200299int mbedtls_ctr_drbg_seed( mbedtls_ctr_drbg_context *ctx,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000300 int (*f_entropy)(void *, unsigned char *, size_t),
301 void *p_entropy,
Paul Bakker1bc9efc2011-12-03 11:29:32 +0000302 const unsigned char *custom,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000303 size_t len );
304
305/**
Gavin Acquroffceb99902020-03-01 17:06:11 -0800306 * \brief This function resets CTR_DRBG context to the state immediately
307 * after initial call of mbedtls_ctr_drbg_init().
Paul Bakkerfff03662014-06-18 16:21:25 +0200308 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000309 * \param ctx The CTR_DRBG context to clear.
Paul Bakkerfff03662014-06-18 16:21:25 +0200310 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311void mbedtls_ctr_drbg_free( mbedtls_ctr_drbg_context *ctx );
Paul Bakkerfff03662014-06-18 16:21:25 +0200312
313/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000314 * \brief This function turns prediction resistance on or off.
315 * The default value is off.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000316 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000317 * \note If enabled, entropy is gathered at the beginning of
Gilles Peskine25e19452019-10-02 18:54:20 +0200318 * every call to mbedtls_ctr_drbg_random_with_add()
319 * or mbedtls_ctr_drbg_random().
Rose Zadik2f8163d2018-01-25 21:55:14 +0000320 * Only use this if your entropy source has sufficient
321 * throughput.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000322 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000323 * \param ctx The CTR_DRBG context.
324 * \param resistance #MBEDTLS_CTR_DRBG_PR_ON or #MBEDTLS_CTR_DRBG_PR_OFF.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000325 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326void mbedtls_ctr_drbg_set_prediction_resistance( mbedtls_ctr_drbg_context *ctx,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000327 int resistance );
328
329/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000330 * \brief This function sets the amount of entropy grabbed on each
Gilles Peskineb729e1b2019-10-04 12:15:55 +0200331 * seed or reseed.
Gilles Peskineeb99c102019-10-02 18:56:17 +0200332 *
333 * The default value is #MBEDTLS_CTR_DRBG_ENTROPY_LEN.
334 *
Gilles Peskinedff36822019-10-03 15:10:21 +0200335 * \note The security strength of CTR_DRBG is bounded by the
336 * entropy length. Thus \p len must be at least
337 * 32 (in bytes) to achieve a 256-bit strength.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000338 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000339 * \param ctx The CTR_DRBG context.
Gilles Peskine25e19452019-10-02 18:54:20 +0200340 * \param len The amount of entropy to grab, in bytes.
341 * This must be at most #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000342 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343void mbedtls_ctr_drbg_set_entropy_len( mbedtls_ctr_drbg_context *ctx,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000344 size_t len );
345
346/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000347 * \brief This function sets the reseed interval.
Gilles Peskine25e19452019-10-02 18:54:20 +0200348 *
349 * The reseed interval is the number of calls to mbedtls_ctr_drbg_random()
350 * or mbedtls_ctr_drbg_random_with_add() after which the entropy function
351 * is called again.
352 *
353 * The default value is #MBEDTLS_CTR_DRBG_RESEED_INTERVAL.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000354 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000355 * \param ctx The CTR_DRBG context.
356 * \param interval The reseed interval.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000357 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358void mbedtls_ctr_drbg_set_reseed_interval( mbedtls_ctr_drbg_context *ctx,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000359 int interval );
360
361/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000362 * \brief This function reseeds the CTR_DRBG context, that is
363 * extracts data from the entropy source.
Paul Bakker9af723c2014-05-01 13:03:14 +0200364 *
Gilles Peskine275598d2021-02-09 18:44:18 +0100365 * \note This function is not thread-safe. It is not safe
366 * to call this function if another thread might be
367 * concurrently obtaining random numbers from the same
368 * context or updating or reseeding the same context.
369 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000370 * \param ctx The CTR_DRBG context.
Gilles Peskine25e19452019-10-02 18:54:20 +0200371 * \param additional Additional data to add to the state. Can be \c NULL.
Rose Zadik2f8163d2018-01-25 21:55:14 +0000372 * \param len The length of the additional data.
Gilles Peskine25e19452019-10-02 18:54:20 +0200373 * This must be less than
374 * #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT - \c entropy_len
375 * where \c entropy_len is the entropy length
376 * configured for the context.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000377 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000378 * \return \c 0 on success, or
379 * #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED on failure.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000380 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381int mbedtls_ctr_drbg_reseed( mbedtls_ctr_drbg_context *ctx,
Paul Bakker1bc9efc2011-12-03 11:29:32 +0000382 const unsigned char *additional, size_t len );
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000383
384/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000385 * \brief This function updates the state of the CTR_DRBG context.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000386 *
Gilles Peskine275598d2021-02-09 18:44:18 +0100387 * \note This function is not thread-safe. It is not safe
388 * to call this function if another thread might be
389 * concurrently obtaining random numbers from the same
390 * context or updating or reseeding the same context.
391 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000392 * \param ctx The CTR_DRBG context.
Gilles Peskine25e19452019-10-02 18:54:20 +0200393 * \param additional The data to update the state with. This must not be
394 * \c NULL unless \p add_len is \c 0.
Gilles Peskine9ce29722018-09-11 16:41:54 +0200395 * \param add_len Length of \p additional in bytes. This must be at
396 * most #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT.
Manuel Pégourié-Gonnard5cb4b312014-11-25 17:41:50 +0100397 *
Gilles Peskine9ce29722018-09-11 16:41:54 +0200398 * \return \c 0 on success.
399 * \return #MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG if
400 * \p add_len is more than
401 * #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT.
402 * \return An error from the underlying AES cipher on failure.
403 */
404int mbedtls_ctr_drbg_update_ret( mbedtls_ctr_drbg_context *ctx,
405 const unsigned char *additional,
406 size_t add_len );
407
408/**
409 * \brief This function updates the state of the CTR_DRBG context.
410 *
411 * \warning This function cannot report errors. You should use
412 * mbedtls_ctr_drbg_update_ret() instead.
413 *
414 * \note If \p add_len is greater than
415 * #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT, only the first
416 * #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT Bytes are used.
417 * The remaining Bytes are silently discarded.
418 *
Gilles Peskine275598d2021-02-09 18:44:18 +0100419 * \note This function is not thread-safe. It is not safe
420 * to call this function if another thread might be
421 * concurrently obtaining random numbers from the same
422 * context or updating or reseeding the same context.
423 *
Gilles Peskine9ce29722018-09-11 16:41:54 +0200424 * \param ctx The CTR_DRBG context.
Gilles Peskine25e19452019-10-02 18:54:20 +0200425 * \param additional The data to update the state with. This must not be
426 * \c NULL unless \p add_len is \c 0.
427 * \param add_len Length of \p additional data. This must be at
428 * most #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000429 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430void mbedtls_ctr_drbg_update( mbedtls_ctr_drbg_context *ctx,
Gilles Peskine9ce29722018-09-11 16:41:54 +0200431 const unsigned char *additional,
432 size_t add_len );
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000433
434/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000435 * \brief This function updates a CTR_DRBG instance with additional
436 * data and uses it to generate random data.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000437 *
Gilles Peskine25e19452019-10-02 18:54:20 +0200438 * This function automatically reseeds if the reseed counter is exceeded
439 * or prediction resistance is enabled.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000440 *
Gilles Peskine275598d2021-02-09 18:44:18 +0100441 * \note This function is not thread-safe. It is not safe
442 * to call this function if another thread might be
443 * concurrently obtaining random numbers from the same
444 * context or updating or reseeding the same context.
445 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000446 * \param p_rng The CTR_DRBG context. This must be a pointer to a
447 * #mbedtls_ctr_drbg_context structure.
448 * \param output The buffer to fill.
Gilles Peskine25e19452019-10-02 18:54:20 +0200449 * \param output_len The length of the buffer in bytes.
450 * \param additional Additional data to update. Can be \c NULL, in which
451 * case the additional data is empty regardless of
452 * the value of \p add_len.
453 * \param add_len The length of the additional data
454 * if \p additional is not \c NULL.
455 * This must be less than #MBEDTLS_CTR_DRBG_MAX_INPUT
456 * and less than
457 * #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT - \c entropy_len
458 * where \c entropy_len is the entropy length
459 * configured for the context.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000460 *
Gilles Peskine25e19452019-10-02 18:54:20 +0200461 * \return \c 0 on success.
462 * \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or
Rose Zadik2f8163d2018-01-25 21:55:14 +0000463 * #MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG on failure.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000464 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465int mbedtls_ctr_drbg_random_with_add( void *p_rng,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000466 unsigned char *output, size_t output_len,
Paul Bakker1bc9efc2011-12-03 11:29:32 +0000467 const unsigned char *additional, size_t add_len );
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000468
469/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000470 * \brief This function uses CTR_DRBG to generate random data.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000471 *
Gilles Peskine25e19452019-10-02 18:54:20 +0200472 * This function automatically reseeds if the reseed counter is exceeded
473 * or prediction resistance is enabled.
Gilles Peskine275598d2021-02-09 18:44:18 +0100474 */
475#if defined(MBEDTLS_THREADING_C)
476/**
477 * \note When Mbed TLS is built with threading support,
478 * it is safe to call mbedtls_ctr_drbg_random()
479 * from multiple threads. Other operations, including
480 * reseeding, are not thread-safe.
481 */
482#endif /* MBEDTLS_THREADING_C */
483/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000484 * \param p_rng The CTR_DRBG context. This must be a pointer to a
485 * #mbedtls_ctr_drbg_context structure.
486 * \param output The buffer to fill.
Gilles Peskine25e19452019-10-02 18:54:20 +0200487 * \param output_len The length of the buffer in bytes.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000488 *
Gilles Peskine25e19452019-10-02 18:54:20 +0200489 * \return \c 0 on success.
490 * \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or
Rose Zadik2f8163d2018-01-25 21:55:14 +0000491 * #MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG on failure.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000492 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493int mbedtls_ctr_drbg_random( void *p_rng,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000494 unsigned char *output, size_t output_len );
495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496#if defined(MBEDTLS_FS_IO)
Paul Bakkerfc754a92011-12-05 13:23:51 +0000497/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000498 * \brief This function writes a seed file.
Paul Bakkerfc754a92011-12-05 13:23:51 +0000499 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000500 * \param ctx The CTR_DRBG context.
501 * \param path The name of the file.
Paul Bakkerfc754a92011-12-05 13:23:51 +0000502 *
Gilles Peskine25e19452019-10-02 18:54:20 +0200503 * \return \c 0 on success.
504 * \return #MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR on file error.
505 * \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED on reseed
Rose Zadik2f8163d2018-01-25 21:55:14 +0000506 * failure.
Paul Bakkerfc754a92011-12-05 13:23:51 +0000507 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508int mbedtls_ctr_drbg_write_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
Paul Bakkerfc754a92011-12-05 13:23:51 +0000509
510/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000511 * \brief This function reads and updates a seed file. The seed
512 * is added to this instance.
Paul Bakkerfc754a92011-12-05 13:23:51 +0000513 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000514 * \param ctx The CTR_DRBG context.
515 * \param path The name of the file.
Paul Bakkerfc754a92011-12-05 13:23:51 +0000516 *
Gilles Peskine25e19452019-10-02 18:54:20 +0200517 * \return \c 0 on success.
518 * \return #MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR on file error.
519 * \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED on
520 * reseed failure.
521 * \return #MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG if the existing
522 * seed file is too large.
Paul Bakkerfc754a92011-12-05 13:23:51 +0000523 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524int mbedtls_ctr_drbg_update_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
525#endif /* MBEDTLS_FS_IO */
Paul Bakkerfc754a92011-12-05 13:23:51 +0000526
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000527/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000528 * \brief The CTR_DRBG checkup routine.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000529 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000530 * \return \c 0 on success, or \c 1 on failure.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000531 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532int mbedtls_ctr_drbg_self_test( int verbose );
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000533
Paul Bakker534f82c2013-06-25 16:47:55 +0200534/* Internal functions (do not call directly) */
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200535int mbedtls_ctr_drbg_seed_entropy_len( mbedtls_ctr_drbg_context *,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200536 int (*)(void *, unsigned char *, size_t), void *,
537 const unsigned char *, size_t, size_t );
Paul Bakker534f82c2013-06-25 16:47:55 +0200538
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000539#ifdef __cplusplus
540}
541#endif
542
543#endif /* ctr_drbg.h */