blob: 3b2b27c08e6094777e57b2a91dd019a23d108201 [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 Bakker9bcf16c2013-06-24 19:31:17 +02006 * Copyright (C) 2006-2013, 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
45#if !defined(POLARSSL_CONFIG_OPTIONS)
Paul Bakker2ceda572014-02-06 15:55:25 +010046#if defined(POLARSSL_SHA512_C) && !defined(POLARSSL_ENTROPY_FORCE_SHA256)
Paul Bakkerfb08fd22013-08-27 15:06:26 +020047#define CTR_DRBG_ENTROPY_LEN 48 /**< Amount of entropy used per seed by default (48 with SHA-512, 32 with SHA-256) */
48#else
49#define CTR_DRBG_ENTROPY_LEN 32 /**< Amount of entropy used per seed by default (48 with SHA-512, 32 with SHA-256) */
50#endif
Paul Bakker0e04d0e2011-11-27 14:46:59 +000051#define CTR_DRBG_RESEED_INTERVAL 10000 /**< Interval before reseed is performed by default */
Paul Bakker9bcf16c2013-06-24 19:31:17 +020052#define CTR_DRBG_MAX_INPUT 256 /**< Maximum number of additional input bytes */
53#define CTR_DRBG_MAX_REQUEST 1024 /**< Maximum number of requested bytes per call */
54#define CTR_DRBG_MAX_SEED_INPUT 384 /**< Maximum size of (re)seed buffer */
55#endif /* !POLARSSL_CONFIG_OPTIONS */
Paul Bakker0e04d0e2011-11-27 14:46:59 +000056
57#define CTR_DRBG_PR_OFF 0 /**< No prediction resistance */
58#define CTR_DRBG_PR_ON 1 /**< Prediction resistance enabled */
59
60#ifdef __cplusplus
61extern "C" {
62#endif
63
64/**
65 * \brief CTR_DRBG context structure
66 */
67typedef struct
68{
69 unsigned char counter[16]; /*!< counter (V) */
70 int reseed_counter; /*!< reseed counter */
71 int prediction_resistance; /*!< enable prediction resistance (Automatic
72 reseed before every random generation) */
73 size_t entropy_len; /*!< amount of entropy grabbed on each (re)seed */
74 int reseed_interval; /*!< reseed interval */
75
76 aes_context aes_ctx; /*!< AES context */
77
78 /*
79 * Callbacks (Entropy)
80 */
81 int (*f_entropy)(void *, unsigned char *, size_t);
82
83 void *p_entropy; /*!< context for the entropy function */
84}
85ctr_drbg_context;
86
87/**
88 * \brief CTR_DRBG initialization
89 *
90 * Note: Personalization data can be provided in addition to the more generic
91 * entropy source to make this instantiation as unique as possible.
92 *
93 * \param ctx CTR_DRBG context to be initialized
94 * \param f_entropy Entropy callback (p_entropy, buffer to fill, buffer
95 * length)
96 * \param p_entropy Entropy context
97 * \param custom Personalization data (Device specific identifiers)
Paul Bakker2bc7cf12011-11-29 10:50:51 +000098 * (Can be NULL)
Paul Bakker0e04d0e2011-11-27 14:46:59 +000099 * \param len Length of personalization data
100 *
101 * \return 0 if successful, or
102 * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED
103 */
104int ctr_drbg_init( ctr_drbg_context *ctx,
105 int (*f_entropy)(void *, unsigned char *, size_t),
106 void *p_entropy,
Paul Bakker1bc9efc2011-12-03 11:29:32 +0000107 const unsigned char *custom,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000108 size_t len );
109
110/**
111 * \brief Enable / disable prediction resistance (Default: Off)
112 *
113 * Note: If enabled, entropy is used for ctx->entropy_len before each call!
114 * Only use this if you have ample supply of good entropy!
115 *
116 * \param ctx CTR_DRBG context
117 * \param resistance CTR_DRBG_PR_ON or CTR_DRBG_PR_OFF
118 */
119void ctr_drbg_set_prediction_resistance( ctr_drbg_context *ctx,
120 int resistance );
121
122/**
123 * \brief Set the amount of entropy grabbed on each (re)seed
124 * (Default: CTR_DRBG_ENTROPY_LEN)
125 *
126 * \param ctx CTR_DRBG context
127 * \param len Amount of entropy to grab
128 */
129void ctr_drbg_set_entropy_len( ctr_drbg_context *ctx,
130 size_t len );
131
132/**
133 * \brief Set the reseed interval
134 * (Default: CTR_DRBG_RESEED_INTERVAL)
135 *
136 * \param ctx CTR_DRBG context
137 * \param interval Reseed interval
138 */
139void ctr_drbg_set_reseed_interval( ctr_drbg_context *ctx,
140 int interval );
141
142/**
143 * \brief CTR_DRBG reseeding (extracts data from entropy source)
144 *
145 * \param ctx CTR_DRBG context
Paul Bakker2bc7cf12011-11-29 10:50:51 +0000146 * \param additional Additional data to add to state (Can be NULL)
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000147 * \param len Length of additional data
148 *
149 * \return 0 if successful, or
150 * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED
151 */
152int ctr_drbg_reseed( ctr_drbg_context *ctx,
Paul Bakker1bc9efc2011-12-03 11:29:32 +0000153 const unsigned char *additional, size_t len );
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000154
155/**
156 * \brief CTR_DRBG update state
157 *
158 * \param ctx CTR_DRBG context
Paul Bakker1bc9efc2011-12-03 11:29:32 +0000159 * \param additional Additional data to update state with
160 * \param add_len Length of additional data
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000161 */
Paul Bakker1bc9efc2011-12-03 11:29:32 +0000162void ctr_drbg_update( ctr_drbg_context *ctx,
Paul Bakkerfc754a92011-12-05 13:23:51 +0000163 const unsigned char *additional, size_t add_len );
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000164
165/**
166 * \brief CTR_DRBG generate random with additional update input
167 *
168 * Note: Automatically reseeds if reseed_counter is reached.
169 *
170 * \param p_rng CTR_DRBG context
171 * \param output Buffer to fill
172 * \param output_len Length of the buffer
Paul Bakker2bc7cf12011-11-29 10:50:51 +0000173 * \param additional Additional data to update with (Can be NULL)
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000174 * \param add_len Length of additional data
175 *
176 * \return 0 if successful, or
177 * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED, or
178 * POLARSSL_ERR_CTR_DRBG_REQUEST_TOO_BIG
179 */
180int ctr_drbg_random_with_add( void *p_rng,
181 unsigned char *output, size_t output_len,
Paul Bakker1bc9efc2011-12-03 11:29:32 +0000182 const unsigned char *additional, size_t add_len );
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000183
184/**
185 * \brief CTR_DRBG generate random
186 *
187 * Note: Automatically reseeds if reseed_counter is reached.
188 *
189 * \param p_rng CTR_DRBG context
190 * \param output Buffer to fill
191 * \param output_len Length of the buffer
192 *
193 * \return 0 if successful, or
194 * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED, or
195 * POLARSSL_ERR_CTR_DRBG_REQUEST_TOO_BIG
196 */
197int ctr_drbg_random( void *p_rng,
198 unsigned char *output, size_t output_len );
199
Paul Bakkerfc754a92011-12-05 13:23:51 +0000200#if defined(POLARSSL_FS_IO)
201/**
202 * \brief Write a seed file
203 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200204 * \param ctx CTR_DRBG context
Paul Bakkerfc754a92011-12-05 13:23:51 +0000205 * \param path Name of the file
206 *
Paul Bakker766a5d02014-03-26 11:51:25 +0100207 * \return 0 if successful,
208 * POLARSSL_ERR_CTR_DRBG_FILE_IO_ERROR on file error, or
Paul Bakkerfc754a92011-12-05 13:23:51 +0000209 * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED
210 */
211int ctr_drbg_write_seed_file( ctr_drbg_context *ctx, const char *path );
212
213/**
214 * \brief Read and update a seed file. Seed is added to this
215 * instance
216 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200217 * \param ctx CTR_DRBG context
Paul Bakkerfc754a92011-12-05 13:23:51 +0000218 * \param path Name of the file
219 *
Paul Bakker766a5d02014-03-26 11:51:25 +0100220 * \return 0 if successful,
221 * POLARSSL_ERR_CTR_DRBG_FILE_IO_ERROR on file error,
Paul Bakkerfc754a92011-12-05 13:23:51 +0000222 * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or
223 * POLARSSL_ERR_CTR_DRBG_INPUT_TOO_BIG
224 */
225int ctr_drbg_update_seed_file( ctr_drbg_context *ctx, const char *path );
226#endif
227
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000228/**
229 * \brief Checkup routine
230 *
231 * \return 0 if successful, or 1 if the test failed
232 */
233int ctr_drbg_self_test( int verbose );
234
Paul Bakker534f82c2013-06-25 16:47:55 +0200235/* Internal functions (do not call directly) */
236int ctr_drbg_init_entropy_len( ctr_drbg_context *, int (*)(void *, unsigned char *, size_t), void *, const unsigned char *, size_t, size_t );
237
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000238#ifdef __cplusplus
239}
240#endif
241
242#endif /* ctr_drbg.h */