blob: 2a7b0de135adc269a29fee18dd431fbf04137a9e [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 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 );
Gilles Peskine7aba0362021-01-31 00:07:11 +0100220
221exit:
222 mbedtls_entropy_free( &ctx );
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200223}
224/* END_CASE */
225
226/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100227void entropy_source_fail( char * path )
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200228{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 mbedtls_entropy_context ctx;
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200230 unsigned char buf[16];
Gilles Peskineed04a672019-10-08 14:37:27 +0200231 entropy_dummy_context dummy = {DUMMY_FAIL, 0, 0};
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 mbedtls_entropy_init( &ctx );
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200234
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200235 TEST_ASSERT( mbedtls_entropy_add_source( &ctx, entropy_dummy_source,
Gilles Peskineed04a672019-10-08 14:37:27 +0200236 &dummy, 16,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200237 MBEDTLS_ENTROPY_SOURCE_WEAK )
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200238 == 0 );
239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 TEST_ASSERT( mbedtls_entropy_func( &ctx, buf, sizeof( buf ) )
241 == MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
242 TEST_ASSERT( mbedtls_entropy_gather( &ctx )
243 == MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Simon Butcherb7f45c52016-09-15 18:42:26 +0100244#if defined(MBEDTLS_FS_IO) && defined(MBEDTLS_ENTROPY_NV_SEED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 TEST_ASSERT( mbedtls_entropy_write_seed_file( &ctx, path )
246 == MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
247 TEST_ASSERT( mbedtls_entropy_update_seed_file( &ctx, path )
248 == MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200249#else
250 ((void) path);
251#endif
252
Paul Bakkerbd51b262014-07-10 15:26:12 +0200253exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 mbedtls_entropy_free( &ctx );
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200255}
256/* END_CASE */
257
Gilles Peskinecbd91e02019-11-25 19:50:54 +0100258/* BEGIN_CASE */
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200259void entropy_threshold( int threshold, int chunk_size, int result )
260{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 mbedtls_entropy_context ctx;
Gilles Peskinecbd91e02019-11-25 19:50:54 +0100262 entropy_dummy_context strong =
263 {DUMMY_CONSTANT_LENGTH, MBEDTLS_ENTROPY_BLOCK_SIZE, 0};
264 entropy_dummy_context weak = {DUMMY_CONSTANT_LENGTH, chunk_size, 0};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200266 int ret;
267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 mbedtls_entropy_init( &ctx );
Gilles Peskinecbd91e02019-11-25 19:50:54 +0100269 entropy_clear_sources( &ctx );
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200270
Gilles Peskinecbd91e02019-11-25 19:50:54 +0100271 /* Set strong source that reaches its threshold immediately and
272 * a weak source whose threshold is a test parameter. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 TEST_ASSERT( mbedtls_entropy_add_source( &ctx, entropy_dummy_source,
Gilles Peskinecbd91e02019-11-25 19:50:54 +0100274 &strong, 1,
275 MBEDTLS_ENTROPY_SOURCE_STRONG ) == 0 );
276 TEST_ASSERT( mbedtls_entropy_add_source( &ctx, entropy_dummy_source,
277 &weak, threshold,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200278 MBEDTLS_ENTROPY_SOURCE_WEAK ) == 0 );
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 ret = mbedtls_entropy_func( &ctx, buf, sizeof( buf ) );
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200281
282 if( result >= 0 )
283 {
284 TEST_ASSERT( ret == 0 );
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100285#if defined(MBEDTLS_ENTROPY_NV_SEED)
Gilles Peskineae679392019-11-25 18:26:23 +0100286 /* If the NV seed functionality is enabled, there are two entropy
287 * updates: before and after updating the NV seed. */
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100288 result *= 2;
289#endif
Gilles Peskinecbd91e02019-11-25 19:50:54 +0100290 TEST_ASSERT( weak.calls == (size_t) result );
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200291 }
292 else
293 {
294 TEST_ASSERT( ret == result );
295 }
296
Paul Bakkerbd51b262014-07-10 15:26:12 +0200297exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 mbedtls_entropy_free( &ctx );
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200299}
300/* END_CASE */
301
Gilles Peskine65fc0682019-10-08 15:01:34 +0200302/* BEGIN_CASE */
303void entropy_calls( int strength1, int strength2,
304 int threshold, int chunk_size,
305 int result )
306{
307 /*
308 * if result >= 0: result = expected number of calls to source 1
309 * if result < 0: result = expected return code from mbedtls_entropy_func()
310 */
311
312 mbedtls_entropy_context ctx;
313 entropy_dummy_context dummy1 = {DUMMY_CONSTANT_LENGTH, chunk_size, 0};
314 entropy_dummy_context dummy2 = {DUMMY_CONSTANT_LENGTH, chunk_size, 0};
315 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
316 int ret;
317
318 mbedtls_entropy_init( &ctx );
319 entropy_clear_sources( &ctx );
320
321 TEST_ASSERT( mbedtls_entropy_add_source( &ctx, entropy_dummy_source,
322 &dummy1, threshold,
323 strength1 ) == 0 );
324 TEST_ASSERT( mbedtls_entropy_add_source( &ctx, entropy_dummy_source,
325 &dummy2, threshold,
326 strength2 ) == 0 );
327
328 ret = mbedtls_entropy_func( &ctx, buf, sizeof( buf ) );
329
330 if( result >= 0 )
331 {
332 TEST_ASSERT( ret == 0 );
Gilles Peskineae679392019-11-25 18:26:23 +0100333#if defined(MBEDTLS_ENTROPY_NV_SEED)
334 /* If the NV seed functionality is enabled, there are two entropy
335 * updates: before and after updating the NV seed. */
336 result *= 2;
337#endif
Gilles Peskine65fc0682019-10-08 15:01:34 +0200338 TEST_ASSERT( dummy1.calls == (size_t) result );
339 }
340 else
341 {
342 TEST_ASSERT( ret == result );
343 }
344
345exit:
346 mbedtls_entropy_free( &ctx );
347}
348/* END_CASE */
349
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100350/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100351void nv_seed_file_create( )
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100352{
353 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
354
355 memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
356
357 TEST_ASSERT( write_nv_seed( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
358}
359/* END_CASE */
360
Paul Bakkerb598c292016-06-01 16:57:11 +0100361/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO:MBEDTLS_PLATFORM_NV_SEED_ALT */
Azim Khanf1aaec92017-05-30 14:23:15 +0100362void entropy_nv_seed_std_io( )
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100363{
364 unsigned char io_seed[MBEDTLS_ENTROPY_BLOCK_SIZE];
365 unsigned char check_seed[MBEDTLS_ENTROPY_BLOCK_SIZE];
366
367 memset( io_seed, 1, MBEDTLS_ENTROPY_BLOCK_SIZE );
368 memset( check_seed, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
369
370 mbedtls_platform_set_nv_seed( mbedtls_platform_std_nv_seed_read,
371 mbedtls_platform_std_nv_seed_write );
372
373 /* Check if platform NV read and write manipulate the same data */
374 TEST_ASSERT( write_nv_seed( io_seed, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
375 TEST_ASSERT( mbedtls_nv_seed_read( check_seed, MBEDTLS_ENTROPY_BLOCK_SIZE ) ==
376 MBEDTLS_ENTROPY_BLOCK_SIZE );
377
378 TEST_ASSERT( memcmp( io_seed, check_seed, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
379
380 memset( check_seed, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
381
382 /* Check if platform NV write and raw read manipulate the same data */
383 TEST_ASSERT( mbedtls_nv_seed_write( io_seed, MBEDTLS_ENTROPY_BLOCK_SIZE ) ==
384 MBEDTLS_ENTROPY_BLOCK_SIZE );
385 TEST_ASSERT( read_nv_seed( check_seed, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
386
387 TEST_ASSERT( memcmp( io_seed, check_seed, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
388}
389/* END_CASE */
390
Gilles Peskine66afcca2019-06-12 19:33:42 +0200391/* BEGIN_CASE depends_on:MBEDTLS_MD_C:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_PLATFORM_NV_SEED_ALT */
Azim Khan5fcca462018-06-29 11:05:32 +0100392void entropy_nv_seed( data_t * read_seed )
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100393{
Gilles Peskine66afcca2019-06-12 19:33:42 +0200394#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
395 const mbedtls_md_info_t *md_info =
396 mbedtls_md_info_from_type( MBEDTLS_MD_SHA512 );
397#elif defined(MBEDTLS_ENTROPY_SHA256_ACCUMULATOR)
398 const mbedtls_md_info_t *md_info =
399 mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 );
400#else
401#error "Unsupported entropy accumulator"
402#endif
403 mbedtls_md_context_t accumulator;
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100404 mbedtls_entropy_context ctx;
Gilles Peskinee39b9032019-06-12 19:31:29 +0200405 int (*original_mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len ) =
406 mbedtls_nv_seed_read;
407 int (*original_mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len ) =
408 mbedtls_nv_seed_write;
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100409
410 unsigned char header[2];
411 unsigned char entropy[MBEDTLS_ENTROPY_BLOCK_SIZE];
412 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
413 unsigned char empty[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100414 unsigned char check_seed[MBEDTLS_ENTROPY_BLOCK_SIZE];
415 unsigned char check_entropy[MBEDTLS_ENTROPY_BLOCK_SIZE];
416
417 memset( entropy, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
418 memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100419 memset( empty, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
420 memset( check_seed, 2, MBEDTLS_ENTROPY_BLOCK_SIZE );
421 memset( check_entropy, 3, MBEDTLS_ENTROPY_BLOCK_SIZE );
422
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100423 // Make sure we read/write NV seed from our buffers
424 mbedtls_platform_set_nv_seed( buffer_nv_seed_read, buffer_nv_seed_write );
425
Gilles Peskine66afcca2019-06-12 19:33:42 +0200426 mbedtls_md_init( &accumulator );
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100427 mbedtls_entropy_init( &ctx );
428 entropy_clear_sources( &ctx );
429
430 TEST_ASSERT( mbedtls_entropy_add_source( &ctx, mbedtls_nv_seed_poll, NULL,
431 MBEDTLS_ENTROPY_BLOCK_SIZE,
432 MBEDTLS_ENTROPY_SOURCE_STRONG ) == 0 );
433
Gilles Peskine66afcca2019-06-12 19:33:42 +0200434 // Set the initial NV seed to read
435 TEST_ASSERT( read_seed->len >= MBEDTLS_ENTROPY_BLOCK_SIZE );
436 memcpy( buffer_seed, read_seed->x, MBEDTLS_ENTROPY_BLOCK_SIZE );
437
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100438 // Do an entropy run
439 TEST_ASSERT( mbedtls_entropy_func( &ctx, entropy, sizeof( entropy ) ) == 0 );
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100440 // Determine what should have happened with manual entropy internal logic
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100441
442 // Init accumulator
443 header[1] = MBEDTLS_ENTROPY_BLOCK_SIZE;
Gilles Peskine66afcca2019-06-12 19:33:42 +0200444 TEST_ASSERT( mbedtls_md_setup( &accumulator, md_info, 0 ) == 0 );
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100445
446 // First run for updating write_seed
447 header[0] = 0;
Gilles Peskine66afcca2019-06-12 19:33:42 +0200448 TEST_ASSERT( mbedtls_md_starts( &accumulator ) == 0 );
449 TEST_ASSERT( mbedtls_md_update( &accumulator, header, 2 ) == 0 );
450 TEST_ASSERT( mbedtls_md_update( &accumulator,
451 read_seed->x, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
452 TEST_ASSERT( mbedtls_md_finish( &accumulator, buf ) == 0 );
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100453
Gilles Peskine66afcca2019-06-12 19:33:42 +0200454 TEST_ASSERT( mbedtls_md_starts( &accumulator ) == 0 );
455 TEST_ASSERT( mbedtls_md_update( &accumulator,
456 buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100457
Gilles Peskine66afcca2019-06-12 19:33:42 +0200458 TEST_ASSERT( mbedtls_md( md_info, buf, MBEDTLS_ENTROPY_BLOCK_SIZE,
459 check_seed ) == 0 );
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100460
461 // Second run for actual entropy (triggers mbedtls_entropy_update_nv_seed)
462 header[0] = MBEDTLS_ENTROPY_SOURCE_MANUAL;
Gilles Peskine66afcca2019-06-12 19:33:42 +0200463 TEST_ASSERT( mbedtls_md_update( &accumulator, header, 2 ) == 0 );
464 TEST_ASSERT( mbedtls_md_update( &accumulator,
465 empty, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100466
467 header[0] = 0;
Gilles Peskine66afcca2019-06-12 19:33:42 +0200468 TEST_ASSERT( mbedtls_md_update( &accumulator, header, 2 ) == 0 );
469 TEST_ASSERT( mbedtls_md_update( &accumulator,
470 check_seed, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
471 TEST_ASSERT( mbedtls_md_finish( &accumulator, buf ) == 0 );
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100472
Gilles Peskine66afcca2019-06-12 19:33:42 +0200473 TEST_ASSERT( mbedtls_md( md_info, buf, MBEDTLS_ENTROPY_BLOCK_SIZE,
474 check_entropy ) == 0 );
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100475
476 // Check result of both NV file and entropy received with the manual calculations
477 TEST_ASSERT( memcmp( check_seed, buffer_seed, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
478 TEST_ASSERT( memcmp( check_entropy, entropy, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
479
Gilles Peskinee39b9032019-06-12 19:31:29 +0200480exit:
Gilles Peskine66afcca2019-06-12 19:33:42 +0200481 mbedtls_md_free( &accumulator );
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100482 mbedtls_entropy_free( &ctx );
Gilles Peskinee39b9032019-06-12 19:31:29 +0200483 mbedtls_nv_seed_read = original_mbedtls_nv_seed_read;
484 mbedtls_nv_seed_write = original_mbedtls_nv_seed_write;
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100485}
486/* END_CASE */
487
Hanno Beckerd4a872e2017-09-07 08:09:33 +0100488/* BEGIN_CASE depends_on:ENTROPY_HAVE_STRONG:MBEDTLS_SELF_TEST */
Simon Butcherb7f45c52016-09-15 18:42:26 +0100489void entropy_selftest( int result )
Manuel Pégourié-Gonnard2c25eb02014-05-30 10:38:18 +0200490{
Andres AG93012e82016-09-09 09:10:28 +0100491 TEST_ASSERT( mbedtls_entropy_self_test( 1 ) == result );
Manuel Pégourié-Gonnard2c25eb02014-05-30 10:38:18 +0200492}
493/* END_CASE */