mbed TLS library
Dependents: HTTPClient-SSL WS_SERVER
include/polarssl/ctr_drbg.h@0:137634ff4186, 2015-06-11 (annotated)
- Committer:
- ansond
- Date:
- Thu Jun 11 03:27:03 2015 +0000
- Revision:
- 0:137634ff4186
initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ansond | 0:137634ff4186 | 1 | /** |
ansond | 0:137634ff4186 | 2 | * \file ctr_drbg.h |
ansond | 0:137634ff4186 | 3 | * |
ansond | 0:137634ff4186 | 4 | * \brief CTR_DRBG based on AES-256 (NIST SP 800-90) |
ansond | 0:137634ff4186 | 5 | * |
ansond | 0:137634ff4186 | 6 | * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved |
ansond | 0:137634ff4186 | 7 | * |
ansond | 0:137634ff4186 | 8 | * This file is part of mbed TLS (https://tls.mbed.org) |
ansond | 0:137634ff4186 | 9 | * |
ansond | 0:137634ff4186 | 10 | * This program is free software; you can redistribute it and/or modify |
ansond | 0:137634ff4186 | 11 | * it under the terms of the GNU General Public License as published by |
ansond | 0:137634ff4186 | 12 | * the Free Software Foundation; either version 2 of the License, or |
ansond | 0:137634ff4186 | 13 | * (at your option) any later version. |
ansond | 0:137634ff4186 | 14 | * |
ansond | 0:137634ff4186 | 15 | * This program is distributed in the hope that it will be useful, |
ansond | 0:137634ff4186 | 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
ansond | 0:137634ff4186 | 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
ansond | 0:137634ff4186 | 18 | * GNU General Public License for more details. |
ansond | 0:137634ff4186 | 19 | * |
ansond | 0:137634ff4186 | 20 | * You should have received a copy of the GNU General Public License along |
ansond | 0:137634ff4186 | 21 | * with this program; if not, write to the Free Software Foundation, Inc., |
ansond | 0:137634ff4186 | 22 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
ansond | 0:137634ff4186 | 23 | */ |
ansond | 0:137634ff4186 | 24 | #ifndef POLARSSL_CTR_DRBG_H |
ansond | 0:137634ff4186 | 25 | #define POLARSSL_CTR_DRBG_H |
ansond | 0:137634ff4186 | 26 | |
ansond | 0:137634ff4186 | 27 | #include "aes.h" |
ansond | 0:137634ff4186 | 28 | |
ansond | 0:137634ff4186 | 29 | #define POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED -0x0034 /**< The entropy source failed. */ |
ansond | 0:137634ff4186 | 30 | #define POLARSSL_ERR_CTR_DRBG_REQUEST_TOO_BIG -0x0036 /**< Too many random requested in single call. */ |
ansond | 0:137634ff4186 | 31 | #define POLARSSL_ERR_CTR_DRBG_INPUT_TOO_BIG -0x0038 /**< Input too large (Entropy + additional). */ |
ansond | 0:137634ff4186 | 32 | #define POLARSSL_ERR_CTR_DRBG_FILE_IO_ERROR -0x003A /**< Read/write error in file. */ |
ansond | 0:137634ff4186 | 33 | |
ansond | 0:137634ff4186 | 34 | #define CTR_DRBG_BLOCKSIZE 16 /**< Block size used by the cipher */ |
ansond | 0:137634ff4186 | 35 | #define CTR_DRBG_KEYSIZE 32 /**< Key size used by the cipher */ |
ansond | 0:137634ff4186 | 36 | #define CTR_DRBG_KEYBITS ( CTR_DRBG_KEYSIZE * 8 ) |
ansond | 0:137634ff4186 | 37 | #define CTR_DRBG_SEEDLEN ( CTR_DRBG_KEYSIZE + CTR_DRBG_BLOCKSIZE ) |
ansond | 0:137634ff4186 | 38 | /**< The seed length (counter + AES key) */ |
ansond | 0:137634ff4186 | 39 | |
ansond | 0:137634ff4186 | 40 | /** |
ansond | 0:137634ff4186 | 41 | * \name SECTION: Module settings |
ansond | 0:137634ff4186 | 42 | * |
ansond | 0:137634ff4186 | 43 | * The configuration options you can set for this module are in this section. |
ansond | 0:137634ff4186 | 44 | * Either change them in config.h or define them on the compiler command line. |
ansond | 0:137634ff4186 | 45 | * \{ |
ansond | 0:137634ff4186 | 46 | */ |
ansond | 0:137634ff4186 | 47 | |
ansond | 0:137634ff4186 | 48 | #if !defined(CTR_DRBG_ENTROPY_LEN) |
ansond | 0:137634ff4186 | 49 | #if defined(POLARSSL_SHA512_C) && !defined(POLARSSL_ENTROPY_FORCE_SHA256) |
ansond | 0:137634ff4186 | 50 | #define CTR_DRBG_ENTROPY_LEN 48 /**< Amount of entropy used per seed by default (48 with SHA-512, 32 with SHA-256) */ |
ansond | 0:137634ff4186 | 51 | #else |
ansond | 0:137634ff4186 | 52 | #define CTR_DRBG_ENTROPY_LEN 32 /**< Amount of entropy used per seed by default (48 with SHA-512, 32 with SHA-256) */ |
ansond | 0:137634ff4186 | 53 | #endif |
ansond | 0:137634ff4186 | 54 | #endif |
ansond | 0:137634ff4186 | 55 | |
ansond | 0:137634ff4186 | 56 | #if !defined(CTR_DRBG_RESEED_INTERVAL) |
ansond | 0:137634ff4186 | 57 | #define CTR_DRBG_RESEED_INTERVAL 10000 /**< Interval before reseed is performed by default */ |
ansond | 0:137634ff4186 | 58 | #endif |
ansond | 0:137634ff4186 | 59 | |
ansond | 0:137634ff4186 | 60 | #if !defined(CTR_DRBG_MAX_INPUT) |
ansond | 0:137634ff4186 | 61 | #define CTR_DRBG_MAX_INPUT 256 /**< Maximum number of additional input bytes */ |
ansond | 0:137634ff4186 | 62 | #endif |
ansond | 0:137634ff4186 | 63 | |
ansond | 0:137634ff4186 | 64 | #if !defined(CTR_DRBG_MAX_REQUEST) |
ansond | 0:137634ff4186 | 65 | #define CTR_DRBG_MAX_REQUEST 1024 /**< Maximum number of requested bytes per call */ |
ansond | 0:137634ff4186 | 66 | #endif |
ansond | 0:137634ff4186 | 67 | |
ansond | 0:137634ff4186 | 68 | #if !defined(CTR_DRBG_MAX_SEED_INPUT) |
ansond | 0:137634ff4186 | 69 | #define CTR_DRBG_MAX_SEED_INPUT 384 /**< Maximum size of (re)seed buffer */ |
ansond | 0:137634ff4186 | 70 | #endif |
ansond | 0:137634ff4186 | 71 | |
ansond | 0:137634ff4186 | 72 | /* \} name SECTION: Module settings */ |
ansond | 0:137634ff4186 | 73 | |
ansond | 0:137634ff4186 | 74 | #define CTR_DRBG_PR_OFF 0 /**< No prediction resistance */ |
ansond | 0:137634ff4186 | 75 | #define CTR_DRBG_PR_ON 1 /**< Prediction resistance enabled */ |
ansond | 0:137634ff4186 | 76 | |
ansond | 0:137634ff4186 | 77 | #ifdef __cplusplus |
ansond | 0:137634ff4186 | 78 | extern "C" { |
ansond | 0:137634ff4186 | 79 | #endif |
ansond | 0:137634ff4186 | 80 | |
ansond | 0:137634ff4186 | 81 | /** |
ansond | 0:137634ff4186 | 82 | * \brief CTR_DRBG context structure |
ansond | 0:137634ff4186 | 83 | */ |
ansond | 0:137634ff4186 | 84 | typedef struct |
ansond | 0:137634ff4186 | 85 | { |
ansond | 0:137634ff4186 | 86 | unsigned char counter[16]; /*!< counter (V) */ |
ansond | 0:137634ff4186 | 87 | int reseed_counter; /*!< reseed counter */ |
ansond | 0:137634ff4186 | 88 | int prediction_resistance; /*!< enable prediction resistance (Automatic |
ansond | 0:137634ff4186 | 89 | reseed before every random generation) */ |
ansond | 0:137634ff4186 | 90 | size_t entropy_len; /*!< amount of entropy grabbed on each |
ansond | 0:137634ff4186 | 91 | (re)seed */ |
ansond | 0:137634ff4186 | 92 | int reseed_interval; /*!< reseed interval */ |
ansond | 0:137634ff4186 | 93 | |
ansond | 0:137634ff4186 | 94 | aes_context aes_ctx; /*!< AES context */ |
ansond | 0:137634ff4186 | 95 | |
ansond | 0:137634ff4186 | 96 | /* |
ansond | 0:137634ff4186 | 97 | * Callbacks (Entropy) |
ansond | 0:137634ff4186 | 98 | */ |
ansond | 0:137634ff4186 | 99 | int (*f_entropy)(void *, unsigned char *, size_t); |
ansond | 0:137634ff4186 | 100 | |
ansond | 0:137634ff4186 | 101 | void *p_entropy; /*!< context for the entropy function */ |
ansond | 0:137634ff4186 | 102 | } |
ansond | 0:137634ff4186 | 103 | ctr_drbg_context; |
ansond | 0:137634ff4186 | 104 | |
ansond | 0:137634ff4186 | 105 | /** |
ansond | 0:137634ff4186 | 106 | * \brief CTR_DRBG initialization |
ansond | 0:137634ff4186 | 107 | * |
ansond | 0:137634ff4186 | 108 | * Note: Personalization data can be provided in addition to the more generic |
ansond | 0:137634ff4186 | 109 | * entropy source to make this instantiation as unique as possible. |
ansond | 0:137634ff4186 | 110 | * |
ansond | 0:137634ff4186 | 111 | * \param ctx CTR_DRBG context to be initialized |
ansond | 0:137634ff4186 | 112 | * \param f_entropy Entropy callback (p_entropy, buffer to fill, buffer |
ansond | 0:137634ff4186 | 113 | * length) |
ansond | 0:137634ff4186 | 114 | * \param p_entropy Entropy context |
ansond | 0:137634ff4186 | 115 | * \param custom Personalization data (Device specific identifiers) |
ansond | 0:137634ff4186 | 116 | * (Can be NULL) |
ansond | 0:137634ff4186 | 117 | * \param len Length of personalization data |
ansond | 0:137634ff4186 | 118 | * |
ansond | 0:137634ff4186 | 119 | * \return 0 if successful, or |
ansond | 0:137634ff4186 | 120 | * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED |
ansond | 0:137634ff4186 | 121 | */ |
ansond | 0:137634ff4186 | 122 | int ctr_drbg_init( ctr_drbg_context *ctx, |
ansond | 0:137634ff4186 | 123 | int (*f_entropy)(void *, unsigned char *, size_t), |
ansond | 0:137634ff4186 | 124 | void *p_entropy, |
ansond | 0:137634ff4186 | 125 | const unsigned char *custom, |
ansond | 0:137634ff4186 | 126 | size_t len ); |
ansond | 0:137634ff4186 | 127 | |
ansond | 0:137634ff4186 | 128 | /** |
ansond | 0:137634ff4186 | 129 | * \brief Clear CTR_CRBG context data |
ansond | 0:137634ff4186 | 130 | * |
ansond | 0:137634ff4186 | 131 | * \param ctx CTR_DRBG context to clear |
ansond | 0:137634ff4186 | 132 | */ |
ansond | 0:137634ff4186 | 133 | void ctr_drbg_free( ctr_drbg_context *ctx ); |
ansond | 0:137634ff4186 | 134 | |
ansond | 0:137634ff4186 | 135 | /** |
ansond | 0:137634ff4186 | 136 | * \brief Enable / disable prediction resistance (Default: Off) |
ansond | 0:137634ff4186 | 137 | * |
ansond | 0:137634ff4186 | 138 | * Note: If enabled, entropy is used for ctx->entropy_len before each call! |
ansond | 0:137634ff4186 | 139 | * Only use this if you have ample supply of good entropy! |
ansond | 0:137634ff4186 | 140 | * |
ansond | 0:137634ff4186 | 141 | * \param ctx CTR_DRBG context |
ansond | 0:137634ff4186 | 142 | * \param resistance CTR_DRBG_PR_ON or CTR_DRBG_PR_OFF |
ansond | 0:137634ff4186 | 143 | */ |
ansond | 0:137634ff4186 | 144 | void ctr_drbg_set_prediction_resistance( ctr_drbg_context *ctx, |
ansond | 0:137634ff4186 | 145 | int resistance ); |
ansond | 0:137634ff4186 | 146 | |
ansond | 0:137634ff4186 | 147 | /** |
ansond | 0:137634ff4186 | 148 | * \brief Set the amount of entropy grabbed on each (re)seed |
ansond | 0:137634ff4186 | 149 | * (Default: CTR_DRBG_ENTROPY_LEN) |
ansond | 0:137634ff4186 | 150 | * |
ansond | 0:137634ff4186 | 151 | * \param ctx CTR_DRBG context |
ansond | 0:137634ff4186 | 152 | * \param len Amount of entropy to grab |
ansond | 0:137634ff4186 | 153 | */ |
ansond | 0:137634ff4186 | 154 | void ctr_drbg_set_entropy_len( ctr_drbg_context *ctx, |
ansond | 0:137634ff4186 | 155 | size_t len ); |
ansond | 0:137634ff4186 | 156 | |
ansond | 0:137634ff4186 | 157 | /** |
ansond | 0:137634ff4186 | 158 | * \brief Set the reseed interval |
ansond | 0:137634ff4186 | 159 | * (Default: CTR_DRBG_RESEED_INTERVAL) |
ansond | 0:137634ff4186 | 160 | * |
ansond | 0:137634ff4186 | 161 | * \param ctx CTR_DRBG context |
ansond | 0:137634ff4186 | 162 | * \param interval Reseed interval |
ansond | 0:137634ff4186 | 163 | */ |
ansond | 0:137634ff4186 | 164 | void ctr_drbg_set_reseed_interval( ctr_drbg_context *ctx, |
ansond | 0:137634ff4186 | 165 | int interval ); |
ansond | 0:137634ff4186 | 166 | |
ansond | 0:137634ff4186 | 167 | /** |
ansond | 0:137634ff4186 | 168 | * \brief CTR_DRBG reseeding (extracts data from entropy source) |
ansond | 0:137634ff4186 | 169 | * |
ansond | 0:137634ff4186 | 170 | * \param ctx CTR_DRBG context |
ansond | 0:137634ff4186 | 171 | * \param additional Additional data to add to state (Can be NULL) |
ansond | 0:137634ff4186 | 172 | * \param len Length of additional data |
ansond | 0:137634ff4186 | 173 | * |
ansond | 0:137634ff4186 | 174 | * \return 0 if successful, or |
ansond | 0:137634ff4186 | 175 | * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED |
ansond | 0:137634ff4186 | 176 | */ |
ansond | 0:137634ff4186 | 177 | int ctr_drbg_reseed( ctr_drbg_context *ctx, |
ansond | 0:137634ff4186 | 178 | const unsigned char *additional, size_t len ); |
ansond | 0:137634ff4186 | 179 | |
ansond | 0:137634ff4186 | 180 | /** |
ansond | 0:137634ff4186 | 181 | * \brief CTR_DRBG update state |
ansond | 0:137634ff4186 | 182 | * |
ansond | 0:137634ff4186 | 183 | * \param ctx CTR_DRBG context |
ansond | 0:137634ff4186 | 184 | * \param additional Additional data to update state with |
ansond | 0:137634ff4186 | 185 | * \param add_len Length of additional data |
ansond | 0:137634ff4186 | 186 | * |
ansond | 0:137634ff4186 | 187 | * \note If add_len is greater than CTR_DRBG_MAX_SEED_INPUT, |
ansond | 0:137634ff4186 | 188 | * only the first CTR_DRBG_MAX_SEED_INPUT bytes are used, |
ansond | 0:137634ff4186 | 189 | * the remaining ones are silently discarded. |
ansond | 0:137634ff4186 | 190 | */ |
ansond | 0:137634ff4186 | 191 | void ctr_drbg_update( ctr_drbg_context *ctx, |
ansond | 0:137634ff4186 | 192 | const unsigned char *additional, size_t add_len ); |
ansond | 0:137634ff4186 | 193 | |
ansond | 0:137634ff4186 | 194 | /** |
ansond | 0:137634ff4186 | 195 | * \brief CTR_DRBG generate random with additional update input |
ansond | 0:137634ff4186 | 196 | * |
ansond | 0:137634ff4186 | 197 | * Note: Automatically reseeds if reseed_counter is reached. |
ansond | 0:137634ff4186 | 198 | * |
ansond | 0:137634ff4186 | 199 | * \param p_rng CTR_DRBG context |
ansond | 0:137634ff4186 | 200 | * \param output Buffer to fill |
ansond | 0:137634ff4186 | 201 | * \param output_len Length of the buffer |
ansond | 0:137634ff4186 | 202 | * \param additional Additional data to update with (Can be NULL) |
ansond | 0:137634ff4186 | 203 | * \param add_len Length of additional data |
ansond | 0:137634ff4186 | 204 | * |
ansond | 0:137634ff4186 | 205 | * \return 0 if successful, or |
ansond | 0:137634ff4186 | 206 | * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED, or |
ansond | 0:137634ff4186 | 207 | * POLARSSL_ERR_CTR_DRBG_REQUEST_TOO_BIG |
ansond | 0:137634ff4186 | 208 | */ |
ansond | 0:137634ff4186 | 209 | int ctr_drbg_random_with_add( void *p_rng, |
ansond | 0:137634ff4186 | 210 | unsigned char *output, size_t output_len, |
ansond | 0:137634ff4186 | 211 | const unsigned char *additional, size_t add_len ); |
ansond | 0:137634ff4186 | 212 | |
ansond | 0:137634ff4186 | 213 | /** |
ansond | 0:137634ff4186 | 214 | * \brief CTR_DRBG generate random |
ansond | 0:137634ff4186 | 215 | * |
ansond | 0:137634ff4186 | 216 | * Note: Automatically reseeds if reseed_counter is reached. |
ansond | 0:137634ff4186 | 217 | * |
ansond | 0:137634ff4186 | 218 | * \param p_rng CTR_DRBG context |
ansond | 0:137634ff4186 | 219 | * \param output Buffer to fill |
ansond | 0:137634ff4186 | 220 | * \param output_len Length of the buffer |
ansond | 0:137634ff4186 | 221 | * |
ansond | 0:137634ff4186 | 222 | * \return 0 if successful, or |
ansond | 0:137634ff4186 | 223 | * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED, or |
ansond | 0:137634ff4186 | 224 | * POLARSSL_ERR_CTR_DRBG_REQUEST_TOO_BIG |
ansond | 0:137634ff4186 | 225 | */ |
ansond | 0:137634ff4186 | 226 | int ctr_drbg_random( void *p_rng, |
ansond | 0:137634ff4186 | 227 | unsigned char *output, size_t output_len ); |
ansond | 0:137634ff4186 | 228 | |
ansond | 0:137634ff4186 | 229 | #if defined(POLARSSL_FS_IO) |
ansond | 0:137634ff4186 | 230 | /** |
ansond | 0:137634ff4186 | 231 | * \brief Write a seed file |
ansond | 0:137634ff4186 | 232 | * |
ansond | 0:137634ff4186 | 233 | * \param ctx CTR_DRBG context |
ansond | 0:137634ff4186 | 234 | * \param path Name of the file |
ansond | 0:137634ff4186 | 235 | * |
ansond | 0:137634ff4186 | 236 | * \return 0 if successful, |
ansond | 0:137634ff4186 | 237 | * POLARSSL_ERR_CTR_DRBG_FILE_IO_ERROR on file error, or |
ansond | 0:137634ff4186 | 238 | * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED |
ansond | 0:137634ff4186 | 239 | */ |
ansond | 0:137634ff4186 | 240 | int ctr_drbg_write_seed_file( ctr_drbg_context *ctx, const char *path ); |
ansond | 0:137634ff4186 | 241 | |
ansond | 0:137634ff4186 | 242 | /** |
ansond | 0:137634ff4186 | 243 | * \brief Read and update a seed file. Seed is added to this |
ansond | 0:137634ff4186 | 244 | * instance |
ansond | 0:137634ff4186 | 245 | * |
ansond | 0:137634ff4186 | 246 | * \param ctx CTR_DRBG context |
ansond | 0:137634ff4186 | 247 | * \param path Name of the file |
ansond | 0:137634ff4186 | 248 | * |
ansond | 0:137634ff4186 | 249 | * \return 0 if successful, |
ansond | 0:137634ff4186 | 250 | * POLARSSL_ERR_CTR_DRBG_FILE_IO_ERROR on file error, |
ansond | 0:137634ff4186 | 251 | * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or |
ansond | 0:137634ff4186 | 252 | * POLARSSL_ERR_CTR_DRBG_INPUT_TOO_BIG |
ansond | 0:137634ff4186 | 253 | */ |
ansond | 0:137634ff4186 | 254 | int ctr_drbg_update_seed_file( ctr_drbg_context *ctx, const char *path ); |
ansond | 0:137634ff4186 | 255 | #endif /* POLARSSL_FS_IO */ |
ansond | 0:137634ff4186 | 256 | |
ansond | 0:137634ff4186 | 257 | /** |
ansond | 0:137634ff4186 | 258 | * \brief Checkup routine |
ansond | 0:137634ff4186 | 259 | * |
ansond | 0:137634ff4186 | 260 | * \return 0 if successful, or 1 if the test failed |
ansond | 0:137634ff4186 | 261 | */ |
ansond | 0:137634ff4186 | 262 | int ctr_drbg_self_test( int verbose ); |
ansond | 0:137634ff4186 | 263 | |
ansond | 0:137634ff4186 | 264 | /* Internal functions (do not call directly) */ |
ansond | 0:137634ff4186 | 265 | int ctr_drbg_init_entropy_len( ctr_drbg_context *, |
ansond | 0:137634ff4186 | 266 | int (*)(void *, unsigned char *, size_t), void *, |
ansond | 0:137634ff4186 | 267 | const unsigned char *, size_t, size_t ); |
ansond | 0:137634ff4186 | 268 | |
ansond | 0:137634ff4186 | 269 | #ifdef __cplusplus |
ansond | 0:137634ff4186 | 270 | } |
ansond | 0:137634ff4186 | 271 | #endif |
ansond | 0:137634ff4186 | 272 | |
ansond | 0:137634ff4186 | 273 | #endif /* ctr_drbg.h */ |
ansond | 0:137634ff4186 | 274 |