Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
Gilles Peskine | ef0624f | 2018-08-03 20:23:09 +0200 | [diff] [blame] | 2 | #include "mbedtls/entropy.h" |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 3 | #include "mbedtls/ctr_drbg.h" |
Mohammad Azim Khan | 67735d5 | 2017-04-06 11:55:43 +0100 | [diff] [blame] | 4 | #include "string.h" |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 5 | |
Gilles Peskine | 5ef5a9a | 2018-08-03 20:27:50 +0200 | [diff] [blame] | 6 | /* Modes for ctr_drbg_validate */ |
| 7 | enum reseed_mode |
| 8 | { |
| 9 | RESEED_NEVER, /* never reseed */ |
| 10 | RESEED_FIRST, /* instantiate, reseed, generate, generate */ |
| 11 | RESEED_SECOND, /* instantiate, generate, reseed, generate */ |
| 12 | RESEED_ALWAYS /* prediction resistance, no explicit reseed */ |
| 13 | }; |
| 14 | |
Nir Sonnenschein | 85fcb58 | 2018-08-29 23:38:57 +0300 | [diff] [blame] | 15 | static size_t test_offset_idx = 0; |
| 16 | static size_t test_max_idx = 0; |
Reut Caspi | e278b36 | 2017-10-19 08:49:19 +0100 | [diff] [blame] | 17 | static int mbedtls_test_entropy_func( void *data, unsigned char *buf, size_t len ) |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 18 | { |
Manuel Pégourié-Gonnard | 7575daa | 2014-01-31 12:16:54 +0100 | [diff] [blame] | 19 | const unsigned char *p = (unsigned char *) data; |
Gilles Peskine | ef0624f | 2018-08-03 20:23:09 +0200 | [diff] [blame] | 20 | if( test_offset_idx + len > test_max_idx ) |
| 21 | return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); |
Paul Bakker | 3ddfa66 | 2013-11-26 17:45:20 +0100 | [diff] [blame] | 22 | memcpy( buf, p + test_offset_idx, len ); |
Manuel Pégourié-Gonnard | 7575daa | 2014-01-31 12:16:54 +0100 | [diff] [blame] | 23 | test_offset_idx += len; |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 24 | return( 0 ); |
| 25 | } |
Nir Sonnenschein | 6275be3 | 2018-08-29 10:25:30 +0300 | [diff] [blame] | 26 | |
Nir Sonnenschein | 85fcb58 | 2018-08-29 23:38:57 +0300 | [diff] [blame] | 27 | static void ctr_drbg_validate_internal( int reseed_mode, data_t * nonce, |
Nir Sonnenschein | 6275be3 | 2018-08-29 10:25:30 +0300 | [diff] [blame] | 28 | int entropy_len_arg, data_t * entropy, |
| 29 | data_t * reseed, |
| 30 | data_t * add1, data_t * add2, |
| 31 | data_t * result ) |
| 32 | { |
| 33 | mbedtls_ctr_drbg_context ctx; |
| 34 | unsigned char buf[64]; |
| 35 | |
| 36 | size_t entropy_chunk_len = (size_t) entropy_len_arg; |
| 37 | |
Nir Sonnenschein | 85fcb58 | 2018-08-29 23:38:57 +0300 | [diff] [blame] | 38 | TEST_ASSERT( entropy_chunk_len <= sizeof( buf ) ); |
| 39 | |
Nir Sonnenschein | 6275be3 | 2018-08-29 10:25:30 +0300 | [diff] [blame] | 40 | test_offset_idx = 0; |
| 41 | mbedtls_ctr_drbg_init( &ctx ); |
| 42 | |
| 43 | test_max_idx = entropy->len; |
| 44 | |
Nir Sonnenschein | 85fcb58 | 2018-08-29 23:38:57 +0300 | [diff] [blame] | 45 | /* CTR_DRBG_Instantiate(entropy[:entropy->len], nonce, perso, <ignored>) |
| 46 | * where nonce||perso = nonce[nonce->len] */ |
Gilles Peskine | 50ed86b | 2019-10-04 12:15:55 +0200 | [diff] [blame] | 47 | mbedtls_ctr_drbg_set_entropy_len( &ctx, entropy_chunk_len ); |
Gilles Peskine | 0ed378a | 2019-10-22 20:33:56 +0200 | [diff] [blame^] | 48 | mbedtls_ctr_drbg_set_nonce_len( &ctx, 0 ); |
Gilles Peskine | 50ed86b | 2019-10-04 12:15:55 +0200 | [diff] [blame] | 49 | TEST_ASSERT( mbedtls_ctr_drbg_seed( |
Nir Sonnenschein | 6275be3 | 2018-08-29 10:25:30 +0300 | [diff] [blame] | 50 | &ctx, |
| 51 | mbedtls_test_entropy_func, entropy->x, |
Gilles Peskine | 50ed86b | 2019-10-04 12:15:55 +0200 | [diff] [blame] | 52 | nonce->x, nonce->len ) == 0 ); |
Nir Sonnenschein | 6275be3 | 2018-08-29 10:25:30 +0300 | [diff] [blame] | 53 | if( reseed_mode == RESEED_ALWAYS ) |
| 54 | mbedtls_ctr_drbg_set_prediction_resistance( |
| 55 | &ctx, |
| 56 | MBEDTLS_CTR_DRBG_PR_ON ); |
| 57 | |
| 58 | if( reseed_mode == RESEED_FIRST ) |
| 59 | { |
Nir Sonnenschein | 85fcb58 | 2018-08-29 23:38:57 +0300 | [diff] [blame] | 60 | /* CTR_DRBG_Reseed(entropy[idx:idx+entropy->len], |
| 61 | * reseed[:reseed->len]) */ |
Nir Sonnenschein | 6275be3 | 2018-08-29 10:25:30 +0300 | [diff] [blame] | 62 | TEST_ASSERT( mbedtls_ctr_drbg_reseed( |
| 63 | &ctx, |
| 64 | reseed->x, reseed->len ) == 0 ); |
| 65 | } |
| 66 | |
Nir Sonnenschein | 85fcb58 | 2018-08-29 23:38:57 +0300 | [diff] [blame] | 67 | /* CTR_DRBG_Generate(result->len * 8 bits, add1[:add1->len]) -> buf */ |
Nir Sonnenschein | 6275be3 | 2018-08-29 10:25:30 +0300 | [diff] [blame] | 68 | /* Then reseed if prediction resistance is enabled. */ |
| 69 | TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( |
| 70 | &ctx, |
| 71 | buf, result->len, |
| 72 | add1->x, add1->len ) == 0 ); |
| 73 | |
| 74 | |
| 75 | if( reseed_mode == RESEED_SECOND ) |
| 76 | { |
Nir Sonnenschein | 85fcb58 | 2018-08-29 23:38:57 +0300 | [diff] [blame] | 77 | /* CTR_DRBG_Reseed(entropy[idx:idx+entropy->len], |
| 78 | * reseed[:reseed->len]) */ |
Nir Sonnenschein | 6275be3 | 2018-08-29 10:25:30 +0300 | [diff] [blame] | 79 | TEST_ASSERT( mbedtls_ctr_drbg_reseed( |
| 80 | &ctx, |
| 81 | reseed->x, reseed->len ) == 0 ); |
| 82 | } |
| 83 | |
| 84 | /* CTR_DRBG_Generate(result->len * 8 bits, add2->x[:add2->len]) -> buf */ |
| 85 | /* Then reseed if prediction resistance is enabled. */ |
| 86 | TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( |
| 87 | &ctx, |
| 88 | buf, result->len, |
| 89 | add2->x, add2->len ) == 0 ); |
| 90 | TEST_ASSERT( memcmp( buf, result->x, result->len ) == 0 ); |
| 91 | |
| 92 | exit: |
| 93 | mbedtls_ctr_drbg_free( &ctx ); |
Nir Sonnenschein | 6275be3 | 2018-08-29 10:25:30 +0300 | [diff] [blame] | 94 | } |
| 95 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 96 | /* END_HEADER */ |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 97 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 98 | /* BEGIN_DEPENDENCIES |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 99 | * depends_on:MBEDTLS_CTR_DRBG_C |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 100 | * END_DEPENDENCIES |
| 101 | */ |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 102 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 103 | /* BEGIN_CASE */ |
Nir Sonnenschein | 6275be3 | 2018-08-29 10:25:30 +0300 | [diff] [blame] | 104 | void ctr_drbg_special_behaviours( ) |
Paul Bakker | 185ccf7 | 2016-07-14 13:21:10 +0100 | [diff] [blame] | 105 | { |
| 106 | mbedtls_ctr_drbg_context ctx; |
| 107 | unsigned char output[512]; |
| 108 | unsigned char additional[512]; |
| 109 | |
| 110 | mbedtls_ctr_drbg_init( &ctx ); |
| 111 | memset( output, 0, sizeof( output ) ); |
| 112 | memset( additional, 0, sizeof( additional ) ); |
| 113 | |
| 114 | TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, |
| 115 | output, MBEDTLS_CTR_DRBG_MAX_REQUEST + 1, |
| 116 | additional, 16 ) == |
| 117 | MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG ); |
| 118 | TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, |
| 119 | output, 16, |
| 120 | additional, MBEDTLS_CTR_DRBG_MAX_INPUT + 1 ) == |
| 121 | MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG ); |
| 122 | |
| 123 | TEST_ASSERT( mbedtls_ctr_drbg_reseed( &ctx, additional, |
| 124 | MBEDTLS_CTR_DRBG_MAX_SEED_INPUT + 1 ) == |
| 125 | MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG ); |
Andres Amaya Garcia | 6a54336 | 2017-01-17 23:04:22 +0000 | [diff] [blame] | 126 | |
| 127 | mbedtls_ctr_drbg_set_entropy_len( &ctx, ~0 ); |
| 128 | TEST_ASSERT( mbedtls_ctr_drbg_reseed( &ctx, additional, |
| 129 | MBEDTLS_CTR_DRBG_MAX_SEED_INPUT ) == |
| 130 | MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG ); |
Paul Bakker | 185ccf7 | 2016-07-14 13:21:10 +0100 | [diff] [blame] | 131 | exit: |
| 132 | mbedtls_ctr_drbg_free( &ctx ); |
| 133 | } |
| 134 | /* END_CASE */ |
| 135 | |
Gilles Peskine | 5ef5a9a | 2018-08-03 20:27:50 +0200 | [diff] [blame] | 136 | |
| 137 | /* BEGIN_CASE */ |
Nir Sonnenschein | 6275be3 | 2018-08-29 10:25:30 +0300 | [diff] [blame] | 138 | void ctr_drbg_validate_no_reseed( data_t * add_init, data_t * entropy, |
| 139 | data_t * add1, data_t * add2, |
| 140 | data_t * result_string ) |
Gilles Peskine | 5ef5a9a | 2018-08-03 20:27:50 +0200 | [diff] [blame] | 141 | { |
Nir Sonnenschein | acedc91 | 2018-08-29 23:57:45 +0300 | [diff] [blame] | 142 | data_t empty = { 0, 0 }; |
Nir Sonnenschein | 85fcb58 | 2018-08-29 23:38:57 +0300 | [diff] [blame] | 143 | ctr_drbg_validate_internal( RESEED_NEVER, add_init, |
| 144 | entropy->len, entropy, |
| 145 | &empty, add1, add2, |
| 146 | result_string ); |
| 147 | goto exit; // goto is needed to avoid warning ( no test assertions in func) |
Gilles Peskine | 5ef5a9a | 2018-08-03 20:27:50 +0200 | [diff] [blame] | 148 | } |
| 149 | /* END_CASE */ |
| 150 | |
| 151 | /* BEGIN_CASE */ |
Nir Sonnenschein | 6275be3 | 2018-08-29 10:25:30 +0300 | [diff] [blame] | 152 | void ctr_drbg_validate_pr( data_t * add_init, data_t * entropy, |
| 153 | data_t * add1, data_t * add2, |
| 154 | data_t * result_string ) |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 155 | { |
Nir Sonnenschein | acedc91 | 2018-08-29 23:57:45 +0300 | [diff] [blame] | 156 | data_t empty = { 0, 0 }; |
Nir Sonnenschein | 85fcb58 | 2018-08-29 23:38:57 +0300 | [diff] [blame] | 157 | ctr_drbg_validate_internal( RESEED_ALWAYS, add_init, |
| 158 | entropy->len / 3, entropy, |
| 159 | &empty, add1, add2, |
| 160 | result_string ); |
| 161 | goto exit; // goto is needed to avoid warning ( no test assertions in func) |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 162 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 163 | /* END_CASE */ |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 164 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 165 | /* BEGIN_CASE */ |
Nir Sonnenschein | 85fcb58 | 2018-08-29 23:38:57 +0300 | [diff] [blame] | 166 | void ctr_drbg_validate_reseed_between( data_t * add_init, data_t * entropy, |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 167 | data_t * add1, data_t * add_reseed, |
Nir Sonnenschein | 6275be3 | 2018-08-29 10:25:30 +0300 | [diff] [blame] | 168 | data_t * add2, data_t * result_string ) |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 169 | { |
Nir Sonnenschein | 85fcb58 | 2018-08-29 23:38:57 +0300 | [diff] [blame] | 170 | ctr_drbg_validate_internal( RESEED_SECOND, add_init, |
| 171 | entropy->len / 2, entropy, |
| 172 | add_reseed, add1, add2, |
| 173 | result_string ); |
| 174 | goto exit; // goto is needed to avoid warning ( no test assertions in func) |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 175 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 176 | /* END_CASE */ |
Manuel Pégourié-Gonnard | b3b205e | 2014-01-31 12:04:06 +0100 | [diff] [blame] | 177 | |
Manuel Pégourié-Gonnard | 7575daa | 2014-01-31 12:16:54 +0100 | [diff] [blame] | 178 | /* BEGIN_CASE */ |
Nir Sonnenschein | 85fcb58 | 2018-08-29 23:38:57 +0300 | [diff] [blame] | 179 | void ctr_drbg_validate_reseed_first( data_t * add_init, data_t * entropy, |
| 180 | data_t * add1, data_t * add_reseed, |
| 181 | data_t * add2, data_t * result_string ) |
| 182 | { |
| 183 | ctr_drbg_validate_internal( RESEED_FIRST, add_init, |
| 184 | entropy->len / 2, entropy, |
| 185 | add_reseed, add1, add2, |
| 186 | result_string ); |
| 187 | goto exit; // goto is needed to avoid warning ( no test assertions in func) |
| 188 | } |
| 189 | /* END_CASE */ |
| 190 | |
| 191 | |
| 192 | |
| 193 | /* BEGIN_CASE */ |
Gilles Peskine | c949de0 | 2019-10-22 19:14:26 +0200 | [diff] [blame] | 194 | void ctr_drbg_entropy_usage( int entropy_nonce_len ) |
Manuel Pégourié-Gonnard | 7575daa | 2014-01-31 12:16:54 +0100 | [diff] [blame] | 195 | { |
| 196 | unsigned char out[16]; |
| 197 | unsigned char add[16]; |
| 198 | unsigned char entropy[1024]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 199 | mbedtls_ctr_drbg_context ctx; |
Manuel Pégourié-Gonnard | 7575daa | 2014-01-31 12:16:54 +0100 | [diff] [blame] | 200 | size_t i, reps = 10; |
Gilles Peskine | 58b56ce | 2019-10-22 19:10:01 +0200 | [diff] [blame] | 201 | size_t expected_idx = 0; |
Manuel Pégourié-Gonnard | 7575daa | 2014-01-31 12:16:54 +0100 | [diff] [blame] | 202 | |
Manuel Pégourié-Gonnard | 8d128ef | 2015-04-28 22:38:08 +0200 | [diff] [blame] | 203 | mbedtls_ctr_drbg_init( &ctx ); |
Manuel Pégourié-Gonnard | 7575daa | 2014-01-31 12:16:54 +0100 | [diff] [blame] | 204 | test_offset_idx = 0; |
Gilles Peskine | ef0624f | 2018-08-03 20:23:09 +0200 | [diff] [blame] | 205 | test_max_idx = sizeof( entropy ); |
Manuel Pégourié-Gonnard | 7575daa | 2014-01-31 12:16:54 +0100 | [diff] [blame] | 206 | memset( entropy, 0, sizeof( entropy ) ); |
| 207 | memset( out, 0, sizeof( out ) ); |
| 208 | memset( add, 0, sizeof( add ) ); |
| 209 | |
Gilles Peskine | c949de0 | 2019-10-22 19:14:26 +0200 | [diff] [blame] | 210 | if( entropy_nonce_len >= 0 ) |
| 211 | TEST_ASSERT( mbedtls_ctr_drbg_set_nonce_len( &ctx, entropy_nonce_len ) == 0 ); |
| 212 | |
Manuel Pégourié-Gonnard | 7575daa | 2014-01-31 12:16:54 +0100 | [diff] [blame] | 213 | /* Init must use entropy */ |
Reut Caspi | e278b36 | 2017-10-19 08:49:19 +0100 | [diff] [blame] | 214 | TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctx, mbedtls_test_entropy_func, entropy, NULL, 0 ) == 0 ); |
Gilles Peskine | 58b56ce | 2019-10-22 19:10:01 +0200 | [diff] [blame] | 215 | expected_idx += MBEDTLS_CTR_DRBG_ENTROPY_LEN; |
Gilles Peskine | c949de0 | 2019-10-22 19:14:26 +0200 | [diff] [blame] | 216 | if( entropy_nonce_len >= 0 ) |
| 217 | expected_idx += entropy_nonce_len; |
Gilles Peskine | 58b56ce | 2019-10-22 19:10:01 +0200 | [diff] [blame] | 218 | TEST_EQUAL( test_offset_idx, expected_idx ); |
Manuel Pégourié-Gonnard | 7575daa | 2014-01-31 12:16:54 +0100 | [diff] [blame] | 219 | |
| 220 | /* By default, PR is off and reseed_interval is large, |
| 221 | * so the next few calls should not use entropy */ |
Manuel Pégourié-Gonnard | 7575daa | 2014-01-31 12:16:54 +0100 | [diff] [blame] | 222 | for( i = 0; i < reps; i++ ) |
| 223 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 224 | TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) - 4 ) == 0 ); |
| 225 | TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, out, sizeof( out ) - 4, |
Manuel Pégourié-Gonnard | 7575daa | 2014-01-31 12:16:54 +0100 | [diff] [blame] | 226 | add, sizeof( add ) ) == 0 ); |
| 227 | } |
Gilles Peskine | 58b56ce | 2019-10-22 19:10:01 +0200 | [diff] [blame] | 228 | TEST_EQUAL( test_offset_idx, expected_idx ); |
Manuel Pégourié-Gonnard | 7575daa | 2014-01-31 12:16:54 +0100 | [diff] [blame] | 229 | |
| 230 | /* While at it, make sure we didn't write past the requested length */ |
| 231 | TEST_ASSERT( out[sizeof( out ) - 4] == 0 ); |
| 232 | TEST_ASSERT( out[sizeof( out ) - 3] == 0 ); |
| 233 | TEST_ASSERT( out[sizeof( out ) - 2] == 0 ); |
| 234 | TEST_ASSERT( out[sizeof( out ) - 1] == 0 ); |
| 235 | |
| 236 | /* Set reseed_interval to the number of calls done, |
| 237 | * so the next call should reseed */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 238 | mbedtls_ctr_drbg_set_reseed_interval( &ctx, 2 * reps ); |
| 239 | TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 ); |
Gilles Peskine | 58b56ce | 2019-10-22 19:10:01 +0200 | [diff] [blame] | 240 | expected_idx += MBEDTLS_CTR_DRBG_ENTROPY_LEN; |
| 241 | TEST_EQUAL( test_offset_idx, expected_idx ); |
Manuel Pégourié-Gonnard | 7575daa | 2014-01-31 12:16:54 +0100 | [diff] [blame] | 242 | |
| 243 | /* The new few calls should not reseed */ |
Manuel Pégourié-Gonnard | 7575daa | 2014-01-31 12:16:54 +0100 | [diff] [blame] | 244 | for( i = 0; i < reps / 2; i++ ) |
| 245 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 246 | TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 ); |
| 247 | TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, out, sizeof( out ) , |
Manuel Pégourié-Gonnard | 7575daa | 2014-01-31 12:16:54 +0100 | [diff] [blame] | 248 | add, sizeof( add ) ) == 0 ); |
| 249 | } |
Gilles Peskine | 58b56ce | 2019-10-22 19:10:01 +0200 | [diff] [blame] | 250 | TEST_EQUAL( test_offset_idx, expected_idx ); |
Manuel Pégourié-Gonnard | 7575daa | 2014-01-31 12:16:54 +0100 | [diff] [blame] | 251 | |
Gilles Peskine | d919993 | 2018-09-11 16:41:54 +0200 | [diff] [blame] | 252 | /* Call update with too much data (sizeof entropy > MAX(_SEED)_INPUT). |
| 253 | * Make sure it's detected as an error and doesn't cause memory |
| 254 | * corruption. */ |
| 255 | TEST_ASSERT( mbedtls_ctr_drbg_update_ret( |
| 256 | &ctx, entropy, sizeof( entropy ) ) != 0 ); |
Manuel Pégourié-Gonnard | f5f25b3 | 2014-11-27 14:04:56 +0100 | [diff] [blame] | 257 | |
Manuel Pégourié-Gonnard | 7575daa | 2014-01-31 12:16:54 +0100 | [diff] [blame] | 258 | /* Now enable PR, so the next few calls should all reseed */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 259 | mbedtls_ctr_drbg_set_prediction_resistance( &ctx, MBEDTLS_CTR_DRBG_PR_ON ); |
| 260 | TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 ); |
Gilles Peskine | 58b56ce | 2019-10-22 19:10:01 +0200 | [diff] [blame] | 261 | expected_idx += MBEDTLS_CTR_DRBG_ENTROPY_LEN; |
| 262 | TEST_EQUAL( test_offset_idx, expected_idx ); |
Manuel Pégourié-Gonnard | 7575daa | 2014-01-31 12:16:54 +0100 | [diff] [blame] | 263 | |
| 264 | /* Finally, check setting entropy_len */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 265 | mbedtls_ctr_drbg_set_entropy_len( &ctx, 42 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 266 | TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 ); |
Gilles Peskine | 58b56ce | 2019-10-22 19:10:01 +0200 | [diff] [blame] | 267 | expected_idx += 42; |
| 268 | TEST_EQUAL( test_offset_idx, expected_idx ); |
Manuel Pégourié-Gonnard | 7575daa | 2014-01-31 12:16:54 +0100 | [diff] [blame] | 269 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 270 | mbedtls_ctr_drbg_set_entropy_len( &ctx, 13 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 271 | TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 ); |
Gilles Peskine | 58b56ce | 2019-10-22 19:10:01 +0200 | [diff] [blame] | 272 | expected_idx += 13; |
| 273 | TEST_EQUAL( test_offset_idx, expected_idx ); |
Paul Bakker | a317a98 | 2014-06-18 16:44:11 +0200 | [diff] [blame] | 274 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 275 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 276 | mbedtls_ctr_drbg_free( &ctx ); |
Manuel Pégourié-Gonnard | 7575daa | 2014-01-31 12:16:54 +0100 | [diff] [blame] | 277 | } |
| 278 | /* END_CASE */ |
| 279 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 280 | /* BEGIN_CASE depends_on:MBEDTLS_FS_IO */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 281 | void ctr_drbg_seed_file( char * path, int ret ) |
Manuel Pégourié-Gonnard | 7575daa | 2014-01-31 12:16:54 +0100 | [diff] [blame] | 282 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 283 | mbedtls_ctr_drbg_context ctx; |
Manuel Pégourié-Gonnard | 7575daa | 2014-01-31 12:16:54 +0100 | [diff] [blame] | 284 | |
Manuel Pégourié-Gonnard | 8d128ef | 2015-04-28 22:38:08 +0200 | [diff] [blame] | 285 | mbedtls_ctr_drbg_init( &ctx ); |
| 286 | |
| 287 | TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctx, rnd_std_rand, NULL, NULL, 0 ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 288 | TEST_ASSERT( mbedtls_ctr_drbg_write_seed_file( &ctx, path ) == ret ); |
| 289 | TEST_ASSERT( mbedtls_ctr_drbg_update_seed_file( &ctx, path ) == ret ); |
Paul Bakker | a317a98 | 2014-06-18 16:44:11 +0200 | [diff] [blame] | 290 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 291 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 292 | mbedtls_ctr_drbg_free( &ctx ); |
Manuel Pégourié-Gonnard | 7575daa | 2014-01-31 12:16:54 +0100 | [diff] [blame] | 293 | } |
| 294 | /* END_CASE */ |
| 295 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 296 | /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 297 | void ctr_drbg_selftest( ) |
Manuel Pégourié-Gonnard | b3b205e | 2014-01-31 12:04:06 +0100 | [diff] [blame] | 298 | { |
Andres AG | 93012e8 | 2016-09-09 09:10:28 +0100 | [diff] [blame] | 299 | TEST_ASSERT( mbedtls_ctr_drbg_self_test( 1 ) == 0 ); |
Manuel Pégourié-Gonnard | b3b205e | 2014-01-31 12:04:06 +0100 | [diff] [blame] | 300 | } |
| 301 | /* END_CASE */ |