blob: db614746a843b1712dbc89aa43ea9f3f9d535543 [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
Nir Sonnenscheina4588d42018-07-30 16:59:36 +030012 * as the underlying block cipher.
Nir Sonnenscheineb73f7a2018-07-30 17:46:49 +030013 *
Nir Sonnenscheinb7ebbca2018-08-29 10:20:12 +030014 * \warning Using 128-bit keys for CTR_DRBG limits the security of generated
15 * keys and operations that use random values generated to 128-bit security.
Darryl Greena40a1012018-01-05 15:33:17 +000016 */
17/*
Gilles Peskine08875d42019-09-24 14:40:40 +020018 * Copyright (C) 2006-2019, Arm Limited (or its affiliates), All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020019 * SPDX-License-Identifier: Apache-2.0
20 *
21 * Licensed under the Apache License, Version 2.0 (the "License"); you may
22 * not use this file except in compliance with the License.
23 * You may obtain a copy of the License at
24 *
25 * http://www.apache.org/licenses/LICENSE-2.0
26 *
27 * Unless required by applicable law or agreed to in writing, software
28 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
29 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
30 * See the License for the specific language governing permissions and
31 * limitations under the License.
Paul Bakker0e04d0e2011-11-27 14:46:59 +000032 *
Rose Zadik2f8163d2018-01-25 21:55:14 +000033 * This file is part of Mbed TLS (https://tls.mbed.org)
Paul Bakker0e04d0e2011-11-27 14:46:59 +000034 */
Rose Zadik2f8163d2018-01-25 21:55:14 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#ifndef MBEDTLS_CTR_DRBG_H
37#define MBEDTLS_CTR_DRBG_H
Paul Bakker0e04d0e2011-11-27 14:46:59 +000038
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050039#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010040#include "mbedtls/config.h"
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050041#else
42#include MBEDTLS_CONFIG_FILE
43#endif
44
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010045#include "mbedtls/aes.h"
Paul Bakker0e04d0e2011-11-27 14:46:59 +000046
Manuel Pégourié-Gonnard0a4fb092015-05-07 12:50:31 +010047#if defined(MBEDTLS_THREADING_C)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010048#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard0a4fb092015-05-07 12:50:31 +010049#endif
50
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#define MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED -0x0034 /**< The entropy source failed. */
Rose Zadik2f8163d2018-01-25 21:55:14 +000052#define MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG -0x0036 /**< The requested random buffer length is too big. */
53#define MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG -0x0038 /**< The input (entropy + additional data) is too large. */
54#define MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR -0x003A /**< Read or write error in file. */
Paul Bakker0e04d0e2011-11-27 14:46:59 +000055
Rose Zadik2f8163d2018-01-25 21:55:14 +000056#define MBEDTLS_CTR_DRBG_BLOCKSIZE 16 /**< The block size used by the cipher. */
Nir Sonnenscheince266e42018-08-29 10:11:46 +030057
Nir Sonnenschein43e4ff02018-09-03 14:15:46 +030058#if defined(MBEDTLS_CTR_DRBG_USE_128_BIT_KEY)
Gilles Peskine08875d42019-09-24 14:40:40 +020059#define MBEDTLS_CTR_DRBG_KEYSIZE 16
60/**< The key size in bytes used by the cipher.
61 *
62 * Compile-time choice: 16 bytes (128 bits)
63 * because #MBEDTLS_CTR_DRBG_USE_128_BIT_KEY is set.
64 */
Nir Sonnenscheineb73f7a2018-07-30 17:46:49 +030065#else
Gilles Peskine08875d42019-09-24 14:40:40 +020066#define MBEDTLS_CTR_DRBG_KEYSIZE 32
67/**< The key size in bytes used by the cipher.
68 *
69 * Compile-time choice: 32 bytes (256 bits)
70 * because `MBEDTLS_CTR_DRBG_USE_128_BIT_KEY` is not set.
71 */
Nir Sonnenscheina4588d42018-07-30 16:59:36 +030072#endif
Nir Sonnenschein43e4ff02018-09-03 14:15:46 +030073
Rose Zadik2f8163d2018-01-25 21:55:14 +000074#define MBEDTLS_CTR_DRBG_KEYBITS ( MBEDTLS_CTR_DRBG_KEYSIZE * 8 ) /**< The key size for the DRBG operation, in bits. */
75#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 +020076
Paul Bakker088c5c52014-04-25 11:11:10 +020077/**
78 * \name SECTION: Module settings
79 *
80 * The configuration options you can set for this module are in this section.
Rose Zadik2f8163d2018-01-25 21:55:14 +000081 * Either change them in config.h or define them using the compiler command
82 * line.
Paul Bakker088c5c52014-04-25 11:11:10 +020083 * \{
84 */
85
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086#if !defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN)
87#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256)
Gilles Peskine08875d42019-09-24 14:40:40 +020088/** The amount of entropy used per seed by default.
89 *
90 * This is 48 bytes because the entropy module uses SHA-512
91 * (`MBEDTLS_ENTROPY_FORCE_SHA256` is not set).
92 *
93 * \note See mbedtls_ctr_drbg_set_entropy_len() regarding what values are
94 * acceptable.
95 */
Rose Zadik2f8163d2018-01-25 21:55:14 +000096#define MBEDTLS_CTR_DRBG_ENTROPY_LEN 48
Paul Bakkerfb08fd22013-08-27 15:06:26 +020097#else
Gilles Peskine08875d42019-09-24 14:40:40 +020098/** The amount of entropy used per seed by default.
99 *
100 * This is 32 bytes because the entropy module uses SHA-256
101 * (the SHA-512 module is disabled or `MBEDTLS_ENTROPY_FORCE_SHA256` is set).
102 *
103 * \note See mbedtls_ctr_drbg_set_entropy_len() regarding what values are
104 * acceptable.
Rose Zadik2f8163d2018-01-25 21:55:14 +0000105 */
Gilles Peskine08875d42019-09-24 14:40:40 +0200106#define MBEDTLS_CTR_DRBG_ENTROPY_LEN 32
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200107#endif
Paul Bakker088c5c52014-04-25 11:11:10 +0200108#endif
109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110#if !defined(MBEDTLS_CTR_DRBG_RESEED_INTERVAL)
Rose Zadik2f8163d2018-01-25 21:55:14 +0000111#define MBEDTLS_CTR_DRBG_RESEED_INTERVAL 10000
112/**< The interval before reseed is performed by default. */
Paul Bakker088c5c52014-04-25 11:11:10 +0200113#endif
114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115#if !defined(MBEDTLS_CTR_DRBG_MAX_INPUT)
Rose Zadik2f8163d2018-01-25 21:55:14 +0000116#define MBEDTLS_CTR_DRBG_MAX_INPUT 256
117/**< The maximum number of additional input Bytes. */
Paul Bakker088c5c52014-04-25 11:11:10 +0200118#endif
119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120#if !defined(MBEDTLS_CTR_DRBG_MAX_REQUEST)
Rose Zadik2f8163d2018-01-25 21:55:14 +0000121#define MBEDTLS_CTR_DRBG_MAX_REQUEST 1024
122/**< The maximum number of requested Bytes per call. */
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_MAX_SEED_INPUT)
Rose Zadik2f8163d2018-01-25 21:55:14 +0000126#define MBEDTLS_CTR_DRBG_MAX_SEED_INPUT 384
Gilles Peskine08875d42019-09-24 14:40:40 +0200127/**< The maximum size of seed or reseed buffer in bytes. */
Paul Bakker088c5c52014-04-25 11:11:10 +0200128#endif
129
130/* \} name SECTION: Module settings */
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000131
Rose Zadik2f8163d2018-01-25 21:55:14 +0000132#define MBEDTLS_CTR_DRBG_PR_OFF 0
133/**< Prediction resistance is disabled. */
134#define MBEDTLS_CTR_DRBG_PR_ON 1
135/**< Prediction resistance is enabled. */
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000136
137#ifdef __cplusplus
138extern "C" {
139#endif
140
141/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000142 * \brief The CTR_DRBG context structure.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000143 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200144typedef struct mbedtls_ctr_drbg_context
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000145{
Rose Zadik2f8163d2018-01-25 21:55:14 +0000146 unsigned char counter[16]; /*!< The counter (V). */
147 int reseed_counter; /*!< The reseed counter. */
148 int prediction_resistance; /*!< This determines whether prediction
149 resistance is enabled, that is
150 whether to systematically reseed before
151 each random generation. */
152 size_t entropy_len; /*!< The amount of entropy grabbed on each
153 seed or reseed operation. */
154 int reseed_interval; /*!< The reseed interval. */
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000155
Rose Zadik2f8163d2018-01-25 21:55:14 +0000156 mbedtls_aes_context aes_ctx; /*!< The AES context. */
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000157
158 /*
159 * Callbacks (Entropy)
160 */
161 int (*f_entropy)(void *, unsigned char *, size_t);
Rose Zadik2f8163d2018-01-25 21:55:14 +0000162 /*!< The entropy callback function. */
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000163
Rose Zadik2f8163d2018-01-25 21:55:14 +0000164 void *p_entropy; /*!< The context for the entropy function. */
Manuel Pégourié-Gonnard0a4fb092015-05-07 12:50:31 +0100165
166#if defined(MBEDTLS_THREADING_C)
167 mbedtls_threading_mutex_t mutex;
168#endif
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000169}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170mbedtls_ctr_drbg_context;
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000171
172/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000173 * \brief This function initializes the CTR_DRBG context,
174 * and prepares it for mbedtls_ctr_drbg_seed()
175 * or mbedtls_ctr_drbg_free().
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200176 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000177 * \param ctx The CTR_DRBG context to initialize.
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200178 */
179void mbedtls_ctr_drbg_init( mbedtls_ctr_drbg_context *ctx );
180
181/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000182 * \brief This function seeds and sets up the CTR_DRBG
183 * entropy source for future reseeds.
Paul Bakker9af723c2014-05-01 13:03:14 +0200184 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000185 * \note Personalization data can be provided in addition to the more generic
186 * entropy source, to make this instantiation as unique as possible.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000187 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000188 * \param ctx The CTR_DRBG context to seed.
189 * \param f_entropy The entropy callback, taking as arguments the
190 * \p p_entropy context, the buffer to fill, and the
Gilles Peskine08875d42019-09-24 14:40:40 +0200191 * length of the buffer.
Rose Zadik2f8163d2018-01-25 21:55:14 +0000192 * \param p_entropy The entropy context.
193 * \param custom Personalization data, that is device-specific
Gilles Peskine08875d42019-09-24 14:40:40 +0200194 * identifiers. This can be NULL, in which case the
195 * personalization data is empty regardless of the value
196 * of \p len.
Rose Zadik2f8163d2018-01-25 21:55:14 +0000197 * \param len The length of the personalization data.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000198 *
Rose Zadikc9474eb2018-03-27 10:58:22 +0100199 * \return \c 0 on success.
200 * \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED on failure.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000201 */
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200202int mbedtls_ctr_drbg_seed( mbedtls_ctr_drbg_context *ctx,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000203 int (*f_entropy)(void *, unsigned char *, size_t),
204 void *p_entropy,
Paul Bakker1bc9efc2011-12-03 11:29:32 +0000205 const unsigned char *custom,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000206 size_t len );
207
208/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000209 * \brief This function clears CTR_CRBG context data.
Paul Bakkerfff03662014-06-18 16:21:25 +0200210 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000211 * \param ctx The CTR_DRBG context to clear.
Paul Bakkerfff03662014-06-18 16:21:25 +0200212 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213void mbedtls_ctr_drbg_free( mbedtls_ctr_drbg_context *ctx );
Paul Bakkerfff03662014-06-18 16:21:25 +0200214
215/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000216 * \brief This function turns prediction resistance on or off.
217 * The default value is off.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000218 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000219 * \note If enabled, entropy is gathered at the beginning of
220 * every call to mbedtls_ctr_drbg_random_with_add().
221 * Only use this if your entropy source has sufficient
222 * throughput.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000223 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000224 * \param ctx The CTR_DRBG context.
225 * \param resistance #MBEDTLS_CTR_DRBG_PR_ON or #MBEDTLS_CTR_DRBG_PR_OFF.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000226 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227void mbedtls_ctr_drbg_set_prediction_resistance( mbedtls_ctr_drbg_context *ctx,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000228 int resistance );
229
230/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000231 * \brief This function sets the amount of entropy grabbed on each
232 * seed or reseed. The default value is
233 * #MBEDTLS_CTR_DRBG_ENTROPY_LEN.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000234 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000235 * \param ctx The CTR_DRBG context.
Gilles Peskine08875d42019-09-24 14:40:40 +0200236 * \param len The amount of entropy to grab, in bytes.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000237 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238void mbedtls_ctr_drbg_set_entropy_len( mbedtls_ctr_drbg_context *ctx,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000239 size_t len );
240
241/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000242 * \brief This function sets the reseed interval.
243 * The default value is #MBEDTLS_CTR_DRBG_RESEED_INTERVAL.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000244 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000245 * \param ctx The CTR_DRBG context.
246 * \param interval The reseed interval.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000247 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248void mbedtls_ctr_drbg_set_reseed_interval( mbedtls_ctr_drbg_context *ctx,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000249 int interval );
250
251/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000252 * \brief This function reseeds the CTR_DRBG context, that is
253 * extracts data from the entropy source.
Paul Bakker9af723c2014-05-01 13:03:14 +0200254 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000255 * \param ctx The CTR_DRBG context.
256 * \param additional Additional data to add to the state. Can be NULL.
257 * \param len The length of the additional data.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000258 *
Rose Zadikc9474eb2018-03-27 10:58:22 +0100259 * \return \c 0 on success.
260 * \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED on failure.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000261 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262int mbedtls_ctr_drbg_reseed( mbedtls_ctr_drbg_context *ctx,
Paul Bakker1bc9efc2011-12-03 11:29:32 +0000263 const unsigned char *additional, size_t len );
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000264
265/**
Rose Zadikc9474eb2018-03-27 10:58:22 +0100266 * \brief This function updates the state of the CTR_DRBG context.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000267 *
Rose Zadikc9474eb2018-03-27 10:58:22 +0100268 * \param ctx The CTR_DRBG context.
Gilles Peskine08875d42019-09-24 14:40:40 +0200269 * \param additional The data to update the state with. This must not be
270 * null unless \p add_len is 0.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500271 * \param add_len Length of \p additional in bytes. This must be at
272 * most #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT.
Rose Zadikc9474eb2018-03-27 10:58:22 +0100273 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500274 * \return \c 0 on success.
275 * \return #MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG if
276 * \p add_len is more than
277 * #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT.
278 * \return An error from the underlying AES cipher on failure.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000279 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500280int mbedtls_ctr_drbg_update_ret( mbedtls_ctr_drbg_context *ctx,
281 const unsigned char *additional,
282 size_t add_len );
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000283
284/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000285 * \brief This function updates a CTR_DRBG instance with additional
286 * data and uses it to generate random data.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000287 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000288 * \note The function automatically reseeds if the reseed counter is exceeded.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000289 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000290 * \param p_rng The CTR_DRBG context. This must be a pointer to a
291 * #mbedtls_ctr_drbg_context structure.
292 * \param output The buffer to fill.
293 * \param output_len The length of the buffer.
Gilles Peskine08875d42019-09-24 14:40:40 +0200294 * \param additional Additional data to update. Can be NULL, in which
295 * case the additional data is empty regardless of
296 * the value of \p add_len.
297 * \param add_len The length of the additional data
298 * if \p additional is non-null.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000299 *
Rose Zadikc9474eb2018-03-27 10:58:22 +0100300 * \return \c 0 on success.
301 * \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or
Rose Zadik2f8163d2018-01-25 21:55:14 +0000302 * #MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG on failure.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000303 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304int mbedtls_ctr_drbg_random_with_add( void *p_rng,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000305 unsigned char *output, size_t output_len,
Paul Bakker1bc9efc2011-12-03 11:29:32 +0000306 const unsigned char *additional, size_t add_len );
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000307
308/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000309 * \brief This function uses CTR_DRBG to generate random data.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000310 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000311 * \note The function automatically reseeds if the reseed counter is exceeded.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000312 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000313 * \param p_rng The CTR_DRBG context. This must be a pointer to a
314 * #mbedtls_ctr_drbg_context structure.
315 * \param output The buffer to fill.
316 * \param output_len The length of the buffer.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000317 *
Rose Zadikc9474eb2018-03-27 10:58:22 +0100318 * \return \c 0 on success.
319 * \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or
Rose Zadik2f8163d2018-01-25 21:55:14 +0000320 * #MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG on failure.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000321 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322int mbedtls_ctr_drbg_random( void *p_rng,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000323 unsigned char *output, size_t output_len );
324
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500325
326#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
327#if defined(MBEDTLS_DEPRECATED_WARNING)
328#define MBEDTLS_DEPRECATED __attribute__((deprecated))
329#else
330#define MBEDTLS_DEPRECATED
331#endif
332/**
333 * \brief This function updates the state of the CTR_DRBG context.
334 *
335 * \deprecated Superseded by mbedtls_ctr_drbg_update_ret()
336 * in 2.16.0.
337 *
338 * \note If \p add_len is greater than
339 * #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT, only the first
340 * #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT Bytes are used.
341 * The remaining Bytes are silently discarded.
342 *
343 * \param ctx The CTR_DRBG context.
344 * \param additional The data to update the state with.
345 * \param add_len Length of \p additional data.
346 */
347MBEDTLS_DEPRECATED void mbedtls_ctr_drbg_update(
348 mbedtls_ctr_drbg_context *ctx,
349 const unsigned char *additional,
350 size_t add_len );
351#undef MBEDTLS_DEPRECATED
352#endif /* !MBEDTLS_DEPRECATED_REMOVED */
353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354#if defined(MBEDTLS_FS_IO)
Paul Bakkerfc754a92011-12-05 13:23:51 +0000355/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000356 * \brief This function writes a seed file.
Paul Bakkerfc754a92011-12-05 13:23:51 +0000357 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000358 * \param ctx The CTR_DRBG context.
359 * \param path The name of the file.
Paul Bakkerfc754a92011-12-05 13:23:51 +0000360 *
Rose Zadikc9474eb2018-03-27 10:58:22 +0100361 * \return \c 0 on success.
Rose Zadikf25eb6e2018-04-16 14:51:52 +0100362 * \return #MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR on file error.
363 * \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED on
Rose Zadik2f8163d2018-01-25 21:55:14 +0000364 * failure.
Paul Bakkerfc754a92011-12-05 13:23:51 +0000365 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366int mbedtls_ctr_drbg_write_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
Paul Bakkerfc754a92011-12-05 13:23:51 +0000367
368/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000369 * \brief This function reads and updates a seed file. The seed
370 * is added to this instance.
Paul Bakkerfc754a92011-12-05 13:23:51 +0000371 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000372 * \param ctx The CTR_DRBG context.
373 * \param path The name of the file.
Paul Bakkerfc754a92011-12-05 13:23:51 +0000374 *
Rose Zadikc9474eb2018-03-27 10:58:22 +0100375 * \return \c 0 on success.
376 * \return #MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR on file error.
377 * \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or
Rose Zadik2f8163d2018-01-25 21:55:14 +0000378 * #MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG on failure.
Paul Bakkerfc754a92011-12-05 13:23:51 +0000379 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380int mbedtls_ctr_drbg_update_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
381#endif /* MBEDTLS_FS_IO */
Paul Bakkerfc754a92011-12-05 13:23:51 +0000382
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500383#if defined(MBEDTLS_SELF_TEST)
384
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000385/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000386 * \brief The CTR_DRBG checkup routine.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000387 *
Rose Zadikc9474eb2018-03-27 10:58:22 +0100388 * \return \c 0 on success.
389 * \return \c 1 on failure.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000390 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391int mbedtls_ctr_drbg_self_test( int verbose );
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000392
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500393#endif /* MBEDTLS_SELF_TEST */
394
Paul Bakker534f82c2013-06-25 16:47:55 +0200395/* Internal functions (do not call directly) */
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200396int mbedtls_ctr_drbg_seed_entropy_len( mbedtls_ctr_drbg_context *,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200397 int (*)(void *, unsigned char *, size_t), void *,
398 const unsigned char *, size_t, size_t );
Paul Bakker534f82c2013-06-25 16:47:55 +0200399
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000400#ifdef __cplusplus
401}
402#endif
403
404#endif /* ctr_drbg.h */