blob: 2d765d51933182b6efdc62bd8b7570cf77f4eb87 [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 *
6 * Copyright (C) 2014, Brainspark B.V.
7 *
8 * This file is part of PolarSSL (http://www.polarssl.org)
9 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
10 *
11 * All rights reserved.
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 */
27#ifndef POLARSSL_HMAC_DRBG_H
28#define POLARSSL_HMAC_DRBG_H
29
30#include "md.h"
31
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +010032/*
Manuel Pégourié-Gonnard9a6e93e2014-03-11 09:34:02 +010033 * Error codes
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +010034 */
Manuel Pégourié-Gonnardcf383672014-02-01 10:22:21 +010035#define POLARSSL_ERR_HMAC_DRBG_REQUEST_TOO_BIG -0x0003 /**< Too many random requested in single call. */
36#define POLARSSL_ERR_HMAC_DRBG_INPUT_TOO_BIG -0x0005 /**< Input too large (Entropy + additional). */
37#define POLARSSL_ERR_HMAC_DRBG_FILE_IO_ERROR -0x0007 /**< Read/write error in file. */
Manuel Pégourié-Gonnard9a6e93e2014-03-11 09:34:02 +010038#define POLARSSL_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED -0x0009 /**< The entropy source failed. */
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +010039
Paul Bakker088c5c52014-04-25 11:11:10 +020040/**
41 * \name SECTION: Module settings
42 *
43 * The configuration options you can set for this module are in this section.
44 * Either change them in config.h or define them on the compiler command line.
45 * \{
46 */
47
48#if !defined(POLARSSL_HMAC_DRBG_RESEED_INTERVAL)
Manuel Pégourié-Gonnardefc8d802014-01-30 19:36:22 +010049#define POLARSSL_HMAC_DRBG_RESEED_INTERVAL 10000 /**< Interval before reseed is performed by default */
Paul Bakker088c5c52014-04-25 11:11:10 +020050#endif
51
52#if !defined(POLARSSL_HMAC_DRBG_MAX_INPUT)
Manuel Pégourié-Gonnardefc8d802014-01-30 19:36:22 +010053#define POLARSSL_HMAC_DRBG_MAX_INPUT 256 /**< Maximum number of additional input bytes */
Paul Bakker088c5c52014-04-25 11:11:10 +020054#endif
55
56#if !defined(POLARSSL_HMAC_DRBG_MAX_REQUEST)
Manuel Pégourié-Gonnardefc8d802014-01-30 19:36:22 +010057#define POLARSSL_HMAC_DRBG_MAX_REQUEST 1024 /**< Maximum number of requested bytes per call */
Paul Bakker088c5c52014-04-25 11:11:10 +020058#endif
59
60#if !defined(POLARSSL_HMAC_DRBG_MAX_SEED_INPUT)
Manuel Pégourié-Gonnardefc8d802014-01-30 19:36:22 +010061#define POLARSSL_HMAC_DRBG_MAX_SEED_INPUT 384 /**< Maximum size of (re)seed buffer */
Paul Bakker088c5c52014-04-25 11:11:10 +020062#endif
63
64/* \} name SECTION: Module settings */
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +010065
Manuel Pégourié-Gonnardefc8d802014-01-30 19:36:22 +010066#define POLARSSL_HMAC_DRBG_PR_OFF 0 /**< No prediction resistance */
67#define POLARSSL_HMAC_DRBG_PR_ON 1 /**< Prediction resistance enabled */
Manuel Pégourié-Gonnardaf786ff2014-01-30 18:44:18 +010068
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +010069#ifdef __cplusplus
70extern "C" {
71#endif
72
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +010073/**
74 * HMAC_DRBG context.
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +010075 */
76typedef struct
77{
Manuel Pégourié-Gonnardb05db2a2014-02-01 11:38:05 +010078 /* Working state: the key K is not stored explicitely,
79 * but is implied by the HMAC context */
80 md_context_t md_ctx; /*!< HMAC context (inc. K) */
81 unsigned char V[POLARSSL_MD_MAX_SIZE]; /*!< V in the spec */
82 int reseed_counter; /*!< reseed counter */
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +010083
Manuel Pégourié-Gonnard658dbed2014-01-30 19:03:45 +010084 /* Administrative state */
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +010085 size_t entropy_len; /*!< entropy bytes grabbed on each (re)seed */
Manuel Pégourié-Gonnardaf786ff2014-01-30 18:44:18 +010086 int prediction_resistance; /*!< enable prediction resistance (Automatic
87 reseed before every random generation) */
Manuel Pégourié-Gonnard658dbed2014-01-30 19:03:45 +010088 int reseed_interval; /*!< reseed interval */
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +010089
Manuel Pégourié-Gonnard658dbed2014-01-30 19:03:45 +010090 /* Callbacks */
Manuel Pégourié-Gonnardaf786ff2014-01-30 18:44:18 +010091 int (*f_entropy)(void *, unsigned char *, size_t); /*!< entropy function */
92 void *p_entropy; /*!< context for the entropy function */
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +010093} hmac_drbg_context;
94
95/**
96 * \brief HMAC_DRBG initialisation
97 *
98 * \param ctx HMAC_DRBG context to be initialised
99 * \param md_info MD algorithm to use for HMAC_DRBG
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +0100100 * \param f_entropy Entropy callback (p_entropy, buffer to fill, buffer
101 * length)
102 * \param p_entropy Entropy context
103 * \param custom Personalization data (Device specific identifiers)
104 * (Can be NULL)
105 * \param len Length of personalization data
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100106 *
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +0100107 * \note The "security strength" as defined by NIST is set to:
108 * 128 bits if md_alg is SHA-1,
109 * 192 bits if md_alg is SHA-224,
110 * 256 bits if md_alg is SHA-256 or higher.
111 * Note that SHA-256 is just as efficient as SHA-224.
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100112 *
113 * \return 0 if successful, or
114 * POLARSSL_ERR_MD_BAD_INPUT_DATA, or
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +0100115 * POLARSSL_ERR_MD_ALLOC_FAILED, or
116 * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED.
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100117 */
118int hmac_drbg_init( hmac_drbg_context *ctx,
119 const md_info_t * md_info,
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +0100120 int (*f_entropy)(void *, unsigned char *, size_t),
121 void *p_entropy,
122 const unsigned char *custom,
123 size_t len );
124
125/**
Manuel Pégourié-Gonnard4e669c62014-01-30 18:06:08 +0100126 * \brief Initilisation of simpified HMAC_DRBG (never reseeds).
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +0100127 * (For use with deterministic ECDSA.)
128 *
129 * \param ctx HMAC_DRBG context to be initialised
130 * \param md_info MD algorithm to use for HMAC_DRBG
131 * \param data Concatenation of entropy string and additional data
132 * \param data_len Length of data in bytes
133 *
134 * \return 0 if successful, or
135 * POLARSSL_ERR_MD_BAD_INPUT_DATA, or
136 * POLARSSL_ERR_MD_ALLOC_FAILED.
137 */
138int hmac_drbg_init_buf( hmac_drbg_context *ctx,
139 const md_info_t * md_info,
140 const unsigned char *data, size_t data_len );
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100141
142/**
Manuel Pégourié-Gonnardaf786ff2014-01-30 18:44:18 +0100143 * \brief Enable / disable prediction resistance (Default: Off)
144 *
145 * Note: If enabled, entropy is used for ctx->entropy_len before each call!
146 * Only use this if you have ample supply of good entropy!
147 *
148 * \param ctx HMAC_DRBG context
Manuel Pégourié-Gonnardefc8d802014-01-30 19:36:22 +0100149 * \param resistance POLARSSL_HMAC_DRBG_PR_ON or POLARSSL_HMAC_DRBG_PR_OFF
Manuel Pégourié-Gonnardaf786ff2014-01-30 18:44:18 +0100150 */
151void hmac_drbg_set_prediction_resistance( hmac_drbg_context *ctx,
152 int resistance );
153
154/**
Manuel Pégourié-Gonnard4e669c62014-01-30 18:06:08 +0100155 * \brief Set the amount of entropy grabbed on each reseed
Manuel Pégourié-Gonnard658dbed2014-01-30 19:03:45 +0100156 * (Default: given by the security strength, which
157 * depends on the hash used, see \c hmac_drbg_init() )
Manuel Pégourié-Gonnard4e669c62014-01-30 18:06:08 +0100158 *
159 * \param ctx HMAC_DRBG context
Manuel Pégourié-Gonnard658dbed2014-01-30 19:03:45 +0100160 * \param len Amount of entropy to grab, in bytes
Manuel Pégourié-Gonnard4e669c62014-01-30 18:06:08 +0100161 */
162void hmac_drbg_set_entropy_len( hmac_drbg_context *ctx,
163 size_t len );
164
165/**
Manuel Pégourié-Gonnard658dbed2014-01-30 19:03:45 +0100166 * \brief Set the reseed interval
Manuel Pégourié-Gonnardefc8d802014-01-30 19:36:22 +0100167 * (Default: POLARSSL_HMAC_DRBG_RESEED_INTERVAL)
Manuel Pégourié-Gonnard658dbed2014-01-30 19:03:45 +0100168 *
169 * \param ctx HMAC_DRBG context
170 * \param interval Reseed interval
171 */
172void hmac_drbg_set_reseed_interval( hmac_drbg_context *ctx,
173 int interval );
174
175/**
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100176 * \brief HMAC_DRBG update state
177 *
178 * \param ctx HMAC_DRBG context
179 * \param additional Additional data to update state with, or NULL
180 * \param add_len Length of additional data, or 0
181 *
182 * \note Additional data is optional, pass NULL and 0 as second
183 * third argument if no additional data is being used.
184 */
185void hmac_drbg_update( hmac_drbg_context *ctx,
186 const unsigned char *additional, size_t add_len );
187
188/**
Manuel Pégourié-Gonnard8fc484d2014-01-30 18:28:09 +0100189 * \brief HMAC_DRBG reseeding (extracts data from entropy source)
190 *
191 * \param ctx HMAC_DRBG context
192 * \param additional Additional data to add to state (Can be NULL)
193 * \param len Length of additional data
194 *
195 * \return 0 if successful, or
196 * POLARSSL_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED
197 */
198int hmac_drbg_reseed( hmac_drbg_context *ctx,
199 const unsigned char *additional, size_t len );
200
201/**
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100202 * \brief HMAC_DRBG generate random with additional update input
203 *
Manuel Pégourié-Gonnard658dbed2014-01-30 19:03:45 +0100204 * Note: Automatically reseeds if reseed_counter is reached or PR is enabled.
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100205 *
206 * \param p_rng HMAC_DRBG context
207 * \param output Buffer to fill
208 * \param output_len Length of the buffer
209 * \param additional Additional data to update with (can be NULL)
210 * \param add_len Length of additional data (can be 0)
211 *
212 * \return 0 if successful, or
Manuel Pégourié-Gonnardf6a17d02014-01-31 11:51:42 +0100213 * POLARSSL_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED, or
214 * POLARSSL_ERR_HMAC_DRBG_REQUEST_TOO_BIG, or
215 * POLARSSL_ERR_HMAC_DRBG_INPUT_TOO_BIG.
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100216 */
217int hmac_drbg_random_with_add( void *p_rng,
218 unsigned char *output, size_t output_len,
219 const unsigned char *additional,
220 size_t add_len );
221
222/**
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100223 * \brief HMAC_DRBG generate random
224 *
Manuel Pégourié-Gonnard658dbed2014-01-30 19:03:45 +0100225 * Note: Automatically reseeds if reseed_counter is reached or PR is enabled.
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100226 *
227 * \param p_rng HMAC_DRBG context
228 * \param output Buffer to fill
Manuel Pégourié-Gonnardf6a17d02014-01-31 11:51:42 +0100229 * \param out_len Length of the buffer
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100230 *
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100231 * \return 0 if successful, or
Manuel Pégourié-Gonnardf6a17d02014-01-31 11:51:42 +0100232 * POLARSSL_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED, or
233 * POLARSSL_ERR_HMAC_DRBG_REQUEST_TOO_BIG
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100234 */
235int hmac_drbg_random( void *p_rng, unsigned char *output, size_t out_len );
236
237/**
238 * \brief Free an HMAC_DRBG context
239 *
240 * \param ctx HMAC_DRBG context to free.
241 */
242void hmac_drbg_free( hmac_drbg_context *ctx );
243
Manuel Pégourié-Gonnard48bc3e82014-01-30 21:11:16 +0100244#if defined(POLARSSL_FS_IO)
245/**
246 * \brief Write a seed file
247 *
248 * \param ctx HMAC_DRBG context
249 * \param path Name of the file
250 *
251 * \return 0 if successful, 1 on file error, or
252 * POLARSSL_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED
253 */
254int hmac_drbg_write_seed_file( hmac_drbg_context *ctx, const char *path );
255
256/**
257 * \brief Read and update a seed file. Seed is added to this
258 * instance
259 *
260 * \param ctx HMAC_DRBG context
261 * \param path Name of the file
262 *
263 * \return 0 if successful, 1 on file error,
264 * POLARSSL_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED or
265 * POLARSSL_ERR_HMAC_DRBG_INPUT_TOO_BIG
266 */
267int hmac_drbg_update_seed_file( hmac_drbg_context *ctx, const char *path );
Paul Bakker9af723c2014-05-01 13:03:14 +0200268#endif /* POLARSSL_FS_IO */
Manuel Pégourié-Gonnard48bc3e82014-01-30 21:11:16 +0100269
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100270
271#if defined(POLARSSL_SELF_TEST)
272/**
273 * \brief Checkup routine
274 *
275 * \return 0 if successful, or 1 if the test failed
276 */
277int hmac_drbg_self_test( int verbose );
278#endif
279
280#ifdef __cplusplus
281}
282#endif
283
284#endif /* hmac_drbg.h */