blob: a9b4cff723c8409b68ec824d2003eea2858e3c92 [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
Manuel Pégourié-Gonnard0520b602014-01-30 19:43:46 +010040#if !defined(POLARSSL_CONFIG_OPTIONS)
Manuel Pégourié-Gonnardefc8d802014-01-30 19:36:22 +010041#define POLARSSL_HMAC_DRBG_RESEED_INTERVAL 10000 /**< Interval before reseed is performed by default */
42#define POLARSSL_HMAC_DRBG_MAX_INPUT 256 /**< Maximum number of additional input bytes */
43#define POLARSSL_HMAC_DRBG_MAX_REQUEST 1024 /**< Maximum number of requested bytes per call */
44#define POLARSSL_HMAC_DRBG_MAX_SEED_INPUT 384 /**< Maximum size of (re)seed buffer */
Manuel Pégourié-Gonnard0520b602014-01-30 19:43:46 +010045#endif /* !POLARSSL_CONFIG_OPTIONS */
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +010046
Manuel Pégourié-Gonnardefc8d802014-01-30 19:36:22 +010047#define POLARSSL_HMAC_DRBG_PR_OFF 0 /**< No prediction resistance */
48#define POLARSSL_HMAC_DRBG_PR_ON 1 /**< Prediction resistance enabled */
Manuel Pégourié-Gonnardaf786ff2014-01-30 18:44:18 +010049
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +010050#ifdef __cplusplus
51extern "C" {
52#endif
53
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +010054/**
55 * HMAC_DRBG context.
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +010056 */
57typedef struct
58{
Manuel Pégourié-Gonnardb05db2a2014-02-01 11:38:05 +010059 /* Working state: the key K is not stored explicitely,
60 * but is implied by the HMAC context */
61 md_context_t md_ctx; /*!< HMAC context (inc. K) */
62 unsigned char V[POLARSSL_MD_MAX_SIZE]; /*!< V in the spec */
63 int reseed_counter; /*!< reseed counter */
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +010064
Manuel Pégourié-Gonnard658dbed2014-01-30 19:03:45 +010065 /* Administrative state */
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +010066 size_t entropy_len; /*!< entropy bytes grabbed on each (re)seed */
Manuel Pégourié-Gonnardaf786ff2014-01-30 18:44:18 +010067 int prediction_resistance; /*!< enable prediction resistance (Automatic
68 reseed before every random generation) */
Manuel Pégourié-Gonnard658dbed2014-01-30 19:03:45 +010069 int reseed_interval; /*!< reseed interval */
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +010070
Manuel Pégourié-Gonnard658dbed2014-01-30 19:03:45 +010071 /* Callbacks */
Manuel Pégourié-Gonnardaf786ff2014-01-30 18:44:18 +010072 int (*f_entropy)(void *, unsigned char *, size_t); /*!< entropy function */
73 void *p_entropy; /*!< context for the entropy function */
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +010074} hmac_drbg_context;
75
76/**
77 * \brief HMAC_DRBG initialisation
78 *
79 * \param ctx HMAC_DRBG context to be initialised
80 * \param md_info MD algorithm to use for HMAC_DRBG
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +010081 * \param f_entropy Entropy callback (p_entropy, buffer to fill, buffer
82 * length)
83 * \param p_entropy Entropy context
84 * \param custom Personalization data (Device specific identifiers)
85 * (Can be NULL)
86 * \param len Length of personalization data
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +010087 *
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +010088 * \note The "security strength" as defined by NIST is set to:
89 * 128 bits if md_alg is SHA-1,
90 * 192 bits if md_alg is SHA-224,
91 * 256 bits if md_alg is SHA-256 or higher.
92 * Note that SHA-256 is just as efficient as SHA-224.
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +010093 *
94 * \return 0 if successful, or
95 * POLARSSL_ERR_MD_BAD_INPUT_DATA, or
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +010096 * POLARSSL_ERR_MD_ALLOC_FAILED, or
97 * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED.
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +010098 */
99int hmac_drbg_init( hmac_drbg_context *ctx,
100 const md_info_t * md_info,
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +0100101 int (*f_entropy)(void *, unsigned char *, size_t),
102 void *p_entropy,
103 const unsigned char *custom,
104 size_t len );
105
106/**
Manuel Pégourié-Gonnard4e669c62014-01-30 18:06:08 +0100107 * \brief Initilisation of simpified HMAC_DRBG (never reseeds).
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +0100108 * (For use with deterministic ECDSA.)
109 *
110 * \param ctx HMAC_DRBG context to be initialised
111 * \param md_info MD algorithm to use for HMAC_DRBG
112 * \param data Concatenation of entropy string and additional data
113 * \param data_len Length of data in bytes
114 *
115 * \return 0 if successful, or
116 * POLARSSL_ERR_MD_BAD_INPUT_DATA, or
117 * POLARSSL_ERR_MD_ALLOC_FAILED.
118 */
119int hmac_drbg_init_buf( hmac_drbg_context *ctx,
120 const md_info_t * md_info,
121 const unsigned char *data, size_t data_len );
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100122
123/**
Manuel Pégourié-Gonnardaf786ff2014-01-30 18:44:18 +0100124 * \brief Enable / disable prediction resistance (Default: Off)
125 *
126 * Note: If enabled, entropy is used for ctx->entropy_len before each call!
127 * Only use this if you have ample supply of good entropy!
128 *
129 * \param ctx HMAC_DRBG context
Manuel Pégourié-Gonnardefc8d802014-01-30 19:36:22 +0100130 * \param resistance POLARSSL_HMAC_DRBG_PR_ON or POLARSSL_HMAC_DRBG_PR_OFF
Manuel Pégourié-Gonnardaf786ff2014-01-30 18:44:18 +0100131 */
132void hmac_drbg_set_prediction_resistance( hmac_drbg_context *ctx,
133 int resistance );
134
135/**
Manuel Pégourié-Gonnard4e669c62014-01-30 18:06:08 +0100136 * \brief Set the amount of entropy grabbed on each reseed
Manuel Pégourié-Gonnard658dbed2014-01-30 19:03:45 +0100137 * (Default: given by the security strength, which
138 * depends on the hash used, see \c hmac_drbg_init() )
Manuel Pégourié-Gonnard4e669c62014-01-30 18:06:08 +0100139 *
140 * \param ctx HMAC_DRBG context
Manuel Pégourié-Gonnard658dbed2014-01-30 19:03:45 +0100141 * \param len Amount of entropy to grab, in bytes
Manuel Pégourié-Gonnard4e669c62014-01-30 18:06:08 +0100142 */
143void hmac_drbg_set_entropy_len( hmac_drbg_context *ctx,
144 size_t len );
145
146/**
Manuel Pégourié-Gonnard658dbed2014-01-30 19:03:45 +0100147 * \brief Set the reseed interval
Manuel Pégourié-Gonnardefc8d802014-01-30 19:36:22 +0100148 * (Default: POLARSSL_HMAC_DRBG_RESEED_INTERVAL)
Manuel Pégourié-Gonnard658dbed2014-01-30 19:03:45 +0100149 *
150 * \param ctx HMAC_DRBG context
151 * \param interval Reseed interval
152 */
153void hmac_drbg_set_reseed_interval( hmac_drbg_context *ctx,
154 int interval );
155
156/**
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100157 * \brief HMAC_DRBG update state
158 *
159 * \param ctx HMAC_DRBG context
160 * \param additional Additional data to update state with, or NULL
161 * \param add_len Length of additional data, or 0
162 *
163 * \note Additional data is optional, pass NULL and 0 as second
164 * third argument if no additional data is being used.
165 */
166void hmac_drbg_update( hmac_drbg_context *ctx,
167 const unsigned char *additional, size_t add_len );
168
169/**
Manuel Pégourié-Gonnard8fc484d2014-01-30 18:28:09 +0100170 * \brief HMAC_DRBG reseeding (extracts data from entropy source)
171 *
172 * \param ctx HMAC_DRBG context
173 * \param additional Additional data to add to state (Can be NULL)
174 * \param len Length of additional data
175 *
176 * \return 0 if successful, or
177 * POLARSSL_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED
178 */
179int hmac_drbg_reseed( hmac_drbg_context *ctx,
180 const unsigned char *additional, size_t len );
181
182/**
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100183 * \brief HMAC_DRBG generate random with additional update input
184 *
Manuel Pégourié-Gonnard658dbed2014-01-30 19:03:45 +0100185 * Note: Automatically reseeds if reseed_counter is reached or PR is enabled.
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100186 *
187 * \param p_rng HMAC_DRBG context
188 * \param output Buffer to fill
189 * \param output_len Length of the buffer
190 * \param additional Additional data to update with (can be NULL)
191 * \param add_len Length of additional data (can be 0)
192 *
193 * \return 0 if successful, or
Manuel Pégourié-Gonnardf6a17d02014-01-31 11:51:42 +0100194 * POLARSSL_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED, or
195 * POLARSSL_ERR_HMAC_DRBG_REQUEST_TOO_BIG, or
196 * POLARSSL_ERR_HMAC_DRBG_INPUT_TOO_BIG.
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100197 */
198int hmac_drbg_random_with_add( void *p_rng,
199 unsigned char *output, size_t output_len,
200 const unsigned char *additional,
201 size_t add_len );
202
203/**
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100204 * \brief HMAC_DRBG generate random
205 *
Manuel Pégourié-Gonnard658dbed2014-01-30 19:03:45 +0100206 * Note: Automatically reseeds if reseed_counter is reached or PR is enabled.
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100207 *
208 * \param p_rng HMAC_DRBG context
209 * \param output Buffer to fill
Manuel Pégourié-Gonnardf6a17d02014-01-31 11:51:42 +0100210 * \param out_len Length of the buffer
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100211 *
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100212 * \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
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100215 */
216int hmac_drbg_random( void *p_rng, unsigned char *output, size_t out_len );
217
218/**
219 * \brief Free an HMAC_DRBG context
220 *
221 * \param ctx HMAC_DRBG context to free.
222 */
223void hmac_drbg_free( hmac_drbg_context *ctx );
224
Manuel Pégourié-Gonnard48bc3e82014-01-30 21:11:16 +0100225#if defined(POLARSSL_FS_IO)
226/**
227 * \brief Write a seed file
228 *
229 * \param ctx HMAC_DRBG context
230 * \param path Name of the file
231 *
232 * \return 0 if successful, 1 on file error, or
233 * POLARSSL_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED
234 */
235int hmac_drbg_write_seed_file( hmac_drbg_context *ctx, const char *path );
236
237/**
238 * \brief Read and update a seed file. Seed is added to this
239 * instance
240 *
241 * \param ctx HMAC_DRBG context
242 * \param path Name of the file
243 *
244 * \return 0 if successful, 1 on file error,
245 * POLARSSL_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED or
246 * POLARSSL_ERR_HMAC_DRBG_INPUT_TOO_BIG
247 */
248int hmac_drbg_update_seed_file( hmac_drbg_context *ctx, const char *path );
249#endif
250
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100251
252#if defined(POLARSSL_SELF_TEST)
253/**
254 * \brief Checkup routine
255 *
256 * \return 0 if successful, or 1 if the test failed
257 */
258int hmac_drbg_self_test( int verbose );
259#endif
260
261#ifdef __cplusplus
262}
263#endif
264
265#endif /* hmac_drbg.h */