blob: 039664b99664ea8d3f7380749473f5d081b3815c [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é-Gonnardfe446432015-03-06 13:17:10 +00008 * This file is part of mbed TLS (https://tls.mbed.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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#ifndef MBEDTLS_ENTROPY_H
25#define MBEDTLS_ENTROPY_H
Paul Bakker6083fd22011-12-03 21:45:14 +000026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker28c7e7f2011-12-15 19:49:30 +000028#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#endif
Paul Bakker28c7e7f2011-12-15 19:49:30 +000032
Rich Evans00ab4702015-02-06 13:43:58 +000033#include <stddef.h>
34
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256)
Paul Bakkerd2681d82013-06-30 14:49:12 +020036#include "sha512.h"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#define MBEDTLS_ENTROPY_SHA512_ACCUMULATOR
Paul Bakkerfb08fd22013-08-27 15:06:26 +020038#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if defined(MBEDTLS_SHA256_C)
40#define MBEDTLS_ENTROPY_SHA256_ACCUMULATOR
Paul Bakkerfb08fd22013-08-27 15:06:26 +020041#include "sha256.h"
42#endif
43#endif
44
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_THREADING_C)
Paul Bakkerf4e7dc52013-09-28 15:23:57 +020046#include "threading.h"
47#endif
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if defined(MBEDTLS_HAVEGE_C)
Paul Bakker28c7e7f2011-12-15 19:49:30 +000050#include "havege.h"
51#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#define MBEDTLS_ERR_ENTROPY_SOURCE_FAILED -0x003C /**< Critical entropy source failure. */
54#define MBEDTLS_ERR_ENTROPY_MAX_SOURCES -0x003E /**< No more sources can be added. */
55#define MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED -0x0040 /**< No sources have been added to poll. */
56#define MBEDTLS_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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#if !defined(MBEDTLS_ENTROPY_MAX_SOURCES)
67#define MBEDTLS_ENTROPY_MAX_SOURCES 20 /**< Maximum number of sources supported */
Paul Bakker088c5c52014-04-25 11:11:10 +020068#endif
69
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070#if !defined(MBEDTLS_ENTROPY_MAX_GATHER)
71#define MBEDTLS_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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
77#define MBEDTLS_ENTROPY_BLOCK_SIZE 64 /**< Block size of entropy accumulator (SHA-512) */
Paul Bakkerfb08fd22013-08-27 15:06:26 +020078#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079#define MBEDTLS_ENTROPY_BLOCK_SIZE 32 /**< Block size of entropy accumulator (SHA-256) */
Paul Bakkerfb08fd22013-08-27 15:06:26 +020080#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082#define MBEDTLS_ENTROPY_MAX_SEED_SIZE 1024 /**< Maximum size of seed we read from seed file */
83#define MBEDTLS_ENTROPY_SOURCE_MANUAL MBEDTLS_ENTROPY_MAX_SOURCES
Paul Bakker6083fd22011-12-03 21:45:14 +000084
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,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 * MBEDTLS_ERR_ENTROPY_SOURCE_FAILED otherwise
Paul Bakker6083fd22011-12-03 21:45:14 +000099 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100typedef int (*mbedtls_entropy_f_source_ptr)(void *data, unsigned char *output, size_t len,
Paul Bakkera36d23e2013-12-30 17:57:27 +0100101 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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108 mbedtls_entropy_f_source_ptr f_source; /**< The entropy source callback */
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000109 void * p_source; /**< The callback data pointer */
110 size_t size; /**< Amount received */
111 size_t threshold; /**< Minimum level required before release */
112}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113mbedtls_entropy_source_state;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000114
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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
121 mbedtls_sha512_context accumulator;
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200122#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 mbedtls_sha256_context accumulator;
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200124#endif
Paul Bakker6083fd22011-12-03 21:45:14 +0000125 int source_count;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 mbedtls_entropy_source_state source[MBEDTLS_ENTROPY_MAX_SOURCES];
127#if defined(MBEDTLS_HAVEGE_C)
128 mbedtls_havege_state havege_data;
Paul Bakker28c7e7f2011-12-15 19:49:30 +0000129#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130#if defined(MBEDTLS_THREADING_C)
131 mbedtls_threading_mutex_t mutex; /*!< mutex */
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200132#endif
Paul Bakker6083fd22011-12-03 21:45:14 +0000133}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134mbedtls_entropy_context;
Paul Bakker6083fd22011-12-03 21:45:14 +0000135
136/**
137 * \brief Initialize the context
138 *
139 * \param ctx Entropy context to initialize
140 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141void mbedtls_entropy_init( mbedtls_entropy_context *ctx );
Paul Bakker6083fd22011-12-03 21:45:14 +0000142
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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148void mbedtls_entropy_free( mbedtls_entropy_context *ctx );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200149
150/**
Paul Bakker6083fd22011-12-03 21:45:14 +0000151 * \brief Adds an entropy source to poll
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 * (Thread-safe if MBEDTLS_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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 * ( with mbedtls_entropy_func() )
Paul Bakker6083fd22011-12-03 21:45:14 +0000159 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 * \return 0 if successful or MBEDTLS_ERR_ENTROPY_MAX_SOURCES
Paul Bakker6083fd22011-12-03 21:45:14 +0000161 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162int mbedtls_entropy_add_source( mbedtls_entropy_context *ctx,
163 mbedtls_entropy_f_source_ptr f_source, void *p_source,
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000164 size_t threshold );
Paul Bakker6083fd22011-12-03 21:45:14 +0000165
166/**
167 * \brief Trigger an extra gather poll for the accumulator
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 * (Thread-safe if MBEDTLS_THREADING_C is enabled)
Paul Bakker6083fd22011-12-03 21:45:14 +0000169 *
170 * \param ctx Entropy context
171 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 * \return 0 if successful, or MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
Paul Bakker6083fd22011-12-03 21:45:14 +0000173 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174int mbedtls_entropy_gather( mbedtls_entropy_context *ctx );
Paul Bakker6083fd22011-12-03 21:45:14 +0000175
176/**
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200177 * \brief Retrieve entropy from the accumulator
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 * (Maximum length: MBEDTLS_ENTROPY_BLOCK_SIZE)
179 * (Thread-safe if MBEDTLS_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é-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 * \param len Number of bytes desired, must be at most MBEDTLS_ENTROPY_BLOCK_SIZE
Paul Bakker6083fd22011-12-03 21:45:14 +0000184 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 * \return 0 if successful, or MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
Paul Bakker6083fd22011-12-03 21:45:14 +0000186 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187int mbedtls_entropy_func( void *data, unsigned char *output, size_t len );
Paul Bakker6083fd22011-12-03 21:45:14 +0000188
189/**
190 * \brief Add data to the accumulator manually
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 * (Thread-safe if MBEDTLS_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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199int mbedtls_entropy_update_manual( mbedtls_entropy_context *ctx,
Paul Bakker6083fd22011-12-03 21:45:14 +0000200 const unsigned char *data, size_t len );
201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202#if defined(MBEDTLS_FS_IO)
Paul Bakker66ff70d2014-03-26 11:54:05 +0100203/**
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,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 * MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR on file error, or
211 * MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
Paul Bakker66ff70d2014-03-26 11:54:05 +0100212 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213int mbedtls_entropy_write_seed_file( mbedtls_entropy_context *ctx, const char *path );
Paul Bakker66ff70d2014-03-26 11:54:05 +0100214
215/**
216 * \brief Read and update a seed file. Seed is added to this
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 * instance. No more than MBEDTLS_ENTROPY_MAX_SEED_SIZE bytes are
Paul Bakker66ff70d2014-03-26 11:54:05 +0100218 * 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,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 * MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR on file error,
225 * MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
Paul Bakker66ff70d2014-03-26 11:54:05 +0100226 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227int mbedtls_entropy_update_seed_file( mbedtls_entropy_context *ctx, const char *path );
228#endif /* MBEDTLS_FS_IO */
Paul Bakker66ff70d2014-03-26 11:54:05 +0100229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200231/**
232 * \brief Checkup routine
233 *
234 * \return 0 if successful, or 1 if a test failed
235 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236int mbedtls_entropy_self_test( int verbose );
237#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200238
Paul Bakker6083fd22011-12-03 21:45:14 +0000239#ifdef __cplusplus
240}
241#endif
242
243#endif /* entropy.h */