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