blob: b34f8cd7a002dc603c24073823d83562c1f85cc4 [file] [log] [blame]
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +01001/* BEGIN_HEADER */
2#include <polarssl/hmac_drbg.h>
3
4typedef struct
5{
6 unsigned char *p;
7 size_t len;
8} entropy_ctx;
9
10int 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é-Gonnard4f880a52014-01-30 22:39:42 +010031/* BEGIN_CASE */
32void 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
47 TEST_ASSERT( ( md_info = md_info_from_type( md_alg ) ) != NULL );
48
49 /* Init must use entropy */
50 last_len = entropy.len;
51 TEST_ASSERT( hmac_drbg_init( &ctx, md_info, entropy_func, &entropy,
52 NULL, 0 ) == 0 );
53 TEST_ASSERT( entropy.len < last_len );
54
55 /* By default, PR is off and reseed_interval is large,
56 * so the next few calls should not use entropy */
57 last_len = entropy.len;
58 for( i = 0; i < reps; i++ )
59 {
60 TEST_ASSERT( hmac_drbg_random( &ctx, out, sizeof( out ) - 4 ) == 0 );
61 TEST_ASSERT( hmac_drbg_random_with_add( &ctx, out, sizeof( out ) - 4,
62 buf, 16 ) == 0 );
63 }
64 TEST_ASSERT( entropy.len == last_len );
65
66 /* While at it, make sure we didn't write past the requested length */
67 TEST_ASSERT( out[sizeof( out ) - 4] == 0 );
68 TEST_ASSERT( out[sizeof( out ) - 3] == 0 );
69 TEST_ASSERT( out[sizeof( out ) - 2] == 0 );
70 TEST_ASSERT( out[sizeof( out ) - 1] == 0 );
71
72 /* Set reseed_interval to the number of calls done,
73 * so the next call should reseed */
74 hmac_drbg_set_reseed_interval( &ctx, 2 * reps );
75 TEST_ASSERT( hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
76 TEST_ASSERT( entropy.len < last_len );
77
78 /* The new few calls should not reseed */
79 last_len = entropy.len;
80 for( i = 0; i < reps / 2; i++ )
81 {
82 TEST_ASSERT( hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
83 TEST_ASSERT( hmac_drbg_random_with_add( &ctx, out, sizeof( out ) ,
84 buf, 16 ) == 0 );
85 }
86 TEST_ASSERT( entropy.len == last_len );
87
88 /* Now enable PR, so the next few calls should all reseed */
89 hmac_drbg_set_prediction_resistance( &ctx, POLARSSL_HMAC_DRBG_PR_ON );
90 TEST_ASSERT( hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
91 TEST_ASSERT( entropy.len < last_len );
92
93 /* Finally, check setting entropy_len */
94 hmac_drbg_set_entropy_len( &ctx, 42 );
95 last_len = entropy.len;
96 TEST_ASSERT( hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
97 TEST_ASSERT( (int) last_len - entropy.len == 42 );
98
99 hmac_drbg_set_entropy_len( &ctx, 13 );
100 last_len = entropy.len;
101 TEST_ASSERT( hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
102 TEST_ASSERT( (int) last_len - entropy.len == 13 );
103 hmac_drbg_free( &ctx );
104}
105/* END_CASE */
106
Manuel Pégourié-Gonnard48bc3e82014-01-30 21:11:16 +0100107/* BEGIN_CASE depends_on:POLARSSL_FS_IO */
108void hmac_drbg_seed_file( int md_alg, char *path, int ret )
109{
110 const md_info_t *md_info;
111 hmac_drbg_context ctx;
112
113 TEST_ASSERT( ( md_info = md_info_from_type( md_alg ) ) != NULL );
114 TEST_ASSERT( hmac_drbg_init( &ctx, md_info, rnd_std_rand, NULL,
115 NULL, 0 ) == 0 );
116
117 TEST_ASSERT( hmac_drbg_write_seed_file( &ctx, path ) == ret );
118 TEST_ASSERT( hmac_drbg_update_seed_file( &ctx, path ) == ret );
119
120 hmac_drbg_free( &ctx );
121}
122/* END_CASE */
123
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100124/* BEGIN_CASE */
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +0100125void hmac_drbg_buf( int md_alg )
126{
127 unsigned char out[16];
128 unsigned char buf[100];
129 const md_info_t *md_info;
130 hmac_drbg_context ctx;
131 size_t i;
132
133 memset( buf, 0, sizeof( buf ) );
134 memset( out, 0, sizeof( out ) );
135
136 TEST_ASSERT( ( md_info = md_info_from_type( md_alg ) ) != NULL );
137 TEST_ASSERT( hmac_drbg_init_buf( &ctx, md_info, buf, sizeof( buf ) ) == 0 );
138
139 /* Make sure it never tries to reseed (would segfault otherwise) */
140 hmac_drbg_set_reseed_interval( &ctx, 3 );
141 hmac_drbg_set_prediction_resistance( &ctx, POLARSSL_HMAC_DRBG_PR_ON );
142
143 for( i = 0; i < 30; i++ )
144 TEST_ASSERT( hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
145
146 hmac_drbg_free( &ctx );
147}
148/* END_CASE */
149
150/* BEGIN_CASE */
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100151void hmac_drbg_no_reseed( int md_alg,
152 char *entropy_hex, char *custom_hex,
153 char *add1_hex, char *add2_hex,
154 char *output_hex )
155{
156 unsigned char entropy[512];
157 unsigned char custom[512];
158 unsigned char add1[512];
159 unsigned char add2[512];
160 unsigned char output[512];
161 unsigned char my_output[512];
162 size_t custom_len, add1_len, add2_len, out_len;
163 entropy_ctx p_entropy;
164 const md_info_t *md_info;
165 hmac_drbg_context ctx;
166
167 memset( my_output, 0, sizeof my_output );
168
169 custom_len = unhexify( custom, custom_hex );
170 add1_len = unhexify( add1, add1_hex );
171 add2_len = unhexify( add2, add2_hex );
172 out_len = unhexify( output, output_hex );
173 p_entropy.len = unhexify( entropy, entropy_hex );
174 p_entropy.p = entropy;
175
176 TEST_ASSERT( ( md_info = md_info_from_type( md_alg ) ) != NULL );
177 TEST_ASSERT( hmac_drbg_init( &ctx, md_info, entropy_func, &p_entropy,
178 custom, custom_len ) == 0 );
179 TEST_ASSERT( hmac_drbg_random_with_add( &ctx, my_output, out_len,
180 add1, add1_len ) == 0 );
181 TEST_ASSERT( hmac_drbg_random_with_add( &ctx, my_output, out_len,
182 add2, add2_len ) == 0 );
183 hmac_drbg_free( &ctx );
184
Manuel Pégourié-Gonnard24600b72014-01-31 09:54:14 +0100185 TEST_ASSERT( memcmp( my_output, output, out_len ) == 0 );
186}
187/* END_CASE */
188
189/* BEGIN_CASE */
190void hmac_drbg_nopr( int md_alg,
191 char *entropy_hex, char *custom_hex,
192 char *add1_hex, char *add2_hex, char *add3_hex,
193 char *output_hex )
194{
195 unsigned char entropy[512];
196 unsigned char custom[512];
197 unsigned char add1[512];
198 unsigned char add2[512];
199 unsigned char add3[512];
200 unsigned char output[512];
201 unsigned char my_output[512];
202 size_t custom_len, add1_len, add2_len, add3_len, out_len;
203 entropy_ctx p_entropy;
204 const md_info_t *md_info;
205 hmac_drbg_context ctx;
206
207 memset( my_output, 0, sizeof my_output );
208
209 custom_len = unhexify( custom, custom_hex );
210 add1_len = unhexify( add1, add1_hex );
211 add2_len = unhexify( add2, add2_hex );
212 add3_len = unhexify( add3, add3_hex );
213 out_len = unhexify( output, output_hex );
214 p_entropy.len = unhexify( entropy, entropy_hex );
215 p_entropy.p = entropy;
216
217 TEST_ASSERT( ( md_info = md_info_from_type( md_alg ) ) != NULL );
218 TEST_ASSERT( hmac_drbg_init( &ctx, md_info, entropy_func, &p_entropy,
219 custom, custom_len ) == 0 );
220 TEST_ASSERT( hmac_drbg_reseed( &ctx, add1, add1_len ) == 0 );
221 TEST_ASSERT( hmac_drbg_random_with_add( &ctx, my_output, out_len,
222 add2, add2_len ) == 0 );
223 TEST_ASSERT( hmac_drbg_random_with_add( &ctx, my_output, out_len,
224 add3, add3_len ) == 0 );
225 hmac_drbg_free( &ctx );
226
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100227 TEST_ASSERT( memcmp( my_output, output, out_len ) == 0 );
228
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100229}
230/* END_CASE */
231