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 | 48bc3e8 | 2014-01-30 21:11:16 +0100 | [diff] [blame^] | 31 | /* BEGIN_CASE depends_on:POLARSSL_FS_IO */ |
| 32 | void hmac_drbg_seed_file( int md_alg, char *path, int ret ) |
| 33 | { |
| 34 | const md_info_t *md_info; |
| 35 | hmac_drbg_context ctx; |
| 36 | |
| 37 | TEST_ASSERT( ( md_info = md_info_from_type( md_alg ) ) != NULL ); |
| 38 | TEST_ASSERT( hmac_drbg_init( &ctx, md_info, rnd_std_rand, NULL, |
| 39 | NULL, 0 ) == 0 ); |
| 40 | |
| 41 | TEST_ASSERT( hmac_drbg_write_seed_file( &ctx, path ) == ret ); |
| 42 | TEST_ASSERT( hmac_drbg_update_seed_file( &ctx, path ) == ret ); |
| 43 | |
| 44 | hmac_drbg_free( &ctx ); |
| 45 | } |
| 46 | /* END_CASE */ |
| 47 | |
Manuel Pégourié-Gonnard | 6801f39 | 2014-01-30 17:22:14 +0100 | [diff] [blame] | 48 | /* BEGIN_CASE */ |
| 49 | void hmac_drbg_no_reseed( int md_alg, |
| 50 | char *entropy_hex, char *custom_hex, |
| 51 | char *add1_hex, char *add2_hex, |
| 52 | char *output_hex ) |
| 53 | { |
| 54 | unsigned char entropy[512]; |
| 55 | unsigned char custom[512]; |
| 56 | unsigned char add1[512]; |
| 57 | unsigned char add2[512]; |
| 58 | unsigned char output[512]; |
| 59 | unsigned char my_output[512]; |
| 60 | size_t custom_len, add1_len, add2_len, out_len; |
| 61 | entropy_ctx p_entropy; |
| 62 | const md_info_t *md_info; |
| 63 | hmac_drbg_context ctx; |
| 64 | |
| 65 | memset( my_output, 0, sizeof my_output ); |
| 66 | |
| 67 | custom_len = unhexify( custom, custom_hex ); |
| 68 | add1_len = unhexify( add1, add1_hex ); |
| 69 | add2_len = unhexify( add2, add2_hex ); |
| 70 | out_len = unhexify( output, output_hex ); |
| 71 | p_entropy.len = unhexify( entropy, entropy_hex ); |
| 72 | p_entropy.p = entropy; |
| 73 | |
| 74 | TEST_ASSERT( ( md_info = md_info_from_type( md_alg ) ) != NULL ); |
| 75 | TEST_ASSERT( hmac_drbg_init( &ctx, md_info, entropy_func, &p_entropy, |
| 76 | custom, custom_len ) == 0 ); |
| 77 | TEST_ASSERT( hmac_drbg_random_with_add( &ctx, my_output, out_len, |
| 78 | add1, add1_len ) == 0 ); |
| 79 | TEST_ASSERT( hmac_drbg_random_with_add( &ctx, my_output, out_len, |
| 80 | add2, add2_len ) == 0 ); |
| 81 | hmac_drbg_free( &ctx ); |
| 82 | |
| 83 | /* Check output is correct */ |
| 84 | TEST_ASSERT( memcmp( my_output, output, out_len ) == 0 ); |
| 85 | |
| 86 | /* Check we didn't write more bytes than needed */ |
| 87 | TEST_ASSERT( my_output[out_len + 0] == 0 ); |
| 88 | TEST_ASSERT( my_output[out_len + 1] == 0 ); |
| 89 | TEST_ASSERT( my_output[out_len + 2] == 0 ); |
| 90 | TEST_ASSERT( my_output[out_len + 3] == 0 ); |
| 91 | } |
| 92 | /* END_CASE */ |
| 93 | |