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