mbed TLS Build
tests/suites/test_suite_ctr_drbg.function@1:1a219dea6cb5, 2019-06-04 (annotated)
- Committer:
- williequesada
- Date:
- Tue Jun 04 16:03:38 2019 +0000
- Revision:
- 1:1a219dea6cb5
- Parent:
- 0:cdf462088d13
compartir a Pablo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
markrad | 0:cdf462088d13 | 1 | /* BEGIN_HEADER */ |
markrad | 0:cdf462088d13 | 2 | #include "mbedtls/ctr_drbg.h" |
markrad | 0:cdf462088d13 | 3 | |
markrad | 0:cdf462088d13 | 4 | int test_offset_idx; |
markrad | 0:cdf462088d13 | 5 | int mbedtls_entropy_func( void *data, unsigned char *buf, size_t len ) |
markrad | 0:cdf462088d13 | 6 | { |
markrad | 0:cdf462088d13 | 7 | const unsigned char *p = (unsigned char *) data; |
markrad | 0:cdf462088d13 | 8 | memcpy( buf, p + test_offset_idx, len ); |
markrad | 0:cdf462088d13 | 9 | test_offset_idx += len; |
markrad | 0:cdf462088d13 | 10 | return( 0 ); |
markrad | 0:cdf462088d13 | 11 | } |
markrad | 0:cdf462088d13 | 12 | /* END_HEADER */ |
markrad | 0:cdf462088d13 | 13 | |
markrad | 0:cdf462088d13 | 14 | /* BEGIN_DEPENDENCIES |
markrad | 0:cdf462088d13 | 15 | * depends_on:MBEDTLS_CTR_DRBG_C |
markrad | 0:cdf462088d13 | 16 | * END_DEPENDENCIES |
markrad | 0:cdf462088d13 | 17 | */ |
markrad | 0:cdf462088d13 | 18 | |
markrad | 0:cdf462088d13 | 19 | /* BEGIN_CASE */ |
markrad | 0:cdf462088d13 | 20 | void ctr_drbg_special_behaviours( ) |
markrad | 0:cdf462088d13 | 21 | { |
markrad | 0:cdf462088d13 | 22 | mbedtls_ctr_drbg_context ctx; |
markrad | 0:cdf462088d13 | 23 | unsigned char output[512]; |
markrad | 0:cdf462088d13 | 24 | unsigned char additional[512]; |
markrad | 0:cdf462088d13 | 25 | |
markrad | 0:cdf462088d13 | 26 | mbedtls_ctr_drbg_init( &ctx ); |
markrad | 0:cdf462088d13 | 27 | memset( output, 0, sizeof( output ) ); |
markrad | 0:cdf462088d13 | 28 | memset( additional, 0, sizeof( additional ) ); |
markrad | 0:cdf462088d13 | 29 | |
markrad | 0:cdf462088d13 | 30 | TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, |
markrad | 0:cdf462088d13 | 31 | output, MBEDTLS_CTR_DRBG_MAX_REQUEST + 1, |
markrad | 0:cdf462088d13 | 32 | additional, 16 ) == |
markrad | 0:cdf462088d13 | 33 | MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG ); |
markrad | 0:cdf462088d13 | 34 | TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, |
markrad | 0:cdf462088d13 | 35 | output, 16, |
markrad | 0:cdf462088d13 | 36 | additional, MBEDTLS_CTR_DRBG_MAX_INPUT + 1 ) == |
markrad | 0:cdf462088d13 | 37 | MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG ); |
markrad | 0:cdf462088d13 | 38 | |
markrad | 0:cdf462088d13 | 39 | TEST_ASSERT( mbedtls_ctr_drbg_reseed( &ctx, additional, |
markrad | 0:cdf462088d13 | 40 | MBEDTLS_CTR_DRBG_MAX_SEED_INPUT + 1 ) == |
markrad | 0:cdf462088d13 | 41 | MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG ); |
markrad | 0:cdf462088d13 | 42 | exit: |
markrad | 0:cdf462088d13 | 43 | mbedtls_ctr_drbg_free( &ctx ); |
markrad | 0:cdf462088d13 | 44 | } |
markrad | 0:cdf462088d13 | 45 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 46 | |
markrad | 0:cdf462088d13 | 47 | /* BEGIN_CASE */ |
markrad | 0:cdf462088d13 | 48 | void ctr_drbg_validate_pr( char *add_init_string, char *entropy_string, |
markrad | 0:cdf462088d13 | 49 | char *add1_string, char *add2_string, |
markrad | 0:cdf462088d13 | 50 | char *result_str ) |
markrad | 0:cdf462088d13 | 51 | { |
markrad | 0:cdf462088d13 | 52 | unsigned char entropy[512]; |
markrad | 0:cdf462088d13 | 53 | unsigned char add_init[512]; |
markrad | 0:cdf462088d13 | 54 | unsigned char add1[512]; |
markrad | 0:cdf462088d13 | 55 | unsigned char add2[512]; |
markrad | 0:cdf462088d13 | 56 | mbedtls_ctr_drbg_context ctx; |
markrad | 0:cdf462088d13 | 57 | unsigned char buf[512]; |
markrad | 0:cdf462088d13 | 58 | unsigned char output_str[512]; |
markrad | 0:cdf462088d13 | 59 | int add_init_len, add1_len, add2_len; |
markrad | 0:cdf462088d13 | 60 | |
markrad | 0:cdf462088d13 | 61 | mbedtls_ctr_drbg_init( &ctx ); |
markrad | 0:cdf462088d13 | 62 | memset( output_str, 0, 512 ); |
markrad | 0:cdf462088d13 | 63 | |
markrad | 0:cdf462088d13 | 64 | unhexify( entropy, entropy_string ); |
markrad | 0:cdf462088d13 | 65 | add_init_len = unhexify( add_init, add_init_string ); |
markrad | 0:cdf462088d13 | 66 | add1_len = unhexify( add1, add1_string ); |
markrad | 0:cdf462088d13 | 67 | add2_len = unhexify( add2, add2_string ); |
markrad | 0:cdf462088d13 | 68 | |
markrad | 0:cdf462088d13 | 69 | test_offset_idx = 0; |
markrad | 0:cdf462088d13 | 70 | TEST_ASSERT( mbedtls_ctr_drbg_seed_entropy_len( &ctx, mbedtls_entropy_func, entropy, add_init, add_init_len, 32 ) == 0 ); |
markrad | 0:cdf462088d13 | 71 | mbedtls_ctr_drbg_set_prediction_resistance( &ctx, MBEDTLS_CTR_DRBG_PR_ON ); |
markrad | 0:cdf462088d13 | 72 | |
markrad | 0:cdf462088d13 | 73 | TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, buf, 16, add1, add1_len ) == 0 ); |
markrad | 0:cdf462088d13 | 74 | TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, buf, 16, add2, add2_len ) == 0 ); |
markrad | 0:cdf462088d13 | 75 | hexify( output_str, buf, 16 ); |
markrad | 0:cdf462088d13 | 76 | TEST_ASSERT( strcmp( (char *) output_str, result_str ) == 0 ); |
markrad | 0:cdf462088d13 | 77 | |
markrad | 0:cdf462088d13 | 78 | exit: |
markrad | 0:cdf462088d13 | 79 | mbedtls_ctr_drbg_free( &ctx ); |
markrad | 0:cdf462088d13 | 80 | } |
markrad | 0:cdf462088d13 | 81 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 82 | |
markrad | 0:cdf462088d13 | 83 | /* BEGIN_CASE */ |
markrad | 0:cdf462088d13 | 84 | void ctr_drbg_validate_nopr( char *add_init_string, char *entropy_string, |
markrad | 0:cdf462088d13 | 85 | char *add1_string, char *add_reseed_string, |
markrad | 0:cdf462088d13 | 86 | char *add2_string, char *result_str ) |
markrad | 0:cdf462088d13 | 87 | { |
markrad | 0:cdf462088d13 | 88 | unsigned char entropy[512]; |
markrad | 0:cdf462088d13 | 89 | unsigned char add_init[512]; |
markrad | 0:cdf462088d13 | 90 | unsigned char add1[512]; |
markrad | 0:cdf462088d13 | 91 | unsigned char add_reseed[512]; |
markrad | 0:cdf462088d13 | 92 | unsigned char add2[512]; |
markrad | 0:cdf462088d13 | 93 | mbedtls_ctr_drbg_context ctx; |
markrad | 0:cdf462088d13 | 94 | unsigned char buf[512]; |
markrad | 0:cdf462088d13 | 95 | unsigned char output_str[512]; |
markrad | 0:cdf462088d13 | 96 | int add_init_len, add1_len, add_reseed_len, add2_len; |
markrad | 0:cdf462088d13 | 97 | |
markrad | 0:cdf462088d13 | 98 | mbedtls_ctr_drbg_init( &ctx ); |
markrad | 0:cdf462088d13 | 99 | memset( output_str, 0, 512 ); |
markrad | 0:cdf462088d13 | 100 | |
markrad | 0:cdf462088d13 | 101 | unhexify( entropy, entropy_string ); |
markrad | 0:cdf462088d13 | 102 | add_init_len = unhexify( add_init, add_init_string ); |
markrad | 0:cdf462088d13 | 103 | add1_len = unhexify( add1, add1_string ); |
markrad | 0:cdf462088d13 | 104 | add_reseed_len = unhexify( add_reseed, add_reseed_string ); |
markrad | 0:cdf462088d13 | 105 | add2_len = unhexify( add2, add2_string ); |
markrad | 0:cdf462088d13 | 106 | |
markrad | 0:cdf462088d13 | 107 | test_offset_idx = 0; |
markrad | 0:cdf462088d13 | 108 | TEST_ASSERT( mbedtls_ctr_drbg_seed_entropy_len( &ctx, mbedtls_entropy_func, entropy, add_init, add_init_len, 32 ) == 0 ); |
markrad | 0:cdf462088d13 | 109 | |
markrad | 0:cdf462088d13 | 110 | TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, buf, 16, add1, add1_len ) == 0 ); |
markrad | 0:cdf462088d13 | 111 | TEST_ASSERT( mbedtls_ctr_drbg_reseed( &ctx, add_reseed, add_reseed_len ) == 0 ); |
markrad | 0:cdf462088d13 | 112 | TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, buf, 16, add2, add2_len ) == 0 ); |
markrad | 0:cdf462088d13 | 113 | hexify( output_str, buf, 16 ); |
markrad | 0:cdf462088d13 | 114 | TEST_ASSERT( strcmp( (char *) output_str, result_str ) == 0 ); |
markrad | 0:cdf462088d13 | 115 | |
markrad | 0:cdf462088d13 | 116 | exit: |
markrad | 0:cdf462088d13 | 117 | mbedtls_ctr_drbg_free( &ctx ); |
markrad | 0:cdf462088d13 | 118 | } |
markrad | 0:cdf462088d13 | 119 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 120 | |
markrad | 0:cdf462088d13 | 121 | /* BEGIN_CASE */ |
markrad | 0:cdf462088d13 | 122 | void ctr_drbg_entropy_usage( ) |
markrad | 0:cdf462088d13 | 123 | { |
markrad | 0:cdf462088d13 | 124 | unsigned char out[16]; |
markrad | 0:cdf462088d13 | 125 | unsigned char add[16]; |
markrad | 0:cdf462088d13 | 126 | unsigned char entropy[1024]; |
markrad | 0:cdf462088d13 | 127 | mbedtls_ctr_drbg_context ctx; |
markrad | 0:cdf462088d13 | 128 | size_t i, reps = 10; |
markrad | 0:cdf462088d13 | 129 | int last_idx; |
markrad | 0:cdf462088d13 | 130 | |
markrad | 0:cdf462088d13 | 131 | mbedtls_ctr_drbg_init( &ctx ); |
markrad | 0:cdf462088d13 | 132 | test_offset_idx = 0; |
markrad | 0:cdf462088d13 | 133 | memset( entropy, 0, sizeof( entropy ) ); |
markrad | 0:cdf462088d13 | 134 | memset( out, 0, sizeof( out ) ); |
markrad | 0:cdf462088d13 | 135 | memset( add, 0, sizeof( add ) ); |
markrad | 0:cdf462088d13 | 136 | |
markrad | 0:cdf462088d13 | 137 | /* Init must use entropy */ |
markrad | 0:cdf462088d13 | 138 | last_idx = test_offset_idx; |
markrad | 0:cdf462088d13 | 139 | TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctx, mbedtls_entropy_func, entropy, NULL, 0 ) == 0 ); |
markrad | 0:cdf462088d13 | 140 | TEST_ASSERT( last_idx < test_offset_idx ); |
markrad | 0:cdf462088d13 | 141 | |
markrad | 0:cdf462088d13 | 142 | /* By default, PR is off and reseed_interval is large, |
markrad | 0:cdf462088d13 | 143 | * so the next few calls should not use entropy */ |
markrad | 0:cdf462088d13 | 144 | last_idx = test_offset_idx; |
markrad | 0:cdf462088d13 | 145 | for( i = 0; i < reps; i++ ) |
markrad | 0:cdf462088d13 | 146 | { |
markrad | 0:cdf462088d13 | 147 | TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) - 4 ) == 0 ); |
markrad | 0:cdf462088d13 | 148 | TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, out, sizeof( out ) - 4, |
markrad | 0:cdf462088d13 | 149 | add, sizeof( add ) ) == 0 ); |
markrad | 0:cdf462088d13 | 150 | } |
markrad | 0:cdf462088d13 | 151 | TEST_ASSERT( last_idx == test_offset_idx ); |
markrad | 0:cdf462088d13 | 152 | |
markrad | 0:cdf462088d13 | 153 | /* While at it, make sure we didn't write past the requested length */ |
markrad | 0:cdf462088d13 | 154 | TEST_ASSERT( out[sizeof( out ) - 4] == 0 ); |
markrad | 0:cdf462088d13 | 155 | TEST_ASSERT( out[sizeof( out ) - 3] == 0 ); |
markrad | 0:cdf462088d13 | 156 | TEST_ASSERT( out[sizeof( out ) - 2] == 0 ); |
markrad | 0:cdf462088d13 | 157 | TEST_ASSERT( out[sizeof( out ) - 1] == 0 ); |
markrad | 0:cdf462088d13 | 158 | |
markrad | 0:cdf462088d13 | 159 | /* Set reseed_interval to the number of calls done, |
markrad | 0:cdf462088d13 | 160 | * so the next call should reseed */ |
markrad | 0:cdf462088d13 | 161 | mbedtls_ctr_drbg_set_reseed_interval( &ctx, 2 * reps ); |
markrad | 0:cdf462088d13 | 162 | TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 ); |
markrad | 0:cdf462088d13 | 163 | TEST_ASSERT( last_idx < test_offset_idx ); |
markrad | 0:cdf462088d13 | 164 | |
markrad | 0:cdf462088d13 | 165 | /* The new few calls should not reseed */ |
markrad | 0:cdf462088d13 | 166 | last_idx = test_offset_idx; |
markrad | 0:cdf462088d13 | 167 | for( i = 0; i < reps / 2; i++ ) |
markrad | 0:cdf462088d13 | 168 | { |
markrad | 0:cdf462088d13 | 169 | TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 ); |
markrad | 0:cdf462088d13 | 170 | TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, out, sizeof( out ) , |
markrad | 0:cdf462088d13 | 171 | add, sizeof( add ) ) == 0 ); |
markrad | 0:cdf462088d13 | 172 | } |
markrad | 0:cdf462088d13 | 173 | TEST_ASSERT( last_idx == test_offset_idx ); |
markrad | 0:cdf462088d13 | 174 | |
markrad | 0:cdf462088d13 | 175 | /* Call update with too much data (sizeof entropy > MAX(_SEED)_INPUT) |
markrad | 0:cdf462088d13 | 176 | * (just make sure it doesn't cause memory corruption) */ |
markrad | 0:cdf462088d13 | 177 | mbedtls_ctr_drbg_update( &ctx, entropy, sizeof( entropy ) ); |
markrad | 0:cdf462088d13 | 178 | |
markrad | 0:cdf462088d13 | 179 | /* Now enable PR, so the next few calls should all reseed */ |
markrad | 0:cdf462088d13 | 180 | mbedtls_ctr_drbg_set_prediction_resistance( &ctx, MBEDTLS_CTR_DRBG_PR_ON ); |
markrad | 0:cdf462088d13 | 181 | TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 ); |
markrad | 0:cdf462088d13 | 182 | TEST_ASSERT( last_idx < test_offset_idx ); |
markrad | 0:cdf462088d13 | 183 | |
markrad | 0:cdf462088d13 | 184 | /* Finally, check setting entropy_len */ |
markrad | 0:cdf462088d13 | 185 | mbedtls_ctr_drbg_set_entropy_len( &ctx, 42 ); |
markrad | 0:cdf462088d13 | 186 | last_idx = test_offset_idx; |
markrad | 0:cdf462088d13 | 187 | TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 ); |
markrad | 0:cdf462088d13 | 188 | TEST_ASSERT( test_offset_idx - last_idx == 42 ); |
markrad | 0:cdf462088d13 | 189 | |
markrad | 0:cdf462088d13 | 190 | mbedtls_ctr_drbg_set_entropy_len( &ctx, 13 ); |
markrad | 0:cdf462088d13 | 191 | last_idx = test_offset_idx; |
markrad | 0:cdf462088d13 | 192 | TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 ); |
markrad | 0:cdf462088d13 | 193 | TEST_ASSERT( test_offset_idx - last_idx == 13 ); |
markrad | 0:cdf462088d13 | 194 | |
markrad | 0:cdf462088d13 | 195 | exit: |
markrad | 0:cdf462088d13 | 196 | mbedtls_ctr_drbg_free( &ctx ); |
markrad | 0:cdf462088d13 | 197 | } |
markrad | 0:cdf462088d13 | 198 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 199 | |
markrad | 0:cdf462088d13 | 200 | /* BEGIN_CASE depends_on:MBEDTLS_FS_IO */ |
markrad | 0:cdf462088d13 | 201 | void ctr_drbg_seed_file( char *path, int ret ) |
markrad | 0:cdf462088d13 | 202 | { |
markrad | 0:cdf462088d13 | 203 | mbedtls_ctr_drbg_context ctx; |
markrad | 0:cdf462088d13 | 204 | |
markrad | 0:cdf462088d13 | 205 | mbedtls_ctr_drbg_init( &ctx ); |
markrad | 0:cdf462088d13 | 206 | |
markrad | 0:cdf462088d13 | 207 | TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctx, rnd_std_rand, NULL, NULL, 0 ) == 0 ); |
markrad | 0:cdf462088d13 | 208 | TEST_ASSERT( mbedtls_ctr_drbg_write_seed_file( &ctx, path ) == ret ); |
markrad | 0:cdf462088d13 | 209 | TEST_ASSERT( mbedtls_ctr_drbg_update_seed_file( &ctx, path ) == ret ); |
markrad | 0:cdf462088d13 | 210 | |
markrad | 0:cdf462088d13 | 211 | exit: |
markrad | 0:cdf462088d13 | 212 | mbedtls_ctr_drbg_free( &ctx ); |
markrad | 0:cdf462088d13 | 213 | } |
markrad | 0:cdf462088d13 | 214 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 215 | |
markrad | 0:cdf462088d13 | 216 | /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */ |
markrad | 0:cdf462088d13 | 217 | void ctr_drbg_selftest( ) |
markrad | 0:cdf462088d13 | 218 | { |
markrad | 0:cdf462088d13 | 219 | TEST_ASSERT( mbedtls_ctr_drbg_self_test( 1 ) == 0 ); |
markrad | 0:cdf462088d13 | 220 | } |
markrad | 0:cdf462088d13 | 221 | /* END_CASE */ |