2
0

drbgtest.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  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. */
  507. static int test_drbg_reseed(int expect_success,
  508. RAND_DRBG *master,
  509. RAND_DRBG *public,
  510. RAND_DRBG *private,
  511. int expect_master_reseed,
  512. int expect_public_reseed,
  513. int expect_private_reseed
  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_prop_counter, 0)
  524. || !TEST_int_ne(public->reseed_prop_counter, 0)
  525. || !TEST_int_ne(private->reseed_prop_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_prop_counter, master->reseed_prop_counter)
  529. || !TEST_int_le(private->reseed_prop_counter, master->reseed_prop_counter))
  530. return 0;
  531. /*
  532. * step 2: generate random output
  533. */
  534. /* Generate random output from the public and private DRBG */
  535. before_reseed = expect_master_reseed == 1 ? time(NULL) : 0;
  536. if (!TEST_int_eq(RAND_bytes(buf, sizeof(buf)), expect_success)
  537. || !TEST_int_eq(RAND_priv_bytes(buf, sizeof(buf)), expect_success))
  538. return 0;
  539. after_reseed = time(NULL);
  540. /*
  541. * step 3: check postconditions
  542. */
  543. /* Test whether reseeding succeeded as expected */
  544. if (!TEST_int_eq(master->state, expected_state)
  545. || !TEST_int_eq(public->state, expected_state)
  546. || !TEST_int_eq(private->state, expected_state))
  547. return 0;
  548. if (expect_master_reseed >= 0) {
  549. /* Test whether master DRBG was reseeded as expected */
  550. if (!TEST_int_eq(master_ctx.reseed_count, expect_master_reseed))
  551. return 0;
  552. }
  553. if (expect_public_reseed >= 0) {
  554. /* Test whether public DRBG was reseeded as expected */
  555. if (!TEST_int_eq(public_ctx.reseed_count, expect_public_reseed))
  556. return 0;
  557. }
  558. if (expect_private_reseed >= 0) {
  559. /* Test whether public DRBG was reseeded as expected */
  560. if (!TEST_int_eq(private_ctx.reseed_count, expect_private_reseed))
  561. return 0;
  562. }
  563. if (expect_success == 1) {
  564. /* Test whether all three reseed counters are synchronized */
  565. if (!TEST_int_eq(public->reseed_prop_counter, master->reseed_prop_counter)
  566. || !TEST_int_eq(private->reseed_prop_counter, master->reseed_prop_counter))
  567. return 0;
  568. /* Test whether reseed time of master DRBG is set correctly */
  569. if (!TEST_time_t_le(before_reseed, master->reseed_time)
  570. || !TEST_time_t_le(master->reseed_time, after_reseed))
  571. return 0;
  572. /* Test whether reseed times of child DRBGs are synchronized with master */
  573. if (!TEST_time_t_ge(public->reseed_time, master->reseed_time)
  574. || !TEST_time_t_ge(private->reseed_time, master->reseed_time))
  575. return 0;
  576. } else {
  577. ERR_clear_error();
  578. }
  579. return 1;
  580. }
  581. /*
  582. * Test whether the default rand_method (RAND_OpenSSL()) is
  583. * setup correctly, in particular whether reseeding works
  584. * as designed.
  585. */
  586. static int test_rand_drbg_reseed(void)
  587. {
  588. RAND_DRBG *master, *public, *private;
  589. unsigned char rand_add_buf[256];
  590. int rv=0;
  591. /* Check whether RAND_OpenSSL() is the default method */
  592. if (!TEST_ptr_eq(RAND_get_rand_method(), RAND_OpenSSL()))
  593. return 0;
  594. /* All three DRBGs should be non-null */
  595. if (!TEST_ptr(master = RAND_DRBG_get0_master())
  596. || !TEST_ptr(public = RAND_DRBG_get0_public())
  597. || !TEST_ptr(private = RAND_DRBG_get0_private()))
  598. return 0;
  599. /* There should be three distinct DRBGs, two of them chained to master */
  600. if (!TEST_ptr_ne(public, private)
  601. || !TEST_ptr_ne(public, master)
  602. || !TEST_ptr_ne(private, master)
  603. || !TEST_ptr_eq(public->parent, master)
  604. || !TEST_ptr_eq(private->parent, master))
  605. return 0;
  606. /* uninstantiate the three global DRBGs */
  607. RAND_DRBG_uninstantiate(private);
  608. RAND_DRBG_uninstantiate(public);
  609. RAND_DRBG_uninstantiate(master);
  610. /* Install hooks for the following tests */
  611. hook_drbg(master, &master_ctx);
  612. hook_drbg(public, &public_ctx);
  613. hook_drbg(private, &private_ctx);
  614. /*
  615. * Test initial seeding of shared DRBGs
  616. */
  617. if (!TEST_true(test_drbg_reseed(1, master, public, private, 1, 1, 1)))
  618. goto error;
  619. reset_drbg_hook_ctx();
  620. /*
  621. * Test initial state of shared DRBGs
  622. */
  623. if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 0, 0)))
  624. goto error;
  625. reset_drbg_hook_ctx();
  626. /*
  627. * Test whether the public and private DRBG are both reseeded when their
  628. * reseed counters differ from the master's reseed counter.
  629. */
  630. master->reseed_prop_counter++;
  631. if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 1, 1)))
  632. goto error;
  633. reset_drbg_hook_ctx();
  634. /*
  635. * Test whether the public DRBG is reseeded when its reseed counter differs
  636. * from the master's reseed counter.
  637. */
  638. master->reseed_prop_counter++;
  639. private->reseed_prop_counter++;
  640. if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 1, 0)))
  641. goto error;
  642. reset_drbg_hook_ctx();
  643. /*
  644. * Test whether the private DRBG is reseeded when its reseed counter differs
  645. * from the master's reseed counter.
  646. */
  647. master->reseed_prop_counter++;
  648. public->reseed_prop_counter++;
  649. if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 0, 1)))
  650. goto error;
  651. reset_drbg_hook_ctx();
  652. /* fill 'randomness' buffer with some arbitrary data */
  653. memset(rand_add_buf, 'r', sizeof(rand_add_buf));
  654. /*
  655. * Test whether all three DRBGs are reseeded by RAND_add()
  656. */
  657. RAND_add(rand_add_buf, sizeof(rand_add_buf), sizeof(rand_add_buf));
  658. if (!TEST_true(test_drbg_reseed(1, master, public, private, 1, 1, 1)))
  659. goto error;
  660. reset_drbg_hook_ctx();
  661. /*
  662. * Test whether none of the DRBGs is reseed if the master fails to reseed
  663. */
  664. master_ctx.fail = 1;
  665. master->reseed_prop_counter++;
  666. RAND_add(rand_add_buf, sizeof(rand_add_buf), sizeof(rand_add_buf));
  667. if (!TEST_true(test_drbg_reseed(0, master, public, private, 0, 0, 0)))
  668. goto error;
  669. reset_drbg_hook_ctx();
  670. rv = 1;
  671. error:
  672. /* Remove hooks */
  673. unhook_drbg(master);
  674. unhook_drbg(public);
  675. unhook_drbg(private);
  676. return rv;
  677. }
  678. #if defined(OPENSSL_THREADS)
  679. static int multi_thread_rand_bytes_succeeded = 1;
  680. static int multi_thread_rand_priv_bytes_succeeded = 1;
  681. static void run_multi_thread_test(void)
  682. {
  683. unsigned char buf[256];
  684. time_t start = time(NULL);
  685. RAND_DRBG *public, *private;
  686. public = RAND_DRBG_get0_public();
  687. private = RAND_DRBG_get0_private();
  688. RAND_DRBG_set_reseed_time_interval(public, 1);
  689. RAND_DRBG_set_reseed_time_interval(private, 1);
  690. do {
  691. if (RAND_bytes(buf, sizeof(buf)) <= 0)
  692. multi_thread_rand_bytes_succeeded = 0;
  693. if (RAND_priv_bytes(buf, sizeof(buf)) <= 0)
  694. multi_thread_rand_priv_bytes_succeeded = 0;
  695. }
  696. while(time(NULL) - start < 5);
  697. }
  698. # if defined(OPENSSL_SYS_WINDOWS)
  699. typedef HANDLE thread_t;
  700. static DWORD WINAPI thread_run(LPVOID arg)
  701. {
  702. run_multi_thread_test();
  703. return 0;
  704. }
  705. static int run_thread(thread_t *t)
  706. {
  707. *t = CreateThread(NULL, 0, thread_run, NULL, 0, NULL);
  708. return *t != NULL;
  709. }
  710. static int wait_for_thread(thread_t thread)
  711. {
  712. return WaitForSingleObject(thread, INFINITE) == 0;
  713. }
  714. # else
  715. typedef pthread_t thread_t;
  716. static void *thread_run(void *arg)
  717. {
  718. run_multi_thread_test();
  719. return NULL;
  720. }
  721. static int run_thread(thread_t *t)
  722. {
  723. return pthread_create(t, NULL, thread_run, NULL) == 0;
  724. }
  725. static int wait_for_thread(thread_t thread)
  726. {
  727. return pthread_join(thread, NULL) == 0;
  728. }
  729. # endif
  730. /*
  731. * The main thread will also run the test, so we'll have THREADS+1 parallel
  732. * tests running
  733. */
  734. # define THREADS 3
  735. static int test_multi_thread(void)
  736. {
  737. thread_t t[THREADS];
  738. int i;
  739. for (i = 0; i < THREADS; i++)
  740. run_thread(&t[i]);
  741. run_multi_thread_test();
  742. for (i = 0; i < THREADS; i++)
  743. wait_for_thread(t[i]);
  744. if (!TEST_true(multi_thread_rand_bytes_succeeded))
  745. return 0;
  746. if (!TEST_true(multi_thread_rand_priv_bytes_succeeded))
  747. return 0;
  748. return 1;
  749. }
  750. #endif
  751. #ifdef OPENSSL_RAND_SEED_NONE
  752. /*
  753. * Calculates the minimum buffer length which needs to be
  754. * provided to RAND_seed() in order to successfully
  755. * instantiate the DRBG.
  756. *
  757. * Copied from rand_drbg_seedlen() in rand_drbg.c
  758. */
  759. static size_t rand_drbg_seedlen(RAND_DRBG *drbg)
  760. {
  761. /*
  762. * If no os entropy source is available then RAND_seed(buffer, bufsize)
  763. * is expected to succeed if and only if the buffer length satisfies
  764. * the following requirements, which follow from the calculations
  765. * in RAND_DRBG_instantiate().
  766. */
  767. size_t min_entropy = drbg->strength;
  768. size_t min_entropylen = drbg->min_entropylen;
  769. /*
  770. * Extra entropy for the random nonce in the absence of a
  771. * get_nonce callback, see comment in RAND_DRBG_instantiate().
  772. */
  773. if (drbg->min_noncelen > 0 && drbg->get_nonce == NULL) {
  774. min_entropy += drbg->strength / 2;
  775. min_entropylen += drbg->min_noncelen;
  776. }
  777. /*
  778. * Convert entropy requirement from bits to bytes
  779. * (dividing by 8 without rounding upwards, because
  780. * all entropy requirements are divisible by 8).
  781. */
  782. min_entropy >>= 3;
  783. /* Return a value that satisfies both requirements */
  784. return min_entropy > min_entropylen ? min_entropy : min_entropylen;
  785. }
  786. #endif /*OPENSSL_RAND_SEED_NONE*/
  787. /*
  788. * Test that instantiation with RAND_seed() works as expected
  789. *
  790. * If no os entropy source is available then RAND_seed(buffer, bufsize)
  791. * is expected to succeed if and only if the buffer length is at least
  792. * rand_drbg_seedlen(master) bytes.
  793. *
  794. * If an os entropy source is available then RAND_seed(buffer, bufsize)
  795. * is expected to succeed always.
  796. */
  797. static int test_rand_seed(void)
  798. {
  799. RAND_DRBG *master = RAND_DRBG_get0_master();
  800. unsigned char rand_buf[256];
  801. size_t rand_buflen;
  802. #ifdef OPENSSL_RAND_SEED_NONE
  803. size_t required_seed_buflen = rand_drbg_seedlen(master);
  804. #else
  805. size_t required_seed_buflen = 0;
  806. #endif
  807. memset(rand_buf, 0xCD, sizeof(rand_buf));
  808. for ( rand_buflen = 256 ; rand_buflen > 0 ; --rand_buflen ) {
  809. RAND_DRBG_uninstantiate(master);
  810. RAND_seed(rand_buf, rand_buflen);
  811. if (!TEST_int_eq(RAND_status(),
  812. (rand_buflen >= required_seed_buflen)))
  813. return 0;
  814. }
  815. return 1;
  816. }
  817. /*
  818. * Test that adding additional data with RAND_add() works as expected
  819. * when the master DRBG is instantiated (and below its reseed limit).
  820. *
  821. * This should succeed regardless of whether an os entropy source is
  822. * available or not.
  823. */
  824. static int test_rand_add(void)
  825. {
  826. unsigned char rand_buf[256];
  827. size_t rand_buflen;
  828. memset(rand_buf, 0xCD, sizeof(rand_buf));
  829. /* make sure it's instantiated */
  830. RAND_seed(rand_buf, sizeof(rand_buf));
  831. if (!TEST_true(RAND_status()))
  832. return 0;
  833. for ( rand_buflen = 256 ; rand_buflen > 0 ; --rand_buflen ) {
  834. RAND_add(rand_buf, rand_buflen, 0.0);
  835. if (!TEST_true(RAND_status()))
  836. return 0;
  837. }
  838. return 1;
  839. }
  840. static int test_multi_set(void)
  841. {
  842. int rv = 0;
  843. RAND_DRBG *drbg = NULL;
  844. /* init drbg with default CTR initializer */
  845. if (!TEST_ptr(drbg = RAND_DRBG_new(0, 0, NULL)))
  846. goto err;
  847. /* change it to use hmac */
  848. if (!TEST_true(RAND_DRBG_set(drbg, NID_sha1, RAND_DRBG_FLAG_HMAC)))
  849. goto err;
  850. /* use same type */
  851. if (!TEST_true(RAND_DRBG_set(drbg, NID_sha1, RAND_DRBG_FLAG_HMAC)))
  852. goto err;
  853. /* change it to use hash */
  854. if (!TEST_true(RAND_DRBG_set(drbg, NID_sha256, 0)))
  855. goto err;
  856. /* use same type */
  857. if (!TEST_true(RAND_DRBG_set(drbg, NID_sha256, 0)))
  858. goto err;
  859. /* change it to use ctr */
  860. if (!TEST_true(RAND_DRBG_set(drbg, NID_aes_192_ctr, 0)))
  861. goto err;
  862. /* use same type */
  863. if (!TEST_true(RAND_DRBG_set(drbg, NID_aes_192_ctr, 0)))
  864. goto err;
  865. if (!TEST_int_gt(RAND_DRBG_instantiate(drbg, NULL, 0), 0))
  866. goto err;
  867. rv = 1;
  868. err:
  869. uninstantiate(drbg);
  870. RAND_DRBG_free(drbg);
  871. return rv;
  872. }
  873. static int test_set_defaults(void)
  874. {
  875. RAND_DRBG *master, *public, *private;
  876. master = RAND_DRBG_get0_master();
  877. public = RAND_DRBG_get0_public();
  878. private = RAND_DRBG_get0_private();
  879. /* Check the default type and flags for master, public and private */
  880. return TEST_int_eq(master->type, RAND_DRBG_TYPE)
  881. && TEST_int_eq(master->flags,
  882. RAND_DRBG_FLAGS | RAND_DRBG_FLAG_MASTER)
  883. && TEST_int_eq(public->type, RAND_DRBG_TYPE)
  884. && TEST_int_eq(public->flags,
  885. RAND_DRBG_FLAGS | RAND_DRBG_FLAG_PUBLIC)
  886. && TEST_int_eq(private->type, RAND_DRBG_TYPE)
  887. && TEST_int_eq(private->flags,
  888. RAND_DRBG_FLAGS | RAND_DRBG_FLAG_PRIVATE)
  889. /* change master DRBG and check again */
  890. && TEST_true(RAND_DRBG_set_defaults(NID_sha256,
  891. RAND_DRBG_FLAG_MASTER))
  892. && TEST_true(RAND_DRBG_uninstantiate(master))
  893. && TEST_int_eq(master->type, NID_sha256)
  894. && TEST_int_eq(master->flags, RAND_DRBG_FLAG_MASTER)
  895. && TEST_int_eq(public->type, RAND_DRBG_TYPE)
  896. && TEST_int_eq(public->flags,
  897. RAND_DRBG_FLAGS | RAND_DRBG_FLAG_PUBLIC)
  898. && TEST_int_eq(private->type, RAND_DRBG_TYPE)
  899. && TEST_int_eq(private->flags,
  900. RAND_DRBG_FLAGS | RAND_DRBG_FLAG_PRIVATE)
  901. /* change private DRBG and check again */
  902. && TEST_true(RAND_DRBG_set_defaults(NID_sha256,
  903. RAND_DRBG_FLAG_PRIVATE|RAND_DRBG_FLAG_HMAC))
  904. && TEST_true(RAND_DRBG_uninstantiate(private))
  905. && TEST_int_eq(master->type, NID_sha256)
  906. && TEST_int_eq(master->flags, RAND_DRBG_FLAG_MASTER)
  907. && TEST_int_eq(public->type, RAND_DRBG_TYPE)
  908. && TEST_int_eq(public->flags,
  909. RAND_DRBG_FLAGS | RAND_DRBG_FLAG_PUBLIC)
  910. && TEST_int_eq(private->type, NID_sha256)
  911. && TEST_int_eq(private->flags,
  912. RAND_DRBG_FLAG_PRIVATE | RAND_DRBG_FLAG_HMAC)
  913. /* change public DRBG and check again */
  914. && TEST_true(RAND_DRBG_set_defaults(NID_sha1,
  915. RAND_DRBG_FLAG_PUBLIC
  916. | RAND_DRBG_FLAG_HMAC))
  917. && TEST_true(RAND_DRBG_uninstantiate(public))
  918. && TEST_int_eq(master->type, NID_sha256)
  919. && TEST_int_eq(master->flags, RAND_DRBG_FLAG_MASTER)
  920. && TEST_int_eq(public->type, NID_sha1)
  921. && TEST_int_eq(public->flags,
  922. RAND_DRBG_FLAG_PUBLIC | RAND_DRBG_FLAG_HMAC)
  923. && TEST_int_eq(private->type, NID_sha256)
  924. && TEST_int_eq(private->flags,
  925. RAND_DRBG_FLAG_PRIVATE | RAND_DRBG_FLAG_HMAC)
  926. /* Change DRBG defaults and change public and check again */
  927. && TEST_true(RAND_DRBG_set_defaults(NID_sha256, 0))
  928. && TEST_true(RAND_DRBG_uninstantiate(public))
  929. && TEST_int_eq(public->type, NID_sha256)
  930. && TEST_int_eq(public->flags, RAND_DRBG_FLAG_PUBLIC)
  931. /* Change DRBG defaults and change master and check again */
  932. && TEST_true(RAND_DRBG_set_defaults(NID_aes_256_ctr,
  933. RAND_DRBG_FLAG_CTR_NO_DF))
  934. && TEST_true(RAND_DRBG_uninstantiate(master))
  935. && TEST_int_eq(master->type, NID_aes_256_ctr)
  936. && TEST_int_eq(master->flags,
  937. RAND_DRBG_FLAG_MASTER|RAND_DRBG_FLAG_CTR_NO_DF)
  938. /* Reset back to the standard defaults */
  939. && TEST_true(RAND_DRBG_set_defaults(RAND_DRBG_TYPE,
  940. RAND_DRBG_FLAGS
  941. | RAND_DRBG_FLAG_MASTER
  942. | RAND_DRBG_FLAG_PUBLIC
  943. | RAND_DRBG_FLAG_PRIVATE))
  944. && TEST_true(RAND_DRBG_uninstantiate(master))
  945. && TEST_true(RAND_DRBG_uninstantiate(public))
  946. && TEST_true(RAND_DRBG_uninstantiate(private));
  947. }
  948. int setup_tests(void)
  949. {
  950. app_data_index = RAND_DRBG_get_ex_new_index(0L, NULL, NULL, NULL, NULL);
  951. ADD_ALL_TESTS(test_kats, OSSL_NELEM(drbg_test));
  952. ADD_ALL_TESTS(test_error_checks, OSSL_NELEM(drbg_test));
  953. ADD_TEST(test_rand_drbg_reseed);
  954. ADD_TEST(test_rand_seed);
  955. ADD_TEST(test_rand_add);
  956. ADD_TEST(test_multi_set);
  957. ADD_TEST(test_set_defaults);
  958. #if defined(OPENSSL_THREADS)
  959. ADD_TEST(test_multi_thread);
  960. #endif
  961. return 1;
  962. }