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