mbed client lightswitch demo

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of mbed-client-classic-example-lwip by Austin Blackstone

Committer:
mbedAustin
Date:
Thu Jun 09 17:08:36 2016 +0000
Revision:
11:cada08fc8a70
Commit for public Consumption

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbedAustin 11:cada08fc8a70 1 /**
mbedAustin 11:cada08fc8a70 2 * \file entropy.h
mbedAustin 11:cada08fc8a70 3 *
mbedAustin 11:cada08fc8a70 4 * \brief Entropy accumulator implementation
mbedAustin 11:cada08fc8a70 5 *
mbedAustin 11:cada08fc8a70 6 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
mbedAustin 11:cada08fc8a70 7 * SPDX-License-Identifier: Apache-2.0
mbedAustin 11:cada08fc8a70 8 *
mbedAustin 11:cada08fc8a70 9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
mbedAustin 11:cada08fc8a70 10 * not use this file except in compliance with the License.
mbedAustin 11:cada08fc8a70 11 * You may obtain a copy of the License at
mbedAustin 11:cada08fc8a70 12 *
mbedAustin 11:cada08fc8a70 13 * http://www.apache.org/licenses/LICENSE-2.0
mbedAustin 11:cada08fc8a70 14 *
mbedAustin 11:cada08fc8a70 15 * Unless required by applicable law or agreed to in writing, software
mbedAustin 11:cada08fc8a70 16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
mbedAustin 11:cada08fc8a70 17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbedAustin 11:cada08fc8a70 18 * See the License for the specific language governing permissions and
mbedAustin 11:cada08fc8a70 19 * limitations under the License.
mbedAustin 11:cada08fc8a70 20 *
mbedAustin 11:cada08fc8a70 21 * This file is part of mbed TLS (https://tls.mbed.org)
mbedAustin 11:cada08fc8a70 22 */
mbedAustin 11:cada08fc8a70 23 #ifndef MBEDTLS_ENTROPY_H
mbedAustin 11:cada08fc8a70 24 #define MBEDTLS_ENTROPY_H
mbedAustin 11:cada08fc8a70 25
mbedAustin 11:cada08fc8a70 26 #if !defined(MBEDTLS_CONFIG_FILE)
mbedAustin 11:cada08fc8a70 27 #include "config.h"
mbedAustin 11:cada08fc8a70 28 #else
mbedAustin 11:cada08fc8a70 29 #include MBEDTLS_CONFIG_FILE
mbedAustin 11:cada08fc8a70 30 #endif
mbedAustin 11:cada08fc8a70 31
mbedAustin 11:cada08fc8a70 32 #include <stddef.h>
mbedAustin 11:cada08fc8a70 33
mbedAustin 11:cada08fc8a70 34 #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256)
mbedAustin 11:cada08fc8a70 35 #include "sha512.h"
mbedAustin 11:cada08fc8a70 36 #define MBEDTLS_ENTROPY_SHA512_ACCUMULATOR
mbedAustin 11:cada08fc8a70 37 #else
mbedAustin 11:cada08fc8a70 38 #if defined(MBEDTLS_SHA256_C)
mbedAustin 11:cada08fc8a70 39 #define MBEDTLS_ENTROPY_SHA256_ACCUMULATOR
mbedAustin 11:cada08fc8a70 40 #include "sha256.h"
mbedAustin 11:cada08fc8a70 41 #endif
mbedAustin 11:cada08fc8a70 42 #endif
mbedAustin 11:cada08fc8a70 43
mbedAustin 11:cada08fc8a70 44 #if defined(MBEDTLS_THREADING_C)
mbedAustin 11:cada08fc8a70 45 #include "threading.h"
mbedAustin 11:cada08fc8a70 46 #endif
mbedAustin 11:cada08fc8a70 47
mbedAustin 11:cada08fc8a70 48 #if defined(MBEDTLS_HAVEGE_C)
mbedAustin 11:cada08fc8a70 49 #include "havege.h"
mbedAustin 11:cada08fc8a70 50 #endif
mbedAustin 11:cada08fc8a70 51
mbedAustin 11:cada08fc8a70 52 #define MBEDTLS_ERR_ENTROPY_SOURCE_FAILED -0x003C /**< Critical entropy source failure. */
mbedAustin 11:cada08fc8a70 53 #define MBEDTLS_ERR_ENTROPY_MAX_SOURCES -0x003E /**< No more sources can be added. */
mbedAustin 11:cada08fc8a70 54 #define MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED -0x0040 /**< No sources have been added to poll. */
mbedAustin 11:cada08fc8a70 55 #define MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE -0x003D /**< No strong sources have been added to poll. */
mbedAustin 11:cada08fc8a70 56 #define MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR -0x003F /**< Read/write error in file. */
mbedAustin 11:cada08fc8a70 57
mbedAustin 11:cada08fc8a70 58 /**
mbedAustin 11:cada08fc8a70 59 * \name SECTION: Module settings
mbedAustin 11:cada08fc8a70 60 *
mbedAustin 11:cada08fc8a70 61 * The configuration options you can set for this module are in this section.
mbedAustin 11:cada08fc8a70 62 * Either change them in config.h or define them on the compiler command line.
mbedAustin 11:cada08fc8a70 63 * \{
mbedAustin 11:cada08fc8a70 64 */
mbedAustin 11:cada08fc8a70 65
mbedAustin 11:cada08fc8a70 66 #if !defined(MBEDTLS_ENTROPY_MAX_SOURCES)
mbedAustin 11:cada08fc8a70 67 #define MBEDTLS_ENTROPY_MAX_SOURCES 20 /**< Maximum number of sources supported */
mbedAustin 11:cada08fc8a70 68 #endif
mbedAustin 11:cada08fc8a70 69
mbedAustin 11:cada08fc8a70 70 #if !defined(MBEDTLS_ENTROPY_MAX_GATHER)
mbedAustin 11:cada08fc8a70 71 #define MBEDTLS_ENTROPY_MAX_GATHER 128 /**< Maximum amount requested from entropy sources */
mbedAustin 11:cada08fc8a70 72 #endif
mbedAustin 11:cada08fc8a70 73
mbedAustin 11:cada08fc8a70 74 /* \} name SECTION: Module settings */
mbedAustin 11:cada08fc8a70 75
mbedAustin 11:cada08fc8a70 76 #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
mbedAustin 11:cada08fc8a70 77 #define MBEDTLS_ENTROPY_BLOCK_SIZE 64 /**< Block size of entropy accumulator (SHA-512) */
mbedAustin 11:cada08fc8a70 78 #else
mbedAustin 11:cada08fc8a70 79 #define MBEDTLS_ENTROPY_BLOCK_SIZE 32 /**< Block size of entropy accumulator (SHA-256) */
mbedAustin 11:cada08fc8a70 80 #endif
mbedAustin 11:cada08fc8a70 81
mbedAustin 11:cada08fc8a70 82 #define MBEDTLS_ENTROPY_MAX_SEED_SIZE 1024 /**< Maximum size of seed we read from seed file */
mbedAustin 11:cada08fc8a70 83 #define MBEDTLS_ENTROPY_SOURCE_MANUAL MBEDTLS_ENTROPY_MAX_SOURCES
mbedAustin 11:cada08fc8a70 84
mbedAustin 11:cada08fc8a70 85 #define MBEDTLS_ENTROPY_SOURCE_STRONG 1 /**< Entropy source is strong */
mbedAustin 11:cada08fc8a70 86 #define MBEDTLS_ENTROPY_SOURCE_WEAK 0 /**< Entropy source is weak */
mbedAustin 11:cada08fc8a70 87
mbedAustin 11:cada08fc8a70 88 #ifdef __cplusplus
mbedAustin 11:cada08fc8a70 89 extern "C" {
mbedAustin 11:cada08fc8a70 90 #endif
mbedAustin 11:cada08fc8a70 91
mbedAustin 11:cada08fc8a70 92 /**
mbedAustin 11:cada08fc8a70 93 * \brief Entropy poll callback pointer
mbedAustin 11:cada08fc8a70 94 *
mbedAustin 11:cada08fc8a70 95 * \param data Callback-specific data pointer
mbedAustin 11:cada08fc8a70 96 * \param output Data to fill
mbedAustin 11:cada08fc8a70 97 * \param len Maximum size to provide
mbedAustin 11:cada08fc8a70 98 * \param olen The actual amount of bytes put into the buffer (Can be 0)
mbedAustin 11:cada08fc8a70 99 *
mbedAustin 11:cada08fc8a70 100 * \return 0 if no critical failures occurred,
mbedAustin 11:cada08fc8a70 101 * MBEDTLS_ERR_ENTROPY_SOURCE_FAILED otherwise
mbedAustin 11:cada08fc8a70 102 */
mbedAustin 11:cada08fc8a70 103 typedef int (*mbedtls_entropy_f_source_ptr)(void *data, unsigned char *output, size_t len,
mbedAustin 11:cada08fc8a70 104 size_t *olen);
mbedAustin 11:cada08fc8a70 105
mbedAustin 11:cada08fc8a70 106 /**
mbedAustin 11:cada08fc8a70 107 * \brief Entropy source state
mbedAustin 11:cada08fc8a70 108 */
mbedAustin 11:cada08fc8a70 109 typedef struct
mbedAustin 11:cada08fc8a70 110 {
mbedAustin 11:cada08fc8a70 111 mbedtls_entropy_f_source_ptr f_source; /**< The entropy source callback */
mbedAustin 11:cada08fc8a70 112 void * p_source; /**< The callback data pointer */
mbedAustin 11:cada08fc8a70 113 size_t size; /**< Amount received in bytes */
mbedAustin 11:cada08fc8a70 114 size_t threshold; /**< Minimum bytes required before release */
mbedAustin 11:cada08fc8a70 115 int strong; /**< Is the source strong? */
mbedAustin 11:cada08fc8a70 116 }
mbedAustin 11:cada08fc8a70 117 mbedtls_entropy_source_state;
mbedAustin 11:cada08fc8a70 118
mbedAustin 11:cada08fc8a70 119 /**
mbedAustin 11:cada08fc8a70 120 * \brief Entropy context structure
mbedAustin 11:cada08fc8a70 121 */
mbedAustin 11:cada08fc8a70 122 typedef struct
mbedAustin 11:cada08fc8a70 123 {
mbedAustin 11:cada08fc8a70 124 #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
mbedAustin 11:cada08fc8a70 125 mbedtls_sha512_context accumulator;
mbedAustin 11:cada08fc8a70 126 #else
mbedAustin 11:cada08fc8a70 127 mbedtls_sha256_context accumulator;
mbedAustin 11:cada08fc8a70 128 #endif
mbedAustin 11:cada08fc8a70 129 int source_count;
mbedAustin 11:cada08fc8a70 130 mbedtls_entropy_source_state source[MBEDTLS_ENTROPY_MAX_SOURCES];
mbedAustin 11:cada08fc8a70 131 #if defined(MBEDTLS_HAVEGE_C)
mbedAustin 11:cada08fc8a70 132 mbedtls_havege_state havege_data;
mbedAustin 11:cada08fc8a70 133 #endif
mbedAustin 11:cada08fc8a70 134 #if defined(MBEDTLS_THREADING_C)
mbedAustin 11:cada08fc8a70 135 mbedtls_threading_mutex_t mutex; /*!< mutex */
mbedAustin 11:cada08fc8a70 136 #endif
mbedAustin 11:cada08fc8a70 137 }
mbedAustin 11:cada08fc8a70 138 mbedtls_entropy_context;
mbedAustin 11:cada08fc8a70 139
mbedAustin 11:cada08fc8a70 140 /**
mbedAustin 11:cada08fc8a70 141 * \brief Initialize the context
mbedAustin 11:cada08fc8a70 142 *
mbedAustin 11:cada08fc8a70 143 * \param ctx Entropy context to initialize
mbedAustin 11:cada08fc8a70 144 */
mbedAustin 11:cada08fc8a70 145 void mbedtls_entropy_init( mbedtls_entropy_context *ctx );
mbedAustin 11:cada08fc8a70 146
mbedAustin 11:cada08fc8a70 147 /**
mbedAustin 11:cada08fc8a70 148 * \brief Free the data in the context
mbedAustin 11:cada08fc8a70 149 *
mbedAustin 11:cada08fc8a70 150 * \param ctx Entropy context to free
mbedAustin 11:cada08fc8a70 151 */
mbedAustin 11:cada08fc8a70 152 void mbedtls_entropy_free( mbedtls_entropy_context *ctx );
mbedAustin 11:cada08fc8a70 153
mbedAustin 11:cada08fc8a70 154 /**
mbedAustin 11:cada08fc8a70 155 * \brief Adds an entropy source to poll
mbedAustin 11:cada08fc8a70 156 * (Thread-safe if MBEDTLS_THREADING_C is enabled)
mbedAustin 11:cada08fc8a70 157 *
mbedAustin 11:cada08fc8a70 158 * \param ctx Entropy context
mbedAustin 11:cada08fc8a70 159 * \param f_source Entropy function
mbedAustin 11:cada08fc8a70 160 * \param p_source Function data
mbedAustin 11:cada08fc8a70 161 * \param threshold Minimum required from source before entropy is released
mbedAustin 11:cada08fc8a70 162 * ( with mbedtls_entropy_func() ) (in bytes)
mbedAustin 11:cada08fc8a70 163 * \param strong MBEDTLS_ENTROPY_SOURCE_STRONG or
mbedAustin 11:cada08fc8a70 164 * MBEDTSL_ENTROPY_SOURCE_WEAK.
mbedAustin 11:cada08fc8a70 165 * At least one strong source needs to be added.
mbedAustin 11:cada08fc8a70 166 * Weaker sources (such as the cycle counter) can be used as
mbedAustin 11:cada08fc8a70 167 * a complement.
mbedAustin 11:cada08fc8a70 168 *
mbedAustin 11:cada08fc8a70 169 * \return 0 if successful or MBEDTLS_ERR_ENTROPY_MAX_SOURCES
mbedAustin 11:cada08fc8a70 170 */
mbedAustin 11:cada08fc8a70 171 int mbedtls_entropy_add_source( mbedtls_entropy_context *ctx,
mbedAustin 11:cada08fc8a70 172 mbedtls_entropy_f_source_ptr f_source, void *p_source,
mbedAustin 11:cada08fc8a70 173 size_t threshold, int strong );
mbedAustin 11:cada08fc8a70 174
mbedAustin 11:cada08fc8a70 175 /**
mbedAustin 11:cada08fc8a70 176 * \brief Trigger an extra gather poll for the accumulator
mbedAustin 11:cada08fc8a70 177 * (Thread-safe if MBEDTLS_THREADING_C is enabled)
mbedAustin 11:cada08fc8a70 178 *
mbedAustin 11:cada08fc8a70 179 * \param ctx Entropy context
mbedAustin 11:cada08fc8a70 180 *
mbedAustin 11:cada08fc8a70 181 * \return 0 if successful, or MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
mbedAustin 11:cada08fc8a70 182 */
mbedAustin 11:cada08fc8a70 183 int mbedtls_entropy_gather( mbedtls_entropy_context *ctx );
mbedAustin 11:cada08fc8a70 184
mbedAustin 11:cada08fc8a70 185 /**
mbedAustin 11:cada08fc8a70 186 * \brief Retrieve entropy from the accumulator
mbedAustin 11:cada08fc8a70 187 * (Maximum length: MBEDTLS_ENTROPY_BLOCK_SIZE)
mbedAustin 11:cada08fc8a70 188 * (Thread-safe if MBEDTLS_THREADING_C is enabled)
mbedAustin 11:cada08fc8a70 189 *
mbedAustin 11:cada08fc8a70 190 * \param data Entropy context
mbedAustin 11:cada08fc8a70 191 * \param output Buffer to fill
mbedAustin 11:cada08fc8a70 192 * \param len Number of bytes desired, must be at most MBEDTLS_ENTROPY_BLOCK_SIZE
mbedAustin 11:cada08fc8a70 193 *
mbedAustin 11:cada08fc8a70 194 * \return 0 if successful, or MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
mbedAustin 11:cada08fc8a70 195 */
mbedAustin 11:cada08fc8a70 196 int mbedtls_entropy_func( void *data, unsigned char *output, size_t len );
mbedAustin 11:cada08fc8a70 197
mbedAustin 11:cada08fc8a70 198 /**
mbedAustin 11:cada08fc8a70 199 * \brief Add data to the accumulator manually
mbedAustin 11:cada08fc8a70 200 * (Thread-safe if MBEDTLS_THREADING_C is enabled)
mbedAustin 11:cada08fc8a70 201 *
mbedAustin 11:cada08fc8a70 202 * \param ctx Entropy context
mbedAustin 11:cada08fc8a70 203 * \param data Data to add
mbedAustin 11:cada08fc8a70 204 * \param len Length of data
mbedAustin 11:cada08fc8a70 205 *
mbedAustin 11:cada08fc8a70 206 * \return 0 if successful
mbedAustin 11:cada08fc8a70 207 */
mbedAustin 11:cada08fc8a70 208 int mbedtls_entropy_update_manual( mbedtls_entropy_context *ctx,
mbedAustin 11:cada08fc8a70 209 const unsigned char *data, size_t len );
mbedAustin 11:cada08fc8a70 210
mbedAustin 11:cada08fc8a70 211 #if defined(MBEDTLS_FS_IO)
mbedAustin 11:cada08fc8a70 212 /**
mbedAustin 11:cada08fc8a70 213 * \brief Write a seed file
mbedAustin 11:cada08fc8a70 214 *
mbedAustin 11:cada08fc8a70 215 * \param ctx Entropy context
mbedAustin 11:cada08fc8a70 216 * \param path Name of the file
mbedAustin 11:cada08fc8a70 217 *
mbedAustin 11:cada08fc8a70 218 * \return 0 if successful,
mbedAustin 11:cada08fc8a70 219 * MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR on file error, or
mbedAustin 11:cada08fc8a70 220 * MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
mbedAustin 11:cada08fc8a70 221 */
mbedAustin 11:cada08fc8a70 222 int mbedtls_entropy_write_seed_file( mbedtls_entropy_context *ctx, const char *path );
mbedAustin 11:cada08fc8a70 223
mbedAustin 11:cada08fc8a70 224 /**
mbedAustin 11:cada08fc8a70 225 * \brief Read and update a seed file. Seed is added to this
mbedAustin 11:cada08fc8a70 226 * instance. No more than MBEDTLS_ENTROPY_MAX_SEED_SIZE bytes are
mbedAustin 11:cada08fc8a70 227 * read from the seed file. The rest is ignored.
mbedAustin 11:cada08fc8a70 228 *
mbedAustin 11:cada08fc8a70 229 * \param ctx Entropy context
mbedAustin 11:cada08fc8a70 230 * \param path Name of the file
mbedAustin 11:cada08fc8a70 231 *
mbedAustin 11:cada08fc8a70 232 * \return 0 if successful,
mbedAustin 11:cada08fc8a70 233 * MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR on file error,
mbedAustin 11:cada08fc8a70 234 * MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
mbedAustin 11:cada08fc8a70 235 */
mbedAustin 11:cada08fc8a70 236 int mbedtls_entropy_update_seed_file( mbedtls_entropy_context *ctx, const char *path );
mbedAustin 11:cada08fc8a70 237 #endif /* MBEDTLS_FS_IO */
mbedAustin 11:cada08fc8a70 238
mbedAustin 11:cada08fc8a70 239 #if defined(MBEDTLS_SELF_TEST)
mbedAustin 11:cada08fc8a70 240 /**
mbedAustin 11:cada08fc8a70 241 * \brief Checkup routine
mbedAustin 11:cada08fc8a70 242 *
mbedAustin 11:cada08fc8a70 243 * \return 0 if successful, or 1 if a test failed
mbedAustin 11:cada08fc8a70 244 */
mbedAustin 11:cada08fc8a70 245 int mbedtls_entropy_self_test( int verbose );
mbedAustin 11:cada08fc8a70 246 #endif /* MBEDTLS_SELF_TEST */
mbedAustin 11:cada08fc8a70 247
mbedAustin 11:cada08fc8a70 248 #ifdef __cplusplus
mbedAustin 11:cada08fc8a70 249 }
mbedAustin 11:cada08fc8a70 250 #endif
mbedAustin 11:cada08fc8a70 251
mbedAustin 11:cada08fc8a70 252 #endif /* entropy.h */