blob: 7604e0f2705e984605cdba0c52e5fbce859dbf6b [file] [log] [blame]
Paul Bakker6083fd22011-12-03 21:45:14 +00001/*
2 * Entropy accumulator implementation
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakker6083fd22011-12-03 21:45:14 +00005 *
Manuel Pégourié-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakker6083fd22011-12-03 21:45:14 +00007 *
Paul Bakker6083fd22011-12-03 21:45:14 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker6083fd22011-12-03 21:45:14 +000024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000028
29#if defined(POLARSSL_ENTROPY_C)
30
31#include "polarssl/entropy.h"
32#include "polarssl/entropy_poll.h"
33
Paul Bakker66ff70d2014-03-26 11:54:05 +010034#if defined(POLARSSL_FS_IO)
35#include <stdio.h>
36#endif
37
Paul Bakker28c7e7f2011-12-15 19:49:30 +000038#if defined(POLARSSL_HAVEGE_C)
39#include "polarssl/havege.h"
40#endif
41
Paul Bakker34617722014-06-13 17:20:13 +020042/* Implementation that should never be optimized out by the compiler */
43static void polarssl_zeroize( void *v, size_t n ) {
44 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
45}
46
Paul Bakker6083fd22011-12-03 21:45:14 +000047#define ENTROPY_MAX_LOOP 256 /**< Maximum amount to loop before error */
48
49void entropy_init( entropy_context *ctx )
50{
51 memset( ctx, 0, sizeof(entropy_context) );
52
Paul Bakkerf4e7dc52013-09-28 15:23:57 +020053#if defined(POLARSSL_THREADING_C)
54 polarssl_mutex_init( &ctx->mutex );
55#endif
56
Paul Bakkerfb08fd22013-08-27 15:06:26 +020057#if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
Paul Bakker9e36f042013-06-30 14:34:05 +020058 sha512_starts( &ctx->accumulator, 0 );
Paul Bakkerfb08fd22013-08-27 15:06:26 +020059#else
60 sha256_starts( &ctx->accumulator, 0 );
61#endif
Paul Bakker43655f42011-12-15 20:11:16 +000062#if defined(POLARSSL_HAVEGE_C)
63 havege_init( &ctx->havege_data );
64#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000065
Paul Bakker43655f42011-12-15 20:11:16 +000066#if !defined(POLARSSL_NO_DEFAULT_ENTROPY_SOURCES)
Paul Bakker6083fd22011-12-03 21:45:14 +000067#if !defined(POLARSSL_NO_PLATFORM_ENTROPY)
Paul Bakkerbd4a9d02011-12-10 17:02:19 +000068 entropy_add_source( ctx, platform_entropy_poll, NULL,
69 ENTROPY_MIN_PLATFORM );
Paul Bakker6083fd22011-12-03 21:45:14 +000070#endif
71#if defined(POLARSSL_TIMING_C)
Paul Bakkerbd4a9d02011-12-10 17:02:19 +000072 entropy_add_source( ctx, hardclock_poll, NULL, ENTROPY_MIN_HARDCLOCK );
Paul Bakker6083fd22011-12-03 21:45:14 +000073#endif
Paul Bakker28c7e7f2011-12-15 19:49:30 +000074#if defined(POLARSSL_HAVEGE_C)
Paul Bakker28c7e7f2011-12-15 19:49:30 +000075 entropy_add_source( ctx, havege_poll, &ctx->havege_data,
76 ENTROPY_MIN_HAVEGE );
77#endif
Paul Bakker43655f42011-12-15 20:11:16 +000078#endif /* POLARSSL_NO_DEFAULT_ENTROPY_SOURCES */
Paul Bakker6083fd22011-12-03 21:45:14 +000079}
80
Paul Bakker1ffefac2013-09-28 15:23:03 +020081void entropy_free( entropy_context *ctx )
82{
Paul Bakkera317a982014-06-18 16:44:11 +020083#if defined(POLARSSL_HAVEGE_C)
84 havege_free( &ctx->havege_data );
85#endif
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 Bakker66d5d072014-06-17 16:39:18 +0200199 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
Paul Bakker5b11d022014-07-10 13:54:38 +0200385#include <stdio.h>
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200386#define polarssl_printf printf
387#endif
388
389/*
390 * Dummy source function
391 */
392static int entropy_dummy_source( void *data, unsigned char *output,
393 size_t len, size_t *olen )
394{
395 ((void) data);
396
397 memset( output, 0x2a, len );
398 *olen = len;
399
400 return( 0 );
401}
402
403/*
404 * The actual entropy quality is hard to test, but we can at least
405 * test that the functions don't cause errors and write the correct
406 * amount of data to buffers.
407 */
408int entropy_self_test( int verbose )
409{
410 int ret = 0;
411 entropy_context ctx;
412 unsigned char buf[ENTROPY_BLOCK_SIZE] = { 0 };
413 unsigned char acc[ENTROPY_BLOCK_SIZE] = { 0 };
414 size_t i, j;
415
416 if( verbose != 0 )
417 polarssl_printf( " ENTROPY test: " );
418
419 entropy_init( &ctx );
420
421 ret = entropy_add_source( &ctx, entropy_dummy_source, NULL, 16 );
422 if( ret != 0 )
423 goto cleanup;
424
425 if( ( ret = entropy_gather( &ctx ) ) != 0 )
426 goto cleanup;
427
428 if( ( ret = entropy_update_manual( &ctx, buf, sizeof buf ) ) != 0 )
429 goto cleanup;
430
431 /*
432 * To test that entropy_func writes correct number of bytes:
433 * - use the whole buffer and rely on ASan to detect overruns
434 * - collect entropy 8 times and OR the result in an accumulator:
435 * any byte should then be 0 with probably 2^(-64), so requiring
436 * each of the 32 or 64 bytes to be non-zero has a false failure rate
437 * of at most 2^(-58) which is acceptable.
438 */
439 for( i = 0; i < 8; i++ )
440 {
441 if( ( ret = entropy_func( &ctx, buf, sizeof( buf ) ) ) != 0 )
442 goto cleanup;
443
444 for( j = 0; j < sizeof( buf ); j++ )
445 acc[j] |= buf[j];
446 }
447
448 for( j = 0; j < sizeof( buf ); j++ )
449 {
450 if( acc[j] == 0 )
451 {
452 ret = 1;
453 goto cleanup;
454 }
455 }
456
457cleanup:
458 entropy_free( &ctx );
459
460 if( verbose != 0 )
461 {
462 if( ret != 0 )
463 polarssl_printf( "failed\n" );
464 else
465 polarssl_printf( "passed\n" );
466
467 polarssl_printf( "\n" );
468 }
469
470 return( ret != 0 );
471}
472#endif /* POLARSSL_SELF_TEST */
473
Paul Bakker9af723c2014-05-01 13:03:14 +0200474#endif /* POLARSSL_ENTROPY_C */