blob: 151a8998270148493310b53b9e60645c7f675a76 [file] [log] [blame]
Paul Bakker0e04d0e2011-11-27 14:46:59 +00001/**
2 * \file ctr_drbg.h
3 *
Gilles Peskineec51dd12019-09-30 15:01:02 +02004 * \brief This file contains definitions and functions for the
5 * CTR_DRBG pseudorandom generator.
Rose Zadikc9474eb2018-03-27 10:58:22 +01006 *
Rose Zadikf25eb6e2018-04-16 14:51:52 +01007 * 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>.
Rose Zadik2f8163d2018-01-25 21:55:14 +000011 *
Nir Sonnenscheineb73f7a2018-07-30 17:46:49 +030012 * The Mbed TLS implementation of CTR_DRBG uses AES-256 (default) or AES-128
Gilles Peskined0c64c82019-10-03 14:20:46 +020013 * (if \c MBEDTLS_CTR_DRBG_USE_128_BIT_KEY is enabled at compile time)
Gilles Peskinedddda812019-10-03 14:22:04 +020014 * as the underlying block cipher, with a derivation function.
Gilles Peskinedddda812019-10-03 14:22:04 +020015 *
Gilles Peskinee9a34542019-10-22 20:43:24 +020016 * 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
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020019 * kept at its default value (and not overridden in mbedtls_config.h) and that
20 * the DRBG instance is set up with default parameters. See the documentation of
21 * mbedtls_ctr_drbg_seed() for more information.
Darryl Greena40a1012018-01-05 15:33:17 +000022 */
23/*
Bence Szépkúti1e148272020-08-07 13:07:28 +020024 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020025 * SPDX-License-Identifier: Apache-2.0
26 *
27 * Licensed under the Apache License, Version 2.0 (the "License"); you may
28 * not use this file except in compliance with the License.
29 * You may obtain a copy of the License at
30 *
31 * http://www.apache.org/licenses/LICENSE-2.0
32 *
33 * Unless required by applicable law or agreed to in writing, software
34 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
35 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
36 * See the License for the specific language governing permissions and
37 * limitations under the License.
Paul Bakker0e04d0e2011-11-27 14:46:59 +000038 */
Rose Zadik2f8163d2018-01-25 21:55:14 +000039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#ifndef MBEDTLS_CTR_DRBG_H
41#define MBEDTLS_CTR_DRBG_H
Mateusz Starzyk846f0212021-05-19 19:44:07 +020042#include "mbedtls/private_access.h"
Paul Bakker0e04d0e2011-11-27 14:46:59 +000043
Bence Szépkútic662b362021-05-27 11:25:03 +020044#include "mbedtls/build_info.h"
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050045
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010046#include "mbedtls/aes.h"
Paul Bakker0e04d0e2011-11-27 14:46:59 +000047
Manuel Pégourié-Gonnard0a4fb092015-05-07 12:50:31 +010048#if defined(MBEDTLS_THREADING_C)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020049# include "mbedtls/threading.h"
Manuel Pégourié-Gonnard0a4fb092015-05-07 12:50:31 +010050#endif
51
Gilles Peskined2971572021-07-26 18:48:10 +020052/** The entropy source failed. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020053#define MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED -0x0034
Gilles Peskined2971572021-07-26 18:48:10 +020054/** The requested random buffer length is too big. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020055#define MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG -0x0036
Gilles Peskined2971572021-07-26 18:48:10 +020056/** The input (entropy + additional data) is too large. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020057#define MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG -0x0038
Gilles Peskined2971572021-07-26 18:48:10 +020058/** Read or write error in file. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020059#define MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR -0x003A
Paul Bakker0e04d0e2011-11-27 14:46:59 +000060
Mateusz Starzyk16fec332021-07-22 16:43:35 +020061/** The block size used by the cipher. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020062#define MBEDTLS_CTR_DRBG_BLOCKSIZE 16
Nir Sonnenscheince266e42018-08-29 10:11:46 +030063
Nir Sonnenschein43e4ff02018-09-03 14:15:46 +030064#if defined(MBEDTLS_CTR_DRBG_USE_128_BIT_KEY)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020065# define MBEDTLS_CTR_DRBG_KEYSIZE 16
Gilles Peskine08875d42019-09-24 14:40:40 +020066/**< The key size in bytes used by the cipher.
67 *
68 * Compile-time choice: 16 bytes (128 bits)
Gilles Peskined0c64c82019-10-03 14:20:46 +020069 * because #MBEDTLS_CTR_DRBG_USE_128_BIT_KEY is enabled.
Gilles Peskine08875d42019-09-24 14:40:40 +020070 */
Nir Sonnenscheineb73f7a2018-07-30 17:46:49 +030071#else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020072# define MBEDTLS_CTR_DRBG_KEYSIZE 32
Gilles Peskine08875d42019-09-24 14:40:40 +020073/**< The key size in bytes used by the cipher.
74 *
75 * Compile-time choice: 32 bytes (256 bits)
Gilles Peskined0c64c82019-10-03 14:20:46 +020076 * because \c MBEDTLS_CTR_DRBG_USE_128_BIT_KEY is disabled.
Gilles Peskine08875d42019-09-24 14:40:40 +020077 */
Nir Sonnenscheina4588d42018-07-30 16:59:36 +030078#endif
Nir Sonnenschein43e4ff02018-09-03 14:15:46 +030079
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020080#define MBEDTLS_CTR_DRBG_KEYBITS \
81 (MBEDTLS_CTR_DRBG_KEYSIZE * 8) /**< The key size for the DRBG operation, \
82 in bits. */
83#define MBEDTLS_CTR_DRBG_SEEDLEN \
84 (MBEDTLS_CTR_DRBG_KEYSIZE + MBEDTLS_CTR_DRBG_BLOCKSIZE) /**< The seed \
85 length, \
86 calculated as \
87 (counter + AES \
88 key). */
Paul Bakker9bcf16c2013-06-24 19:31:17 +020089
Paul Bakker088c5c52014-04-25 11:11:10 +020090/**
91 * \name SECTION: Module settings
92 *
93 * The configuration options you can set for this module are in this section.
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020094 * Either change them in mbedtls_config.h or define them using the compiler
95 * command line.
Paul Bakker088c5c52014-04-25 11:11:10 +020096 * \{
97 */
98
Gilles Peskinedddda812019-10-03 14:22:04 +020099/** \def MBEDTLS_CTR_DRBG_ENTROPY_LEN
100 *
101 * \brief The amount of entropy used per seed by default, in bytes.
102 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103#if !defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200104# if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256)
Gilles Peskinedddda812019-10-03 14:22:04 +0200105/** This is 48 bytes because the entropy module uses SHA-512
Gilles Peskined0c64c82019-10-03 14:20:46 +0200106 * (\c MBEDTLS_ENTROPY_FORCE_SHA256 is disabled).
Gilles Peskine08875d42019-09-24 14:40:40 +0200107 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200108# define MBEDTLS_CTR_DRBG_ENTROPY_LEN 48
Gilles Peskinedddda812019-10-03 14:22:04 +0200109
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200110# else /* defined(MBEDTLS_SHA512_C) && \
111 !defined(MBEDTLS_ENTROPY_FORCE_SHA256) */
Gilles Peskinedddda812019-10-03 14:22:04 +0200112
113/** This is 32 bytes because the entropy module uses SHA-256
114 * (the SHA512 module is disabled or
115 * \c MBEDTLS_ENTROPY_FORCE_SHA256 is enabled).
Rose Zadik2f8163d2018-01-25 21:55:14 +0000116 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200117# if !defined(MBEDTLS_CTR_DRBG_USE_128_BIT_KEY)
Gilles Peskinedddda812019-10-03 14:22:04 +0200118/** \warning To achieve a 256-bit security strength, you must pass a nonce
119 * to mbedtls_ctr_drbg_seed().
120 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200121# endif /* !defined(MBEDTLS_CTR_DRBG_USE_128_BIT_KEY) */
122# define MBEDTLS_CTR_DRBG_ENTROPY_LEN 32
123# endif /* defined(MBEDTLS_SHA512_C) && \
124 !defined(MBEDTLS_ENTROPY_FORCE_SHA256) */
Gilles Peskinedddda812019-10-03 14:22:04 +0200125#endif /* !defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN) */
Paul Bakker088c5c52014-04-25 11:11:10 +0200126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127#if !defined(MBEDTLS_CTR_DRBG_RESEED_INTERVAL)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200128# define MBEDTLS_CTR_DRBG_RESEED_INTERVAL 10000
Rose Zadik2f8163d2018-01-25 21:55:14 +0000129/**< The interval before reseed is performed by default. */
Paul Bakker088c5c52014-04-25 11:11:10 +0200130#endif
131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132#if !defined(MBEDTLS_CTR_DRBG_MAX_INPUT)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200133# define MBEDTLS_CTR_DRBG_MAX_INPUT 256
Rose Zadik2f8163d2018-01-25 21:55:14 +0000134/**< The maximum number of additional input Bytes. */
Paul Bakker088c5c52014-04-25 11:11:10 +0200135#endif
136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137#if !defined(MBEDTLS_CTR_DRBG_MAX_REQUEST)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200138# define MBEDTLS_CTR_DRBG_MAX_REQUEST 1024
Rose Zadik2f8163d2018-01-25 21:55:14 +0000139/**< The maximum number of requested Bytes per call. */
Paul Bakker088c5c52014-04-25 11:11:10 +0200140#endif
141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142#if !defined(MBEDTLS_CTR_DRBG_MAX_SEED_INPUT)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200143# define MBEDTLS_CTR_DRBG_MAX_SEED_INPUT 384
Gilles Peskine08875d42019-09-24 14:40:40 +0200144/**< The maximum size of seed or reseed buffer in bytes. */
Paul Bakker088c5c52014-04-25 11:11:10 +0200145#endif
146
147/* \} name SECTION: Module settings */
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000148
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200149#define MBEDTLS_CTR_DRBG_PR_OFF 0
Rose Zadik2f8163d2018-01-25 21:55:14 +0000150/**< Prediction resistance is disabled. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200151#define MBEDTLS_CTR_DRBG_PR_ON 1
Rose Zadik2f8163d2018-01-25 21:55:14 +0000152/**< Prediction resistance is enabled. */
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000153
154#ifdef __cplusplus
155extern "C" {
156#endif
157
Gilles Peskine69971662019-10-23 19:39:36 +0200158#if MBEDTLS_CTR_DRBG_ENTROPY_LEN >= MBEDTLS_CTR_DRBG_KEYSIZE * 3 / 2
159/** The default length of the nonce read from the entropy source.
160 *
161 * This is \c 0 because a single read from the entropy source is sufficient
162 * to include a nonce.
163 * See the documentation of mbedtls_ctr_drbg_seed() for more information.
164 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200165# define MBEDTLS_CTR_DRBG_ENTROPY_NONCE_LEN 0
Gilles Peskine69971662019-10-23 19:39:36 +0200166#else
167/** The default length of the nonce read from the entropy source.
168 *
169 * This is half of the default entropy length because a single read from
170 * the entropy source does not provide enough material to form a nonce.
171 * See the documentation of mbedtls_ctr_drbg_seed() for more information.
172 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200173# define MBEDTLS_CTR_DRBG_ENTROPY_NONCE_LEN \
174 (MBEDTLS_CTR_DRBG_ENTROPY_LEN + 1) / 2
Gilles Peskine69971662019-10-23 19:39:36 +0200175#endif
176
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000177/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000178 * \brief The CTR_DRBG context structure.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000179 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200180typedef struct mbedtls_ctr_drbg_context {
181 unsigned char MBEDTLS_PRIVATE(counter)[16]; /*!< The counter (V). */
182 int MBEDTLS_PRIVATE(reseed_counter); /*!< The reseed counter.
183 * This is the number of requests that
184 * have been made since the last
185 * (re)seeding, minus one. Before the
186 * initial seeding, this field contains
187 * the amount of entropy in bytes to
188 * use as a nonce for the initial
189 * seeding, or -1 if no nonce length
190 * has been explicitly set (see
191 * mbedtls_ctr_drbg_set_nonce_len()).
192 */
193 int MBEDTLS_PRIVATE(prediction_resistance); /*!< This determines whether
194 prediction resistance is enabled, that is
195 whether to systematically reseed before
196 each random generation. */
197 size_t MBEDTLS_PRIVATE(entropy_len); /*!< The amount of entropy grabbed on
198 each seed or reseed operation, in bytes. */
199 int MBEDTLS_PRIVATE(reseed_interval); /*!< The reseed interval.
200 * This is the maximum number of
201 * requests that can be made between
202 * reseedings. */
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000203
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200204 mbedtls_aes_context MBEDTLS_PRIVATE(aes_ctx); /*!< The AES context. */
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000205
206 /*
207 * Callbacks (Entropy)
208 */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200209 int (*MBEDTLS_PRIVATE(f_entropy))(void *, unsigned char *, size_t);
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200210 /*!< The entropy callback function. */
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000211
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200212 void *MBEDTLS_PRIVATE(p_entropy); /*!< The context for the entropy function.
213 */
Manuel Pégourié-Gonnard0a4fb092015-05-07 12:50:31 +0100214
215#if defined(MBEDTLS_THREADING_C)
Gilles Peskineda290f92021-02-09 18:44:02 +0100216 /* Invariant: the mutex is initialized if and only if f_entropy != NULL.
217 * This means that the mutex is initialized during the initial seeding
218 * in mbedtls_ctr_drbg_seed() and freed in mbedtls_ctr_drbg_free().
219 *
220 * Note that this invariant may change without notice. Do not rely on it
221 * and do not access the mutex directly in application code.
222 */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200223 mbedtls_threading_mutex_t MBEDTLS_PRIVATE(mutex);
Manuel Pégourié-Gonnard0a4fb092015-05-07 12:50:31 +0100224#endif
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200225} mbedtls_ctr_drbg_context;
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000226
227/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000228 * \brief This function initializes the CTR_DRBG context,
229 * and prepares it for mbedtls_ctr_drbg_seed()
230 * or mbedtls_ctr_drbg_free().
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200231 *
Gavin Acquroff6aceb512020-03-01 17:06:11 -0800232 * \note The reseed interval is
233 * #MBEDTLS_CTR_DRBG_RESEED_INTERVAL by default.
234 * You can override it by calling
235 * mbedtls_ctr_drbg_set_reseed_interval().
236 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000237 * \param ctx The CTR_DRBG context to initialize.
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200238 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200239void mbedtls_ctr_drbg_init(mbedtls_ctr_drbg_context *ctx);
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200240
241/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000242 * \brief This function seeds and sets up the CTR_DRBG
243 * entropy source for future reseeds.
Paul Bakker9af723c2014-05-01 13:03:14 +0200244 *
Gilles Peskine223deea2019-09-24 14:48:53 +0200245 * A typical choice for the \p f_entropy and \p p_entropy parameters is
246 * to use the entropy module:
247 * - \p f_entropy is mbedtls_entropy_func();
248 * - \p p_entropy is an instance of ::mbedtls_entropy_context initialized
249 * with mbedtls_entropy_init() (which registers the platform's default
250 * entropy sources).
251 *
Gilles Peskine50ed86b2019-10-04 12:15:55 +0200252 * The entropy length is #MBEDTLS_CTR_DRBG_ENTROPY_LEN by default.
253 * You can override it by calling mbedtls_ctr_drbg_set_entropy_len().
Gilles Peskineec51dd12019-09-30 15:01:02 +0200254 *
Gilles Peskinee9a34542019-10-22 20:43:24 +0200255 * The entropy nonce length is:
256 * - \c 0 if the entropy length is at least 3/2 times the entropy length,
257 * which guarantees that the security strength is the maximum permitted
258 * by the key size and entropy length according to NIST SP 800-90A §10.2.1;
259 * - Half the entropy length otherwise.
260 * You can override it by calling mbedtls_ctr_drbg_set_nonce_len().
Gilles Peskine69971662019-10-23 19:39:36 +0200261 * With the default entropy length, the entropy nonce length is
262 * #MBEDTLS_CTR_DRBG_ENTROPY_NONCE_LEN.
263 *
Gilles Peskine9be50982019-10-22 18:42:27 +0200264 * You can provide a nonce and personalization string in addition to the
Gilles Peskine223deea2019-09-24 14:48:53 +0200265 * entropy source, to make this instantiation as unique as possible.
Gilles Peskine9be50982019-10-22 18:42:27 +0200266 * See SP 800-90A §8.6.7 for more details about nonces.
Gilles Peskine223deea2019-09-24 14:48:53 +0200267 *
Gilles Peskine9be50982019-10-22 18:42:27 +0200268 * The _seed_material_ value passed to the derivation function in
269 * the CTR_DRBG Instantiate Process described in NIST SP 800-90A §10.2.1.3.2
270 * is the concatenation of the following strings:
271 * - A string obtained by calling \p f_entropy function for the entropy
272 * length.
Gilles Peskinee9a34542019-10-22 20:43:24 +0200273 */
Gilles Peskine69971662019-10-23 19:39:36 +0200274#if MBEDTLS_CTR_DRBG_ENTROPY_NONCE_LEN == 0
Gilles Peskinee9a34542019-10-22 20:43:24 +0200275/**
276 * - If mbedtls_ctr_drbg_set_nonce_len() has been called, a string
277 * obtained by calling \p f_entropy function for the specified length.
278 */
279#else
280/**
281 * - A string obtained by calling \p f_entropy function for the entropy nonce
282 * length. If the entropy nonce length is \c 0, this function does not
283 * make a second call to \p f_entropy.
284 */
285#endif
Gilles Peskinef305d922021-02-09 18:44:18 +0100286#if defined(MBEDTLS_THREADING_C)
287/**
288 * \note When Mbed TLS is built with threading support,
289 * after this function returns successfully,
290 * it is safe to call mbedtls_ctr_drbg_random()
291 * from multiple threads. Other operations, including
292 * reseeding, are not thread-safe.
293 */
294#endif /* MBEDTLS_THREADING_C */
Gilles Peskinee9a34542019-10-22 20:43:24 +0200295/**
Gilles Peskine9be50982019-10-22 18:42:27 +0200296 * - The \p custom string.
297 *
298 * \note To achieve the nominal security strength permitted
299 * by CTR_DRBG, the entropy length must be:
300 * - at least 16 bytes for a 128-bit strength
301 * (maximum achievable strength when using AES-128);
302 * - at least 32 bytes for a 256-bit strength
303 * (maximum achievable strength when using AES-256).
304 *
305 * In addition, if you do not pass a nonce in \p custom,
306 * the sum of the entropy length
Gilles Peskinee9a34542019-10-22 20:43:24 +0200307 * and the entropy nonce length must be:
Gilles Peskine9be50982019-10-22 18:42:27 +0200308 * - at least 24 bytes for a 128-bit strength
309 * (maximum achievable strength when using AES-128);
310 * - at least 48 bytes for a 256-bit strength
311 * (maximum achievable strength when using AES-256).
312 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000313 * \param ctx The CTR_DRBG context to seed.
Gilles Peskinebd326f92019-10-28 17:33:07 +0100314 * It must have been initialized with
315 * mbedtls_ctr_drbg_init().
316 * After a successful call to mbedtls_ctr_drbg_seed(),
317 * you may not call mbedtls_ctr_drbg_seed() again on
318 * the same context unless you call
319 * mbedtls_ctr_drbg_free() and mbedtls_ctr_drbg_init()
320 * again first.
Gilles Peskinef305d922021-02-09 18:44:18 +0100321 * After a failed call to mbedtls_ctr_drbg_seed(),
322 * you must call mbedtls_ctr_drbg_free().
Rose Zadik2f8163d2018-01-25 21:55:14 +0000323 * \param f_entropy The entropy callback, taking as arguments the
324 * \p p_entropy context, the buffer to fill, and the
Gilles Peskine08875d42019-09-24 14:40:40 +0200325 * length of the buffer.
Gilles Peskine50ed86b2019-10-04 12:15:55 +0200326 * \p f_entropy is always called with a buffer size
Gilles Peskine9be50982019-10-22 18:42:27 +0200327 * less than or equal to the entropy length.
Gilles Peskineec51dd12019-09-30 15:01:02 +0200328 * \param p_entropy The entropy context to pass to \p f_entropy.
Gilles Peskine217b8152019-10-01 18:39:45 +0200329 * \param custom The personalization string.
330 * This can be \c NULL, in which case the personalization
331 * string is empty regardless of the value of \p len.
332 * \param len The length of the personalization string.
Gilles Peskine944bc582019-09-24 14:48:30 +0200333 * This must be at most
334 * #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT
335 * - #MBEDTLS_CTR_DRBG_ENTROPY_LEN.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000336 *
Rose Zadikc9474eb2018-03-27 10:58:22 +0100337 * \return \c 0 on success.
338 * \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED on failure.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000339 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200340int mbedtls_ctr_drbg_seed(mbedtls_ctr_drbg_context *ctx,
341 int (*f_entropy)(void *, unsigned char *, size_t),
342 void *p_entropy,
343 const unsigned char *custom,
344 size_t len);
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000345
346/**
Gavin Acquroff6aceb512020-03-01 17:06:11 -0800347 * \brief This function resets CTR_DRBG context to the state immediately
348 * after initial call of mbedtls_ctr_drbg_init().
Paul Bakkerfff03662014-06-18 16:21:25 +0200349 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000350 * \param ctx The CTR_DRBG context to clear.
Paul Bakkerfff03662014-06-18 16:21:25 +0200351 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200352void mbedtls_ctr_drbg_free(mbedtls_ctr_drbg_context *ctx);
Paul Bakkerfff03662014-06-18 16:21:25 +0200353
354/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000355 * \brief This function turns prediction resistance on or off.
356 * The default value is off.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000357 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000358 * \note If enabled, entropy is gathered at the beginning of
Gilles Peskineec51dd12019-09-30 15:01:02 +0200359 * every call to mbedtls_ctr_drbg_random_with_add()
360 * or mbedtls_ctr_drbg_random().
Rose Zadik2f8163d2018-01-25 21:55:14 +0000361 * Only use this if your entropy source has sufficient
362 * throughput.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000363 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000364 * \param ctx The CTR_DRBG context.
365 * \param resistance #MBEDTLS_CTR_DRBG_PR_ON or #MBEDTLS_CTR_DRBG_PR_OFF.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000366 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200367void mbedtls_ctr_drbg_set_prediction_resistance(mbedtls_ctr_drbg_context *ctx,
368 int resistance);
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000369
370/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000371 * \brief This function sets the amount of entropy grabbed on each
Gilles Peskine50ed86b2019-10-04 12:15:55 +0200372 * seed or reseed.
Gilles Peskineec51dd12019-09-30 15:01:02 +0200373 *
374 * The default value is #MBEDTLS_CTR_DRBG_ENTROPY_LEN.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000375 *
Gilles Peskinedddda812019-10-03 14:22:04 +0200376 * \note The security strength of CTR_DRBG is bounded by the
377 * entropy length. Thus:
378 * - When using AES-256
379 * (\c MBEDTLS_CTR_DRBG_USE_128_BIT_KEY is disabled,
380 * which is the default),
381 * \p len must be at least 32 (in bytes)
382 * to achieve a 256-bit strength.
383 * - When using AES-128
384 * (\c MBEDTLS_CTR_DRBG_USE_128_BIT_KEY is enabled)
385 * \p len must be at least 16 (in bytes)
386 * to achieve a 128-bit strength.
Gilles Peskine223deea2019-09-24 14:48:53 +0200387 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000388 * \param ctx The CTR_DRBG context.
Gilles Peskine08875d42019-09-24 14:40:40 +0200389 * \param len The amount of entropy to grab, in bytes.
Gilles Peskine9be50982019-10-22 18:42:27 +0200390 * This must be at most #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT
391 * and at most the maximum length accepted by the
392 * entropy function that is set in the context.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000393 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200394void mbedtls_ctr_drbg_set_entropy_len(mbedtls_ctr_drbg_context *ctx,
395 size_t len);
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000396
397/**
Gilles Peskine9be50982019-10-22 18:42:27 +0200398 * \brief This function sets the amount of entropy grabbed
399 * as a nonce for the initial seeding.
400 *
401 * Call this function before calling mbedtls_ctr_drbg_seed() to read
402 * a nonce from the entropy source during the initial seeding.
403 *
404 * \param ctx The CTR_DRBG context.
405 * \param len The amount of entropy to grab for the nonce, in bytes.
406 * This must be at most #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT
407 * and at most the maximum length accepted by the
408 * entropy function that is set in the context.
409 *
410 * \return \c 0 on success.
411 * \return #MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG if \p len is
412 * more than #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT.
413 * \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED
414 * if the initial seeding has already taken place.
415 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200416int mbedtls_ctr_drbg_set_nonce_len(mbedtls_ctr_drbg_context *ctx, size_t len);
Gilles Peskine9be50982019-10-22 18:42:27 +0200417
418/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000419 * \brief This function sets the reseed interval.
Gilles Peskineec51dd12019-09-30 15:01:02 +0200420 *
421 * The reseed interval is the number of calls to mbedtls_ctr_drbg_random()
422 * or mbedtls_ctr_drbg_random_with_add() after which the entropy function
423 * is called again.
424 *
425 * The default value is #MBEDTLS_CTR_DRBG_RESEED_INTERVAL.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000426 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000427 * \param ctx The CTR_DRBG context.
428 * \param interval The reseed interval.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000429 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200430void mbedtls_ctr_drbg_set_reseed_interval(mbedtls_ctr_drbg_context *ctx,
431 int interval);
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000432
433/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000434 * \brief This function reseeds the CTR_DRBG context, that is
435 * extracts data from the entropy source.
Paul Bakker9af723c2014-05-01 13:03:14 +0200436 *
Gilles Peskinef305d922021-02-09 18:44:18 +0100437 * \note This function is not thread-safe. It is not safe
438 * to call this function if another thread might be
439 * concurrently obtaining random numbers from the same
440 * context or updating or reseeding the same context.
441 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000442 * \param ctx The CTR_DRBG context.
Gilles Peskine10f16ac2019-10-01 18:30:02 +0200443 * \param additional Additional data to add to the state. Can be \c NULL.
Rose Zadik2f8163d2018-01-25 21:55:14 +0000444 * \param len The length of the additional data.
Gilles Peskine944bc582019-09-24 14:48:30 +0200445 * This must be less than
446 * #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT - \c entropy_len
447 * where \c entropy_len is the entropy length
448 * configured for the context.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000449 *
Rose Zadikc9474eb2018-03-27 10:58:22 +0100450 * \return \c 0 on success.
451 * \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED on failure.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000452 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200453int mbedtls_ctr_drbg_reseed(mbedtls_ctr_drbg_context *ctx,
454 const unsigned char *additional,
455 size_t len);
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000456
457/**
Rose Zadikc9474eb2018-03-27 10:58:22 +0100458 * \brief This function updates the state of the CTR_DRBG context.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000459 *
Gilles Peskinef305d922021-02-09 18:44:18 +0100460 * \note This function is not thread-safe. It is not safe
461 * to call this function if another thread might be
462 * concurrently obtaining random numbers from the same
463 * context or updating or reseeding the same context.
464 *
Rose Zadikc9474eb2018-03-27 10:58:22 +0100465 * \param ctx The CTR_DRBG context.
Gilles Peskine08875d42019-09-24 14:40:40 +0200466 * \param additional The data to update the state with. This must not be
Gilles Peskine10f16ac2019-10-01 18:30:02 +0200467 * \c NULL unless \p add_len is \c 0.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500468 * \param add_len Length of \p additional in bytes. This must be at
469 * most #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT.
Rose Zadikc9474eb2018-03-27 10:58:22 +0100470 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500471 * \return \c 0 on success.
472 * \return #MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG if
473 * \p add_len is more than
474 * #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT.
475 * \return An error from the underlying AES cipher on failure.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000476 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200477int mbedtls_ctr_drbg_update(mbedtls_ctr_drbg_context *ctx,
478 const unsigned char *additional,
479 size_t add_len);
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000480
481/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000482 * \brief This function updates a CTR_DRBG instance with additional
483 * data and uses it to generate random data.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000484 *
Gilles Peskine2d8f0692019-10-01 18:31:28 +0200485 * This function automatically reseeds if the reseed counter is exceeded
486 * or prediction resistance is enabled.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000487 *
Gilles Peskinef305d922021-02-09 18:44:18 +0100488 * \note This function is not thread-safe. It is not safe
489 * to call this function if another thread might be
490 * concurrently obtaining random numbers from the same
491 * context or updating or reseeding the same context.
492 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000493 * \param p_rng The CTR_DRBG context. This must be a pointer to a
494 * #mbedtls_ctr_drbg_context structure.
495 * \param output The buffer to fill.
Gilles Peskineec51dd12019-09-30 15:01:02 +0200496 * \param output_len The length of the buffer in bytes.
Gilles Peskine10f16ac2019-10-01 18:30:02 +0200497 * \param additional Additional data to update. Can be \c NULL, in which
Gilles Peskine08875d42019-09-24 14:40:40 +0200498 * case the additional data is empty regardless of
499 * the value of \p add_len.
500 * \param add_len The length of the additional data
Gilles Peskine10f16ac2019-10-01 18:30:02 +0200501 * if \p additional is not \c NULL.
Gilles Peskine944bc582019-09-24 14:48:30 +0200502 * This must be less than #MBEDTLS_CTR_DRBG_MAX_INPUT
503 * and less than
504 * #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT - \c entropy_len
505 * where \c entropy_len is the entropy length
506 * configured for the context.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000507 *
Rose Zadikc9474eb2018-03-27 10:58:22 +0100508 * \return \c 0 on success.
509 * \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or
Rose Zadik2f8163d2018-01-25 21:55:14 +0000510 * #MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG on failure.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000511 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200512int mbedtls_ctr_drbg_random_with_add(void *p_rng,
513 unsigned char *output,
514 size_t output_len,
515 const unsigned char *additional,
516 size_t add_len);
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000517
518/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000519 * \brief This function uses CTR_DRBG to generate random data.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000520 *
Gilles Peskine2d8f0692019-10-01 18:31:28 +0200521 * This function automatically reseeds if the reseed counter is exceeded
522 * or prediction resistance is enabled.
Gilles Peskinef305d922021-02-09 18:44:18 +0100523 */
524#if defined(MBEDTLS_THREADING_C)
525/**
526 * \note When Mbed TLS is built with threading support,
527 * it is safe to call mbedtls_ctr_drbg_random()
528 * from multiple threads. Other operations, including
529 * reseeding, are not thread-safe.
530 */
531#endif /* MBEDTLS_THREADING_C */
532/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000533 * \param p_rng The CTR_DRBG context. This must be a pointer to a
534 * #mbedtls_ctr_drbg_context structure.
535 * \param output The buffer to fill.
Gilles Peskine944bc582019-09-24 14:48:30 +0200536 * \param output_len The length of the buffer in bytes.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000537 *
Rose Zadikc9474eb2018-03-27 10:58:22 +0100538 * \return \c 0 on success.
539 * \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or
Rose Zadik2f8163d2018-01-25 21:55:14 +0000540 * #MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG on failure.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000541 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200542int mbedtls_ctr_drbg_random(void *p_rng,
543 unsigned char *output,
544 size_t output_len);
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546#if defined(MBEDTLS_FS_IO)
Paul Bakkerfc754a92011-12-05 13:23:51 +0000547/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000548 * \brief This function writes a seed file.
Paul Bakkerfc754a92011-12-05 13:23:51 +0000549 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000550 * \param ctx The CTR_DRBG context.
551 * \param path The name of the file.
Paul Bakkerfc754a92011-12-05 13:23:51 +0000552 *
Rose Zadikc9474eb2018-03-27 10:58:22 +0100553 * \return \c 0 on success.
Rose Zadikf25eb6e2018-04-16 14:51:52 +0100554 * \return #MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR on file error.
Gilles Peskineec51dd12019-09-30 15:01:02 +0200555 * \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED on reseed
Rose Zadik2f8163d2018-01-25 21:55:14 +0000556 * failure.
Paul Bakkerfc754a92011-12-05 13:23:51 +0000557 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200558int mbedtls_ctr_drbg_write_seed_file(mbedtls_ctr_drbg_context *ctx,
559 const char *path);
Paul Bakkerfc754a92011-12-05 13:23:51 +0000560
561/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000562 * \brief This function reads and updates a seed file. The seed
563 * is added to this instance.
Paul Bakkerfc754a92011-12-05 13:23:51 +0000564 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000565 * \param ctx The CTR_DRBG context.
566 * \param path The name of the file.
Paul Bakkerfc754a92011-12-05 13:23:51 +0000567 *
Rose Zadikc9474eb2018-03-27 10:58:22 +0100568 * \return \c 0 on success.
569 * \return #MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR on file error.
Gilles Peskineec51dd12019-09-30 15:01:02 +0200570 * \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED on
571 * reseed failure.
572 * \return #MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG if the existing
573 * seed file is too large.
Paul Bakkerfc754a92011-12-05 13:23:51 +0000574 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200575int mbedtls_ctr_drbg_update_seed_file(mbedtls_ctr_drbg_context *ctx,
576 const char *path);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577#endif /* MBEDTLS_FS_IO */
Paul Bakkerfc754a92011-12-05 13:23:51 +0000578
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500579#if defined(MBEDTLS_SELF_TEST)
580
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000581/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000582 * \brief The CTR_DRBG checkup routine.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000583 *
Rose Zadikc9474eb2018-03-27 10:58:22 +0100584 * \return \c 0 on success.
585 * \return \c 1 on failure.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000586 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200587int mbedtls_ctr_drbg_self_test(int verbose);
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000588
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500589#endif /* MBEDTLS_SELF_TEST */
590
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000591#ifdef __cplusplus
592}
593#endif
594
595#endif /* ctr_drbg.h */