blob: c275257ba2dc3924b0a6521776bb4805656402e0 [file] [log] [blame]
Manuel Pégourié-Gonnard2c25eb02014-05-30 10:38:18 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include "mbedtls/entropy.h"
Paul Bakkerffbfb4c2016-06-01 15:36:18 +01003#include "mbedtls/entropy_poll.h"
Gilles Peskine1e557b72020-04-14 21:28:42 +02004#include "mbedtls/md.h"
Mohammad Azim Khan67735d52017-04-06 11:55:43 +01005#include "string.h"
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +02006
7/*
8 * Number of calls made to entropy_dummy_source()
9 */
10static size_t entropy_dummy_calls;
11
12/*
13 * Dummy entropy source
14 *
15 * If data is NULL, write exactly the requested length.
16 * Otherwise, write the length indicated by data or error if negative
17 */
18static int entropy_dummy_source( void *data, unsigned char *output,
19 size_t len, size_t *olen )
20{
21 entropy_dummy_calls++;
22
23 if( data == NULL )
24 *olen = len;
25 else
26 {
27 int *d = (int *) data;
28
29 if( *d < 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +020031 else
32 *olen = *d;
33 }
34
35 memset( output, 0x2a, *olen );
36
37 return( 0 );
38}
Paul Bakkerffbfb4c2016-06-01 15:36:18 +010039
Paul Bakker4a6c6fc2016-06-01 16:34:25 +010040#if defined(MBEDTLS_ENTROPY_NV_SEED)
Paul Bakkerffbfb4c2016-06-01 15:36:18 +010041/*
42 * Ability to clear entropy sources to allow testing with just predefined
43 * entropy sources. This function or tests depending on it might break if there
44 * are internal changes to how entropy sources are registered.
45 *
46 * To be called immediately after mbedtls_entropy_init().
47 *
48 * Just resetting the counter. New sources will overwrite existing ones.
49 * This might break memory checks in the future if sources need 'free-ing' then
50 * as well.
51 */
Jaeden Amerof580d432019-06-21 12:56:05 +010052void entropy_clear_sources( mbedtls_entropy_context *ctx )
Paul Bakkerffbfb4c2016-06-01 15:36:18 +010053{
54 ctx->source_count = 0;
55}
56
57/*
58 * NV seed read/write functions that use a buffer instead of a file
59 */
60static unsigned char buffer_seed[MBEDTLS_ENTROPY_BLOCK_SIZE];
61
Jaeden Amerof580d432019-06-21 12:56:05 +010062int buffer_nv_seed_read( unsigned char *buf, size_t buf_len )
Paul Bakkerffbfb4c2016-06-01 15:36:18 +010063{
64 if( buf_len != MBEDTLS_ENTROPY_BLOCK_SIZE )
65 return( -1 );
66
67 memcpy( buf, buffer_seed, MBEDTLS_ENTROPY_BLOCK_SIZE );
68 return( 0 );
69}
70
Jaeden Amerof580d432019-06-21 12:56:05 +010071int buffer_nv_seed_write( unsigned char *buf, size_t buf_len )
Paul Bakkerffbfb4c2016-06-01 15:36:18 +010072{
73 if( buf_len != MBEDTLS_ENTROPY_BLOCK_SIZE )
74 return( -1 );
75
76 memcpy( buffer_seed, buf, MBEDTLS_ENTROPY_BLOCK_SIZE );
77 return( 0 );
78}
79
80/*
81 * NV seed read/write helpers that fill the base seedfile
82 */
Jaeden Amerof580d432019-06-21 12:56:05 +010083int write_nv_seed( unsigned char *buf, size_t buf_len )
Paul Bakkerffbfb4c2016-06-01 15:36:18 +010084{
85 FILE *f;
86
87 if( buf_len != MBEDTLS_ENTROPY_BLOCK_SIZE )
88 return( -1 );
89
90 if( ( f = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "w" ) ) == NULL )
91 return( -1 );
92
93 if( fwrite( buf, 1, MBEDTLS_ENTROPY_BLOCK_SIZE, f ) !=
94 MBEDTLS_ENTROPY_BLOCK_SIZE )
95 return( -1 );
96
97 fclose( f );
98
99 return( 0 );
100}
101
Jaeden Amerof580d432019-06-21 12:56:05 +0100102int read_nv_seed( unsigned char *buf, size_t buf_len )
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100103{
104 FILE *f;
105
106 if( buf_len != MBEDTLS_ENTROPY_BLOCK_SIZE )
107 return( -1 );
108
109 if( ( f = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "rb" ) ) == NULL )
110 return( -1 );
111
112 if( fread( buf, 1, MBEDTLS_ENTROPY_BLOCK_SIZE, f ) !=
113 MBEDTLS_ENTROPY_BLOCK_SIZE )
114 return( -1 );
115
116 fclose( f );
117
118 return( 0 );
119}
Paul Bakker4a6c6fc2016-06-01 16:34:25 +0100120#endif /* MBEDTLS_ENTROPY_NV_SEED */
Manuel Pégourié-Gonnard2c25eb02014-05-30 10:38:18 +0200121/* END_HEADER */
122
123/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 * depends_on:MBEDTLS_ENTROPY_C
Manuel Pégourié-Gonnard2c25eb02014-05-30 10:38:18 +0200125 * END_DEPENDENCIES
126 */
127
Simon Butcherb7f45c52016-09-15 18:42:26 +0100128/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100129void entropy_seed_file( char * path, int ret )
Manuel Pégourié-Gonnard2c25eb02014-05-30 10:38:18 +0200130{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 mbedtls_entropy_context ctx;
Manuel Pégourié-Gonnard2c25eb02014-05-30 10:38:18 +0200132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 mbedtls_entropy_init( &ctx );
Manuel Pégourié-Gonnard2c25eb02014-05-30 10:38:18 +0200134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 TEST_ASSERT( mbedtls_entropy_write_seed_file( &ctx, path ) == ret );
136 TEST_ASSERT( mbedtls_entropy_update_seed_file( &ctx, path ) == ret );
Manuel Pégourié-Gonnard2c25eb02014-05-30 10:38:18 +0200137
Paul Bakkerbd51b262014-07-10 15:26:12 +0200138exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 mbedtls_entropy_free( &ctx );
Manuel Pégourié-Gonnard2c25eb02014-05-30 10:38:18 +0200140}
141/* END_CASE */
142
Victor Krasnoshchok12b89cb2020-08-27 00:19:55 +0300143/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
144void entropy_write_base_seed_file( int ret )
145{
146 mbedtls_entropy_context ctx;
147
148 mbedtls_entropy_init( &ctx );
149
150 TEST_ASSERT( mbedtls_entropy_write_seed_file( &ctx, MBEDTLS_PLATFORM_STD_NV_SEED_FILE ) == ret );
151 TEST_ASSERT( mbedtls_entropy_update_seed_file( &ctx, MBEDTLS_PLATFORM_STD_NV_SEED_FILE ) == ret );
152
153exit:
154 mbedtls_entropy_free( &ctx );
155}
156/* END_CASE */
157
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200158/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100159void entropy_too_many_sources( )
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200160{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 mbedtls_entropy_context ctx;
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200162 size_t i;
163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 mbedtls_entropy_init( &ctx );
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200165
166 /*
167 * It's hard to tell precisely when the error will occur,
168 * since we don't know how many sources were automatically added.
169 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 for( i = 0; i < MBEDTLS_ENTROPY_MAX_SOURCES; i++ )
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200171 (void) mbedtls_entropy_add_source( &ctx, entropy_dummy_source, NULL,
172 16, MBEDTLS_ENTROPY_SOURCE_WEAK );
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200173
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200174 TEST_ASSERT( mbedtls_entropy_add_source( &ctx, entropy_dummy_source, NULL,
175 16, MBEDTLS_ENTROPY_SOURCE_WEAK )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 == MBEDTLS_ERR_ENTROPY_MAX_SOURCES );
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200177
Paul Bakkerbd51b262014-07-10 15:26:12 +0200178exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 mbedtls_entropy_free( &ctx );
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200180}
181/* END_CASE */
182
Hanno Beckerd4a872e2017-09-07 08:09:33 +0100183/* BEGIN_CASE depends_on:ENTROPY_HAVE_STRONG */
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200184void entropy_func_len( int len, int ret )
185{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 mbedtls_entropy_context ctx;
187 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE + 10] = { 0 };
188 unsigned char acc[MBEDTLS_ENTROPY_BLOCK_SIZE + 10] = { 0 };
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200189 size_t i, j;
190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 mbedtls_entropy_init( &ctx );
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200192
193 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 * See comments in mbedtls_entropy_self_test()
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200195 */
196 for( i = 0; i < 8; i++ )
197 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 TEST_ASSERT( mbedtls_entropy_func( &ctx, buf, len ) == ret );
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200199 for( j = 0; j < sizeof( buf ); j++ )
200 acc[j] |= buf[j];
201 }
202
203 if( ret == 0 )
204 for( j = 0; j < (size_t) len; j++ )
205 TEST_ASSERT( acc[j] != 0 );
206
207 for( j = len; j < sizeof( buf ); j++ )
208 TEST_ASSERT( acc[j] == 0 );
209}
210/* END_CASE */
211
212/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100213void entropy_source_fail( char * path )
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200214{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 mbedtls_entropy_context ctx;
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200216 int fail = -1;
217 unsigned char buf[16];
218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219 mbedtls_entropy_init( &ctx );
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200220
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200221 TEST_ASSERT( mbedtls_entropy_add_source( &ctx, entropy_dummy_source,
222 &fail, 16,
223 MBEDTLS_ENTROPY_SOURCE_WEAK )
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200224 == 0 );
225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226 TEST_ASSERT( mbedtls_entropy_func( &ctx, buf, sizeof( buf ) )
227 == MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
228 TEST_ASSERT( mbedtls_entropy_gather( &ctx )
229 == MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Simon Butcherb7f45c52016-09-15 18:42:26 +0100230#if defined(MBEDTLS_FS_IO) && defined(MBEDTLS_ENTROPY_NV_SEED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231 TEST_ASSERT( mbedtls_entropy_write_seed_file( &ctx, path )
232 == MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
233 TEST_ASSERT( mbedtls_entropy_update_seed_file( &ctx, path )
234 == MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200235#else
236 ((void) path);
237#endif
238
Paul Bakkerbd51b262014-07-10 15:26:12 +0200239exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 mbedtls_entropy_free( &ctx );
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200241}
242/* END_CASE */
243
Hanno Beckerd4a872e2017-09-07 08:09:33 +0100244/* BEGIN_CASE depends_on:ENTROPY_HAVE_STRONG */
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200245void entropy_threshold( int threshold, int chunk_size, int result )
246{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247 mbedtls_entropy_context ctx;
248 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200249 int ret;
250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 mbedtls_entropy_init( &ctx );
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 TEST_ASSERT( mbedtls_entropy_add_source( &ctx, entropy_dummy_source,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200254 &chunk_size, threshold,
255 MBEDTLS_ENTROPY_SOURCE_WEAK ) == 0 );
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200256
257 entropy_dummy_calls = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 ret = mbedtls_entropy_func( &ctx, buf, sizeof( buf ) );
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200259
260 if( result >= 0 )
261 {
262 TEST_ASSERT( ret == 0 );
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100263#if defined(MBEDTLS_ENTROPY_NV_SEED)
264 // Two times as much calls due to the NV seed update
265 result *= 2;
266#endif
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200267 TEST_ASSERT( entropy_dummy_calls == (size_t) result );
268 }
269 else
270 {
271 TEST_ASSERT( ret == result );
272 }
273
Paul Bakkerbd51b262014-07-10 15:26:12 +0200274exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275 mbedtls_entropy_free( &ctx );
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200276}
277/* END_CASE */
278
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100279/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100280void nv_seed_file_create( )
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100281{
282 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
283
284 memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
285
286 TEST_ASSERT( write_nv_seed( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
287}
288/* END_CASE */
289
Paul Bakkerb598c292016-06-01 16:57:11 +0100290/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO:MBEDTLS_PLATFORM_NV_SEED_ALT */
Azim Khanf1aaec92017-05-30 14:23:15 +0100291void entropy_nv_seed_std_io( )
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100292{
293 unsigned char io_seed[MBEDTLS_ENTROPY_BLOCK_SIZE];
294 unsigned char check_seed[MBEDTLS_ENTROPY_BLOCK_SIZE];
295
296 memset( io_seed, 1, MBEDTLS_ENTROPY_BLOCK_SIZE );
297 memset( check_seed, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
298
299 mbedtls_platform_set_nv_seed( mbedtls_platform_std_nv_seed_read,
300 mbedtls_platform_std_nv_seed_write );
301
302 /* Check if platform NV read and write manipulate the same data */
303 TEST_ASSERT( write_nv_seed( io_seed, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
304 TEST_ASSERT( mbedtls_nv_seed_read( check_seed, MBEDTLS_ENTROPY_BLOCK_SIZE ) ==
305 MBEDTLS_ENTROPY_BLOCK_SIZE );
306
307 TEST_ASSERT( memcmp( io_seed, check_seed, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
308
309 memset( check_seed, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
310
311 /* Check if platform NV write and raw read manipulate the same data */
312 TEST_ASSERT( mbedtls_nv_seed_write( io_seed, MBEDTLS_ENTROPY_BLOCK_SIZE ) ==
313 MBEDTLS_ENTROPY_BLOCK_SIZE );
314 TEST_ASSERT( read_nv_seed( check_seed, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
315
316 TEST_ASSERT( memcmp( io_seed, check_seed, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
317}
318/* END_CASE */
319
Gilles Peskine756b3f22019-06-12 19:33:42 +0200320/* BEGIN_CASE depends_on:MBEDTLS_MD_C:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_PLATFORM_NV_SEED_ALT */
Azim Khan5fcca462018-06-29 11:05:32 +0100321void entropy_nv_seed( data_t * read_seed )
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100322{
Gilles Peskine756b3f22019-06-12 19:33:42 +0200323#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
324 const mbedtls_md_info_t *md_info =
325 mbedtls_md_info_from_type( MBEDTLS_MD_SHA512 );
326#elif defined(MBEDTLS_ENTROPY_SHA256_ACCUMULATOR)
327 const mbedtls_md_info_t *md_info =
328 mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 );
329#else
330#error "Unsupported entropy accumulator"
331#endif
332 mbedtls_md_context_t accumulator;
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100333 mbedtls_entropy_context ctx;
Gilles Peskine0450eec2019-06-12 19:31:29 +0200334 int (*original_mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len ) =
335 mbedtls_nv_seed_read;
336 int (*original_mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len ) =
337 mbedtls_nv_seed_write;
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100338
339 unsigned char header[2];
340 unsigned char entropy[MBEDTLS_ENTROPY_BLOCK_SIZE];
341 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
342 unsigned char empty[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100343 unsigned char check_seed[MBEDTLS_ENTROPY_BLOCK_SIZE];
344 unsigned char check_entropy[MBEDTLS_ENTROPY_BLOCK_SIZE];
345
346 memset( entropy, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
347 memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100348 memset( empty, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
349 memset( check_seed, 2, MBEDTLS_ENTROPY_BLOCK_SIZE );
350 memset( check_entropy, 3, MBEDTLS_ENTROPY_BLOCK_SIZE );
351
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100352 // Make sure we read/write NV seed from our buffers
353 mbedtls_platform_set_nv_seed( buffer_nv_seed_read, buffer_nv_seed_write );
354
Gilles Peskine756b3f22019-06-12 19:33:42 +0200355 mbedtls_md_init( &accumulator );
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100356 mbedtls_entropy_init( &ctx );
357 entropy_clear_sources( &ctx );
358
359 TEST_ASSERT( mbedtls_entropy_add_source( &ctx, mbedtls_nv_seed_poll, NULL,
360 MBEDTLS_ENTROPY_BLOCK_SIZE,
361 MBEDTLS_ENTROPY_SOURCE_STRONG ) == 0 );
362
Gilles Peskine756b3f22019-06-12 19:33:42 +0200363 // Set the initial NV seed to read
364 TEST_ASSERT( read_seed->len >= MBEDTLS_ENTROPY_BLOCK_SIZE );
365 memcpy( buffer_seed, read_seed->x, MBEDTLS_ENTROPY_BLOCK_SIZE );
366
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100367 // Do an entropy run
368 TEST_ASSERT( mbedtls_entropy_func( &ctx, entropy, sizeof( entropy ) ) == 0 );
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100369 // Determine what should have happened with manual entropy internal logic
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100370
371 // Init accumulator
372 header[1] = MBEDTLS_ENTROPY_BLOCK_SIZE;
Gilles Peskine756b3f22019-06-12 19:33:42 +0200373 TEST_ASSERT( mbedtls_md_setup( &accumulator, md_info, 0 ) == 0 );
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100374
375 // First run for updating write_seed
376 header[0] = 0;
Gilles Peskine756b3f22019-06-12 19:33:42 +0200377 TEST_ASSERT( mbedtls_md_starts( &accumulator ) == 0 );
378 TEST_ASSERT( mbedtls_md_update( &accumulator, header, 2 ) == 0 );
379 TEST_ASSERT( mbedtls_md_update( &accumulator,
380 read_seed->x, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
381 TEST_ASSERT( mbedtls_md_finish( &accumulator, buf ) == 0 );
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100382
Gilles Peskine756b3f22019-06-12 19:33:42 +0200383 TEST_ASSERT( mbedtls_md_starts( &accumulator ) == 0 );
384 TEST_ASSERT( mbedtls_md_update( &accumulator,
385 buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100386
Gilles Peskine756b3f22019-06-12 19:33:42 +0200387 TEST_ASSERT( mbedtls_md( md_info, buf, MBEDTLS_ENTROPY_BLOCK_SIZE,
388 check_seed ) == 0 );
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100389
390 // Second run for actual entropy (triggers mbedtls_entropy_update_nv_seed)
391 header[0] = MBEDTLS_ENTROPY_SOURCE_MANUAL;
Gilles Peskine756b3f22019-06-12 19:33:42 +0200392 TEST_ASSERT( mbedtls_md_update( &accumulator, header, 2 ) == 0 );
393 TEST_ASSERT( mbedtls_md_update( &accumulator,
394 empty, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100395
396 header[0] = 0;
Gilles Peskine756b3f22019-06-12 19:33:42 +0200397 TEST_ASSERT( mbedtls_md_update( &accumulator, header, 2 ) == 0 );
398 TEST_ASSERT( mbedtls_md_update( &accumulator,
399 check_seed, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
400 TEST_ASSERT( mbedtls_md_finish( &accumulator, buf ) == 0 );
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100401
Gilles Peskine756b3f22019-06-12 19:33:42 +0200402 TEST_ASSERT( mbedtls_md( md_info, buf, MBEDTLS_ENTROPY_BLOCK_SIZE,
403 check_entropy ) == 0 );
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100404
405 // Check result of both NV file and entropy received with the manual calculations
406 TEST_ASSERT( memcmp( check_seed, buffer_seed, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
407 TEST_ASSERT( memcmp( check_entropy, entropy, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
408
Gilles Peskine0450eec2019-06-12 19:31:29 +0200409exit:
Gilles Peskine756b3f22019-06-12 19:33:42 +0200410 mbedtls_md_free( &accumulator );
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100411 mbedtls_entropy_free( &ctx );
Gilles Peskine0450eec2019-06-12 19:31:29 +0200412 mbedtls_nv_seed_read = original_mbedtls_nv_seed_read;
413 mbedtls_nv_seed_write = original_mbedtls_nv_seed_write;
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100414}
415/* END_CASE */
416
Hanno Beckerd4a872e2017-09-07 08:09:33 +0100417/* BEGIN_CASE depends_on:ENTROPY_HAVE_STRONG:MBEDTLS_SELF_TEST */
Simon Butcherb7f45c52016-09-15 18:42:26 +0100418void entropy_selftest( int result )
Manuel Pégourié-Gonnard2c25eb02014-05-30 10:38:18 +0200419{
Andres AG93012e82016-09-09 09:10:28 +0100420 TEST_ASSERT( mbedtls_entropy_self_test( 1 ) == result );
Manuel Pégourié-Gonnard2c25eb02014-05-30 10:38:18 +0200421}
422/* END_CASE */