Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 2 | #include <polarssl/ctr_drbg.h> |
| 3 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame^] | 4 | #include <string.h> |
| 5 | |
Paul Bakker | 3ddfa66 | 2013-11-26 17:45:20 +0100 | [diff] [blame] | 6 | int test_offset_idx; |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 7 | int entropy_func( void *data, unsigned char *buf, size_t len ) |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 8 | { |
Manuel Pégourié-Gonnard | 7575daa | 2014-01-31 12:16:54 +0100 | [diff] [blame] | 9 | const unsigned char *p = (unsigned char *) data; |
Paul Bakker | 3ddfa66 | 2013-11-26 17:45:20 +0100 | [diff] [blame] | 10 | memcpy( buf, p + test_offset_idx, len ); |
Manuel Pégourié-Gonnard | 7575daa | 2014-01-31 12:16:54 +0100 | [diff] [blame] | 11 | test_offset_idx += len; |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 12 | return( 0 ); |
| 13 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 14 | /* END_HEADER */ |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 15 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 16 | /* BEGIN_DEPENDENCIES |
| 17 | * depends_on:POLARSSL_CTR_DRBG_C |
| 18 | * END_DEPENDENCIES |
| 19 | */ |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 20 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 21 | /* BEGIN_CASE */ |
| 22 | void ctr_drbg_validate_pr( char *add_init_string, char *entropy_string, |
| 23 | char *add1_string, char *add2_string, |
| 24 | char *result_str ) |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 25 | { |
| 26 | unsigned char entropy[512]; |
| 27 | unsigned char add_init[512]; |
| 28 | unsigned char add1[512]; |
| 29 | unsigned char add2[512]; |
| 30 | ctr_drbg_context ctx; |
| 31 | unsigned char buf[512]; |
| 32 | unsigned char output_str[512]; |
| 33 | int add_init_len, add1_len, add2_len; |
| 34 | |
| 35 | memset( output_str, 0, 512 ); |
| 36 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 37 | unhexify( entropy, entropy_string ); |
| 38 | add_init_len = unhexify( add_init, add_init_string ); |
| 39 | add1_len = unhexify( add1, add1_string ); |
| 40 | add2_len = unhexify( add2, add2_string ); |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 41 | |
Paul Bakker | 3ddfa66 | 2013-11-26 17:45:20 +0100 | [diff] [blame] | 42 | test_offset_idx = 0; |
Paul Bakker | 18d3291 | 2011-12-10 21:42:49 +0000 | [diff] [blame] | 43 | TEST_ASSERT( ctr_drbg_init_entropy_len( &ctx, entropy_func, entropy, add_init, add_init_len, 32 ) == 0 ); |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 44 | ctr_drbg_set_prediction_resistance( &ctx, CTR_DRBG_PR_ON ); |
| 45 | |
| 46 | TEST_ASSERT( ctr_drbg_random_with_add( &ctx, buf, 16, add1, add1_len ) == 0 ); |
| 47 | TEST_ASSERT( ctr_drbg_random_with_add( &ctx, buf, 16, add2, add2_len ) == 0 ); |
| 48 | hexify( output_str, buf, 16 ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 49 | TEST_ASSERT( strcmp( (char *) output_str, result_str ) == 0 ); |
Paul Bakker | a317a98 | 2014-06-18 16:44:11 +0200 | [diff] [blame] | 50 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 51 | exit: |
Paul Bakker | a317a98 | 2014-06-18 16:44:11 +0200 | [diff] [blame] | 52 | ctr_drbg_free( &ctx ); |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 53 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 54 | /* END_CASE */ |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 55 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 56 | /* BEGIN_CASE */ |
| 57 | void ctr_drbg_validate_nopr( char *add_init_string, char *entropy_string, |
| 58 | char *add1_string, char *add_reseed_string, |
| 59 | char *add2_string, char *result_str ) |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 60 | { |
| 61 | unsigned char entropy[512]; |
| 62 | unsigned char add_init[512]; |
| 63 | unsigned char add1[512]; |
| 64 | unsigned char add_reseed[512]; |
| 65 | unsigned char add2[512]; |
| 66 | ctr_drbg_context ctx; |
| 67 | unsigned char buf[512]; |
| 68 | unsigned char output_str[512]; |
| 69 | int add_init_len, add1_len, add_reseed_len, add2_len; |
| 70 | |
| 71 | memset( output_str, 0, 512 ); |
| 72 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 73 | unhexify( entropy, entropy_string ); |
| 74 | add_init_len = unhexify( add_init, add_init_string ); |
| 75 | add1_len = unhexify( add1, add1_string ); |
| 76 | add_reseed_len = unhexify( add_reseed, add_reseed_string ); |
| 77 | add2_len = unhexify( add2, add2_string ); |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 78 | |
Paul Bakker | 3ddfa66 | 2013-11-26 17:45:20 +0100 | [diff] [blame] | 79 | test_offset_idx = 0; |
Paul Bakker | 18d3291 | 2011-12-10 21:42:49 +0000 | [diff] [blame] | 80 | TEST_ASSERT( ctr_drbg_init_entropy_len( &ctx, entropy_func, entropy, add_init, add_init_len, 32 ) == 0 ); |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 81 | |
| 82 | TEST_ASSERT( ctr_drbg_random_with_add( &ctx, buf, 16, add1, add1_len ) == 0 ); |
| 83 | TEST_ASSERT( ctr_drbg_reseed( &ctx, add_reseed, add_reseed_len ) == 0 ); |
| 84 | TEST_ASSERT( ctr_drbg_random_with_add( &ctx, buf, 16, add2, add2_len ) == 0 ); |
| 85 | hexify( output_str, buf, 16 ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 86 | TEST_ASSERT( strcmp( (char *) output_str, result_str ) == 0 ); |
Paul Bakker | a317a98 | 2014-06-18 16:44:11 +0200 | [diff] [blame] | 87 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 88 | exit: |
Paul Bakker | a317a98 | 2014-06-18 16:44:11 +0200 | [diff] [blame] | 89 | ctr_drbg_free( &ctx ); |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 90 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 91 | /* END_CASE */ |
Manuel Pégourié-Gonnard | b3b205e | 2014-01-31 12:04:06 +0100 | [diff] [blame] | 92 | |
Manuel Pégourié-Gonnard | 7575daa | 2014-01-31 12:16:54 +0100 | [diff] [blame] | 93 | /* BEGIN_CASE */ |
| 94 | void ctr_drbg_entropy_usage( ) |
| 95 | { |
| 96 | unsigned char out[16]; |
| 97 | unsigned char add[16]; |
| 98 | unsigned char entropy[1024]; |
| 99 | ctr_drbg_context ctx; |
| 100 | size_t i, reps = 10; |
| 101 | int last_idx; |
| 102 | |
| 103 | test_offset_idx = 0; |
| 104 | memset( entropy, 0, sizeof( entropy ) ); |
| 105 | memset( out, 0, sizeof( out ) ); |
| 106 | memset( add, 0, sizeof( add ) ); |
| 107 | |
| 108 | /* Init must use entropy */ |
| 109 | last_idx = test_offset_idx; |
| 110 | TEST_ASSERT( ctr_drbg_init( &ctx, entropy_func, entropy, NULL, 0 ) == 0 ); |
| 111 | TEST_ASSERT( last_idx < test_offset_idx ); |
| 112 | |
| 113 | /* By default, PR is off and reseed_interval is large, |
| 114 | * so the next few calls should not use entropy */ |
| 115 | last_idx = test_offset_idx; |
| 116 | for( i = 0; i < reps; i++ ) |
| 117 | { |
| 118 | TEST_ASSERT( ctr_drbg_random( &ctx, out, sizeof( out ) - 4 ) == 0 ); |
| 119 | TEST_ASSERT( ctr_drbg_random_with_add( &ctx, out, sizeof( out ) - 4, |
| 120 | add, sizeof( add ) ) == 0 ); |
| 121 | } |
| 122 | TEST_ASSERT( last_idx == test_offset_idx ); |
| 123 | |
| 124 | /* While at it, make sure we didn't write past the requested length */ |
| 125 | TEST_ASSERT( out[sizeof( out ) - 4] == 0 ); |
| 126 | TEST_ASSERT( out[sizeof( out ) - 3] == 0 ); |
| 127 | TEST_ASSERT( out[sizeof( out ) - 2] == 0 ); |
| 128 | TEST_ASSERT( out[sizeof( out ) - 1] == 0 ); |
| 129 | |
| 130 | /* Set reseed_interval to the number of calls done, |
| 131 | * so the next call should reseed */ |
| 132 | ctr_drbg_set_reseed_interval( &ctx, 2 * reps ); |
| 133 | TEST_ASSERT( ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 ); |
| 134 | TEST_ASSERT( last_idx < test_offset_idx ); |
| 135 | |
| 136 | /* The new few calls should not reseed */ |
| 137 | last_idx = test_offset_idx; |
| 138 | for( i = 0; i < reps / 2; i++ ) |
| 139 | { |
| 140 | TEST_ASSERT( ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 ); |
| 141 | TEST_ASSERT( ctr_drbg_random_with_add( &ctx, out, sizeof( out ) , |
| 142 | add, sizeof( add ) ) == 0 ); |
| 143 | } |
| 144 | TEST_ASSERT( last_idx == test_offset_idx ); |
| 145 | |
Manuel Pégourié-Gonnard | f5f25b3 | 2014-11-27 14:04:56 +0100 | [diff] [blame] | 146 | /* Call update with too much data (sizeof entropy > MAX(_SEED)_INPUT) |
| 147 | * (just make sure it doesn't cause memory corruption) */ |
| 148 | ctr_drbg_update( &ctx, entropy, sizeof( entropy ) ); |
| 149 | |
Manuel Pégourié-Gonnard | 7575daa | 2014-01-31 12:16:54 +0100 | [diff] [blame] | 150 | /* Now enable PR, so the next few calls should all reseed */ |
| 151 | ctr_drbg_set_prediction_resistance( &ctx, CTR_DRBG_PR_ON ); |
| 152 | TEST_ASSERT( ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 ); |
| 153 | TEST_ASSERT( last_idx < test_offset_idx ); |
| 154 | |
| 155 | /* Finally, check setting entropy_len */ |
| 156 | ctr_drbg_set_entropy_len( &ctx, 42 ); |
| 157 | last_idx = test_offset_idx; |
| 158 | TEST_ASSERT( ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 ); |
| 159 | TEST_ASSERT( test_offset_idx - last_idx == 42 ); |
| 160 | |
| 161 | ctr_drbg_set_entropy_len( &ctx, 13 ); |
| 162 | last_idx = test_offset_idx; |
| 163 | TEST_ASSERT( ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 ); |
| 164 | TEST_ASSERT( test_offset_idx - last_idx == 13 ); |
Paul Bakker | a317a98 | 2014-06-18 16:44:11 +0200 | [diff] [blame] | 165 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 166 | exit: |
Paul Bakker | a317a98 | 2014-06-18 16:44:11 +0200 | [diff] [blame] | 167 | ctr_drbg_free( &ctx ); |
Manuel Pégourié-Gonnard | 7575daa | 2014-01-31 12:16:54 +0100 | [diff] [blame] | 168 | } |
| 169 | /* END_CASE */ |
| 170 | |
| 171 | /* BEGIN_CASE depends_on:POLARSSL_FS_IO */ |
| 172 | void ctr_drbg_seed_file( char *path, int ret ) |
| 173 | { |
| 174 | ctr_drbg_context ctx; |
| 175 | |
| 176 | TEST_ASSERT( ctr_drbg_init( &ctx, rnd_std_rand, NULL, NULL, 0 ) == 0 ); |
| 177 | TEST_ASSERT( ctr_drbg_write_seed_file( &ctx, path ) == ret ); |
| 178 | TEST_ASSERT( ctr_drbg_update_seed_file( &ctx, path ) == ret ); |
Paul Bakker | a317a98 | 2014-06-18 16:44:11 +0200 | [diff] [blame] | 179 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 180 | exit: |
Paul Bakker | a317a98 | 2014-06-18 16:44:11 +0200 | [diff] [blame] | 181 | ctr_drbg_free( &ctx ); |
Manuel Pégourié-Gonnard | 7575daa | 2014-01-31 12:16:54 +0100 | [diff] [blame] | 182 | } |
| 183 | /* END_CASE */ |
| 184 | |
Manuel Pégourié-Gonnard | b3b205e | 2014-01-31 12:04:06 +0100 | [diff] [blame] | 185 | /* BEGIN_CASE depends_on:POLARSSL_SELF_TEST */ |
| 186 | void ctr_drbg_selftest( ) |
| 187 | { |
| 188 | TEST_ASSERT( ctr_drbg_self_test( 0 ) == 0 ); |
| 189 | } |
| 190 | /* END_CASE */ |