blob: 6dea79a71cc828ba4e57ba127d0ad0173a8e8846 [file] [log] [blame]
Paul Bakker6083fd22011-12-03 21:45:14 +00001/**
2 * \file entropy.h
3 *
4 * \brief Entropy accumulator implementation
5 *
6 * Copyright (C) 2006-2011, Brainspark B.V.
7 *
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 Bakker6083fd22011-12-03 21:45:14 +000041
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 */
44#define ENTROPY_BLOCK_SIZE 64 /**< Block size of entropy accumulator (SHA-512) */
45
46#define ENTROPY_SOURCE_MANUAL ENTROPY_MAX_SOURCES
47
48#ifdef __cplusplus
49extern "C" {
50#endif
51
52/**
53 * \brief Entropy poll callback pointer
54 *
55 * \param data Callback-specific data pointer
56 * \param output Data to fill
57 * \param len Maximum size to provide
58 * \param olen The actual amount of bytes put into the buffer (Can be 0)
59 *
60 * \return 0 if no critical failures occurred,
61 * POLARSSL_ERR_ENTROPY_SOURCE_FAILED otherwise
62 */
63typedef int (*f_source_ptr)(void *, unsigned char *, size_t, size_t *);
64
65/**
Paul Bakkerbd4a9d02011-12-10 17:02:19 +000066 * \brief Entropy source state
67 */
68typedef struct
69{
70 f_source_ptr f_source; /**< The entropy source callback */
71 void * p_source; /**< The callback data pointer */
72 size_t size; /**< Amount received */
73 size_t threshold; /**< Minimum level required before release */
74}
75source_state;
76
77/**
Paul Bakker6083fd22011-12-03 21:45:14 +000078 * \brief Entropy context structure
79 */
80typedef struct
81{
82 sha4_context accumulator;
Paul Bakker6083fd22011-12-03 21:45:14 +000083 int source_count;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +000084 source_state source[ENTROPY_MAX_SOURCES];
Paul Bakker28c7e7f2011-12-15 19:49:30 +000085#if defined(POLARSSL_HAVEGE_C)
86 havege_state havege_data;
87#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000088}
89entropy_context;
90
91/**
92 * \brief Initialize the context
93 *
94 * \param ctx Entropy context to initialize
95 */
96void entropy_init( entropy_context *ctx );
97
98/**
99 * \brief Adds an entropy source to poll
100 *
101 * \param ctx Entropy context
102 * \param f_source Entropy function
103 * \param p_source Function data
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000104 * \param threshold Minimum required from source before entropy is released
105 * ( with entropy_func() )
Paul Bakker6083fd22011-12-03 21:45:14 +0000106 *
107 * \return 0 is successful or POLARSSL_ERR_ENTROPY_MAX_SOURCES
108 */
109int entropy_add_source( entropy_context *ctx,
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000110 f_source_ptr f_source, void *p_source,
111 size_t threshold );
Paul Bakker6083fd22011-12-03 21:45:14 +0000112
113/**
114 * \brief Trigger an extra gather poll for the accumulator
115 *
116 * \param ctx Entropy context
117 *
118 * \return 0 if successful, or POLARSSL_ERR_ENTROPY_SOURCE_FAILED
119 */
120int entropy_gather( entropy_context *ctx );
121
122/**
123 * \brief Retrieve entropy from the accumulator (Max ENTROPY_BLOCK_SIZE)
124 *
125 * \param data Entropy context
126 * \param output Buffer to fill
127 * \param len Length of buffer
128 *
129 * \return 0 if successful, or POLARSSL_ERR_ENTROPY_SOURCE_FAILED
130 */
131int entropy_func( void *data, unsigned char *output, size_t len );
132
133/**
134 * \brief Add data to the accumulator manually
135 *
136 * \param ctx Entropy context
137 * \param data Data to add
138 * \param len Length of data
139 *
140 * \return 0 if successful
141 */
142int entropy_update_manual( entropy_context *ctx,
143 const unsigned char *data, size_t len );
144
145#ifdef __cplusplus
146}
147#endif
148
149#endif /* entropy.h */