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