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