drbgtest.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  1. /*
  2. * Copyright 2011-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  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
  8. */
  9. /* We need to use some deprecated APIs */
  10. #define OPENSSL_SUPPRESS_DEPRECATED
  11. #include <string.h>
  12. #include "internal/nelem.h"
  13. #include <openssl/crypto.h>
  14. #include <openssl/err.h>
  15. #include <openssl/rand.h>
  16. #include <openssl/obj_mac.h>
  17. #include <openssl/evp.h>
  18. #include <openssl/aes.h>
  19. #include "../crypto/rand/rand_local.h"
  20. #include "../include/crypto/rand.h"
  21. #include "../include/crypto/evp.h"
  22. #include "../providers/implementations/rands/drbg_local.h"
  23. #include "../crypto/evp/evp_local.h"
  24. #if defined(_WIN32)
  25. # include <windows.h>
  26. #endif
  27. #if defined(__TANDEM)
  28. # if defined(OPENSSL_TANDEM_FLOSS)
  29. # include <floss.h(floss_fork)>
  30. # endif
  31. #endif
  32. #if defined(OPENSSL_SYS_UNIX)
  33. # include <sys/types.h>
  34. # include <sys/wait.h>
  35. # include <unistd.h>
  36. #endif
  37. #include "testutil.h"
  38. /*
  39. * DRBG generate wrappers
  40. */
  41. static int gen_bytes(EVP_RAND_CTX *drbg, unsigned char *buf, int num)
  42. {
  43. #ifndef OPENSSL_NO_DEPRECATED_3_0
  44. const RAND_METHOD *meth = RAND_get_rand_method();
  45. if (meth != NULL && meth != RAND_OpenSSL()) {
  46. if (meth->bytes != NULL)
  47. return meth->bytes(buf, num);
  48. return -1;
  49. }
  50. #endif
  51. if (drbg != NULL)
  52. return EVP_RAND_generate(drbg, buf, num, 0, 0, NULL, 0);
  53. return 0;
  54. }
  55. static int rand_bytes(unsigned char *buf, int num)
  56. {
  57. return gen_bytes(RAND_get0_public(NULL), buf, num);
  58. }
  59. static int rand_priv_bytes(unsigned char *buf, int num)
  60. {
  61. return gen_bytes(RAND_get0_private(NULL), buf, num);
  62. }
  63. /* size of random output generated in test_drbg_reseed() */
  64. #define RANDOM_SIZE 16
  65. /*
  66. * DRBG query functions
  67. */
  68. static int state(EVP_RAND_CTX *drbg)
  69. {
  70. return EVP_RAND_get_state(drbg);
  71. }
  72. static unsigned int query_rand_uint(EVP_RAND_CTX *drbg, const char *name)
  73. {
  74. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  75. unsigned int n;
  76. *params = OSSL_PARAM_construct_uint(name, &n);
  77. if (EVP_RAND_CTX_get_params(drbg, params))
  78. return n;
  79. return 0;
  80. }
  81. #define DRBG_UINT(name) \
  82. static unsigned int name(EVP_RAND_CTX *drbg) \
  83. { \
  84. return query_rand_uint(drbg, #name); \
  85. }
  86. DRBG_UINT(reseed_counter)
  87. static PROV_DRBG *prov_rand(EVP_RAND_CTX *drbg)
  88. {
  89. return (PROV_DRBG *)drbg->algctx;
  90. }
  91. static void set_reseed_counter(EVP_RAND_CTX *drbg, unsigned int n)
  92. {
  93. PROV_DRBG *p = prov_rand(drbg);
  94. p->reseed_counter = n;
  95. }
  96. static void inc_reseed_counter(EVP_RAND_CTX *drbg)
  97. {
  98. set_reseed_counter(drbg, reseed_counter(drbg) + 1);
  99. }
  100. static time_t reseed_time(EVP_RAND_CTX *drbg)
  101. {
  102. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  103. time_t t;
  104. *params = OSSL_PARAM_construct_time_t(OSSL_DRBG_PARAM_RESEED_TIME, &t);
  105. if (EVP_RAND_CTX_get_params(drbg, params))
  106. return t;
  107. return 0;
  108. }
  109. /*
  110. * When building the FIPS module, it isn't possible to disable the continuous
  111. * RNG tests. Tests that require this are skipped.
  112. */
  113. static int crngt_skip(void)
  114. {
  115. #ifdef FIPS_MODULE
  116. return 1;
  117. #else
  118. return 0;
  119. #endif
  120. }
  121. /*
  122. * Disable CRNG testing if it is enabled.
  123. * This stub remains to indicate the calling locations where it is necessary.
  124. * Once the RNG infrastructure is able to disable these tests, it should be
  125. * reconstituted.
  126. */
  127. static int disable_crngt(EVP_RAND_CTX *drbg)
  128. {
  129. return 1;
  130. }
  131. /*
  132. * Generates random output using rand_bytes() and rand_priv_bytes()
  133. * and checks whether the three shared DRBGs were reseeded as
  134. * expected.
  135. *
  136. * |expect_success|: expected outcome (as reported by RAND_status())
  137. * |primary|, |public|, |private|: pointers to the three shared DRBGs
  138. * |public_random|, |private_random|: generated random output
  139. * |expect_xxx_reseed| =
  140. * 1: it is expected that the specified DRBG is reseeded
  141. * 0: it is expected that the specified DRBG is not reseeded
  142. * -1: don't check whether the specified DRBG was reseeded or not
  143. * |reseed_when|: if nonzero, used instead of time(NULL) to set the
  144. * |before_reseed| time.
  145. */
  146. static int test_drbg_reseed(int expect_success,
  147. EVP_RAND_CTX *primary,
  148. EVP_RAND_CTX *public,
  149. EVP_RAND_CTX *private,
  150. unsigned char *public_random,
  151. unsigned char *private_random,
  152. int expect_primary_reseed,
  153. int expect_public_reseed,
  154. int expect_private_reseed,
  155. time_t reseed_when
  156. )
  157. {
  158. time_t before_reseed, after_reseed;
  159. int expected_state = (expect_success ? DRBG_READY : DRBG_ERROR);
  160. unsigned int primary_reseed, public_reseed, private_reseed;
  161. unsigned char dummy[RANDOM_SIZE];
  162. if (public_random == NULL)
  163. public_random = dummy;
  164. if (private_random == NULL)
  165. private_random = dummy;
  166. /*
  167. * step 1: check preconditions
  168. */
  169. /* Test whether seed propagation is enabled */
  170. if (!TEST_int_ne(primary_reseed = reseed_counter(primary), 0)
  171. || !TEST_int_ne(public_reseed = reseed_counter(public), 0)
  172. || !TEST_int_ne(private_reseed = reseed_counter(private), 0))
  173. return 0;
  174. /*
  175. * step 2: generate random output
  176. */
  177. if (reseed_when == 0)
  178. reseed_when = time(NULL);
  179. /* Generate random output from the public and private DRBG */
  180. before_reseed = expect_primary_reseed == 1 ? reseed_when : 0;
  181. if (!TEST_int_eq(rand_bytes((unsigned char*)public_random,
  182. RANDOM_SIZE), expect_success)
  183. || !TEST_int_eq(rand_priv_bytes((unsigned char*) private_random,
  184. RANDOM_SIZE), expect_success))
  185. return 0;
  186. after_reseed = time(NULL);
  187. /*
  188. * step 3: check postconditions
  189. */
  190. /* Test whether reseeding succeeded as expected */
  191. if (!TEST_int_eq(state(primary), expected_state)
  192. || !TEST_int_eq(state(public), expected_state)
  193. || !TEST_int_eq(state(private), expected_state))
  194. return 0;
  195. if (expect_primary_reseed >= 0) {
  196. /* Test whether primary DRBG was reseeded as expected */
  197. if (!TEST_int_ge(reseed_counter(primary), primary_reseed))
  198. return 0;
  199. }
  200. if (expect_public_reseed >= 0) {
  201. /* Test whether public DRBG was reseeded as expected */
  202. if (!TEST_int_ge(reseed_counter(public), public_reseed)
  203. || !TEST_uint_ge(reseed_counter(public),
  204. reseed_counter(primary)))
  205. return 0;
  206. }
  207. if (expect_private_reseed >= 0) {
  208. /* Test whether public DRBG was reseeded as expected */
  209. if (!TEST_int_ge(reseed_counter(private), private_reseed)
  210. || !TEST_uint_ge(reseed_counter(private),
  211. reseed_counter(primary)))
  212. return 0;
  213. }
  214. if (expect_success == 1) {
  215. /* Test whether reseed time of primary DRBG is set correctly */
  216. if (!TEST_time_t_le(before_reseed, reseed_time(primary))
  217. || !TEST_time_t_le(reseed_time(primary), after_reseed))
  218. return 0;
  219. /* Test whether reseed times of child DRBGs are synchronized with primary */
  220. if (!TEST_time_t_ge(reseed_time(public), reseed_time(primary))
  221. || !TEST_time_t_ge(reseed_time(private), reseed_time(primary)))
  222. return 0;
  223. } else {
  224. ERR_clear_error();
  225. }
  226. return 1;
  227. }
  228. #if defined(OPENSSL_SYS_UNIX)
  229. /* number of children to fork */
  230. #define DRBG_FORK_COUNT 9
  231. /* two results per child, two for the parent */
  232. #define DRBG_FORK_RESULT_COUNT (2 * (DRBG_FORK_COUNT + 1))
  233. typedef struct drbg_fork_result_st {
  234. unsigned char random[RANDOM_SIZE]; /* random output */
  235. int pindex; /* process index (0: parent, 1,2,3...: children)*/
  236. pid_t pid; /* process id */
  237. int private; /* true if the private drbg was used */
  238. char name[10]; /* 'parent' resp. 'child 1', 'child 2', ... */
  239. } drbg_fork_result;
  240. /*
  241. * Sort the drbg_fork_result entries in lexicographical order
  242. *
  243. * This simplifies finding duplicate random output and makes
  244. * the printout in case of an error more readable.
  245. */
  246. static int compare_drbg_fork_result(const void * left, const void * right)
  247. {
  248. int result;
  249. const drbg_fork_result *l = left;
  250. const drbg_fork_result *r = right;
  251. /* separate public and private results */
  252. result = l->private - r->private;
  253. if (result == 0)
  254. result = memcmp(l->random, r->random, RANDOM_SIZE);
  255. if (result == 0)
  256. result = l->pindex - r->pindex;
  257. return result;
  258. }
  259. /*
  260. * Sort two-byte chunks of random data
  261. *
  262. * Used for finding collisions in two-byte chunks
  263. */
  264. static int compare_rand_chunk(const void * left, const void * right)
  265. {
  266. return memcmp(left, right, 2);
  267. }
  268. /*
  269. * Test whether primary, public and private DRBG are reseeded
  270. * in the child after forking the process. Collect the random
  271. * output of the public and private DRBG and send it back to
  272. * the parent process.
  273. */
  274. static int test_drbg_reseed_in_child(EVP_RAND_CTX *primary,
  275. EVP_RAND_CTX *public,
  276. EVP_RAND_CTX *private,
  277. drbg_fork_result result[2])
  278. {
  279. int rv = 0, status;
  280. int fd[2];
  281. pid_t pid;
  282. unsigned char random[2 * RANDOM_SIZE];
  283. if (!TEST_int_ge(pipe(fd), 0))
  284. return 0;
  285. if (!TEST_int_ge(pid = fork(), 0)) {
  286. close(fd[0]);
  287. close(fd[1]);
  288. return 0;
  289. } else if (pid > 0) {
  290. /* I'm the parent; close the write end */
  291. close(fd[1]);
  292. /* wait for children to terminate and collect their random output */
  293. if (TEST_int_eq(waitpid(pid, &status, 0), pid)
  294. && TEST_int_eq(status, 0)
  295. && TEST_true(read(fd[0], &random[0], sizeof(random))
  296. == sizeof(random))) {
  297. /* random output of public drbg */
  298. result[0].pid = pid;
  299. result[0].private = 0;
  300. memcpy(result[0].random, &random[0], RANDOM_SIZE);
  301. /* random output of private drbg */
  302. result[1].pid = pid;
  303. result[1].private = 1;
  304. memcpy(result[1].random, &random[RANDOM_SIZE], RANDOM_SIZE);
  305. rv = 1;
  306. }
  307. /* close the read end */
  308. close(fd[0]);
  309. return rv;
  310. } else {
  311. /* I'm the child; close the read end */
  312. close(fd[0]);
  313. /* check whether all three DRBGs reseed and send output to parent */
  314. if (TEST_true(test_drbg_reseed(1, primary, public, private,
  315. &random[0], &random[RANDOM_SIZE],
  316. 1, 1, 1, 0))
  317. && TEST_true(write(fd[1], random, sizeof(random))
  318. == sizeof(random))) {
  319. rv = 1;
  320. }
  321. /* close the write end */
  322. close(fd[1]);
  323. /* convert boolean to exit code */
  324. exit(rv == 0);
  325. }
  326. }
  327. static int test_rand_reseed_on_fork(EVP_RAND_CTX *primary,
  328. EVP_RAND_CTX *public,
  329. EVP_RAND_CTX *private)
  330. {
  331. unsigned int i;
  332. pid_t pid = getpid();
  333. int verbose = (getenv("V") != NULL);
  334. int success = 1;
  335. int duplicate[2] = {0, 0};
  336. unsigned char random[2 * RANDOM_SIZE];
  337. unsigned char sample[DRBG_FORK_RESULT_COUNT * RANDOM_SIZE];
  338. unsigned char *psample = &sample[0];
  339. drbg_fork_result result[DRBG_FORK_RESULT_COUNT];
  340. drbg_fork_result *presult = &result[2];
  341. memset(&result, 0, sizeof(result));
  342. for (i = 1 ; i <= DRBG_FORK_COUNT ; ++i) {
  343. presult[0].pindex = presult[1].pindex = i;
  344. sprintf(presult[0].name, "child %d", i);
  345. strcpy(presult[1].name, presult[0].name);
  346. /* collect the random output of the children */
  347. if (!TEST_true(test_drbg_reseed_in_child(primary,
  348. public,
  349. private,
  350. presult)))
  351. return 0;
  352. presult += 2;
  353. }
  354. /* collect the random output of the parent */
  355. if (!TEST_true(test_drbg_reseed(1,
  356. primary, public, private,
  357. &random[0], &random[RANDOM_SIZE],
  358. 0, 0, 0, 0)))
  359. return 0;
  360. strcpy(result[0].name, "parent");
  361. strcpy(result[1].name, "parent");
  362. /* output of public drbg */
  363. result[0].pid = pid;
  364. result[0].private = 0;
  365. memcpy(result[0].random, &random[0], RANDOM_SIZE);
  366. /* output of private drbg */
  367. result[1].pid = pid;
  368. result[1].private = 1;
  369. memcpy(result[1].random, &random[RANDOM_SIZE], RANDOM_SIZE);
  370. /* collect all sampled random data in a single buffer */
  371. for (i = 0 ; i < DRBG_FORK_RESULT_COUNT ; ++i) {
  372. memcpy(psample, &result[i].random[0], RANDOM_SIZE);
  373. psample += RANDOM_SIZE;
  374. }
  375. /* sort the results... */
  376. qsort(result, DRBG_FORK_RESULT_COUNT, sizeof(drbg_fork_result),
  377. compare_drbg_fork_result);
  378. /* ...and count duplicate prefixes by looking at the first byte only */
  379. for (i = 1 ; i < DRBG_FORK_RESULT_COUNT ; ++i) {
  380. if (result[i].random[0] == result[i-1].random[0]) {
  381. /* count public and private duplicates separately */
  382. ++duplicate[result[i].private];
  383. }
  384. }
  385. if (duplicate[0] >= DRBG_FORK_COUNT - 1) {
  386. /* just too many duplicates to be a coincidence */
  387. TEST_note("ERROR: %d duplicate prefixes in public random output", duplicate[0]);
  388. success = 0;
  389. }
  390. if (duplicate[1] >= DRBG_FORK_COUNT - 1) {
  391. /* just too many duplicates to be a coincidence */
  392. TEST_note("ERROR: %d duplicate prefixes in private random output", duplicate[1]);
  393. success = 0;
  394. }
  395. duplicate[0] = 0;
  396. /* sort the two-byte chunks... */
  397. qsort(sample, sizeof(sample)/2, 2, compare_rand_chunk);
  398. /* ...and count duplicate chunks */
  399. for (i = 2, psample = sample + 2 ; i < sizeof(sample) ; i += 2, psample += 2) {
  400. if (compare_rand_chunk(psample - 2, psample) == 0)
  401. ++duplicate[0];
  402. }
  403. if (duplicate[0] >= DRBG_FORK_COUNT - 1) {
  404. /* just too many duplicates to be a coincidence */
  405. TEST_note("ERROR: %d duplicate chunks in random output", duplicate[0]);
  406. success = 0;
  407. }
  408. if (verbose || !success) {
  409. for (i = 0 ; i < DRBG_FORK_RESULT_COUNT ; ++i) {
  410. char *rand_hex = OPENSSL_buf2hexstr(result[i].random, RANDOM_SIZE);
  411. TEST_note(" random: %s, pid: %d (%s, %s)",
  412. rand_hex,
  413. result[i].pid,
  414. result[i].name,
  415. result[i].private ? "private" : "public"
  416. );
  417. OPENSSL_free(rand_hex);
  418. }
  419. }
  420. return success;
  421. }
  422. static int test_rand_fork_safety(int i)
  423. {
  424. int success = 1;
  425. unsigned char random[1];
  426. EVP_RAND_CTX *primary, *public, *private;
  427. /* All three DRBGs should be non-null */
  428. if (!TEST_ptr(primary = RAND_get0_primary(NULL))
  429. || !TEST_ptr(public = RAND_get0_public(NULL))
  430. || !TEST_ptr(private = RAND_get0_private(NULL)))
  431. return 0;
  432. /* run the actual test */
  433. if (!TEST_true(test_rand_reseed_on_fork(primary, public, private)))
  434. success = 0;
  435. /* request a single byte from each of the DRBGs before the next run */
  436. if (!TEST_true(RAND_bytes(random, 1) && RAND_priv_bytes(random, 1)))
  437. success = 0;
  438. return success;
  439. }
  440. #endif
  441. /*
  442. * Test whether the default rand_method (RAND_OpenSSL()) is
  443. * setup correctly, in particular whether reseeding works
  444. * as designed.
  445. */
  446. static int test_rand_reseed(void)
  447. {
  448. EVP_RAND_CTX *primary, *public, *private;
  449. unsigned char rand_add_buf[256];
  450. int rv = 0;
  451. time_t before_reseed;
  452. if (crngt_skip())
  453. return TEST_skip("CRNGT cannot be disabled");
  454. #ifndef OPENSSL_NO_DEPRECATED_3_0
  455. /* Check whether RAND_OpenSSL() is the default method */
  456. if (!TEST_ptr_eq(RAND_get_rand_method(), RAND_OpenSSL()))
  457. return 0;
  458. #endif
  459. /* All three DRBGs should be non-null */
  460. if (!TEST_ptr(primary = RAND_get0_primary(NULL))
  461. || !TEST_ptr(public = RAND_get0_public(NULL))
  462. || !TEST_ptr(private = RAND_get0_private(NULL)))
  463. return 0;
  464. /* There should be three distinct DRBGs, two of them chained to primary */
  465. if (!TEST_ptr_ne(public, private)
  466. || !TEST_ptr_ne(public, primary)
  467. || !TEST_ptr_ne(private, primary)
  468. || !TEST_ptr_eq(prov_rand(public)->parent, prov_rand(primary))
  469. || !TEST_ptr_eq(prov_rand(private)->parent, prov_rand(primary)))
  470. return 0;
  471. /* Disable CRNG testing for the primary DRBG */
  472. if (!TEST_true(disable_crngt(primary)))
  473. return 0;
  474. /* uninstantiate the three global DRBGs */
  475. EVP_RAND_uninstantiate(primary);
  476. EVP_RAND_uninstantiate(private);
  477. EVP_RAND_uninstantiate(public);
  478. /*
  479. * Test initial seeding of shared DRBGs
  480. */
  481. if (!TEST_true(test_drbg_reseed(1,
  482. primary, public, private,
  483. NULL, NULL,
  484. 1, 1, 1, 0)))
  485. goto error;
  486. /*
  487. * Test initial state of shared DRBGs
  488. */
  489. if (!TEST_true(test_drbg_reseed(1,
  490. primary, public, private,
  491. NULL, NULL,
  492. 0, 0, 0, 0)))
  493. goto error;
  494. /*
  495. * Test whether the public and private DRBG are both reseeded when their
  496. * reseed counters differ from the primary's reseed counter.
  497. */
  498. inc_reseed_counter(primary);
  499. if (!TEST_true(test_drbg_reseed(1,
  500. primary, public, private,
  501. NULL, NULL,
  502. 0, 1, 1, 0)))
  503. goto error;
  504. /*
  505. * Test whether the public DRBG is reseeded when its reseed counter differs
  506. * from the primary's reseed counter.
  507. */
  508. inc_reseed_counter(primary);
  509. inc_reseed_counter(private);
  510. if (!TEST_true(test_drbg_reseed(1,
  511. primary, public, private,
  512. NULL, NULL,
  513. 0, 1, 0, 0)))
  514. goto error;
  515. /*
  516. * Test whether the private DRBG is reseeded when its reseed counter differs
  517. * from the primary's reseed counter.
  518. */
  519. inc_reseed_counter(primary);
  520. inc_reseed_counter(public);
  521. if (!TEST_true(test_drbg_reseed(1,
  522. primary, public, private,
  523. NULL, NULL,
  524. 0, 0, 1, 0)))
  525. goto error;
  526. /* fill 'randomness' buffer with some arbitrary data */
  527. memset(rand_add_buf, 'r', sizeof(rand_add_buf));
  528. #ifndef FIPS_MODULE
  529. /*
  530. * Test whether all three DRBGs are reseeded by RAND_add().
  531. * The before_reseed time has to be measured here and passed into the
  532. * test_drbg_reseed() test, because the primary DRBG gets already reseeded
  533. * in RAND_add(), whence the check for the condition
  534. * before_reseed <= reseed_time(primary) will fail if the time value happens
  535. * to increase between the RAND_add() and the test_drbg_reseed() call.
  536. */
  537. before_reseed = time(NULL);
  538. RAND_add(rand_add_buf, sizeof(rand_add_buf), sizeof(rand_add_buf));
  539. if (!TEST_true(test_drbg_reseed(1,
  540. primary, public, private,
  541. NULL, NULL,
  542. 1, 1, 1,
  543. before_reseed)))
  544. goto error;
  545. #else /* FIPS_MODULE */
  546. /*
  547. * In FIPS mode, random data provided by the application via RAND_add()
  548. * is not considered a trusted entropy source. It is only treated as
  549. * additional_data and no reseeding is forced. This test assures that
  550. * no reseeding occurs.
  551. */
  552. before_reseed = time(NULL);
  553. RAND_add(rand_add_buf, sizeof(rand_add_buf), sizeof(rand_add_buf));
  554. if (!TEST_true(test_drbg_reseed(1,
  555. primary, public, private,
  556. NULL, NULL,
  557. 0, 0, 0,
  558. before_reseed)))
  559. goto error;
  560. #endif
  561. rv = 1;
  562. error:
  563. return rv;
  564. }
  565. #if defined(OPENSSL_THREADS)
  566. static int multi_thread_rand_bytes_succeeded = 1;
  567. static int multi_thread_rand_priv_bytes_succeeded = 1;
  568. static int set_reseed_time_interval(EVP_RAND_CTX *drbg, int t)
  569. {
  570. OSSL_PARAM params[2];
  571. params[0] = OSSL_PARAM_construct_int(OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL,
  572. &t);
  573. params[1] = OSSL_PARAM_construct_end();
  574. return EVP_RAND_CTX_set_params(drbg, params);
  575. }
  576. static void run_multi_thread_test(void)
  577. {
  578. unsigned char buf[256];
  579. time_t start = time(NULL);
  580. EVP_RAND_CTX *public = NULL, *private = NULL;
  581. if (!TEST_ptr(public = RAND_get0_public(NULL))
  582. || !TEST_ptr(private = RAND_get0_private(NULL))
  583. || !TEST_true(set_reseed_time_interval(private, 1))
  584. || !TEST_true(set_reseed_time_interval(public, 1))) {
  585. multi_thread_rand_bytes_succeeded = 0;
  586. return;
  587. }
  588. do {
  589. if (rand_bytes(buf, sizeof(buf)) <= 0)
  590. multi_thread_rand_bytes_succeeded = 0;
  591. if (rand_priv_bytes(buf, sizeof(buf)) <= 0)
  592. multi_thread_rand_priv_bytes_succeeded = 0;
  593. }
  594. while (time(NULL) - start < 5);
  595. }
  596. # if defined(OPENSSL_SYS_WINDOWS)
  597. typedef HANDLE thread_t;
  598. static DWORD WINAPI thread_run(LPVOID arg)
  599. {
  600. run_multi_thread_test();
  601. /*
  602. * Because we're linking with a static library, we must stop each
  603. * thread explicitly, or so says OPENSSL_thread_stop(3)
  604. */
  605. OPENSSL_thread_stop();
  606. return 0;
  607. }
  608. static int run_thread(thread_t *t)
  609. {
  610. *t = CreateThread(NULL, 0, thread_run, NULL, 0, NULL);
  611. return *t != NULL;
  612. }
  613. static int wait_for_thread(thread_t thread)
  614. {
  615. return WaitForSingleObject(thread, INFINITE) == 0;
  616. }
  617. # else
  618. typedef pthread_t thread_t;
  619. static void *thread_run(void *arg)
  620. {
  621. run_multi_thread_test();
  622. /*
  623. * Because we're linking with a static library, we must stop each
  624. * thread explicitly, or so says OPENSSL_thread_stop(3)
  625. */
  626. OPENSSL_thread_stop();
  627. return NULL;
  628. }
  629. static int run_thread(thread_t *t)
  630. {
  631. return pthread_create(t, NULL, thread_run, NULL) == 0;
  632. }
  633. static int wait_for_thread(thread_t thread)
  634. {
  635. return pthread_join(thread, NULL) == 0;
  636. }
  637. # endif
  638. /*
  639. * The main thread will also run the test, so we'll have THREADS+1 parallel
  640. * tests running
  641. */
  642. # define THREADS 3
  643. static int test_multi_thread(void)
  644. {
  645. thread_t t[THREADS];
  646. int i;
  647. for (i = 0; i < THREADS; i++)
  648. run_thread(&t[i]);
  649. run_multi_thread_test();
  650. for (i = 0; i < THREADS; i++)
  651. wait_for_thread(t[i]);
  652. if (!TEST_true(multi_thread_rand_bytes_succeeded))
  653. return 0;
  654. if (!TEST_true(multi_thread_rand_priv_bytes_succeeded))
  655. return 0;
  656. return 1;
  657. }
  658. #endif
  659. static EVP_RAND_CTX *new_drbg(EVP_RAND_CTX *parent)
  660. {
  661. OSSL_PARAM params[2];
  662. EVP_RAND *rand = NULL;
  663. EVP_RAND_CTX *drbg = NULL;
  664. params[0] = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_CIPHER,
  665. "AES-256-CTR", 0);
  666. params[1] = OSSL_PARAM_construct_end();
  667. if (!TEST_ptr(rand = EVP_RAND_fetch(NULL, "CTR-DRBG", NULL))
  668. || !TEST_ptr(drbg = EVP_RAND_CTX_new(rand, parent))
  669. || !TEST_true(EVP_RAND_CTX_set_params(drbg, params))) {
  670. EVP_RAND_CTX_free(drbg);
  671. drbg = NULL;
  672. }
  673. EVP_RAND_free(rand);
  674. return drbg;
  675. }
  676. static int test_rand_prediction_resistance(void)
  677. {
  678. EVP_RAND_CTX *x = NULL, *y = NULL, *z = NULL;
  679. unsigned char buf1[51], buf2[sizeof(buf1)];
  680. int ret = 0, xreseed, yreseed, zreseed;
  681. if (crngt_skip())
  682. return TEST_skip("CRNGT cannot be disabled");
  683. /* Initialise a three long DRBG chain */
  684. if (!TEST_ptr(x = new_drbg(NULL))
  685. || !TEST_true(disable_crngt(x))
  686. || !TEST_true(EVP_RAND_instantiate(x, 0, 0, NULL, 0, NULL))
  687. || !TEST_ptr(y = new_drbg(x))
  688. || !TEST_true(EVP_RAND_instantiate(y, 0, 0, NULL, 0, NULL))
  689. || !TEST_ptr(z = new_drbg(y))
  690. || !TEST_true(EVP_RAND_instantiate(z, 0, 0, NULL, 0, NULL)))
  691. goto err;
  692. /*
  693. * During a normal reseed, only the last DRBG in the chain should
  694. * be reseeded.
  695. */
  696. inc_reseed_counter(y);
  697. xreseed = reseed_counter(x);
  698. yreseed = reseed_counter(y);
  699. zreseed = reseed_counter(z);
  700. if (!TEST_true(EVP_RAND_reseed(z, 0, NULL, 0, NULL, 0))
  701. || !TEST_int_eq(reseed_counter(x), xreseed)
  702. || !TEST_int_eq(reseed_counter(y), yreseed)
  703. || !TEST_int_gt(reseed_counter(z), zreseed))
  704. goto err;
  705. /*
  706. * When prediction resistance is requested, the request should be
  707. * propagated to the primary, so that the entire DRBG chain reseeds.
  708. */
  709. zreseed = reseed_counter(z);
  710. if (!TEST_true(EVP_RAND_reseed(z, 1, NULL, 0, NULL, 0))
  711. || !TEST_int_gt(reseed_counter(x), xreseed)
  712. || !TEST_int_gt(reseed_counter(y), yreseed)
  713. || !TEST_int_gt(reseed_counter(z), zreseed))
  714. goto err;
  715. /*
  716. * During a normal generate, only the last DRBG should be reseed */
  717. inc_reseed_counter(y);
  718. xreseed = reseed_counter(x);
  719. yreseed = reseed_counter(y);
  720. zreseed = reseed_counter(z);
  721. if (!TEST_true(EVP_RAND_generate(z, buf1, sizeof(buf1), 0, 0, NULL, 0))
  722. || !TEST_int_eq(reseed_counter(x), xreseed)
  723. || !TEST_int_eq(reseed_counter(y), yreseed)
  724. || !TEST_int_gt(reseed_counter(z), zreseed))
  725. goto err;
  726. /*
  727. * When a prediction resistant generate is requested, the request
  728. * should be propagated to the primary, reseeding the entire DRBG chain.
  729. */
  730. zreseed = reseed_counter(z);
  731. if (!TEST_true(EVP_RAND_generate(z, buf2, sizeof(buf2), 0, 1, NULL, 0))
  732. || !TEST_int_gt(reseed_counter(x), xreseed)
  733. || !TEST_int_gt(reseed_counter(y), yreseed)
  734. || !TEST_int_gt(reseed_counter(z), zreseed)
  735. || !TEST_mem_ne(buf1, sizeof(buf1), buf2, sizeof(buf2)))
  736. goto err;
  737. /* Verify that a normal reseed still only reseeds the last DRBG */
  738. inc_reseed_counter(y);
  739. xreseed = reseed_counter(x);
  740. yreseed = reseed_counter(y);
  741. zreseed = reseed_counter(z);
  742. if (!TEST_true(EVP_RAND_reseed(z, 0, NULL, 0, NULL, 0))
  743. || !TEST_int_eq(reseed_counter(x), xreseed)
  744. || !TEST_int_eq(reseed_counter(y), yreseed)
  745. || !TEST_int_gt(reseed_counter(z), zreseed))
  746. goto err;
  747. ret = 1;
  748. err:
  749. EVP_RAND_CTX_free(z);
  750. EVP_RAND_CTX_free(y);
  751. EVP_RAND_CTX_free(x);
  752. return ret;
  753. }
  754. int setup_tests(void)
  755. {
  756. ADD_TEST(test_rand_reseed);
  757. #if defined(OPENSSL_SYS_UNIX)
  758. ADD_ALL_TESTS(test_rand_fork_safety, RANDOM_SIZE);
  759. #endif
  760. ADD_TEST(test_rand_prediction_resistance);
  761. #if defined(OPENSSL_THREADS)
  762. ADD_TEST(test_multi_thread);
  763. #endif
  764. return 1;
  765. }