blob: d58c426cf448c59e2c663946a48885d5ffc60867 [file] [log] [blame]
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +01001/* BEGIN_HEADER */
2#include <polarssl/hmac_drbg.h>
3
Rich Evans00ab4702015-02-06 13:43:58 +00004#include <string.h>
5
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +01006typedef struct
7{
8 unsigned char *p;
9 size_t len;
10} entropy_ctx;
11
12int entropy_func( void *data, unsigned char *buf, size_t len )
13{
14 entropy_ctx *ctx = (entropy_ctx *) data;
15
16 if( len > ctx->len )
17 return( -1 );
18
19 memcpy( buf, ctx->p, len );
20
21 ctx->p += len;
22 ctx->len -= len;
23
24 return( 0 );
25}
26/* END_HEADER */
27
28/* BEGIN_DEPENDENCIES
29 * depends_on:POLARSSL_HMAC_DRBG_C
30 * END_DEPENDENCIES
31 */
32
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +010033/* BEGIN_CASE */
34void hmac_drbg_entropy_usage( int md_alg )
35{
36 unsigned char out[16];
37 unsigned char buf[1024];
38 const md_info_t *md_info;
39 hmac_drbg_context ctx;
40 entropy_ctx entropy;
41 size_t last_len, i, reps = 10;
42
43 memset( buf, 0, sizeof( buf ) );
44 memset( out, 0, sizeof( out ) );
45
46 entropy.len = sizeof( buf );
47 entropy.p = buf;
48
Paul Bakker94b916c2014-04-17 16:07:20 +020049 md_info = md_info_from_type( md_alg );
50 TEST_ASSERT( md_info != NULL );
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +010051
52 /* Init must use entropy */
53 last_len = entropy.len;
54 TEST_ASSERT( hmac_drbg_init( &ctx, md_info, entropy_func, &entropy,
55 NULL, 0 ) == 0 );
56 TEST_ASSERT( entropy.len < last_len );
57
58 /* By default, PR is off and reseed_interval is large,
59 * so the next few calls should not use entropy */
60 last_len = entropy.len;
61 for( i = 0; i < reps; i++ )
62 {
63 TEST_ASSERT( hmac_drbg_random( &ctx, out, sizeof( out ) - 4 ) == 0 );
64 TEST_ASSERT( hmac_drbg_random_with_add( &ctx, out, sizeof( out ) - 4,
65 buf, 16 ) == 0 );
66 }
67 TEST_ASSERT( entropy.len == last_len );
68
69 /* While at it, make sure we didn't write past the requested length */
70 TEST_ASSERT( out[sizeof( out ) - 4] == 0 );
71 TEST_ASSERT( out[sizeof( out ) - 3] == 0 );
72 TEST_ASSERT( out[sizeof( out ) - 2] == 0 );
73 TEST_ASSERT( out[sizeof( out ) - 1] == 0 );
74
75 /* Set reseed_interval to the number of calls done,
76 * so the next call should reseed */
77 hmac_drbg_set_reseed_interval( &ctx, 2 * reps );
78 TEST_ASSERT( hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
79 TEST_ASSERT( entropy.len < last_len );
80
81 /* The new few calls should not reseed */
82 last_len = entropy.len;
83 for( i = 0; i < reps / 2; i++ )
84 {
85 TEST_ASSERT( hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
86 TEST_ASSERT( hmac_drbg_random_with_add( &ctx, out, sizeof( out ) ,
87 buf, 16 ) == 0 );
88 }
89 TEST_ASSERT( entropy.len == last_len );
90
91 /* Now enable PR, so the next few calls should all reseed */
92 hmac_drbg_set_prediction_resistance( &ctx, POLARSSL_HMAC_DRBG_PR_ON );
93 TEST_ASSERT( hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
94 TEST_ASSERT( entropy.len < last_len );
95
96 /* Finally, check setting entropy_len */
97 hmac_drbg_set_entropy_len( &ctx, 42 );
98 last_len = entropy.len;
99 TEST_ASSERT( hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
100 TEST_ASSERT( (int) last_len - entropy.len == 42 );
101
102 hmac_drbg_set_entropy_len( &ctx, 13 );
103 last_len = entropy.len;
104 TEST_ASSERT( hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
105 TEST_ASSERT( (int) last_len - entropy.len == 13 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200106
107exit:
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +0100108 hmac_drbg_free( &ctx );
109}
110/* END_CASE */
111
Manuel Pégourié-Gonnard48bc3e82014-01-30 21:11:16 +0100112/* BEGIN_CASE depends_on:POLARSSL_FS_IO */
113void hmac_drbg_seed_file( int md_alg, char *path, int ret )
114{
115 const md_info_t *md_info;
116 hmac_drbg_context ctx;
117
Paul Bakker94b916c2014-04-17 16:07:20 +0200118 md_info = md_info_from_type( md_alg );
119 TEST_ASSERT( md_info != NULL );
120
Manuel Pégourié-Gonnard48bc3e82014-01-30 21:11:16 +0100121 TEST_ASSERT( hmac_drbg_init( &ctx, md_info, rnd_std_rand, NULL,
122 NULL, 0 ) == 0 );
123
124 TEST_ASSERT( hmac_drbg_write_seed_file( &ctx, path ) == ret );
125 TEST_ASSERT( hmac_drbg_update_seed_file( &ctx, path ) == ret );
126
Paul Bakkerbd51b262014-07-10 15:26:12 +0200127exit:
Manuel Pégourié-Gonnard48bc3e82014-01-30 21:11:16 +0100128 hmac_drbg_free( &ctx );
129}
130/* END_CASE */
131
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100132/* BEGIN_CASE */
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +0100133void hmac_drbg_buf( int md_alg )
134{
135 unsigned char out[16];
136 unsigned char buf[100];
137 const md_info_t *md_info;
138 hmac_drbg_context ctx;
139 size_t i;
140
141 memset( buf, 0, sizeof( buf ) );
142 memset( out, 0, sizeof( out ) );
143
Paul Bakker94b916c2014-04-17 16:07:20 +0200144 md_info = md_info_from_type( md_alg );
145 TEST_ASSERT( md_info != NULL );
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +0100146 TEST_ASSERT( hmac_drbg_init_buf( &ctx, md_info, buf, sizeof( buf ) ) == 0 );
147
148 /* Make sure it never tries to reseed (would segfault otherwise) */
149 hmac_drbg_set_reseed_interval( &ctx, 3 );
150 hmac_drbg_set_prediction_resistance( &ctx, POLARSSL_HMAC_DRBG_PR_ON );
151
152 for( i = 0; i < 30; i++ )
153 TEST_ASSERT( hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
154
Paul Bakkerbd51b262014-07-10 15:26:12 +0200155exit:
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +0100156 hmac_drbg_free( &ctx );
157}
158/* END_CASE */
159
160/* BEGIN_CASE */
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100161void hmac_drbg_no_reseed( int md_alg,
162 char *entropy_hex, char *custom_hex,
163 char *add1_hex, char *add2_hex,
164 char *output_hex )
165{
Manuel Pégourié-Gonnarde6cdbbd2014-02-01 11:30:03 +0100166 unsigned char data[1024];
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100167 unsigned char entropy[512];
168 unsigned char custom[512];
169 unsigned char add1[512];
170 unsigned char add2[512];
171 unsigned char output[512];
172 unsigned char my_output[512];
173 size_t custom_len, add1_len, add2_len, out_len;
174 entropy_ctx p_entropy;
175 const md_info_t *md_info;
176 hmac_drbg_context ctx;
177
178 memset( my_output, 0, sizeof my_output );
179
180 custom_len = unhexify( custom, custom_hex );
181 add1_len = unhexify( add1, add1_hex );
182 add2_len = unhexify( add2, add2_hex );
183 out_len = unhexify( output, output_hex );
184 p_entropy.len = unhexify( entropy, entropy_hex );
185 p_entropy.p = entropy;
186
Paul Bakker94b916c2014-04-17 16:07:20 +0200187 md_info = md_info_from_type( md_alg );
188 TEST_ASSERT( md_info != NULL );
Manuel Pégourié-Gonnarde6cdbbd2014-02-01 11:30:03 +0100189
190 /* Test the simplified buffer-based variant */
191 memcpy( data, entropy, p_entropy.len );
192 memcpy( data + p_entropy.len, custom, custom_len );
193 TEST_ASSERT( hmac_drbg_init_buf( &ctx, md_info,
194 data, p_entropy.len + custom_len ) == 0 );
195 TEST_ASSERT( hmac_drbg_random_with_add( &ctx, my_output, out_len,
196 add1, add1_len ) == 0 );
197 TEST_ASSERT( hmac_drbg_random_with_add( &ctx, my_output, out_len,
198 add2, add2_len ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200199
200 /* clear for second run */
Manuel Pégourié-Gonnarde6cdbbd2014-02-01 11:30:03 +0100201 hmac_drbg_free( &ctx );
202
203 TEST_ASSERT( memcmp( my_output, output, out_len ) == 0 );
204
205 /* And now the normal entropy-based variant */
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100206 TEST_ASSERT( hmac_drbg_init( &ctx, md_info, entropy_func, &p_entropy,
207 custom, custom_len ) == 0 );
208 TEST_ASSERT( hmac_drbg_random_with_add( &ctx, my_output, out_len,
209 add1, add1_len ) == 0 );
210 TEST_ASSERT( hmac_drbg_random_with_add( &ctx, my_output, out_len,
211 add2, add2_len ) == 0 );
Manuel Pégourié-Gonnard24600b72014-01-31 09:54:14 +0100212 TEST_ASSERT( memcmp( my_output, output, out_len ) == 0 );
Manuel Pégourié-Gonnarde6cdbbd2014-02-01 11:30:03 +0100213
Paul Bakkerbd51b262014-07-10 15:26:12 +0200214exit:
215 hmac_drbg_free( &ctx );
Manuel Pégourié-Gonnard24600b72014-01-31 09:54:14 +0100216}
217/* END_CASE */
218
219/* BEGIN_CASE */
220void hmac_drbg_nopr( int md_alg,
221 char *entropy_hex, char *custom_hex,
222 char *add1_hex, char *add2_hex, char *add3_hex,
223 char *output_hex )
224{
225 unsigned char entropy[512];
226 unsigned char custom[512];
227 unsigned char add1[512];
228 unsigned char add2[512];
229 unsigned char add3[512];
230 unsigned char output[512];
231 unsigned char my_output[512];
232 size_t custom_len, add1_len, add2_len, add3_len, out_len;
233 entropy_ctx p_entropy;
234 const md_info_t *md_info;
235 hmac_drbg_context ctx;
236
237 memset( my_output, 0, sizeof my_output );
238
239 custom_len = unhexify( custom, custom_hex );
240 add1_len = unhexify( add1, add1_hex );
241 add2_len = unhexify( add2, add2_hex );
242 add3_len = unhexify( add3, add3_hex );
243 out_len = unhexify( output, output_hex );
244 p_entropy.len = unhexify( entropy, entropy_hex );
245 p_entropy.p = entropy;
246
Paul Bakker94b916c2014-04-17 16:07:20 +0200247 md_info = md_info_from_type( md_alg );
248 TEST_ASSERT( md_info != NULL );
249
Manuel Pégourié-Gonnard24600b72014-01-31 09:54:14 +0100250 TEST_ASSERT( hmac_drbg_init( &ctx, md_info, entropy_func, &p_entropy,
251 custom, custom_len ) == 0 );
252 TEST_ASSERT( hmac_drbg_reseed( &ctx, add1, add1_len ) == 0 );
253 TEST_ASSERT( hmac_drbg_random_with_add( &ctx, my_output, out_len,
254 add2, add2_len ) == 0 );
255 TEST_ASSERT( hmac_drbg_random_with_add( &ctx, my_output, out_len,
256 add3, add3_len ) == 0 );
Manuel Pégourié-Gonnard24600b72014-01-31 09:54:14 +0100257
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100258 TEST_ASSERT( memcmp( my_output, output, out_len ) == 0 );
259
Paul Bakkerbd51b262014-07-10 15:26:12 +0200260exit:
261 hmac_drbg_free( &ctx );
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100262}
263/* END_CASE */
264
Manuel Pégourié-Gonnard62273b82014-01-31 10:16:57 +0100265/* BEGIN_CASE */
266void hmac_drbg_pr( int md_alg,
267 char *entropy_hex, char *custom_hex,
268 char *add1_hex, char *add2_hex,
269 char *output_hex )
270{
271 unsigned char entropy[512];
272 unsigned char custom[512];
273 unsigned char add1[512];
274 unsigned char add2[512];
275 unsigned char output[512];
276 unsigned char my_output[512];
277 size_t custom_len, add1_len, add2_len, out_len;
278 entropy_ctx p_entropy;
279 const md_info_t *md_info;
280 hmac_drbg_context ctx;
281
282 memset( my_output, 0, sizeof my_output );
283
284 custom_len = unhexify( custom, custom_hex );
285 add1_len = unhexify( add1, add1_hex );
286 add2_len = unhexify( add2, add2_hex );
287 out_len = unhexify( output, output_hex );
288 p_entropy.len = unhexify( entropy, entropy_hex );
289 p_entropy.p = entropy;
290
Paul Bakker94b916c2014-04-17 16:07:20 +0200291 md_info = md_info_from_type( md_alg );
292 TEST_ASSERT( md_info != NULL );
293
Manuel Pégourié-Gonnard62273b82014-01-31 10:16:57 +0100294 TEST_ASSERT( hmac_drbg_init( &ctx, md_info, entropy_func, &p_entropy,
295 custom, custom_len ) == 0 );
296 hmac_drbg_set_prediction_resistance( &ctx, POLARSSL_HMAC_DRBG_PR_ON );
297 TEST_ASSERT( hmac_drbg_random_with_add( &ctx, my_output, out_len,
298 add1, add1_len ) == 0 );
299 TEST_ASSERT( hmac_drbg_random_with_add( &ctx, my_output, out_len,
300 add2, add2_len ) == 0 );
Manuel Pégourié-Gonnard62273b82014-01-31 10:16:57 +0100301
302 TEST_ASSERT( memcmp( my_output, output, out_len ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200303
304exit:
305 hmac_drbg_free( &ctx );
Manuel Pégourié-Gonnard62273b82014-01-31 10:16:57 +0100306}
307/* END_CASE */
308
Manuel Pégourié-Gonnard79afaa02014-01-31 11:12:09 +0100309/* BEGIN_CASE depends_on:POLARSSL_SELF_TEST */
310void hmac_drbg_selftest( )
311{
312 TEST_ASSERT( hmac_drbg_self_test( 0 ) == 0 );
313}
314/* END_CASE */