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 entropy.h
markrad 0:cdf462088d13 3 *
markrad 0:cdf462088d13 4 * \brief Entropy accumulator implementation
markrad 0:cdf462088d13 5 *
markrad 0:cdf462088d13 6 * Copyright (C) 2006-2016, 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_ENTROPY_H
markrad 0:cdf462088d13 24 #define MBEDTLS_ENTROPY_H
markrad 0:cdf462088d13 25
markrad 0:cdf462088d13 26 #if !defined(MBEDTLS_CONFIG_FILE)
markrad 0:cdf462088d13 27 #include "config.h"
markrad 0:cdf462088d13 28 #else
markrad 0:cdf462088d13 29 #include MBEDTLS_CONFIG_FILE
markrad 0:cdf462088d13 30 #endif
markrad 0:cdf462088d13 31
markrad 0:cdf462088d13 32 #include <stddef.h>
markrad 0:cdf462088d13 33
markrad 0:cdf462088d13 34 #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256)
markrad 0:cdf462088d13 35 #include "sha512.h"
markrad 0:cdf462088d13 36 #define MBEDTLS_ENTROPY_SHA512_ACCUMULATOR
markrad 0:cdf462088d13 37 #else
markrad 0:cdf462088d13 38 #if defined(MBEDTLS_SHA256_C)
markrad 0:cdf462088d13 39 #define MBEDTLS_ENTROPY_SHA256_ACCUMULATOR
markrad 0:cdf462088d13 40 #include "sha256.h"
markrad 0:cdf462088d13 41 #endif
markrad 0:cdf462088d13 42 #endif
markrad 0:cdf462088d13 43
markrad 0:cdf462088d13 44 #if defined(MBEDTLS_THREADING_C)
markrad 0:cdf462088d13 45 #include "threading.h"
markrad 0:cdf462088d13 46 #endif
markrad 0:cdf462088d13 47
markrad 0:cdf462088d13 48 #if defined(MBEDTLS_HAVEGE_C)
markrad 0:cdf462088d13 49 #include "havege.h"
markrad 0:cdf462088d13 50 #endif
markrad 0:cdf462088d13 51
markrad 0:cdf462088d13 52 #define MBEDTLS_ERR_ENTROPY_SOURCE_FAILED -0x003C /**< Critical entropy source failure. */
markrad 0:cdf462088d13 53 #define MBEDTLS_ERR_ENTROPY_MAX_SOURCES -0x003E /**< No more sources can be added. */
markrad 0:cdf462088d13 54 #define MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED -0x0040 /**< No sources have been added to poll. */
markrad 0:cdf462088d13 55 #define MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE -0x003D /**< No strong sources have been added to poll. */
markrad 0:cdf462088d13 56 #define MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR -0x003F /**< Read/write error in file. */
markrad 0:cdf462088d13 57
markrad 0:cdf462088d13 58 /**
markrad 0:cdf462088d13 59 * \name SECTION: Module settings
markrad 0:cdf462088d13 60 *
markrad 0:cdf462088d13 61 * The configuration options you can set for this module are in this section.
markrad 0:cdf462088d13 62 * Either change them in config.h or define them on the compiler command line.
markrad 0:cdf462088d13 63 * \{
markrad 0:cdf462088d13 64 */
markrad 0:cdf462088d13 65
markrad 0:cdf462088d13 66 #if !defined(MBEDTLS_ENTROPY_MAX_SOURCES)
markrad 0:cdf462088d13 67 #define MBEDTLS_ENTROPY_MAX_SOURCES 20 /**< Maximum number of sources supported */
markrad 0:cdf462088d13 68 #endif
markrad 0:cdf462088d13 69
markrad 0:cdf462088d13 70 #if !defined(MBEDTLS_ENTROPY_MAX_GATHER)
markrad 0:cdf462088d13 71 #define MBEDTLS_ENTROPY_MAX_GATHER 128 /**< Maximum amount requested from entropy sources */
markrad 0:cdf462088d13 72 #endif
markrad 0:cdf462088d13 73
markrad 0:cdf462088d13 74 /* \} name SECTION: Module settings */
markrad 0:cdf462088d13 75
markrad 0:cdf462088d13 76 #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
markrad 0:cdf462088d13 77 #define MBEDTLS_ENTROPY_BLOCK_SIZE 64 /**< Block size of entropy accumulator (SHA-512) */
markrad 0:cdf462088d13 78 #else
markrad 0:cdf462088d13 79 #define MBEDTLS_ENTROPY_BLOCK_SIZE 32 /**< Block size of entropy accumulator (SHA-256) */
markrad 0:cdf462088d13 80 #endif
markrad 0:cdf462088d13 81
markrad 0:cdf462088d13 82 #define MBEDTLS_ENTROPY_MAX_SEED_SIZE 1024 /**< Maximum size of seed we read from seed file */
markrad 0:cdf462088d13 83 #define MBEDTLS_ENTROPY_SOURCE_MANUAL MBEDTLS_ENTROPY_MAX_SOURCES
markrad 0:cdf462088d13 84
markrad 0:cdf462088d13 85 #define MBEDTLS_ENTROPY_SOURCE_STRONG 1 /**< Entropy source is strong */
markrad 0:cdf462088d13 86 #define MBEDTLS_ENTROPY_SOURCE_WEAK 0 /**< Entropy source is weak */
markrad 0:cdf462088d13 87
markrad 0:cdf462088d13 88 #ifdef __cplusplus
markrad 0:cdf462088d13 89 extern "C" {
markrad 0:cdf462088d13 90 #endif
markrad 0:cdf462088d13 91
markrad 0:cdf462088d13 92 /**
markrad 0:cdf462088d13 93 * \brief Entropy poll callback pointer
markrad 0:cdf462088d13 94 *
markrad 0:cdf462088d13 95 * \param data Callback-specific data pointer
markrad 0:cdf462088d13 96 * \param output Data to fill
markrad 0:cdf462088d13 97 * \param len Maximum size to provide
markrad 0:cdf462088d13 98 * \param olen The actual amount of bytes put into the buffer (Can be 0)
markrad 0:cdf462088d13 99 *
markrad 0:cdf462088d13 100 * \return 0 if no critical failures occurred,
markrad 0:cdf462088d13 101 * MBEDTLS_ERR_ENTROPY_SOURCE_FAILED otherwise
markrad 0:cdf462088d13 102 */
markrad 0:cdf462088d13 103 typedef int (*mbedtls_entropy_f_source_ptr)(void *data, unsigned char *output, size_t len,
markrad 0:cdf462088d13 104 size_t *olen);
markrad 0:cdf462088d13 105
markrad 0:cdf462088d13 106 /**
markrad 0:cdf462088d13 107 * \brief Entropy source state
markrad 0:cdf462088d13 108 */
markrad 0:cdf462088d13 109 typedef struct
markrad 0:cdf462088d13 110 {
markrad 0:cdf462088d13 111 mbedtls_entropy_f_source_ptr f_source; /**< The entropy source callback */
markrad 0:cdf462088d13 112 void * p_source; /**< The callback data pointer */
markrad 0:cdf462088d13 113 size_t size; /**< Amount received in bytes */
markrad 0:cdf462088d13 114 size_t threshold; /**< Minimum bytes required before release */
markrad 0:cdf462088d13 115 int strong; /**< Is the source strong? */
markrad 0:cdf462088d13 116 }
markrad 0:cdf462088d13 117 mbedtls_entropy_source_state;
markrad 0:cdf462088d13 118
markrad 0:cdf462088d13 119 /**
markrad 0:cdf462088d13 120 * \brief Entropy context structure
markrad 0:cdf462088d13 121 */
markrad 0:cdf462088d13 122 typedef struct
markrad 0:cdf462088d13 123 {
markrad 0:cdf462088d13 124 #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
markrad 0:cdf462088d13 125 mbedtls_sha512_context accumulator;
markrad 0:cdf462088d13 126 #else
markrad 0:cdf462088d13 127 mbedtls_sha256_context accumulator;
markrad 0:cdf462088d13 128 #endif
markrad 0:cdf462088d13 129 int source_count;
markrad 0:cdf462088d13 130 mbedtls_entropy_source_state source[MBEDTLS_ENTROPY_MAX_SOURCES];
markrad 0:cdf462088d13 131 #if defined(MBEDTLS_HAVEGE_C)
markrad 0:cdf462088d13 132 mbedtls_havege_state havege_data;
markrad 0:cdf462088d13 133 #endif
markrad 0:cdf462088d13 134 #if defined(MBEDTLS_THREADING_C)
markrad 0:cdf462088d13 135 mbedtls_threading_mutex_t mutex; /*!< mutex */
markrad 0:cdf462088d13 136 #endif
markrad 0:cdf462088d13 137 #if defined(MBEDTLS_ENTROPY_NV_SEED)
markrad 0:cdf462088d13 138 int initial_entropy_run;
markrad 0:cdf462088d13 139 #endif
markrad 0:cdf462088d13 140 }
markrad 0:cdf462088d13 141 mbedtls_entropy_context;
markrad 0:cdf462088d13 142
markrad 0:cdf462088d13 143 /**
markrad 0:cdf462088d13 144 * \brief Initialize the context
markrad 0:cdf462088d13 145 *
markrad 0:cdf462088d13 146 * \param ctx Entropy context to initialize
markrad 0:cdf462088d13 147 */
markrad 0:cdf462088d13 148 void mbedtls_entropy_init( mbedtls_entropy_context *ctx );
markrad 0:cdf462088d13 149
markrad 0:cdf462088d13 150 /**
markrad 0:cdf462088d13 151 * \brief Free the data in the context
markrad 0:cdf462088d13 152 *
markrad 0:cdf462088d13 153 * \param ctx Entropy context to free
markrad 0:cdf462088d13 154 */
markrad 0:cdf462088d13 155 void mbedtls_entropy_free( mbedtls_entropy_context *ctx );
markrad 0:cdf462088d13 156
markrad 0:cdf462088d13 157 /**
markrad 0:cdf462088d13 158 * \brief Adds an entropy source to poll
markrad 0:cdf462088d13 159 * (Thread-safe if MBEDTLS_THREADING_C is enabled)
markrad 0:cdf462088d13 160 *
markrad 0:cdf462088d13 161 * \param ctx Entropy context
markrad 0:cdf462088d13 162 * \param f_source Entropy function
markrad 0:cdf462088d13 163 * \param p_source Function data
markrad 0:cdf462088d13 164 * \param threshold Minimum required from source before entropy is released
markrad 0:cdf462088d13 165 * ( with mbedtls_entropy_func() ) (in bytes)
markrad 0:cdf462088d13 166 * \param strong MBEDTLS_ENTROPY_SOURCE_STRONG or
markrad 0:cdf462088d13 167 * MBEDTSL_ENTROPY_SOURCE_WEAK.
markrad 0:cdf462088d13 168 * At least one strong source needs to be added.
markrad 0:cdf462088d13 169 * Weaker sources (such as the cycle counter) can be used as
markrad 0:cdf462088d13 170 * a complement.
markrad 0:cdf462088d13 171 *
markrad 0:cdf462088d13 172 * \return 0 if successful or MBEDTLS_ERR_ENTROPY_MAX_SOURCES
markrad 0:cdf462088d13 173 */
markrad 0:cdf462088d13 174 int mbedtls_entropy_add_source( mbedtls_entropy_context *ctx,
markrad 0:cdf462088d13 175 mbedtls_entropy_f_source_ptr f_source, void *p_source,
markrad 0:cdf462088d13 176 size_t threshold, int strong );
markrad 0:cdf462088d13 177
markrad 0:cdf462088d13 178 /**
markrad 0:cdf462088d13 179 * \brief Trigger an extra gather poll for the accumulator
markrad 0:cdf462088d13 180 * (Thread-safe if MBEDTLS_THREADING_C is enabled)
markrad 0:cdf462088d13 181 *
markrad 0:cdf462088d13 182 * \param ctx Entropy context
markrad 0:cdf462088d13 183 *
markrad 0:cdf462088d13 184 * \return 0 if successful, or MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
markrad 0:cdf462088d13 185 */
markrad 0:cdf462088d13 186 int mbedtls_entropy_gather( mbedtls_entropy_context *ctx );
markrad 0:cdf462088d13 187
markrad 0:cdf462088d13 188 /**
markrad 0:cdf462088d13 189 * \brief Retrieve entropy from the accumulator
markrad 0:cdf462088d13 190 * (Maximum length: MBEDTLS_ENTROPY_BLOCK_SIZE)
markrad 0:cdf462088d13 191 * (Thread-safe if MBEDTLS_THREADING_C is enabled)
markrad 0:cdf462088d13 192 *
markrad 0:cdf462088d13 193 * \param data Entropy context
markrad 0:cdf462088d13 194 * \param output Buffer to fill
markrad 0:cdf462088d13 195 * \param len Number of bytes desired, must be at most MBEDTLS_ENTROPY_BLOCK_SIZE
markrad 0:cdf462088d13 196 *
markrad 0:cdf462088d13 197 * \return 0 if successful, or MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
markrad 0:cdf462088d13 198 */
markrad 0:cdf462088d13 199 int mbedtls_entropy_func( void *data, unsigned char *output, size_t len );
markrad 0:cdf462088d13 200
markrad 0:cdf462088d13 201 /**
markrad 0:cdf462088d13 202 * \brief Add data to the accumulator manually
markrad 0:cdf462088d13 203 * (Thread-safe if MBEDTLS_THREADING_C is enabled)
markrad 0:cdf462088d13 204 *
markrad 0:cdf462088d13 205 * \param ctx Entropy context
markrad 0:cdf462088d13 206 * \param data Data to add
markrad 0:cdf462088d13 207 * \param len Length of data
markrad 0:cdf462088d13 208 *
markrad 0:cdf462088d13 209 * \return 0 if successful
markrad 0:cdf462088d13 210 */
markrad 0:cdf462088d13 211 int mbedtls_entropy_update_manual( mbedtls_entropy_context *ctx,
markrad 0:cdf462088d13 212 const unsigned char *data, size_t len );
markrad 0:cdf462088d13 213
markrad 0:cdf462088d13 214 #if defined(MBEDTLS_ENTROPY_NV_SEED)
markrad 0:cdf462088d13 215 /**
markrad 0:cdf462088d13 216 * \brief Trigger an update of the seed file in NV by using the
markrad 0:cdf462088d13 217 * current entropy pool.
markrad 0:cdf462088d13 218 *
markrad 0:cdf462088d13 219 * \param ctx Entropy context
markrad 0:cdf462088d13 220 *
markrad 0:cdf462088d13 221 * \return 0 if successful
markrad 0:cdf462088d13 222 */
markrad 0:cdf462088d13 223 int mbedtls_entropy_update_nv_seed( mbedtls_entropy_context *ctx );
markrad 0:cdf462088d13 224 #endif /* MBEDTLS_ENTROPY_NV_SEED */
markrad 0:cdf462088d13 225
markrad 0:cdf462088d13 226 #if defined(MBEDTLS_FS_IO)
markrad 0:cdf462088d13 227 /**
markrad 0:cdf462088d13 228 * \brief Write a seed file
markrad 0:cdf462088d13 229 *
markrad 0:cdf462088d13 230 * \param ctx Entropy context
markrad 0:cdf462088d13 231 * \param path Name of the file
markrad 0:cdf462088d13 232 *
markrad 0:cdf462088d13 233 * \return 0 if successful,
markrad 0:cdf462088d13 234 * MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR on file error, or
markrad 0:cdf462088d13 235 * MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
markrad 0:cdf462088d13 236 */
markrad 0:cdf462088d13 237 int mbedtls_entropy_write_seed_file( mbedtls_entropy_context *ctx, const char *path );
markrad 0:cdf462088d13 238
markrad 0:cdf462088d13 239 /**
markrad 0:cdf462088d13 240 * \brief Read and update a seed file. Seed is added to this
markrad 0:cdf462088d13 241 * instance. No more than MBEDTLS_ENTROPY_MAX_SEED_SIZE bytes are
markrad 0:cdf462088d13 242 * read from the seed file. The rest is ignored.
markrad 0:cdf462088d13 243 *
markrad 0:cdf462088d13 244 * \param ctx Entropy context
markrad 0:cdf462088d13 245 * \param path Name of the file
markrad 0:cdf462088d13 246 *
markrad 0:cdf462088d13 247 * \return 0 if successful,
markrad 0:cdf462088d13 248 * MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR on file error,
markrad 0:cdf462088d13 249 * MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
markrad 0:cdf462088d13 250 */
markrad 0:cdf462088d13 251 int mbedtls_entropy_update_seed_file( mbedtls_entropy_context *ctx, const char *path );
markrad 0:cdf462088d13 252 #endif /* MBEDTLS_FS_IO */
markrad 0:cdf462088d13 253
markrad 0:cdf462088d13 254 #if defined(MBEDTLS_SELF_TEST)
markrad 0:cdf462088d13 255 /**
markrad 0:cdf462088d13 256 * \brief Checkup routine
markrad 0:cdf462088d13 257 *
markrad 0:cdf462088d13 258 * This module self-test also calls the entropy self-test,
markrad 0:cdf462088d13 259 * mbedtls_entropy_source_self_test();
markrad 0:cdf462088d13 260 *
markrad 0:cdf462088d13 261 * \return 0 if successful, or 1 if a test failed
markrad 0:cdf462088d13 262 */
markrad 0:cdf462088d13 263 int mbedtls_entropy_self_test( int verbose );
markrad 0:cdf462088d13 264
markrad 0:cdf462088d13 265 #if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
markrad 0:cdf462088d13 266 /**
markrad 0:cdf462088d13 267 * \brief Checkup routine
markrad 0:cdf462088d13 268 *
markrad 0:cdf462088d13 269 * Verifies the integrity of the hardware entropy source
markrad 0:cdf462088d13 270 * provided by the function 'mbedtls_hardware_poll()'.
markrad 0:cdf462088d13 271 *
markrad 0:cdf462088d13 272 * Note this is the only hardware entropy source that is known
markrad 0:cdf462088d13 273 * at link time, and other entropy sources configured
markrad 0:cdf462088d13 274 * dynamically at runtime by the function
markrad 0:cdf462088d13 275 * mbedtls_entropy_add_source() will not be tested.
markrad 0:cdf462088d13 276 *
markrad 0:cdf462088d13 277 * \return 0 if successful, or 1 if a test failed
markrad 0:cdf462088d13 278 */
markrad 0:cdf462088d13 279 int mbedtls_entropy_source_self_test( int verbose );
markrad 0:cdf462088d13 280 #endif /* MBEDTLS_ENTROPY_HARDWARE_ALT */
markrad 0:cdf462088d13 281 #endif /* MBEDTLS_SELF_TEST */
markrad 0:cdf462088d13 282
markrad 0:cdf462088d13 283 #ifdef __cplusplus
markrad 0:cdf462088d13 284 }
markrad 0:cdf462088d13 285 #endif
markrad 0:cdf462088d13 286
markrad 0:cdf462088d13 287 #endif /* entropy.h */