blob: 6626024aaef0b24090fdbc685a1d6a8bcd5f66b5 [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
12 * as the underlying block cipher, with a derivation function. The security
13 * strength is:
14 * - 256 bits under the default configuration of the library,
15 * with #MBEDTLS_CTR_DRBG_ENTROPY_LEN set to 48 or more.
16 * - 256 bits if #MBEDTLS_CTR_DRBG_ENTROPY_LEN is set
17 * to 32 or more and the DRBG is initialized with an explicit
18 * nonce in the \c custom parameter to mbedtls_ctr_drbg_seed().
19 * - 128 bits if #MBEDTLS_CTR_DRBG_ENTROPY_LEN is
20 * between 24 and 47 and the DRBG is not initialized with an explicit
21 * nonce (see mbedtls_ctr_drbg_seed()).
22 *
23 * Note that the value of #MBEDTLS_CTR_DRBG_ENTROPY_LEN defaults to:
24 * - \c 48 if the module #MBEDTLS_SHA512_C is enabled and the symbol
25 * #MBEDTLS_ENTROPY_FORCE_SHA256 is not enabled at compile time.
26 * This is the default configuration of the library.
27 * - \c 32 if the module #MBEDTLS_SHA512_C is disabled at compile time.
28 * - \c 32 if #MBEDTLS_ENTROPY_FORCE_SHA256 is enabled at compile time.
Darryl Greena40a1012018-01-05 15:33:17 +000029 */
30/*
Gilles Peskine25e19452019-10-02 18:54:20 +020031 * Copyright (C) 2006-2019, Arm Limited (or its affiliates), All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020032 * SPDX-License-Identifier: Apache-2.0
33 *
34 * Licensed under the Apache License, Version 2.0 (the "License"); you may
35 * not use this file except in compliance with the License.
36 * You may obtain a copy of the License at
37 *
38 * http://www.apache.org/licenses/LICENSE-2.0
39 *
40 * Unless required by applicable law or agreed to in writing, software
41 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
42 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
43 * See the License for the specific language governing permissions and
44 * limitations under the License.
Paul Bakker0e04d0e2011-11-27 14:46:59 +000045 *
Rose Zadik2f8163d2018-01-25 21:55:14 +000046 * This file is part of Mbed TLS (https://tls.mbed.org)
Paul Bakker0e04d0e2011-11-27 14:46:59 +000047 */
Rose Zadik2f8163d2018-01-25 21:55:14 +000048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#ifndef MBEDTLS_CTR_DRBG_H
50#define MBEDTLS_CTR_DRBG_H
Paul Bakker0e04d0e2011-11-27 14:46:59 +000051
Ron Eldor0559c662018-02-14 16:02:41 +020052#if !defined(MBEDTLS_CONFIG_FILE)
53#include "config.h"
54#else
55#include MBEDTLS_CONFIG_FILE
56#endif
57
Paul Bakker0e04d0e2011-11-27 14:46:59 +000058#include "aes.h"
59
Manuel Pégourié-Gonnard0a4fb092015-05-07 12:50:31 +010060#if defined(MBEDTLS_THREADING_C)
Ron Eldordf9b93e2017-05-14 16:17:33 +030061#include "threading.h"
Manuel Pégourié-Gonnard0a4fb092015-05-07 12:50:31 +010062#endif
63
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#define MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED -0x0034 /**< The entropy source failed. */
Rose Zadik2f8163d2018-01-25 21:55:14 +000065#define MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG -0x0036 /**< The requested random buffer length is too big. */
66#define MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG -0x0038 /**< The input (entropy + additional data) is too large. */
67#define MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR -0x003A /**< Read or write error in file. */
Paul Bakker0e04d0e2011-11-27 14:46:59 +000068
Rose Zadik2f8163d2018-01-25 21:55:14 +000069#define MBEDTLS_CTR_DRBG_BLOCKSIZE 16 /**< The block size used by the cipher. */
70#define MBEDTLS_CTR_DRBG_KEYSIZE 32 /**< The key size used by the cipher. */
71#define MBEDTLS_CTR_DRBG_KEYBITS ( MBEDTLS_CTR_DRBG_KEYSIZE * 8 ) /**< The key size for the DRBG operation, in bits. */
72#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 +020073
Paul Bakker088c5c52014-04-25 11:11:10 +020074/**
75 * \name SECTION: Module settings
76 *
77 * The configuration options you can set for this module are in this section.
Rose Zadik2f8163d2018-01-25 21:55:14 +000078 * Either change them in config.h or define them using the compiler command
79 * line.
Paul Bakker088c5c52014-04-25 11:11:10 +020080 * \{
81 */
82
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083#if !defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN)
84#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256)
Gilles Peskineeb99c102019-10-02 18:56:17 +020085/** The amount of entropy used per seed by default.
86 *
87 * This is 48 bytes because the entropy module uses SHA-512
88 * (#MBEDTLS_ENTROPY_FORCE_SHA256 is not set).
89 *
90 * \note See mbedtls_ctr_drbg_set_entropy_len() regarding what values are
91 * acceptable.
92 */
Rose Zadik2f8163d2018-01-25 21:55:14 +000093#define MBEDTLS_CTR_DRBG_ENTROPY_LEN 48
Paul Bakkerfb08fd22013-08-27 15:06:26 +020094#else
Gilles Peskineeb99c102019-10-02 18:56:17 +020095/** The amount of entropy used per seed by default.
96 *
97 * This is 32 bytes because the entropy module uses SHA-256
98 * (the SHA512 module is disabled or #MBEDTLS_ENTROPY_FORCE_SHA256 is set).
99 *
100 * \note See mbedtls_ctr_drbg_set_entropy_len() regarding what values are
101 * acceptable.
Rose Zadik2f8163d2018-01-25 21:55:14 +0000102 */
Gilles Peskineeb99c102019-10-02 18:56:17 +0200103#define MBEDTLS_CTR_DRBG_ENTROPY_LEN 32
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200104#endif
Paul Bakker088c5c52014-04-25 11:11:10 +0200105#endif
106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107#if !defined(MBEDTLS_CTR_DRBG_RESEED_INTERVAL)
Rose Zadik2f8163d2018-01-25 21:55:14 +0000108#define MBEDTLS_CTR_DRBG_RESEED_INTERVAL 10000
109/**< The interval before reseed is performed by default. */
Paul Bakker088c5c52014-04-25 11:11:10 +0200110#endif
111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112#if !defined(MBEDTLS_CTR_DRBG_MAX_INPUT)
Rose Zadik2f8163d2018-01-25 21:55:14 +0000113#define MBEDTLS_CTR_DRBG_MAX_INPUT 256
114/**< The maximum number of additional input Bytes. */
Paul Bakker088c5c52014-04-25 11:11:10 +0200115#endif
116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117#if !defined(MBEDTLS_CTR_DRBG_MAX_REQUEST)
Rose Zadik2f8163d2018-01-25 21:55:14 +0000118#define MBEDTLS_CTR_DRBG_MAX_REQUEST 1024
119/**< The maximum number of requested Bytes per call. */
Paul Bakker088c5c52014-04-25 11:11:10 +0200120#endif
121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122#if !defined(MBEDTLS_CTR_DRBG_MAX_SEED_INPUT)
Rose Zadik2f8163d2018-01-25 21:55:14 +0000123#define MBEDTLS_CTR_DRBG_MAX_SEED_INPUT 384
Gilles Peskine25e19452019-10-02 18:54:20 +0200124/**< The maximum size of seed or reseed buffer in bytes. */
Paul Bakker088c5c52014-04-25 11:11:10 +0200125#endif
126
127/* \} name SECTION: Module settings */
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000128
Rose Zadik2f8163d2018-01-25 21:55:14 +0000129#define MBEDTLS_CTR_DRBG_PR_OFF 0
130/**< Prediction resistance is disabled. */
131#define MBEDTLS_CTR_DRBG_PR_ON 1
132/**< Prediction resistance is enabled. */
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000133
134#ifdef __cplusplus
135extern "C" {
136#endif
137
138/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000139 * \brief The CTR_DRBG context structure.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000140 */
141typedef struct
142{
Rose Zadik2f8163d2018-01-25 21:55:14 +0000143 unsigned char counter[16]; /*!< The counter (V). */
144 int reseed_counter; /*!< The reseed counter. */
145 int prediction_resistance; /*!< This determines whether prediction
146 resistance is enabled, that is
147 whether to systematically reseed before
148 each random generation. */
149 size_t entropy_len; /*!< The amount of entropy grabbed on each
150 seed or reseed operation. */
151 int reseed_interval; /*!< The reseed interval. */
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000152
Rose Zadik2f8163d2018-01-25 21:55:14 +0000153 mbedtls_aes_context aes_ctx; /*!< The AES context. */
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000154
155 /*
156 * Callbacks (Entropy)
157 */
158 int (*f_entropy)(void *, unsigned char *, size_t);
Rose Zadik2f8163d2018-01-25 21:55:14 +0000159 /*!< The entropy callback function. */
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000160
Rose Zadik2f8163d2018-01-25 21:55:14 +0000161 void *p_entropy; /*!< The context for the entropy function. */
Manuel Pégourié-Gonnard0a4fb092015-05-07 12:50:31 +0100162
163#if defined(MBEDTLS_THREADING_C)
164 mbedtls_threading_mutex_t mutex;
165#endif
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000166}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167mbedtls_ctr_drbg_context;
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000168
169/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000170 * \brief This function initializes the CTR_DRBG context,
171 * and prepares it for mbedtls_ctr_drbg_seed()
172 * or mbedtls_ctr_drbg_free().
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200173 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000174 * \param ctx The CTR_DRBG context to initialize.
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200175 */
176void mbedtls_ctr_drbg_init( mbedtls_ctr_drbg_context *ctx );
177
178/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000179 * \brief This function seeds and sets up the CTR_DRBG
180 * entropy source for future reseeds.
Paul Bakker9af723c2014-05-01 13:03:14 +0200181 *
Gilles Peskine25e19452019-10-02 18:54:20 +0200182 * You can provide a personalization string in addition to the
183 * entropy source, to make this instantiation as unique as possible.
184 *
Gilles Peskineeb99c102019-10-02 18:56:17 +0200185 * \note The _seed_material_ value passed to the derivation
186 * function in the CTR_DRBG Instantiate Process
187 * described in NIST SP 800-90A §10.2.1.3.2
188 * is the concatenation of the string obtained from
189 * calling \p f_entropy and the \p custom string.
190 * The origin of the nonce depends on the value of
191 * the entropy length relative to the security strength.
192 * See the documentation of
193 * mbedtls_ctr_drbg_set_entropy_len() for information
194 * about the entropy length.
195 * - If the entropy length is at least 1.5 times the
196 * security strength then the nonce is taken from the
197 * string obtained with \p f_entropy.
198 * - If the entropy length is less than the security
199 * strength, then the nonce is taken from \p custom.
200 * In this case, for compliance with SP 800-90A,
201 * you must pass a unique value of \p custom at
202 * each invocation. See SP 800-90A §8.6.7 for more
203 * details.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000204 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000205 * \param ctx The CTR_DRBG context to seed.
206 * \param f_entropy The entropy callback, taking as arguments the
207 * \p p_entropy context, the buffer to fill, and the
Gilles Peskine25e19452019-10-02 18:54:20 +0200208 * length of the buffer.
209 * \param p_entropy The entropy context to pass to \p f_entropy.
210 * \param custom The personalization string.
211 * This can be \c NULL, in which case the personalization
212 * string is empty regardless of the value of \p len.
213 * \param len The length of the personalization string.
214 * This must be at most
215 * #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT / 2.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000216 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000217 * \return \c 0 on success, or
218 * #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED on failure.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000219 */
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200220int mbedtls_ctr_drbg_seed( mbedtls_ctr_drbg_context *ctx,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000221 int (*f_entropy)(void *, unsigned char *, size_t),
222 void *p_entropy,
Paul Bakker1bc9efc2011-12-03 11:29:32 +0000223 const unsigned char *custom,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000224 size_t len );
225
226/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000227 * \brief This function clears CTR_CRBG context data.
Paul Bakkerfff03662014-06-18 16:21:25 +0200228 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000229 * \param ctx The CTR_DRBG context to clear.
Paul Bakkerfff03662014-06-18 16:21:25 +0200230 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231void mbedtls_ctr_drbg_free( mbedtls_ctr_drbg_context *ctx );
Paul Bakkerfff03662014-06-18 16:21:25 +0200232
233/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000234 * \brief This function turns prediction resistance on or off.
235 * The default value is off.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000236 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000237 * \note If enabled, entropy is gathered at the beginning of
Gilles Peskine25e19452019-10-02 18:54:20 +0200238 * every call to mbedtls_ctr_drbg_random_with_add()
239 * or mbedtls_ctr_drbg_random().
Rose Zadik2f8163d2018-01-25 21:55:14 +0000240 * Only use this if your entropy source has sufficient
241 * throughput.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000242 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000243 * \param ctx The CTR_DRBG context.
244 * \param resistance #MBEDTLS_CTR_DRBG_PR_ON or #MBEDTLS_CTR_DRBG_PR_OFF.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000245 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246void mbedtls_ctr_drbg_set_prediction_resistance( mbedtls_ctr_drbg_context *ctx,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000247 int resistance );
248
249/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000250 * \brief This function sets the amount of entropy grabbed on each
Gilles Peskineeb99c102019-10-02 18:56:17 +0200251 * seed or reseed.
252 *
253 * The default value is #MBEDTLS_CTR_DRBG_ENTROPY_LEN.
254 *
255 * \note For compliance with NIST SP 800-90A, the entropy length
256 * (\p len bytes = \p len * 8 bits)
257 * must be at least the security strength.
258 * Furthermore, if the entropy input is used to provide
259 * the nonce, the entropy length must be 1.5 times
260 * the security strength.
261 * See NIST SP 800-57A table 2.
262 *
263 * To achieve 256-bit security,
264 * the entropy input must be at least:
265 * - 48 bytes if the \p custom argument to
266 * mbedtls_ctr_drbg_seed() may repeat (for example
267 * because it is empty, or more generally constant);
268 * - 32 bytes if the \p custom argument to
269 * mbedtls_ctr_drbg_seed() includes a nonce.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000270 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000271 * \param ctx The CTR_DRBG context.
Gilles Peskine25e19452019-10-02 18:54:20 +0200272 * \param len The amount of entropy to grab, in bytes.
273 * This must be at most #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000274 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275void mbedtls_ctr_drbg_set_entropy_len( mbedtls_ctr_drbg_context *ctx,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000276 size_t len );
277
278/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000279 * \brief This function sets the reseed interval.
Gilles Peskine25e19452019-10-02 18:54:20 +0200280 *
281 * The reseed interval is the number of calls to mbedtls_ctr_drbg_random()
282 * or mbedtls_ctr_drbg_random_with_add() after which the entropy function
283 * is called again.
284 *
285 * The default value is #MBEDTLS_CTR_DRBG_RESEED_INTERVAL.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000286 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000287 * \param ctx The CTR_DRBG context.
288 * \param interval The reseed interval.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000289 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290void mbedtls_ctr_drbg_set_reseed_interval( mbedtls_ctr_drbg_context *ctx,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000291 int interval );
292
293/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000294 * \brief This function reseeds the CTR_DRBG context, that is
295 * extracts data from the entropy source.
Paul Bakker9af723c2014-05-01 13:03:14 +0200296 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000297 * \param ctx The CTR_DRBG context.
Gilles Peskine25e19452019-10-02 18:54:20 +0200298 * \param additional Additional data to add to the state. Can be \c NULL.
Rose Zadik2f8163d2018-01-25 21:55:14 +0000299 * \param len The length of the additional data.
Gilles Peskine25e19452019-10-02 18:54:20 +0200300 * This must be less than
301 * #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT - \c entropy_len
302 * where \c entropy_len is the entropy length
303 * configured for the context.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000304 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000305 * \return \c 0 on success, or
306 * #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED on failure.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000307 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308int mbedtls_ctr_drbg_reseed( mbedtls_ctr_drbg_context *ctx,
Paul Bakker1bc9efc2011-12-03 11:29:32 +0000309 const unsigned char *additional, size_t len );
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000310
311/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000312 * \brief This function updates the state of the CTR_DRBG context.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000313 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000314 * \param ctx The CTR_DRBG context.
Gilles Peskine25e19452019-10-02 18:54:20 +0200315 * \param additional The data to update the state with. This must not be
316 * \c NULL unless \p add_len is \c 0.
Gilles Peskine9ce29722018-09-11 16:41:54 +0200317 * \param add_len Length of \p additional in bytes. This must be at
318 * most #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT.
Manuel Pégourié-Gonnard5cb4b312014-11-25 17:41:50 +0100319 *
Gilles Peskine9ce29722018-09-11 16:41:54 +0200320 * \return \c 0 on success.
321 * \return #MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG if
322 * \p add_len is more than
323 * #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT.
324 * \return An error from the underlying AES cipher on failure.
325 */
326int mbedtls_ctr_drbg_update_ret( mbedtls_ctr_drbg_context *ctx,
327 const unsigned char *additional,
328 size_t add_len );
329
330/**
331 * \brief This function updates the state of the CTR_DRBG context.
332 *
333 * \warning This function cannot report errors. You should use
334 * mbedtls_ctr_drbg_update_ret() instead.
335 *
336 * \note If \p add_len is greater than
337 * #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT, only the first
338 * #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT Bytes are used.
339 * The remaining Bytes are silently discarded.
340 *
341 * \param ctx The CTR_DRBG context.
Gilles Peskine25e19452019-10-02 18:54:20 +0200342 * \param additional The data to update the state with. This must not be
343 * \c NULL unless \p add_len is \c 0.
344 * \param add_len Length of \p additional data. This must be at
345 * most #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000346 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347void mbedtls_ctr_drbg_update( mbedtls_ctr_drbg_context *ctx,
Gilles Peskine9ce29722018-09-11 16:41:54 +0200348 const unsigned char *additional,
349 size_t add_len );
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000350
351/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000352 * \brief This function updates a CTR_DRBG instance with additional
353 * data and uses it to generate random data.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000354 *
Gilles Peskine25e19452019-10-02 18:54:20 +0200355 * This function automatically reseeds if the reseed counter is exceeded
356 * or prediction resistance is enabled.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000357 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000358 * \param p_rng The CTR_DRBG context. This must be a pointer to a
359 * #mbedtls_ctr_drbg_context structure.
360 * \param output The buffer to fill.
Gilles Peskine25e19452019-10-02 18:54:20 +0200361 * \param output_len The length of the buffer in bytes.
362 * \param additional Additional data to update. Can be \c NULL, in which
363 * case the additional data is empty regardless of
364 * the value of \p add_len.
365 * \param add_len The length of the additional data
366 * if \p additional is not \c NULL.
367 * This must be less than #MBEDTLS_CTR_DRBG_MAX_INPUT
368 * and less than
369 * #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT - \c entropy_len
370 * where \c entropy_len is the entropy length
371 * configured for the context.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000372 *
Gilles Peskine25e19452019-10-02 18:54:20 +0200373 * \return \c 0 on success.
374 * \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or
Rose Zadik2f8163d2018-01-25 21:55:14 +0000375 * #MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG on failure.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000376 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377int mbedtls_ctr_drbg_random_with_add( void *p_rng,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000378 unsigned char *output, size_t output_len,
Paul Bakker1bc9efc2011-12-03 11:29:32 +0000379 const unsigned char *additional, size_t add_len );
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000380
381/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000382 * \brief This function uses CTR_DRBG to generate random data.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000383 *
Gilles Peskine25e19452019-10-02 18:54:20 +0200384 * This function automatically reseeds if the reseed counter is exceeded
385 * or prediction resistance is enabled.
386 *
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000387 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000388 * \param p_rng The CTR_DRBG context. This must be a pointer to a
389 * #mbedtls_ctr_drbg_context structure.
390 * \param output The buffer to fill.
Gilles Peskine25e19452019-10-02 18:54:20 +0200391 * \param output_len The length of the buffer in bytes.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000392 *
Gilles Peskine25e19452019-10-02 18:54:20 +0200393 * \return \c 0 on success.
394 * \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or
Rose Zadik2f8163d2018-01-25 21:55:14 +0000395 * #MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG on failure.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000396 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397int mbedtls_ctr_drbg_random( void *p_rng,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000398 unsigned char *output, size_t output_len );
399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400#if defined(MBEDTLS_FS_IO)
Paul Bakkerfc754a92011-12-05 13:23:51 +0000401/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000402 * \brief This function writes a seed file.
Paul Bakkerfc754a92011-12-05 13:23:51 +0000403 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000404 * \param ctx The CTR_DRBG context.
405 * \param path The name of the file.
Paul Bakkerfc754a92011-12-05 13:23:51 +0000406 *
Gilles Peskine25e19452019-10-02 18:54:20 +0200407 * \return \c 0 on success.
408 * \return #MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR on file error.
409 * \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED on reseed
Rose Zadik2f8163d2018-01-25 21:55:14 +0000410 * failure.
Paul Bakkerfc754a92011-12-05 13:23:51 +0000411 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412int mbedtls_ctr_drbg_write_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
Paul Bakkerfc754a92011-12-05 13:23:51 +0000413
414/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000415 * \brief This function reads and updates a seed file. The seed
416 * is added to this instance.
Paul Bakkerfc754a92011-12-05 13:23:51 +0000417 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000418 * \param ctx The CTR_DRBG context.
419 * \param path The name of the file.
Paul Bakkerfc754a92011-12-05 13:23:51 +0000420 *
Gilles Peskine25e19452019-10-02 18:54:20 +0200421 * \return \c 0 on success.
422 * \return #MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR on file error.
423 * \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED on
424 * reseed failure.
425 * \return #MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG if the existing
426 * seed file is too large.
Paul Bakkerfc754a92011-12-05 13:23:51 +0000427 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428int mbedtls_ctr_drbg_update_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
429#endif /* MBEDTLS_FS_IO */
Paul Bakkerfc754a92011-12-05 13:23:51 +0000430
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000431/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000432 * \brief The CTR_DRBG checkup routine.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000433 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000434 * \return \c 0 on success, or \c 1 on failure.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000435 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436int mbedtls_ctr_drbg_self_test( int verbose );
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000437
Paul Bakker534f82c2013-06-25 16:47:55 +0200438/* Internal functions (do not call directly) */
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200439int mbedtls_ctr_drbg_seed_entropy_len( mbedtls_ctr_drbg_context *,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200440 int (*)(void *, unsigned char *, size_t), void *,
441 const unsigned char *, size_t, size_t );
Paul Bakker534f82c2013-06-25 16:47:55 +0200442
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000443#ifdef __cplusplus
444}
445#endif
446
447#endif /* ctr_drbg.h */