blob: 60a82080b3f33156f423255cf39b899d9229e7ad [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é-Gonnard967a2a52015-01-22 14:28:16 +00006 * This file is part of mbed TLS (http://www.polarssl.org)
Paul Bakker6083fd22011-12-03 21:45:14 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
Paul Bakker6083fd22011-12-03 21:45:14 +00009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker6083fd22011-12-03 21:45:14 +000025#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#else
27#include POLARSSL_CONFIG_FILE
28#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000029
30#if defined(POLARSSL_ENTROPY_C)
31
32#include "polarssl/entropy.h"
33#include "polarssl/entropy_poll.h"
34
Paul Bakker66ff70d2014-03-26 11:54:05 +010035#if defined(POLARSSL_FS_IO)
36#include <stdio.h>
37#endif
38
Paul Bakker28c7e7f2011-12-15 19:49:30 +000039#if defined(POLARSSL_HAVEGE_C)
40#include "polarssl/havege.h"
41#endif
42
Paul Bakker34617722014-06-13 17:20:13 +020043/* Implementation that should never be optimized out by the compiler */
44static void polarssl_zeroize( void *v, size_t n ) {
45 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
46}
47
Paul Bakker6083fd22011-12-03 21:45:14 +000048#define ENTROPY_MAX_LOOP 256 /**< Maximum amount to loop before error */
49
50void entropy_init( entropy_context *ctx )
51{
52 memset( ctx, 0, sizeof(entropy_context) );
53
Paul Bakkerf4e7dc52013-09-28 15:23:57 +020054#if defined(POLARSSL_THREADING_C)
55 polarssl_mutex_init( &ctx->mutex );
56#endif
57
Paul Bakkerfb08fd22013-08-27 15:06:26 +020058#if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
Paul Bakker9e36f042013-06-30 14:34:05 +020059 sha512_starts( &ctx->accumulator, 0 );
Paul Bakkerfb08fd22013-08-27 15:06:26 +020060#else
61 sha256_starts( &ctx->accumulator, 0 );
62#endif
Paul Bakker43655f42011-12-15 20:11:16 +000063#if defined(POLARSSL_HAVEGE_C)
64 havege_init( &ctx->havege_data );
65#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000066
Paul Bakker43655f42011-12-15 20:11:16 +000067#if !defined(POLARSSL_NO_DEFAULT_ENTROPY_SOURCES)
Paul Bakker6083fd22011-12-03 21:45:14 +000068#if !defined(POLARSSL_NO_PLATFORM_ENTROPY)
Paul Bakkerbd4a9d02011-12-10 17:02:19 +000069 entropy_add_source( ctx, platform_entropy_poll, NULL,
70 ENTROPY_MIN_PLATFORM );
Paul Bakker6083fd22011-12-03 21:45:14 +000071#endif
72#if defined(POLARSSL_TIMING_C)
Paul Bakkerbd4a9d02011-12-10 17:02:19 +000073 entropy_add_source( ctx, hardclock_poll, NULL, ENTROPY_MIN_HARDCLOCK );
Paul Bakker6083fd22011-12-03 21:45:14 +000074#endif
Paul Bakker28c7e7f2011-12-15 19:49:30 +000075#if defined(POLARSSL_HAVEGE_C)
Paul Bakker28c7e7f2011-12-15 19:49:30 +000076 entropy_add_source( ctx, havege_poll, &ctx->havege_data,
77 ENTROPY_MIN_HAVEGE );
78#endif
Paul Bakker43655f42011-12-15 20:11:16 +000079#endif /* POLARSSL_NO_DEFAULT_ENTROPY_SOURCES */
Paul Bakker6083fd22011-12-03 21:45:14 +000080}
81
Paul Bakker1ffefac2013-09-28 15:23:03 +020082void entropy_free( entropy_context *ctx )
83{
Paul Bakkera317a982014-06-18 16:44:11 +020084#if defined(POLARSSL_HAVEGE_C)
85 havege_free( &ctx->havege_data );
86#endif
Paul Bakker34617722014-06-13 17:20:13 +020087 polarssl_zeroize( ctx, sizeof( entropy_context ) );
Paul Bakkerf4e7dc52013-09-28 15:23:57 +020088#if defined(POLARSSL_THREADING_C)
89 polarssl_mutex_free( &ctx->mutex );
90#endif
Paul Bakker1ffefac2013-09-28 15:23:03 +020091}
92
Paul Bakker6083fd22011-12-03 21:45:14 +000093int entropy_add_source( entropy_context *ctx,
Paul Bakkerbd4a9d02011-12-10 17:02:19 +000094 f_source_ptr f_source, void *p_source,
95 size_t threshold )
Paul Bakker6083fd22011-12-03 21:45:14 +000096{
Paul Bakker47703a02014-02-06 15:01:20 +010097 int index, ret = 0;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +000098
Paul Bakker47703a02014-02-06 15:01:20 +010099#if defined(POLARSSL_THREADING_C)
100 if( ( ret = polarssl_mutex_lock( &ctx->mutex ) ) != 0 )
101 return( ret );
102#endif
103
104 index = ctx->source_count;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000105 if( index >= ENTROPY_MAX_SOURCES )
Paul Bakker47703a02014-02-06 15:01:20 +0100106 {
107 ret = POLARSSL_ERR_ENTROPY_MAX_SOURCES;
108 goto exit;
109 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000110
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000111 ctx->source[index].f_source = f_source;
112 ctx->source[index].p_source = p_source;
113 ctx->source[index].threshold = threshold;
Paul Bakker6083fd22011-12-03 21:45:14 +0000114
115 ctx->source_count++;
116
Paul Bakker47703a02014-02-06 15:01:20 +0100117exit:
118#if defined(POLARSSL_THREADING_C)
119 if( polarssl_mutex_unlock( &ctx->mutex ) != 0 )
120 return( POLARSSL_ERR_THREADING_MUTEX_ERROR );
121#endif
122
123 return( ret );
Paul Bakker6083fd22011-12-03 21:45:14 +0000124}
125
126/*
127 * Entropy accumulator update
128 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200129static int entropy_update( entropy_context *ctx, unsigned char source_id,
130 const unsigned char *data, size_t len )
Paul Bakker6083fd22011-12-03 21:45:14 +0000131{
132 unsigned char header[2];
133 unsigned char tmp[ENTROPY_BLOCK_SIZE];
134 size_t use_len = len;
135 const unsigned char *p = data;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200136
Paul Bakker6083fd22011-12-03 21:45:14 +0000137 if( use_len > ENTROPY_BLOCK_SIZE )
138 {
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200139#if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
Paul Bakker9e36f042013-06-30 14:34:05 +0200140 sha512( data, len, tmp, 0 );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200141#else
142 sha256( data, len, tmp, 0 );
143#endif
Paul Bakker6083fd22011-12-03 21:45:14 +0000144 p = tmp;
145 use_len = ENTROPY_BLOCK_SIZE;
146 }
147
148 header[0] = source_id;
149 header[1] = use_len & 0xFF;
150
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200151#if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
Paul Bakker9e36f042013-06-30 14:34:05 +0200152 sha512_update( &ctx->accumulator, header, 2 );
153 sha512_update( &ctx->accumulator, p, use_len );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200154#else
155 sha256_update( &ctx->accumulator, header, 2 );
156 sha256_update( &ctx->accumulator, p, use_len );
157#endif
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200158
Paul Bakker6083fd22011-12-03 21:45:14 +0000159 return( 0 );
160}
161
162int entropy_update_manual( entropy_context *ctx,
163 const unsigned char *data, size_t len )
164{
Paul Bakker47703a02014-02-06 15:01:20 +0100165 int ret;
166
167#if defined(POLARSSL_THREADING_C)
168 if( ( ret = polarssl_mutex_lock( &ctx->mutex ) ) != 0 )
169 return( ret );
170#endif
171
172 ret = entropy_update( ctx, ENTROPY_SOURCE_MANUAL, data, len );
173
174#if defined(POLARSSL_THREADING_C)
175 if( polarssl_mutex_unlock( &ctx->mutex ) != 0 )
176 return( POLARSSL_ERR_THREADING_MUTEX_ERROR );
177#endif
178
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200179 return( ret );
Paul Bakker6083fd22011-12-03 21:45:14 +0000180}
181
182/*
183 * Run through the different sources to add entropy to our accumulator
184 */
Paul Bakker47703a02014-02-06 15:01:20 +0100185static int entropy_gather_internal( entropy_context *ctx )
Paul Bakker6083fd22011-12-03 21:45:14 +0000186{
187 int ret, i;
188 unsigned char buf[ENTROPY_MAX_GATHER];
189 size_t olen;
Paul Bakker47703a02014-02-06 15:01:20 +0100190
Paul Bakker43655f42011-12-15 20:11:16 +0000191 if( ctx->source_count == 0 )
192 return( POLARSSL_ERR_ENTROPY_NO_SOURCES_DEFINED );
193
Paul Bakker6083fd22011-12-03 21:45:14 +0000194 /*
195 * Run through our entropy sources
196 */
197 for( i = 0; i < ctx->source_count; i++ )
198 {
199 olen = 0;
Paul Bakker66d5d072014-06-17 16:39:18 +0200200 if( ( ret = ctx->source[i].f_source( ctx->source[i].p_source,
Paul Bakker6083fd22011-12-03 21:45:14 +0000201 buf, ENTROPY_MAX_GATHER, &olen ) ) != 0 )
202 {
203 return( ret );
204 }
205
206 /*
207 * Add if we actually gathered something
208 */
209 if( olen > 0 )
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000210 {
Paul Bakker6083fd22011-12-03 21:45:14 +0000211 entropy_update( ctx, (unsigned char) i, buf, olen );
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000212 ctx->source[i].size += olen;
213 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000214 }
215
216 return( 0 );
217}
218
Paul Bakker47703a02014-02-06 15:01:20 +0100219/*
220 * Thread-safe wrapper for entropy_gather_internal()
221 */
222int entropy_gather( entropy_context *ctx )
223{
Paul Bakkerddd427a2014-04-09 14:47:58 +0200224 int ret;
Paul Bakker47703a02014-02-06 15:01:20 +0100225
226#if defined(POLARSSL_THREADING_C)
Paul Bakkerddd427a2014-04-09 14:47:58 +0200227 if( ( ret = polarssl_mutex_lock( &ctx->mutex ) ) != 0 )
228 return( ret );
Paul Bakker47703a02014-02-06 15:01:20 +0100229#endif
230
Paul Bakkerddd427a2014-04-09 14:47:58 +0200231 ret = entropy_gather_internal( ctx );
Paul Bakker47703a02014-02-06 15:01:20 +0100232
233#if defined(POLARSSL_THREADING_C)
Paul Bakkerddd427a2014-04-09 14:47:58 +0200234 if( polarssl_mutex_unlock( &ctx->mutex ) != 0 )
235 return( POLARSSL_ERR_THREADING_MUTEX_ERROR );
Paul Bakker47703a02014-02-06 15:01:20 +0100236#endif
237
Paul Bakkerddd427a2014-04-09 14:47:58 +0200238 return( ret );
Paul Bakker47703a02014-02-06 15:01:20 +0100239}
240
Paul Bakker6083fd22011-12-03 21:45:14 +0000241int entropy_func( void *data, unsigned char *output, size_t len )
242{
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000243 int ret, count = 0, i, reached;
Paul Bakker6083fd22011-12-03 21:45:14 +0000244 entropy_context *ctx = (entropy_context *) data;
245 unsigned char buf[ENTROPY_BLOCK_SIZE];
246
247 if( len > ENTROPY_BLOCK_SIZE )
248 return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED );
249
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200250#if defined(POLARSSL_THREADING_C)
251 if( ( ret = polarssl_mutex_lock( &ctx->mutex ) ) != 0 )
252 return( ret );
253#endif
254
Paul Bakker6083fd22011-12-03 21:45:14 +0000255 /*
256 * Always gather extra entropy before a call
257 */
258 do
259 {
260 if( count++ > ENTROPY_MAX_LOOP )
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200261 {
262 ret = POLARSSL_ERR_ENTROPY_SOURCE_FAILED;
263 goto exit;
264 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000265
Paul Bakker47703a02014-02-06 15:01:20 +0100266 if( ( ret = entropy_gather_internal( ctx ) ) != 0 )
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200267 goto exit;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000268
269 reached = 0;
270
271 for( i = 0; i < ctx->source_count; i++ )
272 if( ctx->source[i].size >= ctx->source[i].threshold )
273 reached++;
Paul Bakker6083fd22011-12-03 21:45:14 +0000274 }
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000275 while( reached != ctx->source_count );
Paul Bakker6083fd22011-12-03 21:45:14 +0000276
277 memset( buf, 0, ENTROPY_BLOCK_SIZE );
278
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200279#if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
Paul Bakker9e36f042013-06-30 14:34:05 +0200280 sha512_finish( &ctx->accumulator, buf );
281
Paul Bakker6083fd22011-12-03 21:45:14 +0000282 /*
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000283 * Reset accumulator and counters and recycle existing entropy
Paul Bakker6083fd22011-12-03 21:45:14 +0000284 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200285 memset( &ctx->accumulator, 0, sizeof( sha512_context ) );
286 sha512_starts( &ctx->accumulator, 0 );
287 sha512_update( &ctx->accumulator, buf, ENTROPY_BLOCK_SIZE );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200288
289 /*
Paul Bakkerb13d3ff2014-03-26 12:51:25 +0100290 * Perform second SHA-512 on entropy
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200291 */
Paul Bakkerb13d3ff2014-03-26 12:51:25 +0100292 sha512( buf, ENTROPY_BLOCK_SIZE, buf, 0 );
293#else /* POLARSSL_ENTROPY_SHA512_ACCUMULATOR */
294 sha256_finish( &ctx->accumulator, buf );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200295
296 /*
297 * Reset accumulator and counters and recycle existing entropy
298 */
299 memset( &ctx->accumulator, 0, sizeof( sha256_context ) );
300 sha256_starts( &ctx->accumulator, 0 );
301 sha256_update( &ctx->accumulator, buf, ENTROPY_BLOCK_SIZE );
Paul Bakkerb13d3ff2014-03-26 12:51:25 +0100302
303 /*
304 * Perform second SHA-256 on entropy
305 */
306 sha256( buf, ENTROPY_BLOCK_SIZE, buf, 0 );
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200307#endif /* POLARSSL_ENTROPY_SHA512_ACCUMULATOR */
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000308
309 for( i = 0; i < ctx->source_count; i++ )
310 ctx->source[i].size = 0;
Paul Bakker6083fd22011-12-03 21:45:14 +0000311
312 memcpy( output, buf, len );
313
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200314 ret = 0;
315
316exit:
317#if defined(POLARSSL_THREADING_C)
318 if( polarssl_mutex_unlock( &ctx->mutex ) != 0 )
319 return( POLARSSL_ERR_THREADING_MUTEX_ERROR );
320#endif
321
322 return( ret );
Paul Bakker6083fd22011-12-03 21:45:14 +0000323}
324
Paul Bakker66ff70d2014-03-26 11:54:05 +0100325#if defined(POLARSSL_FS_IO)
326int entropy_write_seed_file( entropy_context *ctx, const char *path )
327{
328 int ret = POLARSSL_ERR_ENTROPY_FILE_IO_ERROR;
329 FILE *f;
330 unsigned char buf[ENTROPY_BLOCK_SIZE];
331
332 if( ( f = fopen( path, "wb" ) ) == NULL )
333 return( POLARSSL_ERR_ENTROPY_FILE_IO_ERROR );
334
335 if( ( ret = entropy_func( ctx, buf, ENTROPY_BLOCK_SIZE ) ) != 0 )
336 goto exit;
337
338 if( fwrite( buf, 1, ENTROPY_BLOCK_SIZE, f ) != ENTROPY_BLOCK_SIZE )
339 {
340 ret = POLARSSL_ERR_ENTROPY_FILE_IO_ERROR;
341 goto exit;
342 }
343
344 ret = 0;
345
346exit:
347 fclose( f );
348 return( ret );
349}
350
351int entropy_update_seed_file( entropy_context *ctx, const char *path )
352{
353 FILE *f;
354 size_t n;
355 unsigned char buf[ ENTROPY_MAX_SEED_SIZE ];
356
357 if( ( f = fopen( path, "rb" ) ) == NULL )
358 return( POLARSSL_ERR_ENTROPY_FILE_IO_ERROR );
359
360 fseek( f, 0, SEEK_END );
361 n = (size_t) ftell( f );
362 fseek( f, 0, SEEK_SET );
363
364 if( n > ENTROPY_MAX_SEED_SIZE )
365 n = ENTROPY_MAX_SEED_SIZE;
366
367 if( fread( buf, 1, n, f ) != n )
368 {
369 fclose( f );
370 return( POLARSSL_ERR_ENTROPY_FILE_IO_ERROR );
371 }
372
373 fclose( f );
374
375 entropy_update_manual( ctx, buf, n );
376
377 return( entropy_write_seed_file( ctx, path ) );
378}
379#endif /* POLARSSL_FS_IO */
380
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200381#if defined(POLARSSL_SELF_TEST)
382
383#if defined(POLARSSL_PLATFORM_C)
384#include "polarssl/platform.h"
385#else
Paul Bakker5b11d022014-07-10 13:54:38 +0200386#include <stdio.h>
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200387#define polarssl_printf printf
388#endif
389
390/*
391 * Dummy source function
392 */
393static int entropy_dummy_source( void *data, unsigned char *output,
394 size_t len, size_t *olen )
395{
396 ((void) data);
397
398 memset( output, 0x2a, len );
399 *olen = len;
400
401 return( 0 );
402}
403
404/*
405 * The actual entropy quality is hard to test, but we can at least
406 * test that the functions don't cause errors and write the correct
407 * amount of data to buffers.
408 */
409int entropy_self_test( int verbose )
410{
411 int ret = 0;
412 entropy_context ctx;
413 unsigned char buf[ENTROPY_BLOCK_SIZE] = { 0 };
414 unsigned char acc[ENTROPY_BLOCK_SIZE] = { 0 };
415 size_t i, j;
416
417 if( verbose != 0 )
418 polarssl_printf( " ENTROPY test: " );
419
420 entropy_init( &ctx );
421
422 ret = entropy_add_source( &ctx, entropy_dummy_source, NULL, 16 );
423 if( ret != 0 )
424 goto cleanup;
425
426 if( ( ret = entropy_gather( &ctx ) ) != 0 )
427 goto cleanup;
428
429 if( ( ret = entropy_update_manual( &ctx, buf, sizeof buf ) ) != 0 )
430 goto cleanup;
431
432 /*
433 * To test that entropy_func writes correct number of bytes:
434 * - use the whole buffer and rely on ASan to detect overruns
435 * - collect entropy 8 times and OR the result in an accumulator:
436 * any byte should then be 0 with probably 2^(-64), so requiring
437 * each of the 32 or 64 bytes to be non-zero has a false failure rate
438 * of at most 2^(-58) which is acceptable.
439 */
440 for( i = 0; i < 8; i++ )
441 {
442 if( ( ret = entropy_func( &ctx, buf, sizeof( buf ) ) ) != 0 )
443 goto cleanup;
444
445 for( j = 0; j < sizeof( buf ); j++ )
446 acc[j] |= buf[j];
447 }
448
449 for( j = 0; j < sizeof( buf ); j++ )
450 {
451 if( acc[j] == 0 )
452 {
453 ret = 1;
454 goto cleanup;
455 }
456 }
457
458cleanup:
459 entropy_free( &ctx );
460
461 if( verbose != 0 )
462 {
463 if( ret != 0 )
464 polarssl_printf( "failed\n" );
465 else
466 polarssl_printf( "passed\n" );
467
468 polarssl_printf( "\n" );
469 }
470
471 return( ret != 0 );
472}
473#endif /* POLARSSL_SELF_TEST */
474
Paul Bakker9af723c2014-05-01 13:03:14 +0200475#endif /* POLARSSL_ENTROPY_C */