blob: 468ef03cf18d568435a6bd1a5af9c17fd630f35b [file] [log] [blame]
Paul Bakker6083fd22011-12-03 21:45:14 +00001/*
2 * Entropy accumulator implementation
3 *
4 * Copyright (C) 2006-2011, Brainspark B.V.
5 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
26#include "polarssl/config.h"
27
28#if defined(POLARSSL_ENTROPY_C)
29
30#include "polarssl/entropy.h"
31#include "polarssl/entropy_poll.h"
32
Paul Bakker1e942372014-03-26 11:54:05 +010033#if defined(POLARSSL_FS_IO)
34#include <stdio.h>
35#endif
36
Paul Bakker28c7e7f2011-12-15 19:49:30 +000037#if defined(POLARSSL_HAVEGE_C)
38#include "polarssl/havege.h"
39#endif
40
Paul Bakker6083fd22011-12-03 21:45:14 +000041#define ENTROPY_MAX_LOOP 256 /**< Maximum amount to loop before error */
42
43void entropy_init( entropy_context *ctx )
44{
45 memset( ctx, 0, sizeof(entropy_context) );
46
47 sha4_starts( &ctx->accumulator, 0 );
Paul Bakker43655f42011-12-15 20:11:16 +000048#if defined(POLARSSL_HAVEGE_C)
49 havege_init( &ctx->havege_data );
50#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000051
Paul Bakker43655f42011-12-15 20:11:16 +000052#if !defined(POLARSSL_NO_DEFAULT_ENTROPY_SOURCES)
Paul Bakker6083fd22011-12-03 21:45:14 +000053#if !defined(POLARSSL_NO_PLATFORM_ENTROPY)
Paul Bakkerbd4a9d02011-12-10 17:02:19 +000054 entropy_add_source( ctx, platform_entropy_poll, NULL,
55 ENTROPY_MIN_PLATFORM );
Paul Bakker6083fd22011-12-03 21:45:14 +000056#endif
57#if defined(POLARSSL_TIMING_C)
Paul Bakkerbd4a9d02011-12-10 17:02:19 +000058 entropy_add_source( ctx, hardclock_poll, NULL, ENTROPY_MIN_HARDCLOCK );
Paul Bakker6083fd22011-12-03 21:45:14 +000059#endif
Paul Bakker28c7e7f2011-12-15 19:49:30 +000060#if defined(POLARSSL_HAVEGE_C)
Paul Bakker28c7e7f2011-12-15 19:49:30 +000061 entropy_add_source( ctx, havege_poll, &ctx->havege_data,
62 ENTROPY_MIN_HAVEGE );
63#endif
Paul Bakker43655f42011-12-15 20:11:16 +000064#endif /* POLARSSL_NO_DEFAULT_ENTROPY_SOURCES */
Paul Bakker6083fd22011-12-03 21:45:14 +000065}
66
67int entropy_add_source( entropy_context *ctx,
Paul Bakkerbd4a9d02011-12-10 17:02:19 +000068 f_source_ptr f_source, void *p_source,
69 size_t threshold )
Paul Bakker6083fd22011-12-03 21:45:14 +000070{
Paul Bakkerbd4a9d02011-12-10 17:02:19 +000071 int index = ctx->source_count;
72
73 if( index >= ENTROPY_MAX_SOURCES )
Paul Bakker6083fd22011-12-03 21:45:14 +000074 return( POLARSSL_ERR_ENTROPY_MAX_SOURCES );
75
Paul Bakkerbd4a9d02011-12-10 17:02:19 +000076 ctx->source[index].f_source = f_source;
77 ctx->source[index].p_source = p_source;
78 ctx->source[index].threshold = threshold;
Paul Bakker6083fd22011-12-03 21:45:14 +000079
80 ctx->source_count++;
81
82 return( 0 );
83}
84
85/*
86 * Entropy accumulator update
87 */
88int entropy_update( entropy_context *ctx, unsigned char source_id,
89 const unsigned char *data, size_t len )
90{
91 unsigned char header[2];
92 unsigned char tmp[ENTROPY_BLOCK_SIZE];
93 size_t use_len = len;
94 const unsigned char *p = data;
95
96 if( use_len > ENTROPY_BLOCK_SIZE )
97 {
98 sha4( data, len, tmp, 0 );
99
100 p = tmp;
101 use_len = ENTROPY_BLOCK_SIZE;
102 }
103
104 header[0] = source_id;
105 header[1] = use_len & 0xFF;
106
107 sha4_update( &ctx->accumulator, header, 2 );
108 sha4_update( &ctx->accumulator, p, use_len );
109
Paul Bakker6083fd22011-12-03 21:45:14 +0000110 return( 0 );
111}
112
113int entropy_update_manual( entropy_context *ctx,
114 const unsigned char *data, size_t len )
115{
116 return entropy_update( ctx, ENTROPY_SOURCE_MANUAL, data, len );
117}
118
119/*
120 * Run through the different sources to add entropy to our accumulator
121 */
122int entropy_gather( entropy_context *ctx )
123{
124 int ret, i;
125 unsigned char buf[ENTROPY_MAX_GATHER];
126 size_t olen;
127
Paul Bakker43655f42011-12-15 20:11:16 +0000128 if( ctx->source_count == 0 )
129 return( POLARSSL_ERR_ENTROPY_NO_SOURCES_DEFINED );
130
Paul Bakker6083fd22011-12-03 21:45:14 +0000131 /*
132 * Run through our entropy sources
133 */
134 for( i = 0; i < ctx->source_count; i++ )
135 {
136 olen = 0;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000137 if ( ( ret = ctx->source[i].f_source( ctx->source[i].p_source,
Paul Bakker6083fd22011-12-03 21:45:14 +0000138 buf, ENTROPY_MAX_GATHER, &olen ) ) != 0 )
139 {
140 return( ret );
141 }
142
143 /*
144 * Add if we actually gathered something
145 */
146 if( olen > 0 )
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000147 {
Paul Bakker6083fd22011-12-03 21:45:14 +0000148 entropy_update( ctx, (unsigned char) i, buf, olen );
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000149 ctx->source[i].size += olen;
150 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000151 }
152
153 return( 0 );
154}
155
156int entropy_func( void *data, unsigned char *output, size_t len )
157{
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000158 int ret, count = 0, i, reached;
Paul Bakker6083fd22011-12-03 21:45:14 +0000159 entropy_context *ctx = (entropy_context *) data;
160 unsigned char buf[ENTROPY_BLOCK_SIZE];
161
162 if( len > ENTROPY_BLOCK_SIZE )
163 return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED );
164
165 /*
166 * Always gather extra entropy before a call
167 */
168 do
169 {
170 if( count++ > ENTROPY_MAX_LOOP )
171 return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED );
172
173 if( ( ret = entropy_gather( ctx ) ) != 0 )
174 return( ret );
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000175
176 reached = 0;
177
178 for( i = 0; i < ctx->source_count; i++ )
179 if( ctx->source[i].size >= ctx->source[i].threshold )
180 reached++;
Paul Bakker6083fd22011-12-03 21:45:14 +0000181 }
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000182 while( reached != ctx->source_count );
Paul Bakker6083fd22011-12-03 21:45:14 +0000183
184 memset( buf, 0, ENTROPY_BLOCK_SIZE );
185
186 sha4_finish( &ctx->accumulator, buf );
Paul Bakker6083fd22011-12-03 21:45:14 +0000187
188 /*
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000189 * Reset accumulator and counters and recycle existing entropy
Paul Bakker6083fd22011-12-03 21:45:14 +0000190 */
191 memset( &ctx->accumulator, 0, sizeof( sha4_context ) );
192 sha4_starts( &ctx->accumulator, 0 );
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000193 sha4_update( &ctx->accumulator, buf, ENTROPY_BLOCK_SIZE );
194
Paul Bakkeref3cf702014-03-26 12:51:25 +0100195 /*
196 * Perform second SHA-512 on entropy
197 */
198 sha4( buf, ENTROPY_BLOCK_SIZE, buf, 0 );
199
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000200 for( i = 0; i < ctx->source_count; i++ )
201 ctx->source[i].size = 0;
Paul Bakker6083fd22011-12-03 21:45:14 +0000202
203 memcpy( output, buf, len );
204
205 return( 0 );
206}
207
Paul Bakker1e942372014-03-26 11:54:05 +0100208#if defined(POLARSSL_FS_IO)
209int entropy_write_seed_file( entropy_context *ctx, const char *path )
210{
211 int ret = POLARSSL_ERR_ENTROPY_FILE_IO_ERROR;
212 FILE *f;
213 unsigned char buf[ENTROPY_BLOCK_SIZE];
214
215 if( ( f = fopen( path, "wb" ) ) == NULL )
216 return( POLARSSL_ERR_ENTROPY_FILE_IO_ERROR );
217
218 if( ( ret = entropy_func( ctx, buf, ENTROPY_BLOCK_SIZE ) ) != 0 )
219 goto exit;
220
221 if( fwrite( buf, 1, ENTROPY_BLOCK_SIZE, f ) != ENTROPY_BLOCK_SIZE )
222 {
223 ret = POLARSSL_ERR_ENTROPY_FILE_IO_ERROR;
224 goto exit;
225 }
226
227 ret = 0;
228
229exit:
230 fclose( f );
231 return( ret );
232}
233
234int entropy_update_seed_file( entropy_context *ctx, const char *path )
235{
236 FILE *f;
237 size_t n;
238 unsigned char buf[ ENTROPY_MAX_SEED_SIZE ];
239
240 if( ( f = fopen( path, "rb" ) ) == NULL )
241 return( POLARSSL_ERR_ENTROPY_FILE_IO_ERROR );
242
243 fseek( f, 0, SEEK_END );
244 n = (size_t) ftell( f );
245 fseek( f, 0, SEEK_SET );
246
247 if( n > ENTROPY_MAX_SEED_SIZE )
248 n = ENTROPY_MAX_SEED_SIZE;
249
250 if( fread( buf, 1, n, f ) != n )
251 {
252 fclose( f );
253 return( POLARSSL_ERR_ENTROPY_FILE_IO_ERROR );
254 }
255
256 fclose( f );
257
258 entropy_update_manual( ctx, buf, n );
259
260 return( entropy_write_seed_file( ctx, path ) );
261}
262#endif /* POLARSSL_FS_IO */
263
Paul Bakker6083fd22011-12-03 21:45:14 +0000264#endif