blob: 068a50113ba1eebd5a88e6ac1a6848cd5ab2bef7 [file] [log] [blame]
Paul Bakker6083fd22011-12-03 21:45:14 +00001/*
2 * Entropy accumulator implementation
3 *
Paul Bakker9af723c2014-05-01 13:03:14 +02004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakker6083fd22011-12-03 21:45:14 +00005 *
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
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker6083fd22011-12-03 21:45:14 +000027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000031
32#if defined(POLARSSL_ENTROPY_C)
33
34#include "polarssl/entropy.h"
35#include "polarssl/entropy_poll.h"
36
Paul Bakker66ff70d2014-03-26 11:54:05 +010037#if defined(POLARSSL_FS_IO)
38#include <stdio.h>
39#endif
40
Paul Bakker28c7e7f2011-12-15 19:49:30 +000041#if defined(POLARSSL_HAVEGE_C)
42#include "polarssl/havege.h"
43#endif
44
Paul Bakker34617722014-06-13 17:20:13 +020045/* Implementation that should never be optimized out by the compiler */
46static void polarssl_zeroize( void *v, size_t n ) {
47 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
48}
49
Paul Bakker6083fd22011-12-03 21:45:14 +000050#define ENTROPY_MAX_LOOP 256 /**< Maximum amount to loop before error */
51
52void entropy_init( entropy_context *ctx )
53{
54 memset( ctx, 0, sizeof(entropy_context) );
55
Paul Bakkerf4e7dc52013-09-28 15:23:57 +020056#if defined(POLARSSL_THREADING_C)
57 polarssl_mutex_init( &ctx->mutex );
58#endif
59
Paul Bakkerfb08fd22013-08-27 15:06:26 +020060#if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
Paul Bakker9e36f042013-06-30 14:34:05 +020061 sha512_starts( &ctx->accumulator, 0 );
Paul Bakkerfb08fd22013-08-27 15:06:26 +020062#else
63 sha256_starts( &ctx->accumulator, 0 );
64#endif
Paul Bakker43655f42011-12-15 20:11:16 +000065#if defined(POLARSSL_HAVEGE_C)
66 havege_init( &ctx->havege_data );
67#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000068
Paul Bakker43655f42011-12-15 20:11:16 +000069#if !defined(POLARSSL_NO_DEFAULT_ENTROPY_SOURCES)
Paul Bakker6083fd22011-12-03 21:45:14 +000070#if !defined(POLARSSL_NO_PLATFORM_ENTROPY)
Paul Bakkerbd4a9d02011-12-10 17:02:19 +000071 entropy_add_source( ctx, platform_entropy_poll, NULL,
72 ENTROPY_MIN_PLATFORM );
Paul Bakker6083fd22011-12-03 21:45:14 +000073#endif
74#if defined(POLARSSL_TIMING_C)
Paul Bakkerbd4a9d02011-12-10 17:02:19 +000075 entropy_add_source( ctx, hardclock_poll, NULL, ENTROPY_MIN_HARDCLOCK );
Paul Bakker6083fd22011-12-03 21:45:14 +000076#endif
Paul Bakker28c7e7f2011-12-15 19:49:30 +000077#if defined(POLARSSL_HAVEGE_C)
Paul Bakker28c7e7f2011-12-15 19:49:30 +000078 entropy_add_source( ctx, havege_poll, &ctx->havege_data,
79 ENTROPY_MIN_HAVEGE );
80#endif
Paul Bakker43655f42011-12-15 20:11:16 +000081#endif /* POLARSSL_NO_DEFAULT_ENTROPY_SOURCES */
Paul Bakker6083fd22011-12-03 21:45:14 +000082}
83
Paul Bakker1ffefac2013-09-28 15:23:03 +020084void entropy_free( entropy_context *ctx )
85{
Paul Bakker34617722014-06-13 17:20:13 +020086 polarssl_zeroize( ctx, sizeof( entropy_context ) );
Paul Bakkerf4e7dc52013-09-28 15:23:57 +020087#if defined(POLARSSL_THREADING_C)
88 polarssl_mutex_free( &ctx->mutex );
89#endif
Paul Bakker1ffefac2013-09-28 15:23:03 +020090}
91
Paul Bakker6083fd22011-12-03 21:45:14 +000092int entropy_add_source( entropy_context *ctx,
Paul Bakkerbd4a9d02011-12-10 17:02:19 +000093 f_source_ptr f_source, void *p_source,
94 size_t threshold )
Paul Bakker6083fd22011-12-03 21:45:14 +000095{
Paul Bakker47703a02014-02-06 15:01:20 +010096 int index, ret = 0;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +000097
Paul Bakker47703a02014-02-06 15:01:20 +010098#if defined(POLARSSL_THREADING_C)
99 if( ( ret = polarssl_mutex_lock( &ctx->mutex ) ) != 0 )
100 return( ret );
101#endif
102
103 index = ctx->source_count;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000104 if( index >= ENTROPY_MAX_SOURCES )
Paul Bakker47703a02014-02-06 15:01:20 +0100105 {
106 ret = POLARSSL_ERR_ENTROPY_MAX_SOURCES;
107 goto exit;
108 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000109
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000110 ctx->source[index].f_source = f_source;
111 ctx->source[index].p_source = p_source;
112 ctx->source[index].threshold = threshold;
Paul Bakker6083fd22011-12-03 21:45:14 +0000113
114 ctx->source_count++;
115
Paul Bakker47703a02014-02-06 15:01:20 +0100116exit:
117#if defined(POLARSSL_THREADING_C)
118 if( polarssl_mutex_unlock( &ctx->mutex ) != 0 )
119 return( POLARSSL_ERR_THREADING_MUTEX_ERROR );
120#endif
121
122 return( ret );
Paul Bakker6083fd22011-12-03 21:45:14 +0000123}
124
125/*
126 * Entropy accumulator update
127 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200128static int entropy_update( entropy_context *ctx, unsigned char source_id,
129 const unsigned char *data, size_t len )
Paul Bakker6083fd22011-12-03 21:45:14 +0000130{
131 unsigned char header[2];
132 unsigned char tmp[ENTROPY_BLOCK_SIZE];
133 size_t use_len = len;
134 const unsigned char *p = data;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200135
Paul Bakker6083fd22011-12-03 21:45:14 +0000136 if( use_len > ENTROPY_BLOCK_SIZE )
137 {
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200138#if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
Paul Bakker9e36f042013-06-30 14:34:05 +0200139 sha512( data, len, tmp, 0 );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200140#else
141 sha256( data, len, tmp, 0 );
142#endif
Paul Bakker6083fd22011-12-03 21:45:14 +0000143 p = tmp;
144 use_len = ENTROPY_BLOCK_SIZE;
145 }
146
147 header[0] = source_id;
148 header[1] = use_len & 0xFF;
149
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200150#if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
Paul Bakker9e36f042013-06-30 14:34:05 +0200151 sha512_update( &ctx->accumulator, header, 2 );
152 sha512_update( &ctx->accumulator, p, use_len );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200153#else
154 sha256_update( &ctx->accumulator, header, 2 );
155 sha256_update( &ctx->accumulator, p, use_len );
156#endif
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200157
Paul Bakker6083fd22011-12-03 21:45:14 +0000158 return( 0 );
159}
160
161int entropy_update_manual( entropy_context *ctx,
162 const unsigned char *data, size_t len )
163{
Paul Bakker47703a02014-02-06 15:01:20 +0100164 int ret;
165
166#if defined(POLARSSL_THREADING_C)
167 if( ( ret = polarssl_mutex_lock( &ctx->mutex ) ) != 0 )
168 return( ret );
169#endif
170
171 ret = entropy_update( ctx, ENTROPY_SOURCE_MANUAL, data, len );
172
173#if defined(POLARSSL_THREADING_C)
174 if( polarssl_mutex_unlock( &ctx->mutex ) != 0 )
175 return( POLARSSL_ERR_THREADING_MUTEX_ERROR );
176#endif
177
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200178 return( ret );
Paul Bakker6083fd22011-12-03 21:45:14 +0000179}
180
181/*
182 * Run through the different sources to add entropy to our accumulator
183 */
Paul Bakker47703a02014-02-06 15:01:20 +0100184static int entropy_gather_internal( entropy_context *ctx )
Paul Bakker6083fd22011-12-03 21:45:14 +0000185{
186 int ret, i;
187 unsigned char buf[ENTROPY_MAX_GATHER];
188 size_t olen;
Paul Bakker47703a02014-02-06 15:01:20 +0100189
Paul Bakker43655f42011-12-15 20:11:16 +0000190 if( ctx->source_count == 0 )
191 return( POLARSSL_ERR_ENTROPY_NO_SOURCES_DEFINED );
192
Paul Bakker6083fd22011-12-03 21:45:14 +0000193 /*
194 * Run through our entropy sources
195 */
196 for( i = 0; i < ctx->source_count; i++ )
197 {
198 olen = 0;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000199 if ( ( ret = ctx->source[i].f_source( ctx->source[i].p_source,
Paul Bakker6083fd22011-12-03 21:45:14 +0000200 buf, ENTROPY_MAX_GATHER, &olen ) ) != 0 )
201 {
202 return( ret );
203 }
204
205 /*
206 * Add if we actually gathered something
207 */
208 if( olen > 0 )
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000209 {
Paul Bakker6083fd22011-12-03 21:45:14 +0000210 entropy_update( ctx, (unsigned char) i, buf, olen );
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000211 ctx->source[i].size += olen;
212 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000213 }
214
215 return( 0 );
216}
217
Paul Bakker47703a02014-02-06 15:01:20 +0100218/*
219 * Thread-safe wrapper for entropy_gather_internal()
220 */
221int entropy_gather( entropy_context *ctx )
222{
Paul Bakkerddd427a2014-04-09 14:47:58 +0200223 int ret;
Paul Bakker47703a02014-02-06 15:01:20 +0100224
225#if defined(POLARSSL_THREADING_C)
Paul Bakkerddd427a2014-04-09 14:47:58 +0200226 if( ( ret = polarssl_mutex_lock( &ctx->mutex ) ) != 0 )
227 return( ret );
Paul Bakker47703a02014-02-06 15:01:20 +0100228#endif
229
Paul Bakkerddd427a2014-04-09 14:47:58 +0200230 ret = entropy_gather_internal( ctx );
Paul Bakker47703a02014-02-06 15:01:20 +0100231
232#if defined(POLARSSL_THREADING_C)
Paul Bakkerddd427a2014-04-09 14:47:58 +0200233 if( polarssl_mutex_unlock( &ctx->mutex ) != 0 )
234 return( POLARSSL_ERR_THREADING_MUTEX_ERROR );
Paul Bakker47703a02014-02-06 15:01:20 +0100235#endif
236
Paul Bakkerddd427a2014-04-09 14:47:58 +0200237 return( ret );
Paul Bakker47703a02014-02-06 15:01:20 +0100238}
239
Paul Bakker6083fd22011-12-03 21:45:14 +0000240int entropy_func( void *data, unsigned char *output, size_t len )
241{
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000242 int ret, count = 0, i, reached;
Paul Bakker6083fd22011-12-03 21:45:14 +0000243 entropy_context *ctx = (entropy_context *) data;
244 unsigned char buf[ENTROPY_BLOCK_SIZE];
245
246 if( len > ENTROPY_BLOCK_SIZE )
247 return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED );
248
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200249#if defined(POLARSSL_THREADING_C)
250 if( ( ret = polarssl_mutex_lock( &ctx->mutex ) ) != 0 )
251 return( ret );
252#endif
253
Paul Bakker6083fd22011-12-03 21:45:14 +0000254 /*
255 * Always gather extra entropy before a call
256 */
257 do
258 {
259 if( count++ > ENTROPY_MAX_LOOP )
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200260 {
261 ret = POLARSSL_ERR_ENTROPY_SOURCE_FAILED;
262 goto exit;
263 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000264
Paul Bakker47703a02014-02-06 15:01:20 +0100265 if( ( ret = entropy_gather_internal( ctx ) ) != 0 )
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200266 goto exit;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000267
268 reached = 0;
269
270 for( i = 0; i < ctx->source_count; i++ )
271 if( ctx->source[i].size >= ctx->source[i].threshold )
272 reached++;
Paul Bakker6083fd22011-12-03 21:45:14 +0000273 }
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000274 while( reached != ctx->source_count );
Paul Bakker6083fd22011-12-03 21:45:14 +0000275
276 memset( buf, 0, ENTROPY_BLOCK_SIZE );
277
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200278#if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
Paul Bakker9e36f042013-06-30 14:34:05 +0200279 sha512_finish( &ctx->accumulator, buf );
280
Paul Bakker6083fd22011-12-03 21:45:14 +0000281 /*
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000282 * Reset accumulator and counters and recycle existing entropy
Paul Bakker6083fd22011-12-03 21:45:14 +0000283 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200284 memset( &ctx->accumulator, 0, sizeof( sha512_context ) );
285 sha512_starts( &ctx->accumulator, 0 );
286 sha512_update( &ctx->accumulator, buf, ENTROPY_BLOCK_SIZE );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200287
288 /*
Paul Bakkerb13d3ff2014-03-26 12:51:25 +0100289 * Perform second SHA-512 on entropy
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200290 */
Paul Bakkerb13d3ff2014-03-26 12:51:25 +0100291 sha512( buf, ENTROPY_BLOCK_SIZE, buf, 0 );
292#else /* POLARSSL_ENTROPY_SHA512_ACCUMULATOR */
293 sha256_finish( &ctx->accumulator, buf );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200294
295 /*
296 * Reset accumulator and counters and recycle existing entropy
297 */
298 memset( &ctx->accumulator, 0, sizeof( sha256_context ) );
299 sha256_starts( &ctx->accumulator, 0 );
300 sha256_update( &ctx->accumulator, buf, ENTROPY_BLOCK_SIZE );
Paul Bakkerb13d3ff2014-03-26 12:51:25 +0100301
302 /*
303 * Perform second SHA-256 on entropy
304 */
305 sha256( buf, ENTROPY_BLOCK_SIZE, buf, 0 );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200306#endif /* POLARSSL_ENTROPY_SHA512_ACCUMULATOR */
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000307
308 for( i = 0; i < ctx->source_count; i++ )
309 ctx->source[i].size = 0;
Paul Bakker6083fd22011-12-03 21:45:14 +0000310
311 memcpy( output, buf, len );
312
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200313 ret = 0;
314
315exit:
316#if defined(POLARSSL_THREADING_C)
317 if( polarssl_mutex_unlock( &ctx->mutex ) != 0 )
318 return( POLARSSL_ERR_THREADING_MUTEX_ERROR );
319#endif
320
321 return( ret );
Paul Bakker6083fd22011-12-03 21:45:14 +0000322}
323
Paul Bakker66ff70d2014-03-26 11:54:05 +0100324#if defined(POLARSSL_FS_IO)
325int entropy_write_seed_file( entropy_context *ctx, const char *path )
326{
327 int ret = POLARSSL_ERR_ENTROPY_FILE_IO_ERROR;
328 FILE *f;
329 unsigned char buf[ENTROPY_BLOCK_SIZE];
330
331 if( ( f = fopen( path, "wb" ) ) == NULL )
332 return( POLARSSL_ERR_ENTROPY_FILE_IO_ERROR );
333
334 if( ( ret = entropy_func( ctx, buf, ENTROPY_BLOCK_SIZE ) ) != 0 )
335 goto exit;
336
337 if( fwrite( buf, 1, ENTROPY_BLOCK_SIZE, f ) != ENTROPY_BLOCK_SIZE )
338 {
339 ret = POLARSSL_ERR_ENTROPY_FILE_IO_ERROR;
340 goto exit;
341 }
342
343 ret = 0;
344
345exit:
346 fclose( f );
347 return( ret );
348}
349
350int entropy_update_seed_file( entropy_context *ctx, const char *path )
351{
352 FILE *f;
353 size_t n;
354 unsigned char buf[ ENTROPY_MAX_SEED_SIZE ];
355
356 if( ( f = fopen( path, "rb" ) ) == NULL )
357 return( POLARSSL_ERR_ENTROPY_FILE_IO_ERROR );
358
359 fseek( f, 0, SEEK_END );
360 n = (size_t) ftell( f );
361 fseek( f, 0, SEEK_SET );
362
363 if( n > ENTROPY_MAX_SEED_SIZE )
364 n = ENTROPY_MAX_SEED_SIZE;
365
366 if( fread( buf, 1, n, f ) != n )
367 {
368 fclose( f );
369 return( POLARSSL_ERR_ENTROPY_FILE_IO_ERROR );
370 }
371
372 fclose( f );
373
374 entropy_update_manual( ctx, buf, n );
375
376 return( entropy_write_seed_file( ctx, path ) );
377}
378#endif /* POLARSSL_FS_IO */
379
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200380#if defined(POLARSSL_SELF_TEST)
381
382#if defined(POLARSSL_PLATFORM_C)
383#include "polarssl/platform.h"
384#else
385#define polarssl_printf printf
386#endif
387
388/*
389 * Dummy source function
390 */
391static int entropy_dummy_source( void *data, unsigned char *output,
392 size_t len, size_t *olen )
393{
394 ((void) data);
395
396 memset( output, 0x2a, len );
397 *olen = len;
398
399 return( 0 );
400}
401
402/*
403 * The actual entropy quality is hard to test, but we can at least
404 * test that the functions don't cause errors and write the correct
405 * amount of data to buffers.
406 */
407int entropy_self_test( int verbose )
408{
409 int ret = 0;
410 entropy_context ctx;
411 unsigned char buf[ENTROPY_BLOCK_SIZE] = { 0 };
412 unsigned char acc[ENTROPY_BLOCK_SIZE] = { 0 };
413 size_t i, j;
414
415 if( verbose != 0 )
416 polarssl_printf( " ENTROPY test: " );
417
418 entropy_init( &ctx );
419
420 ret = entropy_add_source( &ctx, entropy_dummy_source, NULL, 16 );
421 if( ret != 0 )
422 goto cleanup;
423
424 if( ( ret = entropy_gather( &ctx ) ) != 0 )
425 goto cleanup;
426
427 if( ( ret = entropy_update_manual( &ctx, buf, sizeof buf ) ) != 0 )
428 goto cleanup;
429
430 /*
431 * To test that entropy_func writes correct number of bytes:
432 * - use the whole buffer and rely on ASan to detect overruns
433 * - collect entropy 8 times and OR the result in an accumulator:
434 * any byte should then be 0 with probably 2^(-64), so requiring
435 * each of the 32 or 64 bytes to be non-zero has a false failure rate
436 * of at most 2^(-58) which is acceptable.
437 */
438 for( i = 0; i < 8; i++ )
439 {
440 if( ( ret = entropy_func( &ctx, buf, sizeof( buf ) ) ) != 0 )
441 goto cleanup;
442
443 for( j = 0; j < sizeof( buf ); j++ )
444 acc[j] |= buf[j];
445 }
446
447 for( j = 0; j < sizeof( buf ); j++ )
448 {
449 if( acc[j] == 0 )
450 {
451 ret = 1;
452 goto cleanup;
453 }
454 }
455
456cleanup:
457 entropy_free( &ctx );
458
459 if( verbose != 0 )
460 {
461 if( ret != 0 )
462 polarssl_printf( "failed\n" );
463 else
464 polarssl_printf( "passed\n" );
465
466 polarssl_printf( "\n" );
467 }
468
469 return( ret != 0 );
470}
471#endif /* POLARSSL_SELF_TEST */
472
Paul Bakker9af723c2014-05-01 13:03:14 +0200473#endif /* POLARSSL_ENTROPY_C */