Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
entropy.h
00001 /** 00002 * \file entropy.h 00003 * 00004 * \brief Entropy accumulator implementation 00005 * 00006 * Copyright (C) 2006-2014, Brainspark B.V. 00007 * 00008 * This file is part of PolarSSL (http://www.polarssl.org) 00009 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> 00010 * 00011 * All rights reserved. 00012 * 00013 * This program is free software; you can redistribute it and/or modify 00014 * it under the terms of the GNU General Public License as published by 00015 * the Free Software Foundation; either version 2 of the License, or 00016 * (at your option) any later version. 00017 * 00018 * This program is distributed in the hope that it will be useful, 00019 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00020 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00021 * GNU General Public License for more details. 00022 * 00023 * You should have received a copy of the GNU General Public License along 00024 * with this program; if not, write to the Free Software Foundation, Inc., 00025 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 00026 */ 00027 #ifndef POLARSSL_ENTROPY_H 00028 #define POLARSSL_ENTROPY_H 00029 00030 #include <string.h> 00031 00032 #if !defined(POLARSSL_CONFIG_FILE) 00033 #include "config.h" 00034 #else 00035 #include POLARSSL_CONFIG_FILE 00036 #endif 00037 00038 #if defined(POLARSSL_SHA512_C) && !defined(POLARSSL_ENTROPY_FORCE_SHA256) 00039 #include "sha512.h" 00040 #define POLARSSL_ENTROPY_SHA512_ACCUMULATOR 00041 #else 00042 #if defined(POLARSSL_SHA256_C) 00043 #define POLARSSL_ENTROPY_SHA256_ACCUMULATOR 00044 #include "sha256.h" 00045 #endif 00046 #endif 00047 00048 #if defined(POLARSSL_THREADING_C) 00049 #include "threading.h" 00050 #endif 00051 00052 #if defined(POLARSSL_HAVEGE_C) 00053 #include "havege.h" 00054 #endif 00055 00056 #define POLARSSL_ERR_ENTROPY_SOURCE_FAILED -0x003C /**< Critical entropy source failure. */ 00057 #define POLARSSL_ERR_ENTROPY_MAX_SOURCES -0x003E /**< No more sources can be added. */ 00058 #define POLARSSL_ERR_ENTROPY_NO_SOURCES_DEFINED -0x0040 /**< No sources have been added to poll. */ 00059 #define POLARSSL_ERR_ENTROPY_FILE_IO_ERROR -0x0058 /**< Read/write error in file. */ 00060 00061 /** 00062 * \name SECTION: Module settings 00063 * 00064 * The configuration options you can set for this module are in this section. 00065 * Either change them in config.h or define them on the compiler command line. 00066 * \{ 00067 */ 00068 00069 #if !defined(ENTROPY_MAX_SOURCES) 00070 #define ENTROPY_MAX_SOURCES 20 /**< Maximum number of sources supported */ 00071 #endif 00072 00073 #if !defined(ENTROPY_MAX_GATHER) 00074 #define ENTROPY_MAX_GATHER 128 /**< Maximum amount requested from entropy sources */ 00075 #endif 00076 00077 /* \} name SECTION: Module settings */ 00078 00079 #if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR) 00080 #define ENTROPY_BLOCK_SIZE 64 /**< Block size of entropy accumulator (SHA-512) */ 00081 #else 00082 #define ENTROPY_BLOCK_SIZE 32 /**< Block size of entropy accumulator (SHA-256) */ 00083 #endif 00084 00085 #define ENTROPY_MAX_SEED_SIZE 1024 /**< Maximum size of seed we read from seed file */ 00086 #define ENTROPY_SOURCE_MANUAL ENTROPY_MAX_SOURCES 00087 00088 #ifdef __cplusplus 00089 extern "C" { 00090 #endif 00091 00092 /** 00093 * \brief Entropy poll callback pointer 00094 * 00095 * \param data Callback-specific data pointer 00096 * \param output Data to fill 00097 * \param len Maximum size to provide 00098 * \param olen The actual amount of bytes put into the buffer (Can be 0) 00099 * 00100 * \return 0 if no critical failures occurred, 00101 * POLARSSL_ERR_ENTROPY_SOURCE_FAILED otherwise 00102 */ 00103 typedef int (*f_source_ptr)(void *data, unsigned char *output, size_t len, 00104 size_t *olen); 00105 00106 /** 00107 * \brief Entropy source state 00108 */ 00109 typedef struct 00110 { 00111 f_source_ptr f_source; /**< The entropy source callback */ 00112 void * p_source; /**< The callback data pointer */ 00113 size_t size; /**< Amount received */ 00114 size_t threshold; /**< Minimum level required before release */ 00115 } 00116 source_state; 00117 00118 /** 00119 * \brief Entropy context structure 00120 */ 00121 typedef struct 00122 { 00123 #if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR) 00124 sha512_context accumulator; 00125 #else 00126 sha256_context accumulator; 00127 #endif 00128 int source_count; 00129 source_state source[ENTROPY_MAX_SOURCES]; 00130 #if defined(POLARSSL_HAVEGE_C) 00131 havege_state havege_data; 00132 #endif 00133 #if defined(POLARSSL_THREADING_C) 00134 threading_mutex_t mutex ; /*!< mutex */ 00135 #endif 00136 } 00137 entropy_context; 00138 00139 /** 00140 * \brief Initialize the context 00141 * 00142 * \param ctx Entropy context to initialize 00143 */ 00144 void entropy_init( entropy_context *ctx ); 00145 00146 /** 00147 * \brief Free the data in the context 00148 * 00149 * \param ctx Entropy context to free 00150 */ 00151 void entropy_free( entropy_context *ctx ); 00152 00153 /** 00154 * \brief Adds an entropy source to poll 00155 * (Thread-safe if POLARSSL_THREADING_C is enabled) 00156 * 00157 * \param ctx Entropy context 00158 * \param f_source Entropy function 00159 * \param p_source Function data 00160 * \param threshold Minimum required from source before entropy is released 00161 * ( with entropy_func() ) 00162 * 00163 * \return 0 if successful or POLARSSL_ERR_ENTROPY_MAX_SOURCES 00164 */ 00165 int entropy_add_source( entropy_context *ctx, 00166 f_source_ptr f_source, void *p_source, 00167 size_t threshold ); 00168 00169 /** 00170 * \brief Trigger an extra gather poll for the accumulator 00171 * (Thread-safe if POLARSSL_THREADING_C is enabled) 00172 * 00173 * \param ctx Entropy context 00174 * 00175 * \return 0 if successful, or POLARSSL_ERR_ENTROPY_SOURCE_FAILED 00176 */ 00177 int entropy_gather( entropy_context *ctx ); 00178 00179 /** 00180 * \brief Retrieve entropy from the accumulator 00181 * (Maximum length: ENTROPY_BLOCK_SIZE) 00182 * (Thread-safe if POLARSSL_THREADING_C is enabled) 00183 * 00184 * \param data Entropy context 00185 * \param output Buffer to fill 00186 * \param len Length of buffer 00187 * 00188 * \return 0 if successful, or POLARSSL_ERR_ENTROPY_SOURCE_FAILED 00189 */ 00190 int entropy_func( void *data, unsigned char *output, size_t len ); 00191 00192 /** 00193 * \brief Add data to the accumulator manually 00194 * (Thread-safe if POLARSSL_THREADING_C is enabled) 00195 * 00196 * \param ctx Entropy context 00197 * \param data Data to add 00198 * \param len Length of data 00199 * 00200 * \return 0 if successful 00201 */ 00202 int entropy_update_manual( entropy_context *ctx, 00203 const unsigned char *data, size_t len ); 00204 00205 #if defined(POLARSSL_FS_IO) 00206 /** 00207 * \brief Write a seed file 00208 * 00209 * \param ctx Entropy context 00210 * \param path Name of the file 00211 * 00212 * \return 0 if successful, 00213 * POLARSSL_ERR_ENTROPY_FILE_IO_ERROR on file error, or 00214 * POLARSSL_ERR_ENTROPY_SOURCE_FAILED 00215 */ 00216 int entropy_write_seed_file( entropy_context *ctx, const char *path ); 00217 00218 /** 00219 * \brief Read and update a seed file. Seed is added to this 00220 * instance. No more than ENTROPY_MAX_SEED_SIZE bytes are 00221 * read from the seed file. The rest is ignored. 00222 * 00223 * \param ctx Entropy context 00224 * \param path Name of the file 00225 * 00226 * \return 0 if successful, 00227 * POLARSSL_ERR_ENTROPY_FILE_IO_ERROR on file error, 00228 * POLARSSL_ERR_ENTROPY_SOURCE_FAILED 00229 */ 00230 int entropy_update_seed_file( entropy_context *ctx, const char *path ); 00231 #endif /* POLARSSL_FS_IO */ 00232 00233 #ifdef __cplusplus 00234 } 00235 #endif 00236 00237 #endif /* entropy.h */ 00238 00239
Generated on Tue Jul 12 2022 19:40:15 by
1.7.2