2 * Copyright 2011-2020 The OpenSSL Project Authors. All Rights Reserved.
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
11 * RAND_DRBG_set is deprecated for public use, but still ok for
14 #include "internal/deprecated.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"
34 #if defined(OPENSSL_SYS_UNIX)
35 # include <sys/types.h>
36 # include <sys/wait.h>
44 * DRBG generate wrappers
46 static int gen_bytes(EVP_RAND_CTX *drbg, unsigned char *buf, int num)
48 const RAND_METHOD *meth = RAND_get_rand_method();
50 if (meth != NULL && meth != RAND_OpenSSL()) {
51 if (meth->bytes != NULL)
52 return meth->bytes(buf, num);
57 return EVP_RAND_generate(drbg, buf, num, 0, 0, NULL, 0);
61 static int rand_bytes(unsigned char *buf, int num)
63 return gen_bytes(RAND_get0_public(NULL), buf, num);
66 static int rand_priv_bytes(unsigned char *buf, int num)
68 return gen_bytes(RAND_get0_private(NULL), buf, num);
72 * DRBG query functions
74 static int state(EVP_RAND_CTX *drbg)
76 return EVP_RAND_state(drbg);
79 static unsigned int query_rand_uint(EVP_RAND_CTX *drbg, const char *name)
81 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
84 *params = OSSL_PARAM_construct_uint(name, &n);
85 if (EVP_RAND_get_ctx_params(drbg, params))
90 #define DRBG_UINT(name) \
91 static unsigned int name(EVP_RAND_CTX *drbg) \
93 return query_rand_uint(drbg, #name); \
95 DRBG_UINT(reseed_counter)
97 static PROV_DRBG *prov_rand(EVP_RAND_CTX *drbg)
99 return (PROV_DRBG *)drbg->data;
102 static void set_reseed_counter(EVP_RAND_CTX *drbg, unsigned int n)
104 PROV_DRBG *p = prov_rand(drbg);
106 p->reseed_counter = n;
109 static void inc_reseed_counter(EVP_RAND_CTX *drbg)
111 set_reseed_counter(drbg, reseed_counter(drbg) + 1);
114 static time_t reseed_time(EVP_RAND_CTX *drbg)
116 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
119 *params = OSSL_PARAM_construct_time_t(OSSL_DRBG_PARAM_RESEED_TIME, &t);
120 if (EVP_RAND_get_ctx_params(drbg, params))
126 * When building the FIPS module, it isn't possible to disable the continuous
127 * RNG tests. Tests that require this are skipped.
129 static int crngt_skip(void)
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
144 static int disable_crngt(EVP_RAND_CTX *drbg)
150 * Generates random output using rand_bytes() and rand_priv_bytes()
151 * and checks whether the three shared DRBGs were reseeded as
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.
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,
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;
179 * step 1: check preconditions
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))
189 * step 2: generate random output
192 if (reseed_when == 0)
193 reseed_when = time(NULL);
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))
200 after_reseed = time(NULL);
204 * step 3: check postconditions
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))
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))
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)))
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)))
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))
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)))
253 #if defined(OPENSSL_SYS_UNIX)
255 * Test whether primary, public and private DRBG are reseeded after
256 * forking the process.
258 static int test_drbg_reseed_after_fork(EVP_RAND_CTX *primary,
259 EVP_RAND_CTX *public,
260 EVP_RAND_CTX *private)
266 if (!TEST_int_ge(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);
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)))
282 * Test whether the default rand_method (RAND_OpenSSL()) is
283 * setup correctly, in particular whether reseeding works
286 static int test_rand_drbg_reseed(void)
288 EVP_RAND_CTX *primary, *public, *private;
289 unsigned char rand_add_buf[256];
291 time_t before_reseed;
294 return TEST_skip("CRNGT cannot be disabled");
296 /* Check whether RAND_OpenSSL() is the default method */
297 if (!TEST_ptr_eq(RAND_get_rand_method(), RAND_OpenSSL()))
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)))
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)))
314 /* Disable CRNG testing for the primary DRBG */
315 if (!TEST_true(disable_crngt(primary)))
318 /* uninstantiate the three global DRBGs */
319 EVP_RAND_uninstantiate(primary);
320 EVP_RAND_uninstantiate(private);
321 EVP_RAND_uninstantiate(public);
325 * Test initial seeding of shared DRBGs
327 if (!TEST_true(test_drbg_reseed(1, primary, public, private, 1, 1, 1, 0)))
332 * Test initial state of shared DRBGs
334 if (!TEST_true(test_drbg_reseed(1, primary, public, private, 0, 0, 0, 0)))
338 * Test whether the public and private DRBG are both reseeded when their
339 * reseed counters differ from the primary's reseed counter.
341 inc_reseed_counter(primary);
342 if (!TEST_true(test_drbg_reseed(1, primary, public, private, 0, 1, 1, 0)))
346 * Test whether the public DRBG is reseeded when its reseed counter differs
347 * from the primary's reseed counter.
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)))
355 * Test whether the private DRBG is reseeded when its reseed counter differs
356 * from the primary's reseed counter.
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)))
363 #if defined(OPENSSL_SYS_UNIX)
364 if (!TEST_true(test_drbg_reseed_after_fork(primary, public, private)))
368 /* fill 'randomness' buffer with some arbitrary data */
369 memset(rand_add_buf, 'r', sizeof(rand_add_buf));
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.
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,
385 #else /* FIPS_MODULE */
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.
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,
405 #if defined(OPENSSL_THREADS)
406 static int multi_thread_rand_bytes_succeeded = 1;
407 static int multi_thread_rand_priv_bytes_succeeded = 1;
409 static int set_reseed_time_interval(EVP_RAND_CTX *drbg, int t)
411 OSSL_PARAM params[2];
413 params[0] = OSSL_PARAM_construct_int(OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL,
415 params[1] = OSSL_PARAM_construct_end();
416 return EVP_RAND_set_ctx_params(drbg, params);
419 static void run_multi_thread_test(void)
421 unsigned char buf[256];
422 time_t start = time(NULL);
423 EVP_RAND_CTX *public = NULL, *private = NULL;
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;
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;
439 while (time(NULL) - start < 5);
442 # if defined(OPENSSL_SYS_WINDOWS)
444 typedef HANDLE thread_t;
446 static DWORD WINAPI thread_run(LPVOID arg)
448 run_multi_thread_test();
450 * Because we're linking with a static library, we must stop each
451 * thread explicitly, or so says OPENSSL_thread_stop(3)
453 OPENSSL_thread_stop();
457 static int run_thread(thread_t *t)
459 *t = CreateThread(NULL, 0, thread_run, NULL, 0, NULL);
463 static int wait_for_thread(thread_t thread)
465 return WaitForSingleObject(thread, INFINITE) == 0;
470 typedef pthread_t thread_t;
472 static void *thread_run(void *arg)
474 run_multi_thread_test();
476 * Because we're linking with a static library, we must stop each
477 * thread explicitly, or so says OPENSSL_thread_stop(3)
479 OPENSSL_thread_stop();
483 static int run_thread(thread_t *t)
485 return pthread_create(t, NULL, thread_run, NULL) == 0;
488 static int wait_for_thread(thread_t thread)
490 return pthread_join(thread, NULL) == 0;
496 * The main thread will also run the test, so we'll have THREADS+1 parallel
501 static int test_multi_thread(void)
506 for (i = 0; i < THREADS; i++)
508 run_multi_thread_test();
509 for (i = 0; i < THREADS; i++)
510 wait_for_thread(t[i]);
512 if (!TEST_true(multi_thread_rand_bytes_succeeded))
514 if (!TEST_true(multi_thread_rand_priv_bytes_succeeded))
521 static EVP_RAND_CTX *new_drbg(EVP_RAND_CTX *parent)
523 OSSL_PARAM params[2];
524 EVP_RAND *rand = NULL;
525 EVP_RAND_CTX *drbg = NULL;
527 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_CIPHER,
529 params[1] = OSSL_PARAM_construct_end();
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);
541 static int test_rand_drbg_prediction_resistance(void)
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;
548 return TEST_skip("CRNGT cannot be disabled");
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)))
561 * During a normal reseed, only the last DRBG in the chain should
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))
575 * When prediction resistance is requested, the request should be
576 * propagated to the primary, so that the entire DRBG chain reseeds.
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))
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))
598 * When a prediction resistant generate is requested, the request
599 * should be propagated to the primary, reseeding the entire DRBG chain.
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)))
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))
622 EVP_RAND_CTX_free(z);
623 EVP_RAND_CTX_free(y);
624 EVP_RAND_CTX_free(x);
628 int setup_tests(void)
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);