4 * \brief This file contains CTR_DRBG definitions and functions.
\r
6 * CTR_DRBG is a standardized way of building a PRNG from a block-cipher
\r
7 * in counter mode operation, as defined in <em>NIST SP 800-90A:
\r
8 * Recommendation for Random Number Generation Using Deterministic Random
\r
9 * Bit Generators</em>.
\r
11 * The Mbed TLS implementation of CTR_DRBG uses AES-256 (default) or AES-128
\r
12 * as the underlying block cipher.
\r
14 * \warning Using 128-bit keys for CTR_DRBG limits the security of generated
\r
15 * keys and operations that use random values generated to 128-bit security.
\r
18 * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
\r
19 * SPDX-License-Identifier: Apache-2.0
\r
21 * Licensed under the Apache License, Version 2.0 (the "License"); you may
\r
22 * not use this file except in compliance with the License.
\r
23 * You may obtain a copy of the License at
\r
25 * http://www.apache.org/licenses/LICENSE-2.0
\r
27 * Unless required by applicable law or agreed to in writing, software
\r
28 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
\r
29 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
30 * See the License for the specific language governing permissions and
\r
31 * limitations under the License.
\r
33 * This file is part of Mbed TLS (https://tls.mbed.org)
\r
36 #ifndef MBEDTLS_CTR_DRBG_H
\r
37 #define MBEDTLS_CTR_DRBG_H
\r
39 #if !defined(MBEDTLS_CONFIG_FILE)
\r
42 #include MBEDTLS_CONFIG_FILE
\r
47 #if defined(MBEDTLS_THREADING_C)
\r
48 #include "threading.h"
\r
51 #define MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED -0x0034 /**< The entropy source failed. */
\r
52 #define MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG -0x0036 /**< The requested random buffer length is too big. */
\r
53 #define MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG -0x0038 /**< The input (entropy + additional data) is too large. */
\r
54 #define MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR -0x003A /**< Read or write error in file. */
\r
56 #define MBEDTLS_CTR_DRBG_BLOCKSIZE 16 /**< The block size used by the cipher. */
\r
58 #if defined(MBEDTLS_CTR_DRBG_USE_128_BIT_KEY)
\r
59 #define MBEDTLS_CTR_DRBG_KEYSIZE 16 /**< The key size used by the cipher (compile-time choice: 128 bits). */
\r
61 #define MBEDTLS_CTR_DRBG_KEYSIZE 32 /**< The key size used by the cipher (compile-time choice: 256 bits). */
\r
64 #define MBEDTLS_CTR_DRBG_KEYBITS ( MBEDTLS_CTR_DRBG_KEYSIZE * 8 ) /**< The key size for the DRBG operation, in bits. */
\r
65 #define MBEDTLS_CTR_DRBG_SEEDLEN ( MBEDTLS_CTR_DRBG_KEYSIZE + MBEDTLS_CTR_DRBG_BLOCKSIZE ) /**< The seed length, calculated as (counter + AES key). */
\r
68 * \name SECTION: Module settings
\r
70 * The configuration options you can set for this module are in this section.
\r
71 * Either change them in config.h or define them using the compiler command
\r
76 #if !defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN)
\r
77 #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256)
\r
78 #define MBEDTLS_CTR_DRBG_ENTROPY_LEN 48
\r
79 /**< The amount of entropy used per seed by default:
\r
80 * <ul><li>48 with SHA-512.</li>
\r
81 * <li>32 with SHA-256.</li></ul>
\r
84 #define MBEDTLS_CTR_DRBG_ENTROPY_LEN 32
\r
85 /**< Amount of entropy used per seed by default:
\r
86 * <ul><li>48 with SHA-512.</li>
\r
87 * <li>32 with SHA-256.</li></ul>
\r
92 #if !defined(MBEDTLS_CTR_DRBG_RESEED_INTERVAL)
\r
93 #define MBEDTLS_CTR_DRBG_RESEED_INTERVAL 10000
\r
94 /**< The interval before reseed is performed by default. */
\r
97 #if !defined(MBEDTLS_CTR_DRBG_MAX_INPUT)
\r
98 #define MBEDTLS_CTR_DRBG_MAX_INPUT 256
\r
99 /**< The maximum number of additional input Bytes. */
\r
102 #if !defined(MBEDTLS_CTR_DRBG_MAX_REQUEST)
\r
103 #define MBEDTLS_CTR_DRBG_MAX_REQUEST 1024
\r
104 /**< The maximum number of requested Bytes per call. */
\r
107 #if !defined(MBEDTLS_CTR_DRBG_MAX_SEED_INPUT)
\r
108 #define MBEDTLS_CTR_DRBG_MAX_SEED_INPUT 384
\r
109 /**< The maximum size of seed or reseed buffer. */
\r
112 /* \} name SECTION: Module settings */
\r
114 #define MBEDTLS_CTR_DRBG_PR_OFF 0
\r
115 /**< Prediction resistance is disabled. */
\r
116 #define MBEDTLS_CTR_DRBG_PR_ON 1
\r
117 /**< Prediction resistance is enabled. */
\r
124 * \brief The CTR_DRBG context structure.
\r
126 typedef struct mbedtls_ctr_drbg_context
\r
128 unsigned char counter[16]; /*!< The counter (V). */
\r
129 int reseed_counter; /*!< The reseed counter. */
\r
130 int prediction_resistance; /*!< This determines whether prediction
\r
131 resistance is enabled, that is
\r
132 whether to systematically reseed before
\r
133 each random generation. */
\r
134 size_t entropy_len; /*!< The amount of entropy grabbed on each
\r
135 seed or reseed operation. */
\r
136 int reseed_interval; /*!< The reseed interval. */
\r
138 mbedtls_aes_context aes_ctx; /*!< The AES context. */
\r
141 * Callbacks (Entropy)
\r
143 int (*f_entropy)(void *, unsigned char *, size_t);
\r
144 /*!< The entropy callback function. */
\r
146 void *p_entropy; /*!< The context for the entropy function. */
\r
148 #if defined(MBEDTLS_THREADING_C)
\r
149 mbedtls_threading_mutex_t mutex;
\r
152 mbedtls_ctr_drbg_context;
\r
155 * \brief This function initializes the CTR_DRBG context,
\r
156 * and prepares it for mbedtls_ctr_drbg_seed()
\r
157 * or mbedtls_ctr_drbg_free().
\r
159 * \param ctx The CTR_DRBG context to initialize.
\r
161 void mbedtls_ctr_drbg_init( mbedtls_ctr_drbg_context *ctx );
\r
164 * \brief This function seeds and sets up the CTR_DRBG
\r
165 * entropy source for future reseeds.
\r
167 * \note Personalization data can be provided in addition to the more generic
\r
168 * entropy source, to make this instantiation as unique as possible.
\r
170 * \param ctx The CTR_DRBG context to seed.
\r
171 * \param f_entropy The entropy callback, taking as arguments the
\r
172 * \p p_entropy context, the buffer to fill, and the
\r
173 length of the buffer.
\r
174 * \param p_entropy The entropy context.
\r
175 * \param custom Personalization data, that is device-specific
\r
176 identifiers. Can be NULL.
\r
177 * \param len The length of the personalization data.
\r
179 * \return \c 0 on success.
\r
180 * \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED on failure.
\r
182 int mbedtls_ctr_drbg_seed( mbedtls_ctr_drbg_context *ctx,
\r
183 int (*f_entropy)(void *, unsigned char *, size_t),
\r
185 const unsigned char *custom,
\r
189 * \brief This function clears CTR_CRBG context data.
\r
191 * \param ctx The CTR_DRBG context to clear.
\r
193 void mbedtls_ctr_drbg_free( mbedtls_ctr_drbg_context *ctx );
\r
196 * \brief This function turns prediction resistance on or off.
\r
197 * The default value is off.
\r
199 * \note If enabled, entropy is gathered at the beginning of
\r
200 * every call to mbedtls_ctr_drbg_random_with_add().
\r
201 * Only use this if your entropy source has sufficient
\r
204 * \param ctx The CTR_DRBG context.
\r
205 * \param resistance #MBEDTLS_CTR_DRBG_PR_ON or #MBEDTLS_CTR_DRBG_PR_OFF.
\r
207 void mbedtls_ctr_drbg_set_prediction_resistance( mbedtls_ctr_drbg_context *ctx,
\r
211 * \brief This function sets the amount of entropy grabbed on each
\r
212 * seed or reseed. The default value is
\r
213 * #MBEDTLS_CTR_DRBG_ENTROPY_LEN.
\r
215 * \param ctx The CTR_DRBG context.
\r
216 * \param len The amount of entropy to grab.
\r
218 void mbedtls_ctr_drbg_set_entropy_len( mbedtls_ctr_drbg_context *ctx,
\r
222 * \brief This function sets the reseed interval.
\r
223 * The default value is #MBEDTLS_CTR_DRBG_RESEED_INTERVAL.
\r
225 * \param ctx The CTR_DRBG context.
\r
226 * \param interval The reseed interval.
\r
228 void mbedtls_ctr_drbg_set_reseed_interval( mbedtls_ctr_drbg_context *ctx,
\r
232 * \brief This function reseeds the CTR_DRBG context, that is
\r
233 * extracts data from the entropy source.
\r
235 * \param ctx The CTR_DRBG context.
\r
236 * \param additional Additional data to add to the state. Can be NULL.
\r
237 * \param len The length of the additional data.
\r
239 * \return \c 0 on success.
\r
240 * \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED on failure.
\r
242 int mbedtls_ctr_drbg_reseed( mbedtls_ctr_drbg_context *ctx,
\r
243 const unsigned char *additional, size_t len );
\r
246 * \brief This function updates the state of the CTR_DRBG context.
\r
248 * \param ctx The CTR_DRBG context.
\r
249 * \param additional The data to update the state with.
\r
250 * \param add_len Length of \p additional in bytes. This must be at
\r
251 * most #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT.
\r
253 * \return \c 0 on success.
\r
254 * \return #MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG if
\r
255 * \p add_len is more than
\r
256 * #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT.
\r
257 * \return An error from the underlying AES cipher on failure.
\r
259 int mbedtls_ctr_drbg_update_ret( mbedtls_ctr_drbg_context *ctx,
\r
260 const unsigned char *additional,
\r
264 * \brief This function updates a CTR_DRBG instance with additional
\r
265 * data and uses it to generate random data.
\r
267 * \note The function automatically reseeds if the reseed counter is exceeded.
\r
269 * \param p_rng The CTR_DRBG context. This must be a pointer to a
\r
270 * #mbedtls_ctr_drbg_context structure.
\r
271 * \param output The buffer to fill.
\r
272 * \param output_len The length of the buffer.
\r
273 * \param additional Additional data to update. Can be NULL.
\r
274 * \param add_len The length of the additional data.
\r
276 * \return \c 0 on success.
\r
277 * \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or
\r
278 * #MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG on failure.
\r
280 int mbedtls_ctr_drbg_random_with_add( void *p_rng,
\r
281 unsigned char *output, size_t output_len,
\r
282 const unsigned char *additional, size_t add_len );
\r
285 * \brief This function uses CTR_DRBG to generate random data.
\r
287 * \note The function automatically reseeds if the reseed counter is exceeded.
\r
289 * \param p_rng The CTR_DRBG context. This must be a pointer to a
\r
290 * #mbedtls_ctr_drbg_context structure.
\r
291 * \param output The buffer to fill.
\r
292 * \param output_len The length of the buffer.
\r
294 * \return \c 0 on success.
\r
295 * \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or
\r
296 * #MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG on failure.
\r
298 int mbedtls_ctr_drbg_random( void *p_rng,
\r
299 unsigned char *output, size_t output_len );
\r
302 #if ! defined(MBEDTLS_DEPRECATED_REMOVED)
\r
303 #if defined(MBEDTLS_DEPRECATED_WARNING)
\r
304 #define MBEDTLS_DEPRECATED __attribute__((deprecated))
\r
306 #define MBEDTLS_DEPRECATED
\r
309 * \brief This function updates the state of the CTR_DRBG context.
\r
311 * \deprecated Superseded by mbedtls_ctr_drbg_update_ret()
\r
314 * \note If \p add_len is greater than
\r
315 * #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT, only the first
\r
316 * #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT Bytes are used.
\r
317 * The remaining Bytes are silently discarded.
\r
319 * \param ctx The CTR_DRBG context.
\r
320 * \param additional The data to update the state with.
\r
321 * \param add_len Length of \p additional data.
\r
323 MBEDTLS_DEPRECATED void mbedtls_ctr_drbg_update(
\r
324 mbedtls_ctr_drbg_context *ctx,
\r
325 const unsigned char *additional,
\r
327 #undef MBEDTLS_DEPRECATED
\r
328 #endif /* !MBEDTLS_DEPRECATED_REMOVED */
\r
330 #if defined(MBEDTLS_FS_IO)
\r
332 * \brief This function writes a seed file.
\r
334 * \param ctx The CTR_DRBG context.
\r
335 * \param path The name of the file.
\r
337 * \return \c 0 on success.
\r
338 * \return #MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR on file error.
\r
339 * \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED on
\r
342 int mbedtls_ctr_drbg_write_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
\r
345 * \brief This function reads and updates a seed file. The seed
\r
346 * is added to this instance.
\r
348 * \param ctx The CTR_DRBG context.
\r
349 * \param path The name of the file.
\r
351 * \return \c 0 on success.
\r
352 * \return #MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR on file error.
\r
353 * \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or
\r
354 * #MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG on failure.
\r
356 int mbedtls_ctr_drbg_update_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
\r
357 #endif /* MBEDTLS_FS_IO */
\r
359 #if defined(MBEDTLS_SELF_TEST)
\r
362 * \brief The CTR_DRBG checkup routine.
\r
364 * \return \c 0 on success.
\r
365 * \return \c 1 on failure.
\r
367 int mbedtls_ctr_drbg_self_test( int verbose );
\r
369 #endif /* MBEDTLS_SELF_TEST */
\r
371 /* Internal functions (do not call directly) */
\r
372 int mbedtls_ctr_drbg_seed_entropy_len( mbedtls_ctr_drbg_context *,
\r
373 int (*)(void *, unsigned char *, size_t), void *,
\r
374 const unsigned char *, size_t, size_t );
\r
380 #endif /* ctr_drbg.h */
\r