Mbed OS Reference
Loading...
Searching...
No Matches
hmac_drbg.h
Go to the documentation of this file.
1/**
2 * \file hmac_drbg.h
3 *
4 * \brief The HMAC_DRBG pseudorandom generator.
5 *
6 * This module implements the HMAC_DRBG pseudorandom generator described
7 * in <em>NIST SP 800-90A: Recommendation for Random Number Generation Using
8 * Deterministic Random Bit Generators</em>.
9 */
10/*
11 * Copyright The Mbed TLS Contributors
12 * SPDX-License-Identifier: Apache-2.0
13 *
14 * Licensed under the Apache License, Version 2.0 (the "License"); you may
15 * not use this file except in compliance with the License.
16 * You may obtain a copy of the License at
17 *
18 * http://www.apache.org/licenses/LICENSE-2.0
19 *
20 * Unless required by applicable law or agreed to in writing, software
21 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
22 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 * See the License for the specific language governing permissions and
24 * limitations under the License.
25 */
26#ifndef MBEDTLS_HMAC_DRBG_H
27#define MBEDTLS_HMAC_DRBG_H
28
29#if !defined(MBEDTLS_CONFIG_FILE)
30#include "mbedtls/config.h"
31#else
32#include MBEDTLS_CONFIG_FILE
33#endif
34
35#include "mbedtls/md.h"
36
37#if defined(MBEDTLS_THREADING_C)
38#include "mbedtls/threading.h"
39#endif
40
41/**
42 * \addtogroup mbedtls
43 * \{
44 * \defgroup mbedtls_hrmac_drbg_module HMAC_DRBG
45 * \{
46 */
47
48/*
49 * Error codes
50 */
51#define MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG -0x0003 /**< Too many random requested in single call. */
52#define MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG -0x0005 /**< Input too large (Entropy + additional). */
53#define MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR -0x0007 /**< Read/write error in file. */
54#define MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED -0x0009 /**< The entropy source failed. */
55
56/**
57 * \name SECTION: Module settings
58 *
59 * The configuration options you can set for this module are in this section.
60 * Either change them in config.h or define them on the compiler command line.
61 * \{
62 */
63
64#if !defined(MBEDTLS_HMAC_DRBG_RESEED_INTERVAL)
65#define MBEDTLS_HMAC_DRBG_RESEED_INTERVAL 10000 /**< Interval before reseed is performed by default */
66#endif
67
68#if !defined(MBEDTLS_HMAC_DRBG_MAX_INPUT)
69#define MBEDTLS_HMAC_DRBG_MAX_INPUT 256 /**< Maximum number of additional input bytes */
70#endif
71
72#if !defined(MBEDTLS_HMAC_DRBG_MAX_REQUEST)
73#define MBEDTLS_HMAC_DRBG_MAX_REQUEST 1024 /**< Maximum number of requested bytes per call */
74#endif
75
76#if !defined(MBEDTLS_HMAC_DRBG_MAX_SEED_INPUT)
77#define MBEDTLS_HMAC_DRBG_MAX_SEED_INPUT 384 /**< Maximum size of (re)seed buffer */
78#endif
79
80/** \} name SECTION: Module settings */
81
82#define MBEDTLS_HMAC_DRBG_PR_OFF 0 /**< No prediction resistance */
83#define MBEDTLS_HMAC_DRBG_PR_ON 1 /**< Prediction resistance enabled */
84
85#ifdef __cplusplus
86extern "C" {
87#endif
88
89/**
90 * HMAC_DRBG context.
91 */
93{
94 /* Working state: the key K is not stored explicitly,
95 * but is implied by the HMAC context */
96 mbedtls_md_context_t md_ctx; /*!< HMAC context (inc. K) */
97 unsigned char V[MBEDTLS_MD_MAX_SIZE]; /*!< V in the spec */
98 int reseed_counter; /*!< reseed counter */
99
100 /* Administrative state */
101 size_t entropy_len; /*!< entropy bytes grabbed on each (re)seed */
102 int prediction_resistance; /*!< enable prediction resistance (Automatic
103 reseed before every random generation) */
104 int reseed_interval; /*!< reseed interval */
105
106 /* Callbacks */
107 int (*f_entropy)(void *, unsigned char *, size_t); /*!< entropy function */
108 void *p_entropy; /*!< context for the entropy function */
109
110#if defined(MBEDTLS_THREADING_C)
111 mbedtls_threading_mutex_t mutex;
112#endif
114
115/**
116 * \brief HMAC_DRBG context initialization.
117 *
118 * This function makes the context ready for mbedtls_hmac_drbg_seed(),
119 * mbedtls_hmac_drbg_seed_buf() or mbedtls_hmac_drbg_free().
120 *
121 * \note The reseed interval is #MBEDTLS_HMAC_DRBG_RESEED_INTERVAL
122 * by default. Override this value by calling
123 * mbedtls_hmac_drbg_set_reseed_interval().
124 *
125 * \param ctx HMAC_DRBG context to be initialized.
126 */
128
129/**
130 * \brief HMAC_DRBG initial seeding.
131 *
132 * Set the initial seed and set up the entropy source for future reseeds.
133 *
134 * A typical choice for the \p f_entropy and \p p_entropy parameters is
135 * to use the entropy module:
136 * - \p f_entropy is mbedtls_entropy_func();
137 * - \p p_entropy is an instance of ::mbedtls_entropy_context initialized
138 * with mbedtls_entropy_init() (which registers the platform's default
139 * entropy sources).
140 *
141 * You can provide a personalization string in addition to the
142 * entropy source, to make this instantiation as unique as possible.
143 *
144 * \note By default, the security strength as defined by NIST is:
145 * - 128 bits if \p md_info is SHA-1;
146 * - 192 bits if \p md_info is SHA-224;
147 * - 256 bits if \p md_info is SHA-256, SHA-384 or SHA-512.
148 * Note that SHA-256 is just as efficient as SHA-224.
149 * The security strength can be reduced if a smaller
150 * entropy length is set with
151 * mbedtls_hmac_drbg_set_entropy_len().
152 *
153 * \note The default entropy length is the security strength
154 * (converted from bits to bytes). You can override
155 * it by calling mbedtls_hmac_drbg_set_entropy_len().
156 *
157 * \note During the initial seeding, this function calls
158 * the entropy source to obtain a nonce
159 * whose length is half the entropy length.
160 *
161 * \param ctx HMAC_DRBG context to be seeded.
162 * \param md_info MD algorithm to use for HMAC_DRBG.
163 * \param f_entropy The entropy callback, taking as arguments the
164 * \p p_entropy context, the buffer to fill, and the
165 * length of the buffer.
166 * \p f_entropy is always called with a length that is
167 * less than or equal to the entropy length.
168 * \param p_entropy The entropy context to pass to \p f_entropy.
169 * \param custom The personalization string.
170 * This can be \c NULL, in which case the personalization
171 * string is empty regardless of the value of \p len.
172 * \param len The length of the personalization string.
173 * This must be at most #MBEDTLS_HMAC_DRBG_MAX_INPUT
174 * and also at most
175 * #MBEDTLS_HMAC_DRBG_MAX_SEED_INPUT - \p entropy_len * 3 / 2
176 * where \p entropy_len is the entropy length
177 * described above.
178 *
179 * \return \c 0 if successful.
180 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA if \p md_info is
181 * invalid.
182 * \return #MBEDTLS_ERR_MD_ALLOC_FAILED if there was not enough
183 * memory to allocate context data.
184 * \return #MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED
185 * if the call to \p f_entropy failed.
186 */
188 const mbedtls_md_info_t * md_info,
189 int (*f_entropy)(void *, unsigned char *, size_t),
190 void *p_entropy,
191 const unsigned char *custom,
192 size_t len );
193
194/**
195 * \brief Initilisation of simpified HMAC_DRBG (never reseeds).
196 *
197 * This function is meant for use in algorithms that need a pseudorandom
198 * input such as deterministic ECDSA.
199 *
200 * \param ctx HMAC_DRBG context to be initialised.
201 * \param md_info MD algorithm to use for HMAC_DRBG.
202 * \param data Concatenation of the initial entropy string and
203 * the additional data.
204 * \param data_len Length of \p data in bytes.
205 *
206 * \return \c 0 if successful. or
207 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA if \p md_info is
208 * invalid.
209 * \return #MBEDTLS_ERR_MD_ALLOC_FAILED if there was not enough
210 * memory to allocate context data.
211 */
213 const mbedtls_md_info_t * md_info,
214 const unsigned char *data, size_t data_len );
215
216/**
217 * \brief This function turns prediction resistance on or off.
218 * The default value is off.
219 *
220 * \note If enabled, entropy is gathered at the beginning of
221 * every call to mbedtls_hmac_drbg_random_with_add()
222 * or mbedtls_hmac_drbg_random().
223 * Only use this if your entropy source has sufficient
224 * throughput.
225 *
226 * \param ctx The HMAC_DRBG context.
227 * \param resistance #MBEDTLS_HMAC_DRBG_PR_ON or #MBEDTLS_HMAC_DRBG_PR_OFF.
228 */
230 int resistance );
231
232/**
233 * \brief This function sets the amount of entropy grabbed on each
234 * seed or reseed.
235 *
236 * See the documentation of mbedtls_hmac_drbg_seed() for the default value.
237 *
238 * \param ctx The HMAC_DRBG context.
239 * \param len The amount of entropy to grab, in bytes.
240 */
242 size_t len );
243
244/**
245 * \brief Set the reseed interval.
246 *
247 * The reseed interval is the number of calls to mbedtls_hmac_drbg_random()
248 * or mbedtls_hmac_drbg_random_with_add() after which the entropy function
249 * is called again.
250 *
251 * The default value is #MBEDTLS_HMAC_DRBG_RESEED_INTERVAL.
252 *
253 * \param ctx The HMAC_DRBG context.
254 * \param interval The reseed interval.
255 */
257 int interval );
258
259/**
260 * \brief This function updates the state of the HMAC_DRBG context.
261 *
262 * \param ctx The HMAC_DRBG context.
263 * \param additional The data to update the state with.
264 * If this is \c NULL, there is no additional data.
265 * \param add_len Length of \p additional in bytes.
266 * Unused if \p additional is \c NULL.
267 *
268 * \return \c 0 on success, or an error from the underlying
269 * hash calculation.
270 */
272 const unsigned char *additional, size_t add_len );
273
274/**
275 * \brief This function reseeds the HMAC_DRBG context, that is
276 * extracts data from the entropy source.
277 *
278 * \param ctx The HMAC_DRBG context.
279 * \param additional Additional data to add to the state.
280 * If this is \c NULL, there is no additional data
281 * and \p len should be \c 0.
282 * \param len The length of the additional data.
283 * This must be at most #MBEDTLS_HMAC_DRBG_MAX_INPUT
284 * and also at most
285 * #MBEDTLS_HMAC_DRBG_MAX_SEED_INPUT - \p entropy_len
286 * where \p entropy_len is the entropy length
287 * (see mbedtls_hmac_drbg_set_entropy_len()).
288 *
289 * \return \c 0 if successful.
290 * \return #MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED
291 * if a call to the entropy function failed.
292 */
294 const unsigned char *additional, size_t len );
295
296/**
297 * \brief This function updates an HMAC_DRBG instance with additional
298 * data and uses it to generate random data.
299 *
300 * This function automatically reseeds if the reseed counter is exceeded
301 * or prediction resistance is enabled.
302 *
303 * \param p_rng The HMAC_DRBG context. This must be a pointer to a
304 * #mbedtls_hmac_drbg_context structure.
305 * \param output The buffer to fill.
306 * \param output_len The length of the buffer in bytes.
307 * This must be at most #MBEDTLS_HMAC_DRBG_MAX_REQUEST.
308 * \param additional Additional data to update with.
309 * If this is \c NULL, there is no additional data
310 * and \p add_len should be \c 0.
311 * \param add_len The length of the additional data.
312 * This must be at most #MBEDTLS_HMAC_DRBG_MAX_INPUT.
313 *
314 * \return \c 0 if successful.
315 * \return #MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED
316 * if a call to the entropy source failed.
317 * \return #MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG if
318 * \p output_len > #MBEDTLS_HMAC_DRBG_MAX_REQUEST.
319 * \return #MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG if
320 * \p add_len > #MBEDTLS_HMAC_DRBG_MAX_INPUT.
321 */
323 unsigned char *output, size_t output_len,
324 const unsigned char *additional,
325 size_t add_len );
326
327/**
328 * \brief This function uses HMAC_DRBG to generate random data.
329 *
330 * This function automatically reseeds if the reseed counter is exceeded
331 * or prediction resistance is enabled.
332 *
333 * \param p_rng The HMAC_DRBG context. This must be a pointer to a
334 * #mbedtls_hmac_drbg_context structure.
335 * \param output The buffer to fill.
336 * \param out_len The length of the buffer in bytes.
337 * This must be at most #MBEDTLS_HMAC_DRBG_MAX_REQUEST.
338 *
339 * \return \c 0 if successful.
340 * \return #MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED
341 * if a call to the entropy source failed.
342 * \return #MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG if
343 * \p out_len > #MBEDTLS_HMAC_DRBG_MAX_REQUEST.
344 */
345int mbedtls_hmac_drbg_random( void *p_rng, unsigned char *output, size_t out_len );
346
347/**
348 * \brief This function resets HMAC_DRBG context to the state immediately
349 * after initial call of mbedtls_hmac_drbg_init().
350 *
351 * \param ctx The HMAC_DRBG context to free.
352 */
354
355#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
356#if defined(MBEDTLS_DEPRECATED_WARNING)
357#define MBEDTLS_DEPRECATED __attribute__((deprecated))
358#else
359#define MBEDTLS_DEPRECATED
360#endif
361/**
362 * \brief This function updates the state of the HMAC_DRBG context.
363 *
364 * \deprecated Superseded by mbedtls_hmac_drbg_update_ret()
365 * in 2.16.0.
366 *
367 * \param ctx The HMAC_DRBG context.
368 * \param additional The data to update the state with.
369 * If this is \c NULL, there is no additional data.
370 * \param add_len Length of \p additional in bytes.
371 * Unused if \p additional is \c NULL.
372 */
373MBEDTLS_DEPRECATED void mbedtls_hmac_drbg_update(
375 const unsigned char *additional, size_t add_len );
376#undef MBEDTLS_DEPRECATED
377#endif /* !MBEDTLS_DEPRECATED_REMOVED */
378
379#if defined(MBEDTLS_FS_IO)
380/**
381 * \brief This function writes a seed file.
382 *
383 * \param ctx The HMAC_DRBG context.
384 * \param path The name of the file.
385 *
386 * \return \c 0 on success.
387 * \return #MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR on file error.
388 * \return #MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED on reseed
389 * failure.
390 */
391int mbedtls_hmac_drbg_write_seed_file( mbedtls_hmac_drbg_context *ctx, const char *path );
392
393/**
394 * \brief This function reads and updates a seed file. The seed
395 * is added to this instance.
396 *
397 * \param ctx The HMAC_DRBG context.
398 * \param path The name of the file.
399 *
400 * \return \c 0 on success.
401 * \return #MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR on file error.
402 * \return #MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED on
403 * reseed failure.
404 * \return #MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG if the existing
405 * seed file is too large.
406 */
407int mbedtls_hmac_drbg_update_seed_file( mbedtls_hmac_drbg_context *ctx, const char *path );
408#endif /* MBEDTLS_FS_IO */
409
410
411#if defined(MBEDTLS_SELF_TEST)
412/**
413 * \brief The HMAC_DRBG Checkup routine.
414 *
415 * \return \c 0 if successful.
416 * \return \c 1 if the test failed.
417 */
418int mbedtls_hmac_drbg_self_test( int verbose );
419#endif
420
421/// \}
422/// \}
423
424#ifdef __cplusplus
425}
426#endif
427
428#endif /* hmac_drbg.h */
Configuration options (set of defines)
void mbedtls_hmac_drbg_set_prediction_resistance(mbedtls_hmac_drbg_context *ctx, int resistance)
This function turns prediction resistance on or off.
int mbedtls_hmac_drbg_seed(mbedtls_hmac_drbg_context *ctx, const mbedtls_md_info_t *md_info, int(*f_entropy)(void *, unsigned char *, size_t), void *p_entropy, const unsigned char *custom, size_t len)
HMAC_DRBG initial seeding.
int mbedtls_hmac_drbg_reseed(mbedtls_hmac_drbg_context *ctx, const unsigned char *additional, size_t len)
This function reseeds the HMAC_DRBG context, that is extracts data from the entropy source.
void mbedtls_hmac_drbg_init(mbedtls_hmac_drbg_context *ctx)
HMAC_DRBG context initialization.
void mbedtls_hmac_drbg_set_reseed_interval(mbedtls_hmac_drbg_context *ctx, int interval)
Set the reseed interval.
void mbedtls_hmac_drbg_update(mbedtls_hmac_drbg_context *ctx, const unsigned char *additional, size_t add_len)
This function updates the state of the HMAC_DRBG context.
void mbedtls_hmac_drbg_free(mbedtls_hmac_drbg_context *ctx)
This function resets HMAC_DRBG context to the state immediately after initial call of mbedtls_hmac_dr...
int mbedtls_hmac_drbg_seed_buf(mbedtls_hmac_drbg_context *ctx, const mbedtls_md_info_t *md_info, const unsigned char *data, size_t data_len)
Initilisation of simpified HMAC_DRBG (never reseeds).
void mbedtls_hmac_drbg_set_entropy_len(mbedtls_hmac_drbg_context *ctx, size_t len)
This function sets the amount of entropy grabbed on each seed or reseed.
int mbedtls_hmac_drbg_random_with_add(void *p_rng, unsigned char *output, size_t output_len, const unsigned char *additional, size_t add_len)
This function updates an HMAC_DRBG instance with additional data and uses it to generate random data.
int mbedtls_hmac_drbg_random(void *p_rng, unsigned char *output, size_t out_len)
This function uses HMAC_DRBG to generate random data.
int mbedtls_hmac_drbg_update_ret(mbedtls_hmac_drbg_context *ctx, const unsigned char *additional, size_t add_len)
This function updates the state of the HMAC_DRBG context.
This file contains the generic message-digest wrapper.
HMAC_DRBG context.
Definition: hmac_drbg.h:93
int(* f_entropy)(void *, unsigned char *, size_t)
Definition: hmac_drbg.h:107
mbedtls_md_context_t md_ctx
Definition: hmac_drbg.h:96
unsigned char V[MBEDTLS_MD_MAX_SIZE]
Definition: hmac_drbg.h:97
The generic message-digest context.
Definition: md.h:98
Message digest information.
Definition: md_internal.h:46
Threading abstraction layer.