blob: 1f830d8436fb57845426b7d9d4bb903b1c5d55e5 [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
40#define HMAC_DRBG_RESEED_INTERVAL 10000 /**< Interval before reseed is performed by default */
41#define HMAC_DRBG_MAX_INPUT 256 /**< Maximum number of additional input bytes */
42#define HMAC_DRBG_MAX_REQUEST 1024 /**< Maximum number of requested bytes per call */
43#define HMAC_DRBG_MAX_SEED_INPUT 384 /**< Maximum size of (re)seed buffer */
44
Manuel Pégourié-Gonnardaf786ff2014-01-30 18:44:18 +010045#define HMAC_DRBG_PR_OFF 0 /**< No prediction resistance */
46#define HMAC_DRBG_PR_ON 1 /**< Prediction resistance enabled */
47
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +010048#ifdef __cplusplus
49extern "C" {
50#endif
51
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +010052/**
53 * HMAC_DRBG context.
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +010054 */
55typedef struct
56{
Manuel Pégourié-Gonnard658dbed2014-01-30 19:03:45 +010057 /* Working state */
58 md_context_t md_ctx; /*!< HMAC context */
59 unsigned char V[POLARSSL_MD_MAX_SIZE]; /*!< V in the spec */
60 unsigned char K[POLARSSL_MD_MAX_SIZE]; /*!< Key in the spec */
61 int reseed_counter; /*!< reseed counter */
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +010062
Manuel Pégourié-Gonnard658dbed2014-01-30 19:03:45 +010063 /* Administrative state */
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +010064 size_t entropy_len; /*!< entropy bytes grabbed on each (re)seed */
Manuel Pégourié-Gonnardaf786ff2014-01-30 18:44:18 +010065 int prediction_resistance; /*!< enable prediction resistance (Automatic
66 reseed before every random generation) */
Manuel Pégourié-Gonnard658dbed2014-01-30 19:03:45 +010067 int reseed_interval; /*!< reseed interval */
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +010068
Manuel Pégourié-Gonnard658dbed2014-01-30 19:03:45 +010069 /* Callbacks */
Manuel Pégourié-Gonnardaf786ff2014-01-30 18:44:18 +010070 int (*f_entropy)(void *, unsigned char *, size_t); /*!< entropy function */
71 void *p_entropy; /*!< context for the entropy function */
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +010072} hmac_drbg_context;
73
74/**
75 * \brief HMAC_DRBG initialisation
76 *
77 * \param ctx HMAC_DRBG context to be initialised
78 * \param md_info MD algorithm to use for HMAC_DRBG
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +010079 * \param f_entropy Entropy callback (p_entropy, buffer to fill, buffer
80 * length)
81 * \param p_entropy Entropy context
82 * \param custom Personalization data (Device specific identifiers)
83 * (Can be NULL)
84 * \param len Length of personalization data
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +010085 *
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +010086 * \note The "security strength" as defined by NIST is set to:
87 * 128 bits if md_alg is SHA-1,
88 * 192 bits if md_alg is SHA-224,
89 * 256 bits if md_alg is SHA-256 or higher.
90 * Note that SHA-256 is just as efficient as SHA-224.
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +010091 *
92 * \return 0 if successful, or
93 * POLARSSL_ERR_MD_BAD_INPUT_DATA, or
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +010094 * POLARSSL_ERR_MD_ALLOC_FAILED, or
95 * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED.
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +010096 */
97int hmac_drbg_init( hmac_drbg_context *ctx,
98 const md_info_t * md_info,
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +010099 int (*f_entropy)(void *, unsigned char *, size_t),
100 void *p_entropy,
101 const unsigned char *custom,
102 size_t len );
103
104/**
Manuel Pégourié-Gonnard4e669c62014-01-30 18:06:08 +0100105 * \brief Initilisation of simpified HMAC_DRBG (never reseeds).
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +0100106 * (For use with deterministic ECDSA.)
107 *
108 * \param ctx HMAC_DRBG context to be initialised
109 * \param md_info MD algorithm to use for HMAC_DRBG
110 * \param data Concatenation of entropy string and additional data
111 * \param data_len Length of data in bytes
112 *
113 * \return 0 if successful, or
114 * POLARSSL_ERR_MD_BAD_INPUT_DATA, or
115 * POLARSSL_ERR_MD_ALLOC_FAILED.
116 */
117int hmac_drbg_init_buf( hmac_drbg_context *ctx,
118 const md_info_t * md_info,
119 const unsigned char *data, size_t data_len );
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100120
121/**
Manuel Pégourié-Gonnardaf786ff2014-01-30 18:44:18 +0100122 * \brief Enable / disable prediction resistance (Default: Off)
123 *
124 * Note: If enabled, entropy is used for ctx->entropy_len before each call!
125 * Only use this if you have ample supply of good entropy!
126 *
127 * \param ctx HMAC_DRBG context
128 * \param resistance HMAC_DRBG_PR_ON or HMAC_DRBG_PR_OFF
129 */
130void hmac_drbg_set_prediction_resistance( hmac_drbg_context *ctx,
131 int resistance );
132
133/**
Manuel Pégourié-Gonnard4e669c62014-01-30 18:06:08 +0100134 * \brief Set the amount of entropy grabbed on each reseed
Manuel Pégourié-Gonnard658dbed2014-01-30 19:03:45 +0100135 * (Default: given by the security strength, which
136 * depends on the hash used, see \c hmac_drbg_init() )
Manuel Pégourié-Gonnard4e669c62014-01-30 18:06:08 +0100137 *
138 * \param ctx HMAC_DRBG context
Manuel Pégourié-Gonnard658dbed2014-01-30 19:03:45 +0100139 * \param len Amount of entropy to grab, in bytes
Manuel Pégourié-Gonnard4e669c62014-01-30 18:06:08 +0100140 */
141void hmac_drbg_set_entropy_len( hmac_drbg_context *ctx,
142 size_t len );
143
144/**
Manuel Pégourié-Gonnard658dbed2014-01-30 19:03:45 +0100145 * \brief Set the reseed interval
146 * (Default: HMAC_DRBG_RESEED_INTERVAL)
147 *
148 * \param ctx HMAC_DRBG context
149 * \param interval Reseed interval
150 */
151void hmac_drbg_set_reseed_interval( hmac_drbg_context *ctx,
152 int interval );
153
154/**
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100155 * \brief HMAC_DRBG update state
156 *
157 * \param ctx HMAC_DRBG context
158 * \param additional Additional data to update state with, or NULL
159 * \param add_len Length of additional data, or 0
160 *
161 * \note Additional data is optional, pass NULL and 0 as second
162 * third argument if no additional data is being used.
163 */
164void hmac_drbg_update( hmac_drbg_context *ctx,
165 const unsigned char *additional, size_t add_len );
166
167/**
Manuel Pégourié-Gonnard8fc484d2014-01-30 18:28:09 +0100168 * \brief HMAC_DRBG reseeding (extracts data from entropy source)
169 *
170 * \param ctx HMAC_DRBG context
171 * \param additional Additional data to add to state (Can be NULL)
172 * \param len Length of additional data
173 *
174 * \return 0 if successful, or
175 * POLARSSL_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED
176 */
177int hmac_drbg_reseed( hmac_drbg_context *ctx,
178 const unsigned char *additional, size_t len );
179
180/**
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100181 * \brief HMAC_DRBG generate random with additional update input
182 *
Manuel Pégourié-Gonnard658dbed2014-01-30 19:03:45 +0100183 * Note: Automatically reseeds if reseed_counter is reached or PR is enabled.
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100184 *
185 * \param p_rng HMAC_DRBG context
186 * \param output Buffer to fill
187 * \param output_len Length of the buffer
188 * \param additional Additional data to update with (can be NULL)
189 * \param add_len Length of additional data (can be 0)
190 *
191 * \return 0 if successful, or
192 * TODO: POLARSSL_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED, or
193 * TODO: POLARSSL_ERR_HMAC_DRBG_REQUEST_TOO_BIG
194 */
195int hmac_drbg_random_with_add( void *p_rng,
196 unsigned char *output, size_t output_len,
197 const unsigned char *additional,
198 size_t add_len );
199
200/**
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100201 * \brief HMAC_DRBG generate random
202 *
Manuel Pégourié-Gonnard658dbed2014-01-30 19:03:45 +0100203 * Note: Automatically reseeds if reseed_counter is reached or PR is enabled.
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100204 *
205 * \param p_rng HMAC_DRBG context
206 * \param output Buffer to fill
207 * \param output_len Length of the buffer
208 *
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100209 * \return 0 if successful, or
210 * TODO: POLARSSL_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED, or
211 * TODO: POLARSSL_ERR_HMAC_DRBG_REQUEST_TOO_BIG
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100212 */
213int hmac_drbg_random( void *p_rng, unsigned char *output, size_t out_len );
214
215/**
216 * \brief Free an HMAC_DRBG context
217 *
218 * \param ctx HMAC_DRBG context to free.
219 */
220void hmac_drbg_free( hmac_drbg_context *ctx );
221
222
223#if defined(POLARSSL_SELF_TEST)
224/**
225 * \brief Checkup routine
226 *
227 * \return 0 if successful, or 1 if the test failed
228 */
229int hmac_drbg_self_test( int verbose );
230#endif
231
232#ifdef __cplusplus
233}
234#endif
235
236#endif /* hmac_drbg.h */