blob: 71587567619e829ab645519f54fe67e1591c32db [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
Paul Bakker94b916c2014-04-17 16:07:20 +020047 md_info = md_info_from_type( md_alg );
48 TEST_ASSERT( md_info != NULL );
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +010049
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 );
104 hmac_drbg_free( &ctx );
105}
106/* END_CASE */
107
Manuel Pégourié-Gonnard48bc3e82014-01-30 21:11:16 +0100108/* BEGIN_CASE depends_on:POLARSSL_FS_IO */
109void hmac_drbg_seed_file( int md_alg, char *path, int ret )
110{
111 const md_info_t *md_info;
112 hmac_drbg_context ctx;
113
Paul Bakker94b916c2014-04-17 16:07:20 +0200114 md_info = md_info_from_type( md_alg );
115 TEST_ASSERT( md_info != NULL );
116
Manuel Pégourié-Gonnard48bc3e82014-01-30 21:11:16 +0100117 TEST_ASSERT( hmac_drbg_init( &ctx, md_info, rnd_std_rand, NULL,
118 NULL, 0 ) == 0 );
119
120 TEST_ASSERT( hmac_drbg_write_seed_file( &ctx, path ) == ret );
121 TEST_ASSERT( hmac_drbg_update_seed_file( &ctx, path ) == ret );
122
123 hmac_drbg_free( &ctx );
124}
125/* END_CASE */
126
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100127/* BEGIN_CASE */
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +0100128void hmac_drbg_buf( int md_alg )
129{
130 unsigned char out[16];
131 unsigned char buf[100];
132 const md_info_t *md_info;
133 hmac_drbg_context ctx;
134 size_t i;
135
136 memset( buf, 0, sizeof( buf ) );
137 memset( out, 0, sizeof( out ) );
138
Paul Bakker94b916c2014-04-17 16:07:20 +0200139 md_info = md_info_from_type( md_alg );
140 TEST_ASSERT( md_info != NULL );
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +0100141 TEST_ASSERT( hmac_drbg_init_buf( &ctx, md_info, buf, sizeof( buf ) ) == 0 );
142
143 /* Make sure it never tries to reseed (would segfault otherwise) */
144 hmac_drbg_set_reseed_interval( &ctx, 3 );
145 hmac_drbg_set_prediction_resistance( &ctx, POLARSSL_HMAC_DRBG_PR_ON );
146
147 for( i = 0; i < 30; i++ )
148 TEST_ASSERT( hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
149
150 hmac_drbg_free( &ctx );
151}
152/* END_CASE */
153
154/* BEGIN_CASE */
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100155void hmac_drbg_no_reseed( int md_alg,
156 char *entropy_hex, char *custom_hex,
157 char *add1_hex, char *add2_hex,
158 char *output_hex )
159{
Manuel Pégourié-Gonnarde6cdbbd2014-02-01 11:30:03 +0100160 unsigned char data[1024];
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100161 unsigned char entropy[512];
162 unsigned char custom[512];
163 unsigned char add1[512];
164 unsigned char add2[512];
165 unsigned char output[512];
166 unsigned char my_output[512];
167 size_t custom_len, add1_len, add2_len, out_len;
168 entropy_ctx p_entropy;
169 const md_info_t *md_info;
170 hmac_drbg_context ctx;
171
172 memset( my_output, 0, sizeof my_output );
173
174 custom_len = unhexify( custom, custom_hex );
175 add1_len = unhexify( add1, add1_hex );
176 add2_len = unhexify( add2, add2_hex );
177 out_len = unhexify( output, output_hex );
178 p_entropy.len = unhexify( entropy, entropy_hex );
179 p_entropy.p = entropy;
180
Paul Bakker94b916c2014-04-17 16:07:20 +0200181 md_info = md_info_from_type( md_alg );
182 TEST_ASSERT( md_info != NULL );
Manuel Pégourié-Gonnarde6cdbbd2014-02-01 11:30:03 +0100183
184 /* Test the simplified buffer-based variant */
185 memcpy( data, entropy, p_entropy.len );
186 memcpy( data + p_entropy.len, custom, custom_len );
187 TEST_ASSERT( hmac_drbg_init_buf( &ctx, md_info,
188 data, p_entropy.len + custom_len ) == 0 );
189 TEST_ASSERT( hmac_drbg_random_with_add( &ctx, my_output, out_len,
190 add1, add1_len ) == 0 );
191 TEST_ASSERT( hmac_drbg_random_with_add( &ctx, my_output, out_len,
192 add2, add2_len ) == 0 );
193 hmac_drbg_free( &ctx );
194
195 TEST_ASSERT( memcmp( my_output, output, out_len ) == 0 );
196
197 /* And now the normal entropy-based variant */
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100198 TEST_ASSERT( hmac_drbg_init( &ctx, md_info, entropy_func, &p_entropy,
199 custom, custom_len ) == 0 );
200 TEST_ASSERT( hmac_drbg_random_with_add( &ctx, my_output, out_len,
201 add1, add1_len ) == 0 );
202 TEST_ASSERT( hmac_drbg_random_with_add( &ctx, my_output, out_len,
203 add2, add2_len ) == 0 );
204 hmac_drbg_free( &ctx );
205
Manuel Pégourié-Gonnard24600b72014-01-31 09:54:14 +0100206 TEST_ASSERT( memcmp( my_output, output, out_len ) == 0 );
Manuel Pégourié-Gonnarde6cdbbd2014-02-01 11:30:03 +0100207
Manuel Pégourié-Gonnard24600b72014-01-31 09:54:14 +0100208}
209/* END_CASE */
210
211/* BEGIN_CASE */
212void hmac_drbg_nopr( int md_alg,
213 char *entropy_hex, char *custom_hex,
214 char *add1_hex, char *add2_hex, char *add3_hex,
215 char *output_hex )
216{
217 unsigned char entropy[512];
218 unsigned char custom[512];
219 unsigned char add1[512];
220 unsigned char add2[512];
221 unsigned char add3[512];
222 unsigned char output[512];
223 unsigned char my_output[512];
224 size_t custom_len, add1_len, add2_len, add3_len, out_len;
225 entropy_ctx p_entropy;
226 const md_info_t *md_info;
227 hmac_drbg_context ctx;
228
229 memset( my_output, 0, sizeof my_output );
230
231 custom_len = unhexify( custom, custom_hex );
232 add1_len = unhexify( add1, add1_hex );
233 add2_len = unhexify( add2, add2_hex );
234 add3_len = unhexify( add3, add3_hex );
235 out_len = unhexify( output, output_hex );
236 p_entropy.len = unhexify( entropy, entropy_hex );
237 p_entropy.p = entropy;
238
Paul Bakker94b916c2014-04-17 16:07:20 +0200239 md_info = md_info_from_type( md_alg );
240 TEST_ASSERT( md_info != NULL );
241
Manuel Pégourié-Gonnard24600b72014-01-31 09:54:14 +0100242 TEST_ASSERT( hmac_drbg_init( &ctx, md_info, entropy_func, &p_entropy,
243 custom, custom_len ) == 0 );
244 TEST_ASSERT( hmac_drbg_reseed( &ctx, add1, add1_len ) == 0 );
245 TEST_ASSERT( hmac_drbg_random_with_add( &ctx, my_output, out_len,
246 add2, add2_len ) == 0 );
247 TEST_ASSERT( hmac_drbg_random_with_add( &ctx, my_output, out_len,
248 add3, add3_len ) == 0 );
249 hmac_drbg_free( &ctx );
250
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100251 TEST_ASSERT( memcmp( my_output, output, out_len ) == 0 );
252
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100253}
254/* END_CASE */
255
Manuel Pégourié-Gonnard62273b82014-01-31 10:16:57 +0100256/* BEGIN_CASE */
257void hmac_drbg_pr( int md_alg,
258 char *entropy_hex, char *custom_hex,
259 char *add1_hex, char *add2_hex,
260 char *output_hex )
261{
262 unsigned char entropy[512];
263 unsigned char custom[512];
264 unsigned char add1[512];
265 unsigned char add2[512];
266 unsigned char output[512];
267 unsigned char my_output[512];
268 size_t custom_len, add1_len, add2_len, out_len;
269 entropy_ctx p_entropy;
270 const md_info_t *md_info;
271 hmac_drbg_context ctx;
272
273 memset( my_output, 0, sizeof my_output );
274
275 custom_len = unhexify( custom, custom_hex );
276 add1_len = unhexify( add1, add1_hex );
277 add2_len = unhexify( add2, add2_hex );
278 out_len = unhexify( output, output_hex );
279 p_entropy.len = unhexify( entropy, entropy_hex );
280 p_entropy.p = entropy;
281
Paul Bakker94b916c2014-04-17 16:07:20 +0200282 md_info = md_info_from_type( md_alg );
283 TEST_ASSERT( md_info != NULL );
284
Manuel Pégourié-Gonnard62273b82014-01-31 10:16:57 +0100285 TEST_ASSERT( hmac_drbg_init( &ctx, md_info, entropy_func, &p_entropy,
286 custom, custom_len ) == 0 );
287 hmac_drbg_set_prediction_resistance( &ctx, POLARSSL_HMAC_DRBG_PR_ON );
288 TEST_ASSERT( hmac_drbg_random_with_add( &ctx, my_output, out_len,
289 add1, add1_len ) == 0 );
290 TEST_ASSERT( hmac_drbg_random_with_add( &ctx, my_output, out_len,
291 add2, add2_len ) == 0 );
292 hmac_drbg_free( &ctx );
293
294 TEST_ASSERT( memcmp( my_output, output, out_len ) == 0 );
295}
296/* END_CASE */
297
Manuel Pégourié-Gonnard79afaa02014-01-31 11:12:09 +0100298/* BEGIN_CASE depends_on:POLARSSL_SELF_TEST */
299void hmac_drbg_selftest( )
300{
301 TEST_ASSERT( hmac_drbg_self_test( 0 ) == 0 );
302}
303/* END_CASE */