blob: 0c1f13fa972954901859f3d53ff2244c205b981c [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
32#include "sha4.h"
33
34#define POLARSSL_ERR_ENTROPY_SOURCE_FAILED -0x003A /**< Critical entropy source failure. */
35#define POLARSSL_ERR_ENTROPY_MAX_SOURCES -0x003C /**< No more sources can be added. */
36
Paul Bakker6083fd22011-12-03 21:45:14 +000037#define ENTROPY_MAX_SOURCES 20 /**< Maximum number of sources supported */
38#define ENTROPY_MAX_GATHER 128 /**< Maximum amount requested from entropy sources */
39#define ENTROPY_BLOCK_SIZE 64 /**< Block size of entropy accumulator (SHA-512) */
40
41#define ENTROPY_SOURCE_MANUAL ENTROPY_MAX_SOURCES
42
43#ifdef __cplusplus
44extern "C" {
45#endif
46
47/**
48 * \brief Entropy poll callback pointer
49 *
50 * \param data Callback-specific data pointer
51 * \param output Data to fill
52 * \param len Maximum size to provide
53 * \param olen The actual amount of bytes put into the buffer (Can be 0)
54 *
55 * \return 0 if no critical failures occurred,
56 * POLARSSL_ERR_ENTROPY_SOURCE_FAILED otherwise
57 */
58typedef int (*f_source_ptr)(void *, unsigned char *, size_t, size_t *);
59
60/**
Paul Bakkerbd4a9d02011-12-10 17:02:19 +000061 * \brief Entropy source state
62 */
63typedef struct
64{
65 f_source_ptr f_source; /**< The entropy source callback */
66 void * p_source; /**< The callback data pointer */
67 size_t size; /**< Amount received */
68 size_t threshold; /**< Minimum level required before release */
69}
70source_state;
71
72/**
Paul Bakker6083fd22011-12-03 21:45:14 +000073 * \brief Entropy context structure
74 */
75typedef struct
76{
77 sha4_context accumulator;
Paul Bakker6083fd22011-12-03 21:45:14 +000078 int source_count;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +000079 source_state source[ENTROPY_MAX_SOURCES];
Paul Bakker6083fd22011-12-03 21:45:14 +000080}
81entropy_context;
82
83/**
84 * \brief Initialize the context
85 *
86 * \param ctx Entropy context to initialize
87 */
88void entropy_init( entropy_context *ctx );
89
90/**
91 * \brief Adds an entropy source to poll
92 *
93 * \param ctx Entropy context
94 * \param f_source Entropy function
95 * \param p_source Function data
Paul Bakkerbd4a9d02011-12-10 17:02:19 +000096 * \param threshold Minimum required from source before entropy is released
97 * ( with entropy_func() )
Paul Bakker6083fd22011-12-03 21:45:14 +000098 *
99 * \return 0 is successful or POLARSSL_ERR_ENTROPY_MAX_SOURCES
100 */
101int entropy_add_source( entropy_context *ctx,
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000102 f_source_ptr f_source, void *p_source,
103 size_t threshold );
Paul Bakker6083fd22011-12-03 21:45:14 +0000104
105/**
106 * \brief Trigger an extra gather poll for the accumulator
107 *
108 * \param ctx Entropy context
109 *
110 * \return 0 if successful, or POLARSSL_ERR_ENTROPY_SOURCE_FAILED
111 */
112int entropy_gather( entropy_context *ctx );
113
114/**
115 * \brief Retrieve entropy from the accumulator (Max ENTROPY_BLOCK_SIZE)
116 *
117 * \param data Entropy context
118 * \param output Buffer to fill
119 * \param len Length of buffer
120 *
121 * \return 0 if successful, or POLARSSL_ERR_ENTROPY_SOURCE_FAILED
122 */
123int entropy_func( void *data, unsigned char *output, size_t len );
124
125/**
126 * \brief Add data to the accumulator manually
127 *
128 * \param ctx Entropy context
129 * \param data Data to add
130 * \param len Length of data
131 *
132 * \return 0 if successful
133 */
134int entropy_update_manual( entropy_context *ctx,
135 const unsigned char *data, size_t len );
136
137#ifdef __cplusplus
138}
139#endif
140
141#endif /* entropy.h */