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) |
| 5 | * |
| 6 | * Copyright (C) 2014, Brainspark B.V. |
| 7 | * |
| 8 | * This file is part of PolarSSL (http://www.polarssl.org) |
| 9 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
| 10 | * |
| 11 | * All rights reserved. |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
| 16 | * (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License along |
| 24 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 25 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 26 | */ |
| 27 | #ifndef POLARSSL_HMAC_DRBG_H |
| 28 | #define POLARSSL_HMAC_DRBG_H |
| 29 | |
| 30 | #include "md.h" |
| 31 | |
Manuel Pégourié-Gonnard | fe34a5f | 2014-01-30 15:06:40 +0100 | [diff] [blame] | 32 | /* |
| 33 | * ! Same values as ctr_drbg.h ! |
| 34 | */ |
| 35 | #define POLARSSL_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED -0x0034 /**< The entropy source failed. */ |
| 36 | #define POLARSSL_ERR_HMAC_DRBG_REQUEST_TOO_BIG -0x0036 /**< Too many random requested in single call. */ |
| 37 | #define POLARSSL_ERR_HMAC_DRBG_INPUT_TOO_BIG -0x0038 /**< Input too large (Entropy + additional). */ |
| 38 | #define POLARSSL_ERR_HMAC_DRBG_FILE_IO_ERROR -0x003A /**< Read/write error in file. */ |
| 39 | |
Manuel Pégourié-Gonnard | 0520b60 | 2014-01-30 19:43:46 +0100 | [diff] [blame] | 40 | #if !defined(POLARSSL_CONFIG_OPTIONS) |
Manuel Pégourié-Gonnard | efc8d80 | 2014-01-30 19:36:22 +0100 | [diff] [blame] | 41 | #define POLARSSL_HMAC_DRBG_RESEED_INTERVAL 10000 /**< Interval before reseed is performed by default */ |
| 42 | #define POLARSSL_HMAC_DRBG_MAX_INPUT 256 /**< Maximum number of additional input bytes */ |
| 43 | #define POLARSSL_HMAC_DRBG_MAX_REQUEST 1024 /**< Maximum number of requested bytes per call */ |
| 44 | #define POLARSSL_HMAC_DRBG_MAX_SEED_INPUT 384 /**< Maximum size of (re)seed buffer */ |
Manuel Pégourié-Gonnard | 0520b60 | 2014-01-30 19:43:46 +0100 | [diff] [blame] | 45 | #endif /* !POLARSSL_CONFIG_OPTIONS */ |
Manuel Pégourié-Gonnard | fe34a5f | 2014-01-30 15:06:40 +0100 | [diff] [blame] | 46 | |
Manuel Pégourié-Gonnard | efc8d80 | 2014-01-30 19:36:22 +0100 | [diff] [blame] | 47 | #define POLARSSL_HMAC_DRBG_PR_OFF 0 /**< No prediction resistance */ |
| 48 | #define POLARSSL_HMAC_DRBG_PR_ON 1 /**< Prediction resistance enabled */ |
Manuel Pégourié-Gonnard | af786ff | 2014-01-30 18:44:18 +0100 | [diff] [blame] | 49 | |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 50 | #ifdef __cplusplus |
| 51 | extern "C" { |
| 52 | #endif |
| 53 | |
Manuel Pégourié-Gonnard | fe34a5f | 2014-01-30 15:06:40 +0100 | [diff] [blame] | 54 | /** |
| 55 | * HMAC_DRBG context. |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 56 | */ |
| 57 | typedef struct |
| 58 | { |
Manuel Pégourié-Gonnard | 658dbed | 2014-01-30 19:03:45 +0100 | [diff] [blame] | 59 | /* Working state */ |
| 60 | md_context_t md_ctx; /*!< HMAC context */ |
| 61 | unsigned char V[POLARSSL_MD_MAX_SIZE]; /*!< V in the spec */ |
| 62 | unsigned char K[POLARSSL_MD_MAX_SIZE]; /*!< Key in the spec */ |
| 63 | int reseed_counter; /*!< reseed counter */ |
Manuel Pégourié-Gonnard | fe34a5f | 2014-01-30 15:06:40 +0100 | [diff] [blame] | 64 | |
Manuel Pégourié-Gonnard | 658dbed | 2014-01-30 19:03:45 +0100 | [diff] [blame] | 65 | /* Administrative state */ |
Manuel Pégourié-Gonnard | fe34a5f | 2014-01-30 15:06:40 +0100 | [diff] [blame] | 66 | 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] | 67 | int prediction_resistance; /*!< enable prediction resistance (Automatic |
| 68 | reseed before every random generation) */ |
Manuel Pégourié-Gonnard | 658dbed | 2014-01-30 19:03:45 +0100 | [diff] [blame] | 69 | int reseed_interval; /*!< reseed interval */ |
Manuel Pégourié-Gonnard | fe34a5f | 2014-01-30 15:06:40 +0100 | [diff] [blame] | 70 | |
Manuel Pégourié-Gonnard | 658dbed | 2014-01-30 19:03:45 +0100 | [diff] [blame] | 71 | /* Callbacks */ |
Manuel Pégourié-Gonnard | af786ff | 2014-01-30 18:44:18 +0100 | [diff] [blame] | 72 | int (*f_entropy)(void *, unsigned char *, size_t); /*!< entropy function */ |
| 73 | void *p_entropy; /*!< context for the entropy function */ |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 74 | } hmac_drbg_context; |
| 75 | |
| 76 | /** |
| 77 | * \brief HMAC_DRBG initialisation |
| 78 | * |
| 79 | * \param ctx HMAC_DRBG context to be initialised |
| 80 | * \param md_info MD algorithm to use for HMAC_DRBG |
Manuel Pégourié-Gonnard | fe34a5f | 2014-01-30 15:06:40 +0100 | [diff] [blame] | 81 | * \param f_entropy Entropy callback (p_entropy, buffer to fill, buffer |
| 82 | * length) |
| 83 | * \param p_entropy Entropy context |
| 84 | * \param custom Personalization data (Device specific identifiers) |
| 85 | * (Can be NULL) |
| 86 | * \param len Length of personalization data |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 87 | * |
Manuel Pégourié-Gonnard | fe34a5f | 2014-01-30 15:06:40 +0100 | [diff] [blame] | 88 | * \note The "security strength" as defined by NIST is set to: |
| 89 | * 128 bits if md_alg is SHA-1, |
| 90 | * 192 bits if md_alg is SHA-224, |
| 91 | * 256 bits if md_alg is SHA-256 or higher. |
| 92 | * 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] | 93 | * |
| 94 | * \return 0 if successful, or |
| 95 | * POLARSSL_ERR_MD_BAD_INPUT_DATA, or |
Manuel Pégourié-Gonnard | fe34a5f | 2014-01-30 15:06:40 +0100 | [diff] [blame] | 96 | * POLARSSL_ERR_MD_ALLOC_FAILED, or |
| 97 | * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED. |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 98 | */ |
| 99 | int hmac_drbg_init( hmac_drbg_context *ctx, |
| 100 | const md_info_t * md_info, |
Manuel Pégourié-Gonnard | fe34a5f | 2014-01-30 15:06:40 +0100 | [diff] [blame] | 101 | int (*f_entropy)(void *, unsigned char *, size_t), |
| 102 | void *p_entropy, |
| 103 | const unsigned char *custom, |
| 104 | size_t len ); |
| 105 | |
| 106 | /** |
Manuel Pégourié-Gonnard | 4e669c6 | 2014-01-30 18:06:08 +0100 | [diff] [blame] | 107 | * \brief Initilisation of simpified HMAC_DRBG (never reseeds). |
Manuel Pégourié-Gonnard | fe34a5f | 2014-01-30 15:06:40 +0100 | [diff] [blame] | 108 | * (For use with deterministic ECDSA.) |
| 109 | * |
| 110 | * \param ctx HMAC_DRBG context to be initialised |
| 111 | * \param md_info MD algorithm to use for HMAC_DRBG |
| 112 | * \param data Concatenation of entropy string and additional data |
| 113 | * \param data_len Length of data in bytes |
| 114 | * |
| 115 | * \return 0 if successful, or |
| 116 | * POLARSSL_ERR_MD_BAD_INPUT_DATA, or |
| 117 | * POLARSSL_ERR_MD_ALLOC_FAILED. |
| 118 | */ |
| 119 | int hmac_drbg_init_buf( hmac_drbg_context *ctx, |
| 120 | const md_info_t * md_info, |
| 121 | const unsigned char *data, size_t data_len ); |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 122 | |
| 123 | /** |
Manuel Pégourié-Gonnard | af786ff | 2014-01-30 18:44:18 +0100 | [diff] [blame] | 124 | * \brief Enable / disable prediction resistance (Default: Off) |
| 125 | * |
| 126 | * Note: If enabled, entropy is used for ctx->entropy_len before each call! |
| 127 | * Only use this if you have ample supply of good entropy! |
| 128 | * |
| 129 | * \param ctx HMAC_DRBG context |
Manuel Pégourié-Gonnard | efc8d80 | 2014-01-30 19:36:22 +0100 | [diff] [blame] | 130 | * \param resistance POLARSSL_HMAC_DRBG_PR_ON or POLARSSL_HMAC_DRBG_PR_OFF |
Manuel Pégourié-Gonnard | af786ff | 2014-01-30 18:44:18 +0100 | [diff] [blame] | 131 | */ |
| 132 | void hmac_drbg_set_prediction_resistance( hmac_drbg_context *ctx, |
| 133 | int resistance ); |
| 134 | |
| 135 | /** |
Manuel Pégourié-Gonnard | 4e669c6 | 2014-01-30 18:06:08 +0100 | [diff] [blame] | 136 | * \brief Set the amount of entropy grabbed on each reseed |
Manuel Pégourié-Gonnard | 658dbed | 2014-01-30 19:03:45 +0100 | [diff] [blame] | 137 | * (Default: given by the security strength, which |
| 138 | * depends on the hash used, see \c hmac_drbg_init() ) |
Manuel Pégourié-Gonnard | 4e669c6 | 2014-01-30 18:06:08 +0100 | [diff] [blame] | 139 | * |
| 140 | * \param ctx HMAC_DRBG context |
Manuel Pégourié-Gonnard | 658dbed | 2014-01-30 19:03:45 +0100 | [diff] [blame] | 141 | * \param len Amount of entropy to grab, in bytes |
Manuel Pégourié-Gonnard | 4e669c6 | 2014-01-30 18:06:08 +0100 | [diff] [blame] | 142 | */ |
| 143 | void hmac_drbg_set_entropy_len( hmac_drbg_context *ctx, |
| 144 | size_t len ); |
| 145 | |
| 146 | /** |
Manuel Pégourié-Gonnard | 658dbed | 2014-01-30 19:03:45 +0100 | [diff] [blame] | 147 | * \brief Set the reseed interval |
Manuel Pégourié-Gonnard | efc8d80 | 2014-01-30 19:36:22 +0100 | [diff] [blame] | 148 | * (Default: POLARSSL_HMAC_DRBG_RESEED_INTERVAL) |
Manuel Pégourié-Gonnard | 658dbed | 2014-01-30 19:03:45 +0100 | [diff] [blame] | 149 | * |
| 150 | * \param ctx HMAC_DRBG context |
| 151 | * \param interval Reseed interval |
| 152 | */ |
| 153 | void hmac_drbg_set_reseed_interval( hmac_drbg_context *ctx, |
| 154 | int interval ); |
| 155 | |
| 156 | /** |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 157 | * \brief HMAC_DRBG update state |
| 158 | * |
| 159 | * \param ctx HMAC_DRBG context |
| 160 | * \param additional Additional data to update state with, or NULL |
| 161 | * \param add_len Length of additional data, or 0 |
| 162 | * |
| 163 | * \note Additional data is optional, pass NULL and 0 as second |
| 164 | * third argument if no additional data is being used. |
| 165 | */ |
| 166 | void hmac_drbg_update( hmac_drbg_context *ctx, |
| 167 | const unsigned char *additional, size_t add_len ); |
| 168 | |
| 169 | /** |
Manuel Pégourié-Gonnard | 8fc484d | 2014-01-30 18:28:09 +0100 | [diff] [blame] | 170 | * \brief HMAC_DRBG reseeding (extracts data from entropy source) |
| 171 | * |
| 172 | * \param ctx HMAC_DRBG context |
| 173 | * \param additional Additional data to add to state (Can be NULL) |
| 174 | * \param len Length of additional data |
| 175 | * |
| 176 | * \return 0 if successful, or |
| 177 | * POLARSSL_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED |
| 178 | */ |
| 179 | int hmac_drbg_reseed( hmac_drbg_context *ctx, |
| 180 | const unsigned char *additional, size_t len ); |
| 181 | |
| 182 | /** |
Manuel Pégourié-Gonnard | 8208d16 | 2014-01-30 12:19:26 +0100 | [diff] [blame] | 183 | * \brief HMAC_DRBG generate random with additional update input |
| 184 | * |
Manuel Pégourié-Gonnard | 658dbed | 2014-01-30 19:03:45 +0100 | [diff] [blame] | 185 | * 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] | 186 | * |
| 187 | * \param p_rng HMAC_DRBG context |
| 188 | * \param output Buffer to fill |
| 189 | * \param output_len Length of the buffer |
| 190 | * \param additional Additional data to update with (can be NULL) |
| 191 | * \param add_len Length of additional data (can be 0) |
| 192 | * |
| 193 | * \return 0 if successful, or |
| 194 | * TODO: POLARSSL_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED, or |
| 195 | * TODO: POLARSSL_ERR_HMAC_DRBG_REQUEST_TOO_BIG |
| 196 | */ |
| 197 | int hmac_drbg_random_with_add( void *p_rng, |
| 198 | unsigned char *output, size_t output_len, |
| 199 | const unsigned char *additional, |
| 200 | size_t add_len ); |
| 201 | |
| 202 | /** |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 203 | * \brief HMAC_DRBG generate random |
| 204 | * |
Manuel Pégourié-Gonnard | 658dbed | 2014-01-30 19:03:45 +0100 | [diff] [blame] | 205 | * 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] | 206 | * |
| 207 | * \param p_rng HMAC_DRBG context |
| 208 | * \param output Buffer to fill |
| 209 | * \param output_len Length of the buffer |
| 210 | * |
Manuel Pégourié-Gonnard | 8208d16 | 2014-01-30 12:19:26 +0100 | [diff] [blame] | 211 | * \return 0 if successful, or |
| 212 | * TODO: POLARSSL_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED, or |
| 213 | * TODO: POLARSSL_ERR_HMAC_DRBG_REQUEST_TOO_BIG |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 214 | */ |
| 215 | int hmac_drbg_random( void *p_rng, unsigned char *output, size_t out_len ); |
| 216 | |
| 217 | /** |
| 218 | * \brief Free an HMAC_DRBG context |
| 219 | * |
| 220 | * \param ctx HMAC_DRBG context to free. |
| 221 | */ |
| 222 | void hmac_drbg_free( hmac_drbg_context *ctx ); |
| 223 | |
Manuel Pégourié-Gonnard | 48bc3e8 | 2014-01-30 21:11:16 +0100 | [diff] [blame^] | 224 | #if defined(POLARSSL_FS_IO) |
| 225 | /** |
| 226 | * \brief Write a seed file |
| 227 | * |
| 228 | * \param ctx HMAC_DRBG context |
| 229 | * \param path Name of the file |
| 230 | * |
| 231 | * \return 0 if successful, 1 on file error, or |
| 232 | * POLARSSL_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED |
| 233 | */ |
| 234 | int hmac_drbg_write_seed_file( hmac_drbg_context *ctx, const char *path ); |
| 235 | |
| 236 | /** |
| 237 | * \brief Read and update a seed file. Seed is added to this |
| 238 | * instance |
| 239 | * |
| 240 | * \param ctx HMAC_DRBG context |
| 241 | * \param path Name of the file |
| 242 | * |
| 243 | * \return 0 if successful, 1 on file error, |
| 244 | * POLARSSL_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED or |
| 245 | * POLARSSL_ERR_HMAC_DRBG_INPUT_TOO_BIG |
| 246 | */ |
| 247 | int hmac_drbg_update_seed_file( hmac_drbg_context *ctx, const char *path ); |
| 248 | #endif |
| 249 | |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 250 | |
| 251 | #if defined(POLARSSL_SELF_TEST) |
| 252 | /** |
| 253 | * \brief Checkup routine |
| 254 | * |
| 255 | * \return 0 if successful, or 1 if the test failed |
| 256 | */ |
| 257 | int hmac_drbg_self_test( int verbose ); |
| 258 | #endif |
| 259 | |
| 260 | #ifdef __cplusplus |
| 261 | } |
| 262 | #endif |
| 263 | |
| 264 | #endif /* hmac_drbg.h */ |