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 | 8b0cf2e | 2018-02-14 16:02:41 +0200 | [diff] [blame] | 27 | #if !defined(MBEDTLS_CONFIG_FILE) |
Jaeden Amero | c49fbbf | 2019-07-04 20:01:14 +0100 | [diff] [blame^] | 28 | #include "mbedtls/config.h" |
Ron Eldor | 8b0cf2e | 2018-02-14 16:02:41 +0200 | [diff] [blame] | 29 | #else |
| 30 | #include MBEDTLS_CONFIG_FILE |
| 31 | #endif |
| 32 | |
Jaeden Amero | c49fbbf | 2019-07-04 20:01:14 +0100 | [diff] [blame^] | 33 | #include "mbedtls/md.h" |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 34 | |
Manuel Pégourié-Gonnard | 0a4fb09 | 2015-05-07 12:50:31 +0100 | [diff] [blame] | 35 | #if defined(MBEDTLS_THREADING_C) |
Jaeden Amero | c49fbbf | 2019-07-04 20:01:14 +0100 | [diff] [blame^] | 36 | #include "mbedtls/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 | */ |
Dawid Drozd | 428cc52 | 2018-07-24 10:02:47 +0200 | [diff] [blame] | 83 | typedef struct mbedtls_hmac_drbg_context |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 84 | { |
Antonin Décimo | 36e89b5 | 2019-01-23 15:24:37 +0100 | [diff] [blame] | 85 | /* Working state: the key K is not stored explicitly, |
Manuel Pégourié-Gonnard | b05db2a | 2014-02-01 11:38:05 +0100 | [diff] [blame] | 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 | e0e9c57 | 2018-09-11 16:47:16 +0200 | [diff] [blame] | 204 | * \return \c 0 on success, or an error from the underlying |
| 205 | * hash calculation. |
| 206 | * |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 207 | * \note Additional data is optional, pass NULL and 0 as second |
| 208 | * third argument if no additional data is being used. |
| 209 | */ |
Gilles Peskine | e0e9c57 | 2018-09-11 16:47:16 +0200 | [diff] [blame] | 210 | int mbedtls_hmac_drbg_update_ret( mbedtls_hmac_drbg_context *ctx, |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 211 | const unsigned char *additional, size_t add_len ); |
| 212 | |
| 213 | /** |
Manuel Pégourié-Gonnard | 8fc484d | 2014-01-30 18:28:09 +0100 | [diff] [blame] | 214 | * \brief HMAC_DRBG reseeding (extracts data from entropy source) |
| 215 | * |
| 216 | * \param ctx HMAC_DRBG context |
| 217 | * \param additional Additional data to add to state (Can be NULL) |
| 218 | * \param len Length of additional data |
| 219 | * |
| 220 | * \return 0 if successful, or |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 221 | * MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED |
Manuel Pégourié-Gonnard | 8fc484d | 2014-01-30 18:28:09 +0100 | [diff] [blame] | 222 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 223 | int mbedtls_hmac_drbg_reseed( mbedtls_hmac_drbg_context *ctx, |
Manuel Pégourié-Gonnard | 8fc484d | 2014-01-30 18:28:09 +0100 | [diff] [blame] | 224 | const unsigned char *additional, size_t len ); |
| 225 | |
| 226 | /** |
Manuel Pégourié-Gonnard | 8208d16 | 2014-01-30 12:19:26 +0100 | [diff] [blame] | 227 | * \brief HMAC_DRBG generate random with additional update input |
| 228 | * |
Manuel Pégourié-Gonnard | 658dbed | 2014-01-30 19:03:45 +0100 | [diff] [blame] | 229 | * 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] | 230 | * |
| 231 | * \param p_rng HMAC_DRBG context |
| 232 | * \param output Buffer to fill |
| 233 | * \param output_len Length of the buffer |
| 234 | * \param additional Additional data to update with (can be NULL) |
| 235 | * \param add_len Length of additional data (can be 0) |
| 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, or |
| 239 | * MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG, or |
| 240 | * MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG. |
Manuel Pégourié-Gonnard | 8208d16 | 2014-01-30 12:19:26 +0100 | [diff] [blame] | 241 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 242 | int mbedtls_hmac_drbg_random_with_add( void *p_rng, |
Manuel Pégourié-Gonnard | 8208d16 | 2014-01-30 12:19:26 +0100 | [diff] [blame] | 243 | unsigned char *output, size_t output_len, |
| 244 | const unsigned char *additional, |
| 245 | size_t add_len ); |
| 246 | |
| 247 | /** |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 248 | * \brief HMAC_DRBG generate random |
| 249 | * |
Manuel Pégourié-Gonnard | 658dbed | 2014-01-30 19:03:45 +0100 | [diff] [blame] | 250 | * 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] | 251 | * |
| 252 | * \param p_rng HMAC_DRBG context |
| 253 | * \param output Buffer to fill |
Manuel Pégourié-Gonnard | f6a17d0 | 2014-01-31 11:51:42 +0100 | [diff] [blame] | 254 | * \param out_len Length of the buffer |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 255 | * |
Manuel Pégourié-Gonnard | 8208d16 | 2014-01-30 12:19:26 +0100 | [diff] [blame] | 256 | * \return 0 if successful, or |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 257 | * MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED, or |
| 258 | * MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 259 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 260 | 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] | 261 | |
| 262 | /** |
| 263 | * \brief Free an HMAC_DRBG context |
| 264 | * |
| 265 | * \param ctx HMAC_DRBG context to free. |
| 266 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 267 | void mbedtls_hmac_drbg_free( mbedtls_hmac_drbg_context *ctx ); |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 268 | |
Gilles Peskine | e0e9c57 | 2018-09-11 16:47:16 +0200 | [diff] [blame] | 269 | #if ! defined(MBEDTLS_DEPRECATED_REMOVED) |
| 270 | #if defined(MBEDTLS_DEPRECATED_WARNING) |
| 271 | #define MBEDTLS_DEPRECATED __attribute__((deprecated)) |
| 272 | #else |
| 273 | #define MBEDTLS_DEPRECATED |
| 274 | #endif |
| 275 | /** |
| 276 | * \brief HMAC_DRBG update state |
| 277 | * |
| 278 | * \deprecated Superseded by mbedtls_hmac_drbg_update_ret() |
| 279 | * in 2.16.0. |
| 280 | * |
| 281 | * \param ctx HMAC_DRBG context |
| 282 | * \param additional Additional data to update state with, or NULL |
| 283 | * \param add_len Length of additional data, or 0 |
| 284 | * |
| 285 | * \note Additional data is optional, pass NULL and 0 as second |
| 286 | * third argument if no additional data is being used. |
| 287 | */ |
| 288 | MBEDTLS_DEPRECATED void mbedtls_hmac_drbg_update( |
| 289 | mbedtls_hmac_drbg_context *ctx, |
| 290 | const unsigned char *additional, size_t add_len ); |
| 291 | #undef MBEDTLS_DEPRECATED |
| 292 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
| 293 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 294 | #if defined(MBEDTLS_FS_IO) |
Manuel Pégourié-Gonnard | 48bc3e8 | 2014-01-30 21:11:16 +0100 | [diff] [blame] | 295 | /** |
| 296 | * \brief Write a seed file |
| 297 | * |
| 298 | * \param ctx HMAC_DRBG context |
| 299 | * \param path Name of the file |
| 300 | * |
| 301 | * \return 0 if successful, 1 on file error, or |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 302 | * MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED |
Manuel Pégourié-Gonnard | 48bc3e8 | 2014-01-30 21:11:16 +0100 | [diff] [blame] | 303 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 304 | 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] | 305 | |
| 306 | /** |
| 307 | * \brief Read and update a seed file. Seed is added to this |
| 308 | * instance |
| 309 | * |
| 310 | * \param ctx HMAC_DRBG context |
| 311 | * \param path Name of the file |
| 312 | * |
| 313 | * \return 0 if successful, 1 on file error, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 314 | * MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED or |
| 315 | * MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG |
Manuel Pégourié-Gonnard | 48bc3e8 | 2014-01-30 21:11:16 +0100 | [diff] [blame] | 316 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 317 | int mbedtls_hmac_drbg_update_seed_file( mbedtls_hmac_drbg_context *ctx, const char *path ); |
| 318 | #endif /* MBEDTLS_FS_IO */ |
Manuel Pégourié-Gonnard | 48bc3e8 | 2014-01-30 21:11:16 +0100 | [diff] [blame] | 319 | |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 320 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 321 | #if defined(MBEDTLS_SELF_TEST) |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 322 | /** |
| 323 | * \brief Checkup routine |
| 324 | * |
| 325 | * \return 0 if successful, or 1 if the test failed |
| 326 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 327 | int mbedtls_hmac_drbg_self_test( int verbose ); |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 328 | #endif |
| 329 | |
| 330 | #ifdef __cplusplus |
| 331 | } |
| 332 | #endif |
| 333 | |
| 334 | #endif /* hmac_drbg.h */ |