Manuel Pégourié-Gonnard | 2c25eb0 | 2014-05-30 10:38:18 +0200 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 2 | #include "mbedtls/entropy.h" |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 3 | #include "mbedtls/entropy_poll.h" |
Mohammad Azim Khan | 67735d5 | 2017-04-06 11:55:43 +0100 | [diff] [blame] | 4 | #include "string.h" |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 5 | |
Gilles Peskine | ed04a67 | 2019-10-08 14:37:27 +0200 | [diff] [blame] | 6 | typedef enum |
| 7 | { |
| 8 | DUMMY_CONSTANT_LENGTH, /* Output context->length bytes */ |
| 9 | DUMMY_REQUESTED_LENGTH, /* Output whatever length was requested */ |
| 10 | DUMMY_FAIL, /* Return an error code */ |
| 11 | } entropy_dummy_instruction; |
| 12 | |
| 13 | typedef struct |
| 14 | { |
| 15 | entropy_dummy_instruction instruction; |
| 16 | size_t length; /* Length to return for DUMMY_CONSTANT_LENGTH */ |
| 17 | size_t calls; /* Incremented at each call */ |
| 18 | } entropy_dummy_context; |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 19 | |
| 20 | /* |
| 21 | * Dummy entropy source |
| 22 | * |
| 23 | * If data is NULL, write exactly the requested length. |
| 24 | * Otherwise, write the length indicated by data or error if negative |
| 25 | */ |
Gilles Peskine | ed04a67 | 2019-10-08 14:37:27 +0200 | [diff] [blame] | 26 | static int entropy_dummy_source( void *arg, unsigned char *output, |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 27 | size_t len, size_t *olen ) |
| 28 | { |
Gilles Peskine | ed04a67 | 2019-10-08 14:37:27 +0200 | [diff] [blame] | 29 | entropy_dummy_context *context = arg; |
| 30 | ++context->calls; |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 31 | |
Gilles Peskine | ed04a67 | 2019-10-08 14:37:27 +0200 | [diff] [blame] | 32 | switch( context->instruction ) |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 33 | { |
Gilles Peskine | ed04a67 | 2019-10-08 14:37:27 +0200 | [diff] [blame] | 34 | case DUMMY_CONSTANT_LENGTH: |
| 35 | *olen = context->length; |
| 36 | break; |
| 37 | case DUMMY_REQUESTED_LENGTH: |
| 38 | *olen = len; |
| 39 | break; |
| 40 | case DUMMY_FAIL: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 41 | return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | memset( output, 0x2a, *olen ); |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 45 | return( 0 ); |
| 46 | } |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 47 | |
| 48 | /* |
| 49 | * Ability to clear entropy sources to allow testing with just predefined |
| 50 | * entropy sources. This function or tests depending on it might break if there |
| 51 | * are internal changes to how entropy sources are registered. |
| 52 | * |
| 53 | * To be called immediately after mbedtls_entropy_init(). |
| 54 | * |
| 55 | * Just resetting the counter. New sources will overwrite existing ones. |
| 56 | * This might break memory checks in the future if sources need 'free-ing' then |
| 57 | * as well. |
| 58 | */ |
Gilles Peskine | 7f24651 | 2019-10-08 14:51:49 +0200 | [diff] [blame] | 59 | static void entropy_clear_sources( mbedtls_entropy_context *ctx ) |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 60 | { |
| 61 | ctx->source_count = 0; |
| 62 | } |
| 63 | |
Gilles Peskine | 7f24651 | 2019-10-08 14:51:49 +0200 | [diff] [blame] | 64 | #if defined(MBEDTLS_ENTROPY_NV_SEED) |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 65 | /* |
| 66 | * NV seed read/write functions that use a buffer instead of a file |
| 67 | */ |
| 68 | static unsigned char buffer_seed[MBEDTLS_ENTROPY_BLOCK_SIZE]; |
| 69 | |
Jaeden Amero | f7dca86 | 2019-06-27 17:31:33 +0100 | [diff] [blame] | 70 | int buffer_nv_seed_read( unsigned char *buf, size_t buf_len ) |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 71 | { |
| 72 | if( buf_len != MBEDTLS_ENTROPY_BLOCK_SIZE ) |
| 73 | return( -1 ); |
| 74 | |
| 75 | memcpy( buf, buffer_seed, MBEDTLS_ENTROPY_BLOCK_SIZE ); |
| 76 | return( 0 ); |
| 77 | } |
| 78 | |
Jaeden Amero | f7dca86 | 2019-06-27 17:31:33 +0100 | [diff] [blame] | 79 | int buffer_nv_seed_write( unsigned char *buf, size_t buf_len ) |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 80 | { |
| 81 | if( buf_len != MBEDTLS_ENTROPY_BLOCK_SIZE ) |
| 82 | return( -1 ); |
| 83 | |
| 84 | memcpy( buffer_seed, buf, MBEDTLS_ENTROPY_BLOCK_SIZE ); |
| 85 | return( 0 ); |
| 86 | } |
| 87 | |
| 88 | /* |
| 89 | * NV seed read/write helpers that fill the base seedfile |
| 90 | */ |
| 91 | static int write_nv_seed( unsigned char *buf, size_t buf_len ) |
| 92 | { |
| 93 | FILE *f; |
| 94 | |
| 95 | if( buf_len != MBEDTLS_ENTROPY_BLOCK_SIZE ) |
| 96 | return( -1 ); |
| 97 | |
| 98 | if( ( f = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "w" ) ) == NULL ) |
| 99 | return( -1 ); |
| 100 | |
| 101 | if( fwrite( buf, 1, MBEDTLS_ENTROPY_BLOCK_SIZE, f ) != |
| 102 | MBEDTLS_ENTROPY_BLOCK_SIZE ) |
| 103 | return( -1 ); |
| 104 | |
| 105 | fclose( f ); |
| 106 | |
| 107 | return( 0 ); |
| 108 | } |
| 109 | |
Jaeden Amero | f7dca86 | 2019-06-27 17:31:33 +0100 | [diff] [blame] | 110 | int read_nv_seed( unsigned char *buf, size_t buf_len ) |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 111 | { |
| 112 | FILE *f; |
| 113 | |
| 114 | if( buf_len != MBEDTLS_ENTROPY_BLOCK_SIZE ) |
| 115 | return( -1 ); |
| 116 | |
| 117 | if( ( f = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "rb" ) ) == NULL ) |
| 118 | return( -1 ); |
| 119 | |
| 120 | if( fread( buf, 1, MBEDTLS_ENTROPY_BLOCK_SIZE, f ) != |
| 121 | MBEDTLS_ENTROPY_BLOCK_SIZE ) |
| 122 | return( -1 ); |
| 123 | |
| 124 | fclose( f ); |
| 125 | |
| 126 | return( 0 ); |
| 127 | } |
Paul Bakker | 4a6c6fc | 2016-06-01 16:34:25 +0100 | [diff] [blame] | 128 | #endif /* MBEDTLS_ENTROPY_NV_SEED */ |
Manuel Pégourié-Gonnard | 2c25eb0 | 2014-05-30 10:38:18 +0200 | [diff] [blame] | 129 | /* END_HEADER */ |
| 130 | |
| 131 | /* BEGIN_DEPENDENCIES |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 132 | * depends_on:MBEDTLS_ENTROPY_C |
Manuel Pégourié-Gonnard | 2c25eb0 | 2014-05-30 10:38:18 +0200 | [diff] [blame] | 133 | * END_DEPENDENCIES |
| 134 | */ |
| 135 | |
Simon Butcher | b7f45c5 | 2016-09-15 18:42:26 +0100 | [diff] [blame] | 136 | /* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 137 | void entropy_seed_file( char * path, int ret ) |
Manuel Pégourié-Gonnard | 2c25eb0 | 2014-05-30 10:38:18 +0200 | [diff] [blame] | 138 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 139 | mbedtls_entropy_context ctx; |
Manuel Pégourié-Gonnard | 2c25eb0 | 2014-05-30 10:38:18 +0200 | [diff] [blame] | 140 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 141 | mbedtls_entropy_init( &ctx ); |
Manuel Pégourié-Gonnard | 2c25eb0 | 2014-05-30 10:38:18 +0200 | [diff] [blame] | 142 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 143 | TEST_ASSERT( mbedtls_entropy_write_seed_file( &ctx, path ) == ret ); |
| 144 | TEST_ASSERT( mbedtls_entropy_update_seed_file( &ctx, path ) == ret ); |
Manuel Pégourié-Gonnard | 2c25eb0 | 2014-05-30 10:38:18 +0200 | [diff] [blame] | 145 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 146 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 147 | mbedtls_entropy_free( &ctx ); |
Manuel Pégourié-Gonnard | 2c25eb0 | 2014-05-30 10:38:18 +0200 | [diff] [blame] | 148 | } |
| 149 | /* END_CASE */ |
| 150 | |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 151 | /* BEGIN_CASE */ |
Gilles Peskine | 7f24651 | 2019-10-08 14:51:49 +0200 | [diff] [blame] | 152 | void entropy_no_sources( ) |
| 153 | { |
| 154 | mbedtls_entropy_context ctx; |
| 155 | unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE]; |
| 156 | |
| 157 | mbedtls_entropy_init( &ctx ); |
| 158 | entropy_clear_sources( &ctx ); |
| 159 | TEST_EQUAL( mbedtls_entropy_func( &ctx, buf, sizeof( buf ) ), |
| 160 | MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED ); |
| 161 | |
| 162 | exit: |
| 163 | mbedtls_entropy_free( &ctx ); |
| 164 | } |
| 165 | /* END_CASE */ |
| 166 | |
| 167 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 168 | void entropy_too_many_sources( ) |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 169 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 170 | mbedtls_entropy_context ctx; |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 171 | size_t i; |
Gilles Peskine | ed04a67 | 2019-10-08 14:37:27 +0200 | [diff] [blame] | 172 | entropy_dummy_context dummy = {DUMMY_REQUESTED_LENGTH, 0, 0}; |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 173 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 174 | mbedtls_entropy_init( &ctx ); |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 175 | |
| 176 | /* |
| 177 | * It's hard to tell precisely when the error will occur, |
| 178 | * since we don't know how many sources were automatically added. |
| 179 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 180 | for( i = 0; i < MBEDTLS_ENTROPY_MAX_SOURCES; i++ ) |
Gilles Peskine | ed04a67 | 2019-10-08 14:37:27 +0200 | [diff] [blame] | 181 | (void) mbedtls_entropy_add_source( &ctx, entropy_dummy_source, &dummy, |
Manuel Pégourié-Gonnard | 7580ba4 | 2015-06-19 10:26:32 +0200 | [diff] [blame] | 182 | 16, MBEDTLS_ENTROPY_SOURCE_WEAK ); |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 183 | |
Gilles Peskine | ed04a67 | 2019-10-08 14:37:27 +0200 | [diff] [blame] | 184 | TEST_ASSERT( mbedtls_entropy_add_source( &ctx, entropy_dummy_source, &dummy, |
Manuel Pégourié-Gonnard | 7580ba4 | 2015-06-19 10:26:32 +0200 | [diff] [blame] | 185 | 16, MBEDTLS_ENTROPY_SOURCE_WEAK ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 186 | == MBEDTLS_ERR_ENTROPY_MAX_SOURCES ); |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 187 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 188 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 189 | mbedtls_entropy_free( &ctx ); |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 190 | } |
| 191 | /* END_CASE */ |
| 192 | |
Hanno Becker | d4a872e | 2017-09-07 08:09:33 +0100 | [diff] [blame] | 193 | /* BEGIN_CASE depends_on:ENTROPY_HAVE_STRONG */ |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 194 | void entropy_func_len( int len, int ret ) |
| 195 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 196 | mbedtls_entropy_context ctx; |
| 197 | unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE + 10] = { 0 }; |
| 198 | unsigned char acc[MBEDTLS_ENTROPY_BLOCK_SIZE + 10] = { 0 }; |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 199 | size_t i, j; |
| 200 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 201 | mbedtls_entropy_init( &ctx ); |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 202 | |
| 203 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 204 | * See comments in mbedtls_entropy_self_test() |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 205 | */ |
| 206 | for( i = 0; i < 8; i++ ) |
| 207 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 208 | TEST_ASSERT( mbedtls_entropy_func( &ctx, buf, len ) == ret ); |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 209 | for( j = 0; j < sizeof( buf ); j++ ) |
| 210 | acc[j] |= buf[j]; |
| 211 | } |
| 212 | |
| 213 | if( ret == 0 ) |
| 214 | for( j = 0; j < (size_t) len; j++ ) |
| 215 | TEST_ASSERT( acc[j] != 0 ); |
| 216 | |
| 217 | for( j = len; j < sizeof( buf ); j++ ) |
| 218 | TEST_ASSERT( acc[j] == 0 ); |
| 219 | } |
| 220 | /* END_CASE */ |
| 221 | |
| 222 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 223 | void entropy_source_fail( char * path ) |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 224 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 225 | mbedtls_entropy_context ctx; |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 226 | unsigned char buf[16]; |
Gilles Peskine | ed04a67 | 2019-10-08 14:37:27 +0200 | [diff] [blame] | 227 | entropy_dummy_context dummy = {DUMMY_FAIL, 0, 0}; |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 228 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 229 | mbedtls_entropy_init( &ctx ); |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 230 | |
Manuel Pégourié-Gonnard | 7580ba4 | 2015-06-19 10:26:32 +0200 | [diff] [blame] | 231 | TEST_ASSERT( mbedtls_entropy_add_source( &ctx, entropy_dummy_source, |
Gilles Peskine | ed04a67 | 2019-10-08 14:37:27 +0200 | [diff] [blame] | 232 | &dummy, 16, |
Manuel Pégourié-Gonnard | 7580ba4 | 2015-06-19 10:26:32 +0200 | [diff] [blame] | 233 | MBEDTLS_ENTROPY_SOURCE_WEAK ) |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 234 | == 0 ); |
| 235 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 236 | TEST_ASSERT( mbedtls_entropy_func( &ctx, buf, sizeof( buf ) ) |
| 237 | == MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); |
| 238 | TEST_ASSERT( mbedtls_entropy_gather( &ctx ) |
| 239 | == MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); |
Simon Butcher | b7f45c5 | 2016-09-15 18:42:26 +0100 | [diff] [blame] | 240 | #if defined(MBEDTLS_FS_IO) && defined(MBEDTLS_ENTROPY_NV_SEED) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 241 | TEST_ASSERT( mbedtls_entropy_write_seed_file( &ctx, path ) |
| 242 | == MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); |
| 243 | TEST_ASSERT( mbedtls_entropy_update_seed_file( &ctx, path ) |
| 244 | == MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 245 | #else |
| 246 | ((void) path); |
| 247 | #endif |
| 248 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 249 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 250 | mbedtls_entropy_free( &ctx ); |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 251 | } |
| 252 | /* END_CASE */ |
| 253 | |
Hanno Becker | d4a872e | 2017-09-07 08:09:33 +0100 | [diff] [blame] | 254 | /* BEGIN_CASE depends_on:ENTROPY_HAVE_STRONG */ |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 255 | void entropy_threshold( int threshold, int chunk_size, int result ) |
| 256 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 257 | mbedtls_entropy_context ctx; |
Gilles Peskine | ed04a67 | 2019-10-08 14:37:27 +0200 | [diff] [blame] | 258 | entropy_dummy_context dummy = {DUMMY_CONSTANT_LENGTH, chunk_size, 0}; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 259 | unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 }; |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 260 | int ret; |
| 261 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 262 | mbedtls_entropy_init( &ctx ); |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 263 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 264 | TEST_ASSERT( mbedtls_entropy_add_source( &ctx, entropy_dummy_source, |
Gilles Peskine | ed04a67 | 2019-10-08 14:37:27 +0200 | [diff] [blame] | 265 | &dummy, threshold, |
Manuel Pégourié-Gonnard | 7580ba4 | 2015-06-19 10:26:32 +0200 | [diff] [blame] | 266 | MBEDTLS_ENTROPY_SOURCE_WEAK ) == 0 ); |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 267 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 268 | ret = mbedtls_entropy_func( &ctx, buf, sizeof( buf ) ); |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 269 | |
| 270 | if( result >= 0 ) |
| 271 | { |
| 272 | TEST_ASSERT( ret == 0 ); |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 273 | #if defined(MBEDTLS_ENTROPY_NV_SEED) |
| 274 | // Two times as much calls due to the NV seed update |
| 275 | result *= 2; |
| 276 | #endif |
Gilles Peskine | ed04a67 | 2019-10-08 14:37:27 +0200 | [diff] [blame] | 277 | TEST_ASSERT( dummy.calls == (size_t) result ); |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 278 | } |
| 279 | else |
| 280 | { |
| 281 | TEST_ASSERT( ret == result ); |
| 282 | } |
| 283 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 284 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 285 | mbedtls_entropy_free( &ctx ); |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 286 | } |
| 287 | /* END_CASE */ |
| 288 | |
Gilles Peskine | 65fc068 | 2019-10-08 15:01:34 +0200 | [diff] [blame^] | 289 | /* BEGIN_CASE */ |
| 290 | void entropy_calls( int strength1, int strength2, |
| 291 | int threshold, int chunk_size, |
| 292 | int result ) |
| 293 | { |
| 294 | /* |
| 295 | * if result >= 0: result = expected number of calls to source 1 |
| 296 | * if result < 0: result = expected return code from mbedtls_entropy_func() |
| 297 | */ |
| 298 | |
| 299 | mbedtls_entropy_context ctx; |
| 300 | entropy_dummy_context dummy1 = {DUMMY_CONSTANT_LENGTH, chunk_size, 0}; |
| 301 | entropy_dummy_context dummy2 = {DUMMY_CONSTANT_LENGTH, chunk_size, 0}; |
| 302 | unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 }; |
| 303 | int ret; |
| 304 | |
| 305 | mbedtls_entropy_init( &ctx ); |
| 306 | entropy_clear_sources( &ctx ); |
| 307 | |
| 308 | TEST_ASSERT( mbedtls_entropy_add_source( &ctx, entropy_dummy_source, |
| 309 | &dummy1, threshold, |
| 310 | strength1 ) == 0 ); |
| 311 | TEST_ASSERT( mbedtls_entropy_add_source( &ctx, entropy_dummy_source, |
| 312 | &dummy2, threshold, |
| 313 | strength2 ) == 0 ); |
| 314 | |
| 315 | ret = mbedtls_entropy_func( &ctx, buf, sizeof( buf ) ); |
| 316 | |
| 317 | if( result >= 0 ) |
| 318 | { |
| 319 | TEST_ASSERT( ret == 0 ); |
| 320 | TEST_ASSERT( dummy1.calls == (size_t) result ); |
| 321 | } |
| 322 | else |
| 323 | { |
| 324 | TEST_ASSERT( ret == result ); |
| 325 | } |
| 326 | |
| 327 | exit: |
| 328 | mbedtls_entropy_free( &ctx ); |
| 329 | } |
| 330 | /* END_CASE */ |
| 331 | |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 332 | /* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 333 | void nv_seed_file_create( ) |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 334 | { |
| 335 | unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE]; |
| 336 | |
| 337 | memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE ); |
| 338 | |
| 339 | TEST_ASSERT( write_nv_seed( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 ); |
| 340 | } |
| 341 | /* END_CASE */ |
| 342 | |
Paul Bakker | b598c29 | 2016-06-01 16:57:11 +0100 | [diff] [blame] | 343 | /* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO:MBEDTLS_PLATFORM_NV_SEED_ALT */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 344 | void entropy_nv_seed_std_io( ) |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 345 | { |
| 346 | unsigned char io_seed[MBEDTLS_ENTROPY_BLOCK_SIZE]; |
| 347 | unsigned char check_seed[MBEDTLS_ENTROPY_BLOCK_SIZE]; |
| 348 | |
| 349 | memset( io_seed, 1, MBEDTLS_ENTROPY_BLOCK_SIZE ); |
| 350 | memset( check_seed, 0, MBEDTLS_ENTROPY_BLOCK_SIZE ); |
| 351 | |
| 352 | mbedtls_platform_set_nv_seed( mbedtls_platform_std_nv_seed_read, |
| 353 | mbedtls_platform_std_nv_seed_write ); |
| 354 | |
| 355 | /* Check if platform NV read and write manipulate the same data */ |
| 356 | TEST_ASSERT( write_nv_seed( io_seed, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 ); |
| 357 | TEST_ASSERT( mbedtls_nv_seed_read( check_seed, MBEDTLS_ENTROPY_BLOCK_SIZE ) == |
| 358 | MBEDTLS_ENTROPY_BLOCK_SIZE ); |
| 359 | |
| 360 | TEST_ASSERT( memcmp( io_seed, check_seed, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 ); |
| 361 | |
| 362 | memset( check_seed, 0, MBEDTLS_ENTROPY_BLOCK_SIZE ); |
| 363 | |
| 364 | /* Check if platform NV write and raw read manipulate the same data */ |
| 365 | TEST_ASSERT( mbedtls_nv_seed_write( io_seed, MBEDTLS_ENTROPY_BLOCK_SIZE ) == |
| 366 | MBEDTLS_ENTROPY_BLOCK_SIZE ); |
| 367 | TEST_ASSERT( read_nv_seed( check_seed, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 ); |
| 368 | |
| 369 | TEST_ASSERT( memcmp( io_seed, check_seed, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 ); |
| 370 | } |
| 371 | /* END_CASE */ |
| 372 | |
Gilles Peskine | 66afcca | 2019-06-12 19:33:42 +0200 | [diff] [blame] | 373 | /* BEGIN_CASE depends_on:MBEDTLS_MD_C:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_PLATFORM_NV_SEED_ALT */ |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 374 | void entropy_nv_seed( data_t * read_seed ) |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 375 | { |
Gilles Peskine | 66afcca | 2019-06-12 19:33:42 +0200 | [diff] [blame] | 376 | #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR) |
| 377 | const mbedtls_md_info_t *md_info = |
| 378 | mbedtls_md_info_from_type( MBEDTLS_MD_SHA512 ); |
| 379 | #elif defined(MBEDTLS_ENTROPY_SHA256_ACCUMULATOR) |
| 380 | const mbedtls_md_info_t *md_info = |
| 381 | mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ); |
| 382 | #else |
| 383 | #error "Unsupported entropy accumulator" |
| 384 | #endif |
| 385 | mbedtls_md_context_t accumulator; |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 386 | mbedtls_entropy_context ctx; |
Gilles Peskine | e39b903 | 2019-06-12 19:31:29 +0200 | [diff] [blame] | 387 | int (*original_mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len ) = |
| 388 | mbedtls_nv_seed_read; |
| 389 | int (*original_mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len ) = |
| 390 | mbedtls_nv_seed_write; |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 391 | |
| 392 | unsigned char header[2]; |
| 393 | unsigned char entropy[MBEDTLS_ENTROPY_BLOCK_SIZE]; |
| 394 | unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE]; |
| 395 | unsigned char empty[MBEDTLS_ENTROPY_BLOCK_SIZE]; |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 396 | unsigned char check_seed[MBEDTLS_ENTROPY_BLOCK_SIZE]; |
| 397 | unsigned char check_entropy[MBEDTLS_ENTROPY_BLOCK_SIZE]; |
| 398 | |
| 399 | memset( entropy, 0, MBEDTLS_ENTROPY_BLOCK_SIZE ); |
| 400 | memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE ); |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 401 | memset( empty, 0, MBEDTLS_ENTROPY_BLOCK_SIZE ); |
| 402 | memset( check_seed, 2, MBEDTLS_ENTROPY_BLOCK_SIZE ); |
| 403 | memset( check_entropy, 3, MBEDTLS_ENTROPY_BLOCK_SIZE ); |
| 404 | |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 405 | // Make sure we read/write NV seed from our buffers |
| 406 | mbedtls_platform_set_nv_seed( buffer_nv_seed_read, buffer_nv_seed_write ); |
| 407 | |
Gilles Peskine | 66afcca | 2019-06-12 19:33:42 +0200 | [diff] [blame] | 408 | mbedtls_md_init( &accumulator ); |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 409 | mbedtls_entropy_init( &ctx ); |
| 410 | entropy_clear_sources( &ctx ); |
| 411 | |
| 412 | TEST_ASSERT( mbedtls_entropy_add_source( &ctx, mbedtls_nv_seed_poll, NULL, |
| 413 | MBEDTLS_ENTROPY_BLOCK_SIZE, |
| 414 | MBEDTLS_ENTROPY_SOURCE_STRONG ) == 0 ); |
| 415 | |
Gilles Peskine | 66afcca | 2019-06-12 19:33:42 +0200 | [diff] [blame] | 416 | // Set the initial NV seed to read |
| 417 | TEST_ASSERT( read_seed->len >= MBEDTLS_ENTROPY_BLOCK_SIZE ); |
| 418 | memcpy( buffer_seed, read_seed->x, MBEDTLS_ENTROPY_BLOCK_SIZE ); |
| 419 | |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 420 | // Do an entropy run |
| 421 | TEST_ASSERT( mbedtls_entropy_func( &ctx, entropy, sizeof( entropy ) ) == 0 ); |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 422 | // Determine what should have happened with manual entropy internal logic |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 423 | |
| 424 | // Init accumulator |
| 425 | header[1] = MBEDTLS_ENTROPY_BLOCK_SIZE; |
Gilles Peskine | 66afcca | 2019-06-12 19:33:42 +0200 | [diff] [blame] | 426 | TEST_ASSERT( mbedtls_md_setup( &accumulator, md_info, 0 ) == 0 ); |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 427 | |
| 428 | // First run for updating write_seed |
| 429 | header[0] = 0; |
Gilles Peskine | 66afcca | 2019-06-12 19:33:42 +0200 | [diff] [blame] | 430 | TEST_ASSERT( mbedtls_md_starts( &accumulator ) == 0 ); |
| 431 | TEST_ASSERT( mbedtls_md_update( &accumulator, header, 2 ) == 0 ); |
| 432 | TEST_ASSERT( mbedtls_md_update( &accumulator, |
| 433 | read_seed->x, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 ); |
| 434 | TEST_ASSERT( mbedtls_md_finish( &accumulator, buf ) == 0 ); |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 435 | |
Gilles Peskine | 66afcca | 2019-06-12 19:33:42 +0200 | [diff] [blame] | 436 | TEST_ASSERT( mbedtls_md_starts( &accumulator ) == 0 ); |
| 437 | TEST_ASSERT( mbedtls_md_update( &accumulator, |
| 438 | buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 ); |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 439 | |
Gilles Peskine | 66afcca | 2019-06-12 19:33:42 +0200 | [diff] [blame] | 440 | TEST_ASSERT( mbedtls_md( md_info, buf, MBEDTLS_ENTROPY_BLOCK_SIZE, |
| 441 | check_seed ) == 0 ); |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 442 | |
| 443 | // Second run for actual entropy (triggers mbedtls_entropy_update_nv_seed) |
| 444 | header[0] = MBEDTLS_ENTROPY_SOURCE_MANUAL; |
Gilles Peskine | 66afcca | 2019-06-12 19:33:42 +0200 | [diff] [blame] | 445 | TEST_ASSERT( mbedtls_md_update( &accumulator, header, 2 ) == 0 ); |
| 446 | TEST_ASSERT( mbedtls_md_update( &accumulator, |
| 447 | empty, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 ); |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 448 | |
| 449 | header[0] = 0; |
Gilles Peskine | 66afcca | 2019-06-12 19:33:42 +0200 | [diff] [blame] | 450 | TEST_ASSERT( mbedtls_md_update( &accumulator, header, 2 ) == 0 ); |
| 451 | TEST_ASSERT( mbedtls_md_update( &accumulator, |
| 452 | check_seed, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 ); |
| 453 | TEST_ASSERT( mbedtls_md_finish( &accumulator, buf ) == 0 ); |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 454 | |
Gilles Peskine | 66afcca | 2019-06-12 19:33:42 +0200 | [diff] [blame] | 455 | TEST_ASSERT( mbedtls_md( md_info, buf, MBEDTLS_ENTROPY_BLOCK_SIZE, |
| 456 | check_entropy ) == 0 ); |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 457 | |
| 458 | // Check result of both NV file and entropy received with the manual calculations |
| 459 | TEST_ASSERT( memcmp( check_seed, buffer_seed, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 ); |
| 460 | TEST_ASSERT( memcmp( check_entropy, entropy, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 ); |
| 461 | |
Gilles Peskine | e39b903 | 2019-06-12 19:31:29 +0200 | [diff] [blame] | 462 | exit: |
Gilles Peskine | 66afcca | 2019-06-12 19:33:42 +0200 | [diff] [blame] | 463 | mbedtls_md_free( &accumulator ); |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 464 | mbedtls_entropy_free( &ctx ); |
Gilles Peskine | e39b903 | 2019-06-12 19:31:29 +0200 | [diff] [blame] | 465 | mbedtls_nv_seed_read = original_mbedtls_nv_seed_read; |
| 466 | mbedtls_nv_seed_write = original_mbedtls_nv_seed_write; |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 467 | } |
| 468 | /* END_CASE */ |
| 469 | |
Hanno Becker | d4a872e | 2017-09-07 08:09:33 +0100 | [diff] [blame] | 470 | /* BEGIN_CASE depends_on:ENTROPY_HAVE_STRONG:MBEDTLS_SELF_TEST */ |
Simon Butcher | b7f45c5 | 2016-09-15 18:42:26 +0100 | [diff] [blame] | 471 | void entropy_selftest( int result ) |
Manuel Pégourié-Gonnard | 2c25eb0 | 2014-05-30 10:38:18 +0200 | [diff] [blame] | 472 | { |
Andres AG | 93012e8 | 2016-09-09 09:10:28 +0100 | [diff] [blame] | 473 | TEST_ASSERT( mbedtls_entropy_self_test( 1 ) == result ); |
Manuel Pégourié-Gonnard | 2c25eb0 | 2014-05-30 10:38:18 +0200 | [diff] [blame] | 474 | } |
| 475 | /* END_CASE */ |