blob: 235b7733c9c4acb85d7965d5444ec7adde9b6ab0 [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
Paul Bakkerfb08fd22013-08-27 15:06:26 +020034#if defined(POLARSSL_SHA512_C)
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 Bakker6083fd22011-12-03 21:45:14 +000055
Paul Bakker9bcf16c2013-06-24 19:31:17 +020056#if !defined(POLARSSL_CONFIG_OPTIONS)
Paul Bakker6083fd22011-12-03 21:45:14 +000057#define ENTROPY_MAX_SOURCES 20 /**< Maximum number of sources supported */
58#define ENTROPY_MAX_GATHER 128 /**< Maximum amount requested from entropy sources */
Paul Bakker9bcf16c2013-06-24 19:31:17 +020059#endif /* !POLARSSL_CONFIG_OPTIONS */
60
Paul Bakkerfb08fd22013-08-27 15:06:26 +020061#if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
Paul Bakker6083fd22011-12-03 21:45:14 +000062#define ENTROPY_BLOCK_SIZE 64 /**< Block size of entropy accumulator (SHA-512) */
Paul Bakkerfb08fd22013-08-27 15:06:26 +020063#else
64#define ENTROPY_BLOCK_SIZE 32 /**< Block size of entropy accumulator (SHA-256) */
65#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000066
67#define ENTROPY_SOURCE_MANUAL ENTROPY_MAX_SOURCES
68
69#ifdef __cplusplus
70extern "C" {
71#endif
72
73/**
74 * \brief Entropy poll callback pointer
75 *
76 * \param data Callback-specific data pointer
77 * \param output Data to fill
78 * \param len Maximum size to provide
79 * \param olen The actual amount of bytes put into the buffer (Can be 0)
80 *
81 * \return 0 if no critical failures occurred,
82 * POLARSSL_ERR_ENTROPY_SOURCE_FAILED otherwise
83 */
84typedef int (*f_source_ptr)(void *, unsigned char *, size_t, size_t *);
85
86/**
Paul Bakkerbd4a9d02011-12-10 17:02:19 +000087 * \brief Entropy source state
88 */
89typedef struct
90{
91 f_source_ptr f_source; /**< The entropy source callback */
92 void * p_source; /**< The callback data pointer */
93 size_t size; /**< Amount received */
94 size_t threshold; /**< Minimum level required before release */
95}
96source_state;
97
98/**
Paul Bakker6083fd22011-12-03 21:45:14 +000099 * \brief Entropy context structure
100 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200101typedef struct
Paul Bakker6083fd22011-12-03 21:45:14 +0000102{
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200103#if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
Paul Bakker9e36f042013-06-30 14:34:05 +0200104 sha512_context accumulator;
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200105#else
106 sha256_context accumulator;
107#endif
Paul Bakker6083fd22011-12-03 21:45:14 +0000108 int source_count;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000109 source_state source[ENTROPY_MAX_SOURCES];
Paul Bakker28c7e7f2011-12-15 19:49:30 +0000110#if defined(POLARSSL_HAVEGE_C)
111 havege_state havege_data;
112#endif
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200113#if defined(POLARSSL_THREADING_C)
114 threading_mutex_t mutex; /*!< mutex */
115#endif
Paul Bakker6083fd22011-12-03 21:45:14 +0000116}
117entropy_context;
118
119/**
120 * \brief Initialize the context
121 *
122 * \param ctx Entropy context to initialize
123 */
124void entropy_init( entropy_context *ctx );
125
126/**
Paul Bakker1ffefac2013-09-28 15:23:03 +0200127 * \brief Free the data in the context
128 *
129 * \param ctx Entropy context to free
130 */
131void entropy_free( entropy_context *ctx );
132
133/**
Paul Bakker6083fd22011-12-03 21:45:14 +0000134 * \brief Adds an entropy source to poll
135 *
136 * \param ctx Entropy context
137 * \param f_source Entropy function
138 * \param p_source Function data
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000139 * \param threshold Minimum required from source before entropy is released
140 * ( with entropy_func() )
Paul Bakker6083fd22011-12-03 21:45:14 +0000141 *
Paul Bakker43655f42011-12-15 20:11:16 +0000142 * \return 0 if successful or POLARSSL_ERR_ENTROPY_MAX_SOURCES
Paul Bakker6083fd22011-12-03 21:45:14 +0000143 */
144int entropy_add_source( entropy_context *ctx,
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000145 f_source_ptr f_source, void *p_source,
146 size_t threshold );
Paul Bakker6083fd22011-12-03 21:45:14 +0000147
148/**
149 * \brief Trigger an extra gather poll for the accumulator
150 *
151 * \param ctx Entropy context
152 *
153 * \return 0 if successful, or POLARSSL_ERR_ENTROPY_SOURCE_FAILED
154 */
155int entropy_gather( entropy_context *ctx );
156
157/**
158 * \brief Retrieve entropy from the accumulator (Max ENTROPY_BLOCK_SIZE)
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200159 * (Thread-safe if POLARSSL_THREADING_C is enabled)
Paul Bakker6083fd22011-12-03 21:45:14 +0000160 *
161 * \param data Entropy context
162 * \param output Buffer to fill
163 * \param len Length of buffer
164 *
165 * \return 0 if successful, or POLARSSL_ERR_ENTROPY_SOURCE_FAILED
166 */
167int entropy_func( void *data, unsigned char *output, size_t len );
168
169/**
170 * \brief Add data to the accumulator manually
171 *
172 * \param ctx Entropy context
173 * \param data Data to add
174 * \param len Length of data
175 *
176 * \return 0 if successful
177 */
178int entropy_update_manual( entropy_context *ctx,
179 const unsigned char *data, size_t len );
180
181#ifdef __cplusplus
182}
183#endif
184
185#endif /* entropy.h */