blob: f0f81631239daacbf3a105137cefcc85129cef52 [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 Sonnenscheina4588d42018-07-30 16:59:36 +030014 * * \warning ARC4 is considered a weak cipher and its use constitutes a
15 * security risk. We recommend considering stronger ciphers instead.
Darryl Greena40a1012018-01-05 15:33:17 +000016 */
17/*
Rose Zadik2f8163d2018-01-25 21:55:14 +000018 * Copyright (C) 2006-2018, 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
Paul Bakker0e04d0e2011-11-27 14:46:59 +000039#include "aes.h"
40
Manuel Pégourié-Gonnard0a4fb092015-05-07 12:50:31 +010041#if defined(MBEDTLS_THREADING_C)
Ron Eldor6fd941f2017-05-14 16:17:33 +030042#include "threading.h"
Manuel Pégourié-Gonnard0a4fb092015-05-07 12:50:31 +010043#endif
44
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#define MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED -0x0034 /**< The entropy source failed. */
Rose Zadik2f8163d2018-01-25 21:55:14 +000046#define MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG -0x0036 /**< The requested random buffer length is too big. */
47#define MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG -0x0038 /**< The input (entropy + additional data) is too large. */
48#define MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR -0x003A /**< Read or write error in file. */
Paul Bakker0e04d0e2011-11-27 14:46:59 +000049
Rose Zadik2f8163d2018-01-25 21:55:14 +000050#define MBEDTLS_CTR_DRBG_BLOCKSIZE 16 /**< The block size used by the cipher. */
Nir Sonnenscheineb73f7a2018-07-30 17:46:49 +030051#if defined(MBEDTLS_CTR_DRBG_KEY_SIZE_256)
Rose Zadik2f8163d2018-01-25 21:55:14 +000052#define MBEDTLS_CTR_DRBG_KEYSIZE 32 /**< The key size used by the cipher. */
Nir Sonnenscheineb73f7a2018-07-30 17:46:49 +030053#else
54#if defined(MBEDTLS_CTR_DRBG_KEY_SIZE_128)
Nir Sonnenscheina4588d42018-07-30 16:59:36 +030055#warning Warning: using smaller (128bit) key size for CTR DRBG may reduce the security of some operations.
56#define MBEDTLS_CTR_DRBG_KEYSIZE 16 /**< The key size used by the cipher. */
Nir Sonnenscheineb73f7a2018-07-30 17:46:49 +030057#else
58#error for ctr DRBG either MBEDTLS_CTR_DRBG_KEY_SIZE_256 (default) or MBEDTLS_CTR_DRBG_KEY_SIZE_128 must be set
Nir Sonnenscheina4588d42018-07-30 16:59:36 +030059#endif
60#endif
Rose Zadik2f8163d2018-01-25 21:55:14 +000061#define MBEDTLS_CTR_DRBG_KEYBITS ( MBEDTLS_CTR_DRBG_KEYSIZE * 8 ) /**< The key size for the DRBG operation, in bits. */
62#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 +020063
Paul Bakker088c5c52014-04-25 11:11:10 +020064/**
65 * \name SECTION: Module settings
66 *
67 * The configuration options you can set for this module are in this section.
Rose Zadik2f8163d2018-01-25 21:55:14 +000068 * Either change them in config.h or define them using the compiler command
69 * line.
Paul Bakker088c5c52014-04-25 11:11:10 +020070 * \{
71 */
72
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073#if !defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN)
74#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256)
Rose Zadik2f8163d2018-01-25 21:55:14 +000075#define MBEDTLS_CTR_DRBG_ENTROPY_LEN 48
76/**< The amount of entropy used per seed by default:
77 * <ul><li>48 with SHA-512.</li>
78 * <li>32 with SHA-256.</li></ul>
79 */
Paul Bakkerfb08fd22013-08-27 15:06:26 +020080#else
Rose Zadik2f8163d2018-01-25 21:55:14 +000081#define MBEDTLS_CTR_DRBG_ENTROPY_LEN 32
82/**< Amount of entropy used per seed by default:
83 * <ul><li>48 with SHA-512.</li>
84 * <li>32 with SHA-256.</li></ul>
85 */
Paul Bakkerfb08fd22013-08-27 15:06:26 +020086#endif
Paul Bakker088c5c52014-04-25 11:11:10 +020087#endif
88
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089#if !defined(MBEDTLS_CTR_DRBG_RESEED_INTERVAL)
Rose Zadik2f8163d2018-01-25 21:55:14 +000090#define MBEDTLS_CTR_DRBG_RESEED_INTERVAL 10000
91/**< The interval before reseed is performed by default. */
Paul Bakker088c5c52014-04-25 11:11:10 +020092#endif
93
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094#if !defined(MBEDTLS_CTR_DRBG_MAX_INPUT)
Rose Zadik2f8163d2018-01-25 21:55:14 +000095#define MBEDTLS_CTR_DRBG_MAX_INPUT 256
96/**< The maximum number of additional input Bytes. */
Paul Bakker088c5c52014-04-25 11:11:10 +020097#endif
98
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099#if !defined(MBEDTLS_CTR_DRBG_MAX_REQUEST)
Rose Zadik2f8163d2018-01-25 21:55:14 +0000100#define MBEDTLS_CTR_DRBG_MAX_REQUEST 1024
101/**< The maximum number of requested Bytes per call. */
Paul Bakker088c5c52014-04-25 11:11:10 +0200102#endif
103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104#if !defined(MBEDTLS_CTR_DRBG_MAX_SEED_INPUT)
Rose Zadik2f8163d2018-01-25 21:55:14 +0000105#define MBEDTLS_CTR_DRBG_MAX_SEED_INPUT 384
106/**< The maximum size of seed or reseed buffer. */
Paul Bakker088c5c52014-04-25 11:11:10 +0200107#endif
108
109/* \} name SECTION: Module settings */
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000110
Rose Zadik2f8163d2018-01-25 21:55:14 +0000111#define MBEDTLS_CTR_DRBG_PR_OFF 0
112/**< Prediction resistance is disabled. */
113#define MBEDTLS_CTR_DRBG_PR_ON 1
114/**< Prediction resistance is enabled. */
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000115
116#ifdef __cplusplus
117extern "C" {
118#endif
119
120/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000121 * \brief The CTR_DRBG context structure.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000122 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200123typedef struct mbedtls_ctr_drbg_context
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000124{
Rose Zadik2f8163d2018-01-25 21:55:14 +0000125 unsigned char counter[16]; /*!< The counter (V). */
126 int reseed_counter; /*!< The reseed counter. */
127 int prediction_resistance; /*!< This determines whether prediction
128 resistance is enabled, that is
129 whether to systematically reseed before
130 each random generation. */
131 size_t entropy_len; /*!< The amount of entropy grabbed on each
132 seed or reseed operation. */
133 int reseed_interval; /*!< The reseed interval. */
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000134
Rose Zadik2f8163d2018-01-25 21:55:14 +0000135 mbedtls_aes_context aes_ctx; /*!< The AES context. */
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000136
137 /*
138 * Callbacks (Entropy)
139 */
140 int (*f_entropy)(void *, unsigned char *, size_t);
Rose Zadik2f8163d2018-01-25 21:55:14 +0000141 /*!< The entropy callback function. */
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000142
Rose Zadik2f8163d2018-01-25 21:55:14 +0000143 void *p_entropy; /*!< The context for the entropy function. */
Manuel Pégourié-Gonnard0a4fb092015-05-07 12:50:31 +0100144
145#if defined(MBEDTLS_THREADING_C)
146 mbedtls_threading_mutex_t mutex;
147#endif
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000148}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149mbedtls_ctr_drbg_context;
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000150
151/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000152 * \brief This function initializes the CTR_DRBG context,
153 * and prepares it for mbedtls_ctr_drbg_seed()
154 * or mbedtls_ctr_drbg_free().
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200155 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000156 * \param ctx The CTR_DRBG context to initialize.
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200157 */
158void mbedtls_ctr_drbg_init( mbedtls_ctr_drbg_context *ctx );
159
160/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000161 * \brief This function seeds and sets up the CTR_DRBG
162 * entropy source for future reseeds.
Paul Bakker9af723c2014-05-01 13:03:14 +0200163 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000164 * \note Personalization data can be provided in addition to the more generic
165 * entropy source, to make this instantiation as unique as possible.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000166 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000167 * \param ctx The CTR_DRBG context to seed.
168 * \param f_entropy The entropy callback, taking as arguments the
169 * \p p_entropy context, the buffer to fill, and the
170 length of the buffer.
171 * \param p_entropy The entropy context.
172 * \param custom Personalization data, that is device-specific
173 identifiers. Can be NULL.
174 * \param len The length of the personalization data.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000175 *
Rose Zadikc9474eb2018-03-27 10:58:22 +0100176 * \return \c 0 on success.
177 * \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED on failure.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000178 */
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200179int mbedtls_ctr_drbg_seed( mbedtls_ctr_drbg_context *ctx,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000180 int (*f_entropy)(void *, unsigned char *, size_t),
181 void *p_entropy,
Paul Bakker1bc9efc2011-12-03 11:29:32 +0000182 const unsigned char *custom,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000183 size_t len );
184
185/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000186 * \brief This function clears CTR_CRBG context data.
Paul Bakkerfff03662014-06-18 16:21:25 +0200187 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000188 * \param ctx The CTR_DRBG context to clear.
Paul Bakkerfff03662014-06-18 16:21:25 +0200189 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190void mbedtls_ctr_drbg_free( mbedtls_ctr_drbg_context *ctx );
Paul Bakkerfff03662014-06-18 16:21:25 +0200191
192/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000193 * \brief This function turns prediction resistance on or off.
194 * The default value is off.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000195 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000196 * \note If enabled, entropy is gathered at the beginning of
197 * every call to mbedtls_ctr_drbg_random_with_add().
198 * Only use this if your entropy source has sufficient
199 * throughput.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000200 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000201 * \param ctx The CTR_DRBG context.
202 * \param resistance #MBEDTLS_CTR_DRBG_PR_ON or #MBEDTLS_CTR_DRBG_PR_OFF.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000203 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204void mbedtls_ctr_drbg_set_prediction_resistance( mbedtls_ctr_drbg_context *ctx,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000205 int resistance );
206
207/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000208 * \brief This function sets the amount of entropy grabbed on each
209 * seed or reseed. The default value is
210 * #MBEDTLS_CTR_DRBG_ENTROPY_LEN.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000211 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000212 * \param ctx The CTR_DRBG context.
213 * \param len The amount of entropy to grab.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000214 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215void mbedtls_ctr_drbg_set_entropy_len( mbedtls_ctr_drbg_context *ctx,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000216 size_t len );
217
218/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000219 * \brief This function sets the reseed interval.
220 * The default value is #MBEDTLS_CTR_DRBG_RESEED_INTERVAL.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000221 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000222 * \param ctx The CTR_DRBG context.
223 * \param interval The reseed interval.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000224 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225void mbedtls_ctr_drbg_set_reseed_interval( mbedtls_ctr_drbg_context *ctx,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000226 int interval );
227
228/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000229 * \brief This function reseeds the CTR_DRBG context, that is
230 * extracts data from the entropy source.
Paul Bakker9af723c2014-05-01 13:03:14 +0200231 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000232 * \param ctx The CTR_DRBG context.
233 * \param additional Additional data to add to the state. Can be NULL.
234 * \param len The length of the additional data.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000235 *
Rose Zadikc9474eb2018-03-27 10:58:22 +0100236 * \return \c 0 on success.
237 * \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED on failure.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000238 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239int mbedtls_ctr_drbg_reseed( mbedtls_ctr_drbg_context *ctx,
Paul Bakker1bc9efc2011-12-03 11:29:32 +0000240 const unsigned char *additional, size_t len );
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000241
242/**
Rose Zadikc9474eb2018-03-27 10:58:22 +0100243 * \brief This function updates the state of the CTR_DRBG context.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000244 *
Rose Zadikc9474eb2018-03-27 10:58:22 +0100245 * \note If \p add_len is greater than
246 * #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT, only the first
247 * #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT Bytes are used.
248 * The remaining Bytes are silently discarded.
Manuel Pégourié-Gonnard5cb4b312014-11-25 17:41:50 +0100249 *
Rose Zadikc9474eb2018-03-27 10:58:22 +0100250 * \param ctx The CTR_DRBG context.
251 * \param additional The data to update the state with.
252 * \param add_len Length of \p additional data.
253 *
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000254 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255void mbedtls_ctr_drbg_update( mbedtls_ctr_drbg_context *ctx,
Paul Bakkerfc754a92011-12-05 13:23:51 +0000256 const unsigned char *additional, size_t add_len );
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000257
258/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000259 * \brief This function updates a CTR_DRBG instance with additional
260 * data and uses it to generate random data.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000261 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000262 * \note The function automatically reseeds if the reseed counter is exceeded.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000263 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000264 * \param p_rng The CTR_DRBG context. This must be a pointer to a
265 * #mbedtls_ctr_drbg_context structure.
266 * \param output The buffer to fill.
267 * \param output_len The length of the buffer.
268 * \param additional Additional data to update. Can be NULL.
269 * \param add_len The length of the additional data.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000270 *
Rose Zadikc9474eb2018-03-27 10:58:22 +0100271 * \return \c 0 on success.
272 * \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or
Rose Zadik2f8163d2018-01-25 21:55:14 +0000273 * #MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG on failure.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000274 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275int mbedtls_ctr_drbg_random_with_add( void *p_rng,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000276 unsigned char *output, size_t output_len,
Paul Bakker1bc9efc2011-12-03 11:29:32 +0000277 const unsigned char *additional, size_t add_len );
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000278
279/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000280 * \brief This function uses CTR_DRBG to generate random data.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000281 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000282 * \note The function automatically reseeds if the reseed counter is exceeded.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000283 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000284 * \param p_rng The CTR_DRBG context. This must be a pointer to a
285 * #mbedtls_ctr_drbg_context structure.
286 * \param output The buffer to fill.
287 * \param output_len The length of the buffer.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000288 *
Rose Zadikc9474eb2018-03-27 10:58:22 +0100289 * \return \c 0 on success.
290 * \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or
Rose Zadik2f8163d2018-01-25 21:55:14 +0000291 * #MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG on failure.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000292 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293int mbedtls_ctr_drbg_random( void *p_rng,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000294 unsigned char *output, size_t output_len );
295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296#if defined(MBEDTLS_FS_IO)
Paul Bakkerfc754a92011-12-05 13:23:51 +0000297/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000298 * \brief This function writes a seed file.
Paul Bakkerfc754a92011-12-05 13:23:51 +0000299 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000300 * \param ctx The CTR_DRBG context.
301 * \param path The name of the file.
Paul Bakkerfc754a92011-12-05 13:23:51 +0000302 *
Rose Zadikc9474eb2018-03-27 10:58:22 +0100303 * \return \c 0 on success.
Rose Zadikf25eb6e2018-04-16 14:51:52 +0100304 * \return #MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR on file error.
305 * \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED on
Rose Zadik2f8163d2018-01-25 21:55:14 +0000306 * failure.
Paul Bakkerfc754a92011-12-05 13:23:51 +0000307 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308int mbedtls_ctr_drbg_write_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
Paul Bakkerfc754a92011-12-05 13:23:51 +0000309
310/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000311 * \brief This function reads and updates a seed file. The seed
312 * is added to this instance.
Paul Bakkerfc754a92011-12-05 13:23:51 +0000313 *
Rose Zadik2f8163d2018-01-25 21:55:14 +0000314 * \param ctx The CTR_DRBG context.
315 * \param path The name of the file.
Paul Bakkerfc754a92011-12-05 13:23:51 +0000316 *
Rose Zadikc9474eb2018-03-27 10:58:22 +0100317 * \return \c 0 on success.
318 * \return #MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR on file error.
319 * \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or
Rose Zadik2f8163d2018-01-25 21:55:14 +0000320 * #MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG on failure.
Paul Bakkerfc754a92011-12-05 13:23:51 +0000321 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322int mbedtls_ctr_drbg_update_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
323#endif /* MBEDTLS_FS_IO */
Paul Bakkerfc754a92011-12-05 13:23:51 +0000324
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000325/**
Rose Zadik2f8163d2018-01-25 21:55:14 +0000326 * \brief The CTR_DRBG checkup routine.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000327 *
Rose Zadikc9474eb2018-03-27 10:58:22 +0100328 * \return \c 0 on success.
329 * \return \c 1 on failure.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000330 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331int mbedtls_ctr_drbg_self_test( int verbose );
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000332
Paul Bakker534f82c2013-06-25 16:47:55 +0200333/* Internal functions (do not call directly) */
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200334int mbedtls_ctr_drbg_seed_entropy_len( mbedtls_ctr_drbg_context *,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200335 int (*)(void *, unsigned char *, size_t), void *,
336 const unsigned char *, size_t, size_t );
Paul Bakker534f82c2013-06-25 16:47:55 +0200337
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000338#ifdef __cplusplus
339}
340#endif
341
342#endif /* ctr_drbg.h */