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,
122 int prediction_resistance)
124 TEST_CTX *t = (TEST_CTX *)RAND_DRBG_get_ex_data(drbg, app_data_index);
127 *pout = (unsigned char *)t->entropy;
128 return t->entropylen;
131 static size_t kat_nonce(RAND_DRBG *drbg, unsigned char **pout,
132 int entropy, size_t min_len, size_t max_len)
134 TEST_CTX *t = (TEST_CTX *)RAND_DRBG_get_ex_data(drbg, app_data_index);
137 *pout = (unsigned char *)t->nonce;
141 static int uninstantiate(RAND_DRBG *drbg)
143 int ret = drbg == NULL ? 1 : RAND_DRBG_uninstantiate(drbg);
150 * Do a single KAT test. Return 0 on failure.
152 static int single_kat(DRBG_SELFTEST_DATA *td)
154 RAND_DRBG *drbg = NULL;
157 unsigned char buff[1024];
160 * Test without PR: Instantiate DRBG with test entropy, nonce and
161 * personalisation string.
163 if (!TEST_ptr(drbg = RAND_DRBG_new(td->nid, td->flags, NULL)))
165 if (!TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
170 memset(&t, 0, sizeof(t));
171 t.entropy = td->entropy;
172 t.entropylen = td->entropylen;
174 t.noncelen = td->noncelen;
175 RAND_DRBG_set_ex_data(drbg, app_data_index, &t);
177 if (!TEST_true(RAND_DRBG_instantiate(drbg, td->pers, td->perslen))
178 || !TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
179 td->adin, td->adinlen))
180 || !TEST_mem_eq(td->expected, td->exlen, buff, td->exlen))
183 /* Reseed DRBG with test entropy and additional input */
184 t.entropy = td->entropyreseed;
185 t.entropylen = td->entropyreseedlen;
186 if (!TEST_true(RAND_DRBG_reseed(drbg, td->adinreseed, td->adinreseedlen, 0)
187 || !TEST_true(RAND_DRBG_generate(drbg, buff, td->kat2len, 0,
188 td->adin2, td->adin2len))
189 || !TEST_mem_eq(td->kat2, td->kat2len, buff, td->kat2len)))
194 * Now test with PR: Instantiate DRBG with test entropy, nonce and
195 * personalisation string.
197 if (!TEST_true(RAND_DRBG_set(drbg, td->nid, td->flags))
198 || !TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
201 RAND_DRBG_set_ex_data(drbg, app_data_index, &t);
202 t.entropy = td->entropy_pr;
203 t.entropylen = td->entropylen_pr;
204 t.nonce = td->nonce_pr;
205 t.noncelen = td->noncelen_pr;
208 if (!TEST_true(RAND_DRBG_instantiate(drbg, td->pers_pr, td->perslen_pr)))
212 * Now generate with PR: we need to supply entropy as this will
213 * perform a reseed operation.
215 t.entropy = td->entropypr_pr;
216 t.entropylen = td->entropyprlen_pr;
217 if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->katlen_pr, 1,
218 td->adin_pr, td->adinlen_pr))
219 || !TEST_mem_eq(td->kat_pr, td->katlen_pr, buff, td->katlen_pr))
223 * Now generate again with PR: supply new entropy again.
225 t.entropy = td->entropyg_pr;
226 t.entropylen = td->entropyglen_pr;
228 if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->kat2len_pr, 1,
229 td->ading_pr, td->adinglen_pr))
230 || !TEST_mem_eq(td->kat2_pr, td->kat2len_pr,
231 buff, td->kat2len_pr))
236 RAND_DRBG_free(drbg);
237 return failures == 0;
241 * Initialise a DRBG based on selftest data
243 static int init(RAND_DRBG *drbg, DRBG_SELFTEST_DATA *td, TEST_CTX *t)
245 if (!TEST_true(RAND_DRBG_set(drbg, td->nid, td->flags))
246 || !TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
249 RAND_DRBG_set_ex_data(drbg, app_data_index, t);
250 t->entropy = td->entropy;
251 t->entropylen = td->entropylen;
252 t->nonce = td->nonce;
253 t->noncelen = td->noncelen;
260 * Initialise and instantiate DRBG based on selftest data
262 static int instantiate(RAND_DRBG *drbg, DRBG_SELFTEST_DATA *td,
265 if (!TEST_true(init(drbg, td, t))
266 || !TEST_true(RAND_DRBG_instantiate(drbg, td->pers, td->perslen)))
272 * Perform extensive error checking as required by SP800-90.
273 * Induce several failure modes and check an error condition is set.
275 static int error_check(DRBG_SELFTEST_DATA *td)
277 static char zero[sizeof(RAND_DRBG)];
278 RAND_DRBG *drbg = NULL;
280 unsigned char buff[1024];
281 unsigned int generate_counter_tmp;
284 if (!TEST_ptr(drbg = RAND_DRBG_new(0, 0, NULL)))
288 * Personalisation string tests
291 /* Test detection of too large personlisation string */
292 if (!init(drbg, td, &t)
293 || RAND_DRBG_instantiate(drbg, td->pers, drbg->max_perslen + 1) > 0)
297 * Entropy source tests
300 /* Test entropy source failure detecion: i.e. returns no data */
302 if (TEST_int_le(RAND_DRBG_instantiate(drbg, td->pers, td->perslen), 0))
305 /* Try to generate output from uninstantiated DRBG */
306 if (!TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
307 td->adin, td->adinlen))
308 || !uninstantiate(drbg))
311 /* Test insufficient entropy */
312 t.entropylen = drbg->min_entropylen - 1;
313 if (!init(drbg, td, &t)
314 || RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0
315 || !uninstantiate(drbg))
318 /* Test too much entropy */
319 t.entropylen = drbg->max_entropylen + 1;
320 if (!init(drbg, td, &t)
321 || RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0
322 || !uninstantiate(drbg))
329 /* Test too small nonce */
330 if (drbg->min_noncelen) {
331 t.noncelen = drbg->min_noncelen - 1;
332 if (!init(drbg, td, &t)
333 || RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0
334 || !uninstantiate(drbg))
338 /* Test too large nonce */
339 if (drbg->max_noncelen) {
340 t.noncelen = drbg->max_noncelen + 1;
341 if (!init(drbg, td, &t)
342 || RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0
343 || !uninstantiate(drbg))
347 /* Instantiate with valid data, Check generation is now OK */
348 if (!instantiate(drbg, td, &t)
349 || !TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
350 td->adin, td->adinlen)))
353 /* Request too much data for one request */
354 if (!TEST_false(RAND_DRBG_generate(drbg, buff, drbg->max_request + 1, 0,
355 td->adin, td->adinlen)))
358 /* Try too large additional input */
359 if (!TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
360 td->adin, drbg->max_adinlen + 1)))
364 * Check prediction resistance request fails if entropy source
368 if (TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 1,
369 td->adin, td->adinlen))
370 || !uninstantiate(drbg))
373 /* Instantiate again with valid data */
374 if (!instantiate(drbg, td, &t))
376 generate_counter_tmp = drbg->generate_counter;
377 drbg->generate_counter = drbg->reseed_interval;
379 /* Generate output and check entropy has been requested for reseed */
381 if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
382 td->adin, td->adinlen))
383 || !TEST_int_eq(t.entropycnt, 1)
384 || !TEST_int_eq(drbg->generate_counter, generate_counter_tmp + 1)
385 || !uninstantiate(drbg))
389 * Check prediction resistance request fails if entropy source
393 if (!TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 1,
394 td->adin, td->adinlen))
395 || !uninstantiate(drbg))
398 /* Test reseed counter works */
399 if (!instantiate(drbg, td, &t))
401 generate_counter_tmp = drbg->generate_counter;
402 drbg->generate_counter = drbg->reseed_interval;
404 /* Generate output and check entropy has been requested for reseed */
406 if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
407 td->adin, td->adinlen))
408 || !TEST_int_eq(t.entropycnt, 1)
409 || !TEST_int_eq(drbg->generate_counter, generate_counter_tmp + 1)
410 || !uninstantiate(drbg))
414 * Explicit reseed tests
417 /* Test explicit reseed with too large additional input */
418 if (!init(drbg, td, &t)
419 || RAND_DRBG_reseed(drbg, td->adin, drbg->max_adinlen + 1, 0) > 0)
422 /* Test explicit reseed with entropy source failure */
424 if (!TEST_int_le(RAND_DRBG_reseed(drbg, td->adin, td->adinlen, 0), 0)
425 || !uninstantiate(drbg))
428 /* Test explicit reseed with too much entropy */
429 if (!init(drbg, td, &t))
431 t.entropylen = drbg->max_entropylen + 1;
432 if (!TEST_int_le(RAND_DRBG_reseed(drbg, td->adin, td->adinlen, 0), 0)
433 || !uninstantiate(drbg))
436 /* Test explicit reseed with too little entropy */
437 if (!init(drbg, td, &t))
439 t.entropylen = drbg->min_entropylen - 1;
440 if (!TEST_int_le(RAND_DRBG_reseed(drbg, td->adin, td->adinlen, 0), 0)
441 || !uninstantiate(drbg))
444 /* Standard says we have to check uninstantiate really zeroes */
445 if (!TEST_mem_eq(zero, sizeof(drbg->data), &drbg->data, sizeof(drbg->data)))
452 RAND_DRBG_free(drbg);
456 static int test_kats(int i)
458 DRBG_SELFTEST_DATA *td = &drbg_test[i];
469 static int test_error_checks(int i)
471 DRBG_SELFTEST_DATA *td = &drbg_test[i];
483 * Hook context data, attached as EXDATA to the RAND_DRBG
485 typedef struct hook_ctx_st {
488 * Currently, all DRBGs use the same get_entropy() callback.
489 * The tests however, don't assume this and store
490 * the original callback for every DRBG separately.
492 RAND_DRBG_get_entropy_fn get_entropy;
493 /* forces a failure of the get_entropy() call if nonzero */
495 /* counts successful reseeds */
499 static HOOK_CTX master_ctx, public_ctx, private_ctx;
501 static HOOK_CTX *get_hook_ctx(RAND_DRBG *drbg)
503 return (HOOK_CTX *)RAND_DRBG_get_ex_data(drbg, app_data_index);
506 /* Intercepts and counts calls to the get_entropy() callback */
507 static size_t get_entropy_hook(RAND_DRBG *drbg, unsigned char **pout,
508 int entropy, size_t min_len, size_t max_len,
509 int prediction_resistance)
512 HOOK_CTX *ctx = get_hook_ctx(drbg);
517 ret = ctx->get_entropy(drbg, pout, entropy, min_len, max_len,
518 prediction_resistance);
525 /* Installs a hook for the get_entropy() callback of the given drbg */
526 static void hook_drbg(RAND_DRBG *drbg, HOOK_CTX *ctx)
528 memset(ctx, 0, sizeof(*ctx));
530 ctx->get_entropy = drbg->get_entropy;
531 drbg->get_entropy = get_entropy_hook;
532 RAND_DRBG_set_ex_data(drbg, app_data_index, ctx);
535 /* Installs the hook for the get_entropy() callback of the given drbg */
536 static void unhook_drbg(RAND_DRBG *drbg)
538 HOOK_CTX *ctx = get_hook_ctx(drbg);
540 drbg->get_entropy = ctx->get_entropy;
541 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DRBG, drbg, &drbg->ex_data);
544 /* Resets the given hook context */
545 static void reset_hook_ctx(HOOK_CTX *ctx)
548 ctx->reseed_count = 0;
551 /* Resets all drbg hook contexts */
552 static void reset_drbg_hook_ctx()
554 reset_hook_ctx(&master_ctx);
555 reset_hook_ctx(&public_ctx);
556 reset_hook_ctx(&private_ctx);
560 * Generates random output using RAND_bytes() and RAND_priv_bytes()
561 * and checks whether the three shared DRBGs were reseeded as
564 * |expect_success|: expected outcome (as reported by RAND_status())
565 * |master|, |public|, |private|: pointers to the three shared DRBGs
566 * |expect_xxx_reseed| =
567 * 1: it is expected that the specified DRBG is reseeded
568 * 0: it is expected that the specified DRBG is not reseeded
569 * -1: don't check whether the specified DRBG was reseeded or not
571 static int test_drbg_reseed(int expect_success,
575 int expect_master_reseed,
576 int expect_public_reseed,
577 int expect_private_reseed
580 unsigned char buf[32];
581 time_t before_reseed, after_reseed;
582 int expected_state = (expect_success ? DRBG_READY : DRBG_ERROR);
585 * step 1: check preconditions
588 /* Test whether seed propagation is enabled */
589 if (!TEST_int_ne(master->reseed_counter, 0)
590 || !TEST_int_ne(public->reseed_counter, 0)
591 || !TEST_int_ne(private->reseed_counter, 0))
594 /* Check whether the master DRBG's reseed counter is the largest one */
595 if (!TEST_int_le(public->reseed_counter, master->reseed_counter)
596 || !TEST_int_le(private->reseed_counter, master->reseed_counter))
600 * step 2: generate random output
603 /* Generate random output from the public and private DRBG */
604 before_reseed = expect_master_reseed == 1 ? time(NULL) : 0;
605 if (!TEST_int_eq(RAND_bytes(buf, sizeof(buf)), expect_success)
606 || !TEST_int_eq(RAND_priv_bytes(buf, sizeof(buf)), expect_success))
608 after_reseed = time(NULL);
612 * step 3: check postconditions
615 /* Test whether reseeding succeeded as expected */
616 if (!TEST_int_eq(master->state, expected_state)
617 || !TEST_int_eq(public->state, expected_state)
618 || !TEST_int_eq(private->state, expected_state))
621 if (expect_master_reseed >= 0) {
622 /* Test whether master DRBG was reseeded as expected */
623 if (!TEST_int_eq(master_ctx.reseed_count, expect_master_reseed))
627 if (expect_public_reseed >= 0) {
628 /* Test whether public DRBG was reseeded as expected */
629 if (!TEST_int_eq(public_ctx.reseed_count, expect_public_reseed))
633 if (expect_private_reseed >= 0) {
634 /* Test whether public DRBG was reseeded as expected */
635 if (!TEST_int_eq(private_ctx.reseed_count, expect_private_reseed))
639 if (expect_success == 1) {
640 /* Test whether all three reseed counters are synchronized */
641 if (!TEST_int_eq(public->reseed_counter, master->reseed_counter)
642 || !TEST_int_eq(private->reseed_counter, master->reseed_counter))
645 /* Test whether reseed time of master DRBG is set correctly */
646 if (!TEST_time_t_le(before_reseed, master->reseed_time)
647 || !TEST_time_t_le(master->reseed_time, after_reseed))
650 /* Test whether reseed times of child DRBGs are synchronized with master */
651 if (!TEST_time_t_ge(public->reseed_time, master->reseed_time)
652 || !TEST_time_t_ge(private->reseed_time, master->reseed_time))
662 * Test whether the default rand_method (RAND_OpenSSL()) is
663 * setup correctly, in particular whether reseeding works
666 static int test_rand_reseed(void)
668 RAND_DRBG *master, *public, *private;
669 unsigned char rand_add_buf[256];
672 /* Check whether RAND_OpenSSL() is the default method */
673 if (!TEST_ptr_eq(RAND_get_rand_method(), RAND_OpenSSL()))
676 /* All three DRBGs should be non-null */
677 if (!TEST_ptr(master = RAND_DRBG_get0_master())
678 || !TEST_ptr(public = RAND_DRBG_get0_public())
679 || !TEST_ptr(private = RAND_DRBG_get0_private()))
682 /* There should be three distinct DRBGs, two of them chained to master */
683 if (!TEST_ptr_ne(public, private)
684 || !TEST_ptr_ne(public, master)
685 || !TEST_ptr_ne(private, master)
686 || !TEST_ptr_eq(public->parent, master)
687 || !TEST_ptr_eq(private->parent, master))
690 /* Install hooks for the following tests */
691 hook_drbg(master, &master_ctx);
692 hook_drbg(public, &public_ctx);
693 hook_drbg(private, &private_ctx);
696 * Test initial state of shared DRBs
698 if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 0, 0)))
700 reset_drbg_hook_ctx();
703 * Test whether the public and private DRBG are both reseeded when their
704 * reseed counters differ from the master's reseed counter.
706 master->reseed_counter++;
707 if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 1, 1)))
709 reset_drbg_hook_ctx();
712 * Test whether the public DRBG is reseeded when its reseed counter differs
713 * from the master's reseed counter.
715 master->reseed_counter++;
716 private->reseed_counter++;
717 if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 1, 0)))
719 reset_drbg_hook_ctx();
722 * Test whether the private DRBG is reseeded when its reseed counter differs
723 * from the master's reseed counter.
725 master->reseed_counter++;
726 public->reseed_counter++;
727 if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 0, 1)))
729 reset_drbg_hook_ctx();
732 /* fill 'randomness' buffer with some arbitrary data */
733 memset(rand_add_buf, 'r', sizeof(rand_add_buf));
736 * Test whether all three DRBGs are reseeded by RAND_add()
738 RAND_add(rand_add_buf, sizeof(rand_add_buf), sizeof(rand_add_buf));
739 if (!TEST_true(test_drbg_reseed(1, master, public, private, 1, 1, 1)))
741 reset_drbg_hook_ctx();
745 * Test whether none of the DRBGs is reseed if the master fails to reseed
748 master->reseed_counter++;
749 RAND_add(rand_add_buf, sizeof(rand_add_buf), sizeof(rand_add_buf));
750 if (!TEST_true(test_drbg_reseed(0, master, public, private, 0, 0, 0)))
752 reset_drbg_hook_ctx();
760 unhook_drbg(private);
766 int setup_tests(void)
768 app_data_index = RAND_DRBG_get_ex_new_index(0L, NULL, NULL, NULL, NULL);
770 ADD_ALL_TESTS(test_kats, OSSL_NELEM(drbg_test));
771 ADD_ALL_TESTS(test_error_checks, OSSL_NELEM(drbg_test));
772 ADD_TEST(test_rand_reseed);