]> git.ipfire.org Git - thirdparty/openssl.git/blob - test/drbgtest.c
Fix Typos
[thirdparty/openssl.git] / test / drbgtest.c
1 /*
2 * Copyright 2011-2018 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 #include <string.h>
11 #include "internal/nelem.h"
12 #include <openssl/crypto.h>
13 #include <openssl/err.h>
14 #include <openssl/rand.h>
15 #include <openssl/obj_mac.h>
16 #include <openssl/evp.h>
17 #include <openssl/aes.h>
18 #include "../crypto/rand/rand_lcl.h"
19 #include "../crypto/include/internal/rand_int.h"
20
21 #if defined(_WIN32)
22 # include <windows.h>
23 #endif
24
25 #include "testutil.h"
26 #include "drbgtest.h"
27
28 typedef struct drbg_selftest_data_st {
29 int post;
30 int nid;
31 unsigned int flags;
32
33 /* KAT data for no PR */
34 const unsigned char *entropy;
35 size_t entropylen;
36 const unsigned char *nonce;
37 size_t noncelen;
38 const unsigned char *pers;
39 size_t perslen;
40 const unsigned char *adin;
41 size_t adinlen;
42 const unsigned char *entropyreseed;
43 size_t entropyreseedlen;
44 const unsigned char *adinreseed;
45 size_t adinreseedlen;
46 const unsigned char *adin2;
47 size_t adin2len;
48 const unsigned char *expected;
49 size_t exlen;
50 const unsigned char *kat2;
51 size_t kat2len;
52
53 /* KAT data for PR */
54 const unsigned char *entropy_pr;
55 size_t entropylen_pr;
56 const unsigned char *nonce_pr;
57 size_t noncelen_pr;
58 const unsigned char *pers_pr;
59 size_t perslen_pr;
60 const unsigned char *adin_pr;
61 size_t adinlen_pr;
62 const unsigned char *entropypr_pr;
63 size_t entropyprlen_pr;
64 const unsigned char *ading_pr;
65 size_t adinglen_pr;
66 const unsigned char *entropyg_pr;
67 size_t entropyglen_pr;
68 const unsigned char *kat_pr;
69 size_t katlen_pr;
70 const unsigned char *kat2_pr;
71 size_t kat2len_pr;
72 } DRBG_SELFTEST_DATA;
73
74 #define make_drbg_test_data(nid, flag, pr, post) {\
75 post, nid, flag, \
76 pr##_entropyinput, sizeof(pr##_entropyinput), \
77 pr##_nonce, sizeof(pr##_nonce), \
78 pr##_personalizationstring, sizeof(pr##_personalizationstring), \
79 pr##_additionalinput, sizeof(pr##_additionalinput), \
80 pr##_entropyinputreseed, sizeof(pr##_entropyinputreseed), \
81 pr##_additionalinputreseed, sizeof(pr##_additionalinputreseed), \
82 pr##_additionalinput2, sizeof(pr##_additionalinput2), \
83 pr##_int_returnedbits, sizeof(pr##_int_returnedbits), \
84 pr##_returnedbits, sizeof(pr##_returnedbits), \
85 pr##_pr_entropyinput, sizeof(pr##_pr_entropyinput), \
86 pr##_pr_nonce, sizeof(pr##_pr_nonce), \
87 pr##_pr_personalizationstring, sizeof(pr##_pr_personalizationstring), \
88 pr##_pr_additionalinput, sizeof(pr##_pr_additionalinput), \
89 pr##_pr_entropyinputpr, sizeof(pr##_pr_entropyinputpr), \
90 pr##_pr_additionalinput2, sizeof(pr##_pr_additionalinput2), \
91 pr##_pr_entropyinputpr2, sizeof(pr##_pr_entropyinputpr2), \
92 pr##_pr_int_returnedbits, sizeof(pr##_pr_int_returnedbits), \
93 pr##_pr_returnedbits, sizeof(pr##_pr_returnedbits) \
94 }
95
96 #define make_drbg_test_data_use_df(nid, pr, p) \
97 make_drbg_test_data(nid, 0, pr, p)
98
99 #define make_drbg_test_data_no_df(nid, pr, p) \
100 make_drbg_test_data(nid, RAND_DRBG_FLAG_CTR_NO_DF, pr, p)
101
102 #define make_drbg_test_data_hash(nid, pr, p) \
103 make_drbg_test_data(nid, RAND_DRBG_FLAG_HMAC, hmac_##pr, p), \
104 make_drbg_test_data(nid, 0, pr, p)
105
106 static DRBG_SELFTEST_DATA drbg_test[] = {
107 #ifndef FIPS_MODE
108 /* FIPS mode doesn't support CTR DRBG without a derivation function */
109 make_drbg_test_data_no_df (NID_aes_128_ctr, aes_128_no_df, 0),
110 make_drbg_test_data_no_df (NID_aes_192_ctr, aes_192_no_df, 0),
111 make_drbg_test_data_no_df (NID_aes_256_ctr, aes_256_no_df, 1),
112 #endif
113 make_drbg_test_data_use_df(NID_aes_128_ctr, aes_128_use_df, 0),
114 make_drbg_test_data_use_df(NID_aes_192_ctr, aes_192_use_df, 0),
115 make_drbg_test_data_use_df(NID_aes_256_ctr, aes_256_use_df, 1),
116 make_drbg_test_data_hash(NID_sha1, sha1, 0),
117 make_drbg_test_data_hash(NID_sha224, sha224, 0),
118 make_drbg_test_data_hash(NID_sha256, sha256, 1),
119 make_drbg_test_data_hash(NID_sha384, sha384, 0),
120 make_drbg_test_data_hash(NID_sha512, sha512, 0),
121 };
122
123 static int app_data_index;
124
125 /*
126 * Test context data, attached as EXDATA to the RAND_DRBG
127 */
128 typedef struct test_ctx_st {
129 const unsigned char *entropy;
130 size_t entropylen;
131 int entropycnt;
132 const unsigned char *nonce;
133 size_t noncelen;
134 int noncecnt;
135 } TEST_CTX;
136
137 static size_t kat_entropy(RAND_DRBG *drbg, unsigned char **pout,
138 int entropy, size_t min_len, size_t max_len,
139 int prediction_resistance)
140 {
141 TEST_CTX *t = (TEST_CTX *)RAND_DRBG_get_ex_data(drbg, app_data_index);
142
143 t->entropycnt++;
144 *pout = (unsigned char *)t->entropy;
145 return t->entropylen;
146 }
147
148 static size_t kat_nonce(RAND_DRBG *drbg, unsigned char **pout,
149 int entropy, size_t min_len, size_t max_len)
150 {
151 TEST_CTX *t = (TEST_CTX *)RAND_DRBG_get_ex_data(drbg, app_data_index);
152
153 t->noncecnt++;
154 *pout = (unsigned char *)t->nonce;
155 return t->noncelen;
156 }
157
158 /*
159 * Disable CRNG testing if it is enabled.
160 * If the DRBG is ready or in an error state, this means an instantiate cycle
161 * for which the default personalisation string is used.
162 */
163 static int disable_crngt(RAND_DRBG *drbg)
164 {
165 static const char pers[] = DRBG_DEFAULT_PERS_STRING;
166 const int instantiate = drbg->state != DRBG_UNINITIALISED;
167
168 if (drbg->get_entropy != rand_crngt_get_entropy)
169 return 1;
170
171 if ((instantiate && !RAND_DRBG_uninstantiate(drbg))
172 || !TEST_true(RAND_DRBG_set_callbacks(drbg, &rand_drbg_get_entropy,
173 &rand_drbg_cleanup_entropy,
174 &rand_drbg_get_nonce,
175 &rand_drbg_cleanup_nonce))
176 || (instantiate
177 && !RAND_DRBG_instantiate(drbg, (const unsigned char *)pers,
178 sizeof(pers) - 1)))
179 return 0;
180 return 1;
181 }
182
183 static int uninstantiate(RAND_DRBG *drbg)
184 {
185 int ret = drbg == NULL ? 1 : RAND_DRBG_uninstantiate(drbg);
186
187 ERR_clear_error();
188 return ret;
189 }
190
191 /*
192 * Do a single KAT test. Return 0 on failure.
193 */
194 static int single_kat(DRBG_SELFTEST_DATA *td)
195 {
196 RAND_DRBG *drbg = NULL;
197 TEST_CTX t;
198 int failures = 0;
199 unsigned char buff[1024];
200
201 /*
202 * Test without PR: Instantiate DRBG with test entropy, nonce and
203 * personalisation string.
204 */
205 if (!TEST_ptr(drbg = RAND_DRBG_new(td->nid, td->flags, NULL)))
206 return 0;
207 if (!TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
208 kat_nonce, NULL))
209 || !TEST_true(disable_crngt(drbg))) {
210 failures++;
211 goto err;
212 }
213 memset(&t, 0, sizeof(t));
214 t.entropy = td->entropy;
215 t.entropylen = td->entropylen;
216 t.nonce = td->nonce;
217 t.noncelen = td->noncelen;
218 RAND_DRBG_set_ex_data(drbg, app_data_index, &t);
219
220 if (!TEST_true(RAND_DRBG_instantiate(drbg, td->pers, td->perslen))
221 || !TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
222 td->adin, td->adinlen))
223 || !TEST_mem_eq(td->expected, td->exlen, buff, td->exlen))
224 failures++;
225
226 /* Reseed DRBG with test entropy and additional input */
227 t.entropy = td->entropyreseed;
228 t.entropylen = td->entropyreseedlen;
229 if (!TEST_true(RAND_DRBG_reseed(drbg, td->adinreseed, td->adinreseedlen, 0)
230 || !TEST_true(RAND_DRBG_generate(drbg, buff, td->kat2len, 0,
231 td->adin2, td->adin2len))
232 || !TEST_mem_eq(td->kat2, td->kat2len, buff, td->kat2len)))
233 failures++;
234 uninstantiate(drbg);
235
236 /*
237 * Now test with PR: Instantiate DRBG with test entropy, nonce and
238 * personalisation string.
239 */
240 if (!TEST_true(RAND_DRBG_set(drbg, td->nid, td->flags))
241 || !TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
242 kat_nonce, NULL)))
243 failures++;
244 RAND_DRBG_set_ex_data(drbg, app_data_index, &t);
245 t.entropy = td->entropy_pr;
246 t.entropylen = td->entropylen_pr;
247 t.nonce = td->nonce_pr;
248 t.noncelen = td->noncelen_pr;
249 t.entropycnt = 0;
250 t.noncecnt = 0;
251 if (!TEST_true(RAND_DRBG_instantiate(drbg, td->pers_pr, td->perslen_pr)))
252 failures++;
253
254 /*
255 * Now generate with PR: we need to supply entropy as this will
256 * perform a reseed operation.
257 */
258 t.entropy = td->entropypr_pr;
259 t.entropylen = td->entropyprlen_pr;
260 if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->katlen_pr, 1,
261 td->adin_pr, td->adinlen_pr))
262 || !TEST_mem_eq(td->kat_pr, td->katlen_pr, buff, td->katlen_pr))
263 failures++;
264
265 /*
266 * Now generate again with PR: supply new entropy again.
267 */
268 t.entropy = td->entropyg_pr;
269 t.entropylen = td->entropyglen_pr;
270
271 if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->kat2len_pr, 1,
272 td->ading_pr, td->adinglen_pr))
273 || !TEST_mem_eq(td->kat2_pr, td->kat2len_pr,
274 buff, td->kat2len_pr))
275 failures++;
276
277 err:
278 uninstantiate(drbg);
279 RAND_DRBG_free(drbg);
280 return failures == 0;
281 }
282
283 /*
284 * Initialise a DRBG based on selftest data
285 */
286 static int init(RAND_DRBG *drbg, DRBG_SELFTEST_DATA *td, TEST_CTX *t)
287 {
288 if (!TEST_true(RAND_DRBG_set(drbg, td->nid, td->flags))
289 || !TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
290 kat_nonce, NULL)))
291 return 0;
292 RAND_DRBG_set_ex_data(drbg, app_data_index, t);
293 t->entropy = td->entropy;
294 t->entropylen = td->entropylen;
295 t->nonce = td->nonce;
296 t->noncelen = td->noncelen;
297 t->entropycnt = 0;
298 t->noncecnt = 0;
299 return 1;
300 }
301
302 /*
303 * Initialise and instantiate DRBG based on selftest data
304 */
305 static int instantiate(RAND_DRBG *drbg, DRBG_SELFTEST_DATA *td,
306 TEST_CTX *t)
307 {
308 if (!TEST_true(init(drbg, td, t))
309 || !TEST_true(RAND_DRBG_instantiate(drbg, td->pers, td->perslen)))
310 return 0;
311 return 1;
312 }
313
314 /*
315 * Perform extensive error checking as required by SP800-90.
316 * Induce several failure modes and check an error condition is set.
317 */
318 static int error_check(DRBG_SELFTEST_DATA *td)
319 {
320 static char zero[sizeof(RAND_DRBG)];
321 RAND_DRBG *drbg = NULL;
322 TEST_CTX t;
323 unsigned char buff[1024];
324 unsigned int reseed_counter_tmp;
325 int ret = 0;
326
327 if (!TEST_ptr(drbg = RAND_DRBG_new(td->nid, td->flags, NULL))
328 || !TEST_true(disable_crngt(drbg)))
329 goto err;
330
331 /*
332 * Personalisation string tests
333 */
334
335 /* Test detection of too large personalisation string */
336 if (!init(drbg, td, &t)
337 || RAND_DRBG_instantiate(drbg, td->pers, drbg->max_perslen + 1) > 0)
338 goto err;
339
340 /*
341 * Entropy source tests
342 */
343
344 /* Test entropy source failure detection: i.e. returns no data */
345 t.entropylen = 0;
346 if (TEST_int_le(RAND_DRBG_instantiate(drbg, td->pers, td->perslen), 0))
347 goto err;
348
349 /* Try to generate output from uninstantiated DRBG */
350 if (!TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
351 td->adin, td->adinlen))
352 || !uninstantiate(drbg))
353 goto err;
354
355 /* Test insufficient entropy */
356 t.entropylen = drbg->min_entropylen - 1;
357 if (!init(drbg, td, &t)
358 || RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0
359 || !uninstantiate(drbg))
360 goto err;
361
362 /* Test too much entropy */
363 t.entropylen = drbg->max_entropylen + 1;
364 if (!init(drbg, td, &t)
365 || RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0
366 || !uninstantiate(drbg))
367 goto err;
368
369 /*
370 * Nonce tests
371 */
372
373 /* Test too small nonce */
374 if (drbg->min_noncelen) {
375 t.noncelen = drbg->min_noncelen - 1;
376 if (!init(drbg, td, &t)
377 || RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0
378 || !uninstantiate(drbg))
379 goto err;
380 }
381
382 /* Test too large nonce */
383 if (drbg->max_noncelen) {
384 t.noncelen = drbg->max_noncelen + 1;
385 if (!init(drbg, td, &t)
386 || RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0
387 || !uninstantiate(drbg))
388 goto err;
389 }
390
391 /* Instantiate with valid data, Check generation is now OK */
392 if (!instantiate(drbg, td, &t)
393 || !TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
394 td->adin, td->adinlen)))
395 goto err;
396
397 /* Request too much data for one request */
398 if (!TEST_false(RAND_DRBG_generate(drbg, buff, drbg->max_request + 1, 0,
399 td->adin, td->adinlen)))
400 goto err;
401
402 /* Try too large additional input */
403 if (!TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
404 td->adin, drbg->max_adinlen + 1)))
405 goto err;
406
407 /*
408 * Check prediction resistance request fails if entropy source
409 * failure.
410 */
411 t.entropylen = 0;
412 if (TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 1,
413 td->adin, td->adinlen))
414 || !uninstantiate(drbg))
415 goto err;
416
417 /* Instantiate again with valid data */
418 if (!instantiate(drbg, td, &t))
419 goto err;
420 reseed_counter_tmp = drbg->reseed_gen_counter;
421 drbg->reseed_gen_counter = drbg->reseed_interval;
422
423 /* Generate output and check entropy has been requested for reseed */
424 t.entropycnt = 0;
425 if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
426 td->adin, td->adinlen))
427 || !TEST_int_eq(t.entropycnt, 1)
428 || !TEST_int_eq(drbg->reseed_gen_counter, reseed_counter_tmp + 1)
429 || !uninstantiate(drbg))
430 goto err;
431
432 /*
433 * Check prediction resistance request fails if entropy source
434 * failure.
435 */
436 t.entropylen = 0;
437 if (!TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 1,
438 td->adin, td->adinlen))
439 || !uninstantiate(drbg))
440 goto err;
441
442 /* Test reseed counter works */
443 if (!instantiate(drbg, td, &t))
444 goto err;
445 reseed_counter_tmp = drbg->reseed_gen_counter;
446 drbg->reseed_gen_counter = drbg->reseed_interval;
447
448 /* Generate output and check entropy has been requested for reseed */
449 t.entropycnt = 0;
450 if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
451 td->adin, td->adinlen))
452 || !TEST_int_eq(t.entropycnt, 1)
453 || !TEST_int_eq(drbg->reseed_gen_counter, reseed_counter_tmp + 1)
454 || !uninstantiate(drbg))
455 goto err;
456
457 /*
458 * Explicit reseed tests
459 */
460
461 /* Test explicit reseed with too large additional input */
462 if (!instantiate(drbg, td, &t)
463 || RAND_DRBG_reseed(drbg, td->adin, drbg->max_adinlen + 1, 0) > 0)
464 goto err;
465
466 /* Test explicit reseed with entropy source failure */
467 t.entropylen = 0;
468 if (!TEST_int_le(RAND_DRBG_reseed(drbg, td->adin, td->adinlen, 0), 0)
469 || !uninstantiate(drbg))
470 goto err;
471
472 /* Test explicit reseed with too much entropy */
473 if (!instantiate(drbg, td, &t))
474 goto err;
475 t.entropylen = drbg->max_entropylen + 1;
476 if (!TEST_int_le(RAND_DRBG_reseed(drbg, td->adin, td->adinlen, 0), 0)
477 || !uninstantiate(drbg))
478 goto err;
479
480 /* Test explicit reseed with too little entropy */
481 if (!instantiate(drbg, td, &t))
482 goto err;
483 t.entropylen = drbg->min_entropylen - 1;
484 if (!TEST_int_le(RAND_DRBG_reseed(drbg, td->adin, td->adinlen, 0), 0)
485 || !uninstantiate(drbg))
486 goto err;
487
488 /* Standard says we have to check uninstantiate really zeroes */
489 if (!TEST_mem_eq(zero, sizeof(drbg->data), &drbg->data, sizeof(drbg->data)))
490 goto err;
491
492 ret = 1;
493
494 err:
495 uninstantiate(drbg);
496 RAND_DRBG_free(drbg);
497 return ret;
498 }
499
500 static int test_kats(int i)
501 {
502 DRBG_SELFTEST_DATA *td = &drbg_test[i];
503 int rv = 0;
504
505 if (!single_kat(td))
506 goto err;
507 rv = 1;
508
509 err:
510 return rv;
511 }
512
513 static int test_error_checks(int i)
514 {
515 DRBG_SELFTEST_DATA *td = &drbg_test[i];
516 int rv = 0;
517
518 if (error_check(td))
519 goto err;
520 rv = 1;
521
522 err:
523 return rv;
524 }
525
526 /*
527 * Hook context data, attached as EXDATA to the RAND_DRBG
528 */
529 typedef struct hook_ctx_st {
530 RAND_DRBG *drbg;
531 /*
532 * Currently, all DRBGs use the same get_entropy() callback.
533 * The tests however, don't assume this and store
534 * the original callback for every DRBG separately.
535 */
536 RAND_DRBG_get_entropy_fn get_entropy;
537 /* forces a failure of the get_entropy() call if nonzero */
538 int fail;
539 /* counts successful reseeds */
540 int reseed_count;
541 } HOOK_CTX;
542
543 static HOOK_CTX master_ctx, public_ctx, private_ctx;
544
545 static HOOK_CTX *get_hook_ctx(RAND_DRBG *drbg)
546 {
547 return (HOOK_CTX *)RAND_DRBG_get_ex_data(drbg, app_data_index);
548 }
549
550 /* Intercepts and counts calls to the get_entropy() callback */
551 static size_t get_entropy_hook(RAND_DRBG *drbg, unsigned char **pout,
552 int entropy, size_t min_len, size_t max_len,
553 int prediction_resistance)
554 {
555 size_t ret;
556 HOOK_CTX *ctx = get_hook_ctx(drbg);
557
558 if (ctx->fail != 0)
559 return 0;
560
561 ret = ctx->get_entropy(drbg, pout, entropy, min_len, max_len,
562 prediction_resistance);
563
564 if (ret != 0)
565 ctx->reseed_count++;
566 return ret;
567 }
568
569 /* Installs a hook for the get_entropy() callback of the given drbg */
570 static void hook_drbg(RAND_DRBG *drbg, HOOK_CTX *ctx)
571 {
572 memset(ctx, 0, sizeof(*ctx));
573 ctx->drbg = drbg;
574 ctx->get_entropy = drbg->get_entropy;
575 drbg->get_entropy = get_entropy_hook;
576 RAND_DRBG_set_ex_data(drbg, app_data_index, ctx);
577 }
578
579 /* Installs the hook for the get_entropy() callback of the given drbg */
580 static void unhook_drbg(RAND_DRBG *drbg)
581 {
582 HOOK_CTX *ctx = get_hook_ctx(drbg);
583
584 drbg->get_entropy = ctx->get_entropy;
585 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DRBG, drbg, &drbg->ex_data);
586 }
587
588 /* Resets the given hook context */
589 static void reset_hook_ctx(HOOK_CTX *ctx)
590 {
591 ctx->fail = 0;
592 ctx->reseed_count = 0;
593 }
594
595 /* Resets all drbg hook contexts */
596 static void reset_drbg_hook_ctx(void)
597 {
598 reset_hook_ctx(&master_ctx);
599 reset_hook_ctx(&public_ctx);
600 reset_hook_ctx(&private_ctx);
601 }
602
603 /*
604 * Generates random output using RAND_bytes() and RAND_priv_bytes()
605 * and checks whether the three shared DRBGs were reseeded as
606 * expected.
607 *
608 * |expect_success|: expected outcome (as reported by RAND_status())
609 * |master|, |public|, |private|: pointers to the three shared DRBGs
610 * |expect_xxx_reseed| =
611 * 1: it is expected that the specified DRBG is reseeded
612 * 0: it is expected that the specified DRBG is not reseeded
613 * -1: don't check whether the specified DRBG was reseeded or not
614 * |reseed_time|: if nonzero, used instead of time(NULL) to set the
615 * |before_reseed| time.
616 */
617 static int test_drbg_reseed(int expect_success,
618 RAND_DRBG *master,
619 RAND_DRBG *public,
620 RAND_DRBG *private,
621 int expect_master_reseed,
622 int expect_public_reseed,
623 int expect_private_reseed,
624 time_t reseed_time
625 )
626 {
627 unsigned char buf[32];
628 time_t before_reseed, after_reseed;
629 int expected_state = (expect_success ? DRBG_READY : DRBG_ERROR);
630
631 /*
632 * step 1: check preconditions
633 */
634
635 /* Test whether seed propagation is enabled */
636 if (!TEST_int_ne(master->reseed_prop_counter, 0)
637 || !TEST_int_ne(public->reseed_prop_counter, 0)
638 || !TEST_int_ne(private->reseed_prop_counter, 0))
639 return 0;
640
641 /* Check whether the master DRBG's reseed counter is the largest one */
642 if (!TEST_int_le(public->reseed_prop_counter, master->reseed_prop_counter)
643 || !TEST_int_le(private->reseed_prop_counter, master->reseed_prop_counter))
644 return 0;
645
646 /*
647 * step 2: generate random output
648 */
649
650 if (reseed_time == 0)
651 reseed_time = time(NULL);
652
653 /* Generate random output from the public and private DRBG */
654 before_reseed = expect_master_reseed == 1 ? reseed_time : 0;
655 if (!TEST_int_eq(RAND_bytes(buf, sizeof(buf)), expect_success)
656 || !TEST_int_eq(RAND_priv_bytes(buf, sizeof(buf)), expect_success))
657 return 0;
658 after_reseed = time(NULL);
659
660
661 /*
662 * step 3: check postconditions
663 */
664
665 /* Test whether reseeding succeeded as expected */
666 if (!TEST_int_eq(master->state, expected_state)
667 || !TEST_int_eq(public->state, expected_state)
668 || !TEST_int_eq(private->state, expected_state))
669 return 0;
670
671 if (expect_master_reseed >= 0) {
672 /* Test whether master DRBG was reseeded as expected */
673 if (!TEST_int_eq(master_ctx.reseed_count, expect_master_reseed))
674 return 0;
675 }
676
677 if (expect_public_reseed >= 0) {
678 /* Test whether public DRBG was reseeded as expected */
679 if (!TEST_int_eq(public_ctx.reseed_count, expect_public_reseed))
680 return 0;
681 }
682
683 if (expect_private_reseed >= 0) {
684 /* Test whether public DRBG was reseeded as expected */
685 if (!TEST_int_eq(private_ctx.reseed_count, expect_private_reseed))
686 return 0;
687 }
688
689 if (expect_success == 1) {
690 /* Test whether all three reseed counters are synchronized */
691 if (!TEST_int_eq(public->reseed_prop_counter, master->reseed_prop_counter)
692 || !TEST_int_eq(private->reseed_prop_counter, master->reseed_prop_counter))
693 return 0;
694
695 /* Test whether reseed time of master DRBG is set correctly */
696 if (!TEST_time_t_le(before_reseed, master->reseed_time)
697 || !TEST_time_t_le(master->reseed_time, after_reseed))
698 return 0;
699
700 /* Test whether reseed times of child DRBGs are synchronized with master */
701 if (!TEST_time_t_ge(public->reseed_time, master->reseed_time)
702 || !TEST_time_t_ge(private->reseed_time, master->reseed_time))
703 return 0;
704 } else {
705 ERR_clear_error();
706 }
707
708 return 1;
709 }
710
711 /*
712 * Test whether the default rand_method (RAND_OpenSSL()) is
713 * setup correctly, in particular whether reseeding works
714 * as designed.
715 */
716 static int test_rand_drbg_reseed(void)
717 {
718 RAND_DRBG *master, *public, *private;
719 unsigned char rand_add_buf[256];
720 int rv=0;
721 time_t before_reseed;
722
723 /* Check whether RAND_OpenSSL() is the default method */
724 if (!TEST_ptr_eq(RAND_get_rand_method(), RAND_OpenSSL()))
725 return 0;
726
727 /* All three DRBGs should be non-null */
728 if (!TEST_ptr(master = RAND_DRBG_get0_master())
729 || !TEST_ptr(public = RAND_DRBG_get0_public())
730 || !TEST_ptr(private = RAND_DRBG_get0_private()))
731 return 0;
732
733 /* There should be three distinct DRBGs, two of them chained to master */
734 if (!TEST_ptr_ne(public, private)
735 || !TEST_ptr_ne(public, master)
736 || !TEST_ptr_ne(private, master)
737 || !TEST_ptr_eq(public->parent, master)
738 || !TEST_ptr_eq(private->parent, master))
739 return 0;
740
741 /* Disable CRNG testing for the master DRBG */
742 if (!TEST_true(disable_crngt(master)))
743 return 0;
744
745 /* uninstantiate the three global DRBGs */
746 RAND_DRBG_uninstantiate(private);
747 RAND_DRBG_uninstantiate(public);
748 RAND_DRBG_uninstantiate(master);
749
750
751 /* Install hooks for the following tests */
752 hook_drbg(master, &master_ctx);
753 hook_drbg(public, &public_ctx);
754 hook_drbg(private, &private_ctx);
755
756
757 /*
758 * Test initial seeding of shared DRBGs
759 */
760 if (!TEST_true(test_drbg_reseed(1, master, public, private, 1, 1, 1, 0)))
761 goto error;
762 reset_drbg_hook_ctx();
763
764
765 /*
766 * Test initial state of shared DRBGs
767 */
768 if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 0, 0, 0)))
769 goto error;
770 reset_drbg_hook_ctx();
771
772 /*
773 * Test whether the public and private DRBG are both reseeded when their
774 * reseed counters differ from the master's reseed counter.
775 */
776 master->reseed_prop_counter++;
777 if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 1, 1, 0)))
778 goto error;
779 reset_drbg_hook_ctx();
780
781 /*
782 * Test whether the public DRBG is reseeded when its reseed counter differs
783 * from the master's reseed counter.
784 */
785 master->reseed_prop_counter++;
786 private->reseed_prop_counter++;
787 if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 1, 0, 0)))
788 goto error;
789 reset_drbg_hook_ctx();
790
791 /*
792 * Test whether the private DRBG is reseeded when its reseed counter differs
793 * from the master's reseed counter.
794 */
795 master->reseed_prop_counter++;
796 public->reseed_prop_counter++;
797 if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 0, 1, 0)))
798 goto error;
799 reset_drbg_hook_ctx();
800
801
802 /* fill 'randomness' buffer with some arbitrary data */
803 memset(rand_add_buf, 'r', sizeof(rand_add_buf));
804
805 #ifndef FIPS_MODE
806 /*
807 * Test whether all three DRBGs are reseeded by RAND_add().
808 * The before_reseed time has to be measured here and passed into the
809 * test_drbg_reseed() test, because the master DRBG gets already reseeded
810 * in RAND_add(), whence the check for the condition
811 * before_reseed <= master->reseed_time will fail if the time value happens
812 * to increase between the RAND_add() and the test_drbg_reseed() call.
813 */
814 before_reseed = time(NULL);
815 RAND_add(rand_add_buf, sizeof(rand_add_buf), sizeof(rand_add_buf));
816 if (!TEST_true(test_drbg_reseed(1, master, public, private, 1, 1, 1,
817 before_reseed)))
818 goto error;
819 reset_drbg_hook_ctx();
820
821
822 /*
823 * Test whether none of the DRBGs is reseed if the master fails to reseed
824 */
825 master_ctx.fail = 1;
826 master->reseed_prop_counter++;
827 RAND_add(rand_add_buf, sizeof(rand_add_buf), sizeof(rand_add_buf));
828 if (!TEST_true(test_drbg_reseed(0, master, public, private, 0, 0, 0, 0)))
829 goto error;
830 reset_drbg_hook_ctx();
831 #else /* FIPS_MODE */
832 /*
833 * In FIPS mode, random data provided by the application via RAND_add()
834 * is not considered a trusted entropy source. It is only treated as
835 * additional_data and no reseeding is forced. This test assures that
836 * no reseeding occurs.
837 */
838 before_reseed = time(NULL);
839 RAND_add(rand_add_buf, sizeof(rand_add_buf), sizeof(rand_add_buf));
840 if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 0, 0,
841 before_reseed)))
842 goto error;
843 reset_drbg_hook_ctx();
844 #endif
845
846 rv = 1;
847
848 error:
849 /* Remove hooks */
850 unhook_drbg(master);
851 unhook_drbg(public);
852 unhook_drbg(private);
853
854 return rv;
855 }
856
857 #if defined(OPENSSL_THREADS)
858 static int multi_thread_rand_bytes_succeeded = 1;
859 static int multi_thread_rand_priv_bytes_succeeded = 1;
860
861 static void run_multi_thread_test(void)
862 {
863 unsigned char buf[256];
864 time_t start = time(NULL);
865 RAND_DRBG *public = NULL, *private = NULL;
866
867 if (!TEST_ptr(public = RAND_DRBG_get0_public())
868 || !TEST_ptr(private = RAND_DRBG_get0_private())) {
869 multi_thread_rand_bytes_succeeded = 0;
870 return;
871 }
872 RAND_DRBG_set_reseed_time_interval(private, 1);
873 RAND_DRBG_set_reseed_time_interval(public, 1);
874
875 do {
876 if (RAND_bytes(buf, sizeof(buf)) <= 0)
877 multi_thread_rand_bytes_succeeded = 0;
878 if (RAND_priv_bytes(buf, sizeof(buf)) <= 0)
879 multi_thread_rand_priv_bytes_succeeded = 0;
880 }
881 while(time(NULL) - start < 5);
882 }
883
884 # if defined(OPENSSL_SYS_WINDOWS)
885
886 typedef HANDLE thread_t;
887
888 static DWORD WINAPI thread_run(LPVOID arg)
889 {
890 run_multi_thread_test();
891 /*
892 * Because we're linking with a static library, we must stop each
893 * thread explicitly, or so says OPENSSL_thread_stop(3)
894 */
895 OPENSSL_thread_stop();
896 return 0;
897 }
898
899 static int run_thread(thread_t *t)
900 {
901 *t = CreateThread(NULL, 0, thread_run, NULL, 0, NULL);
902 return *t != NULL;
903 }
904
905 static int wait_for_thread(thread_t thread)
906 {
907 return WaitForSingleObject(thread, INFINITE) == 0;
908 }
909
910 # else
911
912 typedef pthread_t thread_t;
913
914 static void *thread_run(void *arg)
915 {
916 run_multi_thread_test();
917 /*
918 * Because we're linking with a static library, we must stop each
919 * thread explicitly, or so says OPENSSL_thread_stop(3)
920 */
921 OPENSSL_thread_stop();
922 return NULL;
923 }
924
925 static int run_thread(thread_t *t)
926 {
927 return pthread_create(t, NULL, thread_run, NULL) == 0;
928 }
929
930 static int wait_for_thread(thread_t thread)
931 {
932 return pthread_join(thread, NULL) == 0;
933 }
934
935 # endif
936
937 /*
938 * The main thread will also run the test, so we'll have THREADS+1 parallel
939 * tests running
940 */
941 # define THREADS 3
942
943 static int test_multi_thread(void)
944 {
945 thread_t t[THREADS];
946 int i;
947
948 for (i = 0; i < THREADS; i++)
949 run_thread(&t[i]);
950 run_multi_thread_test();
951 for (i = 0; i < THREADS; i++)
952 wait_for_thread(t[i]);
953
954 if (!TEST_true(multi_thread_rand_bytes_succeeded))
955 return 0;
956 if (!TEST_true(multi_thread_rand_priv_bytes_succeeded))
957 return 0;
958
959 return 1;
960 }
961 #endif
962
963 /*
964 * Test that instantiation with RAND_seed() works as expected
965 *
966 * If no os entropy source is available then RAND_seed(buffer, bufsize)
967 * is expected to succeed if and only if the buffer length is at least
968 * rand_drbg_seedlen(master) bytes.
969 *
970 * If an os entropy source is available then RAND_seed(buffer, bufsize)
971 * is expected to succeed always.
972 */
973 static int test_rand_seed(void)
974 {
975 RAND_DRBG *master = NULL;
976 unsigned char rand_buf[256];
977 size_t rand_buflen;
978 size_t required_seed_buflen = 0;
979
980 if (!TEST_ptr(master = RAND_DRBG_get0_master())
981 || !TEST_true(disable_crngt(master)))
982 return 0;
983
984 #ifdef OPENSSL_RAND_SEED_NONE
985 required_seed_buflen = rand_drbg_seedlen(master);
986 #endif
987
988 memset(rand_buf, 0xCD, sizeof(rand_buf));
989
990 for ( rand_buflen = 256 ; rand_buflen > 0 ; --rand_buflen ) {
991 RAND_DRBG_uninstantiate(master);
992 RAND_seed(rand_buf, rand_buflen);
993
994 if (!TEST_int_eq(RAND_status(),
995 (rand_buflen >= required_seed_buflen)))
996 return 0;
997 }
998
999 return 1;
1000 }
1001
1002 /*
1003 * Test that adding additional data with RAND_add() works as expected
1004 * when the master DRBG is instantiated (and below its reseed limit).
1005 *
1006 * This should succeed regardless of whether an os entropy source is
1007 * available or not.
1008 */
1009 static int test_rand_add(void)
1010 {
1011 unsigned char rand_buf[256];
1012 size_t rand_buflen;
1013
1014 memset(rand_buf, 0xCD, sizeof(rand_buf));
1015
1016 /* make sure it's instantiated */
1017 RAND_seed(rand_buf, sizeof(rand_buf));
1018 if (!TEST_true(RAND_status()))
1019 return 0;
1020
1021 for ( rand_buflen = 256 ; rand_buflen > 0 ; --rand_buflen ) {
1022 RAND_add(rand_buf, rand_buflen, 0.0);
1023 if (!TEST_true(RAND_status()))
1024 return 0;
1025 }
1026
1027 return 1;
1028 }
1029
1030 static int test_rand_drbg_prediction_resistance(void)
1031 {
1032 RAND_DRBG *m = NULL, *i = NULL, *s = NULL;
1033 unsigned char buf1[51], buf2[sizeof(buf1)];
1034 int ret = 0, mreseed, ireseed, sreseed;
1035
1036 /* Initialise a three long DRBG chain */
1037 if (!TEST_ptr(m = RAND_DRBG_new(0, 0, NULL))
1038 || !TEST_true(disable_crngt(m))
1039 || !TEST_true(RAND_DRBG_instantiate(m, NULL, 0))
1040 || !TEST_ptr(i = RAND_DRBG_new(0, 0, m))
1041 || !TEST_true(RAND_DRBG_instantiate(i, NULL, 0))
1042 || !TEST_ptr(s = RAND_DRBG_new(0, 0, i))
1043 || !TEST_true(RAND_DRBG_instantiate(s, NULL, 0)))
1044 goto err;
1045
1046 /* During a normal reseed, only the slave DRBG should be reseed */
1047 mreseed = ++m->reseed_prop_counter;
1048 ireseed = ++i->reseed_prop_counter;
1049 sreseed = s->reseed_prop_counter;
1050 if (!TEST_true(RAND_DRBG_reseed(s, NULL, 0, 0))
1051 || !TEST_int_eq(m->reseed_prop_counter, mreseed)
1052 || !TEST_int_eq(i->reseed_prop_counter, ireseed)
1053 || !TEST_int_gt(s->reseed_prop_counter, sreseed))
1054 goto err;
1055
1056 /*
1057 * When prediction resistance is requested, the request should be
1058 * propagated to the master, so that the entire DRBG chain reseeds.
1059 */
1060 sreseed = s->reseed_prop_counter;
1061 if (!TEST_true(RAND_DRBG_reseed(s, NULL, 0, 1))
1062 || !TEST_int_gt(m->reseed_prop_counter, mreseed)
1063 || !TEST_int_gt(i->reseed_prop_counter, ireseed)
1064 || !TEST_int_gt(s->reseed_prop_counter, sreseed))
1065 goto err;
1066
1067 /* During a normal generate, only the slave DRBG should be reseed */
1068 mreseed = ++m->reseed_prop_counter;
1069 ireseed = ++i->reseed_prop_counter;
1070 sreseed = s->reseed_prop_counter;
1071 if (!TEST_true(RAND_DRBG_generate(s, buf1, sizeof(buf1), 0, NULL, 0))
1072 || !TEST_int_eq(m->reseed_prop_counter, mreseed)
1073 || !TEST_int_eq(i->reseed_prop_counter, ireseed)
1074 || !TEST_int_gt(s->reseed_prop_counter, sreseed))
1075 goto err;
1076
1077 /*
1078 * When a prediction resistant generate is requested, the request
1079 * should be propagated to the master, reseeding the entire DRBG chain.
1080 */
1081 sreseed = s->reseed_prop_counter;
1082 if (!TEST_true(RAND_DRBG_generate(s, buf2, sizeof(buf2), 1, NULL, 0))
1083 || !TEST_int_gt(m->reseed_prop_counter, mreseed)
1084 || !TEST_int_gt(i->reseed_prop_counter, ireseed)
1085 || !TEST_int_gt(s->reseed_prop_counter, sreseed)
1086 || !TEST_mem_ne(buf1, sizeof(buf1), buf2, sizeof(buf2)))
1087 goto err;
1088
1089 /* Verify that a normal reseed still only reseeds the slave DRBG */
1090 mreseed = ++m->reseed_prop_counter;
1091 ireseed = ++i->reseed_prop_counter;
1092 sreseed = s->reseed_prop_counter;
1093 if (!TEST_true(RAND_DRBG_reseed(s, NULL, 0, 0))
1094 || !TEST_int_eq(m->reseed_prop_counter, mreseed)
1095 || !TEST_int_eq(i->reseed_prop_counter, ireseed)
1096 || !TEST_int_gt(s->reseed_prop_counter, sreseed))
1097 goto err;
1098
1099 ret = 1;
1100 err:
1101 RAND_DRBG_free(s);
1102 RAND_DRBG_free(i);
1103 RAND_DRBG_free(m);
1104 return ret;
1105 }
1106
1107 static int test_multi_set(void)
1108 {
1109 int rv = 0;
1110 RAND_DRBG *drbg = NULL;
1111
1112 /* init drbg with default CTR initializer */
1113 if (!TEST_ptr(drbg = RAND_DRBG_new(0, 0, NULL))
1114 || !TEST_true(disable_crngt(drbg)))
1115 goto err;
1116 /* change it to use hmac */
1117 if (!TEST_true(RAND_DRBG_set(drbg, NID_sha1, RAND_DRBG_FLAG_HMAC)))
1118 goto err;
1119 /* use same type */
1120 if (!TEST_true(RAND_DRBG_set(drbg, NID_sha1, RAND_DRBG_FLAG_HMAC)))
1121 goto err;
1122 /* change it to use hash */
1123 if (!TEST_true(RAND_DRBG_set(drbg, NID_sha256, 0)))
1124 goto err;
1125 /* use same type */
1126 if (!TEST_true(RAND_DRBG_set(drbg, NID_sha256, 0)))
1127 goto err;
1128 /* change it to use ctr */
1129 if (!TEST_true(RAND_DRBG_set(drbg, NID_aes_192_ctr, 0)))
1130 goto err;
1131 /* use same type */
1132 if (!TEST_true(RAND_DRBG_set(drbg, NID_aes_192_ctr, 0)))
1133 goto err;
1134 if (!TEST_int_gt(RAND_DRBG_instantiate(drbg, NULL, 0), 0))
1135 goto err;
1136
1137 rv = 1;
1138 err:
1139 uninstantiate(drbg);
1140 RAND_DRBG_free(drbg);
1141 return rv;
1142 }
1143
1144 static int test_set_defaults(void)
1145 {
1146 RAND_DRBG *master = NULL, *public = NULL, *private = NULL;
1147
1148 /* Check the default type and flags for master, public and private */
1149 return TEST_ptr(master = RAND_DRBG_get0_master())
1150 && TEST_ptr(public = RAND_DRBG_get0_public())
1151 && TEST_ptr(private = RAND_DRBG_get0_private())
1152 && TEST_int_eq(master->type, RAND_DRBG_TYPE)
1153 && TEST_int_eq(master->flags,
1154 RAND_DRBG_FLAGS | RAND_DRBG_FLAG_MASTER)
1155 && TEST_int_eq(public->type, RAND_DRBG_TYPE)
1156 && TEST_int_eq(public->flags,
1157 RAND_DRBG_FLAGS | RAND_DRBG_FLAG_PUBLIC)
1158 && TEST_int_eq(private->type, RAND_DRBG_TYPE)
1159 && TEST_int_eq(private->flags,
1160 RAND_DRBG_FLAGS | RAND_DRBG_FLAG_PRIVATE)
1161
1162 /* change master DRBG and check again */
1163 && TEST_true(RAND_DRBG_set_defaults(NID_sha256,
1164 RAND_DRBG_FLAG_MASTER))
1165 && TEST_true(RAND_DRBG_uninstantiate(master))
1166 && TEST_int_eq(master->type, NID_sha256)
1167 && TEST_int_eq(master->flags, RAND_DRBG_FLAG_MASTER)
1168 && TEST_int_eq(public->type, RAND_DRBG_TYPE)
1169 && TEST_int_eq(public->flags,
1170 RAND_DRBG_FLAGS | RAND_DRBG_FLAG_PUBLIC)
1171 && TEST_int_eq(private->type, RAND_DRBG_TYPE)
1172 && TEST_int_eq(private->flags,
1173 RAND_DRBG_FLAGS | RAND_DRBG_FLAG_PRIVATE)
1174 /* change private DRBG and check again */
1175 && TEST_true(RAND_DRBG_set_defaults(NID_sha256,
1176 RAND_DRBG_FLAG_PRIVATE|RAND_DRBG_FLAG_HMAC))
1177 && TEST_true(RAND_DRBG_uninstantiate(private))
1178 && TEST_int_eq(master->type, NID_sha256)
1179 && TEST_int_eq(master->flags, RAND_DRBG_FLAG_MASTER)
1180 && TEST_int_eq(public->type, RAND_DRBG_TYPE)
1181 && TEST_int_eq(public->flags,
1182 RAND_DRBG_FLAGS | RAND_DRBG_FLAG_PUBLIC)
1183 && TEST_int_eq(private->type, NID_sha256)
1184 && TEST_int_eq(private->flags,
1185 RAND_DRBG_FLAG_PRIVATE | RAND_DRBG_FLAG_HMAC)
1186 /* change public DRBG and check again */
1187 && TEST_true(RAND_DRBG_set_defaults(NID_sha1,
1188 RAND_DRBG_FLAG_PUBLIC
1189 | RAND_DRBG_FLAG_HMAC))
1190 && TEST_true(RAND_DRBG_uninstantiate(public))
1191 && TEST_int_eq(master->type, NID_sha256)
1192 && TEST_int_eq(master->flags, RAND_DRBG_FLAG_MASTER)
1193 && TEST_int_eq(public->type, NID_sha1)
1194 && TEST_int_eq(public->flags,
1195 RAND_DRBG_FLAG_PUBLIC | RAND_DRBG_FLAG_HMAC)
1196 && TEST_int_eq(private->type, NID_sha256)
1197 && TEST_int_eq(private->flags,
1198 RAND_DRBG_FLAG_PRIVATE | RAND_DRBG_FLAG_HMAC)
1199 /* Change DRBG defaults and change public and check again */
1200 && TEST_true(RAND_DRBG_set_defaults(NID_sha256, 0))
1201 && TEST_true(RAND_DRBG_uninstantiate(public))
1202 && TEST_int_eq(public->type, NID_sha256)
1203 && TEST_int_eq(public->flags, RAND_DRBG_FLAG_PUBLIC)
1204
1205 /* FIPS mode doesn't support CTR DRBG without a derivation function */
1206 #ifndef FIPS_MODE
1207 /* Change DRBG defaults and change master and check again */
1208 && TEST_true(RAND_DRBG_set_defaults(NID_aes_256_ctr,
1209 RAND_DRBG_FLAG_CTR_NO_DF))
1210 && TEST_true(RAND_DRBG_uninstantiate(master))
1211 && TEST_int_eq(master->type, NID_aes_256_ctr)
1212 && TEST_int_eq(master->flags,
1213 RAND_DRBG_FLAG_MASTER|RAND_DRBG_FLAG_CTR_NO_DF)
1214 #endif
1215 /* Reset back to the standard defaults */
1216 && TEST_true(RAND_DRBG_set_defaults(RAND_DRBG_TYPE,
1217 RAND_DRBG_FLAGS
1218 | RAND_DRBG_FLAG_MASTER
1219 | RAND_DRBG_FLAG_PUBLIC
1220 | RAND_DRBG_FLAG_PRIVATE))
1221 && TEST_true(RAND_DRBG_uninstantiate(master))
1222 && TEST_true(RAND_DRBG_uninstantiate(public))
1223 && TEST_true(RAND_DRBG_uninstantiate(private));
1224 }
1225
1226 /*
1227 * A list of the FIPS DRGB types.
1228 * Because of the way HMAC DRGBs are implemented, both the NID and flags
1229 * are required.
1230 */
1231 static const struct s_drgb_types {
1232 int nid;
1233 int flags;
1234 } drgb_types[] = {
1235 { NID_aes_128_ctr, 0 },
1236 { NID_aes_192_ctr, 0 },
1237 { NID_aes_256_ctr, 0 },
1238 { NID_sha1, 0 },
1239 { NID_sha224, 0 },
1240 { NID_sha256, 0 },
1241 { NID_sha384, 0 },
1242 { NID_sha512, 0 },
1243 { NID_sha512_224, 0 },
1244 { NID_sha512_256, 0 },
1245 { NID_sha3_224, 0 },
1246 { NID_sha3_256, 0 },
1247 { NID_sha3_384, 0 },
1248 { NID_sha3_512, 0 },
1249 { NID_sha1, RAND_DRBG_FLAG_HMAC },
1250 { NID_sha224, RAND_DRBG_FLAG_HMAC },
1251 { NID_sha256, RAND_DRBG_FLAG_HMAC },
1252 { NID_sha384, RAND_DRBG_FLAG_HMAC },
1253 { NID_sha512, RAND_DRBG_FLAG_HMAC },
1254 { NID_sha512_224, RAND_DRBG_FLAG_HMAC },
1255 { NID_sha512_256, RAND_DRBG_FLAG_HMAC },
1256 { NID_sha3_224, RAND_DRBG_FLAG_HMAC },
1257 { NID_sha3_256, RAND_DRBG_FLAG_HMAC },
1258 { NID_sha3_384, RAND_DRBG_FLAG_HMAC },
1259 { NID_sha3_512, RAND_DRBG_FLAG_HMAC },
1260 };
1261
1262 /* Six cases for each covers seed sizes up to 32 bytes */
1263 static const size_t crngt_num_cases = 6;
1264
1265 static size_t crngt_case, crngt_idx;
1266
1267 static int crngt_entropy_cb(OPENSSL_CTX *ctx, RAND_POOL *pool,
1268 unsigned char *buf, unsigned char *md,
1269 unsigned int *md_size)
1270 {
1271 size_t i, z;
1272
1273 if (!TEST_int_lt(crngt_idx, crngt_num_cases))
1274 return 0;
1275 /* Generate a block of unique data unless this is the duplication point */
1276 z = crngt_idx++;
1277 if (z > 0 && crngt_case == z)
1278 z--;
1279 for (i = 0; i < CRNGT_BUFSIZ; i++)
1280 buf[i] = (unsigned char)(i + 'A' + z);
1281 return EVP_Digest(buf, CRNGT_BUFSIZ, md, md_size, EVP_sha256(), NULL);
1282 }
1283
1284 static int test_crngt(int n)
1285 {
1286 const struct s_drgb_types *dt = drgb_types + n / crngt_num_cases;
1287 RAND_DRBG *drbg = NULL;
1288 unsigned char buff[100];
1289 size_t ent;
1290 int res = 0;
1291 int expect;
1292 OPENSSL_CTX *ctx = OPENSSL_CTX_new();
1293
1294 if (!TEST_ptr(ctx))
1295 return 0;
1296 if (!TEST_ptr(drbg = RAND_DRBG_new_ex(ctx, dt->nid, dt->flags, NULL)))
1297 goto err;
1298 ent = (drbg->min_entropylen + CRNGT_BUFSIZ - 1) / CRNGT_BUFSIZ;
1299 crngt_case = n % crngt_num_cases;
1300 crngt_idx = 0;
1301 crngt_get_entropy = &crngt_entropy_cb;
1302 #ifndef FIPS_MODE
1303 if (!TEST_true(RAND_DRBG_set_callbacks(drbg, &rand_crngt_get_entropy,
1304 &rand_crngt_cleanup_entropy,
1305 &rand_drbg_get_nonce,
1306 &rand_drbg_cleanup_nonce)))
1307 goto err;
1308 #endif
1309 expect = crngt_case == 0 || crngt_case > ent;
1310 if (!TEST_int_eq(RAND_DRBG_instantiate(drbg, NULL, 0), expect))
1311 goto err;
1312 if (!expect)
1313 goto fin;
1314 if (!TEST_true(RAND_DRBG_generate(drbg, buff, sizeof(buff), 0, NULL, 0)))
1315 goto err;
1316
1317 expect = crngt_case == 0 || crngt_case > 2 * ent;
1318 if (!TEST_int_eq(RAND_DRBG_reseed(drbg, NULL, 0, 0), expect))
1319 goto err;
1320 if (!expect)
1321 goto fin;
1322 if (!TEST_true(RAND_DRBG_generate(drbg, buff, sizeof(buff), 0, NULL, 0)))
1323 goto err;
1324
1325 fin:
1326 res = 1;
1327 err:
1328 if (!res)
1329 TEST_note("DRBG %zd case %zd block %zd", n / crngt_num_cases,
1330 crngt_case, crngt_idx);
1331 uninstantiate(drbg);
1332 RAND_DRBG_free(drbg);
1333 crngt_get_entropy = &rand_crngt_get_entropy_cb;
1334 OPENSSL_CTX_free(ctx);
1335 return res;
1336 }
1337
1338 int setup_tests(void)
1339 {
1340 app_data_index = RAND_DRBG_get_ex_new_index(0L, NULL, NULL, NULL, NULL);
1341
1342 ADD_ALL_TESTS(test_kats, OSSL_NELEM(drbg_test));
1343 ADD_ALL_TESTS(test_error_checks, OSSL_NELEM(drbg_test));
1344 ADD_TEST(test_rand_drbg_reseed);
1345 ADD_TEST(test_rand_seed);
1346 ADD_TEST(test_rand_add);
1347 ADD_TEST(test_rand_drbg_prediction_resistance);
1348 ADD_TEST(test_multi_set);
1349 ADD_TEST(test_set_defaults);
1350 #if defined(OPENSSL_THREADS)
1351 ADD_TEST(test_multi_thread);
1352 #endif
1353 ADD_ALL_TESTS(test_crngt, crngt_num_cases * OSSL_NELEM(drgb_types));
1354 return 1;
1355 }