blob: 4b8e11bb0ab0104b8d3da7d2d76b908d784bc6be [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/*
33 * ! Same values as ctr_drbg.h !
34 */
35#define POLARSSL_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED -0x0034 /**< The entropy source failed. */
36#define POLARSSL_ERR_HMAC_DRBG_REQUEST_TOO_BIG -0x0036 /**< Too many random requested in single call. */
37#define POLARSSL_ERR_HMAC_DRBG_INPUT_TOO_BIG -0x0038 /**< Input too large (Entropy + additional). */
38#define POLARSSL_ERR_HMAC_DRBG_FILE_IO_ERROR -0x003A /**< Read/write error in file. */
39
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é-Gonnard658dbed2014-01-30 19:03:45 +010059 /* Working state */
60 md_context_t md_ctx; /*!< HMAC context */
61 unsigned char V[POLARSSL_MD_MAX_SIZE]; /*!< V in the spec */
62 unsigned char K[POLARSSL_MD_MAX_SIZE]; /*!< Key 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
194 * TODO: POLARSSL_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED, or
195 * TODO: POLARSSL_ERR_HMAC_DRBG_REQUEST_TOO_BIG
196 */
197int hmac_drbg_random_with_add( void *p_rng,
198 unsigned char *output, size_t output_len,
199 const unsigned char *additional,
200 size_t add_len );
201
202/**
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100203 * \brief HMAC_DRBG generate random
204 *
Manuel Pégourié-Gonnard658dbed2014-01-30 19:03:45 +0100205 * Note: Automatically reseeds if reseed_counter is reached or PR is enabled.
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100206 *
207 * \param p_rng HMAC_DRBG context
208 * \param output Buffer to fill
209 * \param output_len Length of the buffer
210 *
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100211 * \return 0 if successful, or
212 * TODO: POLARSSL_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED, or
213 * TODO: POLARSSL_ERR_HMAC_DRBG_REQUEST_TOO_BIG
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100214 */
215int hmac_drbg_random( void *p_rng, unsigned char *output, size_t out_len );
216
217/**
218 * \brief Free an HMAC_DRBG context
219 *
220 * \param ctx HMAC_DRBG context to free.
221 */
222void hmac_drbg_free( hmac_drbg_context *ctx );
223
Manuel Pégourié-Gonnard48bc3e82014-01-30 21:11:16 +0100224#if defined(POLARSSL_FS_IO)
225/**
226 * \brief Write a seed file
227 *
228 * \param ctx HMAC_DRBG context
229 * \param path Name of the file
230 *
231 * \return 0 if successful, 1 on file error, or
232 * POLARSSL_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED
233 */
234int hmac_drbg_write_seed_file( hmac_drbg_context *ctx, const char *path );
235
236/**
237 * \brief Read and update a seed file. Seed is added to this
238 * instance
239 *
240 * \param ctx HMAC_DRBG context
241 * \param path Name of the file
242 *
243 * \return 0 if successful, 1 on file error,
244 * POLARSSL_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED or
245 * POLARSSL_ERR_HMAC_DRBG_INPUT_TOO_BIG
246 */
247int hmac_drbg_update_seed_file( hmac_drbg_context *ctx, const char *path );
248#endif
249
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100250
251#if defined(POLARSSL_SELF_TEST)
252/**
253 * \brief Checkup routine
254 *
255 * \return 0 if successful, or 1 if the test failed
256 */
257int hmac_drbg_self_test( int verbose );
258#endif
259
260#ifdef __cplusplus
261}
262#endif
263
264#endif /* hmac_drbg.h */