Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file entropy.h |
| 3 | * |
| 4 | * \brief Entropy accumulator implementation |
| 5 | * |
Paul Bakker | 9bcf16c | 2013-06-24 19:31:17 +0200 | [diff] [blame] | 6 | * Copyright (C) 2006-2013, Brainspark B.V. |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 7 | * |
| 8 | * This file is part of PolarSSL (http://www.polarssl.org) |
| 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 | |
Paul Bakker | 28c7e7f | 2011-12-15 19:49:30 +0000 | [diff] [blame] | 32 | #include "config.h" |
| 33 | |
Manuel Pégourié-Gonnard | e442111 | 2014-04-02 13:50:05 +0200 | [diff] [blame^] | 34 | #if defined(POLARSSL_SHA512_C) && !defined(POLARSSL_ENTROPY_FORCE_SHA256) |
Paul Bakker | d2681d8 | 2013-06-30 14:49:12 +0200 | [diff] [blame] | 35 | #include "sha512.h" |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 36 | #define POLARSSL_ENTROPY_SHA512_ACCUMULATOR |
| 37 | #else |
| 38 | #if defined(POLARSSL_SHA256_C) |
| 39 | #define POLARSSL_ENTROPY_SHA256_ACCUMULATOR |
| 40 | #include "sha256.h" |
| 41 | #endif |
| 42 | #endif |
| 43 | |
Paul Bakker | f4e7dc5 | 2013-09-28 15:23:57 +0200 | [diff] [blame] | 44 | #if defined(POLARSSL_THREADING_C) |
| 45 | #include "threading.h" |
| 46 | #endif |
| 47 | |
Paul Bakker | 28c7e7f | 2011-12-15 19:49:30 +0000 | [diff] [blame] | 48 | #if defined(POLARSSL_HAVEGE_C) |
| 49 | #include "havege.h" |
| 50 | #endif |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 51 | |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 52 | #define POLARSSL_ERR_ENTROPY_SOURCE_FAILED -0x003C /**< Critical entropy source failure. */ |
| 53 | #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] | 54 | #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] | 55 | #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] | 56 | |
Paul Bakker | 9bcf16c | 2013-06-24 19:31:17 +0200 | [diff] [blame] | 57 | #if !defined(POLARSSL_CONFIG_OPTIONS) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 58 | #define ENTROPY_MAX_SOURCES 20 /**< Maximum number of sources supported */ |
| 59 | #define ENTROPY_MAX_GATHER 128 /**< Maximum amount requested from entropy sources */ |
Paul Bakker | 9bcf16c | 2013-06-24 19:31:17 +0200 | [diff] [blame] | 60 | #endif /* !POLARSSL_CONFIG_OPTIONS */ |
| 61 | |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 62 | #if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 63 | #define ENTROPY_BLOCK_SIZE 64 /**< Block size of entropy accumulator (SHA-512) */ |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 64 | #else |
| 65 | #define ENTROPY_BLOCK_SIZE 32 /**< Block size of entropy accumulator (SHA-256) */ |
| 66 | #endif |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 67 | |
Paul Bakker | 66ff70d | 2014-03-26 11:54:05 +0100 | [diff] [blame] | 68 | #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] | 69 | #define ENTROPY_SOURCE_MANUAL ENTROPY_MAX_SOURCES |
| 70 | |
| 71 | #ifdef __cplusplus |
| 72 | extern "C" { |
| 73 | #endif |
| 74 | |
| 75 | /** |
| 76 | * \brief Entropy poll callback pointer |
| 77 | * |
| 78 | * \param data Callback-specific data pointer |
| 79 | * \param output Data to fill |
| 80 | * \param len Maximum size to provide |
| 81 | * \param olen The actual amount of bytes put into the buffer (Can be 0) |
| 82 | * |
| 83 | * \return 0 if no critical failures occurred, |
| 84 | * POLARSSL_ERR_ENTROPY_SOURCE_FAILED otherwise |
| 85 | */ |
Paul Bakker | a36d23e | 2013-12-30 17:57:27 +0100 | [diff] [blame] | 86 | typedef int (*f_source_ptr)(void *data, unsigned char *output, size_t len, |
| 87 | size_t *olen); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 88 | |
| 89 | /** |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 90 | * \brief Entropy source state |
| 91 | */ |
| 92 | typedef struct |
| 93 | { |
| 94 | f_source_ptr f_source; /**< The entropy source callback */ |
| 95 | void * p_source; /**< The callback data pointer */ |
| 96 | size_t size; /**< Amount received */ |
| 97 | size_t threshold; /**< Minimum level required before release */ |
| 98 | } |
| 99 | source_state; |
| 100 | |
| 101 | /** |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 102 | * \brief Entropy context structure |
| 103 | */ |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 104 | typedef struct |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 105 | { |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 106 | #if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR) |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 107 | sha512_context accumulator; |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 108 | #else |
| 109 | sha256_context accumulator; |
| 110 | #endif |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 111 | int source_count; |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 112 | source_state source[ENTROPY_MAX_SOURCES]; |
Paul Bakker | 28c7e7f | 2011-12-15 19:49:30 +0000 | [diff] [blame] | 113 | #if defined(POLARSSL_HAVEGE_C) |
| 114 | havege_state havege_data; |
| 115 | #endif |
Paul Bakker | f4e7dc5 | 2013-09-28 15:23:57 +0200 | [diff] [blame] | 116 | #if defined(POLARSSL_THREADING_C) |
| 117 | threading_mutex_t mutex; /*!< mutex */ |
| 118 | #endif |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 119 | } |
| 120 | entropy_context; |
| 121 | |
| 122 | /** |
| 123 | * \brief Initialize the context |
| 124 | * |
| 125 | * \param ctx Entropy context to initialize |
| 126 | */ |
| 127 | void entropy_init( entropy_context *ctx ); |
| 128 | |
| 129 | /** |
Paul Bakker | 1ffefac | 2013-09-28 15:23:03 +0200 | [diff] [blame] | 130 | * \brief Free the data in the context |
| 131 | * |
| 132 | * \param ctx Entropy context to free |
| 133 | */ |
| 134 | void entropy_free( entropy_context *ctx ); |
| 135 | |
| 136 | /** |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 137 | * \brief Adds an entropy source to poll |
Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 138 | * (Thread-safe if POLARSSL_THREADING_C is enabled) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 139 | * |
| 140 | * \param ctx Entropy context |
| 141 | * \param f_source Entropy function |
| 142 | * \param p_source Function data |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 143 | * \param threshold Minimum required from source before entropy is released |
| 144 | * ( with entropy_func() ) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 145 | * |
Paul Bakker | 43655f4 | 2011-12-15 20:11:16 +0000 | [diff] [blame] | 146 | * \return 0 if successful or POLARSSL_ERR_ENTROPY_MAX_SOURCES |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 147 | */ |
| 148 | int entropy_add_source( entropy_context *ctx, |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 149 | f_source_ptr f_source, void *p_source, |
| 150 | size_t threshold ); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 151 | |
| 152 | /** |
| 153 | * \brief Trigger an extra gather poll for the accumulator |
Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 154 | * (Thread-safe if POLARSSL_THREADING_C is enabled) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 155 | * |
| 156 | * \param ctx Entropy context |
| 157 | * |
| 158 | * \return 0 if successful, or POLARSSL_ERR_ENTROPY_SOURCE_FAILED |
| 159 | */ |
| 160 | int entropy_gather( entropy_context *ctx ); |
| 161 | |
| 162 | /** |
| 163 | * \brief Retrieve entropy from the accumulator (Max ENTROPY_BLOCK_SIZE) |
Paul Bakker | f4e7dc5 | 2013-09-28 15:23:57 +0200 | [diff] [blame] | 164 | * (Thread-safe if POLARSSL_THREADING_C is enabled) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 165 | * |
| 166 | * \param data Entropy context |
| 167 | * \param output Buffer to fill |
| 168 | * \param len Length of buffer |
| 169 | * |
| 170 | * \return 0 if successful, or POLARSSL_ERR_ENTROPY_SOURCE_FAILED |
| 171 | */ |
| 172 | int entropy_func( void *data, unsigned char *output, size_t len ); |
| 173 | |
| 174 | /** |
| 175 | * \brief Add data to the accumulator manually |
Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 176 | * (Thread-safe if POLARSSL_THREADING_C is enabled) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 177 | * |
| 178 | * \param ctx Entropy context |
| 179 | * \param data Data to add |
| 180 | * \param len Length of data |
| 181 | * |
| 182 | * \return 0 if successful |
| 183 | */ |
| 184 | int entropy_update_manual( entropy_context *ctx, |
| 185 | const unsigned char *data, size_t len ); |
| 186 | |
Paul Bakker | 66ff70d | 2014-03-26 11:54:05 +0100 | [diff] [blame] | 187 | #if defined(POLARSSL_FS_IO) |
| 188 | /** |
| 189 | * \brief Write a seed file |
| 190 | * |
| 191 | * \param ctx Entropy context |
| 192 | * \param path Name of the file |
| 193 | * |
| 194 | * \return 0 if successful, |
| 195 | * POLARSSL_ERR_ENTROPY_FILE_IO_ERROR on file error, or |
| 196 | * POLARSSL_ERR_ENTROPY_SOURCE_FAILED |
| 197 | */ |
| 198 | int entropy_write_seed_file( entropy_context *ctx, const char *path ); |
| 199 | |
| 200 | /** |
| 201 | * \brief Read and update a seed file. Seed is added to this |
| 202 | * instance. No more than ENTROPY_MAX_SEED_SIZE bytes are |
| 203 | * read from the seed file. The rest is ignored. |
| 204 | * |
| 205 | * \param ctx Entropy context |
| 206 | * \param path Name of the file |
| 207 | * |
| 208 | * \return 0 if successful, |
| 209 | * POLARSSL_ERR_ENTROPY_FILE_IO_ERROR on file error, |
| 210 | * POLARSSL_ERR_ENTROPY_SOURCE_FAILED |
| 211 | */ |
| 212 | int entropy_update_seed_file( entropy_context *ctx, const char *path ); |
| 213 | #endif |
| 214 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 215 | #ifdef __cplusplus |
| 216 | } |
| 217 | #endif |
| 218 | |
| 219 | #endif /* entropy.h */ |