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"
23 typedef struct drbg_selftest_data_st {
28 /* KAT data for no PR */
29 const unsigned char *entropy;
31 const unsigned char *nonce;
33 const unsigned char *pers;
35 const unsigned char *adin;
37 const unsigned char *entropyreseed;
38 size_t entropyreseedlen;
39 const unsigned char *adinreseed;
41 const unsigned char *adin2;
43 const unsigned char *expected;
45 const unsigned char *kat2;
49 const unsigned char *entropy_pr;
51 const unsigned char *nonce_pr;
53 const unsigned char *pers_pr;
55 const unsigned char *adin_pr;
57 const unsigned char *entropypr_pr;
58 size_t entropyprlen_pr;
59 const unsigned char *ading_pr;
61 const unsigned char *entropyg_pr;
62 size_t entropyglen_pr;
63 const unsigned char *kat_pr;
65 const unsigned char *kat2_pr;
69 #define make_drbg_test_data(nid, flag, pr, post) {\
71 pr##_entropyinput, sizeof(pr##_entropyinput), \
72 pr##_nonce, sizeof(pr##_nonce), \
73 pr##_personalizationstring, sizeof(pr##_personalizationstring), \
74 pr##_additionalinput, sizeof(pr##_additionalinput), \
75 pr##_entropyinputreseed, sizeof(pr##_entropyinputreseed), \
76 pr##_additionalinputreseed, sizeof(pr##_additionalinputreseed), \
77 pr##_additionalinput2, sizeof(pr##_additionalinput2), \
78 pr##_int_returnedbits, sizeof(pr##_int_returnedbits), \
79 pr##_returnedbits, sizeof(pr##_returnedbits), \
80 pr##_pr_entropyinput, sizeof(pr##_pr_entropyinput), \
81 pr##_pr_nonce, sizeof(pr##_pr_nonce), \
82 pr##_pr_personalizationstring, sizeof(pr##_pr_personalizationstring), \
83 pr##_pr_additionalinput, sizeof(pr##_pr_additionalinput), \
84 pr##_pr_entropyinputpr, sizeof(pr##_pr_entropyinputpr), \
85 pr##_pr_additionalinput2, sizeof(pr##_pr_additionalinput2), \
86 pr##_pr_entropyinputpr2, sizeof(pr##_pr_entropyinputpr2), \
87 pr##_pr_int_returnedbits, sizeof(pr##_pr_int_returnedbits), \
88 pr##_pr_returnedbits, sizeof(pr##_pr_returnedbits) \
91 #define make_drbg_test_data_use_df(nid, pr, p) \
92 make_drbg_test_data(nid, 0, pr, p)
94 #define make_drbg_test_data_no_df(nid, pr, p) \
95 make_drbg_test_data(nid, RAND_DRBG_FLAG_CTR_NO_DF, pr, p)
97 static DRBG_SELFTEST_DATA drbg_test[] = {
98 make_drbg_test_data_no_df (NID_aes_128_ctr, aes_128_no_df, 0),
99 make_drbg_test_data_no_df (NID_aes_192_ctr, aes_192_no_df, 0),
100 make_drbg_test_data_no_df (NID_aes_256_ctr, aes_256_no_df, 1),
101 make_drbg_test_data_use_df(NID_aes_128_ctr, aes_128_use_df, 0),
102 make_drbg_test_data_use_df(NID_aes_192_ctr, aes_192_use_df, 0),
103 make_drbg_test_data_use_df(NID_aes_256_ctr, aes_256_use_df, 1),
106 static int app_data_index;
109 * Test context data, attached as EXDATA to the RAND_DRBG
111 typedef struct test_ctx_st {
112 const unsigned char *entropy;
115 const unsigned char *nonce;
120 static size_t kat_entropy(RAND_DRBG *drbg, unsigned char **pout,
121 int entropy, size_t min_len, size_t max_len)
123 TEST_CTX *t = (TEST_CTX *)RAND_DRBG_get_ex_data(drbg, app_data_index);
126 *pout = (unsigned char *)t->entropy;
127 return t->entropylen;
130 static size_t kat_nonce(RAND_DRBG *drbg, unsigned char **pout,
131 int entropy, size_t min_len, size_t max_len)
133 TEST_CTX *t = (TEST_CTX *)RAND_DRBG_get_ex_data(drbg, app_data_index);
136 *pout = (unsigned char *)t->nonce;
140 static int uninstantiate(RAND_DRBG *drbg)
142 int ret = drbg == NULL ? 1 : RAND_DRBG_uninstantiate(drbg);
149 * Do a single KAT test. Return 0 on failure.
151 static int single_kat(DRBG_SELFTEST_DATA *td)
153 RAND_DRBG *drbg = NULL;
156 unsigned char buff[1024];
159 * Test without PR: Instantiate DRBG with test entropy, nonce and
160 * personalisation string.
162 if (!TEST_ptr(drbg = RAND_DRBG_new(td->nid, td->flags, NULL)))
164 if (!TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
169 memset(&t, 0, sizeof(t));
170 t.entropy = td->entropy;
171 t.entropylen = td->entropylen;
173 t.noncelen = td->noncelen;
174 RAND_DRBG_set_ex_data(drbg, app_data_index, &t);
176 if (!TEST_true(RAND_DRBG_instantiate(drbg, td->pers, td->perslen))
177 || !TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
178 td->adin, td->adinlen))
179 || !TEST_mem_eq(td->expected, td->exlen, buff, td->exlen))
182 /* Reseed DRBG with test entropy and additional input */
183 t.entropy = td->entropyreseed;
184 t.entropylen = td->entropyreseedlen;
185 if (!TEST_true(RAND_DRBG_reseed(drbg, td->adinreseed, td->adinreseedlen)
186 || !TEST_true(RAND_DRBG_generate(drbg, buff, td->kat2len, 0,
187 td->adin2, td->adin2len))
188 || !TEST_mem_eq(td->kat2, td->kat2len, buff, td->kat2len)))
193 * Now test with PR: Instantiate DRBG with test entropy, nonce and
194 * personalisation string.
196 if (!TEST_true(RAND_DRBG_set(drbg, td->nid, td->flags))
197 || !TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
200 RAND_DRBG_set_ex_data(drbg, app_data_index, &t);
201 t.entropy = td->entropy_pr;
202 t.entropylen = td->entropylen_pr;
203 t.nonce = td->nonce_pr;
204 t.noncelen = td->noncelen_pr;
207 if (!TEST_true(RAND_DRBG_instantiate(drbg, td->pers_pr, td->perslen_pr)))
211 * Now generate with PR: we need to supply entropy as this will
212 * perform a reseed operation.
214 t.entropy = td->entropypr_pr;
215 t.entropylen = td->entropyprlen_pr;
216 if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->katlen_pr, 1,
217 td->adin_pr, td->adinlen_pr))
218 || !TEST_mem_eq(td->kat_pr, td->katlen_pr, buff, td->katlen_pr))
222 * Now generate again with PR: supply new entropy again.
224 t.entropy = td->entropyg_pr;
225 t.entropylen = td->entropyglen_pr;
227 if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->kat2len_pr, 1,
228 td->ading_pr, td->adinglen_pr))
229 || !TEST_mem_eq(td->kat2_pr, td->kat2len_pr,
230 buff, td->kat2len_pr))
235 RAND_DRBG_free(drbg);
236 return failures == 0;
240 * Initialise a DRBG based on selftest data
242 static int init(RAND_DRBG *drbg, DRBG_SELFTEST_DATA *td, TEST_CTX *t)
244 if (!TEST_true(RAND_DRBG_set(drbg, td->nid, td->flags))
245 || !TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
248 RAND_DRBG_set_ex_data(drbg, app_data_index, t);
249 t->entropy = td->entropy;
250 t->entropylen = td->entropylen;
251 t->nonce = td->nonce;
252 t->noncelen = td->noncelen;
259 * Initialise and instantiate DRBG based on selftest data
261 static int instantiate(RAND_DRBG *drbg, DRBG_SELFTEST_DATA *td,
264 if (!TEST_true(init(drbg, td, t))
265 || !TEST_true(RAND_DRBG_instantiate(drbg, td->pers, td->perslen)))
271 * Perform extensive error checking as required by SP800-90.
272 * Induce several failure modes and check an error condition is set.
274 static int error_check(DRBG_SELFTEST_DATA *td)
276 static char zero[sizeof(RAND_DRBG)];
277 RAND_DRBG *drbg = NULL;
279 unsigned char buff[1024];
280 unsigned int generate_counter_tmp;
283 if (!TEST_ptr(drbg = RAND_DRBG_new(0, 0, NULL)))
287 * Personalisation string tests
290 /* Test detection of too large personlisation string */
291 if (!init(drbg, td, &t)
292 || RAND_DRBG_instantiate(drbg, td->pers, drbg->max_perslen + 1) > 0)
296 * Entropy source tests
299 /* Test entropy source failure detecion: i.e. returns no data */
301 if (TEST_int_le(RAND_DRBG_instantiate(drbg, td->pers, td->perslen), 0))
304 /* Try to generate output from uninstantiated DRBG */
305 if (!TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
306 td->adin, td->adinlen))
307 || !uninstantiate(drbg))
310 /* Test insufficient entropy */
311 t.entropylen = drbg->min_entropylen - 1;
312 if (!init(drbg, td, &t)
313 || RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0
314 || !uninstantiate(drbg))
317 /* Test too much entropy */
318 t.entropylen = drbg->max_entropylen + 1;
319 if (!init(drbg, td, &t)
320 || RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0
321 || !uninstantiate(drbg))
328 /* Test too small nonce */
329 if (drbg->min_noncelen) {
330 t.noncelen = drbg->min_noncelen - 1;
331 if (!init(drbg, td, &t)
332 || RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0
333 || !uninstantiate(drbg))
337 /* Test too large nonce */
338 if (drbg->max_noncelen) {
339 t.noncelen = drbg->max_noncelen + 1;
340 if (!init(drbg, td, &t)
341 || RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0
342 || !uninstantiate(drbg))
346 /* Instantiate with valid data, Check generation is now OK */
347 if (!instantiate(drbg, td, &t)
348 || !TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
349 td->adin, td->adinlen)))
352 /* Request too much data for one request */
353 if (!TEST_false(RAND_DRBG_generate(drbg, buff, drbg->max_request + 1, 0,
354 td->adin, td->adinlen)))
357 /* Try too large additional input */
358 if (!TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
359 td->adin, drbg->max_adinlen + 1)))
363 * Check prediction resistance request fails if entropy source
367 if (TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 1,
368 td->adin, td->adinlen))
369 || !uninstantiate(drbg))
372 /* Instantiate again with valid data */
373 if (!instantiate(drbg, td, &t))
375 generate_counter_tmp = drbg->generate_counter;
376 drbg->generate_counter = drbg->reseed_interval;
378 /* Generate output and check entropy has been requested for reseed */
380 if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
381 td->adin, td->adinlen))
382 || !TEST_int_eq(t.entropycnt, 1)
383 || !TEST_int_eq(drbg->generate_counter, generate_counter_tmp + 1)
384 || !uninstantiate(drbg))
388 * Check prediction resistance request fails if entropy source
392 if (!TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 1,
393 td->adin, td->adinlen))
394 || !uninstantiate(drbg))
397 /* Test reseed counter works */
398 if (!instantiate(drbg, td, &t))
400 generate_counter_tmp = drbg->generate_counter;
401 drbg->generate_counter = drbg->reseed_interval;
403 /* Generate output and check entropy has been requested for reseed */
405 if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
406 td->adin, td->adinlen))
407 || !TEST_int_eq(t.entropycnt, 1)
408 || !TEST_int_eq(drbg->generate_counter, generate_counter_tmp + 1)
409 || !uninstantiate(drbg))
413 * Explicit reseed tests
416 /* Test explicit reseed with too large additional input */
417 if (!init(drbg, td, &t)
418 || RAND_DRBG_reseed(drbg, td->adin, drbg->max_adinlen + 1) > 0)
421 /* Test explicit reseed with entropy source failure */
423 if (!TEST_int_le(RAND_DRBG_reseed(drbg, td->adin, td->adinlen), 0)
424 || !uninstantiate(drbg))
427 /* Test explicit reseed with too much entropy */
428 if (!init(drbg, td, &t))
430 t.entropylen = drbg->max_entropylen + 1;
431 if (!TEST_int_le(RAND_DRBG_reseed(drbg, td->adin, td->adinlen), 0)
432 || !uninstantiate(drbg))
435 /* Test explicit reseed with too little entropy */
436 if (!init(drbg, td, &t))
438 t.entropylen = drbg->min_entropylen - 1;
439 if (!TEST_int_le(RAND_DRBG_reseed(drbg, td->adin, td->adinlen), 0)
440 || !uninstantiate(drbg))
443 /* Standard says we have to check uninstantiate really zeroes */
444 if (!TEST_mem_eq(zero, sizeof(drbg->data), &drbg->data, sizeof(drbg->data)))
451 RAND_DRBG_free(drbg);
455 static int test_kats(int i)
457 DRBG_SELFTEST_DATA *td = &drbg_test[i];
468 static int test_error_checks(int i)
470 DRBG_SELFTEST_DATA *td = &drbg_test[i];
482 * Hook context data, attached as EXDATA to the RAND_DRBG
484 typedef struct hook_ctx_st {
487 * Currently, all DRBGs use the same get_entropy() callback.
488 * The tests however, don't assume this and store
489 * the original callback for every DRBG separately.
491 RAND_DRBG_get_entropy_fn get_entropy;
492 /* forces a failure of the get_entropy() call if nonzero */
494 /* counts successful reseeds */
498 static HOOK_CTX master_ctx, public_ctx, private_ctx;
500 static HOOK_CTX *get_hook_ctx(RAND_DRBG *drbg)
502 return (HOOK_CTX *)RAND_DRBG_get_ex_data(drbg, app_data_index);
505 /* Intercepts and counts calls to the get_entropy() callback */
506 static size_t get_entropy_hook(RAND_DRBG *drbg, unsigned char **pout,
507 int entropy, size_t min_len, size_t max_len)
510 HOOK_CTX *ctx = get_hook_ctx(drbg);
515 ret = ctx->get_entropy(
516 drbg, pout, entropy, min_len, max_len);
523 /* Installs a hook for the get_entropy() callback of the given drbg */
524 static void hook_drbg(RAND_DRBG *drbg, HOOK_CTX *ctx)
526 memset(ctx, 0, sizeof(*ctx));
528 ctx->get_entropy = drbg->get_entropy;
529 drbg->get_entropy = get_entropy_hook;
530 RAND_DRBG_set_ex_data(drbg, app_data_index, ctx);
533 /* Installs the hook for the get_entropy() callback of the given drbg */
534 static void unhook_drbg(RAND_DRBG *drbg)
536 HOOK_CTX *ctx = get_hook_ctx(drbg);
538 drbg->get_entropy = ctx->get_entropy;
539 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DRBG, drbg, &drbg->ex_data);
542 /* Resets the given hook context */
543 static void reset_hook_ctx(HOOK_CTX *ctx)
546 ctx->reseed_count = 0;
549 /* Resets all drbg hook contexts */
550 static void reset_drbg_hook_ctx()
552 reset_hook_ctx(&master_ctx);
553 reset_hook_ctx(&public_ctx);
554 reset_hook_ctx(&private_ctx);
558 * Generates random output using RAND_bytes() and RAND_priv_bytes()
559 * and checks whether the three shared DRBGs were reseeded as
562 * |expect_success|: expected outcome (as reported by RAND_status())
563 * |master|, |public|, |private|: pointers to the three shared DRBGs
564 * |expect_xxx_reseed| =
565 * 1: it is expected that the specified DRBG is reseeded
566 * 0: it is expected that the specified DRBG is not reseeded
567 * -1: don't check whether the specified DRBG was reseeded or not
569 static int test_drbg_reseed(int expect_success,
573 int expect_master_reseed,
574 int expect_public_reseed,
575 int expect_private_reseed
578 unsigned char buf[32];
579 time_t before_reseed, after_reseed;
580 int expected_state = (expect_success ? DRBG_READY : DRBG_ERROR);
583 * step 1: check preconditions
586 /* Test whether seed propagation is enabled */
587 if (!TEST_int_ne(master->reseed_counter, 0)
588 || !TEST_int_ne(public->reseed_counter, 0)
589 || !TEST_int_ne(private->reseed_counter, 0))
592 /* Check whether the master DRBG's reseed counter is the largest one */
593 if (!TEST_int_le(public->reseed_counter, master->reseed_counter)
594 || !TEST_int_le(private->reseed_counter, master->reseed_counter))
598 * step 2: generate random output
601 /* Generate random output from the public and private DRBG */
602 before_reseed = expect_master_reseed == 1 ? time(NULL) : 0;
603 if (!TEST_int_eq(RAND_bytes(buf, sizeof(buf)), expect_success)
604 || !TEST_int_eq(RAND_priv_bytes(buf, sizeof(buf)), expect_success))
606 after_reseed = time(NULL);
610 * step 3: check postconditions
613 /* Test whether reseeding succeeded as expected */
614 if (!TEST_int_eq(master->state, expected_state)
615 || !TEST_int_eq(public->state, expected_state)
616 || !TEST_int_eq(private->state, expected_state))
619 if (expect_master_reseed >= 0) {
620 /* Test whether master DRBG was reseeded as expected */
621 if (!TEST_int_eq(master_ctx.reseed_count, expect_master_reseed))
625 if (expect_public_reseed >= 0) {
626 /* Test whether public DRBG was reseeded as expected */
627 if (!TEST_int_eq(public_ctx.reseed_count, expect_public_reseed))
631 if (expect_private_reseed >= 0) {
632 /* Test whether public DRBG was reseeded as expected */
633 if (!TEST_int_eq(private_ctx.reseed_count, expect_private_reseed))
637 if (expect_success == 1) {
638 /* Test whether all three reseed counters are synchronized */
639 if (!TEST_int_eq(public->reseed_counter, master->reseed_counter)
640 || !TEST_int_eq(private->reseed_counter, master->reseed_counter))
643 /* Test whether reseed time of master DRBG is set correctly */
644 if (!TEST_time_t_le(before_reseed, master->reseed_time)
645 || !TEST_time_t_le(master->reseed_time, after_reseed))
648 /* Test whether reseed times of child DRBGs are synchronized with master */
649 if (!TEST_time_t_ge(public->reseed_time, master->reseed_time)
650 || !TEST_time_t_ge(private->reseed_time, master->reseed_time))
660 * Test whether the default rand_method (RAND_OpenSSL()) is
661 * setup correctly, in particular whether reseeding works
664 static int test_rand_reseed(void)
666 RAND_DRBG *master, *public, *private;
667 unsigned char rand_add_buf[256];
670 /* Check whether RAND_OpenSSL() is the default method */
671 if (!TEST_ptr_eq(RAND_get_rand_method(), RAND_OpenSSL()))
674 /* All three DRBGs should be non-null */
675 if (!TEST_ptr(master = RAND_DRBG_get0_master())
676 || !TEST_ptr(public = RAND_DRBG_get0_public())
677 || !TEST_ptr(private = RAND_DRBG_get0_private()))
680 /* There should be three distinct DRBGs, two of them chained to master */
681 if (!TEST_ptr_ne(public, private)
682 || !TEST_ptr_ne(public, master)
683 || !TEST_ptr_ne(private, master)
684 || !TEST_ptr_eq(public->parent, master)
685 || !TEST_ptr_eq(private->parent, master))
688 /* Install hooks for the following tests */
689 hook_drbg(master, &master_ctx);
690 hook_drbg(public, &public_ctx);
691 hook_drbg(private, &private_ctx);
694 * Test initial state of shared DRBs
696 if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 0, 0)))
698 reset_drbg_hook_ctx();
701 * Test whether the public and private DRBG are both reseeded when their
702 * reseed counters differ from the master's reseed counter.
704 master->reseed_counter++;
705 if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 1, 1)))
707 reset_drbg_hook_ctx();
710 * Test whether the public DRBG is reseeded when its reseed counter differs
711 * from the master's reseed counter.
713 master->reseed_counter++;
714 private->reseed_counter++;
715 if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 1, 0)))
717 reset_drbg_hook_ctx();
720 * Test whether the private DRBG is reseeded when its reseed counter differs
721 * from the master's reseed counter.
723 master->reseed_counter++;
724 public->reseed_counter++;
725 if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 0, 1)))
727 reset_drbg_hook_ctx();
730 /* fill 'randomness' buffer with some arbitrary data */
731 memset(rand_add_buf, 'r', sizeof(rand_add_buf));
734 * Test whether all three DRBGs are reseeded by RAND_add()
736 RAND_add(rand_add_buf, sizeof(rand_add_buf), sizeof(rand_add_buf));
737 if (!TEST_true(test_drbg_reseed(1, master, public, private, 1, 1, 1)))
739 reset_drbg_hook_ctx();
743 * Test whether none of the DRBGs is reseed if the master fails to reseed
746 master->reseed_counter++;
747 RAND_add(rand_add_buf, sizeof(rand_add_buf), sizeof(rand_add_buf));
748 if (!TEST_true(test_drbg_reseed(0, master, public, private, 0, 0, 0)))
750 reset_drbg_hook_ctx();
758 unhook_drbg(private);
764 int setup_tests(void)
766 app_data_index = RAND_DRBG_get_ex_new_index(0L, NULL, NULL, NULL, NULL);
768 ADD_ALL_TESTS(test_kats, OSSL_NELEM(drbg_test));
769 ADD_ALL_TESTS(test_error_checks, OSSL_NELEM(drbg_test));
770 ADD_TEST(test_rand_reseed);