Manuel Pégourié-Gonnard | 6801f39 | 2014-01-30 17:22:14 +0100 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
| 2 | #include <polarssl/hmac_drbg.h> |
| 3 | |
| 4 | typedef struct |
| 5 | { |
| 6 | unsigned char *p; |
| 7 | size_t len; |
| 8 | } entropy_ctx; |
| 9 | |
| 10 | int entropy_func( void *data, unsigned char *buf, size_t len ) |
| 11 | { |
| 12 | entropy_ctx *ctx = (entropy_ctx *) data; |
| 13 | |
| 14 | if( len > ctx->len ) |
| 15 | return( -1 ); |
| 16 | |
| 17 | memcpy( buf, ctx->p, len ); |
| 18 | |
| 19 | ctx->p += len; |
| 20 | ctx->len -= len; |
| 21 | |
| 22 | return( 0 ); |
| 23 | } |
| 24 | /* END_HEADER */ |
| 25 | |
| 26 | /* BEGIN_DEPENDENCIES |
| 27 | * depends_on:POLARSSL_HMAC_DRBG_C |
| 28 | * END_DEPENDENCIES |
| 29 | */ |
| 30 | |
Manuel Pégourié-Gonnard | 4f880a5 | 2014-01-30 22:39:42 +0100 | [diff] [blame] | 31 | /* BEGIN_CASE */ |
| 32 | void hmac_drbg_entropy_usage( int md_alg ) |
| 33 | { |
| 34 | unsigned char out[16]; |
| 35 | unsigned char buf[1024]; |
| 36 | const md_info_t *md_info; |
| 37 | hmac_drbg_context ctx; |
| 38 | entropy_ctx entropy; |
| 39 | size_t last_len, i, reps = 10; |
| 40 | |
| 41 | memset( buf, 0, sizeof( buf ) ); |
| 42 | memset( out, 0, sizeof( out ) ); |
| 43 | |
| 44 | entropy.len = sizeof( buf ); |
| 45 | entropy.p = buf; |
| 46 | |
Paul Bakker | 94b916c | 2014-04-17 16:07:20 +0200 | [diff] [blame] | 47 | md_info = md_info_from_type( md_alg ); |
| 48 | TEST_ASSERT( md_info != NULL ); |
Manuel Pégourié-Gonnard | 4f880a5 | 2014-01-30 22:39:42 +0100 | [diff] [blame] | 49 | |
| 50 | /* Init must use entropy */ |
| 51 | last_len = entropy.len; |
| 52 | TEST_ASSERT( hmac_drbg_init( &ctx, md_info, entropy_func, &entropy, |
| 53 | NULL, 0 ) == 0 ); |
| 54 | TEST_ASSERT( entropy.len < last_len ); |
| 55 | |
| 56 | /* By default, PR is off and reseed_interval is large, |
| 57 | * so the next few calls should not use entropy */ |
| 58 | last_len = entropy.len; |
| 59 | for( i = 0; i < reps; i++ ) |
| 60 | { |
| 61 | TEST_ASSERT( hmac_drbg_random( &ctx, out, sizeof( out ) - 4 ) == 0 ); |
| 62 | TEST_ASSERT( hmac_drbg_random_with_add( &ctx, out, sizeof( out ) - 4, |
| 63 | buf, 16 ) == 0 ); |
| 64 | } |
| 65 | TEST_ASSERT( entropy.len == last_len ); |
| 66 | |
| 67 | /* While at it, make sure we didn't write past the requested length */ |
| 68 | TEST_ASSERT( out[sizeof( out ) - 4] == 0 ); |
| 69 | TEST_ASSERT( out[sizeof( out ) - 3] == 0 ); |
| 70 | TEST_ASSERT( out[sizeof( out ) - 2] == 0 ); |
| 71 | TEST_ASSERT( out[sizeof( out ) - 1] == 0 ); |
| 72 | |
| 73 | /* Set reseed_interval to the number of calls done, |
| 74 | * so the next call should reseed */ |
| 75 | hmac_drbg_set_reseed_interval( &ctx, 2 * reps ); |
| 76 | TEST_ASSERT( hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 ); |
| 77 | TEST_ASSERT( entropy.len < last_len ); |
| 78 | |
| 79 | /* The new few calls should not reseed */ |
| 80 | last_len = entropy.len; |
| 81 | for( i = 0; i < reps / 2; i++ ) |
| 82 | { |
| 83 | TEST_ASSERT( hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 ); |
| 84 | TEST_ASSERT( hmac_drbg_random_with_add( &ctx, out, sizeof( out ) , |
| 85 | buf, 16 ) == 0 ); |
| 86 | } |
| 87 | TEST_ASSERT( entropy.len == last_len ); |
| 88 | |
| 89 | /* Now enable PR, so the next few calls should all reseed */ |
| 90 | hmac_drbg_set_prediction_resistance( &ctx, POLARSSL_HMAC_DRBG_PR_ON ); |
| 91 | TEST_ASSERT( hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 ); |
| 92 | TEST_ASSERT( entropy.len < last_len ); |
| 93 | |
| 94 | /* Finally, check setting entropy_len */ |
| 95 | hmac_drbg_set_entropy_len( &ctx, 42 ); |
| 96 | last_len = entropy.len; |
| 97 | TEST_ASSERT( hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 ); |
| 98 | TEST_ASSERT( (int) last_len - entropy.len == 42 ); |
| 99 | |
| 100 | hmac_drbg_set_entropy_len( &ctx, 13 ); |
| 101 | last_len = entropy.len; |
| 102 | TEST_ASSERT( hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 ); |
| 103 | TEST_ASSERT( (int) last_len - entropy.len == 13 ); |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame^] | 104 | |
| 105 | exit: |
Manuel Pégourié-Gonnard | 4f880a5 | 2014-01-30 22:39:42 +0100 | [diff] [blame] | 106 | hmac_drbg_free( &ctx ); |
| 107 | } |
| 108 | /* END_CASE */ |
| 109 | |
Manuel Pégourié-Gonnard | 48bc3e8 | 2014-01-30 21:11:16 +0100 | [diff] [blame] | 110 | /* BEGIN_CASE depends_on:POLARSSL_FS_IO */ |
| 111 | void hmac_drbg_seed_file( int md_alg, char *path, int ret ) |
| 112 | { |
| 113 | const md_info_t *md_info; |
| 114 | hmac_drbg_context ctx; |
| 115 | |
Paul Bakker | 94b916c | 2014-04-17 16:07:20 +0200 | [diff] [blame] | 116 | md_info = md_info_from_type( md_alg ); |
| 117 | TEST_ASSERT( md_info != NULL ); |
| 118 | |
Manuel Pégourié-Gonnard | 48bc3e8 | 2014-01-30 21:11:16 +0100 | [diff] [blame] | 119 | TEST_ASSERT( hmac_drbg_init( &ctx, md_info, rnd_std_rand, NULL, |
| 120 | NULL, 0 ) == 0 ); |
| 121 | |
| 122 | TEST_ASSERT( hmac_drbg_write_seed_file( &ctx, path ) == ret ); |
| 123 | TEST_ASSERT( hmac_drbg_update_seed_file( &ctx, path ) == ret ); |
| 124 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame^] | 125 | exit: |
Manuel Pégourié-Gonnard | 48bc3e8 | 2014-01-30 21:11:16 +0100 | [diff] [blame] | 126 | hmac_drbg_free( &ctx ); |
| 127 | } |
| 128 | /* END_CASE */ |
| 129 | |
Manuel Pégourié-Gonnard | 6801f39 | 2014-01-30 17:22:14 +0100 | [diff] [blame] | 130 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 4f880a5 | 2014-01-30 22:39:42 +0100 | [diff] [blame] | 131 | void hmac_drbg_buf( int md_alg ) |
| 132 | { |
| 133 | unsigned char out[16]; |
| 134 | unsigned char buf[100]; |
| 135 | const md_info_t *md_info; |
| 136 | hmac_drbg_context ctx; |
| 137 | size_t i; |
| 138 | |
| 139 | memset( buf, 0, sizeof( buf ) ); |
| 140 | memset( out, 0, sizeof( out ) ); |
| 141 | |
Paul Bakker | 94b916c | 2014-04-17 16:07:20 +0200 | [diff] [blame] | 142 | md_info = md_info_from_type( md_alg ); |
| 143 | TEST_ASSERT( md_info != NULL ); |
Manuel Pégourié-Gonnard | 4f880a5 | 2014-01-30 22:39:42 +0100 | [diff] [blame] | 144 | TEST_ASSERT( hmac_drbg_init_buf( &ctx, md_info, buf, sizeof( buf ) ) == 0 ); |
| 145 | |
| 146 | /* Make sure it never tries to reseed (would segfault otherwise) */ |
| 147 | hmac_drbg_set_reseed_interval( &ctx, 3 ); |
| 148 | hmac_drbg_set_prediction_resistance( &ctx, POLARSSL_HMAC_DRBG_PR_ON ); |
| 149 | |
| 150 | for( i = 0; i < 30; i++ ) |
| 151 | TEST_ASSERT( hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 ); |
| 152 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame^] | 153 | exit: |
Manuel Pégourié-Gonnard | 4f880a5 | 2014-01-30 22:39:42 +0100 | [diff] [blame] | 154 | hmac_drbg_free( &ctx ); |
| 155 | } |
| 156 | /* END_CASE */ |
| 157 | |
| 158 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 6801f39 | 2014-01-30 17:22:14 +0100 | [diff] [blame] | 159 | void hmac_drbg_no_reseed( int md_alg, |
| 160 | char *entropy_hex, char *custom_hex, |
| 161 | char *add1_hex, char *add2_hex, |
| 162 | char *output_hex ) |
| 163 | { |
Manuel Pégourié-Gonnard | e6cdbbd | 2014-02-01 11:30:03 +0100 | [diff] [blame] | 164 | unsigned char data[1024]; |
Manuel Pégourié-Gonnard | 6801f39 | 2014-01-30 17:22:14 +0100 | [diff] [blame] | 165 | unsigned char entropy[512]; |
| 166 | unsigned char custom[512]; |
| 167 | unsigned char add1[512]; |
| 168 | unsigned char add2[512]; |
| 169 | unsigned char output[512]; |
| 170 | unsigned char my_output[512]; |
| 171 | size_t custom_len, add1_len, add2_len, out_len; |
| 172 | entropy_ctx p_entropy; |
| 173 | const md_info_t *md_info; |
| 174 | hmac_drbg_context ctx; |
| 175 | |
| 176 | memset( my_output, 0, sizeof my_output ); |
| 177 | |
| 178 | custom_len = unhexify( custom, custom_hex ); |
| 179 | add1_len = unhexify( add1, add1_hex ); |
| 180 | add2_len = unhexify( add2, add2_hex ); |
| 181 | out_len = unhexify( output, output_hex ); |
| 182 | p_entropy.len = unhexify( entropy, entropy_hex ); |
| 183 | p_entropy.p = entropy; |
| 184 | |
Paul Bakker | 94b916c | 2014-04-17 16:07:20 +0200 | [diff] [blame] | 185 | md_info = md_info_from_type( md_alg ); |
| 186 | TEST_ASSERT( md_info != NULL ); |
Manuel Pégourié-Gonnard | e6cdbbd | 2014-02-01 11:30:03 +0100 | [diff] [blame] | 187 | |
| 188 | /* Test the simplified buffer-based variant */ |
| 189 | memcpy( data, entropy, p_entropy.len ); |
| 190 | memcpy( data + p_entropy.len, custom, custom_len ); |
| 191 | TEST_ASSERT( hmac_drbg_init_buf( &ctx, md_info, |
| 192 | data, p_entropy.len + custom_len ) == 0 ); |
| 193 | TEST_ASSERT( hmac_drbg_random_with_add( &ctx, my_output, out_len, |
| 194 | add1, add1_len ) == 0 ); |
| 195 | TEST_ASSERT( hmac_drbg_random_with_add( &ctx, my_output, out_len, |
| 196 | add2, add2_len ) == 0 ); |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame^] | 197 | |
| 198 | /* clear for second run */ |
Manuel Pégourié-Gonnard | e6cdbbd | 2014-02-01 11:30:03 +0100 | [diff] [blame] | 199 | hmac_drbg_free( &ctx ); |
| 200 | |
| 201 | TEST_ASSERT( memcmp( my_output, output, out_len ) == 0 ); |
| 202 | |
| 203 | /* And now the normal entropy-based variant */ |
Manuel Pégourié-Gonnard | 6801f39 | 2014-01-30 17:22:14 +0100 | [diff] [blame] | 204 | TEST_ASSERT( hmac_drbg_init( &ctx, md_info, entropy_func, &p_entropy, |
| 205 | custom, custom_len ) == 0 ); |
| 206 | TEST_ASSERT( hmac_drbg_random_with_add( &ctx, my_output, out_len, |
| 207 | add1, add1_len ) == 0 ); |
| 208 | TEST_ASSERT( hmac_drbg_random_with_add( &ctx, my_output, out_len, |
| 209 | add2, add2_len ) == 0 ); |
Manuel Pégourié-Gonnard | 24600b7 | 2014-01-31 09:54:14 +0100 | [diff] [blame] | 210 | TEST_ASSERT( memcmp( my_output, output, out_len ) == 0 ); |
Manuel Pégourié-Gonnard | e6cdbbd | 2014-02-01 11:30:03 +0100 | [diff] [blame] | 211 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame^] | 212 | exit: |
| 213 | hmac_drbg_free( &ctx ); |
Manuel Pégourié-Gonnard | 24600b7 | 2014-01-31 09:54:14 +0100 | [diff] [blame] | 214 | } |
| 215 | /* END_CASE */ |
| 216 | |
| 217 | /* BEGIN_CASE */ |
| 218 | void hmac_drbg_nopr( int md_alg, |
| 219 | char *entropy_hex, char *custom_hex, |
| 220 | char *add1_hex, char *add2_hex, char *add3_hex, |
| 221 | char *output_hex ) |
| 222 | { |
| 223 | unsigned char entropy[512]; |
| 224 | unsigned char custom[512]; |
| 225 | unsigned char add1[512]; |
| 226 | unsigned char add2[512]; |
| 227 | unsigned char add3[512]; |
| 228 | unsigned char output[512]; |
| 229 | unsigned char my_output[512]; |
| 230 | size_t custom_len, add1_len, add2_len, add3_len, out_len; |
| 231 | entropy_ctx p_entropy; |
| 232 | const md_info_t *md_info; |
| 233 | hmac_drbg_context ctx; |
| 234 | |
| 235 | memset( my_output, 0, sizeof my_output ); |
| 236 | |
| 237 | custom_len = unhexify( custom, custom_hex ); |
| 238 | add1_len = unhexify( add1, add1_hex ); |
| 239 | add2_len = unhexify( add2, add2_hex ); |
| 240 | add3_len = unhexify( add3, add3_hex ); |
| 241 | out_len = unhexify( output, output_hex ); |
| 242 | p_entropy.len = unhexify( entropy, entropy_hex ); |
| 243 | p_entropy.p = entropy; |
| 244 | |
Paul Bakker | 94b916c | 2014-04-17 16:07:20 +0200 | [diff] [blame] | 245 | md_info = md_info_from_type( md_alg ); |
| 246 | TEST_ASSERT( md_info != NULL ); |
| 247 | |
Manuel Pégourié-Gonnard | 24600b7 | 2014-01-31 09:54:14 +0100 | [diff] [blame] | 248 | TEST_ASSERT( hmac_drbg_init( &ctx, md_info, entropy_func, &p_entropy, |
| 249 | custom, custom_len ) == 0 ); |
| 250 | TEST_ASSERT( hmac_drbg_reseed( &ctx, add1, add1_len ) == 0 ); |
| 251 | TEST_ASSERT( hmac_drbg_random_with_add( &ctx, my_output, out_len, |
| 252 | add2, add2_len ) == 0 ); |
| 253 | TEST_ASSERT( hmac_drbg_random_with_add( &ctx, my_output, out_len, |
| 254 | add3, add3_len ) == 0 ); |
Manuel Pégourié-Gonnard | 24600b7 | 2014-01-31 09:54:14 +0100 | [diff] [blame] | 255 | |
Manuel Pégourié-Gonnard | 6801f39 | 2014-01-30 17:22:14 +0100 | [diff] [blame] | 256 | TEST_ASSERT( memcmp( my_output, output, out_len ) == 0 ); |
| 257 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame^] | 258 | exit: |
| 259 | hmac_drbg_free( &ctx ); |
Manuel Pégourié-Gonnard | 6801f39 | 2014-01-30 17:22:14 +0100 | [diff] [blame] | 260 | } |
| 261 | /* END_CASE */ |
| 262 | |
Manuel Pégourié-Gonnard | 62273b8 | 2014-01-31 10:16:57 +0100 | [diff] [blame] | 263 | /* BEGIN_CASE */ |
| 264 | void hmac_drbg_pr( int md_alg, |
| 265 | char *entropy_hex, char *custom_hex, |
| 266 | char *add1_hex, char *add2_hex, |
| 267 | char *output_hex ) |
| 268 | { |
| 269 | unsigned char entropy[512]; |
| 270 | unsigned char custom[512]; |
| 271 | unsigned char add1[512]; |
| 272 | unsigned char add2[512]; |
| 273 | unsigned char output[512]; |
| 274 | unsigned char my_output[512]; |
| 275 | size_t custom_len, add1_len, add2_len, out_len; |
| 276 | entropy_ctx p_entropy; |
| 277 | const md_info_t *md_info; |
| 278 | hmac_drbg_context ctx; |
| 279 | |
| 280 | memset( my_output, 0, sizeof my_output ); |
| 281 | |
| 282 | custom_len = unhexify( custom, custom_hex ); |
| 283 | add1_len = unhexify( add1, add1_hex ); |
| 284 | add2_len = unhexify( add2, add2_hex ); |
| 285 | out_len = unhexify( output, output_hex ); |
| 286 | p_entropy.len = unhexify( entropy, entropy_hex ); |
| 287 | p_entropy.p = entropy; |
| 288 | |
Paul Bakker | 94b916c | 2014-04-17 16:07:20 +0200 | [diff] [blame] | 289 | md_info = md_info_from_type( md_alg ); |
| 290 | TEST_ASSERT( md_info != NULL ); |
| 291 | |
Manuel Pégourié-Gonnard | 62273b8 | 2014-01-31 10:16:57 +0100 | [diff] [blame] | 292 | TEST_ASSERT( hmac_drbg_init( &ctx, md_info, entropy_func, &p_entropy, |
| 293 | custom, custom_len ) == 0 ); |
| 294 | hmac_drbg_set_prediction_resistance( &ctx, POLARSSL_HMAC_DRBG_PR_ON ); |
| 295 | TEST_ASSERT( hmac_drbg_random_with_add( &ctx, my_output, out_len, |
| 296 | add1, add1_len ) == 0 ); |
| 297 | TEST_ASSERT( hmac_drbg_random_with_add( &ctx, my_output, out_len, |
| 298 | add2, add2_len ) == 0 ); |
Manuel Pégourié-Gonnard | 62273b8 | 2014-01-31 10:16:57 +0100 | [diff] [blame] | 299 | |
| 300 | TEST_ASSERT( memcmp( my_output, output, out_len ) == 0 ); |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame^] | 301 | |
| 302 | exit: |
| 303 | hmac_drbg_free( &ctx ); |
Manuel Pégourié-Gonnard | 62273b8 | 2014-01-31 10:16:57 +0100 | [diff] [blame] | 304 | } |
| 305 | /* END_CASE */ |
| 306 | |
Manuel Pégourié-Gonnard | 79afaa0 | 2014-01-31 11:12:09 +0100 | [diff] [blame] | 307 | /* BEGIN_CASE depends_on:POLARSSL_SELF_TEST */ |
| 308 | void hmac_drbg_selftest( ) |
| 309 | { |
| 310 | TEST_ASSERT( hmac_drbg_self_test( 0 ) == 0 ); |
| 311 | } |
| 312 | /* END_CASE */ |