drbgtest.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  1. /*
  2. * Copyright 2011-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #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 (!instantiate(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 (!instantiate(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 (!instantiate(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. /*
  720. * Because we're linking with a static library, we must stop each
  721. * thread explicitly, or so says OPENSSL_thread_stop(3)
  722. */
  723. OPENSSL_thread_stop();
  724. return 0;
  725. }
  726. static int run_thread(thread_t *t)
  727. {
  728. *t = CreateThread(NULL, 0, thread_run, NULL, 0, NULL);
  729. return *t != NULL;
  730. }
  731. static int wait_for_thread(thread_t thread)
  732. {
  733. return WaitForSingleObject(thread, INFINITE) == 0;
  734. }
  735. # else
  736. typedef pthread_t thread_t;
  737. static void *thread_run(void *arg)
  738. {
  739. run_multi_thread_test();
  740. /*
  741. * Because we're linking with a static library, we must stop each
  742. * thread explicitly, or so says OPENSSL_thread_stop(3)
  743. */
  744. OPENSSL_thread_stop();
  745. return NULL;
  746. }
  747. static int run_thread(thread_t *t)
  748. {
  749. return pthread_create(t, NULL, thread_run, NULL) == 0;
  750. }
  751. static int wait_for_thread(thread_t thread)
  752. {
  753. return pthread_join(thread, NULL) == 0;
  754. }
  755. # endif
  756. /*
  757. * The main thread will also run the test, so we'll have THREADS+1 parallel
  758. * tests running
  759. */
  760. # define THREADS 3
  761. static int test_multi_thread(void)
  762. {
  763. thread_t t[THREADS];
  764. int i;
  765. for (i = 0; i < THREADS; i++)
  766. run_thread(&t[i]);
  767. run_multi_thread_test();
  768. for (i = 0; i < THREADS; i++)
  769. wait_for_thread(t[i]);
  770. if (!TEST_true(multi_thread_rand_bytes_succeeded))
  771. return 0;
  772. if (!TEST_true(multi_thread_rand_priv_bytes_succeeded))
  773. return 0;
  774. return 1;
  775. }
  776. #endif
  777. /*
  778. * Test that instantiation with RAND_seed() works as expected
  779. *
  780. * If no os entropy source is available then RAND_seed(buffer, bufsize)
  781. * is expected to succeed if and only if the buffer length is at least
  782. * rand_drbg_seedlen(master) bytes.
  783. *
  784. * If an os entropy source is available then RAND_seed(buffer, bufsize)
  785. * is expected to succeed always.
  786. */
  787. static int test_rand_seed(void)
  788. {
  789. RAND_DRBG *master = NULL;
  790. unsigned char rand_buf[256];
  791. size_t rand_buflen;
  792. size_t required_seed_buflen = 0;
  793. if (!TEST_ptr(master = RAND_DRBG_get0_master()))
  794. return 0;
  795. #ifdef OPENSSL_RAND_SEED_NONE
  796. required_seed_buflen = rand_drbg_seedlen(master);
  797. #endif
  798. memset(rand_buf, 0xCD, sizeof(rand_buf));
  799. for ( rand_buflen = 256 ; rand_buflen > 0 ; --rand_buflen ) {
  800. RAND_DRBG_uninstantiate(master);
  801. RAND_seed(rand_buf, rand_buflen);
  802. if (!TEST_int_eq(RAND_status(),
  803. (rand_buflen >= required_seed_buflen)))
  804. return 0;
  805. }
  806. return 1;
  807. }
  808. /*
  809. * Test that adding additional data with RAND_add() works as expected
  810. * when the master DRBG is instantiated (and below its reseed limit).
  811. *
  812. * This should succeed regardless of whether an os entropy source is
  813. * available or not.
  814. */
  815. static int test_rand_add(void)
  816. {
  817. unsigned char rand_buf[256];
  818. size_t rand_buflen;
  819. memset(rand_buf, 0xCD, sizeof(rand_buf));
  820. /* make sure it's instantiated */
  821. RAND_seed(rand_buf, sizeof(rand_buf));
  822. if (!TEST_true(RAND_status()))
  823. return 0;
  824. for ( rand_buflen = 256 ; rand_buflen > 0 ; --rand_buflen ) {
  825. RAND_add(rand_buf, rand_buflen, 0.0);
  826. if (!TEST_true(RAND_status()))
  827. return 0;
  828. }
  829. return 1;
  830. }
  831. static int test_multi_set(void)
  832. {
  833. int rv = 0;
  834. RAND_DRBG *drbg = NULL;
  835. /* init drbg with default CTR initializer */
  836. if (!TEST_ptr(drbg = RAND_DRBG_new(0, 0, NULL)))
  837. goto err;
  838. /* change it to use hmac */
  839. if (!TEST_true(RAND_DRBG_set(drbg, NID_sha1, RAND_DRBG_FLAG_HMAC)))
  840. goto err;
  841. /* use same type */
  842. if (!TEST_true(RAND_DRBG_set(drbg, NID_sha1, RAND_DRBG_FLAG_HMAC)))
  843. goto err;
  844. /* change it to use hash */
  845. if (!TEST_true(RAND_DRBG_set(drbg, NID_sha256, 0)))
  846. goto err;
  847. /* use same type */
  848. if (!TEST_true(RAND_DRBG_set(drbg, NID_sha256, 0)))
  849. goto err;
  850. /* change it to use ctr */
  851. if (!TEST_true(RAND_DRBG_set(drbg, NID_aes_192_ctr, 0)))
  852. goto err;
  853. /* use same type */
  854. if (!TEST_true(RAND_DRBG_set(drbg, NID_aes_192_ctr, 0)))
  855. goto err;
  856. if (!TEST_int_gt(RAND_DRBG_instantiate(drbg, NULL, 0), 0))
  857. goto err;
  858. rv = 1;
  859. err:
  860. uninstantiate(drbg);
  861. RAND_DRBG_free(drbg);
  862. return rv;
  863. }
  864. static int test_set_defaults(void)
  865. {
  866. RAND_DRBG *master = NULL, *public = NULL, *private = NULL;
  867. /* Check the default type and flags for master, public and private */
  868. return TEST_ptr(master = RAND_DRBG_get0_master())
  869. && TEST_ptr(public = RAND_DRBG_get0_public())
  870. && TEST_ptr(private = RAND_DRBG_get0_private())
  871. && TEST_int_eq(master->type, RAND_DRBG_TYPE)
  872. && TEST_int_eq(master->flags,
  873. RAND_DRBG_FLAGS | RAND_DRBG_FLAG_MASTER)
  874. && TEST_int_eq(public->type, RAND_DRBG_TYPE)
  875. && TEST_int_eq(public->flags,
  876. RAND_DRBG_FLAGS | RAND_DRBG_FLAG_PUBLIC)
  877. && TEST_int_eq(private->type, RAND_DRBG_TYPE)
  878. && TEST_int_eq(private->flags,
  879. RAND_DRBG_FLAGS | RAND_DRBG_FLAG_PRIVATE)
  880. /* change master DRBG and check again */
  881. && TEST_true(RAND_DRBG_set_defaults(NID_sha256,
  882. RAND_DRBG_FLAG_MASTER))
  883. && TEST_true(RAND_DRBG_uninstantiate(master))
  884. && TEST_int_eq(master->type, NID_sha256)
  885. && TEST_int_eq(master->flags, RAND_DRBG_FLAG_MASTER)
  886. && TEST_int_eq(public->type, RAND_DRBG_TYPE)
  887. && TEST_int_eq(public->flags,
  888. RAND_DRBG_FLAGS | RAND_DRBG_FLAG_PUBLIC)
  889. && TEST_int_eq(private->type, RAND_DRBG_TYPE)
  890. && TEST_int_eq(private->flags,
  891. RAND_DRBG_FLAGS | RAND_DRBG_FLAG_PRIVATE)
  892. /* change private DRBG and check again */
  893. && TEST_true(RAND_DRBG_set_defaults(NID_sha256,
  894. RAND_DRBG_FLAG_PRIVATE|RAND_DRBG_FLAG_HMAC))
  895. && TEST_true(RAND_DRBG_uninstantiate(private))
  896. && TEST_int_eq(master->type, NID_sha256)
  897. && TEST_int_eq(master->flags, RAND_DRBG_FLAG_MASTER)
  898. && TEST_int_eq(public->type, RAND_DRBG_TYPE)
  899. && TEST_int_eq(public->flags,
  900. RAND_DRBG_FLAGS | RAND_DRBG_FLAG_PUBLIC)
  901. && TEST_int_eq(private->type, NID_sha256)
  902. && TEST_int_eq(private->flags,
  903. RAND_DRBG_FLAG_PRIVATE | RAND_DRBG_FLAG_HMAC)
  904. /* change public DRBG and check again */
  905. && TEST_true(RAND_DRBG_set_defaults(NID_sha1,
  906. RAND_DRBG_FLAG_PUBLIC
  907. | RAND_DRBG_FLAG_HMAC))
  908. && TEST_true(RAND_DRBG_uninstantiate(public))
  909. && TEST_int_eq(master->type, NID_sha256)
  910. && TEST_int_eq(master->flags, RAND_DRBG_FLAG_MASTER)
  911. && TEST_int_eq(public->type, NID_sha1)
  912. && TEST_int_eq(public->flags,
  913. RAND_DRBG_FLAG_PUBLIC | RAND_DRBG_FLAG_HMAC)
  914. && TEST_int_eq(private->type, NID_sha256)
  915. && TEST_int_eq(private->flags,
  916. RAND_DRBG_FLAG_PRIVATE | RAND_DRBG_FLAG_HMAC)
  917. /* Change DRBG defaults and change public and check again */
  918. && TEST_true(RAND_DRBG_set_defaults(NID_sha256, 0))
  919. && TEST_true(RAND_DRBG_uninstantiate(public))
  920. && TEST_int_eq(public->type, NID_sha256)
  921. && TEST_int_eq(public->flags, RAND_DRBG_FLAG_PUBLIC)
  922. /* Change DRBG defaults and change master and check again */
  923. && TEST_true(RAND_DRBG_set_defaults(NID_aes_256_ctr,
  924. RAND_DRBG_FLAG_CTR_NO_DF))
  925. && TEST_true(RAND_DRBG_uninstantiate(master))
  926. && TEST_int_eq(master->type, NID_aes_256_ctr)
  927. && TEST_int_eq(master->flags,
  928. RAND_DRBG_FLAG_MASTER|RAND_DRBG_FLAG_CTR_NO_DF)
  929. /* Reset back to the standard defaults */
  930. && TEST_true(RAND_DRBG_set_defaults(RAND_DRBG_TYPE,
  931. RAND_DRBG_FLAGS
  932. | RAND_DRBG_FLAG_MASTER
  933. | RAND_DRBG_FLAG_PUBLIC
  934. | RAND_DRBG_FLAG_PRIVATE))
  935. && TEST_true(RAND_DRBG_uninstantiate(master))
  936. && TEST_true(RAND_DRBG_uninstantiate(public))
  937. && TEST_true(RAND_DRBG_uninstantiate(private));
  938. }
  939. int setup_tests(void)
  940. {
  941. app_data_index = RAND_DRBG_get_ex_new_index(0L, NULL, NULL, NULL, NULL);
  942. ADD_ALL_TESTS(test_kats, OSSL_NELEM(drbg_test));
  943. ADD_ALL_TESTS(test_error_checks, OSSL_NELEM(drbg_test));
  944. ADD_TEST(test_rand_drbg_reseed);
  945. ADD_TEST(test_rand_seed);
  946. ADD_TEST(test_rand_add);
  947. ADD_TEST(test_multi_set);
  948. ADD_TEST(test_set_defaults);
  949. #if defined(OPENSSL_THREADS)
  950. ADD_TEST(test_multi_thread);
  951. #endif
  952. return 1;
  953. }