4 * \brief HMAC_DRBG (NIST SP 800-90A)
6 * Copyright (C) 2014, ARM Limited, All Rights Reserved
8 * This file is part of mbed TLS (https://tls.mbed.org)
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #ifndef MBEDTLS_HMAC_DRBG_H
25 #define MBEDTLS_HMAC_DRBG_H
29 #if defined(MBEDTLS_THREADING_C)
30 #include "mbedtls/threading.h"
36 #define MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG -0x0003 /**< Too many random requested in single call. */
37 #define MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG -0x0005 /**< Input too large (Entropy + additional). */
38 #define MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR -0x0007 /**< Read/write error in file. */
39 #define MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED -0x0009 /**< The entropy source failed. */
42 * \name SECTION: Module settings
44 * The configuration options you can set for this module are in this section.
45 * Either change them in config.h or define them on the compiler command line.
49 #if !defined(MBEDTLS_HMAC_DRBG_RESEED_INTERVAL)
50 #define MBEDTLS_HMAC_DRBG_RESEED_INTERVAL 10000 /**< Interval before reseed is performed by default */
53 #if !defined(MBEDTLS_HMAC_DRBG_MAX_INPUT)
54 #define MBEDTLS_HMAC_DRBG_MAX_INPUT 256 /**< Maximum number of additional input bytes */
57 #if !defined(MBEDTLS_HMAC_DRBG_MAX_REQUEST)
58 #define MBEDTLS_HMAC_DRBG_MAX_REQUEST 1024 /**< Maximum number of requested bytes per call */
61 #if !defined(MBEDTLS_HMAC_DRBG_MAX_SEED_INPUT)
62 #define MBEDTLS_HMAC_DRBG_MAX_SEED_INPUT 384 /**< Maximum size of (re)seed buffer */
65 /* \} name SECTION: Module settings */
67 #define MBEDTLS_HMAC_DRBG_PR_OFF 0 /**< No prediction resistance */
68 #define MBEDTLS_HMAC_DRBG_PR_ON 1 /**< Prediction resistance enabled */
79 /* Working state: the key K is not stored explicitely,
80 * but is implied by the HMAC context */
81 mbedtls_md_context_t md_ctx
; /*!< HMAC context (inc. K) */
82 unsigned char V
[MBEDTLS_MD_MAX_SIZE
]; /*!< V in the spec */
83 int reseed_counter
; /*!< reseed counter */
85 /* Administrative state */
86 size_t entropy_len
; /*!< entropy bytes grabbed on each (re)seed */
87 int prediction_resistance
; /*!< enable prediction resistance (Automatic
88 reseed before every random generation) */
89 int reseed_interval
; /*!< reseed interval */
92 int (*f_entropy
)(void *, unsigned char *, size_t); /*!< entropy function */
93 void *p_entropy
; /*!< context for the entropy function */
95 #if defined(MBEDTLS_THREADING_C)
96 mbedtls_threading_mutex_t mutex
;
98 } mbedtls_hmac_drbg_context
;
101 * \brief HMAC_DRBG context initialization
102 * Makes the context ready for mbetls_hmac_drbg_seed(),
103 * mbedtls_hmac_drbg_seed_buf() or
104 * mbedtls_hmac_drbg_free().
106 * \param ctx HMAC_DRBG context to be initialized
108 void mbedtls_hmac_drbg_init( mbedtls_hmac_drbg_context
*ctx
);
111 * \brief HMAC_DRBG initial seeding
112 * Seed and setup entropy source for future reseeds.
114 * \param ctx HMAC_DRBG context to be seeded
115 * \param md_info MD algorithm to use for HMAC_DRBG
116 * \param f_entropy Entropy callback (p_entropy, buffer to fill, buffer
118 * \param p_entropy Entropy context
119 * \param custom Personalization data (Device specific identifiers)
121 * \param len Length of personalization data
123 * \note The "security strength" as defined by NIST is set to:
124 * 128 bits if md_alg is SHA-1,
125 * 192 bits if md_alg is SHA-224,
126 * 256 bits if md_alg is SHA-256 or higher.
127 * Note that SHA-256 is just as efficient as SHA-224.
129 * \return 0 if successful, or
130 * MBEDTLS_ERR_MD_BAD_INPUT_DATA, or
131 * MBEDTLS_ERR_MD_ALLOC_FAILED, or
132 * MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED.
134 int mbedtls_hmac_drbg_seed( mbedtls_hmac_drbg_context
*ctx
,
135 const mbedtls_md_info_t
* md_info
,
136 int (*f_entropy
)(void *, unsigned char *, size_t),
138 const unsigned char *custom
,
142 * \brief Initilisation of simpified HMAC_DRBG (never reseeds).
143 * (For use with deterministic ECDSA.)
145 * \param ctx HMAC_DRBG context to be initialised
146 * \param md_info MD algorithm to use for HMAC_DRBG
147 * \param data Concatenation of entropy string and additional data
148 * \param data_len Length of data in bytes
150 * \return 0 if successful, or
151 * MBEDTLS_ERR_MD_BAD_INPUT_DATA, or
152 * MBEDTLS_ERR_MD_ALLOC_FAILED.
154 int mbedtls_hmac_drbg_seed_buf( mbedtls_hmac_drbg_context
*ctx
,
155 const mbedtls_md_info_t
* md_info
,
156 const unsigned char *data
, size_t data_len
);
159 * \brief Enable / disable prediction resistance (Default: Off)
161 * Note: If enabled, entropy is used for ctx->entropy_len before each call!
162 * Only use this if you have ample supply of good entropy!
164 * \param ctx HMAC_DRBG context
165 * \param resistance MBEDTLS_HMAC_DRBG_PR_ON or MBEDTLS_HMAC_DRBG_PR_OFF
167 void mbedtls_hmac_drbg_set_prediction_resistance( mbedtls_hmac_drbg_context
*ctx
,
171 * \brief Set the amount of entropy grabbed on each reseed
172 * (Default: given by the security strength, which
173 * depends on the hash used, see \c mbedtls_hmac_drbg_init() )
175 * \param ctx HMAC_DRBG context
176 * \param len Amount of entropy to grab, in bytes
178 void mbedtls_hmac_drbg_set_entropy_len( mbedtls_hmac_drbg_context
*ctx
,
182 * \brief Set the reseed interval
183 * (Default: MBEDTLS_HMAC_DRBG_RESEED_INTERVAL)
185 * \param ctx HMAC_DRBG context
186 * \param interval Reseed interval
188 void mbedtls_hmac_drbg_set_reseed_interval( mbedtls_hmac_drbg_context
*ctx
,
192 * \brief HMAC_DRBG update state
194 * \param ctx HMAC_DRBG context
195 * \param additional Additional data to update state with, or NULL
196 * \param add_len Length of additional data, or 0
198 * \note Additional data is optional, pass NULL and 0 as second
199 * third argument if no additional data is being used.
201 void mbedtls_hmac_drbg_update( mbedtls_hmac_drbg_context
*ctx
,
202 const unsigned char *additional
, size_t add_len
);
205 * \brief HMAC_DRBG reseeding (extracts data from entropy source)
207 * \param ctx HMAC_DRBG context
208 * \param additional Additional data to add to state (Can be NULL)
209 * \param len Length of additional data
211 * \return 0 if successful, or
212 * MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED
214 int mbedtls_hmac_drbg_reseed( mbedtls_hmac_drbg_context
*ctx
,
215 const unsigned char *additional
, size_t len
);
218 * \brief HMAC_DRBG generate random with additional update input
220 * Note: Automatically reseeds if reseed_counter is reached or PR is enabled.
222 * \param p_rng HMAC_DRBG context
223 * \param output Buffer to fill
224 * \param output_len Length of the buffer
225 * \param additional Additional data to update with (can be NULL)
226 * \param add_len Length of additional data (can be 0)
228 * \return 0 if successful, or
229 * MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED, or
230 * MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG, or
231 * MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG.
233 int mbedtls_hmac_drbg_random_with_add( void *p_rng
,
234 unsigned char *output
, size_t output_len
,
235 const unsigned char *additional
,
239 * \brief HMAC_DRBG generate random
241 * Note: Automatically reseeds if reseed_counter is reached or PR is enabled.
243 * \param p_rng HMAC_DRBG context
244 * \param output Buffer to fill
245 * \param out_len Length of the buffer
247 * \return 0 if successful, or
248 * MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED, or
249 * MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG
251 int mbedtls_hmac_drbg_random( void *p_rng
, unsigned char *output
, size_t out_len
);
254 * \brief Free an HMAC_DRBG context
256 * \param ctx HMAC_DRBG context to free.
258 void mbedtls_hmac_drbg_free( mbedtls_hmac_drbg_context
*ctx
);
260 #if defined(MBEDTLS_FS_IO)
262 * \brief Write a seed file
264 * \param ctx HMAC_DRBG context
265 * \param path Name of the file
267 * \return 0 if successful, 1 on file error, or
268 * MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED
270 int mbedtls_hmac_drbg_write_seed_file( mbedtls_hmac_drbg_context
*ctx
, const char *path
);
273 * \brief Read and update a seed file. Seed is added to this
276 * \param ctx HMAC_DRBG context
277 * \param path Name of the file
279 * \return 0 if successful, 1 on file error,
280 * MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED or
281 * MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG
283 int mbedtls_hmac_drbg_update_seed_file( mbedtls_hmac_drbg_context
*ctx
, const char *path
);
284 #endif /* MBEDTLS_FS_IO */
287 #if defined(MBEDTLS_SELF_TEST)
289 * \brief Checkup routine
291 * \return 0 if successful, or 1 if the test failed
293 int mbedtls_hmac_drbg_self_test( int verbose
);
300 #endif /* hmac_drbg.h */