drbgtest.c 27 KB

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