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