blob: a453aadf3ccd547b93347be7bd2f853c5e7666fd [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
Victor Krasnoshchokb3129ba2020-08-29 22:54:37 +0300152/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
153void entropy_write_base_seed_file( int ret )
154{
155 mbedtls_entropy_context ctx;
156
157 mbedtls_entropy_init( &ctx );
158
159 TEST_ASSERT( mbedtls_entropy_write_seed_file( &ctx, MBEDTLS_PLATFORM_STD_NV_SEED_FILE ) == ret );
160 TEST_ASSERT( mbedtls_entropy_update_seed_file( &ctx, MBEDTLS_PLATFORM_STD_NV_SEED_FILE ) == ret );
161
162exit:
163 mbedtls_entropy_free( &ctx );
164}
165/* END_CASE */
166
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200167/* BEGIN_CASE */
Gilles Peskine7f246512019-10-08 14:51:49 +0200168void entropy_no_sources( )
169{
170 mbedtls_entropy_context ctx;
171 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
172
173 mbedtls_entropy_init( &ctx );
174 entropy_clear_sources( &ctx );
175 TEST_EQUAL( mbedtls_entropy_func( &ctx, buf, sizeof( buf ) ),
176 MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED );
177
178exit:
179 mbedtls_entropy_free( &ctx );
180}
181/* END_CASE */
182
183/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100184void entropy_too_many_sources( )
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200185{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 mbedtls_entropy_context ctx;
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200187 size_t i;
Gilles Peskineed04a672019-10-08 14:37:27 +0200188 entropy_dummy_context dummy = {DUMMY_REQUESTED_LENGTH, 0, 0};
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 mbedtls_entropy_init( &ctx );
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200191
192 /*
193 * It's hard to tell precisely when the error will occur,
194 * since we don't know how many sources were automatically added.
195 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 for( i = 0; i < MBEDTLS_ENTROPY_MAX_SOURCES; i++ )
Gilles Peskineed04a672019-10-08 14:37:27 +0200197 (void) mbedtls_entropy_add_source( &ctx, entropy_dummy_source, &dummy,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200198 16, MBEDTLS_ENTROPY_SOURCE_WEAK );
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200199
Gilles Peskineed04a672019-10-08 14:37:27 +0200200 TEST_ASSERT( mbedtls_entropy_add_source( &ctx, entropy_dummy_source, &dummy,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200201 16, MBEDTLS_ENTROPY_SOURCE_WEAK )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 == MBEDTLS_ERR_ENTROPY_MAX_SOURCES );
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200203
Paul Bakkerbd51b262014-07-10 15:26:12 +0200204exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 mbedtls_entropy_free( &ctx );
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200206}
207/* END_CASE */
208
Hanno Beckerd4a872e2017-09-07 08:09:33 +0100209/* BEGIN_CASE depends_on:ENTROPY_HAVE_STRONG */
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200210void entropy_func_len( int len, int ret )
211{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212 mbedtls_entropy_context ctx;
213 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE + 10] = { 0 };
214 unsigned char acc[MBEDTLS_ENTROPY_BLOCK_SIZE + 10] = { 0 };
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200215 size_t i, j;
216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 mbedtls_entropy_init( &ctx );
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200218
219 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 * See comments in mbedtls_entropy_self_test()
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200221 */
222 for( i = 0; i < 8; i++ )
223 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 TEST_ASSERT( mbedtls_entropy_func( &ctx, buf, len ) == ret );
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200225 for( j = 0; j < sizeof( buf ); j++ )
226 acc[j] |= buf[j];
227 }
228
229 if( ret == 0 )
230 for( j = 0; j < (size_t) len; j++ )
231 TEST_ASSERT( acc[j] != 0 );
232
233 for( j = len; j < sizeof( buf ); j++ )
234 TEST_ASSERT( acc[j] == 0 );
235}
236/* END_CASE */
237
238/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100239void entropy_source_fail( char * path )
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200240{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 mbedtls_entropy_context ctx;
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200242 unsigned char buf[16];
Gilles Peskineed04a672019-10-08 14:37:27 +0200243 entropy_dummy_context dummy = {DUMMY_FAIL, 0, 0};
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 mbedtls_entropy_init( &ctx );
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200246
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200247 TEST_ASSERT( mbedtls_entropy_add_source( &ctx, entropy_dummy_source,
Gilles Peskineed04a672019-10-08 14:37:27 +0200248 &dummy, 16,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200249 MBEDTLS_ENTROPY_SOURCE_WEAK )
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200250 == 0 );
251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 TEST_ASSERT( mbedtls_entropy_func( &ctx, buf, sizeof( buf ) )
253 == MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
254 TEST_ASSERT( mbedtls_entropy_gather( &ctx )
255 == MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Simon Butcherb7f45c52016-09-15 18:42:26 +0100256#if defined(MBEDTLS_FS_IO) && defined(MBEDTLS_ENTROPY_NV_SEED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 TEST_ASSERT( mbedtls_entropy_write_seed_file( &ctx, path )
258 == MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
259 TEST_ASSERT( mbedtls_entropy_update_seed_file( &ctx, path )
260 == MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200261#else
262 ((void) path);
263#endif
264
Paul Bakkerbd51b262014-07-10 15:26:12 +0200265exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 mbedtls_entropy_free( &ctx );
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200267}
268/* END_CASE */
269
Gilles Peskinecbd91e02019-11-25 19:50:54 +0100270/* BEGIN_CASE */
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200271void entropy_threshold( int threshold, int chunk_size, int result )
272{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 mbedtls_entropy_context ctx;
Gilles Peskinecbd91e02019-11-25 19:50:54 +0100274 entropy_dummy_context strong =
275 {DUMMY_CONSTANT_LENGTH, MBEDTLS_ENTROPY_BLOCK_SIZE, 0};
276 entropy_dummy_context weak = {DUMMY_CONSTANT_LENGTH, chunk_size, 0};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200278 int ret;
279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 mbedtls_entropy_init( &ctx );
Gilles Peskinecbd91e02019-11-25 19:50:54 +0100281 entropy_clear_sources( &ctx );
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200282
Gilles Peskinecbd91e02019-11-25 19:50:54 +0100283 /* Set strong source that reaches its threshold immediately and
284 * a weak source whose threshold is a test parameter. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 TEST_ASSERT( mbedtls_entropy_add_source( &ctx, entropy_dummy_source,
Gilles Peskinecbd91e02019-11-25 19:50:54 +0100286 &strong, 1,
287 MBEDTLS_ENTROPY_SOURCE_STRONG ) == 0 );
288 TEST_ASSERT( mbedtls_entropy_add_source( &ctx, entropy_dummy_source,
289 &weak, threshold,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200290 MBEDTLS_ENTROPY_SOURCE_WEAK ) == 0 );
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 ret = mbedtls_entropy_func( &ctx, buf, sizeof( buf ) );
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200293
294 if( result >= 0 )
295 {
296 TEST_ASSERT( ret == 0 );
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100297#if defined(MBEDTLS_ENTROPY_NV_SEED)
Gilles Peskineae679392019-11-25 18:26:23 +0100298 /* If the NV seed functionality is enabled, there are two entropy
299 * updates: before and after updating the NV seed. */
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100300 result *= 2;
301#endif
Gilles Peskinecbd91e02019-11-25 19:50:54 +0100302 TEST_ASSERT( weak.calls == (size_t) result );
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200303 }
304 else
305 {
306 TEST_ASSERT( ret == result );
307 }
308
Paul Bakkerbd51b262014-07-10 15:26:12 +0200309exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310 mbedtls_entropy_free( &ctx );
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200311}
312/* END_CASE */
313
Gilles Peskine65fc0682019-10-08 15:01:34 +0200314/* BEGIN_CASE */
315void entropy_calls( int strength1, int strength2,
316 int threshold, int chunk_size,
317 int result )
318{
319 /*
320 * if result >= 0: result = expected number of calls to source 1
321 * if result < 0: result = expected return code from mbedtls_entropy_func()
322 */
323
324 mbedtls_entropy_context ctx;
325 entropy_dummy_context dummy1 = {DUMMY_CONSTANT_LENGTH, chunk_size, 0};
326 entropy_dummy_context dummy2 = {DUMMY_CONSTANT_LENGTH, chunk_size, 0};
327 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
328 int ret;
329
330 mbedtls_entropy_init( &ctx );
331 entropy_clear_sources( &ctx );
332
333 TEST_ASSERT( mbedtls_entropy_add_source( &ctx, entropy_dummy_source,
334 &dummy1, threshold,
335 strength1 ) == 0 );
336 TEST_ASSERT( mbedtls_entropy_add_source( &ctx, entropy_dummy_source,
337 &dummy2, threshold,
338 strength2 ) == 0 );
339
340 ret = mbedtls_entropy_func( &ctx, buf, sizeof( buf ) );
341
342 if( result >= 0 )
343 {
344 TEST_ASSERT( ret == 0 );
Gilles Peskineae679392019-11-25 18:26:23 +0100345#if defined(MBEDTLS_ENTROPY_NV_SEED)
346 /* If the NV seed functionality is enabled, there are two entropy
347 * updates: before and after updating the NV seed. */
348 result *= 2;
349#endif
Gilles Peskine65fc0682019-10-08 15:01:34 +0200350 TEST_ASSERT( dummy1.calls == (size_t) result );
351 }
352 else
353 {
354 TEST_ASSERT( ret == result );
355 }
356
357exit:
358 mbedtls_entropy_free( &ctx );
359}
360/* END_CASE */
361
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100362/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100363void nv_seed_file_create( )
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100364{
365 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
366
367 memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
368
369 TEST_ASSERT( write_nv_seed( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
370}
371/* END_CASE */
372
Paul Bakkerb598c292016-06-01 16:57:11 +0100373/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO:MBEDTLS_PLATFORM_NV_SEED_ALT */
Azim Khanf1aaec92017-05-30 14:23:15 +0100374void entropy_nv_seed_std_io( )
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100375{
376 unsigned char io_seed[MBEDTLS_ENTROPY_BLOCK_SIZE];
377 unsigned char check_seed[MBEDTLS_ENTROPY_BLOCK_SIZE];
378
379 memset( io_seed, 1, MBEDTLS_ENTROPY_BLOCK_SIZE );
380 memset( check_seed, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
381
382 mbedtls_platform_set_nv_seed( mbedtls_platform_std_nv_seed_read,
383 mbedtls_platform_std_nv_seed_write );
384
385 /* Check if platform NV read and write manipulate the same data */
386 TEST_ASSERT( write_nv_seed( io_seed, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
387 TEST_ASSERT( mbedtls_nv_seed_read( check_seed, MBEDTLS_ENTROPY_BLOCK_SIZE ) ==
388 MBEDTLS_ENTROPY_BLOCK_SIZE );
389
390 TEST_ASSERT( memcmp( io_seed, check_seed, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
391
392 memset( check_seed, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
393
394 /* Check if platform NV write and raw read manipulate the same data */
395 TEST_ASSERT( mbedtls_nv_seed_write( io_seed, MBEDTLS_ENTROPY_BLOCK_SIZE ) ==
396 MBEDTLS_ENTROPY_BLOCK_SIZE );
397 TEST_ASSERT( read_nv_seed( check_seed, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
398
399 TEST_ASSERT( memcmp( io_seed, check_seed, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
400}
401/* END_CASE */
402
Gilles Peskine66afcca2019-06-12 19:33:42 +0200403/* BEGIN_CASE depends_on:MBEDTLS_MD_C:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_PLATFORM_NV_SEED_ALT */
Azim Khan5fcca462018-06-29 11:05:32 +0100404void entropy_nv_seed( data_t * read_seed )
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100405{
Gilles Peskine66afcca2019-06-12 19:33:42 +0200406#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
407 const mbedtls_md_info_t *md_info =
408 mbedtls_md_info_from_type( MBEDTLS_MD_SHA512 );
409#elif defined(MBEDTLS_ENTROPY_SHA256_ACCUMULATOR)
410 const mbedtls_md_info_t *md_info =
411 mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 );
412#else
413#error "Unsupported entropy accumulator"
414#endif
415 mbedtls_md_context_t accumulator;
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100416 mbedtls_entropy_context ctx;
Gilles Peskinee39b9032019-06-12 19:31:29 +0200417 int (*original_mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len ) =
418 mbedtls_nv_seed_read;
419 int (*original_mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len ) =
420 mbedtls_nv_seed_write;
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100421
422 unsigned char header[2];
423 unsigned char entropy[MBEDTLS_ENTROPY_BLOCK_SIZE];
424 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
425 unsigned char empty[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100426 unsigned char check_seed[MBEDTLS_ENTROPY_BLOCK_SIZE];
427 unsigned char check_entropy[MBEDTLS_ENTROPY_BLOCK_SIZE];
428
429 memset( entropy, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
430 memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100431 memset( empty, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
432 memset( check_seed, 2, MBEDTLS_ENTROPY_BLOCK_SIZE );
433 memset( check_entropy, 3, MBEDTLS_ENTROPY_BLOCK_SIZE );
434
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100435 // Make sure we read/write NV seed from our buffers
436 mbedtls_platform_set_nv_seed( buffer_nv_seed_read, buffer_nv_seed_write );
437
Gilles Peskine66afcca2019-06-12 19:33:42 +0200438 mbedtls_md_init( &accumulator );
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100439 mbedtls_entropy_init( &ctx );
440 entropy_clear_sources( &ctx );
441
442 TEST_ASSERT( mbedtls_entropy_add_source( &ctx, mbedtls_nv_seed_poll, NULL,
443 MBEDTLS_ENTROPY_BLOCK_SIZE,
444 MBEDTLS_ENTROPY_SOURCE_STRONG ) == 0 );
445
Gilles Peskine66afcca2019-06-12 19:33:42 +0200446 // Set the initial NV seed to read
447 TEST_ASSERT( read_seed->len >= MBEDTLS_ENTROPY_BLOCK_SIZE );
448 memcpy( buffer_seed, read_seed->x, MBEDTLS_ENTROPY_BLOCK_SIZE );
449
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100450 // Do an entropy run
451 TEST_ASSERT( mbedtls_entropy_func( &ctx, entropy, sizeof( entropy ) ) == 0 );
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100452 // Determine what should have happened with manual entropy internal logic
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100453
454 // Init accumulator
455 header[1] = MBEDTLS_ENTROPY_BLOCK_SIZE;
Gilles Peskine66afcca2019-06-12 19:33:42 +0200456 TEST_ASSERT( mbedtls_md_setup( &accumulator, md_info, 0 ) == 0 );
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100457
458 // First run for updating write_seed
459 header[0] = 0;
Gilles Peskine66afcca2019-06-12 19:33:42 +0200460 TEST_ASSERT( mbedtls_md_starts( &accumulator ) == 0 );
461 TEST_ASSERT( mbedtls_md_update( &accumulator, header, 2 ) == 0 );
462 TEST_ASSERT( mbedtls_md_update( &accumulator,
463 read_seed->x, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
464 TEST_ASSERT( mbedtls_md_finish( &accumulator, buf ) == 0 );
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100465
Gilles Peskine66afcca2019-06-12 19:33:42 +0200466 TEST_ASSERT( mbedtls_md_starts( &accumulator ) == 0 );
467 TEST_ASSERT( mbedtls_md_update( &accumulator,
468 buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 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_seed ) == 0 );
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100472
473 // Second run for actual entropy (triggers mbedtls_entropy_update_nv_seed)
474 header[0] = MBEDTLS_ENTROPY_SOURCE_MANUAL;
Gilles Peskine66afcca2019-06-12 19:33:42 +0200475 TEST_ASSERT( mbedtls_md_update( &accumulator, header, 2 ) == 0 );
476 TEST_ASSERT( mbedtls_md_update( &accumulator,
477 empty, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100478
479 header[0] = 0;
Gilles Peskine66afcca2019-06-12 19:33:42 +0200480 TEST_ASSERT( mbedtls_md_update( &accumulator, header, 2 ) == 0 );
481 TEST_ASSERT( mbedtls_md_update( &accumulator,
482 check_seed, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
483 TEST_ASSERT( mbedtls_md_finish( &accumulator, buf ) == 0 );
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100484
Gilles Peskine66afcca2019-06-12 19:33:42 +0200485 TEST_ASSERT( mbedtls_md( md_info, buf, MBEDTLS_ENTROPY_BLOCK_SIZE,
486 check_entropy ) == 0 );
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100487
488 // Check result of both NV file and entropy received with the manual calculations
489 TEST_ASSERT( memcmp( check_seed, buffer_seed, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
490 TEST_ASSERT( memcmp( check_entropy, entropy, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 );
491
Gilles Peskinee39b9032019-06-12 19:31:29 +0200492exit:
Gilles Peskine66afcca2019-06-12 19:33:42 +0200493 mbedtls_md_free( &accumulator );
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100494 mbedtls_entropy_free( &ctx );
Gilles Peskinee39b9032019-06-12 19:31:29 +0200495 mbedtls_nv_seed_read = original_mbedtls_nv_seed_read;
496 mbedtls_nv_seed_write = original_mbedtls_nv_seed_write;
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100497}
498/* END_CASE */
499
Hanno Beckerd4a872e2017-09-07 08:09:33 +0100500/* BEGIN_CASE depends_on:ENTROPY_HAVE_STRONG:MBEDTLS_SELF_TEST */
Simon Butcherb7f45c52016-09-15 18:42:26 +0100501void entropy_selftest( int result )
Manuel Pégourié-Gonnard2c25eb02014-05-30 10:38:18 +0200502{
Andres AG93012e82016-09-09 09:10:28 +0100503 TEST_ASSERT( mbedtls_entropy_self_test( 1 ) == result );
Manuel Pégourié-Gonnard2c25eb02014-05-30 10:38:18 +0200504}
505/* END_CASE */