blob: 8d8882ac4e68e5b44d9d4bf778844aa947182e39 [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`).
Darryl Greena40a1012018-01-05 15:33:17 +000027 */
28/*
Gilles Peskine08875d42019-09-24 14:40:40 +020029 * Copyright (C) 2006-2019, Arm Limited (or its affiliates), All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020030 * SPDX-License-Identifier: Apache-2.0
31 *
32 * Licensed under the Apache License, Version 2.0 (the "License"); you may
33 * not use this file except in compliance with the License.
34 * You may obtain a copy of the License at
35 *
36 * http://www.apache.org/licenses/LICENSE-2.0
37 *
38 * Unless required by applicable law or agreed to in writing, software
39 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
40 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
41 * See the License for the specific language governing permissions and
42 * limitations under the License.
Paul Bakker0e04d0e2011-11-27 14:46:59 +000043 *
Rose Zadik2f8163d2018-01-25 21:55:14 +000044 * This file is part of Mbed TLS (https://tls.mbed.org)
Paul Bakker0e04d0e2011-11-27 14:46:59 +000045 */
Rose Zadik2f8163d2018-01-25 21:55:14 +000046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#ifndef MBEDTLS_CTR_DRBG_H
48#define MBEDTLS_CTR_DRBG_H
Paul Bakker0e04d0e2011-11-27 14:46:59 +000049
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050050#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010051#include "mbedtls/config.h"
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050052#else
53#include MBEDTLS_CONFIG_FILE
54#endif
55
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010056#include "mbedtls/aes.h"
Paul Bakker0e04d0e2011-11-27 14:46:59 +000057
Manuel Pégourié-Gonnard0a4fb092015-05-07 12:50:31 +010058#if defined(MBEDTLS_THREADING_C)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010059#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard0a4fb092015-05-07 12:50:31 +010060#endif
61
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#define MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED -0x0034 /**< The entropy source failed. */
Rose Zadik2f8163d2018-01-25 21:55:14 +000063#define MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG -0x0036 /**< The requested random buffer length is too big. */
64#define MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG -0x0038 /**< The input (entropy + additional data) is too large. */
65#define MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR -0x003A /**< Read or write error in file. */
Paul Bakker0e04d0e2011-11-27 14:46:59 +000066
Rose Zadik2f8163d2018-01-25 21:55:14 +000067#define MBEDTLS_CTR_DRBG_BLOCKSIZE 16 /**< The block size used by the cipher. */
Nir Sonnenscheince266e42018-08-29 10:11:46 +030068
Nir Sonnenschein43e4ff02018-09-03 14:15:46 +030069#if defined(MBEDTLS_CTR_DRBG_USE_128_BIT_KEY)
Gilles Peskine08875d42019-09-24 14:40:40 +020070#define MBEDTLS_CTR_DRBG_KEYSIZE 16
71/**< The key size in bytes used by the cipher.
72 *
73 * Compile-time choice: 16 bytes (128 bits)
74 * because #MBEDTLS_CTR_DRBG_USE_128_BIT_KEY is set.
75 */
Nir Sonnenscheineb73f7a2018-07-30 17:46:49 +030076#else
Gilles Peskine08875d42019-09-24 14:40:40 +020077#define MBEDTLS_CTR_DRBG_KEYSIZE 32
78/**< The key size in bytes used by the cipher.
79 *
80 * Compile-time choice: 32 bytes (256 bits)
81 * because `MBEDTLS_CTR_DRBG_USE_128_BIT_KEY` is not set.
82 */
Nir Sonnenscheina4588d42018-07-30 16:59:36 +030083#endif
Nir Sonnenschein43e4ff02018-09-03 14:15:46 +030084
Rose Zadik2f8163d2018-01-25 21:55:14 +000085#define MBEDTLS_CTR_DRBG_KEYBITS ( MBEDTLS_CTR_DRBG_KEYSIZE * 8 ) /**< The key size for the DRBG operation, in bits. */
86#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 +020087
Paul Bakker088c5c52014-04-25 11:11:10 +020088/**
89 * \name SECTION: Module settings
90 *
91 * The configuration options you can set for this module are in this section.
Rose Zadik2f8163d2018-01-25 21:55:14 +000092 * Either change them in config.h or define them using the compiler command
93 * line.
Paul Bakker088c5c52014-04-25 11:11:10 +020094 * \{
95 */
96
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097#if !defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN)
98#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256)
Gilles Peskine08875d42019-09-24 14:40:40 +020099/** The amount of entropy used per seed by default.
100 *
101 * This is 48 bytes because the entropy module uses SHA-512
102 * (`MBEDTLS_ENTROPY_FORCE_SHA256` is not set).
103 *
104 * \note See mbedtls_ctr_drbg_set_entropy_len() regarding what values are
105 * acceptable.
106 */
Rose Zadik2f8163d2018-01-25 21:55:14 +0000107#define MBEDTLS_CTR_DRBG_ENTROPY_LEN 48
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200108#else
Gilles Peskine08875d42019-09-24 14:40:40 +0200109/** The amount of entropy used per seed by default.
110 *
111 * This is 32 bytes because the entropy module uses SHA-256
112 * (the SHA-512 module is disabled or `MBEDTLS_ENTROPY_FORCE_SHA256` is set).
113 *
114 * \note See mbedtls_ctr_drbg_set_entropy_len() regarding what values are
115 * acceptable.
Rose Zadik2f8163d2018-01-25 21:55:14 +0000116 */
Gilles Peskine08875d42019-09-24 14:40:40 +0200117#define MBEDTLS_CTR_DRBG_ENTROPY_LEN 32
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200118#endif
Paul Bakker088c5c52014-04-25 11:11:10 +0200119#endif
120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121#if !defined(MBEDTLS_CTR_DRBG_RESEED_INTERVAL)
Rose Zadik2f8163d2018-01-25 21:55:14 +0000122#define MBEDTLS_CTR_DRBG_RESEED_INTERVAL 10000
123/**< The interval before reseed is performed by default. */
Paul Bakker088c5c52014-04-25 11:11:10 +0200124#endif
125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126#if !defined(MBEDTLS_CTR_DRBG_MAX_INPUT)
Rose Zadik2f8163d2018-01-25 21:55:14 +0000127#define MBEDTLS_CTR_DRBG_MAX_INPUT 256
128/**< The maximum number of additional input Bytes. */
Paul Bakker088c5c52014-04-25 11:11:10 +0200129#endif
130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131#if !defined(MBEDTLS_CTR_DRBG_MAX_REQUEST)
Rose Zadik2f8163d2018-01-25 21:55:14 +0000132#define MBEDTLS_CTR_DRBG_MAX_REQUEST 1024
133/**< The maximum number of requested Bytes per call. */
Paul Bakker088c5c52014-04-25 11:11:10 +0200134#endif
135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136#if !defined(MBEDTLS_CTR_DRBG_MAX_SEED_INPUT)
Rose Zadik2f8163d2018-01-25 21:55:14 +0000137#define MBEDTLS_CTR_DRBG_MAX_SEED_INPUT 384
Gilles Peskine08875d42019-09-24 14:40:40 +0200138/**< The maximum size of seed or reseed buffer in bytes. */
Paul Bakker088c5c52014-04-25 11:11:10 +0200139#endif
140
141/* \} name SECTION: Module settings */
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000142
Rose Zadik2f8163d2018-01-25 21:55:14 +0000143#define MBEDTLS_CTR_DRBG_PR_OFF 0
144/**< Prediction resistance is disabled. */
145#define MBEDTLS_CTR_DRBG_PR_ON 1
146/**< Prediction resistance is enabled. */
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000147
148#ifdef __cplusplus
149extern "C" {
150#endif
151
152/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000153 * \brief The CTR_DRBG context structure.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000154 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200155typedef struct mbedtls_ctr_drbg_context
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000156{
Rose Zadik2f8163d2018-01-25 21:55:14 +0000157 unsigned char counter[16]; /*!< The counter (V). */
158 int reseed_counter; /*!< The reseed counter. */
159 int prediction_resistance; /*!< This determines whether prediction
160 resistance is enabled, that is
161 whether to systematically reseed before
162 each random generation. */
163 size_t entropy_len; /*!< The amount of entropy grabbed on each
164 seed or reseed operation. */
165 int reseed_interval; /*!< The reseed interval. */
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000166
Rose Zadik2f8163d2018-01-25 21:55:14 +0000167 mbedtls_aes_context aes_ctx; /*!< The AES context. */
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000168
169 /*
170 * Callbacks (Entropy)
171 */
172 int (*f_entropy)(void *, unsigned char *, size_t);
Rose Zadik2f8163d2018-01-25 21:55:14 +0000173 /*!< The entropy callback function. */
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000174
Rose Zadik2f8163d2018-01-25 21:55:14 +0000175 void *p_entropy; /*!< The context for the entropy function. */
Manuel Pégourié-Gonnard0a4fb092015-05-07 12:50:31 +0100176
177#if defined(MBEDTLS_THREADING_C)
178 mbedtls_threading_mutex_t mutex;
179#endif
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000180}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181mbedtls_ctr_drbg_context;
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000182
183/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000184 * \brief This function initializes the CTR_DRBG context,
185 * and prepares it for mbedtls_ctr_drbg_seed()
186 * or mbedtls_ctr_drbg_free().
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200187 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000188 * \param ctx The CTR_DRBG context to initialize.
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200189 */
190void mbedtls_ctr_drbg_init( mbedtls_ctr_drbg_context *ctx );
191
192/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000193 * \brief This function seeds and sets up the CTR_DRBG
194 * entropy source for future reseeds.
Paul Bakker9af723c2014-05-01 13:03:14 +0200195 *
Gilles Peskine223deea2019-09-24 14:48:53 +0200196 * A typical choice for the \p f_entropy and \p p_entropy parameters is
197 * to use the entropy module:
198 * - \p f_entropy is mbedtls_entropy_func();
199 * - \p p_entropy is an instance of ::mbedtls_entropy_context initialized
200 * with mbedtls_entropy_init() (which registers the platform's default
201 * entropy sources).
202 *
203 * Personalization data can be provided in addition to the more generic
204 * entropy source, to make this instantiation as unique as possible.
205 *
206 * \note The _seed_material_ value passed to the derivation
207 * function in the CTR_DRBG Instantiate Process
208 * described in NIST SP 800-90A §10.2.1.3.2
209 * is the concatenation of the string obtained from
210 * calling \p f_entropy and the \p custom string.
211 * The origin of the nonce depends on the value of
212 * the entropy length relative to the security strength.
213 * See the documentation of
214 * mbedtls_ctr_drbg_set_entropy_len() for information
215 * about the entropy length.
216 * - If the entropy length is at least 1.5 times the
217 * security strength then the nonce is taken from the
218 * string obtained with \p f_entropy.
219 * - If the entropy length is less than the security
220 * strength, then the nonce is taken from \p custom.
221 * In this case, for compliance with SP 800-90A,
222 * you must pass a unique value of \p custom at
223 * each invocation. See SP 800-90A §8.6.7 for more
224 * details.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000225 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000226 * \param ctx The CTR_DRBG context to seed.
227 * \param f_entropy The entropy callback, taking as arguments the
228 * \p p_entropy context, the buffer to fill, and the
Gilles Peskine08875d42019-09-24 14:40:40 +0200229 * length of the buffer.
Rose Zadik2f8163d2018-01-25 21:55:14 +0000230 * \param p_entropy The entropy context.
231 * \param custom Personalization data, that is device-specific
Gilles Peskine08875d42019-09-24 14:40:40 +0200232 * identifiers. This can be NULL, in which case the
233 * personalization data is empty regardless of the value
234 * of \p len.
Rose Zadik2f8163d2018-01-25 21:55:14 +0000235 * \param len The length of the personalization data.
Gilles Peskine944bc582019-09-24 14:48:30 +0200236 * This must be at most
237 * #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT
238 * - #MBEDTLS_CTR_DRBG_ENTROPY_LEN.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000239 *
Rose Zadikc9474eb2018-03-27 10:58:22 +0100240 * \return \c 0 on success.
241 * \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED on failure.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000242 */
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200243int mbedtls_ctr_drbg_seed( mbedtls_ctr_drbg_context *ctx,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000244 int (*f_entropy)(void *, unsigned char *, size_t),
245 void *p_entropy,
Paul Bakker1bc9efc2011-12-03 11:29:32 +0000246 const unsigned char *custom,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000247 size_t len );
248
249/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000250 * \brief This function clears CTR_CRBG context data.
Paul Bakkerfff03662014-06-18 16:21:25 +0200251 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000252 * \param ctx The CTR_DRBG context to clear.
Paul Bakkerfff03662014-06-18 16:21:25 +0200253 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254void mbedtls_ctr_drbg_free( mbedtls_ctr_drbg_context *ctx );
Paul Bakkerfff03662014-06-18 16:21:25 +0200255
256/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000257 * \brief This function turns prediction resistance on or off.
258 * The default value is off.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000259 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000260 * \note If enabled, entropy is gathered at the beginning of
261 * every call to mbedtls_ctr_drbg_random_with_add().
262 * Only use this if your entropy source has sufficient
263 * throughput.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000264 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000265 * \param ctx The CTR_DRBG context.
266 * \param resistance #MBEDTLS_CTR_DRBG_PR_ON or #MBEDTLS_CTR_DRBG_PR_OFF.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000267 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268void mbedtls_ctr_drbg_set_prediction_resistance( mbedtls_ctr_drbg_context *ctx,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000269 int resistance );
270
271/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000272 * \brief This function sets the amount of entropy grabbed on each
273 * seed or reseed. The default value is
274 * #MBEDTLS_CTR_DRBG_ENTROPY_LEN.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000275 *
Gilles Peskine223deea2019-09-24 14:48:53 +0200276 * \note For compliance with NIST SP 800-90A, the entropy length
Gilles Peskine6fdf0b32019-09-25 20:22:40 +0200277 * (\p len bytes = \p len * 8 bits)
278 * must be at least the security strength.
279 * Furthermore, if the entropy input is used to provide
280 * the nonce, the entropy length must be 1.5 times
281 * the security strength.
282 * Per NIST SP 800-57A table 2, the achievable security
283 * strength is 128 bits if using AES-128 and
284 * 256 bits if using AES-256.
285 * Therefore, to provide full security,
286 * the entropy input must be at least:
287 * - 24 bytes if using AES-128 and the \p custom
288 * argument to mbedtls_ctr_drbg_seed() may repeat
289 * (for example because it is empty, or more generally
290 * constant);
291 * - 48 bytes if using AES-256 and the \p custom
292 * argument to mbedtls_ctr_drbg_seed() may repeat
293 * (for example because it is empty, or more generally
294 * constant);
295 * - 16 bytes if using AES-128 and the \p custom
296 * argument to mbedtls_ctr_drbg_seed() includes
297 * a nonce;
298 * - 32 bytes if using AES-256 and the \p custom
299 * argument to mbedtls_ctr_drbg_seed() includes
300 * a nonce.
Gilles Peskine223deea2019-09-24 14:48:53 +0200301 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000302 * \param ctx The CTR_DRBG context.
Gilles Peskine08875d42019-09-24 14:40:40 +0200303 * \param len The amount of entropy to grab, in bytes.
Gilles Peskine944bc582019-09-24 14:48:30 +0200304 * This must be at most #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000305 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306void mbedtls_ctr_drbg_set_entropy_len( mbedtls_ctr_drbg_context *ctx,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000307 size_t len );
308
309/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000310 * \brief This function sets the reseed interval.
311 * The default value is #MBEDTLS_CTR_DRBG_RESEED_INTERVAL.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000312 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000313 * \param ctx The CTR_DRBG context.
314 * \param interval The reseed interval.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000315 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316void mbedtls_ctr_drbg_set_reseed_interval( mbedtls_ctr_drbg_context *ctx,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000317 int interval );
318
319/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000320 * \brief This function reseeds the CTR_DRBG context, that is
321 * extracts data from the entropy source.
Paul Bakker9af723c2014-05-01 13:03:14 +0200322 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000323 * \param ctx The CTR_DRBG context.
324 * \param additional Additional data to add to the state. Can be NULL.
325 * \param len The length of the additional data.
Gilles Peskine944bc582019-09-24 14:48:30 +0200326 * This must be less than
327 * #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT - \c entropy_len
328 * where \c entropy_len is the entropy length
329 * configured for the context.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000330 *
Rose Zadikc9474eb2018-03-27 10:58:22 +0100331 * \return \c 0 on success.
332 * \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED on failure.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000333 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334int mbedtls_ctr_drbg_reseed( mbedtls_ctr_drbg_context *ctx,
Paul Bakker1bc9efc2011-12-03 11:29:32 +0000335 const unsigned char *additional, size_t len );
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000336
337/**
Rose Zadikc9474eb2018-03-27 10:58:22 +0100338 * \brief This function updates the state of the CTR_DRBG context.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000339 *
Rose Zadikc9474eb2018-03-27 10:58:22 +0100340 * \param ctx The CTR_DRBG context.
Gilles Peskine08875d42019-09-24 14:40:40 +0200341 * \param additional The data to update the state with. This must not be
342 * null unless \p add_len is 0.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500343 * \param add_len Length of \p additional in bytes. This must be at
344 * most #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT.
Rose Zadikc9474eb2018-03-27 10:58:22 +0100345 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500346 * \return \c 0 on success.
347 * \return #MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG if
348 * \p add_len is more than
349 * #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT.
350 * \return An error from the underlying AES cipher on failure.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000351 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500352int mbedtls_ctr_drbg_update_ret( mbedtls_ctr_drbg_context *ctx,
353 const unsigned char *additional,
354 size_t add_len );
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000355
356/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000357 * \brief This function updates a CTR_DRBG instance with additional
358 * data and uses it to generate random data.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000359 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000360 * \note The function automatically reseeds if the reseed counter is exceeded.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000361 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000362 * \param p_rng The CTR_DRBG context. This must be a pointer to a
363 * #mbedtls_ctr_drbg_context structure.
364 * \param output The buffer to fill.
365 * \param output_len The length of the buffer.
Gilles Peskine08875d42019-09-24 14:40:40 +0200366 * \param additional Additional data to update. Can be NULL, in which
367 * case the additional data is empty regardless of
368 * the value of \p add_len.
369 * \param add_len The length of the additional data
370 * if \p additional is non-null.
Gilles Peskine944bc582019-09-24 14:48:30 +0200371 * This must be less than #MBEDTLS_CTR_DRBG_MAX_INPUT
372 * and less than
373 * #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT - \c entropy_len
374 * where \c entropy_len is the entropy length
375 * configured for the context.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000376 *
Rose Zadikc9474eb2018-03-27 10:58:22 +0100377 * \return \c 0 on success.
378 * \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or
Rose Zadik2f8163d2018-01-25 21:55:14 +0000379 * #MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG on failure.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000380 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381int mbedtls_ctr_drbg_random_with_add( void *p_rng,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000382 unsigned char *output, size_t output_len,
Paul Bakker1bc9efc2011-12-03 11:29:32 +0000383 const unsigned char *additional, size_t add_len );
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000384
385/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000386 * \brief This function uses CTR_DRBG to generate random data.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000387 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000388 * \note The function automatically reseeds if the reseed counter is exceeded.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000389 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000390 * \param p_rng The CTR_DRBG context. This must be a pointer to a
391 * #mbedtls_ctr_drbg_context structure.
392 * \param output The buffer to fill.
Gilles Peskine944bc582019-09-24 14:48:30 +0200393 * \param output_len The length of the buffer in bytes.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000394 *
Rose Zadikc9474eb2018-03-27 10:58:22 +0100395 * \return \c 0 on success.
396 * \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or
Rose Zadik2f8163d2018-01-25 21:55:14 +0000397 * #MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG on failure.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000398 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399int mbedtls_ctr_drbg_random( void *p_rng,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000400 unsigned char *output, size_t output_len );
401
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500402
403#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
404#if defined(MBEDTLS_DEPRECATED_WARNING)
405#define MBEDTLS_DEPRECATED __attribute__((deprecated))
406#else
407#define MBEDTLS_DEPRECATED
408#endif
409/**
410 * \brief This function updates the state of the CTR_DRBG context.
411 *
412 * \deprecated Superseded by mbedtls_ctr_drbg_update_ret()
413 * in 2.16.0.
414 *
415 * \note If \p add_len is greater than
416 * #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT, only the first
417 * #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT Bytes are used.
418 * The remaining Bytes are silently discarded.
419 *
420 * \param ctx The CTR_DRBG context.
421 * \param additional The data to update the state with.
422 * \param add_len Length of \p additional data.
423 */
424MBEDTLS_DEPRECATED void mbedtls_ctr_drbg_update(
425 mbedtls_ctr_drbg_context *ctx,
426 const unsigned char *additional,
427 size_t add_len );
428#undef MBEDTLS_DEPRECATED
429#endif /* !MBEDTLS_DEPRECATED_REMOVED */
430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431#if defined(MBEDTLS_FS_IO)
Paul Bakkerfc754a92011-12-05 13:23:51 +0000432/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000433 * \brief This function writes a seed file.
Paul Bakkerfc754a92011-12-05 13:23:51 +0000434 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000435 * \param ctx The CTR_DRBG context.
436 * \param path The name of the file.
Paul Bakkerfc754a92011-12-05 13:23:51 +0000437 *
Rose Zadikc9474eb2018-03-27 10:58:22 +0100438 * \return \c 0 on success.
Rose Zadikf25eb6e2018-04-16 14:51:52 +0100439 * \return #MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR on file error.
440 * \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED on
Rose Zadik2f8163d2018-01-25 21:55:14 +0000441 * failure.
Paul Bakkerfc754a92011-12-05 13:23:51 +0000442 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443int mbedtls_ctr_drbg_write_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
Paul Bakkerfc754a92011-12-05 13:23:51 +0000444
445/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000446 * \brief This function reads and updates a seed file. The seed
447 * is added to this instance.
Paul Bakkerfc754a92011-12-05 13:23:51 +0000448 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000449 * \param ctx The CTR_DRBG context.
450 * \param path The name of the file.
Paul Bakkerfc754a92011-12-05 13:23:51 +0000451 *
Rose Zadikc9474eb2018-03-27 10:58:22 +0100452 * \return \c 0 on success.
453 * \return #MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR on file error.
454 * \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or
Rose Zadik2f8163d2018-01-25 21:55:14 +0000455 * #MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG on failure.
Paul Bakkerfc754a92011-12-05 13:23:51 +0000456 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457int mbedtls_ctr_drbg_update_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
458#endif /* MBEDTLS_FS_IO */
Paul Bakkerfc754a92011-12-05 13:23:51 +0000459
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500460#if defined(MBEDTLS_SELF_TEST)
461
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000462/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000463 * \brief The CTR_DRBG checkup routine.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000464 *
Rose Zadikc9474eb2018-03-27 10:58:22 +0100465 * \return \c 0 on success.
466 * \return \c 1 on failure.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000467 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468int mbedtls_ctr_drbg_self_test( int verbose );
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000469
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500470#endif /* MBEDTLS_SELF_TEST */
471
Paul Bakker534f82c2013-06-25 16:47:55 +0200472/* Internal functions (do not call directly) */
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200473int mbedtls_ctr_drbg_seed_entropy_len( mbedtls_ctr_drbg_context *,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200474 int (*)(void *, unsigned char *, size_t), void *,
475 const unsigned char *, size_t, size_t );
Paul Bakker534f82c2013-06-25 16:47:55 +0200476
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000477#ifdef __cplusplus
478}
479#endif
480
481#endif /* ctr_drbg.h */