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_df(nid, pr, p) \
92 make_drbg_test_data(nid, RAND_DRBG_FLAG_CTR_USE_DF, pr, p)
94 static DRBG_SELFTEST_DATA drbg_test[] = {
95 make_drbg_test_data (NID_aes_128_ctr, 0, aes_128_no_df, 0),
96 make_drbg_test_data (NID_aes_192_ctr, 0, aes_192_no_df, 0),
97 make_drbg_test_data (NID_aes_256_ctr, 0, aes_256_no_df, 1),
98 make_drbg_test_data_df(NID_aes_128_ctr, aes_128_use_df, 0),
99 make_drbg_test_data_df(NID_aes_192_ctr, aes_192_use_df, 0),
100 make_drbg_test_data_df(NID_aes_256_ctr, aes_256_use_df, 1),
103 static int app_data_index;
106 * Test context data, attached as EXDATA to the RAND_DRBG
108 typedef struct test_ctx_st {
109 const unsigned char *entropy;
112 const unsigned char *nonce;
117 static size_t kat_entropy(RAND_DRBG *drbg, unsigned char **pout,
118 int entropy, size_t min_len, size_t max_len)
120 TEST_CTX *t = (TEST_CTX *)RAND_DRBG_get_ex_data(drbg, app_data_index);
123 *pout = (unsigned char *)t->entropy;
124 return t->entropylen;
127 static size_t kat_nonce(RAND_DRBG *drbg, unsigned char **pout,
128 int entropy, size_t min_len, size_t max_len)
130 TEST_CTX *t = (TEST_CTX *)RAND_DRBG_get_ex_data(drbg, app_data_index);
133 *pout = (unsigned char *)t->nonce;
137 static int uninstantiate(RAND_DRBG *drbg)
139 int ret = drbg == NULL ? 1 : RAND_DRBG_uninstantiate(drbg);
146 * Do a single KAT test. Return 0 on failure.
148 static int single_kat(DRBG_SELFTEST_DATA *td)
150 RAND_DRBG *drbg = NULL;
153 unsigned char buff[1024];
156 * Test without PR: Instantiate DRBG with test entropy, nonce and
157 * personalisation string.
159 if (!TEST_ptr(drbg = RAND_DRBG_new(td->nid, td->flags, NULL)))
161 if (!TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
166 memset(&t, 0, sizeof(t));
167 t.entropy = td->entropy;
168 t.entropylen = td->entropylen;
170 t.noncelen = td->noncelen;
171 RAND_DRBG_set_ex_data(drbg, app_data_index, &t);
173 if (!TEST_true(RAND_DRBG_instantiate(drbg, td->pers, td->perslen))
174 || !TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
175 td->adin, td->adinlen))
176 || !TEST_mem_eq(td->expected, td->exlen, buff, td->exlen))
179 /* Reseed DRBG with test entropy and additional input */
180 t.entropy = td->entropyreseed;
181 t.entropylen = td->entropyreseedlen;
182 if (!TEST_true(RAND_DRBG_reseed(drbg, td->adinreseed, td->adinreseedlen)
183 || !TEST_true(RAND_DRBG_generate(drbg, buff, td->kat2len, 0,
184 td->adin2, td->adin2len))
185 || !TEST_mem_eq(td->kat2, td->kat2len, buff, td->kat2len)))
190 * Now test with PR: Instantiate DRBG with test entropy, nonce and
191 * personalisation string.
193 if (!TEST_true(RAND_DRBG_set(drbg, td->nid, td->flags))
194 || !TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
197 RAND_DRBG_set_ex_data(drbg, app_data_index, &t);
198 t.entropy = td->entropy_pr;
199 t.entropylen = td->entropylen_pr;
200 t.nonce = td->nonce_pr;
201 t.noncelen = td->noncelen_pr;
204 if (!TEST_true(RAND_DRBG_instantiate(drbg, td->pers_pr, td->perslen_pr)))
208 * Now generate with PR: we need to supply entropy as this will
209 * perform a reseed operation.
211 t.entropy = td->entropypr_pr;
212 t.entropylen = td->entropyprlen_pr;
213 if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->katlen_pr, 1,
214 td->adin_pr, td->adinlen_pr))
215 || !TEST_mem_eq(td->kat_pr, td->katlen_pr, buff, td->katlen_pr))
219 * Now generate again with PR: supply new entropy again.
221 t.entropy = td->entropyg_pr;
222 t.entropylen = td->entropyglen_pr;
224 if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->kat2len_pr, 1,
225 td->ading_pr, td->adinglen_pr))
226 || !TEST_mem_eq(td->kat2_pr, td->kat2len_pr,
227 buff, td->kat2len_pr))
232 RAND_DRBG_free(drbg);
233 return failures == 0;
237 * Initialise a DRBG based on selftest data
239 static int init(RAND_DRBG *drbg, DRBG_SELFTEST_DATA *td, TEST_CTX *t)
241 if (!TEST_true(RAND_DRBG_set(drbg, td->nid, td->flags))
242 || !TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
245 RAND_DRBG_set_ex_data(drbg, app_data_index, t);
246 t->entropy = td->entropy;
247 t->entropylen = td->entropylen;
248 t->nonce = td->nonce;
249 t->noncelen = td->noncelen;
256 * Initialise and instantiate DRBG based on selftest data
258 static int instantiate(RAND_DRBG *drbg, DRBG_SELFTEST_DATA *td,
261 if (!TEST_true(init(drbg, td, t))
262 || !TEST_true(RAND_DRBG_instantiate(drbg, td->pers, td->perslen)))
268 * Perform extensive error checking as required by SP800-90.
269 * Induce several failure modes and check an error condition is set.
271 static int error_check(DRBG_SELFTEST_DATA *td)
273 static char zero[sizeof(RAND_DRBG)];
274 RAND_DRBG *drbg = NULL;
276 unsigned char buff[1024];
277 unsigned int generate_counter_tmp;
280 if (!TEST_ptr(drbg = RAND_DRBG_new(0, 0, NULL)))
284 * Personalisation string tests
287 /* Test detection of too large personlisation string */
288 if (!init(drbg, td, &t)
289 || RAND_DRBG_instantiate(drbg, td->pers, drbg->max_perslen + 1) > 0)
293 * Entropy source tests
296 /* Test entropy source failure detecion: i.e. returns no data */
298 if (TEST_int_le(RAND_DRBG_instantiate(drbg, td->pers, td->perslen), 0))
301 /* Try to generate output from uninstantiated DRBG */
302 if (!TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
303 td->adin, td->adinlen))
304 || !uninstantiate(drbg))
307 /* Test insufficient entropy */
308 t.entropylen = drbg->min_entropylen - 1;
309 if (!init(drbg, td, &t)
310 || RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0
311 || !uninstantiate(drbg))
314 /* Test too much entropy */
315 t.entropylen = drbg->max_entropylen + 1;
316 if (!init(drbg, td, &t)
317 || RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0
318 || !uninstantiate(drbg))
325 /* Test too small nonce */
326 if (drbg->min_noncelen) {
327 t.noncelen = drbg->min_noncelen - 1;
328 if (!init(drbg, td, &t)
329 || RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0
330 || !uninstantiate(drbg))
334 /* Test too large nonce */
335 if (drbg->max_noncelen) {
336 t.noncelen = drbg->max_noncelen + 1;
337 if (!init(drbg, td, &t)
338 || RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0
339 || !uninstantiate(drbg))
343 /* Instantiate with valid data, Check generation is now OK */
344 if (!instantiate(drbg, td, &t)
345 || !TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
346 td->adin, td->adinlen)))
349 /* Request too much data for one request */
350 if (!TEST_false(RAND_DRBG_generate(drbg, buff, drbg->max_request + 1, 0,
351 td->adin, td->adinlen)))
354 /* Try too large additional input */
355 if (!TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
356 td->adin, drbg->max_adinlen + 1)))
360 * Check prediction resistance request fails if entropy source
364 if (TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 1,
365 td->adin, td->adinlen))
366 || !uninstantiate(drbg))
369 /* Instantiate again with valid data */
370 if (!instantiate(drbg, td, &t))
372 generate_counter_tmp = drbg->generate_counter;
373 drbg->generate_counter = drbg->reseed_interval;
375 /* Generate output and check entropy has been requested for reseed */
377 if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
378 td->adin, td->adinlen))
379 || !TEST_int_eq(t.entropycnt, 1)
380 || !TEST_int_eq(drbg->generate_counter, generate_counter_tmp + 1)
381 || !uninstantiate(drbg))
385 * Check prediction resistance request fails if entropy source
389 if (!TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 1,
390 td->adin, td->adinlen))
391 || !uninstantiate(drbg))
394 /* Test reseed counter works */
395 if (!instantiate(drbg, td, &t))
397 generate_counter_tmp = drbg->generate_counter;
398 drbg->generate_counter = drbg->reseed_interval;
400 /* Generate output and check entropy has been requested for reseed */
402 if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
403 td->adin, td->adinlen))
404 || !TEST_int_eq(t.entropycnt, 1)
405 || !TEST_int_eq(drbg->generate_counter, generate_counter_tmp + 1)
406 || !uninstantiate(drbg))
410 * Explicit reseed tests
413 /* Test explicit reseed with too large additional input */
414 if (!init(drbg, td, &t)
415 || RAND_DRBG_reseed(drbg, td->adin, drbg->max_adinlen + 1) > 0)
418 /* Test explicit reseed with entropy source failure */
420 if (!TEST_int_le(RAND_DRBG_reseed(drbg, td->adin, td->adinlen), 0)
421 || !uninstantiate(drbg))
424 /* Test explicit reseed with too much entropy */
425 if (!init(drbg, td, &t))
427 t.entropylen = drbg->max_entropylen + 1;
428 if (!TEST_int_le(RAND_DRBG_reseed(drbg, td->adin, td->adinlen), 0)
429 || !uninstantiate(drbg))
432 /* Test explicit reseed with too little entropy */
433 if (!init(drbg, td, &t))
435 t.entropylen = drbg->min_entropylen - 1;
436 if (!TEST_int_le(RAND_DRBG_reseed(drbg, td->adin, td->adinlen), 0)
437 || !uninstantiate(drbg))
440 /* Standard says we have to check uninstantiate really zeroes */
441 if (!TEST_mem_eq(zero, sizeof(drbg->data), &drbg->data, sizeof(drbg->data)))
448 RAND_DRBG_free(drbg);
452 static int test_kats(int i)
454 DRBG_SELFTEST_DATA *td = &drbg_test[i];
465 static int test_error_checks(int i)
467 DRBG_SELFTEST_DATA *td = &drbg_test[i];
479 * Hook context data, attached as EXDATA to the RAND_DRBG
481 typedef struct hook_ctx_st {
484 * Currently, all DRBGs use the same get_entropy() callback.
485 * The tests however, don't assume this and store
486 * the original callback for every DRBG separately.
488 RAND_DRBG_get_entropy_fn get_entropy;
489 /* forces a failure of the get_entropy() call if nonzero */
491 /* counts successful reseeds */
495 static HOOK_CTX master_ctx, public_ctx, private_ctx;
497 static HOOK_CTX *get_hook_ctx(RAND_DRBG *drbg)
499 return (HOOK_CTX *)RAND_DRBG_get_ex_data(drbg, app_data_index);
502 /* Intercepts and counts calls to the get_entropy() callback */
503 static size_t get_entropy_hook(RAND_DRBG *drbg, unsigned char **pout,
504 int entropy, size_t min_len, size_t max_len)
507 HOOK_CTX *ctx = get_hook_ctx(drbg);
512 ret = ctx->get_entropy(
513 drbg, pout, entropy, min_len, max_len);
520 /* Installs a hook for the get_entropy() callback of the given drbg */
521 static void hook_drbg(RAND_DRBG *drbg, HOOK_CTX *ctx)
523 memset(ctx, 0, sizeof(*ctx));
525 ctx->get_entropy = drbg->get_entropy;
526 drbg->get_entropy = get_entropy_hook;
527 RAND_DRBG_set_ex_data(drbg, app_data_index, ctx);
530 /* Installs the hook for the get_entropy() callback of the given drbg */
531 static void unhook_drbg(RAND_DRBG *drbg)
533 HOOK_CTX *ctx = get_hook_ctx(drbg);
535 drbg->get_entropy = ctx->get_entropy;
536 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DRBG, drbg, &drbg->ex_data);
539 /* Resets the given hook context */
540 static void reset_hook_ctx(HOOK_CTX *ctx)
543 ctx->reseed_count = 0;
546 /* Resets all drbg hook contexts */
547 static void reset_drbg_hook_ctx()
549 reset_hook_ctx(&master_ctx);
550 reset_hook_ctx(&public_ctx);
551 reset_hook_ctx(&private_ctx);
555 * Generates random output using RAND_bytes() and RAND_priv_bytes()
556 * and checks whether the three shared DRBGs were reseeded as
559 * |expect_success|: expected outcome (as reported by RAND_status())
560 * |master|, |public|, |private|: pointers to the three shared DRBGs
561 * |expect_xxx_reseed| =
562 * 1: it is expected that the specified DRBG is reseeded
563 * 0: it is expected that the specified DRBG is not reseeded
564 * -1: don't check whether the specified DRBG was reseeded or not
566 static int test_drbg_reseed(int expect_success,
570 int expect_master_reseed,
571 int expect_public_reseed,
572 int expect_private_reseed
575 unsigned char buf[32];
576 time_t before_reseed, after_reseed;
577 int expected_state = (expect_success ? DRBG_READY : DRBG_ERROR);
580 * step 1: check preconditions
583 /* Test whether seed propagation is enabled */
584 if (!TEST_int_ne(master->reseed_counter, 0)
585 || !TEST_int_ne(public->reseed_counter, 0)
586 || !TEST_int_ne(private->reseed_counter, 0))
589 /* Check whether the master DRBG's reseed counter is the largest one */
590 if (!TEST_int_le(public->reseed_counter, master->reseed_counter)
591 || !TEST_int_le(private->reseed_counter, master->reseed_counter))
595 * step 2: generate random output
598 /* Generate random output from the public and private DRBG */
599 before_reseed = expect_master_reseed == 1 ? time(NULL) : 0;
600 if (!TEST_int_eq(RAND_bytes(buf, sizeof(buf)), expect_success)
601 || !TEST_int_eq(RAND_priv_bytes(buf, sizeof(buf)), expect_success))
603 after_reseed = time(NULL);
607 * step 3: check postconditions
610 /* Test whether reseeding succeeded as expected */
611 if (!TEST_int_eq(master->state, expected_state)
612 || !TEST_int_eq(public->state, expected_state)
613 || !TEST_int_eq(private->state, expected_state))
616 if (expect_master_reseed >= 0) {
617 /* Test whether master DRBG was reseeded as expected */
618 if (!TEST_int_eq(master_ctx.reseed_count, expect_master_reseed))
622 if (expect_public_reseed >= 0) {
623 /* Test whether public DRBG was reseeded as expected */
624 if (!TEST_int_eq(public_ctx.reseed_count, expect_public_reseed))
628 if (expect_private_reseed >= 0) {
629 /* Test whether public DRBG was reseeded as expected */
630 if (!TEST_int_eq(private_ctx.reseed_count, expect_private_reseed))
634 if (expect_success == 1) {
635 /* Test whether all three reseed counters are synchronized */
636 if (!TEST_int_eq(public->reseed_counter, master->reseed_counter)
637 || !TEST_int_eq(private->reseed_counter, master->reseed_counter))
640 /* Test whether reseed time of master DRBG is set correctly */
641 if (!TEST_time_t_le(before_reseed, master->reseed_time)
642 || !TEST_time_t_le(master->reseed_time, after_reseed))
645 /* Test whether reseed times of child DRBGs are synchronized with master */
646 if (!TEST_time_t_ge(public->reseed_time, master->reseed_time)
647 || !TEST_time_t_ge(private->reseed_time, master->reseed_time))
657 * Test whether the default rand_method (RAND_OpenSSL()) is
658 * setup correctly, in particular whether reseeding works
661 static int test_rand_reseed(void)
663 RAND_DRBG *master, *public, *private;
664 unsigned char rand_add_buf[256];
667 /* Check whether RAND_OpenSSL() is the default method */
668 if (!TEST_ptr_eq(RAND_get_rand_method(), RAND_OpenSSL()))
671 /* All three DRBGs should be non-null */
672 if (!TEST_ptr(master = RAND_DRBG_get0_master())
673 || !TEST_ptr(public = RAND_DRBG_get0_public())
674 || !TEST_ptr(private = RAND_DRBG_get0_private()))
677 /* There should be three distinct DRBGs, two of them chained to master */
678 if (!TEST_ptr_ne(public, private)
679 || !TEST_ptr_ne(public, master)
680 || !TEST_ptr_ne(private, master)
681 || !TEST_ptr_eq(public->parent, master)
682 || !TEST_ptr_eq(private->parent, master))
685 /* Install hooks for the following tests */
686 hook_drbg(master, &master_ctx);
687 hook_drbg(public, &public_ctx);
688 hook_drbg(private, &private_ctx);
691 * Test initial state of shared DRBs
693 if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 0, 0)))
695 reset_drbg_hook_ctx();
698 * Test whether the public and private DRBG are both reseeded when their
699 * reseed counters differ from the master's reseed counter.
701 master->reseed_counter++;
702 if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 1, 1)))
704 reset_drbg_hook_ctx();
707 * Test whether the public DRBG is reseeded when its reseed counter differs
708 * from the master's reseed counter.
710 master->reseed_counter++;
711 private->reseed_counter++;
712 if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 1, 0)))
714 reset_drbg_hook_ctx();
717 * Test whether the private DRBG is reseeded when its reseed counter differs
718 * from the master's reseed counter.
720 master->reseed_counter++;
721 public->reseed_counter++;
722 if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 0, 1)))
724 reset_drbg_hook_ctx();
727 /* fill 'randomness' buffer with some arbitrary data */
728 memset(rand_add_buf, 'r', sizeof(rand_add_buf));
731 * Test whether all three DRBGs are reseeded by RAND_add()
733 RAND_add(rand_add_buf, sizeof(rand_add_buf), sizeof(rand_add_buf));
734 if (!TEST_true(test_drbg_reseed(1, master, public, private, 1, 1, 1)))
736 reset_drbg_hook_ctx();
740 * Test whether none of the DRBGs is reseed if the master fails to reseed
743 master->reseed_counter++;
744 RAND_add(rand_add_buf, sizeof(rand_add_buf), sizeof(rand_add_buf));
745 if (!TEST_true(test_drbg_reseed(0, master, public, private, 0, 0, 0)))
747 reset_drbg_hook_ctx();
755 unhook_drbg(private);
761 int setup_tests(void)
763 app_data_index = RAND_DRBG_get_ex_new_index(0L, NULL, NULL, NULL, NULL);
765 ADD_ALL_TESTS(test_kats, OSSL_NELEM(drbg_test));
766 ADD_ALL_TESTS(test_error_checks, OSSL_NELEM(drbg_test));
767 ADD_TEST(test_rand_reseed);