mbed TLS library

Dependents:   HTTPClient-SSL WS_SERVER

Committer:
ansond
Date:
Thu Jun 11 03:27:03 2015 +0000
Revision:
0:137634ff4186
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 0:137634ff4186 1 /**
ansond 0:137634ff4186 2 * \file hmac_drbg.h
ansond 0:137634ff4186 3 *
ansond 0:137634ff4186 4 * \brief HMAC_DRBG (NIST SP 800-90A)
ansond 0:137634ff4186 5 *
ansond 0:137634ff4186 6 * Copyright (C) 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_HMAC_DRBG_H
ansond 0:137634ff4186 25 #define POLARSSL_HMAC_DRBG_H
ansond 0:137634ff4186 26
ansond 0:137634ff4186 27 #include "md.h"
ansond 0:137634ff4186 28
ansond 0:137634ff4186 29 /*
ansond 0:137634ff4186 30 * Error codes
ansond 0:137634ff4186 31 */
ansond 0:137634ff4186 32 #define POLARSSL_ERR_HMAC_DRBG_REQUEST_TOO_BIG -0x0003 /**< Too many random requested in single call. */
ansond 0:137634ff4186 33 #define POLARSSL_ERR_HMAC_DRBG_INPUT_TOO_BIG -0x0005 /**< Input too large (Entropy + additional). */
ansond 0:137634ff4186 34 #define POLARSSL_ERR_HMAC_DRBG_FILE_IO_ERROR -0x0007 /**< Read/write error in file. */
ansond 0:137634ff4186 35 #define POLARSSL_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED -0x0009 /**< The entropy source failed. */
ansond 0:137634ff4186 36
ansond 0:137634ff4186 37 /**
ansond 0:137634ff4186 38 * \name SECTION: Module settings
ansond 0:137634ff4186 39 *
ansond 0:137634ff4186 40 * The configuration options you can set for this module are in this section.
ansond 0:137634ff4186 41 * Either change them in config.h or define them on the compiler command line.
ansond 0:137634ff4186 42 * \{
ansond 0:137634ff4186 43 */
ansond 0:137634ff4186 44
ansond 0:137634ff4186 45 #if !defined(POLARSSL_HMAC_DRBG_RESEED_INTERVAL)
ansond 0:137634ff4186 46 #define POLARSSL_HMAC_DRBG_RESEED_INTERVAL 10000 /**< Interval before reseed is performed by default */
ansond 0:137634ff4186 47 #endif
ansond 0:137634ff4186 48
ansond 0:137634ff4186 49 #if !defined(POLARSSL_HMAC_DRBG_MAX_INPUT)
ansond 0:137634ff4186 50 #define POLARSSL_HMAC_DRBG_MAX_INPUT 256 /**< Maximum number of additional input bytes */
ansond 0:137634ff4186 51 #endif
ansond 0:137634ff4186 52
ansond 0:137634ff4186 53 #if !defined(POLARSSL_HMAC_DRBG_MAX_REQUEST)
ansond 0:137634ff4186 54 #define POLARSSL_HMAC_DRBG_MAX_REQUEST 1024 /**< Maximum number of requested bytes per call */
ansond 0:137634ff4186 55 #endif
ansond 0:137634ff4186 56
ansond 0:137634ff4186 57 #if !defined(POLARSSL_HMAC_DRBG_MAX_SEED_INPUT)
ansond 0:137634ff4186 58 #define POLARSSL_HMAC_DRBG_MAX_SEED_INPUT 384 /**< Maximum size of (re)seed buffer */
ansond 0:137634ff4186 59 #endif
ansond 0:137634ff4186 60
ansond 0:137634ff4186 61 /* \} name SECTION: Module settings */
ansond 0:137634ff4186 62
ansond 0:137634ff4186 63 #define POLARSSL_HMAC_DRBG_PR_OFF 0 /**< No prediction resistance */
ansond 0:137634ff4186 64 #define POLARSSL_HMAC_DRBG_PR_ON 1 /**< Prediction resistance enabled */
ansond 0:137634ff4186 65
ansond 0:137634ff4186 66 #ifdef __cplusplus
ansond 0:137634ff4186 67 extern "C" {
ansond 0:137634ff4186 68 #endif
ansond 0:137634ff4186 69
ansond 0:137634ff4186 70 /**
ansond 0:137634ff4186 71 * HMAC_DRBG context.
ansond 0:137634ff4186 72 */
ansond 0:137634ff4186 73 typedef struct
ansond 0:137634ff4186 74 {
ansond 0:137634ff4186 75 /* Working state: the key K is not stored explicitely,
ansond 0:137634ff4186 76 * but is implied by the HMAC context */
ansond 0:137634ff4186 77 md_context_t md_ctx; /*!< HMAC context (inc. K) */
ansond 0:137634ff4186 78 unsigned char V[POLARSSL_MD_MAX_SIZE]; /*!< V in the spec */
ansond 0:137634ff4186 79 int reseed_counter; /*!< reseed counter */
ansond 0:137634ff4186 80
ansond 0:137634ff4186 81 /* Administrative state */
ansond 0:137634ff4186 82 size_t entropy_len; /*!< entropy bytes grabbed on each (re)seed */
ansond 0:137634ff4186 83 int prediction_resistance; /*!< enable prediction resistance (Automatic
ansond 0:137634ff4186 84 reseed before every random generation) */
ansond 0:137634ff4186 85 int reseed_interval; /*!< reseed interval */
ansond 0:137634ff4186 86
ansond 0:137634ff4186 87 /* Callbacks */
ansond 0:137634ff4186 88 int (*f_entropy)(void *, unsigned char *, size_t); /*!< entropy function */
ansond 0:137634ff4186 89 void *p_entropy; /*!< context for the entropy function */
ansond 0:137634ff4186 90 } hmac_drbg_context;
ansond 0:137634ff4186 91
ansond 0:137634ff4186 92 /**
ansond 0:137634ff4186 93 * \brief HMAC_DRBG initialisation
ansond 0:137634ff4186 94 *
ansond 0:137634ff4186 95 * \param ctx HMAC_DRBG context to be initialised
ansond 0:137634ff4186 96 * \param md_info MD algorithm to use for HMAC_DRBG
ansond 0:137634ff4186 97 * \param f_entropy Entropy callback (p_entropy, buffer to fill, buffer
ansond 0:137634ff4186 98 * length)
ansond 0:137634ff4186 99 * \param p_entropy Entropy context
ansond 0:137634ff4186 100 * \param custom Personalization data (Device specific identifiers)
ansond 0:137634ff4186 101 * (Can be NULL)
ansond 0:137634ff4186 102 * \param len Length of personalization data
ansond 0:137634ff4186 103 *
ansond 0:137634ff4186 104 * \note The "security strength" as defined by NIST is set to:
ansond 0:137634ff4186 105 * 128 bits if md_alg is SHA-1,
ansond 0:137634ff4186 106 * 192 bits if md_alg is SHA-224,
ansond 0:137634ff4186 107 * 256 bits if md_alg is SHA-256 or higher.
ansond 0:137634ff4186 108 * Note that SHA-256 is just as efficient as SHA-224.
ansond 0:137634ff4186 109 *
ansond 0:137634ff4186 110 * \return 0 if successful, or
ansond 0:137634ff4186 111 * POLARSSL_ERR_MD_BAD_INPUT_DATA, or
ansond 0:137634ff4186 112 * POLARSSL_ERR_MD_ALLOC_FAILED, or
ansond 0:137634ff4186 113 * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED.
ansond 0:137634ff4186 114 */
ansond 0:137634ff4186 115 int hmac_drbg_init( hmac_drbg_context *ctx,
ansond 0:137634ff4186 116 const md_info_t * md_info,
ansond 0:137634ff4186 117 int (*f_entropy)(void *, unsigned char *, size_t),
ansond 0:137634ff4186 118 void *p_entropy,
ansond 0:137634ff4186 119 const unsigned char *custom,
ansond 0:137634ff4186 120 size_t len );
ansond 0:137634ff4186 121
ansond 0:137634ff4186 122 /**
ansond 0:137634ff4186 123 * \brief Initilisation of simpified HMAC_DRBG (never reseeds).
ansond 0:137634ff4186 124 * (For use with deterministic ECDSA.)
ansond 0:137634ff4186 125 *
ansond 0:137634ff4186 126 * \param ctx HMAC_DRBG context to be initialised
ansond 0:137634ff4186 127 * \param md_info MD algorithm to use for HMAC_DRBG
ansond 0:137634ff4186 128 * \param data Concatenation of entropy string and additional data
ansond 0:137634ff4186 129 * \param data_len Length of data in bytes
ansond 0:137634ff4186 130 *
ansond 0:137634ff4186 131 * \return 0 if successful, or
ansond 0:137634ff4186 132 * POLARSSL_ERR_MD_BAD_INPUT_DATA, or
ansond 0:137634ff4186 133 * POLARSSL_ERR_MD_ALLOC_FAILED.
ansond 0:137634ff4186 134 */
ansond 0:137634ff4186 135 int hmac_drbg_init_buf( hmac_drbg_context *ctx,
ansond 0:137634ff4186 136 const md_info_t * md_info,
ansond 0:137634ff4186 137 const unsigned char *data, size_t data_len );
ansond 0:137634ff4186 138
ansond 0:137634ff4186 139 /**
ansond 0:137634ff4186 140 * \brief Enable / disable prediction resistance (Default: Off)
ansond 0:137634ff4186 141 *
ansond 0:137634ff4186 142 * Note: If enabled, entropy is used for ctx->entropy_len before each call!
ansond 0:137634ff4186 143 * Only use this if you have ample supply of good entropy!
ansond 0:137634ff4186 144 *
ansond 0:137634ff4186 145 * \param ctx HMAC_DRBG context
ansond 0:137634ff4186 146 * \param resistance POLARSSL_HMAC_DRBG_PR_ON or POLARSSL_HMAC_DRBG_PR_OFF
ansond 0:137634ff4186 147 */
ansond 0:137634ff4186 148 void hmac_drbg_set_prediction_resistance( hmac_drbg_context *ctx,
ansond 0:137634ff4186 149 int resistance );
ansond 0:137634ff4186 150
ansond 0:137634ff4186 151 /**
ansond 0:137634ff4186 152 * \brief Set the amount of entropy grabbed on each reseed
ansond 0:137634ff4186 153 * (Default: given by the security strength, which
ansond 0:137634ff4186 154 * depends on the hash used, see \c hmac_drbg_init() )
ansond 0:137634ff4186 155 *
ansond 0:137634ff4186 156 * \param ctx HMAC_DRBG context
ansond 0:137634ff4186 157 * \param len Amount of entropy to grab, in bytes
ansond 0:137634ff4186 158 */
ansond 0:137634ff4186 159 void hmac_drbg_set_entropy_len( hmac_drbg_context *ctx,
ansond 0:137634ff4186 160 size_t len );
ansond 0:137634ff4186 161
ansond 0:137634ff4186 162 /**
ansond 0:137634ff4186 163 * \brief Set the reseed interval
ansond 0:137634ff4186 164 * (Default: POLARSSL_HMAC_DRBG_RESEED_INTERVAL)
ansond 0:137634ff4186 165 *
ansond 0:137634ff4186 166 * \param ctx HMAC_DRBG context
ansond 0:137634ff4186 167 * \param interval Reseed interval
ansond 0:137634ff4186 168 */
ansond 0:137634ff4186 169 void hmac_drbg_set_reseed_interval( hmac_drbg_context *ctx,
ansond 0:137634ff4186 170 int interval );
ansond 0:137634ff4186 171
ansond 0:137634ff4186 172 /**
ansond 0:137634ff4186 173 * \brief HMAC_DRBG update state
ansond 0:137634ff4186 174 *
ansond 0:137634ff4186 175 * \param ctx HMAC_DRBG context
ansond 0:137634ff4186 176 * \param additional Additional data to update state with, or NULL
ansond 0:137634ff4186 177 * \param add_len Length of additional data, or 0
ansond 0:137634ff4186 178 *
ansond 0:137634ff4186 179 * \note Additional data is optional, pass NULL and 0 as second
ansond 0:137634ff4186 180 * third argument if no additional data is being used.
ansond 0:137634ff4186 181 */
ansond 0:137634ff4186 182 void hmac_drbg_update( hmac_drbg_context *ctx,
ansond 0:137634ff4186 183 const unsigned char *additional, size_t add_len );
ansond 0:137634ff4186 184
ansond 0:137634ff4186 185 /**
ansond 0:137634ff4186 186 * \brief HMAC_DRBG reseeding (extracts data from entropy source)
ansond 0:137634ff4186 187 *
ansond 0:137634ff4186 188 * \param ctx HMAC_DRBG context
ansond 0:137634ff4186 189 * \param additional Additional data to add to state (Can be NULL)
ansond 0:137634ff4186 190 * \param len Length of additional data
ansond 0:137634ff4186 191 *
ansond 0:137634ff4186 192 * \return 0 if successful, or
ansond 0:137634ff4186 193 * POLARSSL_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED
ansond 0:137634ff4186 194 */
ansond 0:137634ff4186 195 int hmac_drbg_reseed( hmac_drbg_context *ctx,
ansond 0:137634ff4186 196 const unsigned char *additional, size_t len );
ansond 0:137634ff4186 197
ansond 0:137634ff4186 198 /**
ansond 0:137634ff4186 199 * \brief HMAC_DRBG generate random with additional update input
ansond 0:137634ff4186 200 *
ansond 0:137634ff4186 201 * Note: Automatically reseeds if reseed_counter is reached or PR is enabled.
ansond 0:137634ff4186 202 *
ansond 0:137634ff4186 203 * \param p_rng HMAC_DRBG context
ansond 0:137634ff4186 204 * \param output Buffer to fill
ansond 0:137634ff4186 205 * \param output_len Length of the buffer
ansond 0:137634ff4186 206 * \param additional Additional data to update with (can be NULL)
ansond 0:137634ff4186 207 * \param add_len Length of additional data (can be 0)
ansond 0:137634ff4186 208 *
ansond 0:137634ff4186 209 * \return 0 if successful, or
ansond 0:137634ff4186 210 * POLARSSL_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED, or
ansond 0:137634ff4186 211 * POLARSSL_ERR_HMAC_DRBG_REQUEST_TOO_BIG, or
ansond 0:137634ff4186 212 * POLARSSL_ERR_HMAC_DRBG_INPUT_TOO_BIG.
ansond 0:137634ff4186 213 */
ansond 0:137634ff4186 214 int hmac_drbg_random_with_add( void *p_rng,
ansond 0:137634ff4186 215 unsigned char *output, size_t output_len,
ansond 0:137634ff4186 216 const unsigned char *additional,
ansond 0:137634ff4186 217 size_t add_len );
ansond 0:137634ff4186 218
ansond 0:137634ff4186 219 /**
ansond 0:137634ff4186 220 * \brief HMAC_DRBG generate random
ansond 0:137634ff4186 221 *
ansond 0:137634ff4186 222 * Note: Automatically reseeds if reseed_counter is reached or PR is enabled.
ansond 0:137634ff4186 223 *
ansond 0:137634ff4186 224 * \param p_rng HMAC_DRBG context
ansond 0:137634ff4186 225 * \param output Buffer to fill
ansond 0:137634ff4186 226 * \param out_len Length of the buffer
ansond 0:137634ff4186 227 *
ansond 0:137634ff4186 228 * \return 0 if successful, or
ansond 0:137634ff4186 229 * POLARSSL_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED, or
ansond 0:137634ff4186 230 * POLARSSL_ERR_HMAC_DRBG_REQUEST_TOO_BIG
ansond 0:137634ff4186 231 */
ansond 0:137634ff4186 232 int hmac_drbg_random( void *p_rng, unsigned char *output, size_t out_len );
ansond 0:137634ff4186 233
ansond 0:137634ff4186 234 /**
ansond 0:137634ff4186 235 * \brief Free an HMAC_DRBG context
ansond 0:137634ff4186 236 *
ansond 0:137634ff4186 237 * \param ctx HMAC_DRBG context to free.
ansond 0:137634ff4186 238 */
ansond 0:137634ff4186 239 void hmac_drbg_free( hmac_drbg_context *ctx );
ansond 0:137634ff4186 240
ansond 0:137634ff4186 241 #if defined(POLARSSL_FS_IO)
ansond 0:137634ff4186 242 /**
ansond 0:137634ff4186 243 * \brief Write a seed file
ansond 0:137634ff4186 244 *
ansond 0:137634ff4186 245 * \param ctx HMAC_DRBG context
ansond 0:137634ff4186 246 * \param path Name of the file
ansond 0:137634ff4186 247 *
ansond 0:137634ff4186 248 * \return 0 if successful, 1 on file error, or
ansond 0:137634ff4186 249 * POLARSSL_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED
ansond 0:137634ff4186 250 */
ansond 0:137634ff4186 251 int hmac_drbg_write_seed_file( hmac_drbg_context *ctx, const char *path );
ansond 0:137634ff4186 252
ansond 0:137634ff4186 253 /**
ansond 0:137634ff4186 254 * \brief Read and update a seed file. Seed is added to this
ansond 0:137634ff4186 255 * instance
ansond 0:137634ff4186 256 *
ansond 0:137634ff4186 257 * \param ctx HMAC_DRBG context
ansond 0:137634ff4186 258 * \param path Name of the file
ansond 0:137634ff4186 259 *
ansond 0:137634ff4186 260 * \return 0 if successful, 1 on file error,
ansond 0:137634ff4186 261 * POLARSSL_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED or
ansond 0:137634ff4186 262 * POLARSSL_ERR_HMAC_DRBG_INPUT_TOO_BIG
ansond 0:137634ff4186 263 */
ansond 0:137634ff4186 264 int hmac_drbg_update_seed_file( hmac_drbg_context *ctx, const char *path );
ansond 0:137634ff4186 265 #endif /* POLARSSL_FS_IO */
ansond 0:137634ff4186 266
ansond 0:137634ff4186 267
ansond 0:137634ff4186 268 #if defined(POLARSSL_SELF_TEST)
ansond 0:137634ff4186 269 /**
ansond 0:137634ff4186 270 * \brief Checkup routine
ansond 0:137634ff4186 271 *
ansond 0:137634ff4186 272 * \return 0 if successful, or 1 if the test failed
ansond 0:137634ff4186 273 */
ansond 0:137634ff4186 274 int hmac_drbg_self_test( int verbose );
ansond 0:137634ff4186 275 #endif
ansond 0:137634ff4186 276
ansond 0:137634ff4186 277 #ifdef __cplusplus
ansond 0:137634ff4186 278 }
ansond 0:137634ff4186 279 #endif
ansond 0:137634ff4186 280
ansond 0:137634ff4186 281 #endif /* hmac_drbg.h */
ansond 0:137634ff4186 282