blob: 0fe7b0311b7d8a38d4ea816af8d7874807708fbb [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 */
24#ifndef POLARSSL_HMAC_DRBG_H
25#define POLARSSL_HMAC_DRBG_H
26
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é-Gonnardcf383672014-02-01 10:22:21 +010032#define POLARSSL_ERR_HMAC_DRBG_REQUEST_TOO_BIG -0x0003 /**< Too many random requested in single call. */
33#define POLARSSL_ERR_HMAC_DRBG_INPUT_TOO_BIG -0x0005 /**< Input too large (Entropy + additional). */
34#define POLARSSL_ERR_HMAC_DRBG_FILE_IO_ERROR -0x0007 /**< Read/write error in file. */
Manuel Pégourié-Gonnard9a6e93e2014-03-11 09:34:02 +010035#define POLARSSL_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
45#if !defined(POLARSSL_HMAC_DRBG_RESEED_INTERVAL)
Manuel Pégourié-Gonnardefc8d802014-01-30 19:36:22 +010046#define POLARSSL_HMAC_DRBG_RESEED_INTERVAL 10000 /**< Interval before reseed is performed by default */
Paul Bakker088c5c52014-04-25 11:11:10 +020047#endif
48
49#if !defined(POLARSSL_HMAC_DRBG_MAX_INPUT)
Manuel Pégourié-Gonnardefc8d802014-01-30 19:36:22 +010050#define POLARSSL_HMAC_DRBG_MAX_INPUT 256 /**< Maximum number of additional input bytes */
Paul Bakker088c5c52014-04-25 11:11:10 +020051#endif
52
53#if !defined(POLARSSL_HMAC_DRBG_MAX_REQUEST)
Manuel Pégourié-Gonnardefc8d802014-01-30 19:36:22 +010054#define POLARSSL_HMAC_DRBG_MAX_REQUEST 1024 /**< Maximum number of requested bytes per call */
Paul Bakker088c5c52014-04-25 11:11:10 +020055#endif
56
57#if !defined(POLARSSL_HMAC_DRBG_MAX_SEED_INPUT)
Manuel Pégourié-Gonnardefc8d802014-01-30 19:36:22 +010058#define POLARSSL_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é-Gonnardefc8d802014-01-30 19:36:22 +010063#define POLARSSL_HMAC_DRBG_PR_OFF 0 /**< No prediction resistance */
64#define POLARSSL_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 */
77 md_context_t md_ctx; /*!< HMAC context (inc. K) */
78 unsigned char V[POLARSSL_MD_MAX_SIZE]; /*!< V in the spec */
79 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é-Gonnard490bdf32014-01-27 14:03:10 +010090} hmac_drbg_context;
91
92/**
93 * \brief HMAC_DRBG initialisation
94 *
95 * \param ctx HMAC_DRBG context to be initialised
96 * \param md_info MD algorithm to use for HMAC_DRBG
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +010097 * \param f_entropy Entropy callback (p_entropy, buffer to fill, buffer
98 * length)
99 * \param p_entropy Entropy context
100 * \param custom Personalization data (Device specific identifiers)
101 * (Can be NULL)
102 * \param len Length of personalization data
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100103 *
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +0100104 * \note The "security strength" as defined by NIST is set to:
105 * 128 bits if md_alg is SHA-1,
106 * 192 bits if md_alg is SHA-224,
107 * 256 bits if md_alg is SHA-256 or higher.
108 * Note that SHA-256 is just as efficient as SHA-224.
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100109 *
110 * \return 0 if successful, or
111 * POLARSSL_ERR_MD_BAD_INPUT_DATA, or
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +0100112 * POLARSSL_ERR_MD_ALLOC_FAILED, or
113 * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED.
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100114 */
115int hmac_drbg_init( hmac_drbg_context *ctx,
116 const md_info_t * md_info,
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +0100117 int (*f_entropy)(void *, unsigned char *, size_t),
118 void *p_entropy,
119 const unsigned char *custom,
120 size_t len );
121
122/**
Manuel Pégourié-Gonnard4e669c62014-01-30 18:06:08 +0100123 * \brief Initilisation of simpified HMAC_DRBG (never reseeds).
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +0100124 * (For use with deterministic ECDSA.)
125 *
126 * \param ctx HMAC_DRBG context to be initialised
127 * \param md_info MD algorithm to use for HMAC_DRBG
128 * \param data Concatenation of entropy string and additional data
129 * \param data_len Length of data in bytes
130 *
131 * \return 0 if successful, or
132 * POLARSSL_ERR_MD_BAD_INPUT_DATA, or
133 * POLARSSL_ERR_MD_ALLOC_FAILED.
134 */
135int hmac_drbg_init_buf( hmac_drbg_context *ctx,
136 const md_info_t * md_info,
137 const unsigned char *data, size_t data_len );
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100138
139/**
Manuel Pégourié-Gonnardaf786ff2014-01-30 18:44:18 +0100140 * \brief Enable / disable prediction resistance (Default: Off)
141 *
142 * Note: If enabled, entropy is used for ctx->entropy_len before each call!
143 * Only use this if you have ample supply of good entropy!
144 *
145 * \param ctx HMAC_DRBG context
Manuel Pégourié-Gonnardefc8d802014-01-30 19:36:22 +0100146 * \param resistance POLARSSL_HMAC_DRBG_PR_ON or POLARSSL_HMAC_DRBG_PR_OFF
Manuel Pégourié-Gonnardaf786ff2014-01-30 18:44:18 +0100147 */
148void hmac_drbg_set_prediction_resistance( hmac_drbg_context *ctx,
149 int resistance );
150
151/**
Manuel Pégourié-Gonnard4e669c62014-01-30 18:06:08 +0100152 * \brief Set the amount of entropy grabbed on each reseed
Manuel Pégourié-Gonnard658dbed2014-01-30 19:03:45 +0100153 * (Default: given by the security strength, which
154 * depends on the hash used, see \c hmac_drbg_init() )
Manuel Pégourié-Gonnard4e669c62014-01-30 18:06:08 +0100155 *
156 * \param ctx HMAC_DRBG context
Manuel Pégourié-Gonnard658dbed2014-01-30 19:03:45 +0100157 * \param len Amount of entropy to grab, in bytes
Manuel Pégourié-Gonnard4e669c62014-01-30 18:06:08 +0100158 */
159void hmac_drbg_set_entropy_len( hmac_drbg_context *ctx,
160 size_t len );
161
162/**
Manuel Pégourié-Gonnard658dbed2014-01-30 19:03:45 +0100163 * \brief Set the reseed interval
Manuel Pégourié-Gonnardefc8d802014-01-30 19:36:22 +0100164 * (Default: POLARSSL_HMAC_DRBG_RESEED_INTERVAL)
Manuel Pégourié-Gonnard658dbed2014-01-30 19:03:45 +0100165 *
166 * \param ctx HMAC_DRBG context
167 * \param interval Reseed interval
168 */
169void hmac_drbg_set_reseed_interval( hmac_drbg_context *ctx,
170 int interval );
171
172/**
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100173 * \brief HMAC_DRBG update state
174 *
175 * \param ctx HMAC_DRBG context
176 * \param additional Additional data to update state with, or NULL
177 * \param add_len Length of additional data, or 0
178 *
179 * \note Additional data is optional, pass NULL and 0 as second
180 * third argument if no additional data is being used.
181 */
182void hmac_drbg_update( hmac_drbg_context *ctx,
183 const unsigned char *additional, size_t add_len );
184
185/**
Manuel Pégourié-Gonnard8fc484d2014-01-30 18:28:09 +0100186 * \brief HMAC_DRBG reseeding (extracts data from entropy source)
187 *
188 * \param ctx HMAC_DRBG context
189 * \param additional Additional data to add to state (Can be NULL)
190 * \param len Length of additional data
191 *
192 * \return 0 if successful, or
193 * POLARSSL_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED
194 */
195int hmac_drbg_reseed( hmac_drbg_context *ctx,
196 const unsigned char *additional, size_t len );
197
198/**
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100199 * \brief HMAC_DRBG generate random with additional update input
200 *
Manuel Pégourié-Gonnard658dbed2014-01-30 19:03:45 +0100201 * Note: Automatically reseeds if reseed_counter is reached or PR is enabled.
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100202 *
203 * \param p_rng HMAC_DRBG context
204 * \param output Buffer to fill
205 * \param output_len Length of the buffer
206 * \param additional Additional data to update with (can be NULL)
207 * \param add_len Length of additional data (can be 0)
208 *
209 * \return 0 if successful, or
Manuel Pégourié-Gonnardf6a17d02014-01-31 11:51:42 +0100210 * POLARSSL_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED, or
211 * POLARSSL_ERR_HMAC_DRBG_REQUEST_TOO_BIG, or
212 * POLARSSL_ERR_HMAC_DRBG_INPUT_TOO_BIG.
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100213 */
214int hmac_drbg_random_with_add( void *p_rng,
215 unsigned char *output, size_t output_len,
216 const unsigned char *additional,
217 size_t add_len );
218
219/**
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100220 * \brief HMAC_DRBG generate random
221 *
Manuel Pégourié-Gonnard658dbed2014-01-30 19:03:45 +0100222 * Note: Automatically reseeds if reseed_counter is reached or PR is enabled.
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100223 *
224 * \param p_rng HMAC_DRBG context
225 * \param output Buffer to fill
Manuel Pégourié-Gonnardf6a17d02014-01-31 11:51:42 +0100226 * \param out_len Length of the buffer
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100227 *
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100228 * \return 0 if successful, or
Manuel Pégourié-Gonnardf6a17d02014-01-31 11:51:42 +0100229 * POLARSSL_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED, or
230 * POLARSSL_ERR_HMAC_DRBG_REQUEST_TOO_BIG
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100231 */
232int hmac_drbg_random( void *p_rng, unsigned char *output, size_t out_len );
233
234/**
235 * \brief Free an HMAC_DRBG context
236 *
237 * \param ctx HMAC_DRBG context to free.
238 */
239void hmac_drbg_free( hmac_drbg_context *ctx );
240
Manuel Pégourié-Gonnard48bc3e82014-01-30 21:11:16 +0100241#if defined(POLARSSL_FS_IO)
242/**
243 * \brief Write a seed file
244 *
245 * \param ctx HMAC_DRBG context
246 * \param path Name of the file
247 *
248 * \return 0 if successful, 1 on file error, or
249 * POLARSSL_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED
250 */
251int hmac_drbg_write_seed_file( hmac_drbg_context *ctx, const char *path );
252
253/**
254 * \brief Read and update a seed file. Seed is added to this
255 * instance
256 *
257 * \param ctx HMAC_DRBG context
258 * \param path Name of the file
259 *
260 * \return 0 if successful, 1 on file error,
261 * POLARSSL_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED or
262 * POLARSSL_ERR_HMAC_DRBG_INPUT_TOO_BIG
263 */
264int hmac_drbg_update_seed_file( hmac_drbg_context *ctx, const char *path );
Paul Bakker9af723c2014-05-01 13:03:14 +0200265#endif /* POLARSSL_FS_IO */
Manuel Pégourié-Gonnard48bc3e82014-01-30 21:11:16 +0100266
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100267
268#if defined(POLARSSL_SELF_TEST)
269/**
270 * \brief Checkup routine
271 *
272 * \return 0 if successful, or 1 if the test failed
273 */
274int hmac_drbg_self_test( int verbose );
275#endif
276
277#ifdef __cplusplus
278}
279#endif
280
281#endif /* hmac_drbg.h */