blob: bebbfe93118ef546e1732c2bc3cbf90e18907268 [file] [log] [blame]
Paul Bakker0e04d0e2011-11-27 14:46:59 +00001/**
2 * \file ctr_drbg.h
3 *
4 * \brief CTR_DRBG based on AES-256 (NIST SP 800-90)
5 *
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakker0e04d0e2011-11-27 14:46:59 +00007 *
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_CTR_DRBG_H
28#define POLARSSL_CTR_DRBG_H
29
30#include <string.h>
31
32#include "aes.h"
33
34#define POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED -0x0034 /**< The entropy source failed. */
35#define POLARSSL_ERR_CTR_DRBG_REQUEST_TOO_BIG -0x0036 /**< Too many random requested in single call. */
36#define POLARSSL_ERR_CTR_DRBG_INPUT_TOO_BIG -0x0038 /**< Input too large (Entropy + additional). */
Paul Bakker69e095c2011-12-10 21:55:01 +000037#define POLARSSL_ERR_CTR_DRBG_FILE_IO_ERROR -0x003A /**< Read/write error in file. */
Paul Bakker0e04d0e2011-11-27 14:46:59 +000038
Paul Bakker2bc7cf12011-11-29 10:50:51 +000039#define CTR_DRBG_BLOCKSIZE 16 /**< Block size used by the cipher */
40#define CTR_DRBG_KEYSIZE 32 /**< Key size used by the cipher */
41#define CTR_DRBG_KEYBITS ( CTR_DRBG_KEYSIZE * 8 )
42#define CTR_DRBG_SEEDLEN ( CTR_DRBG_KEYSIZE + CTR_DRBG_BLOCKSIZE )
43 /**< The seed length (counter + AES key) */
Paul Bakker9bcf16c2013-06-24 19:31:17 +020044
Paul Bakker088c5c52014-04-25 11:11:10 +020045/**
46 * \name SECTION: Module settings
47 *
48 * The configuration options you can set for this module are in this section.
49 * Either change them in config.h or define them on the compiler command line.
50 * \{
51 */
52
53#if !defined(CTR_DRBG_ENTROPY_LEN)
Paul Bakker2ceda572014-02-06 15:55:25 +010054#if defined(POLARSSL_SHA512_C) && !defined(POLARSSL_ENTROPY_FORCE_SHA256)
Paul Bakkerfb08fd22013-08-27 15:06:26 +020055#define CTR_DRBG_ENTROPY_LEN 48 /**< Amount of entropy used per seed by default (48 with SHA-512, 32 with SHA-256) */
56#else
57#define CTR_DRBG_ENTROPY_LEN 32 /**< Amount of entropy used per seed by default (48 with SHA-512, 32 with SHA-256) */
58#endif
Paul Bakker088c5c52014-04-25 11:11:10 +020059#endif
60
61#if !defined(CTR_DRBG_RESEED_INTERVAL)
Paul Bakker0e04d0e2011-11-27 14:46:59 +000062#define CTR_DRBG_RESEED_INTERVAL 10000 /**< Interval before reseed is performed by default */
Paul Bakker088c5c52014-04-25 11:11:10 +020063#endif
64
65#if !defined(CTR_DRBG_MAX_INPUT)
Paul Bakker9bcf16c2013-06-24 19:31:17 +020066#define CTR_DRBG_MAX_INPUT 256 /**< Maximum number of additional input bytes */
Paul Bakker088c5c52014-04-25 11:11:10 +020067#endif
68
69#if !defined(CTR_DRBG_MAX_REQUEST)
Paul Bakker9bcf16c2013-06-24 19:31:17 +020070#define CTR_DRBG_MAX_REQUEST 1024 /**< Maximum number of requested bytes per call */
Paul Bakker088c5c52014-04-25 11:11:10 +020071#endif
72
73#if !defined(CTR_DRBG_MAX_SEED_INPUT)
Paul Bakker9bcf16c2013-06-24 19:31:17 +020074#define CTR_DRBG_MAX_SEED_INPUT 384 /**< Maximum size of (re)seed buffer */
Paul Bakker088c5c52014-04-25 11:11:10 +020075#endif
76
77/* \} name SECTION: Module settings */
Paul Bakker0e04d0e2011-11-27 14:46:59 +000078
79#define CTR_DRBG_PR_OFF 0 /**< No prediction resistance */
80#define CTR_DRBG_PR_ON 1 /**< Prediction resistance enabled */
81
82#ifdef __cplusplus
83extern "C" {
84#endif
85
86/**
87 * \brief CTR_DRBG context structure
88 */
89typedef struct
90{
91 unsigned char counter[16]; /*!< counter (V) */
92 int reseed_counter; /*!< reseed counter */
93 int prediction_resistance; /*!< enable prediction resistance (Automatic
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020094 reseed before every random generation) */
95 size_t entropy_len; /*!< amount of entropy grabbed on each
96 (re)seed */
Paul Bakker0e04d0e2011-11-27 14:46:59 +000097 int reseed_interval; /*!< reseed interval */
98
99 aes_context aes_ctx; /*!< AES context */
100
101 /*
102 * Callbacks (Entropy)
103 */
104 int (*f_entropy)(void *, unsigned char *, size_t);
105
106 void *p_entropy; /*!< context for the entropy function */
107}
108ctr_drbg_context;
109
110/**
111 * \brief CTR_DRBG initialization
Paul Bakker9af723c2014-05-01 13:03:14 +0200112 *
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000113 * Note: Personalization data can be provided in addition to the more generic
114 * entropy source to make this instantiation as unique as possible.
115 *
116 * \param ctx CTR_DRBG context to be initialized
117 * \param f_entropy Entropy callback (p_entropy, buffer to fill, buffer
118 * length)
119 * \param p_entropy Entropy context
120 * \param custom Personalization data (Device specific identifiers)
Paul Bakker2bc7cf12011-11-29 10:50:51 +0000121 * (Can be NULL)
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000122 * \param len Length of personalization data
123 *
124 * \return 0 if successful, or
125 * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED
126 */
127int ctr_drbg_init( ctr_drbg_context *ctx,
128 int (*f_entropy)(void *, unsigned char *, size_t),
129 void *p_entropy,
Paul Bakker1bc9efc2011-12-03 11:29:32 +0000130 const unsigned char *custom,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000131 size_t len );
132
133/**
Paul Bakkerfff03662014-06-18 16:21:25 +0200134 * \brief Clear CTR_CRBG context data
135 *
136 * \param ctx CTR_DRBG context to clear
137 */
138void ctr_drbg_free( ctr_drbg_context *ctx );
139
140/**
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000141 * \brief Enable / disable prediction resistance (Default: Off)
142 *
143 * Note: If enabled, entropy is used for ctx->entropy_len before each call!
144 * Only use this if you have ample supply of good entropy!
145 *
146 * \param ctx CTR_DRBG context
147 * \param resistance CTR_DRBG_PR_ON or CTR_DRBG_PR_OFF
148 */
149void ctr_drbg_set_prediction_resistance( ctr_drbg_context *ctx,
150 int resistance );
151
152/**
153 * \brief Set the amount of entropy grabbed on each (re)seed
154 * (Default: CTR_DRBG_ENTROPY_LEN)
155 *
156 * \param ctx CTR_DRBG context
157 * \param len Amount of entropy to grab
158 */
159void ctr_drbg_set_entropy_len( ctr_drbg_context *ctx,
160 size_t len );
161
162/**
163 * \brief Set the reseed interval
164 * (Default: CTR_DRBG_RESEED_INTERVAL)
165 *
166 * \param ctx CTR_DRBG context
167 * \param interval Reseed interval
168 */
169void ctr_drbg_set_reseed_interval( ctr_drbg_context *ctx,
170 int interval );
171
172/**
173 * \brief CTR_DRBG reseeding (extracts data from entropy source)
Paul Bakker9af723c2014-05-01 13:03:14 +0200174 *
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000175 * \param ctx CTR_DRBG context
Paul Bakker2bc7cf12011-11-29 10:50:51 +0000176 * \param additional Additional data to add to state (Can be NULL)
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000177 * \param len Length of additional data
178 *
179 * \return 0 if successful, or
180 * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED
181 */
182int ctr_drbg_reseed( ctr_drbg_context *ctx,
Paul Bakker1bc9efc2011-12-03 11:29:32 +0000183 const unsigned char *additional, size_t len );
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000184
185/**
186 * \brief CTR_DRBG update state
187 *
188 * \param ctx CTR_DRBG context
Paul Bakker1bc9efc2011-12-03 11:29:32 +0000189 * \param additional Additional data to update state with
190 * \param add_len Length of additional data
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000191 */
Paul Bakker1bc9efc2011-12-03 11:29:32 +0000192void ctr_drbg_update( ctr_drbg_context *ctx,
Paul Bakkerfc754a92011-12-05 13:23:51 +0000193 const unsigned char *additional, size_t add_len );
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000194
195/**
196 * \brief CTR_DRBG generate random with additional update input
197 *
198 * Note: Automatically reseeds if reseed_counter is reached.
199 *
200 * \param p_rng CTR_DRBG context
201 * \param output Buffer to fill
202 * \param output_len Length of the buffer
Paul Bakker2bc7cf12011-11-29 10:50:51 +0000203 * \param additional Additional data to update with (Can be NULL)
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000204 * \param add_len Length of additional data
205 *
206 * \return 0 if successful, or
207 * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED, or
208 * POLARSSL_ERR_CTR_DRBG_REQUEST_TOO_BIG
209 */
210int ctr_drbg_random_with_add( void *p_rng,
211 unsigned char *output, size_t output_len,
Paul Bakker1bc9efc2011-12-03 11:29:32 +0000212 const unsigned char *additional, size_t add_len );
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000213
214/**
215 * \brief CTR_DRBG generate random
216 *
217 * Note: Automatically reseeds if reseed_counter is reached.
218 *
219 * \param p_rng CTR_DRBG context
220 * \param output Buffer to fill
221 * \param output_len Length of the buffer
222 *
223 * \return 0 if successful, or
224 * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED, or
225 * POLARSSL_ERR_CTR_DRBG_REQUEST_TOO_BIG
226 */
227int ctr_drbg_random( void *p_rng,
228 unsigned char *output, size_t output_len );
229
Paul Bakkerfc754a92011-12-05 13:23:51 +0000230#if defined(POLARSSL_FS_IO)
231/**
232 * \brief Write a seed file
233 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200234 * \param ctx CTR_DRBG context
Paul Bakkerfc754a92011-12-05 13:23:51 +0000235 * \param path Name of the file
236 *
Paul Bakker766a5d02014-03-26 11:51:25 +0100237 * \return 0 if successful,
238 * POLARSSL_ERR_CTR_DRBG_FILE_IO_ERROR on file error, or
Paul Bakkerfc754a92011-12-05 13:23:51 +0000239 * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED
240 */
241int ctr_drbg_write_seed_file( ctr_drbg_context *ctx, const char *path );
242
243/**
244 * \brief Read and update a seed file. Seed is added to this
245 * instance
246 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200247 * \param ctx CTR_DRBG context
Paul Bakkerfc754a92011-12-05 13:23:51 +0000248 * \param path Name of the file
249 *
Paul Bakker766a5d02014-03-26 11:51:25 +0100250 * \return 0 if successful,
251 * POLARSSL_ERR_CTR_DRBG_FILE_IO_ERROR on file error,
Paul Bakkerfc754a92011-12-05 13:23:51 +0000252 * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or
253 * POLARSSL_ERR_CTR_DRBG_INPUT_TOO_BIG
254 */
255int ctr_drbg_update_seed_file( ctr_drbg_context *ctx, const char *path );
Paul Bakker9af723c2014-05-01 13:03:14 +0200256#endif /* POLARSSL_FS_IO */
Paul Bakkerfc754a92011-12-05 13:23:51 +0000257
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000258/**
259 * \brief Checkup routine
260 *
261 * \return 0 if successful, or 1 if the test failed
262 */
263int ctr_drbg_self_test( int verbose );
264
Paul Bakker534f82c2013-06-25 16:47:55 +0200265/* Internal functions (do not call directly) */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200266int ctr_drbg_init_entropy_len( ctr_drbg_context *,
267 int (*)(void *, unsigned char *, size_t), void *,
268 const unsigned char *, size_t, size_t );
Paul Bakker534f82c2013-06-25 16:47:55 +0200269
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000270#ifdef __cplusplus
271}
272#endif
273
274#endif /* ctr_drbg.h */