blob: bc7fb0fe461113f0fc69127e82cdee34f4b95550 [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 Bakkera317a982014-06-18 16:44:11 +020086#if defined(POLARSSL_HAVEGE_C)
87 havege_free( &ctx->havege_data );
88#endif
Paul Bakker34617722014-06-13 17:20:13 +020089 polarssl_zeroize( ctx, sizeof( entropy_context ) );
Paul Bakkerf4e7dc52013-09-28 15:23:57 +020090#if defined(POLARSSL_THREADING_C)
91 polarssl_mutex_free( &ctx->mutex );
92#endif
Paul Bakker1ffefac2013-09-28 15:23:03 +020093}
94
Paul Bakker6083fd22011-12-03 21:45:14 +000095int entropy_add_source( entropy_context *ctx,
Paul Bakkerbd4a9d02011-12-10 17:02:19 +000096 f_source_ptr f_source, void *p_source,
97 size_t threshold )
Paul Bakker6083fd22011-12-03 21:45:14 +000098{
Paul Bakker47703a02014-02-06 15:01:20 +010099 int index, ret = 0;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000100
Paul Bakker47703a02014-02-06 15:01:20 +0100101#if defined(POLARSSL_THREADING_C)
102 if( ( ret = polarssl_mutex_lock( &ctx->mutex ) ) != 0 )
103 return( ret );
104#endif
105
106 index = ctx->source_count;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000107 if( index >= ENTROPY_MAX_SOURCES )
Paul Bakker47703a02014-02-06 15:01:20 +0100108 {
109 ret = POLARSSL_ERR_ENTROPY_MAX_SOURCES;
110 goto exit;
111 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000112
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000113 ctx->source[index].f_source = f_source;
114 ctx->source[index].p_source = p_source;
115 ctx->source[index].threshold = threshold;
Paul Bakker6083fd22011-12-03 21:45:14 +0000116
117 ctx->source_count++;
118
Paul Bakker47703a02014-02-06 15:01:20 +0100119exit:
120#if defined(POLARSSL_THREADING_C)
121 if( polarssl_mutex_unlock( &ctx->mutex ) != 0 )
122 return( POLARSSL_ERR_THREADING_MUTEX_ERROR );
123#endif
124
125 return( ret );
Paul Bakker6083fd22011-12-03 21:45:14 +0000126}
127
128/*
129 * Entropy accumulator update
130 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200131static int entropy_update( entropy_context *ctx, unsigned char source_id,
132 const unsigned char *data, size_t len )
Paul Bakker6083fd22011-12-03 21:45:14 +0000133{
134 unsigned char header[2];
135 unsigned char tmp[ENTROPY_BLOCK_SIZE];
136 size_t use_len = len;
137 const unsigned char *p = data;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200138
Paul Bakker6083fd22011-12-03 21:45:14 +0000139 if( use_len > ENTROPY_BLOCK_SIZE )
140 {
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200141#if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
Paul Bakker9e36f042013-06-30 14:34:05 +0200142 sha512( data, len, tmp, 0 );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200143#else
144 sha256( data, len, tmp, 0 );
145#endif
Paul Bakker6083fd22011-12-03 21:45:14 +0000146 p = tmp;
147 use_len = ENTROPY_BLOCK_SIZE;
148 }
149
150 header[0] = source_id;
151 header[1] = use_len & 0xFF;
152
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200153#if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
Paul Bakker9e36f042013-06-30 14:34:05 +0200154 sha512_update( &ctx->accumulator, header, 2 );
155 sha512_update( &ctx->accumulator, p, use_len );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200156#else
157 sha256_update( &ctx->accumulator, header, 2 );
158 sha256_update( &ctx->accumulator, p, use_len );
159#endif
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200160
Paul Bakker6083fd22011-12-03 21:45:14 +0000161 return( 0 );
162}
163
164int entropy_update_manual( entropy_context *ctx,
165 const unsigned char *data, size_t len )
166{
Paul Bakker47703a02014-02-06 15:01:20 +0100167 int ret;
168
169#if defined(POLARSSL_THREADING_C)
170 if( ( ret = polarssl_mutex_lock( &ctx->mutex ) ) != 0 )
171 return( ret );
172#endif
173
174 ret = entropy_update( ctx, ENTROPY_SOURCE_MANUAL, data, len );
175
176#if defined(POLARSSL_THREADING_C)
177 if( polarssl_mutex_unlock( &ctx->mutex ) != 0 )
178 return( POLARSSL_ERR_THREADING_MUTEX_ERROR );
179#endif
180
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200181 return( ret );
Paul Bakker6083fd22011-12-03 21:45:14 +0000182}
183
184/*
185 * Run through the different sources to add entropy to our accumulator
186 */
Paul Bakker47703a02014-02-06 15:01:20 +0100187static int entropy_gather_internal( entropy_context *ctx )
Paul Bakker6083fd22011-12-03 21:45:14 +0000188{
189 int ret, i;
190 unsigned char buf[ENTROPY_MAX_GATHER];
191 size_t olen;
Paul Bakker47703a02014-02-06 15:01:20 +0100192
Paul Bakker43655f42011-12-15 20:11:16 +0000193 if( ctx->source_count == 0 )
194 return( POLARSSL_ERR_ENTROPY_NO_SOURCES_DEFINED );
195
Paul Bakker6083fd22011-12-03 21:45:14 +0000196 /*
197 * Run through our entropy sources
198 */
199 for( i = 0; i < ctx->source_count; i++ )
200 {
201 olen = 0;
Paul Bakker66d5d072014-06-17 16:39:18 +0200202 if( ( ret = ctx->source[i].f_source( ctx->source[i].p_source,
Paul Bakker6083fd22011-12-03 21:45:14 +0000203 buf, ENTROPY_MAX_GATHER, &olen ) ) != 0 )
204 {
205 return( ret );
206 }
207
208 /*
209 * Add if we actually gathered something
210 */
211 if( olen > 0 )
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000212 {
Paul Bakker6083fd22011-12-03 21:45:14 +0000213 entropy_update( ctx, (unsigned char) i, buf, olen );
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000214 ctx->source[i].size += olen;
215 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000216 }
217
218 return( 0 );
219}
220
Paul Bakker47703a02014-02-06 15:01:20 +0100221/*
222 * Thread-safe wrapper for entropy_gather_internal()
223 */
224int entropy_gather( entropy_context *ctx )
225{
Paul Bakkerddd427a2014-04-09 14:47:58 +0200226 int ret;
Paul Bakker47703a02014-02-06 15:01:20 +0100227
228#if defined(POLARSSL_THREADING_C)
Paul Bakkerddd427a2014-04-09 14:47:58 +0200229 if( ( ret = polarssl_mutex_lock( &ctx->mutex ) ) != 0 )
230 return( ret );
Paul Bakker47703a02014-02-06 15:01:20 +0100231#endif
232
Paul Bakkerddd427a2014-04-09 14:47:58 +0200233 ret = entropy_gather_internal( ctx );
Paul Bakker47703a02014-02-06 15:01:20 +0100234
235#if defined(POLARSSL_THREADING_C)
Paul Bakkerddd427a2014-04-09 14:47:58 +0200236 if( polarssl_mutex_unlock( &ctx->mutex ) != 0 )
237 return( POLARSSL_ERR_THREADING_MUTEX_ERROR );
Paul Bakker47703a02014-02-06 15:01:20 +0100238#endif
239
Paul Bakkerddd427a2014-04-09 14:47:58 +0200240 return( ret );
Paul Bakker47703a02014-02-06 15:01:20 +0100241}
242
Paul Bakker6083fd22011-12-03 21:45:14 +0000243int entropy_func( void *data, unsigned char *output, size_t len )
244{
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000245 int ret, count = 0, i, reached;
Paul Bakker6083fd22011-12-03 21:45:14 +0000246 entropy_context *ctx = (entropy_context *) data;
247 unsigned char buf[ENTROPY_BLOCK_SIZE];
248
249 if( len > ENTROPY_BLOCK_SIZE )
250 return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED );
251
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200252#if defined(POLARSSL_THREADING_C)
253 if( ( ret = polarssl_mutex_lock( &ctx->mutex ) ) != 0 )
254 return( ret );
255#endif
256
Paul Bakker6083fd22011-12-03 21:45:14 +0000257 /*
258 * Always gather extra entropy before a call
259 */
260 do
261 {
262 if( count++ > ENTROPY_MAX_LOOP )
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200263 {
264 ret = POLARSSL_ERR_ENTROPY_SOURCE_FAILED;
265 goto exit;
266 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000267
Paul Bakker47703a02014-02-06 15:01:20 +0100268 if( ( ret = entropy_gather_internal( ctx ) ) != 0 )
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200269 goto exit;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000270
271 reached = 0;
272
273 for( i = 0; i < ctx->source_count; i++ )
274 if( ctx->source[i].size >= ctx->source[i].threshold )
275 reached++;
Paul Bakker6083fd22011-12-03 21:45:14 +0000276 }
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000277 while( reached != ctx->source_count );
Paul Bakker6083fd22011-12-03 21:45:14 +0000278
279 memset( buf, 0, ENTROPY_BLOCK_SIZE );
280
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200281#if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
Paul Bakker9e36f042013-06-30 14:34:05 +0200282 sha512_finish( &ctx->accumulator, buf );
283
Paul Bakker6083fd22011-12-03 21:45:14 +0000284 /*
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000285 * Reset accumulator and counters and recycle existing entropy
Paul Bakker6083fd22011-12-03 21:45:14 +0000286 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200287 memset( &ctx->accumulator, 0, sizeof( sha512_context ) );
288 sha512_starts( &ctx->accumulator, 0 );
289 sha512_update( &ctx->accumulator, buf, ENTROPY_BLOCK_SIZE );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200290
291 /*
Paul Bakkerb13d3ff2014-03-26 12:51:25 +0100292 * Perform second SHA-512 on entropy
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200293 */
Paul Bakkerb13d3ff2014-03-26 12:51:25 +0100294 sha512( buf, ENTROPY_BLOCK_SIZE, buf, 0 );
295#else /* POLARSSL_ENTROPY_SHA512_ACCUMULATOR */
296 sha256_finish( &ctx->accumulator, buf );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200297
298 /*
299 * Reset accumulator and counters and recycle existing entropy
300 */
301 memset( &ctx->accumulator, 0, sizeof( sha256_context ) );
302 sha256_starts( &ctx->accumulator, 0 );
303 sha256_update( &ctx->accumulator, buf, ENTROPY_BLOCK_SIZE );
Paul Bakkerb13d3ff2014-03-26 12:51:25 +0100304
305 /*
306 * Perform second SHA-256 on entropy
307 */
308 sha256( buf, ENTROPY_BLOCK_SIZE, buf, 0 );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200309#endif /* POLARSSL_ENTROPY_SHA512_ACCUMULATOR */
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000310
311 for( i = 0; i < ctx->source_count; i++ )
312 ctx->source[i].size = 0;
Paul Bakker6083fd22011-12-03 21:45:14 +0000313
314 memcpy( output, buf, len );
315
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200316 ret = 0;
317
318exit:
319#if defined(POLARSSL_THREADING_C)
320 if( polarssl_mutex_unlock( &ctx->mutex ) != 0 )
321 return( POLARSSL_ERR_THREADING_MUTEX_ERROR );
322#endif
323
324 return( ret );
Paul Bakker6083fd22011-12-03 21:45:14 +0000325}
326
Paul Bakker66ff70d2014-03-26 11:54:05 +0100327#if defined(POLARSSL_FS_IO)
328int entropy_write_seed_file( entropy_context *ctx, const char *path )
329{
330 int ret = POLARSSL_ERR_ENTROPY_FILE_IO_ERROR;
331 FILE *f;
332 unsigned char buf[ENTROPY_BLOCK_SIZE];
333
334 if( ( f = fopen( path, "wb" ) ) == NULL )
335 return( POLARSSL_ERR_ENTROPY_FILE_IO_ERROR );
336
337 if( ( ret = entropy_func( ctx, buf, ENTROPY_BLOCK_SIZE ) ) != 0 )
338 goto exit;
339
340 if( fwrite( buf, 1, ENTROPY_BLOCK_SIZE, f ) != ENTROPY_BLOCK_SIZE )
341 {
342 ret = POLARSSL_ERR_ENTROPY_FILE_IO_ERROR;
343 goto exit;
344 }
345
346 ret = 0;
347
348exit:
349 fclose( f );
350 return( ret );
351}
352
353int entropy_update_seed_file( entropy_context *ctx, const char *path )
354{
355 FILE *f;
356 size_t n;
357 unsigned char buf[ ENTROPY_MAX_SEED_SIZE ];
358
359 if( ( f = fopen( path, "rb" ) ) == NULL )
360 return( POLARSSL_ERR_ENTROPY_FILE_IO_ERROR );
361
362 fseek( f, 0, SEEK_END );
363 n = (size_t) ftell( f );
364 fseek( f, 0, SEEK_SET );
365
366 if( n > ENTROPY_MAX_SEED_SIZE )
367 n = ENTROPY_MAX_SEED_SIZE;
368
369 if( fread( buf, 1, n, f ) != n )
370 {
371 fclose( f );
372 return( POLARSSL_ERR_ENTROPY_FILE_IO_ERROR );
373 }
374
375 fclose( f );
376
377 entropy_update_manual( ctx, buf, n );
378
379 return( entropy_write_seed_file( ctx, path ) );
380}
381#endif /* POLARSSL_FS_IO */
382
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200383#if defined(POLARSSL_SELF_TEST)
384
385#if defined(POLARSSL_PLATFORM_C)
386#include "polarssl/platform.h"
387#else
Paul Bakker5b11d022014-07-10 13:54:38 +0200388#include <stdio.h>
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200389#define polarssl_printf printf
390#endif
391
392/*
393 * Dummy source function
394 */
395static int entropy_dummy_source( void *data, unsigned char *output,
396 size_t len, size_t *olen )
397{
398 ((void) data);
399
400 memset( output, 0x2a, len );
401 *olen = len;
402
403 return( 0 );
404}
405
406/*
407 * The actual entropy quality is hard to test, but we can at least
408 * test that the functions don't cause errors and write the correct
409 * amount of data to buffers.
410 */
411int entropy_self_test( int verbose )
412{
413 int ret = 0;
414 entropy_context ctx;
415 unsigned char buf[ENTROPY_BLOCK_SIZE] = { 0 };
416 unsigned char acc[ENTROPY_BLOCK_SIZE] = { 0 };
417 size_t i, j;
418
419 if( verbose != 0 )
420 polarssl_printf( " ENTROPY test: " );
421
422 entropy_init( &ctx );
423
424 ret = entropy_add_source( &ctx, entropy_dummy_source, NULL, 16 );
425 if( ret != 0 )
426 goto cleanup;
427
428 if( ( ret = entropy_gather( &ctx ) ) != 0 )
429 goto cleanup;
430
431 if( ( ret = entropy_update_manual( &ctx, buf, sizeof buf ) ) != 0 )
432 goto cleanup;
433
434 /*
435 * To test that entropy_func writes correct number of bytes:
436 * - use the whole buffer and rely on ASan to detect overruns
437 * - collect entropy 8 times and OR the result in an accumulator:
438 * any byte should then be 0 with probably 2^(-64), so requiring
439 * each of the 32 or 64 bytes to be non-zero has a false failure rate
440 * of at most 2^(-58) which is acceptable.
441 */
442 for( i = 0; i < 8; i++ )
443 {
444 if( ( ret = entropy_func( &ctx, buf, sizeof( buf ) ) ) != 0 )
445 goto cleanup;
446
447 for( j = 0; j < sizeof( buf ); j++ )
448 acc[j] |= buf[j];
449 }
450
451 for( j = 0; j < sizeof( buf ); j++ )
452 {
453 if( acc[j] == 0 )
454 {
455 ret = 1;
456 goto cleanup;
457 }
458 }
459
460cleanup:
461 entropy_free( &ctx );
462
463 if( verbose != 0 )
464 {
465 if( ret != 0 )
466 polarssl_printf( "failed\n" );
467 else
468 polarssl_printf( "passed\n" );
469
470 polarssl_printf( "\n" );
471 }
472
473 return( ret != 0 );
474}
475#endif /* POLARSSL_SELF_TEST */
476
Paul Bakker9af723c2014-05-01 13:03:14 +0200477#endif /* POLARSSL_ENTROPY_C */