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_lcl.h"
19 #include "../crypto/include/internal/rand_int.h"
28 typedef struct drbg_selftest_data_st {
33 /* KAT data for no PR */
34 const unsigned char *entropy;
36 const unsigned char *nonce;
38 const unsigned char *pers;
40 const unsigned char *adin;
42 const unsigned char *entropyreseed;
43 size_t entropyreseedlen;
44 const unsigned char *adinreseed;
46 const unsigned char *adin2;
48 const unsigned char *expected;
50 const unsigned char *kat2;
54 const unsigned char *entropy_pr;
56 const unsigned char *nonce_pr;
58 const unsigned char *pers_pr;
60 const unsigned char *adin_pr;
62 const unsigned char *entropypr_pr;
63 size_t entropyprlen_pr;
64 const unsigned char *ading_pr;
66 const unsigned char *entropyg_pr;
67 size_t entropyglen_pr;
68 const unsigned char *kat_pr;
70 const unsigned char *kat2_pr;
74 #define make_drbg_test_data(nid, flag, pr, post) {\
76 pr##_entropyinput, sizeof(pr##_entropyinput), \
77 pr##_nonce, sizeof(pr##_nonce), \
78 pr##_personalizationstring, sizeof(pr##_personalizationstring), \
79 pr##_additionalinput, sizeof(pr##_additionalinput), \
80 pr##_entropyinputreseed, sizeof(pr##_entropyinputreseed), \
81 pr##_additionalinputreseed, sizeof(pr##_additionalinputreseed), \
82 pr##_additionalinput2, sizeof(pr##_additionalinput2), \
83 pr##_int_returnedbits, sizeof(pr##_int_returnedbits), \
84 pr##_returnedbits, sizeof(pr##_returnedbits), \
85 pr##_pr_entropyinput, sizeof(pr##_pr_entropyinput), \
86 pr##_pr_nonce, sizeof(pr##_pr_nonce), \
87 pr##_pr_personalizationstring, sizeof(pr##_pr_personalizationstring), \
88 pr##_pr_additionalinput, sizeof(pr##_pr_additionalinput), \
89 pr##_pr_entropyinputpr, sizeof(pr##_pr_entropyinputpr), \
90 pr##_pr_additionalinput2, sizeof(pr##_pr_additionalinput2), \
91 pr##_pr_entropyinputpr2, sizeof(pr##_pr_entropyinputpr2), \
92 pr##_pr_int_returnedbits, sizeof(pr##_pr_int_returnedbits), \
93 pr##_pr_returnedbits, sizeof(pr##_pr_returnedbits) \
96 #define make_drbg_test_data_use_df(nid, pr, p) \
97 make_drbg_test_data(nid, 0, pr, p)
99 #define make_drbg_test_data_no_df(nid, pr, p) \
100 make_drbg_test_data(nid, RAND_DRBG_FLAG_CTR_NO_DF, pr, p)
102 #define make_drbg_test_data_hash(nid, pr, p) \
103 make_drbg_test_data(nid, RAND_DRBG_FLAG_HMAC, hmac_##pr, p), \
104 make_drbg_test_data(nid, 0, pr, p)
106 static DRBG_SELFTEST_DATA drbg_test[] = {
108 /* FIPS mode doesn't support CTR DRBG without a derivation function */
109 make_drbg_test_data_no_df (NID_aes_128_ctr, aes_128_no_df, 0),
110 make_drbg_test_data_no_df (NID_aes_192_ctr, aes_192_no_df, 0),
111 make_drbg_test_data_no_df (NID_aes_256_ctr, aes_256_no_df, 1),
113 make_drbg_test_data_use_df(NID_aes_128_ctr, aes_128_use_df, 0),
114 make_drbg_test_data_use_df(NID_aes_192_ctr, aes_192_use_df, 0),
115 make_drbg_test_data_use_df(NID_aes_256_ctr, aes_256_use_df, 1),
116 make_drbg_test_data_hash(NID_sha1, sha1, 0),
117 make_drbg_test_data_hash(NID_sha224, sha224, 0),
118 make_drbg_test_data_hash(NID_sha256, sha256, 1),
119 make_drbg_test_data_hash(NID_sha384, sha384, 0),
120 make_drbg_test_data_hash(NID_sha512, sha512, 0),
123 static int app_data_index;
126 * Test context data, attached as EXDATA to the RAND_DRBG
128 typedef struct test_ctx_st {
129 const unsigned char *entropy;
132 const unsigned char *nonce;
137 static size_t kat_entropy(RAND_DRBG *drbg, unsigned char **pout,
138 int entropy, size_t min_len, size_t max_len,
139 int prediction_resistance)
141 TEST_CTX *t = (TEST_CTX *)RAND_DRBG_get_ex_data(drbg, app_data_index);
144 *pout = (unsigned char *)t->entropy;
145 return t->entropylen;
148 static size_t kat_nonce(RAND_DRBG *drbg, unsigned char **pout,
149 int entropy, size_t min_len, size_t max_len)
151 TEST_CTX *t = (TEST_CTX *)RAND_DRBG_get_ex_data(drbg, app_data_index);
154 *pout = (unsigned char *)t->nonce;
159 * Disable CRNG testing if it is enabled.
160 * If the DRBG is ready or in an error state, this means an instantiate cycle
161 * for which the default personalisation string is used.
163 static int disable_crngt(RAND_DRBG *drbg)
165 static const char pers[] = DRBG_DEFAULT_PERS_STRING;
166 const int instantiate = drbg->state != DRBG_UNINITIALISED;
168 if (drbg->get_entropy != rand_crngt_get_entropy)
171 if ((instantiate && !RAND_DRBG_uninstantiate(drbg))
172 || !TEST_true(RAND_DRBG_set_callbacks(drbg, &rand_drbg_get_entropy,
173 &rand_drbg_cleanup_entropy,
174 &rand_drbg_get_nonce,
175 &rand_drbg_cleanup_nonce))
177 && !RAND_DRBG_instantiate(drbg, (const unsigned char *)pers,
183 static int uninstantiate(RAND_DRBG *drbg)
185 int ret = drbg == NULL ? 1 : RAND_DRBG_uninstantiate(drbg);
192 * Do a single KAT test. Return 0 on failure.
194 static int single_kat(DRBG_SELFTEST_DATA *td)
196 RAND_DRBG *drbg = NULL;
199 unsigned char buff[1024];
202 * Test without PR: Instantiate DRBG with test entropy, nonce and
203 * personalisation string.
205 if (!TEST_ptr(drbg = RAND_DRBG_new(td->nid, td->flags, NULL)))
207 if (!TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
209 || !TEST_true(disable_crngt(drbg))) {
213 memset(&t, 0, sizeof(t));
214 t.entropy = td->entropy;
215 t.entropylen = td->entropylen;
217 t.noncelen = td->noncelen;
218 RAND_DRBG_set_ex_data(drbg, app_data_index, &t);
220 if (!TEST_true(RAND_DRBG_instantiate(drbg, td->pers, td->perslen))
221 || !TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
222 td->adin, td->adinlen))
223 || !TEST_mem_eq(td->expected, td->exlen, buff, td->exlen))
226 /* Reseed DRBG with test entropy and additional input */
227 t.entropy = td->entropyreseed;
228 t.entropylen = td->entropyreseedlen;
229 if (!TEST_true(RAND_DRBG_reseed(drbg, td->adinreseed, td->adinreseedlen, 0)
230 || !TEST_true(RAND_DRBG_generate(drbg, buff, td->kat2len, 0,
231 td->adin2, td->adin2len))
232 || !TEST_mem_eq(td->kat2, td->kat2len, buff, td->kat2len)))
237 * Now test with PR: Instantiate DRBG with test entropy, nonce and
238 * personalisation string.
240 if (!TEST_true(RAND_DRBG_set(drbg, td->nid, td->flags))
241 || !TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
244 RAND_DRBG_set_ex_data(drbg, app_data_index, &t);
245 t.entropy = td->entropy_pr;
246 t.entropylen = td->entropylen_pr;
247 t.nonce = td->nonce_pr;
248 t.noncelen = td->noncelen_pr;
251 if (!TEST_true(RAND_DRBG_instantiate(drbg, td->pers_pr, td->perslen_pr)))
255 * Now generate with PR: we need to supply entropy as this will
256 * perform a reseed operation.
258 t.entropy = td->entropypr_pr;
259 t.entropylen = td->entropyprlen_pr;
260 if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->katlen_pr, 1,
261 td->adin_pr, td->adinlen_pr))
262 || !TEST_mem_eq(td->kat_pr, td->katlen_pr, buff, td->katlen_pr))
266 * Now generate again with PR: supply new entropy again.
268 t.entropy = td->entropyg_pr;
269 t.entropylen = td->entropyglen_pr;
271 if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->kat2len_pr, 1,
272 td->ading_pr, td->adinglen_pr))
273 || !TEST_mem_eq(td->kat2_pr, td->kat2len_pr,
274 buff, td->kat2len_pr))
279 RAND_DRBG_free(drbg);
280 return failures == 0;
284 * Initialise a DRBG based on selftest data
286 static int init(RAND_DRBG *drbg, DRBG_SELFTEST_DATA *td, TEST_CTX *t)
288 if (!TEST_true(RAND_DRBG_set(drbg, td->nid, td->flags))
289 || !TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
292 RAND_DRBG_set_ex_data(drbg, app_data_index, t);
293 t->entropy = td->entropy;
294 t->entropylen = td->entropylen;
295 t->nonce = td->nonce;
296 t->noncelen = td->noncelen;
303 * Initialise and instantiate DRBG based on selftest data
305 static int instantiate(RAND_DRBG *drbg, DRBG_SELFTEST_DATA *td,
308 if (!TEST_true(init(drbg, td, t))
309 || !TEST_true(RAND_DRBG_instantiate(drbg, td->pers, td->perslen)))
315 * Perform extensive error checking as required by SP800-90.
316 * Induce several failure modes and check an error condition is set.
318 static int error_check(DRBG_SELFTEST_DATA *td)
320 static char zero[sizeof(RAND_DRBG)];
321 RAND_DRBG *drbg = NULL;
323 unsigned char buff[1024];
324 unsigned int reseed_counter_tmp;
327 if (!TEST_ptr(drbg = RAND_DRBG_new(td->nid, td->flags, NULL))
328 || !TEST_true(disable_crngt(drbg)))
332 * Personalisation string tests
335 /* Test detection of too large personlisation string */
336 if (!init(drbg, td, &t)
337 || RAND_DRBG_instantiate(drbg, td->pers, drbg->max_perslen + 1) > 0)
341 * Entropy source tests
344 /* Test entropy source failure detection: i.e. returns no data */
346 if (TEST_int_le(RAND_DRBG_instantiate(drbg, td->pers, td->perslen), 0))
349 /* Try to generate output from uninstantiated DRBG */
350 if (!TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
351 td->adin, td->adinlen))
352 || !uninstantiate(drbg))
355 /* Test insufficient entropy */
356 t.entropylen = drbg->min_entropylen - 1;
357 if (!init(drbg, td, &t)
358 || RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0
359 || !uninstantiate(drbg))
362 /* Test too much entropy */
363 t.entropylen = drbg->max_entropylen + 1;
364 if (!init(drbg, td, &t)
365 || RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0
366 || !uninstantiate(drbg))
373 /* Test too small nonce */
374 if (drbg->min_noncelen) {
375 t.noncelen = drbg->min_noncelen - 1;
376 if (!init(drbg, td, &t)
377 || RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0
378 || !uninstantiate(drbg))
382 /* Test too large nonce */
383 if (drbg->max_noncelen) {
384 t.noncelen = drbg->max_noncelen + 1;
385 if (!init(drbg, td, &t)
386 || RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0
387 || !uninstantiate(drbg))
391 /* Instantiate with valid data, Check generation is now OK */
392 if (!instantiate(drbg, td, &t)
393 || !TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
394 td->adin, td->adinlen)))
397 /* Request too much data for one request */
398 if (!TEST_false(RAND_DRBG_generate(drbg, buff, drbg->max_request + 1, 0,
399 td->adin, td->adinlen)))
402 /* Try too large additional input */
403 if (!TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
404 td->adin, drbg->max_adinlen + 1)))
408 * Check prediction resistance request fails if entropy source
412 if (TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 1,
413 td->adin, td->adinlen))
414 || !uninstantiate(drbg))
417 /* Instantiate again with valid data */
418 if (!instantiate(drbg, td, &t))
420 reseed_counter_tmp = drbg->reseed_gen_counter;
421 drbg->reseed_gen_counter = drbg->reseed_interval;
423 /* Generate output and check entropy has been requested for reseed */
425 if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
426 td->adin, td->adinlen))
427 || !TEST_int_eq(t.entropycnt, 1)
428 || !TEST_int_eq(drbg->reseed_gen_counter, reseed_counter_tmp + 1)
429 || !uninstantiate(drbg))
433 * Check prediction resistance request fails if entropy source
437 if (!TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 1,
438 td->adin, td->adinlen))
439 || !uninstantiate(drbg))
442 /* Test reseed counter works */
443 if (!instantiate(drbg, td, &t))
445 reseed_counter_tmp = drbg->reseed_gen_counter;
446 drbg->reseed_gen_counter = drbg->reseed_interval;
448 /* Generate output and check entropy has been requested for reseed */
450 if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
451 td->adin, td->adinlen))
452 || !TEST_int_eq(t.entropycnt, 1)
453 || !TEST_int_eq(drbg->reseed_gen_counter, reseed_counter_tmp + 1)
454 || !uninstantiate(drbg))
458 * Explicit reseed tests
461 /* Test explicit reseed with too large additional input */
462 if (!instantiate(drbg, td, &t)
463 || RAND_DRBG_reseed(drbg, td->adin, drbg->max_adinlen + 1, 0) > 0)
466 /* Test explicit reseed with entropy source failure */
468 if (!TEST_int_le(RAND_DRBG_reseed(drbg, td->adin, td->adinlen, 0), 0)
469 || !uninstantiate(drbg))
472 /* Test explicit reseed with too much entropy */
473 if (!instantiate(drbg, td, &t))
475 t.entropylen = drbg->max_entropylen + 1;
476 if (!TEST_int_le(RAND_DRBG_reseed(drbg, td->adin, td->adinlen, 0), 0)
477 || !uninstantiate(drbg))
480 /* Test explicit reseed with too little entropy */
481 if (!instantiate(drbg, td, &t))
483 t.entropylen = drbg->min_entropylen - 1;
484 if (!TEST_int_le(RAND_DRBG_reseed(drbg, td->adin, td->adinlen, 0), 0)
485 || !uninstantiate(drbg))
488 /* Standard says we have to check uninstantiate really zeroes */
489 if (!TEST_mem_eq(zero, sizeof(drbg->data), &drbg->data, sizeof(drbg->data)))
496 RAND_DRBG_free(drbg);
500 static int test_kats(int i)
502 DRBG_SELFTEST_DATA *td = &drbg_test[i];
513 static int test_error_checks(int i)
515 DRBG_SELFTEST_DATA *td = &drbg_test[i];
527 * Hook context data, attached as EXDATA to the RAND_DRBG
529 typedef struct hook_ctx_st {
532 * Currently, all DRBGs use the same get_entropy() callback.
533 * The tests however, don't assume this and store
534 * the original callback for every DRBG separately.
536 RAND_DRBG_get_entropy_fn get_entropy;
537 /* forces a failure of the get_entropy() call if nonzero */
539 /* counts successful reseeds */
543 static HOOK_CTX master_ctx, public_ctx, private_ctx;
545 static HOOK_CTX *get_hook_ctx(RAND_DRBG *drbg)
547 return (HOOK_CTX *)RAND_DRBG_get_ex_data(drbg, app_data_index);
550 /* Intercepts and counts calls to the get_entropy() callback */
551 static size_t get_entropy_hook(RAND_DRBG *drbg, unsigned char **pout,
552 int entropy, size_t min_len, size_t max_len,
553 int prediction_resistance)
556 HOOK_CTX *ctx = get_hook_ctx(drbg);
561 ret = ctx->get_entropy(drbg, pout, entropy, min_len, max_len,
562 prediction_resistance);
569 /* Installs a hook for the get_entropy() callback of the given drbg */
570 static void hook_drbg(RAND_DRBG *drbg, HOOK_CTX *ctx)
572 memset(ctx, 0, sizeof(*ctx));
574 ctx->get_entropy = drbg->get_entropy;
575 drbg->get_entropy = get_entropy_hook;
576 RAND_DRBG_set_ex_data(drbg, app_data_index, ctx);
579 /* Installs the hook for the get_entropy() callback of the given drbg */
580 static void unhook_drbg(RAND_DRBG *drbg)
582 HOOK_CTX *ctx = get_hook_ctx(drbg);
584 drbg->get_entropy = ctx->get_entropy;
585 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DRBG, drbg, &drbg->ex_data);
588 /* Resets the given hook context */
589 static void reset_hook_ctx(HOOK_CTX *ctx)
592 ctx->reseed_count = 0;
595 /* Resets all drbg hook contexts */
596 static void reset_drbg_hook_ctx(void)
598 reset_hook_ctx(&master_ctx);
599 reset_hook_ctx(&public_ctx);
600 reset_hook_ctx(&private_ctx);
604 * Generates random output using RAND_bytes() and RAND_priv_bytes()
605 * and checks whether the three shared DRBGs were reseeded as
608 * |expect_success|: expected outcome (as reported by RAND_status())
609 * |master|, |public|, |private|: pointers to the three shared DRBGs
610 * |expect_xxx_reseed| =
611 * 1: it is expected that the specified DRBG is reseeded
612 * 0: it is expected that the specified DRBG is not reseeded
613 * -1: don't check whether the specified DRBG was reseeded or not
614 * |reseed_time|: if nonzero, used instead of time(NULL) to set the
615 * |before_reseed| time.
617 static int test_drbg_reseed(int expect_success,
621 int expect_master_reseed,
622 int expect_public_reseed,
623 int expect_private_reseed,
627 unsigned char buf[32];
628 time_t before_reseed, after_reseed;
629 int expected_state = (expect_success ? DRBG_READY : DRBG_ERROR);
632 * step 1: check preconditions
635 /* Test whether seed propagation is enabled */
636 if (!TEST_int_ne(master->reseed_prop_counter, 0)
637 || !TEST_int_ne(public->reseed_prop_counter, 0)
638 || !TEST_int_ne(private->reseed_prop_counter, 0))
641 /* Check whether the master DRBG's reseed counter is the largest one */
642 if (!TEST_int_le(public->reseed_prop_counter, master->reseed_prop_counter)
643 || !TEST_int_le(private->reseed_prop_counter, master->reseed_prop_counter))
647 * step 2: generate random output
650 if (reseed_time == 0)
651 reseed_time = time(NULL);
653 /* Generate random output from the public and private DRBG */
654 before_reseed = expect_master_reseed == 1 ? reseed_time : 0;
655 if (!TEST_int_eq(RAND_bytes(buf, sizeof(buf)), expect_success)
656 || !TEST_int_eq(RAND_priv_bytes(buf, sizeof(buf)), expect_success))
658 after_reseed = time(NULL);
662 * step 3: check postconditions
665 /* Test whether reseeding succeeded as expected */
666 if (!TEST_int_eq(master->state, expected_state)
667 || !TEST_int_eq(public->state, expected_state)
668 || !TEST_int_eq(private->state, expected_state))
671 if (expect_master_reseed >= 0) {
672 /* Test whether master DRBG was reseeded as expected */
673 if (!TEST_int_eq(master_ctx.reseed_count, expect_master_reseed))
677 if (expect_public_reseed >= 0) {
678 /* Test whether public DRBG was reseeded as expected */
679 if (!TEST_int_eq(public_ctx.reseed_count, expect_public_reseed))
683 if (expect_private_reseed >= 0) {
684 /* Test whether public DRBG was reseeded as expected */
685 if (!TEST_int_eq(private_ctx.reseed_count, expect_private_reseed))
689 if (expect_success == 1) {
690 /* Test whether all three reseed counters are synchronized */
691 if (!TEST_int_eq(public->reseed_prop_counter, master->reseed_prop_counter)
692 || !TEST_int_eq(private->reseed_prop_counter, master->reseed_prop_counter))
695 /* Test whether reseed time of master DRBG is set correctly */
696 if (!TEST_time_t_le(before_reseed, master->reseed_time)
697 || !TEST_time_t_le(master->reseed_time, after_reseed))
700 /* Test whether reseed times of child DRBGs are synchronized with master */
701 if (!TEST_time_t_ge(public->reseed_time, master->reseed_time)
702 || !TEST_time_t_ge(private->reseed_time, master->reseed_time))
712 * Test whether the default rand_method (RAND_OpenSSL()) is
713 * setup correctly, in particular whether reseeding works
716 static int test_rand_drbg_reseed(void)
718 RAND_DRBG *master, *public, *private;
719 unsigned char rand_add_buf[256];
721 time_t before_reseed;
723 /* Check whether RAND_OpenSSL() is the default method */
724 if (!TEST_ptr_eq(RAND_get_rand_method(), RAND_OpenSSL()))
727 /* All three DRBGs should be non-null */
728 if (!TEST_ptr(master = RAND_DRBG_get0_master())
729 || !TEST_ptr(public = RAND_DRBG_get0_public())
730 || !TEST_ptr(private = RAND_DRBG_get0_private()))
733 /* There should be three distinct DRBGs, two of them chained to master */
734 if (!TEST_ptr_ne(public, private)
735 || !TEST_ptr_ne(public, master)
736 || !TEST_ptr_ne(private, master)
737 || !TEST_ptr_eq(public->parent, master)
738 || !TEST_ptr_eq(private->parent, master))
741 /* Disable CRNG testing for the master DRBG */
742 if (!TEST_true(disable_crngt(master)))
745 /* uninstantiate the three global DRBGs */
746 RAND_DRBG_uninstantiate(private);
747 RAND_DRBG_uninstantiate(public);
748 RAND_DRBG_uninstantiate(master);
751 /* Install hooks for the following tests */
752 hook_drbg(master, &master_ctx);
753 hook_drbg(public, &public_ctx);
754 hook_drbg(private, &private_ctx);
758 * Test initial seeding of shared DRBGs
760 if (!TEST_true(test_drbg_reseed(1, master, public, private, 1, 1, 1, 0)))
762 reset_drbg_hook_ctx();
766 * Test initial state of shared DRBGs
768 if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 0, 0, 0)))
770 reset_drbg_hook_ctx();
773 * Test whether the public and private DRBG are both reseeded when their
774 * reseed counters differ from the master's reseed counter.
776 master->reseed_prop_counter++;
777 if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 1, 1, 0)))
779 reset_drbg_hook_ctx();
782 * Test whether the public DRBG is reseeded when its reseed counter differs
783 * from the master's reseed counter.
785 master->reseed_prop_counter++;
786 private->reseed_prop_counter++;
787 if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 1, 0, 0)))
789 reset_drbg_hook_ctx();
792 * Test whether the private DRBG is reseeded when its reseed counter differs
793 * from the master's reseed counter.
795 master->reseed_prop_counter++;
796 public->reseed_prop_counter++;
797 if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 0, 1, 0)))
799 reset_drbg_hook_ctx();
802 /* fill 'randomness' buffer with some arbitrary data */
803 memset(rand_add_buf, 'r', sizeof(rand_add_buf));
806 * Test whether all three DRBGs are reseeded by RAND_add().
807 * The before_reseed time has to be measured here and passed into the
808 * test_drbg_reseed() test, because the master DRBG gets already reseeded
809 * in RAND_add(), whence the check for the condition
810 * before_reseed <= master->reseed_time will fail if the time value happens
811 * to increase between the RAND_add() and the test_drbg_reseed() call.
813 before_reseed = time(NULL);
814 RAND_add(rand_add_buf, sizeof(rand_add_buf), sizeof(rand_add_buf));
815 if (!TEST_true(test_drbg_reseed(1, master, public, private, 1, 1, 1,
818 reset_drbg_hook_ctx();
822 * Test whether none of the DRBGs is reseed if the master fails to reseed
825 master->reseed_prop_counter++;
826 RAND_add(rand_add_buf, sizeof(rand_add_buf), sizeof(rand_add_buf));
827 if (!TEST_true(test_drbg_reseed(0, master, public, private, 0, 0, 0, 0)))
829 reset_drbg_hook_ctx();
837 unhook_drbg(private);
842 #if defined(OPENSSL_THREADS)
843 static int multi_thread_rand_bytes_succeeded = 1;
844 static int multi_thread_rand_priv_bytes_succeeded = 1;
846 static void run_multi_thread_test(void)
848 unsigned char buf[256];
849 time_t start = time(NULL);
850 RAND_DRBG *public = NULL, *private = NULL;
852 if (!TEST_ptr(public = RAND_DRBG_get0_public())
853 || !TEST_ptr(private = RAND_DRBG_get0_private())) {
854 multi_thread_rand_bytes_succeeded = 0;
857 RAND_DRBG_set_reseed_time_interval(private, 1);
858 RAND_DRBG_set_reseed_time_interval(public, 1);
861 if (RAND_bytes(buf, sizeof(buf)) <= 0)
862 multi_thread_rand_bytes_succeeded = 0;
863 if (RAND_priv_bytes(buf, sizeof(buf)) <= 0)
864 multi_thread_rand_priv_bytes_succeeded = 0;
866 while(time(NULL) - start < 5);
869 # if defined(OPENSSL_SYS_WINDOWS)
871 typedef HANDLE thread_t;
873 static DWORD WINAPI thread_run(LPVOID arg)
875 run_multi_thread_test();
877 * Because we're linking with a static library, we must stop each
878 * thread explicitly, or so says OPENSSL_thread_stop(3)
880 OPENSSL_thread_stop();
884 static int run_thread(thread_t *t)
886 *t = CreateThread(NULL, 0, thread_run, NULL, 0, NULL);
890 static int wait_for_thread(thread_t thread)
892 return WaitForSingleObject(thread, INFINITE) == 0;
897 typedef pthread_t thread_t;
899 static void *thread_run(void *arg)
901 run_multi_thread_test();
903 * Because we're linking with a static library, we must stop each
904 * thread explicitly, or so says OPENSSL_thread_stop(3)
906 OPENSSL_thread_stop();
910 static int run_thread(thread_t *t)
912 return pthread_create(t, NULL, thread_run, NULL) == 0;
915 static int wait_for_thread(thread_t thread)
917 return pthread_join(thread, NULL) == 0;
923 * The main thread will also run the test, so we'll have THREADS+1 parallel
928 static int test_multi_thread(void)
933 for (i = 0; i < THREADS; i++)
935 run_multi_thread_test();
936 for (i = 0; i < THREADS; i++)
937 wait_for_thread(t[i]);
939 if (!TEST_true(multi_thread_rand_bytes_succeeded))
941 if (!TEST_true(multi_thread_rand_priv_bytes_succeeded))
949 * Test that instantiation with RAND_seed() works as expected
951 * If no os entropy source is available then RAND_seed(buffer, bufsize)
952 * is expected to succeed if and only if the buffer length is at least
953 * rand_drbg_seedlen(master) bytes.
955 * If an os entropy source is available then RAND_seed(buffer, bufsize)
956 * is expected to succeed always.
958 static int test_rand_seed(void)
960 RAND_DRBG *master = NULL;
961 unsigned char rand_buf[256];
963 size_t required_seed_buflen = 0;
965 if (!TEST_ptr(master = RAND_DRBG_get0_master())
966 || !TEST_true(disable_crngt(master)))
969 #ifdef OPENSSL_RAND_SEED_NONE
970 required_seed_buflen = rand_drbg_seedlen(master);
973 memset(rand_buf, 0xCD, sizeof(rand_buf));
975 for ( rand_buflen = 256 ; rand_buflen > 0 ; --rand_buflen ) {
976 RAND_DRBG_uninstantiate(master);
977 RAND_seed(rand_buf, rand_buflen);
979 if (!TEST_int_eq(RAND_status(),
980 (rand_buflen >= required_seed_buflen)))
988 * Test that adding additional data with RAND_add() works as expected
989 * when the master DRBG is instantiated (and below its reseed limit).
991 * This should succeed regardless of whether an os entropy source is
994 static int test_rand_add(void)
996 unsigned char rand_buf[256];
999 memset(rand_buf, 0xCD, sizeof(rand_buf));
1001 /* make sure it's instantiated */
1002 RAND_seed(rand_buf, sizeof(rand_buf));
1003 if (!TEST_true(RAND_status()))
1006 for ( rand_buflen = 256 ; rand_buflen > 0 ; --rand_buflen ) {
1007 RAND_add(rand_buf, rand_buflen, 0.0);
1008 if (!TEST_true(RAND_status()))
1015 static int test_multi_set(void)
1018 RAND_DRBG *drbg = NULL;
1020 /* init drbg with default CTR initializer */
1021 if (!TEST_ptr(drbg = RAND_DRBG_new(0, 0, NULL))
1022 || !TEST_true(disable_crngt(drbg)))
1024 /* change it to use hmac */
1025 if (!TEST_true(RAND_DRBG_set(drbg, NID_sha1, RAND_DRBG_FLAG_HMAC)))
1028 if (!TEST_true(RAND_DRBG_set(drbg, NID_sha1, RAND_DRBG_FLAG_HMAC)))
1030 /* change it to use hash */
1031 if (!TEST_true(RAND_DRBG_set(drbg, NID_sha256, 0)))
1034 if (!TEST_true(RAND_DRBG_set(drbg, NID_sha256, 0)))
1036 /* change it to use ctr */
1037 if (!TEST_true(RAND_DRBG_set(drbg, NID_aes_192_ctr, 0)))
1040 if (!TEST_true(RAND_DRBG_set(drbg, NID_aes_192_ctr, 0)))
1042 if (!TEST_int_gt(RAND_DRBG_instantiate(drbg, NULL, 0), 0))
1047 uninstantiate(drbg);
1048 RAND_DRBG_free(drbg);
1052 static int test_set_defaults(void)
1054 RAND_DRBG *master = NULL, *public = NULL, *private = NULL;
1056 /* Check the default type and flags for master, public and private */
1057 return TEST_ptr(master = RAND_DRBG_get0_master())
1058 && TEST_ptr(public = RAND_DRBG_get0_public())
1059 && TEST_ptr(private = RAND_DRBG_get0_private())
1060 && TEST_int_eq(master->type, RAND_DRBG_TYPE)
1061 && TEST_int_eq(master->flags,
1062 RAND_DRBG_FLAGS | RAND_DRBG_FLAG_MASTER)
1063 && TEST_int_eq(public->type, RAND_DRBG_TYPE)
1064 && TEST_int_eq(public->flags,
1065 RAND_DRBG_FLAGS | RAND_DRBG_FLAG_PUBLIC)
1066 && TEST_int_eq(private->type, RAND_DRBG_TYPE)
1067 && TEST_int_eq(private->flags,
1068 RAND_DRBG_FLAGS | RAND_DRBG_FLAG_PRIVATE)
1070 /* change master DRBG and check again */
1071 && TEST_true(RAND_DRBG_set_defaults(NID_sha256,
1072 RAND_DRBG_FLAG_MASTER))
1073 && TEST_true(RAND_DRBG_uninstantiate(master))
1074 && TEST_int_eq(master->type, NID_sha256)
1075 && TEST_int_eq(master->flags, RAND_DRBG_FLAG_MASTER)
1076 && TEST_int_eq(public->type, RAND_DRBG_TYPE)
1077 && TEST_int_eq(public->flags,
1078 RAND_DRBG_FLAGS | RAND_DRBG_FLAG_PUBLIC)
1079 && TEST_int_eq(private->type, RAND_DRBG_TYPE)
1080 && TEST_int_eq(private->flags,
1081 RAND_DRBG_FLAGS | RAND_DRBG_FLAG_PRIVATE)
1082 /* change private DRBG and check again */
1083 && TEST_true(RAND_DRBG_set_defaults(NID_sha256,
1084 RAND_DRBG_FLAG_PRIVATE|RAND_DRBG_FLAG_HMAC))
1085 && TEST_true(RAND_DRBG_uninstantiate(private))
1086 && TEST_int_eq(master->type, NID_sha256)
1087 && TEST_int_eq(master->flags, RAND_DRBG_FLAG_MASTER)
1088 && TEST_int_eq(public->type, RAND_DRBG_TYPE)
1089 && TEST_int_eq(public->flags,
1090 RAND_DRBG_FLAGS | RAND_DRBG_FLAG_PUBLIC)
1091 && TEST_int_eq(private->type, NID_sha256)
1092 && TEST_int_eq(private->flags,
1093 RAND_DRBG_FLAG_PRIVATE | RAND_DRBG_FLAG_HMAC)
1094 /* change public DRBG and check again */
1095 && TEST_true(RAND_DRBG_set_defaults(NID_sha1,
1096 RAND_DRBG_FLAG_PUBLIC
1097 | RAND_DRBG_FLAG_HMAC))
1098 && TEST_true(RAND_DRBG_uninstantiate(public))
1099 && TEST_int_eq(master->type, NID_sha256)
1100 && TEST_int_eq(master->flags, RAND_DRBG_FLAG_MASTER)
1101 && TEST_int_eq(public->type, NID_sha1)
1102 && TEST_int_eq(public->flags,
1103 RAND_DRBG_FLAG_PUBLIC | RAND_DRBG_FLAG_HMAC)
1104 && TEST_int_eq(private->type, NID_sha256)
1105 && TEST_int_eq(private->flags,
1106 RAND_DRBG_FLAG_PRIVATE | RAND_DRBG_FLAG_HMAC)
1107 /* Change DRBG defaults and change public and check again */
1108 && TEST_true(RAND_DRBG_set_defaults(NID_sha256, 0))
1109 && TEST_true(RAND_DRBG_uninstantiate(public))
1110 && TEST_int_eq(public->type, NID_sha256)
1111 && TEST_int_eq(public->flags, RAND_DRBG_FLAG_PUBLIC)
1113 /* FIPS mode doesn't support CTR DRBG without a derivation function */
1115 /* Change DRBG defaults and change master and check again */
1116 && TEST_true(RAND_DRBG_set_defaults(NID_aes_256_ctr,
1117 RAND_DRBG_FLAG_CTR_NO_DF))
1118 && TEST_true(RAND_DRBG_uninstantiate(master))
1119 && TEST_int_eq(master->type, NID_aes_256_ctr)
1120 && TEST_int_eq(master->flags,
1121 RAND_DRBG_FLAG_MASTER|RAND_DRBG_FLAG_CTR_NO_DF)
1123 /* Reset back to the standard defaults */
1124 && TEST_true(RAND_DRBG_set_defaults(RAND_DRBG_TYPE,
1126 | RAND_DRBG_FLAG_MASTER
1127 | RAND_DRBG_FLAG_PUBLIC
1128 | RAND_DRBG_FLAG_PRIVATE))
1129 && TEST_true(RAND_DRBG_uninstantiate(master))
1130 && TEST_true(RAND_DRBG_uninstantiate(public))
1131 && TEST_true(RAND_DRBG_uninstantiate(private));
1135 * A list of the FIPS DRGB types.
1136 * Because of the way HMAC DRGBs are implemented, both the NID and flags
1139 static const struct s_drgb_types {
1143 { NID_aes_128_ctr, 0 },
1144 { NID_aes_192_ctr, 0 },
1145 { NID_aes_256_ctr, 0 },
1151 { NID_sha512_224, 0 },
1152 { NID_sha512_256, 0 },
1153 { NID_sha3_224, 0 },
1154 { NID_sha3_256, 0 },
1155 { NID_sha3_384, 0 },
1156 { NID_sha3_512, 0 },
1157 { NID_sha1, RAND_DRBG_FLAG_HMAC },
1158 { NID_sha224, RAND_DRBG_FLAG_HMAC },
1159 { NID_sha256, RAND_DRBG_FLAG_HMAC },
1160 { NID_sha384, RAND_DRBG_FLAG_HMAC },
1161 { NID_sha512, RAND_DRBG_FLAG_HMAC },
1162 { NID_sha512_224, RAND_DRBG_FLAG_HMAC },
1163 { NID_sha512_256, RAND_DRBG_FLAG_HMAC },
1164 { NID_sha3_224, RAND_DRBG_FLAG_HMAC },
1165 { NID_sha3_256, RAND_DRBG_FLAG_HMAC },
1166 { NID_sha3_384, RAND_DRBG_FLAG_HMAC },
1167 { NID_sha3_512, RAND_DRBG_FLAG_HMAC },
1170 /* Six cases for each covers seed sizes up to 32 bytes */
1171 static const size_t crngt_num_cases = 6;
1173 static size_t crngt_case, crngt_idx;
1175 static int crngt_entropy_cb(unsigned char *buf)
1179 if (!TEST_int_lt(crngt_idx, crngt_num_cases))
1181 /* Generate a block of unique data unless this is the duplication point */
1183 if (z > 0 && crngt_case == z)
1185 for (i = 0; i < CRNGT_BUFSIZ; i++)
1186 buf[i] = (unsigned char)(i + 'A' + z);
1190 static int test_crngt(int n)
1192 const struct s_drgb_types *dt = drgb_types + n / crngt_num_cases;
1193 RAND_DRBG *drbg = NULL;
1194 unsigned char buff[100];
1199 if (!TEST_true(rand_crngt_single_init()))
1201 rand_crngt_cleanup();
1203 if (!TEST_ptr(drbg = RAND_DRBG_new(dt->nid, dt->flags, NULL)))
1205 ent = (drbg->min_entropylen + CRNGT_BUFSIZ - 1) / CRNGT_BUFSIZ;
1206 crngt_case = n % crngt_num_cases;
1208 crngt_get_entropy = &crngt_entropy_cb;
1209 if (!TEST_true(rand_crngt_init()))
1212 if (!TEST_true(RAND_DRBG_set_callbacks(drbg, &rand_crngt_get_entropy,
1213 &rand_crngt_cleanup_entropy,
1214 &rand_drbg_get_nonce,
1215 &rand_drbg_cleanup_nonce)))
1218 expect = crngt_case == 0 || crngt_case > ent;
1219 if (!TEST_int_eq(RAND_DRBG_instantiate(drbg, NULL, 0), expect))
1223 if (!TEST_true(RAND_DRBG_generate(drbg, buff, sizeof(buff), 0, NULL, 0)))
1226 expect = crngt_case == 0 || crngt_case > 2 * ent;
1227 if (!TEST_int_eq(RAND_DRBG_reseed(drbg, NULL, 0, 0), expect))
1231 if (!TEST_true(RAND_DRBG_generate(drbg, buff, sizeof(buff), 0, NULL, 0)))
1238 TEST_note("DRBG %zd case %zd block %zd", n / crngt_num_cases,
1239 crngt_case, crngt_idx);
1240 uninstantiate(drbg);
1241 RAND_DRBG_free(drbg);
1242 crngt_get_entropy = &rand_crngt_get_entropy_cb;
1246 int setup_tests(void)
1248 app_data_index = RAND_DRBG_get_ex_new_index(0L, NULL, NULL, NULL, NULL);
1250 ADD_ALL_TESTS(test_kats, OSSL_NELEM(drbg_test));
1251 ADD_ALL_TESTS(test_error_checks, OSSL_NELEM(drbg_test));
1252 ADD_TEST(test_rand_drbg_reseed);
1253 ADD_TEST(test_rand_seed);
1254 ADD_TEST(test_rand_add);
1255 ADD_TEST(test_multi_set);
1256 ADD_TEST(test_set_defaults);
1257 #if defined(OPENSSL_THREADS)
1258 ADD_TEST(test_multi_thread);
1260 ADD_ALL_TESTS(test_crngt, crngt_num_cases * OSSL_NELEM(drgb_types));