drbgtest.c 28 KB

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