4 * \brief HMAC_DRBG (NIST SP 800-90A)
\r
7 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
\r
8 * SPDX-License-Identifier: Apache-2.0
\r
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
\r
11 * not use this file except in compliance with the License.
\r
12 * You may obtain a copy of the License at
\r
14 * http://www.apache.org/licenses/LICENSE-2.0
\r
16 * Unless required by applicable law or agreed to in writing, software
\r
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
\r
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
19 * See the License for the specific language governing permissions and
\r
20 * limitations under the License.
\r
22 * This file is part of mbed TLS (https://tls.mbed.org)
\r
24 #ifndef MBEDTLS_HMAC_DRBG_H
\r
25 #define MBEDTLS_HMAC_DRBG_H
\r
27 #if !defined(MBEDTLS_CONFIG_FILE)
\r
30 #include MBEDTLS_CONFIG_FILE
\r
35 #if defined(MBEDTLS_THREADING_C)
\r
36 #include "threading.h"
\r
42 #define MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG -0x0003 /**< Too many random requested in single call. */
\r
43 #define MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG -0x0005 /**< Input too large (Entropy + additional). */
\r
44 #define MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR -0x0007 /**< Read/write error in file. */
\r
45 #define MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED -0x0009 /**< The entropy source failed. */
\r
48 * \name SECTION: Module settings
\r
50 * The configuration options you can set for this module are in this section.
\r
51 * Either change them in config.h or define them on the compiler command line.
\r
55 #if !defined(MBEDTLS_HMAC_DRBG_RESEED_INTERVAL)
\r
56 #define MBEDTLS_HMAC_DRBG_RESEED_INTERVAL 10000 /**< Interval before reseed is performed by default */
\r
59 #if !defined(MBEDTLS_HMAC_DRBG_MAX_INPUT)
\r
60 #define MBEDTLS_HMAC_DRBG_MAX_INPUT 256 /**< Maximum number of additional input bytes */
\r
63 #if !defined(MBEDTLS_HMAC_DRBG_MAX_REQUEST)
\r
64 #define MBEDTLS_HMAC_DRBG_MAX_REQUEST 1024 /**< Maximum number of requested bytes per call */
\r
67 #if !defined(MBEDTLS_HMAC_DRBG_MAX_SEED_INPUT)
\r
68 #define MBEDTLS_HMAC_DRBG_MAX_SEED_INPUT 384 /**< Maximum size of (re)seed buffer */
\r
71 /* \} name SECTION: Module settings */
\r
73 #define MBEDTLS_HMAC_DRBG_PR_OFF 0 /**< No prediction resistance */
\r
74 #define MBEDTLS_HMAC_DRBG_PR_ON 1 /**< Prediction resistance enabled */
\r
81 * HMAC_DRBG context.
\r
83 typedef struct mbedtls_hmac_drbg_context
\r
85 /* Working state: the key K is not stored explicitly,
\r
86 * but is implied by the HMAC context */
\r
87 mbedtls_md_context_t md_ctx; /*!< HMAC context (inc. K) */
\r
88 unsigned char V[MBEDTLS_MD_MAX_SIZE]; /*!< V in the spec */
\r
89 int reseed_counter; /*!< reseed counter */
\r
91 /* Administrative state */
\r
92 size_t entropy_len; /*!< entropy bytes grabbed on each (re)seed */
\r
93 int prediction_resistance; /*!< enable prediction resistance (Automatic
\r
94 reseed before every random generation) */
\r
95 int reseed_interval; /*!< reseed interval */
\r
98 int (*f_entropy)(void *, unsigned char *, size_t); /*!< entropy function */
\r
99 void *p_entropy; /*!< context for the entropy function */
\r
101 #if defined(MBEDTLS_THREADING_C)
\r
102 mbedtls_threading_mutex_t mutex;
\r
104 } mbedtls_hmac_drbg_context;
\r
107 * \brief HMAC_DRBG context initialization
\r
108 * Makes the context ready for mbedtls_hmac_drbg_seed(),
\r
109 * mbedtls_hmac_drbg_seed_buf() or
\r
110 * mbedtls_hmac_drbg_free().
\r
112 * \param ctx HMAC_DRBG context to be initialized
\r
114 void mbedtls_hmac_drbg_init( mbedtls_hmac_drbg_context *ctx );
\r
117 * \brief HMAC_DRBG initial seeding
\r
118 * Seed and setup entropy source for future reseeds.
\r
120 * \param ctx HMAC_DRBG context to be seeded
\r
121 * \param md_info MD algorithm to use for HMAC_DRBG
\r
122 * \param f_entropy Entropy callback (p_entropy, buffer to fill, buffer
\r
124 * \param p_entropy Entropy context
\r
125 * \param custom Personalization data (Device specific identifiers)
\r
127 * \param len Length of personalization data
\r
129 * \note The "security strength" as defined by NIST is set to:
\r
130 * 128 bits if md_alg is SHA-1,
\r
131 * 192 bits if md_alg is SHA-224,
\r
132 * 256 bits if md_alg is SHA-256 or higher.
\r
133 * Note that SHA-256 is just as efficient as SHA-224.
\r
135 * \return 0 if successful, or
\r
136 * MBEDTLS_ERR_MD_BAD_INPUT_DATA, or
\r
137 * MBEDTLS_ERR_MD_ALLOC_FAILED, or
\r
138 * MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED.
\r
140 int mbedtls_hmac_drbg_seed( mbedtls_hmac_drbg_context *ctx,
\r
141 const mbedtls_md_info_t * md_info,
\r
142 int (*f_entropy)(void *, unsigned char *, size_t),
\r
144 const unsigned char *custom,
\r
148 * \brief Initilisation of simpified HMAC_DRBG (never reseeds).
\r
149 * (For use with deterministic ECDSA.)
\r
151 * \param ctx HMAC_DRBG context to be initialised
\r
152 * \param md_info MD algorithm to use for HMAC_DRBG
\r
153 * \param data Concatenation of entropy string and additional data
\r
154 * \param data_len Length of data in bytes
\r
156 * \return 0 if successful, or
\r
157 * MBEDTLS_ERR_MD_BAD_INPUT_DATA, or
\r
158 * MBEDTLS_ERR_MD_ALLOC_FAILED.
\r
160 int mbedtls_hmac_drbg_seed_buf( mbedtls_hmac_drbg_context *ctx,
\r
161 const mbedtls_md_info_t * md_info,
\r
162 const unsigned char *data, size_t data_len );
\r
165 * \brief Enable / disable prediction resistance (Default: Off)
\r
167 * Note: If enabled, entropy is used for ctx->entropy_len before each call!
\r
168 * Only use this if you have ample supply of good entropy!
\r
170 * \param ctx HMAC_DRBG context
\r
171 * \param resistance MBEDTLS_HMAC_DRBG_PR_ON or MBEDTLS_HMAC_DRBG_PR_OFF
\r
173 void mbedtls_hmac_drbg_set_prediction_resistance( mbedtls_hmac_drbg_context *ctx,
\r
177 * \brief Set the amount of entropy grabbed on each reseed
\r
178 * (Default: given by the security strength, which
\r
179 * depends on the hash used, see \c mbedtls_hmac_drbg_init() )
\r
181 * \param ctx HMAC_DRBG context
\r
182 * \param len Amount of entropy to grab, in bytes
\r
184 void mbedtls_hmac_drbg_set_entropy_len( mbedtls_hmac_drbg_context *ctx,
\r
188 * \brief Set the reseed interval
\r
189 * (Default: MBEDTLS_HMAC_DRBG_RESEED_INTERVAL)
\r
191 * \param ctx HMAC_DRBG context
\r
192 * \param interval Reseed interval
\r
194 void mbedtls_hmac_drbg_set_reseed_interval( mbedtls_hmac_drbg_context *ctx,
\r
198 * \brief HMAC_DRBG update state
\r
200 * \param ctx HMAC_DRBG context
\r
201 * \param additional Additional data to update state with, or NULL
\r
202 * \param add_len Length of additional data, or 0
\r
204 * \return \c 0 on success, or an error from the underlying
\r
205 * hash calculation.
\r
207 * \note Additional data is optional, pass NULL and 0 as second
\r
208 * third argument if no additional data is being used.
\r
210 int mbedtls_hmac_drbg_update_ret( mbedtls_hmac_drbg_context *ctx,
\r
211 const unsigned char *additional, size_t add_len );
\r
214 * \brief HMAC_DRBG reseeding (extracts data from entropy source)
\r
216 * \param ctx HMAC_DRBG context
\r
217 * \param additional Additional data to add to state (Can be NULL)
\r
218 * \param len Length of additional data
\r
220 * \return 0 if successful, or
\r
221 * MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED
\r
223 int mbedtls_hmac_drbg_reseed( mbedtls_hmac_drbg_context *ctx,
\r
224 const unsigned char *additional, size_t len );
\r
227 * \brief HMAC_DRBG generate random with additional update input
\r
229 * Note: Automatically reseeds if reseed_counter is reached or PR is enabled.
\r
231 * \param p_rng HMAC_DRBG context
\r
232 * \param output Buffer to fill
\r
233 * \param output_len Length of the buffer
\r
234 * \param additional Additional data to update with (can be NULL)
\r
235 * \param add_len Length of additional data (can be 0)
\r
237 * \return 0 if successful, or
\r
238 * MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED, or
\r
239 * MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG, or
\r
240 * MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG.
\r
242 int mbedtls_hmac_drbg_random_with_add( void *p_rng,
\r
243 unsigned char *output, size_t output_len,
\r
244 const unsigned char *additional,
\r
248 * \brief HMAC_DRBG generate random
\r
250 * Note: Automatically reseeds if reseed_counter is reached or PR is enabled.
\r
252 * \param p_rng HMAC_DRBG context
\r
253 * \param output Buffer to fill
\r
254 * \param out_len Length of the buffer
\r
256 * \return 0 if successful, or
\r
257 * MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED, or
\r
258 * MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG
\r
260 int mbedtls_hmac_drbg_random( void *p_rng, unsigned char *output, size_t out_len );
\r
263 * \brief Free an HMAC_DRBG context
\r
265 * \param ctx HMAC_DRBG context to free.
\r
267 void mbedtls_hmac_drbg_free( mbedtls_hmac_drbg_context *ctx );
\r
269 #if ! defined(MBEDTLS_DEPRECATED_REMOVED)
\r
270 #if defined(MBEDTLS_DEPRECATED_WARNING)
\r
271 #define MBEDTLS_DEPRECATED __attribute__((deprecated))
\r
273 #define MBEDTLS_DEPRECATED
\r
276 * \brief HMAC_DRBG update state
\r
278 * \deprecated Superseded by mbedtls_hmac_drbg_update_ret()
\r
281 * \param ctx HMAC_DRBG context
\r
282 * \param additional Additional data to update state with, or NULL
\r
283 * \param add_len Length of additional data, or 0
\r
285 * \note Additional data is optional, pass NULL and 0 as second
\r
286 * third argument if no additional data is being used.
\r
288 MBEDTLS_DEPRECATED void mbedtls_hmac_drbg_update(
\r
289 mbedtls_hmac_drbg_context *ctx,
\r
290 const unsigned char *additional, size_t add_len );
\r
291 #undef MBEDTLS_DEPRECATED
\r
292 #endif /* !MBEDTLS_DEPRECATED_REMOVED */
\r
294 #if defined(MBEDTLS_FS_IO)
\r
296 * \brief Write a seed file
\r
298 * \param ctx HMAC_DRBG context
\r
299 * \param path Name of the file
\r
301 * \return 0 if successful, 1 on file error, or
\r
302 * MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED
\r
304 int mbedtls_hmac_drbg_write_seed_file( mbedtls_hmac_drbg_context *ctx, const char *path );
\r
307 * \brief Read and update a seed file. Seed is added to this
\r
310 * \param ctx HMAC_DRBG context
\r
311 * \param path Name of the file
\r
313 * \return 0 if successful, 1 on file error,
\r
314 * MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED or
\r
315 * MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG
\r
317 int mbedtls_hmac_drbg_update_seed_file( mbedtls_hmac_drbg_context *ctx, const char *path );
\r
318 #endif /* MBEDTLS_FS_IO */
\r
321 #if defined(MBEDTLS_SELF_TEST)
\r
323 * \brief Checkup routine
\r
325 * \return 0 if successful, or 1 if the test failed
\r
327 int mbedtls_hmac_drbg_self_test( int verbose );
\r
334 #endif /* hmac_drbg.h */
\r