blob: 88698962d1d8d154d9a1a4bb37300b61831f3a3b [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"
Chris Jonesea0a8652021-03-09 19:11:19 +00003#include "entropy_poll.h"
Gilles Peskine72d40fc2020-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
Gilles Peskineed04a672019-10-08 14:37:27 +02007typedef enum
8{
9 DUMMY_CONSTANT_LENGTH, /* Output context->length bytes */
10 DUMMY_REQUESTED_LENGTH, /* Output whatever length was requested */
11 DUMMY_FAIL, /* Return an error code */
12} entropy_dummy_instruction;
13
14typedef struct
15{
16 entropy_dummy_instruction instruction;
17 size_t length; /* Length to return for DUMMY_CONSTANT_LENGTH */
18 size_t calls; /* Incremented at each call */
19} entropy_dummy_context;
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +020020
21/*
22 * Dummy entropy source
23 *
24 * If data is NULL, write exactly the requested length.
25 * Otherwise, write the length indicated by data or error if negative
26 */
Gilles Peskineed04a672019-10-08 14:37:27 +020027static int entropy_dummy_source( void *arg, unsigned char *output,
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +020028 size_t len, size_t *olen )
29{
Gilles Peskineed04a672019-10-08 14:37:27 +020030 entropy_dummy_context *context = arg;
31 ++context->calls;
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +020032
Gilles Peskineed04a672019-10-08 14:37:27 +020033 switch( context->instruction )
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +020034 {
Gilles Peskineed04a672019-10-08 14:37:27 +020035 case DUMMY_CONSTANT_LENGTH:
36 *olen = context->length;
37 break;
38 case DUMMY_REQUESTED_LENGTH:
39 *olen = len;
40 break;
41 case DUMMY_FAIL:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +020043 }
44
45 memset( output, 0x2a, *olen );
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +020046 return( 0 );
47}
Paul Bakkerffbfb4c2016-06-01 15:36:18 +010048
49/*
50 * Ability to clear entropy sources to allow testing with just predefined
51 * entropy sources. This function or tests depending on it might break if there
52 * are internal changes to how entropy sources are registered.
53 *
54 * To be called immediately after mbedtls_entropy_init().
55 *
56 * Just resetting the counter. New sources will overwrite existing ones.
57 * This might break memory checks in the future if sources need 'free-ing' then
58 * as well.
59 */
Gilles Peskine7f246512019-10-08 14:51:49 +020060static void entropy_clear_sources( mbedtls_entropy_context *ctx )
Paul Bakkerffbfb4c2016-06-01 15:36:18 +010061{
62 ctx->source_count = 0;
63}
64
Gilles Peskine7f246512019-10-08 14:51:49 +020065#if defined(MBEDTLS_ENTROPY_NV_SEED)
Paul Bakkerffbfb4c2016-06-01 15:36:18 +010066/*
67 * NV seed read/write functions that use a buffer instead of a file
68 */
69static unsigned char buffer_seed[MBEDTLS_ENTROPY_BLOCK_SIZE];
70
Jaeden Amerof7dca862019-06-27 17:31:33 +010071int buffer_nv_seed_read( 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( buf, buffer_seed, MBEDTLS_ENTROPY_BLOCK_SIZE );
77 return( 0 );
78}
79
Jaeden Amerof7dca862019-06-27 17:31:33 +010080int buffer_nv_seed_write( unsigned char *buf, size_t buf_len )
Paul Bakkerffbfb4c2016-06-01 15:36:18 +010081{
82 if( buf_len != MBEDTLS_ENTROPY_BLOCK_SIZE )
83 return( -1 );
84
85 memcpy( buffer_seed, buf, MBEDTLS_ENTROPY_BLOCK_SIZE );
86 return( 0 );
87}
88
89/*
90 * NV seed read/write helpers that fill the base seedfile
91 */
92static int write_nv_seed( unsigned char *buf, size_t buf_len )
93{
94 FILE *f;
95
96 if( buf_len != MBEDTLS_ENTROPY_BLOCK_SIZE )
97 return( -1 );
98
99 if( ( f = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "w" ) ) == NULL )
100 return( -1 );
101
102 if( fwrite( buf, 1, MBEDTLS_ENTROPY_BLOCK_SIZE, f ) !=
103 MBEDTLS_ENTROPY_BLOCK_SIZE )
104 return( -1 );
105
106 fclose( f );
107
108 return( 0 );
109}
110
Jaeden Amerof7dca862019-06-27 17:31:33 +0100111int read_nv_seed( unsigned char *buf, size_t buf_len )
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100112{
113 FILE *f;
114
115 if( buf_len != MBEDTLS_ENTROPY_BLOCK_SIZE )
116 return( -1 );
117
118 if( ( f = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "rb" ) ) == NULL )
119 return( -1 );
120
121 if( fread( buf, 1, MBEDTLS_ENTROPY_BLOCK_SIZE, f ) !=
122 MBEDTLS_ENTROPY_BLOCK_SIZE )
123 return( -1 );
124
125 fclose( f );
126
127 return( 0 );
128}
Paul Bakker4a6c6fc2016-06-01 16:34:25 +0100129#endif /* MBEDTLS_ENTROPY_NV_SEED */
Manuel Pégourié-Gonnard2c25eb02014-05-30 10:38:18 +0200130/* END_HEADER */
131
132/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 * depends_on:MBEDTLS_ENTROPY_C
Manuel Pégourié-Gonnard2c25eb02014-05-30 10:38:18 +0200134 * END_DEPENDENCIES
135 */
136
Simon Butcherb7f45c52016-09-15 18:42:26 +0100137/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100138void entropy_seed_file( char * path, int ret )
Manuel Pégourié-Gonnard2c25eb02014-05-30 10:38:18 +0200139{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 mbedtls_entropy_context ctx;
Manuel Pégourié-Gonnard2c25eb02014-05-30 10:38:18 +0200141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 mbedtls_entropy_init( &ctx );
Manuel Pégourié-Gonnard2c25eb02014-05-30 10:38:18 +0200143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 TEST_ASSERT( mbedtls_entropy_write_seed_file( &ctx, path ) == ret );
145 TEST_ASSERT( mbedtls_entropy_update_seed_file( &ctx, path ) == ret );
Manuel Pégourié-Gonnard2c25eb02014-05-30 10:38:18 +0200146
Paul Bakkerbd51b262014-07-10 15:26:12 +0200147exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 mbedtls_entropy_free( &ctx );
Manuel Pégourié-Gonnard2c25eb02014-05-30 10:38:18 +0200149}
150/* END_CASE */
151
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200152/* BEGIN_CASE */
Gilles Peskine7f246512019-10-08 14:51:49 +0200153void entropy_no_sources( )
154{
155 mbedtls_entropy_context ctx;
156 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
157
158 mbedtls_entropy_init( &ctx );
159 entropy_clear_sources( &ctx );
160 TEST_EQUAL( mbedtls_entropy_func( &ctx, buf, sizeof( buf ) ),
161 MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED );
162
163exit:
164 mbedtls_entropy_free( &ctx );
165}
166/* END_CASE */
167
168/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100169void entropy_too_many_sources( )
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200170{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 mbedtls_entropy_context ctx;
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200172 size_t i;
Gilles Peskineed04a672019-10-08 14:37:27 +0200173 entropy_dummy_context dummy = {DUMMY_REQUESTED_LENGTH, 0, 0};
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 mbedtls_entropy_init( &ctx );
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200176
177 /*
178 * It's hard to tell precisely when the error will occur,
179 * since we don't know how many sources were automatically added.
180 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 for( i = 0; i < MBEDTLS_ENTROPY_MAX_SOURCES; i++ )
Gilles Peskineed04a672019-10-08 14:37:27 +0200182 (void) mbedtls_entropy_add_source( &ctx, entropy_dummy_source, &dummy,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200183 16, MBEDTLS_ENTROPY_SOURCE_WEAK );
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200184
Gilles Peskineed04a672019-10-08 14:37:27 +0200185 TEST_ASSERT( mbedtls_entropy_add_source( &ctx, entropy_dummy_source, &dummy,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200186 16, MBEDTLS_ENTROPY_SOURCE_WEAK )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 == MBEDTLS_ERR_ENTROPY_MAX_SOURCES );
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200188
Paul Bakkerbd51b262014-07-10 15:26:12 +0200189exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 mbedtls_entropy_free( &ctx );
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200191}
192/* END_CASE */
193
Hanno Beckerd4a872e2017-09-07 08:09:33 +0100194/* BEGIN_CASE depends_on:ENTROPY_HAVE_STRONG */
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200195void entropy_func_len( int len, int ret )
196{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 mbedtls_entropy_context ctx;
198 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE + 10] = { 0 };
199 unsigned char acc[MBEDTLS_ENTROPY_BLOCK_SIZE + 10] = { 0 };
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200200 size_t i, j;
201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 mbedtls_entropy_init( &ctx );
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200203
204 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 * See comments in mbedtls_entropy_self_test()
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200206 */
207 for( i = 0; i < 8; i++ )
208 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 TEST_ASSERT( mbedtls_entropy_func( &ctx, buf, len ) == ret );
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200210 for( j = 0; j < sizeof( buf ); j++ )
211 acc[j] |= buf[j];
212 }
213
214 if( ret == 0 )
215 for( j = 0; j < (size_t) len; j++ )
216 TEST_ASSERT( acc[j] != 0 );
217
218 for( j = len; j < sizeof( buf ); j++ )
219 TEST_ASSERT( acc[j] == 0 );
220}
221/* END_CASE */
222
223/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100224void entropy_source_fail( char * path )
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200225{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226 mbedtls_entropy_context ctx;
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200227 unsigned char buf[16];
Gilles Peskineed04a672019-10-08 14:37:27 +0200228 entropy_dummy_context dummy = {DUMMY_FAIL, 0, 0};
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 mbedtls_entropy_init( &ctx );
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200231
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200232 TEST_ASSERT( mbedtls_entropy_add_source( &ctx, entropy_dummy_source,
Gilles Peskineed04a672019-10-08 14:37:27 +0200233 &dummy, 16,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200234 MBEDTLS_ENTROPY_SOURCE_WEAK )
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200235 == 0 );
236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 TEST_ASSERT( mbedtls_entropy_func( &ctx, buf, sizeof( buf ) )
238 == MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
239 TEST_ASSERT( mbedtls_entropy_gather( &ctx )
240 == MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Simon Butcherb7f45c52016-09-15 18:42:26 +0100241#if defined(MBEDTLS_FS_IO) && defined(MBEDTLS_ENTROPY_NV_SEED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 TEST_ASSERT( mbedtls_entropy_write_seed_file( &ctx, path )
243 == MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
244 TEST_ASSERT( mbedtls_entropy_update_seed_file( &ctx, path )
245 == MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200246#else
247 ((void) path);
248#endif
249
Paul Bakkerbd51b262014-07-10 15:26:12 +0200250exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 mbedtls_entropy_free( &ctx );
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200252}
253/* END_CASE */
254
Gilles Peskinecbd91e02019-11-25 19:50:54 +0100255/* BEGIN_CASE */
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200256void entropy_threshold( int threshold, int chunk_size, int result )
257{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 mbedtls_entropy_context ctx;
Gilles Peskinecbd91e02019-11-25 19:50:54 +0100259 entropy_dummy_context strong =
260 {DUMMY_CONSTANT_LENGTH, MBEDTLS_ENTROPY_BLOCK_SIZE, 0};
261 entropy_dummy_context weak = {DUMMY_CONSTANT_LENGTH, chunk_size, 0};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200263 int ret;
264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 mbedtls_entropy_init( &ctx );
Gilles Peskinecbd91e02019-11-25 19:50:54 +0100266 entropy_clear_sources( &ctx );
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200267
Gilles Peskinecbd91e02019-11-25 19:50:54 +0100268 /* Set strong source that reaches its threshold immediately and
269 * a weak source whose threshold is a test parameter. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 TEST_ASSERT( mbedtls_entropy_add_source( &ctx, entropy_dummy_source,
Gilles Peskinecbd91e02019-11-25 19:50:54 +0100271 &strong, 1,
272 MBEDTLS_ENTROPY_SOURCE_STRONG ) == 0 );
273 TEST_ASSERT( mbedtls_entropy_add_source( &ctx, entropy_dummy_source,
274 &weak, threshold,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200275 MBEDTLS_ENTROPY_SOURCE_WEAK ) == 0 );
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 ret = mbedtls_entropy_func( &ctx, buf, sizeof( buf ) );
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200278
279 if( result >= 0 )
280 {
281 TEST_ASSERT( ret == 0 );
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100282#if defined(MBEDTLS_ENTROPY_NV_SEED)
Gilles Peskineae679392019-11-25 18:26:23 +0100283 /* If the NV seed functionality is enabled, there are two entropy
284 * updates: before and after updating the NV seed. */
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100285 result *= 2;
286#endif
Gilles Peskinecbd91e02019-11-25 19:50:54 +0100287 TEST_ASSERT( weak.calls == (size_t) result );
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200288 }
289 else
290 {
291 TEST_ASSERT( ret == result );
292 }
293
Paul Bakkerbd51b262014-07-10 15:26:12 +0200294exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 mbedtls_entropy_free( &ctx );
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200296}
297/* END_CASE */
298
Gilles Peskine65fc0682019-10-08 15:01:34 +0200299/* BEGIN_CASE */
300void entropy_calls( int strength1, int strength2,
301 int threshold, int chunk_size,
302 int result )
303{
304 /*
305 * if result >= 0: result = expected number of calls to source 1
306 * if result < 0: result = expected return code from mbedtls_entropy_func()
307 */
308
309 mbedtls_entropy_context ctx;
310 entropy_dummy_context dummy1 = {DUMMY_CONSTANT_LENGTH, chunk_size, 0};
311 entropy_dummy_context dummy2 = {DUMMY_CONSTANT_LENGTH, chunk_size, 0};
312 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
313 int ret;
314
315 mbedtls_entropy_init( &ctx );
316 entropy_clear_sources( &ctx );
317
318 TEST_ASSERT( mbedtls_entropy_add_source( &ctx, entropy_dummy_source,
319 &dummy1, threshold,
320 strength1 ) == 0 );
321 TEST_ASSERT( mbedtls_entropy_add_source( &ctx, entropy_dummy_source,
322 &dummy2, threshold,
323 strength2 ) == 0 );
324
325 ret = mbedtls_entropy_func( &ctx, buf, sizeof( buf ) );
326
327 if( result >= 0 )
328 {
329 TEST_ASSERT( ret == 0 );
Gilles Peskineae679392019-11-25 18:26:23 +0100330#if defined(MBEDTLS_ENTROPY_NV_SEED)
331 /* If the NV seed functionality is enabled, there are two entropy
332 * updates: before and after updating the NV seed. */
333 result *= 2;
334#endif
Gilles Peskine65fc0682019-10-08 15:01:34 +0200335 TEST_ASSERT( dummy1.calls == (size_t) result );
336 }
337 else
338 {
339 TEST_ASSERT( ret == result );
340 }
341
342exit:
343 mbedtls_entropy_free( &ctx );
344}
345/* END_CASE */
346
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100347/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100348void nv_seed_file_create( )
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100349{
350 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
351
352 memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
353
354 TEST_ASSERT( write_nv_seed( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
355}
356/* END_CASE */
357
Paul Bakkerb598c292016-06-01 16:57:11 +0100358/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO:MBEDTLS_PLATFORM_NV_SEED_ALT */
Azim Khanf1aaec92017-05-30 14:23:15 +0100359void entropy_nv_seed_std_io( )
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100360{
361 unsigned char io_seed[MBEDTLS_ENTROPY_BLOCK_SIZE];
362 unsigned char check_seed[MBEDTLS_ENTROPY_BLOCK_SIZE];
363
364 memset( io_seed, 1, MBEDTLS_ENTROPY_BLOCK_SIZE );
365 memset( check_seed, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
366
367 mbedtls_platform_set_nv_seed( mbedtls_platform_std_nv_seed_read,
368 mbedtls_platform_std_nv_seed_write );
369
370 /* Check if platform NV read and write manipulate the same data */
371 TEST_ASSERT( write_nv_seed( io_seed, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
372 TEST_ASSERT( mbedtls_nv_seed_read( check_seed, MBEDTLS_ENTROPY_BLOCK_SIZE ) ==
373 MBEDTLS_ENTROPY_BLOCK_SIZE );
374
375 TEST_ASSERT( memcmp( io_seed, check_seed, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
376
377 memset( check_seed, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
378
379 /* Check if platform NV write and raw read manipulate the same data */
380 TEST_ASSERT( mbedtls_nv_seed_write( io_seed, MBEDTLS_ENTROPY_BLOCK_SIZE ) ==
381 MBEDTLS_ENTROPY_BLOCK_SIZE );
382 TEST_ASSERT( read_nv_seed( check_seed, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
383
384 TEST_ASSERT( memcmp( io_seed, check_seed, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
385}
386/* END_CASE */
387
Gilles Peskine66afcca2019-06-12 19:33:42 +0200388/* BEGIN_CASE depends_on:MBEDTLS_MD_C:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_PLATFORM_NV_SEED_ALT */
Azim Khan5fcca462018-06-29 11:05:32 +0100389void entropy_nv_seed( data_t * read_seed )
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100390{
Gilles Peskine66afcca2019-06-12 19:33:42 +0200391#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
392 const mbedtls_md_info_t *md_info =
393 mbedtls_md_info_from_type( MBEDTLS_MD_SHA512 );
394#elif defined(MBEDTLS_ENTROPY_SHA256_ACCUMULATOR)
395 const mbedtls_md_info_t *md_info =
396 mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 );
397#else
398#error "Unsupported entropy accumulator"
399#endif
400 mbedtls_md_context_t accumulator;
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100401 mbedtls_entropy_context ctx;
Gilles Peskinee39b9032019-06-12 19:31:29 +0200402 int (*original_mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len ) =
403 mbedtls_nv_seed_read;
404 int (*original_mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len ) =
405 mbedtls_nv_seed_write;
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100406
407 unsigned char header[2];
408 unsigned char entropy[MBEDTLS_ENTROPY_BLOCK_SIZE];
409 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
410 unsigned char empty[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100411 unsigned char check_seed[MBEDTLS_ENTROPY_BLOCK_SIZE];
412 unsigned char check_entropy[MBEDTLS_ENTROPY_BLOCK_SIZE];
413
414 memset( entropy, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
415 memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100416 memset( empty, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
417 memset( check_seed, 2, MBEDTLS_ENTROPY_BLOCK_SIZE );
418 memset( check_entropy, 3, MBEDTLS_ENTROPY_BLOCK_SIZE );
419
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100420 // Make sure we read/write NV seed from our buffers
421 mbedtls_platform_set_nv_seed( buffer_nv_seed_read, buffer_nv_seed_write );
422
Gilles Peskine66afcca2019-06-12 19:33:42 +0200423 mbedtls_md_init( &accumulator );
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100424 mbedtls_entropy_init( &ctx );
425 entropy_clear_sources( &ctx );
426
427 TEST_ASSERT( mbedtls_entropy_add_source( &ctx, mbedtls_nv_seed_poll, NULL,
428 MBEDTLS_ENTROPY_BLOCK_SIZE,
429 MBEDTLS_ENTROPY_SOURCE_STRONG ) == 0 );
430
Gilles Peskine66afcca2019-06-12 19:33:42 +0200431 // Set the initial NV seed to read
432 TEST_ASSERT( read_seed->len >= MBEDTLS_ENTROPY_BLOCK_SIZE );
433 memcpy( buffer_seed, read_seed->x, MBEDTLS_ENTROPY_BLOCK_SIZE );
434
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100435 // Do an entropy run
436 TEST_ASSERT( mbedtls_entropy_func( &ctx, entropy, sizeof( entropy ) ) == 0 );
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100437 // Determine what should have happened with manual entropy internal logic
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100438
439 // Init accumulator
440 header[1] = MBEDTLS_ENTROPY_BLOCK_SIZE;
Gilles Peskine66afcca2019-06-12 19:33:42 +0200441 TEST_ASSERT( mbedtls_md_setup( &accumulator, md_info, 0 ) == 0 );
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100442
443 // First run for updating write_seed
444 header[0] = 0;
Gilles Peskine66afcca2019-06-12 19:33:42 +0200445 TEST_ASSERT( mbedtls_md_starts( &accumulator ) == 0 );
446 TEST_ASSERT( mbedtls_md_update( &accumulator, header, 2 ) == 0 );
447 TEST_ASSERT( mbedtls_md_update( &accumulator,
448 read_seed->x, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
449 TEST_ASSERT( mbedtls_md_finish( &accumulator, buf ) == 0 );
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100450
Gilles Peskine66afcca2019-06-12 19:33:42 +0200451 TEST_ASSERT( mbedtls_md_starts( &accumulator ) == 0 );
452 TEST_ASSERT( mbedtls_md_update( &accumulator,
453 buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100454
Gilles Peskine66afcca2019-06-12 19:33:42 +0200455 TEST_ASSERT( mbedtls_md( md_info, buf, MBEDTLS_ENTROPY_BLOCK_SIZE,
456 check_seed ) == 0 );
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100457
458 // Second run for actual entropy (triggers mbedtls_entropy_update_nv_seed)
459 header[0] = MBEDTLS_ENTROPY_SOURCE_MANUAL;
Gilles Peskine66afcca2019-06-12 19:33:42 +0200460 TEST_ASSERT( mbedtls_md_update( &accumulator, header, 2 ) == 0 );
461 TEST_ASSERT( mbedtls_md_update( &accumulator,
462 empty, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100463
464 header[0] = 0;
Gilles Peskine66afcca2019-06-12 19:33:42 +0200465 TEST_ASSERT( mbedtls_md_update( &accumulator, header, 2 ) == 0 );
466 TEST_ASSERT( mbedtls_md_update( &accumulator,
467 check_seed, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
468 TEST_ASSERT( mbedtls_md_finish( &accumulator, buf ) == 0 );
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100469
Gilles Peskine66afcca2019-06-12 19:33:42 +0200470 TEST_ASSERT( mbedtls_md( md_info, buf, MBEDTLS_ENTROPY_BLOCK_SIZE,
471 check_entropy ) == 0 );
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100472
473 // Check result of both NV file and entropy received with the manual calculations
474 TEST_ASSERT( memcmp( check_seed, buffer_seed, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
475 TEST_ASSERT( memcmp( check_entropy, entropy, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
476
Gilles Peskinee39b9032019-06-12 19:31:29 +0200477exit:
Gilles Peskine66afcca2019-06-12 19:33:42 +0200478 mbedtls_md_free( &accumulator );
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100479 mbedtls_entropy_free( &ctx );
Gilles Peskinee39b9032019-06-12 19:31:29 +0200480 mbedtls_nv_seed_read = original_mbedtls_nv_seed_read;
481 mbedtls_nv_seed_write = original_mbedtls_nv_seed_write;
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100482}
483/* END_CASE */
484
Hanno Beckerd4a872e2017-09-07 08:09:33 +0100485/* BEGIN_CASE depends_on:ENTROPY_HAVE_STRONG:MBEDTLS_SELF_TEST */
Simon Butcherb7f45c52016-09-15 18:42:26 +0100486void entropy_selftest( int result )
Manuel Pégourié-Gonnard2c25eb02014-05-30 10:38:18 +0200487{
Andres AG93012e82016-09-09 09:10:28 +0100488 TEST_ASSERT( mbedtls_entropy_self_test( 1 ) == result );
Manuel Pégourié-Gonnard2c25eb02014-05-30 10:38:18 +0200489}
490/* END_CASE */