blob: 1424bd741b2abb946124bfa7a462f7e92d629b5a [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 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00006 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakker0e04d0e2011-11-27 14:46:59 +00007 *
Manuel Pégourié-Gonnard860b5162015-01-28 17:12:07 +00008 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakker0e04d0e2011-11-27 14:46:59 +00009 *
Paul Bakker0e04d0e2011-11-27 14:46:59 +000010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 */
24#ifndef POLARSSL_CTR_DRBG_H
25#define POLARSSL_CTR_DRBG_H
26
27#include <string.h>
28
29#include "aes.h"
30
31#define POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED -0x0034 /**< The entropy source failed. */
32#define POLARSSL_ERR_CTR_DRBG_REQUEST_TOO_BIG -0x0036 /**< Too many random requested in single call. */
33#define POLARSSL_ERR_CTR_DRBG_INPUT_TOO_BIG -0x0038 /**< Input too large (Entropy + additional). */
Paul Bakker69e095c2011-12-10 21:55:01 +000034#define POLARSSL_ERR_CTR_DRBG_FILE_IO_ERROR -0x003A /**< Read/write error in file. */
Paul Bakker0e04d0e2011-11-27 14:46:59 +000035
Paul Bakker2bc7cf12011-11-29 10:50:51 +000036#define CTR_DRBG_BLOCKSIZE 16 /**< Block size used by the cipher */
37#define CTR_DRBG_KEYSIZE 32 /**< Key size used by the cipher */
38#define CTR_DRBG_KEYBITS ( CTR_DRBG_KEYSIZE * 8 )
39#define CTR_DRBG_SEEDLEN ( CTR_DRBG_KEYSIZE + CTR_DRBG_BLOCKSIZE )
40 /**< The seed length (counter + AES key) */
Paul Bakker9bcf16c2013-06-24 19:31:17 +020041
Paul Bakker088c5c52014-04-25 11:11:10 +020042/**
43 * \name SECTION: Module settings
44 *
45 * The configuration options you can set for this module are in this section.
46 * Either change them in config.h or define them on the compiler command line.
47 * \{
48 */
49
50#if !defined(CTR_DRBG_ENTROPY_LEN)
Paul Bakker2ceda572014-02-06 15:55:25 +010051#if defined(POLARSSL_SHA512_C) && !defined(POLARSSL_ENTROPY_FORCE_SHA256)
Paul Bakkerfb08fd22013-08-27 15:06:26 +020052#define CTR_DRBG_ENTROPY_LEN 48 /**< Amount of entropy used per seed by default (48 with SHA-512, 32 with SHA-256) */
53#else
54#define CTR_DRBG_ENTROPY_LEN 32 /**< Amount of entropy used per seed by default (48 with SHA-512, 32 with SHA-256) */
55#endif
Paul Bakker088c5c52014-04-25 11:11:10 +020056#endif
57
58#if !defined(CTR_DRBG_RESEED_INTERVAL)
Paul Bakker0e04d0e2011-11-27 14:46:59 +000059#define CTR_DRBG_RESEED_INTERVAL 10000 /**< Interval before reseed is performed by default */
Paul Bakker088c5c52014-04-25 11:11:10 +020060#endif
61
62#if !defined(CTR_DRBG_MAX_INPUT)
Paul Bakker9bcf16c2013-06-24 19:31:17 +020063#define CTR_DRBG_MAX_INPUT 256 /**< Maximum number of additional input bytes */
Paul Bakker088c5c52014-04-25 11:11:10 +020064#endif
65
66#if !defined(CTR_DRBG_MAX_REQUEST)
Paul Bakker9bcf16c2013-06-24 19:31:17 +020067#define CTR_DRBG_MAX_REQUEST 1024 /**< Maximum number of requested bytes per call */
Paul Bakker088c5c52014-04-25 11:11:10 +020068#endif
69
70#if !defined(CTR_DRBG_MAX_SEED_INPUT)
Paul Bakker9bcf16c2013-06-24 19:31:17 +020071#define CTR_DRBG_MAX_SEED_INPUT 384 /**< Maximum size of (re)seed buffer */
Paul Bakker088c5c52014-04-25 11:11:10 +020072#endif
73
74/* \} name SECTION: Module settings */
Paul Bakker0e04d0e2011-11-27 14:46:59 +000075
76#define CTR_DRBG_PR_OFF 0 /**< No prediction resistance */
77#define CTR_DRBG_PR_ON 1 /**< Prediction resistance enabled */
78
79#ifdef __cplusplus
80extern "C" {
81#endif
82
83/**
84 * \brief CTR_DRBG context structure
85 */
86typedef struct
87{
88 unsigned char counter[16]; /*!< counter (V) */
89 int reseed_counter; /*!< reseed counter */
90 int prediction_resistance; /*!< enable prediction resistance (Automatic
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020091 reseed before every random generation) */
92 size_t entropy_len; /*!< amount of entropy grabbed on each
93 (re)seed */
Paul Bakker0e04d0e2011-11-27 14:46:59 +000094 int reseed_interval; /*!< reseed interval */
95
96 aes_context aes_ctx; /*!< AES context */
97
98 /*
99 * Callbacks (Entropy)
100 */
101 int (*f_entropy)(void *, unsigned char *, size_t);
102
103 void *p_entropy; /*!< context for the entropy function */
104}
105ctr_drbg_context;
106
107/**
108 * \brief CTR_DRBG initialization
Paul Bakker9af723c2014-05-01 13:03:14 +0200109 *
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000110 * Note: Personalization data can be provided in addition to the more generic
111 * entropy source to make this instantiation as unique as possible.
112 *
113 * \param ctx CTR_DRBG context to be initialized
114 * \param f_entropy Entropy callback (p_entropy, buffer to fill, buffer
115 * length)
116 * \param p_entropy Entropy context
117 * \param custom Personalization data (Device specific identifiers)
Paul Bakker2bc7cf12011-11-29 10:50:51 +0000118 * (Can be NULL)
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000119 * \param len Length of personalization data
120 *
121 * \return 0 if successful, or
122 * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED
123 */
124int ctr_drbg_init( ctr_drbg_context *ctx,
125 int (*f_entropy)(void *, unsigned char *, size_t),
126 void *p_entropy,
Paul Bakker1bc9efc2011-12-03 11:29:32 +0000127 const unsigned char *custom,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000128 size_t len );
129
130/**
Paul Bakkerfff03662014-06-18 16:21:25 +0200131 * \brief Clear CTR_CRBG context data
132 *
133 * \param ctx CTR_DRBG context to clear
134 */
135void ctr_drbg_free( ctr_drbg_context *ctx );
136
137/**
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000138 * \brief Enable / disable prediction resistance (Default: Off)
139 *
140 * Note: If enabled, entropy is used for ctx->entropy_len before each call!
141 * Only use this if you have ample supply of good entropy!
142 *
143 * \param ctx CTR_DRBG context
144 * \param resistance CTR_DRBG_PR_ON or CTR_DRBG_PR_OFF
145 */
146void ctr_drbg_set_prediction_resistance( ctr_drbg_context *ctx,
147 int resistance );
148
149/**
150 * \brief Set the amount of entropy grabbed on each (re)seed
151 * (Default: CTR_DRBG_ENTROPY_LEN)
152 *
153 * \param ctx CTR_DRBG context
154 * \param len Amount of entropy to grab
155 */
156void ctr_drbg_set_entropy_len( ctr_drbg_context *ctx,
157 size_t len );
158
159/**
160 * \brief Set the reseed interval
161 * (Default: CTR_DRBG_RESEED_INTERVAL)
162 *
163 * \param ctx CTR_DRBG context
164 * \param interval Reseed interval
165 */
166void ctr_drbg_set_reseed_interval( ctr_drbg_context *ctx,
167 int interval );
168
169/**
170 * \brief CTR_DRBG reseeding (extracts data from entropy source)
Paul Bakker9af723c2014-05-01 13:03:14 +0200171 *
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000172 * \param ctx CTR_DRBG context
Paul Bakker2bc7cf12011-11-29 10:50:51 +0000173 * \param additional Additional data to add to state (Can be NULL)
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000174 * \param len Length of additional data
175 *
176 * \return 0 if successful, or
177 * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED
178 */
179int ctr_drbg_reseed( ctr_drbg_context *ctx,
Paul Bakker1bc9efc2011-12-03 11:29:32 +0000180 const unsigned char *additional, size_t len );
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000181
182/**
183 * \brief CTR_DRBG update state
184 *
185 * \param ctx CTR_DRBG context
Paul Bakker1bc9efc2011-12-03 11:29:32 +0000186 * \param additional Additional data to update state with
187 * \param add_len Length of additional data
Manuel Pégourié-Gonnard5cb4b312014-11-25 17:41:50 +0100188 *
189 * \note If add_len is greater than CTR_DRBG_MAX_SEED_INPUT,
190 * only the first CTR_DRBG_MAX_SEED_INPUT bytes are used,
191 * the remaining ones are silently discarded.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000192 */
Paul Bakker1bc9efc2011-12-03 11:29:32 +0000193void ctr_drbg_update( ctr_drbg_context *ctx,
Paul Bakkerfc754a92011-12-05 13:23:51 +0000194 const unsigned char *additional, size_t add_len );
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000195
196/**
197 * \brief CTR_DRBG generate random with additional update input
198 *
199 * Note: Automatically reseeds if reseed_counter is reached.
200 *
201 * \param p_rng CTR_DRBG context
202 * \param output Buffer to fill
203 * \param output_len Length of the buffer
Paul Bakker2bc7cf12011-11-29 10:50:51 +0000204 * \param additional Additional data to update with (Can be NULL)
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000205 * \param add_len Length of additional data
206 *
207 * \return 0 if successful, or
208 * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED, or
209 * POLARSSL_ERR_CTR_DRBG_REQUEST_TOO_BIG
210 */
211int ctr_drbg_random_with_add( void *p_rng,
212 unsigned char *output, size_t output_len,
Paul Bakker1bc9efc2011-12-03 11:29:32 +0000213 const unsigned char *additional, size_t add_len );
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000214
215/**
216 * \brief CTR_DRBG generate random
217 *
218 * Note: Automatically reseeds if reseed_counter is reached.
219 *
220 * \param p_rng CTR_DRBG context
221 * \param output Buffer to fill
222 * \param output_len Length of the buffer
223 *
224 * \return 0 if successful, or
225 * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED, or
226 * POLARSSL_ERR_CTR_DRBG_REQUEST_TOO_BIG
227 */
228int ctr_drbg_random( void *p_rng,
229 unsigned char *output, size_t output_len );
230
Paul Bakkerfc754a92011-12-05 13:23:51 +0000231#if defined(POLARSSL_FS_IO)
232/**
233 * \brief Write a seed file
234 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200235 * \param ctx CTR_DRBG context
Paul Bakkerfc754a92011-12-05 13:23:51 +0000236 * \param path Name of the file
237 *
Paul Bakker766a5d02014-03-26 11:51:25 +0100238 * \return 0 if successful,
239 * POLARSSL_ERR_CTR_DRBG_FILE_IO_ERROR on file error, or
Paul Bakkerfc754a92011-12-05 13:23:51 +0000240 * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED
241 */
242int ctr_drbg_write_seed_file( ctr_drbg_context *ctx, const char *path );
243
244/**
245 * \brief Read and update a seed file. Seed is added to this
246 * instance
247 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200248 * \param ctx CTR_DRBG context
Paul Bakkerfc754a92011-12-05 13:23:51 +0000249 * \param path Name of the file
250 *
Paul Bakker766a5d02014-03-26 11:51:25 +0100251 * \return 0 if successful,
252 * POLARSSL_ERR_CTR_DRBG_FILE_IO_ERROR on file error,
Paul Bakkerfc754a92011-12-05 13:23:51 +0000253 * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or
254 * POLARSSL_ERR_CTR_DRBG_INPUT_TOO_BIG
255 */
256int ctr_drbg_update_seed_file( ctr_drbg_context *ctx, const char *path );
Paul Bakker9af723c2014-05-01 13:03:14 +0200257#endif /* POLARSSL_FS_IO */
Paul Bakkerfc754a92011-12-05 13:23:51 +0000258
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000259/**
260 * \brief Checkup routine
261 *
262 * \return 0 if successful, or 1 if the test failed
263 */
264int ctr_drbg_self_test( int verbose );
265
Paul Bakker534f82c2013-06-25 16:47:55 +0200266/* Internal functions (do not call directly) */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200267int ctr_drbg_init_entropy_len( ctr_drbg_context *,
268 int (*)(void *, unsigned char *, size_t), void *,
269 const unsigned char *, size_t, size_t );
Paul Bakker534f82c2013-06-25 16:47:55 +0200270
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000271#ifdef __cplusplus
272}
273#endif
274
275#endif /* ctr_drbg.h */