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