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" |
Chris Jones | ea0a865 | 2021-03-09 19:11:19 +0000 | [diff] [blame^] | 3 | #include "entropy_poll.h" |
Gilles Peskine | 72d40fc | 2020-04-14 21:28:42 +0200 | [diff] [blame] | 4 | #include "mbedtls/md.h" |
Mohammad Azim Khan | 67735d5 | 2017-04-06 11:55:43 +0100 | [diff] [blame] | 5 | #include "string.h" |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 6 | |
Gilles Peskine | ed04a67 | 2019-10-08 14:37:27 +0200 | [diff] [blame] | 7 | typedef 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 | |
| 14 | typedef 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é-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 20 | |
| 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 Peskine | ed04a67 | 2019-10-08 14:37:27 +0200 | [diff] [blame] | 27 | static int entropy_dummy_source( void *arg, unsigned char *output, |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 28 | size_t len, size_t *olen ) |
| 29 | { |
Gilles Peskine | ed04a67 | 2019-10-08 14:37:27 +0200 | [diff] [blame] | 30 | entropy_dummy_context *context = arg; |
| 31 | ++context->calls; |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 32 | |
Gilles Peskine | ed04a67 | 2019-10-08 14:37:27 +0200 | [diff] [blame] | 33 | switch( context->instruction ) |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 34 | { |
Gilles Peskine | ed04a67 | 2019-10-08 14:37:27 +0200 | [diff] [blame] | 35 | 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é-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 42 | return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | memset( output, 0x2a, *olen ); |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 46 | return( 0 ); |
| 47 | } |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 48 | |
| 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 Peskine | 7f24651 | 2019-10-08 14:51:49 +0200 | [diff] [blame] | 60 | static void entropy_clear_sources( mbedtls_entropy_context *ctx ) |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 61 | { |
| 62 | ctx->source_count = 0; |
| 63 | } |
| 64 | |
Gilles Peskine | 7f24651 | 2019-10-08 14:51:49 +0200 | [diff] [blame] | 65 | #if defined(MBEDTLS_ENTROPY_NV_SEED) |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 66 | /* |
| 67 | * NV seed read/write functions that use a buffer instead of a file |
| 68 | */ |
| 69 | static unsigned char buffer_seed[MBEDTLS_ENTROPY_BLOCK_SIZE]; |
| 70 | |
Jaeden Amero | f7dca86 | 2019-06-27 17:31:33 +0100 | [diff] [blame] | 71 | int buffer_nv_seed_read( unsigned char *buf, size_t buf_len ) |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 72 | { |
| 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 Amero | f7dca86 | 2019-06-27 17:31:33 +0100 | [diff] [blame] | 80 | int buffer_nv_seed_write( unsigned char *buf, size_t buf_len ) |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 81 | { |
| 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 | */ |
| 92 | static 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 Amero | f7dca86 | 2019-06-27 17:31:33 +0100 | [diff] [blame] | 111 | int read_nv_seed( unsigned char *buf, size_t buf_len ) |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 112 | { |
| 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 Bakker | 4a6c6fc | 2016-06-01 16:34:25 +0100 | [diff] [blame] | 129 | #endif /* MBEDTLS_ENTROPY_NV_SEED */ |
Manuel Pégourié-Gonnard | 2c25eb0 | 2014-05-30 10:38:18 +0200 | [diff] [blame] | 130 | /* END_HEADER */ |
| 131 | |
| 132 | /* BEGIN_DEPENDENCIES |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 133 | * depends_on:MBEDTLS_ENTROPY_C |
Manuel Pégourié-Gonnard | 2c25eb0 | 2014-05-30 10:38:18 +0200 | [diff] [blame] | 134 | * END_DEPENDENCIES |
| 135 | */ |
| 136 | |
Simon Butcher | b7f45c5 | 2016-09-15 18:42:26 +0100 | [diff] [blame] | 137 | /* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 138 | void entropy_seed_file( char * path, int ret ) |
Manuel Pégourié-Gonnard | 2c25eb0 | 2014-05-30 10:38:18 +0200 | [diff] [blame] | 139 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 140 | mbedtls_entropy_context ctx; |
Manuel Pégourié-Gonnard | 2c25eb0 | 2014-05-30 10:38:18 +0200 | [diff] [blame] | 141 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 142 | mbedtls_entropy_init( &ctx ); |
Manuel Pégourié-Gonnard | 2c25eb0 | 2014-05-30 10:38:18 +0200 | [diff] [blame] | 143 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 144 | TEST_ASSERT( mbedtls_entropy_write_seed_file( &ctx, path ) == ret ); |
| 145 | TEST_ASSERT( mbedtls_entropy_update_seed_file( &ctx, path ) == ret ); |
Manuel Pégourié-Gonnard | 2c25eb0 | 2014-05-30 10:38:18 +0200 | [diff] [blame] | 146 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 147 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 148 | mbedtls_entropy_free( &ctx ); |
Manuel Pégourié-Gonnard | 2c25eb0 | 2014-05-30 10:38:18 +0200 | [diff] [blame] | 149 | } |
| 150 | /* END_CASE */ |
| 151 | |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 152 | /* BEGIN_CASE */ |
Gilles Peskine | 7f24651 | 2019-10-08 14:51:49 +0200 | [diff] [blame] | 153 | void 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 | |
| 163 | exit: |
| 164 | mbedtls_entropy_free( &ctx ); |
| 165 | } |
| 166 | /* END_CASE */ |
| 167 | |
| 168 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 169 | void entropy_too_many_sources( ) |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 170 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 171 | mbedtls_entropy_context ctx; |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 172 | size_t i; |
Gilles Peskine | ed04a67 | 2019-10-08 14:37:27 +0200 | [diff] [blame] | 173 | entropy_dummy_context dummy = {DUMMY_REQUESTED_LENGTH, 0, 0}; |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 174 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 175 | mbedtls_entropy_init( &ctx ); |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 176 | |
| 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é-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 181 | for( i = 0; i < MBEDTLS_ENTROPY_MAX_SOURCES; i++ ) |
Gilles Peskine | ed04a67 | 2019-10-08 14:37:27 +0200 | [diff] [blame] | 182 | (void) mbedtls_entropy_add_source( &ctx, entropy_dummy_source, &dummy, |
Manuel Pégourié-Gonnard | 7580ba4 | 2015-06-19 10:26:32 +0200 | [diff] [blame] | 183 | 16, MBEDTLS_ENTROPY_SOURCE_WEAK ); |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 184 | |
Gilles Peskine | ed04a67 | 2019-10-08 14:37:27 +0200 | [diff] [blame] | 185 | 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] | 186 | 16, MBEDTLS_ENTROPY_SOURCE_WEAK ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 187 | == MBEDTLS_ERR_ENTROPY_MAX_SOURCES ); |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 188 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 189 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 190 | mbedtls_entropy_free( &ctx ); |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 191 | } |
| 192 | /* END_CASE */ |
| 193 | |
Hanno Becker | d4a872e | 2017-09-07 08:09:33 +0100 | [diff] [blame] | 194 | /* BEGIN_CASE depends_on:ENTROPY_HAVE_STRONG */ |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 195 | void entropy_func_len( int len, int ret ) |
| 196 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 197 | 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é-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 200 | size_t i, j; |
| 201 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 202 | mbedtls_entropy_init( &ctx ); |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 203 | |
| 204 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 205 | * See comments in mbedtls_entropy_self_test() |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 206 | */ |
| 207 | for( i = 0; i < 8; i++ ) |
| 208 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 209 | TEST_ASSERT( mbedtls_entropy_func( &ctx, buf, len ) == ret ); |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 210 | 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 Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 224 | void entropy_source_fail( char * path ) |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 225 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 226 | mbedtls_entropy_context ctx; |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 227 | unsigned char buf[16]; |
Gilles Peskine | ed04a67 | 2019-10-08 14:37:27 +0200 | [diff] [blame] | 228 | entropy_dummy_context dummy = {DUMMY_FAIL, 0, 0}; |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 229 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 230 | mbedtls_entropy_init( &ctx ); |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 231 | |
Manuel Pégourié-Gonnard | 7580ba4 | 2015-06-19 10:26:32 +0200 | [diff] [blame] | 232 | TEST_ASSERT( mbedtls_entropy_add_source( &ctx, entropy_dummy_source, |
Gilles Peskine | ed04a67 | 2019-10-08 14:37:27 +0200 | [diff] [blame] | 233 | &dummy, 16, |
Manuel Pégourié-Gonnard | 7580ba4 | 2015-06-19 10:26:32 +0200 | [diff] [blame] | 234 | MBEDTLS_ENTROPY_SOURCE_WEAK ) |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 235 | == 0 ); |
| 236 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 237 | 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 Butcher | b7f45c5 | 2016-09-15 18:42:26 +0100 | [diff] [blame] | 241 | #if defined(MBEDTLS_FS_IO) && defined(MBEDTLS_ENTROPY_NV_SEED) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 242 | 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é-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 246 | #else |
| 247 | ((void) path); |
| 248 | #endif |
| 249 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 250 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 251 | mbedtls_entropy_free( &ctx ); |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 252 | } |
| 253 | /* END_CASE */ |
| 254 | |
Gilles Peskine | cbd91e0 | 2019-11-25 19:50:54 +0100 | [diff] [blame] | 255 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 256 | void entropy_threshold( int threshold, int chunk_size, int result ) |
| 257 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 258 | mbedtls_entropy_context ctx; |
Gilles Peskine | cbd91e0 | 2019-11-25 19:50:54 +0100 | [diff] [blame] | 259 | 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é-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 262 | unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 }; |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 263 | int ret; |
| 264 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 265 | mbedtls_entropy_init( &ctx ); |
Gilles Peskine | cbd91e0 | 2019-11-25 19:50:54 +0100 | [diff] [blame] | 266 | entropy_clear_sources( &ctx ); |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 267 | |
Gilles Peskine | cbd91e0 | 2019-11-25 19:50:54 +0100 | [diff] [blame] | 268 | /* Set strong source that reaches its threshold immediately and |
| 269 | * a weak source whose threshold is a test parameter. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 270 | TEST_ASSERT( mbedtls_entropy_add_source( &ctx, entropy_dummy_source, |
Gilles Peskine | cbd91e0 | 2019-11-25 19:50:54 +0100 | [diff] [blame] | 271 | &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é-Gonnard | 7580ba4 | 2015-06-19 10:26:32 +0200 | [diff] [blame] | 275 | MBEDTLS_ENTROPY_SOURCE_WEAK ) == 0 ); |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 276 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 277 | ret = mbedtls_entropy_func( &ctx, buf, sizeof( buf ) ); |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 278 | |
| 279 | if( result >= 0 ) |
| 280 | { |
| 281 | TEST_ASSERT( ret == 0 ); |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 282 | #if defined(MBEDTLS_ENTROPY_NV_SEED) |
Gilles Peskine | ae67939 | 2019-11-25 18:26:23 +0100 | [diff] [blame] | 283 | /* If the NV seed functionality is enabled, there are two entropy |
| 284 | * updates: before and after updating the NV seed. */ |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 285 | result *= 2; |
| 286 | #endif |
Gilles Peskine | cbd91e0 | 2019-11-25 19:50:54 +0100 | [diff] [blame] | 287 | TEST_ASSERT( weak.calls == (size_t) result ); |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 288 | } |
| 289 | else |
| 290 | { |
| 291 | TEST_ASSERT( ret == result ); |
| 292 | } |
| 293 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 294 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 295 | mbedtls_entropy_free( &ctx ); |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 296 | } |
| 297 | /* END_CASE */ |
| 298 | |
Gilles Peskine | 65fc068 | 2019-10-08 15:01:34 +0200 | [diff] [blame] | 299 | /* BEGIN_CASE */ |
| 300 | void 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 Peskine | ae67939 | 2019-11-25 18:26:23 +0100 | [diff] [blame] | 330 | #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 Peskine | 65fc068 | 2019-10-08 15:01:34 +0200 | [diff] [blame] | 335 | TEST_ASSERT( dummy1.calls == (size_t) result ); |
| 336 | } |
| 337 | else |
| 338 | { |
| 339 | TEST_ASSERT( ret == result ); |
| 340 | } |
| 341 | |
| 342 | exit: |
| 343 | mbedtls_entropy_free( &ctx ); |
| 344 | } |
| 345 | /* END_CASE */ |
| 346 | |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 347 | /* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 348 | void nv_seed_file_create( ) |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 349 | { |
| 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 Bakker | b598c29 | 2016-06-01 16:57:11 +0100 | [diff] [blame] | 358 | /* 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] | 359 | void entropy_nv_seed_std_io( ) |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 360 | { |
| 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 Peskine | 66afcca | 2019-06-12 19:33:42 +0200 | [diff] [blame] | 388 | /* 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] | 389 | void entropy_nv_seed( data_t * read_seed ) |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 390 | { |
Gilles Peskine | 66afcca | 2019-06-12 19:33:42 +0200 | [diff] [blame] | 391 | #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 Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 401 | mbedtls_entropy_context ctx; |
Gilles Peskine | e39b903 | 2019-06-12 19:31:29 +0200 | [diff] [blame] | 402 | 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 Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 406 | |
| 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 Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 411 | 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 Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 416 | 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 Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 420 | // 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 Peskine | 66afcca | 2019-06-12 19:33:42 +0200 | [diff] [blame] | 423 | mbedtls_md_init( &accumulator ); |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 424 | 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 Peskine | 66afcca | 2019-06-12 19:33:42 +0200 | [diff] [blame] | 431 | // 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 Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 435 | // Do an entropy run |
| 436 | TEST_ASSERT( mbedtls_entropy_func( &ctx, entropy, sizeof( entropy ) ) == 0 ); |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 437 | // Determine what should have happened with manual entropy internal logic |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 438 | |
| 439 | // Init accumulator |
| 440 | header[1] = MBEDTLS_ENTROPY_BLOCK_SIZE; |
Gilles Peskine | 66afcca | 2019-06-12 19:33:42 +0200 | [diff] [blame] | 441 | TEST_ASSERT( mbedtls_md_setup( &accumulator, md_info, 0 ) == 0 ); |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 442 | |
| 443 | // First run for updating write_seed |
| 444 | header[0] = 0; |
Gilles Peskine | 66afcca | 2019-06-12 19:33:42 +0200 | [diff] [blame] | 445 | 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 Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 450 | |
Gilles Peskine | 66afcca | 2019-06-12 19:33:42 +0200 | [diff] [blame] | 451 | TEST_ASSERT( mbedtls_md_starts( &accumulator ) == 0 ); |
| 452 | TEST_ASSERT( mbedtls_md_update( &accumulator, |
| 453 | buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 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_seed ) == 0 ); |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 457 | |
| 458 | // Second run for actual entropy (triggers mbedtls_entropy_update_nv_seed) |
| 459 | header[0] = MBEDTLS_ENTROPY_SOURCE_MANUAL; |
Gilles Peskine | 66afcca | 2019-06-12 19:33:42 +0200 | [diff] [blame] | 460 | TEST_ASSERT( mbedtls_md_update( &accumulator, header, 2 ) == 0 ); |
| 461 | TEST_ASSERT( mbedtls_md_update( &accumulator, |
| 462 | empty, MBEDTLS_ENTROPY_BLOCK_SIZE ) == 0 ); |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 463 | |
| 464 | header[0] = 0; |
Gilles Peskine | 66afcca | 2019-06-12 19:33:42 +0200 | [diff] [blame] | 465 | 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 Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 469 | |
Gilles Peskine | 66afcca | 2019-06-12 19:33:42 +0200 | [diff] [blame] | 470 | TEST_ASSERT( mbedtls_md( md_info, buf, MBEDTLS_ENTROPY_BLOCK_SIZE, |
| 471 | check_entropy ) == 0 ); |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 472 | |
| 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 Peskine | e39b903 | 2019-06-12 19:31:29 +0200 | [diff] [blame] | 477 | exit: |
Gilles Peskine | 66afcca | 2019-06-12 19:33:42 +0200 | [diff] [blame] | 478 | mbedtls_md_free( &accumulator ); |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 479 | mbedtls_entropy_free( &ctx ); |
Gilles Peskine | e39b903 | 2019-06-12 19:31:29 +0200 | [diff] [blame] | 480 | mbedtls_nv_seed_read = original_mbedtls_nv_seed_read; |
| 481 | mbedtls_nv_seed_write = original_mbedtls_nv_seed_write; |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 482 | } |
| 483 | /* END_CASE */ |
| 484 | |
Hanno Becker | d4a872e | 2017-09-07 08:09:33 +0100 | [diff] [blame] | 485 | /* BEGIN_CASE depends_on:ENTROPY_HAVE_STRONG:MBEDTLS_SELF_TEST */ |
Simon Butcher | b7f45c5 | 2016-09-15 18:42:26 +0100 | [diff] [blame] | 486 | void entropy_selftest( int result ) |
Manuel Pégourié-Gonnard | 2c25eb0 | 2014-05-30 10:38:18 +0200 | [diff] [blame] | 487 | { |
Andres AG | 93012e8 | 2016-09-09 09:10:28 +0100 | [diff] [blame] | 488 | TEST_ASSERT( mbedtls_entropy_self_test( 1 ) == result ); |
Manuel Pégourié-Gonnard | 2c25eb0 | 2014-05-30 10:38:18 +0200 | [diff] [blame] | 489 | } |
| 490 | /* END_CASE */ |