drbgtest.c 27 KB

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