blob: 059d3c5c9afd7ba8c18716ae0c81561fa9e72a02 [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é-Gonnard6fb81872015-07-27 11:11:48 +02006 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02007 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
Paul Bakker0e04d0e2011-11-27 14:46:59 +000020 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000021 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker0e04d0e2011-11-27 14:46:59 +000022 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#ifndef MBEDTLS_CTR_DRBG_H
24#define MBEDTLS_CTR_DRBG_H
Paul Bakker0e04d0e2011-11-27 14:46:59 +000025
Paul Bakker0e04d0e2011-11-27 14:46:59 +000026#include "aes.h"
27
Manuel Pégourié-Gonnard0a4fb092015-05-07 12:50:31 +010028#if defined(MBEDTLS_THREADING_C)
29#include "mbedtls/threading.h"
30#endif
31
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#define MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED -0x0034 /**< The entropy source failed. */
33#define MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG -0x0036 /**< Too many random requested in single call. */
34#define MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG -0x0038 /**< Input too large (Entropy + additional). */
35#define MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR -0x003A /**< Read/write error in file. */
Paul Bakker0e04d0e2011-11-27 14:46:59 +000036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#define MBEDTLS_CTR_DRBG_BLOCKSIZE 16 /**< Block size used by the cipher */
38#define MBEDTLS_CTR_DRBG_KEYSIZE 32 /**< Key size used by the cipher */
39#define MBEDTLS_CTR_DRBG_KEYBITS ( MBEDTLS_CTR_DRBG_KEYSIZE * 8 )
40#define MBEDTLS_CTR_DRBG_SEEDLEN ( MBEDTLS_CTR_DRBG_KEYSIZE + MBEDTLS_CTR_DRBG_BLOCKSIZE )
Paul Bakker2bc7cf12011-11-29 10:50:51 +000041 /**< The seed length (counter + AES key) */
Paul Bakker9bcf16c2013-06-24 19:31:17 +020042
Paul Bakker088c5c52014-04-25 11:11:10 +020043/**
44 * \name SECTION: Module settings
45 *
46 * The configuration options you can set for this module are in this section.
47 * Either change them in config.h or define them on the compiler command line.
48 * \{
49 */
50
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#if !defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN)
52#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256)
53#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 +020054#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#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 +020056#endif
Paul Bakker088c5c52014-04-25 11:11:10 +020057#endif
58
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#if !defined(MBEDTLS_CTR_DRBG_RESEED_INTERVAL)
60#define MBEDTLS_CTR_DRBG_RESEED_INTERVAL 10000 /**< Interval before reseed is performed by default */
Paul Bakker088c5c52014-04-25 11:11:10 +020061#endif
62
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if !defined(MBEDTLS_CTR_DRBG_MAX_INPUT)
64#define MBEDTLS_CTR_DRBG_MAX_INPUT 256 /**< Maximum number of additional input bytes */
Paul Bakker088c5c52014-04-25 11:11:10 +020065#endif
66
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#if !defined(MBEDTLS_CTR_DRBG_MAX_REQUEST)
68#define MBEDTLS_CTR_DRBG_MAX_REQUEST 1024 /**< Maximum number of requested bytes per call */
Paul Bakker088c5c52014-04-25 11:11:10 +020069#endif
70
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071#if !defined(MBEDTLS_CTR_DRBG_MAX_SEED_INPUT)
72#define MBEDTLS_CTR_DRBG_MAX_SEED_INPUT 384 /**< Maximum size of (re)seed buffer */
Paul Bakker088c5c52014-04-25 11:11:10 +020073#endif
74
75/* \} name SECTION: Module settings */
Paul Bakker0e04d0e2011-11-27 14:46:59 +000076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077#define MBEDTLS_CTR_DRBG_PR_OFF 0 /**< No prediction resistance */
78#define MBEDTLS_CTR_DRBG_PR_ON 1 /**< Prediction resistance enabled */
Paul Bakker0e04d0e2011-11-27 14:46:59 +000079
80#ifdef __cplusplus
81extern "C" {
82#endif
83
84/**
85 * \brief CTR_DRBG context structure
86 */
87typedef struct
88{
89 unsigned char counter[16]; /*!< counter (V) */
90 int reseed_counter; /*!< reseed counter */
91 int prediction_resistance; /*!< enable prediction resistance (Automatic
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020092 reseed before every random generation) */
93 size_t entropy_len; /*!< amount of entropy grabbed on each
94 (re)seed */
Paul Bakker0e04d0e2011-11-27 14:46:59 +000095 int reseed_interval; /*!< reseed interval */
96
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097 mbedtls_aes_context aes_ctx; /*!< AES context */
Paul Bakker0e04d0e2011-11-27 14:46:59 +000098
99 /*
100 * Callbacks (Entropy)
101 */
102 int (*f_entropy)(void *, unsigned char *, size_t);
103
104 void *p_entropy; /*!< context for the entropy function */
Manuel Pégourié-Gonnard0a4fb092015-05-07 12:50:31 +0100105
106#if defined(MBEDTLS_THREADING_C)
107 mbedtls_threading_mutex_t mutex;
108#endif
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000109}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110mbedtls_ctr_drbg_context;
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000111
112/**
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200113 * \brief CTR_DRBG context initialization
Tillmann Karras588ad502015-09-25 04:27:22 +0200114 * Makes the context ready for mbedtls_ctr_drbg_seed() or
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200115 * mbedtls_ctr_drbg_free().
116 *
117 * \param ctx CTR_DRBG context to be initialized
118 */
119void mbedtls_ctr_drbg_init( mbedtls_ctr_drbg_context *ctx );
120
121/**
122 * \brief CTR_DRBG initial seeding
123 * Seed and setup entropy source for future reseeds.
Paul Bakker9af723c2014-05-01 13:03:14 +0200124 *
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000125 * Note: Personalization data can be provided in addition to the more generic
126 * entropy source to make this instantiation as unique as possible.
127 *
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200128 * \param ctx CTR_DRBG context to be seeded
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000129 * \param f_entropy Entropy callback (p_entropy, buffer to fill, buffer
130 * length)
131 * \param p_entropy Entropy context
132 * \param custom Personalization data (Device specific identifiers)
Paul Bakker2bc7cf12011-11-29 10:50:51 +0000133 * (Can be NULL)
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000134 * \param len Length of personalization data
135 *
136 * \return 0 if successful, or
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 * MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000138 */
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200139int mbedtls_ctr_drbg_seed( mbedtls_ctr_drbg_context *ctx,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000140 int (*f_entropy)(void *, unsigned char *, size_t),
141 void *p_entropy,
Paul Bakker1bc9efc2011-12-03 11:29:32 +0000142 const unsigned char *custom,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000143 size_t len );
144
145/**
Paul Bakkerfff03662014-06-18 16:21:25 +0200146 * \brief Clear CTR_CRBG context data
147 *
148 * \param ctx CTR_DRBG context to clear
149 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150void mbedtls_ctr_drbg_free( mbedtls_ctr_drbg_context *ctx );
Paul Bakkerfff03662014-06-18 16:21:25 +0200151
152/**
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000153 * \brief Enable / disable prediction resistance (Default: Off)
154 *
155 * Note: If enabled, entropy is used for ctx->entropy_len before each call!
156 * Only use this if you have ample supply of good entropy!
157 *
158 * \param ctx CTR_DRBG context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 * \param resistance MBEDTLS_CTR_DRBG_PR_ON or MBEDTLS_CTR_DRBG_PR_OFF
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000160 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161void mbedtls_ctr_drbg_set_prediction_resistance( mbedtls_ctr_drbg_context *ctx,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000162 int resistance );
163
164/**
165 * \brief Set the amount of entropy grabbed on each (re)seed
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 * (Default: MBEDTLS_CTR_DRBG_ENTROPY_LEN)
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000167 *
168 * \param ctx CTR_DRBG context
169 * \param len Amount of entropy to grab
170 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171void mbedtls_ctr_drbg_set_entropy_len( mbedtls_ctr_drbg_context *ctx,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000172 size_t len );
173
174/**
175 * \brief Set the reseed interval
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 * (Default: MBEDTLS_CTR_DRBG_RESEED_INTERVAL)
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000177 *
178 * \param ctx CTR_DRBG context
179 * \param interval Reseed interval
180 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181void mbedtls_ctr_drbg_set_reseed_interval( mbedtls_ctr_drbg_context *ctx,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000182 int interval );
183
184/**
185 * \brief CTR_DRBG reseeding (extracts data from entropy source)
Paul Bakker9af723c2014-05-01 13:03:14 +0200186 *
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000187 * \param ctx CTR_DRBG context
Paul Bakker2bc7cf12011-11-29 10:50:51 +0000188 * \param additional Additional data to add to state (Can be NULL)
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000189 * \param len Length of additional data
190 *
191 * \return 0 if successful, or
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192 * MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000193 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194int mbedtls_ctr_drbg_reseed( mbedtls_ctr_drbg_context *ctx,
Paul Bakker1bc9efc2011-12-03 11:29:32 +0000195 const unsigned char *additional, size_t len );
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000196
197/**
198 * \brief CTR_DRBG update state
199 *
200 * \param ctx CTR_DRBG context
Paul Bakker1bc9efc2011-12-03 11:29:32 +0000201 * \param additional Additional data to update state with
202 * \param add_len Length of additional data
Manuel Pégourié-Gonnard5cb4b312014-11-25 17:41:50 +0100203 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 * \note If add_len is greater than MBEDTLS_CTR_DRBG_MAX_SEED_INPUT,
205 * only the first MBEDTLS_CTR_DRBG_MAX_SEED_INPUT bytes are used,
Manuel Pégourié-Gonnard5cb4b312014-11-25 17:41:50 +0100206 * the remaining ones are silently discarded.
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000207 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208void mbedtls_ctr_drbg_update( mbedtls_ctr_drbg_context *ctx,
Paul Bakkerfc754a92011-12-05 13:23:51 +0000209 const unsigned char *additional, size_t add_len );
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000210
211/**
212 * \brief CTR_DRBG generate random with additional update input
213 *
214 * Note: Automatically reseeds if reseed_counter is reached.
215 *
216 * \param p_rng CTR_DRBG context
217 * \param output Buffer to fill
218 * \param output_len Length of the buffer
Paul Bakker2bc7cf12011-11-29 10:50:51 +0000219 * \param additional Additional data to update with (Can be NULL)
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000220 * \param add_len Length of additional data
221 *
222 * \return 0 if successful, or
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 * MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED, or
224 * MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000225 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226int mbedtls_ctr_drbg_random_with_add( void *p_rng,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000227 unsigned char *output, size_t output_len,
Paul Bakker1bc9efc2011-12-03 11:29:32 +0000228 const unsigned char *additional, size_t add_len );
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000229
230/**
231 * \brief CTR_DRBG generate random
232 *
233 * Note: Automatically reseeds if reseed_counter is reached.
234 *
235 * \param p_rng CTR_DRBG context
236 * \param output Buffer to fill
237 * \param output_len Length of the buffer
238 *
239 * \return 0 if successful, or
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 * MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED, or
241 * MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000242 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243int mbedtls_ctr_drbg_random( void *p_rng,
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000244 unsigned char *output, size_t output_len );
245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246#if defined(MBEDTLS_FS_IO)
Paul Bakkerfc754a92011-12-05 13:23:51 +0000247/**
248 * \brief Write a seed file
249 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200250 * \param ctx CTR_DRBG context
Paul Bakkerfc754a92011-12-05 13:23:51 +0000251 * \param path Name of the file
252 *
Paul Bakker766a5d02014-03-26 11:51:25 +0100253 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 * MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR on file error, or
255 * MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED
Paul Bakkerfc754a92011-12-05 13:23:51 +0000256 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257int mbedtls_ctr_drbg_write_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
Paul Bakkerfc754a92011-12-05 13:23:51 +0000258
259/**
260 * \brief Read and update a seed file. Seed is added to this
261 * instance
262 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200263 * \param ctx CTR_DRBG context
Paul Bakkerfc754a92011-12-05 13:23:51 +0000264 * \param path Name of the file
265 *
Paul Bakker766a5d02014-03-26 11:51:25 +0100266 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 * MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR on file error,
268 * MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or
269 * MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG
Paul Bakkerfc754a92011-12-05 13:23:51 +0000270 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271int mbedtls_ctr_drbg_update_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
272#endif /* MBEDTLS_FS_IO */
Paul Bakkerfc754a92011-12-05 13:23:51 +0000273
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000274/**
275 * \brief Checkup routine
276 *
277 * \return 0 if successful, or 1 if the test failed
278 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279int mbedtls_ctr_drbg_self_test( int verbose );
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000280
Paul Bakker534f82c2013-06-25 16:47:55 +0200281/* Internal functions (do not call directly) */
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200282int mbedtls_ctr_drbg_seed_entropy_len( mbedtls_ctr_drbg_context *,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200283 int (*)(void *, unsigned char *, size_t), void *,
284 const unsigned char *, size_t, size_t );
Paul Bakker534f82c2013-06-25 16:47:55 +0200285
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000286#ifdef __cplusplus
287}
288#endif
289
290#endif /* ctr_drbg.h */