2
0

drbgtest.c 27 KB

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