blob: 7ce7a5a28380ffb3fe6b52d9758560b9f4060c9b [file] [log] [blame]
Paul Bakker6083fd22011-12-03 21:45:14 +00001/**
2 * \file entropy.h
3 *
4 * \brief Entropy accumulator implementation
5 *
Paul Bakker530927b2015-02-13 14:24:10 +01006 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Paul Bakker6083fd22011-12-03 21:45:14 +00007 *
Manuel Pégourié-Gonnarde12abf92015-01-28 17:13:45 +00008 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakker6083fd22011-12-03 21:45:14 +00009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 */
24#ifndef POLARSSL_ENTROPY_H
25#define POLARSSL_ENTROPY_H
26
27#include <string.h>
28
Paul Bakker28c7e7f2011-12-15 19:49:30 +000029#include "config.h"
30
Paul Bakker6083fd22011-12-03 21:45:14 +000031#include "sha4.h"
Paul Bakker28c7e7f2011-12-15 19:49:30 +000032#if defined(POLARSSL_HAVEGE_C)
33#include "havege.h"
34#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000035
Paul Bakker69e095c2011-12-10 21:55:01 +000036#define POLARSSL_ERR_ENTROPY_SOURCE_FAILED -0x003C /**< Critical entropy source failure. */
37#define POLARSSL_ERR_ENTROPY_MAX_SOURCES -0x003E /**< No more sources can be added. */
Paul Bakker43655f42011-12-15 20:11:16 +000038#define POLARSSL_ERR_ENTROPY_NO_SOURCES_DEFINED -0x0040 /**< No sources have been added to poll. */
Paul Bakker1e942372014-03-26 11:54:05 +010039#define POLARSSL_ERR_ENTROPY_FILE_IO_ERROR -0x0058 /**< Read/write error in file. */
Paul Bakker6083fd22011-12-03 21:45:14 +000040
Paul Bakker6fa54882013-06-17 15:44:03 +020041#if !defined(POLARSSL_CONFIG_OPTIONS)
Paul Bakker6083fd22011-12-03 21:45:14 +000042#define ENTROPY_MAX_SOURCES 20 /**< Maximum number of sources supported */
43#define ENTROPY_MAX_GATHER 128 /**< Maximum amount requested from entropy sources */
Paul Bakker6fa54882013-06-17 15:44:03 +020044#endif /* !POLARSSL_CONFIG_OPTIONS */
45
Paul Bakker6083fd22011-12-03 21:45:14 +000046#define ENTROPY_BLOCK_SIZE 64 /**< Block size of entropy accumulator (SHA-512) */
47
Paul Bakker1e942372014-03-26 11:54:05 +010048#define ENTROPY_MAX_SEED_SIZE 1024 /**< Maximum size of seed we read from seed file */
Paul Bakker6083fd22011-12-03 21:45:14 +000049#define ENTROPY_SOURCE_MANUAL ENTROPY_MAX_SOURCES
50
51#ifdef __cplusplus
52extern "C" {
53#endif
54
55/**
56 * \brief Entropy poll callback pointer
57 *
58 * \param data Callback-specific data pointer
59 * \param output Data to fill
60 * \param len Maximum size to provide
61 * \param olen The actual amount of bytes put into the buffer (Can be 0)
62 *
63 * \return 0 if no critical failures occurred,
64 * POLARSSL_ERR_ENTROPY_SOURCE_FAILED otherwise
65 */
Manuel Pégourié-Gonnard7bf9f7e2014-11-17 11:20:21 +010066typedef int (*f_source_ptr)(void *data, unsigned char *output, size_t len, size_t *olen);
Paul Bakker6083fd22011-12-03 21:45:14 +000067
68/**
Paul Bakkerbd4a9d02011-12-10 17:02:19 +000069 * \brief Entropy source state
70 */
71typedef struct
72{
73 f_source_ptr f_source; /**< The entropy source callback */
74 void * p_source; /**< The callback data pointer */
75 size_t size; /**< Amount received */
76 size_t threshold; /**< Minimum level required before release */
77}
78source_state;
79
80/**
Paul Bakker6083fd22011-12-03 21:45:14 +000081 * \brief Entropy context structure
82 */
83typedef struct
84{
85 sha4_context accumulator;
Paul Bakker6083fd22011-12-03 21:45:14 +000086 int source_count;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +000087 source_state source[ENTROPY_MAX_SOURCES];
Paul Bakker28c7e7f2011-12-15 19:49:30 +000088#if defined(POLARSSL_HAVEGE_C)
89 havege_state havege_data;
90#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000091}
92entropy_context;
93
94/**
95 * \brief Initialize the context
96 *
97 * \param ctx Entropy context to initialize
98 */
99void entropy_init( entropy_context *ctx );
100
101/**
102 * \brief Adds an entropy source to poll
103 *
104 * \param ctx Entropy context
105 * \param f_source Entropy function
106 * \param p_source Function data
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000107 * \param threshold Minimum required from source before entropy is released
108 * ( with entropy_func() )
Paul Bakker6083fd22011-12-03 21:45:14 +0000109 *
Paul Bakker43655f42011-12-15 20:11:16 +0000110 * \return 0 if successful or POLARSSL_ERR_ENTROPY_MAX_SOURCES
Paul Bakker6083fd22011-12-03 21:45:14 +0000111 */
112int entropy_add_source( entropy_context *ctx,
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000113 f_source_ptr f_source, void *p_source,
114 size_t threshold );
Paul Bakker6083fd22011-12-03 21:45:14 +0000115
116/**
117 * \brief Trigger an extra gather poll for the accumulator
118 *
119 * \param ctx Entropy context
120 *
121 * \return 0 if successful, or POLARSSL_ERR_ENTROPY_SOURCE_FAILED
122 */
123int entropy_gather( entropy_context *ctx );
124
125/**
126 * \brief Retrieve entropy from the accumulator (Max ENTROPY_BLOCK_SIZE)
127 *
128 * \param data Entropy context
129 * \param output Buffer to fill
130 * \param len Length of buffer
131 *
132 * \return 0 if successful, or POLARSSL_ERR_ENTROPY_SOURCE_FAILED
133 */
134int entropy_func( void *data, unsigned char *output, size_t len );
135
136/**
137 * \brief Add data to the accumulator manually
138 *
139 * \param ctx Entropy context
140 * \param data Data to add
141 * \param len Length of data
142 *
143 * \return 0 if successful
144 */
145int entropy_update_manual( entropy_context *ctx,
146 const unsigned char *data, size_t len );
147
Paul Bakker1e942372014-03-26 11:54:05 +0100148#if defined(POLARSSL_FS_IO)
149/**
150 * \brief Write a seed file
151 *
152 * \param ctx Entropy context
153 * \param path Name of the file
154 *
155 * \return 0 if successful,
156 * POLARSSL_ERR_ENTROPY_FILE_IO_ERROR on file error, or
157 * POLARSSL_ERR_ENTROPY_SOURCE_FAILED
158 */
159int entropy_write_seed_file( entropy_context *ctx, const char *path );
160
161/**
162 * \brief Read and update a seed file. Seed is added to this
163 * instance. No more than ENTROPY_MAX_SEED_SIZE bytes are
164 * read from the seed file. The rest is ignored.
165 *
166 * \param ctx Entropy context
167 * \param path Name of the file
168 *
169 * \return 0 if successful,
170 * POLARSSL_ERR_ENTROPY_FILE_IO_ERROR on file error,
171 * POLARSSL_ERR_ENTROPY_SOURCE_FAILED
172 */
173int entropy_update_seed_file( entropy_context *ctx, const char *path );
174#endif
175
Paul Bakker6083fd22011-12-03 21:45:14 +0000176#ifdef __cplusplus
177}
178#endif
179
180#endif /* entropy.h */