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