Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 1 | /** |
| 2 | * \file hmac_drbg.h |
| 3 | * |
| 4 | * \brief HMAC_DRBG (NIST SP 800-90A) |
Darryl Green | a40a101 | 2018-01-05 15:33:17 +0000 | [diff] [blame] | 5 | */ |
| 6 | /* |
Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 7 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 8 | * SPDX-License-Identifier: Apache-2.0 |
| 9 | * |
| 10 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 11 | * not use this file except in compliance with the License. |
| 12 | * You may obtain a copy of the License at |
| 13 | * |
| 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 15 | * |
| 16 | * Unless required by applicable law or agreed to in writing, software |
| 17 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 18 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 19 | * See the License for the specific language governing permissions and |
| 20 | * limitations under the License. |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 21 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 22 | * This file is part of mbed TLS (https://tls.mbed.org) |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 23 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 24 | #ifndef MBEDTLS_HMAC_DRBG_H |
| 25 | #define MBEDTLS_HMAC_DRBG_H |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 26 | |
Ron Eldor | 0559c66 | 2018-02-14 16:02:41 +0200 | [diff] [blame^] | 27 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 28 | #include "config.h" |
| 29 | #else |
| 30 | #include MBEDTLS_CONFIG_FILE |
| 31 | #endif |
| 32 | |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 33 | #include "md.h" |
| 34 | |
Manuel Pégourié-Gonnard | 0a4fb09 | 2015-05-07 12:50:31 +0100 | [diff] [blame] | 35 | #if defined(MBEDTLS_THREADING_C) |
Ron Eldor | df9b93e | 2017-05-14 16:17:33 +0300 | [diff] [blame] | 36 | #include "threading.h" |
Manuel Pégourié-Gonnard | 0a4fb09 | 2015-05-07 12:50:31 +0100 | [diff] [blame] | 37 | #endif |
| 38 | |
Manuel Pégourié-Gonnard | fe34a5f | 2014-01-30 15:06:40 +0100 | [diff] [blame] | 39 | /* |
Manuel Pégourié-Gonnard | 9a6e93e | 2014-03-11 09:34:02 +0100 | [diff] [blame] | 40 | * Error codes |
Manuel Pégourié-Gonnard | fe34a5f | 2014-01-30 15:06:40 +0100 | [diff] [blame] | 41 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 42 | #define MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG -0x0003 /**< Too many random requested in single call. */ |
| 43 | #define MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG -0x0005 /**< Input too large (Entropy + additional). */ |
| 44 | #define MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR -0x0007 /**< Read/write error in file. */ |
| 45 | #define MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED -0x0009 /**< The entropy source failed. */ |
Manuel Pégourié-Gonnard | fe34a5f | 2014-01-30 15:06:40 +0100 | [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. |
| 51 | * Either change them in config.h or define them on the compiler command line. |
| 52 | * \{ |
| 53 | */ |
| 54 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 55 | #if !defined(MBEDTLS_HMAC_DRBG_RESEED_INTERVAL) |
| 56 | #define MBEDTLS_HMAC_DRBG_RESEED_INTERVAL 10000 /**< Interval before reseed is performed by default */ |
Paul Bakker | 088c5c5 | 2014-04-25 11:11:10 +0200 | [diff] [blame] | 57 | #endif |
| 58 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 59 | #if !defined(MBEDTLS_HMAC_DRBG_MAX_INPUT) |
| 60 | #define MBEDTLS_HMAC_DRBG_MAX_INPUT 256 /**< Maximum number of additional input bytes */ |
Paul Bakker | 088c5c5 | 2014-04-25 11:11:10 +0200 | [diff] [blame] | 61 | #endif |
| 62 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 63 | #if !defined(MBEDTLS_HMAC_DRBG_MAX_REQUEST) |
| 64 | #define MBEDTLS_HMAC_DRBG_MAX_REQUEST 1024 /**< Maximum number of requested bytes per call */ |
Paul Bakker | 088c5c5 | 2014-04-25 11:11:10 +0200 | [diff] [blame] | 65 | #endif |
| 66 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 67 | #if !defined(MBEDTLS_HMAC_DRBG_MAX_SEED_INPUT) |
| 68 | #define MBEDTLS_HMAC_DRBG_MAX_SEED_INPUT 384 /**< Maximum size of (re)seed buffer */ |
Paul Bakker | 088c5c5 | 2014-04-25 11:11:10 +0200 | [diff] [blame] | 69 | #endif |
| 70 | |
| 71 | /* \} name SECTION: Module settings */ |
Manuel Pégourié-Gonnard | fe34a5f | 2014-01-30 15:06:40 +0100 | [diff] [blame] | 72 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 73 | #define MBEDTLS_HMAC_DRBG_PR_OFF 0 /**< No prediction resistance */ |
| 74 | #define MBEDTLS_HMAC_DRBG_PR_ON 1 /**< Prediction resistance enabled */ |
Manuel Pégourié-Gonnard | af786ff | 2014-01-30 18:44:18 +0100 | [diff] [blame] | 75 | |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 76 | #ifdef __cplusplus |
| 77 | extern "C" { |
| 78 | #endif |
| 79 | |
Manuel Pégourié-Gonnard | fe34a5f | 2014-01-30 15:06:40 +0100 | [diff] [blame] | 80 | /** |
| 81 | * HMAC_DRBG context. |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 82 | */ |
| 83 | typedef struct |
| 84 | { |
Manuel Pégourié-Gonnard | b05db2a | 2014-02-01 11:38:05 +0100 | [diff] [blame] | 85 | /* Working state: the key K is not stored explicitely, |
| 86 | * but is implied by the HMAC context */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 87 | mbedtls_md_context_t md_ctx; /*!< HMAC context (inc. K) */ |
| 88 | unsigned char V[MBEDTLS_MD_MAX_SIZE]; /*!< V in the spec */ |
Manuel Pégourié-Gonnard | b05db2a | 2014-02-01 11:38:05 +0100 | [diff] [blame] | 89 | int reseed_counter; /*!< reseed counter */ |
Manuel Pégourié-Gonnard | fe34a5f | 2014-01-30 15:06:40 +0100 | [diff] [blame] | 90 | |
Manuel Pégourié-Gonnard | 658dbed | 2014-01-30 19:03:45 +0100 | [diff] [blame] | 91 | /* Administrative state */ |
Manuel Pégourié-Gonnard | fe34a5f | 2014-01-30 15:06:40 +0100 | [diff] [blame] | 92 | size_t entropy_len; /*!< entropy bytes grabbed on each (re)seed */ |
Manuel Pégourié-Gonnard | af786ff | 2014-01-30 18:44:18 +0100 | [diff] [blame] | 93 | int prediction_resistance; /*!< enable prediction resistance (Automatic |
| 94 | reseed before every random generation) */ |
Manuel Pégourié-Gonnard | 658dbed | 2014-01-30 19:03:45 +0100 | [diff] [blame] | 95 | int reseed_interval; /*!< reseed interval */ |
Manuel Pégourié-Gonnard | fe34a5f | 2014-01-30 15:06:40 +0100 | [diff] [blame] | 96 | |
Manuel Pégourié-Gonnard | 658dbed | 2014-01-30 19:03:45 +0100 | [diff] [blame] | 97 | /* Callbacks */ |
Manuel Pégourié-Gonnard | af786ff | 2014-01-30 18:44:18 +0100 | [diff] [blame] | 98 | int (*f_entropy)(void *, unsigned char *, size_t); /*!< entropy function */ |
| 99 | void *p_entropy; /*!< context for the entropy function */ |
Manuel Pégourié-Gonnard | 0a4fb09 | 2015-05-07 12:50:31 +0100 | [diff] [blame] | 100 | |
| 101 | #if defined(MBEDTLS_THREADING_C) |
| 102 | mbedtls_threading_mutex_t mutex; |
| 103 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 104 | } mbedtls_hmac_drbg_context; |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 105 | |
| 106 | /** |
Manuel Pégourié-Gonnard | 8d128ef | 2015-04-28 22:38:08 +0200 | [diff] [blame] | 107 | * \brief HMAC_DRBG context initialization |
Tillmann Karras | 588ad50 | 2015-09-25 04:27:22 +0200 | [diff] [blame] | 108 | * Makes the context ready for mbedtls_hmac_drbg_seed(), |
Manuel Pégourié-Gonnard | f9e9481 | 2015-04-28 22:07:14 +0200 | [diff] [blame] | 109 | * mbedtls_hmac_drbg_seed_buf() or |
| 110 | * mbedtls_hmac_drbg_free(). |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 111 | * |
Manuel Pégourié-Gonnard | f9e9481 | 2015-04-28 22:07:14 +0200 | [diff] [blame] | 112 | * \param ctx HMAC_DRBG context to be initialized |
| 113 | */ |
| 114 | void mbedtls_hmac_drbg_init( mbedtls_hmac_drbg_context *ctx ); |
| 115 | |
| 116 | /** |
| 117 | * \brief HMAC_DRBG initial seeding |
Manuel Pégourié-Gonnard | 8d128ef | 2015-04-28 22:38:08 +0200 | [diff] [blame] | 118 | * Seed and setup entropy source for future reseeds. |
Manuel Pégourié-Gonnard | f9e9481 | 2015-04-28 22:07:14 +0200 | [diff] [blame] | 119 | * |
| 120 | * \param ctx HMAC_DRBG context to be seeded |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 121 | * \param md_info MD algorithm to use for HMAC_DRBG |
Manuel Pégourié-Gonnard | fe34a5f | 2014-01-30 15:06:40 +0100 | [diff] [blame] | 122 | * \param f_entropy Entropy callback (p_entropy, buffer to fill, buffer |
| 123 | * length) |
| 124 | * \param p_entropy Entropy context |
| 125 | * \param custom Personalization data (Device specific identifiers) |
| 126 | * (Can be NULL) |
| 127 | * \param len Length of personalization data |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 128 | * |
Manuel Pégourié-Gonnard | fe34a5f | 2014-01-30 15:06:40 +0100 | [diff] [blame] | 129 | * \note The "security strength" as defined by NIST is set to: |
| 130 | * 128 bits if md_alg is SHA-1, |
| 131 | * 192 bits if md_alg is SHA-224, |
| 132 | * 256 bits if md_alg is SHA-256 or higher. |
| 133 | * Note that SHA-256 is just as efficient as SHA-224. |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 134 | * |
| 135 | * \return 0 if successful, or |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 136 | * MBEDTLS_ERR_MD_BAD_INPUT_DATA, or |
| 137 | * MBEDTLS_ERR_MD_ALLOC_FAILED, or |
Manuel Pégourié-Gonnard | f9e9481 | 2015-04-28 22:07:14 +0200 | [diff] [blame] | 138 | * MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED. |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 139 | */ |
Manuel Pégourié-Gonnard | f9e9481 | 2015-04-28 22:07:14 +0200 | [diff] [blame] | 140 | int mbedtls_hmac_drbg_seed( mbedtls_hmac_drbg_context *ctx, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 141 | const mbedtls_md_info_t * md_info, |
Manuel Pégourié-Gonnard | fe34a5f | 2014-01-30 15:06:40 +0100 | [diff] [blame] | 142 | int (*f_entropy)(void *, unsigned char *, size_t), |
| 143 | void *p_entropy, |
| 144 | const unsigned char *custom, |
| 145 | size_t len ); |
| 146 | |
| 147 | /** |
Manuel Pégourié-Gonnard | 4e669c6 | 2014-01-30 18:06:08 +0100 | [diff] [blame] | 148 | * \brief Initilisation of simpified HMAC_DRBG (never reseeds). |
Manuel Pégourié-Gonnard | fe34a5f | 2014-01-30 15:06:40 +0100 | [diff] [blame] | 149 | * (For use with deterministic ECDSA.) |
| 150 | * |
| 151 | * \param ctx HMAC_DRBG context to be initialised |
| 152 | * \param md_info MD algorithm to use for HMAC_DRBG |
| 153 | * \param data Concatenation of entropy string and additional data |
| 154 | * \param data_len Length of data in bytes |
| 155 | * |
| 156 | * \return 0 if successful, or |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 157 | * MBEDTLS_ERR_MD_BAD_INPUT_DATA, or |
| 158 | * MBEDTLS_ERR_MD_ALLOC_FAILED. |
Manuel Pégourié-Gonnard | fe34a5f | 2014-01-30 15:06:40 +0100 | [diff] [blame] | 159 | */ |
Manuel Pégourié-Gonnard | f9e9481 | 2015-04-28 22:07:14 +0200 | [diff] [blame] | 160 | int mbedtls_hmac_drbg_seed_buf( mbedtls_hmac_drbg_context *ctx, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 161 | const mbedtls_md_info_t * md_info, |
Manuel Pégourié-Gonnard | fe34a5f | 2014-01-30 15:06:40 +0100 | [diff] [blame] | 162 | const unsigned char *data, size_t data_len ); |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 163 | |
| 164 | /** |
Manuel Pégourié-Gonnard | af786ff | 2014-01-30 18:44:18 +0100 | [diff] [blame] | 165 | * \brief Enable / disable prediction resistance (Default: Off) |
| 166 | * |
| 167 | * Note: If enabled, entropy is used for ctx->entropy_len before each call! |
| 168 | * Only use this if you have ample supply of good entropy! |
| 169 | * |
| 170 | * \param ctx HMAC_DRBG context |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 171 | * \param resistance MBEDTLS_HMAC_DRBG_PR_ON or MBEDTLS_HMAC_DRBG_PR_OFF |
Manuel Pégourié-Gonnard | af786ff | 2014-01-30 18:44:18 +0100 | [diff] [blame] | 172 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 173 | void mbedtls_hmac_drbg_set_prediction_resistance( mbedtls_hmac_drbg_context *ctx, |
Manuel Pégourié-Gonnard | af786ff | 2014-01-30 18:44:18 +0100 | [diff] [blame] | 174 | int resistance ); |
| 175 | |
| 176 | /** |
Manuel Pégourié-Gonnard | 4e669c6 | 2014-01-30 18:06:08 +0100 | [diff] [blame] | 177 | * \brief Set the amount of entropy grabbed on each reseed |
Manuel Pégourié-Gonnard | 658dbed | 2014-01-30 19:03:45 +0100 | [diff] [blame] | 178 | * (Default: given by the security strength, which |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 179 | * depends on the hash used, see \c mbedtls_hmac_drbg_init() ) |
Manuel Pégourié-Gonnard | 4e669c6 | 2014-01-30 18:06:08 +0100 | [diff] [blame] | 180 | * |
| 181 | * \param ctx HMAC_DRBG context |
Manuel Pégourié-Gonnard | 658dbed | 2014-01-30 19:03:45 +0100 | [diff] [blame] | 182 | * \param len Amount of entropy to grab, in bytes |
Manuel Pégourié-Gonnard | 4e669c6 | 2014-01-30 18:06:08 +0100 | [diff] [blame] | 183 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 184 | void mbedtls_hmac_drbg_set_entropy_len( mbedtls_hmac_drbg_context *ctx, |
Manuel Pégourié-Gonnard | 4e669c6 | 2014-01-30 18:06:08 +0100 | [diff] [blame] | 185 | size_t len ); |
| 186 | |
| 187 | /** |
Manuel Pégourié-Gonnard | 658dbed | 2014-01-30 19:03:45 +0100 | [diff] [blame] | 188 | * \brief Set the reseed interval |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 189 | * (Default: MBEDTLS_HMAC_DRBG_RESEED_INTERVAL) |
Manuel Pégourié-Gonnard | 658dbed | 2014-01-30 19:03:45 +0100 | [diff] [blame] | 190 | * |
| 191 | * \param ctx HMAC_DRBG context |
| 192 | * \param interval Reseed interval |
| 193 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 194 | void mbedtls_hmac_drbg_set_reseed_interval( mbedtls_hmac_drbg_context *ctx, |
Manuel Pégourié-Gonnard | 658dbed | 2014-01-30 19:03:45 +0100 | [diff] [blame] | 195 | int interval ); |
| 196 | |
| 197 | /** |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 198 | * \brief HMAC_DRBG update state |
| 199 | * |
| 200 | * \param ctx HMAC_DRBG context |
| 201 | * \param additional Additional data to update state with, or NULL |
| 202 | * \param add_len Length of additional data, or 0 |
| 203 | * |
Gilles Peskine | 4d23757 | 2018-09-13 22:19:57 +0200 | [diff] [blame] | 204 | * \return \c 0 on success, or an error from the underlying |
| 205 | * hash calculation. |
| 206 | * |
| 207 | * \note Additional data is optional, pass NULL and 0 as second |
| 208 | * third argument if no additional data is being used. |
| 209 | */ |
| 210 | int mbedtls_hmac_drbg_update_ret( mbedtls_hmac_drbg_context *ctx, |
| 211 | const unsigned char *additional, size_t add_len ); |
| 212 | |
| 213 | /** |
| 214 | * \brief HMAC_DRBG update state |
| 215 | * |
| 216 | * \warning This function cannot report errors. You should use |
| 217 | * mbedtls_hmac_drbg_update_ret() instead. |
| 218 | * |
| 219 | * \param ctx HMAC_DRBG context |
| 220 | * \param additional Additional data to update state with, or NULL |
| 221 | * \param add_len Length of additional data, or 0 |
| 222 | * |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 223 | * \note Additional data is optional, pass NULL and 0 as second |
| 224 | * third argument if no additional data is being used. |
| 225 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 226 | void mbedtls_hmac_drbg_update( mbedtls_hmac_drbg_context *ctx, |
Gilles Peskine | 4d23757 | 2018-09-13 22:19:57 +0200 | [diff] [blame] | 227 | const unsigned char *additional, |
| 228 | size_t add_len ); |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 229 | |
| 230 | /** |
Manuel Pégourié-Gonnard | 8fc484d | 2014-01-30 18:28:09 +0100 | [diff] [blame] | 231 | * \brief HMAC_DRBG reseeding (extracts data from entropy source) |
| 232 | * |
| 233 | * \param ctx HMAC_DRBG context |
| 234 | * \param additional Additional data to add to state (Can be NULL) |
| 235 | * \param len Length of additional data |
| 236 | * |
| 237 | * \return 0 if successful, or |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 238 | * MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED |
Manuel Pégourié-Gonnard | 8fc484d | 2014-01-30 18:28:09 +0100 | [diff] [blame] | 239 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 240 | int mbedtls_hmac_drbg_reseed( mbedtls_hmac_drbg_context *ctx, |
Manuel Pégourié-Gonnard | 8fc484d | 2014-01-30 18:28:09 +0100 | [diff] [blame] | 241 | const unsigned char *additional, size_t len ); |
| 242 | |
| 243 | /** |
Manuel Pégourié-Gonnard | 8208d16 | 2014-01-30 12:19:26 +0100 | [diff] [blame] | 244 | * \brief HMAC_DRBG generate random with additional update input |
| 245 | * |
Manuel Pégourié-Gonnard | 658dbed | 2014-01-30 19:03:45 +0100 | [diff] [blame] | 246 | * Note: Automatically reseeds if reseed_counter is reached or PR is enabled. |
Manuel Pégourié-Gonnard | 8208d16 | 2014-01-30 12:19:26 +0100 | [diff] [blame] | 247 | * |
| 248 | * \param p_rng HMAC_DRBG context |
| 249 | * \param output Buffer to fill |
| 250 | * \param output_len Length of the buffer |
| 251 | * \param additional Additional data to update with (can be NULL) |
| 252 | * \param add_len Length of additional data (can be 0) |
| 253 | * |
| 254 | * \return 0 if successful, or |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 255 | * MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED, or |
| 256 | * MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG, or |
| 257 | * MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG. |
Manuel Pégourié-Gonnard | 8208d16 | 2014-01-30 12:19:26 +0100 | [diff] [blame] | 258 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 259 | int mbedtls_hmac_drbg_random_with_add( void *p_rng, |
Manuel Pégourié-Gonnard | 8208d16 | 2014-01-30 12:19:26 +0100 | [diff] [blame] | 260 | unsigned char *output, size_t output_len, |
| 261 | const unsigned char *additional, |
| 262 | size_t add_len ); |
| 263 | |
| 264 | /** |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 265 | * \brief HMAC_DRBG generate random |
| 266 | * |
Manuel Pégourié-Gonnard | 658dbed | 2014-01-30 19:03:45 +0100 | [diff] [blame] | 267 | * Note: Automatically reseeds if reseed_counter is reached or PR is enabled. |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 268 | * |
| 269 | * \param p_rng HMAC_DRBG context |
| 270 | * \param output Buffer to fill |
Manuel Pégourié-Gonnard | f6a17d0 | 2014-01-31 11:51:42 +0100 | [diff] [blame] | 271 | * \param out_len Length of the buffer |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 272 | * |
Manuel Pégourié-Gonnard | 8208d16 | 2014-01-30 12:19:26 +0100 | [diff] [blame] | 273 | * \return 0 if successful, or |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 274 | * MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED, or |
| 275 | * MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 276 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 277 | int mbedtls_hmac_drbg_random( void *p_rng, unsigned char *output, size_t out_len ); |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 278 | |
| 279 | /** |
| 280 | * \brief Free an HMAC_DRBG context |
| 281 | * |
| 282 | * \param ctx HMAC_DRBG context to free. |
| 283 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 284 | void mbedtls_hmac_drbg_free( mbedtls_hmac_drbg_context *ctx ); |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 285 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 286 | #if defined(MBEDTLS_FS_IO) |
Manuel Pégourié-Gonnard | 48bc3e8 | 2014-01-30 21:11:16 +0100 | [diff] [blame] | 287 | /** |
| 288 | * \brief Write a seed file |
| 289 | * |
| 290 | * \param ctx HMAC_DRBG context |
| 291 | * \param path Name of the file |
| 292 | * |
| 293 | * \return 0 if successful, 1 on file error, or |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 294 | * MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED |
Manuel Pégourié-Gonnard | 48bc3e8 | 2014-01-30 21:11:16 +0100 | [diff] [blame] | 295 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 296 | int mbedtls_hmac_drbg_write_seed_file( mbedtls_hmac_drbg_context *ctx, const char *path ); |
Manuel Pégourié-Gonnard | 48bc3e8 | 2014-01-30 21:11:16 +0100 | [diff] [blame] | 297 | |
| 298 | /** |
| 299 | * \brief Read and update a seed file. Seed is added to this |
| 300 | * instance |
| 301 | * |
| 302 | * \param ctx HMAC_DRBG context |
| 303 | * \param path Name of the file |
| 304 | * |
| 305 | * \return 0 if successful, 1 on file error, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 306 | * MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED or |
| 307 | * MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG |
Manuel Pégourié-Gonnard | 48bc3e8 | 2014-01-30 21:11:16 +0100 | [diff] [blame] | 308 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 309 | int mbedtls_hmac_drbg_update_seed_file( mbedtls_hmac_drbg_context *ctx, const char *path ); |
| 310 | #endif /* MBEDTLS_FS_IO */ |
Manuel Pégourié-Gonnard | 48bc3e8 | 2014-01-30 21:11:16 +0100 | [diff] [blame] | 311 | |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 312 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 313 | #if defined(MBEDTLS_SELF_TEST) |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 314 | /** |
| 315 | * \brief Checkup routine |
| 316 | * |
| 317 | * \return 0 if successful, or 1 if the test failed |
| 318 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 319 | int mbedtls_hmac_drbg_self_test( int verbose ); |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 320 | #endif |
| 321 | |
| 322 | #ifdef __cplusplus |
| 323 | } |
| 324 | #endif |
| 325 | |
| 326 | #endif /* hmac_drbg.h */ |