drbgtest.c 45 KB

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