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