blob: 53a1f0e257776f1160a52af9a14ae0a0bcefbb3c [file] [log] [blame]
Paul Bakker6083fd22011-12-03 21:45:14 +00001/**
2 * \file entropy.h
3 *
4 * \brief Entropy accumulator implementation
5 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00006 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakker6083fd22011-12-03 21:45:14 +00007 *
Manuel Pégourié-Gonnard860b5162015-01-28 17:12:07 +00008 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakker6083fd22011-12-03 21:45:14 +00009 *
Paul Bakker6083fd22011-12-03 21:45:14 +000010 * 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
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker28c7e7f2011-12-15 19:49:30 +000028#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
30#include POLARSSL_CONFIG_FILE
31#endif
Paul Bakker28c7e7f2011-12-15 19:49:30 +000032
Rich Evans00ab4702015-02-06 13:43:58 +000033#include <stddef.h>
34
Manuel Pégourié-Gonnarde4421112014-04-02 13:50:05 +020035#if defined(POLARSSL_SHA512_C) && !defined(POLARSSL_ENTROPY_FORCE_SHA256)
Paul Bakkerd2681d82013-06-30 14:49:12 +020036#include "sha512.h"
Paul Bakkerfb08fd22013-08-27 15:06:26 +020037#define POLARSSL_ENTROPY_SHA512_ACCUMULATOR
38#else
39#if defined(POLARSSL_SHA256_C)
40#define POLARSSL_ENTROPY_SHA256_ACCUMULATOR
41#include "sha256.h"
42#endif
43#endif
44
Paul Bakkerf4e7dc52013-09-28 15:23:57 +020045#if defined(POLARSSL_THREADING_C)
46#include "threading.h"
47#endif
48
Paul Bakker28c7e7f2011-12-15 19:49:30 +000049#if defined(POLARSSL_HAVEGE_C)
50#include "havege.h"
51#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000052
Paul Bakker69e095c2011-12-10 21:55:01 +000053#define POLARSSL_ERR_ENTROPY_SOURCE_FAILED -0x003C /**< Critical entropy source failure. */
54#define POLARSSL_ERR_ENTROPY_MAX_SOURCES -0x003E /**< No more sources can be added. */
Paul Bakker43655f42011-12-15 20:11:16 +000055#define POLARSSL_ERR_ENTROPY_NO_SOURCES_DEFINED -0x0040 /**< No sources have been added to poll. */
Paul Bakker66ff70d2014-03-26 11:54:05 +010056#define POLARSSL_ERR_ENTROPY_FILE_IO_ERROR -0x0058 /**< Read/write error in file. */
Paul Bakker6083fd22011-12-03 21:45:14 +000057
Paul Bakker088c5c52014-04-25 11:11:10 +020058/**
59 * \name SECTION: Module settings
60 *
61 * The configuration options you can set for this module are in this section.
62 * Either change them in config.h or define them on the compiler command line.
63 * \{
64 */
65
66#if !defined(ENTROPY_MAX_SOURCES)
Paul Bakker6083fd22011-12-03 21:45:14 +000067#define ENTROPY_MAX_SOURCES 20 /**< Maximum number of sources supported */
Paul Bakker088c5c52014-04-25 11:11:10 +020068#endif
69
70#if !defined(ENTROPY_MAX_GATHER)
Paul Bakker6083fd22011-12-03 21:45:14 +000071#define ENTROPY_MAX_GATHER 128 /**< Maximum amount requested from entropy sources */
Paul Bakker088c5c52014-04-25 11:11:10 +020072#endif
73
74/* \} name SECTION: Module settings */
Paul Bakker9bcf16c2013-06-24 19:31:17 +020075
Paul Bakkerfb08fd22013-08-27 15:06:26 +020076#if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
Paul Bakker6083fd22011-12-03 21:45:14 +000077#define ENTROPY_BLOCK_SIZE 64 /**< Block size of entropy accumulator (SHA-512) */
Paul Bakkerfb08fd22013-08-27 15:06:26 +020078#else
79#define ENTROPY_BLOCK_SIZE 32 /**< Block size of entropy accumulator (SHA-256) */
80#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000081
Paul Bakker66ff70d2014-03-26 11:54:05 +010082#define ENTROPY_MAX_SEED_SIZE 1024 /**< Maximum size of seed we read from seed file */
Paul Bakker6083fd22011-12-03 21:45:14 +000083#define ENTROPY_SOURCE_MANUAL ENTROPY_MAX_SOURCES
84
85#ifdef __cplusplus
86extern "C" {
87#endif
88
89/**
90 * \brief Entropy poll callback pointer
91 *
92 * \param data Callback-specific data pointer
93 * \param output Data to fill
94 * \param len Maximum size to provide
95 * \param olen The actual amount of bytes put into the buffer (Can be 0)
96 *
97 * \return 0 if no critical failures occurred,
98 * POLARSSL_ERR_ENTROPY_SOURCE_FAILED otherwise
99 */
Paul Bakkera36d23e2013-12-30 17:57:27 +0100100typedef int (*f_source_ptr)(void *data, unsigned char *output, size_t len,
101 size_t *olen);
Paul Bakker6083fd22011-12-03 21:45:14 +0000102
103/**
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000104 * \brief Entropy source state
105 */
106typedef struct
107{
108 f_source_ptr f_source; /**< The entropy source callback */
109 void * p_source; /**< The callback data pointer */
110 size_t size; /**< Amount received */
111 size_t threshold; /**< Minimum level required before release */
112}
113source_state;
114
115/**
Paul Bakker6083fd22011-12-03 21:45:14 +0000116 * \brief Entropy context structure
117 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200118typedef struct
Paul Bakker6083fd22011-12-03 21:45:14 +0000119{
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200120#if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
Paul Bakker9e36f042013-06-30 14:34:05 +0200121 sha512_context accumulator;
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200122#else
123 sha256_context accumulator;
124#endif
Paul Bakker6083fd22011-12-03 21:45:14 +0000125 int source_count;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000126 source_state source[ENTROPY_MAX_SOURCES];
Paul Bakker28c7e7f2011-12-15 19:49:30 +0000127#if defined(POLARSSL_HAVEGE_C)
128 havege_state havege_data;
129#endif
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200130#if defined(POLARSSL_THREADING_C)
131 threading_mutex_t mutex; /*!< mutex */
132#endif
Paul Bakker6083fd22011-12-03 21:45:14 +0000133}
134entropy_context;
135
136/**
137 * \brief Initialize the context
138 *
139 * \param ctx Entropy context to initialize
140 */
141void entropy_init( entropy_context *ctx );
142
143/**
Paul Bakker1ffefac2013-09-28 15:23:03 +0200144 * \brief Free the data in the context
145 *
146 * \param ctx Entropy context to free
147 */
148void entropy_free( entropy_context *ctx );
149
150/**
Paul Bakker6083fd22011-12-03 21:45:14 +0000151 * \brief Adds an entropy source to poll
Paul Bakker47703a02014-02-06 15:01:20 +0100152 * (Thread-safe if POLARSSL_THREADING_C is enabled)
Paul Bakker6083fd22011-12-03 21:45:14 +0000153 *
154 * \param ctx Entropy context
155 * \param f_source Entropy function
156 * \param p_source Function data
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000157 * \param threshold Minimum required from source before entropy is released
158 * ( with entropy_func() )
Paul Bakker6083fd22011-12-03 21:45:14 +0000159 *
Paul Bakker43655f42011-12-15 20:11:16 +0000160 * \return 0 if successful or POLARSSL_ERR_ENTROPY_MAX_SOURCES
Paul Bakker6083fd22011-12-03 21:45:14 +0000161 */
162int entropy_add_source( entropy_context *ctx,
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000163 f_source_ptr f_source, void *p_source,
164 size_t threshold );
Paul Bakker6083fd22011-12-03 21:45:14 +0000165
166/**
167 * \brief Trigger an extra gather poll for the accumulator
Paul Bakker47703a02014-02-06 15:01:20 +0100168 * (Thread-safe if POLARSSL_THREADING_C is enabled)
Paul Bakker6083fd22011-12-03 21:45:14 +0000169 *
170 * \param ctx Entropy context
171 *
172 * \return 0 if successful, or POLARSSL_ERR_ENTROPY_SOURCE_FAILED
173 */
174int entropy_gather( entropy_context *ctx );
175
176/**
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200177 * \brief Retrieve entropy from the accumulator
178 * (Maximum length: ENTROPY_BLOCK_SIZE)
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200179 * (Thread-safe if POLARSSL_THREADING_C is enabled)
Paul Bakker6083fd22011-12-03 21:45:14 +0000180 *
181 * \param data Entropy context
182 * \param output Buffer to fill
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200183 * \param len Number of bytes desired, must be at most ENTROPY_BLOCK_SIZE
Paul Bakker6083fd22011-12-03 21:45:14 +0000184 *
185 * \return 0 if successful, or POLARSSL_ERR_ENTROPY_SOURCE_FAILED
186 */
187int entropy_func( void *data, unsigned char *output, size_t len );
188
189/**
190 * \brief Add data to the accumulator manually
Paul Bakker47703a02014-02-06 15:01:20 +0100191 * (Thread-safe if POLARSSL_THREADING_C is enabled)
Paul Bakker9af723c2014-05-01 13:03:14 +0200192 *
Paul Bakker6083fd22011-12-03 21:45:14 +0000193 * \param ctx Entropy context
194 * \param data Data to add
195 * \param len Length of data
196 *
197 * \return 0 if successful
198 */
199int entropy_update_manual( entropy_context *ctx,
200 const unsigned char *data, size_t len );
201
Paul Bakker66ff70d2014-03-26 11:54:05 +0100202#if defined(POLARSSL_FS_IO)
203/**
204 * \brief Write a seed file
205 *
206 * \param ctx Entropy context
207 * \param path Name of the file
208 *
209 * \return 0 if successful,
210 * POLARSSL_ERR_ENTROPY_FILE_IO_ERROR on file error, or
211 * POLARSSL_ERR_ENTROPY_SOURCE_FAILED
212 */
213int entropy_write_seed_file( entropy_context *ctx, const char *path );
214
215/**
216 * \brief Read and update a seed file. Seed is added to this
217 * instance. No more than ENTROPY_MAX_SEED_SIZE bytes are
218 * read from the seed file. The rest is ignored.
219 *
220 * \param ctx Entropy context
221 * \param path Name of the file
222 *
223 * \return 0 if successful,
224 * POLARSSL_ERR_ENTROPY_FILE_IO_ERROR on file error,
225 * POLARSSL_ERR_ENTROPY_SOURCE_FAILED
226 */
227int entropy_update_seed_file( entropy_context *ctx, const char *path );
Paul Bakker9af723c2014-05-01 13:03:14 +0200228#endif /* POLARSSL_FS_IO */
Paul Bakker66ff70d2014-03-26 11:54:05 +0100229
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200230#if defined(POLARSSL_SELF_TEST)
231/**
232 * \brief Checkup routine
233 *
234 * \return 0 if successful, or 1 if a test failed
235 */
236int entropy_self_test( int verbose );
237#endif /* POLARSSL_SELF_TEST */
238
Paul Bakker6083fd22011-12-03 21:45:14 +0000239#ifdef __cplusplus
240}
241#endif
242
243#endif /* entropy.h */