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