drbg_lib.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234
  1. /*
  2. * Copyright 2011-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <string.h>
  10. #include <openssl/crypto.h>
  11. #include <openssl/err.h>
  12. #include <openssl/rand.h>
  13. #include "rand_lcl.h"
  14. #include "internal/thread_once.h"
  15. #include "internal/rand_int.h"
  16. #include "internal/cryptlib_int.h"
  17. /*
  18. * Support framework for NIST SP 800-90A DRBG
  19. *
  20. * See manual page RAND_DRBG(7) for a general overview.
  21. *
  22. * The OpenSSL model is to have new and free functions, and that new
  23. * does all initialization. That is not the NIST model, which has
  24. * instantiation and un-instantiate, and re-use within a new/free
  25. * lifecycle. (No doubt this comes from the desire to support hardware
  26. * DRBG, where allocation of resources on something like an HSM is
  27. * a much bigger deal than just re-setting an allocated resource.)
  28. */
  29. /*
  30. * The three shared DRBG instances
  31. *
  32. * There are three shared DRBG instances: <master>, <public>, and <private>.
  33. */
  34. /*
  35. * The <master> DRBG
  36. *
  37. * Not used directly by the application, only for reseeding the two other
  38. * DRBGs. It reseeds itself by pulling either randomness from os entropy
  39. * sources or by consuming randomness which was added by RAND_add().
  40. *
  41. * The <master> DRBG is a global instance which is accessed concurrently by
  42. * all threads. The necessary locking is managed automatically by its child
  43. * DRBG instances during reseeding.
  44. */
  45. static RAND_DRBG *master_drbg;
  46. /*
  47. * The <public> DRBG
  48. *
  49. * Used by default for generating random bytes using RAND_bytes().
  50. *
  51. * The <public> DRBG is thread-local, i.e., there is one instance per thread.
  52. */
  53. static CRYPTO_THREAD_LOCAL public_drbg;
  54. /*
  55. * The <private> DRBG
  56. *
  57. * Used by default for generating private keys using RAND_priv_bytes()
  58. *
  59. * The <private> DRBG is thread-local, i.e., there is one instance per thread.
  60. */
  61. static CRYPTO_THREAD_LOCAL private_drbg;
  62. /* NIST SP 800-90A DRBG recommends the use of a personalization string. */
  63. static const char ossl_pers_string[] = "OpenSSL NIST SP 800-90A DRBG";
  64. static CRYPTO_ONCE rand_drbg_init = CRYPTO_ONCE_STATIC_INIT;
  65. #define RAND_DRBG_TYPE_FLAGS ( \
  66. RAND_DRBG_FLAG_MASTER | RAND_DRBG_FLAG_PUBLIC | RAND_DRBG_FLAG_PRIVATE )
  67. #define RAND_DRBG_TYPE_MASTER 0
  68. #define RAND_DRBG_TYPE_PUBLIC 1
  69. #define RAND_DRBG_TYPE_PRIVATE 2
  70. /* Defaults */
  71. static int rand_drbg_type[3] = {
  72. RAND_DRBG_TYPE, /* Master */
  73. RAND_DRBG_TYPE, /* Public */
  74. RAND_DRBG_TYPE /* Private */
  75. };
  76. static unsigned int rand_drbg_flags[3] = {
  77. RAND_DRBG_FLAGS | RAND_DRBG_FLAG_MASTER, /* Master */
  78. RAND_DRBG_FLAGS | RAND_DRBG_FLAG_PUBLIC, /* Public */
  79. RAND_DRBG_FLAGS | RAND_DRBG_FLAG_PRIVATE /* Private */
  80. };
  81. static unsigned int master_reseed_interval = MASTER_RESEED_INTERVAL;
  82. static unsigned int slave_reseed_interval = SLAVE_RESEED_INTERVAL;
  83. static time_t master_reseed_time_interval = MASTER_RESEED_TIME_INTERVAL;
  84. static time_t slave_reseed_time_interval = SLAVE_RESEED_TIME_INTERVAL;
  85. /* A logical OR of all used DRBG flag bits (currently there is only one) */
  86. static const unsigned int rand_drbg_used_flags =
  87. RAND_DRBG_FLAG_CTR_NO_DF | RAND_DRBG_FLAG_HMAC | RAND_DRBG_TYPE_FLAGS;
  88. static RAND_DRBG *drbg_setup(RAND_DRBG *parent, int drbg_type);
  89. static RAND_DRBG *rand_drbg_new(int secure,
  90. int type,
  91. unsigned int flags,
  92. RAND_DRBG *parent);
  93. static int is_ctr(int type)
  94. {
  95. switch (type) {
  96. case NID_aes_128_ctr:
  97. case NID_aes_192_ctr:
  98. case NID_aes_256_ctr:
  99. return 1;
  100. default:
  101. return 0;
  102. }
  103. }
  104. static int is_digest(int type)
  105. {
  106. switch (type) {
  107. case NID_sha1:
  108. case NID_sha224:
  109. case NID_sha256:
  110. case NID_sha384:
  111. case NID_sha512:
  112. case NID_sha512_224:
  113. case NID_sha512_256:
  114. case NID_sha3_224:
  115. case NID_sha3_256:
  116. case NID_sha3_384:
  117. case NID_sha3_512:
  118. return 1;
  119. default:
  120. return 0;
  121. }
  122. }
  123. /*
  124. * Set/initialize |drbg| to be of type |type|, with optional |flags|.
  125. *
  126. * If |type| and |flags| are zero, use the defaults
  127. *
  128. * Returns 1 on success, 0 on failure.
  129. */
  130. int RAND_DRBG_set(RAND_DRBG *drbg, int type, unsigned int flags)
  131. {
  132. int ret = 1;
  133. if (type == 0 && flags == 0) {
  134. type = rand_drbg_type[RAND_DRBG_TYPE_MASTER];
  135. flags = rand_drbg_flags[RAND_DRBG_TYPE_MASTER];
  136. }
  137. /* If set is called multiple times - clear the old one */
  138. if (drbg->type != 0 && (type != drbg->type || flags != drbg->flags)) {
  139. drbg->meth->uninstantiate(drbg);
  140. rand_pool_free(drbg->adin_pool);
  141. drbg->adin_pool = NULL;
  142. }
  143. drbg->state = DRBG_UNINITIALISED;
  144. drbg->flags = flags;
  145. drbg->type = type;
  146. if (type == 0) {
  147. /* Uninitialized; that's okay. */
  148. drbg->meth = NULL;
  149. return 1;
  150. } else if (is_ctr(type)) {
  151. ret = drbg_ctr_init(drbg);
  152. } else if (is_digest(type)) {
  153. if (flags & RAND_DRBG_FLAG_HMAC)
  154. ret = drbg_hmac_init(drbg);
  155. else
  156. ret = drbg_hash_init(drbg);
  157. } else {
  158. drbg->type = 0;
  159. drbg->flags = 0;
  160. drbg->meth = NULL;
  161. RANDerr(RAND_F_RAND_DRBG_SET, RAND_R_UNSUPPORTED_DRBG_TYPE);
  162. return 0;
  163. }
  164. if (ret == 0) {
  165. drbg->state = DRBG_ERROR;
  166. RANDerr(RAND_F_RAND_DRBG_SET, RAND_R_ERROR_INITIALISING_DRBG);
  167. }
  168. return ret;
  169. }
  170. /*
  171. * Set/initialize default |type| and |flag| for new drbg instances.
  172. *
  173. * Returns 1 on success, 0 on failure.
  174. */
  175. int RAND_DRBG_set_defaults(int type, unsigned int flags)
  176. {
  177. int all;
  178. if (!(is_digest(type) || is_ctr(type))) {
  179. RANDerr(RAND_F_RAND_DRBG_SET_DEFAULTS, RAND_R_UNSUPPORTED_DRBG_TYPE);
  180. return 0;
  181. }
  182. if ((flags & ~rand_drbg_used_flags) != 0) {
  183. RANDerr(RAND_F_RAND_DRBG_SET_DEFAULTS, RAND_R_UNSUPPORTED_DRBG_FLAGS);
  184. return 0;
  185. }
  186. all = ((flags & RAND_DRBG_TYPE_FLAGS) == 0);
  187. if (all || (flags & RAND_DRBG_FLAG_MASTER) != 0) {
  188. rand_drbg_type[RAND_DRBG_TYPE_MASTER] = type;
  189. rand_drbg_flags[RAND_DRBG_TYPE_MASTER] = flags | RAND_DRBG_FLAG_MASTER;
  190. }
  191. if (all || (flags & RAND_DRBG_FLAG_PUBLIC) != 0) {
  192. rand_drbg_type[RAND_DRBG_TYPE_PUBLIC] = type;
  193. rand_drbg_flags[RAND_DRBG_TYPE_PUBLIC] = flags | RAND_DRBG_FLAG_PUBLIC;
  194. }
  195. if (all || (flags & RAND_DRBG_FLAG_PRIVATE) != 0) {
  196. rand_drbg_type[RAND_DRBG_TYPE_PRIVATE] = type;
  197. rand_drbg_flags[RAND_DRBG_TYPE_PRIVATE] = flags | RAND_DRBG_FLAG_PRIVATE;
  198. }
  199. return 1;
  200. }
  201. /*
  202. * Allocate memory and initialize a new DRBG. The DRBG is allocated on
  203. * the secure heap if |secure| is nonzero and the secure heap is enabled.
  204. * The |parent|, if not NULL, will be used as random source for reseeding.
  205. *
  206. * Returns a pointer to the new DRBG instance on success, NULL on failure.
  207. */
  208. static RAND_DRBG *rand_drbg_new(int secure,
  209. int type,
  210. unsigned int flags,
  211. RAND_DRBG *parent)
  212. {
  213. RAND_DRBG *drbg = secure ?
  214. OPENSSL_secure_zalloc(sizeof(*drbg)) : OPENSSL_zalloc(sizeof(*drbg));
  215. if (drbg == NULL) {
  216. RANDerr(RAND_F_RAND_DRBG_NEW, ERR_R_MALLOC_FAILURE);
  217. return NULL;
  218. }
  219. drbg->secure = secure && CRYPTO_secure_allocated(drbg);
  220. drbg->fork_count = rand_fork_count;
  221. drbg->parent = parent;
  222. if (parent == NULL) {
  223. drbg->get_entropy = rand_drbg_get_entropy;
  224. drbg->cleanup_entropy = rand_drbg_cleanup_entropy;
  225. #ifndef RAND_DRBG_GET_RANDOM_NONCE
  226. drbg->get_nonce = rand_drbg_get_nonce;
  227. drbg->cleanup_nonce = rand_drbg_cleanup_nonce;
  228. #endif
  229. drbg->reseed_interval = master_reseed_interval;
  230. drbg->reseed_time_interval = master_reseed_time_interval;
  231. } else {
  232. drbg->get_entropy = rand_drbg_get_entropy;
  233. drbg->cleanup_entropy = rand_drbg_cleanup_entropy;
  234. /*
  235. * Do not provide nonce callbacks, the child DRBGs will
  236. * obtain their nonce using random bits from the parent.
  237. */
  238. drbg->reseed_interval = slave_reseed_interval;
  239. drbg->reseed_time_interval = slave_reseed_time_interval;
  240. }
  241. if (RAND_DRBG_set(drbg, type, flags) == 0)
  242. goto err;
  243. if (parent != NULL) {
  244. rand_drbg_lock(parent);
  245. if (drbg->strength > parent->strength) {
  246. /*
  247. * We currently don't support the algorithm from NIST SP 800-90C
  248. * 10.1.2 to use a weaker DRBG as source
  249. */
  250. rand_drbg_unlock(parent);
  251. RANDerr(RAND_F_RAND_DRBG_NEW, RAND_R_PARENT_STRENGTH_TOO_WEAK);
  252. goto err;
  253. }
  254. rand_drbg_unlock(parent);
  255. }
  256. return drbg;
  257. err:
  258. RAND_DRBG_free(drbg);
  259. return NULL;
  260. }
  261. RAND_DRBG *RAND_DRBG_new(int type, unsigned int flags, RAND_DRBG *parent)
  262. {
  263. return rand_drbg_new(0, type, flags, parent);
  264. }
  265. RAND_DRBG *RAND_DRBG_secure_new(int type, unsigned int flags, RAND_DRBG *parent)
  266. {
  267. return rand_drbg_new(1, type, flags, parent);
  268. }
  269. /*
  270. * Uninstantiate |drbg| and free all memory.
  271. */
  272. void RAND_DRBG_free(RAND_DRBG *drbg)
  273. {
  274. if (drbg == NULL)
  275. return;
  276. if (drbg->meth != NULL)
  277. drbg->meth->uninstantiate(drbg);
  278. rand_pool_free(drbg->adin_pool);
  279. CRYPTO_THREAD_lock_free(drbg->lock);
  280. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DRBG, drbg, &drbg->ex_data);
  281. if (drbg->secure)
  282. OPENSSL_secure_clear_free(drbg, sizeof(*drbg));
  283. else
  284. OPENSSL_clear_free(drbg, sizeof(*drbg));
  285. }
  286. /*
  287. * Instantiate |drbg|, after it has been initialized. Use |pers| and
  288. * |perslen| as prediction-resistance input.
  289. *
  290. * Requires that drbg->lock is already locked for write, if non-null.
  291. *
  292. * Returns 1 on success, 0 on failure.
  293. */
  294. int RAND_DRBG_instantiate(RAND_DRBG *drbg,
  295. const unsigned char *pers, size_t perslen)
  296. {
  297. unsigned char *nonce = NULL, *entropy = NULL;
  298. size_t noncelen = 0, entropylen = 0;
  299. size_t min_entropy = drbg->strength;
  300. size_t min_entropylen = drbg->min_entropylen;
  301. size_t max_entropylen = drbg->max_entropylen;
  302. if (perslen > drbg->max_perslen) {
  303. RANDerr(RAND_F_RAND_DRBG_INSTANTIATE,
  304. RAND_R_PERSONALISATION_STRING_TOO_LONG);
  305. goto end;
  306. }
  307. if (drbg->meth == NULL) {
  308. RANDerr(RAND_F_RAND_DRBG_INSTANTIATE,
  309. RAND_R_NO_DRBG_IMPLEMENTATION_SELECTED);
  310. goto end;
  311. }
  312. if (drbg->state != DRBG_UNINITIALISED) {
  313. RANDerr(RAND_F_RAND_DRBG_INSTANTIATE,
  314. drbg->state == DRBG_ERROR ? RAND_R_IN_ERROR_STATE
  315. : RAND_R_ALREADY_INSTANTIATED);
  316. goto end;
  317. }
  318. drbg->state = DRBG_ERROR;
  319. /*
  320. * NIST SP800-90Ar1 section 9.1 says you can combine getting the entropy
  321. * and nonce in 1 call by increasing the entropy with 50% and increasing
  322. * the minimum length to accomadate the length of the nonce.
  323. * We do this in case a nonce is require and get_nonce is NULL.
  324. */
  325. if (drbg->min_noncelen > 0 && drbg->get_nonce == NULL) {
  326. min_entropy += drbg->strength / 2;
  327. min_entropylen += drbg->min_noncelen;
  328. max_entropylen += drbg->max_noncelen;
  329. }
  330. drbg->reseed_next_counter = tsan_load(&drbg->reseed_prop_counter);
  331. if (drbg->reseed_next_counter) {
  332. drbg->reseed_next_counter++;
  333. if(!drbg->reseed_next_counter)
  334. drbg->reseed_next_counter = 1;
  335. }
  336. if (drbg->get_entropy != NULL)
  337. entropylen = drbg->get_entropy(drbg, &entropy, min_entropy,
  338. min_entropylen, max_entropylen, 0);
  339. if (entropylen < min_entropylen
  340. || entropylen > max_entropylen) {
  341. RANDerr(RAND_F_RAND_DRBG_INSTANTIATE, RAND_R_ERROR_RETRIEVING_ENTROPY);
  342. goto end;
  343. }
  344. if (drbg->min_noncelen > 0 && drbg->get_nonce != NULL) {
  345. noncelen = drbg->get_nonce(drbg, &nonce, drbg->strength / 2,
  346. drbg->min_noncelen, drbg->max_noncelen);
  347. if (noncelen < drbg->min_noncelen || noncelen > drbg->max_noncelen) {
  348. RANDerr(RAND_F_RAND_DRBG_INSTANTIATE, RAND_R_ERROR_RETRIEVING_NONCE);
  349. goto end;
  350. }
  351. }
  352. if (!drbg->meth->instantiate(drbg, entropy, entropylen,
  353. nonce, noncelen, pers, perslen)) {
  354. RANDerr(RAND_F_RAND_DRBG_INSTANTIATE, RAND_R_ERROR_INSTANTIATING_DRBG);
  355. goto end;
  356. }
  357. drbg->state = DRBG_READY;
  358. drbg->reseed_gen_counter = 1;
  359. drbg->reseed_time = time(NULL);
  360. tsan_store(&drbg->reseed_prop_counter, drbg->reseed_next_counter);
  361. end:
  362. if (entropy != NULL && drbg->cleanup_entropy != NULL)
  363. drbg->cleanup_entropy(drbg, entropy, entropylen);
  364. if (nonce != NULL && drbg->cleanup_nonce != NULL)
  365. drbg->cleanup_nonce(drbg, nonce, noncelen);
  366. if (drbg->state == DRBG_READY)
  367. return 1;
  368. return 0;
  369. }
  370. /*
  371. * Uninstantiate |drbg|. Must be instantiated before it can be used.
  372. *
  373. * Requires that drbg->lock is already locked for write, if non-null.
  374. *
  375. * Returns 1 on success, 0 on failure.
  376. */
  377. int RAND_DRBG_uninstantiate(RAND_DRBG *drbg)
  378. {
  379. int index = -1, type, flags;
  380. if (drbg->meth == NULL) {
  381. drbg->state = DRBG_ERROR;
  382. RANDerr(RAND_F_RAND_DRBG_UNINSTANTIATE,
  383. RAND_R_NO_DRBG_IMPLEMENTATION_SELECTED);
  384. return 0;
  385. }
  386. /* Clear the entire drbg->ctr struct, then reset some important
  387. * members of the drbg->ctr struct (e.g. keysize, df_ks) to their
  388. * initial values.
  389. */
  390. drbg->meth->uninstantiate(drbg);
  391. /* The reset uses the default values for type and flags */
  392. if (drbg->flags & RAND_DRBG_FLAG_MASTER)
  393. index = RAND_DRBG_TYPE_MASTER;
  394. else if (drbg->flags & RAND_DRBG_FLAG_PRIVATE)
  395. index = RAND_DRBG_TYPE_PRIVATE;
  396. else if (drbg->flags & RAND_DRBG_FLAG_PUBLIC)
  397. index = RAND_DRBG_TYPE_PUBLIC;
  398. if (index != -1) {
  399. flags = rand_drbg_flags[index];
  400. type = rand_drbg_type[index];
  401. } else {
  402. flags = drbg->flags;
  403. type = drbg->type;
  404. }
  405. return RAND_DRBG_set(drbg, type, flags);
  406. }
  407. /*
  408. * Reseed |drbg|, mixing in the specified data
  409. *
  410. * Requires that drbg->lock is already locked for write, if non-null.
  411. *
  412. * Returns 1 on success, 0 on failure.
  413. */
  414. int RAND_DRBG_reseed(RAND_DRBG *drbg,
  415. const unsigned char *adin, size_t adinlen,
  416. int prediction_resistance)
  417. {
  418. unsigned char *entropy = NULL;
  419. size_t entropylen = 0;
  420. if (drbg->state == DRBG_ERROR) {
  421. RANDerr(RAND_F_RAND_DRBG_RESEED, RAND_R_IN_ERROR_STATE);
  422. return 0;
  423. }
  424. if (drbg->state == DRBG_UNINITIALISED) {
  425. RANDerr(RAND_F_RAND_DRBG_RESEED, RAND_R_NOT_INSTANTIATED);
  426. return 0;
  427. }
  428. if (adin == NULL) {
  429. adinlen = 0;
  430. } else if (adinlen > drbg->max_adinlen) {
  431. RANDerr(RAND_F_RAND_DRBG_RESEED, RAND_R_ADDITIONAL_INPUT_TOO_LONG);
  432. return 0;
  433. }
  434. drbg->state = DRBG_ERROR;
  435. drbg->reseed_next_counter = tsan_load(&drbg->reseed_prop_counter);
  436. if (drbg->reseed_next_counter) {
  437. drbg->reseed_next_counter++;
  438. if(!drbg->reseed_next_counter)
  439. drbg->reseed_next_counter = 1;
  440. }
  441. if (drbg->get_entropy != NULL)
  442. entropylen = drbg->get_entropy(drbg, &entropy, drbg->strength,
  443. drbg->min_entropylen,
  444. drbg->max_entropylen,
  445. prediction_resistance);
  446. if (entropylen < drbg->min_entropylen
  447. || entropylen > drbg->max_entropylen) {
  448. RANDerr(RAND_F_RAND_DRBG_RESEED, RAND_R_ERROR_RETRIEVING_ENTROPY);
  449. goto end;
  450. }
  451. if (!drbg->meth->reseed(drbg, entropy, entropylen, adin, adinlen))
  452. goto end;
  453. drbg->state = DRBG_READY;
  454. drbg->reseed_gen_counter = 1;
  455. drbg->reseed_time = time(NULL);
  456. tsan_store(&drbg->reseed_prop_counter, drbg->reseed_next_counter);
  457. end:
  458. if (entropy != NULL && drbg->cleanup_entropy != NULL)
  459. drbg->cleanup_entropy(drbg, entropy, entropylen);
  460. if (drbg->state == DRBG_READY)
  461. return 1;
  462. return 0;
  463. }
  464. /*
  465. * Restart |drbg|, using the specified entropy or additional input
  466. *
  467. * Tries its best to get the drbg instantiated by all means,
  468. * regardless of its current state.
  469. *
  470. * Optionally, a |buffer| of |len| random bytes can be passed,
  471. * which is assumed to contain at least |entropy| bits of entropy.
  472. *
  473. * If |entropy| > 0, the buffer content is used as entropy input.
  474. *
  475. * If |entropy| == 0, the buffer content is used as additional input
  476. *
  477. * Returns 1 on success, 0 on failure.
  478. *
  479. * This function is used internally only.
  480. */
  481. int rand_drbg_restart(RAND_DRBG *drbg,
  482. const unsigned char *buffer, size_t len, size_t entropy)
  483. {
  484. int reseeded = 0;
  485. const unsigned char *adin = NULL;
  486. size_t adinlen = 0;
  487. if (drbg->pool != NULL) {
  488. RANDerr(RAND_F_RAND_DRBG_RESTART, ERR_R_INTERNAL_ERROR);
  489. drbg->state = DRBG_ERROR;
  490. rand_pool_free(drbg->pool);
  491. drbg->pool = NULL;
  492. return 0;
  493. }
  494. if (buffer != NULL) {
  495. if (entropy > 0) {
  496. if (drbg->max_entropylen < len) {
  497. RANDerr(RAND_F_RAND_DRBG_RESTART,
  498. RAND_R_ENTROPY_INPUT_TOO_LONG);
  499. drbg->state = DRBG_ERROR;
  500. return 0;
  501. }
  502. if (entropy > 8 * len) {
  503. RANDerr(RAND_F_RAND_DRBG_RESTART, RAND_R_ENTROPY_OUT_OF_RANGE);
  504. drbg->state = DRBG_ERROR;
  505. return 0;
  506. }
  507. /* will be picked up by the rand_drbg_get_entropy() callback */
  508. drbg->pool = rand_pool_attach(buffer, len, entropy);
  509. if (drbg->pool == NULL)
  510. return 0;
  511. } else {
  512. if (drbg->max_adinlen < len) {
  513. RANDerr(RAND_F_RAND_DRBG_RESTART,
  514. RAND_R_ADDITIONAL_INPUT_TOO_LONG);
  515. drbg->state = DRBG_ERROR;
  516. return 0;
  517. }
  518. adin = buffer;
  519. adinlen = len;
  520. }
  521. }
  522. /* repair error state */
  523. if (drbg->state == DRBG_ERROR)
  524. RAND_DRBG_uninstantiate(drbg);
  525. /* repair uninitialized state */
  526. if (drbg->state == DRBG_UNINITIALISED) {
  527. /* reinstantiate drbg */
  528. RAND_DRBG_instantiate(drbg,
  529. (const unsigned char *) ossl_pers_string,
  530. sizeof(ossl_pers_string) - 1);
  531. /* already reseeded. prevent second reseeding below */
  532. reseeded = (drbg->state == DRBG_READY);
  533. }
  534. /* refresh current state if entropy or additional input has been provided */
  535. if (drbg->state == DRBG_READY) {
  536. if (adin != NULL) {
  537. /*
  538. * mix in additional input without reseeding
  539. *
  540. * Similar to RAND_DRBG_reseed(), but the provided additional
  541. * data |adin| is mixed into the current state without pulling
  542. * entropy from the trusted entropy source using get_entropy().
  543. * This is not a reseeding in the strict sense of NIST SP 800-90A.
  544. */
  545. drbg->meth->reseed(drbg, adin, adinlen, NULL, 0);
  546. } else if (reseeded == 0) {
  547. /* do a full reseeding if it has not been done yet above */
  548. RAND_DRBG_reseed(drbg, NULL, 0, 0);
  549. }
  550. }
  551. rand_pool_free(drbg->pool);
  552. drbg->pool = NULL;
  553. return drbg->state == DRBG_READY;
  554. }
  555. /*
  556. * Generate |outlen| bytes into the buffer at |out|. Reseed if we need
  557. * to or if |prediction_resistance| is set. Additional input can be
  558. * sent in |adin| and |adinlen|.
  559. *
  560. * Requires that drbg->lock is already locked for write, if non-null.
  561. *
  562. * Returns 1 on success, 0 on failure.
  563. *
  564. */
  565. int RAND_DRBG_generate(RAND_DRBG *drbg, unsigned char *out, size_t outlen,
  566. int prediction_resistance,
  567. const unsigned char *adin, size_t adinlen)
  568. {
  569. int reseed_required = 0;
  570. if (drbg->state != DRBG_READY) {
  571. /* try to recover from previous errors */
  572. rand_drbg_restart(drbg, NULL, 0, 0);
  573. if (drbg->state == DRBG_ERROR) {
  574. RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_IN_ERROR_STATE);
  575. return 0;
  576. }
  577. if (drbg->state == DRBG_UNINITIALISED) {
  578. RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_NOT_INSTANTIATED);
  579. return 0;
  580. }
  581. }
  582. if (outlen > drbg->max_request) {
  583. RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_REQUEST_TOO_LARGE_FOR_DRBG);
  584. return 0;
  585. }
  586. if (adinlen > drbg->max_adinlen) {
  587. RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_ADDITIONAL_INPUT_TOO_LONG);
  588. return 0;
  589. }
  590. if (drbg->fork_count != rand_fork_count) {
  591. drbg->fork_count = rand_fork_count;
  592. reseed_required = 1;
  593. }
  594. if (drbg->reseed_interval > 0) {
  595. if (drbg->reseed_gen_counter > drbg->reseed_interval)
  596. reseed_required = 1;
  597. }
  598. if (drbg->reseed_time_interval > 0) {
  599. time_t now = time(NULL);
  600. if (now < drbg->reseed_time
  601. || now - drbg->reseed_time >= drbg->reseed_time_interval)
  602. reseed_required = 1;
  603. }
  604. if (drbg->parent != NULL) {
  605. unsigned int reseed_counter = tsan_load(&drbg->reseed_prop_counter);
  606. if (reseed_counter > 0
  607. && tsan_load(&drbg->parent->reseed_prop_counter)
  608. != reseed_counter)
  609. reseed_required = 1;
  610. }
  611. if (reseed_required || prediction_resistance) {
  612. if (!RAND_DRBG_reseed(drbg, adin, adinlen, prediction_resistance)) {
  613. RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_RESEED_ERROR);
  614. return 0;
  615. }
  616. adin = NULL;
  617. adinlen = 0;
  618. }
  619. if (!drbg->meth->generate(drbg, out, outlen, adin, adinlen)) {
  620. drbg->state = DRBG_ERROR;
  621. RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_GENERATE_ERROR);
  622. return 0;
  623. }
  624. drbg->reseed_gen_counter++;
  625. return 1;
  626. }
  627. /*
  628. * Generates |outlen| random bytes and stores them in |out|. It will
  629. * using the given |drbg| to generate the bytes.
  630. *
  631. * Requires that drbg->lock is already locked for write, if non-null.
  632. *
  633. * Returns 1 on success 0 on failure.
  634. */
  635. int RAND_DRBG_bytes(RAND_DRBG *drbg, unsigned char *out, size_t outlen)
  636. {
  637. unsigned char *additional = NULL;
  638. size_t additional_len;
  639. size_t chunk;
  640. size_t ret = 0;
  641. if (drbg->adin_pool == NULL) {
  642. if (drbg->type == 0)
  643. goto err;
  644. drbg->adin_pool = rand_pool_new(0, 0, drbg->max_adinlen);
  645. if (drbg->adin_pool == NULL)
  646. goto err;
  647. }
  648. additional_len = rand_drbg_get_additional_data(drbg->adin_pool,
  649. &additional);
  650. for ( ; outlen > 0; outlen -= chunk, out += chunk) {
  651. chunk = outlen;
  652. if (chunk > drbg->max_request)
  653. chunk = drbg->max_request;
  654. ret = RAND_DRBG_generate(drbg, out, chunk, 0, additional, additional_len);
  655. if (!ret)
  656. goto err;
  657. }
  658. ret = 1;
  659. err:
  660. if (additional != NULL)
  661. rand_drbg_cleanup_additional_data(drbg->adin_pool, additional);
  662. return ret;
  663. }
  664. /*
  665. * Set the RAND_DRBG callbacks for obtaining entropy and nonce.
  666. *
  667. * Setting the callbacks is allowed only if the drbg has not been
  668. * initialized yet. Otherwise, the operation will fail.
  669. *
  670. * Returns 1 on success, 0 on failure.
  671. */
  672. int RAND_DRBG_set_callbacks(RAND_DRBG *drbg,
  673. RAND_DRBG_get_entropy_fn get_entropy,
  674. RAND_DRBG_cleanup_entropy_fn cleanup_entropy,
  675. RAND_DRBG_get_nonce_fn get_nonce,
  676. RAND_DRBG_cleanup_nonce_fn cleanup_nonce)
  677. {
  678. if (drbg->state != DRBG_UNINITIALISED
  679. || drbg->parent != NULL)
  680. return 0;
  681. drbg->get_entropy = get_entropy;
  682. drbg->cleanup_entropy = cleanup_entropy;
  683. drbg->get_nonce = get_nonce;
  684. drbg->cleanup_nonce = cleanup_nonce;
  685. return 1;
  686. }
  687. /*
  688. * Set the reseed interval.
  689. *
  690. * The drbg will reseed automatically whenever the number of generate
  691. * requests exceeds the given reseed interval. If the reseed interval
  692. * is 0, then this feature is disabled.
  693. *
  694. * Returns 1 on success, 0 on failure.
  695. */
  696. int RAND_DRBG_set_reseed_interval(RAND_DRBG *drbg, unsigned int interval)
  697. {
  698. if (interval > MAX_RESEED_INTERVAL)
  699. return 0;
  700. drbg->reseed_interval = interval;
  701. return 1;
  702. }
  703. /*
  704. * Set the reseed time interval.
  705. *
  706. * The drbg will reseed automatically whenever the time elapsed since
  707. * the last reseeding exceeds the given reseed time interval. For safety,
  708. * a reseeding will also occur if the clock has been reset to a smaller
  709. * value.
  710. *
  711. * Returns 1 on success, 0 on failure.
  712. */
  713. int RAND_DRBG_set_reseed_time_interval(RAND_DRBG *drbg, time_t interval)
  714. {
  715. if (interval > MAX_RESEED_TIME_INTERVAL)
  716. return 0;
  717. drbg->reseed_time_interval = interval;
  718. return 1;
  719. }
  720. /*
  721. * Set the default values for reseed (time) intervals of new DRBG instances
  722. *
  723. * The default values can be set independently for master DRBG instances
  724. * (without a parent) and slave DRBG instances (with parent).
  725. *
  726. * Returns 1 on success, 0 on failure.
  727. */
  728. int RAND_DRBG_set_reseed_defaults(
  729. unsigned int _master_reseed_interval,
  730. unsigned int _slave_reseed_interval,
  731. time_t _master_reseed_time_interval,
  732. time_t _slave_reseed_time_interval
  733. )
  734. {
  735. if (_master_reseed_interval > MAX_RESEED_INTERVAL
  736. || _slave_reseed_interval > MAX_RESEED_INTERVAL)
  737. return 0;
  738. if (_master_reseed_time_interval > MAX_RESEED_TIME_INTERVAL
  739. || _slave_reseed_time_interval > MAX_RESEED_TIME_INTERVAL)
  740. return 0;
  741. master_reseed_interval = _master_reseed_interval;
  742. slave_reseed_interval = _slave_reseed_interval;
  743. master_reseed_time_interval = _master_reseed_time_interval;
  744. slave_reseed_time_interval = _slave_reseed_time_interval;
  745. return 1;
  746. }
  747. /*
  748. * Locks the given drbg. Locking a drbg which does not have locking
  749. * enabled is considered a successful no-op.
  750. *
  751. * Returns 1 on success, 0 on failure.
  752. */
  753. int rand_drbg_lock(RAND_DRBG *drbg)
  754. {
  755. if (drbg->lock != NULL)
  756. return CRYPTO_THREAD_write_lock(drbg->lock);
  757. return 1;
  758. }
  759. /*
  760. * Unlocks the given drbg. Unlocking a drbg which does not have locking
  761. * enabled is considered a successful no-op.
  762. *
  763. * Returns 1 on success, 0 on failure.
  764. */
  765. int rand_drbg_unlock(RAND_DRBG *drbg)
  766. {
  767. if (drbg->lock != NULL)
  768. return CRYPTO_THREAD_unlock(drbg->lock);
  769. return 1;
  770. }
  771. /*
  772. * Enables locking for the given drbg
  773. *
  774. * Locking can only be enabled if the random generator
  775. * is in the uninitialized state.
  776. *
  777. * Returns 1 on success, 0 on failure.
  778. */
  779. int rand_drbg_enable_locking(RAND_DRBG *drbg)
  780. {
  781. if (drbg->state != DRBG_UNINITIALISED) {
  782. RANDerr(RAND_F_RAND_DRBG_ENABLE_LOCKING,
  783. RAND_R_DRBG_ALREADY_INITIALIZED);
  784. return 0;
  785. }
  786. if (drbg->lock == NULL) {
  787. if (drbg->parent != NULL && drbg->parent->lock == NULL) {
  788. RANDerr(RAND_F_RAND_DRBG_ENABLE_LOCKING,
  789. RAND_R_PARENT_LOCKING_NOT_ENABLED);
  790. return 0;
  791. }
  792. drbg->lock = CRYPTO_THREAD_lock_new();
  793. if (drbg->lock == NULL) {
  794. RANDerr(RAND_F_RAND_DRBG_ENABLE_LOCKING,
  795. RAND_R_FAILED_TO_CREATE_LOCK);
  796. return 0;
  797. }
  798. }
  799. return 1;
  800. }
  801. /*
  802. * Get and set the EXDATA
  803. */
  804. int RAND_DRBG_set_ex_data(RAND_DRBG *drbg, int idx, void *arg)
  805. {
  806. return CRYPTO_set_ex_data(&drbg->ex_data, idx, arg);
  807. }
  808. void *RAND_DRBG_get_ex_data(const RAND_DRBG *drbg, int idx)
  809. {
  810. return CRYPTO_get_ex_data(&drbg->ex_data, idx);
  811. }
  812. /*
  813. * The following functions provide a RAND_METHOD that works on the
  814. * global DRBG. They lock.
  815. */
  816. /*
  817. * Allocates a new global DRBG on the secure heap (if enabled) and
  818. * initializes it with default settings.
  819. *
  820. * Returns a pointer to the new DRBG instance on success, NULL on failure.
  821. */
  822. static RAND_DRBG *drbg_setup(RAND_DRBG *parent, int drbg_type)
  823. {
  824. RAND_DRBG *drbg;
  825. drbg = RAND_DRBG_secure_new(rand_drbg_type[drbg_type],
  826. rand_drbg_flags[drbg_type], parent);
  827. if (drbg == NULL)
  828. return NULL;
  829. /* Only the master DRBG needs to have a lock */
  830. if (parent == NULL && rand_drbg_enable_locking(drbg) == 0)
  831. goto err;
  832. /* enable seed propagation */
  833. tsan_store(&drbg->reseed_prop_counter, 1);
  834. /*
  835. * Ignore instantiation error to support just-in-time instantiation.
  836. *
  837. * The state of the drbg will be checked in RAND_DRBG_generate() and
  838. * an automatic recovery is attempted.
  839. */
  840. (void)RAND_DRBG_instantiate(drbg,
  841. (const unsigned char *) ossl_pers_string,
  842. sizeof(ossl_pers_string) - 1);
  843. return drbg;
  844. err:
  845. RAND_DRBG_free(drbg);
  846. return NULL;
  847. }
  848. /*
  849. * Initialize the global DRBGs on first use.
  850. * Returns 1 on success, 0 on failure.
  851. */
  852. DEFINE_RUN_ONCE_STATIC(do_rand_drbg_init)
  853. {
  854. /*
  855. * ensure that libcrypto is initialized, otherwise the
  856. * DRBG locks are not cleaned up properly
  857. */
  858. if (!OPENSSL_init_crypto(0, NULL))
  859. return 0;
  860. if (!CRYPTO_THREAD_init_local(&private_drbg, NULL))
  861. return 0;
  862. if (!CRYPTO_THREAD_init_local(&public_drbg, NULL))
  863. goto err1;
  864. master_drbg = drbg_setup(NULL, RAND_DRBG_TYPE_MASTER);
  865. if (master_drbg == NULL)
  866. goto err2;
  867. return 1;
  868. err2:
  869. CRYPTO_THREAD_cleanup_local(&public_drbg);
  870. err1:
  871. CRYPTO_THREAD_cleanup_local(&private_drbg);
  872. return 0;
  873. }
  874. /* Clean up the global DRBGs before exit */
  875. void rand_drbg_cleanup_int(void)
  876. {
  877. if (master_drbg != NULL) {
  878. RAND_DRBG_free(master_drbg);
  879. master_drbg = NULL;
  880. CRYPTO_THREAD_cleanup_local(&private_drbg);
  881. CRYPTO_THREAD_cleanup_local(&public_drbg);
  882. }
  883. }
  884. void drbg_delete_thread_state(void)
  885. {
  886. RAND_DRBG *drbg;
  887. drbg = CRYPTO_THREAD_get_local(&public_drbg);
  888. CRYPTO_THREAD_set_local(&public_drbg, NULL);
  889. RAND_DRBG_free(drbg);
  890. drbg = CRYPTO_THREAD_get_local(&private_drbg);
  891. CRYPTO_THREAD_set_local(&private_drbg, NULL);
  892. RAND_DRBG_free(drbg);
  893. }
  894. /* Implements the default OpenSSL RAND_bytes() method */
  895. static int drbg_bytes(unsigned char *out, int count)
  896. {
  897. int ret;
  898. RAND_DRBG *drbg = RAND_DRBG_get0_public();
  899. if (drbg == NULL)
  900. return 0;
  901. ret = RAND_DRBG_bytes(drbg, out, count);
  902. return ret;
  903. }
  904. /*
  905. * Calculates the minimum length of a full entropy buffer
  906. * which is necessary to seed (i.e. instantiate) the DRBG
  907. * successfully.
  908. *
  909. * NOTE: There is a copy of this function in drbgtest.c.
  910. * If you change anything here, you need to update
  911. * the copy accordingly.
  912. */
  913. static size_t rand_drbg_seedlen(RAND_DRBG *drbg)
  914. {
  915. /*
  916. * If no os entropy source is available then RAND_seed(buffer, bufsize)
  917. * is expected to succeed if and only if the buffer length satisfies
  918. * the following requirements, which follow from the calculations
  919. * in RAND_DRBG_instantiate().
  920. */
  921. size_t min_entropy = drbg->strength;
  922. size_t min_entropylen = drbg->min_entropylen;
  923. /*
  924. * Extra entropy for the random nonce in the absence of a
  925. * get_nonce callback, see comment in RAND_DRBG_instantiate().
  926. */
  927. if (drbg->min_noncelen > 0 && drbg->get_nonce == NULL) {
  928. min_entropy += drbg->strength / 2;
  929. min_entropylen += drbg->min_noncelen;
  930. }
  931. /*
  932. * Convert entropy requirement from bits to bytes
  933. * (dividing by 8 without rounding upwards, because
  934. * all entropy requirements are divisible by 8).
  935. */
  936. min_entropy >>= 3;
  937. /* Return a value that satisfies both requirements */
  938. return min_entropy > min_entropylen ? min_entropy : min_entropylen;
  939. }
  940. /* Implements the default OpenSSL RAND_add() method */
  941. static int drbg_add(const void *buf, int num, double randomness)
  942. {
  943. int ret = 0;
  944. RAND_DRBG *drbg = RAND_DRBG_get0_master();
  945. size_t buflen;
  946. size_t seedlen;
  947. if (drbg == NULL)
  948. return 0;
  949. if (num < 0 || randomness < 0.0)
  950. return 0;
  951. rand_drbg_lock(drbg);
  952. seedlen = rand_drbg_seedlen(drbg);
  953. buflen = (size_t)num;
  954. if (buflen < seedlen || randomness < (double) seedlen) {
  955. #if defined(OPENSSL_RAND_SEED_NONE)
  956. /*
  957. * If no os entropy source is available, a reseeding will fail
  958. * inevitably. So we use a trick to mix the buffer contents into
  959. * the DRBG state without forcing a reseeding: we generate a
  960. * dummy random byte, using the buffer content as additional data.
  961. * Note: This won't work with RAND_DRBG_FLAG_CTR_NO_DF.
  962. */
  963. unsigned char dummy[1];
  964. ret = RAND_DRBG_generate(drbg, dummy, sizeof(dummy), 0, buf, buflen);
  965. rand_drbg_unlock(drbg);
  966. return ret;
  967. #else
  968. /*
  969. * If an os entropy source is avaible then we declare the buffer content
  970. * as additional data by setting randomness to zero and trigger a regular
  971. * reseeding.
  972. */
  973. randomness = 0.0;
  974. #endif
  975. }
  976. if (randomness > (double)seedlen) {
  977. /*
  978. * The purpose of this check is to bound |randomness| by a
  979. * relatively small value in order to prevent an integer
  980. * overflow when multiplying by 8 in the rand_drbg_restart()
  981. * call below. Note that randomness is measured in bytes,
  982. * not bits, so this value corresponds to eight times the
  983. * security strength.
  984. */
  985. randomness = (double)seedlen;
  986. }
  987. ret = rand_drbg_restart(drbg, buf, buflen, (size_t)(8 * randomness));
  988. rand_drbg_unlock(drbg);
  989. return ret;
  990. }
  991. /* Implements the default OpenSSL RAND_seed() method */
  992. static int drbg_seed(const void *buf, int num)
  993. {
  994. return drbg_add(buf, num, num);
  995. }
  996. /* Implements the default OpenSSL RAND_status() method */
  997. static int drbg_status(void)
  998. {
  999. int ret;
  1000. RAND_DRBG *drbg = RAND_DRBG_get0_master();
  1001. if (drbg == NULL)
  1002. return 0;
  1003. rand_drbg_lock(drbg);
  1004. ret = drbg->state == DRBG_READY ? 1 : 0;
  1005. rand_drbg_unlock(drbg);
  1006. return ret;
  1007. }
  1008. /*
  1009. * Get the master DRBG.
  1010. * Returns pointer to the DRBG on success, NULL on failure.
  1011. *
  1012. */
  1013. RAND_DRBG *RAND_DRBG_get0_master(void)
  1014. {
  1015. if (!RUN_ONCE(&rand_drbg_init, do_rand_drbg_init))
  1016. return NULL;
  1017. return master_drbg;
  1018. }
  1019. /*
  1020. * Get the public DRBG.
  1021. * Returns pointer to the DRBG on success, NULL on failure.
  1022. */
  1023. RAND_DRBG *RAND_DRBG_get0_public(void)
  1024. {
  1025. RAND_DRBG *drbg;
  1026. if (!RUN_ONCE(&rand_drbg_init, do_rand_drbg_init))
  1027. return NULL;
  1028. drbg = CRYPTO_THREAD_get_local(&public_drbg);
  1029. if (drbg == NULL) {
  1030. if (!ossl_init_thread_start(OPENSSL_INIT_THREAD_RAND))
  1031. return NULL;
  1032. drbg = drbg_setup(master_drbg, RAND_DRBG_TYPE_PUBLIC);
  1033. CRYPTO_THREAD_set_local(&public_drbg, drbg);
  1034. }
  1035. return drbg;
  1036. }
  1037. /*
  1038. * Get the private DRBG.
  1039. * Returns pointer to the DRBG on success, NULL on failure.
  1040. */
  1041. RAND_DRBG *RAND_DRBG_get0_private(void)
  1042. {
  1043. RAND_DRBG *drbg;
  1044. if (!RUN_ONCE(&rand_drbg_init, do_rand_drbg_init))
  1045. return NULL;
  1046. drbg = CRYPTO_THREAD_get_local(&private_drbg);
  1047. if (drbg == NULL) {
  1048. if (!ossl_init_thread_start(OPENSSL_INIT_THREAD_RAND))
  1049. return NULL;
  1050. drbg = drbg_setup(master_drbg, RAND_DRBG_TYPE_PRIVATE);
  1051. CRYPTO_THREAD_set_local(&private_drbg, drbg);
  1052. }
  1053. return drbg;
  1054. }
  1055. RAND_METHOD rand_meth = {
  1056. drbg_seed,
  1057. drbg_bytes,
  1058. NULL,
  1059. drbg_add,
  1060. drbg_bytes,
  1061. drbg_status
  1062. };
  1063. RAND_METHOD *RAND_OpenSSL(void)
  1064. {
  1065. return &rand_meth;
  1066. }