blob: 3ea4af2ec2285e3ba923f28bf9fbdf2867e58ebb [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é-Gonnardfe446432015-03-06 13:17:10 +00008 * This file is part of mbed TLS (https://tls.mbed.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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#ifndef MBEDTLS_CTR_DRBG_H
25#define MBEDTLS_CTR_DRBG_H
Paul Bakker0e04d0e2011-11-27 14:46:59 +000026
Paul Bakker0e04d0e2011-11-27 14:46:59 +000027#include "aes.h"
28
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#define MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED -0x0034 /**< The entropy source failed. */
30#define MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG -0x0036 /**< Too many random requested in single call. */
31#define MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG -0x0038 /**< Input too large (Entropy + additional). */
32#define MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR -0x003A /**< Read/write error in file. */
Paul Bakker0e04d0e2011-11-27 14:46:59 +000033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#define MBEDTLS_CTR_DRBG_BLOCKSIZE 16 /**< Block size used by the cipher */
35#define MBEDTLS_CTR_DRBG_KEYSIZE 32 /**< Key size used by the cipher */
36#define MBEDTLS_CTR_DRBG_KEYBITS ( MBEDTLS_CTR_DRBG_KEYSIZE * 8 )
37#define MBEDTLS_CTR_DRBG_SEEDLEN ( MBEDTLS_CTR_DRBG_KEYSIZE + MBEDTLS_CTR_DRBG_BLOCKSIZE )
Paul Bakker2bc7cf12011-11-29 10:50:51 +000038 /**< The seed length (counter + AES key) */
Paul Bakker9bcf16c2013-06-24 19:31:17 +020039
Paul Bakker088c5c52014-04-25 11:11:10 +020040/**
41 * \name SECTION: Module settings
42 *
43 * The configuration options you can set for this module are in this section.
44 * Either change them in config.h or define them on the compiler command line.
45 * \{
46 */
47
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#if !defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN)
49#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256)
50#define MBEDTLS_CTR_DRBG_ENTROPY_LEN 48 /**< Amount of entropy used per seed by default (48 with SHA-512, 32 with SHA-256) */
Paul Bakkerfb08fd22013-08-27 15:06:26 +020051#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#define MBEDTLS_CTR_DRBG_ENTROPY_LEN 32 /**< Amount of entropy used per seed by default (48 with SHA-512, 32 with SHA-256) */
Paul Bakkerfb08fd22013-08-27 15:06:26 +020053#endif
Paul Bakker088c5c52014-04-25 11:11:10 +020054#endif
55
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#if !defined(MBEDTLS_CTR_DRBG_RESEED_INTERVAL)
57#define MBEDTLS_CTR_DRBG_RESEED_INTERVAL 10000 /**< Interval before reseed is performed by default */
Paul Bakker088c5c52014-04-25 11:11:10 +020058#endif
59
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#if !defined(MBEDTLS_CTR_DRBG_MAX_INPUT)
61#define MBEDTLS_CTR_DRBG_MAX_INPUT 256 /**< Maximum number of additional input bytes */
Paul Bakker088c5c52014-04-25 11:11:10 +020062#endif
63
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#if !defined(MBEDTLS_CTR_DRBG_MAX_REQUEST)
65#define MBEDTLS_CTR_DRBG_MAX_REQUEST 1024 /**< Maximum number of requested bytes per call */
Paul Bakker088c5c52014-04-25 11:11:10 +020066#endif
67
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068#if !defined(MBEDTLS_CTR_DRBG_MAX_SEED_INPUT)
69#define MBEDTLS_CTR_DRBG_MAX_SEED_INPUT 384 /**< Maximum size of (re)seed buffer */
Paul Bakker088c5c52014-04-25 11:11:10 +020070#endif
71
72/* \} name SECTION: Module settings */
Paul Bakker0e04d0e2011-11-27 14:46:59 +000073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074#define MBEDTLS_CTR_DRBG_PR_OFF 0 /**< No prediction resistance */
75#define MBEDTLS_CTR_DRBG_PR_ON 1 /**< Prediction resistance enabled */
Paul Bakker0e04d0e2011-11-27 14:46:59 +000076
77#ifdef __cplusplus
78extern "C" {
79#endif
80
81/**
82 * \brief CTR_DRBG context structure
83 */
84typedef struct
85{
86 unsigned char counter[16]; /*!< counter (V) */
87 int reseed_counter; /*!< reseed counter */
88 int prediction_resistance; /*!< enable prediction resistance (Automatic
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020089 reseed before every random generation) */
90 size_t entropy_len; /*!< amount of entropy grabbed on each
91 (re)seed */
Paul Bakker0e04d0e2011-11-27 14:46:59 +000092 int reseed_interval; /*!< reseed interval */
93
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 mbedtls_aes_context aes_ctx; /*!< AES context */
Paul Bakker0e04d0e2011-11-27 14:46:59 +000095
96 /*
97 * Callbacks (Entropy)
98 */
99 int (*f_entropy)(void *, unsigned char *, size_t);
100
101 void *p_entropy; /*!< context for the entropy function */
102}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103mbedtls_ctr_drbg_context;
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000104
105/**
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200106 * \brief CTR_DRBG context initialization
107 * Makes the context ready for mbetls_ctr_drbg_seed() or
108 * mbedtls_ctr_drbg_free().
109 *
110 * \param ctx CTR_DRBG context to be initialized
111 */
112void mbedtls_ctr_drbg_init( mbedtls_ctr_drbg_context *ctx );
113
114/**
115 * \brief CTR_DRBG initial seeding
116 * Seed and setup entropy source for future reseeds.
Paul Bakker9af723c2014-05-01 13:03:14 +0200117 *
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000118 * Note: Personalization data can be provided in addition to the more generic
119 * entropy source to make this instantiation as unique as possible.
120 *
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200121 * \param ctx CTR_DRBG context to be seeded
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000122 * \param f_entropy Entropy callback (p_entropy, buffer to fill, buffer
123 * length)
124 * \param p_entropy Entropy context
125 * \param custom Personalization data (Device specific identifiers)
Paul Bakker2bc7cf12011-11-29 10:50:51 +0000126 * (Can be NULL)
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000127 * \param len Length of personalization data
128 *
129 * \return 0 if successful, or
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 * MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000131 */
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200132int mbedtls_ctr_drbg_seed( mbedtls_ctr_drbg_context *ctx,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000133 int (*f_entropy)(void *, unsigned char *, size_t),
134 void *p_entropy,
Paul Bakker1bc9efc2011-12-03 11:29:32 +0000135 const unsigned char *custom,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000136 size_t len );
137
138/**
Paul Bakkerfff03662014-06-18 16:21:25 +0200139 * \brief Clear CTR_CRBG context data
140 *
141 * \param ctx CTR_DRBG context to clear
142 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143void mbedtls_ctr_drbg_free( mbedtls_ctr_drbg_context *ctx );
Paul Bakkerfff03662014-06-18 16:21:25 +0200144
145/**
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000146 * \brief Enable / disable prediction resistance (Default: Off)
147 *
148 * Note: If enabled, entropy is used for ctx->entropy_len before each call!
149 * Only use this if you have ample supply of good entropy!
150 *
151 * \param ctx CTR_DRBG context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 * \param resistance MBEDTLS_CTR_DRBG_PR_ON or MBEDTLS_CTR_DRBG_PR_OFF
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000153 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154void mbedtls_ctr_drbg_set_prediction_resistance( mbedtls_ctr_drbg_context *ctx,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000155 int resistance );
156
157/**
158 * \brief Set the amount of entropy grabbed on each (re)seed
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 * (Default: MBEDTLS_CTR_DRBG_ENTROPY_LEN)
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000160 *
161 * \param ctx CTR_DRBG context
162 * \param len Amount of entropy to grab
163 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164void mbedtls_ctr_drbg_set_entropy_len( mbedtls_ctr_drbg_context *ctx,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000165 size_t len );
166
167/**
168 * \brief Set the reseed interval
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169 * (Default: MBEDTLS_CTR_DRBG_RESEED_INTERVAL)
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000170 *
171 * \param ctx CTR_DRBG context
172 * \param interval Reseed interval
173 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174void mbedtls_ctr_drbg_set_reseed_interval( mbedtls_ctr_drbg_context *ctx,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000175 int interval );
176
177/**
178 * \brief CTR_DRBG reseeding (extracts data from entropy source)
Paul Bakker9af723c2014-05-01 13:03:14 +0200179 *
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000180 * \param ctx CTR_DRBG context
Paul Bakker2bc7cf12011-11-29 10:50:51 +0000181 * \param additional Additional data to add to state (Can be NULL)
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000182 * \param len Length of additional data
183 *
184 * \return 0 if successful, or
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 * MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000186 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187int mbedtls_ctr_drbg_reseed( mbedtls_ctr_drbg_context *ctx,
Paul Bakker1bc9efc2011-12-03 11:29:32 +0000188 const unsigned char *additional, size_t len );
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000189
190/**
191 * \brief CTR_DRBG update state
192 *
193 * \param ctx CTR_DRBG context
Paul Bakker1bc9efc2011-12-03 11:29:32 +0000194 * \param additional Additional data to update state with
195 * \param add_len Length of additional data
Manuel Pégourié-Gonnard5cb4b312014-11-25 17:41:50 +0100196 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 * \note If add_len is greater than MBEDTLS_CTR_DRBG_MAX_SEED_INPUT,
198 * only the first MBEDTLS_CTR_DRBG_MAX_SEED_INPUT bytes are used,
Manuel Pégourié-Gonnard5cb4b312014-11-25 17:41:50 +0100199 * the remaining ones are silently discarded.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000200 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201void mbedtls_ctr_drbg_update( mbedtls_ctr_drbg_context *ctx,
Paul Bakkerfc754a92011-12-05 13:23:51 +0000202 const unsigned char *additional, size_t add_len );
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000203
204/**
205 * \brief CTR_DRBG generate random with additional update input
206 *
207 * Note: Automatically reseeds if reseed_counter is reached.
208 *
209 * \param p_rng CTR_DRBG context
210 * \param output Buffer to fill
211 * \param output_len Length of the buffer
Paul Bakker2bc7cf12011-11-29 10:50:51 +0000212 * \param additional Additional data to update with (Can be NULL)
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000213 * \param add_len Length of additional data
214 *
215 * \return 0 if successful, or
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 * MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED, or
217 * MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000218 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219int mbedtls_ctr_drbg_random_with_add( void *p_rng,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000220 unsigned char *output, size_t output_len,
Paul Bakker1bc9efc2011-12-03 11:29:32 +0000221 const unsigned char *additional, size_t add_len );
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000222
223/**
224 * \brief CTR_DRBG generate random
225 *
226 * Note: Automatically reseeds if reseed_counter is reached.
227 *
228 * \param p_rng CTR_DRBG context
229 * \param output Buffer to fill
230 * \param output_len Length of the buffer
231 *
232 * \return 0 if successful, or
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 * MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED, or
234 * MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000235 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236int mbedtls_ctr_drbg_random( void *p_rng,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000237 unsigned char *output, size_t output_len );
238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239#if defined(MBEDTLS_FS_IO)
Paul Bakkerfc754a92011-12-05 13:23:51 +0000240/**
241 * \brief Write a seed file
242 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200243 * \param ctx CTR_DRBG context
Paul Bakkerfc754a92011-12-05 13:23:51 +0000244 * \param path Name of the file
245 *
Paul Bakker766a5d02014-03-26 11:51:25 +0100246 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247 * MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR on file error, or
248 * MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED
Paul Bakkerfc754a92011-12-05 13:23:51 +0000249 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250int mbedtls_ctr_drbg_write_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
Paul Bakkerfc754a92011-12-05 13:23:51 +0000251
252/**
253 * \brief Read and update a seed file. Seed is added to this
254 * instance
255 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200256 * \param ctx CTR_DRBG context
Paul Bakkerfc754a92011-12-05 13:23:51 +0000257 * \param path Name of the file
258 *
Paul Bakker766a5d02014-03-26 11:51:25 +0100259 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 * MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR on file error,
261 * MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or
262 * MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG
Paul Bakkerfc754a92011-12-05 13:23:51 +0000263 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264int mbedtls_ctr_drbg_update_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
265#endif /* MBEDTLS_FS_IO */
Paul Bakkerfc754a92011-12-05 13:23:51 +0000266
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000267/**
268 * \brief Checkup routine
269 *
270 * \return 0 if successful, or 1 if the test failed
271 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272int mbedtls_ctr_drbg_self_test( int verbose );
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000273
Paul Bakker534f82c2013-06-25 16:47:55 +0200274/* Internal functions (do not call directly) */
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200275int mbedtls_ctr_drbg_seed_entropy_len( mbedtls_ctr_drbg_context *,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200276 int (*)(void *, unsigned char *, size_t), void *,
277 const unsigned char *, size_t, size_t );
Paul Bakker534f82c2013-06-25 16:47:55 +0200278
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000279#ifdef __cplusplus
280}
281#endif
282
283#endif /* ctr_drbg.h */