insert nonsensical useragent for sourceshit, so it won't try to show its idiotic...
[syren.git] / src / libpolarssl / entropy.h
blob55b262f8f12e5d58dd8ba08d5564448febcd135f
1 /**
2 * \file entropy.h
4 * \brief Entropy accumulator implementation
6 * Copyright (C) 2006-2014, Brainspark B.V.
8 * This file is part of PolarSSL (http://www.polarssl.org)
9 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
11 * All rights reserved.
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.
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.
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.
27 #ifndef POLARSSL_ENTROPY_H
28 #define POLARSSL_ENTROPY_H
30 #include <string.h>
32 #if !defined(POLARSSL_CONFIG_FILE)
33 #include "config.h"
34 #else
35 #include POLARSSL_CONFIG_FILE
36 #endif
38 #if defined(POLARSSL_SHA512_C) && !defined(POLARSSL_ENTROPY_FORCE_SHA256)
39 #include "sha512.h"
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
48 #if defined(POLARSSL_THREADING_C)
49 #include "threading.h"
50 #endif
52 #if defined(POLARSSL_HAVEGE_C)
53 #include "havege.h"
54 #endif
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. */
58 #define POLARSSL_ERR_ENTROPY_NO_SOURCES_DEFINED -0x0040 /**< No sources have been added to poll. */
59 #define POLARSSL_ERR_ENTROPY_FILE_IO_ERROR -0x0058 /**< Read/write error in file. */
61 /**
62 * \name SECTION: Module settings
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 * \{
69 #if !defined(ENTROPY_MAX_SOURCES)
70 #define ENTROPY_MAX_SOURCES 20 /**< Maximum number of sources supported */
71 #endif
73 #if !defined(ENTROPY_MAX_GATHER)
74 #define ENTROPY_MAX_GATHER 128 /**< Maximum amount requested from entropy sources */
75 #endif
77 /* \} name SECTION: Module settings */
79 #if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
80 #define ENTROPY_BLOCK_SIZE 64 /**< Block size of entropy accumulator (SHA-512) */
81 #else
82 #define ENTROPY_BLOCK_SIZE 32 /**< Block size of entropy accumulator (SHA-256) */
83 #endif
85 #define ENTROPY_MAX_SEED_SIZE 1024 /**< Maximum size of seed we read from seed file */
86 #define ENTROPY_SOURCE_MANUAL ENTROPY_MAX_SOURCES
88 #ifdef __cplusplus
89 extern "C" {
90 #endif
92 /**
93 * \brief Entropy poll callback pointer
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)
100 * \return 0 if no critical failures occurred,
101 * POLARSSL_ERR_ENTROPY_SOURCE_FAILED otherwise
103 typedef int (*f_source_ptr)(void *data, unsigned char *output, size_t len,
104 size_t *olen);
107 * \brief Entropy source state
109 typedef struct
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 */
116 source_state;
119 * \brief Entropy context structure
121 typedef struct
123 #if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
124 sha512_context accumulator;
125 #else
126 sha256_context accumulator;
127 #endif
128 int source_count;
129 source_state source[ENTROPY_MAX_SOURCES];
130 #if defined(POLARSSL_HAVEGE_C)
131 havege_state havege_data;
132 #endif
133 #if defined(POLARSSL_THREADING_C)
134 threading_mutex_t mutex; /*!< mutex */
135 #endif
137 entropy_context;
140 * \brief Initialize the context
142 * \param ctx Entropy context to initialize
144 void entropy_init( entropy_context *ctx );
147 * \brief Free the data in the context
149 * \param ctx Entropy context to free
151 void entropy_free( entropy_context *ctx );
154 * \brief Adds an entropy source to poll
155 * (Thread-safe if POLARSSL_THREADING_C is enabled)
157 * \param ctx Entropy context
158 * \param f_source Entropy function
159 * \param p_source Function data
160 * \param threshold Minimum required from source before entropy is released
161 * ( with entropy_func() )
163 * \return 0 if successful or POLARSSL_ERR_ENTROPY_MAX_SOURCES
165 int entropy_add_source( entropy_context *ctx,
166 f_source_ptr f_source, void *p_source,
167 size_t threshold );
170 * \brief Trigger an extra gather poll for the accumulator
171 * (Thread-safe if POLARSSL_THREADING_C is enabled)
173 * \param ctx Entropy context
175 * \return 0 if successful, or POLARSSL_ERR_ENTROPY_SOURCE_FAILED
177 int entropy_gather( entropy_context *ctx );
180 * \brief Retrieve entropy from the accumulator
181 * (Maximum length: ENTROPY_BLOCK_SIZE)
182 * (Thread-safe if POLARSSL_THREADING_C is enabled)
184 * \param data Entropy context
185 * \param output Buffer to fill
186 * \param len Length of buffer
188 * \return 0 if successful, or POLARSSL_ERR_ENTROPY_SOURCE_FAILED
190 int entropy_func( void *data, unsigned char *output, size_t len );
193 * \brief Add data to the accumulator manually
194 * (Thread-safe if POLARSSL_THREADING_C is enabled)
196 * \param ctx Entropy context
197 * \param data Data to add
198 * \param len Length of data
200 * \return 0 if successful
202 int entropy_update_manual( entropy_context *ctx,
203 const unsigned char *data, size_t len );
205 #if defined(POLARSSL_FS_IO)
207 * \brief Write a seed file
209 * \param ctx Entropy context
210 * \param path Name of the file
212 * \return 0 if successful,
213 * POLARSSL_ERR_ENTROPY_FILE_IO_ERROR on file error, or
214 * POLARSSL_ERR_ENTROPY_SOURCE_FAILED
216 int entropy_write_seed_file( entropy_context *ctx, const char *path );
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.
223 * \param ctx Entropy context
224 * \param path Name of the file
226 * \return 0 if successful,
227 * POLARSSL_ERR_ENTROPY_FILE_IO_ERROR on file error,
228 * POLARSSL_ERR_ENTROPY_SOURCE_FAILED
230 int entropy_update_seed_file( entropy_context *ctx, const char *path );
231 #endif /* POLARSSL_FS_IO */
233 #ifdef __cplusplus
235 #endif
237 #endif /* entropy.h */