blob: 371e470772de939952318475270ac74a5be68511 [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 *
6 * Copyright (C) 2006-2010, 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_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). */
37
Paul Bakker2bc7cf12011-11-29 10:50:51 +000038#define CTR_DRBG_BLOCKSIZE 16 /**< Block size used by the cipher */
39#define CTR_DRBG_KEYSIZE 32 /**< Key size used by the cipher */
40#define CTR_DRBG_KEYBITS ( CTR_DRBG_KEYSIZE * 8 )
41#define CTR_DRBG_SEEDLEN ( CTR_DRBG_KEYSIZE + CTR_DRBG_BLOCKSIZE )
42 /**< The seed length (counter + AES key) */
Paul Bakker0e04d0e2011-11-27 14:46:59 +000043#define CTR_DRBG_ENTROPY_LEN 32 /**< Amount of entropy used per seed by default */
44#define CTR_DRBG_RESEED_INTERVAL 10000 /**< Interval before reseed is performed by default */
45#define CTR_DRBG_MAX_INPUT 256 /**< Maximum number of additional input bytes */
Paul Bakker2bc7cf12011-11-29 10:50:51 +000046#define CTR_DRBG_MAX_REQUEST 1024 /**< Maximum number of requested bytes per call */
Paul Bakker0e04d0e2011-11-27 14:46:59 +000047#define CTR_DRBG_MAX_SEED_INPUT 384 /**< Maximum size of (re)seed buffer */
48
49#define CTR_DRBG_PR_OFF 0 /**< No prediction resistance */
50#define CTR_DRBG_PR_ON 1 /**< Prediction resistance enabled */
51
52#ifdef __cplusplus
53extern "C" {
54#endif
55
56/**
57 * \brief CTR_DRBG context structure
58 */
59typedef struct
60{
61 unsigned char counter[16]; /*!< counter (V) */
62 int reseed_counter; /*!< reseed counter */
63 int prediction_resistance; /*!< enable prediction resistance (Automatic
64 reseed before every random generation) */
65 size_t entropy_len; /*!< amount of entropy grabbed on each (re)seed */
66 int reseed_interval; /*!< reseed interval */
67
68 aes_context aes_ctx; /*!< AES context */
69
70 /*
71 * Callbacks (Entropy)
72 */
73 int (*f_entropy)(void *, unsigned char *, size_t);
74
75 void *p_entropy; /*!< context for the entropy function */
76}
77ctr_drbg_context;
78
79/**
80 * \brief CTR_DRBG initialization
81 *
82 * Note: Personalization data can be provided in addition to the more generic
83 * entropy source to make this instantiation as unique as possible.
84 *
85 * \param ctx CTR_DRBG context to be initialized
86 * \param f_entropy Entropy callback (p_entropy, buffer to fill, buffer
87 * length)
88 * \param p_entropy Entropy context
89 * \param custom Personalization data (Device specific identifiers)
Paul Bakker2bc7cf12011-11-29 10:50:51 +000090 * (Can be NULL)
Paul Bakker0e04d0e2011-11-27 14:46:59 +000091 * \param len Length of personalization data
92 *
93 * \return 0 if successful, or
94 * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED
95 */
96int ctr_drbg_init( ctr_drbg_context *ctx,
97 int (*f_entropy)(void *, unsigned char *, size_t),
98 void *p_entropy,
99 unsigned char *custom,
100 size_t len );
101
102/**
103 * \brief Enable / disable prediction resistance (Default: Off)
104 *
105 * Note: If enabled, entropy is used for ctx->entropy_len before each call!
106 * Only use this if you have ample supply of good entropy!
107 *
108 * \param ctx CTR_DRBG context
109 * \param resistance CTR_DRBG_PR_ON or CTR_DRBG_PR_OFF
110 */
111void ctr_drbg_set_prediction_resistance( ctr_drbg_context *ctx,
112 int resistance );
113
114/**
115 * \brief Set the amount of entropy grabbed on each (re)seed
116 * (Default: CTR_DRBG_ENTROPY_LEN)
117 *
118 * \param ctx CTR_DRBG context
119 * \param len Amount of entropy to grab
120 */
121void ctr_drbg_set_entropy_len( ctr_drbg_context *ctx,
122 size_t len );
123
124/**
125 * \brief Set the reseed interval
126 * (Default: CTR_DRBG_RESEED_INTERVAL)
127 *
128 * \param ctx CTR_DRBG context
129 * \param interval Reseed interval
130 */
131void ctr_drbg_set_reseed_interval( ctr_drbg_context *ctx,
132 int interval );
133
134/**
135 * \brief CTR_DRBG reseeding (extracts data from entropy source)
136 *
137 * \param ctx CTR_DRBG context
Paul Bakker2bc7cf12011-11-29 10:50:51 +0000138 * \param additional Additional data to add to state (Can be NULL)
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000139 * \param len Length of additional data
140 *
141 * \return 0 if successful, or
142 * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED
143 */
144int ctr_drbg_reseed( ctr_drbg_context *ctx,
145 unsigned char *additional, size_t len );
146
147/**
148 * \brief CTR_DRBG update state
149 *
150 * \param ctx CTR_DRBG context
151 * \param data Data to update with
152 *
153 * \return 0 if successful
154 */
155int ctr_drbg_update( ctr_drbg_context *ctx,
156 unsigned char data[CTR_DRBG_SEEDLEN] );
157
158/**
159 * \brief CTR_DRBG generate random with additional update input
160 *
161 * Note: Automatically reseeds if reseed_counter is reached.
162 *
163 * \param p_rng CTR_DRBG context
164 * \param output Buffer to fill
165 * \param output_len Length of the buffer
Paul Bakker2bc7cf12011-11-29 10:50:51 +0000166 * \param additional Additional data to update with (Can be NULL)
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000167 * \param add_len Length of additional data
168 *
169 * \return 0 if successful, or
170 * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED, or
171 * POLARSSL_ERR_CTR_DRBG_REQUEST_TOO_BIG
172 */
173int ctr_drbg_random_with_add( void *p_rng,
174 unsigned char *output, size_t output_len,
175 unsigned char *additional, size_t add_len );
176
177/**
178 * \brief CTR_DRBG generate random
179 *
180 * Note: Automatically reseeds if reseed_counter is reached.
181 *
182 * \param p_rng CTR_DRBG context
183 * \param output Buffer to fill
184 * \param output_len Length of the buffer
185 *
186 * \return 0 if successful, or
187 * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED, or
188 * POLARSSL_ERR_CTR_DRBG_REQUEST_TOO_BIG
189 */
190int ctr_drbg_random( void *p_rng,
191 unsigned char *output, size_t output_len );
192
193/**
194 * \brief Checkup routine
195 *
196 * \return 0 if successful, or 1 if the test failed
197 */
198int ctr_drbg_self_test( int verbose );
199
200#ifdef __cplusplus
201}
202#endif
203
204#endif /* ctr_drbg.h */