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