blob: 01cd826a17365bf6e23f5c3a46b89e92c81ecb44 [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)
Darryl Greena40a1012018-01-05 15:33:17 +00005 */
6/*
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02007 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02008 * SPDX-License-Identifier: Apache-2.0
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
Paul Bakker0e04d0e2011-11-27 14:46:59 +000021 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000022 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker0e04d0e2011-11-27 14:46:59 +000023 */
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é-Gonnard0a4fb092015-05-07 12:50:31 +010029#if defined(MBEDTLS_THREADING_C)
30#include "mbedtls/threading.h"
31#endif
32
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#define MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED -0x0034 /**< The entropy source failed. */
34#define MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG -0x0036 /**< Too many random requested in single call. */
35#define MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG -0x0038 /**< Input too large (Entropy + additional). */
36#define MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR -0x003A /**< Read/write error in file. */
Paul Bakker0e04d0e2011-11-27 14:46:59 +000037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#define MBEDTLS_CTR_DRBG_BLOCKSIZE 16 /**< Block size used by the cipher */
39#define MBEDTLS_CTR_DRBG_KEYSIZE 32 /**< Key size used by the cipher */
40#define MBEDTLS_CTR_DRBG_KEYBITS ( MBEDTLS_CTR_DRBG_KEYSIZE * 8 )
41#define MBEDTLS_CTR_DRBG_SEEDLEN ( MBEDTLS_CTR_DRBG_KEYSIZE + MBEDTLS_CTR_DRBG_BLOCKSIZE )
Paul Bakker2bc7cf12011-11-29 10:50:51 +000042 /**< The seed length (counter + AES key) */
Paul Bakker9bcf16c2013-06-24 19:31:17 +020043
Paul Bakker088c5c52014-04-25 11:11:10 +020044/**
45 * \name SECTION: Module settings
46 *
47 * The configuration options you can set for this module are in this section.
48 * Either change them in config.h or define them on the compiler command line.
49 * \{
50 */
51
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if !defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN)
53#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256)
54#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 +020055#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#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 +020057#endif
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_RESEED_INTERVAL)
61#define MBEDTLS_CTR_DRBG_RESEED_INTERVAL 10000 /**< Interval before reseed is performed by default */
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_INPUT)
65#define MBEDTLS_CTR_DRBG_MAX_INPUT 256 /**< Maximum number of additional input bytes */
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_REQUEST)
69#define MBEDTLS_CTR_DRBG_MAX_REQUEST 1024 /**< Maximum number of requested bytes per call */
Paul Bakker088c5c52014-04-25 11:11:10 +020070#endif
71
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#if !defined(MBEDTLS_CTR_DRBG_MAX_SEED_INPUT)
73#define MBEDTLS_CTR_DRBG_MAX_SEED_INPUT 384 /**< Maximum size of (re)seed buffer */
Paul Bakker088c5c52014-04-25 11:11:10 +020074#endif
75
76/* \} name SECTION: Module settings */
Paul Bakker0e04d0e2011-11-27 14:46:59 +000077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078#define MBEDTLS_CTR_DRBG_PR_OFF 0 /**< No prediction resistance */
79#define MBEDTLS_CTR_DRBG_PR_ON 1 /**< Prediction resistance enabled */
Paul Bakker0e04d0e2011-11-27 14:46:59 +000080
81#ifdef __cplusplus
82extern "C" {
83#endif
84
85/**
86 * \brief CTR_DRBG context structure
87 */
88typedef struct
89{
90 unsigned char counter[16]; /*!< counter (V) */
91 int reseed_counter; /*!< reseed counter */
92 int prediction_resistance; /*!< enable prediction resistance (Automatic
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020093 reseed before every random generation) */
94 size_t entropy_len; /*!< amount of entropy grabbed on each
95 (re)seed */
Paul Bakker0e04d0e2011-11-27 14:46:59 +000096 int reseed_interval; /*!< reseed interval */
97
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 mbedtls_aes_context aes_ctx; /*!< AES context */
Paul Bakker0e04d0e2011-11-27 14:46:59 +000099
100 /*
101 * Callbacks (Entropy)
102 */
103 int (*f_entropy)(void *, unsigned char *, size_t);
104
105 void *p_entropy; /*!< context for the entropy function */
Manuel Pégourié-Gonnard0a4fb092015-05-07 12:50:31 +0100106
107#if defined(MBEDTLS_THREADING_C)
108 mbedtls_threading_mutex_t mutex;
109#endif
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000110}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111mbedtls_ctr_drbg_context;
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000112
113/**
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200114 * \brief CTR_DRBG context initialization
Tillmann Karras588ad502015-09-25 04:27:22 +0200115 * Makes the context ready for mbedtls_ctr_drbg_seed() or
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200116 * mbedtls_ctr_drbg_free().
117 *
118 * \param ctx CTR_DRBG context to be initialized
119 */
120void mbedtls_ctr_drbg_init( mbedtls_ctr_drbg_context *ctx );
121
122/**
123 * \brief CTR_DRBG initial seeding
124 * Seed and setup entropy source for future reseeds.
Paul Bakker9af723c2014-05-01 13:03:14 +0200125 *
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000126 * Note: Personalization data can be provided in addition to the more generic
127 * entropy source to make this instantiation as unique as possible.
128 *
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200129 * \param ctx CTR_DRBG context to be seeded
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000130 * \param f_entropy Entropy callback (p_entropy, buffer to fill, buffer
131 * length)
132 * \param p_entropy Entropy context
133 * \param custom Personalization data (Device specific identifiers)
Paul Bakker2bc7cf12011-11-29 10:50:51 +0000134 * (Can be NULL)
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000135 * \param len Length of personalization data
136 *
137 * \return 0 if successful, or
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 * MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000139 */
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200140int mbedtls_ctr_drbg_seed( mbedtls_ctr_drbg_context *ctx,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000141 int (*f_entropy)(void *, unsigned char *, size_t),
142 void *p_entropy,
Paul Bakker1bc9efc2011-12-03 11:29:32 +0000143 const unsigned char *custom,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000144 size_t len );
145
146/**
Paul Bakkerfff03662014-06-18 16:21:25 +0200147 * \brief Clear CTR_CRBG context data
148 *
149 * \param ctx CTR_DRBG context to clear
150 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151void mbedtls_ctr_drbg_free( mbedtls_ctr_drbg_context *ctx );
Paul Bakkerfff03662014-06-18 16:21:25 +0200152
153/**
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000154 * \brief Enable / disable prediction resistance (Default: Off)
155 *
156 * Note: If enabled, entropy is used for ctx->entropy_len before each call!
157 * Only use this if you have ample supply of good entropy!
158 *
159 * \param ctx CTR_DRBG context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 * \param resistance MBEDTLS_CTR_DRBG_PR_ON or MBEDTLS_CTR_DRBG_PR_OFF
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000161 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162void mbedtls_ctr_drbg_set_prediction_resistance( mbedtls_ctr_drbg_context *ctx,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000163 int resistance );
164
165/**
166 * \brief Set the amount of entropy grabbed on each (re)seed
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 * (Default: MBEDTLS_CTR_DRBG_ENTROPY_LEN)
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000168 *
169 * \param ctx CTR_DRBG context
170 * \param len Amount of entropy to grab
171 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172void mbedtls_ctr_drbg_set_entropy_len( mbedtls_ctr_drbg_context *ctx,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000173 size_t len );
174
175/**
176 * \brief Set the reseed interval
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 * (Default: MBEDTLS_CTR_DRBG_RESEED_INTERVAL)
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000178 *
179 * \param ctx CTR_DRBG context
180 * \param interval Reseed interval
181 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182void mbedtls_ctr_drbg_set_reseed_interval( mbedtls_ctr_drbg_context *ctx,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000183 int interval );
184
185/**
186 * \brief CTR_DRBG reseeding (extracts data from entropy source)
Paul Bakker9af723c2014-05-01 13:03:14 +0200187 *
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000188 * \param ctx CTR_DRBG context
Paul Bakker2bc7cf12011-11-29 10:50:51 +0000189 * \param additional Additional data to add to state (Can be NULL)
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000190 * \param len Length of additional data
191 *
192 * \return 0 if successful, or
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 * MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000194 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195int mbedtls_ctr_drbg_reseed( mbedtls_ctr_drbg_context *ctx,
Paul Bakker1bc9efc2011-12-03 11:29:32 +0000196 const unsigned char *additional, size_t len );
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000197
198/**
199 * \brief CTR_DRBG update state
200 *
201 * \param ctx CTR_DRBG context
Paul Bakker1bc9efc2011-12-03 11:29:32 +0000202 * \param additional Additional data to update state with
203 * \param add_len Length of additional data
Manuel Pégourié-Gonnard5cb4b312014-11-25 17:41:50 +0100204 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 * \note If add_len is greater than MBEDTLS_CTR_DRBG_MAX_SEED_INPUT,
206 * only the first MBEDTLS_CTR_DRBG_MAX_SEED_INPUT bytes are used,
Manuel Pégourié-Gonnard5cb4b312014-11-25 17:41:50 +0100207 * the remaining ones are silently discarded.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000208 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209void mbedtls_ctr_drbg_update( mbedtls_ctr_drbg_context *ctx,
Paul Bakkerfc754a92011-12-05 13:23:51 +0000210 const unsigned char *additional, size_t add_len );
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000211
212/**
213 * \brief CTR_DRBG generate random with additional update input
214 *
215 * Note: Automatically reseeds if reseed_counter is reached.
216 *
217 * \param p_rng CTR_DRBG context
218 * \param output Buffer to fill
219 * \param output_len Length of the buffer
Paul Bakker2bc7cf12011-11-29 10:50:51 +0000220 * \param additional Additional data to update with (Can be NULL)
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000221 * \param add_len Length of additional data
222 *
223 * \return 0 if successful, or
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 * MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED, or
225 * MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000226 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227int mbedtls_ctr_drbg_random_with_add( void *p_rng,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000228 unsigned char *output, size_t output_len,
Paul Bakker1bc9efc2011-12-03 11:29:32 +0000229 const unsigned char *additional, size_t add_len );
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000230
231/**
232 * \brief CTR_DRBG generate random
233 *
234 * Note: Automatically reseeds if reseed_counter is reached.
235 *
236 * \param p_rng CTR_DRBG context
237 * \param output Buffer to fill
238 * \param output_len Length of the buffer
239 *
240 * \return 0 if successful, or
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 * MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED, or
242 * MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000243 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244int mbedtls_ctr_drbg_random( void *p_rng,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000245 unsigned char *output, size_t output_len );
246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247#if defined(MBEDTLS_FS_IO)
Paul Bakkerfc754a92011-12-05 13:23:51 +0000248/**
249 * \brief Write a seed file
250 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200251 * \param ctx CTR_DRBG context
Paul Bakkerfc754a92011-12-05 13:23:51 +0000252 * \param path Name of the file
253 *
Paul Bakker766a5d02014-03-26 11:51:25 +0100254 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 * MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR on file error, or
256 * MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED
Paul Bakkerfc754a92011-12-05 13:23:51 +0000257 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258int mbedtls_ctr_drbg_write_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
Paul Bakkerfc754a92011-12-05 13:23:51 +0000259
260/**
261 * \brief Read and update a seed file. Seed is added to this
262 * instance
263 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200264 * \param ctx CTR_DRBG context
Paul Bakkerfc754a92011-12-05 13:23:51 +0000265 * \param path Name of the file
266 *
Paul Bakker766a5d02014-03-26 11:51:25 +0100267 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 * MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR on file error,
269 * MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or
270 * MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG
Paul Bakkerfc754a92011-12-05 13:23:51 +0000271 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272int mbedtls_ctr_drbg_update_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
273#endif /* MBEDTLS_FS_IO */
Paul Bakkerfc754a92011-12-05 13:23:51 +0000274
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000275/**
276 * \brief Checkup routine
277 *
278 * \return 0 if successful, or 1 if the test failed
279 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280int mbedtls_ctr_drbg_self_test( int verbose );
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000281
Paul Bakker534f82c2013-06-25 16:47:55 +0200282/* Internal functions (do not call directly) */
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200283int mbedtls_ctr_drbg_seed_entropy_len( mbedtls_ctr_drbg_context *,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200284 int (*)(void *, unsigned char *, size_t), void *,
285 const unsigned char *, size_t, size_t );
Paul Bakker534f82c2013-06-25 16:47:55 +0200286
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000287#ifdef __cplusplus
288}
289#endif
290
291#endif /* ctr_drbg.h */