drbgtest.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341
  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. #ifndef FIPS_MODE
  97. /* FIPS mode doesn't support CTR DRBG without a derivation function */
  98. make_drbg_test_data_no_df (NID_aes_128_ctr, aes_128_no_df, 0),
  99. make_drbg_test_data_no_df (NID_aes_192_ctr, aes_192_no_df, 0),
  100. make_drbg_test_data_no_df (NID_aes_256_ctr, aes_256_no_df, 1),
  101. #endif
  102. make_drbg_test_data_use_df(NID_aes_128_ctr, aes_128_use_df, 0),
  103. make_drbg_test_data_use_df(NID_aes_192_ctr, aes_192_use_df, 0),
  104. make_drbg_test_data_use_df(NID_aes_256_ctr, aes_256_use_df, 1),
  105. make_drbg_test_data_hash(NID_sha1, sha1, 0),
  106. make_drbg_test_data_hash(NID_sha224, sha224, 0),
  107. make_drbg_test_data_hash(NID_sha256, sha256, 1),
  108. make_drbg_test_data_hash(NID_sha384, sha384, 0),
  109. make_drbg_test_data_hash(NID_sha512, sha512, 0),
  110. };
  111. static int app_data_index;
  112. /*
  113. * Test context data, attached as EXDATA to the RAND_DRBG
  114. */
  115. typedef struct test_ctx_st {
  116. const unsigned char *entropy;
  117. size_t entropylen;
  118. int entropycnt;
  119. const unsigned char *nonce;
  120. size_t noncelen;
  121. int noncecnt;
  122. } TEST_CTX;
  123. static size_t kat_entropy(RAND_DRBG *drbg, unsigned char **pout,
  124. int entropy, size_t min_len, size_t max_len,
  125. int prediction_resistance)
  126. {
  127. TEST_CTX *t = (TEST_CTX *)RAND_DRBG_get_ex_data(drbg, app_data_index);
  128. t->entropycnt++;
  129. *pout = (unsigned char *)t->entropy;
  130. return t->entropylen;
  131. }
  132. static size_t kat_nonce(RAND_DRBG *drbg, unsigned char **pout,
  133. int entropy, size_t min_len, size_t max_len)
  134. {
  135. TEST_CTX *t = (TEST_CTX *)RAND_DRBG_get_ex_data(drbg, app_data_index);
  136. t->noncecnt++;
  137. *pout = (unsigned char *)t->nonce;
  138. return t->noncelen;
  139. }
  140. /*
  141. * Disable CRNG testing if it is enabled.
  142. * If the DRBG is ready or in an error state, this means an instantiate cycle
  143. * for which the default personalisation string is used.
  144. */
  145. static int disable_crngt(RAND_DRBG *drbg)
  146. {
  147. static const char pers[] = DRBG_DEFAULT_PERS_STRING;
  148. const int instantiate = drbg->state != DRBG_UNINITIALISED;
  149. if (drbg->get_entropy != rand_crngt_get_entropy)
  150. return 1;
  151. if ((instantiate && !RAND_DRBG_uninstantiate(drbg))
  152. || !TEST_true(RAND_DRBG_set_callbacks(drbg, &rand_drbg_get_entropy,
  153. &rand_drbg_cleanup_entropy,
  154. &rand_drbg_get_nonce,
  155. &rand_drbg_cleanup_nonce))
  156. || (instantiate
  157. && !RAND_DRBG_instantiate(drbg, (const unsigned char *)pers,
  158. sizeof(pers) - 1)))
  159. return 0;
  160. return 1;
  161. }
  162. static int uninstantiate(RAND_DRBG *drbg)
  163. {
  164. int ret = drbg == NULL ? 1 : RAND_DRBG_uninstantiate(drbg);
  165. ERR_clear_error();
  166. return ret;
  167. }
  168. /*
  169. * Do a single KAT test. Return 0 on failure.
  170. */
  171. static int single_kat(DRBG_SELFTEST_DATA *td)
  172. {
  173. RAND_DRBG *drbg = NULL;
  174. TEST_CTX t;
  175. int failures = 0;
  176. unsigned char buff[1024];
  177. /*
  178. * Test without PR: Instantiate DRBG with test entropy, nonce and
  179. * personalisation string.
  180. */
  181. if (!TEST_ptr(drbg = RAND_DRBG_new(td->nid, td->flags, NULL)))
  182. return 0;
  183. if (!TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
  184. kat_nonce, NULL))
  185. || !TEST_true(disable_crngt(drbg))) {
  186. failures++;
  187. goto err;
  188. }
  189. memset(&t, 0, sizeof(t));
  190. t.entropy = td->entropy;
  191. t.entropylen = td->entropylen;
  192. t.nonce = td->nonce;
  193. t.noncelen = td->noncelen;
  194. RAND_DRBG_set_ex_data(drbg, app_data_index, &t);
  195. if (!TEST_true(RAND_DRBG_instantiate(drbg, td->pers, td->perslen))
  196. || !TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
  197. td->adin, td->adinlen))
  198. || !TEST_mem_eq(td->expected, td->exlen, buff, td->exlen))
  199. failures++;
  200. /* Reseed DRBG with test entropy and additional input */
  201. t.entropy = td->entropyreseed;
  202. t.entropylen = td->entropyreseedlen;
  203. if (!TEST_true(RAND_DRBG_reseed(drbg, td->adinreseed, td->adinreseedlen, 0)
  204. || !TEST_true(RAND_DRBG_generate(drbg, buff, td->kat2len, 0,
  205. td->adin2, td->adin2len))
  206. || !TEST_mem_eq(td->kat2, td->kat2len, buff, td->kat2len)))
  207. failures++;
  208. uninstantiate(drbg);
  209. /*
  210. * Now test with PR: Instantiate DRBG with test entropy, nonce and
  211. * personalisation string.
  212. */
  213. if (!TEST_true(RAND_DRBG_set(drbg, td->nid, td->flags))
  214. || !TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
  215. kat_nonce, NULL)))
  216. failures++;
  217. RAND_DRBG_set_ex_data(drbg, app_data_index, &t);
  218. t.entropy = td->entropy_pr;
  219. t.entropylen = td->entropylen_pr;
  220. t.nonce = td->nonce_pr;
  221. t.noncelen = td->noncelen_pr;
  222. t.entropycnt = 0;
  223. t.noncecnt = 0;
  224. if (!TEST_true(RAND_DRBG_instantiate(drbg, td->pers_pr, td->perslen_pr)))
  225. failures++;
  226. /*
  227. * Now generate with PR: we need to supply entropy as this will
  228. * perform a reseed operation.
  229. */
  230. t.entropy = td->entropypr_pr;
  231. t.entropylen = td->entropyprlen_pr;
  232. if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->katlen_pr, 1,
  233. td->adin_pr, td->adinlen_pr))
  234. || !TEST_mem_eq(td->kat_pr, td->katlen_pr, buff, td->katlen_pr))
  235. failures++;
  236. /*
  237. * Now generate again with PR: supply new entropy again.
  238. */
  239. t.entropy = td->entropyg_pr;
  240. t.entropylen = td->entropyglen_pr;
  241. if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->kat2len_pr, 1,
  242. td->ading_pr, td->adinglen_pr))
  243. || !TEST_mem_eq(td->kat2_pr, td->kat2len_pr,
  244. buff, td->kat2len_pr))
  245. failures++;
  246. err:
  247. uninstantiate(drbg);
  248. RAND_DRBG_free(drbg);
  249. return failures == 0;
  250. }
  251. /*
  252. * Initialise a DRBG based on selftest data
  253. */
  254. static int init(RAND_DRBG *drbg, DRBG_SELFTEST_DATA *td, TEST_CTX *t)
  255. {
  256. if (!TEST_true(RAND_DRBG_set(drbg, td->nid, td->flags))
  257. || !TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
  258. kat_nonce, NULL)))
  259. return 0;
  260. RAND_DRBG_set_ex_data(drbg, app_data_index, t);
  261. t->entropy = td->entropy;
  262. t->entropylen = td->entropylen;
  263. t->nonce = td->nonce;
  264. t->noncelen = td->noncelen;
  265. t->entropycnt = 0;
  266. t->noncecnt = 0;
  267. return 1;
  268. }
  269. /*
  270. * Initialise and instantiate DRBG based on selftest data
  271. */
  272. static int instantiate(RAND_DRBG *drbg, DRBG_SELFTEST_DATA *td,
  273. TEST_CTX *t)
  274. {
  275. if (!TEST_true(init(drbg, td, t))
  276. || !TEST_true(RAND_DRBG_instantiate(drbg, td->pers, td->perslen)))
  277. return 0;
  278. return 1;
  279. }
  280. /*
  281. * Perform extensive error checking as required by SP800-90.
  282. * Induce several failure modes and check an error condition is set.
  283. */
  284. static int error_check(DRBG_SELFTEST_DATA *td)
  285. {
  286. static char zero[sizeof(RAND_DRBG)];
  287. RAND_DRBG *drbg = NULL;
  288. TEST_CTX t;
  289. unsigned char buff[1024];
  290. unsigned int reseed_counter_tmp;
  291. int ret = 0;
  292. if (!TEST_ptr(drbg = RAND_DRBG_new(td->nid, td->flags, NULL))
  293. || !TEST_true(disable_crngt(drbg)))
  294. goto err;
  295. /*
  296. * Personalisation string tests
  297. */
  298. /* Test detection of too large personlisation string */
  299. if (!init(drbg, td, &t)
  300. || RAND_DRBG_instantiate(drbg, td->pers, drbg->max_perslen + 1) > 0)
  301. goto err;
  302. /*
  303. * Entropy source tests
  304. */
  305. /* Test entropy source failure detection: i.e. returns no data */
  306. t.entropylen = 0;
  307. if (TEST_int_le(RAND_DRBG_instantiate(drbg, td->pers, td->perslen), 0))
  308. goto err;
  309. /* Try to generate output from uninstantiated DRBG */
  310. if (!TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
  311. td->adin, td->adinlen))
  312. || !uninstantiate(drbg))
  313. goto err;
  314. /* Test insufficient entropy */
  315. t.entropylen = drbg->min_entropylen - 1;
  316. if (!init(drbg, td, &t)
  317. || RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0
  318. || !uninstantiate(drbg))
  319. goto err;
  320. /* Test too much entropy */
  321. t.entropylen = drbg->max_entropylen + 1;
  322. if (!init(drbg, td, &t)
  323. || RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0
  324. || !uninstantiate(drbg))
  325. goto err;
  326. /*
  327. * Nonce tests
  328. */
  329. /* Test too small nonce */
  330. if (drbg->min_noncelen) {
  331. t.noncelen = drbg->min_noncelen - 1;
  332. if (!init(drbg, td, &t)
  333. || RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0
  334. || !uninstantiate(drbg))
  335. goto err;
  336. }
  337. /* Test too large nonce */
  338. if (drbg->max_noncelen) {
  339. t.noncelen = drbg->max_noncelen + 1;
  340. if (!init(drbg, td, &t)
  341. || RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0
  342. || !uninstantiate(drbg))
  343. goto err;
  344. }
  345. /* Instantiate with valid data, Check generation is now OK */
  346. if (!instantiate(drbg, td, &t)
  347. || !TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
  348. td->adin, td->adinlen)))
  349. goto err;
  350. /* Request too much data for one request */
  351. if (!TEST_false(RAND_DRBG_generate(drbg, buff, drbg->max_request + 1, 0,
  352. td->adin, td->adinlen)))
  353. goto err;
  354. /* Try too large additional input */
  355. if (!TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
  356. td->adin, drbg->max_adinlen + 1)))
  357. goto err;
  358. /*
  359. * Check prediction resistance request fails if entropy source
  360. * failure.
  361. */
  362. t.entropylen = 0;
  363. if (TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 1,
  364. td->adin, td->adinlen))
  365. || !uninstantiate(drbg))
  366. goto err;
  367. /* Instantiate again with valid data */
  368. if (!instantiate(drbg, td, &t))
  369. goto err;
  370. reseed_counter_tmp = drbg->reseed_gen_counter;
  371. drbg->reseed_gen_counter = drbg->reseed_interval;
  372. /* Generate output and check entropy has been requested for reseed */
  373. t.entropycnt = 0;
  374. if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
  375. td->adin, td->adinlen))
  376. || !TEST_int_eq(t.entropycnt, 1)
  377. || !TEST_int_eq(drbg->reseed_gen_counter, reseed_counter_tmp + 1)
  378. || !uninstantiate(drbg))
  379. goto err;
  380. /*
  381. * Check prediction resistance request fails if entropy source
  382. * failure.
  383. */
  384. t.entropylen = 0;
  385. if (!TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 1,
  386. td->adin, td->adinlen))
  387. || !uninstantiate(drbg))
  388. goto err;
  389. /* Test reseed counter works */
  390. if (!instantiate(drbg, td, &t))
  391. goto err;
  392. reseed_counter_tmp = drbg->reseed_gen_counter;
  393. drbg->reseed_gen_counter = drbg->reseed_interval;
  394. /* Generate output and check entropy has been requested for reseed */
  395. t.entropycnt = 0;
  396. if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
  397. td->adin, td->adinlen))
  398. || !TEST_int_eq(t.entropycnt, 1)
  399. || !TEST_int_eq(drbg->reseed_gen_counter, reseed_counter_tmp + 1)
  400. || !uninstantiate(drbg))
  401. goto err;
  402. /*
  403. * Explicit reseed tests
  404. */
  405. /* Test explicit reseed with too large additional input */
  406. if (!instantiate(drbg, td, &t)
  407. || RAND_DRBG_reseed(drbg, td->adin, drbg->max_adinlen + 1, 0) > 0)
  408. goto err;
  409. /* Test explicit reseed with entropy source failure */
  410. t.entropylen = 0;
  411. if (!TEST_int_le(RAND_DRBG_reseed(drbg, td->adin, td->adinlen, 0), 0)
  412. || !uninstantiate(drbg))
  413. goto err;
  414. /* Test explicit reseed with too much entropy */
  415. if (!instantiate(drbg, td, &t))
  416. goto err;
  417. t.entropylen = drbg->max_entropylen + 1;
  418. if (!TEST_int_le(RAND_DRBG_reseed(drbg, td->adin, td->adinlen, 0), 0)
  419. || !uninstantiate(drbg))
  420. goto err;
  421. /* Test explicit reseed with too little entropy */
  422. if (!instantiate(drbg, td, &t))
  423. goto err;
  424. t.entropylen = drbg->min_entropylen - 1;
  425. if (!TEST_int_le(RAND_DRBG_reseed(drbg, td->adin, td->adinlen, 0), 0)
  426. || !uninstantiate(drbg))
  427. goto err;
  428. /* Standard says we have to check uninstantiate really zeroes */
  429. if (!TEST_mem_eq(zero, sizeof(drbg->data), &drbg->data, sizeof(drbg->data)))
  430. goto err;
  431. ret = 1;
  432. err:
  433. uninstantiate(drbg);
  434. RAND_DRBG_free(drbg);
  435. return ret;
  436. }
  437. static int test_kats(int i)
  438. {
  439. DRBG_SELFTEST_DATA *td = &drbg_test[i];
  440. int rv = 0;
  441. if (!single_kat(td))
  442. goto err;
  443. rv = 1;
  444. err:
  445. return rv;
  446. }
  447. static int test_error_checks(int i)
  448. {
  449. DRBG_SELFTEST_DATA *td = &drbg_test[i];
  450. int rv = 0;
  451. if (error_check(td))
  452. goto err;
  453. rv = 1;
  454. err:
  455. return rv;
  456. }
  457. /*
  458. * Hook context data, attached as EXDATA to the RAND_DRBG
  459. */
  460. typedef struct hook_ctx_st {
  461. RAND_DRBG *drbg;
  462. /*
  463. * Currently, all DRBGs use the same get_entropy() callback.
  464. * The tests however, don't assume this and store
  465. * the original callback for every DRBG separately.
  466. */
  467. RAND_DRBG_get_entropy_fn get_entropy;
  468. /* forces a failure of the get_entropy() call if nonzero */
  469. int fail;
  470. /* counts successful reseeds */
  471. int reseed_count;
  472. } HOOK_CTX;
  473. static HOOK_CTX master_ctx, public_ctx, private_ctx;
  474. static HOOK_CTX *get_hook_ctx(RAND_DRBG *drbg)
  475. {
  476. return (HOOK_CTX *)RAND_DRBG_get_ex_data(drbg, app_data_index);
  477. }
  478. /* Intercepts and counts calls to the get_entropy() callback */
  479. static size_t get_entropy_hook(RAND_DRBG *drbg, unsigned char **pout,
  480. int entropy, size_t min_len, size_t max_len,
  481. int prediction_resistance)
  482. {
  483. size_t ret;
  484. HOOK_CTX *ctx = get_hook_ctx(drbg);
  485. if (ctx->fail != 0)
  486. return 0;
  487. ret = ctx->get_entropy(drbg, pout, entropy, min_len, max_len,
  488. prediction_resistance);
  489. if (ret != 0)
  490. ctx->reseed_count++;
  491. return ret;
  492. }
  493. /* Installs a hook for the get_entropy() callback of the given drbg */
  494. static void hook_drbg(RAND_DRBG *drbg, HOOK_CTX *ctx)
  495. {
  496. memset(ctx, 0, sizeof(*ctx));
  497. ctx->drbg = drbg;
  498. ctx->get_entropy = drbg->get_entropy;
  499. drbg->get_entropy = get_entropy_hook;
  500. RAND_DRBG_set_ex_data(drbg, app_data_index, ctx);
  501. }
  502. /* Installs the hook for the get_entropy() callback of the given drbg */
  503. static void unhook_drbg(RAND_DRBG *drbg)
  504. {
  505. HOOK_CTX *ctx = get_hook_ctx(drbg);
  506. drbg->get_entropy = ctx->get_entropy;
  507. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DRBG, drbg, &drbg->ex_data);
  508. }
  509. /* Resets the given hook context */
  510. static void reset_hook_ctx(HOOK_CTX *ctx)
  511. {
  512. ctx->fail = 0;
  513. ctx->reseed_count = 0;
  514. }
  515. /* Resets all drbg hook contexts */
  516. static void reset_drbg_hook_ctx(void)
  517. {
  518. reset_hook_ctx(&master_ctx);
  519. reset_hook_ctx(&public_ctx);
  520. reset_hook_ctx(&private_ctx);
  521. }
  522. /*
  523. * Generates random output using RAND_bytes() and RAND_priv_bytes()
  524. * and checks whether the three shared DRBGs were reseeded as
  525. * expected.
  526. *
  527. * |expect_success|: expected outcome (as reported by RAND_status())
  528. * |master|, |public|, |private|: pointers to the three shared DRBGs
  529. * |expect_xxx_reseed| =
  530. * 1: it is expected that the specified DRBG is reseeded
  531. * 0: it is expected that the specified DRBG is not reseeded
  532. * -1: don't check whether the specified DRBG was reseeded or not
  533. * |reseed_time|: if nonzero, used instead of time(NULL) to set the
  534. * |before_reseed| time.
  535. */
  536. static int test_drbg_reseed(int expect_success,
  537. RAND_DRBG *master,
  538. RAND_DRBG *public,
  539. RAND_DRBG *private,
  540. int expect_master_reseed,
  541. int expect_public_reseed,
  542. int expect_private_reseed,
  543. time_t reseed_time
  544. )
  545. {
  546. unsigned char buf[32];
  547. time_t before_reseed, after_reseed;
  548. int expected_state = (expect_success ? DRBG_READY : DRBG_ERROR);
  549. /*
  550. * step 1: check preconditions
  551. */
  552. /* Test whether seed propagation is enabled */
  553. if (!TEST_int_ne(master->reseed_prop_counter, 0)
  554. || !TEST_int_ne(public->reseed_prop_counter, 0)
  555. || !TEST_int_ne(private->reseed_prop_counter, 0))
  556. return 0;
  557. /* Check whether the master DRBG's reseed counter is the largest one */
  558. if (!TEST_int_le(public->reseed_prop_counter, master->reseed_prop_counter)
  559. || !TEST_int_le(private->reseed_prop_counter, master->reseed_prop_counter))
  560. return 0;
  561. /*
  562. * step 2: generate random output
  563. */
  564. if (reseed_time == 0)
  565. reseed_time = time(NULL);
  566. /* Generate random output from the public and private DRBG */
  567. before_reseed = expect_master_reseed == 1 ? reseed_time : 0;
  568. if (!TEST_int_eq(RAND_bytes(buf, sizeof(buf)), expect_success)
  569. || !TEST_int_eq(RAND_priv_bytes(buf, sizeof(buf)), expect_success))
  570. return 0;
  571. after_reseed = time(NULL);
  572. /*
  573. * step 3: check postconditions
  574. */
  575. /* Test whether reseeding succeeded as expected */
  576. if (!TEST_int_eq(master->state, expected_state)
  577. || !TEST_int_eq(public->state, expected_state)
  578. || !TEST_int_eq(private->state, expected_state))
  579. return 0;
  580. if (expect_master_reseed >= 0) {
  581. /* Test whether master DRBG was reseeded as expected */
  582. if (!TEST_int_eq(master_ctx.reseed_count, expect_master_reseed))
  583. return 0;
  584. }
  585. if (expect_public_reseed >= 0) {
  586. /* Test whether public DRBG was reseeded as expected */
  587. if (!TEST_int_eq(public_ctx.reseed_count, expect_public_reseed))
  588. return 0;
  589. }
  590. if (expect_private_reseed >= 0) {
  591. /* Test whether public DRBG was reseeded as expected */
  592. if (!TEST_int_eq(private_ctx.reseed_count, expect_private_reseed))
  593. return 0;
  594. }
  595. if (expect_success == 1) {
  596. /* Test whether all three reseed counters are synchronized */
  597. if (!TEST_int_eq(public->reseed_prop_counter, master->reseed_prop_counter)
  598. || !TEST_int_eq(private->reseed_prop_counter, master->reseed_prop_counter))
  599. return 0;
  600. /* Test whether reseed time of master DRBG is set correctly */
  601. if (!TEST_time_t_le(before_reseed, master->reseed_time)
  602. || !TEST_time_t_le(master->reseed_time, after_reseed))
  603. return 0;
  604. /* Test whether reseed times of child DRBGs are synchronized with master */
  605. if (!TEST_time_t_ge(public->reseed_time, master->reseed_time)
  606. || !TEST_time_t_ge(private->reseed_time, master->reseed_time))
  607. return 0;
  608. } else {
  609. ERR_clear_error();
  610. }
  611. return 1;
  612. }
  613. /*
  614. * Test whether the default rand_method (RAND_OpenSSL()) is
  615. * setup correctly, in particular whether reseeding works
  616. * as designed.
  617. */
  618. static int test_rand_drbg_reseed(void)
  619. {
  620. RAND_DRBG *master, *public, *private;
  621. unsigned char rand_add_buf[256];
  622. int rv=0;
  623. time_t before_reseed;
  624. /* Check whether RAND_OpenSSL() is the default method */
  625. if (!TEST_ptr_eq(RAND_get_rand_method(), RAND_OpenSSL()))
  626. return 0;
  627. /* All three DRBGs should be non-null */
  628. if (!TEST_ptr(master = RAND_DRBG_get0_master())
  629. || !TEST_ptr(public = RAND_DRBG_get0_public())
  630. || !TEST_ptr(private = RAND_DRBG_get0_private()))
  631. return 0;
  632. /* There should be three distinct DRBGs, two of them chained to master */
  633. if (!TEST_ptr_ne(public, private)
  634. || !TEST_ptr_ne(public, master)
  635. || !TEST_ptr_ne(private, master)
  636. || !TEST_ptr_eq(public->parent, master)
  637. || !TEST_ptr_eq(private->parent, master))
  638. return 0;
  639. /* Disable CRNG testing for the master DRBG */
  640. if (!TEST_true(disable_crngt(master)))
  641. return 0;
  642. /* uninstantiate the three global DRBGs */
  643. RAND_DRBG_uninstantiate(private);
  644. RAND_DRBG_uninstantiate(public);
  645. RAND_DRBG_uninstantiate(master);
  646. /* Install hooks for the following tests */
  647. hook_drbg(master, &master_ctx);
  648. hook_drbg(public, &public_ctx);
  649. hook_drbg(private, &private_ctx);
  650. /*
  651. * Test initial seeding of shared DRBGs
  652. */
  653. if (!TEST_true(test_drbg_reseed(1, master, public, private, 1, 1, 1, 0)))
  654. goto error;
  655. reset_drbg_hook_ctx();
  656. /*
  657. * Test initial state of shared DRBGs
  658. */
  659. if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 0, 0, 0)))
  660. goto error;
  661. reset_drbg_hook_ctx();
  662. /*
  663. * Test whether the public and private DRBG are both reseeded when their
  664. * reseed counters differ from the master's reseed counter.
  665. */
  666. master->reseed_prop_counter++;
  667. if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 1, 1, 0)))
  668. goto error;
  669. reset_drbg_hook_ctx();
  670. /*
  671. * Test whether the public DRBG is reseeded when its reseed counter differs
  672. * from the master's reseed counter.
  673. */
  674. master->reseed_prop_counter++;
  675. private->reseed_prop_counter++;
  676. if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 1, 0, 0)))
  677. goto error;
  678. reset_drbg_hook_ctx();
  679. /*
  680. * Test whether the private DRBG is reseeded when its reseed counter differs
  681. * from the master's reseed counter.
  682. */
  683. master->reseed_prop_counter++;
  684. public->reseed_prop_counter++;
  685. if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 0, 1, 0)))
  686. goto error;
  687. reset_drbg_hook_ctx();
  688. /* fill 'randomness' buffer with some arbitrary data */
  689. memset(rand_add_buf, 'r', sizeof(rand_add_buf));
  690. /*
  691. * Test whether all three DRBGs are reseeded by RAND_add().
  692. * The before_reseed time has to be measured here and passed into the
  693. * test_drbg_reseed() test, because the master DRBG gets already reseeded
  694. * in RAND_add(), whence the check for the condition
  695. * before_reseed <= master->reseed_time will fail if the time value happens
  696. * to increase between the RAND_add() and the test_drbg_reseed() call.
  697. */
  698. before_reseed = time(NULL);
  699. RAND_add(rand_add_buf, sizeof(rand_add_buf), sizeof(rand_add_buf));
  700. if (!TEST_true(test_drbg_reseed(1, master, public, private, 1, 1, 1,
  701. before_reseed)))
  702. goto error;
  703. reset_drbg_hook_ctx();
  704. /*
  705. * Test whether none of the DRBGs is reseed if the master fails to reseed
  706. */
  707. master_ctx.fail = 1;
  708. master->reseed_prop_counter++;
  709. RAND_add(rand_add_buf, sizeof(rand_add_buf), sizeof(rand_add_buf));
  710. if (!TEST_true(test_drbg_reseed(0, master, public, private, 0, 0, 0, 0)))
  711. goto error;
  712. reset_drbg_hook_ctx();
  713. rv = 1;
  714. error:
  715. /* Remove hooks */
  716. unhook_drbg(master);
  717. unhook_drbg(public);
  718. unhook_drbg(private);
  719. return rv;
  720. }
  721. #if defined(OPENSSL_THREADS)
  722. static int multi_thread_rand_bytes_succeeded = 1;
  723. static int multi_thread_rand_priv_bytes_succeeded = 1;
  724. static void run_multi_thread_test(void)
  725. {
  726. unsigned char buf[256];
  727. time_t start = time(NULL);
  728. RAND_DRBG *public = NULL, *private = NULL;
  729. if (!TEST_ptr(public = RAND_DRBG_get0_public())
  730. || !TEST_ptr(private = RAND_DRBG_get0_private())) {
  731. multi_thread_rand_bytes_succeeded = 0;
  732. return;
  733. }
  734. RAND_DRBG_set_reseed_time_interval(private, 1);
  735. RAND_DRBG_set_reseed_time_interval(public, 1);
  736. do {
  737. if (RAND_bytes(buf, sizeof(buf)) <= 0)
  738. multi_thread_rand_bytes_succeeded = 0;
  739. if (RAND_priv_bytes(buf, sizeof(buf)) <= 0)
  740. multi_thread_rand_priv_bytes_succeeded = 0;
  741. }
  742. while(time(NULL) - start < 5);
  743. }
  744. # if defined(OPENSSL_SYS_WINDOWS)
  745. typedef HANDLE thread_t;
  746. static DWORD WINAPI thread_run(LPVOID arg)
  747. {
  748. run_multi_thread_test();
  749. /*
  750. * Because we're linking with a static library, we must stop each
  751. * thread explicitly, or so says OPENSSL_thread_stop(3)
  752. */
  753. OPENSSL_thread_stop();
  754. return 0;
  755. }
  756. static int run_thread(thread_t *t)
  757. {
  758. *t = CreateThread(NULL, 0, thread_run, NULL, 0, NULL);
  759. return *t != NULL;
  760. }
  761. static int wait_for_thread(thread_t thread)
  762. {
  763. return WaitForSingleObject(thread, INFINITE) == 0;
  764. }
  765. # else
  766. typedef pthread_t thread_t;
  767. static void *thread_run(void *arg)
  768. {
  769. run_multi_thread_test();
  770. /*
  771. * Because we're linking with a static library, we must stop each
  772. * thread explicitly, or so says OPENSSL_thread_stop(3)
  773. */
  774. OPENSSL_thread_stop();
  775. return NULL;
  776. }
  777. static int run_thread(thread_t *t)
  778. {
  779. return pthread_create(t, NULL, thread_run, NULL) == 0;
  780. }
  781. static int wait_for_thread(thread_t thread)
  782. {
  783. return pthread_join(thread, NULL) == 0;
  784. }
  785. # endif
  786. /*
  787. * The main thread will also run the test, so we'll have THREADS+1 parallel
  788. * tests running
  789. */
  790. # define THREADS 3
  791. static int test_multi_thread(void)
  792. {
  793. thread_t t[THREADS];
  794. int i;
  795. for (i = 0; i < THREADS; i++)
  796. run_thread(&t[i]);
  797. run_multi_thread_test();
  798. for (i = 0; i < THREADS; i++)
  799. wait_for_thread(t[i]);
  800. if (!TEST_true(multi_thread_rand_bytes_succeeded))
  801. return 0;
  802. if (!TEST_true(multi_thread_rand_priv_bytes_succeeded))
  803. return 0;
  804. return 1;
  805. }
  806. #endif
  807. /*
  808. * Test that instantiation with RAND_seed() works as expected
  809. *
  810. * If no os entropy source is available then RAND_seed(buffer, bufsize)
  811. * is expected to succeed if and only if the buffer length is at least
  812. * rand_drbg_seedlen(master) bytes.
  813. *
  814. * If an os entropy source is available then RAND_seed(buffer, bufsize)
  815. * is expected to succeed always.
  816. */
  817. static int test_rand_seed(void)
  818. {
  819. RAND_DRBG *master = NULL;
  820. unsigned char rand_buf[256];
  821. size_t rand_buflen;
  822. size_t required_seed_buflen = 0;
  823. if (!TEST_ptr(master = RAND_DRBG_get0_master())
  824. || !TEST_true(disable_crngt(master)))
  825. return 0;
  826. #ifdef OPENSSL_RAND_SEED_NONE
  827. required_seed_buflen = rand_drbg_seedlen(master);
  828. #endif
  829. memset(rand_buf, 0xCD, sizeof(rand_buf));
  830. for ( rand_buflen = 256 ; rand_buflen > 0 ; --rand_buflen ) {
  831. RAND_DRBG_uninstantiate(master);
  832. RAND_seed(rand_buf, rand_buflen);
  833. if (!TEST_int_eq(RAND_status(),
  834. (rand_buflen >= required_seed_buflen)))
  835. return 0;
  836. }
  837. return 1;
  838. }
  839. /*
  840. * Test that adding additional data with RAND_add() works as expected
  841. * when the master DRBG is instantiated (and below its reseed limit).
  842. *
  843. * This should succeed regardless of whether an os entropy source is
  844. * available or not.
  845. */
  846. static int test_rand_add(void)
  847. {
  848. unsigned char rand_buf[256];
  849. size_t rand_buflen;
  850. memset(rand_buf, 0xCD, sizeof(rand_buf));
  851. /* make sure it's instantiated */
  852. RAND_seed(rand_buf, sizeof(rand_buf));
  853. if (!TEST_true(RAND_status()))
  854. return 0;
  855. for ( rand_buflen = 256 ; rand_buflen > 0 ; --rand_buflen ) {
  856. RAND_add(rand_buf, rand_buflen, 0.0);
  857. if (!TEST_true(RAND_status()))
  858. return 0;
  859. }
  860. return 1;
  861. }
  862. static int test_rand_drbg_prediction_resistance(void)
  863. {
  864. RAND_DRBG *m = NULL, *i = NULL, *s = NULL;
  865. unsigned char buf1[51], buf2[sizeof(buf1)];
  866. int ret = 0, mreseed, ireseed, sreseed;
  867. /* Initialise a three long DRBG chain */
  868. if (!TEST_ptr(m = RAND_DRBG_new(0, 0, NULL))
  869. || !TEST_true(disable_crngt(m))
  870. || !TEST_true(RAND_DRBG_instantiate(m, NULL, 0))
  871. || !TEST_ptr(i = RAND_DRBG_new(0, 0, m))
  872. || !TEST_true(RAND_DRBG_instantiate(i, NULL, 0))
  873. || !TEST_ptr(s = RAND_DRBG_new(0, 0, i))
  874. || !TEST_true(RAND_DRBG_instantiate(s, NULL, 0)))
  875. goto err;
  876. /* During a normal reseed, only the slave DRBG should be reseed */
  877. mreseed = ++m->reseed_prop_counter;
  878. ireseed = ++i->reseed_prop_counter;
  879. sreseed = s->reseed_prop_counter;
  880. if (!TEST_true(RAND_DRBG_reseed(s, NULL, 0, 0))
  881. || !TEST_int_eq(m->reseed_prop_counter, mreseed)
  882. || !TEST_int_eq(i->reseed_prop_counter, ireseed)
  883. || !TEST_int_gt(s->reseed_prop_counter, sreseed))
  884. goto err;
  885. /*
  886. * When prediction resistance is requested, the request should be
  887. * propagated to the master, so that the entire DRBG chain reseeds.
  888. */
  889. sreseed = s->reseed_prop_counter;
  890. if (!TEST_true(RAND_DRBG_reseed(s, NULL, 0, 1))
  891. || !TEST_int_gt(m->reseed_prop_counter, mreseed)
  892. || !TEST_int_gt(i->reseed_prop_counter, ireseed)
  893. || !TEST_int_gt(s->reseed_prop_counter, sreseed))
  894. goto err;
  895. /* During a normal generate, only the slave DRBG should be reseed */
  896. mreseed = ++m->reseed_prop_counter;
  897. ireseed = ++i->reseed_prop_counter;
  898. sreseed = s->reseed_prop_counter;
  899. if (!TEST_true(RAND_DRBG_generate(s, buf1, sizeof(buf1), 0, NULL, 0))
  900. || !TEST_int_eq(m->reseed_prop_counter, mreseed)
  901. || !TEST_int_eq(i->reseed_prop_counter, ireseed)
  902. || !TEST_int_gt(s->reseed_prop_counter, sreseed))
  903. goto err;
  904. /*
  905. * When a prediction resistant generate is requested, the request
  906. * should be propagated to the master, reseeding the entire DRBG chain.
  907. */
  908. sreseed = s->reseed_prop_counter;
  909. if (!TEST_true(RAND_DRBG_generate(s, buf2, sizeof(buf2), 1, NULL, 0))
  910. || !TEST_int_gt(m->reseed_prop_counter, mreseed)
  911. || !TEST_int_gt(i->reseed_prop_counter, ireseed)
  912. || !TEST_int_gt(s->reseed_prop_counter, sreseed)
  913. || !TEST_mem_ne(buf1, sizeof(buf1), buf2, sizeof(buf2)))
  914. goto err;
  915. /* Verify that a normal reseed still only reseeds the slave DRBG */
  916. mreseed = ++m->reseed_prop_counter;
  917. ireseed = ++i->reseed_prop_counter;
  918. sreseed = s->reseed_prop_counter;
  919. if (!TEST_true(RAND_DRBG_reseed(s, NULL, 0, 0))
  920. || !TEST_int_eq(m->reseed_prop_counter, mreseed)
  921. || !TEST_int_eq(i->reseed_prop_counter, ireseed)
  922. || !TEST_int_gt(s->reseed_prop_counter, sreseed))
  923. goto err;
  924. ret = 1;
  925. err:
  926. RAND_DRBG_free(s);
  927. RAND_DRBG_free(i);
  928. RAND_DRBG_free(m);
  929. return ret;
  930. }
  931. static int test_multi_set(void)
  932. {
  933. int rv = 0;
  934. RAND_DRBG *drbg = NULL;
  935. /* init drbg with default CTR initializer */
  936. if (!TEST_ptr(drbg = RAND_DRBG_new(0, 0, NULL))
  937. || !TEST_true(disable_crngt(drbg)))
  938. goto err;
  939. /* change it to use hmac */
  940. if (!TEST_true(RAND_DRBG_set(drbg, NID_sha1, RAND_DRBG_FLAG_HMAC)))
  941. goto err;
  942. /* use same type */
  943. if (!TEST_true(RAND_DRBG_set(drbg, NID_sha1, RAND_DRBG_FLAG_HMAC)))
  944. goto err;
  945. /* change it to use hash */
  946. if (!TEST_true(RAND_DRBG_set(drbg, NID_sha256, 0)))
  947. goto err;
  948. /* use same type */
  949. if (!TEST_true(RAND_DRBG_set(drbg, NID_sha256, 0)))
  950. goto err;
  951. /* change it to use ctr */
  952. if (!TEST_true(RAND_DRBG_set(drbg, NID_aes_192_ctr, 0)))
  953. goto err;
  954. /* use same type */
  955. if (!TEST_true(RAND_DRBG_set(drbg, NID_aes_192_ctr, 0)))
  956. goto err;
  957. if (!TEST_int_gt(RAND_DRBG_instantiate(drbg, NULL, 0), 0))
  958. goto err;
  959. rv = 1;
  960. err:
  961. uninstantiate(drbg);
  962. RAND_DRBG_free(drbg);
  963. return rv;
  964. }
  965. static int test_set_defaults(void)
  966. {
  967. RAND_DRBG *master = NULL, *public = NULL, *private = NULL;
  968. /* Check the default type and flags for master, public and private */
  969. return TEST_ptr(master = RAND_DRBG_get0_master())
  970. && TEST_ptr(public = RAND_DRBG_get0_public())
  971. && TEST_ptr(private = RAND_DRBG_get0_private())
  972. && TEST_int_eq(master->type, RAND_DRBG_TYPE)
  973. && TEST_int_eq(master->flags,
  974. RAND_DRBG_FLAGS | RAND_DRBG_FLAG_MASTER)
  975. && TEST_int_eq(public->type, RAND_DRBG_TYPE)
  976. && TEST_int_eq(public->flags,
  977. RAND_DRBG_FLAGS | RAND_DRBG_FLAG_PUBLIC)
  978. && TEST_int_eq(private->type, RAND_DRBG_TYPE)
  979. && TEST_int_eq(private->flags,
  980. RAND_DRBG_FLAGS | RAND_DRBG_FLAG_PRIVATE)
  981. /* change master DRBG and check again */
  982. && TEST_true(RAND_DRBG_set_defaults(NID_sha256,
  983. RAND_DRBG_FLAG_MASTER))
  984. && TEST_true(RAND_DRBG_uninstantiate(master))
  985. && TEST_int_eq(master->type, NID_sha256)
  986. && TEST_int_eq(master->flags, RAND_DRBG_FLAG_MASTER)
  987. && TEST_int_eq(public->type, RAND_DRBG_TYPE)
  988. && TEST_int_eq(public->flags,
  989. RAND_DRBG_FLAGS | RAND_DRBG_FLAG_PUBLIC)
  990. && TEST_int_eq(private->type, RAND_DRBG_TYPE)
  991. && TEST_int_eq(private->flags,
  992. RAND_DRBG_FLAGS | RAND_DRBG_FLAG_PRIVATE)
  993. /* change private DRBG and check again */
  994. && TEST_true(RAND_DRBG_set_defaults(NID_sha256,
  995. RAND_DRBG_FLAG_PRIVATE|RAND_DRBG_FLAG_HMAC))
  996. && TEST_true(RAND_DRBG_uninstantiate(private))
  997. && TEST_int_eq(master->type, NID_sha256)
  998. && TEST_int_eq(master->flags, RAND_DRBG_FLAG_MASTER)
  999. && TEST_int_eq(public->type, RAND_DRBG_TYPE)
  1000. && TEST_int_eq(public->flags,
  1001. RAND_DRBG_FLAGS | RAND_DRBG_FLAG_PUBLIC)
  1002. && TEST_int_eq(private->type, NID_sha256)
  1003. && TEST_int_eq(private->flags,
  1004. RAND_DRBG_FLAG_PRIVATE | RAND_DRBG_FLAG_HMAC)
  1005. /* change public DRBG and check again */
  1006. && TEST_true(RAND_DRBG_set_defaults(NID_sha1,
  1007. RAND_DRBG_FLAG_PUBLIC
  1008. | RAND_DRBG_FLAG_HMAC))
  1009. && TEST_true(RAND_DRBG_uninstantiate(public))
  1010. && TEST_int_eq(master->type, NID_sha256)
  1011. && TEST_int_eq(master->flags, RAND_DRBG_FLAG_MASTER)
  1012. && TEST_int_eq(public->type, NID_sha1)
  1013. && TEST_int_eq(public->flags,
  1014. RAND_DRBG_FLAG_PUBLIC | RAND_DRBG_FLAG_HMAC)
  1015. && TEST_int_eq(private->type, NID_sha256)
  1016. && TEST_int_eq(private->flags,
  1017. RAND_DRBG_FLAG_PRIVATE | RAND_DRBG_FLAG_HMAC)
  1018. /* Change DRBG defaults and change public and check again */
  1019. && TEST_true(RAND_DRBG_set_defaults(NID_sha256, 0))
  1020. && TEST_true(RAND_DRBG_uninstantiate(public))
  1021. && TEST_int_eq(public->type, NID_sha256)
  1022. && TEST_int_eq(public->flags, RAND_DRBG_FLAG_PUBLIC)
  1023. /* FIPS mode doesn't support CTR DRBG without a derivation function */
  1024. #ifndef FIPS_MODE
  1025. /* Change DRBG defaults and change master and check again */
  1026. && TEST_true(RAND_DRBG_set_defaults(NID_aes_256_ctr,
  1027. RAND_DRBG_FLAG_CTR_NO_DF))
  1028. && TEST_true(RAND_DRBG_uninstantiate(master))
  1029. && TEST_int_eq(master->type, NID_aes_256_ctr)
  1030. && TEST_int_eq(master->flags,
  1031. RAND_DRBG_FLAG_MASTER|RAND_DRBG_FLAG_CTR_NO_DF)
  1032. #endif
  1033. /* Reset back to the standard defaults */
  1034. && TEST_true(RAND_DRBG_set_defaults(RAND_DRBG_TYPE,
  1035. RAND_DRBG_FLAGS
  1036. | RAND_DRBG_FLAG_MASTER
  1037. | RAND_DRBG_FLAG_PUBLIC
  1038. | RAND_DRBG_FLAG_PRIVATE))
  1039. && TEST_true(RAND_DRBG_uninstantiate(master))
  1040. && TEST_true(RAND_DRBG_uninstantiate(public))
  1041. && TEST_true(RAND_DRBG_uninstantiate(private));
  1042. }
  1043. /*
  1044. * A list of the FIPS DRGB types.
  1045. * Because of the way HMAC DRGBs are implemented, both the NID and flags
  1046. * are required.
  1047. */
  1048. static const struct s_drgb_types {
  1049. int nid;
  1050. int flags;
  1051. } drgb_types[] = {
  1052. { NID_aes_128_ctr, 0 },
  1053. { NID_aes_192_ctr, 0 },
  1054. { NID_aes_256_ctr, 0 },
  1055. { NID_sha1, 0 },
  1056. { NID_sha224, 0 },
  1057. { NID_sha256, 0 },
  1058. { NID_sha384, 0 },
  1059. { NID_sha512, 0 },
  1060. { NID_sha512_224, 0 },
  1061. { NID_sha512_256, 0 },
  1062. { NID_sha3_224, 0 },
  1063. { NID_sha3_256, 0 },
  1064. { NID_sha3_384, 0 },
  1065. { NID_sha3_512, 0 },
  1066. { NID_sha1, RAND_DRBG_FLAG_HMAC },
  1067. { NID_sha224, RAND_DRBG_FLAG_HMAC },
  1068. { NID_sha256, RAND_DRBG_FLAG_HMAC },
  1069. { NID_sha384, RAND_DRBG_FLAG_HMAC },
  1070. { NID_sha512, RAND_DRBG_FLAG_HMAC },
  1071. { NID_sha512_224, RAND_DRBG_FLAG_HMAC },
  1072. { NID_sha512_256, RAND_DRBG_FLAG_HMAC },
  1073. { NID_sha3_224, RAND_DRBG_FLAG_HMAC },
  1074. { NID_sha3_256, RAND_DRBG_FLAG_HMAC },
  1075. { NID_sha3_384, RAND_DRBG_FLAG_HMAC },
  1076. { NID_sha3_512, RAND_DRBG_FLAG_HMAC },
  1077. };
  1078. /* Six cases for each covers seed sizes up to 32 bytes */
  1079. static const size_t crngt_num_cases = 6;
  1080. static size_t crngt_case, crngt_idx;
  1081. static int crngt_entropy_cb(unsigned char *buf, unsigned char *md,
  1082. unsigned int *md_size)
  1083. {
  1084. size_t i, z;
  1085. if (!TEST_int_lt(crngt_idx, crngt_num_cases))
  1086. return 0;
  1087. /* Generate a block of unique data unless this is the duplication point */
  1088. z = crngt_idx++;
  1089. if (z > 0 && crngt_case == z)
  1090. z--;
  1091. for (i = 0; i < CRNGT_BUFSIZ; i++)
  1092. buf[i] = (unsigned char)(i + 'A' + z);
  1093. return EVP_Digest(buf, CRNGT_BUFSIZ, md, md_size, EVP_sha256(), NULL);
  1094. }
  1095. static int test_crngt(int n)
  1096. {
  1097. const struct s_drgb_types *dt = drgb_types + n / crngt_num_cases;
  1098. RAND_DRBG *drbg = NULL;
  1099. unsigned char buff[100];
  1100. size_t ent;
  1101. int res = 0;
  1102. int expect;
  1103. if (!TEST_true(rand_crngt_single_init()))
  1104. return 0;
  1105. rand_crngt_cleanup();
  1106. if (!TEST_ptr(drbg = RAND_DRBG_new(dt->nid, dt->flags, NULL)))
  1107. return 0;
  1108. ent = (drbg->min_entropylen + CRNGT_BUFSIZ - 1) / CRNGT_BUFSIZ;
  1109. crngt_case = n % crngt_num_cases;
  1110. crngt_idx = 0;
  1111. crngt_get_entropy = &crngt_entropy_cb;
  1112. if (!TEST_true(rand_crngt_init()))
  1113. goto err;
  1114. #ifndef FIPS_MODE
  1115. if (!TEST_true(RAND_DRBG_set_callbacks(drbg, &rand_crngt_get_entropy,
  1116. &rand_crngt_cleanup_entropy,
  1117. &rand_drbg_get_nonce,
  1118. &rand_drbg_cleanup_nonce)))
  1119. goto err;
  1120. #endif
  1121. expect = crngt_case == 0 || crngt_case > ent;
  1122. if (!TEST_int_eq(RAND_DRBG_instantiate(drbg, NULL, 0), expect))
  1123. goto err;
  1124. if (!expect)
  1125. goto fin;
  1126. if (!TEST_true(RAND_DRBG_generate(drbg, buff, sizeof(buff), 0, NULL, 0)))
  1127. goto err;
  1128. expect = crngt_case == 0 || crngt_case > 2 * ent;
  1129. if (!TEST_int_eq(RAND_DRBG_reseed(drbg, NULL, 0, 0), expect))
  1130. goto err;
  1131. if (!expect)
  1132. goto fin;
  1133. if (!TEST_true(RAND_DRBG_generate(drbg, buff, sizeof(buff), 0, NULL, 0)))
  1134. goto err;
  1135. fin:
  1136. res = 1;
  1137. err:
  1138. if (!res)
  1139. TEST_note("DRBG %zd case %zd block %zd", n / crngt_num_cases,
  1140. crngt_case, crngt_idx);
  1141. uninstantiate(drbg);
  1142. RAND_DRBG_free(drbg);
  1143. crngt_get_entropy = &rand_crngt_get_entropy_cb;
  1144. return res;
  1145. }
  1146. int setup_tests(void)
  1147. {
  1148. app_data_index = RAND_DRBG_get_ex_new_index(0L, NULL, NULL, NULL, NULL);
  1149. ADD_ALL_TESTS(test_kats, OSSL_NELEM(drbg_test));
  1150. ADD_ALL_TESTS(test_error_checks, OSSL_NELEM(drbg_test));
  1151. ADD_TEST(test_rand_drbg_reseed);
  1152. ADD_TEST(test_rand_seed);
  1153. ADD_TEST(test_rand_add);
  1154. ADD_TEST(test_rand_drbg_prediction_resistance);
  1155. ADD_TEST(test_multi_set);
  1156. ADD_TEST(test_set_defaults);
  1157. #if defined(OPENSSL_THREADS)
  1158. ADD_TEST(test_multi_thread);
  1159. #endif
  1160. ADD_ALL_TESTS(test_crngt, crngt_num_cases * OSSL_NELEM(drgb_types));
  1161. return 1;
  1162. }