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