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 | |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame^] | 34 | #if defined(POLARSSL_SHA512_C) |
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 | 28c7e7f | 2011-12-15 19:49:30 +0000 | [diff] [blame] | 44 | #if defined(POLARSSL_HAVEGE_C) |
| 45 | #include "havege.h" |
| 46 | #endif |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 47 | |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 48 | #define POLARSSL_ERR_ENTROPY_SOURCE_FAILED -0x003C /**< Critical entropy source failure. */ |
| 49 | #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] | 50 | #define POLARSSL_ERR_ENTROPY_NO_SOURCES_DEFINED -0x0040 /**< No sources have been added to poll. */ |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 51 | |
Paul Bakker | 9bcf16c | 2013-06-24 19:31:17 +0200 | [diff] [blame] | 52 | #if !defined(POLARSSL_CONFIG_OPTIONS) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 53 | #define ENTROPY_MAX_SOURCES 20 /**< Maximum number of sources supported */ |
| 54 | #define ENTROPY_MAX_GATHER 128 /**< Maximum amount requested from entropy sources */ |
Paul Bakker | 9bcf16c | 2013-06-24 19:31:17 +0200 | [diff] [blame] | 55 | #endif /* !POLARSSL_CONFIG_OPTIONS */ |
| 56 | |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame^] | 57 | #if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 58 | #define ENTROPY_BLOCK_SIZE 64 /**< Block size of entropy accumulator (SHA-512) */ |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame^] | 59 | #else |
| 60 | #define ENTROPY_BLOCK_SIZE 32 /**< Block size of entropy accumulator (SHA-256) */ |
| 61 | #endif |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 62 | |
| 63 | #define ENTROPY_SOURCE_MANUAL ENTROPY_MAX_SOURCES |
| 64 | |
| 65 | #ifdef __cplusplus |
| 66 | extern "C" { |
| 67 | #endif |
| 68 | |
| 69 | /** |
| 70 | * \brief Entropy poll callback pointer |
| 71 | * |
| 72 | * \param data Callback-specific data pointer |
| 73 | * \param output Data to fill |
| 74 | * \param len Maximum size to provide |
| 75 | * \param olen The actual amount of bytes put into the buffer (Can be 0) |
| 76 | * |
| 77 | * \return 0 if no critical failures occurred, |
| 78 | * POLARSSL_ERR_ENTROPY_SOURCE_FAILED otherwise |
| 79 | */ |
| 80 | typedef int (*f_source_ptr)(void *, unsigned char *, size_t, size_t *); |
| 81 | |
| 82 | /** |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 83 | * \brief Entropy source state |
| 84 | */ |
| 85 | typedef struct |
| 86 | { |
| 87 | f_source_ptr f_source; /**< The entropy source callback */ |
| 88 | void * p_source; /**< The callback data pointer */ |
| 89 | size_t size; /**< Amount received */ |
| 90 | size_t threshold; /**< Minimum level required before release */ |
| 91 | } |
| 92 | source_state; |
| 93 | |
| 94 | /** |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 95 | * \brief Entropy context structure |
| 96 | */ |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 97 | typedef struct |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 98 | { |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame^] | 99 | #if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR) |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 100 | sha512_context accumulator; |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame^] | 101 | #else |
| 102 | sha256_context accumulator; |
| 103 | #endif |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 104 | int source_count; |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 105 | source_state source[ENTROPY_MAX_SOURCES]; |
Paul Bakker | 28c7e7f | 2011-12-15 19:49:30 +0000 | [diff] [blame] | 106 | #if defined(POLARSSL_HAVEGE_C) |
| 107 | havege_state havege_data; |
| 108 | #endif |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 109 | } |
| 110 | entropy_context; |
| 111 | |
| 112 | /** |
| 113 | * \brief Initialize the context |
| 114 | * |
| 115 | * \param ctx Entropy context to initialize |
| 116 | */ |
| 117 | void entropy_init( entropy_context *ctx ); |
| 118 | |
| 119 | /** |
| 120 | * \brief Adds an entropy source to poll |
| 121 | * |
| 122 | * \param ctx Entropy context |
| 123 | * \param f_source Entropy function |
| 124 | * \param p_source Function data |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 125 | * \param threshold Minimum required from source before entropy is released |
| 126 | * ( with entropy_func() ) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 127 | * |
Paul Bakker | 43655f4 | 2011-12-15 20:11:16 +0000 | [diff] [blame] | 128 | * \return 0 if successful or POLARSSL_ERR_ENTROPY_MAX_SOURCES |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 129 | */ |
| 130 | int entropy_add_source( entropy_context *ctx, |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 131 | f_source_ptr f_source, void *p_source, |
| 132 | size_t threshold ); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 133 | |
| 134 | /** |
| 135 | * \brief Trigger an extra gather poll for the accumulator |
| 136 | * |
| 137 | * \param ctx Entropy context |
| 138 | * |
| 139 | * \return 0 if successful, or POLARSSL_ERR_ENTROPY_SOURCE_FAILED |
| 140 | */ |
| 141 | int entropy_gather( entropy_context *ctx ); |
| 142 | |
| 143 | /** |
| 144 | * \brief Retrieve entropy from the accumulator (Max ENTROPY_BLOCK_SIZE) |
| 145 | * |
| 146 | * \param data Entropy context |
| 147 | * \param output Buffer to fill |
| 148 | * \param len Length of buffer |
| 149 | * |
| 150 | * \return 0 if successful, or POLARSSL_ERR_ENTROPY_SOURCE_FAILED |
| 151 | */ |
| 152 | int entropy_func( void *data, unsigned char *output, size_t len ); |
| 153 | |
| 154 | /** |
| 155 | * \brief Add data to the accumulator manually |
| 156 | * |
| 157 | * \param ctx Entropy context |
| 158 | * \param data Data to add |
| 159 | * \param len Length of data |
| 160 | * |
| 161 | * \return 0 if successful |
| 162 | */ |
| 163 | int entropy_update_manual( entropy_context *ctx, |
| 164 | const unsigned char *data, size_t len ); |
| 165 | |
| 166 | #ifdef __cplusplus |
| 167 | } |
| 168 | #endif |
| 169 | |
| 170 | #endif /* entropy.h */ |