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>
43 typedef struct drbg_selftest_data_st {
48 /* KAT data for no PR */
49 const unsigned char *entropy;
51 const unsigned char *nonce;
53 const unsigned char *pers;
55 const unsigned char *adin;
57 const unsigned char *entropyreseed;
58 size_t entropyreseedlen;
59 const unsigned char *adinreseed;
61 const unsigned char *adin2;
63 const unsigned char *expected;
65 const unsigned char *kat2;
69 const unsigned char *entropy_pr;
71 const unsigned char *nonce_pr;
73 const unsigned char *pers_pr;
75 const unsigned char *adin_pr;
77 const unsigned char *entropypr_pr;
78 size_t entropyprlen_pr;
79 const unsigned char *ading_pr;
81 const unsigned char *entropyg_pr;
82 size_t entropyglen_pr;
83 const unsigned char *kat_pr;
85 const unsigned char *kat2_pr;
89 #define make_drbg_test_data(nid, flag, pr, post) {\
91 pr##_entropyinput, sizeof(pr##_entropyinput), \
92 pr##_nonce, sizeof(pr##_nonce), \
93 pr##_personalizationstring, sizeof(pr##_personalizationstring), \
94 pr##_additionalinput, sizeof(pr##_additionalinput), \
95 pr##_entropyinputreseed, sizeof(pr##_entropyinputreseed), \
96 pr##_additionalinputreseed, sizeof(pr##_additionalinputreseed), \
97 pr##_additionalinput2, sizeof(pr##_additionalinput2), \
98 pr##_int_returnedbits, sizeof(pr##_int_returnedbits), \
99 pr##_returnedbits, sizeof(pr##_returnedbits), \
100 pr##_pr_entropyinput, sizeof(pr##_pr_entropyinput), \
101 pr##_pr_nonce, sizeof(pr##_pr_nonce), \
102 pr##_pr_personalizationstring, sizeof(pr##_pr_personalizationstring), \
103 pr##_pr_additionalinput, sizeof(pr##_pr_additionalinput), \
104 pr##_pr_entropyinputpr, sizeof(pr##_pr_entropyinputpr), \
105 pr##_pr_additionalinput2, sizeof(pr##_pr_additionalinput2), \
106 pr##_pr_entropyinputpr2, sizeof(pr##_pr_entropyinputpr2), \
107 pr##_pr_int_returnedbits, sizeof(pr##_pr_int_returnedbits), \
108 pr##_pr_returnedbits, sizeof(pr##_pr_returnedbits) \
111 #define make_drbg_test_data_use_df(nid, pr, p) \
112 make_drbg_test_data(nid, 0, pr, p)
114 #define make_drbg_test_data_no_df(nid, pr, p) \
115 make_drbg_test_data(nid, RAND_DRBG_FLAG_CTR_NO_DF, pr, p)
117 #define make_drbg_test_data_hash(nid, pr, p) \
118 make_drbg_test_data(nid, RAND_DRBG_FLAG_HMAC, hmac_##pr, p), \
119 make_drbg_test_data(nid, 0, pr, p)
121 static DRBG_SELFTEST_DATA drbg_test[] = {
123 /* FIPS mode doesn't support CTR DRBG without a derivation function */
124 make_drbg_test_data_no_df (NID_aes_128_ctr, aes_128_no_df, 0),
125 make_drbg_test_data_no_df (NID_aes_192_ctr, aes_192_no_df, 0),
126 make_drbg_test_data_no_df (NID_aes_256_ctr, aes_256_no_df, 1),
128 make_drbg_test_data_use_df(NID_aes_128_ctr, aes_128_use_df, 0),
129 make_drbg_test_data_use_df(NID_aes_192_ctr, aes_192_use_df, 0),
130 make_drbg_test_data_use_df(NID_aes_256_ctr, aes_256_use_df, 1),
131 make_drbg_test_data_hash(NID_sha1, sha1, 0),
132 make_drbg_test_data_hash(NID_sha224, sha224, 0),
133 make_drbg_test_data_hash(NID_sha256, sha256, 1),
134 make_drbg_test_data_hash(NID_sha384, sha384, 0),
135 make_drbg_test_data_hash(NID_sha512, sha512, 0),
139 * DRBG query functions
141 static int state(RAND_DRBG *drbg)
143 return EVP_RAND_state(drbg->rand);
146 static size_t query_rand_size_t(RAND_DRBG *drbg, const char *name)
148 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
151 *params = OSSL_PARAM_construct_size_t(name, &n);
152 if (EVP_RAND_get_ctx_params(drbg->rand, params))
157 static unsigned int query_rand_uint(RAND_DRBG *drbg, const char *name)
159 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
162 *params = OSSL_PARAM_construct_uint(name, &n);
163 if (EVP_RAND_get_ctx_params(drbg->rand, params))
168 #define DRBG_SIZE_T(name) \
169 static size_t name(RAND_DRBG *drbg) \
171 return query_rand_size_t(drbg, #name); \
173 DRBG_SIZE_T(min_entropylen)
174 DRBG_SIZE_T(max_entropylen)
175 DRBG_SIZE_T(min_noncelen)
176 DRBG_SIZE_T(max_noncelen)
177 DRBG_SIZE_T(max_perslen)
178 DRBG_SIZE_T(max_adinlen)
179 DRBG_SIZE_T(max_request)
181 #define DRBG_UINT(name) \
182 static unsigned int name(RAND_DRBG *drbg) \
184 return query_rand_uint(drbg, #name); \
186 DRBG_UINT(reseed_requests)
187 DRBG_UINT(reseed_counter)
189 static PROV_DRBG *prov_rand(RAND_DRBG *drbg)
191 return (PROV_DRBG *)drbg->rand->data;
194 static void set_reseed_counter(RAND_DRBG *drbg, unsigned int n)
196 PROV_DRBG *p = prov_rand(drbg);
198 p->reseed_counter = n;
201 static void inc_reseed_counter(RAND_DRBG *drbg)
203 set_reseed_counter(drbg, reseed_counter(drbg) + 1);
206 static time_t reseed_time(RAND_DRBG *drbg)
208 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
211 *params = OSSL_PARAM_construct_time_t(OSSL_DRBG_PARAM_RESEED_TIME, &t);
212 if (EVP_RAND_get_ctx_params(drbg->rand, params))
218 * Test context data, attached as EXDATA to the RAND_DRBG
220 typedef struct test_ctx_st {
221 const unsigned char *entropy;
224 const unsigned char *nonce;
229 static size_t kat_entropy(RAND_DRBG *drbg, unsigned char **pout,
230 int entropy, size_t min_len, size_t max_len,
231 int prediction_resistance)
233 TEST_CTX *t = (TEST_CTX *)RAND_DRBG_get_callback_data(drbg);
236 *pout = (unsigned char *)t->entropy;
237 return t->entropylen;
240 static size_t kat_nonce(RAND_DRBG *drbg, unsigned char **pout,
241 int entropy, size_t min_len, size_t max_len)
243 TEST_CTX *t = (TEST_CTX *)RAND_DRBG_get_callback_data(drbg);
246 *pout = (unsigned char *)t->nonce;
251 * When building the FIPS module, it isn't possible to disable the continuous
252 * RNG tests. Tests that require this are skipped.
254 static int crngt_skip(void)
264 * Disable CRNG testing if it is enabled.
265 * This stub remains to indicate the calling locations where it is necessary.
266 * Once the RNG infrastructure is able to disable these tests, it should be
269 static int disable_crngt(RAND_DRBG *drbg)
274 static int uninstantiate(RAND_DRBG *drbg)
276 int ret = drbg == NULL ? 1 : RAND_DRBG_uninstantiate(drbg);
283 * Do a single KAT test. Return 0 on failure.
285 static int single_kat(DRBG_SELFTEST_DATA *td)
287 RAND_DRBG *drbg = NULL;
290 unsigned char buff[1024];
293 return TEST_skip("CRNGT cannot be disabled");
296 * Test without PR: Instantiate DRBG with test entropy, nonce and
297 * personalisation string.
299 if (!TEST_ptr(drbg = RAND_DRBG_new(td->nid, td->flags, NULL)))
301 if (!TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
303 || !TEST_true(RAND_DRBG_set_callback_data(drbg, &t))
304 || !TEST_true(disable_crngt(drbg))) {
308 memset(&t, 0, sizeof(t));
309 t.entropy = td->entropy;
310 t.entropylen = td->entropylen;
312 t.noncelen = td->noncelen;
314 if (!TEST_true(RAND_DRBG_instantiate(drbg, td->pers, td->perslen))
315 || !TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
316 td->adin, td->adinlen))
317 || !TEST_mem_eq(td->expected, td->exlen, buff, td->exlen))
320 /* Reseed DRBG with test entropy and additional input */
321 t.entropy = td->entropyreseed;
322 t.entropylen = td->entropyreseedlen;
323 if (!TEST_true(RAND_DRBG_reseed(drbg, td->adinreseed, td->adinreseedlen, 0)
324 || !TEST_true(RAND_DRBG_generate(drbg, buff, td->kat2len, 0,
325 td->adin2, td->adin2len))
326 || !TEST_mem_eq(td->kat2, td->kat2len, buff, td->kat2len)))
331 * Now test with PR: Instantiate DRBG with test entropy, nonce and
332 * personalisation string.
334 if (!TEST_true(RAND_DRBG_set(drbg, td->nid, td->flags))
335 || !TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
337 || !TEST_true(RAND_DRBG_set_callback_data(drbg, &t)))
339 t.entropy = td->entropy_pr;
340 t.entropylen = td->entropylen_pr;
341 t.nonce = td->nonce_pr;
342 t.noncelen = td->noncelen_pr;
345 if (!TEST_true(RAND_DRBG_instantiate(drbg, td->pers_pr, td->perslen_pr)))
349 * Now generate with PR: we need to supply entropy as this will
350 * perform a reseed operation.
352 t.entropy = td->entropypr_pr;
353 t.entropylen = td->entropyprlen_pr;
354 if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->katlen_pr, 1,
355 td->adin_pr, td->adinlen_pr))
356 || !TEST_mem_eq(td->kat_pr, td->katlen_pr, buff, td->katlen_pr))
360 * Now generate again with PR: supply new entropy again.
362 t.entropy = td->entropyg_pr;
363 t.entropylen = td->entropyglen_pr;
365 if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->kat2len_pr, 1,
366 td->ading_pr, td->adinglen_pr))
367 || !TEST_mem_eq(td->kat2_pr, td->kat2len_pr,
368 buff, td->kat2len_pr))
373 RAND_DRBG_free(drbg);
374 return failures == 0;
378 * Initialise a DRBG based on selftest data
380 static int init(RAND_DRBG *drbg, DRBG_SELFTEST_DATA *td, TEST_CTX *t)
382 if (!TEST_true(RAND_DRBG_set(drbg, td->nid, td->flags))
383 || !TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
386 RAND_DRBG_set_callback_data(drbg, t);
387 t->entropy = td->entropy;
388 t->entropylen = td->entropylen;
389 t->nonce = td->nonce;
390 t->noncelen = td->noncelen;
397 * Initialise and instantiate DRBG based on selftest data
399 static int instantiate(RAND_DRBG *drbg, DRBG_SELFTEST_DATA *td,
402 if (!TEST_true(init(drbg, td, t))
403 || !TEST_true(RAND_DRBG_instantiate(drbg, td->pers, td->perslen)))
409 * Perform extensive error checking as required by SP800-90.
410 * Induce several failure modes and check an error condition is set.
412 static int error_check(DRBG_SELFTEST_DATA *td)
414 RAND_DRBG *drbg = NULL;
416 unsigned char buff[1024];
417 unsigned int reseed_counter_tmp;
420 if (!TEST_ptr(drbg = RAND_DRBG_new(td->nid, td->flags, NULL))
421 || !TEST_true(disable_crngt(drbg)))
425 * Personalisation string tests
428 /* Test detection of too large personalisation string */
429 if (!init(drbg, td, &t)
430 || RAND_DRBG_instantiate(drbg, td->pers, max_perslen(drbg) + 1) > 0)
434 * Entropy source tests
437 /* Test entropy source failure detection: i.e. returns no data */
439 if (TEST_int_le(RAND_DRBG_instantiate(drbg, td->pers, td->perslen), 0))
442 /* Try to generate output from uninstantiated DRBG */
443 if (!TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
444 td->adin, td->adinlen))
445 || !uninstantiate(drbg))
448 /* Test insufficient entropy */
449 t.entropylen = min_entropylen(drbg) - 1;
450 if (!init(drbg, td, &t)
451 || RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0
452 || !uninstantiate(drbg))
455 /* Test too much entropy */
456 t.entropylen = max_entropylen(drbg) + 1;
457 if (!init(drbg, td, &t)
458 || RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0
459 || !uninstantiate(drbg))
466 /* Test too small nonce */
467 if (min_noncelen(drbg) != 0) {
468 t.noncelen = min_noncelen(drbg) - 1;
469 if (!init(drbg, td, &t)
470 || RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0
471 || !uninstantiate(drbg))
475 /* Test too large nonce */
476 if (max_noncelen(drbg) != 0) {
477 t.noncelen = max_noncelen(drbg) + 1;
478 if (!init(drbg, td, &t)
479 || RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0
480 || !uninstantiate(drbg))
484 /* Instantiate with valid data, Check generation is now OK */
485 if (!instantiate(drbg, td, &t)
486 || !TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
487 td->adin, td->adinlen)))
490 /* Request too much data for one request */
491 if (!TEST_false(RAND_DRBG_generate(drbg, buff, max_request(drbg) + 1, 0,
492 td->adin, td->adinlen)))
495 /* Try too large additional input */
496 if (!TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
497 td->adin, max_adinlen(drbg) + 1)))
501 * Check prediction resistance request fails if entropy source
505 if (TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 1,
506 td->adin, td->adinlen))
507 || !uninstantiate(drbg))
510 /* Instantiate again with valid data */
511 if (!instantiate(drbg, td, &t))
513 reseed_counter_tmp = reseed_counter(drbg);
514 set_reseed_counter(drbg, reseed_requests(drbg));
516 /* Generate output and check entropy has been requested for reseed */
518 if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
519 td->adin, td->adinlen))
520 || !TEST_int_eq(t.entropycnt, 1)
521 || !TEST_int_eq(reseed_counter(drbg), reseed_counter_tmp + 1)
522 || !uninstantiate(drbg))
526 * Check prediction resistance request fails if entropy source
530 if (!TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 1,
531 td->adin, td->adinlen))
532 || !uninstantiate(drbg))
535 /* Test reseed counter works */
536 if (!instantiate(drbg, td, &t))
538 reseed_counter_tmp = reseed_counter(drbg);
539 set_reseed_counter(drbg, reseed_requests(drbg));
541 /* Generate output and check entropy has been requested for reseed */
543 if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
544 td->adin, td->adinlen))
545 || !TEST_int_eq(t.entropycnt, 1)
546 || !TEST_int_eq(reseed_counter(drbg), reseed_counter_tmp + 1)
547 || !uninstantiate(drbg))
551 * Explicit reseed tests
554 /* Test explicit reseed with too large additional input */
555 if (!instantiate(drbg, td, &t)
556 || RAND_DRBG_reseed(drbg, td->adin, max_adinlen(drbg) + 1, 0) > 0)
559 /* Test explicit reseed with entropy source failure */
561 if (!TEST_int_le(RAND_DRBG_reseed(drbg, td->adin, td->adinlen, 0), 0)
562 || !uninstantiate(drbg))
565 /* Test explicit reseed with too much entropy */
566 if (!instantiate(drbg, td, &t))
568 t.entropylen = max_entropylen(drbg) + 1;
569 if (!TEST_int_le(RAND_DRBG_reseed(drbg, td->adin, td->adinlen, 0), 0)
570 || !uninstantiate(drbg))
573 /* Test explicit reseed with too little entropy */
574 if (!instantiate(drbg, td, &t))
576 t.entropylen = min_entropylen(drbg) - 1;
577 if (!TEST_int_le(RAND_DRBG_reseed(drbg, td->adin, td->adinlen, 0), 0)
578 || !uninstantiate(drbg))
581 /* Standard says we have to check uninstantiate really zeroes */
582 if (!TEST_true(EVP_RAND_verify_zeroization(drbg->rand)))
589 RAND_DRBG_free(drbg);
593 static int test_kats(int i)
595 DRBG_SELFTEST_DATA *td = &drbg_test[i];
606 static int test_error_checks(int i)
608 DRBG_SELFTEST_DATA *td = &drbg_test[i];
612 return TEST_skip("CRNGT cannot be disabled");
623 * Generates random output using RAND_bytes() and RAND_priv_bytes()
624 * and checks whether the three shared DRBGs were reseeded as
627 * |expect_success|: expected outcome (as reported by RAND_status())
628 * |master|, |public|, |private|: pointers to the three shared DRBGs
629 * |expect_xxx_reseed| =
630 * 1: it is expected that the specified DRBG is reseeded
631 * 0: it is expected that the specified DRBG is not reseeded
632 * -1: don't check whether the specified DRBG was reseeded or not
633 * |reseed_time|: if nonzero, used instead of time(NULL) to set the
634 * |before_reseed| time.
636 static int test_drbg_reseed(int expect_success,
640 int expect_master_reseed,
641 int expect_public_reseed,
642 int expect_private_reseed,
646 unsigned char buf[32];
647 time_t before_reseed, after_reseed;
648 int expected_state = (expect_success ? DRBG_READY : DRBG_ERROR);
649 unsigned int master_reseed, public_reseed, private_reseed;
652 * step 1: check preconditions
655 /* Test whether seed propagation is enabled */
656 if (!TEST_int_ne(master_reseed = reseed_counter(master), 0)
657 || !TEST_int_ne(public_reseed = reseed_counter(public), 0)
658 || !TEST_int_ne(private_reseed = reseed_counter(private), 0))
662 * step 2: generate random output
665 if (reseed_when == 0)
666 reseed_when = time(NULL);
668 /* Generate random output from the public and private DRBG */
669 before_reseed = expect_master_reseed == 1 ? reseed_when : 0;
670 if (!TEST_int_eq(RAND_bytes(buf, sizeof(buf)), expect_success)
671 || !TEST_int_eq(RAND_priv_bytes(buf, sizeof(buf)), expect_success))
673 after_reseed = time(NULL);
677 * step 3: check postconditions
680 /* Test whether reseeding succeeded as expected */
681 if (/*!TEST_int_eq(state(master), expected_state)
682 || */!TEST_int_eq(state(public), expected_state)
683 || !TEST_int_eq(state(private), expected_state))
686 if (expect_master_reseed >= 0) {
687 /* Test whether master DRBG was reseeded as expected */
688 if (!TEST_int_ge(reseed_counter(master), master_reseed))
692 if (expect_public_reseed >= 0) {
693 /* Test whether public DRBG was reseeded as expected */
694 if (!TEST_int_ge(reseed_counter(public), public_reseed)
695 || !TEST_uint_ge(reseed_counter(public),
696 reseed_counter(master)))
700 if (expect_private_reseed >= 0) {
701 /* Test whether public DRBG was reseeded as expected */
702 if (!TEST_int_ge(reseed_counter(private), private_reseed)
703 || !TEST_uint_ge(reseed_counter(private),
704 reseed_counter(master)))
708 if (expect_success == 1) {
709 /* Test whether reseed time of master DRBG is set correctly */
710 if (!TEST_time_t_le(before_reseed, reseed_time(master))
711 || !TEST_time_t_le(reseed_time(master), after_reseed))
714 /* Test whether reseed times of child DRBGs are synchronized with master */
715 if (!TEST_time_t_ge(reseed_time(public), reseed_time(master))
716 || !TEST_time_t_ge(reseed_time(private), reseed_time(master)))
726 #if defined(OPENSSL_SYS_UNIX)
728 * Test whether master, public and private DRBG are reseeded after
729 * forking the process.
731 static int test_drbg_reseed_after_fork(RAND_DRBG *master,
739 if (!TEST_int_ge(pid, 0))
743 /* I'm the parent; wait for the child and check its exit code */
744 return TEST_int_eq(waitpid(pid, &status, 0), pid) && TEST_int_eq(status, 0);
747 /* I'm the child; check whether all three DRBGs reseed. */
748 if (!TEST_true(test_drbg_reseed(1, master, public, private, 1, 1, 1, 0)))
755 * Test whether the default rand_method (RAND_OpenSSL()) is
756 * setup correctly, in particular whether reseeding works
759 static int test_rand_drbg_reseed(void)
761 RAND_DRBG *master, *public, *private;
762 unsigned char rand_add_buf[256];
764 time_t before_reseed;
767 return TEST_skip("CRNGT cannot be disabled");
769 /* Check whether RAND_OpenSSL() is the default method */
770 if (!TEST_ptr_eq(RAND_get_rand_method(), RAND_OpenSSL()))
773 /* All three DRBGs should be non-null */
774 if (!TEST_ptr(master = RAND_DRBG_get0_master())
775 || !TEST_ptr(public = RAND_DRBG_get0_public())
776 || !TEST_ptr(private = RAND_DRBG_get0_private()))
779 /* There should be three distinct DRBGs, two of them chained to master */
780 if (!TEST_ptr_ne(public, private)
781 || !TEST_ptr_ne(public, master)
782 || !TEST_ptr_ne(private, master)
783 || !TEST_ptr_eq(public->parent, master)
784 || !TEST_ptr_eq(private->parent, master))
787 /* Disable CRNG testing for the master DRBG */
788 if (!TEST_true(disable_crngt(master)))
791 /* uninstantiate the three global DRBGs */
792 RAND_DRBG_uninstantiate(master);
793 RAND_DRBG_uninstantiate(private);
794 RAND_DRBG_uninstantiate(public);
798 * Test initial seeding of shared DRBGs
800 if (!TEST_true(test_drbg_reseed(1, master, public, private, 1, 1, 1, 0)))
805 * Test initial state of shared DRBGs
807 if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 0, 0, 0)))
811 * Test whether the public and private DRBG are both reseeded when their
812 * reseed counters differ from the master's reseed counter.
814 inc_reseed_counter(master);
815 if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 1, 1, 0)))
819 * Test whether the public DRBG is reseeded when its reseed counter differs
820 * from the master's reseed counter.
822 inc_reseed_counter(master);
823 inc_reseed_counter(private);
824 if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 1, 0, 0)))
828 * Test whether the private DRBG is reseeded when its reseed counter differs
829 * from the master's reseed counter.
831 inc_reseed_counter(master);
832 inc_reseed_counter(public);
833 if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 0, 1, 0)))
836 #if defined(OPENSSL_SYS_UNIX)
837 if (!TEST_true(test_drbg_reseed_after_fork(master, public, private)))
841 /* fill 'randomness' buffer with some arbitrary data */
842 memset(rand_add_buf, 'r', sizeof(rand_add_buf));
846 * Test whether all three DRBGs are reseeded by RAND_add().
847 * The before_reseed time has to be measured here and passed into the
848 * test_drbg_reseed() test, because the master DRBG gets already reseeded
849 * in RAND_add(), whence the check for the condition
850 * before_reseed <= reseed_time(master) will fail if the time value happens
851 * to increase between the RAND_add() and the test_drbg_reseed() call.
853 before_reseed = time(NULL);
854 RAND_add(rand_add_buf, sizeof(rand_add_buf), sizeof(rand_add_buf));
855 if (!TEST_true(test_drbg_reseed(1, master, public, private, 1, 1, 1,
858 #else /* FIPS_MODULE */
860 * In FIPS mode, random data provided by the application via RAND_add()
861 * is not considered a trusted entropy source. It is only treated as
862 * additional_data and no reseeding is forced. This test assures that
863 * no reseeding occurs.
865 before_reseed = time(NULL);
866 RAND_add(rand_add_buf, sizeof(rand_add_buf), sizeof(rand_add_buf));
867 if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 0, 0,
878 #if defined(OPENSSL_THREADS)
879 static int multi_thread_rand_bytes_succeeded = 1;
880 static int multi_thread_rand_priv_bytes_succeeded = 1;
882 static void run_multi_thread_test(void)
884 unsigned char buf[256];
885 time_t start = time(NULL);
886 RAND_DRBG *public = NULL, *private = NULL;
888 if (!TEST_ptr(public = RAND_DRBG_get0_public())
889 || !TEST_ptr(private = RAND_DRBG_get0_private())) {
890 multi_thread_rand_bytes_succeeded = 0;
893 RAND_DRBG_set_reseed_time_interval(private, 1);
894 RAND_DRBG_set_reseed_time_interval(public, 1);
897 if (RAND_bytes(buf, sizeof(buf)) <= 0)
898 multi_thread_rand_bytes_succeeded = 0;
899 if (RAND_priv_bytes(buf, sizeof(buf)) <= 0)
900 multi_thread_rand_priv_bytes_succeeded = 0;
902 while(time(NULL) - start < 5);
905 # if defined(OPENSSL_SYS_WINDOWS)
907 typedef HANDLE thread_t;
909 static DWORD WINAPI thread_run(LPVOID arg)
911 run_multi_thread_test();
913 * Because we're linking with a static library, we must stop each
914 * thread explicitly, or so says OPENSSL_thread_stop(3)
916 OPENSSL_thread_stop();
920 static int run_thread(thread_t *t)
922 *t = CreateThread(NULL, 0, thread_run, NULL, 0, NULL);
926 static int wait_for_thread(thread_t thread)
928 return WaitForSingleObject(thread, INFINITE) == 0;
933 typedef pthread_t thread_t;
935 static void *thread_run(void *arg)
937 run_multi_thread_test();
939 * Because we're linking with a static library, we must stop each
940 * thread explicitly, or so says OPENSSL_thread_stop(3)
942 OPENSSL_thread_stop();
946 static int run_thread(thread_t *t)
948 return pthread_create(t, NULL, thread_run, NULL) == 0;
951 static int wait_for_thread(thread_t thread)
953 return pthread_join(thread, NULL) == 0;
959 * The main thread will also run the test, so we'll have THREADS+1 parallel
964 static int test_multi_thread(void)
969 for (i = 0; i < THREADS; i++)
971 run_multi_thread_test();
972 for (i = 0; i < THREADS; i++)
973 wait_for_thread(t[i]);
975 if (!TEST_true(multi_thread_rand_bytes_succeeded))
977 if (!TEST_true(multi_thread_rand_priv_bytes_succeeded))
984 static int test_rand_drbg_prediction_resistance(void)
986 RAND_DRBG *m = NULL, *i = NULL, *s = NULL;
987 unsigned char buf1[51], buf2[sizeof(buf1)];
988 int ret = 0, mreseed, ireseed, sreseed;
991 return TEST_skip("CRNGT cannot be disabled");
993 /* Initialise a three long DRBG chain */
994 if (!TEST_ptr(m = RAND_DRBG_new(0, 0, NULL))
995 || !TEST_true(disable_crngt(m))
996 || !TEST_true(RAND_DRBG_instantiate(m, NULL, 0))
997 || !TEST_ptr(i = RAND_DRBG_new(0, 0, m))
998 || !TEST_true(RAND_DRBG_instantiate(i, NULL, 0))
999 || !TEST_ptr(s = RAND_DRBG_new(0, 0, i))
1000 || !TEST_true(RAND_DRBG_instantiate(s, NULL, 0)))
1003 /* During a normal reseed, only the slave DRBG should be reseed */
1004 inc_reseed_counter(i);
1005 mreseed = reseed_counter(m);
1006 ireseed = reseed_counter(i);
1007 sreseed = reseed_counter(s);
1008 if (!TEST_true(RAND_DRBG_reseed(s, NULL, 0, 0))
1009 || !TEST_int_eq(reseed_counter(m), mreseed)
1010 || !TEST_int_eq(reseed_counter(i), ireseed)
1011 || !TEST_int_gt(reseed_counter(s), sreseed))
1015 * When prediction resistance is requested, the request should be
1016 * propagated to the master, so that the entire DRBG chain reseeds.
1018 sreseed = reseed_counter(s);
1019 if (!TEST_true(RAND_DRBG_reseed(s, NULL, 0, 1))
1020 || !TEST_int_gt(reseed_counter(m), mreseed)
1021 || !TEST_int_gt(reseed_counter(i), ireseed)
1022 || !TEST_int_gt(reseed_counter(s), sreseed))
1025 /* During a normal generate, only the slave DRBG should be reseed */
1026 inc_reseed_counter(i);
1027 mreseed = reseed_counter(m);
1028 ireseed = reseed_counter(i);
1029 sreseed = reseed_counter(s);
1030 if (!TEST_true(RAND_DRBG_generate(s, buf1, sizeof(buf1), 0, NULL, 0))
1031 || !TEST_int_eq(reseed_counter(m), mreseed)
1032 || !TEST_int_eq(reseed_counter(i), ireseed)
1033 || !TEST_int_gt(reseed_counter(s), sreseed))
1037 * When a prediction resistant generate is requested, the request
1038 * should be propagated to the master, reseeding the entire DRBG chain.
1040 sreseed = reseed_counter(s);
1041 if (!TEST_true(RAND_DRBG_generate(s, buf2, sizeof(buf2), 1, NULL, 0))
1042 || !TEST_int_gt(reseed_counter(m), mreseed)
1043 || !TEST_int_gt(reseed_counter(i), ireseed)
1044 || !TEST_int_gt(reseed_counter(s), sreseed)
1045 || !TEST_mem_ne(buf1, sizeof(buf1), buf2, sizeof(buf2)))
1048 /* Verify that a normal reseed still only reseeds the slave DRBG */
1049 inc_reseed_counter(i);
1050 mreseed = reseed_counter(m);
1051 ireseed = reseed_counter(i);
1052 sreseed = reseed_counter(s);
1053 if (!TEST_true(RAND_DRBG_reseed(s, NULL, 0, 0))
1054 || !TEST_int_eq(reseed_counter(m), mreseed)
1055 || !TEST_int_eq(reseed_counter(i), ireseed)
1056 || !TEST_int_gt(reseed_counter(s), sreseed))
1067 static int test_multi_set(void)
1070 RAND_DRBG *drbg = NULL;
1073 return TEST_skip("CRNGT cannot be disabled");
1075 /* init drbg with default CTR initializer */
1076 if (!TEST_ptr(drbg = RAND_DRBG_new(0, 0, NULL))
1077 || !TEST_true(disable_crngt(drbg)))
1079 /* change it to use hmac */
1080 if (!TEST_true(RAND_DRBG_set(drbg, NID_sha1, RAND_DRBG_FLAG_HMAC)))
1083 if (!TEST_true(RAND_DRBG_set(drbg, NID_sha1, RAND_DRBG_FLAG_HMAC)))
1085 /* change it to use hash */
1086 if (!TEST_true(RAND_DRBG_set(drbg, NID_sha256, 0)))
1089 if (!TEST_true(RAND_DRBG_set(drbg, NID_sha256, 0)))
1091 /* change it to use ctr */
1092 if (!TEST_true(RAND_DRBG_set(drbg, NID_aes_192_ctr, 0)))
1095 if (!TEST_true(RAND_DRBG_set(drbg, NID_aes_192_ctr, 0)))
1097 if (!TEST_int_gt(RAND_DRBG_instantiate(drbg, NULL, 0), 0))
1102 uninstantiate(drbg);
1103 RAND_DRBG_free(drbg);
1107 static int test_set_defaults(void)
1109 RAND_DRBG *master = NULL, *public = NULL, *private = NULL;
1111 /* Check the default type and flags for master, public and private */
1112 return TEST_ptr(master = RAND_DRBG_get0_master())
1113 && TEST_ptr(public = RAND_DRBG_get0_public())
1114 && TEST_ptr(private = RAND_DRBG_get0_private())
1115 && TEST_int_eq(master->type, RAND_DRBG_TYPE)
1116 && TEST_int_eq(master->flags,
1117 RAND_DRBG_FLAGS | RAND_DRBG_FLAG_MASTER)
1118 && TEST_int_eq(public->type, RAND_DRBG_TYPE)
1119 && TEST_int_eq(public->flags,
1120 RAND_DRBG_FLAGS | RAND_DRBG_FLAG_PUBLIC)
1121 && TEST_int_eq(private->type, RAND_DRBG_TYPE)
1122 && TEST_int_eq(private->flags,
1123 RAND_DRBG_FLAGS | RAND_DRBG_FLAG_PRIVATE)
1125 /* change master DRBG and check again */
1126 && TEST_true(RAND_DRBG_set_defaults(NID_sha256,
1127 RAND_DRBG_FLAG_MASTER))
1128 && TEST_true(RAND_DRBG_uninstantiate(master))
1129 && TEST_int_eq(master->type, NID_sha256)
1130 && TEST_int_eq(master->flags, RAND_DRBG_FLAG_MASTER)
1131 && TEST_int_eq(public->type, RAND_DRBG_TYPE)
1132 && TEST_int_eq(public->flags,
1133 RAND_DRBG_FLAGS | RAND_DRBG_FLAG_PUBLIC)
1134 && TEST_int_eq(private->type, RAND_DRBG_TYPE)
1135 && TEST_int_eq(private->flags,
1136 RAND_DRBG_FLAGS | RAND_DRBG_FLAG_PRIVATE)
1137 /* change private DRBG and check again */
1138 && TEST_true(RAND_DRBG_set_defaults(NID_sha256,
1139 RAND_DRBG_FLAG_PRIVATE|RAND_DRBG_FLAG_HMAC))
1140 && TEST_true(RAND_DRBG_uninstantiate(private))
1141 && TEST_int_eq(master->type, NID_sha256)
1142 && TEST_int_eq(master->flags, RAND_DRBG_FLAG_MASTER)
1143 && TEST_int_eq(public->type, RAND_DRBG_TYPE)
1144 && TEST_int_eq(public->flags,
1145 RAND_DRBG_FLAGS | RAND_DRBG_FLAG_PUBLIC)
1146 && TEST_int_eq(private->type, NID_sha256)
1147 && TEST_int_eq(private->flags,
1148 RAND_DRBG_FLAG_PRIVATE | RAND_DRBG_FLAG_HMAC)
1149 /* change public DRBG and check again */
1150 && TEST_true(RAND_DRBG_set_defaults(NID_sha1,
1151 RAND_DRBG_FLAG_PUBLIC
1152 | RAND_DRBG_FLAG_HMAC))
1153 && TEST_true(RAND_DRBG_uninstantiate(public))
1154 && TEST_int_eq(master->type, NID_sha256)
1155 && TEST_int_eq(master->flags, RAND_DRBG_FLAG_MASTER)
1156 && TEST_int_eq(public->type, NID_sha1)
1157 && TEST_int_eq(public->flags,
1158 RAND_DRBG_FLAG_PUBLIC | RAND_DRBG_FLAG_HMAC)
1159 && TEST_int_eq(private->type, NID_sha256)
1160 && TEST_int_eq(private->flags,
1161 RAND_DRBG_FLAG_PRIVATE | RAND_DRBG_FLAG_HMAC)
1162 /* Change DRBG defaults and change public and check again */
1163 && TEST_true(RAND_DRBG_set_defaults(NID_sha256, 0))
1164 && TEST_true(RAND_DRBG_uninstantiate(public))
1165 && TEST_int_eq(public->type, NID_sha256)
1166 && TEST_int_eq(public->flags, RAND_DRBG_FLAG_PUBLIC)
1168 /* FIPS mode doesn't support CTR DRBG without a derivation function */
1170 /* Change DRBG defaults and change master and check again */
1171 && TEST_true(RAND_DRBG_set_defaults(NID_aes_256_ctr,
1172 RAND_DRBG_FLAG_CTR_NO_DF))
1173 && TEST_true(RAND_DRBG_uninstantiate(master))
1174 && TEST_int_eq(master->type, NID_aes_256_ctr)
1175 && TEST_int_eq(master->flags,
1176 RAND_DRBG_FLAG_MASTER|RAND_DRBG_FLAG_CTR_NO_DF)
1178 /* Reset back to the standard defaults */
1179 && TEST_true(RAND_DRBG_set_defaults(RAND_DRBG_TYPE,
1181 | RAND_DRBG_FLAG_MASTER
1182 | RAND_DRBG_FLAG_PUBLIC
1183 | RAND_DRBG_FLAG_PRIVATE))
1184 && TEST_true(RAND_DRBG_uninstantiate(master))
1185 && TEST_true(RAND_DRBG_uninstantiate(public))
1186 && TEST_true(RAND_DRBG_uninstantiate(private));
1191 * A list of the FIPS DRGB types.
1192 * Because of the way HMAC DRGBs are implemented, both the NID and flags
1195 static const struct s_drgb_types {
1199 { NID_aes_128_ctr, 0 },
1200 { NID_aes_192_ctr, 0 },
1201 { NID_aes_256_ctr, 0 },
1207 { NID_sha512_224, 0 },
1208 { NID_sha512_256, 0 },
1209 { NID_sha3_224, 0 },
1210 { NID_sha3_256, 0 },
1211 { NID_sha3_384, 0 },
1212 { NID_sha3_512, 0 },
1213 { NID_sha1, RAND_DRBG_FLAG_HMAC },
1214 { NID_sha224, RAND_DRBG_FLAG_HMAC },
1215 { NID_sha256, RAND_DRBG_FLAG_HMAC },
1216 { NID_sha384, RAND_DRBG_FLAG_HMAC },
1217 { NID_sha512, RAND_DRBG_FLAG_HMAC },
1218 { NID_sha512_224, RAND_DRBG_FLAG_HMAC },
1219 { NID_sha512_256, RAND_DRBG_FLAG_HMAC },
1220 { NID_sha3_224, RAND_DRBG_FLAG_HMAC },
1221 { NID_sha3_256, RAND_DRBG_FLAG_HMAC },
1222 { NID_sha3_384, RAND_DRBG_FLAG_HMAC },
1223 { NID_sha3_512, RAND_DRBG_FLAG_HMAC },
1226 /* Six cases for each covers seed sizes up to 32 bytes */
1227 static const size_t crngt_num_cases = 6;
1229 static size_t crngt_case, crngt_idx;
1231 static int crngt_entropy_cb(OPENSSL_CTX *ctx, RAND_POOL *pool,
1232 unsigned char *buf, unsigned char *md,
1233 unsigned int *md_size)
1237 if (!TEST_int_lt(crngt_idx, crngt_num_cases))
1239 /* Generate a block of unique data unless this is the duplication point */
1241 if (z > 0 && crngt_case == z)
1243 for (i = 0; i < CRNGT_BUFSIZ; i++)
1244 buf[i] = (unsigned char)(i + 'A' + z);
1245 return EVP_Digest(buf, CRNGT_BUFSIZ, md, md_size, EVP_sha256(), NULL);
1249 int setup_tests(void)
1251 ADD_ALL_TESTS(test_kats, 1);
1252 ADD_ALL_TESTS(test_error_checks, OSSL_NELEM(drbg_test));
1253 ADD_TEST(test_rand_drbg_reseed);
1254 ADD_TEST(test_rand_drbg_prediction_resistance);
1255 ADD_TEST(test_multi_set);
1256 ADD_TEST(test_set_defaults);
1257 #if defined(OPENSSL_THREADS)
1258 ADD_TEST(test_multi_thread);