Avnet / WNCInterface

Dependencies:   WncControllerK64F

Dependents:   WNCProximityMqtt Pubnub_ATT_IoT_SK_WNC_sync BluemixDemo BluemixQS ... more

See the WNCInterface README in the Wiki tab for detailed information on this library.

Committer:
JMF
Date:
Fri Dec 02 16:53:14 2016 +0000
Revision:
21:552b630becf2
Parent:
12:0071cb144c7a
Adding HTTPS support

Who changed what in which revision?

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