2 * Entropy accumulator implementation
4 * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * This file is part of mbed TLS (https://tls.mbed.org)
22 #if !defined(MBEDTLS_CONFIG_FILE)
23 #include "mbedtls/config.h"
25 #include MBEDTLS_CONFIG_FILE
28 #if defined(MBEDTLS_ENTROPY_C)
30 #if defined(MBEDTLS_TEST_NULL_ENTROPY)
31 #warning "**** WARNING! MBEDTLS_TEST_NULL_ENTROPY defined! "
32 #warning "**** THIS BUILD HAS NO DEFINED ENTROPY SOURCES "
33 #warning "**** THIS BUILD IS *NOT* SUITABLE FOR PRODUCTION USE "
36 #include "mbedtls/entropy.h"
37 #include "mbedtls/entropy_poll.h"
38 #include "mbedtls/platform_util.h"
42 #if defined(MBEDTLS_FS_IO)
46 #if defined(MBEDTLS_ENTROPY_NV_SEED)
47 #include "mbedtls/platform.h"
50 #if defined(MBEDTLS_SELF_TEST)
51 #if defined(MBEDTLS_PLATFORM_C)
52 #include "mbedtls/platform.h"
55 #define mbedtls_printf printf
56 #endif /* MBEDTLS_PLATFORM_C */
57 #endif /* MBEDTLS_SELF_TEST */
59 #if defined(MBEDTLS_HAVEGE_C)
60 #include "mbedtls/havege.h"
63 #define ENTROPY_MAX_LOOP 256 /**< Maximum amount to loop before error */
65 void mbedtls_entropy_init( mbedtls_entropy_context *ctx )
67 ctx->source_count = 0;
68 memset( ctx->source, 0, sizeof( ctx->source ) );
70 #if defined(MBEDTLS_THREADING_C)
71 mbedtls_mutex_init( &ctx->mutex );
74 ctx->accumulator_started = 0;
75 #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
76 mbedtls_sha512_init( &ctx->accumulator );
78 mbedtls_sha256_init( &ctx->accumulator );
80 #if defined(MBEDTLS_HAVEGE_C)
81 mbedtls_havege_init( &ctx->havege_data );
84 /* Reminder: Update ENTROPY_HAVE_STRONG in the test files
85 * when adding more strong entropy sources here. */
87 #if defined(MBEDTLS_TEST_NULL_ENTROPY)
88 mbedtls_entropy_add_source( ctx, mbedtls_null_entropy_poll, NULL,
89 1, MBEDTLS_ENTROPY_SOURCE_STRONG );
92 #if !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
93 #if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
94 mbedtls_entropy_add_source( ctx, mbedtls_platform_entropy_poll, NULL,
95 MBEDTLS_ENTROPY_MIN_PLATFORM,
96 MBEDTLS_ENTROPY_SOURCE_STRONG );
98 #if defined(MBEDTLS_TIMING_C)
99 mbedtls_entropy_add_source( ctx, mbedtls_hardclock_poll, NULL,
100 MBEDTLS_ENTROPY_MIN_HARDCLOCK,
101 MBEDTLS_ENTROPY_SOURCE_WEAK );
103 #if defined(MBEDTLS_HAVEGE_C)
104 mbedtls_entropy_add_source( ctx, mbedtls_havege_poll, &ctx->havege_data,
105 MBEDTLS_ENTROPY_MIN_HAVEGE,
106 MBEDTLS_ENTROPY_SOURCE_STRONG );
108 #if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
109 mbedtls_entropy_add_source( ctx, mbedtls_hardware_poll, NULL,
110 MBEDTLS_ENTROPY_MIN_HARDWARE,
111 MBEDTLS_ENTROPY_SOURCE_STRONG );
113 #if defined(MBEDTLS_ENTROPY_NV_SEED)
114 mbedtls_entropy_add_source( ctx, mbedtls_nv_seed_poll, NULL,
115 MBEDTLS_ENTROPY_BLOCK_SIZE,
116 MBEDTLS_ENTROPY_SOURCE_STRONG );
117 ctx->initial_entropy_run = 0;
119 #endif /* MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES */
122 void mbedtls_entropy_free( mbedtls_entropy_context *ctx )
124 #if defined(MBEDTLS_HAVEGE_C)
125 mbedtls_havege_free( &ctx->havege_data );
127 #if defined(MBEDTLS_THREADING_C)
128 mbedtls_mutex_free( &ctx->mutex );
130 #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
131 mbedtls_sha512_free( &ctx->accumulator );
133 mbedtls_sha256_free( &ctx->accumulator );
135 #if defined(MBEDTLS_ENTROPY_NV_SEED)
136 ctx->initial_entropy_run = 0;
138 ctx->source_count = 0;
139 mbedtls_platform_zeroize( ctx->source, sizeof( ctx->source ) );
140 ctx->accumulator_started = 0;
143 int mbedtls_entropy_add_source( mbedtls_entropy_context *ctx,
144 mbedtls_entropy_f_source_ptr f_source, void *p_source,
145 size_t threshold, int strong )
149 #if defined(MBEDTLS_THREADING_C)
150 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
154 idx = ctx->source_count;
155 if( idx >= MBEDTLS_ENTROPY_MAX_SOURCES )
157 ret = MBEDTLS_ERR_ENTROPY_MAX_SOURCES;
161 ctx->source[idx].f_source = f_source;
162 ctx->source[idx].p_source = p_source;
163 ctx->source[idx].threshold = threshold;
164 ctx->source[idx].strong = strong;
169 #if defined(MBEDTLS_THREADING_C)
170 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
171 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
178 * Entropy accumulator update
180 static int entropy_update( mbedtls_entropy_context *ctx, unsigned char source_id,
181 const unsigned char *data, size_t len )
183 unsigned char header[2];
184 unsigned char tmp[MBEDTLS_ENTROPY_BLOCK_SIZE];
185 size_t use_len = len;
186 const unsigned char *p = data;
189 if( use_len > MBEDTLS_ENTROPY_BLOCK_SIZE )
191 #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
192 if( ( ret = mbedtls_sha512_ret( data, len, tmp, 0 ) ) != 0 )
195 if( ( ret = mbedtls_sha256_ret( data, len, tmp, 0 ) ) != 0 )
199 use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
202 header[0] = source_id;
203 header[1] = use_len & 0xFF;
206 * Start the accumulator if this has not already happened. Note that
207 * it is sufficient to start the accumulator here only because all calls to
208 * gather entropy eventually execute this code.
210 #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
211 if( ctx->accumulator_started == 0 &&
212 ( ret = mbedtls_sha512_starts_ret( &ctx->accumulator, 0 ) ) != 0 )
215 ctx->accumulator_started = 1;
216 if( ( ret = mbedtls_sha512_update_ret( &ctx->accumulator, header, 2 ) ) != 0 )
218 ret = mbedtls_sha512_update_ret( &ctx->accumulator, p, use_len );
220 if( ctx->accumulator_started == 0 &&
221 ( ret = mbedtls_sha256_starts_ret( &ctx->accumulator, 0 ) ) != 0 )
224 ctx->accumulator_started = 1;
225 if( ( ret = mbedtls_sha256_update_ret( &ctx->accumulator, header, 2 ) ) != 0 )
227 ret = mbedtls_sha256_update_ret( &ctx->accumulator, p, use_len );
231 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
236 int mbedtls_entropy_update_manual( mbedtls_entropy_context *ctx,
237 const unsigned char *data, size_t len )
241 #if defined(MBEDTLS_THREADING_C)
242 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
246 ret = entropy_update( ctx, MBEDTLS_ENTROPY_SOURCE_MANUAL, data, len );
248 #if defined(MBEDTLS_THREADING_C)
249 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
250 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
257 * Run through the different sources to add entropy to our accumulator
259 static int entropy_gather_internal( mbedtls_entropy_context *ctx )
261 int ret, i, have_one_strong = 0;
262 unsigned char buf[MBEDTLS_ENTROPY_MAX_GATHER];
265 if( ctx->source_count == 0 )
266 return( MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED );
269 * Run through our entropy sources
271 for( i = 0; i < ctx->source_count; i++ )
273 if( ctx->source[i].strong == MBEDTLS_ENTROPY_SOURCE_STRONG )
277 if( ( ret = ctx->source[i].f_source( ctx->source[i].p_source,
278 buf, MBEDTLS_ENTROPY_MAX_GATHER, &olen ) ) != 0 )
284 * Add if we actually gathered something
288 if( ( ret = entropy_update( ctx, (unsigned char) i,
291 ctx->source[i].size += olen;
295 if( have_one_strong == 0 )
296 ret = MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE;
299 mbedtls_platform_zeroize( buf, sizeof( buf ) );
305 * Thread-safe wrapper for entropy_gather_internal()
307 int mbedtls_entropy_gather( mbedtls_entropy_context *ctx )
311 #if defined(MBEDTLS_THREADING_C)
312 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
316 ret = entropy_gather_internal( ctx );
318 #if defined(MBEDTLS_THREADING_C)
319 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
320 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
326 int mbedtls_entropy_func( void *data, unsigned char *output, size_t len )
328 int ret, count = 0, i, done;
329 mbedtls_entropy_context *ctx = (mbedtls_entropy_context *) data;
330 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
332 if( len > MBEDTLS_ENTROPY_BLOCK_SIZE )
333 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
335 #if defined(MBEDTLS_ENTROPY_NV_SEED)
336 /* Update the NV entropy seed before generating any entropy for outside
339 if( ctx->initial_entropy_run == 0 )
341 ctx->initial_entropy_run = 1;
342 if( ( ret = mbedtls_entropy_update_nv_seed( ctx ) ) != 0 )
347 #if defined(MBEDTLS_THREADING_C)
348 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
353 * Always gather extra entropy before a call
357 if( count++ > ENTROPY_MAX_LOOP )
359 ret = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
363 if( ( ret = entropy_gather_internal( ctx ) ) != 0 )
367 for( i = 0; i < ctx->source_count; i++ )
368 if( ctx->source[i].size < ctx->source[i].threshold )
373 memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
375 #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
377 * Note that at this stage it is assumed that the accumulator was started
378 * in a previous call to entropy_update(). If this is not guaranteed, the
379 * code below will fail.
381 if( ( ret = mbedtls_sha512_finish_ret( &ctx->accumulator, buf ) ) != 0 )
385 * Reset accumulator and counters and recycle existing entropy
387 mbedtls_sha512_free( &ctx->accumulator );
388 mbedtls_sha512_init( &ctx->accumulator );
389 if( ( ret = mbedtls_sha512_starts_ret( &ctx->accumulator, 0 ) ) != 0 )
391 if( ( ret = mbedtls_sha512_update_ret( &ctx->accumulator, buf,
392 MBEDTLS_ENTROPY_BLOCK_SIZE ) ) != 0 )
396 * Perform second SHA-512 on entropy
398 if( ( ret = mbedtls_sha512_ret( buf, MBEDTLS_ENTROPY_BLOCK_SIZE,
401 #else /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
402 if( ( ret = mbedtls_sha256_finish_ret( &ctx->accumulator, buf ) ) != 0 )
406 * Reset accumulator and counters and recycle existing entropy
408 mbedtls_sha256_free( &ctx->accumulator );
409 mbedtls_sha256_init( &ctx->accumulator );
410 if( ( ret = mbedtls_sha256_starts_ret( &ctx->accumulator, 0 ) ) != 0 )
412 if( ( ret = mbedtls_sha256_update_ret( &ctx->accumulator, buf,
413 MBEDTLS_ENTROPY_BLOCK_SIZE ) ) != 0 )
417 * Perform second SHA-256 on entropy
419 if( ( ret = mbedtls_sha256_ret( buf, MBEDTLS_ENTROPY_BLOCK_SIZE,
422 #endif /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
424 for( i = 0; i < ctx->source_count; i++ )
425 ctx->source[i].size = 0;
427 memcpy( output, buf, len );
432 mbedtls_platform_zeroize( buf, sizeof( buf ) );
434 #if defined(MBEDTLS_THREADING_C)
435 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
436 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
442 #if defined(MBEDTLS_ENTROPY_NV_SEED)
443 int mbedtls_entropy_update_nv_seed( mbedtls_entropy_context *ctx )
445 int ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
446 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
448 /* Read new seed and write it to NV */
449 if( ( ret = mbedtls_entropy_func( ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) ) != 0 )
452 if( mbedtls_nv_seed_write( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) < 0 )
453 return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
455 /* Manually update the remaining stream with a separator value to diverge */
456 memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
457 ret = mbedtls_entropy_update_manual( ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE );
461 #endif /* MBEDTLS_ENTROPY_NV_SEED */
463 #if defined(MBEDTLS_FS_IO)
464 int mbedtls_entropy_write_seed_file( mbedtls_entropy_context *ctx, const char *path )
466 int ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
468 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
470 if( ( f = fopen( path, "wb" ) ) == NULL )
471 return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
473 if( ( ret = mbedtls_entropy_func( ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) ) != 0 )
476 if( fwrite( buf, 1, MBEDTLS_ENTROPY_BLOCK_SIZE, f ) != MBEDTLS_ENTROPY_BLOCK_SIZE )
478 ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
485 mbedtls_platform_zeroize( buf, sizeof( buf ) );
491 int mbedtls_entropy_update_seed_file( mbedtls_entropy_context *ctx, const char *path )
496 unsigned char buf[ MBEDTLS_ENTROPY_MAX_SEED_SIZE ];
498 if( ( f = fopen( path, "rb" ) ) == NULL )
499 return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
501 fseek( f, 0, SEEK_END );
502 n = (size_t) ftell( f );
503 fseek( f, 0, SEEK_SET );
505 if( n > MBEDTLS_ENTROPY_MAX_SEED_SIZE )
506 n = MBEDTLS_ENTROPY_MAX_SEED_SIZE;
508 if( fread( buf, 1, n, f ) != n )
509 ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
511 ret = mbedtls_entropy_update_manual( ctx, buf, n );
515 mbedtls_platform_zeroize( buf, sizeof( buf ) );
520 return( mbedtls_entropy_write_seed_file( ctx, path ) );
522 #endif /* MBEDTLS_FS_IO */
524 #if defined(MBEDTLS_SELF_TEST)
525 #if !defined(MBEDTLS_TEST_NULL_ENTROPY)
527 * Dummy source function
529 static int entropy_dummy_source( void *data, unsigned char *output,
530 size_t len, size_t *olen )
534 memset( output, 0x2a, len );
539 #endif /* !MBEDTLS_TEST_NULL_ENTROPY */
541 #if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
543 static int mbedtls_entropy_source_self_test_gather( unsigned char *buf, size_t buf_len )
546 size_t entropy_len = 0;
548 size_t attempts = buf_len;
550 while( attempts > 0 && entropy_len < buf_len )
552 if( ( ret = mbedtls_hardware_poll( NULL, buf + entropy_len,
553 buf_len - entropy_len, &olen ) ) != 0 )
560 if( entropy_len < buf_len )
569 static int mbedtls_entropy_source_self_test_check_bits( const unsigned char *buf,
572 unsigned char set= 0xFF;
573 unsigned char unset = 0x00;
576 for( i = 0; i < buf_len; i++ )
582 return( set == 0xFF || unset == 0x00 );
586 * A test to ensure hat the entropy sources are functioning correctly
587 * and there is no obvious failure. The test performs the following checks:
588 * - The entropy source is not providing only 0s (all bits unset) or 1s (all
590 * - The entropy source is not providing values in a pattern. Because the
591 * hardware could be providing data in an arbitrary length, this check polls
592 * the hardware entropy source twice and compares the result to ensure they
594 * - The error code returned by the entropy source is not an error.
596 int mbedtls_entropy_source_self_test( int verbose )
599 unsigned char buf0[2 * sizeof( unsigned long long int )];
600 unsigned char buf1[2 * sizeof( unsigned long long int )];
603 mbedtls_printf( " ENTROPY_BIAS test: " );
605 memset( buf0, 0x00, sizeof( buf0 ) );
606 memset( buf1, 0x00, sizeof( buf1 ) );
608 if( ( ret = mbedtls_entropy_source_self_test_gather( buf0, sizeof( buf0 ) ) ) != 0 )
610 if( ( ret = mbedtls_entropy_source_self_test_gather( buf1, sizeof( buf1 ) ) ) != 0 )
613 /* Make sure that the returned values are not all 0 or 1 */
614 if( ( ret = mbedtls_entropy_source_self_test_check_bits( buf0, sizeof( buf0 ) ) ) != 0 )
616 if( ( ret = mbedtls_entropy_source_self_test_check_bits( buf1, sizeof( buf1 ) ) ) != 0 )
619 /* Make sure that the entropy source is not returning values in a
621 ret = memcmp( buf0, buf1, sizeof( buf0 ) ) == 0;
627 mbedtls_printf( "failed\n" );
629 mbedtls_printf( "passed\n" );
631 mbedtls_printf( "\n" );
637 #endif /* MBEDTLS_ENTROPY_HARDWARE_ALT */
640 * The actual entropy quality is hard to test, but we can at least
641 * test that the functions don't cause errors and write the correct
642 * amount of data to buffers.
644 int mbedtls_entropy_self_test( int verbose )
647 #if !defined(MBEDTLS_TEST_NULL_ENTROPY)
648 mbedtls_entropy_context ctx;
649 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
650 unsigned char acc[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
652 #endif /* !MBEDTLS_TEST_NULL_ENTROPY */
655 mbedtls_printf( " ENTROPY test: " );
657 #if !defined(MBEDTLS_TEST_NULL_ENTROPY)
658 mbedtls_entropy_init( &ctx );
660 /* First do a gather to make sure we have default sources */
661 if( ( ret = mbedtls_entropy_gather( &ctx ) ) != 0 )
664 ret = mbedtls_entropy_add_source( &ctx, entropy_dummy_source, NULL, 16,
665 MBEDTLS_ENTROPY_SOURCE_WEAK );
669 if( ( ret = mbedtls_entropy_update_manual( &ctx, buf, sizeof buf ) ) != 0 )
673 * To test that mbedtls_entropy_func writes correct number of bytes:
674 * - use the whole buffer and rely on ASan to detect overruns
675 * - collect entropy 8 times and OR the result in an accumulator:
676 * any byte should then be 0 with probably 2^(-64), so requiring
677 * each of the 32 or 64 bytes to be non-zero has a false failure rate
678 * of at most 2^(-58) which is acceptable.
680 for( i = 0; i < 8; i++ )
682 if( ( ret = mbedtls_entropy_func( &ctx, buf, sizeof( buf ) ) ) != 0 )
685 for( j = 0; j < sizeof( buf ); j++ )
689 for( j = 0; j < sizeof( buf ); j++ )
698 #if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
699 if( ( ret = mbedtls_entropy_source_self_test( 0 ) ) != 0 )
704 mbedtls_entropy_free( &ctx );
705 #endif /* !MBEDTLS_TEST_NULL_ENTROPY */
710 mbedtls_printf( "failed\n" );
712 mbedtls_printf( "passed\n" );
714 mbedtls_printf( "\n" );
719 #endif /* MBEDTLS_SELF_TEST */
721 #endif /* MBEDTLS_ENTROPY_C */