blob: ed97dcabb85ffe0e915408903070c956d711cb8a [file] [log] [blame]
Paul Bakker6083fd22011-12-03 21:45:14 +00001/**
2 * \file entropy.h
3 *
4 * \brief Entropy accumulator implementation
5 *
Paul Bakker6fa54882013-06-17 15:44:03 +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
Paul Bakker6083fd22011-12-03 21:45:14 +000034#include "sha4.h"
Paul Bakker28c7e7f2011-12-15 19:49:30 +000035#if defined(POLARSSL_HAVEGE_C)
36#include "havege.h"
37#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000038
Paul Bakker69e095c2011-12-10 21:55:01 +000039#define POLARSSL_ERR_ENTROPY_SOURCE_FAILED -0x003C /**< Critical entropy source failure. */
40#define POLARSSL_ERR_ENTROPY_MAX_SOURCES -0x003E /**< No more sources can be added. */
Paul Bakker43655f42011-12-15 20:11:16 +000041#define POLARSSL_ERR_ENTROPY_NO_SOURCES_DEFINED -0x0040 /**< No sources have been added to poll. */
Paul Bakker1e942372014-03-26 11:54:05 +010042#define POLARSSL_ERR_ENTROPY_FILE_IO_ERROR -0x0058 /**< Read/write error in file. */
Paul Bakker6083fd22011-12-03 21:45:14 +000043
Paul Bakker6fa54882013-06-17 15:44:03 +020044#if !defined(POLARSSL_CONFIG_OPTIONS)
Paul Bakker6083fd22011-12-03 21:45:14 +000045#define ENTROPY_MAX_SOURCES 20 /**< Maximum number of sources supported */
46#define ENTROPY_MAX_GATHER 128 /**< Maximum amount requested from entropy sources */
Paul Bakker6fa54882013-06-17 15:44:03 +020047#endif /* !POLARSSL_CONFIG_OPTIONS */
48
Paul Bakker6083fd22011-12-03 21:45:14 +000049#define ENTROPY_BLOCK_SIZE 64 /**< Block size of entropy accumulator (SHA-512) */
50
Paul Bakker1e942372014-03-26 11:54:05 +010051#define ENTROPY_MAX_SEED_SIZE 1024 /**< Maximum size of seed we read from seed file */
Paul Bakker6083fd22011-12-03 21:45:14 +000052#define ENTROPY_SOURCE_MANUAL ENTROPY_MAX_SOURCES
53
54#ifdef __cplusplus
55extern "C" {
56#endif
57
58/**
59 * \brief Entropy poll callback pointer
60 *
61 * \param data Callback-specific data pointer
62 * \param output Data to fill
63 * \param len Maximum size to provide
64 * \param olen The actual amount of bytes put into the buffer (Can be 0)
65 *
66 * \return 0 if no critical failures occurred,
67 * POLARSSL_ERR_ENTROPY_SOURCE_FAILED otherwise
68 */
69typedef int (*f_source_ptr)(void *, unsigned char *, size_t, size_t *);
70
71/**
Paul Bakkerbd4a9d02011-12-10 17:02:19 +000072 * \brief Entropy source state
73 */
74typedef struct
75{
76 f_source_ptr f_source; /**< The entropy source callback */
77 void * p_source; /**< The callback data pointer */
78 size_t size; /**< Amount received */
79 size_t threshold; /**< Minimum level required before release */
80}
81source_state;
82
83/**
Paul Bakker6083fd22011-12-03 21:45:14 +000084 * \brief Entropy context structure
85 */
86typedef struct
87{
88 sha4_context accumulator;
Paul Bakker6083fd22011-12-03 21:45:14 +000089 int source_count;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +000090 source_state source[ENTROPY_MAX_SOURCES];
Paul Bakker28c7e7f2011-12-15 19:49:30 +000091#if defined(POLARSSL_HAVEGE_C)
92 havege_state havege_data;
93#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000094}
95entropy_context;
96
97/**
98 * \brief Initialize the context
99 *
100 * \param ctx Entropy context to initialize
101 */
102void entropy_init( entropy_context *ctx );
103
104/**
105 * \brief Adds an entropy source to poll
106 *
107 * \param ctx Entropy context
108 * \param f_source Entropy function
109 * \param p_source Function data
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000110 * \param threshold Minimum required from source before entropy is released
111 * ( with entropy_func() )
Paul Bakker6083fd22011-12-03 21:45:14 +0000112 *
Paul Bakker43655f42011-12-15 20:11:16 +0000113 * \return 0 if successful or POLARSSL_ERR_ENTROPY_MAX_SOURCES
Paul Bakker6083fd22011-12-03 21:45:14 +0000114 */
115int entropy_add_source( entropy_context *ctx,
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000116 f_source_ptr f_source, void *p_source,
117 size_t threshold );
Paul Bakker6083fd22011-12-03 21:45:14 +0000118
119/**
120 * \brief Trigger an extra gather poll for the accumulator
121 *
122 * \param ctx Entropy context
123 *
124 * \return 0 if successful, or POLARSSL_ERR_ENTROPY_SOURCE_FAILED
125 */
126int entropy_gather( entropy_context *ctx );
127
128/**
129 * \brief Retrieve entropy from the accumulator (Max ENTROPY_BLOCK_SIZE)
130 *
131 * \param data Entropy context
132 * \param output Buffer to fill
133 * \param len Length of buffer
134 *
135 * \return 0 if successful, or POLARSSL_ERR_ENTROPY_SOURCE_FAILED
136 */
137int entropy_func( void *data, unsigned char *output, size_t len );
138
139/**
140 * \brief Add data to the accumulator manually
141 *
142 * \param ctx Entropy context
143 * \param data Data to add
144 * \param len Length of data
145 *
146 * \return 0 if successful
147 */
148int entropy_update_manual( entropy_context *ctx,
149 const unsigned char *data, size_t len );
150
Paul Bakker1e942372014-03-26 11:54:05 +0100151#if defined(POLARSSL_FS_IO)
152/**
153 * \brief Write a seed file
154 *
155 * \param ctx Entropy context
156 * \param path Name of the file
157 *
158 * \return 0 if successful,
159 * POLARSSL_ERR_ENTROPY_FILE_IO_ERROR on file error, or
160 * POLARSSL_ERR_ENTROPY_SOURCE_FAILED
161 */
162int entropy_write_seed_file( entropy_context *ctx, const char *path );
163
164/**
165 * \brief Read and update a seed file. Seed is added to this
166 * instance. No more than ENTROPY_MAX_SEED_SIZE bytes are
167 * read from the seed file. The rest is ignored.
168 *
169 * \param ctx Entropy context
170 * \param path Name of the file
171 *
172 * \return 0 if successful,
173 * POLARSSL_ERR_ENTROPY_FILE_IO_ERROR on file error,
174 * POLARSSL_ERR_ENTROPY_SOURCE_FAILED
175 */
176int entropy_update_seed_file( entropy_context *ctx, const char *path );
177#endif
178
Paul Bakker6083fd22011-12-03 21:45:14 +0000179#ifdef __cplusplus
180}
181#endif
182
183#endif /* entropy.h */