blob: bd451126794a26cf43a540882ad21b498e926c27 [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 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200104
105exit:
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +0100106 hmac_drbg_free( &ctx );
107}
108/* END_CASE */
109
Manuel Pégourié-Gonnard48bc3e82014-01-30 21:11:16 +0100110/* BEGIN_CASE depends_on:POLARSSL_FS_IO */
111void 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 Bakker94b916c2014-04-17 16:07:20 +0200116 md_info = md_info_from_type( md_alg );
117 TEST_ASSERT( md_info != NULL );
118
Manuel Pégourié-Gonnard48bc3e82014-01-30 21:11:16 +0100119 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 Bakkerbd51b262014-07-10 15:26:12 +0200125exit:
Manuel Pégourié-Gonnard48bc3e82014-01-30 21:11:16 +0100126 hmac_drbg_free( &ctx );
127}
128/* END_CASE */
129
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100130/* BEGIN_CASE */
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +0100131void 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 Bakker94b916c2014-04-17 16:07:20 +0200142 md_info = md_info_from_type( md_alg );
143 TEST_ASSERT( md_info != NULL );
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +0100144 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 Bakkerbd51b262014-07-10 15:26:12 +0200153exit:
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +0100154 hmac_drbg_free( &ctx );
155}
156/* END_CASE */
157
158/* BEGIN_CASE */
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100159void 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é-Gonnarde6cdbbd2014-02-01 11:30:03 +0100164 unsigned char data[1024];
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100165 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 Bakker94b916c2014-04-17 16:07:20 +0200185 md_info = md_info_from_type( md_alg );
186 TEST_ASSERT( md_info != NULL );
Manuel Pégourié-Gonnarde6cdbbd2014-02-01 11:30:03 +0100187
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 Bakkerbd51b262014-07-10 15:26:12 +0200197
198 /* clear for second run */
Manuel Pégourié-Gonnarde6cdbbd2014-02-01 11:30:03 +0100199 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é-Gonnard6801f392014-01-30 17:22:14 +0100204 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é-Gonnard24600b72014-01-31 09:54:14 +0100210 TEST_ASSERT( memcmp( my_output, output, out_len ) == 0 );
Manuel Pégourié-Gonnarde6cdbbd2014-02-01 11:30:03 +0100211
Paul Bakkerbd51b262014-07-10 15:26:12 +0200212exit:
213 hmac_drbg_free( &ctx );
Manuel Pégourié-Gonnard24600b72014-01-31 09:54:14 +0100214}
215/* END_CASE */
216
217/* BEGIN_CASE */
218void 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 Bakker94b916c2014-04-17 16:07:20 +0200245 md_info = md_info_from_type( md_alg );
246 TEST_ASSERT( md_info != NULL );
247
Manuel Pégourié-Gonnard24600b72014-01-31 09:54:14 +0100248 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é-Gonnard24600b72014-01-31 09:54:14 +0100255
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100256 TEST_ASSERT( memcmp( my_output, output, out_len ) == 0 );
257
Paul Bakkerbd51b262014-07-10 15:26:12 +0200258exit:
259 hmac_drbg_free( &ctx );
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100260}
261/* END_CASE */
262
Manuel Pégourié-Gonnard62273b82014-01-31 10:16:57 +0100263/* BEGIN_CASE */
264void 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 Bakker94b916c2014-04-17 16:07:20 +0200289 md_info = md_info_from_type( md_alg );
290 TEST_ASSERT( md_info != NULL );
291
Manuel Pégourié-Gonnard62273b82014-01-31 10:16:57 +0100292 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é-Gonnard62273b82014-01-31 10:16:57 +0100299
300 TEST_ASSERT( memcmp( my_output, output, out_len ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200301
302exit:
303 hmac_drbg_free( &ctx );
Manuel Pégourié-Gonnard62273b82014-01-31 10:16:57 +0100304}
305/* END_CASE */
306
Manuel Pégourié-Gonnard79afaa02014-01-31 11:12:09 +0100307/* BEGIN_CASE depends_on:POLARSSL_SELF_TEST */
308void hmac_drbg_selftest( )
309{
310 TEST_ASSERT( hmac_drbg_self_test( 0 ) == 0 );
311}
312/* END_CASE */