blob: 3b5d692bc8d5e134b5ef418fbaa586ed845bc0ce [file] [log] [blame]
Paul Bakker6083fd22011-12-03 21:45:14 +00001/**
2 * \file entropy.h
3 *
4 * \brief Entropy accumulator implementation
5 *
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006 * Copyright (C) 2006-2014, 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
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker28c7e7f2011-12-15 19:49:30 +000033#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#else
35#include POLARSSL_CONFIG_FILE
36#endif
Paul Bakker28c7e7f2011-12-15 19:49:30 +000037
Manuel Pégourié-Gonnarde4421112014-04-02 13:50:05 +020038#if defined(POLARSSL_SHA512_C) && !defined(POLARSSL_ENTROPY_FORCE_SHA256)
Paul Bakkerd2681d82013-06-30 14:49:12 +020039#include "sha512.h"
Paul Bakkerfb08fd22013-08-27 15:06:26 +020040#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
47
Paul Bakkerf4e7dc52013-09-28 15:23:57 +020048#if defined(POLARSSL_THREADING_C)
49#include "threading.h"
50#endif
51
Paul Bakker28c7e7f2011-12-15 19:49:30 +000052#if defined(POLARSSL_HAVEGE_C)
53#include "havege.h"
54#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000055
Paul Bakker69e095c2011-12-10 21:55:01 +000056#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. */
Paul Bakker43655f42011-12-15 20:11:16 +000058#define POLARSSL_ERR_ENTROPY_NO_SOURCES_DEFINED -0x0040 /**< No sources have been added to poll. */
Paul Bakker66ff70d2014-03-26 11:54:05 +010059#define POLARSSL_ERR_ENTROPY_FILE_IO_ERROR -0x0058 /**< Read/write error in file. */
Paul Bakker6083fd22011-12-03 21:45:14 +000060
Paul Bakker088c5c52014-04-25 11:11:10 +020061/**
62 * \name SECTION: Module settings
63 *
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 * \{
67 */
68
69#if !defined(ENTROPY_MAX_SOURCES)
Paul Bakker6083fd22011-12-03 21:45:14 +000070#define ENTROPY_MAX_SOURCES 20 /**< Maximum number of sources supported */
Paul Bakker088c5c52014-04-25 11:11:10 +020071#endif
72
73#if !defined(ENTROPY_MAX_GATHER)
Paul Bakker6083fd22011-12-03 21:45:14 +000074#define ENTROPY_MAX_GATHER 128 /**< Maximum amount requested from entropy sources */
Paul Bakker088c5c52014-04-25 11:11:10 +020075#endif
76
77/* \} name SECTION: Module settings */
Paul Bakker9bcf16c2013-06-24 19:31:17 +020078
Paul Bakkerfb08fd22013-08-27 15:06:26 +020079#if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
Paul Bakker6083fd22011-12-03 21:45:14 +000080#define ENTROPY_BLOCK_SIZE 64 /**< Block size of entropy accumulator (SHA-512) */
Paul Bakkerfb08fd22013-08-27 15:06:26 +020081#else
82#define ENTROPY_BLOCK_SIZE 32 /**< Block size of entropy accumulator (SHA-256) */
83#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000084
Paul Bakker66ff70d2014-03-26 11:54:05 +010085#define ENTROPY_MAX_SEED_SIZE 1024 /**< Maximum size of seed we read from seed file */
Paul Bakker6083fd22011-12-03 21:45:14 +000086#define ENTROPY_SOURCE_MANUAL ENTROPY_MAX_SOURCES
87
88#ifdef __cplusplus
89extern "C" {
90#endif
91
92/**
93 * \brief Entropy poll callback pointer
94 *
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)
99 *
100 * \return 0 if no critical failures occurred,
101 * POLARSSL_ERR_ENTROPY_SOURCE_FAILED otherwise
102 */
Paul Bakkera36d23e2013-12-30 17:57:27 +0100103typedef int (*f_source_ptr)(void *data, unsigned char *output, size_t len,
104 size_t *olen);
Paul Bakker6083fd22011-12-03 21:45:14 +0000105
106/**
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000107 * \brief Entropy source state
108 */
109typedef struct
110{
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 */
115}
116source_state;
117
118/**
Paul Bakker6083fd22011-12-03 21:45:14 +0000119 * \brief Entropy context structure
120 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200121typedef struct
Paul Bakker6083fd22011-12-03 21:45:14 +0000122{
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200123#if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
Paul Bakker9e36f042013-06-30 14:34:05 +0200124 sha512_context accumulator;
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200125#else
126 sha256_context accumulator;
127#endif
Paul Bakker6083fd22011-12-03 21:45:14 +0000128 int source_count;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000129 source_state source[ENTROPY_MAX_SOURCES];
Paul Bakker28c7e7f2011-12-15 19:49:30 +0000130#if defined(POLARSSL_HAVEGE_C)
131 havege_state havege_data;
132#endif
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200133#if defined(POLARSSL_THREADING_C)
134 threading_mutex_t mutex; /*!< mutex */
135#endif
Paul Bakker6083fd22011-12-03 21:45:14 +0000136}
137entropy_context;
138
139/**
140 * \brief Initialize the context
141 *
142 * \param ctx Entropy context to initialize
143 */
144void entropy_init( entropy_context *ctx );
145
146/**
Paul Bakker1ffefac2013-09-28 15:23:03 +0200147 * \brief Free the data in the context
148 *
149 * \param ctx Entropy context to free
150 */
151void entropy_free( entropy_context *ctx );
152
153/**
Paul Bakker6083fd22011-12-03 21:45:14 +0000154 * \brief Adds an entropy source to poll
Paul Bakker47703a02014-02-06 15:01:20 +0100155 * (Thread-safe if POLARSSL_THREADING_C is enabled)
Paul Bakker6083fd22011-12-03 21:45:14 +0000156 *
157 * \param ctx Entropy context
158 * \param f_source Entropy function
159 * \param p_source Function data
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000160 * \param threshold Minimum required from source before entropy is released
161 * ( with entropy_func() )
Paul Bakker6083fd22011-12-03 21:45:14 +0000162 *
Paul Bakker43655f42011-12-15 20:11:16 +0000163 * \return 0 if successful or POLARSSL_ERR_ENTROPY_MAX_SOURCES
Paul Bakker6083fd22011-12-03 21:45:14 +0000164 */
165int entropy_add_source( entropy_context *ctx,
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000166 f_source_ptr f_source, void *p_source,
167 size_t threshold );
Paul Bakker6083fd22011-12-03 21:45:14 +0000168
169/**
170 * \brief Trigger an extra gather poll for the accumulator
Paul Bakker47703a02014-02-06 15:01:20 +0100171 * (Thread-safe if POLARSSL_THREADING_C is enabled)
Paul Bakker6083fd22011-12-03 21:45:14 +0000172 *
173 * \param ctx Entropy context
174 *
175 * \return 0 if successful, or POLARSSL_ERR_ENTROPY_SOURCE_FAILED
176 */
177int entropy_gather( entropy_context *ctx );
178
179/**
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200180 * \brief Retrieve entropy from the accumulator
181 * (Maximum length: ENTROPY_BLOCK_SIZE)
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200182 * (Thread-safe if POLARSSL_THREADING_C is enabled)
Paul Bakker6083fd22011-12-03 21:45:14 +0000183 *
184 * \param data Entropy context
185 * \param output Buffer to fill
186 * \param len Length of buffer
187 *
188 * \return 0 if successful, or POLARSSL_ERR_ENTROPY_SOURCE_FAILED
189 */
190int entropy_func( void *data, unsigned char *output, size_t len );
191
192/**
193 * \brief Add data to the accumulator manually
Paul Bakker47703a02014-02-06 15:01:20 +0100194 * (Thread-safe if POLARSSL_THREADING_C is enabled)
Paul Bakker9af723c2014-05-01 13:03:14 +0200195 *
Paul Bakker6083fd22011-12-03 21:45:14 +0000196 * \param ctx Entropy context
197 * \param data Data to add
198 * \param len Length of data
199 *
200 * \return 0 if successful
201 */
202int entropy_update_manual( entropy_context *ctx,
203 const unsigned char *data, size_t len );
204
Paul Bakker66ff70d2014-03-26 11:54:05 +0100205#if defined(POLARSSL_FS_IO)
206/**
207 * \brief Write a seed file
208 *
209 * \param ctx Entropy context
210 * \param path Name of the file
211 *
212 * \return 0 if successful,
213 * POLARSSL_ERR_ENTROPY_FILE_IO_ERROR on file error, or
214 * POLARSSL_ERR_ENTROPY_SOURCE_FAILED
215 */
216int entropy_write_seed_file( entropy_context *ctx, const char *path );
217
218/**
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.
222 *
223 * \param ctx Entropy context
224 * \param path Name of the file
225 *
226 * \return 0 if successful,
227 * POLARSSL_ERR_ENTROPY_FILE_IO_ERROR on file error,
228 * POLARSSL_ERR_ENTROPY_SOURCE_FAILED
229 */
230int entropy_update_seed_file( entropy_context *ctx, const char *path );
Paul Bakker9af723c2014-05-01 13:03:14 +0200231#endif /* POLARSSL_FS_IO */
Paul Bakker66ff70d2014-03-26 11:54:05 +0100232
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200233#if defined(POLARSSL_SELF_TEST)
234/**
235 * \brief Checkup routine
236 *
237 * \return 0 if successful, or 1 if a test failed
238 */
239int entropy_self_test( int verbose );
240#endif /* POLARSSL_SELF_TEST */
241
Paul Bakker6083fd22011-12-03 21:45:14 +0000242#ifdef __cplusplus
243}
244#endif
245
246#endif /* entropy.h */