drbgtest.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  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. #define make_drbg_test_data_hash(nid, pr, p) \
  93. make_drbg_test_data(nid, RAND_DRBG_FLAG_HMAC, hmac_##pr, p), \
  94. make_drbg_test_data(nid, 0, pr, p)
  95. static DRBG_SELFTEST_DATA drbg_test[] = {
  96. make_drbg_test_data_no_df (NID_aes_128_ctr, aes_128_no_df, 0),
  97. make_drbg_test_data_no_df (NID_aes_192_ctr, aes_192_no_df, 0),
  98. make_drbg_test_data_no_df (NID_aes_256_ctr, aes_256_no_df, 1),
  99. make_drbg_test_data_use_df(NID_aes_128_ctr, aes_128_use_df, 0),
  100. make_drbg_test_data_use_df(NID_aes_192_ctr, aes_192_use_df, 0),
  101. make_drbg_test_data_use_df(NID_aes_256_ctr, aes_256_use_df, 1),
  102. make_drbg_test_data_hash(NID_sha1, sha1, 0),
  103. make_drbg_test_data_hash(NID_sha224, sha224, 0),
  104. make_drbg_test_data_hash(NID_sha256, sha256, 1),
  105. make_drbg_test_data_hash(NID_sha384, sha384, 0),
  106. make_drbg_test_data_hash(NID_sha512, sha512, 0),
  107. };
  108. static int app_data_index;
  109. /*
  110. * Test context data, attached as EXDATA to the RAND_DRBG
  111. */
  112. typedef struct test_ctx_st {
  113. const unsigned char *entropy;
  114. size_t entropylen;
  115. int entropycnt;
  116. const unsigned char *nonce;
  117. size_t noncelen;
  118. int noncecnt;
  119. } TEST_CTX;
  120. static size_t kat_entropy(RAND_DRBG *drbg, unsigned char **pout,
  121. int entropy, size_t min_len, size_t max_len,
  122. int prediction_resistance)
  123. {
  124. TEST_CTX *t = (TEST_CTX *)RAND_DRBG_get_ex_data(drbg, app_data_index);
  125. t->entropycnt++;
  126. *pout = (unsigned char *)t->entropy;
  127. return t->entropylen;
  128. }
  129. static size_t kat_nonce(RAND_DRBG *drbg, unsigned char **pout,
  130. int entropy, size_t min_len, size_t max_len)
  131. {
  132. TEST_CTX *t = (TEST_CTX *)RAND_DRBG_get_ex_data(drbg, app_data_index);
  133. t->noncecnt++;
  134. *pout = (unsigned char *)t->nonce;
  135. return t->noncelen;
  136. }
  137. static int uninstantiate(RAND_DRBG *drbg)
  138. {
  139. int ret = drbg == NULL ? 1 : RAND_DRBG_uninstantiate(drbg);
  140. ERR_clear_error();
  141. return ret;
  142. }
  143. /*
  144. * Do a single KAT test. Return 0 on failure.
  145. */
  146. static int single_kat(DRBG_SELFTEST_DATA *td)
  147. {
  148. RAND_DRBG *drbg = NULL;
  149. TEST_CTX t;
  150. int failures = 0;
  151. unsigned char buff[1024];
  152. /*
  153. * Test without PR: Instantiate DRBG with test entropy, nonce and
  154. * personalisation string.
  155. */
  156. if (!TEST_ptr(drbg = RAND_DRBG_new(td->nid, td->flags, NULL)))
  157. return 0;
  158. if (!TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
  159. kat_nonce, NULL))) {
  160. failures++;
  161. goto err;
  162. }
  163. memset(&t, 0, sizeof(t));
  164. t.entropy = td->entropy;
  165. t.entropylen = td->entropylen;
  166. t.nonce = td->nonce;
  167. t.noncelen = td->noncelen;
  168. RAND_DRBG_set_ex_data(drbg, app_data_index, &t);
  169. if (!TEST_true(RAND_DRBG_instantiate(drbg, td->pers, td->perslen))
  170. || !TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
  171. td->adin, td->adinlen))
  172. || !TEST_mem_eq(td->expected, td->exlen, buff, td->exlen))
  173. failures++;
  174. /* Reseed DRBG with test entropy and additional input */
  175. t.entropy = td->entropyreseed;
  176. t.entropylen = td->entropyreseedlen;
  177. if (!TEST_true(RAND_DRBG_reseed(drbg, td->adinreseed, td->adinreseedlen, 0)
  178. || !TEST_true(RAND_DRBG_generate(drbg, buff, td->kat2len, 0,
  179. td->adin2, td->adin2len))
  180. || !TEST_mem_eq(td->kat2, td->kat2len, buff, td->kat2len)))
  181. failures++;
  182. uninstantiate(drbg);
  183. /*
  184. * Now test with PR: Instantiate DRBG with test entropy, nonce and
  185. * personalisation string.
  186. */
  187. if (!TEST_true(RAND_DRBG_set(drbg, td->nid, td->flags))
  188. || !TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
  189. kat_nonce, NULL)))
  190. failures++;
  191. RAND_DRBG_set_ex_data(drbg, app_data_index, &t);
  192. t.entropy = td->entropy_pr;
  193. t.entropylen = td->entropylen_pr;
  194. t.nonce = td->nonce_pr;
  195. t.noncelen = td->noncelen_pr;
  196. t.entropycnt = 0;
  197. t.noncecnt = 0;
  198. if (!TEST_true(RAND_DRBG_instantiate(drbg, td->pers_pr, td->perslen_pr)))
  199. failures++;
  200. /*
  201. * Now generate with PR: we need to supply entropy as this will
  202. * perform a reseed operation.
  203. */
  204. t.entropy = td->entropypr_pr;
  205. t.entropylen = td->entropyprlen_pr;
  206. if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->katlen_pr, 1,
  207. td->adin_pr, td->adinlen_pr))
  208. || !TEST_mem_eq(td->kat_pr, td->katlen_pr, buff, td->katlen_pr))
  209. failures++;
  210. /*
  211. * Now generate again with PR: supply new entropy again.
  212. */
  213. t.entropy = td->entropyg_pr;
  214. t.entropylen = td->entropyglen_pr;
  215. if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->kat2len_pr, 1,
  216. td->ading_pr, td->adinglen_pr))
  217. || !TEST_mem_eq(td->kat2_pr, td->kat2len_pr,
  218. buff, td->kat2len_pr))
  219. failures++;
  220. err:
  221. uninstantiate(drbg);
  222. RAND_DRBG_free(drbg);
  223. return failures == 0;
  224. }
  225. /*
  226. * Initialise a DRBG based on selftest data
  227. */
  228. static int init(RAND_DRBG *drbg, DRBG_SELFTEST_DATA *td, TEST_CTX *t)
  229. {
  230. if (!TEST_true(RAND_DRBG_set(drbg, td->nid, td->flags))
  231. || !TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
  232. kat_nonce, NULL)))
  233. return 0;
  234. RAND_DRBG_set_ex_data(drbg, app_data_index, t);
  235. t->entropy = td->entropy;
  236. t->entropylen = td->entropylen;
  237. t->nonce = td->nonce;
  238. t->noncelen = td->noncelen;
  239. t->entropycnt = 0;
  240. t->noncecnt = 0;
  241. return 1;
  242. }
  243. /*
  244. * Initialise and instantiate DRBG based on selftest data
  245. */
  246. static int instantiate(RAND_DRBG *drbg, DRBG_SELFTEST_DATA *td,
  247. TEST_CTX *t)
  248. {
  249. if (!TEST_true(init(drbg, td, t))
  250. || !TEST_true(RAND_DRBG_instantiate(drbg, td->pers, td->perslen)))
  251. return 0;
  252. return 1;
  253. }
  254. /*
  255. * Perform extensive error checking as required by SP800-90.
  256. * Induce several failure modes and check an error condition is set.
  257. */
  258. static int error_check(DRBG_SELFTEST_DATA *td)
  259. {
  260. static char zero[sizeof(RAND_DRBG)];
  261. RAND_DRBG *drbg = NULL;
  262. TEST_CTX t;
  263. unsigned char buff[1024];
  264. unsigned int reseed_counter_tmp;
  265. int ret = 0;
  266. if (!TEST_ptr(drbg = RAND_DRBG_new(td->nid, td->flags, NULL)))
  267. goto err;
  268. /*
  269. * Personalisation string tests
  270. */
  271. /* Test detection of too large personlisation string */
  272. if (!init(drbg, td, &t)
  273. || RAND_DRBG_instantiate(drbg, td->pers, drbg->max_perslen + 1) > 0)
  274. goto err;
  275. /*
  276. * Entropy source tests
  277. */
  278. /* Test entropy source failure detection: i.e. returns no data */
  279. t.entropylen = 0;
  280. if (TEST_int_le(RAND_DRBG_instantiate(drbg, td->pers, td->perslen), 0))
  281. goto err;
  282. /* Try to generate output from uninstantiated DRBG */
  283. if (!TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
  284. td->adin, td->adinlen))
  285. || !uninstantiate(drbg))
  286. goto err;
  287. /* Test insufficient entropy */
  288. t.entropylen = drbg->min_entropylen - 1;
  289. if (!init(drbg, td, &t)
  290. || RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0
  291. || !uninstantiate(drbg))
  292. goto err;
  293. /* Test too much entropy */
  294. t.entropylen = drbg->max_entropylen + 1;
  295. if (!init(drbg, td, &t)
  296. || RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0
  297. || !uninstantiate(drbg))
  298. goto err;
  299. /*
  300. * Nonce tests
  301. */
  302. /* Test too small nonce */
  303. if (drbg->min_noncelen) {
  304. t.noncelen = drbg->min_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. /* Test too large nonce */
  311. if (drbg->max_noncelen) {
  312. t.noncelen = drbg->max_noncelen + 1;
  313. if (!init(drbg, td, &t)
  314. || RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0
  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->reseed_gen_counter;
  344. drbg->reseed_gen_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->reseed_gen_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->reseed_gen_counter;
  366. drbg->reseed_gen_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->reseed_gen_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 (!init(drbg, td, &t)
  380. || RAND_DRBG_reseed(drbg, td->adin, drbg->max_adinlen + 1, 0) > 0)
  381. goto err;
  382. /* Test explicit reseed with entropy source failure */
  383. t.entropylen = 0;
  384. if (!TEST_int_le(RAND_DRBG_reseed(drbg, td->adin, td->adinlen, 0), 0)
  385. || !uninstantiate(drbg))
  386. goto err;
  387. /* Test explicit reseed with too much entropy */
  388. if (!init(drbg, td, &t))
  389. goto err;
  390. t.entropylen = drbg->max_entropylen + 1;
  391. if (!TEST_int_le(RAND_DRBG_reseed(drbg, td->adin, td->adinlen, 0), 0)
  392. || !uninstantiate(drbg))
  393. goto err;
  394. /* Test explicit reseed with too little entropy */
  395. if (!init(drbg, td, &t))
  396. goto err;
  397. t.entropylen = drbg->min_entropylen - 1;
  398. if (!TEST_int_le(RAND_DRBG_reseed(drbg, td->adin, td->adinlen, 0), 0)
  399. || !uninstantiate(drbg))
  400. goto err;
  401. /* Standard says we have to check uninstantiate really zeroes */
  402. if (!TEST_mem_eq(zero, sizeof(drbg->data), &drbg->data, sizeof(drbg->data)))
  403. goto err;
  404. ret = 1;
  405. err:
  406. uninstantiate(drbg);
  407. RAND_DRBG_free(drbg);
  408. return ret;
  409. }
  410. static int test_kats(int i)
  411. {
  412. DRBG_SELFTEST_DATA *td = &drbg_test[i];
  413. int rv = 0;
  414. if (!single_kat(td))
  415. goto err;
  416. rv = 1;
  417. err:
  418. return rv;
  419. }
  420. static int test_error_checks(int i)
  421. {
  422. DRBG_SELFTEST_DATA *td = &drbg_test[i];
  423. int rv = 0;
  424. if (error_check(td))
  425. goto err;
  426. rv = 1;
  427. err:
  428. return rv;
  429. }
  430. /*
  431. * Hook context data, attached as EXDATA to the RAND_DRBG
  432. */
  433. typedef struct hook_ctx_st {
  434. RAND_DRBG *drbg;
  435. /*
  436. * Currently, all DRBGs use the same get_entropy() callback.
  437. * The tests however, don't assume this and store
  438. * the original callback for every DRBG separately.
  439. */
  440. RAND_DRBG_get_entropy_fn get_entropy;
  441. /* forces a failure of the get_entropy() call if nonzero */
  442. int fail;
  443. /* counts successful reseeds */
  444. int reseed_count;
  445. } HOOK_CTX;
  446. static HOOK_CTX master_ctx, public_ctx, private_ctx;
  447. static HOOK_CTX *get_hook_ctx(RAND_DRBG *drbg)
  448. {
  449. return (HOOK_CTX *)RAND_DRBG_get_ex_data(drbg, app_data_index);
  450. }
  451. /* Intercepts and counts calls to the get_entropy() callback */
  452. static size_t get_entropy_hook(RAND_DRBG *drbg, unsigned char **pout,
  453. int entropy, size_t min_len, size_t max_len,
  454. int prediction_resistance)
  455. {
  456. size_t ret;
  457. HOOK_CTX *ctx = get_hook_ctx(drbg);
  458. if (ctx->fail != 0)
  459. return 0;
  460. ret = ctx->get_entropy(drbg, pout, entropy, min_len, max_len,
  461. prediction_resistance);
  462. if (ret != 0)
  463. ctx->reseed_count++;
  464. return ret;
  465. }
  466. /* Installs a hook for the get_entropy() callback of the given drbg */
  467. static void hook_drbg(RAND_DRBG *drbg, HOOK_CTX *ctx)
  468. {
  469. memset(ctx, 0, sizeof(*ctx));
  470. ctx->drbg = drbg;
  471. ctx->get_entropy = drbg->get_entropy;
  472. drbg->get_entropy = get_entropy_hook;
  473. RAND_DRBG_set_ex_data(drbg, app_data_index, ctx);
  474. }
  475. /* Installs the hook for the get_entropy() callback of the given drbg */
  476. static void unhook_drbg(RAND_DRBG *drbg)
  477. {
  478. HOOK_CTX *ctx = get_hook_ctx(drbg);
  479. drbg->get_entropy = ctx->get_entropy;
  480. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DRBG, drbg, &drbg->ex_data);
  481. }
  482. /* Resets the given hook context */
  483. static void reset_hook_ctx(HOOK_CTX *ctx)
  484. {
  485. ctx->fail = 0;
  486. ctx->reseed_count = 0;
  487. }
  488. /* Resets all drbg hook contexts */
  489. static void reset_drbg_hook_ctx(void)
  490. {
  491. reset_hook_ctx(&master_ctx);
  492. reset_hook_ctx(&public_ctx);
  493. reset_hook_ctx(&private_ctx);
  494. }
  495. /*
  496. * Generates random output using RAND_bytes() and RAND_priv_bytes()
  497. * and checks whether the three shared DRBGs were reseeded as
  498. * expected.
  499. *
  500. * |expect_success|: expected outcome (as reported by RAND_status())
  501. * |master|, |public|, |private|: pointers to the three shared DRBGs
  502. * |expect_xxx_reseed| =
  503. * 1: it is expected that the specified DRBG is reseeded
  504. * 0: it is expected that the specified DRBG is not reseeded
  505. * -1: don't check whether the specified DRBG was reseeded or not
  506. * |reseed_time|: if nonzero, used instead of time(NULL) to set the
  507. * |before_reseed| time.
  508. */
  509. static int test_drbg_reseed(int expect_success,
  510. RAND_DRBG *master,
  511. RAND_DRBG *public,
  512. RAND_DRBG *private,
  513. int expect_master_reseed,
  514. int expect_public_reseed,
  515. int expect_private_reseed,
  516. time_t reseed_time
  517. )
  518. {
  519. unsigned char buf[32];
  520. time_t before_reseed, after_reseed;
  521. int expected_state = (expect_success ? DRBG_READY : DRBG_ERROR);
  522. /*
  523. * step 1: check preconditions
  524. */
  525. /* Test whether seed propagation is enabled */
  526. if (!TEST_int_ne(master->reseed_prop_counter, 0)
  527. || !TEST_int_ne(public->reseed_prop_counter, 0)
  528. || !TEST_int_ne(private->reseed_prop_counter, 0))
  529. return 0;
  530. /* Check whether the master DRBG's reseed counter is the largest one */
  531. if (!TEST_int_le(public->reseed_prop_counter, master->reseed_prop_counter)
  532. || !TEST_int_le(private->reseed_prop_counter, master->reseed_prop_counter))
  533. return 0;
  534. /*
  535. * step 2: generate random output
  536. */
  537. if (reseed_time == 0)
  538. reseed_time = time(NULL);
  539. /* Generate random output from the public and private DRBG */
  540. before_reseed = expect_master_reseed == 1 ? reseed_time : 0;
  541. if (!TEST_int_eq(RAND_bytes(buf, sizeof(buf)), expect_success)
  542. || !TEST_int_eq(RAND_priv_bytes(buf, sizeof(buf)), expect_success))
  543. return 0;
  544. after_reseed = time(NULL);
  545. /*
  546. * step 3: check postconditions
  547. */
  548. /* Test whether reseeding succeeded as expected */
  549. if (!TEST_int_eq(master->state, expected_state)
  550. || !TEST_int_eq(public->state, expected_state)
  551. || !TEST_int_eq(private->state, expected_state))
  552. return 0;
  553. if (expect_master_reseed >= 0) {
  554. /* Test whether master DRBG was reseeded as expected */
  555. if (!TEST_int_eq(master_ctx.reseed_count, expect_master_reseed))
  556. return 0;
  557. }
  558. if (expect_public_reseed >= 0) {
  559. /* Test whether public DRBG was reseeded as expected */
  560. if (!TEST_int_eq(public_ctx.reseed_count, expect_public_reseed))
  561. return 0;
  562. }
  563. if (expect_private_reseed >= 0) {
  564. /* Test whether public DRBG was reseeded as expected */
  565. if (!TEST_int_eq(private_ctx.reseed_count, expect_private_reseed))
  566. return 0;
  567. }
  568. if (expect_success == 1) {
  569. /* Test whether all three reseed counters are synchronized */
  570. if (!TEST_int_eq(public->reseed_prop_counter, master->reseed_prop_counter)
  571. || !TEST_int_eq(private->reseed_prop_counter, master->reseed_prop_counter))
  572. return 0;
  573. /* Test whether reseed time of master DRBG is set correctly */
  574. if (!TEST_time_t_le(before_reseed, master->reseed_time)
  575. || !TEST_time_t_le(master->reseed_time, after_reseed))
  576. return 0;
  577. /* Test whether reseed times of child DRBGs are synchronized with master */
  578. if (!TEST_time_t_ge(public->reseed_time, master->reseed_time)
  579. || !TEST_time_t_ge(private->reseed_time, master->reseed_time))
  580. return 0;
  581. } else {
  582. ERR_clear_error();
  583. }
  584. return 1;
  585. }
  586. /*
  587. * Test whether the default rand_method (RAND_OpenSSL()) is
  588. * setup correctly, in particular whether reseeding works
  589. * as designed.
  590. */
  591. static int test_rand_drbg_reseed(void)
  592. {
  593. RAND_DRBG *master, *public, *private;
  594. unsigned char rand_add_buf[256];
  595. int rv=0;
  596. time_t before_reseed;
  597. /* Check whether RAND_OpenSSL() is the default method */
  598. if (!TEST_ptr_eq(RAND_get_rand_method(), RAND_OpenSSL()))
  599. return 0;
  600. /* All three DRBGs should be non-null */
  601. if (!TEST_ptr(master = RAND_DRBG_get0_master())
  602. || !TEST_ptr(public = RAND_DRBG_get0_public())
  603. || !TEST_ptr(private = RAND_DRBG_get0_private()))
  604. return 0;
  605. /* There should be three distinct DRBGs, two of them chained to master */
  606. if (!TEST_ptr_ne(public, private)
  607. || !TEST_ptr_ne(public, master)
  608. || !TEST_ptr_ne(private, master)
  609. || !TEST_ptr_eq(public->parent, master)
  610. || !TEST_ptr_eq(private->parent, master))
  611. return 0;
  612. /* uninstantiate the three global DRBGs */
  613. RAND_DRBG_uninstantiate(private);
  614. RAND_DRBG_uninstantiate(public);
  615. RAND_DRBG_uninstantiate(master);
  616. /* Install hooks for the following tests */
  617. hook_drbg(master, &master_ctx);
  618. hook_drbg(public, &public_ctx);
  619. hook_drbg(private, &private_ctx);
  620. /*
  621. * Test initial seeding of shared DRBGs
  622. */
  623. if (!TEST_true(test_drbg_reseed(1, master, public, private, 1, 1, 1, 0)))
  624. goto error;
  625. reset_drbg_hook_ctx();
  626. /*
  627. * Test initial state of shared DRBGs
  628. */
  629. if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 0, 0, 0)))
  630. goto error;
  631. reset_drbg_hook_ctx();
  632. /*
  633. * Test whether the public and private DRBG are both reseeded when their
  634. * reseed counters differ from the master's reseed counter.
  635. */
  636. master->reseed_prop_counter++;
  637. if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 1, 1, 0)))
  638. goto error;
  639. reset_drbg_hook_ctx();
  640. /*
  641. * Test whether the public DRBG is reseeded when its reseed counter differs
  642. * from the master's reseed counter.
  643. */
  644. master->reseed_prop_counter++;
  645. private->reseed_prop_counter++;
  646. if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 1, 0, 0)))
  647. goto error;
  648. reset_drbg_hook_ctx();
  649. /*
  650. * Test whether the private DRBG is reseeded when its reseed counter differs
  651. * from the master's reseed counter.
  652. */
  653. master->reseed_prop_counter++;
  654. public->reseed_prop_counter++;
  655. if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 0, 1, 0)))
  656. goto error;
  657. reset_drbg_hook_ctx();
  658. /* fill 'randomness' buffer with some arbitrary data */
  659. memset(rand_add_buf, 'r', sizeof(rand_add_buf));
  660. /*
  661. * Test whether all three DRBGs are reseeded by RAND_add().
  662. * The before_reseed time has to be measured here and passed into the
  663. * test_drbg_reseed() test, because the master DRBG gets already reseeded
  664. * in RAND_add(), whence the check for the condition
  665. * before_reseed <= master->reseed_time will fail if the time value happens
  666. * to increase between the RAND_add() and the test_drbg_reseed() call.
  667. */
  668. before_reseed = time(NULL);
  669. RAND_add(rand_add_buf, sizeof(rand_add_buf), sizeof(rand_add_buf));
  670. if (!TEST_true(test_drbg_reseed(1, master, public, private, 1, 1, 1,
  671. before_reseed)))
  672. goto error;
  673. reset_drbg_hook_ctx();
  674. /*
  675. * Test whether none of the DRBGs is reseed if the master fails to reseed
  676. */
  677. master_ctx.fail = 1;
  678. master->reseed_prop_counter++;
  679. RAND_add(rand_add_buf, sizeof(rand_add_buf), sizeof(rand_add_buf));
  680. if (!TEST_true(test_drbg_reseed(0, master, public, private, 0, 0, 0, 0)))
  681. goto error;
  682. reset_drbg_hook_ctx();
  683. rv = 1;
  684. error:
  685. /* Remove hooks */
  686. unhook_drbg(master);
  687. unhook_drbg(public);
  688. unhook_drbg(private);
  689. return rv;
  690. }
  691. #if defined(OPENSSL_THREADS)
  692. static int multi_thread_rand_bytes_succeeded = 1;
  693. static int multi_thread_rand_priv_bytes_succeeded = 1;
  694. static void run_multi_thread_test(void)
  695. {
  696. unsigned char buf[256];
  697. time_t start = time(NULL);
  698. RAND_DRBG *public = NULL, *private = NULL;
  699. if (!TEST_ptr(public = RAND_DRBG_get0_public())
  700. || !TEST_ptr(private = RAND_DRBG_get0_private())) {
  701. multi_thread_rand_bytes_succeeded = 0;
  702. return;
  703. }
  704. RAND_DRBG_set_reseed_time_interval(private, 1);
  705. RAND_DRBG_set_reseed_time_interval(public, 1);
  706. do {
  707. if (RAND_bytes(buf, sizeof(buf)) <= 0)
  708. multi_thread_rand_bytes_succeeded = 0;
  709. if (RAND_priv_bytes(buf, sizeof(buf)) <= 0)
  710. multi_thread_rand_priv_bytes_succeeded = 0;
  711. }
  712. while(time(NULL) - start < 5);
  713. }
  714. # if defined(OPENSSL_SYS_WINDOWS)
  715. typedef HANDLE thread_t;
  716. static DWORD WINAPI thread_run(LPVOID arg)
  717. {
  718. run_multi_thread_test();
  719. return 0;
  720. }
  721. static int run_thread(thread_t *t)
  722. {
  723. *t = CreateThread(NULL, 0, thread_run, NULL, 0, NULL);
  724. return *t != NULL;
  725. }
  726. static int wait_for_thread(thread_t thread)
  727. {
  728. return WaitForSingleObject(thread, INFINITE) == 0;
  729. }
  730. # else
  731. typedef pthread_t thread_t;
  732. static void *thread_run(void *arg)
  733. {
  734. run_multi_thread_test();
  735. return NULL;
  736. }
  737. static int run_thread(thread_t *t)
  738. {
  739. return pthread_create(t, NULL, thread_run, NULL) == 0;
  740. }
  741. static int wait_for_thread(thread_t thread)
  742. {
  743. return pthread_join(thread, NULL) == 0;
  744. }
  745. # endif
  746. /*
  747. * The main thread will also run the test, so we'll have THREADS+1 parallel
  748. * tests running
  749. */
  750. # define THREADS 3
  751. static int test_multi_thread(void)
  752. {
  753. thread_t t[THREADS];
  754. int i;
  755. for (i = 0; i < THREADS; i++)
  756. run_thread(&t[i]);
  757. run_multi_thread_test();
  758. for (i = 0; i < THREADS; i++)
  759. wait_for_thread(t[i]);
  760. if (!TEST_true(multi_thread_rand_bytes_succeeded))
  761. return 0;
  762. if (!TEST_true(multi_thread_rand_priv_bytes_succeeded))
  763. return 0;
  764. return 1;
  765. }
  766. #endif
  767. /*
  768. * Test that instantiation with RAND_seed() works as expected
  769. *
  770. * If no os entropy source is available then RAND_seed(buffer, bufsize)
  771. * is expected to succeed if and only if the buffer length is at least
  772. * rand_drbg_seedlen(master) bytes.
  773. *
  774. * If an os entropy source is available then RAND_seed(buffer, bufsize)
  775. * is expected to succeed always.
  776. */
  777. static int test_rand_seed(void)
  778. {
  779. RAND_DRBG *master = NULL;
  780. unsigned char rand_buf[256];
  781. size_t rand_buflen;
  782. size_t required_seed_buflen = 0;
  783. if (!TEST_ptr(master = RAND_DRBG_get0_master()))
  784. return 0;
  785. #ifdef OPENSSL_RAND_SEED_NONE
  786. required_seed_buflen = rand_drbg_seedlen(master);
  787. #endif
  788. memset(rand_buf, 0xCD, sizeof(rand_buf));
  789. for ( rand_buflen = 256 ; rand_buflen > 0 ; --rand_buflen ) {
  790. RAND_DRBG_uninstantiate(master);
  791. RAND_seed(rand_buf, rand_buflen);
  792. if (!TEST_int_eq(RAND_status(),
  793. (rand_buflen >= required_seed_buflen)))
  794. return 0;
  795. }
  796. return 1;
  797. }
  798. /*
  799. * Test that adding additional data with RAND_add() works as expected
  800. * when the master DRBG is instantiated (and below its reseed limit).
  801. *
  802. * This should succeed regardless of whether an os entropy source is
  803. * available or not.
  804. */
  805. static int test_rand_add(void)
  806. {
  807. unsigned char rand_buf[256];
  808. size_t rand_buflen;
  809. memset(rand_buf, 0xCD, sizeof(rand_buf));
  810. /* make sure it's instantiated */
  811. RAND_seed(rand_buf, sizeof(rand_buf));
  812. if (!TEST_true(RAND_status()))
  813. return 0;
  814. for ( rand_buflen = 256 ; rand_buflen > 0 ; --rand_buflen ) {
  815. RAND_add(rand_buf, rand_buflen, 0.0);
  816. if (!TEST_true(RAND_status()))
  817. return 0;
  818. }
  819. return 1;
  820. }
  821. static int test_multi_set(void)
  822. {
  823. int rv = 0;
  824. RAND_DRBG *drbg = NULL;
  825. /* init drbg with default CTR initializer */
  826. if (!TEST_ptr(drbg = RAND_DRBG_new(0, 0, NULL)))
  827. goto err;
  828. /* change it to use hmac */
  829. if (!TEST_true(RAND_DRBG_set(drbg, NID_sha1, RAND_DRBG_FLAG_HMAC)))
  830. goto err;
  831. /* use same type */
  832. if (!TEST_true(RAND_DRBG_set(drbg, NID_sha1, RAND_DRBG_FLAG_HMAC)))
  833. goto err;
  834. /* change it to use hash */
  835. if (!TEST_true(RAND_DRBG_set(drbg, NID_sha256, 0)))
  836. goto err;
  837. /* use same type */
  838. if (!TEST_true(RAND_DRBG_set(drbg, NID_sha256, 0)))
  839. goto err;
  840. /* change it to use ctr */
  841. if (!TEST_true(RAND_DRBG_set(drbg, NID_aes_192_ctr, 0)))
  842. goto err;
  843. /* use same type */
  844. if (!TEST_true(RAND_DRBG_set(drbg, NID_aes_192_ctr, 0)))
  845. goto err;
  846. if (!TEST_int_gt(RAND_DRBG_instantiate(drbg, NULL, 0), 0))
  847. goto err;
  848. rv = 1;
  849. err:
  850. uninstantiate(drbg);
  851. RAND_DRBG_free(drbg);
  852. return rv;
  853. }
  854. static int test_set_defaults(void)
  855. {
  856. RAND_DRBG *master = NULL, *public = NULL, *private = NULL;
  857. /* Check the default type and flags for master, public and private */
  858. return TEST_ptr(master = RAND_DRBG_get0_master())
  859. && TEST_ptr(public = RAND_DRBG_get0_public())
  860. && TEST_ptr(private = RAND_DRBG_get0_private())
  861. && TEST_int_eq(master->type, RAND_DRBG_TYPE)
  862. && TEST_int_eq(master->flags,
  863. RAND_DRBG_FLAGS | RAND_DRBG_FLAG_MASTER)
  864. && TEST_int_eq(public->type, RAND_DRBG_TYPE)
  865. && TEST_int_eq(public->flags,
  866. RAND_DRBG_FLAGS | RAND_DRBG_FLAG_PUBLIC)
  867. && TEST_int_eq(private->type, RAND_DRBG_TYPE)
  868. && TEST_int_eq(private->flags,
  869. RAND_DRBG_FLAGS | RAND_DRBG_FLAG_PRIVATE)
  870. /* change master DRBG and check again */
  871. && TEST_true(RAND_DRBG_set_defaults(NID_sha256,
  872. RAND_DRBG_FLAG_MASTER))
  873. && TEST_true(RAND_DRBG_uninstantiate(master))
  874. && TEST_int_eq(master->type, NID_sha256)
  875. && TEST_int_eq(master->flags, RAND_DRBG_FLAG_MASTER)
  876. && TEST_int_eq(public->type, RAND_DRBG_TYPE)
  877. && TEST_int_eq(public->flags,
  878. RAND_DRBG_FLAGS | RAND_DRBG_FLAG_PUBLIC)
  879. && TEST_int_eq(private->type, RAND_DRBG_TYPE)
  880. && TEST_int_eq(private->flags,
  881. RAND_DRBG_FLAGS | RAND_DRBG_FLAG_PRIVATE)
  882. /* change private DRBG and check again */
  883. && TEST_true(RAND_DRBG_set_defaults(NID_sha256,
  884. RAND_DRBG_FLAG_PRIVATE|RAND_DRBG_FLAG_HMAC))
  885. && TEST_true(RAND_DRBG_uninstantiate(private))
  886. && TEST_int_eq(master->type, NID_sha256)
  887. && TEST_int_eq(master->flags, RAND_DRBG_FLAG_MASTER)
  888. && TEST_int_eq(public->type, RAND_DRBG_TYPE)
  889. && TEST_int_eq(public->flags,
  890. RAND_DRBG_FLAGS | RAND_DRBG_FLAG_PUBLIC)
  891. && TEST_int_eq(private->type, NID_sha256)
  892. && TEST_int_eq(private->flags,
  893. RAND_DRBG_FLAG_PRIVATE | RAND_DRBG_FLAG_HMAC)
  894. /* change public DRBG and check again */
  895. && TEST_true(RAND_DRBG_set_defaults(NID_sha1,
  896. RAND_DRBG_FLAG_PUBLIC
  897. | RAND_DRBG_FLAG_HMAC))
  898. && TEST_true(RAND_DRBG_uninstantiate(public))
  899. && TEST_int_eq(master->type, NID_sha256)
  900. && TEST_int_eq(master->flags, RAND_DRBG_FLAG_MASTER)
  901. && TEST_int_eq(public->type, NID_sha1)
  902. && TEST_int_eq(public->flags,
  903. RAND_DRBG_FLAG_PUBLIC | RAND_DRBG_FLAG_HMAC)
  904. && TEST_int_eq(private->type, NID_sha256)
  905. && TEST_int_eq(private->flags,
  906. RAND_DRBG_FLAG_PRIVATE | RAND_DRBG_FLAG_HMAC)
  907. /* Change DRBG defaults and change public and check again */
  908. && TEST_true(RAND_DRBG_set_defaults(NID_sha256, 0))
  909. && TEST_true(RAND_DRBG_uninstantiate(public))
  910. && TEST_int_eq(public->type, NID_sha256)
  911. && TEST_int_eq(public->flags, RAND_DRBG_FLAG_PUBLIC)
  912. /* Change DRBG defaults and change master and check again */
  913. && TEST_true(RAND_DRBG_set_defaults(NID_aes_256_ctr,
  914. RAND_DRBG_FLAG_CTR_NO_DF))
  915. && TEST_true(RAND_DRBG_uninstantiate(master))
  916. && TEST_int_eq(master->type, NID_aes_256_ctr)
  917. && TEST_int_eq(master->flags,
  918. RAND_DRBG_FLAG_MASTER|RAND_DRBG_FLAG_CTR_NO_DF)
  919. /* Reset back to the standard defaults */
  920. && TEST_true(RAND_DRBG_set_defaults(RAND_DRBG_TYPE,
  921. RAND_DRBG_FLAGS
  922. | RAND_DRBG_FLAG_MASTER
  923. | RAND_DRBG_FLAG_PUBLIC
  924. | RAND_DRBG_FLAG_PRIVATE))
  925. && TEST_true(RAND_DRBG_uninstantiate(master))
  926. && TEST_true(RAND_DRBG_uninstantiate(public))
  927. && TEST_true(RAND_DRBG_uninstantiate(private));
  928. }
  929. int setup_tests(void)
  930. {
  931. app_data_index = RAND_DRBG_get_ex_new_index(0L, NULL, NULL, NULL, NULL);
  932. ADD_ALL_TESTS(test_kats, OSSL_NELEM(drbg_test));
  933. ADD_ALL_TESTS(test_error_checks, OSSL_NELEM(drbg_test));
  934. ADD_TEST(test_rand_drbg_reseed);
  935. ADD_TEST(test_rand_seed);
  936. ADD_TEST(test_rand_add);
  937. ADD_TEST(test_multi_set);
  938. ADD_TEST(test_set_defaults);
  939. #if defined(OPENSSL_THREADS)
  940. ADD_TEST(test_multi_thread);
  941. #endif
  942. return 1;
  943. }