2 * Copyright 2011-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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[] = {
107 make_drbg_test_data_no_df (NID_aes_128_ctr, aes_128_no_df, 0),
108 make_drbg_test_data_no_df (NID_aes_192_ctr, aes_192_no_df, 0),
109 make_drbg_test_data_no_df (NID_aes_256_ctr, aes_256_no_df, 1),
110 make_drbg_test_data_use_df(NID_aes_128_ctr, aes_128_use_df, 0),
111 make_drbg_test_data_use_df(NID_aes_192_ctr, aes_192_use_df, 0),
112 make_drbg_test_data_use_df(NID_aes_256_ctr, aes_256_use_df, 1),
113 make_drbg_test_data_hash(NID_sha1, sha1, 0),
114 make_drbg_test_data_hash(NID_sha224, sha224, 0),
115 make_drbg_test_data_hash(NID_sha256, sha256, 1),
116 make_drbg_test_data_hash(NID_sha384, sha384, 0),
117 make_drbg_test_data_hash(NID_sha512, sha512, 0),
120 static int app_data_index;
123 * Test context data, attached as EXDATA to the RAND_DRBG
125 typedef struct test_ctx_st {
126 const unsigned char *entropy;
129 const unsigned char *nonce;
134 static size_t kat_entropy(RAND_DRBG *drbg, unsigned char **pout,
135 int entropy, size_t min_len, size_t max_len,
136 int prediction_resistance)
138 TEST_CTX *t = (TEST_CTX *)RAND_DRBG_get_ex_data(drbg, app_data_index);
141 *pout = (unsigned char *)t->entropy;
142 return t->entropylen;
145 static size_t kat_nonce(RAND_DRBG *drbg, unsigned char **pout,
146 int entropy, size_t min_len, size_t max_len)
148 TEST_CTX *t = (TEST_CTX *)RAND_DRBG_get_ex_data(drbg, app_data_index);
151 *pout = (unsigned char *)t->nonce;
155 static int uninstantiate(RAND_DRBG *drbg)
157 int ret = drbg == NULL ? 1 : RAND_DRBG_uninstantiate(drbg);
164 * Do a single KAT test. Return 0 on failure.
166 static int single_kat(DRBG_SELFTEST_DATA *td)
168 RAND_DRBG *drbg = NULL;
171 unsigned char buff[1024];
174 * Test without PR: Instantiate DRBG with test entropy, nonce and
175 * personalisation string.
177 if (!TEST_ptr(drbg = RAND_DRBG_new(td->nid, td->flags, NULL)))
179 if (!TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
184 memset(&t, 0, sizeof(t));
185 t.entropy = td->entropy;
186 t.entropylen = td->entropylen;
188 t.noncelen = td->noncelen;
189 RAND_DRBG_set_ex_data(drbg, app_data_index, &t);
191 if (!TEST_true(RAND_DRBG_instantiate(drbg, td->pers, td->perslen))
192 || !TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
193 td->adin, td->adinlen))
194 || !TEST_mem_eq(td->expected, td->exlen, buff, td->exlen))
197 /* Reseed DRBG with test entropy and additional input */
198 t.entropy = td->entropyreseed;
199 t.entropylen = td->entropyreseedlen;
200 if (!TEST_true(RAND_DRBG_reseed(drbg, td->adinreseed, td->adinreseedlen, 0)
201 || !TEST_true(RAND_DRBG_generate(drbg, buff, td->kat2len, 0,
202 td->adin2, td->adin2len))
203 || !TEST_mem_eq(td->kat2, td->kat2len, buff, td->kat2len)))
208 * Now test with PR: Instantiate DRBG with test entropy, nonce and
209 * personalisation string.
211 if (!TEST_true(RAND_DRBG_set(drbg, td->nid, td->flags))
212 || !TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
215 RAND_DRBG_set_ex_data(drbg, app_data_index, &t);
216 t.entropy = td->entropy_pr;
217 t.entropylen = td->entropylen_pr;
218 t.nonce = td->nonce_pr;
219 t.noncelen = td->noncelen_pr;
222 if (!TEST_true(RAND_DRBG_instantiate(drbg, td->pers_pr, td->perslen_pr)))
226 * Now generate with PR: we need to supply entropy as this will
227 * perform a reseed operation.
229 t.entropy = td->entropypr_pr;
230 t.entropylen = td->entropyprlen_pr;
231 if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->katlen_pr, 1,
232 td->adin_pr, td->adinlen_pr))
233 || !TEST_mem_eq(td->kat_pr, td->katlen_pr, buff, td->katlen_pr))
237 * Now generate again with PR: supply new entropy again.
239 t.entropy = td->entropyg_pr;
240 t.entropylen = td->entropyglen_pr;
242 if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->kat2len_pr, 1,
243 td->ading_pr, td->adinglen_pr))
244 || !TEST_mem_eq(td->kat2_pr, td->kat2len_pr,
245 buff, td->kat2len_pr))
250 RAND_DRBG_free(drbg);
251 return failures == 0;
255 * Initialise a DRBG based on selftest data
257 static int init(RAND_DRBG *drbg, DRBG_SELFTEST_DATA *td, TEST_CTX *t)
259 if (!TEST_true(RAND_DRBG_set(drbg, td->nid, td->flags))
260 || !TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
263 RAND_DRBG_set_ex_data(drbg, app_data_index, t);
264 t->entropy = td->entropy;
265 t->entropylen = td->entropylen;
266 t->nonce = td->nonce;
267 t->noncelen = td->noncelen;
274 * Initialise and instantiate DRBG based on selftest data
276 static int instantiate(RAND_DRBG *drbg, DRBG_SELFTEST_DATA *td,
279 if (!TEST_true(init(drbg, td, t))
280 || !TEST_true(RAND_DRBG_instantiate(drbg, td->pers, td->perslen)))
286 * Perform extensive error checking as required by SP800-90.
287 * Induce several failure modes and check an error condition is set.
289 static int error_check(DRBG_SELFTEST_DATA *td)
291 static char zero[sizeof(RAND_DRBG)];
292 RAND_DRBG *drbg = NULL;
294 unsigned char buff[1024];
295 unsigned int reseed_counter_tmp;
298 if (!TEST_ptr(drbg = RAND_DRBG_new(td->nid, td->flags, NULL)))
302 * Personalisation string tests
305 /* Test detection of too large personlisation string */
306 if (!init(drbg, td, &t)
307 || RAND_DRBG_instantiate(drbg, td->pers, drbg->max_perslen + 1) > 0)
311 * Entropy source tests
314 /* Test entropy source failure detection: i.e. returns no data */
316 if (TEST_int_le(RAND_DRBG_instantiate(drbg, td->pers, td->perslen), 0))
319 /* Try to generate output from uninstantiated DRBG */
320 if (!TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
321 td->adin, td->adinlen))
322 || !uninstantiate(drbg))
325 /* Test insufficient entropy */
326 t.entropylen = drbg->min_entropylen - 1;
327 if (!init(drbg, td, &t)
328 || RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0
329 || !uninstantiate(drbg))
332 /* Test too much entropy */
333 t.entropylen = drbg->max_entropylen + 1;
334 if (!init(drbg, td, &t)
335 || RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0
336 || !uninstantiate(drbg))
343 /* Test too small nonce */
344 if (drbg->min_noncelen) {
345 t.noncelen = drbg->min_noncelen - 1;
346 if (!init(drbg, td, &t)
347 || RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0
348 || !uninstantiate(drbg))
352 /* Test too large nonce */
353 if (drbg->max_noncelen) {
354 t.noncelen = drbg->max_noncelen + 1;
355 if (!init(drbg, td, &t)
356 || RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0
357 || !uninstantiate(drbg))
361 /* Instantiate with valid data, Check generation is now OK */
362 if (!instantiate(drbg, td, &t)
363 || !TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
364 td->adin, td->adinlen)))
367 /* Request too much data for one request */
368 if (!TEST_false(RAND_DRBG_generate(drbg, buff, drbg->max_request + 1, 0,
369 td->adin, td->adinlen)))
372 /* Try too large additional input */
373 if (!TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
374 td->adin, drbg->max_adinlen + 1)))
378 * Check prediction resistance request fails if entropy source
382 if (TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 1,
383 td->adin, td->adinlen))
384 || !uninstantiate(drbg))
387 /* Instantiate again with valid data */
388 if (!instantiate(drbg, td, &t))
390 reseed_counter_tmp = drbg->reseed_gen_counter;
391 drbg->reseed_gen_counter = drbg->reseed_interval;
393 /* Generate output and check entropy has been requested for reseed */
395 if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
396 td->adin, td->adinlen))
397 || !TEST_int_eq(t.entropycnt, 1)
398 || !TEST_int_eq(drbg->reseed_gen_counter, reseed_counter_tmp + 1)
399 || !uninstantiate(drbg))
403 * Check prediction resistance request fails if entropy source
407 if (!TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 1,
408 td->adin, td->adinlen))
409 || !uninstantiate(drbg))
412 /* Test reseed counter works */
413 if (!instantiate(drbg, td, &t))
415 reseed_counter_tmp = drbg->reseed_gen_counter;
416 drbg->reseed_gen_counter = drbg->reseed_interval;
418 /* Generate output and check entropy has been requested for reseed */
420 if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
421 td->adin, td->adinlen))
422 || !TEST_int_eq(t.entropycnt, 1)
423 || !TEST_int_eq(drbg->reseed_gen_counter, reseed_counter_tmp + 1)
424 || !uninstantiate(drbg))
428 * Explicit reseed tests
431 /* Test explicit reseed with too large additional input */
432 if (!init(drbg, td, &t)
433 || RAND_DRBG_reseed(drbg, td->adin, drbg->max_adinlen + 1, 0) > 0)
436 /* Test explicit reseed with entropy source failure */
438 if (!TEST_int_le(RAND_DRBG_reseed(drbg, td->adin, td->adinlen, 0), 0)
439 || !uninstantiate(drbg))
442 /* Test explicit reseed with too much entropy */
443 if (!init(drbg, td, &t))
445 t.entropylen = drbg->max_entropylen + 1;
446 if (!TEST_int_le(RAND_DRBG_reseed(drbg, td->adin, td->adinlen, 0), 0)
447 || !uninstantiate(drbg))
450 /* Test explicit reseed with too little entropy */
451 if (!init(drbg, td, &t))
453 t.entropylen = drbg->min_entropylen - 1;
454 if (!TEST_int_le(RAND_DRBG_reseed(drbg, td->adin, td->adinlen, 0), 0)
455 || !uninstantiate(drbg))
458 /* Standard says we have to check uninstantiate really zeroes */
459 if (!TEST_mem_eq(zero, sizeof(drbg->data), &drbg->data, sizeof(drbg->data)))
466 RAND_DRBG_free(drbg);
470 static int test_kats(int i)
472 DRBG_SELFTEST_DATA *td = &drbg_test[i];
483 static int test_error_checks(int i)
485 DRBG_SELFTEST_DATA *td = &drbg_test[i];
497 * Hook context data, attached as EXDATA to the RAND_DRBG
499 typedef struct hook_ctx_st {
502 * Currently, all DRBGs use the same get_entropy() callback.
503 * The tests however, don't assume this and store
504 * the original callback for every DRBG separately.
506 RAND_DRBG_get_entropy_fn get_entropy;
507 /* forces a failure of the get_entropy() call if nonzero */
509 /* counts successful reseeds */
513 static HOOK_CTX master_ctx, public_ctx, private_ctx;
515 static HOOK_CTX *get_hook_ctx(RAND_DRBG *drbg)
517 return (HOOK_CTX *)RAND_DRBG_get_ex_data(drbg, app_data_index);
520 /* Intercepts and counts calls to the get_entropy() callback */
521 static size_t get_entropy_hook(RAND_DRBG *drbg, unsigned char **pout,
522 int entropy, size_t min_len, size_t max_len,
523 int prediction_resistance)
526 HOOK_CTX *ctx = get_hook_ctx(drbg);
531 ret = ctx->get_entropy(drbg, pout, entropy, min_len, max_len,
532 prediction_resistance);
539 /* Installs a hook for the get_entropy() callback of the given drbg */
540 static void hook_drbg(RAND_DRBG *drbg, HOOK_CTX *ctx)
542 memset(ctx, 0, sizeof(*ctx));
544 ctx->get_entropy = drbg->get_entropy;
545 drbg->get_entropy = get_entropy_hook;
546 RAND_DRBG_set_ex_data(drbg, app_data_index, ctx);
549 /* Installs the hook for the get_entropy() callback of the given drbg */
550 static void unhook_drbg(RAND_DRBG *drbg)
552 HOOK_CTX *ctx = get_hook_ctx(drbg);
554 drbg->get_entropy = ctx->get_entropy;
555 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DRBG, drbg, &drbg->ex_data);
558 /* Resets the given hook context */
559 static void reset_hook_ctx(HOOK_CTX *ctx)
562 ctx->reseed_count = 0;
565 /* Resets all drbg hook contexts */
566 static void reset_drbg_hook_ctx(void)
568 reset_hook_ctx(&master_ctx);
569 reset_hook_ctx(&public_ctx);
570 reset_hook_ctx(&private_ctx);
574 * Generates random output using RAND_bytes() and RAND_priv_bytes()
575 * and checks whether the three shared DRBGs were reseeded as
578 * |expect_success|: expected outcome (as reported by RAND_status())
579 * |master|, |public|, |private|: pointers to the three shared DRBGs
580 * |expect_xxx_reseed| =
581 * 1: it is expected that the specified DRBG is reseeded
582 * 0: it is expected that the specified DRBG is not reseeded
583 * -1: don't check whether the specified DRBG was reseeded or not
585 static int test_drbg_reseed(int expect_success,
589 int expect_master_reseed,
590 int expect_public_reseed,
591 int expect_private_reseed
594 unsigned char buf[32];
595 time_t before_reseed, after_reseed;
596 int expected_state = (expect_success ? DRBG_READY : DRBG_ERROR);
599 * step 1: check preconditions
602 /* Test whether seed propagation is enabled */
603 if (!TEST_int_ne(master->reseed_prop_counter, 0)
604 || !TEST_int_ne(public->reseed_prop_counter, 0)
605 || !TEST_int_ne(private->reseed_prop_counter, 0))
608 /* Check whether the master DRBG's reseed counter is the largest one */
609 if (!TEST_int_le(public->reseed_prop_counter, master->reseed_prop_counter)
610 || !TEST_int_le(private->reseed_prop_counter, master->reseed_prop_counter))
614 * step 2: generate random output
617 /* Generate random output from the public and private DRBG */
618 before_reseed = expect_master_reseed == 1 ? time(NULL) : 0;
619 if (!TEST_int_eq(RAND_bytes(buf, sizeof(buf)), expect_success)
620 || !TEST_int_eq(RAND_priv_bytes(buf, sizeof(buf)), expect_success))
622 after_reseed = time(NULL);
626 * step 3: check postconditions
629 /* Test whether reseeding succeeded as expected */
630 if (!TEST_int_eq(master->state, expected_state)
631 || !TEST_int_eq(public->state, expected_state)
632 || !TEST_int_eq(private->state, expected_state))
635 if (expect_master_reseed >= 0) {
636 /* Test whether master DRBG was reseeded as expected */
637 if (!TEST_int_eq(master_ctx.reseed_count, expect_master_reseed))
641 if (expect_public_reseed >= 0) {
642 /* Test whether public DRBG was reseeded as expected */
643 if (!TEST_int_eq(public_ctx.reseed_count, expect_public_reseed))
647 if (expect_private_reseed >= 0) {
648 /* Test whether public DRBG was reseeded as expected */
649 if (!TEST_int_eq(private_ctx.reseed_count, expect_private_reseed))
653 if (expect_success == 1) {
654 /* Test whether all three reseed counters are synchronized */
655 if (!TEST_int_eq(public->reseed_prop_counter, master->reseed_prop_counter)
656 || !TEST_int_eq(private->reseed_prop_counter, master->reseed_prop_counter))
659 /* Test whether reseed time of master DRBG is set correctly */
660 if (!TEST_time_t_le(before_reseed, master->reseed_time)
661 || !TEST_time_t_le(master->reseed_time, after_reseed))
664 /* Test whether reseed times of child DRBGs are synchronized with master */
665 if (!TEST_time_t_ge(public->reseed_time, master->reseed_time)
666 || !TEST_time_t_ge(private->reseed_time, master->reseed_time))
676 * Test whether the default rand_method (RAND_OpenSSL()) is
677 * setup correctly, in particular whether reseeding works
680 static int test_rand_drbg_reseed(void)
682 RAND_DRBG *master, *public, *private;
683 unsigned char rand_add_buf[256];
686 /* Check whether RAND_OpenSSL() is the default method */
687 if (!TEST_ptr_eq(RAND_get_rand_method(), RAND_OpenSSL()))
690 /* All three DRBGs should be non-null */
691 if (!TEST_ptr(master = RAND_DRBG_get0_master())
692 || !TEST_ptr(public = RAND_DRBG_get0_public())
693 || !TEST_ptr(private = RAND_DRBG_get0_private()))
696 /* There should be three distinct DRBGs, two of them chained to master */
697 if (!TEST_ptr_ne(public, private)
698 || !TEST_ptr_ne(public, master)
699 || !TEST_ptr_ne(private, master)
700 || !TEST_ptr_eq(public->parent, master)
701 || !TEST_ptr_eq(private->parent, master))
704 /* uninstantiate the three global DRBGs */
705 RAND_DRBG_uninstantiate(private);
706 RAND_DRBG_uninstantiate(public);
707 RAND_DRBG_uninstantiate(master);
710 /* Install hooks for the following tests */
711 hook_drbg(master, &master_ctx);
712 hook_drbg(public, &public_ctx);
713 hook_drbg(private, &private_ctx);
717 * Test initial seeding of shared DRBGs
719 if (!TEST_true(test_drbg_reseed(1, master, public, private, 1, 1, 1)))
721 reset_drbg_hook_ctx();
725 * Test initial state of shared DRBGs
727 if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 0, 0)))
729 reset_drbg_hook_ctx();
732 * Test whether the public and private DRBG are both reseeded when their
733 * reseed counters differ from the master's reseed counter.
735 master->reseed_prop_counter++;
736 if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 1, 1)))
738 reset_drbg_hook_ctx();
741 * Test whether the public DRBG is reseeded when its reseed counter differs
742 * from the master's reseed counter.
744 master->reseed_prop_counter++;
745 private->reseed_prop_counter++;
746 if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 1, 0)))
748 reset_drbg_hook_ctx();
751 * Test whether the private DRBG is reseeded when its reseed counter differs
752 * from the master's reseed counter.
754 master->reseed_prop_counter++;
755 public->reseed_prop_counter++;
756 if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 0, 1)))
758 reset_drbg_hook_ctx();
761 /* fill 'randomness' buffer with some arbitrary data */
762 memset(rand_add_buf, 'r', sizeof(rand_add_buf));
765 * Test whether all three DRBGs are reseeded by RAND_add()
767 RAND_add(rand_add_buf, sizeof(rand_add_buf), sizeof(rand_add_buf));
768 if (!TEST_true(test_drbg_reseed(1, master, public, private, 1, 1, 1)))
770 reset_drbg_hook_ctx();
774 * Test whether none of the DRBGs is reseed if the master fails to reseed
777 master->reseed_prop_counter++;
778 RAND_add(rand_add_buf, sizeof(rand_add_buf), sizeof(rand_add_buf));
779 if (!TEST_true(test_drbg_reseed(0, master, public, private, 0, 0, 0)))
781 reset_drbg_hook_ctx();
789 unhook_drbg(private);
794 #if defined(OPENSSL_THREADS)
795 static int multi_thread_rand_bytes_succeeded = 1;
796 static int multi_thread_rand_priv_bytes_succeeded = 1;
798 static void run_multi_thread_test(void)
800 unsigned char buf[256];
801 time_t start = time(NULL);
802 RAND_DRBG *public, *private;
804 public = RAND_DRBG_get0_public();
805 private = RAND_DRBG_get0_private();
806 RAND_DRBG_set_reseed_time_interval(public, 1);
807 RAND_DRBG_set_reseed_time_interval(private, 1);
810 if (RAND_bytes(buf, sizeof(buf)) <= 0)
811 multi_thread_rand_bytes_succeeded = 0;
812 if (RAND_priv_bytes(buf, sizeof(buf)) <= 0)
813 multi_thread_rand_priv_bytes_succeeded = 0;
815 while(time(NULL) - start < 5);
818 # if defined(OPENSSL_SYS_WINDOWS)
820 typedef HANDLE thread_t;
822 static DWORD WINAPI thread_run(LPVOID arg)
824 run_multi_thread_test();
828 static int run_thread(thread_t *t)
830 *t = CreateThread(NULL, 0, thread_run, NULL, 0, NULL);
834 static int wait_for_thread(thread_t thread)
836 return WaitForSingleObject(thread, INFINITE) == 0;
841 typedef pthread_t thread_t;
843 static void *thread_run(void *arg)
845 run_multi_thread_test();
849 static int run_thread(thread_t *t)
851 return pthread_create(t, NULL, thread_run, NULL) == 0;
854 static int wait_for_thread(thread_t thread)
856 return pthread_join(thread, NULL) == 0;
862 * The main thread will also run the test, so we'll have THREADS+1 parallel
867 static int test_multi_thread(void)
872 for (i = 0; i < THREADS; i++)
874 run_multi_thread_test();
875 for (i = 0; i < THREADS; i++)
876 wait_for_thread(t[i]);
878 if (!TEST_true(multi_thread_rand_bytes_succeeded))
880 if (!TEST_true(multi_thread_rand_priv_bytes_succeeded))
887 #ifdef OPENSSL_RAND_SEED_NONE
889 * Calculates the minimum buffer length which needs to be
890 * provided to RAND_seed() in order to successfully
891 * instantiate the DRBG.
893 * Copied from rand_drbg_seedlen() in rand_drbg.c
895 static size_t rand_drbg_seedlen(RAND_DRBG *drbg)
898 * If no os entropy source is available then RAND_seed(buffer, bufsize)
899 * is expected to succeed if and only if the buffer length satisfies
900 * the following requirements, which follow from the calculations
901 * in RAND_DRBG_instantiate().
903 size_t min_entropy = drbg->strength;
904 size_t min_entropylen = drbg->min_entropylen;
907 * Extra entropy for the random nonce in the absence of a
908 * get_nonce callback, see comment in RAND_DRBG_instantiate().
910 if (drbg->min_noncelen > 0 && drbg->get_nonce == NULL) {
911 min_entropy += drbg->strength / 2;
912 min_entropylen += drbg->min_noncelen;
916 * Convert entropy requirement from bits to bytes
917 * (dividing by 8 without rounding upwards, because
918 * all entropy requirements are divisible by 8).
922 /* Return a value that satisfies both requirements */
923 return min_entropy > min_entropylen ? min_entropy : min_entropylen;
925 #endif /*OPENSSL_RAND_SEED_NONE*/
928 * Test that instantiation with RAND_seed() works as expected
930 * If no os entropy source is available then RAND_seed(buffer, bufsize)
931 * is expected to succeed if and only if the buffer length is at least
932 * rand_drbg_seedlen(master) bytes.
934 * If an os entropy source is available then RAND_seed(buffer, bufsize)
935 * is expected to succeed always.
937 static int test_rand_seed(void)
939 RAND_DRBG *master = RAND_DRBG_get0_master();
940 unsigned char rand_buf[256];
942 #ifdef OPENSSL_RAND_SEED_NONE
943 size_t required_seed_buflen = rand_drbg_seedlen(master);
945 size_t required_seed_buflen = 0;
948 memset(rand_buf, 0xCD, sizeof(rand_buf));
950 for ( rand_buflen = 256 ; rand_buflen > 0 ; --rand_buflen ) {
951 RAND_DRBG_uninstantiate(master);
952 RAND_seed(rand_buf, rand_buflen);
954 if (!TEST_int_eq(RAND_status(),
955 (rand_buflen >= required_seed_buflen)))
963 * Test that adding additional data with RAND_add() works as expected
964 * when the master DRBG is instantiated (and below its reseed limit).
966 * This should succeed regardless of whether an os entropy source is
969 static int test_rand_add(void)
971 unsigned char rand_buf[256];
974 memset(rand_buf, 0xCD, sizeof(rand_buf));
976 /* make sure it's instantiated */
977 RAND_seed(rand_buf, sizeof(rand_buf));
978 if (!TEST_true(RAND_status()))
981 for ( rand_buflen = 256 ; rand_buflen > 0 ; --rand_buflen ) {
982 RAND_add(rand_buf, rand_buflen, 0.0);
983 if (!TEST_true(RAND_status()))
990 static int test_multi_set(void)
993 RAND_DRBG *drbg = NULL;
995 /* init drbg with default CTR initializer */
996 if (!TEST_ptr(drbg = RAND_DRBG_new(0, 0, NULL)))
998 /* change it to use hmac */
999 if (!TEST_true(RAND_DRBG_set(drbg, NID_sha1, RAND_DRBG_FLAG_HMAC)))
1002 if (!TEST_true(RAND_DRBG_set(drbg, NID_sha1, RAND_DRBG_FLAG_HMAC)))
1004 /* change it to use hash */
1005 if (!TEST_true(RAND_DRBG_set(drbg, NID_sha256, 0)))
1008 if (!TEST_true(RAND_DRBG_set(drbg, NID_sha256, 0)))
1010 /* change it to use ctr */
1011 if (!TEST_true(RAND_DRBG_set(drbg, NID_aes_192_ctr, 0)))
1014 if (!TEST_true(RAND_DRBG_set(drbg, NID_aes_192_ctr, 0)))
1016 if (!TEST_int_gt(RAND_DRBG_instantiate(drbg, NULL, 0), 0))
1021 uninstantiate(drbg);
1022 RAND_DRBG_free(drbg);
1026 static int test_set_defaults(void)
1028 RAND_DRBG *master, *public, *private;
1030 master = RAND_DRBG_get0_master();
1031 public = RAND_DRBG_get0_public();
1032 private = RAND_DRBG_get0_private();
1034 /* Check the default type and flags for master, public and private */
1035 return TEST_int_eq(master->type, RAND_DRBG_TYPE)
1036 && TEST_int_eq(master->flags,
1037 RAND_DRBG_FLAGS | RAND_DRBG_FLAG_MASTER)
1038 && TEST_int_eq(public->type, RAND_DRBG_TYPE)
1039 && TEST_int_eq(public->flags,
1040 RAND_DRBG_FLAGS | RAND_DRBG_FLAG_PUBLIC)
1041 && TEST_int_eq(private->type, RAND_DRBG_TYPE)
1042 && TEST_int_eq(private->flags,
1043 RAND_DRBG_FLAGS | RAND_DRBG_FLAG_PRIVATE)
1045 /* change master DRBG and check again */
1046 && TEST_true(RAND_DRBG_set_defaults(NID_sha256,
1047 RAND_DRBG_FLAG_MASTER))
1048 && TEST_true(RAND_DRBG_uninstantiate(master))
1049 && TEST_int_eq(master->type, NID_sha256)
1050 && TEST_int_eq(master->flags, RAND_DRBG_FLAG_MASTER)
1051 && TEST_int_eq(public->type, RAND_DRBG_TYPE)
1052 && TEST_int_eq(public->flags,
1053 RAND_DRBG_FLAGS | RAND_DRBG_FLAG_PUBLIC)
1054 && TEST_int_eq(private->type, RAND_DRBG_TYPE)
1055 && TEST_int_eq(private->flags,
1056 RAND_DRBG_FLAGS | RAND_DRBG_FLAG_PRIVATE)
1057 /* change private DRBG and check again */
1058 && TEST_true(RAND_DRBG_set_defaults(NID_sha256,
1059 RAND_DRBG_FLAG_PRIVATE|RAND_DRBG_FLAG_HMAC))
1060 && TEST_true(RAND_DRBG_uninstantiate(private))
1061 && TEST_int_eq(master->type, NID_sha256)
1062 && TEST_int_eq(master->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, NID_sha256)
1067 && TEST_int_eq(private->flags,
1068 RAND_DRBG_FLAG_PRIVATE | RAND_DRBG_FLAG_HMAC)
1069 /* change public DRBG and check again */
1070 && TEST_true(RAND_DRBG_set_defaults(NID_sha1,
1071 RAND_DRBG_FLAG_PUBLIC
1072 | RAND_DRBG_FLAG_HMAC))
1073 && TEST_true(RAND_DRBG_uninstantiate(public))
1074 && TEST_int_eq(master->type, NID_sha256)
1075 && TEST_int_eq(master->flags, RAND_DRBG_FLAG_MASTER)
1076 && TEST_int_eq(public->type, NID_sha1)
1077 && TEST_int_eq(public->flags,
1078 RAND_DRBG_FLAG_PUBLIC | RAND_DRBG_FLAG_HMAC)
1079 && TEST_int_eq(private->type, NID_sha256)
1080 && TEST_int_eq(private->flags,
1081 RAND_DRBG_FLAG_PRIVATE | RAND_DRBG_FLAG_HMAC)
1082 /* Change DRBG defaults and change public and check again */
1083 && TEST_true(RAND_DRBG_set_defaults(NID_sha256, 0))
1084 && TEST_true(RAND_DRBG_uninstantiate(public))
1085 && TEST_int_eq(public->type, NID_sha256)
1086 && TEST_int_eq(public->flags, RAND_DRBG_FLAG_PUBLIC)
1088 /* Change DRBG defaults and change master and check again */
1089 && TEST_true(RAND_DRBG_set_defaults(NID_aes_256_ctr,
1090 RAND_DRBG_FLAG_CTR_NO_DF))
1091 && TEST_true(RAND_DRBG_uninstantiate(master))
1092 && TEST_int_eq(master->type, NID_aes_256_ctr)
1093 && TEST_int_eq(master->flags,
1094 RAND_DRBG_FLAG_MASTER|RAND_DRBG_FLAG_CTR_NO_DF)
1096 /* Reset back to the standard defaults */
1097 && TEST_true(RAND_DRBG_set_defaults(RAND_DRBG_TYPE,
1099 | RAND_DRBG_FLAG_MASTER
1100 | RAND_DRBG_FLAG_PUBLIC
1101 | RAND_DRBG_FLAG_PRIVATE))
1102 && TEST_true(RAND_DRBG_uninstantiate(master))
1103 && TEST_true(RAND_DRBG_uninstantiate(public))
1104 && TEST_true(RAND_DRBG_uninstantiate(private));
1107 int setup_tests(void)
1109 app_data_index = RAND_DRBG_get_ex_new_index(0L, NULL, NULL, NULL, NULL);
1111 ADD_ALL_TESTS(test_kats, OSSL_NELEM(drbg_test));
1112 ADD_ALL_TESTS(test_error_checks, OSSL_NELEM(drbg_test));
1113 ADD_TEST(test_rand_drbg_reseed);
1114 ADD_TEST(test_rand_seed);
1115 ADD_TEST(test_rand_add);
1116 ADD_TEST(test_multi_set);
1117 ADD_TEST(test_set_defaults);
1118 #if defined(OPENSSL_THREADS)
1119 ADD_TEST(test_multi_thread);