drbgtest.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  1. /*
  2. * Copyright 2011-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  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. #if defined(_WIN32)
  20. # include <windows.h>
  21. #endif
  22. #if defined(OPENSSL_SYS_UNIX)
  23. # include <sys/types.h>
  24. # include <sys/wait.h>
  25. # include <unistd.h>
  26. #endif
  27. #include "testutil.h"
  28. #include "drbgtest.h"
  29. typedef struct drbg_selftest_data_st {
  30. int post;
  31. int nid;
  32. unsigned int flags;
  33. /* KAT data for no PR */
  34. const unsigned char *entropy;
  35. size_t entropylen;
  36. const unsigned char *nonce;
  37. size_t noncelen;
  38. const unsigned char *pers;
  39. size_t perslen;
  40. const unsigned char *adin;
  41. size_t adinlen;
  42. const unsigned char *entropyreseed;
  43. size_t entropyreseedlen;
  44. const unsigned char *adinreseed;
  45. size_t adinreseedlen;
  46. const unsigned char *adin2;
  47. size_t adin2len;
  48. const unsigned char *expected;
  49. size_t exlen;
  50. const unsigned char *kat2;
  51. size_t kat2len;
  52. /* KAT data for PR */
  53. const unsigned char *entropy_pr;
  54. size_t entropylen_pr;
  55. const unsigned char *nonce_pr;
  56. size_t noncelen_pr;
  57. const unsigned char *pers_pr;
  58. size_t perslen_pr;
  59. const unsigned char *adin_pr;
  60. size_t adinlen_pr;
  61. const unsigned char *entropypr_pr;
  62. size_t entropyprlen_pr;
  63. const unsigned char *ading_pr;
  64. size_t adinglen_pr;
  65. const unsigned char *entropyg_pr;
  66. size_t entropyglen_pr;
  67. const unsigned char *kat_pr;
  68. size_t katlen_pr;
  69. const unsigned char *kat2_pr;
  70. size_t kat2len_pr;
  71. } DRBG_SELFTEST_DATA;
  72. #define make_drbg_test_data(nid, flag, pr, post) {\
  73. post, nid, flag, \
  74. pr##_entropyinput, sizeof(pr##_entropyinput), \
  75. pr##_nonce, sizeof(pr##_nonce), \
  76. pr##_personalizationstring, sizeof(pr##_personalizationstring), \
  77. pr##_additionalinput, sizeof(pr##_additionalinput), \
  78. pr##_entropyinputreseed, sizeof(pr##_entropyinputreseed), \
  79. pr##_additionalinputreseed, sizeof(pr##_additionalinputreseed), \
  80. pr##_additionalinput2, sizeof(pr##_additionalinput2), \
  81. pr##_int_returnedbits, sizeof(pr##_int_returnedbits), \
  82. pr##_returnedbits, sizeof(pr##_returnedbits), \
  83. pr##_pr_entropyinput, sizeof(pr##_pr_entropyinput), \
  84. pr##_pr_nonce, sizeof(pr##_pr_nonce), \
  85. pr##_pr_personalizationstring, sizeof(pr##_pr_personalizationstring), \
  86. pr##_pr_additionalinput, sizeof(pr##_pr_additionalinput), \
  87. pr##_pr_entropyinputpr, sizeof(pr##_pr_entropyinputpr), \
  88. pr##_pr_additionalinput2, sizeof(pr##_pr_additionalinput2), \
  89. pr##_pr_entropyinputpr2, sizeof(pr##_pr_entropyinputpr2), \
  90. pr##_pr_int_returnedbits, sizeof(pr##_pr_int_returnedbits), \
  91. pr##_pr_returnedbits, sizeof(pr##_pr_returnedbits) \
  92. }
  93. #define make_drbg_test_data_use_df(nid, pr, p) \
  94. make_drbg_test_data(nid, 0, pr, p)
  95. #define make_drbg_test_data_no_df(nid, pr, p) \
  96. make_drbg_test_data(nid, RAND_DRBG_FLAG_CTR_NO_DF, pr, p)
  97. static DRBG_SELFTEST_DATA drbg_test[] = {
  98. make_drbg_test_data_no_df (NID_aes_128_ctr, aes_128_no_df, 0),
  99. make_drbg_test_data_no_df (NID_aes_192_ctr, aes_192_no_df, 0),
  100. make_drbg_test_data_no_df (NID_aes_256_ctr, aes_256_no_df, 1),
  101. make_drbg_test_data_use_df(NID_aes_128_ctr, aes_128_use_df, 0),
  102. make_drbg_test_data_use_df(NID_aes_192_ctr, aes_192_use_df, 0),
  103. make_drbg_test_data_use_df(NID_aes_256_ctr, aes_256_use_df, 1),
  104. };
  105. static int app_data_index;
  106. /*
  107. * Test context data, attached as EXDATA to the RAND_DRBG
  108. */
  109. typedef struct test_ctx_st {
  110. const unsigned char *entropy;
  111. size_t entropylen;
  112. int entropycnt;
  113. const unsigned char *nonce;
  114. size_t noncelen;
  115. int noncecnt;
  116. } TEST_CTX;
  117. static size_t kat_entropy(RAND_DRBG *drbg, unsigned char **pout,
  118. int entropy, size_t min_len, size_t max_len,
  119. int prediction_resistance)
  120. {
  121. TEST_CTX *t = (TEST_CTX *)RAND_DRBG_get_ex_data(drbg, app_data_index);
  122. t->entropycnt++;
  123. *pout = (unsigned char *)t->entropy;
  124. return t->entropylen;
  125. }
  126. static size_t kat_nonce(RAND_DRBG *drbg, unsigned char **pout,
  127. int entropy, size_t min_len, size_t max_len)
  128. {
  129. TEST_CTX *t = (TEST_CTX *)RAND_DRBG_get_ex_data(drbg, app_data_index);
  130. t->noncecnt++;
  131. *pout = (unsigned char *)t->nonce;
  132. return t->noncelen;
  133. }
  134. static int uninstantiate(RAND_DRBG *drbg)
  135. {
  136. int ret = drbg == NULL ? 1 : RAND_DRBG_uninstantiate(drbg);
  137. ERR_clear_error();
  138. return ret;
  139. }
  140. /*
  141. * Do a single KAT test. Return 0 on failure.
  142. */
  143. static int single_kat(DRBG_SELFTEST_DATA *td)
  144. {
  145. RAND_DRBG *drbg = NULL;
  146. TEST_CTX t;
  147. int failures = 0;
  148. unsigned char buff[1024];
  149. /*
  150. * Test without PR: Instantiate DRBG with test entropy, nonce and
  151. * personalisation string.
  152. */
  153. if (!TEST_ptr(drbg = RAND_DRBG_new(td->nid, td->flags, NULL)))
  154. return 0;
  155. if (!TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
  156. kat_nonce, NULL))) {
  157. failures++;
  158. goto err;
  159. }
  160. memset(&t, 0, sizeof(t));
  161. t.entropy = td->entropy;
  162. t.entropylen = td->entropylen;
  163. t.nonce = td->nonce;
  164. t.noncelen = td->noncelen;
  165. RAND_DRBG_set_ex_data(drbg, app_data_index, &t);
  166. if (!TEST_true(RAND_DRBG_instantiate(drbg, td->pers, td->perslen))
  167. || !TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
  168. td->adin, td->adinlen))
  169. || !TEST_mem_eq(td->expected, td->exlen, buff, td->exlen))
  170. failures++;
  171. /* Reseed DRBG with test entropy and additional input */
  172. t.entropy = td->entropyreseed;
  173. t.entropylen = td->entropyreseedlen;
  174. if (!TEST_true(RAND_DRBG_reseed(drbg, td->adinreseed, td->adinreseedlen, 0)
  175. || !TEST_true(RAND_DRBG_generate(drbg, buff, td->kat2len, 0,
  176. td->adin2, td->adin2len))
  177. || !TEST_mem_eq(td->kat2, td->kat2len, buff, td->kat2len)))
  178. failures++;
  179. uninstantiate(drbg);
  180. /*
  181. * Now test with PR: Instantiate DRBG with test entropy, nonce and
  182. * personalisation string.
  183. */
  184. if (!TEST_true(RAND_DRBG_set(drbg, td->nid, td->flags))
  185. || !TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
  186. kat_nonce, NULL)))
  187. failures++;
  188. RAND_DRBG_set_ex_data(drbg, app_data_index, &t);
  189. t.entropy = td->entropy_pr;
  190. t.entropylen = td->entropylen_pr;
  191. t.nonce = td->nonce_pr;
  192. t.noncelen = td->noncelen_pr;
  193. t.entropycnt = 0;
  194. t.noncecnt = 0;
  195. if (!TEST_true(RAND_DRBG_instantiate(drbg, td->pers_pr, td->perslen_pr)))
  196. failures++;
  197. /*
  198. * Now generate with PR: we need to supply entropy as this will
  199. * perform a reseed operation.
  200. */
  201. t.entropy = td->entropypr_pr;
  202. t.entropylen = td->entropyprlen_pr;
  203. if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->katlen_pr, 1,
  204. td->adin_pr, td->adinlen_pr))
  205. || !TEST_mem_eq(td->kat_pr, td->katlen_pr, buff, td->katlen_pr))
  206. failures++;
  207. /*
  208. * Now generate again with PR: supply new entropy again.
  209. */
  210. t.entropy = td->entropyg_pr;
  211. t.entropylen = td->entropyglen_pr;
  212. if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->kat2len_pr, 1,
  213. td->ading_pr, td->adinglen_pr))
  214. || !TEST_mem_eq(td->kat2_pr, td->kat2len_pr,
  215. buff, td->kat2len_pr))
  216. failures++;
  217. err:
  218. uninstantiate(drbg);
  219. RAND_DRBG_free(drbg);
  220. return failures == 0;
  221. }
  222. /*
  223. * Initialise a DRBG based on selftest data
  224. */
  225. static int init(RAND_DRBG *drbg, DRBG_SELFTEST_DATA *td, TEST_CTX *t)
  226. {
  227. if (!TEST_true(RAND_DRBG_set(drbg, td->nid, td->flags))
  228. || !TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
  229. kat_nonce, NULL)))
  230. return 0;
  231. RAND_DRBG_set_ex_data(drbg, app_data_index, t);
  232. t->entropy = td->entropy;
  233. t->entropylen = td->entropylen;
  234. t->nonce = td->nonce;
  235. t->noncelen = td->noncelen;
  236. t->entropycnt = 0;
  237. t->noncecnt = 0;
  238. return 1;
  239. }
  240. /*
  241. * Initialise and instantiate DRBG based on selftest data
  242. */
  243. static int instantiate(RAND_DRBG *drbg, DRBG_SELFTEST_DATA *td,
  244. TEST_CTX *t)
  245. {
  246. if (!TEST_true(init(drbg, td, t))
  247. || !TEST_true(RAND_DRBG_instantiate(drbg, td->pers, td->perslen)))
  248. return 0;
  249. return 1;
  250. }
  251. /*
  252. * Perform extensive error checking as required by SP800-90.
  253. * Induce several failure modes and check an error condition is set.
  254. */
  255. static int error_check(DRBG_SELFTEST_DATA *td)
  256. {
  257. RAND_DRBG *drbg = NULL;
  258. TEST_CTX t;
  259. unsigned char buff[1024];
  260. unsigned int reseed_counter_tmp;
  261. int ret = 0;
  262. if (!TEST_ptr(drbg = RAND_DRBG_new(0, 0, NULL)))
  263. goto err;
  264. /*
  265. * Personalisation string tests
  266. */
  267. /* Test detection of too large personalisation string */
  268. if (!init(drbg, td, &t)
  269. || !TEST_false(RAND_DRBG_instantiate(drbg, td->pers, drbg->max_perslen + 1)))
  270. goto err;
  271. /*
  272. * Entropy source tests
  273. */
  274. /* Test entropy source failure detection: i.e. returns no data */
  275. t.entropylen = 0;
  276. if (!TEST_false(RAND_DRBG_instantiate(drbg, td->pers, td->perslen)))
  277. goto err;
  278. /* Try to generate output from uninstantiated DRBG */
  279. if (!TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
  280. td->adin, td->adinlen))
  281. || !uninstantiate(drbg))
  282. goto err;
  283. /* Test insufficient entropy */
  284. if (!init(drbg, td, &t))
  285. goto err;
  286. t.entropylen = drbg->min_entropylen - 1;
  287. if (!TEST_false(RAND_DRBG_instantiate(drbg, td->pers, td->perslen))
  288. || !uninstantiate(drbg))
  289. goto err;
  290. /* Test too much entropy */
  291. if (!init(drbg, td, &t))
  292. goto err;
  293. t.entropylen = drbg->max_entropylen + 1;
  294. if (!TEST_false(RAND_DRBG_instantiate(drbg, td->pers, td->perslen))
  295. || !uninstantiate(drbg))
  296. goto err;
  297. /*
  298. * Nonce tests
  299. */
  300. /* Test too small nonce */
  301. if (drbg->min_noncelen) {
  302. if (!init(drbg, td, &t))
  303. goto err;
  304. t.noncelen = drbg->min_noncelen - 1;
  305. if (!TEST_false(RAND_DRBG_instantiate(drbg, td->pers, td->perslen))
  306. || !uninstantiate(drbg))
  307. goto err;
  308. }
  309. /* Test too large nonce */
  310. if (drbg->max_noncelen) {
  311. if (!init(drbg, td, &t))
  312. goto err;
  313. t.noncelen = drbg->max_noncelen + 1;
  314. if (!TEST_false(RAND_DRBG_instantiate(drbg, td->pers, td->perslen))
  315. || !uninstantiate(drbg))
  316. goto err;
  317. }
  318. /* Instantiate with valid data, Check generation is now OK */
  319. if (!instantiate(drbg, td, &t)
  320. || !TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
  321. td->adin, td->adinlen)))
  322. goto err;
  323. /* Request too much data for one request */
  324. if (!TEST_false(RAND_DRBG_generate(drbg, buff, drbg->max_request + 1, 0,
  325. td->adin, td->adinlen)))
  326. goto err;
  327. /* Try too large additional input */
  328. if (!TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
  329. td->adin, drbg->max_adinlen + 1)))
  330. goto err;
  331. /*
  332. * Check prediction resistance request fails if entropy source
  333. * failure.
  334. */
  335. t.entropylen = 0;
  336. if (!TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 1,
  337. td->adin, td->adinlen))
  338. || !uninstantiate(drbg))
  339. goto err;
  340. /* Instantiate again with valid data */
  341. if (!instantiate(drbg, td, &t))
  342. goto err;
  343. reseed_counter_tmp = drbg->generate_counter;
  344. drbg->generate_counter = drbg->reseed_interval;
  345. /* Generate output and check entropy has been requested for reseed */
  346. t.entropycnt = 0;
  347. if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
  348. td->adin, td->adinlen))
  349. || !TEST_int_eq(t.entropycnt, 1)
  350. || !TEST_int_eq(drbg->generate_counter, reseed_counter_tmp + 1)
  351. || !uninstantiate(drbg))
  352. goto err;
  353. /*
  354. * Check prediction resistance request fails if entropy source
  355. * failure.
  356. */
  357. t.entropylen = 0;
  358. if (!TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 1,
  359. td->adin, td->adinlen))
  360. || !uninstantiate(drbg))
  361. goto err;
  362. /* Test reseed counter works */
  363. if (!instantiate(drbg, td, &t))
  364. goto err;
  365. reseed_counter_tmp = drbg->generate_counter;
  366. drbg->generate_counter = drbg->reseed_interval;
  367. /* Generate output and check entropy has been requested for reseed */
  368. t.entropycnt = 0;
  369. if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
  370. td->adin, td->adinlen))
  371. || !TEST_int_eq(t.entropycnt, 1)
  372. || !TEST_int_eq(drbg->generate_counter, reseed_counter_tmp + 1)
  373. || !uninstantiate(drbg))
  374. goto err;
  375. /*
  376. * Explicit reseed tests
  377. */
  378. /* Test explicit reseed with too large additional input */
  379. if (!instantiate(drbg, td, &t)
  380. || !TEST_false(RAND_DRBG_reseed(drbg, td->adin, drbg->max_adinlen + 1, 0)))
  381. goto err;
  382. /* Test explicit reseed with entropy source failure */
  383. t.entropylen = 0;
  384. if (!TEST_false(RAND_DRBG_reseed(drbg, td->adin, td->adinlen, 0))
  385. || !uninstantiate(drbg))
  386. goto err;
  387. /* Test explicit reseed with too much entropy */
  388. if (!instantiate(drbg, td, &t))
  389. goto err;
  390. t.entropylen = drbg->max_entropylen + 1;
  391. if (!TEST_false(RAND_DRBG_reseed(drbg, td->adin, td->adinlen, 0))
  392. || !uninstantiate(drbg))
  393. goto err;
  394. /* Test explicit reseed with too little entropy */
  395. if (!instantiate(drbg, td, &t))
  396. goto err;
  397. t.entropylen = drbg->min_entropylen - 1;
  398. if (!TEST_false(RAND_DRBG_reseed(drbg, td->adin, td->adinlen, 0))
  399. || !uninstantiate(drbg))
  400. goto err;
  401. ret = 1;
  402. err:
  403. uninstantiate(drbg);
  404. RAND_DRBG_free(drbg);
  405. return ret;
  406. }
  407. static int test_kats(int i)
  408. {
  409. DRBG_SELFTEST_DATA *td = &drbg_test[i];
  410. int rv = 0;
  411. if (!single_kat(td))
  412. goto err;
  413. rv = 1;
  414. err:
  415. return rv;
  416. }
  417. static int test_error_checks(int i)
  418. {
  419. DRBG_SELFTEST_DATA *td = &drbg_test[i];
  420. int rv = 0;
  421. if (!error_check(td))
  422. goto err;
  423. rv = 1;
  424. err:
  425. return rv;
  426. }
  427. /*
  428. * Hook context data, attached as EXDATA to the RAND_DRBG
  429. */
  430. typedef struct hook_ctx_st {
  431. RAND_DRBG *drbg;
  432. /*
  433. * Currently, all DRBGs use the same get_entropy() callback.
  434. * The tests however, don't assume this and store
  435. * the original callback for every DRBG separately.
  436. */
  437. RAND_DRBG_get_entropy_fn get_entropy;
  438. /* forces a failure of the get_entropy() call if nonzero */
  439. int fail;
  440. /* counts successful reseeds */
  441. int reseed_count;
  442. } HOOK_CTX;
  443. static HOOK_CTX master_ctx, public_ctx, private_ctx;
  444. static HOOK_CTX *get_hook_ctx(RAND_DRBG *drbg)
  445. {
  446. return (HOOK_CTX *)RAND_DRBG_get_ex_data(drbg, app_data_index);
  447. }
  448. /* Intercepts and counts calls to the get_entropy() callback */
  449. static size_t get_entropy_hook(RAND_DRBG *drbg, unsigned char **pout,
  450. int entropy, size_t min_len, size_t max_len,
  451. int prediction_resistance)
  452. {
  453. size_t ret;
  454. HOOK_CTX *ctx = get_hook_ctx(drbg);
  455. if (ctx->fail != 0)
  456. return 0;
  457. ret = ctx->get_entropy(drbg, pout, entropy, min_len, max_len,
  458. prediction_resistance);
  459. if (ret != 0)
  460. ctx->reseed_count++;
  461. return ret;
  462. }
  463. /* Installs a hook for the get_entropy() callback of the given drbg */
  464. static void hook_drbg(RAND_DRBG *drbg, HOOK_CTX *ctx)
  465. {
  466. memset(ctx, 0, sizeof(*ctx));
  467. ctx->drbg = drbg;
  468. ctx->get_entropy = drbg->get_entropy;
  469. drbg->get_entropy = get_entropy_hook;
  470. RAND_DRBG_set_ex_data(drbg, app_data_index, ctx);
  471. }
  472. /* Installs the hook for the get_entropy() callback of the given drbg */
  473. static void unhook_drbg(RAND_DRBG *drbg)
  474. {
  475. HOOK_CTX *ctx = get_hook_ctx(drbg);
  476. drbg->get_entropy = ctx->get_entropy;
  477. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DRBG, drbg, &drbg->ex_data);
  478. }
  479. /* Resets the given hook context */
  480. static void reset_hook_ctx(HOOK_CTX *ctx)
  481. {
  482. ctx->fail = 0;
  483. ctx->reseed_count = 0;
  484. }
  485. /* Resets all drbg hook contexts */
  486. static void reset_drbg_hook_ctx(void)
  487. {
  488. reset_hook_ctx(&master_ctx);
  489. reset_hook_ctx(&public_ctx);
  490. reset_hook_ctx(&private_ctx);
  491. }
  492. /*
  493. * Generates random output using RAND_bytes() and RAND_priv_bytes()
  494. * and checks whether the three shared DRBGs were reseeded as
  495. * expected.
  496. *
  497. * |expect_success|: expected outcome (as reported by RAND_status())
  498. * |master|, |public|, |private|: pointers to the three shared DRBGs
  499. * |expect_xxx_reseed| =
  500. * 1: it is expected that the specified DRBG is reseeded
  501. * 0: it is expected that the specified DRBG is not reseeded
  502. * -1: don't check whether the specified DRBG was reseeded or not
  503. * |reseed_time|: if nonzero, used instead of time(NULL) to set the
  504. * |before_reseed| time.
  505. */
  506. static int test_drbg_reseed(int expect_success,
  507. RAND_DRBG *master,
  508. RAND_DRBG *public,
  509. RAND_DRBG *private,
  510. int expect_master_reseed,
  511. int expect_public_reseed,
  512. int expect_private_reseed,
  513. time_t reseed_time
  514. )
  515. {
  516. unsigned char buf[32];
  517. time_t before_reseed, after_reseed;
  518. int expected_state = (expect_success ? DRBG_READY : DRBG_ERROR);
  519. /*
  520. * step 1: check preconditions
  521. */
  522. /* Test whether seed propagation is enabled */
  523. if (!TEST_int_ne(master->reseed_counter, 0)
  524. || !TEST_int_ne(public->reseed_counter, 0)
  525. || !TEST_int_ne(private->reseed_counter, 0))
  526. return 0;
  527. /* Check whether the master DRBG's reseed counter is the largest one */
  528. if (!TEST_int_le(public->reseed_counter, master->reseed_counter)
  529. || !TEST_int_le(private->reseed_counter, master->reseed_counter))
  530. return 0;
  531. /*
  532. * step 2: generate random output
  533. */
  534. if (reseed_time == 0)
  535. reseed_time = time(NULL);
  536. /* Generate random output from the public and private DRBG */
  537. before_reseed = expect_master_reseed == 1 ? reseed_time : 0;
  538. if (!TEST_int_eq(RAND_bytes(buf, sizeof(buf)), expect_success)
  539. || !TEST_int_eq(RAND_priv_bytes(buf, sizeof(buf)), expect_success))
  540. return 0;
  541. after_reseed = time(NULL);
  542. /*
  543. * step 3: check postconditions
  544. */
  545. /* Test whether reseeding succeeded as expected */
  546. if (!TEST_int_eq(master->state, expected_state)
  547. || !TEST_int_eq(public->state, expected_state)
  548. || !TEST_int_eq(private->state, expected_state))
  549. return 0;
  550. if (expect_master_reseed >= 0) {
  551. /* Test whether master DRBG was reseeded as expected */
  552. if (!TEST_int_eq(master_ctx.reseed_count, expect_master_reseed))
  553. return 0;
  554. }
  555. if (expect_public_reseed >= 0) {
  556. /* Test whether public DRBG was reseeded as expected */
  557. if (!TEST_int_eq(public_ctx.reseed_count, expect_public_reseed))
  558. return 0;
  559. }
  560. if (expect_private_reseed >= 0) {
  561. /* Test whether public DRBG was reseeded as expected */
  562. if (!TEST_int_eq(private_ctx.reseed_count, expect_private_reseed))
  563. return 0;
  564. }
  565. if (expect_success == 1) {
  566. /* Test whether all three reseed counters are synchronized */
  567. if (!TEST_int_eq(public->reseed_counter, master->reseed_counter)
  568. || !TEST_int_eq(private->reseed_counter, master->reseed_counter))
  569. return 0;
  570. /* Test whether reseed time of master DRBG is set correctly */
  571. if (!TEST_time_t_le(before_reseed, master->reseed_time)
  572. || !TEST_time_t_le(master->reseed_time, after_reseed))
  573. return 0;
  574. /* Test whether reseed times of child DRBGs are synchronized with master */
  575. if (!TEST_time_t_ge(public->reseed_time, master->reseed_time)
  576. || !TEST_time_t_ge(private->reseed_time, master->reseed_time))
  577. return 0;
  578. } else {
  579. ERR_clear_error();
  580. }
  581. return 1;
  582. }
  583. #if defined(OPENSSL_SYS_UNIX)
  584. /*
  585. * Test whether master, public and private DRBG are reseeded after
  586. * forking the process.
  587. */
  588. static int test_drbg_reseed_after_fork(RAND_DRBG *master,
  589. RAND_DRBG *public,
  590. RAND_DRBG *private)
  591. {
  592. pid_t pid;
  593. int status=0;
  594. pid = fork();
  595. if (!TEST_int_ge(pid, 0))
  596. return 0;
  597. if (pid > 0) {
  598. /* I'm the parent; wait for the child and check its exit code */
  599. return TEST_int_eq(waitpid(pid, &status, 0), pid) && TEST_int_eq(status, 0);
  600. }
  601. /* I'm the child; check whether all three DRBGs reseed. */
  602. if (!TEST_true(test_drbg_reseed(1, master, public, private, 1, 1, 1, 0)))
  603. status = 1;
  604. /* Remove hooks */
  605. unhook_drbg(master);
  606. unhook_drbg(public);
  607. unhook_drbg(private);
  608. exit(status);
  609. }
  610. #endif
  611. /*
  612. * Test whether the default rand_method (RAND_OpenSSL()) is
  613. * setup correctly, in particular whether reseeding works
  614. * as designed.
  615. */
  616. static int test_rand_drbg_reseed(void)
  617. {
  618. RAND_DRBG *master, *public, *private;
  619. unsigned char rand_add_buf[256];
  620. int rv=0;
  621. time_t before_reseed;
  622. /* Check whether RAND_OpenSSL() is the default method */
  623. if (!TEST_ptr_eq(RAND_get_rand_method(), RAND_OpenSSL()))
  624. return 0;
  625. /* All three DRBGs should be non-null */
  626. if (!TEST_ptr(master = RAND_DRBG_get0_master())
  627. || !TEST_ptr(public = RAND_DRBG_get0_public())
  628. || !TEST_ptr(private = RAND_DRBG_get0_private()))
  629. return 0;
  630. /* There should be three distinct DRBGs, two of them chained to master */
  631. if (!TEST_ptr_ne(public, private)
  632. || !TEST_ptr_ne(public, master)
  633. || !TEST_ptr_ne(private, master)
  634. || !TEST_ptr_eq(public->parent, master)
  635. || !TEST_ptr_eq(private->parent, master))
  636. return 0;
  637. /* uninstantiate the three global DRBGs */
  638. RAND_DRBG_uninstantiate(private);
  639. RAND_DRBG_uninstantiate(public);
  640. RAND_DRBG_uninstantiate(master);
  641. /* Install hooks for the following tests */
  642. hook_drbg(master, &master_ctx);
  643. hook_drbg(public, &public_ctx);
  644. hook_drbg(private, &private_ctx);
  645. /*
  646. * Test initial seeding of shared DRBGs
  647. */
  648. if (!TEST_true(test_drbg_reseed(1, master, public, private, 1, 1, 1, 0)))
  649. goto error;
  650. reset_drbg_hook_ctx();
  651. /*
  652. * Test initial state of shared DRBGs
  653. */
  654. if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 0, 0, 0)))
  655. goto error;
  656. reset_drbg_hook_ctx();
  657. /*
  658. * Test whether the public and private DRBG are both reseeded when their
  659. * reseed counters differ from the master's reseed counter.
  660. */
  661. master->reseed_counter++;
  662. if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 1, 1, 0)))
  663. goto error;
  664. reset_drbg_hook_ctx();
  665. /*
  666. * Test whether the public DRBG is reseeded when its reseed counter differs
  667. * from the master's reseed counter.
  668. */
  669. master->reseed_counter++;
  670. private->reseed_counter++;
  671. if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 1, 0, 0)))
  672. goto error;
  673. reset_drbg_hook_ctx();
  674. /*
  675. * Test whether the private DRBG is reseeded when its reseed counter differs
  676. * from the master's reseed counter.
  677. */
  678. master->reseed_counter++;
  679. public->reseed_counter++;
  680. if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 0, 1, 0)))
  681. goto error;
  682. reset_drbg_hook_ctx();
  683. #if defined(OPENSSL_SYS_UNIX)
  684. if (!TEST_true(test_drbg_reseed_after_fork(master, public, private)))
  685. goto error;
  686. #endif
  687. /* fill 'randomness' buffer with some arbitrary data */
  688. memset(rand_add_buf, 'r', sizeof(rand_add_buf));
  689. /*
  690. * Test whether all three DRBGs are reseeded by RAND_add().
  691. * The before_reseed time has to be measured here and passed into the
  692. * test_drbg_reseed() test, because the master DRBG gets already reseeded
  693. * in RAND_add(), whence the check for the condition
  694. * before_reseed <= master->reseed_time will fail if the time value happens
  695. * to increase between the RAND_add() and the test_drbg_reseed() call.
  696. */
  697. before_reseed = time(NULL);
  698. RAND_add(rand_add_buf, sizeof(rand_add_buf), sizeof(rand_add_buf));
  699. if (!TEST_true(test_drbg_reseed(1, master, public, private, 1, 1, 1,
  700. before_reseed)))
  701. goto error;
  702. reset_drbg_hook_ctx();
  703. /*
  704. * Test whether none of the DRBGs is reseed if the master fails to reseed
  705. */
  706. master_ctx.fail = 1;
  707. master->reseed_counter++;
  708. RAND_add(rand_add_buf, sizeof(rand_add_buf), sizeof(rand_add_buf));
  709. if (!TEST_true(test_drbg_reseed(0, master, public, private, 0, 0, 0, 0)))
  710. goto error;
  711. reset_drbg_hook_ctx();
  712. rv = 1;
  713. error:
  714. /* Remove hooks */
  715. unhook_drbg(master);
  716. unhook_drbg(public);
  717. unhook_drbg(private);
  718. return rv;
  719. }
  720. #if defined(OPENSSL_THREADS)
  721. static int multi_thread_rand_bytes_succeeded = 1;
  722. static int multi_thread_rand_priv_bytes_succeeded = 1;
  723. static void run_multi_thread_test(void)
  724. {
  725. unsigned char buf[256];
  726. time_t start = time(NULL);
  727. RAND_DRBG *public = NULL, *private = NULL;
  728. if (!TEST_ptr(public = RAND_DRBG_get0_public())
  729. || !TEST_ptr(private = RAND_DRBG_get0_private())) {
  730. multi_thread_rand_bytes_succeeded = 0;
  731. return;
  732. }
  733. RAND_DRBG_set_reseed_time_interval(private, 1);
  734. RAND_DRBG_set_reseed_time_interval(public, 1);
  735. do {
  736. if (RAND_bytes(buf, sizeof(buf)) <= 0)
  737. multi_thread_rand_bytes_succeeded = 0;
  738. if (RAND_priv_bytes(buf, sizeof(buf)) <= 0)
  739. multi_thread_rand_priv_bytes_succeeded = 0;
  740. }
  741. while(time(NULL) - start < 5);
  742. }
  743. # if defined(OPENSSL_SYS_WINDOWS)
  744. typedef HANDLE thread_t;
  745. static DWORD WINAPI thread_run(LPVOID arg)
  746. {
  747. run_multi_thread_test();
  748. /*
  749. * Because we're linking with a static library, we must stop each
  750. * thread explicitly, or so says OPENSSL_thread_stop(3)
  751. */
  752. OPENSSL_thread_stop();
  753. return 0;
  754. }
  755. static int run_thread(thread_t *t)
  756. {
  757. *t = CreateThread(NULL, 0, thread_run, NULL, 0, NULL);
  758. return *t != NULL;
  759. }
  760. static int wait_for_thread(thread_t thread)
  761. {
  762. return WaitForSingleObject(thread, INFINITE) == 0;
  763. }
  764. # else
  765. typedef pthread_t thread_t;
  766. static void *thread_run(void *arg)
  767. {
  768. run_multi_thread_test();
  769. /*
  770. * Because we're linking with a static library, we must stop each
  771. * thread explicitly, or so says OPENSSL_thread_stop(3)
  772. */
  773. OPENSSL_thread_stop();
  774. return NULL;
  775. }
  776. static int run_thread(thread_t *t)
  777. {
  778. return pthread_create(t, NULL, thread_run, NULL) == 0;
  779. }
  780. static int wait_for_thread(thread_t thread)
  781. {
  782. return pthread_join(thread, NULL) == 0;
  783. }
  784. # endif
  785. /*
  786. * The main thread will also run the test, so we'll have THREADS+1 parallel
  787. * tests running
  788. */
  789. # define THREADS 3
  790. static int test_multi_thread(void)
  791. {
  792. thread_t t[THREADS];
  793. int i;
  794. for (i = 0; i < THREADS; i++)
  795. run_thread(&t[i]);
  796. run_multi_thread_test();
  797. for (i = 0; i < THREADS; i++)
  798. wait_for_thread(t[i]);
  799. if (!TEST_true(multi_thread_rand_bytes_succeeded))
  800. return 0;
  801. if (!TEST_true(multi_thread_rand_priv_bytes_succeeded))
  802. return 0;
  803. return 1;
  804. }
  805. #endif
  806. /*
  807. * Test that instantiation with RAND_seed() works as expected
  808. *
  809. * If no os entropy source is available then RAND_seed(buffer, bufsize)
  810. * is expected to succeed if and only if the buffer length is at least
  811. * rand_drbg_seedlen(master) bytes.
  812. *
  813. * If an os entropy source is available then RAND_seed(buffer, bufsize)
  814. * is expected to succeed always.
  815. */
  816. static int test_rand_seed(void)
  817. {
  818. RAND_DRBG *master = NULL;
  819. unsigned char rand_buf[256];
  820. size_t rand_buflen;
  821. size_t required_seed_buflen = 0;
  822. if (!TEST_ptr(master = RAND_DRBG_get0_master()))
  823. return 0;
  824. #ifdef OPENSSL_RAND_SEED_NONE
  825. required_seed_buflen = rand_drbg_seedlen(master);
  826. #endif
  827. memset(rand_buf, 0xCD, sizeof(rand_buf));
  828. for ( rand_buflen = 256 ; rand_buflen > 0 ; --rand_buflen ) {
  829. RAND_DRBG_uninstantiate(master);
  830. RAND_seed(rand_buf, rand_buflen);
  831. if (!TEST_int_eq(RAND_status(),
  832. (rand_buflen >= required_seed_buflen)))
  833. return 0;
  834. }
  835. return 1;
  836. }
  837. /*
  838. * Test that adding additional data with RAND_add() works as expected
  839. * when the master DRBG is instantiated (and below its reseed limit).
  840. *
  841. * This should succeed regardless of whether an os entropy source is
  842. * available or not.
  843. */
  844. static int test_rand_add(void)
  845. {
  846. unsigned char rand_buf[256];
  847. size_t rand_buflen;
  848. memset(rand_buf, 0xCD, sizeof(rand_buf));
  849. /* make sure it's instantiated */
  850. RAND_seed(rand_buf, sizeof(rand_buf));
  851. if (!TEST_true(RAND_status()))
  852. return 0;
  853. for ( rand_buflen = 256 ; rand_buflen > 0 ; --rand_buflen ) {
  854. RAND_add(rand_buf, rand_buflen, 0.0);
  855. if (!TEST_true(RAND_status()))
  856. return 0;
  857. }
  858. return 1;
  859. }
  860. int setup_tests(void)
  861. {
  862. app_data_index = RAND_DRBG_get_ex_new_index(0L, NULL, NULL, NULL, NULL);
  863. ADD_ALL_TESTS(test_kats, OSSL_NELEM(drbg_test));
  864. ADD_ALL_TESTS(test_error_checks, OSSL_NELEM(drbg_test));
  865. ADD_TEST(test_rand_drbg_reseed);
  866. ADD_TEST(test_rand_seed);
  867. ADD_TEST(test_rand_add);
  868. #if defined(OPENSSL_THREADS)
  869. ADD_TEST(test_multi_thread);
  870. #endif
  871. return 1;
  872. }