Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file entropy.h |
| 3 | * |
| 4 | * \brief Entropy accumulator implementation |
| 5 | * |
Manuel Pégourié-Gonnard | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame^] | 6 | * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 7 | * |
Manuel Pégourié-Gonnard | 967a2a5 | 2015-01-22 14:28:16 +0000 | [diff] [blame] | 8 | * This file is part of mbed TLS (http://www.polarssl.org) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 9 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
| 10 | * |
| 11 | * All rights reserved. |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
| 16 | * (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License along |
| 24 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 25 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 26 | */ |
| 27 | #ifndef POLARSSL_ENTROPY_H |
| 28 | #define POLARSSL_ENTROPY_H |
| 29 | |
| 30 | #include <string.h> |
| 31 | |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 32 | #if !defined(POLARSSL_CONFIG_FILE) |
Paul Bakker | 28c7e7f | 2011-12-15 19:49:30 +0000 | [diff] [blame] | 33 | #include "config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 34 | #else |
| 35 | #include POLARSSL_CONFIG_FILE |
| 36 | #endif |
Paul Bakker | 28c7e7f | 2011-12-15 19:49:30 +0000 | [diff] [blame] | 37 | |
Manuel Pégourié-Gonnard | e442111 | 2014-04-02 13:50:05 +0200 | [diff] [blame] | 38 | #if defined(POLARSSL_SHA512_C) && !defined(POLARSSL_ENTROPY_FORCE_SHA256) |
Paul Bakker | d2681d8 | 2013-06-30 14:49:12 +0200 | [diff] [blame] | 39 | #include "sha512.h" |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 40 | #define POLARSSL_ENTROPY_SHA512_ACCUMULATOR |
| 41 | #else |
| 42 | #if defined(POLARSSL_SHA256_C) |
| 43 | #define POLARSSL_ENTROPY_SHA256_ACCUMULATOR |
| 44 | #include "sha256.h" |
| 45 | #endif |
| 46 | #endif |
| 47 | |
Paul Bakker | f4e7dc5 | 2013-09-28 15:23:57 +0200 | [diff] [blame] | 48 | #if defined(POLARSSL_THREADING_C) |
| 49 | #include "threading.h" |
| 50 | #endif |
| 51 | |
Paul Bakker | 28c7e7f | 2011-12-15 19:49:30 +0000 | [diff] [blame] | 52 | #if defined(POLARSSL_HAVEGE_C) |
| 53 | #include "havege.h" |
| 54 | #endif |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 55 | |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 56 | #define POLARSSL_ERR_ENTROPY_SOURCE_FAILED -0x003C /**< Critical entropy source failure. */ |
| 57 | #define POLARSSL_ERR_ENTROPY_MAX_SOURCES -0x003E /**< No more sources can be added. */ |
Paul Bakker | 43655f4 | 2011-12-15 20:11:16 +0000 | [diff] [blame] | 58 | #define POLARSSL_ERR_ENTROPY_NO_SOURCES_DEFINED -0x0040 /**< No sources have been added to poll. */ |
Paul Bakker | 66ff70d | 2014-03-26 11:54:05 +0100 | [diff] [blame] | 59 | #define POLARSSL_ERR_ENTROPY_FILE_IO_ERROR -0x0058 /**< Read/write error in file. */ |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 60 | |
Paul Bakker | 088c5c5 | 2014-04-25 11:11:10 +0200 | [diff] [blame] | 61 | /** |
| 62 | * \name SECTION: Module settings |
| 63 | * |
| 64 | * The configuration options you can set for this module are in this section. |
| 65 | * Either change them in config.h or define them on the compiler command line. |
| 66 | * \{ |
| 67 | */ |
| 68 | |
| 69 | #if !defined(ENTROPY_MAX_SOURCES) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 70 | #define ENTROPY_MAX_SOURCES 20 /**< Maximum number of sources supported */ |
Paul Bakker | 088c5c5 | 2014-04-25 11:11:10 +0200 | [diff] [blame] | 71 | #endif |
| 72 | |
| 73 | #if !defined(ENTROPY_MAX_GATHER) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 74 | #define ENTROPY_MAX_GATHER 128 /**< Maximum amount requested from entropy sources */ |
Paul Bakker | 088c5c5 | 2014-04-25 11:11:10 +0200 | [diff] [blame] | 75 | #endif |
| 76 | |
| 77 | /* \} name SECTION: Module settings */ |
Paul Bakker | 9bcf16c | 2013-06-24 19:31:17 +0200 | [diff] [blame] | 78 | |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 79 | #if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 80 | #define ENTROPY_BLOCK_SIZE 64 /**< Block size of entropy accumulator (SHA-512) */ |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 81 | #else |
| 82 | #define ENTROPY_BLOCK_SIZE 32 /**< Block size of entropy accumulator (SHA-256) */ |
| 83 | #endif |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 84 | |
Paul Bakker | 66ff70d | 2014-03-26 11:54:05 +0100 | [diff] [blame] | 85 | #define ENTROPY_MAX_SEED_SIZE 1024 /**< Maximum size of seed we read from seed file */ |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 86 | #define ENTROPY_SOURCE_MANUAL ENTROPY_MAX_SOURCES |
| 87 | |
| 88 | #ifdef __cplusplus |
| 89 | extern "C" { |
| 90 | #endif |
| 91 | |
| 92 | /** |
| 93 | * \brief Entropy poll callback pointer |
| 94 | * |
| 95 | * \param data Callback-specific data pointer |
| 96 | * \param output Data to fill |
| 97 | * \param len Maximum size to provide |
| 98 | * \param olen The actual amount of bytes put into the buffer (Can be 0) |
| 99 | * |
| 100 | * \return 0 if no critical failures occurred, |
| 101 | * POLARSSL_ERR_ENTROPY_SOURCE_FAILED otherwise |
| 102 | */ |
Paul Bakker | a36d23e | 2013-12-30 17:57:27 +0100 | [diff] [blame] | 103 | typedef int (*f_source_ptr)(void *data, unsigned char *output, size_t len, |
| 104 | size_t *olen); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 105 | |
| 106 | /** |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 107 | * \brief Entropy source state |
| 108 | */ |
| 109 | typedef struct |
| 110 | { |
| 111 | f_source_ptr f_source; /**< The entropy source callback */ |
| 112 | void * p_source; /**< The callback data pointer */ |
| 113 | size_t size; /**< Amount received */ |
| 114 | size_t threshold; /**< Minimum level required before release */ |
| 115 | } |
| 116 | source_state; |
| 117 | |
| 118 | /** |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 119 | * \brief Entropy context structure |
| 120 | */ |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 121 | typedef struct |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 122 | { |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 123 | #if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR) |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 124 | sha512_context accumulator; |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 125 | #else |
| 126 | sha256_context accumulator; |
| 127 | #endif |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 128 | int source_count; |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 129 | source_state source[ENTROPY_MAX_SOURCES]; |
Paul Bakker | 28c7e7f | 2011-12-15 19:49:30 +0000 | [diff] [blame] | 130 | #if defined(POLARSSL_HAVEGE_C) |
| 131 | havege_state havege_data; |
| 132 | #endif |
Paul Bakker | f4e7dc5 | 2013-09-28 15:23:57 +0200 | [diff] [blame] | 133 | #if defined(POLARSSL_THREADING_C) |
| 134 | threading_mutex_t mutex; /*!< mutex */ |
| 135 | #endif |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 136 | } |
| 137 | entropy_context; |
| 138 | |
| 139 | /** |
| 140 | * \brief Initialize the context |
| 141 | * |
| 142 | * \param ctx Entropy context to initialize |
| 143 | */ |
| 144 | void entropy_init( entropy_context *ctx ); |
| 145 | |
| 146 | /** |
Paul Bakker | 1ffefac | 2013-09-28 15:23:03 +0200 | [diff] [blame] | 147 | * \brief Free the data in the context |
| 148 | * |
| 149 | * \param ctx Entropy context to free |
| 150 | */ |
| 151 | void entropy_free( entropy_context *ctx ); |
| 152 | |
| 153 | /** |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 154 | * \brief Adds an entropy source to poll |
Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 155 | * (Thread-safe if POLARSSL_THREADING_C is enabled) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 156 | * |
| 157 | * \param ctx Entropy context |
| 158 | * \param f_source Entropy function |
| 159 | * \param p_source Function data |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 160 | * \param threshold Minimum required from source before entropy is released |
| 161 | * ( with entropy_func() ) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 162 | * |
Paul Bakker | 43655f4 | 2011-12-15 20:11:16 +0000 | [diff] [blame] | 163 | * \return 0 if successful or POLARSSL_ERR_ENTROPY_MAX_SOURCES |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 164 | */ |
| 165 | int entropy_add_source( entropy_context *ctx, |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 166 | f_source_ptr f_source, void *p_source, |
| 167 | size_t threshold ); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 168 | |
| 169 | /** |
| 170 | * \brief Trigger an extra gather poll for the accumulator |
Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 171 | * (Thread-safe if POLARSSL_THREADING_C is enabled) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 172 | * |
| 173 | * \param ctx Entropy context |
| 174 | * |
| 175 | * \return 0 if successful, or POLARSSL_ERR_ENTROPY_SOURCE_FAILED |
| 176 | */ |
| 177 | int entropy_gather( entropy_context *ctx ); |
| 178 | |
| 179 | /** |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 180 | * \brief Retrieve entropy from the accumulator |
| 181 | * (Maximum length: ENTROPY_BLOCK_SIZE) |
Paul Bakker | f4e7dc5 | 2013-09-28 15:23:57 +0200 | [diff] [blame] | 182 | * (Thread-safe if POLARSSL_THREADING_C is enabled) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 183 | * |
| 184 | * \param data Entropy context |
| 185 | * \param output Buffer to fill |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 186 | * \param len Number of bytes desired, must be at most ENTROPY_BLOCK_SIZE |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 187 | * |
| 188 | * \return 0 if successful, or POLARSSL_ERR_ENTROPY_SOURCE_FAILED |
| 189 | */ |
| 190 | int entropy_func( void *data, unsigned char *output, size_t len ); |
| 191 | |
| 192 | /** |
| 193 | * \brief Add data to the accumulator manually |
Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 194 | * (Thread-safe if POLARSSL_THREADING_C is enabled) |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 195 | * |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 196 | * \param ctx Entropy context |
| 197 | * \param data Data to add |
| 198 | * \param len Length of data |
| 199 | * |
| 200 | * \return 0 if successful |
| 201 | */ |
| 202 | int entropy_update_manual( entropy_context *ctx, |
| 203 | const unsigned char *data, size_t len ); |
| 204 | |
Paul Bakker | 66ff70d | 2014-03-26 11:54:05 +0100 | [diff] [blame] | 205 | #if defined(POLARSSL_FS_IO) |
| 206 | /** |
| 207 | * \brief Write a seed file |
| 208 | * |
| 209 | * \param ctx Entropy context |
| 210 | * \param path Name of the file |
| 211 | * |
| 212 | * \return 0 if successful, |
| 213 | * POLARSSL_ERR_ENTROPY_FILE_IO_ERROR on file error, or |
| 214 | * POLARSSL_ERR_ENTROPY_SOURCE_FAILED |
| 215 | */ |
| 216 | int entropy_write_seed_file( entropy_context *ctx, const char *path ); |
| 217 | |
| 218 | /** |
| 219 | * \brief Read and update a seed file. Seed is added to this |
| 220 | * instance. No more than ENTROPY_MAX_SEED_SIZE bytes are |
| 221 | * read from the seed file. The rest is ignored. |
| 222 | * |
| 223 | * \param ctx Entropy context |
| 224 | * \param path Name of the file |
| 225 | * |
| 226 | * \return 0 if successful, |
| 227 | * POLARSSL_ERR_ENTROPY_FILE_IO_ERROR on file error, |
| 228 | * POLARSSL_ERR_ENTROPY_SOURCE_FAILED |
| 229 | */ |
| 230 | int entropy_update_seed_file( entropy_context *ctx, const char *path ); |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 231 | #endif /* POLARSSL_FS_IO */ |
Paul Bakker | 66ff70d | 2014-03-26 11:54:05 +0100 | [diff] [blame] | 232 | |
Manuel Pégourié-Gonnard | 4dd7392 | 2014-05-30 10:34:15 +0200 | [diff] [blame] | 233 | #if defined(POLARSSL_SELF_TEST) |
| 234 | /** |
| 235 | * \brief Checkup routine |
| 236 | * |
| 237 | * \return 0 if successful, or 1 if a test failed |
| 238 | */ |
| 239 | int entropy_self_test( int verbose ); |
| 240 | #endif /* POLARSSL_SELF_TEST */ |
| 241 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 242 | #ifdef __cplusplus |
| 243 | } |
| 244 | #endif |
| 245 | |
| 246 | #endif /* entropy.h */ |