blob: 4485d3e633d61f18fceac3d5f960c3038c7f6120 [file] [log] [blame]
Paul Bakker6083fd22011-12-03 21:45:14 +00001/**
2 * \file entropy.h
3 *
4 * \brief Entropy accumulator implementation
5 *
Paul Bakker9bcf16c2013-06-24 19:31:17 +02006 * Copyright (C) 2006-2013, Brainspark B.V.
Paul Bakker6083fd22011-12-03 21:45:14 +00007 *
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 Bakker28c7e7f2011-12-15 19:49:30 +000032#include "config.h"
33
Manuel Pégourié-Gonnarde4421112014-04-02 13:50:05 +020034#if defined(POLARSSL_SHA512_C) && !defined(POLARSSL_ENTROPY_FORCE_SHA256)
Paul Bakkerd2681d82013-06-30 14:49:12 +020035#include "sha512.h"
Paul Bakkerfb08fd22013-08-27 15:06:26 +020036#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 Bakkerf4e7dc52013-09-28 15:23:57 +020044#if defined(POLARSSL_THREADING_C)
45#include "threading.h"
46#endif
47
Paul Bakker28c7e7f2011-12-15 19:49:30 +000048#if defined(POLARSSL_HAVEGE_C)
49#include "havege.h"
50#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000051
Paul Bakker69e095c2011-12-10 21:55:01 +000052#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 Bakker43655f42011-12-15 20:11:16 +000054#define POLARSSL_ERR_ENTROPY_NO_SOURCES_DEFINED -0x0040 /**< No sources have been added to poll. */
Paul Bakker66ff70d2014-03-26 11:54:05 +010055#define POLARSSL_ERR_ENTROPY_FILE_IO_ERROR -0x0058 /**< Read/write error in file. */
Paul Bakker6083fd22011-12-03 21:45:14 +000056
Paul Bakker088c5c52014-04-25 11:11:10 +020057/**
58 * \name SECTION: Module settings
59 *
60 * The configuration options you can set for this module are in this section.
61 * Either change them in config.h or define them on the compiler command line.
62 * \{
63 */
64
65#if !defined(ENTROPY_MAX_SOURCES)
Paul Bakker6083fd22011-12-03 21:45:14 +000066#define ENTROPY_MAX_SOURCES 20 /**< Maximum number of sources supported */
Paul Bakker088c5c52014-04-25 11:11:10 +020067#endif
68
69#if !defined(ENTROPY_MAX_GATHER)
Paul Bakker6083fd22011-12-03 21:45:14 +000070#define ENTROPY_MAX_GATHER 128 /**< Maximum amount requested from entropy sources */
Paul Bakker088c5c52014-04-25 11:11:10 +020071#endif
72
73/* \} name SECTION: Module settings */
Paul Bakker9bcf16c2013-06-24 19:31:17 +020074
Paul Bakkerfb08fd22013-08-27 15:06:26 +020075#if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
Paul Bakker6083fd22011-12-03 21:45:14 +000076#define ENTROPY_BLOCK_SIZE 64 /**< Block size of entropy accumulator (SHA-512) */
Paul Bakkerfb08fd22013-08-27 15:06:26 +020077#else
78#define ENTROPY_BLOCK_SIZE 32 /**< Block size of entropy accumulator (SHA-256) */
79#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000080
Paul Bakker66ff70d2014-03-26 11:54:05 +010081#define ENTROPY_MAX_SEED_SIZE 1024 /**< Maximum size of seed we read from seed file */
Paul Bakker6083fd22011-12-03 21:45:14 +000082#define ENTROPY_SOURCE_MANUAL ENTROPY_MAX_SOURCES
83
84#ifdef __cplusplus
85extern "C" {
86#endif
87
88/**
89 * \brief Entropy poll callback pointer
90 *
91 * \param data Callback-specific data pointer
92 * \param output Data to fill
93 * \param len Maximum size to provide
94 * \param olen The actual amount of bytes put into the buffer (Can be 0)
95 *
96 * \return 0 if no critical failures occurred,
97 * POLARSSL_ERR_ENTROPY_SOURCE_FAILED otherwise
98 */
Paul Bakkera36d23e2013-12-30 17:57:27 +010099typedef int (*f_source_ptr)(void *data, unsigned char *output, size_t len,
100 size_t *olen);
Paul Bakker6083fd22011-12-03 21:45:14 +0000101
102/**
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000103 * \brief Entropy source state
104 */
105typedef struct
106{
107 f_source_ptr f_source; /**< The entropy source callback */
108 void * p_source; /**< The callback data pointer */
109 size_t size; /**< Amount received */
110 size_t threshold; /**< Minimum level required before release */
111}
112source_state;
113
114/**
Paul Bakker6083fd22011-12-03 21:45:14 +0000115 * \brief Entropy context structure
116 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200117typedef struct
Paul Bakker6083fd22011-12-03 21:45:14 +0000118{
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200119#if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
Paul Bakker9e36f042013-06-30 14:34:05 +0200120 sha512_context accumulator;
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200121#else
122 sha256_context accumulator;
123#endif
Paul Bakker6083fd22011-12-03 21:45:14 +0000124 int source_count;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000125 source_state source[ENTROPY_MAX_SOURCES];
Paul Bakker28c7e7f2011-12-15 19:49:30 +0000126#if defined(POLARSSL_HAVEGE_C)
127 havege_state havege_data;
128#endif
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200129#if defined(POLARSSL_THREADING_C)
130 threading_mutex_t mutex; /*!< mutex */
131#endif
Paul Bakker6083fd22011-12-03 21:45:14 +0000132}
133entropy_context;
134
135/**
136 * \brief Initialize the context
137 *
138 * \param ctx Entropy context to initialize
139 */
140void entropy_init( entropy_context *ctx );
141
142/**
Paul Bakker1ffefac2013-09-28 15:23:03 +0200143 * \brief Free the data in the context
144 *
145 * \param ctx Entropy context to free
146 */
147void entropy_free( entropy_context *ctx );
148
149/**
Paul Bakker6083fd22011-12-03 21:45:14 +0000150 * \brief Adds an entropy source to poll
Paul Bakker47703a02014-02-06 15:01:20 +0100151 * (Thread-safe if POLARSSL_THREADING_C is enabled)
Paul Bakker6083fd22011-12-03 21:45:14 +0000152 *
153 * \param ctx Entropy context
154 * \param f_source Entropy function
155 * \param p_source Function data
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000156 * \param threshold Minimum required from source before entropy is released
157 * ( with entropy_func() )
Paul Bakker6083fd22011-12-03 21:45:14 +0000158 *
Paul Bakker43655f42011-12-15 20:11:16 +0000159 * \return 0 if successful or POLARSSL_ERR_ENTROPY_MAX_SOURCES
Paul Bakker6083fd22011-12-03 21:45:14 +0000160 */
161int entropy_add_source( entropy_context *ctx,
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000162 f_source_ptr f_source, void *p_source,
163 size_t threshold );
Paul Bakker6083fd22011-12-03 21:45:14 +0000164
165/**
166 * \brief Trigger an extra gather poll for the accumulator
Paul Bakker47703a02014-02-06 15:01:20 +0100167 * (Thread-safe if POLARSSL_THREADING_C is enabled)
Paul Bakker6083fd22011-12-03 21:45:14 +0000168 *
169 * \param ctx Entropy context
170 *
171 * \return 0 if successful, or POLARSSL_ERR_ENTROPY_SOURCE_FAILED
172 */
173int entropy_gather( entropy_context *ctx );
174
175/**
176 * \brief Retrieve entropy from the accumulator (Max ENTROPY_BLOCK_SIZE)
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200177 * (Thread-safe if POLARSSL_THREADING_C is enabled)
Paul Bakker6083fd22011-12-03 21:45:14 +0000178 *
179 * \param data Entropy context
180 * \param output Buffer to fill
181 * \param len Length of buffer
182 *
183 * \return 0 if successful, or POLARSSL_ERR_ENTROPY_SOURCE_FAILED
184 */
185int entropy_func( void *data, unsigned char *output, size_t len );
186
187/**
188 * \brief Add data to the accumulator manually
Paul Bakker47703a02014-02-06 15:01:20 +0100189 * (Thread-safe if POLARSSL_THREADING_C is enabled)
Paul Bakker6083fd22011-12-03 21:45:14 +0000190 *
191 * \param ctx Entropy context
192 * \param data Data to add
193 * \param len Length of data
194 *
195 * \return 0 if successful
196 */
197int entropy_update_manual( entropy_context *ctx,
198 const unsigned char *data, size_t len );
199
Paul Bakker66ff70d2014-03-26 11:54:05 +0100200#if defined(POLARSSL_FS_IO)
201/**
202 * \brief Write a seed file
203 *
204 * \param ctx Entropy context
205 * \param path Name of the file
206 *
207 * \return 0 if successful,
208 * POLARSSL_ERR_ENTROPY_FILE_IO_ERROR on file error, or
209 * POLARSSL_ERR_ENTROPY_SOURCE_FAILED
210 */
211int entropy_write_seed_file( entropy_context *ctx, const char *path );
212
213/**
214 * \brief Read and update a seed file. Seed is added to this
215 * instance. No more than ENTROPY_MAX_SEED_SIZE bytes are
216 * read from the seed file. The rest is ignored.
217 *
218 * \param ctx Entropy context
219 * \param path Name of the file
220 *
221 * \return 0 if successful,
222 * POLARSSL_ERR_ENTROPY_FILE_IO_ERROR on file error,
223 * POLARSSL_ERR_ENTROPY_SOURCE_FAILED
224 */
225int entropy_update_seed_file( entropy_context *ctx, const char *path );
226#endif
227
Paul Bakker6083fd22011-12-03 21:45:14 +0000228#ifdef __cplusplus
229}
230#endif
231
232#endif /* entropy.h */