Invoke tear_down when exiting test_encode_tls_sct() prematurely
[openssl.git] / test / drbgtest.c
1 /*
2  * Copyright 2011-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 /*
11  * RAND_DRBG_set is deprecated for public use, but still ok for
12  * internal use.
13  */
14 #include "internal/deprecated.h"
15
16 #include <string.h>
17 #include "internal/nelem.h"
18 #include <openssl/crypto.h>
19 #include <openssl/err.h>
20 #include <openssl/rand.h>
21 #include <openssl/obj_mac.h>
22 #include <openssl/evp.h>
23 #include <openssl/aes.h>
24 #include "../crypto/rand/rand_local.h"
25 #include "../include/crypto/rand.h"
26 #include "../providers/implementations/rands/drbg_local.h"
27 #include "../crypto/evp/evp_local.h"
28
29 #if defined(_WIN32)
30 # include <windows.h>
31 #endif
32
33
34 #if defined(OPENSSL_SYS_UNIX)
35 # include <sys/types.h>
36 # include <sys/wait.h>
37 # include <unistd.h>
38 #endif
39
40 #include "testutil.h"
41 #include "drbgtest.h"
42
43 /*
44  * DRBG generate wrappers
45  */
46 static int gen_bytes(EVP_RAND_CTX *drbg, unsigned char *buf, int num)
47 {
48     const RAND_METHOD *meth = RAND_get_rand_method();
49
50     if (meth != NULL && meth != RAND_OpenSSL()) {
51         if (meth->bytes != NULL)
52             return meth->bytes(buf, num);
53         return -1;
54     }
55
56     if (drbg != NULL)
57         return EVP_RAND_generate(drbg, buf, num, 0, 0, NULL, 0);
58     return 0;
59 }
60
61 static int rand_bytes(unsigned char *buf, int num)
62 {
63     return gen_bytes(RAND_get0_public(NULL), buf, num);
64 }
65
66 static int rand_priv_bytes(unsigned char *buf, int num)
67 {
68     return gen_bytes(RAND_get0_private(NULL), buf, num);
69 }
70
71 /*
72  * DRBG query functions
73  */
74 static int state(EVP_RAND_CTX *drbg)
75 {
76     return EVP_RAND_state(drbg);
77 }
78
79 static unsigned int query_rand_uint(EVP_RAND_CTX *drbg, const char *name)
80 {
81     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
82     unsigned int n;
83
84     *params = OSSL_PARAM_construct_uint(name, &n);
85     if (EVP_RAND_get_ctx_params(drbg, params))
86         return n;
87     return 0;
88 }
89
90 #define DRBG_UINT(name)                                 \
91     static unsigned int name(EVP_RAND_CTX *drbg)        \
92     {                                                   \
93         return query_rand_uint(drbg, #name);            \
94     }
95 DRBG_UINT(reseed_counter)
96
97 static PROV_DRBG *prov_rand(EVP_RAND_CTX *drbg)
98 {
99     return (PROV_DRBG *)drbg->data;
100 }
101
102 static void set_reseed_counter(EVP_RAND_CTX *drbg, unsigned int n)
103 {
104     PROV_DRBG *p = prov_rand(drbg);
105
106     p->reseed_counter = n;
107 }
108
109 static void inc_reseed_counter(EVP_RAND_CTX *drbg)
110 {
111     set_reseed_counter(drbg, reseed_counter(drbg) + 1);
112 }
113
114 static time_t reseed_time(EVP_RAND_CTX *drbg)
115 {
116     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
117     time_t t;
118
119     *params = OSSL_PARAM_construct_time_t(OSSL_DRBG_PARAM_RESEED_TIME, &t);
120     if (EVP_RAND_get_ctx_params(drbg, params))
121         return t;
122     return 0;
123 }
124
125 /*
126  * When building the FIPS module, it isn't possible to disable the continuous
127  * RNG tests.  Tests that require this are skipped.
128  */
129 static int crngt_skip(void)
130 {
131 #ifdef FIPS_MODULE
132     return 1;
133 #else
134     return 0;
135 #endif
136 }
137
138  /*
139  * Disable CRNG testing if it is enabled.
140  * This stub remains to indicate the calling locations where it is necessary.
141  * Once the RNG infrastructure is able to disable these tests, it should be
142  * reconstituted.
143  */
144 static int disable_crngt(EVP_RAND_CTX *drbg)
145 {
146     return 1;
147 }
148
149 /*
150  * Generates random output using rand_bytes() and rand_priv_bytes()
151  * and checks whether the three shared DRBGs were reseeded as
152  * expected.
153  *
154  * |expect_success|: expected outcome (as reported by RAND_status())
155  * |primary|, |public|, |private|: pointers to the three shared DRBGs
156  * |expect_xxx_reseed| =
157  *       1:  it is expected that the specified DRBG is reseeded
158  *       0:  it is expected that the specified DRBG is not reseeded
159  *      -1:  don't check whether the specified DRBG was reseeded or not
160  * |reseed_time|: if nonzero, used instead of time(NULL) to set the
161  *                |before_reseed| time.
162  */
163 static int test_drbg_reseed(int expect_success,
164                             EVP_RAND_CTX *primary,
165                             EVP_RAND_CTX *public,
166                             EVP_RAND_CTX *private,
167                             int expect_primary_reseed,
168                             int expect_public_reseed,
169                             int expect_private_reseed,
170                             time_t reseed_when
171                            )
172 {
173     unsigned char buf[32];
174     time_t before_reseed, after_reseed;
175     int expected_state = (expect_success ? DRBG_READY : DRBG_ERROR);
176     unsigned int primary_reseed, public_reseed, private_reseed;
177
178     /*
179      * step 1: check preconditions
180      */
181
182     /* Test whether seed propagation is enabled */
183     if (!TEST_int_ne(primary_reseed = reseed_counter(primary), 0)
184         || !TEST_int_ne(public_reseed = reseed_counter(public), 0)
185         || !TEST_int_ne(private_reseed = reseed_counter(private), 0))
186         return 0;
187
188     /*
189      * step 2: generate random output
190      */
191
192     if (reseed_when == 0)
193         reseed_when = time(NULL);
194
195     /* Generate random output from the public and private DRBG */
196     before_reseed = expect_primary_reseed == 1 ? reseed_when : 0;
197     if (!TEST_int_eq(rand_bytes(buf, sizeof(buf)), expect_success)
198         || !TEST_int_eq(rand_priv_bytes(buf, sizeof(buf)), expect_success))
199         return 0;
200     after_reseed = time(NULL);
201
202
203     /*
204      * step 3: check postconditions
205      */
206
207     /* Test whether reseeding succeeded as expected */
208     if (!TEST_int_eq(state(primary), expected_state)
209         || !TEST_int_eq(state(public), expected_state)
210         || !TEST_int_eq(state(private), expected_state))
211         return 0;
212
213     if (expect_primary_reseed >= 0) {
214         /* Test whether primary DRBG was reseeded as expected */
215         if (!TEST_int_ge(reseed_counter(primary), primary_reseed))
216             return 0;
217     }
218
219     if (expect_public_reseed >= 0) {
220         /* Test whether public DRBG was reseeded as expected */
221         if (!TEST_int_ge(reseed_counter(public), public_reseed)
222                 || !TEST_uint_ge(reseed_counter(public),
223                                  reseed_counter(primary)))
224             return 0;
225     }
226
227     if (expect_private_reseed >= 0) {
228         /* Test whether public DRBG was reseeded as expected */
229         if (!TEST_int_ge(reseed_counter(private), private_reseed)
230                 || !TEST_uint_ge(reseed_counter(private),
231                                  reseed_counter(primary)))
232             return 0;
233     }
234
235     if (expect_success == 1) {
236         /* Test whether reseed time of primary DRBG is set correctly */
237         if (!TEST_time_t_le(before_reseed, reseed_time(primary))
238             || !TEST_time_t_le(reseed_time(primary), after_reseed))
239             return 0;
240
241         /* Test whether reseed times of child DRBGs are synchronized with primary */
242         if (!TEST_time_t_ge(reseed_time(public), reseed_time(primary))
243             || !TEST_time_t_ge(reseed_time(private), reseed_time(primary)))
244             return 0;
245     } else {
246         ERR_clear_error();
247     }
248
249     return 1;
250 }
251
252
253 #if defined(OPENSSL_SYS_UNIX)
254 /*
255  * Test whether primary, public and private DRBG are reseeded after
256  * forking the process.
257  */
258 static int test_drbg_reseed_after_fork(EVP_RAND_CTX *primary,
259                                        EVP_RAND_CTX *public,
260                                        EVP_RAND_CTX *private)
261 {
262     pid_t pid;
263     int status=0;
264
265     pid = fork();
266     if (!TEST_int_ge(pid, 0))
267         return 0;
268
269     if (pid > 0) {
270         /* I'm the parent; wait for the child and check its exit code */
271         return TEST_int_eq(waitpid(pid, &status, 0), pid) && TEST_int_eq(status, 0);
272     }
273
274     /* I'm the child; check whether all three DRBGs reseed. */
275     if (!TEST_true(test_drbg_reseed(1, primary, public, private, 1, 1, 1, 0)))
276         status = 1;
277     exit(status);
278 }
279 #endif
280
281 /*
282  * Test whether the default rand_method (RAND_OpenSSL()) is
283  * setup correctly, in particular whether reseeding  works
284  * as designed.
285  */
286 static int test_rand_drbg_reseed(void)
287 {
288     EVP_RAND_CTX *primary, *public, *private;
289     unsigned char rand_add_buf[256];
290     int rv = 0;
291     time_t before_reseed;
292
293     if (crngt_skip())
294         return TEST_skip("CRNGT cannot be disabled");
295
296     /* Check whether RAND_OpenSSL() is the default method */
297     if (!TEST_ptr_eq(RAND_get_rand_method(), RAND_OpenSSL()))
298         return 0;
299
300     /* All three DRBGs should be non-null */
301     if (!TEST_ptr(primary = RAND_get0_primary(NULL))
302         || !TEST_ptr(public = RAND_get0_public(NULL))
303         || !TEST_ptr(private = RAND_get0_private(NULL)))
304         return 0;
305
306     /* There should be three distinct DRBGs, two of them chained to primary */
307     if (!TEST_ptr_ne(public, private)
308         || !TEST_ptr_ne(public, primary)
309         || !TEST_ptr_ne(private, primary)
310         || !TEST_ptr_eq(prov_rand(public)->parent, prov_rand(primary))
311         || !TEST_ptr_eq(prov_rand(private)->parent, prov_rand(primary)))
312         return 0;
313
314     /* Disable CRNG testing for the primary DRBG */
315     if (!TEST_true(disable_crngt(primary)))
316         return 0;
317
318     /* uninstantiate the three global DRBGs */
319     EVP_RAND_uninstantiate(primary);
320     EVP_RAND_uninstantiate(private);
321     EVP_RAND_uninstantiate(public);
322
323
324     /*
325      * Test initial seeding of shared DRBGs
326      */
327     if (!TEST_true(test_drbg_reseed(1, primary, public, private, 1, 1, 1, 0)))
328         goto error;
329
330
331     /*
332      * Test initial state of shared DRBGs
333      */
334     if (!TEST_true(test_drbg_reseed(1, primary, public, private, 0, 0, 0, 0)))
335         goto error;
336
337     /*
338      * Test whether the public and private DRBG are both reseeded when their
339      * reseed counters differ from the primary's reseed counter.
340      */
341     inc_reseed_counter(primary);
342     if (!TEST_true(test_drbg_reseed(1, primary, public, private, 0, 1, 1, 0)))
343         goto error;
344
345     /*
346      * Test whether the public DRBG is reseeded when its reseed counter differs
347      * from the primary's reseed counter.
348      */
349     inc_reseed_counter(primary);
350     inc_reseed_counter(private);
351     if (!TEST_true(test_drbg_reseed(1, primary, public, private, 0, 1, 0, 0)))
352         goto error;
353
354     /*
355      * Test whether the private DRBG is reseeded when its reseed counter differs
356      * from the primary's reseed counter.
357      */
358     inc_reseed_counter(primary);
359     inc_reseed_counter(public);
360     if (!TEST_true(test_drbg_reseed(1, primary, public, private, 0, 0, 1, 0)))
361         goto error;
362
363 #if defined(OPENSSL_SYS_UNIX)
364     if (!TEST_true(test_drbg_reseed_after_fork(primary, public, private)))
365         goto error;
366 #endif
367
368     /* fill 'randomness' buffer with some arbitrary data */
369     memset(rand_add_buf, 'r', sizeof(rand_add_buf));
370
371 #ifndef FIPS_MODULE
372     /*
373      * Test whether all three DRBGs are reseeded by RAND_add().
374      * The before_reseed time has to be measured here and passed into the
375      * test_drbg_reseed() test, because the primary DRBG gets already reseeded
376      * in RAND_add(), whence the check for the condition
377      * before_reseed <= reseed_time(primary) will fail if the time value happens
378      * to increase between the RAND_add() and the test_drbg_reseed() call.
379      */
380     before_reseed = time(NULL);
381     RAND_add(rand_add_buf, sizeof(rand_add_buf), sizeof(rand_add_buf));
382     if (!TEST_true(test_drbg_reseed(1, primary, public, private, 1, 1, 1,
383                                     before_reseed)))
384         goto error;
385 #else /* FIPS_MODULE */
386     /*
387      * In FIPS mode, random data provided by the application via RAND_add()
388      * is not considered a trusted entropy source. It is only treated as
389      * additional_data and no reseeding is forced. This test assures that
390      * no reseeding occurs.
391      */
392     before_reseed = time(NULL);
393     RAND_add(rand_add_buf, sizeof(rand_add_buf), sizeof(rand_add_buf));
394     if (!TEST_true(test_drbg_reseed(1, primary, public, private, 0, 0, 0,
395                                     before_reseed)))
396         goto error;
397 #endif
398
399     rv = 1;
400
401 error:
402    return rv;
403 }
404
405 #if defined(OPENSSL_THREADS)
406 static int multi_thread_rand_bytes_succeeded = 1;
407 static int multi_thread_rand_priv_bytes_succeeded = 1;
408
409 static int set_reseed_time_interval(EVP_RAND_CTX *drbg, int t)
410 {
411     OSSL_PARAM params[2];
412
413     params[0] = OSSL_PARAM_construct_int(OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL,
414                                          &t);
415     params[1] = OSSL_PARAM_construct_end();
416     return EVP_RAND_set_ctx_params(drbg, params);
417 }
418
419 static void run_multi_thread_test(void)
420 {
421     unsigned char buf[256];
422     time_t start = time(NULL);
423     EVP_RAND_CTX *public = NULL, *private = NULL;
424
425     if (!TEST_ptr(public = RAND_get0_public(NULL))
426             || !TEST_ptr(private = RAND_get0_private(NULL))
427             || !TEST_true(set_reseed_time_interval(private, 1))
428             || !TEST_true(set_reseed_time_interval(public, 1))) {
429         multi_thread_rand_bytes_succeeded = 0;
430         return;
431     }
432
433     do {
434         if (rand_bytes(buf, sizeof(buf)) <= 0)
435             multi_thread_rand_bytes_succeeded = 0;
436         if (rand_priv_bytes(buf, sizeof(buf)) <= 0)
437             multi_thread_rand_priv_bytes_succeeded = 0;
438     }
439     while (time(NULL) - start < 5);
440 }
441
442 # if defined(OPENSSL_SYS_WINDOWS)
443
444 typedef HANDLE thread_t;
445
446 static DWORD WINAPI thread_run(LPVOID arg)
447 {
448     run_multi_thread_test();
449     /*
450      * Because we're linking with a static library, we must stop each
451      * thread explicitly, or so says OPENSSL_thread_stop(3)
452      */
453     OPENSSL_thread_stop();
454     return 0;
455 }
456
457 static int run_thread(thread_t *t)
458 {
459     *t = CreateThread(NULL, 0, thread_run, NULL, 0, NULL);
460     return *t != NULL;
461 }
462
463 static int wait_for_thread(thread_t thread)
464 {
465     return WaitForSingleObject(thread, INFINITE) == 0;
466 }
467
468 # else
469
470 typedef pthread_t thread_t;
471
472 static void *thread_run(void *arg)
473 {
474     run_multi_thread_test();
475     /*
476      * Because we're linking with a static library, we must stop each
477      * thread explicitly, or so says OPENSSL_thread_stop(3)
478      */
479     OPENSSL_thread_stop();
480     return NULL;
481 }
482
483 static int run_thread(thread_t *t)
484 {
485     return pthread_create(t, NULL, thread_run, NULL) == 0;
486 }
487
488 static int wait_for_thread(thread_t thread)
489 {
490     return pthread_join(thread, NULL) == 0;
491 }
492
493 # endif
494
495 /*
496  * The main thread will also run the test, so we'll have THREADS+1 parallel
497  * tests running
498  */
499 # define THREADS 3
500
501 static int test_multi_thread(void)
502 {
503     thread_t t[THREADS];
504     int i;
505
506     for (i = 0; i < THREADS; i++)
507         run_thread(&t[i]);
508     run_multi_thread_test();
509     for (i = 0; i < THREADS; i++)
510         wait_for_thread(t[i]);
511
512     if (!TEST_true(multi_thread_rand_bytes_succeeded))
513         return 0;
514     if (!TEST_true(multi_thread_rand_priv_bytes_succeeded))
515         return 0;
516
517     return 1;
518 }
519 #endif
520
521 static EVP_RAND_CTX *new_drbg(EVP_RAND_CTX *parent)
522 {
523     OSSL_PARAM params[2];
524     EVP_RAND *rand = NULL;
525     EVP_RAND_CTX *drbg = NULL;
526
527     params[0] = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_CIPHER,
528                                                  "AES-256-CTR", 0);
529     params[1] = OSSL_PARAM_construct_end();
530
531     if (!TEST_ptr(rand = EVP_RAND_fetch(NULL, "CTR-DRBG", NULL))
532             || !TEST_ptr(drbg = EVP_RAND_CTX_new(rand, parent))
533             || !TEST_true(EVP_RAND_set_ctx_params(drbg, params))) {
534         EVP_RAND_CTX_free(drbg);
535         drbg = NULL;
536     }
537     EVP_RAND_free(rand);
538     return drbg;
539 }
540
541 static int test_rand_drbg_prediction_resistance(void)
542 {
543     EVP_RAND_CTX *x = NULL, *y = NULL, *z = NULL;
544     unsigned char buf1[51], buf2[sizeof(buf1)];
545     int ret = 0, xreseed, yreseed, zreseed;
546
547     if (crngt_skip())
548         return TEST_skip("CRNGT cannot be disabled");
549
550     /* Initialise a three long DRBG chain */
551     if (!TEST_ptr(x = new_drbg(NULL))
552         || !TEST_true(disable_crngt(x))
553         || !TEST_true(EVP_RAND_instantiate(x, 0, 0, NULL, 0))
554         || !TEST_ptr(y = new_drbg(x))
555         || !TEST_true(EVP_RAND_instantiate(y, 0, 0, NULL, 0))
556         || !TEST_ptr(z = new_drbg(y))
557         || !TEST_true(EVP_RAND_instantiate(z, 0, 0, NULL, 0)))
558         goto err;
559
560     /*
561      * During a normal reseed, only the last DRBG in the chain should
562      * be reseeded.
563      */
564     inc_reseed_counter(y);
565     xreseed = reseed_counter(x);
566     yreseed = reseed_counter(y);
567     zreseed = reseed_counter(z);
568     if (!TEST_true(EVP_RAND_reseed(z, 0, NULL, 0, NULL, 0))
569         || !TEST_int_eq(reseed_counter(x), xreseed)
570         || !TEST_int_eq(reseed_counter(y), yreseed)
571         || !TEST_int_gt(reseed_counter(z), zreseed))
572         goto err;
573
574     /*
575      * When prediction resistance is requested, the request should be
576      * propagated to the primary, so that the entire DRBG chain reseeds.
577      */
578     zreseed = reseed_counter(z);
579     if (!TEST_true(EVP_RAND_reseed(z, 1, NULL, 0, NULL, 0))
580         || !TEST_int_gt(reseed_counter(x), xreseed)
581         || !TEST_int_gt(reseed_counter(y), yreseed)
582         || !TEST_int_gt(reseed_counter(z), zreseed))
583         goto err;
584
585     /*
586      * During a normal generate, only the last DRBG should be reseed */
587     inc_reseed_counter(y);
588     xreseed = reseed_counter(x);
589     yreseed = reseed_counter(y);
590     zreseed = reseed_counter(z);
591     if (!TEST_true(EVP_RAND_generate(z, buf1, sizeof(buf1), 0, 0, NULL, 0))
592         || !TEST_int_eq(reseed_counter(x), xreseed)
593         || !TEST_int_eq(reseed_counter(y), yreseed)
594         || !TEST_int_gt(reseed_counter(z), zreseed))
595         goto err;
596
597     /*
598      * When a prediction resistant generate is requested, the request
599      * should be propagated to the primary, reseeding the entire DRBG chain.
600      */
601     zreseed = reseed_counter(z);
602     if (!TEST_true(EVP_RAND_generate(z, buf2, sizeof(buf2), 0, 1, NULL, 0))
603         || !TEST_int_gt(reseed_counter(x), xreseed)
604         || !TEST_int_gt(reseed_counter(y), yreseed)
605         || !TEST_int_gt(reseed_counter(z), zreseed)
606         || !TEST_mem_ne(buf1, sizeof(buf1), buf2, sizeof(buf2)))
607         goto err;
608
609     /* Verify that a normal reseed still only reseeds the last DRBG */
610     inc_reseed_counter(y);
611     xreseed = reseed_counter(x);
612     yreseed = reseed_counter(y);
613     zreseed = reseed_counter(z);
614     if (!TEST_true(EVP_RAND_reseed(z, 0, NULL, 0, NULL, 0))
615         || !TEST_int_eq(reseed_counter(x), xreseed)
616         || !TEST_int_eq(reseed_counter(y), yreseed)
617         || !TEST_int_gt(reseed_counter(z), zreseed))
618         goto err;
619
620     ret = 1;
621 err:
622     EVP_RAND_CTX_free(z);
623     EVP_RAND_CTX_free(y);
624     EVP_RAND_CTX_free(x);
625     return ret;
626 }
627
628 int setup_tests(void)
629 {
630     ADD_TEST(test_rand_drbg_reseed);
631     ADD_TEST(test_rand_drbg_prediction_resistance);
632 #if defined(OPENSSL_THREADS)
633     ADD_TEST(test_multi_thread);
634 #endif
635     return 1;
636 }