1 /** 2 * \file ctr_drbg.h 3 * 4 * \brief This file contains definitions and functions for the 5 * CTR_DRBG pseudorandom generator. 6 * 7 * CTR_DRBG is a standardized way of building a PRNG from a block-cipher 8 * in counter mode operation, as defined in <em>NIST SP 800-90A: 9 * Recommendation for Random Number Generation Using Deterministic Random 10 * Bit Generators</em>. 11 * 12 * The Mbed TLS implementation of CTR_DRBG uses AES-256 (default) or AES-128 13 * (if \c MBEDTLS_CTR_DRBG_USE_128_BIT_KEY is enabled at compile time) 14 * as the underlying block cipher, with a derivation function. 15 * The initial seeding grabs #MBEDTLS_CTR_DRBG_ENTROPY_LEN bytes of entropy. 16 * See the documentation of mbedtls_ctr_drbg_seed() for more details. 17 * 18 * Based on NIST SP 800-90A §10.2.1 table 3 and NIST SP 800-57 part 1 table 2, 19 * here are the security strengths achieved in typical configuration: 20 * - 256 bits under the default configuration of the library, with AES-256 21 * and with #MBEDTLS_CTR_DRBG_ENTROPY_LEN set to 48 or more. 22 * - 256 bits if AES-256 is used, #MBEDTLS_CTR_DRBG_ENTROPY_LEN is set 23 * to 32 or more, and the DRBG is initialized with an explicit 24 * nonce in the \c custom parameter to mbedtls_ctr_drbg_seed(). 25 * - 128 bits if AES-256 is used but #MBEDTLS_CTR_DRBG_ENTROPY_LEN is 26 * between 24 and 47 and the DRBG is not initialized with an explicit 27 * nonce (see mbedtls_ctr_drbg_seed()). 28 * - 128 bits if AES-128 is used (\c MBEDTLS_CTR_DRBG_USE_128_BIT_KEY enabled) 29 * and #MBEDTLS_CTR_DRBG_ENTROPY_LEN is set to 24 or more (which is 30 * always the case unless it is explicitly set to a different value 31 * in config.h). 32 * 33 * Note that the value of #MBEDTLS_CTR_DRBG_ENTROPY_LEN defaults to: 34 * - \c 48 if the module \c MBEDTLS_SHA512_C is enabled and the symbol 35 * \c MBEDTLS_ENTROPY_FORCE_SHA256 is disabled at compile time. 36 * This is the default configuration of the library. 37 * - \c 32 if the module \c MBEDTLS_SHA512_C is disabled at compile time. 38 * - \c 32 if \c MBEDTLS_ENTROPY_FORCE_SHA256 is enabled at compile time. 39 */ 40 /* 41 * Copyright (C) 2006-2019, Arm Limited (or its affiliates), All Rights Reserved 42 * SPDX-License-Identifier: Apache-2.0 43 * 44 * Licensed under the Apache License, Version 2.0 (the "License"); you may 45 * not use this file except in compliance with the License. 46 * You may obtain a copy of the License at 47 * 48 * http://www.apache.org/licenses/LICENSE-2.0 49 * 50 * Unless required by applicable law or agreed to in writing, software 51 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 52 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 53 * See the License for the specific language governing permissions and 54 * limitations under the License. 55 * 56 * This file is part of Mbed TLS (https://tls.mbed.org) 57 */ 58 59 #ifndef MBEDTLS_CTR_DRBG_H 60 #define MBEDTLS_CTR_DRBG_H 61 62 #if !defined(MBEDTLS_CONFIG_FILE) 63 #include "config.h" 64 #else 65 #include MBEDTLS_CONFIG_FILE 66 #endif 67 68 #include "aes.h" 69 70 #if defined(MBEDTLS_THREADING_C) 71 #include "threading.h" 72 #endif 73 74 #define MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED -0x0034 /**< The entropy source failed. */ 75 #define MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG -0x0036 /**< The requested random buffer length is too big. */ 76 #define MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG -0x0038 /**< The input (entropy + additional data) is too large. */ 77 #define MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR -0x003A /**< Read or write error in file. */ 78 79 #define MBEDTLS_CTR_DRBG_BLOCKSIZE 16 /**< The block size used by the cipher. */ 80 81 #if defined(MBEDTLS_CTR_DRBG_USE_128_BIT_KEY) 82 #define MBEDTLS_CTR_DRBG_KEYSIZE 16 83 /**< The key size in bytes used by the cipher. 84 * 85 * Compile-time choice: 16 bytes (128 bits) 86 * because #MBEDTLS_CTR_DRBG_USE_128_BIT_KEY is enabled. 87 */ 88 #else 89 #define MBEDTLS_CTR_DRBG_KEYSIZE 32 90 /**< The key size in bytes used by the cipher. 91 * 92 * Compile-time choice: 32 bytes (256 bits) 93 * because \c MBEDTLS_CTR_DRBG_USE_128_BIT_KEY is disabled. 94 */ 95 #endif 96 97 #define MBEDTLS_CTR_DRBG_KEYBITS ( MBEDTLS_CTR_DRBG_KEYSIZE * 8 ) /**< The key size for the DRBG operation, in bits. */ 98 #define MBEDTLS_CTR_DRBG_SEEDLEN ( MBEDTLS_CTR_DRBG_KEYSIZE + MBEDTLS_CTR_DRBG_BLOCKSIZE ) /**< The seed length, calculated as (counter + AES key). */ 99 100 /** 101 * \name SECTION: Module settings 102 * 103 * The configuration options you can set for this module are in this section. 104 * Either change them in config.h or define them using the compiler command 105 * line. 106 * \{ 107 */ 108 109 /** \def MBEDTLS_CTR_DRBG_ENTROPY_LEN 110 * 111 * \brief The amount of entropy used per seed by default, in bytes. 112 */ 113 #if !defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN) 114 #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256) 115 /** This is 48 bytes because the entropy module uses SHA-512 116 * (\c MBEDTLS_ENTROPY_FORCE_SHA256 is disabled). 117 */ 118 #define MBEDTLS_CTR_DRBG_ENTROPY_LEN 48 119 120 #else /* defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256) */ 121 122 /** This is 32 bytes because the entropy module uses SHA-256 123 * (the SHA512 module is disabled or 124 * \c MBEDTLS_ENTROPY_FORCE_SHA256 is enabled). 125 */ 126 #if !defined(MBEDTLS_CTR_DRBG_USE_128_BIT_KEY) 127 /** \warning To achieve a 256-bit security strength, you must pass a nonce 128 * to mbedtls_ctr_drbg_seed(). 129 */ 130 #endif /* !defined(MBEDTLS_CTR_DRBG_USE_128_BIT_KEY) */ 131 #define MBEDTLS_CTR_DRBG_ENTROPY_LEN 32 132 #endif /* defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256) */ 133 #endif /* !defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN) */ 134 135 #if !defined(MBEDTLS_CTR_DRBG_RESEED_INTERVAL) 136 #define MBEDTLS_CTR_DRBG_RESEED_INTERVAL 10000 137 /**< The interval before reseed is performed by default. */ 138 #endif 139 140 #if !defined(MBEDTLS_CTR_DRBG_MAX_INPUT) 141 #define MBEDTLS_CTR_DRBG_MAX_INPUT 256 142 /**< The maximum number of additional input Bytes. */ 143 #endif 144 145 #if !defined(MBEDTLS_CTR_DRBG_MAX_REQUEST) 146 #define MBEDTLS_CTR_DRBG_MAX_REQUEST 1024 147 /**< The maximum number of requested Bytes per call. */ 148 #endif 149 150 #if !defined(MBEDTLS_CTR_DRBG_MAX_SEED_INPUT) 151 #define MBEDTLS_CTR_DRBG_MAX_SEED_INPUT 384 152 /**< The maximum size of seed or reseed buffer in bytes. */ 153 #endif 154 155 /* \} name SECTION: Module settings */ 156 157 #define MBEDTLS_CTR_DRBG_PR_OFF 0 158 /**< Prediction resistance is disabled. */ 159 #define MBEDTLS_CTR_DRBG_PR_ON 1 160 /**< Prediction resistance is enabled. */ 161 162 #ifdef __cplusplus 163 extern "C" { 164 #endif 165 166 /** 167 * \brief The CTR_DRBG context structure. 168 */ 169 typedef struct mbedtls_ctr_drbg_context 170 { 171 unsigned char counter[16]; /*!< The counter (V). */ 172 int reseed_counter; /*!< The reseed counter. */ 173 int prediction_resistance; /*!< This determines whether prediction 174 resistance is enabled, that is 175 whether to systematically reseed before 176 each random generation. */ 177 size_t entropy_len; /*!< The amount of entropy grabbed on each 178 seed or reseed operation. */ 179 int reseed_interval; /*!< The reseed interval. */ 180 181 mbedtls_aes_context aes_ctx; /*!< The AES context. */ 182 183 /* 184 * Callbacks (Entropy) 185 */ 186 int (*f_entropy)(void *, unsigned char *, size_t); 187 /*!< The entropy callback function. */ 188 189 void *p_entropy; /*!< The context for the entropy function. */ 190 191 #if defined(MBEDTLS_THREADING_C) 192 mbedtls_threading_mutex_t mutex; 193 #endif 194 } 195 mbedtls_ctr_drbg_context; 196 197 /** 198 * \brief This function initializes the CTR_DRBG context, 199 * and prepares it for mbedtls_ctr_drbg_seed() 200 * or mbedtls_ctr_drbg_free(). 201 * 202 * \param ctx The CTR_DRBG context to initialize. 203 */ 204 void mbedtls_ctr_drbg_init( mbedtls_ctr_drbg_context *ctx ); 205 206 /** 207 * \brief This function seeds and sets up the CTR_DRBG 208 * entropy source for future reseeds. 209 * 210 * A typical choice for the \p f_entropy and \p p_entropy parameters is 211 * to use the entropy module: 212 * - \p f_entropy is mbedtls_entropy_func(); 213 * - \p p_entropy is an instance of ::mbedtls_entropy_context initialized 214 * with mbedtls_entropy_init() (which registers the platform's default 215 * entropy sources). 216 * 217 * The entropy length is #MBEDTLS_CTR_DRBG_ENTROPY_LEN by default. 218 * You can override it by calling mbedtls_ctr_drbg_set_entropy_len(). 219 * 220 * You can provide a personalization string in addition to the 221 * entropy source, to make this instantiation as unique as possible. 222 * 223 * \note The _seed_material_ value passed to the derivation 224 * function in the CTR_DRBG Instantiate Process 225 * described in NIST SP 800-90A §10.2.1.3.2 226 * is the concatenation of the string obtained from 227 * calling \p f_entropy and the \p custom string. 228 * The origin of the nonce depends on the value of 229 * the entropy length relative to the security strength. 230 * - If the entropy length is at least 1.5 times the 231 * security strength then the nonce is taken from the 232 * string obtained with \p f_entropy. 233 * - If the entropy length is less than the security 234 * strength, then the nonce is taken from \p custom. 235 * In this case, for compliance with SP 800-90A, 236 * you must pass a unique value of \p custom at 237 * each invocation. See SP 800-90A §8.6.7 for more 238 * details. 239 */ 240 #if MBEDTLS_CTR_DRBG_ENTROPY_LEN < MBEDTLS_CTR_DRBG_KEYSIZE * 3 / 2 241 /** \warning When #MBEDTLS_CTR_DRBG_ENTROPY_LEN is less than 242 * #MBEDTLS_CTR_DRBG_KEYSIZE * 3 / 2, to achieve the 243 * maximum security strength permitted by CTR_DRBG, 244 * you must pass a value of \p custom that is a nonce: 245 * this value must never be repeated in subsequent 246 * runs of the same application or on a different 247 * device. 248 */ 249 #endif 250 /** 251 * \param ctx The CTR_DRBG context to seed. 252 * It must have been initialized with 253 * mbedtls_ctr_drbg_init(). 254 * After a successful call to mbedtls_ctr_drbg_seed(), 255 * you may not call mbedtls_ctr_drbg_seed() again on 256 * the same context unless you call 257 * mbedtls_ctr_drbg_free() and mbedtls_ctr_drbg_init() 258 * again first. 259 * \param f_entropy The entropy callback, taking as arguments the 260 * \p p_entropy context, the buffer to fill, and the 261 * length of the buffer. 262 * \p f_entropy is always called with a buffer size 263 * equal to the entropy length. 264 * \param p_entropy The entropy context to pass to \p f_entropy. 265 * \param custom The personalization string. 266 * This can be \c NULL, in which case the personalization 267 * string is empty regardless of the value of \p len. 268 * \param len The length of the personalization string. 269 * This must be at most 270 * #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT 271 * - #MBEDTLS_CTR_DRBG_ENTROPY_LEN. 272 * 273 * \return \c 0 on success. 274 * \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED on failure. 275 */ 276 int mbedtls_ctr_drbg_seed( mbedtls_ctr_drbg_context *ctx, 277 int (*f_entropy)(void *, unsigned char *, size_t), 278 void *p_entropy, 279 const unsigned char *custom, 280 size_t len ); 281 282 /** 283 * \brief This function clears CTR_CRBG context data. 284 * 285 * \param ctx The CTR_DRBG context to clear. 286 */ 287 void mbedtls_ctr_drbg_free( mbedtls_ctr_drbg_context *ctx ); 288 289 /** 290 * \brief This function turns prediction resistance on or off. 291 * The default value is off. 292 * 293 * \note If enabled, entropy is gathered at the beginning of 294 * every call to mbedtls_ctr_drbg_random_with_add() 295 * or mbedtls_ctr_drbg_random(). 296 * Only use this if your entropy source has sufficient 297 * throughput. 298 * 299 * \param ctx The CTR_DRBG context. 300 * \param resistance #MBEDTLS_CTR_DRBG_PR_ON or #MBEDTLS_CTR_DRBG_PR_OFF. 301 */ 302 void mbedtls_ctr_drbg_set_prediction_resistance( mbedtls_ctr_drbg_context *ctx, 303 int resistance ); 304 305 /** 306 * \brief This function sets the amount of entropy grabbed on each 307 * seed or reseed. 308 * 309 * The default value is #MBEDTLS_CTR_DRBG_ENTROPY_LEN. 310 * 311 * \note The security strength of CTR_DRBG is bounded by the 312 * entropy length. Thus: 313 * - When using AES-256 314 * (\c MBEDTLS_CTR_DRBG_USE_128_BIT_KEY is disabled, 315 * which is the default), 316 * \p len must be at least 32 (in bytes) 317 * to achieve a 256-bit strength. 318 * - When using AES-128 319 * (\c MBEDTLS_CTR_DRBG_USE_128_BIT_KEY is enabled) 320 * \p len must be at least 16 (in bytes) 321 * to achieve a 128-bit strength. 322 * 323 * \param ctx The CTR_DRBG context. 324 * \param len The amount of entropy to grab, in bytes. 325 * This must be at most #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT. 326 */ 327 void mbedtls_ctr_drbg_set_entropy_len( mbedtls_ctr_drbg_context *ctx, 328 size_t len ); 329 330 /** 331 * \brief This function sets the reseed interval. 332 * 333 * The reseed interval is the number of calls to mbedtls_ctr_drbg_random() 334 * or mbedtls_ctr_drbg_random_with_add() after which the entropy function 335 * is called again. 336 * 337 * The default value is #MBEDTLS_CTR_DRBG_RESEED_INTERVAL. 338 * 339 * \param ctx The CTR_DRBG context. 340 * \param interval The reseed interval. 341 */ 342 void mbedtls_ctr_drbg_set_reseed_interval( mbedtls_ctr_drbg_context *ctx, 343 int interval ); 344 345 /** 346 * \brief This function reseeds the CTR_DRBG context, that is 347 * extracts data from the entropy source. 348 * 349 * \param ctx The CTR_DRBG context. 350 * \param additional Additional data to add to the state. Can be \c NULL. 351 * \param len The length of the additional data. 352 * This must be less than 353 * #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT - \c entropy_len 354 * where \c entropy_len is the entropy length 355 * configured for the context. 356 * 357 * \return \c 0 on success. 358 * \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED on failure. 359 */ 360 int mbedtls_ctr_drbg_reseed( mbedtls_ctr_drbg_context *ctx, 361 const unsigned char *additional, size_t len ); 362 363 /** 364 * \brief This function updates the state of the CTR_DRBG context. 365 * 366 * \param ctx The CTR_DRBG context. 367 * \param additional The data to update the state with. This must not be 368 * \c NULL unless \p add_len is \c 0. 369 * \param add_len Length of \p additional in bytes. This must be at 370 * most #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT. 371 * 372 * \return \c 0 on success. 373 * \return #MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG if 374 * \p add_len is more than 375 * #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT. 376 * \return An error from the underlying AES cipher on failure. 377 */ 378 int mbedtls_ctr_drbg_update_ret( mbedtls_ctr_drbg_context *ctx, 379 const unsigned char *additional, 380 size_t add_len ); 381 382 /** 383 * \brief This function updates a CTR_DRBG instance with additional 384 * data and uses it to generate random data. 385 * 386 * This function automatically reseeds if the reseed counter is exceeded 387 * or prediction resistance is enabled. 388 * 389 * \param p_rng The CTR_DRBG context. This must be a pointer to a 390 * #mbedtls_ctr_drbg_context structure. 391 * \param output The buffer to fill. 392 * \param output_len The length of the buffer in bytes. 393 * \param additional Additional data to update. Can be \c NULL, in which 394 * case the additional data is empty regardless of 395 * the value of \p add_len. 396 * \param add_len The length of the additional data 397 * if \p additional is not \c NULL. 398 * This must be less than #MBEDTLS_CTR_DRBG_MAX_INPUT 399 * and less than 400 * #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT - \c entropy_len 401 * where \c entropy_len is the entropy length 402 * configured for the context. 403 * 404 * \return \c 0 on success. 405 * \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or 406 * #MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG on failure. 407 */ 408 int mbedtls_ctr_drbg_random_with_add( void *p_rng, 409 unsigned char *output, size_t output_len, 410 const unsigned char *additional, size_t add_len ); 411 412 /** 413 * \brief This function uses CTR_DRBG to generate random data. 414 * 415 * This function automatically reseeds if the reseed counter is exceeded 416 * or prediction resistance is enabled. 417 * 418 * 419 * \param p_rng The CTR_DRBG context. This must be a pointer to a 420 * #mbedtls_ctr_drbg_context structure. 421 * \param output The buffer to fill. 422 * \param output_len The length of the buffer in bytes. 423 * 424 * \return \c 0 on success. 425 * \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or 426 * #MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG on failure. 427 */ 428 int mbedtls_ctr_drbg_random( void *p_rng, 429 unsigned char *output, size_t output_len ); 430 431 432 #if ! defined(MBEDTLS_DEPRECATED_REMOVED) 433 #if defined(MBEDTLS_DEPRECATED_WARNING) 434 #define MBEDTLS_DEPRECATED __attribute__((deprecated)) 435 #else 436 #define MBEDTLS_DEPRECATED 437 #endif 438 /** 439 * \brief This function updates the state of the CTR_DRBG context. 440 * 441 * \deprecated Superseded by mbedtls_ctr_drbg_update_ret() 442 * in 2.16.0. 443 * 444 * \note If \p add_len is greater than 445 * #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT, only the first 446 * #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT Bytes are used. 447 * The remaining Bytes are silently discarded. 448 * 449 * \param ctx The CTR_DRBG context. 450 * \param additional The data to update the state with. 451 * \param add_len Length of \p additional data. 452 */ 453 MBEDTLS_DEPRECATED void mbedtls_ctr_drbg_update( 454 mbedtls_ctr_drbg_context *ctx, 455 const unsigned char *additional, 456 size_t add_len ); 457 #undef MBEDTLS_DEPRECATED 458 #endif /* !MBEDTLS_DEPRECATED_REMOVED */ 459 460 #if defined(MBEDTLS_FS_IO) 461 /** 462 * \brief This function writes a seed file. 463 * 464 * \param ctx The CTR_DRBG context. 465 * \param path The name of the file. 466 * 467 * \return \c 0 on success. 468 * \return #MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR on file error. 469 * \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED on reseed 470 * failure. 471 */ 472 int mbedtls_ctr_drbg_write_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path ); 473 474 /** 475 * \brief This function reads and updates a seed file. The seed 476 * is added to this instance. 477 * 478 * \param ctx The CTR_DRBG context. 479 * \param path The name of the file. 480 * 481 * \return \c 0 on success. 482 * \return #MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR on file error. 483 * \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED on 484 * reseed failure. 485 * \return #MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG if the existing 486 * seed file is too large. 487 */ 488 int mbedtls_ctr_drbg_update_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path ); 489 #endif /* MBEDTLS_FS_IO */ 490 491 #if defined(MBEDTLS_SELF_TEST) 492 493 /** 494 * \brief The CTR_DRBG checkup routine. 495 * 496 * \return \c 0 on success. 497 * \return \c 1 on failure. 498 */ 499 int mbedtls_ctr_drbg_self_test( int verbose ); 500 501 #endif /* MBEDTLS_SELF_TEST */ 502 503 /* Internal functions (do not call directly) */ 504 int mbedtls_ctr_drbg_seed_entropy_len( mbedtls_ctr_drbg_context *, 505 int (*)(void *, unsigned char *, size_t), void *, 506 const unsigned char *, size_t, size_t ); 507 508 #ifdef __cplusplus 509 } 510 #endif 511 512 #endif /* ctr_drbg.h */ 513