xref: /optee_os/lib/libmbedtls/mbedtls/library/entropy.c (revision a1d5c81f8834a9d2c6f4372cce2e59e70e709121)
1 // SPDX-License-Identifier: Apache-2.0
2 /*
3  *  Entropy accumulator implementation
4  *
5  *  Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
6  *
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
10  *
11  *  http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  *  This file is part of mbed TLS (https://tls.mbed.org)
20  */
21 
22 #if !defined(MBEDTLS_CONFIG_FILE)
23 #include "mbedtls/config.h"
24 #else
25 #include MBEDTLS_CONFIG_FILE
26 #endif
27 
28 #if defined(MBEDTLS_ENTROPY_C)
29 
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 "
34 #endif
35 
36 #include "mbedtls/entropy.h"
37 #include "mbedtls/entropy_poll.h"
38 #include "mbedtls/platform_util.h"
39 #include "mbedtls/error.h"
40 
41 #include <string.h>
42 
43 #if defined(MBEDTLS_FS_IO)
44 #include <stdio.h>
45 #endif
46 
47 #if defined(MBEDTLS_ENTROPY_NV_SEED)
48 #include "mbedtls/platform.h"
49 #endif
50 
51 #if defined(MBEDTLS_SELF_TEST)
52 #if defined(MBEDTLS_PLATFORM_C)
53 #include "mbedtls/platform.h"
54 #else
55 #include <stdio.h>
56 #define mbedtls_printf     printf
57 #endif /* MBEDTLS_PLATFORM_C */
58 #endif /* MBEDTLS_SELF_TEST */
59 
60 #if defined(MBEDTLS_HAVEGE_C)
61 #include "mbedtls/havege.h"
62 #endif
63 
64 #define ENTROPY_MAX_LOOP    256     /**< Maximum amount to loop before error */
65 
66 void mbedtls_entropy_init( mbedtls_entropy_context *ctx )
67 {
68     ctx->source_count = 0;
69     memset( ctx->source, 0, sizeof( ctx->source ) );
70 
71 #if defined(MBEDTLS_THREADING_C)
72     mbedtls_mutex_init( &ctx->mutex );
73 #endif
74 
75     ctx->accumulator_started = 0;
76 #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
77     mbedtls_sha512_init( &ctx->accumulator );
78 #else
79     mbedtls_sha256_init( &ctx->accumulator );
80 #endif
81 #if defined(MBEDTLS_HAVEGE_C)
82     mbedtls_havege_init( &ctx->havege_data );
83 #endif
84 
85     /* Reminder: Update ENTROPY_HAVE_STRONG in the test files
86      *           when adding more strong entropy sources here. */
87 
88 #if defined(MBEDTLS_TEST_NULL_ENTROPY)
89     mbedtls_entropy_add_source( ctx, mbedtls_null_entropy_poll, NULL,
90                                 1, MBEDTLS_ENTROPY_SOURCE_STRONG );
91 #endif
92 
93 #if !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
94 #if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
95     mbedtls_entropy_add_source( ctx, mbedtls_platform_entropy_poll, NULL,
96                                 MBEDTLS_ENTROPY_MIN_PLATFORM,
97                                 MBEDTLS_ENTROPY_SOURCE_STRONG );
98 #endif
99 #if defined(MBEDTLS_TIMING_C)
100     mbedtls_entropy_add_source( ctx, mbedtls_hardclock_poll, NULL,
101                                 MBEDTLS_ENTROPY_MIN_HARDCLOCK,
102                                 MBEDTLS_ENTROPY_SOURCE_WEAK );
103 #endif
104 #if defined(MBEDTLS_HAVEGE_C)
105     mbedtls_entropy_add_source( ctx, mbedtls_havege_poll, &ctx->havege_data,
106                                 MBEDTLS_ENTROPY_MIN_HAVEGE,
107                                 MBEDTLS_ENTROPY_SOURCE_STRONG );
108 #endif
109 #if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
110     mbedtls_entropy_add_source( ctx, mbedtls_hardware_poll, NULL,
111                                 MBEDTLS_ENTROPY_MIN_HARDWARE,
112                                 MBEDTLS_ENTROPY_SOURCE_STRONG );
113 #endif
114 #if defined(MBEDTLS_ENTROPY_NV_SEED)
115     mbedtls_entropy_add_source( ctx, mbedtls_nv_seed_poll, NULL,
116                                 MBEDTLS_ENTROPY_BLOCK_SIZE,
117                                 MBEDTLS_ENTROPY_SOURCE_STRONG );
118     ctx->initial_entropy_run = 0;
119 #endif
120 #endif /* MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES */
121 }
122 
123 void mbedtls_entropy_free( mbedtls_entropy_context *ctx )
124 {
125 #if defined(MBEDTLS_HAVEGE_C)
126     mbedtls_havege_free( &ctx->havege_data );
127 #endif
128 #if defined(MBEDTLS_THREADING_C)
129     mbedtls_mutex_free( &ctx->mutex );
130 #endif
131 #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
132     mbedtls_sha512_free( &ctx->accumulator );
133 #else
134     mbedtls_sha256_free( &ctx->accumulator );
135 #endif
136 #if defined(MBEDTLS_ENTROPY_NV_SEED)
137     ctx->initial_entropy_run = 0;
138 #endif
139     ctx->source_count = 0;
140     mbedtls_platform_zeroize( ctx->source, sizeof( ctx->source ) );
141     ctx->accumulator_started = 0;
142 }
143 
144 int mbedtls_entropy_add_source( mbedtls_entropy_context *ctx,
145                         mbedtls_entropy_f_source_ptr f_source, void *p_source,
146                         size_t threshold, int strong )
147 {
148     int idx, ret = 0;
149 
150 #if defined(MBEDTLS_THREADING_C)
151     if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
152         return( ret );
153 #endif
154 
155     idx = ctx->source_count;
156     if( idx >= MBEDTLS_ENTROPY_MAX_SOURCES )
157     {
158         ret = MBEDTLS_ERR_ENTROPY_MAX_SOURCES;
159         goto exit;
160     }
161 
162     ctx->source[idx].f_source  = f_source;
163     ctx->source[idx].p_source  = p_source;
164     ctx->source[idx].threshold = threshold;
165     ctx->source[idx].strong    = strong;
166 
167     ctx->source_count++;
168 
169 exit:
170 #if defined(MBEDTLS_THREADING_C)
171     if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
172         return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
173 #endif
174 
175     return( ret );
176 }
177 
178 /*
179  * Entropy accumulator update
180  */
181 static int entropy_update( mbedtls_entropy_context *ctx, unsigned char source_id,
182                            const unsigned char *data, size_t len )
183 {
184     unsigned char header[2];
185     unsigned char tmp[MBEDTLS_ENTROPY_BLOCK_SIZE];
186     size_t use_len = len;
187     const unsigned char *p = data;
188     int ret = 0;
189 
190     if( use_len > MBEDTLS_ENTROPY_BLOCK_SIZE )
191     {
192 #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
193         if( ( ret = mbedtls_sha512_ret( data, len, tmp, 0 ) ) != 0 )
194             goto cleanup;
195 #else
196         if( ( ret = mbedtls_sha256_ret( data, len, tmp, 0 ) ) != 0 )
197             goto cleanup;
198 #endif
199         p = tmp;
200         use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
201     }
202 
203     header[0] = source_id;
204     header[1] = use_len & 0xFF;
205 
206     /*
207      * Start the accumulator if this has not already happened. Note that
208      * it is sufficient to start the accumulator here only because all calls to
209      * gather entropy eventually execute this code.
210      */
211 #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
212     if( ctx->accumulator_started == 0 &&
213         ( ret = mbedtls_sha512_starts_ret( &ctx->accumulator, 0 ) ) != 0 )
214         goto cleanup;
215     else
216         ctx->accumulator_started = 1;
217     if( ( ret = mbedtls_sha512_update_ret( &ctx->accumulator, header, 2 ) ) != 0 )
218         goto cleanup;
219     ret = mbedtls_sha512_update_ret( &ctx->accumulator, p, use_len );
220 #else
221     if( ctx->accumulator_started == 0 &&
222         ( ret = mbedtls_sha256_starts_ret( &ctx->accumulator, 0 ) ) != 0 )
223         goto cleanup;
224     else
225         ctx->accumulator_started = 1;
226     if( ( ret = mbedtls_sha256_update_ret( &ctx->accumulator, header, 2 ) ) != 0 )
227         goto cleanup;
228     ret = mbedtls_sha256_update_ret( &ctx->accumulator, p, use_len );
229 #endif
230 
231 cleanup:
232     mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
233 
234     return( ret );
235 }
236 
237 int mbedtls_entropy_update_manual( mbedtls_entropy_context *ctx,
238                            const unsigned char *data, size_t len )
239 {
240     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
241 
242 #if defined(MBEDTLS_THREADING_C)
243     if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
244         return( ret );
245 #endif
246 
247     ret = entropy_update( ctx, MBEDTLS_ENTROPY_SOURCE_MANUAL, data, len );
248 
249 #if defined(MBEDTLS_THREADING_C)
250     if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
251         return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
252 #endif
253 
254     return( ret );
255 }
256 
257 /*
258  * Run through the different sources to add entropy to our accumulator
259  */
260 static int entropy_gather_internal( mbedtls_entropy_context *ctx )
261 {
262     int ret = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
263     int i;
264     int have_one_strong = 0;
265     unsigned char buf[MBEDTLS_ENTROPY_MAX_GATHER];
266     size_t olen;
267 
268     if( ctx->source_count == 0 )
269         return( MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED );
270 
271     /*
272      * Run through our entropy sources
273      */
274     for( i = 0; i < ctx->source_count; i++ )
275     {
276         if( ctx->source[i].strong == MBEDTLS_ENTROPY_SOURCE_STRONG )
277             have_one_strong = 1;
278 
279         olen = 0;
280         if( ( ret = ctx->source[i].f_source( ctx->source[i].p_source,
281                         buf, MBEDTLS_ENTROPY_MAX_GATHER, &olen ) ) != 0 )
282         {
283             goto cleanup;
284         }
285 
286         /*
287          * Add if we actually gathered something
288          */
289         if( olen > 0 )
290         {
291             if( ( ret = entropy_update( ctx, (unsigned char) i,
292                                         buf, olen ) ) != 0 )
293                 return( ret );
294             ctx->source[i].size += olen;
295         }
296     }
297 
298     if( have_one_strong == 0 )
299         ret = MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE;
300 
301 cleanup:
302     mbedtls_platform_zeroize( buf, sizeof( buf ) );
303 
304     return( ret );
305 }
306 
307 /*
308  * Thread-safe wrapper for entropy_gather_internal()
309  */
310 int mbedtls_entropy_gather( mbedtls_entropy_context *ctx )
311 {
312     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
313 
314 #if defined(MBEDTLS_THREADING_C)
315     if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
316         return( ret );
317 #endif
318 
319     ret = entropy_gather_internal( ctx );
320 
321 #if defined(MBEDTLS_THREADING_C)
322     if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
323         return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
324 #endif
325 
326     return( ret );
327 }
328 
329 int mbedtls_entropy_func( void *data, unsigned char *output, size_t len )
330 {
331     int ret, count = 0, i, thresholds_reached;
332     size_t strong_size;
333     mbedtls_entropy_context *ctx = (mbedtls_entropy_context *) data;
334     unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
335 
336     if( len > MBEDTLS_ENTROPY_BLOCK_SIZE )
337         return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
338 
339 #if defined(MBEDTLS_ENTROPY_NV_SEED)
340     /* Update the NV entropy seed before generating any entropy for outside
341      * use.
342      */
343     if( ctx->initial_entropy_run == 0 )
344     {
345         ctx->initial_entropy_run = 1;
346         if( ( ret = mbedtls_entropy_update_nv_seed( ctx ) ) != 0 )
347             return( ret );
348     }
349 #endif
350 
351 #if defined(MBEDTLS_THREADING_C)
352     if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
353         return( ret );
354 #endif
355 
356     /*
357      * Always gather extra entropy before a call
358      */
359     do
360     {
361         if( count++ > ENTROPY_MAX_LOOP )
362         {
363             ret = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
364             goto exit;
365         }
366 
367         if( ( ret = entropy_gather_internal( ctx ) ) != 0 )
368             goto exit;
369 
370         thresholds_reached = 1;
371         strong_size = 0;
372         for( i = 0; i < ctx->source_count; i++ )
373         {
374             if( ctx->source[i].size < ctx->source[i].threshold )
375                 thresholds_reached = 0;
376             if( ctx->source[i].strong == MBEDTLS_ENTROPY_SOURCE_STRONG )
377                 strong_size += ctx->source[i].size;
378         }
379     }
380     while( ! thresholds_reached || strong_size < MBEDTLS_ENTROPY_BLOCK_SIZE );
381 
382     memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
383 
384 #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
385     /*
386      * Note that at this stage it is assumed that the accumulator was started
387      * in a previous call to entropy_update(). If this is not guaranteed, the
388      * code below will fail.
389      */
390     if( ( ret = mbedtls_sha512_finish_ret( &ctx->accumulator, buf ) ) != 0 )
391         goto exit;
392 
393     /*
394      * Reset accumulator and counters and recycle existing entropy
395      */
396     mbedtls_sha512_free( &ctx->accumulator );
397     mbedtls_sha512_init( &ctx->accumulator );
398     if( ( ret = mbedtls_sha512_starts_ret( &ctx->accumulator, 0 ) ) != 0 )
399         goto exit;
400     if( ( ret = mbedtls_sha512_update_ret( &ctx->accumulator, buf,
401                                            MBEDTLS_ENTROPY_BLOCK_SIZE ) ) != 0 )
402         goto exit;
403 
404     /*
405      * Perform second SHA-512 on entropy
406      */
407     if( ( ret = mbedtls_sha512_ret( buf, MBEDTLS_ENTROPY_BLOCK_SIZE,
408                                     buf, 0 ) ) != 0 )
409         goto exit;
410 #else /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
411     if( ( ret = mbedtls_sha256_finish_ret( &ctx->accumulator, buf ) ) != 0 )
412         goto exit;
413 
414     /*
415      * Reset accumulator and counters and recycle existing entropy
416      */
417     mbedtls_sha256_free( &ctx->accumulator );
418     mbedtls_sha256_init( &ctx->accumulator );
419     if( ( ret = mbedtls_sha256_starts_ret( &ctx->accumulator, 0 ) ) != 0 )
420         goto exit;
421     if( ( ret = mbedtls_sha256_update_ret( &ctx->accumulator, buf,
422                                            MBEDTLS_ENTROPY_BLOCK_SIZE ) ) != 0 )
423         goto exit;
424 
425     /*
426      * Perform second SHA-256 on entropy
427      */
428     if( ( ret = mbedtls_sha256_ret( buf, MBEDTLS_ENTROPY_BLOCK_SIZE,
429                                     buf, 0 ) ) != 0 )
430         goto exit;
431 #endif /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
432 
433     for( i = 0; i < ctx->source_count; i++ )
434         ctx->source[i].size = 0;
435 
436     memcpy( output, buf, len );
437 
438     ret = 0;
439 
440 exit:
441     mbedtls_platform_zeroize( buf, sizeof( buf ) );
442 
443 #if defined(MBEDTLS_THREADING_C)
444     if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
445         return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
446 #endif
447 
448     return( ret );
449 }
450 
451 #if defined(MBEDTLS_ENTROPY_NV_SEED)
452 int mbedtls_entropy_update_nv_seed( mbedtls_entropy_context *ctx )
453 {
454     int ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
455     unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
456 
457     /* Read new seed  and write it to NV */
458     if( ( ret = mbedtls_entropy_func( ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) ) != 0 )
459         return( ret );
460 
461     if( mbedtls_nv_seed_write( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) < 0 )
462         return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
463 
464     /* Manually update the remaining stream with a separator value to diverge */
465     memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
466     ret = mbedtls_entropy_update_manual( ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE );
467 
468     return( ret );
469 }
470 #endif /* MBEDTLS_ENTROPY_NV_SEED */
471 
472 #if defined(MBEDTLS_FS_IO)
473 int mbedtls_entropy_write_seed_file( mbedtls_entropy_context *ctx, const char *path )
474 {
475     int ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
476     FILE *f;
477     unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
478 
479     if( ( f = fopen( path, "wb" ) ) == NULL )
480         return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
481 
482     if( ( ret = mbedtls_entropy_func( ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) ) != 0 )
483         goto exit;
484 
485     if( fwrite( buf, 1, MBEDTLS_ENTROPY_BLOCK_SIZE, f ) != MBEDTLS_ENTROPY_BLOCK_SIZE )
486     {
487         ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
488         goto exit;
489     }
490 
491     ret = 0;
492 
493 exit:
494     mbedtls_platform_zeroize( buf, sizeof( buf ) );
495 
496     fclose( f );
497     return( ret );
498 }
499 
500 int mbedtls_entropy_update_seed_file( mbedtls_entropy_context *ctx, const char *path )
501 {
502     int ret = 0;
503     FILE *f;
504     size_t n;
505     unsigned char buf[ MBEDTLS_ENTROPY_MAX_SEED_SIZE ];
506 
507     if( ( f = fopen( path, "rb" ) ) == NULL )
508         return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
509 
510     fseek( f, 0, SEEK_END );
511     n = (size_t) ftell( f );
512     fseek( f, 0, SEEK_SET );
513 
514     if( n > MBEDTLS_ENTROPY_MAX_SEED_SIZE )
515         n = MBEDTLS_ENTROPY_MAX_SEED_SIZE;
516 
517     if( fread( buf, 1, n, f ) != n )
518         ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
519     else
520         ret = mbedtls_entropy_update_manual( ctx, buf, n );
521 
522     fclose( f );
523 
524     mbedtls_platform_zeroize( buf, sizeof( buf ) );
525 
526     if( ret != 0 )
527         return( ret );
528 
529     return( mbedtls_entropy_write_seed_file( ctx, path ) );
530 }
531 #endif /* MBEDTLS_FS_IO */
532 
533 #if defined(MBEDTLS_SELF_TEST)
534 #if !defined(MBEDTLS_TEST_NULL_ENTROPY)
535 /*
536  * Dummy source function
537  */
538 static int entropy_dummy_source( void *data, unsigned char *output,
539                                  size_t len, size_t *olen )
540 {
541     ((void) data);
542 
543     memset( output, 0x2a, len );
544     *olen = len;
545 
546     return( 0 );
547 }
548 #endif /* !MBEDTLS_TEST_NULL_ENTROPY */
549 
550 #if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
551 
552 static int mbedtls_entropy_source_self_test_gather( unsigned char *buf, size_t buf_len )
553 {
554     int ret = 0;
555     size_t entropy_len = 0;
556     size_t olen = 0;
557     size_t attempts = buf_len;
558 
559     while( attempts > 0 && entropy_len < buf_len )
560     {
561         if( ( ret = mbedtls_hardware_poll( NULL, buf + entropy_len,
562             buf_len - entropy_len, &olen ) ) != 0 )
563             return( ret );
564 
565         entropy_len += olen;
566         attempts--;
567     }
568 
569     if( entropy_len < buf_len )
570     {
571         ret = 1;
572     }
573 
574     return( ret );
575 }
576 
577 
578 static int mbedtls_entropy_source_self_test_check_bits( const unsigned char *buf,
579                                                         size_t buf_len )
580 {
581     unsigned char set= 0xFF;
582     unsigned char unset = 0x00;
583     size_t i;
584 
585     for( i = 0; i < buf_len; i++ )
586     {
587         set &= buf[i];
588         unset |= buf[i];
589     }
590 
591     return( set == 0xFF || unset == 0x00 );
592 }
593 
594 /*
595  * A test to ensure hat the entropy sources are functioning correctly
596  * and there is no obvious failure. The test performs the following checks:
597  *  - The entropy source is not providing only 0s (all bits unset) or 1s (all
598  *    bits set).
599  *  - The entropy source is not providing values in a pattern. Because the
600  *    hardware could be providing data in an arbitrary length, this check polls
601  *    the hardware entropy source twice and compares the result to ensure they
602  *    are not equal.
603  *  - The error code returned by the entropy source is not an error.
604  */
605 int mbedtls_entropy_source_self_test( int verbose )
606 {
607     int ret = 0;
608     unsigned char buf0[2 * sizeof( unsigned long long int )];
609     unsigned char buf1[2 * sizeof( unsigned long long int )];
610 
611     if( verbose != 0 )
612         mbedtls_printf( "  ENTROPY_BIAS test: " );
613 
614     memset( buf0, 0x00, sizeof( buf0 ) );
615     memset( buf1, 0x00, sizeof( buf1 ) );
616 
617     if( ( ret = mbedtls_entropy_source_self_test_gather( buf0, sizeof( buf0 ) ) ) != 0 )
618         goto cleanup;
619     if( ( ret = mbedtls_entropy_source_self_test_gather( buf1, sizeof( buf1 ) ) ) != 0 )
620         goto cleanup;
621 
622     /* Make sure that the returned values are not all 0 or 1 */
623     if( ( ret = mbedtls_entropy_source_self_test_check_bits( buf0, sizeof( buf0 ) ) ) != 0 )
624         goto cleanup;
625     if( ( ret = mbedtls_entropy_source_self_test_check_bits( buf1, sizeof( buf1 ) ) ) != 0 )
626         goto cleanup;
627 
628     /* Make sure that the entropy source is not returning values in a
629      * pattern */
630     ret = memcmp( buf0, buf1, sizeof( buf0 ) ) == 0;
631 
632 cleanup:
633     if( verbose != 0 )
634     {
635         if( ret != 0 )
636             mbedtls_printf( "failed\n" );
637         else
638             mbedtls_printf( "passed\n" );
639 
640         mbedtls_printf( "\n" );
641     }
642 
643     return( ret != 0 );
644 }
645 
646 #endif /* MBEDTLS_ENTROPY_HARDWARE_ALT */
647 
648 /*
649  * The actual entropy quality is hard to test, but we can at least
650  * test that the functions don't cause errors and write the correct
651  * amount of data to buffers.
652  */
653 int mbedtls_entropy_self_test( int verbose )
654 {
655     int ret = 1;
656 #if !defined(MBEDTLS_TEST_NULL_ENTROPY)
657     mbedtls_entropy_context ctx;
658     unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
659     unsigned char acc[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
660     size_t i, j;
661 #endif /* !MBEDTLS_TEST_NULL_ENTROPY */
662 
663     if( verbose != 0 )
664         mbedtls_printf( "  ENTROPY test: " );
665 
666 #if !defined(MBEDTLS_TEST_NULL_ENTROPY)
667     mbedtls_entropy_init( &ctx );
668 
669     /* First do a gather to make sure we have default sources */
670     if( ( ret = mbedtls_entropy_gather( &ctx ) ) != 0 )
671         goto cleanup;
672 
673     ret = mbedtls_entropy_add_source( &ctx, entropy_dummy_source, NULL, 16,
674                                       MBEDTLS_ENTROPY_SOURCE_WEAK );
675     if( ret != 0 )
676         goto cleanup;
677 
678     if( ( ret = mbedtls_entropy_update_manual( &ctx, buf, sizeof buf ) ) != 0 )
679         goto cleanup;
680 
681     /*
682      * To test that mbedtls_entropy_func writes correct number of bytes:
683      * - use the whole buffer and rely on ASan to detect overruns
684      * - collect entropy 8 times and OR the result in an accumulator:
685      *   any byte should then be 0 with probably 2^(-64), so requiring
686      *   each of the 32 or 64 bytes to be non-zero has a false failure rate
687      *   of at most 2^(-58) which is acceptable.
688      */
689     for( i = 0; i < 8; i++ )
690     {
691         if( ( ret = mbedtls_entropy_func( &ctx, buf, sizeof( buf ) ) ) != 0 )
692             goto cleanup;
693 
694         for( j = 0; j < sizeof( buf ); j++ )
695             acc[j] |= buf[j];
696     }
697 
698     for( j = 0; j < sizeof( buf ); j++ )
699     {
700         if( acc[j] == 0 )
701         {
702             ret = 1;
703             goto cleanup;
704         }
705     }
706 
707 #if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
708     if( ( ret = mbedtls_entropy_source_self_test( 0 ) ) != 0 )
709         goto cleanup;
710 #endif
711 
712 cleanup:
713     mbedtls_entropy_free( &ctx );
714 #endif /* !MBEDTLS_TEST_NULL_ENTROPY */
715 
716     if( verbose != 0 )
717     {
718         if( ret != 0 )
719             mbedtls_printf( "failed\n" );
720         else
721             mbedtls_printf( "passed\n" );
722 
723         mbedtls_printf( "\n" );
724     }
725 
726     return( ret != 0 );
727 }
728 #endif /* MBEDTLS_SELF_TEST */
729 
730 #endif /* MBEDTLS_ENTROPY_C */
731