Propagate the request for prediction resistance to the get entropy call
[openssl.git] / test / drbgtest.c
1 /*
2  * Copyright 2011-2018 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (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
20 #include "testutil.h"
21 #include "drbgtest.h"
22
23 typedef struct drbg_selftest_data_st {
24     int post;
25     int nid;
26     unsigned int flags;
27
28     /* KAT data for no PR */
29     const unsigned char *entropy;
30     size_t entropylen;
31     const unsigned char *nonce;
32     size_t noncelen;
33     const unsigned char *pers;
34     size_t perslen;
35     const unsigned char *adin;
36     size_t adinlen;
37     const unsigned char *entropyreseed;
38     size_t entropyreseedlen;
39     const unsigned char *adinreseed;
40     size_t adinreseedlen;
41     const unsigned char *adin2;
42     size_t adin2len;
43     const unsigned char *expected;
44     size_t exlen;
45     const unsigned char *kat2;
46     size_t kat2len;
47
48     /* KAT data for PR */
49     const unsigned char *entropy_pr;
50     size_t entropylen_pr;
51     const unsigned char *nonce_pr;
52     size_t noncelen_pr;
53     const unsigned char *pers_pr;
54     size_t perslen_pr;
55     const unsigned char *adin_pr;
56     size_t adinlen_pr;
57     const unsigned char *entropypr_pr;
58     size_t entropyprlen_pr;
59     const unsigned char *ading_pr;
60     size_t adinglen_pr;
61     const unsigned char *entropyg_pr;
62     size_t entropyglen_pr;
63     const unsigned char *kat_pr;
64     size_t katlen_pr;
65     const unsigned char *kat2_pr;
66     size_t kat2len_pr;
67 } DRBG_SELFTEST_DATA;
68
69 #define make_drbg_test_data(nid, flag, pr, post) {\
70     post, nid, flag, \
71     pr##_entropyinput, sizeof(pr##_entropyinput), \
72     pr##_nonce, sizeof(pr##_nonce), \
73     pr##_personalizationstring, sizeof(pr##_personalizationstring), \
74     pr##_additionalinput, sizeof(pr##_additionalinput), \
75     pr##_entropyinputreseed, sizeof(pr##_entropyinputreseed), \
76     pr##_additionalinputreseed, sizeof(pr##_additionalinputreseed), \
77     pr##_additionalinput2, sizeof(pr##_additionalinput2), \
78     pr##_int_returnedbits, sizeof(pr##_int_returnedbits), \
79     pr##_returnedbits, sizeof(pr##_returnedbits), \
80     pr##_pr_entropyinput, sizeof(pr##_pr_entropyinput), \
81     pr##_pr_nonce, sizeof(pr##_pr_nonce), \
82     pr##_pr_personalizationstring, sizeof(pr##_pr_personalizationstring), \
83     pr##_pr_additionalinput, sizeof(pr##_pr_additionalinput), \
84     pr##_pr_entropyinputpr, sizeof(pr##_pr_entropyinputpr), \
85     pr##_pr_additionalinput2, sizeof(pr##_pr_additionalinput2), \
86     pr##_pr_entropyinputpr2, sizeof(pr##_pr_entropyinputpr2), \
87     pr##_pr_int_returnedbits, sizeof(pr##_pr_int_returnedbits), \
88     pr##_pr_returnedbits, sizeof(pr##_pr_returnedbits) \
89     }
90
91 #define make_drbg_test_data_use_df(nid, pr, p) \
92     make_drbg_test_data(nid, 0, pr, p)
93
94 #define make_drbg_test_data_no_df(nid, pr, p)                      \
95     make_drbg_test_data(nid, RAND_DRBG_FLAG_CTR_NO_DF, pr, p)
96
97 static DRBG_SELFTEST_DATA drbg_test[] = {
98     make_drbg_test_data_no_df (NID_aes_128_ctr, aes_128_no_df,  0),
99     make_drbg_test_data_no_df (NID_aes_192_ctr, aes_192_no_df,  0),
100     make_drbg_test_data_no_df (NID_aes_256_ctr, aes_256_no_df,  1),
101     make_drbg_test_data_use_df(NID_aes_128_ctr, aes_128_use_df, 0),
102     make_drbg_test_data_use_df(NID_aes_192_ctr, aes_192_use_df, 0),
103     make_drbg_test_data_use_df(NID_aes_256_ctr, aes_256_use_df, 1),
104 };
105
106 static int app_data_index;
107
108 /*
109  * Test context data, attached as EXDATA to the RAND_DRBG
110  */
111 typedef struct test_ctx_st {
112     const unsigned char *entropy;
113     size_t entropylen;
114     int entropycnt;
115     const unsigned char *nonce;
116     size_t noncelen;
117     int noncecnt;
118 } TEST_CTX;
119
120 static size_t kat_entropy(RAND_DRBG *drbg, unsigned char **pout,
121                           int entropy, size_t min_len, size_t max_len,
122                           int prediction_resistance)
123 {
124     TEST_CTX *t = (TEST_CTX *)RAND_DRBG_get_ex_data(drbg, app_data_index);
125
126     t->entropycnt++;
127     *pout = (unsigned char *)t->entropy;
128     return t->entropylen;
129 }
130
131 static size_t kat_nonce(RAND_DRBG *drbg, unsigned char **pout,
132                         int entropy, size_t min_len, size_t max_len)
133 {
134     TEST_CTX *t = (TEST_CTX *)RAND_DRBG_get_ex_data(drbg, app_data_index);
135
136     t->noncecnt++;
137     *pout = (unsigned char *)t->nonce;
138     return t->noncelen;
139 }
140
141 static int uninstantiate(RAND_DRBG *drbg)
142 {
143     int ret = drbg == NULL ? 1 : RAND_DRBG_uninstantiate(drbg);
144
145     ERR_clear_error();
146     return ret;
147 }
148
149 /*
150  * Do a single KAT test.  Return 0 on failure.
151  */
152 static int single_kat(DRBG_SELFTEST_DATA *td)
153 {
154     RAND_DRBG *drbg = NULL;
155     TEST_CTX t;
156     int failures = 0;
157     unsigned char buff[1024];
158
159     /*
160      * Test without PR: Instantiate DRBG with test entropy, nonce and
161      * personalisation string.
162      */
163     if (!TEST_ptr(drbg = RAND_DRBG_new(td->nid, td->flags, NULL)))
164         return 0;
165     if (!TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
166                                            kat_nonce, NULL))) {
167         failures++;
168         goto err;
169     }
170     memset(&t, 0, sizeof(t));
171     t.entropy = td->entropy;
172     t.entropylen = td->entropylen;
173     t.nonce = td->nonce;
174     t.noncelen = td->noncelen;
175     RAND_DRBG_set_ex_data(drbg, app_data_index, &t);
176
177     if (!TEST_true(RAND_DRBG_instantiate(drbg, td->pers, td->perslen))
178             || !TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
179                                              td->adin, td->adinlen))
180             || !TEST_mem_eq(td->expected, td->exlen, buff, td->exlen))
181         failures++;
182
183     /* Reseed DRBG with test entropy and additional input */
184     t.entropy = td->entropyreseed;
185     t.entropylen = td->entropyreseedlen;
186     if (!TEST_true(RAND_DRBG_reseed(drbg, td->adinreseed, td->adinreseedlen, 0)
187             || !TEST_true(RAND_DRBG_generate(drbg, buff, td->kat2len, 0,
188                                              td->adin2, td->adin2len))
189             || !TEST_mem_eq(td->kat2, td->kat2len, buff, td->kat2len)))
190         failures++;
191     uninstantiate(drbg);
192
193     /*
194      * Now test with PR: Instantiate DRBG with test entropy, nonce and
195      * personalisation string.
196      */
197     if (!TEST_true(RAND_DRBG_set(drbg, td->nid, td->flags))
198             || !TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
199                                                   kat_nonce, NULL)))
200         failures++;
201     RAND_DRBG_set_ex_data(drbg, app_data_index, &t);
202     t.entropy = td->entropy_pr;
203     t.entropylen = td->entropylen_pr;
204     t.nonce = td->nonce_pr;
205     t.noncelen = td->noncelen_pr;
206     t.entropycnt = 0;
207     t.noncecnt = 0;
208     if (!TEST_true(RAND_DRBG_instantiate(drbg, td->pers_pr, td->perslen_pr)))
209         failures++;
210
211     /*
212      * Now generate with PR: we need to supply entropy as this will
213      * perform a reseed operation.
214      */
215     t.entropy = td->entropypr_pr;
216     t.entropylen = td->entropyprlen_pr;
217     if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->katlen_pr, 1,
218                                       td->adin_pr, td->adinlen_pr))
219             || !TEST_mem_eq(td->kat_pr, td->katlen_pr, buff, td->katlen_pr))
220         failures++;
221
222     /*
223      * Now generate again with PR: supply new entropy again.
224      */
225     t.entropy = td->entropyg_pr;
226     t.entropylen = td->entropyglen_pr;
227
228     if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->kat2len_pr, 1,
229                                       td->ading_pr, td->adinglen_pr))
230                 || !TEST_mem_eq(td->kat2_pr, td->kat2len_pr,
231                                 buff, td->kat2len_pr))
232         failures++;
233
234 err:
235     uninstantiate(drbg);
236     RAND_DRBG_free(drbg);
237     return failures == 0;
238 }
239
240 /*
241  * Initialise a DRBG based on selftest data
242  */
243 static int init(RAND_DRBG *drbg, DRBG_SELFTEST_DATA *td, TEST_CTX *t)
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         return 0;
249     RAND_DRBG_set_ex_data(drbg, app_data_index, t);
250     t->entropy = td->entropy;
251     t->entropylen = td->entropylen;
252     t->nonce = td->nonce;
253     t->noncelen = td->noncelen;
254     t->entropycnt = 0;
255     t->noncecnt = 0;
256     return 1;
257 }
258
259 /*
260  * Initialise and instantiate DRBG based on selftest data
261  */
262 static int instantiate(RAND_DRBG *drbg, DRBG_SELFTEST_DATA *td,
263                        TEST_CTX *t)
264 {
265     if (!TEST_true(init(drbg, td, t))
266             || !TEST_true(RAND_DRBG_instantiate(drbg, td->pers, td->perslen)))
267         return 0;
268     return 1;
269 }
270
271 /*
272  * Perform extensive error checking as required by SP800-90.
273  * Induce several failure modes and check an error condition is set.
274  */
275 static int error_check(DRBG_SELFTEST_DATA *td)
276 {
277     static char zero[sizeof(RAND_DRBG)];
278     RAND_DRBG *drbg = NULL;
279     TEST_CTX t;
280     unsigned char buff[1024];
281     unsigned int generate_counter_tmp;
282     int ret = 0;
283
284     if (!TEST_ptr(drbg = RAND_DRBG_new(0, 0, NULL)))
285         goto err;
286
287     /*
288      * Personalisation string tests
289      */
290
291     /* Test detection of too large personlisation string */
292     if (!init(drbg, td, &t)
293             || RAND_DRBG_instantiate(drbg, td->pers, drbg->max_perslen + 1) > 0)
294         goto err;
295
296     /*
297      * Entropy source tests
298      */
299
300     /* Test entropy source failure detecion: i.e. returns no data */
301     t.entropylen = 0;
302     if (TEST_int_le(RAND_DRBG_instantiate(drbg, td->pers, td->perslen), 0))
303         goto err;
304
305     /* Try to generate output from uninstantiated DRBG */
306     if (!TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
307                                        td->adin, td->adinlen))
308             || !uninstantiate(drbg))
309         goto err;
310
311     /* Test insufficient entropy */
312     t.entropylen = drbg->min_entropylen - 1;
313     if (!init(drbg, td, &t)
314             || RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0
315             || !uninstantiate(drbg))
316         goto err;
317
318     /* Test too much entropy */
319     t.entropylen = drbg->max_entropylen + 1;
320     if (!init(drbg, td, &t)
321             || RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0
322             || !uninstantiate(drbg))
323         goto err;
324
325     /*
326      * Nonce tests
327      */
328
329     /* Test too small nonce */
330     if (drbg->min_noncelen) {
331         t.noncelen = drbg->min_noncelen - 1;
332         if (!init(drbg, td, &t)
333                 || RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0
334                 || !uninstantiate(drbg))
335             goto err;
336     }
337
338     /* Test too large nonce */
339     if (drbg->max_noncelen) {
340         t.noncelen = drbg->max_noncelen + 1;
341         if (!init(drbg, td, &t)
342                 || RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0
343                 || !uninstantiate(drbg))
344             goto err;
345     }
346
347     /* Instantiate with valid data, Check generation is now OK */
348     if (!instantiate(drbg, td, &t)
349             || !TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
350                                              td->adin, td->adinlen)))
351         goto err;
352
353     /* Request too much data for one request */
354     if (!TEST_false(RAND_DRBG_generate(drbg, buff, drbg->max_request + 1, 0,
355                                        td->adin, td->adinlen)))
356         goto err;
357
358     /* Try too large additional input */
359     if (!TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
360                                        td->adin, drbg->max_adinlen + 1)))
361         goto err;
362
363     /*
364      * Check prediction resistance request fails if entropy source
365      * failure.
366      */
367     t.entropylen = 0;
368     if (TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 1,
369                                       td->adin, td->adinlen))
370             || !uninstantiate(drbg))
371         goto err;
372
373     /* Instantiate again with valid data */
374     if (!instantiate(drbg, td, &t))
375         goto err;
376     generate_counter_tmp = drbg->generate_counter;
377     drbg->generate_counter = drbg->reseed_interval;
378
379     /* Generate output and check entropy has been requested for reseed */
380     t.entropycnt = 0;
381     if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
382                                       td->adin, td->adinlen))
383             || !TEST_int_eq(t.entropycnt, 1)
384             || !TEST_int_eq(drbg->generate_counter, generate_counter_tmp + 1)
385             || !uninstantiate(drbg))
386         goto err;
387
388     /*
389      * Check prediction resistance request fails if entropy source
390      * failure.
391      */
392     t.entropylen = 0;
393     if (!TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 1,
394                                        td->adin, td->adinlen))
395             || !uninstantiate(drbg))
396         goto err;
397
398     /* Test reseed counter works */
399     if (!instantiate(drbg, td, &t))
400         goto err;
401     generate_counter_tmp = drbg->generate_counter;
402     drbg->generate_counter = drbg->reseed_interval;
403
404     /* Generate output and check entropy has been requested for reseed */
405     t.entropycnt = 0;
406     if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
407                                       td->adin, td->adinlen))
408             || !TEST_int_eq(t.entropycnt, 1)
409             || !TEST_int_eq(drbg->generate_counter, generate_counter_tmp + 1)
410             || !uninstantiate(drbg))
411         goto err;
412
413     /*
414      * Explicit reseed tests
415      */
416
417     /* Test explicit reseed with too large additional input */
418     if (!init(drbg, td, &t)
419             || RAND_DRBG_reseed(drbg, td->adin, drbg->max_adinlen + 1, 0) > 0)
420         goto err;
421
422     /* Test explicit reseed with entropy source failure */
423     t.entropylen = 0;
424     if (!TEST_int_le(RAND_DRBG_reseed(drbg, td->adin, td->adinlen, 0), 0)
425             || !uninstantiate(drbg))
426         goto err;
427
428     /* Test explicit reseed with too much entropy */
429     if (!init(drbg, td, &t))
430         goto err;
431     t.entropylen = drbg->max_entropylen + 1;
432     if (!TEST_int_le(RAND_DRBG_reseed(drbg, td->adin, td->adinlen, 0), 0)
433             || !uninstantiate(drbg))
434         goto err;
435
436     /* Test explicit reseed with too little entropy */
437     if (!init(drbg, td, &t))
438         goto err;
439     t.entropylen = drbg->min_entropylen - 1;
440     if (!TEST_int_le(RAND_DRBG_reseed(drbg, td->adin, td->adinlen, 0), 0)
441             || !uninstantiate(drbg))
442         goto err;
443
444     /* Standard says we have to check uninstantiate really zeroes */
445     if (!TEST_mem_eq(zero, sizeof(drbg->data), &drbg->data, sizeof(drbg->data)))
446         goto err;
447
448     ret = 1;
449
450 err:
451     uninstantiate(drbg);
452     RAND_DRBG_free(drbg);
453     return ret;
454 }
455
456 static int test_kats(int i)
457 {
458     DRBG_SELFTEST_DATA *td = &drbg_test[i];
459     int rv = 0;
460
461     if (!single_kat(td))
462         goto err;
463     rv = 1;
464
465 err:
466     return rv;
467 }
468
469 static int test_error_checks(int i)
470 {
471     DRBG_SELFTEST_DATA *td = &drbg_test[i];
472     int rv = 0;
473
474     if (error_check(td))
475         goto err;
476     rv = 1;
477
478 err:
479     return rv;
480 }
481
482 /*
483  * Hook context data, attached as EXDATA to the RAND_DRBG
484  */
485 typedef struct hook_ctx_st {
486     RAND_DRBG *drbg;
487     /*
488      * Currently, all DRBGs use the same get_entropy() callback.
489      * The tests however, don't assume this and store
490      * the original callback for every DRBG separately.
491      */
492     RAND_DRBG_get_entropy_fn get_entropy;
493     /* forces a failure of the get_entropy() call if nonzero */
494     int fail;
495     /* counts successful reseeds */
496     int reseed_count;
497 } HOOK_CTX;
498
499 static HOOK_CTX master_ctx, public_ctx, private_ctx;
500
501 static HOOK_CTX *get_hook_ctx(RAND_DRBG *drbg)
502 {
503     return (HOOK_CTX *)RAND_DRBG_get_ex_data(drbg, app_data_index);
504 }
505
506 /* Intercepts and counts calls to the get_entropy() callback */
507 static size_t get_entropy_hook(RAND_DRBG *drbg, unsigned char **pout,
508                               int entropy, size_t min_len, size_t max_len,
509                               int prediction_resistance)
510 {
511     size_t ret;
512     HOOK_CTX *ctx = get_hook_ctx(drbg);
513
514     if (ctx->fail != 0)
515         return 0;
516
517     ret = ctx->get_entropy(drbg, pout, entropy, min_len, max_len,
518                            prediction_resistance);
519
520     if (ret != 0)
521         ctx->reseed_count++;
522     return ret;
523 }
524
525 /* Installs a hook for the get_entropy() callback of the given drbg */
526 static void hook_drbg(RAND_DRBG *drbg, HOOK_CTX *ctx)
527 {
528     memset(ctx, 0, sizeof(*ctx));
529     ctx->drbg = drbg;
530     ctx->get_entropy = drbg->get_entropy;
531     drbg->get_entropy = get_entropy_hook;
532     RAND_DRBG_set_ex_data(drbg, app_data_index, ctx);
533 }
534
535 /* Installs the hook for the get_entropy() callback of the given drbg */
536 static void unhook_drbg(RAND_DRBG *drbg)
537 {
538     HOOK_CTX *ctx = get_hook_ctx(drbg);
539
540     drbg->get_entropy = ctx->get_entropy;
541     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DRBG, drbg, &drbg->ex_data);
542 }
543
544 /* Resets the given hook context */
545 static void reset_hook_ctx(HOOK_CTX *ctx)
546 {
547     ctx->fail = 0;
548     ctx->reseed_count = 0;
549 }
550
551 /* Resets all drbg hook contexts */
552 static void reset_drbg_hook_ctx()
553 {
554     reset_hook_ctx(&master_ctx);
555     reset_hook_ctx(&public_ctx);
556     reset_hook_ctx(&private_ctx);
557 }
558
559 /*
560  * Generates random output using RAND_bytes() and RAND_priv_bytes()
561  * and checks whether the three shared DRBGs were reseeded as
562  * expected.
563  *
564  * |expect_success|: expected outcome (as reported by RAND_status())
565  * |master|, |public|, |private|: pointers to the three shared DRBGs
566  * |expect_xxx_reseed| =
567  *       1:  it is expected that the specified DRBG is reseeded
568  *       0:  it is expected that the specified DRBG is not reseeded
569  *      -1:  don't check whether the specified DRBG was reseeded or not
570  */
571 static int test_drbg_reseed(int expect_success,
572                             RAND_DRBG *master,
573                             RAND_DRBG *public,
574                             RAND_DRBG *private,
575                             int expect_master_reseed,
576                             int expect_public_reseed,
577                             int expect_private_reseed
578                            )
579 {
580     unsigned char buf[32];
581     time_t before_reseed, after_reseed;
582     int expected_state = (expect_success ? DRBG_READY : DRBG_ERROR);
583
584     /*
585      * step 1: check preconditions
586      */
587
588     /* Test whether seed propagation is enabled */
589     if (!TEST_int_ne(master->reseed_counter, 0)
590         || !TEST_int_ne(public->reseed_counter, 0)
591         || !TEST_int_ne(private->reseed_counter, 0))
592         return 0;
593
594     /* Check whether the master DRBG's reseed counter is the largest one */
595     if (!TEST_int_le(public->reseed_counter, master->reseed_counter)
596         || !TEST_int_le(private->reseed_counter, master->reseed_counter))
597         return 0;
598
599     /*
600      * step 2: generate random output
601      */
602
603     /* Generate random output from the public and private DRBG */
604     before_reseed = expect_master_reseed == 1 ? time(NULL) : 0;
605     if (!TEST_int_eq(RAND_bytes(buf, sizeof(buf)), expect_success)
606         || !TEST_int_eq(RAND_priv_bytes(buf, sizeof(buf)), expect_success))
607         return 0;
608     after_reseed = time(NULL);
609
610
611     /*
612      * step 3: check postconditions
613      */
614
615     /* Test whether reseeding succeeded as expected */
616     if (!TEST_int_eq(master->state, expected_state)
617         || !TEST_int_eq(public->state, expected_state)
618         || !TEST_int_eq(private->state, expected_state))
619         return 0;
620
621     if (expect_master_reseed >= 0) {
622         /* Test whether master DRBG was reseeded as expected */
623         if (!TEST_int_eq(master_ctx.reseed_count, expect_master_reseed))
624             return 0;
625     }
626
627     if (expect_public_reseed >= 0) {
628         /* Test whether public DRBG was reseeded as expected */
629         if (!TEST_int_eq(public_ctx.reseed_count, expect_public_reseed))
630             return 0;
631     }
632
633     if (expect_private_reseed >= 0) {
634         /* Test whether public DRBG was reseeded as expected */
635         if (!TEST_int_eq(private_ctx.reseed_count, expect_private_reseed))
636             return 0;
637     }
638
639     if (expect_success == 1) {
640         /* Test whether all three reseed counters are synchronized */
641         if (!TEST_int_eq(public->reseed_counter, master->reseed_counter)
642             || !TEST_int_eq(private->reseed_counter, master->reseed_counter))
643             return 0;
644
645         /* Test whether reseed time of master DRBG is set correctly */
646         if (!TEST_time_t_le(before_reseed, master->reseed_time)
647             || !TEST_time_t_le(master->reseed_time, after_reseed))
648             return 0;
649
650         /* Test whether reseed times of child DRBGs are synchronized with master */
651         if (!TEST_time_t_ge(public->reseed_time, master->reseed_time)
652             || !TEST_time_t_ge(private->reseed_time, master->reseed_time))
653             return 0;
654     } else {
655         ERR_clear_error();
656     }
657
658     return 1;
659 }
660
661 /*
662  * Test whether the default rand_method (RAND_OpenSSL()) is
663  * setup correctly, in particular whether reseeding  works
664  * as designed.
665  */
666 static int test_rand_reseed(void)
667 {
668     RAND_DRBG *master, *public, *private;
669     unsigned char rand_add_buf[256];
670     int rv=0;
671
672     /* Check whether RAND_OpenSSL() is the default method */
673     if (!TEST_ptr_eq(RAND_get_rand_method(), RAND_OpenSSL()))
674         return 0;
675
676     /* All three DRBGs should be non-null */
677     if (!TEST_ptr(master = RAND_DRBG_get0_master())
678         || !TEST_ptr(public = RAND_DRBG_get0_public())
679         || !TEST_ptr(private = RAND_DRBG_get0_private()))
680         return 0;
681
682     /* There should be three distinct DRBGs, two of them chained to master */
683     if (!TEST_ptr_ne(public, private)
684         || !TEST_ptr_ne(public, master)
685         || !TEST_ptr_ne(private, master)
686         || !TEST_ptr_eq(public->parent, master)
687         || !TEST_ptr_eq(private->parent, master))
688         return 0;
689
690     /* Install hooks for the following tests */
691     hook_drbg(master,  &master_ctx);
692     hook_drbg(public,  &public_ctx);
693     hook_drbg(private, &private_ctx);
694
695     /*
696      * Test initial state of shared DRBs
697      */
698     if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 0, 0)))
699         goto error;
700     reset_drbg_hook_ctx();
701
702     /*
703      * Test whether the public and private DRBG are both reseeded when their
704      * reseed counters differ from the master's reseed counter.
705      */
706     master->reseed_counter++;
707     if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 1, 1)))
708         goto error;
709     reset_drbg_hook_ctx();
710
711     /*
712      * Test whether the public DRBG is reseeded when its reseed counter differs
713      * from the master's reseed counter.
714      */
715     master->reseed_counter++;
716     private->reseed_counter++;
717     if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 1, 0)))
718         goto error;
719     reset_drbg_hook_ctx();
720
721     /*
722      * Test whether the private DRBG is reseeded when its reseed counter differs
723      * from the master's reseed counter.
724      */
725     master->reseed_counter++;
726     public->reseed_counter++;
727     if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 0, 1)))
728         goto error;
729     reset_drbg_hook_ctx();
730
731
732     /* fill 'randomness' buffer with some arbitrary data */
733     memset(rand_add_buf, 'r', sizeof(rand_add_buf));
734
735     /*
736      * Test whether all three DRBGs are reseeded by RAND_add()
737      */
738     RAND_add(rand_add_buf, sizeof(rand_add_buf), sizeof(rand_add_buf));
739     if (!TEST_true(test_drbg_reseed(1, master, public, private, 1, 1, 1)))
740         goto error;
741     reset_drbg_hook_ctx();
742
743
744     /*
745      * Test whether none of the DRBGs is reseed if the master fails to reseed
746      */
747     master_ctx.fail = 1;
748     master->reseed_counter++;
749     RAND_add(rand_add_buf, sizeof(rand_add_buf), sizeof(rand_add_buf));
750     if (!TEST_true(test_drbg_reseed(0, master, public, private, 0, 0, 0)))
751         goto error;
752     reset_drbg_hook_ctx();
753
754     rv = 1;
755
756 error:
757     /* Remove hooks  */
758     unhook_drbg(master);
759     unhook_drbg(public);
760     unhook_drbg(private);
761
762     return rv;
763 }
764
765
766 int setup_tests(void)
767 {
768     app_data_index = RAND_DRBG_get_ex_new_index(0L, NULL, NULL, NULL, NULL);
769
770     ADD_ALL_TESTS(test_kats, OSSL_NELEM(drbg_test));
771     ADD_ALL_TESTS(test_error_checks, OSSL_NELEM(drbg_test));
772     ADD_TEST(test_rand_reseed);
773     return 1;
774 }