2 * Entropy accumulator implementation
4 * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: GPL-2.0
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 * This file is part of mbed TLS (https://tls.mbed.org)
24 #if !defined(MBEDTLS_CONFIG_FILE)
25 #include "mbedtls/config.h"
27 #include MBEDTLS_CONFIG_FILE
30 #if defined(MBEDTLS_ENTROPY_C)
32 #if defined(MBEDTLS_TEST_NULL_ENTROPY)
33 #warning "**** WARNING! MBEDTLS_TEST_NULL_ENTROPY defined! "
34 #warning "**** THIS BUILD HAS NO DEFINED ENTROPY SOURCES "
35 #warning "**** THIS BUILD IS *NOT* SUITABLE FOR PRODUCTION USE "
38 #include "mbedtls/entropy.h"
39 #include "mbedtls/entropy_poll.h"
40 #include "mbedtls/platform_util.h"
44 #if defined(MBEDTLS_FS_IO)
48 #if defined(MBEDTLS_ENTROPY_NV_SEED)
49 #include "mbedtls/platform.h"
52 #if defined(MBEDTLS_SELF_TEST)
53 #if defined(MBEDTLS_PLATFORM_C)
54 #include "mbedtls/platform.h"
57 #define mbedtls_printf printf
58 #endif /* MBEDTLS_PLATFORM_C */
59 #endif /* MBEDTLS_SELF_TEST */
61 #if defined(MBEDTLS_HAVEGE_C)
62 #include "mbedtls/havege.h"
65 #define ENTROPY_MAX_LOOP 256 /**< Maximum amount to loop before error */
67 void mbedtls_entropy_init( mbedtls_entropy_context
*ctx
)
69 ctx
->source_count
= 0;
70 memset( ctx
->source
, 0, sizeof( ctx
->source
) );
72 #if defined(MBEDTLS_THREADING_C)
73 mbedtls_mutex_init( &ctx
->mutex
);
76 ctx
->accumulator_started
= 0;
77 #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
78 mbedtls_sha512_init( &ctx
->accumulator
);
80 mbedtls_sha256_init( &ctx
->accumulator
);
82 #if defined(MBEDTLS_HAVEGE_C)
83 mbedtls_havege_init( &ctx
->havege_data
);
86 /* Reminder: Update ENTROPY_HAVE_STRONG in the test files
87 * when adding more strong entropy sources here. */
89 #if defined(MBEDTLS_TEST_NULL_ENTROPY)
90 mbedtls_entropy_add_source( ctx
, mbedtls_null_entropy_poll
, NULL
,
91 1, MBEDTLS_ENTROPY_SOURCE_STRONG
);
94 #if !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
95 #if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
96 mbedtls_entropy_add_source( ctx
, mbedtls_platform_entropy_poll
, NULL
,
97 MBEDTLS_ENTROPY_MIN_PLATFORM
,
98 MBEDTLS_ENTROPY_SOURCE_STRONG
);
100 #if defined(MBEDTLS_TIMING_C)
101 mbedtls_entropy_add_source( ctx
, mbedtls_hardclock_poll
, NULL
,
102 MBEDTLS_ENTROPY_MIN_HARDCLOCK
,
103 MBEDTLS_ENTROPY_SOURCE_WEAK
);
105 #if defined(MBEDTLS_HAVEGE_C)
106 mbedtls_entropy_add_source( ctx
, mbedtls_havege_poll
, &ctx
->havege_data
,
107 MBEDTLS_ENTROPY_MIN_HAVEGE
,
108 MBEDTLS_ENTROPY_SOURCE_STRONG
);
110 #if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
111 mbedtls_entropy_add_source( ctx
, mbedtls_hardware_poll
, NULL
,
112 MBEDTLS_ENTROPY_MIN_HARDWARE
,
113 MBEDTLS_ENTROPY_SOURCE_STRONG
);
115 #if defined(MBEDTLS_ENTROPY_NV_SEED)
116 mbedtls_entropy_add_source( ctx
, mbedtls_nv_seed_poll
, NULL
,
117 MBEDTLS_ENTROPY_BLOCK_SIZE
,
118 MBEDTLS_ENTROPY_SOURCE_STRONG
);
119 ctx
->initial_entropy_run
= 0;
121 #endif /* MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES */
124 void mbedtls_entropy_free( mbedtls_entropy_context
*ctx
)
126 #if defined(MBEDTLS_HAVEGE_C)
127 mbedtls_havege_free( &ctx
->havege_data
);
129 #if defined(MBEDTLS_THREADING_C)
130 mbedtls_mutex_free( &ctx
->mutex
);
132 #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
133 mbedtls_sha512_free( &ctx
->accumulator
);
135 mbedtls_sha256_free( &ctx
->accumulator
);
137 #if defined(MBEDTLS_ENTROPY_NV_SEED)
138 ctx
->initial_entropy_run
= 0;
140 ctx
->source_count
= 0;
141 mbedtls_platform_zeroize( ctx
->source
, sizeof( ctx
->source
) );
142 ctx
->accumulator_started
= 0;
145 int mbedtls_entropy_add_source( mbedtls_entropy_context
*ctx
,
146 mbedtls_entropy_f_source_ptr f_source
, void *p_source
,
147 size_t threshold
, int strong
)
151 #if defined(MBEDTLS_THREADING_C)
152 if( ( ret
= mbedtls_mutex_lock( &ctx
->mutex
) ) != 0 )
156 idx
= ctx
->source_count
;
157 if( idx
>= MBEDTLS_ENTROPY_MAX_SOURCES
)
159 ret
= MBEDTLS_ERR_ENTROPY_MAX_SOURCES
;
163 ctx
->source
[idx
].f_source
= f_source
;
164 ctx
->source
[idx
].p_source
= p_source
;
165 ctx
->source
[idx
].threshold
= threshold
;
166 ctx
->source
[idx
].strong
= strong
;
171 #if defined(MBEDTLS_THREADING_C)
172 if( mbedtls_mutex_unlock( &ctx
->mutex
) != 0 )
173 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR
);
180 * Entropy accumulator update
182 static int entropy_update( mbedtls_entropy_context
*ctx
, unsigned char source_id
,
183 const unsigned char *data
, size_t len
)
185 unsigned char header
[2];
186 unsigned char tmp
[MBEDTLS_ENTROPY_BLOCK_SIZE
];
187 size_t use_len
= len
;
188 const unsigned char *p
= data
;
191 if( use_len
> MBEDTLS_ENTROPY_BLOCK_SIZE
)
193 #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
194 if( ( ret
= mbedtls_sha512_ret( data
, len
, tmp
, 0 ) ) != 0 )
197 if( ( ret
= mbedtls_sha256_ret( data
, len
, tmp
, 0 ) ) != 0 )
201 use_len
= MBEDTLS_ENTROPY_BLOCK_SIZE
;
204 header
[0] = source_id
;
205 header
[1] = use_len
& 0xFF;
208 * Start the accumulator if this has not already happened. Note that
209 * it is sufficient to start the accumulator here only because all calls to
210 * gather entropy eventually execute this code.
212 #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
213 if( ctx
->accumulator_started
== 0 &&
214 ( ret
= mbedtls_sha512_starts_ret( &ctx
->accumulator
, 0 ) ) != 0 )
217 ctx
->accumulator_started
= 1;
218 if( ( ret
= mbedtls_sha512_update_ret( &ctx
->accumulator
, header
, 2 ) ) != 0 )
220 ret
= mbedtls_sha512_update_ret( &ctx
->accumulator
, p
, use_len
);
222 if( ctx
->accumulator_started
== 0 &&
223 ( ret
= mbedtls_sha256_starts_ret( &ctx
->accumulator
, 0 ) ) != 0 )
226 ctx
->accumulator_started
= 1;
227 if( ( ret
= mbedtls_sha256_update_ret( &ctx
->accumulator
, header
, 2 ) ) != 0 )
229 ret
= mbedtls_sha256_update_ret( &ctx
->accumulator
, p
, use_len
);
233 mbedtls_platform_zeroize( tmp
, sizeof( tmp
) );
238 int mbedtls_entropy_update_manual( mbedtls_entropy_context
*ctx
,
239 const unsigned char *data
, size_t len
)
243 #if defined(MBEDTLS_THREADING_C)
244 if( ( ret
= mbedtls_mutex_lock( &ctx
->mutex
) ) != 0 )
248 ret
= entropy_update( ctx
, MBEDTLS_ENTROPY_SOURCE_MANUAL
, data
, len
);
250 #if defined(MBEDTLS_THREADING_C)
251 if( mbedtls_mutex_unlock( &ctx
->mutex
) != 0 )
252 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR
);
259 * Run through the different sources to add entropy to our accumulator
261 static int entropy_gather_internal( mbedtls_entropy_context
*ctx
)
263 int ret
, i
, have_one_strong
= 0;
264 unsigned char buf
[MBEDTLS_ENTROPY_MAX_GATHER
];
267 if( ctx
->source_count
== 0 )
268 return( MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED
);
271 * Run through our entropy sources
273 for( i
= 0; i
< ctx
->source_count
; i
++ )
275 if( ctx
->source
[i
].strong
== MBEDTLS_ENTROPY_SOURCE_STRONG
)
279 if( ( ret
= ctx
->source
[i
].f_source( ctx
->source
[i
].p_source
,
280 buf
, MBEDTLS_ENTROPY_MAX_GATHER
, &olen
) ) != 0 )
286 * Add if we actually gathered something
290 if( ( ret
= entropy_update( ctx
, (unsigned char) i
,
293 ctx
->source
[i
].size
+= olen
;
297 if( have_one_strong
== 0 )
298 ret
= MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE
;
301 mbedtls_platform_zeroize( buf
, sizeof( buf
) );
307 * Thread-safe wrapper for entropy_gather_internal()
309 int mbedtls_entropy_gather( mbedtls_entropy_context
*ctx
)
313 #if defined(MBEDTLS_THREADING_C)
314 if( ( ret
= mbedtls_mutex_lock( &ctx
->mutex
) ) != 0 )
318 ret
= entropy_gather_internal( ctx
);
320 #if defined(MBEDTLS_THREADING_C)
321 if( mbedtls_mutex_unlock( &ctx
->mutex
) != 0 )
322 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR
);
328 int mbedtls_entropy_func( void *data
, unsigned char *output
, size_t len
)
330 int ret
, count
= 0, i
, done
;
331 mbedtls_entropy_context
*ctx
= (mbedtls_entropy_context
*) data
;
332 unsigned char buf
[MBEDTLS_ENTROPY_BLOCK_SIZE
];
334 if( len
> MBEDTLS_ENTROPY_BLOCK_SIZE
)
335 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
);
337 #if defined(MBEDTLS_ENTROPY_NV_SEED)
338 /* Update the NV entropy seed before generating any entropy for outside
341 if( ctx
->initial_entropy_run
== 0 )
343 ctx
->initial_entropy_run
= 1;
344 if( ( ret
= mbedtls_entropy_update_nv_seed( ctx
) ) != 0 )
349 #if defined(MBEDTLS_THREADING_C)
350 if( ( ret
= mbedtls_mutex_lock( &ctx
->mutex
) ) != 0 )
355 * Always gather extra entropy before a call
359 if( count
++ > ENTROPY_MAX_LOOP
)
361 ret
= MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
;
365 if( ( ret
= entropy_gather_internal( ctx
) ) != 0 )
369 for( i
= 0; i
< ctx
->source_count
; i
++ )
370 if( ctx
->source
[i
].size
< ctx
->source
[i
].threshold
)
375 memset( buf
, 0, MBEDTLS_ENTROPY_BLOCK_SIZE
);
377 #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
379 * Note that at this stage it is assumed that the accumulator was started
380 * in a previous call to entropy_update(). If this is not guaranteed, the
381 * code below will fail.
383 if( ( ret
= mbedtls_sha512_finish_ret( &ctx
->accumulator
, buf
) ) != 0 )
387 * Reset accumulator and counters and recycle existing entropy
389 mbedtls_sha512_free( &ctx
->accumulator
);
390 mbedtls_sha512_init( &ctx
->accumulator
);
391 if( ( ret
= mbedtls_sha512_starts_ret( &ctx
->accumulator
, 0 ) ) != 0 )
393 if( ( ret
= mbedtls_sha512_update_ret( &ctx
->accumulator
, buf
,
394 MBEDTLS_ENTROPY_BLOCK_SIZE
) ) != 0 )
398 * Perform second SHA-512 on entropy
400 if( ( ret
= mbedtls_sha512_ret( buf
, MBEDTLS_ENTROPY_BLOCK_SIZE
,
403 #else /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
404 if( ( ret
= mbedtls_sha256_finish_ret( &ctx
->accumulator
, buf
) ) != 0 )
408 * Reset accumulator and counters and recycle existing entropy
410 mbedtls_sha256_free( &ctx
->accumulator
);
411 mbedtls_sha256_init( &ctx
->accumulator
);
412 if( ( ret
= mbedtls_sha256_starts_ret( &ctx
->accumulator
, 0 ) ) != 0 )
414 if( ( ret
= mbedtls_sha256_update_ret( &ctx
->accumulator
, buf
,
415 MBEDTLS_ENTROPY_BLOCK_SIZE
) ) != 0 )
419 * Perform second SHA-256 on entropy
421 if( ( ret
= mbedtls_sha256_ret( buf
, MBEDTLS_ENTROPY_BLOCK_SIZE
,
424 #endif /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
426 for( i
= 0; i
< ctx
->source_count
; i
++ )
427 ctx
->source
[i
].size
= 0;
429 memcpy( output
, buf
, len
);
434 mbedtls_platform_zeroize( buf
, sizeof( buf
) );
436 #if defined(MBEDTLS_THREADING_C)
437 if( mbedtls_mutex_unlock( &ctx
->mutex
) != 0 )
438 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR
);
444 #if defined(MBEDTLS_ENTROPY_NV_SEED)
445 int mbedtls_entropy_update_nv_seed( mbedtls_entropy_context
*ctx
)
447 int ret
= MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR
;
448 unsigned char buf
[MBEDTLS_ENTROPY_BLOCK_SIZE
];
450 /* Read new seed and write it to NV */
451 if( ( ret
= mbedtls_entropy_func( ctx
, buf
, MBEDTLS_ENTROPY_BLOCK_SIZE
) ) != 0 )
454 if( mbedtls_nv_seed_write( buf
, MBEDTLS_ENTROPY_BLOCK_SIZE
) < 0 )
455 return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR
);
457 /* Manually update the remaining stream with a separator value to diverge */
458 memset( buf
, 0, MBEDTLS_ENTROPY_BLOCK_SIZE
);
459 ret
= mbedtls_entropy_update_manual( ctx
, buf
, MBEDTLS_ENTROPY_BLOCK_SIZE
);
463 #endif /* MBEDTLS_ENTROPY_NV_SEED */
465 #if defined(MBEDTLS_FS_IO)
466 int mbedtls_entropy_write_seed_file( mbedtls_entropy_context
*ctx
, const char *path
)
468 int ret
= MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR
;
470 unsigned char buf
[MBEDTLS_ENTROPY_BLOCK_SIZE
];
472 if( ( f
= fopen( path
, "wb" ) ) == NULL
)
473 return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR
);
475 if( ( ret
= mbedtls_entropy_func( ctx
, buf
, MBEDTLS_ENTROPY_BLOCK_SIZE
) ) != 0 )
478 if( fwrite( buf
, 1, MBEDTLS_ENTROPY_BLOCK_SIZE
, f
) != MBEDTLS_ENTROPY_BLOCK_SIZE
)
480 ret
= MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR
;
487 mbedtls_platform_zeroize( buf
, sizeof( buf
) );
493 int mbedtls_entropy_update_seed_file( mbedtls_entropy_context
*ctx
, const char *path
)
498 unsigned char buf
[ MBEDTLS_ENTROPY_MAX_SEED_SIZE
];
500 if( ( f
= fopen( path
, "rb" ) ) == NULL
)
501 return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR
);
503 fseek( f
, 0, SEEK_END
);
504 n
= (size_t) ftell( f
);
505 fseek( f
, 0, SEEK_SET
);
507 if( n
> MBEDTLS_ENTROPY_MAX_SEED_SIZE
)
508 n
= MBEDTLS_ENTROPY_MAX_SEED_SIZE
;
510 if( fread( buf
, 1, n
, f
) != n
)
511 ret
= MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR
;
513 ret
= mbedtls_entropy_update_manual( ctx
, buf
, n
);
517 mbedtls_platform_zeroize( buf
, sizeof( buf
) );
522 return( mbedtls_entropy_write_seed_file( ctx
, path
) );
524 #endif /* MBEDTLS_FS_IO */
526 #if defined(MBEDTLS_SELF_TEST)
527 #if !defined(MBEDTLS_TEST_NULL_ENTROPY)
529 * Dummy source function
531 static int entropy_dummy_source( void *data
, unsigned char *output
,
532 size_t len
, size_t *olen
)
536 memset( output
, 0x2a, len
);
541 #endif /* !MBEDTLS_TEST_NULL_ENTROPY */
543 #if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
545 static int mbedtls_entropy_source_self_test_gather( unsigned char *buf
, size_t buf_len
)
548 size_t entropy_len
= 0;
550 size_t attempts
= buf_len
;
552 while( attempts
> 0 && entropy_len
< buf_len
)
554 if( ( ret
= mbedtls_hardware_poll( NULL
, buf
+ entropy_len
,
555 buf_len
- entropy_len
, &olen
) ) != 0 )
562 if( entropy_len
< buf_len
)
571 static int mbedtls_entropy_source_self_test_check_bits( const unsigned char *buf
,
574 unsigned char set
= 0xFF;
575 unsigned char unset
= 0x00;
578 for( i
= 0; i
< buf_len
; i
++ )
584 return( set
== 0xFF || unset
== 0x00 );
588 * A test to ensure hat the entropy sources are functioning correctly
589 * and there is no obvious failure. The test performs the following checks:
590 * - The entropy source is not providing only 0s (all bits unset) or 1s (all
592 * - The entropy source is not providing values in a pattern. Because the
593 * hardware could be providing data in an arbitrary length, this check polls
594 * the hardware entropy source twice and compares the result to ensure they
596 * - The error code returned by the entropy source is not an error.
598 int mbedtls_entropy_source_self_test( int verbose
)
601 unsigned char buf0
[2 * sizeof( unsigned long long int )];
602 unsigned char buf1
[2 * sizeof( unsigned long long int )];
605 mbedtls_printf( " ENTROPY_BIAS test: " );
607 memset( buf0
, 0x00, sizeof( buf0
) );
608 memset( buf1
, 0x00, sizeof( buf1
) );
610 if( ( ret
= mbedtls_entropy_source_self_test_gather( buf0
, sizeof( buf0
) ) ) != 0 )
612 if( ( ret
= mbedtls_entropy_source_self_test_gather( buf1
, sizeof( buf1
) ) ) != 0 )
615 /* Make sure that the returned values are not all 0 or 1 */
616 if( ( ret
= mbedtls_entropy_source_self_test_check_bits( buf0
, sizeof( buf0
) ) ) != 0 )
618 if( ( ret
= mbedtls_entropy_source_self_test_check_bits( buf1
, sizeof( buf1
) ) ) != 0 )
621 /* Make sure that the entropy source is not returning values in a
623 ret
= memcmp( buf0
, buf1
, sizeof( buf0
) ) == 0;
629 mbedtls_printf( "failed\n" );
631 mbedtls_printf( "passed\n" );
633 mbedtls_printf( "\n" );
639 #endif /* MBEDTLS_ENTROPY_HARDWARE_ALT */
642 * The actual entropy quality is hard to test, but we can at least
643 * test that the functions don't cause errors and write the correct
644 * amount of data to buffers.
646 int mbedtls_entropy_self_test( int verbose
)
649 #if !defined(MBEDTLS_TEST_NULL_ENTROPY)
650 mbedtls_entropy_context ctx
;
651 unsigned char buf
[MBEDTLS_ENTROPY_BLOCK_SIZE
] = { 0 };
652 unsigned char acc
[MBEDTLS_ENTROPY_BLOCK_SIZE
] = { 0 };
654 #endif /* !MBEDTLS_TEST_NULL_ENTROPY */
657 mbedtls_printf( " ENTROPY test: " );
659 #if !defined(MBEDTLS_TEST_NULL_ENTROPY)
660 mbedtls_entropy_init( &ctx
);
662 /* First do a gather to make sure we have default sources */
663 if( ( ret
= mbedtls_entropy_gather( &ctx
) ) != 0 )
666 ret
= mbedtls_entropy_add_source( &ctx
, entropy_dummy_source
, NULL
, 16,
667 MBEDTLS_ENTROPY_SOURCE_WEAK
);
671 if( ( ret
= mbedtls_entropy_update_manual( &ctx
, buf
, sizeof buf
) ) != 0 )
675 * To test that mbedtls_entropy_func writes correct number of bytes:
676 * - use the whole buffer and rely on ASan to detect overruns
677 * - collect entropy 8 times and OR the result in an accumulator:
678 * any byte should then be 0 with probably 2^(-64), so requiring
679 * each of the 32 or 64 bytes to be non-zero has a false failure rate
680 * of at most 2^(-58) which is acceptable.
682 for( i
= 0; i
< 8; i
++ )
684 if( ( ret
= mbedtls_entropy_func( &ctx
, buf
, sizeof( buf
) ) ) != 0 )
687 for( j
= 0; j
< sizeof( buf
); j
++ )
691 for( j
= 0; j
< sizeof( buf
); j
++ )
700 #if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
701 if( ( ret
= mbedtls_entropy_source_self_test( 0 ) ) != 0 )
706 mbedtls_entropy_free( &ctx
);
707 #endif /* !MBEDTLS_TEST_NULL_ENTROPY */
712 mbedtls_printf( "failed\n" );
714 mbedtls_printf( "passed\n" );
716 mbedtls_printf( "\n" );
721 #endif /* MBEDTLS_SELF_TEST */
723 #endif /* MBEDTLS_ENTROPY_C */