blob: d88b50d3f200843f6299e1ef3eb6f08f61c3936c [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é-Gonnardaf786ff2014-01-30 18:44:18 +010054 * TODO: reseed counter.
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +010055 */
56typedef struct
57{
58 md_context_t md_ctx;
59 unsigned char V[POLARSSL_MD_MAX_SIZE];
60 unsigned char K[POLARSSL_MD_MAX_SIZE];
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +010061
62 size_t entropy_len; /*!< entropy bytes grabbed on each (re)seed */
Manuel Pégourié-Gonnardaf786ff2014-01-30 18:44:18 +010063 int prediction_resistance; /*!< enable prediction resistance (Automatic
64 reseed before every random generation) */
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +010065
Manuel Pégourié-Gonnardaf786ff2014-01-30 18:44:18 +010066 int (*f_entropy)(void *, unsigned char *, size_t); /*!< entropy function */
67 void *p_entropy; /*!< context for the entropy function */
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +010068} hmac_drbg_context;
69
70/**
71 * \brief HMAC_DRBG initialisation
72 *
73 * \param ctx HMAC_DRBG context to be initialised
74 * \param md_info MD algorithm to use for HMAC_DRBG
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +010075 * \param f_entropy Entropy callback (p_entropy, buffer to fill, buffer
76 * length)
77 * \param p_entropy Entropy context
78 * \param custom Personalization data (Device specific identifiers)
79 * (Can be NULL)
80 * \param len Length of personalization data
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +010081 *
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +010082 * \note The "security strength" as defined by NIST is set to:
83 * 128 bits if md_alg is SHA-1,
84 * 192 bits if md_alg is SHA-224,
85 * 256 bits if md_alg is SHA-256 or higher.
86 * Note that SHA-256 is just as efficient as SHA-224.
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +010087 *
88 * \return 0 if successful, or
89 * POLARSSL_ERR_MD_BAD_INPUT_DATA, or
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +010090 * POLARSSL_ERR_MD_ALLOC_FAILED, or
91 * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED.
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +010092 */
93int hmac_drbg_init( hmac_drbg_context *ctx,
94 const md_info_t * md_info,
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +010095 int (*f_entropy)(void *, unsigned char *, size_t),
96 void *p_entropy,
97 const unsigned char *custom,
98 size_t len );
99
100/**
Manuel Pégourié-Gonnard4e669c62014-01-30 18:06:08 +0100101 * \brief Initilisation of simpified HMAC_DRBG (never reseeds).
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +0100102 * (For use with deterministic ECDSA.)
103 *
104 * \param ctx HMAC_DRBG context to be initialised
105 * \param md_info MD algorithm to use for HMAC_DRBG
106 * \param data Concatenation of entropy string and additional data
107 * \param data_len Length of data in bytes
108 *
109 * \return 0 if successful, or
110 * POLARSSL_ERR_MD_BAD_INPUT_DATA, or
111 * POLARSSL_ERR_MD_ALLOC_FAILED.
112 */
113int hmac_drbg_init_buf( hmac_drbg_context *ctx,
114 const md_info_t * md_info,
115 const unsigned char *data, size_t data_len );
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100116
117/**
Manuel Pégourié-Gonnardaf786ff2014-01-30 18:44:18 +0100118 * \brief Enable / disable prediction resistance (Default: Off)
119 *
120 * Note: If enabled, entropy is used for ctx->entropy_len before each call!
121 * Only use this if you have ample supply of good entropy!
122 *
123 * \param ctx HMAC_DRBG context
124 * \param resistance HMAC_DRBG_PR_ON or HMAC_DRBG_PR_OFF
125 */
126void hmac_drbg_set_prediction_resistance( hmac_drbg_context *ctx,
127 int resistance );
128
129/**
Manuel Pégourié-Gonnard4e669c62014-01-30 18:06:08 +0100130 * \brief Set the amount of entropy grabbed on each reseed
131 * (Default: HMAC_DRBG_ENTROPY_LEN)
132 *
133 * \param ctx HMAC_DRBG context
134 * \param len Amount of entropy to grab
135 */
136void hmac_drbg_set_entropy_len( hmac_drbg_context *ctx,
137 size_t len );
138
139/**
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100140 * \brief HMAC_DRBG update state
141 *
142 * \param ctx HMAC_DRBG context
143 * \param additional Additional data to update state with, or NULL
144 * \param add_len Length of additional data, or 0
145 *
146 * \note Additional data is optional, pass NULL and 0 as second
147 * third argument if no additional data is being used.
148 */
149void hmac_drbg_update( hmac_drbg_context *ctx,
150 const unsigned char *additional, size_t add_len );
151
152/**
Manuel Pégourié-Gonnard8fc484d2014-01-30 18:28:09 +0100153 * \brief HMAC_DRBG reseeding (extracts data from entropy source)
154 *
155 * \param ctx HMAC_DRBG context
156 * \param additional Additional data to add to state (Can be NULL)
157 * \param len Length of additional data
158 *
159 * \return 0 if successful, or
160 * POLARSSL_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED
161 */
162int hmac_drbg_reseed( hmac_drbg_context *ctx,
163 const unsigned char *additional, size_t len );
164
165/**
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100166 * \brief HMAC_DRBG generate random with additional update input
167 *
168 * Note: Automatically reseeds if reseed_counter is reached.
169 *
170 * \param p_rng HMAC_DRBG context
171 * \param output Buffer to fill
172 * \param output_len Length of the buffer
173 * \param additional Additional data to update with (can be NULL)
174 * \param add_len Length of additional data (can be 0)
175 *
176 * \return 0 if successful, or
177 * TODO: POLARSSL_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED, or
178 * TODO: POLARSSL_ERR_HMAC_DRBG_REQUEST_TOO_BIG
179 */
180int hmac_drbg_random_with_add( void *p_rng,
181 unsigned char *output, size_t output_len,
182 const unsigned char *additional,
183 size_t add_len );
184
185/**
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100186 * \brief HMAC_DRBG generate random
187 *
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100188 * Note: Automatically reseeds if reseed_counter is reached.
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100189 *
190 * \param p_rng HMAC_DRBG context
191 * \param output Buffer to fill
192 * \param output_len Length of the buffer
193 *
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100194 * \return 0 if successful, or
195 * TODO: POLARSSL_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED, or
196 * TODO: POLARSSL_ERR_HMAC_DRBG_REQUEST_TOO_BIG
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100197 */
198int hmac_drbg_random( void *p_rng, unsigned char *output, size_t out_len );
199
200/**
201 * \brief Free an HMAC_DRBG context
202 *
203 * \param ctx HMAC_DRBG context to free.
204 */
205void hmac_drbg_free( hmac_drbg_context *ctx );
206
207
208#if defined(POLARSSL_SELF_TEST)
209/**
210 * \brief Checkup routine
211 *
212 * \return 0 if successful, or 1 if the test failed
213 */
214int hmac_drbg_self_test( int verbose );
215#endif
216
217#ifdef __cplusplus
218}
219#endif
220
221#endif /* hmac_drbg.h */