blob: 9a5f4d7eb6bbe8446bfd63796371fedebe116149 [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 Bakker9bcf16c2013-06-24 19:31:17 +020057#if !defined(POLARSSL_CONFIG_OPTIONS)
Paul Bakker6083fd22011-12-03 21:45:14 +000058#define ENTROPY_MAX_SOURCES 20 /**< Maximum number of sources supported */
59#define ENTROPY_MAX_GATHER 128 /**< Maximum amount requested from entropy sources */
Paul Bakker9bcf16c2013-06-24 19:31:17 +020060#endif /* !POLARSSL_CONFIG_OPTIONS */
61
Paul Bakkerfb08fd22013-08-27 15:06:26 +020062#if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
Paul Bakker6083fd22011-12-03 21:45:14 +000063#define ENTROPY_BLOCK_SIZE 64 /**< Block size of entropy accumulator (SHA-512) */
Paul Bakkerfb08fd22013-08-27 15:06:26 +020064#else
65#define ENTROPY_BLOCK_SIZE 32 /**< Block size of entropy accumulator (SHA-256) */
66#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000067
Paul Bakker66ff70d2014-03-26 11:54:05 +010068#define ENTROPY_MAX_SEED_SIZE 1024 /**< Maximum size of seed we read from seed file */
Paul Bakker6083fd22011-12-03 21:45:14 +000069#define ENTROPY_SOURCE_MANUAL ENTROPY_MAX_SOURCES
70
71#ifdef __cplusplus
72extern "C" {
73#endif
74
75/**
76 * \brief Entropy poll callback pointer
77 *
78 * \param data Callback-specific data pointer
79 * \param output Data to fill
80 * \param len Maximum size to provide
81 * \param olen The actual amount of bytes put into the buffer (Can be 0)
82 *
83 * \return 0 if no critical failures occurred,
84 * POLARSSL_ERR_ENTROPY_SOURCE_FAILED otherwise
85 */
Paul Bakkera36d23e2013-12-30 17:57:27 +010086typedef int (*f_source_ptr)(void *data, unsigned char *output, size_t len,
87 size_t *olen);
Paul Bakker6083fd22011-12-03 21:45:14 +000088
89/**
Paul Bakkerbd4a9d02011-12-10 17:02:19 +000090 * \brief Entropy source state
91 */
92typedef struct
93{
94 f_source_ptr f_source; /**< The entropy source callback */
95 void * p_source; /**< The callback data pointer */
96 size_t size; /**< Amount received */
97 size_t threshold; /**< Minimum level required before release */
98}
99source_state;
100
101/**
Paul Bakker6083fd22011-12-03 21:45:14 +0000102 * \brief Entropy context structure
103 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200104typedef struct
Paul Bakker6083fd22011-12-03 21:45:14 +0000105{
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200106#if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
Paul Bakker9e36f042013-06-30 14:34:05 +0200107 sha512_context accumulator;
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200108#else
109 sha256_context accumulator;
110#endif
Paul Bakker6083fd22011-12-03 21:45:14 +0000111 int source_count;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000112 source_state source[ENTROPY_MAX_SOURCES];
Paul Bakker28c7e7f2011-12-15 19:49:30 +0000113#if defined(POLARSSL_HAVEGE_C)
114 havege_state havege_data;
115#endif
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200116#if defined(POLARSSL_THREADING_C)
117 threading_mutex_t mutex; /*!< mutex */
118#endif
Paul Bakker6083fd22011-12-03 21:45:14 +0000119}
120entropy_context;
121
122/**
123 * \brief Initialize the context
124 *
125 * \param ctx Entropy context to initialize
126 */
127void entropy_init( entropy_context *ctx );
128
129/**
Paul Bakker1ffefac2013-09-28 15:23:03 +0200130 * \brief Free the data in the context
131 *
132 * \param ctx Entropy context to free
133 */
134void entropy_free( entropy_context *ctx );
135
136/**
Paul Bakker6083fd22011-12-03 21:45:14 +0000137 * \brief Adds an entropy source to poll
Paul Bakker47703a02014-02-06 15:01:20 +0100138 * (Thread-safe if POLARSSL_THREADING_C is enabled)
Paul Bakker6083fd22011-12-03 21:45:14 +0000139 *
140 * \param ctx Entropy context
141 * \param f_source Entropy function
142 * \param p_source Function data
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000143 * \param threshold Minimum required from source before entropy is released
144 * ( with entropy_func() )
Paul Bakker6083fd22011-12-03 21:45:14 +0000145 *
Paul Bakker43655f42011-12-15 20:11:16 +0000146 * \return 0 if successful or POLARSSL_ERR_ENTROPY_MAX_SOURCES
Paul Bakker6083fd22011-12-03 21:45:14 +0000147 */
148int entropy_add_source( entropy_context *ctx,
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000149 f_source_ptr f_source, void *p_source,
150 size_t threshold );
Paul Bakker6083fd22011-12-03 21:45:14 +0000151
152/**
153 * \brief Trigger an extra gather poll for the accumulator
Paul Bakker47703a02014-02-06 15:01:20 +0100154 * (Thread-safe if POLARSSL_THREADING_C is enabled)
Paul Bakker6083fd22011-12-03 21:45:14 +0000155 *
156 * \param ctx Entropy context
157 *
158 * \return 0 if successful, or POLARSSL_ERR_ENTROPY_SOURCE_FAILED
159 */
160int entropy_gather( entropy_context *ctx );
161
162/**
163 * \brief Retrieve entropy from the accumulator (Max ENTROPY_BLOCK_SIZE)
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200164 * (Thread-safe if POLARSSL_THREADING_C is enabled)
Paul Bakker6083fd22011-12-03 21:45:14 +0000165 *
166 * \param data Entropy context
167 * \param output Buffer to fill
168 * \param len Length of buffer
169 *
170 * \return 0 if successful, or POLARSSL_ERR_ENTROPY_SOURCE_FAILED
171 */
172int entropy_func( void *data, unsigned char *output, size_t len );
173
174/**
175 * \brief Add data to the accumulator manually
Paul Bakker47703a02014-02-06 15:01:20 +0100176 * (Thread-safe if POLARSSL_THREADING_C is enabled)
Paul Bakker6083fd22011-12-03 21:45:14 +0000177 *
178 * \param ctx Entropy context
179 * \param data Data to add
180 * \param len Length of data
181 *
182 * \return 0 if successful
183 */
184int entropy_update_manual( entropy_context *ctx,
185 const unsigned char *data, size_t len );
186
Paul Bakker66ff70d2014-03-26 11:54:05 +0100187#if defined(POLARSSL_FS_IO)
188/**
189 * \brief Write a seed file
190 *
191 * \param ctx Entropy context
192 * \param path Name of the file
193 *
194 * \return 0 if successful,
195 * POLARSSL_ERR_ENTROPY_FILE_IO_ERROR on file error, or
196 * POLARSSL_ERR_ENTROPY_SOURCE_FAILED
197 */
198int entropy_write_seed_file( entropy_context *ctx, const char *path );
199
200/**
201 * \brief Read and update a seed file. Seed is added to this
202 * instance. No more than ENTROPY_MAX_SEED_SIZE bytes are
203 * read from the seed file. The rest is ignored.
204 *
205 * \param ctx Entropy context
206 * \param path Name of the file
207 *
208 * \return 0 if successful,
209 * POLARSSL_ERR_ENTROPY_FILE_IO_ERROR on file error,
210 * POLARSSL_ERR_ENTROPY_SOURCE_FAILED
211 */
212int entropy_update_seed_file( entropy_context *ctx, const char *path );
213#endif
214
Paul Bakker6083fd22011-12-03 21:45:14 +0000215#ifdef __cplusplus
216}
217#endif
218
219#endif /* entropy.h */