blob: 1a9f5e80f2336d3d8a75808c40679ea4d27140d2 [file] [log] [blame]
Paul Bakker0e04d0e2011-11-27 14:46:59 +00001/**
2 * \file ctr_drbg.h
3 *
Rose Zadikc9474eb2018-03-27 10:58:22 +01004 * \brief This file contains CTR_DRBG definitions and functions.
5 *
Rose Zadikf25eb6e2018-04-16 14:51:52 +01006 * 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 *
Nir Sonnenscheineb73f7a2018-07-30 17:46:49 +030011 * The Mbed TLS implementation of CTR_DRBG uses AES-256 (default) or AES-128
Gilles Peskine223deea2019-09-24 14:48:53 +020012 * as the underlying block cipher, with a derivation function. The security
13 * strength is:
14 * - 256 bits under the default configuration of the library, with AES-256
15 * (`MBEDTLS_CTR_DRBG_USE_128_BIT_KEY` not set) and
16 * with #MBEDTLS_CTR_DRBG_ENTROPY_LEN set to 48 or more.
17 * - 256 bits if AES-256 is used, #MBEDTLS_CTR_DRBG_ENTROPY_LEN is set
18 * to 32 or more, and the DRBG is initialized with an explicit
19 * nonce in the \c custom parameter to mbedtls_ctr_drbg_seed().
20 * - 128 bits if AES-256 is used but #MBEDTLS_CTR_DRBG_ENTROPY_LEN is
21 * between 24 and 47 and the DRBG is not initialized with an explicit
22 * nonce (see mbedtls_ctr_drbg_seed()).
23 * - 128 bits if AES-128 is used (`MBEDTLS_CTR_DRBG_USE_128_BIT_KEY` set)
24 * and #MBEDTLS_CTR_DRBG_ENTROPY_LEN is set to 24 or more (which is
25 * always the case unless it is explicitly set to a different value
26 * in `config.h`).
Nir Sonnenscheineb73f7a2018-07-30 17:46:49 +030027 *
Gilles Peskine223deea2019-09-24 14:48:53 +020028 * \warning Using 128-bit keys for CTR_DRBG or using SHA-256 as the entropy
29 * compression function limits the security of generated
Nir Sonnenscheinb7ebbca2018-08-29 10:20:12 +030030 * keys and operations that use random values generated to 128-bit security.
Darryl Greena40a1012018-01-05 15:33:17 +000031 */
32/*
Gilles Peskine08875d42019-09-24 14:40:40 +020033 * Copyright (C) 2006-2019, Arm Limited (or its affiliates), All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020034 * SPDX-License-Identifier: Apache-2.0
35 *
36 * Licensed under the Apache License, Version 2.0 (the "License"); you may
37 * not use this file except in compliance with the License.
38 * You may obtain a copy of the License at
39 *
40 * http://www.apache.org/licenses/LICENSE-2.0
41 *
42 * Unless required by applicable law or agreed to in writing, software
43 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
44 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
45 * See the License for the specific language governing permissions and
46 * limitations under the License.
Paul Bakker0e04d0e2011-11-27 14:46:59 +000047 *
Rose Zadik2f8163d2018-01-25 21:55:14 +000048 * This file is part of Mbed TLS (https://tls.mbed.org)
Paul Bakker0e04d0e2011-11-27 14:46:59 +000049 */
Rose Zadik2f8163d2018-01-25 21:55:14 +000050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#ifndef MBEDTLS_CTR_DRBG_H
52#define MBEDTLS_CTR_DRBG_H
Paul Bakker0e04d0e2011-11-27 14:46:59 +000053
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050054#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010055#include "mbedtls/config.h"
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050056#else
57#include MBEDTLS_CONFIG_FILE
58#endif
59
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010060#include "mbedtls/aes.h"
Paul Bakker0e04d0e2011-11-27 14:46:59 +000061
Manuel Pégourié-Gonnard0a4fb092015-05-07 12:50:31 +010062#if defined(MBEDTLS_THREADING_C)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010063#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard0a4fb092015-05-07 12:50:31 +010064#endif
65
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#define MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED -0x0034 /**< The entropy source failed. */
Rose Zadik2f8163d2018-01-25 21:55:14 +000067#define MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG -0x0036 /**< The requested random buffer length is too big. */
68#define MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG -0x0038 /**< The input (entropy + additional data) is too large. */
69#define MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR -0x003A /**< Read or write error in file. */
Paul Bakker0e04d0e2011-11-27 14:46:59 +000070
Rose Zadik2f8163d2018-01-25 21:55:14 +000071#define MBEDTLS_CTR_DRBG_BLOCKSIZE 16 /**< The block size used by the cipher. */
Nir Sonnenscheince266e42018-08-29 10:11:46 +030072
Nir Sonnenschein43e4ff02018-09-03 14:15:46 +030073#if defined(MBEDTLS_CTR_DRBG_USE_128_BIT_KEY)
Gilles Peskine08875d42019-09-24 14:40:40 +020074#define MBEDTLS_CTR_DRBG_KEYSIZE 16
75/**< The key size in bytes used by the cipher.
76 *
77 * Compile-time choice: 16 bytes (128 bits)
78 * because #MBEDTLS_CTR_DRBG_USE_128_BIT_KEY is set.
79 */
Nir Sonnenscheineb73f7a2018-07-30 17:46:49 +030080#else
Gilles Peskine08875d42019-09-24 14:40:40 +020081#define MBEDTLS_CTR_DRBG_KEYSIZE 32
82/**< The key size in bytes used by the cipher.
83 *
84 * Compile-time choice: 32 bytes (256 bits)
85 * because `MBEDTLS_CTR_DRBG_USE_128_BIT_KEY` is not set.
86 */
Nir Sonnenscheina4588d42018-07-30 16:59:36 +030087#endif
Nir Sonnenschein43e4ff02018-09-03 14:15:46 +030088
Rose Zadik2f8163d2018-01-25 21:55:14 +000089#define MBEDTLS_CTR_DRBG_KEYBITS ( MBEDTLS_CTR_DRBG_KEYSIZE * 8 ) /**< The key size for the DRBG operation, in bits. */
90#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 +020091
Paul Bakker088c5c52014-04-25 11:11:10 +020092/**
93 * \name SECTION: Module settings
94 *
95 * The configuration options you can set for this module are in this section.
Rose Zadik2f8163d2018-01-25 21:55:14 +000096 * Either change them in config.h or define them using the compiler command
97 * line.
Paul Bakker088c5c52014-04-25 11:11:10 +020098 * \{
99 */
100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101#if !defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN)
102#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256)
Gilles Peskine08875d42019-09-24 14:40:40 +0200103/** The amount of entropy used per seed by default.
104 *
105 * This is 48 bytes because the entropy module uses SHA-512
106 * (`MBEDTLS_ENTROPY_FORCE_SHA256` is not set).
107 *
108 * \note See mbedtls_ctr_drbg_set_entropy_len() regarding what values are
109 * acceptable.
110 */
Rose Zadik2f8163d2018-01-25 21:55:14 +0000111#define MBEDTLS_CTR_DRBG_ENTROPY_LEN 48
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200112#else
Gilles Peskine08875d42019-09-24 14:40:40 +0200113/** The amount of entropy used per seed by default.
114 *
115 * This is 32 bytes because the entropy module uses SHA-256
116 * (the SHA-512 module is disabled or `MBEDTLS_ENTROPY_FORCE_SHA256` is set).
117 *
118 * \note See mbedtls_ctr_drbg_set_entropy_len() regarding what values are
119 * acceptable.
Rose Zadik2f8163d2018-01-25 21:55:14 +0000120 */
Gilles Peskine08875d42019-09-24 14:40:40 +0200121#define MBEDTLS_CTR_DRBG_ENTROPY_LEN 32
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200122#endif
Paul Bakker088c5c52014-04-25 11:11:10 +0200123#endif
124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125#if !defined(MBEDTLS_CTR_DRBG_RESEED_INTERVAL)
Rose Zadik2f8163d2018-01-25 21:55:14 +0000126#define MBEDTLS_CTR_DRBG_RESEED_INTERVAL 10000
127/**< The interval before reseed is performed by default. */
Paul Bakker088c5c52014-04-25 11:11:10 +0200128#endif
129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130#if !defined(MBEDTLS_CTR_DRBG_MAX_INPUT)
Rose Zadik2f8163d2018-01-25 21:55:14 +0000131#define MBEDTLS_CTR_DRBG_MAX_INPUT 256
132/**< The maximum number of additional input Bytes. */
Paul Bakker088c5c52014-04-25 11:11:10 +0200133#endif
134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135#if !defined(MBEDTLS_CTR_DRBG_MAX_REQUEST)
Rose Zadik2f8163d2018-01-25 21:55:14 +0000136#define MBEDTLS_CTR_DRBG_MAX_REQUEST 1024
137/**< The maximum number of requested Bytes per call. */
Paul Bakker088c5c52014-04-25 11:11:10 +0200138#endif
139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140#if !defined(MBEDTLS_CTR_DRBG_MAX_SEED_INPUT)
Rose Zadik2f8163d2018-01-25 21:55:14 +0000141#define MBEDTLS_CTR_DRBG_MAX_SEED_INPUT 384
Gilles Peskine08875d42019-09-24 14:40:40 +0200142/**< The maximum size of seed or reseed buffer in bytes. */
Paul Bakker088c5c52014-04-25 11:11:10 +0200143#endif
144
145/* \} name SECTION: Module settings */
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000146
Rose Zadik2f8163d2018-01-25 21:55:14 +0000147#define MBEDTLS_CTR_DRBG_PR_OFF 0
148/**< Prediction resistance is disabled. */
149#define MBEDTLS_CTR_DRBG_PR_ON 1
150/**< Prediction resistance is enabled. */
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000151
152#ifdef __cplusplus
153extern "C" {
154#endif
155
156/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000157 * \brief The CTR_DRBG context structure.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000158 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200159typedef struct mbedtls_ctr_drbg_context
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000160{
Rose Zadik2f8163d2018-01-25 21:55:14 +0000161 unsigned char counter[16]; /*!< The counter (V). */
162 int reseed_counter; /*!< The reseed counter. */
163 int prediction_resistance; /*!< This determines whether prediction
164 resistance is enabled, that is
165 whether to systematically reseed before
166 each random generation. */
167 size_t entropy_len; /*!< The amount of entropy grabbed on each
168 seed or reseed operation. */
169 int reseed_interval; /*!< The reseed interval. */
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000170
Rose Zadik2f8163d2018-01-25 21:55:14 +0000171 mbedtls_aes_context aes_ctx; /*!< The AES context. */
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000172
173 /*
174 * Callbacks (Entropy)
175 */
176 int (*f_entropy)(void *, unsigned char *, size_t);
Rose Zadik2f8163d2018-01-25 21:55:14 +0000177 /*!< The entropy callback function. */
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000178
Rose Zadik2f8163d2018-01-25 21:55:14 +0000179 void *p_entropy; /*!< The context for the entropy function. */
Manuel Pégourié-Gonnard0a4fb092015-05-07 12:50:31 +0100180
181#if defined(MBEDTLS_THREADING_C)
182 mbedtls_threading_mutex_t mutex;
183#endif
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000184}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185mbedtls_ctr_drbg_context;
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000186
187/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000188 * \brief This function initializes the CTR_DRBG context,
189 * and prepares it for mbedtls_ctr_drbg_seed()
190 * or mbedtls_ctr_drbg_free().
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200191 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000192 * \param ctx The CTR_DRBG context to initialize.
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200193 */
194void mbedtls_ctr_drbg_init( mbedtls_ctr_drbg_context *ctx );
195
196/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000197 * \brief This function seeds and sets up the CTR_DRBG
198 * entropy source for future reseeds.
Paul Bakker9af723c2014-05-01 13:03:14 +0200199 *
Gilles Peskine223deea2019-09-24 14:48:53 +0200200 * A typical choice for the \p f_entropy and \p p_entropy parameters is
201 * to use the entropy module:
202 * - \p f_entropy is mbedtls_entropy_func();
203 * - \p p_entropy is an instance of ::mbedtls_entropy_context initialized
204 * with mbedtls_entropy_init() (which registers the platform's default
205 * entropy sources).
206 *
207 * Personalization data can be provided in addition to the more generic
208 * entropy source, to make this instantiation as unique as possible.
209 *
210 * \note The _seed_material_ value passed to the derivation
211 * function in the CTR_DRBG Instantiate Process
212 * described in NIST SP 800-90A §10.2.1.3.2
213 * is the concatenation of the string obtained from
214 * calling \p f_entropy and the \p custom string.
215 * The origin of the nonce depends on the value of
216 * the entropy length relative to the security strength.
217 * See the documentation of
218 * mbedtls_ctr_drbg_set_entropy_len() for information
219 * about the entropy length.
220 * - If the entropy length is at least 1.5 times the
221 * security strength then the nonce is taken from the
222 * string obtained with \p f_entropy.
223 * - If the entropy length is less than the security
224 * strength, then the nonce is taken from \p custom.
225 * In this case, for compliance with SP 800-90A,
226 * you must pass a unique value of \p custom at
227 * each invocation. See SP 800-90A §8.6.7 for more
228 * details.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000229 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000230 * \param ctx The CTR_DRBG context to seed.
231 * \param f_entropy The entropy callback, taking as arguments the
232 * \p p_entropy context, the buffer to fill, and the
Gilles Peskine08875d42019-09-24 14:40:40 +0200233 * length of the buffer.
Rose Zadik2f8163d2018-01-25 21:55:14 +0000234 * \param p_entropy The entropy context.
235 * \param custom Personalization data, that is device-specific
Gilles Peskine08875d42019-09-24 14:40:40 +0200236 * identifiers. This can be NULL, in which case the
237 * personalization data is empty regardless of the value
238 * of \p len.
Rose Zadik2f8163d2018-01-25 21:55:14 +0000239 * \param len The length of the personalization data.
Gilles Peskine944bc582019-09-24 14:48:30 +0200240 * This must be at most
241 * #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT
242 * - #MBEDTLS_CTR_DRBG_ENTROPY_LEN.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000243 *
Rose Zadikc9474eb2018-03-27 10:58:22 +0100244 * \return \c 0 on success.
245 * \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED on failure.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000246 */
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200247int mbedtls_ctr_drbg_seed( mbedtls_ctr_drbg_context *ctx,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000248 int (*f_entropy)(void *, unsigned char *, size_t),
249 void *p_entropy,
Paul Bakker1bc9efc2011-12-03 11:29:32 +0000250 const unsigned char *custom,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000251 size_t len );
252
253/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000254 * \brief This function clears CTR_CRBG context data.
Paul Bakkerfff03662014-06-18 16:21:25 +0200255 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000256 * \param ctx The CTR_DRBG context to clear.
Paul Bakkerfff03662014-06-18 16:21:25 +0200257 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258void mbedtls_ctr_drbg_free( mbedtls_ctr_drbg_context *ctx );
Paul Bakkerfff03662014-06-18 16:21:25 +0200259
260/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000261 * \brief This function turns prediction resistance on or off.
262 * The default value is off.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000263 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000264 * \note If enabled, entropy is gathered at the beginning of
265 * every call to mbedtls_ctr_drbg_random_with_add().
266 * Only use this if your entropy source has sufficient
267 * throughput.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000268 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000269 * \param ctx The CTR_DRBG context.
270 * \param resistance #MBEDTLS_CTR_DRBG_PR_ON or #MBEDTLS_CTR_DRBG_PR_OFF.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000271 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272void mbedtls_ctr_drbg_set_prediction_resistance( mbedtls_ctr_drbg_context *ctx,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000273 int resistance );
274
275/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000276 * \brief This function sets the amount of entropy grabbed on each
277 * seed or reseed. The default value is
278 * #MBEDTLS_CTR_DRBG_ENTROPY_LEN.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000279 *
Gilles Peskine223deea2019-09-24 14:48:53 +0200280 * \note For compliance with NIST SP 800-90A, the entropy length
281 * must be at least 1.5 times security strength, since
282 * the entropy source is used both as the entropy input
283 * and to provide the initial nonce:
284 * - 24 bytes if using AES-128;
285 * - 48 bytes if using AES-256.
286 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000287 * \param ctx The CTR_DRBG context.
Gilles Peskine08875d42019-09-24 14:40:40 +0200288 * \param len The amount of entropy to grab, in bytes.
Gilles Peskine944bc582019-09-24 14:48:30 +0200289 * This must be at most #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000290 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291void mbedtls_ctr_drbg_set_entropy_len( mbedtls_ctr_drbg_context *ctx,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000292 size_t len );
293
294/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000295 * \brief This function sets the reseed interval.
296 * The default value is #MBEDTLS_CTR_DRBG_RESEED_INTERVAL.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000297 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000298 * \param ctx The CTR_DRBG context.
299 * \param interval The reseed interval.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000300 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301void mbedtls_ctr_drbg_set_reseed_interval( mbedtls_ctr_drbg_context *ctx,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000302 int interval );
303
304/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000305 * \brief This function reseeds the CTR_DRBG context, that is
306 * extracts data from the entropy source.
Paul Bakker9af723c2014-05-01 13:03:14 +0200307 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000308 * \param ctx The CTR_DRBG context.
309 * \param additional Additional data to add to the state. Can be NULL.
310 * \param len The length of the additional data.
Gilles Peskine944bc582019-09-24 14:48:30 +0200311 * This must be less than
312 * #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT - \c entropy_len
313 * where \c entropy_len is the entropy length
314 * configured for the context.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000315 *
Rose Zadikc9474eb2018-03-27 10:58:22 +0100316 * \return \c 0 on success.
317 * \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED on failure.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000318 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319int mbedtls_ctr_drbg_reseed( mbedtls_ctr_drbg_context *ctx,
Paul Bakker1bc9efc2011-12-03 11:29:32 +0000320 const unsigned char *additional, size_t len );
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000321
322/**
Rose Zadikc9474eb2018-03-27 10:58:22 +0100323 * \brief This function updates the state of the CTR_DRBG context.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000324 *
Rose Zadikc9474eb2018-03-27 10:58:22 +0100325 * \param ctx The CTR_DRBG context.
Gilles Peskine08875d42019-09-24 14:40:40 +0200326 * \param additional The data to update the state with. This must not be
327 * null unless \p add_len is 0.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500328 * \param add_len Length of \p additional in bytes. This must be at
329 * most #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT.
Rose Zadikc9474eb2018-03-27 10:58:22 +0100330 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500331 * \return \c 0 on success.
332 * \return #MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG if
333 * \p add_len is more than
334 * #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT.
335 * \return An error from the underlying AES cipher on failure.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000336 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500337int mbedtls_ctr_drbg_update_ret( mbedtls_ctr_drbg_context *ctx,
338 const unsigned char *additional,
339 size_t add_len );
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000340
341/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000342 * \brief This function updates a CTR_DRBG instance with additional
343 * data and uses it to generate random data.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000344 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000345 * \note The function automatically reseeds if the reseed counter is exceeded.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000346 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000347 * \param p_rng The CTR_DRBG context. This must be a pointer to a
348 * #mbedtls_ctr_drbg_context structure.
349 * \param output The buffer to fill.
350 * \param output_len The length of the buffer.
Gilles Peskine08875d42019-09-24 14:40:40 +0200351 * \param additional Additional data to update. Can be NULL, in which
352 * case the additional data is empty regardless of
353 * the value of \p add_len.
354 * \param add_len The length of the additional data
355 * if \p additional is non-null.
Gilles Peskine944bc582019-09-24 14:48:30 +0200356 * This must be less than #MBEDTLS_CTR_DRBG_MAX_INPUT
357 * and less than
358 * #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT - \c entropy_len
359 * where \c entropy_len is the entropy length
360 * configured for the context.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000361 *
Rose Zadikc9474eb2018-03-27 10:58:22 +0100362 * \return \c 0 on success.
363 * \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or
Rose Zadik2f8163d2018-01-25 21:55:14 +0000364 * #MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG on failure.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000365 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366int mbedtls_ctr_drbg_random_with_add( void *p_rng,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000367 unsigned char *output, size_t output_len,
Paul Bakker1bc9efc2011-12-03 11:29:32 +0000368 const unsigned char *additional, size_t add_len );
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000369
370/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000371 * \brief This function uses CTR_DRBG to generate random data.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000372 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000373 * \note The function automatically reseeds if the reseed counter is exceeded.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000374 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000375 * \param p_rng The CTR_DRBG context. This must be a pointer to a
376 * #mbedtls_ctr_drbg_context structure.
377 * \param output The buffer to fill.
Gilles Peskine944bc582019-09-24 14:48:30 +0200378 * \param output_len The length of the buffer in bytes.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000379 *
Rose Zadikc9474eb2018-03-27 10:58:22 +0100380 * \return \c 0 on success.
381 * \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or
Rose Zadik2f8163d2018-01-25 21:55:14 +0000382 * #MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG on failure.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000383 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384int mbedtls_ctr_drbg_random( void *p_rng,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000385 unsigned char *output, size_t output_len );
386
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500387
388#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
389#if defined(MBEDTLS_DEPRECATED_WARNING)
390#define MBEDTLS_DEPRECATED __attribute__((deprecated))
391#else
392#define MBEDTLS_DEPRECATED
393#endif
394/**
395 * \brief This function updates the state of the CTR_DRBG context.
396 *
397 * \deprecated Superseded by mbedtls_ctr_drbg_update_ret()
398 * in 2.16.0.
399 *
400 * \note If \p add_len is greater than
401 * #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT, only the first
402 * #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT Bytes are used.
403 * The remaining Bytes are silently discarded.
404 *
405 * \param ctx The CTR_DRBG context.
406 * \param additional The data to update the state with.
407 * \param add_len Length of \p additional data.
408 */
409MBEDTLS_DEPRECATED void mbedtls_ctr_drbg_update(
410 mbedtls_ctr_drbg_context *ctx,
411 const unsigned char *additional,
412 size_t add_len );
413#undef MBEDTLS_DEPRECATED
414#endif /* !MBEDTLS_DEPRECATED_REMOVED */
415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416#if defined(MBEDTLS_FS_IO)
Paul Bakkerfc754a92011-12-05 13:23:51 +0000417/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000418 * \brief This function writes a seed file.
Paul Bakkerfc754a92011-12-05 13:23:51 +0000419 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000420 * \param ctx The CTR_DRBG context.
421 * \param path The name of the file.
Paul Bakkerfc754a92011-12-05 13:23:51 +0000422 *
Rose Zadikc9474eb2018-03-27 10:58:22 +0100423 * \return \c 0 on success.
Rose Zadikf25eb6e2018-04-16 14:51:52 +0100424 * \return #MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR on file error.
425 * \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED on
Rose Zadik2f8163d2018-01-25 21:55:14 +0000426 * failure.
Paul Bakkerfc754a92011-12-05 13:23:51 +0000427 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428int mbedtls_ctr_drbg_write_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
Paul Bakkerfc754a92011-12-05 13:23:51 +0000429
430/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000431 * \brief This function reads and updates a seed file. The seed
432 * is added to this instance.
Paul Bakkerfc754a92011-12-05 13:23:51 +0000433 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000434 * \param ctx The CTR_DRBG context.
435 * \param path The name of the file.
Paul Bakkerfc754a92011-12-05 13:23:51 +0000436 *
Rose Zadikc9474eb2018-03-27 10:58:22 +0100437 * \return \c 0 on success.
438 * \return #MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR on file error.
439 * \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or
Rose Zadik2f8163d2018-01-25 21:55:14 +0000440 * #MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG on failure.
Paul Bakkerfc754a92011-12-05 13:23:51 +0000441 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442int mbedtls_ctr_drbg_update_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
443#endif /* MBEDTLS_FS_IO */
Paul Bakkerfc754a92011-12-05 13:23:51 +0000444
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500445#if defined(MBEDTLS_SELF_TEST)
446
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000447/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000448 * \brief The CTR_DRBG checkup routine.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000449 *
Rose Zadikc9474eb2018-03-27 10:58:22 +0100450 * \return \c 0 on success.
451 * \return \c 1 on failure.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000452 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453int mbedtls_ctr_drbg_self_test( int verbose );
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000454
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500455#endif /* MBEDTLS_SELF_TEST */
456
Paul Bakker534f82c2013-06-25 16:47:55 +0200457/* Internal functions (do not call directly) */
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200458int mbedtls_ctr_drbg_seed_entropy_len( mbedtls_ctr_drbg_context *,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200459 int (*)(void *, unsigned char *, size_t), void *,
460 const unsigned char *, size_t, size_t );
Paul Bakker534f82c2013-06-25 16:47:55 +0200461
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000462#ifdef __cplusplus
463}
464#endif
465
466#endif /* ctr_drbg.h */