blob: 46df9293fda89537a318c0c2dc0b78f4c2a5ef83 [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
37#define ENTROPY_MIN_POOL 128 /**< Minimum amount of pool entropy needed for release */
38#define ENTROPY_MAX_SOURCES 20 /**< Maximum number of sources supported */
39#define ENTROPY_MAX_GATHER 128 /**< Maximum amount requested from entropy sources */
40#define ENTROPY_BLOCK_SIZE 64 /**< Block size of entropy accumulator (SHA-512) */
41
42#define ENTROPY_SOURCE_MANUAL ENTROPY_MAX_SOURCES
43
44#ifdef __cplusplus
45extern "C" {
46#endif
47
48/**
49 * \brief Entropy poll callback pointer
50 *
51 * \param data Callback-specific data pointer
52 * \param output Data to fill
53 * \param len Maximum size to provide
54 * \param olen The actual amount of bytes put into the buffer (Can be 0)
55 *
56 * \return 0 if no critical failures occurred,
57 * POLARSSL_ERR_ENTROPY_SOURCE_FAILED otherwise
58 */
59typedef int (*f_source_ptr)(void *, unsigned char *, size_t, size_t *);
60
61/**
62 * \brief Entropy context structure
63 */
64typedef struct
65{
66 sha4_context accumulator;
67 size_t size;
68 int source_count;
69 f_source_ptr f_source[ENTROPY_MAX_SOURCES];
70 void * p_source[ENTROPY_MAX_SOURCES];
71}
72entropy_context;
73
74/**
75 * \brief Initialize the context
76 *
77 * \param ctx Entropy context to initialize
78 */
79void entropy_init( entropy_context *ctx );
80
81/**
82 * \brief Adds an entropy source to poll
83 *
84 * \param ctx Entropy context
85 * \param f_source Entropy function
86 * \param p_source Function data
87 *
88 * \return 0 is successful or POLARSSL_ERR_ENTROPY_MAX_SOURCES
89 */
90int entropy_add_source( entropy_context *ctx,
91 f_source_ptr f_source, void *p_source );
92
93/**
94 * \brief Trigger an extra gather poll for the accumulator
95 *
96 * \param ctx Entropy context
97 *
98 * \return 0 if successful, or POLARSSL_ERR_ENTROPY_SOURCE_FAILED
99 */
100int entropy_gather( entropy_context *ctx );
101
102/**
103 * \brief Retrieve entropy from the accumulator (Max ENTROPY_BLOCK_SIZE)
104 *
105 * \param data Entropy context
106 * \param output Buffer to fill
107 * \param len Length of buffer
108 *
109 * \return 0 if successful, or POLARSSL_ERR_ENTROPY_SOURCE_FAILED
110 */
111int entropy_func( void *data, unsigned char *output, size_t len );
112
113/**
114 * \brief Add data to the accumulator manually
115 *
116 * \param ctx Entropy context
117 * \param data Data to add
118 * \param len Length of data
119 *
120 * \return 0 if successful
121 */
122int entropy_update_manual( entropy_context *ctx,
123 const unsigned char *data, size_t len );
124
125#ifdef __cplusplus
126}
127#endif
128
129#endif /* entropy.h */