2 * Copyright 2011-2018 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 #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_local.h"
19 #include "../include/crypto/rand.h"
26 #if defined(OPENSSL_SYS_UNIX)
27 # include <sys/types.h>
28 # include <sys/wait.h>
35 typedef struct drbg_selftest_data_st {
40 /* KAT data for no PR */
41 const unsigned char *entropy;
43 const unsigned char *nonce;
45 const unsigned char *pers;
47 const unsigned char *adin;
49 const unsigned char *entropyreseed;
50 size_t entropyreseedlen;
51 const unsigned char *adinreseed;
53 const unsigned char *adin2;
55 const unsigned char *expected;
57 const unsigned char *kat2;
61 const unsigned char *entropy_pr;
63 const unsigned char *nonce_pr;
65 const unsigned char *pers_pr;
67 const unsigned char *adin_pr;
69 const unsigned char *entropypr_pr;
70 size_t entropyprlen_pr;
71 const unsigned char *ading_pr;
73 const unsigned char *entropyg_pr;
74 size_t entropyglen_pr;
75 const unsigned char *kat_pr;
77 const unsigned char *kat2_pr;
81 #define make_drbg_test_data(nid, flag, pr, post) {\
83 pr##_entropyinput, sizeof(pr##_entropyinput), \
84 pr##_nonce, sizeof(pr##_nonce), \
85 pr##_personalizationstring, sizeof(pr##_personalizationstring), \
86 pr##_additionalinput, sizeof(pr##_additionalinput), \
87 pr##_entropyinputreseed, sizeof(pr##_entropyinputreseed), \
88 pr##_additionalinputreseed, sizeof(pr##_additionalinputreseed), \
89 pr##_additionalinput2, sizeof(pr##_additionalinput2), \
90 pr##_int_returnedbits, sizeof(pr##_int_returnedbits), \
91 pr##_returnedbits, sizeof(pr##_returnedbits), \
92 pr##_pr_entropyinput, sizeof(pr##_pr_entropyinput), \
93 pr##_pr_nonce, sizeof(pr##_pr_nonce), \
94 pr##_pr_personalizationstring, sizeof(pr##_pr_personalizationstring), \
95 pr##_pr_additionalinput, sizeof(pr##_pr_additionalinput), \
96 pr##_pr_entropyinputpr, sizeof(pr##_pr_entropyinputpr), \
97 pr##_pr_additionalinput2, sizeof(pr##_pr_additionalinput2), \
98 pr##_pr_entropyinputpr2, sizeof(pr##_pr_entropyinputpr2), \
99 pr##_pr_int_returnedbits, sizeof(pr##_pr_int_returnedbits), \
100 pr##_pr_returnedbits, sizeof(pr##_pr_returnedbits) \
103 #define make_drbg_test_data_use_df(nid, pr, p) \
104 make_drbg_test_data(nid, 0, pr, p)
106 #define make_drbg_test_data_no_df(nid, pr, p) \
107 make_drbg_test_data(nid, RAND_DRBG_FLAG_CTR_NO_DF, pr, p)
109 #define make_drbg_test_data_hash(nid, pr, p) \
110 make_drbg_test_data(nid, RAND_DRBG_FLAG_HMAC, hmac_##pr, p), \
111 make_drbg_test_data(nid, 0, pr, p)
113 static DRBG_SELFTEST_DATA drbg_test[] = {
115 /* FIPS mode doesn't support CTR DRBG without a derivation function */
116 make_drbg_test_data_no_df (NID_aes_128_ctr, aes_128_no_df, 0),
117 make_drbg_test_data_no_df (NID_aes_192_ctr, aes_192_no_df, 0),
118 make_drbg_test_data_no_df (NID_aes_256_ctr, aes_256_no_df, 1),
120 make_drbg_test_data_use_df(NID_aes_128_ctr, aes_128_use_df, 0),
121 make_drbg_test_data_use_df(NID_aes_192_ctr, aes_192_use_df, 0),
122 make_drbg_test_data_use_df(NID_aes_256_ctr, aes_256_use_df, 1),
123 make_drbg_test_data_hash(NID_sha1, sha1, 0),
124 make_drbg_test_data_hash(NID_sha224, sha224, 0),
125 make_drbg_test_data_hash(NID_sha256, sha256, 1),
126 make_drbg_test_data_hash(NID_sha384, sha384, 0),
127 make_drbg_test_data_hash(NID_sha512, sha512, 0),
131 * Test context data, attached as EXDATA to the RAND_DRBG
133 typedef struct test_ctx_st {
134 const unsigned char *entropy;
137 const unsigned char *nonce;
142 static size_t kat_entropy(RAND_DRBG *drbg, unsigned char **pout,
143 int entropy, size_t min_len, size_t max_len,
144 int prediction_resistance)
146 TEST_CTX *t = (TEST_CTX *)RAND_DRBG_get_callback_data(drbg);
149 *pout = (unsigned char *)t->entropy;
150 return t->entropylen;
153 static size_t kat_nonce(RAND_DRBG *drbg, unsigned char **pout,
154 int entropy, size_t min_len, size_t max_len)
156 TEST_CTX *t = (TEST_CTX *)RAND_DRBG_get_callback_data(drbg);
159 *pout = (unsigned char *)t->nonce;
164 * Disable CRNG testing if it is enabled.
165 * If the DRBG is ready or in an error state, this means an instantiate cycle
166 * for which the default personalisation string is used.
168 static int disable_crngt(RAND_DRBG *drbg)
170 static const char pers[] = DRBG_DEFAULT_PERS_STRING;
171 const int instantiate = drbg->state != DRBG_UNINITIALISED;
173 if (drbg->get_entropy != rand_crngt_get_entropy)
176 if ((instantiate && !RAND_DRBG_uninstantiate(drbg))
177 || !TEST_true(RAND_DRBG_set_callbacks(drbg, &rand_drbg_get_entropy,
178 &rand_drbg_cleanup_entropy,
179 &rand_drbg_get_nonce,
180 &rand_drbg_cleanup_nonce))
182 && !RAND_DRBG_instantiate(drbg, (const unsigned char *)pers,
188 static int uninstantiate(RAND_DRBG *drbg)
190 int ret = drbg == NULL ? 1 : RAND_DRBG_uninstantiate(drbg);
197 * Do a single KAT test. Return 0 on failure.
199 static int single_kat(DRBG_SELFTEST_DATA *td)
201 RAND_DRBG *drbg = NULL;
204 unsigned char buff[1024];
207 * Test without PR: Instantiate DRBG with test entropy, nonce and
208 * personalisation string.
210 if (!TEST_ptr(drbg = RAND_DRBG_new(td->nid, td->flags, NULL)))
212 if (!TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
214 || !TEST_true(RAND_DRBG_set_callback_data(drbg, &t))
215 || !TEST_true(disable_crngt(drbg))) {
219 memset(&t, 0, sizeof(t));
220 t.entropy = td->entropy;
221 t.entropylen = td->entropylen;
223 t.noncelen = td->noncelen;
225 if (!TEST_true(RAND_DRBG_instantiate(drbg, td->pers, td->perslen))
226 || !TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
227 td->adin, td->adinlen))
228 || !TEST_mem_eq(td->expected, td->exlen, buff, td->exlen))
231 /* Reseed DRBG with test entropy and additional input */
232 t.entropy = td->entropyreseed;
233 t.entropylen = td->entropyreseedlen;
234 if (!TEST_true(RAND_DRBG_reseed(drbg, td->adinreseed, td->adinreseedlen, 0)
235 || !TEST_true(RAND_DRBG_generate(drbg, buff, td->kat2len, 0,
236 td->adin2, td->adin2len))
237 || !TEST_mem_eq(td->kat2, td->kat2len, buff, td->kat2len)))
242 * Now test with PR: Instantiate DRBG with test entropy, nonce and
243 * personalisation string.
245 if (!TEST_true(RAND_DRBG_set(drbg, td->nid, td->flags))
246 || !TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
248 || !TEST_true(RAND_DRBG_set_callback_data(drbg, &t)))
250 t.entropy = td->entropy_pr;
251 t.entropylen = td->entropylen_pr;
252 t.nonce = td->nonce_pr;
253 t.noncelen = td->noncelen_pr;
256 if (!TEST_true(RAND_DRBG_instantiate(drbg, td->pers_pr, td->perslen_pr)))
260 * Now generate with PR: we need to supply entropy as this will
261 * perform a reseed operation.
263 t.entropy = td->entropypr_pr;
264 t.entropylen = td->entropyprlen_pr;
265 if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->katlen_pr, 1,
266 td->adin_pr, td->adinlen_pr))
267 || !TEST_mem_eq(td->kat_pr, td->katlen_pr, buff, td->katlen_pr))
271 * Now generate again with PR: supply new entropy again.
273 t.entropy = td->entropyg_pr;
274 t.entropylen = td->entropyglen_pr;
276 if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->kat2len_pr, 1,
277 td->ading_pr, td->adinglen_pr))
278 || !TEST_mem_eq(td->kat2_pr, td->kat2len_pr,
279 buff, td->kat2len_pr))
284 RAND_DRBG_free(drbg);
285 return failures == 0;
289 * Initialise a DRBG based on selftest data
291 static int init(RAND_DRBG *drbg, DRBG_SELFTEST_DATA *td, TEST_CTX *t)
293 if (!TEST_true(RAND_DRBG_set(drbg, td->nid, td->flags))
294 || !TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
297 RAND_DRBG_set_callback_data(drbg, t);
298 t->entropy = td->entropy;
299 t->entropylen = td->entropylen;
300 t->nonce = td->nonce;
301 t->noncelen = td->noncelen;
308 * Initialise and instantiate DRBG based on selftest data
310 static int instantiate(RAND_DRBG *drbg, DRBG_SELFTEST_DATA *td,
313 if (!TEST_true(init(drbg, td, t))
314 || !TEST_true(RAND_DRBG_instantiate(drbg, td->pers, td->perslen)))
320 * Perform extensive error checking as required by SP800-90.
321 * Induce several failure modes and check an error condition is set.
323 static int error_check(DRBG_SELFTEST_DATA *td)
325 static char zero[sizeof(RAND_DRBG)];
326 RAND_DRBG *drbg = NULL;
328 unsigned char buff[1024];
329 unsigned int reseed_counter_tmp;
332 if (!TEST_ptr(drbg = RAND_DRBG_new(td->nid, td->flags, NULL))
333 || !TEST_true(disable_crngt(drbg)))
337 * Personalisation string tests
340 /* Test detection of too large personalisation string */
341 if (!init(drbg, td, &t)
342 || RAND_DRBG_instantiate(drbg, td->pers, drbg->max_perslen + 1) > 0)
346 * Entropy source tests
349 /* Test entropy source failure detection: i.e. returns no data */
351 if (TEST_int_le(RAND_DRBG_instantiate(drbg, td->pers, td->perslen), 0))
354 /* Try to generate output from uninstantiated DRBG */
355 if (!TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
356 td->adin, td->adinlen))
357 || !uninstantiate(drbg))
360 /* Test insufficient entropy */
361 t.entropylen = drbg->min_entropylen - 1;
362 if (!init(drbg, td, &t)
363 || RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0
364 || !uninstantiate(drbg))
367 /* Test too much entropy */
368 t.entropylen = drbg->max_entropylen + 1;
369 if (!init(drbg, td, &t)
370 || RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0
371 || !uninstantiate(drbg))
378 /* Test too small nonce */
379 if (drbg->min_noncelen) {
380 t.noncelen = drbg->min_noncelen - 1;
381 if (!init(drbg, td, &t)
382 || RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0
383 || !uninstantiate(drbg))
387 /* Test too large nonce */
388 if (drbg->max_noncelen) {
389 t.noncelen = drbg->max_noncelen + 1;
390 if (!init(drbg, td, &t)
391 || RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0
392 || !uninstantiate(drbg))
396 /* Instantiate with valid data, Check generation is now OK */
397 if (!instantiate(drbg, td, &t)
398 || !TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
399 td->adin, td->adinlen)))
402 /* Request too much data for one request */
403 if (!TEST_false(RAND_DRBG_generate(drbg, buff, drbg->max_request + 1, 0,
404 td->adin, td->adinlen)))
407 /* Try too large additional input */
408 if (!TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
409 td->adin, drbg->max_adinlen + 1)))
413 * Check prediction resistance request fails if entropy source
417 if (TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 1,
418 td->adin, td->adinlen))
419 || !uninstantiate(drbg))
422 /* Instantiate again with valid data */
423 if (!instantiate(drbg, td, &t))
425 reseed_counter_tmp = drbg->reseed_gen_counter;
426 drbg->reseed_gen_counter = drbg->reseed_interval;
428 /* Generate output and check entropy has been requested for reseed */
430 if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
431 td->adin, td->adinlen))
432 || !TEST_int_eq(t.entropycnt, 1)
433 || !TEST_int_eq(drbg->reseed_gen_counter, reseed_counter_tmp + 1)
434 || !uninstantiate(drbg))
438 * Check prediction resistance request fails if entropy source
442 if (!TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 1,
443 td->adin, td->adinlen))
444 || !uninstantiate(drbg))
447 /* Test reseed counter works */
448 if (!instantiate(drbg, td, &t))
450 reseed_counter_tmp = drbg->reseed_gen_counter;
451 drbg->reseed_gen_counter = drbg->reseed_interval;
453 /* Generate output and check entropy has been requested for reseed */
455 if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
456 td->adin, td->adinlen))
457 || !TEST_int_eq(t.entropycnt, 1)
458 || !TEST_int_eq(drbg->reseed_gen_counter, reseed_counter_tmp + 1)
459 || !uninstantiate(drbg))
463 * Explicit reseed tests
466 /* Test explicit reseed with too large additional input */
467 if (!instantiate(drbg, td, &t)
468 || RAND_DRBG_reseed(drbg, td->adin, drbg->max_adinlen + 1, 0) > 0)
471 /* Test explicit reseed with entropy source failure */
473 if (!TEST_int_le(RAND_DRBG_reseed(drbg, td->adin, td->adinlen, 0), 0)
474 || !uninstantiate(drbg))
477 /* Test explicit reseed with too much entropy */
478 if (!instantiate(drbg, td, &t))
480 t.entropylen = drbg->max_entropylen + 1;
481 if (!TEST_int_le(RAND_DRBG_reseed(drbg, td->adin, td->adinlen, 0), 0)
482 || !uninstantiate(drbg))
485 /* Test explicit reseed with too little entropy */
486 if (!instantiate(drbg, td, &t))
488 t.entropylen = drbg->min_entropylen - 1;
489 if (!TEST_int_le(RAND_DRBG_reseed(drbg, td->adin, td->adinlen, 0), 0)
490 || !uninstantiate(drbg))
493 /* Standard says we have to check uninstantiate really zeroes */
494 if (!TEST_mem_eq(zero, sizeof(drbg->data), &drbg->data, sizeof(drbg->data)))
501 RAND_DRBG_free(drbg);
505 static int test_kats(int i)
507 DRBG_SELFTEST_DATA *td = &drbg_test[i];
518 static int test_error_checks(int i)
520 DRBG_SELFTEST_DATA *td = &drbg_test[i];
532 * Hook context data, attached as EXDATA to the RAND_DRBG
534 typedef struct hook_ctx_st {
537 * Currently, all DRBGs use the same get_entropy() callback.
538 * The tests however, don't assume this and store
539 * the original callback for every DRBG separately.
541 RAND_DRBG_get_entropy_fn get_entropy;
542 /* forces a failure of the get_entropy() call if nonzero */
544 /* counts successful reseeds */
548 static HOOK_CTX master_ctx, public_ctx, private_ctx;
550 static HOOK_CTX *get_hook_ctx(RAND_DRBG *drbg)
552 return (HOOK_CTX *)RAND_DRBG_get_callback_data(drbg);
555 /* Intercepts and counts calls to the get_entropy() callback */
556 static size_t get_entropy_hook(RAND_DRBG *drbg, unsigned char **pout,
557 int entropy, size_t min_len, size_t max_len,
558 int prediction_resistance)
561 HOOK_CTX *ctx = get_hook_ctx(drbg);
566 ret = ctx->get_entropy(drbg, pout, entropy, min_len, max_len,
567 prediction_resistance);
574 /* Installs a hook for the get_entropy() callback of the given drbg */
575 static void hook_drbg(RAND_DRBG *drbg, HOOK_CTX *ctx)
577 memset(ctx, 0, sizeof(*ctx));
579 ctx->get_entropy = drbg->get_entropy;
582 * We can't use the public API here, since it prohibits modifying
583 * the callbacks or the callback data of chained DRBGs.
585 drbg->get_entropy = get_entropy_hook;
586 drbg->callback_data = ctx;
589 /* Installs the hook for the get_entropy() callback of the given drbg */
590 static void unhook_drbg(RAND_DRBG *drbg)
592 HOOK_CTX *ctx = drbg->callback_data;
595 drbg->get_entropy = ctx->get_entropy;
598 /* Resets the given hook context */
599 static void reset_hook_ctx(HOOK_CTX *ctx)
602 ctx->reseed_count = 0;
605 /* Resets all drbg hook contexts */
606 static void reset_drbg_hook_ctx(void)
608 reset_hook_ctx(&master_ctx);
609 reset_hook_ctx(&public_ctx);
610 reset_hook_ctx(&private_ctx);
614 * Generates random output using RAND_bytes() and RAND_priv_bytes()
615 * and checks whether the three shared DRBGs were reseeded as
618 * |expect_success|: expected outcome (as reported by RAND_status())
619 * |master|, |public|, |private|: pointers to the three shared DRBGs
620 * |expect_xxx_reseed| =
621 * 1: it is expected that the specified DRBG is reseeded
622 * 0: it is expected that the specified DRBG is not reseeded
623 * -1: don't check whether the specified DRBG was reseeded or not
624 * |reseed_time|: if nonzero, used instead of time(NULL) to set the
625 * |before_reseed| time.
627 static int test_drbg_reseed(int expect_success,
631 int expect_master_reseed,
632 int expect_public_reseed,
633 int expect_private_reseed,
637 unsigned char buf[32];
638 time_t before_reseed, after_reseed;
639 int expected_state = (expect_success ? DRBG_READY : DRBG_ERROR);
642 * step 1: check preconditions
645 /* Test whether seed propagation is enabled */
646 if (!TEST_int_ne(master->reseed_prop_counter, 0)
647 || !TEST_int_ne(public->reseed_prop_counter, 0)
648 || !TEST_int_ne(private->reseed_prop_counter, 0))
651 /* Check whether the master DRBG's reseed counter is the largest one */
652 if (!TEST_int_le(public->reseed_prop_counter, master->reseed_prop_counter)
653 || !TEST_int_le(private->reseed_prop_counter, master->reseed_prop_counter))
657 * step 2: generate random output
660 if (reseed_time == 0)
661 reseed_time = time(NULL);
663 /* Generate random output from the public and private DRBG */
664 before_reseed = expect_master_reseed == 1 ? reseed_time : 0;
665 if (!TEST_int_eq(RAND_bytes(buf, sizeof(buf)), expect_success)
666 || !TEST_int_eq(RAND_priv_bytes(buf, sizeof(buf)), expect_success))
668 after_reseed = time(NULL);
672 * step 3: check postconditions
675 /* Test whether reseeding succeeded as expected */
676 if (!TEST_int_eq(master->state, expected_state)
677 || !TEST_int_eq(public->state, expected_state)
678 || !TEST_int_eq(private->state, expected_state))
681 if (expect_master_reseed >= 0) {
682 /* Test whether master DRBG was reseeded as expected */
683 if (!TEST_int_eq(master_ctx.reseed_count, expect_master_reseed))
687 if (expect_public_reseed >= 0) {
688 /* Test whether public DRBG was reseeded as expected */
689 if (!TEST_int_eq(public_ctx.reseed_count, expect_public_reseed))
693 if (expect_private_reseed >= 0) {
694 /* Test whether public DRBG was reseeded as expected */
695 if (!TEST_int_eq(private_ctx.reseed_count, expect_private_reseed))
699 if (expect_success == 1) {
700 /* Test whether all three reseed counters are synchronized */
701 if (!TEST_int_eq(public->reseed_prop_counter, master->reseed_prop_counter)
702 || !TEST_int_eq(private->reseed_prop_counter, master->reseed_prop_counter))
705 /* Test whether reseed time of master DRBG is set correctly */
706 if (!TEST_time_t_le(before_reseed, master->reseed_time)
707 || !TEST_time_t_le(master->reseed_time, after_reseed))
710 /* Test whether reseed times of child DRBGs are synchronized with master */
711 if (!TEST_time_t_ge(public->reseed_time, master->reseed_time)
712 || !TEST_time_t_ge(private->reseed_time, master->reseed_time))
722 #if defined(OPENSSL_SYS_UNIX)
724 * Test whether master, public and private DRBG are reseeded after
725 * forking the process.
727 static int test_drbg_reseed_after_fork(RAND_DRBG *master,
735 if (!TEST_int_ge(pid, 0))
739 /* I'm the parent; wait for the child and check its exit code */
740 return TEST_int_eq(waitpid(pid, &status, 0), pid) && TEST_int_eq(status, 0);
743 /* I'm the child; check whether all three DRBGs reseed. */
744 if (!TEST_true(test_drbg_reseed(1, master, public, private, 1, 1, 1, 0)))
750 unhook_drbg(private);
756 * Test whether the default rand_method (RAND_OpenSSL()) is
757 * setup correctly, in particular whether reseeding works
760 static int test_rand_drbg_reseed(void)
762 RAND_DRBG *master, *public, *private;
763 unsigned char rand_add_buf[256];
765 time_t before_reseed;
767 /* Check whether RAND_OpenSSL() is the default method */
768 if (!TEST_ptr_eq(RAND_get_rand_method(), RAND_OpenSSL()))
771 /* All three DRBGs should be non-null */
772 if (!TEST_ptr(master = RAND_DRBG_get0_master())
773 || !TEST_ptr(public = RAND_DRBG_get0_public())
774 || !TEST_ptr(private = RAND_DRBG_get0_private()))
777 /* There should be three distinct DRBGs, two of them chained to master */
778 if (!TEST_ptr_ne(public, private)
779 || !TEST_ptr_ne(public, master)
780 || !TEST_ptr_ne(private, master)
781 || !TEST_ptr_eq(public->parent, master)
782 || !TEST_ptr_eq(private->parent, master))
785 /* Disable CRNG testing for the master DRBG */
786 if (!TEST_true(disable_crngt(master)))
789 /* uninstantiate the three global DRBGs */
790 RAND_DRBG_uninstantiate(private);
791 RAND_DRBG_uninstantiate(public);
792 RAND_DRBG_uninstantiate(master);
795 /* Install hooks for the following tests */
796 hook_drbg(master, &master_ctx);
797 hook_drbg(public, &public_ctx);
798 hook_drbg(private, &private_ctx);
802 * Test initial seeding of shared DRBGs
804 if (!TEST_true(test_drbg_reseed(1, master, public, private, 1, 1, 1, 0)))
806 reset_drbg_hook_ctx();
810 * Test initial state of shared DRBGs
812 if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 0, 0, 0)))
814 reset_drbg_hook_ctx();
817 * Test whether the public and private DRBG are both reseeded when their
818 * reseed counters differ from the master's reseed counter.
820 master->reseed_prop_counter++;
821 if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 1, 1, 0)))
823 reset_drbg_hook_ctx();
826 * Test whether the public DRBG is reseeded when its reseed counter differs
827 * from the master's reseed counter.
829 master->reseed_prop_counter++;
830 private->reseed_prop_counter++;
831 if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 1, 0, 0)))
833 reset_drbg_hook_ctx();
836 * Test whether the private DRBG is reseeded when its reseed counter differs
837 * from the master's reseed counter.
839 master->reseed_prop_counter++;
840 public->reseed_prop_counter++;
841 if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 0, 1, 0)))
843 reset_drbg_hook_ctx();
845 #if defined(OPENSSL_SYS_UNIX)
846 if (!TEST_true(test_drbg_reseed_after_fork(master, public, private)))
850 /* fill 'randomness' buffer with some arbitrary data */
851 memset(rand_add_buf, 'r', sizeof(rand_add_buf));
855 * Test whether all three DRBGs are reseeded by RAND_add().
856 * The before_reseed time has to be measured here and passed into the
857 * test_drbg_reseed() test, because the master DRBG gets already reseeded
858 * in RAND_add(), whence the check for the condition
859 * before_reseed <= master->reseed_time will fail if the time value happens
860 * to increase between the RAND_add() and the test_drbg_reseed() call.
862 before_reseed = time(NULL);
863 RAND_add(rand_add_buf, sizeof(rand_add_buf), sizeof(rand_add_buf));
864 if (!TEST_true(test_drbg_reseed(1, master, public, private, 1, 1, 1,
867 reset_drbg_hook_ctx();
871 * Test whether none of the DRBGs is reseed if the master fails to reseed
874 master->reseed_prop_counter++;
875 RAND_add(rand_add_buf, sizeof(rand_add_buf), sizeof(rand_add_buf));
876 if (!TEST_true(test_drbg_reseed(0, master, public, private, 0, 0, 0, 0)))
878 reset_drbg_hook_ctx();
879 #else /* FIPS_MODE */
881 * In FIPS mode, random data provided by the application via RAND_add()
882 * is not considered a trusted entropy source. It is only treated as
883 * additional_data and no reseeding is forced. This test assures that
884 * no reseeding occurs.
886 before_reseed = time(NULL);
887 RAND_add(rand_add_buf, sizeof(rand_add_buf), sizeof(rand_add_buf));
888 if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 0, 0,
891 reset_drbg_hook_ctx();
900 unhook_drbg(private);
905 #if defined(OPENSSL_THREADS)
906 static int multi_thread_rand_bytes_succeeded = 1;
907 static int multi_thread_rand_priv_bytes_succeeded = 1;
909 static void run_multi_thread_test(void)
911 unsigned char buf[256];
912 time_t start = time(NULL);
913 RAND_DRBG *public = NULL, *private = NULL;
915 if (!TEST_ptr(public = RAND_DRBG_get0_public())
916 || !TEST_ptr(private = RAND_DRBG_get0_private())) {
917 multi_thread_rand_bytes_succeeded = 0;
920 RAND_DRBG_set_reseed_time_interval(private, 1);
921 RAND_DRBG_set_reseed_time_interval(public, 1);
924 if (RAND_bytes(buf, sizeof(buf)) <= 0)
925 multi_thread_rand_bytes_succeeded = 0;
926 if (RAND_priv_bytes(buf, sizeof(buf)) <= 0)
927 multi_thread_rand_priv_bytes_succeeded = 0;
929 while(time(NULL) - start < 5);
932 # if defined(OPENSSL_SYS_WINDOWS)
934 typedef HANDLE thread_t;
936 static DWORD WINAPI thread_run(LPVOID arg)
938 run_multi_thread_test();
940 * Because we're linking with a static library, we must stop each
941 * thread explicitly, or so says OPENSSL_thread_stop(3)
943 OPENSSL_thread_stop();
947 static int run_thread(thread_t *t)
949 *t = CreateThread(NULL, 0, thread_run, NULL, 0, NULL);
953 static int wait_for_thread(thread_t thread)
955 return WaitForSingleObject(thread, INFINITE) == 0;
960 typedef pthread_t thread_t;
962 static void *thread_run(void *arg)
964 run_multi_thread_test();
966 * Because we're linking with a static library, we must stop each
967 * thread explicitly, or so says OPENSSL_thread_stop(3)
969 OPENSSL_thread_stop();
973 static int run_thread(thread_t *t)
975 return pthread_create(t, NULL, thread_run, NULL) == 0;
978 static int wait_for_thread(thread_t thread)
980 return pthread_join(thread, NULL) == 0;
986 * The main thread will also run the test, so we'll have THREADS+1 parallel
991 static int test_multi_thread(void)
996 for (i = 0; i < THREADS; i++)
998 run_multi_thread_test();
999 for (i = 0; i < THREADS; i++)
1000 wait_for_thread(t[i]);
1002 if (!TEST_true(multi_thread_rand_bytes_succeeded))
1004 if (!TEST_true(multi_thread_rand_priv_bytes_succeeded))
1012 * Test that instantiation with RAND_seed() works as expected
1014 * If no os entropy source is available then RAND_seed(buffer, bufsize)
1015 * is expected to succeed if and only if the buffer length is at least
1016 * rand_drbg_seedlen(master) bytes.
1018 * If an os entropy source is available then RAND_seed(buffer, bufsize)
1019 * is expected to succeed always.
1021 static int test_rand_seed(void)
1023 RAND_DRBG *master = NULL;
1024 unsigned char rand_buf[256];
1026 size_t required_seed_buflen = 0;
1028 if (!TEST_ptr(master = RAND_DRBG_get0_master())
1029 || !TEST_true(disable_crngt(master)))
1032 #ifdef OPENSSL_RAND_SEED_NONE
1033 required_seed_buflen = rand_drbg_seedlen(master);
1036 memset(rand_buf, 0xCD, sizeof(rand_buf));
1038 for ( rand_buflen = 256 ; rand_buflen > 0 ; --rand_buflen ) {
1039 RAND_DRBG_uninstantiate(master);
1040 RAND_seed(rand_buf, rand_buflen);
1042 if (!TEST_int_eq(RAND_status(),
1043 (rand_buflen >= required_seed_buflen)))
1051 * Test that adding additional data with RAND_add() works as expected
1052 * when the master DRBG is instantiated (and below its reseed limit).
1054 * This should succeed regardless of whether an os entropy source is
1057 static int test_rand_add(void)
1059 unsigned char rand_buf[256];
1062 memset(rand_buf, 0xCD, sizeof(rand_buf));
1064 /* make sure it's instantiated */
1065 RAND_seed(rand_buf, sizeof(rand_buf));
1066 if (!TEST_true(RAND_status()))
1069 for ( rand_buflen = 256 ; rand_buflen > 0 ; --rand_buflen ) {
1070 RAND_add(rand_buf, rand_buflen, 0.0);
1071 if (!TEST_true(RAND_status()))
1078 static int test_rand_drbg_prediction_resistance(void)
1080 RAND_DRBG *m = NULL, *i = NULL, *s = NULL;
1081 unsigned char buf1[51], buf2[sizeof(buf1)];
1082 int ret = 0, mreseed, ireseed, sreseed;
1084 /* Initialise a three long DRBG chain */
1085 if (!TEST_ptr(m = RAND_DRBG_new(0, 0, NULL))
1086 || !TEST_true(disable_crngt(m))
1087 || !TEST_true(RAND_DRBG_instantiate(m, NULL, 0))
1088 || !TEST_ptr(i = RAND_DRBG_new(0, 0, m))
1089 || !TEST_true(RAND_DRBG_instantiate(i, NULL, 0))
1090 || !TEST_ptr(s = RAND_DRBG_new(0, 0, i))
1091 || !TEST_true(RAND_DRBG_instantiate(s, NULL, 0)))
1094 /* During a normal reseed, only the slave DRBG should be reseed */
1095 mreseed = ++m->reseed_prop_counter;
1096 ireseed = ++i->reseed_prop_counter;
1097 sreseed = s->reseed_prop_counter;
1098 if (!TEST_true(RAND_DRBG_reseed(s, NULL, 0, 0))
1099 || !TEST_int_eq(m->reseed_prop_counter, mreseed)
1100 || !TEST_int_eq(i->reseed_prop_counter, ireseed)
1101 || !TEST_int_gt(s->reseed_prop_counter, sreseed))
1105 * When prediction resistance is requested, the request should be
1106 * propagated to the master, so that the entire DRBG chain reseeds.
1108 sreseed = s->reseed_prop_counter;
1109 if (!TEST_true(RAND_DRBG_reseed(s, NULL, 0, 1))
1110 || !TEST_int_gt(m->reseed_prop_counter, mreseed)
1111 || !TEST_int_gt(i->reseed_prop_counter, ireseed)
1112 || !TEST_int_gt(s->reseed_prop_counter, sreseed))
1115 /* During a normal generate, only the slave DRBG should be reseed */
1116 mreseed = ++m->reseed_prop_counter;
1117 ireseed = ++i->reseed_prop_counter;
1118 sreseed = s->reseed_prop_counter;
1119 if (!TEST_true(RAND_DRBG_generate(s, buf1, sizeof(buf1), 0, NULL, 0))
1120 || !TEST_int_eq(m->reseed_prop_counter, mreseed)
1121 || !TEST_int_eq(i->reseed_prop_counter, ireseed)
1122 || !TEST_int_gt(s->reseed_prop_counter, sreseed))
1126 * When a prediction resistant generate is requested, the request
1127 * should be propagated to the master, reseeding the entire DRBG chain.
1129 sreseed = s->reseed_prop_counter;
1130 if (!TEST_true(RAND_DRBG_generate(s, buf2, sizeof(buf2), 1, NULL, 0))
1131 || !TEST_int_gt(m->reseed_prop_counter, mreseed)
1132 || !TEST_int_gt(i->reseed_prop_counter, ireseed)
1133 || !TEST_int_gt(s->reseed_prop_counter, sreseed)
1134 || !TEST_mem_ne(buf1, sizeof(buf1), buf2, sizeof(buf2)))
1137 /* Verify that a normal reseed still only reseeds the slave DRBG */
1138 mreseed = ++m->reseed_prop_counter;
1139 ireseed = ++i->reseed_prop_counter;
1140 sreseed = s->reseed_prop_counter;
1141 if (!TEST_true(RAND_DRBG_reseed(s, NULL, 0, 0))
1142 || !TEST_int_eq(m->reseed_prop_counter, mreseed)
1143 || !TEST_int_eq(i->reseed_prop_counter, ireseed)
1144 || !TEST_int_gt(s->reseed_prop_counter, sreseed))
1155 static int test_multi_set(void)
1158 RAND_DRBG *drbg = NULL;
1160 /* init drbg with default CTR initializer */
1161 if (!TEST_ptr(drbg = RAND_DRBG_new(0, 0, NULL))
1162 || !TEST_true(disable_crngt(drbg)))
1164 /* change it to use hmac */
1165 if (!TEST_true(RAND_DRBG_set(drbg, NID_sha1, RAND_DRBG_FLAG_HMAC)))
1168 if (!TEST_true(RAND_DRBG_set(drbg, NID_sha1, RAND_DRBG_FLAG_HMAC)))
1170 /* change it to use hash */
1171 if (!TEST_true(RAND_DRBG_set(drbg, NID_sha256, 0)))
1174 if (!TEST_true(RAND_DRBG_set(drbg, NID_sha256, 0)))
1176 /* change it to use ctr */
1177 if (!TEST_true(RAND_DRBG_set(drbg, NID_aes_192_ctr, 0)))
1180 if (!TEST_true(RAND_DRBG_set(drbg, NID_aes_192_ctr, 0)))
1182 if (!TEST_int_gt(RAND_DRBG_instantiate(drbg, NULL, 0), 0))
1187 uninstantiate(drbg);
1188 RAND_DRBG_free(drbg);
1192 static int test_set_defaults(void)
1194 RAND_DRBG *master = NULL, *public = NULL, *private = NULL;
1196 /* Check the default type and flags for master, public and private */
1197 return TEST_ptr(master = RAND_DRBG_get0_master())
1198 && TEST_ptr(public = RAND_DRBG_get0_public())
1199 && TEST_ptr(private = RAND_DRBG_get0_private())
1200 && TEST_int_eq(master->type, RAND_DRBG_TYPE)
1201 && TEST_int_eq(master->flags,
1202 RAND_DRBG_FLAGS | RAND_DRBG_FLAG_MASTER)
1203 && TEST_int_eq(public->type, RAND_DRBG_TYPE)
1204 && TEST_int_eq(public->flags,
1205 RAND_DRBG_FLAGS | RAND_DRBG_FLAG_PUBLIC)
1206 && TEST_int_eq(private->type, RAND_DRBG_TYPE)
1207 && TEST_int_eq(private->flags,
1208 RAND_DRBG_FLAGS | RAND_DRBG_FLAG_PRIVATE)
1210 /* change master DRBG and check again */
1211 && TEST_true(RAND_DRBG_set_defaults(NID_sha256,
1212 RAND_DRBG_FLAG_MASTER))
1213 && TEST_true(RAND_DRBG_uninstantiate(master))
1214 && TEST_int_eq(master->type, NID_sha256)
1215 && TEST_int_eq(master->flags, RAND_DRBG_FLAG_MASTER)
1216 && TEST_int_eq(public->type, RAND_DRBG_TYPE)
1217 && TEST_int_eq(public->flags,
1218 RAND_DRBG_FLAGS | RAND_DRBG_FLAG_PUBLIC)
1219 && TEST_int_eq(private->type, RAND_DRBG_TYPE)
1220 && TEST_int_eq(private->flags,
1221 RAND_DRBG_FLAGS | RAND_DRBG_FLAG_PRIVATE)
1222 /* change private DRBG and check again */
1223 && TEST_true(RAND_DRBG_set_defaults(NID_sha256,
1224 RAND_DRBG_FLAG_PRIVATE|RAND_DRBG_FLAG_HMAC))
1225 && TEST_true(RAND_DRBG_uninstantiate(private))
1226 && TEST_int_eq(master->type, NID_sha256)
1227 && TEST_int_eq(master->flags, RAND_DRBG_FLAG_MASTER)
1228 && TEST_int_eq(public->type, RAND_DRBG_TYPE)
1229 && TEST_int_eq(public->flags,
1230 RAND_DRBG_FLAGS | RAND_DRBG_FLAG_PUBLIC)
1231 && TEST_int_eq(private->type, NID_sha256)
1232 && TEST_int_eq(private->flags,
1233 RAND_DRBG_FLAG_PRIVATE | RAND_DRBG_FLAG_HMAC)
1234 /* change public DRBG and check again */
1235 && TEST_true(RAND_DRBG_set_defaults(NID_sha1,
1236 RAND_DRBG_FLAG_PUBLIC
1237 | RAND_DRBG_FLAG_HMAC))
1238 && TEST_true(RAND_DRBG_uninstantiate(public))
1239 && TEST_int_eq(master->type, NID_sha256)
1240 && TEST_int_eq(master->flags, RAND_DRBG_FLAG_MASTER)
1241 && TEST_int_eq(public->type, NID_sha1)
1242 && TEST_int_eq(public->flags,
1243 RAND_DRBG_FLAG_PUBLIC | RAND_DRBG_FLAG_HMAC)
1244 && TEST_int_eq(private->type, NID_sha256)
1245 && TEST_int_eq(private->flags,
1246 RAND_DRBG_FLAG_PRIVATE | RAND_DRBG_FLAG_HMAC)
1247 /* Change DRBG defaults and change public and check again */
1248 && TEST_true(RAND_DRBG_set_defaults(NID_sha256, 0))
1249 && TEST_true(RAND_DRBG_uninstantiate(public))
1250 && TEST_int_eq(public->type, NID_sha256)
1251 && TEST_int_eq(public->flags, RAND_DRBG_FLAG_PUBLIC)
1253 /* FIPS mode doesn't support CTR DRBG without a derivation function */
1255 /* Change DRBG defaults and change master and check again */
1256 && TEST_true(RAND_DRBG_set_defaults(NID_aes_256_ctr,
1257 RAND_DRBG_FLAG_CTR_NO_DF))
1258 && TEST_true(RAND_DRBG_uninstantiate(master))
1259 && TEST_int_eq(master->type, NID_aes_256_ctr)
1260 && TEST_int_eq(master->flags,
1261 RAND_DRBG_FLAG_MASTER|RAND_DRBG_FLAG_CTR_NO_DF)
1263 /* Reset back to the standard defaults */
1264 && TEST_true(RAND_DRBG_set_defaults(RAND_DRBG_TYPE,
1266 | RAND_DRBG_FLAG_MASTER
1267 | RAND_DRBG_FLAG_PUBLIC
1268 | RAND_DRBG_FLAG_PRIVATE))
1269 && TEST_true(RAND_DRBG_uninstantiate(master))
1270 && TEST_true(RAND_DRBG_uninstantiate(public))
1271 && TEST_true(RAND_DRBG_uninstantiate(private));
1275 * A list of the FIPS DRGB types.
1276 * Because of the way HMAC DRGBs are implemented, both the NID and flags
1279 static const struct s_drgb_types {
1283 { NID_aes_128_ctr, 0 },
1284 { NID_aes_192_ctr, 0 },
1285 { NID_aes_256_ctr, 0 },
1291 { NID_sha512_224, 0 },
1292 { NID_sha512_256, 0 },
1293 { NID_sha3_224, 0 },
1294 { NID_sha3_256, 0 },
1295 { NID_sha3_384, 0 },
1296 { NID_sha3_512, 0 },
1297 { NID_sha1, RAND_DRBG_FLAG_HMAC },
1298 { NID_sha224, RAND_DRBG_FLAG_HMAC },
1299 { NID_sha256, RAND_DRBG_FLAG_HMAC },
1300 { NID_sha384, RAND_DRBG_FLAG_HMAC },
1301 { NID_sha512, RAND_DRBG_FLAG_HMAC },
1302 { NID_sha512_224, RAND_DRBG_FLAG_HMAC },
1303 { NID_sha512_256, RAND_DRBG_FLAG_HMAC },
1304 { NID_sha3_224, RAND_DRBG_FLAG_HMAC },
1305 { NID_sha3_256, RAND_DRBG_FLAG_HMAC },
1306 { NID_sha3_384, RAND_DRBG_FLAG_HMAC },
1307 { NID_sha3_512, RAND_DRBG_FLAG_HMAC },
1310 /* Six cases for each covers seed sizes up to 32 bytes */
1311 static const size_t crngt_num_cases = 6;
1313 static size_t crngt_case, crngt_idx;
1315 static int crngt_entropy_cb(OPENSSL_CTX *ctx, RAND_POOL *pool,
1316 unsigned char *buf, unsigned char *md,
1317 unsigned int *md_size)
1321 if (!TEST_int_lt(crngt_idx, crngt_num_cases))
1323 /* Generate a block of unique data unless this is the duplication point */
1325 if (z > 0 && crngt_case == z)
1327 for (i = 0; i < CRNGT_BUFSIZ; i++)
1328 buf[i] = (unsigned char)(i + 'A' + z);
1329 return EVP_Digest(buf, CRNGT_BUFSIZ, md, md_size, EVP_sha256(), NULL);
1332 static int test_crngt(int n)
1334 const struct s_drgb_types *dt = drgb_types + n / crngt_num_cases;
1335 RAND_DRBG *drbg = NULL;
1336 unsigned char buff[100];
1340 OPENSSL_CTX *ctx = OPENSSL_CTX_new();
1344 if (!TEST_ptr(drbg = RAND_DRBG_new_ex(ctx, dt->nid, dt->flags, NULL)))
1346 ent = (drbg->min_entropylen + CRNGT_BUFSIZ - 1) / CRNGT_BUFSIZ;
1347 crngt_case = n % crngt_num_cases;
1349 crngt_get_entropy = &crngt_entropy_cb;
1351 if (!TEST_true(RAND_DRBG_set_callbacks(drbg, &rand_crngt_get_entropy,
1352 &rand_crngt_cleanup_entropy,
1353 &rand_drbg_get_nonce,
1354 &rand_drbg_cleanup_nonce)))
1357 expect = crngt_case == 0 || crngt_case > ent;
1358 if (!TEST_int_eq(RAND_DRBG_instantiate(drbg, NULL, 0), expect))
1362 if (!TEST_true(RAND_DRBG_generate(drbg, buff, sizeof(buff), 0, NULL, 0)))
1365 expect = crngt_case == 0 || crngt_case > 2 * ent;
1366 if (!TEST_int_eq(RAND_DRBG_reseed(drbg, NULL, 0, 0), expect))
1370 if (!TEST_true(RAND_DRBG_generate(drbg, buff, sizeof(buff), 0, NULL, 0)))
1377 TEST_note("DRBG %zd case %zd block %zd", n / crngt_num_cases,
1378 crngt_case, crngt_idx);
1379 uninstantiate(drbg);
1380 RAND_DRBG_free(drbg);
1381 crngt_get_entropy = &rand_crngt_get_entropy_cb;
1382 OPENSSL_CTX_free(ctx);
1386 int setup_tests(void)
1388 ADD_ALL_TESTS(test_kats, OSSL_NELEM(drbg_test));
1389 ADD_ALL_TESTS(test_error_checks, OSSL_NELEM(drbg_test));
1390 ADD_TEST(test_rand_drbg_reseed);
1391 ADD_TEST(test_rand_seed);
1392 ADD_TEST(test_rand_add);
1393 ADD_TEST(test_rand_drbg_prediction_resistance);
1394 ADD_TEST(test_multi_set);
1395 ADD_TEST(test_set_defaults);
1396 #if defined(OPENSSL_THREADS)
1397 ADD_TEST(test_multi_thread);
1399 ADD_ALL_TESTS(test_crngt, crngt_num_cases * OSSL_NELEM(drgb_types));