mbed TLS Build

Dependents:   Slave-prot-prod

Committer:
williequesada
Date:
Tue Jun 04 16:03:38 2019 +0000
Revision:
1:1a219dea6cb5
Parent:
0:cdf462088d13
compartir a Pablo

Who changed what in which revision?

UserRevisionLine numberNew contents of line
markrad 0:cdf462088d13 1 /**
markrad 0:cdf462088d13 2 * \file ctr_drbg.h
markrad 0:cdf462088d13 3 *
markrad 0:cdf462088d13 4 * \brief CTR_DRBG based on AES-256 (NIST SP 800-90)
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_CTR_DRBG_H
markrad 0:cdf462088d13 24 #define MBEDTLS_CTR_DRBG_H
markrad 0:cdf462088d13 25
markrad 0:cdf462088d13 26 #include "aes.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 #define MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED -0x0034 /**< The entropy source failed. */
markrad 0:cdf462088d13 33 #define MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG -0x0036 /**< Too many random requested in single call. */
markrad 0:cdf462088d13 34 #define MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG -0x0038 /**< Input too large (Entropy + additional). */
markrad 0:cdf462088d13 35 #define MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR -0x003A /**< Read/write error in file. */
markrad 0:cdf462088d13 36
markrad 0:cdf462088d13 37 #define MBEDTLS_CTR_DRBG_BLOCKSIZE 16 /**< Block size used by the cipher */
markrad 0:cdf462088d13 38 #define MBEDTLS_CTR_DRBG_KEYSIZE 32 /**< Key size used by the cipher */
markrad 0:cdf462088d13 39 #define MBEDTLS_CTR_DRBG_KEYBITS ( MBEDTLS_CTR_DRBG_KEYSIZE * 8 )
markrad 0:cdf462088d13 40 #define MBEDTLS_CTR_DRBG_SEEDLEN ( MBEDTLS_CTR_DRBG_KEYSIZE + MBEDTLS_CTR_DRBG_BLOCKSIZE )
markrad 0:cdf462088d13 41 /**< The seed length (counter + AES key) */
markrad 0:cdf462088d13 42
markrad 0:cdf462088d13 43 /**
markrad 0:cdf462088d13 44 * \name SECTION: Module settings
markrad 0:cdf462088d13 45 *
markrad 0:cdf462088d13 46 * The configuration options you can set for this module are in this section.
markrad 0:cdf462088d13 47 * Either change them in config.h or define them on the compiler command line.
markrad 0:cdf462088d13 48 * \{
markrad 0:cdf462088d13 49 */
markrad 0:cdf462088d13 50
markrad 0:cdf462088d13 51 #if !defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN)
markrad 0:cdf462088d13 52 #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256)
markrad 0:cdf462088d13 53 #define MBEDTLS_CTR_DRBG_ENTROPY_LEN 48 /**< Amount of entropy used per seed by default (48 with SHA-512, 32 with SHA-256) */
markrad 0:cdf462088d13 54 #else
markrad 0:cdf462088d13 55 #define MBEDTLS_CTR_DRBG_ENTROPY_LEN 32 /**< Amount of entropy used per seed by default (48 with SHA-512, 32 with SHA-256) */
markrad 0:cdf462088d13 56 #endif
markrad 0:cdf462088d13 57 #endif
markrad 0:cdf462088d13 58
markrad 0:cdf462088d13 59 #if !defined(MBEDTLS_CTR_DRBG_RESEED_INTERVAL)
markrad 0:cdf462088d13 60 #define MBEDTLS_CTR_DRBG_RESEED_INTERVAL 10000 /**< Interval before reseed is performed by default */
markrad 0:cdf462088d13 61 #endif
markrad 0:cdf462088d13 62
markrad 0:cdf462088d13 63 #if !defined(MBEDTLS_CTR_DRBG_MAX_INPUT)
markrad 0:cdf462088d13 64 #define MBEDTLS_CTR_DRBG_MAX_INPUT 256 /**< Maximum number of additional input bytes */
markrad 0:cdf462088d13 65 #endif
markrad 0:cdf462088d13 66
markrad 0:cdf462088d13 67 #if !defined(MBEDTLS_CTR_DRBG_MAX_REQUEST)
markrad 0:cdf462088d13 68 #define MBEDTLS_CTR_DRBG_MAX_REQUEST 1024 /**< Maximum number of requested bytes per call */
markrad 0:cdf462088d13 69 #endif
markrad 0:cdf462088d13 70
markrad 0:cdf462088d13 71 #if !defined(MBEDTLS_CTR_DRBG_MAX_SEED_INPUT)
markrad 0:cdf462088d13 72 #define MBEDTLS_CTR_DRBG_MAX_SEED_INPUT 384 /**< Maximum size of (re)seed buffer */
markrad 0:cdf462088d13 73 #endif
markrad 0:cdf462088d13 74
markrad 0:cdf462088d13 75 /* \} name SECTION: Module settings */
markrad 0:cdf462088d13 76
markrad 0:cdf462088d13 77 #define MBEDTLS_CTR_DRBG_PR_OFF 0 /**< No prediction resistance */
markrad 0:cdf462088d13 78 #define MBEDTLS_CTR_DRBG_PR_ON 1 /**< Prediction resistance enabled */
markrad 0:cdf462088d13 79
markrad 0:cdf462088d13 80 #ifdef __cplusplus
markrad 0:cdf462088d13 81 extern "C" {
markrad 0:cdf462088d13 82 #endif
markrad 0:cdf462088d13 83
markrad 0:cdf462088d13 84 /**
markrad 0:cdf462088d13 85 * \brief CTR_DRBG context structure
markrad 0:cdf462088d13 86 */
markrad 0:cdf462088d13 87 typedef struct
markrad 0:cdf462088d13 88 {
markrad 0:cdf462088d13 89 unsigned char counter[16]; /*!< counter (V) */
markrad 0:cdf462088d13 90 int reseed_counter; /*!< reseed counter */
markrad 0:cdf462088d13 91 int prediction_resistance; /*!< enable prediction resistance (Automatic
markrad 0:cdf462088d13 92 reseed before every random generation) */
markrad 0:cdf462088d13 93 size_t entropy_len; /*!< amount of entropy grabbed on each
markrad 0:cdf462088d13 94 (re)seed */
markrad 0:cdf462088d13 95 int reseed_interval; /*!< reseed interval */
markrad 0:cdf462088d13 96
markrad 0:cdf462088d13 97 mbedtls_aes_context aes_ctx; /*!< AES context */
markrad 0:cdf462088d13 98
markrad 0:cdf462088d13 99 /*
markrad 0:cdf462088d13 100 * Callbacks (Entropy)
markrad 0:cdf462088d13 101 */
markrad 0:cdf462088d13 102 int (*f_entropy)(void *, unsigned char *, size_t);
markrad 0:cdf462088d13 103
markrad 0:cdf462088d13 104 void *p_entropy; /*!< context for the entropy function */
markrad 0:cdf462088d13 105
markrad 0:cdf462088d13 106 #if defined(MBEDTLS_THREADING_C)
markrad 0:cdf462088d13 107 mbedtls_threading_mutex_t mutex;
markrad 0:cdf462088d13 108 #endif
markrad 0:cdf462088d13 109 }
markrad 0:cdf462088d13 110 mbedtls_ctr_drbg_context;
markrad 0:cdf462088d13 111
markrad 0:cdf462088d13 112 /**
markrad 0:cdf462088d13 113 * \brief CTR_DRBG context initialization
markrad 0:cdf462088d13 114 * Makes the context ready for mbedtls_ctr_drbg_seed() or
markrad 0:cdf462088d13 115 * mbedtls_ctr_drbg_free().
markrad 0:cdf462088d13 116 *
markrad 0:cdf462088d13 117 * \param ctx CTR_DRBG context to be initialized
markrad 0:cdf462088d13 118 */
markrad 0:cdf462088d13 119 void mbedtls_ctr_drbg_init( mbedtls_ctr_drbg_context *ctx );
markrad 0:cdf462088d13 120
markrad 0:cdf462088d13 121 /**
markrad 0:cdf462088d13 122 * \brief CTR_DRBG initial seeding
markrad 0:cdf462088d13 123 * Seed and setup entropy source for future reseeds.
markrad 0:cdf462088d13 124 *
markrad 0:cdf462088d13 125 * Note: Personalization data can be provided in addition to the more generic
markrad 0:cdf462088d13 126 * entropy source to make this instantiation as unique as possible.
markrad 0:cdf462088d13 127 *
markrad 0:cdf462088d13 128 * \param ctx CTR_DRBG context to be seeded
markrad 0:cdf462088d13 129 * \param f_entropy Entropy callback (p_entropy, buffer to fill, buffer
markrad 0:cdf462088d13 130 * length)
markrad 0:cdf462088d13 131 * \param p_entropy Entropy context
markrad 0:cdf462088d13 132 * \param custom Personalization data (Device specific identifiers)
markrad 0:cdf462088d13 133 * (Can be NULL)
markrad 0:cdf462088d13 134 * \param len Length of personalization data
markrad 0:cdf462088d13 135 *
markrad 0:cdf462088d13 136 * \return 0 if successful, or
markrad 0:cdf462088d13 137 * MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED
markrad 0:cdf462088d13 138 */
markrad 0:cdf462088d13 139 int mbedtls_ctr_drbg_seed( mbedtls_ctr_drbg_context *ctx,
markrad 0:cdf462088d13 140 int (*f_entropy)(void *, unsigned char *, size_t),
markrad 0:cdf462088d13 141 void *p_entropy,
markrad 0:cdf462088d13 142 const unsigned char *custom,
markrad 0:cdf462088d13 143 size_t len );
markrad 0:cdf462088d13 144
markrad 0:cdf462088d13 145 /**
markrad 0:cdf462088d13 146 * \brief Clear CTR_CRBG context data
markrad 0:cdf462088d13 147 *
markrad 0:cdf462088d13 148 * \param ctx CTR_DRBG context to clear
markrad 0:cdf462088d13 149 */
markrad 0:cdf462088d13 150 void mbedtls_ctr_drbg_free( mbedtls_ctr_drbg_context *ctx );
markrad 0:cdf462088d13 151
markrad 0:cdf462088d13 152 /**
markrad 0:cdf462088d13 153 * \brief Enable / disable prediction resistance (Default: Off)
markrad 0:cdf462088d13 154 *
markrad 0:cdf462088d13 155 * Note: If enabled, entropy is used for ctx->entropy_len before each call!
markrad 0:cdf462088d13 156 * Only use this if you have ample supply of good entropy!
markrad 0:cdf462088d13 157 *
markrad 0:cdf462088d13 158 * \param ctx CTR_DRBG context
markrad 0:cdf462088d13 159 * \param resistance MBEDTLS_CTR_DRBG_PR_ON or MBEDTLS_CTR_DRBG_PR_OFF
markrad 0:cdf462088d13 160 */
markrad 0:cdf462088d13 161 void mbedtls_ctr_drbg_set_prediction_resistance( mbedtls_ctr_drbg_context *ctx,
markrad 0:cdf462088d13 162 int resistance );
markrad 0:cdf462088d13 163
markrad 0:cdf462088d13 164 /**
markrad 0:cdf462088d13 165 * \brief Set the amount of entropy grabbed on each (re)seed
markrad 0:cdf462088d13 166 * (Default: MBEDTLS_CTR_DRBG_ENTROPY_LEN)
markrad 0:cdf462088d13 167 *
markrad 0:cdf462088d13 168 * \param ctx CTR_DRBG context
markrad 0:cdf462088d13 169 * \param len Amount of entropy to grab
markrad 0:cdf462088d13 170 */
markrad 0:cdf462088d13 171 void mbedtls_ctr_drbg_set_entropy_len( mbedtls_ctr_drbg_context *ctx,
markrad 0:cdf462088d13 172 size_t len );
markrad 0:cdf462088d13 173
markrad 0:cdf462088d13 174 /**
markrad 0:cdf462088d13 175 * \brief Set the reseed interval
markrad 0:cdf462088d13 176 * (Default: MBEDTLS_CTR_DRBG_RESEED_INTERVAL)
markrad 0:cdf462088d13 177 *
markrad 0:cdf462088d13 178 * \param ctx CTR_DRBG context
markrad 0:cdf462088d13 179 * \param interval Reseed interval
markrad 0:cdf462088d13 180 */
markrad 0:cdf462088d13 181 void mbedtls_ctr_drbg_set_reseed_interval( mbedtls_ctr_drbg_context *ctx,
markrad 0:cdf462088d13 182 int interval );
markrad 0:cdf462088d13 183
markrad 0:cdf462088d13 184 /**
markrad 0:cdf462088d13 185 * \brief CTR_DRBG reseeding (extracts data from entropy source)
markrad 0:cdf462088d13 186 *
markrad 0:cdf462088d13 187 * \param ctx CTR_DRBG context
markrad 0:cdf462088d13 188 * \param additional Additional data to add to state (Can be NULL)
markrad 0:cdf462088d13 189 * \param len Length of additional data
markrad 0:cdf462088d13 190 *
markrad 0:cdf462088d13 191 * \return 0 if successful, or
markrad 0:cdf462088d13 192 * MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED
markrad 0:cdf462088d13 193 */
markrad 0:cdf462088d13 194 int mbedtls_ctr_drbg_reseed( mbedtls_ctr_drbg_context *ctx,
markrad 0:cdf462088d13 195 const unsigned char *additional, size_t len );
markrad 0:cdf462088d13 196
markrad 0:cdf462088d13 197 /**
markrad 0:cdf462088d13 198 * \brief CTR_DRBG update state
markrad 0:cdf462088d13 199 *
markrad 0:cdf462088d13 200 * \param ctx CTR_DRBG context
markrad 0:cdf462088d13 201 * \param additional Additional data to update state with
markrad 0:cdf462088d13 202 * \param add_len Length of additional data
markrad 0:cdf462088d13 203 *
markrad 0:cdf462088d13 204 * \note If add_len is greater than MBEDTLS_CTR_DRBG_MAX_SEED_INPUT,
markrad 0:cdf462088d13 205 * only the first MBEDTLS_CTR_DRBG_MAX_SEED_INPUT bytes are used,
markrad 0:cdf462088d13 206 * the remaining ones are silently discarded.
markrad 0:cdf462088d13 207 */
markrad 0:cdf462088d13 208 void mbedtls_ctr_drbg_update( mbedtls_ctr_drbg_context *ctx,
markrad 0:cdf462088d13 209 const unsigned char *additional, size_t add_len );
markrad 0:cdf462088d13 210
markrad 0:cdf462088d13 211 /**
markrad 0:cdf462088d13 212 * \brief CTR_DRBG generate random with additional update input
markrad 0:cdf462088d13 213 *
markrad 0:cdf462088d13 214 * Note: Automatically reseeds if reseed_counter is reached.
markrad 0:cdf462088d13 215 *
markrad 0:cdf462088d13 216 * \param p_rng CTR_DRBG context
markrad 0:cdf462088d13 217 * \param output Buffer to fill
markrad 0:cdf462088d13 218 * \param output_len Length of the buffer
markrad 0:cdf462088d13 219 * \param additional Additional data to update with (Can be NULL)
markrad 0:cdf462088d13 220 * \param add_len Length of additional data
markrad 0:cdf462088d13 221 *
markrad 0:cdf462088d13 222 * \return 0 if successful, or
markrad 0:cdf462088d13 223 * MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED, or
markrad 0:cdf462088d13 224 * MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG
markrad 0:cdf462088d13 225 */
markrad 0:cdf462088d13 226 int mbedtls_ctr_drbg_random_with_add( void *p_rng,
markrad 0:cdf462088d13 227 unsigned char *output, size_t output_len,
markrad 0:cdf462088d13 228 const unsigned char *additional, size_t add_len );
markrad 0:cdf462088d13 229
markrad 0:cdf462088d13 230 /**
markrad 0:cdf462088d13 231 * \brief CTR_DRBG generate random
markrad 0:cdf462088d13 232 *
markrad 0:cdf462088d13 233 * Note: Automatically reseeds if reseed_counter is reached.
markrad 0:cdf462088d13 234 *
markrad 0:cdf462088d13 235 * \param p_rng CTR_DRBG context
markrad 0:cdf462088d13 236 * \param output Buffer to fill
markrad 0:cdf462088d13 237 * \param output_len Length of the buffer
markrad 0:cdf462088d13 238 *
markrad 0:cdf462088d13 239 * \return 0 if successful, or
markrad 0:cdf462088d13 240 * MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED, or
markrad 0:cdf462088d13 241 * MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG
markrad 0:cdf462088d13 242 */
markrad 0:cdf462088d13 243 int mbedtls_ctr_drbg_random( void *p_rng,
markrad 0:cdf462088d13 244 unsigned char *output, size_t output_len );
markrad 0:cdf462088d13 245
markrad 0:cdf462088d13 246 #if defined(MBEDTLS_FS_IO)
markrad 0:cdf462088d13 247 /**
markrad 0:cdf462088d13 248 * \brief Write a seed file
markrad 0:cdf462088d13 249 *
markrad 0:cdf462088d13 250 * \param ctx CTR_DRBG context
markrad 0:cdf462088d13 251 * \param path Name of the file
markrad 0:cdf462088d13 252 *
markrad 0:cdf462088d13 253 * \return 0 if successful,
markrad 0:cdf462088d13 254 * MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR on file error, or
markrad 0:cdf462088d13 255 * MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED
markrad 0:cdf462088d13 256 */
markrad 0:cdf462088d13 257 int mbedtls_ctr_drbg_write_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
markrad 0:cdf462088d13 258
markrad 0:cdf462088d13 259 /**
markrad 0:cdf462088d13 260 * \brief Read and update a seed file. Seed is added to this
markrad 0:cdf462088d13 261 * instance
markrad 0:cdf462088d13 262 *
markrad 0:cdf462088d13 263 * \param ctx CTR_DRBG context
markrad 0:cdf462088d13 264 * \param path Name of the file
markrad 0:cdf462088d13 265 *
markrad 0:cdf462088d13 266 * \return 0 if successful,
markrad 0:cdf462088d13 267 * MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR on file error,
markrad 0:cdf462088d13 268 * MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or
markrad 0:cdf462088d13 269 * MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG
markrad 0:cdf462088d13 270 */
markrad 0:cdf462088d13 271 int mbedtls_ctr_drbg_update_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
markrad 0:cdf462088d13 272 #endif /* MBEDTLS_FS_IO */
markrad 0:cdf462088d13 273
markrad 0:cdf462088d13 274 /**
markrad 0:cdf462088d13 275 * \brief Checkup routine
markrad 0:cdf462088d13 276 *
markrad 0:cdf462088d13 277 * \return 0 if successful, or 1 if the test failed
markrad 0:cdf462088d13 278 */
markrad 0:cdf462088d13 279 int mbedtls_ctr_drbg_self_test( int verbose );
markrad 0:cdf462088d13 280
markrad 0:cdf462088d13 281 /* Internal functions (do not call directly) */
markrad 0:cdf462088d13 282 int mbedtls_ctr_drbg_seed_entropy_len( mbedtls_ctr_drbg_context *,
markrad 0:cdf462088d13 283 int (*)(void *, unsigned char *, size_t), void *,
markrad 0:cdf462088d13 284 const unsigned char *, size_t, size_t );
markrad 0:cdf462088d13 285
markrad 0:cdf462088d13 286 #ifdef __cplusplus
markrad 0:cdf462088d13 287 }
markrad 0:cdf462088d13 288 #endif
markrad 0:cdf462088d13 289
markrad 0:cdf462088d13 290 #endif /* ctr_drbg.h */