blob: 82ac101d5724e963bab7197c4fc1b4fc2cab4f42 [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é-Gonnard0edee5e2015-01-26 15:29:40 +00006 * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved
Paul Bakker0e04d0e2011-11-27 14:46:59 +00007 *
Manuel Pégourié-Gonnard0edee5e2015-01-26 15:29:40 +00008 * This file is part of mbed TLS (https://www.polarssl.org)
Paul Bakker0e04d0e2011-11-27 14:46:59 +00009 *
10 * 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 Bakker6fa54882013-06-17 15:44:03 +020041
42#if !defined(POLARSSL_CONFIG_OPTIONS)
43#define CTR_DRBG_ENTROPY_LEN 48 /**< Amount of entropy used per seed by default */
Paul Bakker0e04d0e2011-11-27 14:46:59 +000044#define CTR_DRBG_RESEED_INTERVAL 10000 /**< Interval before reseed is performed by default */
Paul Bakker6fa54882013-06-17 15:44:03 +020045#define CTR_DRBG_MAX_INPUT 256 /**< Maximum number of additional input bytes */
46#define CTR_DRBG_MAX_REQUEST 1024 /**< Maximum number of requested bytes per call */
47#define CTR_DRBG_MAX_SEED_INPUT 384 /**< Maximum size of (re)seed buffer */
48#endif /* !POLARSSL_CONFIG_OPTIONS */
Paul Bakker0e04d0e2011-11-27 14:46:59 +000049
50#define CTR_DRBG_PR_OFF 0 /**< No prediction resistance */
51#define CTR_DRBG_PR_ON 1 /**< Prediction resistance enabled */
52
53#ifdef __cplusplus
54extern "C" {
55#endif
56
57/**
58 * \brief CTR_DRBG context structure
59 */
60typedef struct
61{
62 unsigned char counter[16]; /*!< counter (V) */
63 int reseed_counter; /*!< reseed counter */
64 int prediction_resistance; /*!< enable prediction resistance (Automatic
65 reseed before every random generation) */
66 size_t entropy_len; /*!< amount of entropy grabbed on each (re)seed */
67 int reseed_interval; /*!< reseed interval */
68
69 aes_context aes_ctx; /*!< AES context */
70
71 /*
72 * Callbacks (Entropy)
73 */
74 int (*f_entropy)(void *, unsigned char *, size_t);
75
76 void *p_entropy; /*!< context for the entropy function */
77}
78ctr_drbg_context;
79
80/**
81 * \brief CTR_DRBG initialization
82 *
83 * Note: Personalization data can be provided in addition to the more generic
84 * entropy source to make this instantiation as unique as possible.
85 *
86 * \param ctx CTR_DRBG context to be initialized
87 * \param f_entropy Entropy callback (p_entropy, buffer to fill, buffer
88 * length)
89 * \param p_entropy Entropy context
90 * \param custom Personalization data (Device specific identifiers)
Paul Bakker2bc7cf12011-11-29 10:50:51 +000091 * (Can be NULL)
Paul Bakker0e04d0e2011-11-27 14:46:59 +000092 * \param len Length of personalization data
93 *
94 * \return 0 if successful, or
95 * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED
96 */
97int ctr_drbg_init( ctr_drbg_context *ctx,
98 int (*f_entropy)(void *, unsigned char *, size_t),
99 void *p_entropy,
Paul Bakker1bc9efc2011-12-03 11:29:32 +0000100 const unsigned char *custom,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000101 size_t len );
102
103/**
104 * \brief Enable / disable prediction resistance (Default: Off)
105 *
106 * Note: If enabled, entropy is used for ctx->entropy_len before each call!
107 * Only use this if you have ample supply of good entropy!
108 *
109 * \param ctx CTR_DRBG context
110 * \param resistance CTR_DRBG_PR_ON or CTR_DRBG_PR_OFF
111 */
112void ctr_drbg_set_prediction_resistance( ctr_drbg_context *ctx,
113 int resistance );
114
115/**
116 * \brief Set the amount of entropy grabbed on each (re)seed
117 * (Default: CTR_DRBG_ENTROPY_LEN)
118 *
119 * \param ctx CTR_DRBG context
120 * \param len Amount of entropy to grab
121 */
122void ctr_drbg_set_entropy_len( ctr_drbg_context *ctx,
123 size_t len );
124
125/**
126 * \brief Set the reseed interval
127 * (Default: CTR_DRBG_RESEED_INTERVAL)
128 *
129 * \param ctx CTR_DRBG context
130 * \param interval Reseed interval
131 */
132void ctr_drbg_set_reseed_interval( ctr_drbg_context *ctx,
133 int interval );
134
135/**
136 * \brief CTR_DRBG reseeding (extracts data from entropy source)
137 *
138 * \param ctx CTR_DRBG context
Paul Bakker2bc7cf12011-11-29 10:50:51 +0000139 * \param additional Additional data to add to state (Can be NULL)
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000140 * \param len Length of additional data
141 *
142 * \return 0 if successful, or
143 * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED
144 */
145int ctr_drbg_reseed( ctr_drbg_context *ctx,
Paul Bakker1bc9efc2011-12-03 11:29:32 +0000146 const unsigned char *additional, size_t len );
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000147
148/**
149 * \brief CTR_DRBG update state
150 *
151 * \param ctx CTR_DRBG context
Paul Bakker1bc9efc2011-12-03 11:29:32 +0000152 * \param additional Additional data to update state with
153 * \param add_len Length of additional data
Manuel Pégourié-Gonnard258bab02014-11-25 17:41:50 +0100154 *
155 * \note If add_len is greater than CTR_DRBG_MAX_SEED_INPUT,
156 * only the first CTR_DRBG_MAX_SEED_INPUT bytes are used,
157 * the remaining ones are silently discarded.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000158 */
Paul Bakker1bc9efc2011-12-03 11:29:32 +0000159void ctr_drbg_update( ctr_drbg_context *ctx,
Paul Bakkerfc754a92011-12-05 13:23:51 +0000160 const unsigned char *additional, size_t add_len );
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000161
162/**
163 * \brief CTR_DRBG generate random with additional update input
164 *
165 * Note: Automatically reseeds if reseed_counter is reached.
166 *
167 * \param p_rng CTR_DRBG context
168 * \param output Buffer to fill
169 * \param output_len Length of the buffer
Paul Bakker2bc7cf12011-11-29 10:50:51 +0000170 * \param additional Additional data to update with (Can be NULL)
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000171 * \param add_len Length of additional data
172 *
173 * \return 0 if successful, or
174 * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED, or
175 * POLARSSL_ERR_CTR_DRBG_REQUEST_TOO_BIG
176 */
177int ctr_drbg_random_with_add( void *p_rng,
178 unsigned char *output, size_t output_len,
Paul Bakker1bc9efc2011-12-03 11:29:32 +0000179 const unsigned char *additional, size_t add_len );
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000180
181/**
182 * \brief CTR_DRBG generate random
183 *
184 * Note: Automatically reseeds if reseed_counter is reached.
185 *
186 * \param p_rng CTR_DRBG context
187 * \param output Buffer to fill
188 * \param output_len Length of the buffer
189 *
190 * \return 0 if successful, or
191 * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED, or
192 * POLARSSL_ERR_CTR_DRBG_REQUEST_TOO_BIG
193 */
194int ctr_drbg_random( void *p_rng,
195 unsigned char *output, size_t output_len );
196
Paul Bakkerfc754a92011-12-05 13:23:51 +0000197#if defined(POLARSSL_FS_IO)
198/**
199 * \brief Write a seed file
200 *
201 * \param path Name of the file
202 *
Paul Bakkerec8e5db2014-03-26 11:51:25 +0100203 * \return 0 if successful,
204 * POLARSSL_ERR_CTR_DRBG_FILE_IO_ERROR on file error, or
Paul Bakkerfc754a92011-12-05 13:23:51 +0000205 * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED
206 */
207int ctr_drbg_write_seed_file( ctr_drbg_context *ctx, const char *path );
208
209/**
210 * \brief Read and update a seed file. Seed is added to this
211 * instance
212 *
213 * \param path Name of the file
214 *
Paul Bakkerec8e5db2014-03-26 11:51:25 +0100215 * \return 0 if successful,
216 * POLARSSL_ERR_CTR_DRBG_FILE_IO_ERROR on file error,
Paul Bakkerfc754a92011-12-05 13:23:51 +0000217 * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or
218 * POLARSSL_ERR_CTR_DRBG_INPUT_TOO_BIG
219 */
220int ctr_drbg_update_seed_file( ctr_drbg_context *ctx, const char *path );
221#endif
222
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000223/**
224 * \brief Checkup routine
225 *
226 * \return 0 if successful, or 1 if the test failed
227 */
228int ctr_drbg_self_test( int verbose );
229
230#ifdef __cplusplus
231}
232#endif
233
234#endif /* ctr_drbg.h */