mbed TLS Build

Dependents:   Slave-prot-prod

Committer:
markrad
Date:
Thu Jan 05 00:18:44 2017 +0000
Revision:
0:cdf462088d13
Initial commit

Who changed what in which revision?

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