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