blob: fb2ee0eb17ce60e0198625e8dc10d5e8d8e19dad [file] [log] [blame]
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +01001/**
2 * \file hmac_drbg.h
3 *
4 * \brief HMAC_DRBG (NIST SP 800-90A)
5 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00006 * Copyright (C) 2014, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +01007 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00008 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +01009 *
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +010010 * 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.
14 *
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.
19 *
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.
23 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#ifndef MBEDTLS_HMAC_DRBG_H
25#define MBEDTLS_HMAC_DRBG_H
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +010026
27#include "md.h"
28
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +010029/*
Manuel Pégourié-Gonnard9a6e93e2014-03-11 09:34:02 +010030 * Error codes
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +010031 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#define MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG -0x0003 /**< Too many random requested in single call. */
33#define MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG -0x0005 /**< Input too large (Entropy + additional). */
34#define MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR -0x0007 /**< Read/write error in file. */
35#define MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED -0x0009 /**< The entropy source failed. */
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +010036
Paul Bakker088c5c52014-04-25 11:11:10 +020037/**
38 * \name SECTION: Module settings
39 *
40 * The configuration options you can set for this module are in this section.
41 * Either change them in config.h or define them on the compiler command line.
42 * \{
43 */
44
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if !defined(MBEDTLS_HMAC_DRBG_RESEED_INTERVAL)
46#define MBEDTLS_HMAC_DRBG_RESEED_INTERVAL 10000 /**< Interval before reseed is performed by default */
Paul Bakker088c5c52014-04-25 11:11:10 +020047#endif
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if !defined(MBEDTLS_HMAC_DRBG_MAX_INPUT)
50#define MBEDTLS_HMAC_DRBG_MAX_INPUT 256 /**< Maximum number of additional input bytes */
Paul Bakker088c5c52014-04-25 11:11:10 +020051#endif
52
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if !defined(MBEDTLS_HMAC_DRBG_MAX_REQUEST)
54#define MBEDTLS_HMAC_DRBG_MAX_REQUEST 1024 /**< Maximum number of requested bytes per call */
Paul Bakker088c5c52014-04-25 11:11:10 +020055#endif
56
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#if !defined(MBEDTLS_HMAC_DRBG_MAX_SEED_INPUT)
58#define MBEDTLS_HMAC_DRBG_MAX_SEED_INPUT 384 /**< Maximum size of (re)seed buffer */
Paul Bakker088c5c52014-04-25 11:11:10 +020059#endif
60
61/* \} name SECTION: Module settings */
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +010062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#define MBEDTLS_HMAC_DRBG_PR_OFF 0 /**< No prediction resistance */
64#define MBEDTLS_HMAC_DRBG_PR_ON 1 /**< Prediction resistance enabled */
Manuel Pégourié-Gonnardaf786ff2014-01-30 18:44:18 +010065
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +010066#ifdef __cplusplus
67extern "C" {
68#endif
69
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +010070/**
71 * HMAC_DRBG context.
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +010072 */
73typedef struct
74{
Manuel Pégourié-Gonnardb05db2a2014-02-01 11:38:05 +010075 /* Working state: the key K is not stored explicitely,
76 * but is implied by the HMAC context */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077 mbedtls_md_context_t md_ctx; /*!< HMAC context (inc. K) */
78 unsigned char V[MBEDTLS_MD_MAX_SIZE]; /*!< V in the spec */
Manuel Pégourié-Gonnardb05db2a2014-02-01 11:38:05 +010079 int reseed_counter; /*!< reseed counter */
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +010080
Manuel Pégourié-Gonnard658dbed2014-01-30 19:03:45 +010081 /* Administrative state */
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +010082 size_t entropy_len; /*!< entropy bytes grabbed on each (re)seed */
Manuel Pégourié-Gonnardaf786ff2014-01-30 18:44:18 +010083 int prediction_resistance; /*!< enable prediction resistance (Automatic
84 reseed before every random generation) */
Manuel Pégourié-Gonnard658dbed2014-01-30 19:03:45 +010085 int reseed_interval; /*!< reseed interval */
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +010086
Manuel Pégourié-Gonnard658dbed2014-01-30 19:03:45 +010087 /* Callbacks */
Manuel Pégourié-Gonnardaf786ff2014-01-30 18:44:18 +010088 int (*f_entropy)(void *, unsigned char *, size_t); /*!< entropy function */
89 void *p_entropy; /*!< context for the entropy function */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090} mbedtls_hmac_drbg_context;
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +010091
92/**
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +020093 * \brief HMAC_DRBG context initialization
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +020094 * Makes the context ready for mbetls_hmac_drbg_seed(),
95 * mbedtls_hmac_drbg_seed_buf() or
96 * mbedtls_hmac_drbg_free().
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +010097 *
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +020098 * \param ctx HMAC_DRBG context to be initialized
99 */
100void mbedtls_hmac_drbg_init( mbedtls_hmac_drbg_context *ctx );
101
102/**
103 * \brief HMAC_DRBG initial seeding
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200104 * Seed and setup entropy source for future reseeds.
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200105 *
106 * \param ctx HMAC_DRBG context to be seeded
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100107 * \param md_info MD algorithm to use for HMAC_DRBG
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +0100108 * \param f_entropy Entropy callback (p_entropy, buffer to fill, buffer
109 * length)
110 * \param p_entropy Entropy context
111 * \param custom Personalization data (Device specific identifiers)
112 * (Can be NULL)
113 * \param len Length of personalization data
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100114 *
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +0100115 * \note The "security strength" as defined by NIST is set to:
116 * 128 bits if md_alg is SHA-1,
117 * 192 bits if md_alg is SHA-224,
118 * 256 bits if md_alg is SHA-256 or higher.
119 * Note that SHA-256 is just as efficient as SHA-224.
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100120 *
121 * \return 0 if successful, or
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 * MBEDTLS_ERR_MD_BAD_INPUT_DATA, or
123 * MBEDTLS_ERR_MD_ALLOC_FAILED, or
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200124 * MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED.
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100125 */
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200126int mbedtls_hmac_drbg_seed( mbedtls_hmac_drbg_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 const mbedtls_md_info_t * md_info,
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +0100128 int (*f_entropy)(void *, unsigned char *, size_t),
129 void *p_entropy,
130 const unsigned char *custom,
131 size_t len );
132
133/**
Manuel Pégourié-Gonnard4e669c62014-01-30 18:06:08 +0100134 * \brief Initilisation of simpified HMAC_DRBG (never reseeds).
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +0100135 * (For use with deterministic ECDSA.)
136 *
137 * \param ctx HMAC_DRBG context to be initialised
138 * \param md_info MD algorithm to use for HMAC_DRBG
139 * \param data Concatenation of entropy string and additional data
140 * \param data_len Length of data in bytes
141 *
142 * \return 0 if successful, or
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 * MBEDTLS_ERR_MD_BAD_INPUT_DATA, or
144 * MBEDTLS_ERR_MD_ALLOC_FAILED.
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +0100145 */
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200146int mbedtls_hmac_drbg_seed_buf( mbedtls_hmac_drbg_context *ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 const mbedtls_md_info_t * md_info,
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +0100148 const unsigned char *data, size_t data_len );
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100149
150/**
Manuel Pégourié-Gonnardaf786ff2014-01-30 18:44:18 +0100151 * \brief Enable / disable prediction resistance (Default: Off)
152 *
153 * Note: If enabled, entropy is used for ctx->entropy_len before each call!
154 * Only use this if you have ample supply of good entropy!
155 *
156 * \param ctx HMAC_DRBG context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 * \param resistance MBEDTLS_HMAC_DRBG_PR_ON or MBEDTLS_HMAC_DRBG_PR_OFF
Manuel Pégourié-Gonnardaf786ff2014-01-30 18:44:18 +0100158 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159void mbedtls_hmac_drbg_set_prediction_resistance( mbedtls_hmac_drbg_context *ctx,
Manuel Pégourié-Gonnardaf786ff2014-01-30 18:44:18 +0100160 int resistance );
161
162/**
Manuel Pégourié-Gonnard4e669c62014-01-30 18:06:08 +0100163 * \brief Set the amount of entropy grabbed on each reseed
Manuel Pégourié-Gonnard658dbed2014-01-30 19:03:45 +0100164 * (Default: given by the security strength, which
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 * depends on the hash used, see \c mbedtls_hmac_drbg_init() )
Manuel Pégourié-Gonnard4e669c62014-01-30 18:06:08 +0100166 *
167 * \param ctx HMAC_DRBG context
Manuel Pégourié-Gonnard658dbed2014-01-30 19:03:45 +0100168 * \param len Amount of entropy to grab, in bytes
Manuel Pégourié-Gonnard4e669c62014-01-30 18:06:08 +0100169 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170void mbedtls_hmac_drbg_set_entropy_len( mbedtls_hmac_drbg_context *ctx,
Manuel Pégourié-Gonnard4e669c62014-01-30 18:06:08 +0100171 size_t len );
172
173/**
Manuel Pégourié-Gonnard658dbed2014-01-30 19:03:45 +0100174 * \brief Set the reseed interval
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 * (Default: MBEDTLS_HMAC_DRBG_RESEED_INTERVAL)
Manuel Pégourié-Gonnard658dbed2014-01-30 19:03:45 +0100176 *
177 * \param ctx HMAC_DRBG context
178 * \param interval Reseed interval
179 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180void mbedtls_hmac_drbg_set_reseed_interval( mbedtls_hmac_drbg_context *ctx,
Manuel Pégourié-Gonnard658dbed2014-01-30 19:03:45 +0100181 int interval );
182
183/**
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100184 * \brief HMAC_DRBG update state
185 *
186 * \param ctx HMAC_DRBG context
187 * \param additional Additional data to update state with, or NULL
188 * \param add_len Length of additional data, or 0
189 *
190 * \note Additional data is optional, pass NULL and 0 as second
191 * third argument if no additional data is being used.
192 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193void mbedtls_hmac_drbg_update( mbedtls_hmac_drbg_context *ctx,
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100194 const unsigned char *additional, size_t add_len );
195
196/**
Manuel Pégourié-Gonnard8fc484d2014-01-30 18:28:09 +0100197 * \brief HMAC_DRBG reseeding (extracts data from entropy source)
198 *
199 * \param ctx HMAC_DRBG context
200 * \param additional Additional data to add to state (Can be NULL)
201 * \param len Length of additional data
202 *
203 * \return 0 if successful, or
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 * MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED
Manuel Pégourié-Gonnard8fc484d2014-01-30 18:28:09 +0100205 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206int mbedtls_hmac_drbg_reseed( mbedtls_hmac_drbg_context *ctx,
Manuel Pégourié-Gonnard8fc484d2014-01-30 18:28:09 +0100207 const unsigned char *additional, size_t len );
208
209/**
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100210 * \brief HMAC_DRBG generate random with additional update input
211 *
Manuel Pégourié-Gonnard658dbed2014-01-30 19:03:45 +0100212 * Note: Automatically reseeds if reseed_counter is reached or PR is enabled.
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100213 *
214 * \param p_rng HMAC_DRBG context
215 * \param output Buffer to fill
216 * \param output_len Length of the buffer
217 * \param additional Additional data to update with (can be NULL)
218 * \param add_len Length of additional data (can be 0)
219 *
220 * \return 0 if successful, or
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 * MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED, or
222 * MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG, or
223 * MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG.
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100224 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225int mbedtls_hmac_drbg_random_with_add( void *p_rng,
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100226 unsigned char *output, size_t output_len,
227 const unsigned char *additional,
228 size_t add_len );
229
230/**
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100231 * \brief HMAC_DRBG generate random
232 *
Manuel Pégourié-Gonnard658dbed2014-01-30 19:03:45 +0100233 * Note: Automatically reseeds if reseed_counter is reached or PR is enabled.
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100234 *
235 * \param p_rng HMAC_DRBG context
236 * \param output Buffer to fill
Manuel Pégourié-Gonnardf6a17d02014-01-31 11:51:42 +0100237 * \param out_len Length of the buffer
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100238 *
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100239 * \return 0 if successful, or
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 * MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED, or
241 * MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100242 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243int mbedtls_hmac_drbg_random( void *p_rng, unsigned char *output, size_t out_len );
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100244
245/**
246 * \brief Free an HMAC_DRBG context
247 *
248 * \param ctx HMAC_DRBG context to free.
249 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250void mbedtls_hmac_drbg_free( mbedtls_hmac_drbg_context *ctx );
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard48bc3e82014-01-30 21:11:16 +0100253/**
254 * \brief Write a seed file
255 *
256 * \param ctx HMAC_DRBG context
257 * \param path Name of the file
258 *
259 * \return 0 if successful, 1 on file error, or
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 * MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED
Manuel Pégourié-Gonnard48bc3e82014-01-30 21:11:16 +0100261 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262int mbedtls_hmac_drbg_write_seed_file( mbedtls_hmac_drbg_context *ctx, const char *path );
Manuel Pégourié-Gonnard48bc3e82014-01-30 21:11:16 +0100263
264/**
265 * \brief Read and update a seed file. Seed is added to this
266 * instance
267 *
268 * \param ctx HMAC_DRBG context
269 * \param path Name of the file
270 *
271 * \return 0 if successful, 1 on file error,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272 * MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED or
273 * MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG
Manuel Pégourié-Gonnard48bc3e82014-01-30 21:11:16 +0100274 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275int mbedtls_hmac_drbg_update_seed_file( mbedtls_hmac_drbg_context *ctx, const char *path );
276#endif /* MBEDTLS_FS_IO */
Manuel Pégourié-Gonnard48bc3e82014-01-30 21:11:16 +0100277
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100280/**
281 * \brief Checkup routine
282 *
283 * \return 0 if successful, or 1 if the test failed
284 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285int mbedtls_hmac_drbg_self_test( int verbose );
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100286#endif
287
288#ifdef __cplusplus
289}
290#endif
291
292#endif /* hmac_drbg.h */