drbg_lib.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062
  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. static int rand_drbg_type = RAND_DRBG_TYPE;
  66. static unsigned int rand_drbg_flags = RAND_DRBG_FLAGS;
  67. static unsigned int master_reseed_interval = MASTER_RESEED_INTERVAL;
  68. static unsigned int slave_reseed_interval = SLAVE_RESEED_INTERVAL;
  69. static time_t master_reseed_time_interval = MASTER_RESEED_TIME_INTERVAL;
  70. static time_t slave_reseed_time_interval = SLAVE_RESEED_TIME_INTERVAL;
  71. static RAND_DRBG *drbg_setup(RAND_DRBG *parent);
  72. static RAND_DRBG *rand_drbg_new(int secure,
  73. int type,
  74. unsigned int flags,
  75. RAND_DRBG *parent);
  76. /*
  77. * Set/initialize |drbg| to be of type |type|, with optional |flags|.
  78. *
  79. * If |type| and |flags| are zero, use the defaults
  80. *
  81. * Returns 1 on success, 0 on failure.
  82. */
  83. int RAND_DRBG_set(RAND_DRBG *drbg, int type, unsigned int flags)
  84. {
  85. int ret = 1;
  86. if (type == 0 && flags == 0) {
  87. type = rand_drbg_type;
  88. flags = rand_drbg_flags;
  89. }
  90. drbg->state = DRBG_UNINITIALISED;
  91. drbg->flags = flags;
  92. drbg->type = type;
  93. switch (type) {
  94. default:
  95. RANDerr(RAND_F_RAND_DRBG_SET, RAND_R_UNSUPPORTED_DRBG_TYPE);
  96. return 0;
  97. case 0:
  98. /* Uninitialized; that's okay. */
  99. return 1;
  100. case NID_aes_128_ctr:
  101. case NID_aes_192_ctr:
  102. case NID_aes_256_ctr:
  103. ret = drbg_ctr_init(drbg);
  104. break;
  105. }
  106. if (ret == 0)
  107. RANDerr(RAND_F_RAND_DRBG_SET, RAND_R_ERROR_INITIALISING_DRBG);
  108. return ret;
  109. }
  110. /*
  111. * Set/initialize default |type| and |flag| for new drbg instances.
  112. *
  113. * Returns 1 on success, 0 on failure.
  114. */
  115. int RAND_DRBG_set_defaults(int type, unsigned int flags)
  116. {
  117. int ret = 1;
  118. switch (type) {
  119. default:
  120. RANDerr(RAND_F_RAND_DRBG_SET_DEFAULTS, RAND_R_UNSUPPORTED_DRBG_TYPE);
  121. return 0;
  122. case NID_aes_128_ctr:
  123. case NID_aes_192_ctr:
  124. case NID_aes_256_ctr:
  125. break;
  126. }
  127. if ((flags & ~RAND_DRBG_USED_FLAGS) != 0) {
  128. RANDerr(RAND_F_RAND_DRBG_SET_DEFAULTS, RAND_R_UNSUPPORTED_DRBG_FLAGS);
  129. return 0;
  130. }
  131. rand_drbg_type = type;
  132. rand_drbg_flags = flags;
  133. return ret;
  134. }
  135. /*
  136. * Allocate memory and initialize a new DRBG. The DRBG is allocated on
  137. * the secure heap if |secure| is nonzero and the secure heap is enabled.
  138. * The |parent|, if not NULL, will be used as random source for reseeding.
  139. *
  140. * Returns a pointer to the new DRBG instance on success, NULL on failure.
  141. */
  142. static RAND_DRBG *rand_drbg_new(int secure,
  143. int type,
  144. unsigned int flags,
  145. RAND_DRBG *parent)
  146. {
  147. RAND_DRBG *drbg = secure ?
  148. OPENSSL_secure_zalloc(sizeof(*drbg)) : OPENSSL_zalloc(sizeof(*drbg));
  149. if (drbg == NULL) {
  150. RANDerr(RAND_F_RAND_DRBG_NEW, ERR_R_MALLOC_FAILURE);
  151. return NULL;
  152. }
  153. drbg->secure = secure && CRYPTO_secure_allocated(drbg);
  154. drbg->fork_count = rand_fork_count;
  155. drbg->parent = parent;
  156. if (parent == NULL) {
  157. drbg->get_entropy = rand_drbg_get_entropy;
  158. drbg->cleanup_entropy = rand_drbg_cleanup_entropy;
  159. #ifndef RAND_DRBG_GET_RANDOM_NONCE
  160. drbg->get_nonce = rand_drbg_get_nonce;
  161. drbg->cleanup_nonce = rand_drbg_cleanup_nonce;
  162. #endif
  163. drbg->reseed_interval = master_reseed_interval;
  164. drbg->reseed_time_interval = master_reseed_time_interval;
  165. } else {
  166. drbg->get_entropy = rand_drbg_get_entropy;
  167. drbg->cleanup_entropy = rand_drbg_cleanup_entropy;
  168. /*
  169. * Do not provide nonce callbacks, the child DRBGs will
  170. * obtain their nonce using random bits from the parent.
  171. */
  172. drbg->reseed_interval = slave_reseed_interval;
  173. drbg->reseed_time_interval = slave_reseed_time_interval;
  174. }
  175. if (RAND_DRBG_set(drbg, type, flags) == 0)
  176. goto err;
  177. if (parent != NULL) {
  178. rand_drbg_lock(parent);
  179. if (drbg->strength > parent->strength) {
  180. /*
  181. * We currently don't support the algorithm from NIST SP 800-90C
  182. * 10.1.2 to use a weaker DRBG as source
  183. */
  184. rand_drbg_unlock(parent);
  185. RANDerr(RAND_F_RAND_DRBG_NEW, RAND_R_PARENT_STRENGTH_TOO_WEAK);
  186. goto err;
  187. }
  188. rand_drbg_unlock(parent);
  189. }
  190. return drbg;
  191. err:
  192. if (drbg->secure)
  193. OPENSSL_secure_free(drbg);
  194. else
  195. OPENSSL_free(drbg);
  196. return NULL;
  197. }
  198. RAND_DRBG *RAND_DRBG_new(int type, unsigned int flags, RAND_DRBG *parent)
  199. {
  200. return rand_drbg_new(0, type, flags, parent);
  201. }
  202. RAND_DRBG *RAND_DRBG_secure_new(int type, unsigned int flags, RAND_DRBG *parent)
  203. {
  204. return rand_drbg_new(1, type, flags, parent);
  205. }
  206. /*
  207. * Uninstantiate |drbg| and free all memory.
  208. */
  209. void RAND_DRBG_free(RAND_DRBG *drbg)
  210. {
  211. if (drbg == NULL)
  212. return;
  213. if (drbg->meth != NULL)
  214. drbg->meth->uninstantiate(drbg);
  215. CRYPTO_THREAD_lock_free(drbg->lock);
  216. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DRBG, drbg, &drbg->ex_data);
  217. if (drbg->secure)
  218. OPENSSL_secure_clear_free(drbg, sizeof(*drbg));
  219. else
  220. OPENSSL_clear_free(drbg, sizeof(*drbg));
  221. }
  222. /*
  223. * Instantiate |drbg|, after it has been initialized. Use |pers| and
  224. * |perslen| as prediction-resistance input.
  225. *
  226. * Requires that drbg->lock is already locked for write, if non-null.
  227. *
  228. * Returns 1 on success, 0 on failure.
  229. */
  230. int RAND_DRBG_instantiate(RAND_DRBG *drbg,
  231. const unsigned char *pers, size_t perslen)
  232. {
  233. unsigned char *nonce = NULL, *entropy = NULL;
  234. size_t noncelen = 0, entropylen = 0;
  235. size_t min_entropy = drbg->strength;
  236. size_t min_entropylen = drbg->min_entropylen;
  237. size_t max_entropylen = drbg->max_entropylen;
  238. if (perslen > drbg->max_perslen) {
  239. RANDerr(RAND_F_RAND_DRBG_INSTANTIATE,
  240. RAND_R_PERSONALISATION_STRING_TOO_LONG);
  241. goto end;
  242. }
  243. if (drbg->meth == NULL)
  244. {
  245. RANDerr(RAND_F_RAND_DRBG_INSTANTIATE,
  246. RAND_R_NO_DRBG_IMPLEMENTATION_SELECTED);
  247. goto end;
  248. }
  249. if (drbg->state != DRBG_UNINITIALISED) {
  250. RANDerr(RAND_F_RAND_DRBG_INSTANTIATE,
  251. drbg->state == DRBG_ERROR ? RAND_R_IN_ERROR_STATE
  252. : RAND_R_ALREADY_INSTANTIATED);
  253. goto end;
  254. }
  255. drbg->state = DRBG_ERROR;
  256. /*
  257. * NIST SP800-90Ar1 section 9.1 says you can combine getting the entropy
  258. * and nonce in 1 call by increasing the entropy with 50% and increasing
  259. * the minimum length to accomadate the length of the nonce.
  260. * We do this in case a nonce is require and get_nonce is NULL.
  261. */
  262. if (drbg->min_noncelen > 0 && drbg->get_nonce == NULL) {
  263. min_entropy += drbg->strength / 2;
  264. min_entropylen += drbg->min_noncelen;
  265. max_entropylen += drbg->max_noncelen;
  266. }
  267. if (drbg->get_entropy != NULL)
  268. entropylen = drbg->get_entropy(drbg, &entropy, min_entropy,
  269. min_entropylen, max_entropylen, 0);
  270. if (entropylen < min_entropylen
  271. || entropylen > max_entropylen) {
  272. RANDerr(RAND_F_RAND_DRBG_INSTANTIATE, RAND_R_ERROR_RETRIEVING_ENTROPY);
  273. goto end;
  274. }
  275. if (drbg->min_noncelen > 0 && drbg->get_nonce != NULL) {
  276. noncelen = drbg->get_nonce(drbg, &nonce, drbg->strength / 2,
  277. drbg->min_noncelen, drbg->max_noncelen);
  278. if (noncelen < drbg->min_noncelen || noncelen > drbg->max_noncelen) {
  279. RANDerr(RAND_F_RAND_DRBG_INSTANTIATE, RAND_R_ERROR_RETRIEVING_NONCE);
  280. goto end;
  281. }
  282. }
  283. if (!drbg->meth->instantiate(drbg, entropy, entropylen,
  284. nonce, noncelen, pers, perslen)) {
  285. RANDerr(RAND_F_RAND_DRBG_INSTANTIATE, RAND_R_ERROR_INSTANTIATING_DRBG);
  286. goto end;
  287. }
  288. drbg->state = DRBG_READY;
  289. drbg->generate_counter = 0;
  290. drbg->reseed_time = time(NULL);
  291. if (drbg->reseed_counter > 0) {
  292. if (drbg->parent == NULL)
  293. drbg->reseed_counter++;
  294. else
  295. drbg->reseed_counter = drbg->parent->reseed_counter;
  296. }
  297. end:
  298. if (entropy != NULL && drbg->cleanup_entropy != NULL)
  299. drbg->cleanup_entropy(drbg, entropy, entropylen);
  300. if (nonce != NULL && drbg->cleanup_nonce!= NULL )
  301. drbg->cleanup_nonce(drbg, nonce, noncelen);
  302. if (drbg->pool != NULL) {
  303. if (drbg->state == DRBG_READY) {
  304. RANDerr(RAND_F_RAND_DRBG_INSTANTIATE,
  305. RAND_R_ERROR_ENTROPY_POOL_WAS_IGNORED);
  306. drbg->state = DRBG_ERROR;
  307. }
  308. rand_pool_free(drbg->pool);
  309. drbg->pool = NULL;
  310. }
  311. if (drbg->state == DRBG_READY)
  312. return 1;
  313. return 0;
  314. }
  315. /*
  316. * Uninstantiate |drbg|. Must be instantiated before it can be used.
  317. *
  318. * Requires that drbg->lock is already locked for write, if non-null.
  319. *
  320. * Returns 1 on success, 0 on failure.
  321. */
  322. int RAND_DRBG_uninstantiate(RAND_DRBG *drbg)
  323. {
  324. if (drbg->meth == NULL)
  325. {
  326. RANDerr(RAND_F_RAND_DRBG_UNINSTANTIATE,
  327. RAND_R_NO_DRBG_IMPLEMENTATION_SELECTED);
  328. return 0;
  329. }
  330. /* Clear the entire drbg->ctr struct, then reset some important
  331. * members of the drbg->ctr struct (e.g. keysize, df_ks) to their
  332. * initial values.
  333. */
  334. drbg->meth->uninstantiate(drbg);
  335. return RAND_DRBG_set(drbg, drbg->type, drbg->flags);
  336. }
  337. /*
  338. * Reseed |drbg|, mixing in the specified data
  339. *
  340. * Requires that drbg->lock is already locked for write, if non-null.
  341. *
  342. * Returns 1 on success, 0 on failure.
  343. */
  344. int RAND_DRBG_reseed(RAND_DRBG *drbg,
  345. const unsigned char *adin, size_t adinlen,
  346. int prediction_resistance)
  347. {
  348. unsigned char *entropy = NULL;
  349. size_t entropylen = 0;
  350. if (drbg->state == DRBG_ERROR) {
  351. RANDerr(RAND_F_RAND_DRBG_RESEED, RAND_R_IN_ERROR_STATE);
  352. return 0;
  353. }
  354. if (drbg->state == DRBG_UNINITIALISED) {
  355. RANDerr(RAND_F_RAND_DRBG_RESEED, RAND_R_NOT_INSTANTIATED);
  356. return 0;
  357. }
  358. if (adin == NULL)
  359. adinlen = 0;
  360. else if (adinlen > drbg->max_adinlen) {
  361. RANDerr(RAND_F_RAND_DRBG_RESEED, RAND_R_ADDITIONAL_INPUT_TOO_LONG);
  362. return 0;
  363. }
  364. drbg->state = DRBG_ERROR;
  365. if (drbg->get_entropy != NULL)
  366. entropylen = drbg->get_entropy(drbg, &entropy, drbg->strength,
  367. drbg->min_entropylen,
  368. drbg->max_entropylen,
  369. prediction_resistance);
  370. if (entropylen < drbg->min_entropylen
  371. || entropylen > drbg->max_entropylen) {
  372. RANDerr(RAND_F_RAND_DRBG_RESEED, RAND_R_ERROR_RETRIEVING_ENTROPY);
  373. goto end;
  374. }
  375. if (!drbg->meth->reseed(drbg, entropy, entropylen, adin, adinlen))
  376. goto end;
  377. drbg->state = DRBG_READY;
  378. drbg->generate_counter = 0;
  379. drbg->reseed_time = time(NULL);
  380. if (drbg->reseed_counter > 0) {
  381. if (drbg->parent == NULL)
  382. drbg->reseed_counter++;
  383. else
  384. drbg->reseed_counter = drbg->parent->reseed_counter;
  385. }
  386. end:
  387. if (entropy != NULL && drbg->cleanup_entropy != NULL)
  388. drbg->cleanup_entropy(drbg, entropy, entropylen);
  389. if (drbg->state == DRBG_READY)
  390. return 1;
  391. return 0;
  392. }
  393. /*
  394. * Restart |drbg|, using the specified entropy or additional input
  395. *
  396. * Tries its best to get the drbg instantiated by all means,
  397. * regardless of its current state.
  398. *
  399. * Optionally, a |buffer| of |len| random bytes can be passed,
  400. * which is assumed to contain at least |entropy| bits of entropy.
  401. *
  402. * If |entropy| > 0, the buffer content is used as entropy input.
  403. *
  404. * If |entropy| == 0, the buffer content is used as additional input
  405. *
  406. * Returns 1 on success, 0 on failure.
  407. *
  408. * This function is used internally only.
  409. */
  410. int rand_drbg_restart(RAND_DRBG *drbg,
  411. const unsigned char *buffer, size_t len, size_t entropy)
  412. {
  413. int reseeded = 0;
  414. const unsigned char *adin = NULL;
  415. size_t adinlen = 0;
  416. if (drbg->pool != NULL) {
  417. RANDerr(RAND_F_RAND_DRBG_RESTART, ERR_R_INTERNAL_ERROR);
  418. rand_pool_free(drbg->pool);
  419. drbg->pool = NULL;
  420. }
  421. if (buffer != NULL) {
  422. if (entropy > 0) {
  423. if (drbg->max_entropylen < len) {
  424. RANDerr(RAND_F_RAND_DRBG_RESTART,
  425. RAND_R_ENTROPY_INPUT_TOO_LONG);
  426. return 0;
  427. }
  428. if (entropy > 8 * len) {
  429. RANDerr(RAND_F_RAND_DRBG_RESTART, RAND_R_ENTROPY_OUT_OF_RANGE);
  430. return 0;
  431. }
  432. /* will be picked up by the rand_drbg_get_entropy() callback */
  433. drbg->pool = rand_pool_new(entropy, len, len);
  434. if (drbg->pool == NULL)
  435. return 0;
  436. rand_pool_add(drbg->pool, buffer, len, entropy);
  437. } else {
  438. if (drbg->max_adinlen < len) {
  439. RANDerr(RAND_F_RAND_DRBG_RESTART,
  440. RAND_R_ADDITIONAL_INPUT_TOO_LONG);
  441. return 0;
  442. }
  443. adin = buffer;
  444. adinlen = len;
  445. }
  446. }
  447. /* repair error state */
  448. if (drbg->state == DRBG_ERROR)
  449. RAND_DRBG_uninstantiate(drbg);
  450. /* repair uninitialized state */
  451. if (drbg->state == DRBG_UNINITIALISED) {
  452. /* reinstantiate drbg */
  453. RAND_DRBG_instantiate(drbg,
  454. (const unsigned char *) ossl_pers_string,
  455. sizeof(ossl_pers_string) - 1);
  456. /* already reseeded. prevent second reseeding below */
  457. reseeded = (drbg->state == DRBG_READY);
  458. }
  459. /* refresh current state if entropy or additional input has been provided */
  460. if (drbg->state == DRBG_READY) {
  461. if (adin != NULL) {
  462. /*
  463. * mix in additional input without reseeding
  464. *
  465. * Similar to RAND_DRBG_reseed(), but the provided additional
  466. * data |adin| is mixed into the current state without pulling
  467. * entropy from the trusted entropy source using get_entropy().
  468. * This is not a reseeding in the strict sense of NIST SP 800-90A.
  469. */
  470. drbg->meth->reseed(drbg, adin, adinlen, NULL, 0);
  471. } else if (reseeded == 0) {
  472. /* do a full reseeding if it has not been done yet above */
  473. RAND_DRBG_reseed(drbg, NULL, 0, 0);
  474. }
  475. }
  476. /* check whether a given entropy pool was cleared properly during reseed */
  477. if (drbg->pool != NULL) {
  478. drbg->state = DRBG_ERROR;
  479. RANDerr(RAND_F_RAND_DRBG_RESTART, ERR_R_INTERNAL_ERROR);
  480. rand_pool_free(drbg->pool);
  481. drbg->pool = NULL;
  482. return 0;
  483. }
  484. return drbg->state == DRBG_READY;
  485. }
  486. /*
  487. * Generate |outlen| bytes into the buffer at |out|. Reseed if we need
  488. * to or if |prediction_resistance| is set. Additional input can be
  489. * sent in |adin| and |adinlen|.
  490. *
  491. * Requires that drbg->lock is already locked for write, if non-null.
  492. *
  493. * Returns 1 on success, 0 on failure.
  494. *
  495. */
  496. int RAND_DRBG_generate(RAND_DRBG *drbg, unsigned char *out, size_t outlen,
  497. int prediction_resistance,
  498. const unsigned char *adin, size_t adinlen)
  499. {
  500. int reseed_required = 0;
  501. if (drbg->state != DRBG_READY) {
  502. /* try to recover from previous errors */
  503. rand_drbg_restart(drbg, NULL, 0, 0);
  504. if (drbg->state == DRBG_ERROR) {
  505. RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_IN_ERROR_STATE);
  506. return 0;
  507. }
  508. if (drbg->state == DRBG_UNINITIALISED) {
  509. RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_NOT_INSTANTIATED);
  510. return 0;
  511. }
  512. }
  513. if (outlen > drbg->max_request) {
  514. RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_REQUEST_TOO_LARGE_FOR_DRBG);
  515. return 0;
  516. }
  517. if (adinlen > drbg->max_adinlen) {
  518. RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_ADDITIONAL_INPUT_TOO_LONG);
  519. return 0;
  520. }
  521. if (drbg->fork_count != rand_fork_count) {
  522. drbg->fork_count = rand_fork_count;
  523. reseed_required = 1;
  524. }
  525. if (drbg->reseed_interval > 0) {
  526. if (drbg->generate_counter >= drbg->reseed_interval)
  527. reseed_required = 1;
  528. }
  529. if (drbg->reseed_time_interval > 0) {
  530. time_t now = time(NULL);
  531. if (now < drbg->reseed_time
  532. || now - drbg->reseed_time >= drbg->reseed_time_interval)
  533. reseed_required = 1;
  534. }
  535. if (drbg->reseed_counter > 0 && drbg->parent != NULL) {
  536. if (drbg->reseed_counter != drbg->parent->reseed_counter)
  537. reseed_required = 1;
  538. }
  539. if (reseed_required || prediction_resistance) {
  540. if (!RAND_DRBG_reseed(drbg, adin, adinlen, prediction_resistance)) {
  541. RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_RESEED_ERROR);
  542. return 0;
  543. }
  544. adin = NULL;
  545. adinlen = 0;
  546. }
  547. if (!drbg->meth->generate(drbg, out, outlen, adin, adinlen)) {
  548. drbg->state = DRBG_ERROR;
  549. RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_GENERATE_ERROR);
  550. return 0;
  551. }
  552. drbg->generate_counter++;
  553. return 1;
  554. }
  555. /*
  556. * Generates |outlen| random bytes and stores them in |out|. It will
  557. * using the given |drbg| to generate the bytes.
  558. *
  559. * Requires that drbg->lock is already locked for write, if non-null.
  560. *
  561. * Returns 1 on success 0 on failure.
  562. */
  563. int RAND_DRBG_bytes(RAND_DRBG *drbg, unsigned char *out, size_t outlen)
  564. {
  565. unsigned char *additional = NULL;
  566. size_t additional_len;
  567. size_t chunk;
  568. size_t ret;
  569. additional_len = rand_drbg_get_additional_data(&additional, drbg->max_adinlen);
  570. for ( ; outlen > 0; outlen -= chunk, out += chunk) {
  571. chunk = outlen;
  572. if (chunk > drbg->max_request)
  573. chunk = drbg->max_request;
  574. ret = RAND_DRBG_generate(drbg, out, chunk, 0, additional, additional_len);
  575. if (!ret)
  576. goto err;
  577. }
  578. ret = 1;
  579. err:
  580. if (additional_len != 0)
  581. OPENSSL_secure_clear_free(additional, additional_len);
  582. return ret;
  583. }
  584. /*
  585. * Set the RAND_DRBG callbacks for obtaining entropy and nonce.
  586. *
  587. * Setting the callbacks is allowed only if the drbg has not been
  588. * initialized yet. Otherwise, the operation will fail.
  589. *
  590. * Returns 1 on success, 0 on failure.
  591. */
  592. int RAND_DRBG_set_callbacks(RAND_DRBG *drbg,
  593. RAND_DRBG_get_entropy_fn get_entropy,
  594. RAND_DRBG_cleanup_entropy_fn cleanup_entropy,
  595. RAND_DRBG_get_nonce_fn get_nonce,
  596. RAND_DRBG_cleanup_nonce_fn cleanup_nonce)
  597. {
  598. if (drbg->state != DRBG_UNINITIALISED)
  599. return 0;
  600. drbg->get_entropy = get_entropy;
  601. drbg->cleanup_entropy = cleanup_entropy;
  602. drbg->get_nonce = get_nonce;
  603. drbg->cleanup_nonce = cleanup_nonce;
  604. return 1;
  605. }
  606. /*
  607. * Set the reseed interval.
  608. *
  609. * The drbg will reseed automatically whenever the number of generate
  610. * requests exceeds the given reseed interval. If the reseed interval
  611. * is 0, then this feature is disabled.
  612. *
  613. * Returns 1 on success, 0 on failure.
  614. */
  615. int RAND_DRBG_set_reseed_interval(RAND_DRBG *drbg, unsigned int interval)
  616. {
  617. if (interval > MAX_RESEED_INTERVAL)
  618. return 0;
  619. drbg->reseed_interval = interval;
  620. return 1;
  621. }
  622. /*
  623. * Set the reseed time interval.
  624. *
  625. * The drbg will reseed automatically whenever the time elapsed since
  626. * the last reseeding exceeds the given reseed time interval. For safety,
  627. * a reseeding will also occur if the clock has been reset to a smaller
  628. * value.
  629. *
  630. * Returns 1 on success, 0 on failure.
  631. */
  632. int RAND_DRBG_set_reseed_time_interval(RAND_DRBG *drbg, time_t interval)
  633. {
  634. if (interval > MAX_RESEED_TIME_INTERVAL)
  635. return 0;
  636. drbg->reseed_time_interval = interval;
  637. return 1;
  638. }
  639. /*
  640. * Set the default values for reseed (time) intervals of new DRBG instances
  641. *
  642. * The default values can be set independently for master DRBG instances
  643. * (without a parent) and slave DRBG instances (with parent).
  644. *
  645. * Returns 1 on success, 0 on failure.
  646. */
  647. int RAND_DRBG_set_reseed_defaults(
  648. unsigned int _master_reseed_interval,
  649. unsigned int _slave_reseed_interval,
  650. time_t _master_reseed_time_interval,
  651. time_t _slave_reseed_time_interval
  652. )
  653. {
  654. if (_master_reseed_interval > MAX_RESEED_INTERVAL
  655. || _slave_reseed_interval > MAX_RESEED_INTERVAL)
  656. return 0;
  657. if (_master_reseed_time_interval > MAX_RESEED_TIME_INTERVAL
  658. || _slave_reseed_time_interval > MAX_RESEED_TIME_INTERVAL)
  659. return 0;
  660. master_reseed_interval = _master_reseed_interval;
  661. slave_reseed_interval = _slave_reseed_interval;
  662. master_reseed_time_interval = _master_reseed_time_interval;
  663. slave_reseed_time_interval = _slave_reseed_time_interval;
  664. return 1;
  665. }
  666. /*
  667. * Locks the given drbg. Locking a drbg which does not have locking
  668. * enabled is considered a successful no-op.
  669. *
  670. * Returns 1 on success, 0 on failure.
  671. */
  672. int rand_drbg_lock(RAND_DRBG *drbg)
  673. {
  674. if (drbg->lock != NULL)
  675. return CRYPTO_THREAD_write_lock(drbg->lock);
  676. return 1;
  677. }
  678. /*
  679. * Unlocks the given drbg. Unlocking a drbg which does not have locking
  680. * enabled is considered a successful no-op.
  681. *
  682. * Returns 1 on success, 0 on failure.
  683. */
  684. int rand_drbg_unlock(RAND_DRBG *drbg)
  685. {
  686. if (drbg->lock != NULL)
  687. return CRYPTO_THREAD_unlock(drbg->lock);
  688. return 1;
  689. }
  690. /*
  691. * Enables locking for the given drbg
  692. *
  693. * Locking can only be enabled if the random generator
  694. * is in the uninitialized state.
  695. *
  696. * Returns 1 on success, 0 on failure.
  697. */
  698. int rand_drbg_enable_locking(RAND_DRBG *drbg)
  699. {
  700. if (drbg->state != DRBG_UNINITIALISED) {
  701. RANDerr(RAND_F_RAND_DRBG_ENABLE_LOCKING,
  702. RAND_R_DRBG_ALREADY_INITIALIZED);
  703. return 0;
  704. }
  705. if (drbg->lock == NULL) {
  706. if (drbg->parent != NULL && drbg->parent->lock == NULL) {
  707. RANDerr(RAND_F_RAND_DRBG_ENABLE_LOCKING,
  708. RAND_R_PARENT_LOCKING_NOT_ENABLED);
  709. return 0;
  710. }
  711. drbg->lock = CRYPTO_THREAD_lock_new();
  712. if (drbg->lock == NULL) {
  713. RANDerr(RAND_F_RAND_DRBG_ENABLE_LOCKING,
  714. RAND_R_FAILED_TO_CREATE_LOCK);
  715. return 0;
  716. }
  717. }
  718. return 1;
  719. }
  720. /*
  721. * Get and set the EXDATA
  722. */
  723. int RAND_DRBG_set_ex_data(RAND_DRBG *drbg, int idx, void *arg)
  724. {
  725. return CRYPTO_set_ex_data(&drbg->ex_data, idx, arg);
  726. }
  727. void *RAND_DRBG_get_ex_data(const RAND_DRBG *drbg, int idx)
  728. {
  729. return CRYPTO_get_ex_data(&drbg->ex_data, idx);
  730. }
  731. /*
  732. * The following functions provide a RAND_METHOD that works on the
  733. * global DRBG. They lock.
  734. */
  735. /*
  736. * Allocates a new global DRBG on the secure heap (if enabled) and
  737. * initializes it with default settings.
  738. *
  739. * Returns a pointer to the new DRBG instance on success, NULL on failure.
  740. */
  741. static RAND_DRBG *drbg_setup(RAND_DRBG *parent)
  742. {
  743. RAND_DRBG *drbg;
  744. drbg = RAND_DRBG_secure_new(rand_drbg_type, rand_drbg_flags, parent);
  745. if (drbg == NULL)
  746. return NULL;
  747. /* Only the master DRBG needs to have a lock */
  748. if (parent == NULL && rand_drbg_enable_locking(drbg) == 0)
  749. goto err;
  750. /* enable seed propagation */
  751. drbg->reseed_counter = 1;
  752. /*
  753. * Ignore instantiation error to support just-in-time instantiation.
  754. *
  755. * The state of the drbg will be checked in RAND_DRBG_generate() and
  756. * an automatic recovery is attempted.
  757. */
  758. (void)RAND_DRBG_instantiate(drbg,
  759. (const unsigned char *) ossl_pers_string,
  760. sizeof(ossl_pers_string) - 1);
  761. return drbg;
  762. err:
  763. RAND_DRBG_free(drbg);
  764. return NULL;
  765. }
  766. /*
  767. * Initialize the global DRBGs on first use.
  768. * Returns 1 on success, 0 on failure.
  769. */
  770. DEFINE_RUN_ONCE_STATIC(do_rand_drbg_init)
  771. {
  772. int ret = 1;
  773. /*
  774. * ensure that libcrypto is initialized, otherwise the
  775. * DRBG locks are not cleaned up properly
  776. */
  777. if (!OPENSSL_init_crypto(0, NULL))
  778. return 0;
  779. ossl_init_thread_start(OPENSSL_INIT_THREAD_RAND);
  780. master_drbg = drbg_setup(NULL);
  781. ret &= CRYPTO_THREAD_init_local(&private_drbg, NULL);
  782. ret &= CRYPTO_THREAD_init_local(&public_drbg, NULL);
  783. if (master_drbg == NULL || ret == 0)
  784. return 0;
  785. return 1;
  786. }
  787. /* Clean up the global DRBGs before exit */
  788. void rand_drbg_cleanup_int(void)
  789. {
  790. RAND_DRBG_free(master_drbg);
  791. master_drbg = NULL;
  792. CRYPTO_THREAD_cleanup_local(&private_drbg);
  793. CRYPTO_THREAD_cleanup_local(&public_drbg);
  794. }
  795. void drbg_delete_thread_state()
  796. {
  797. RAND_DRBG *drbg;
  798. drbg = CRYPTO_THREAD_get_local(&public_drbg);
  799. RAND_DRBG_free(drbg);
  800. drbg = CRYPTO_THREAD_get_local(&private_drbg);
  801. RAND_DRBG_free(drbg);
  802. }
  803. /* Implements the default OpenSSL RAND_bytes() method */
  804. static int drbg_bytes(unsigned char *out, int count)
  805. {
  806. int ret;
  807. RAND_DRBG *drbg = RAND_DRBG_get0_public();
  808. if (drbg == NULL)
  809. return 0;
  810. ret = RAND_DRBG_bytes(drbg, out, count);
  811. return ret;
  812. }
  813. /* Implements the default OpenSSL RAND_add() method */
  814. static int drbg_add(const void *buf, int num, double randomness)
  815. {
  816. int ret = 0;
  817. RAND_DRBG *drbg = RAND_DRBG_get0_master();
  818. if (drbg == NULL)
  819. return 0;
  820. if (num < 0 || randomness < 0.0)
  821. return 0;
  822. if (randomness > (double)drbg->max_entropylen) {
  823. /*
  824. * The purpose of this check is to bound |randomness| by a
  825. * relatively small value in order to prevent an integer
  826. * overflow when multiplying by 8 in the rand_drbg_restart()
  827. * call below.
  828. */
  829. return 0;
  830. }
  831. rand_drbg_lock(drbg);
  832. ret = rand_drbg_restart(drbg, buf,
  833. (size_t)(unsigned int)num,
  834. (size_t)(8*randomness));
  835. rand_drbg_unlock(drbg);
  836. return ret;
  837. }
  838. /* Implements the default OpenSSL RAND_seed() method */
  839. static int drbg_seed(const void *buf, int num)
  840. {
  841. return drbg_add(buf, num, num);
  842. }
  843. /* Implements the default OpenSSL RAND_status() method */
  844. static int drbg_status(void)
  845. {
  846. int ret;
  847. RAND_DRBG *drbg = RAND_DRBG_get0_master();
  848. if (drbg == NULL)
  849. return 0;
  850. rand_drbg_lock(drbg);
  851. ret = drbg->state == DRBG_READY ? 1 : 0;
  852. rand_drbg_unlock(drbg);
  853. return ret;
  854. }
  855. /*
  856. * Get the master DRBG.
  857. * Returns pointer to the DRBG on success, NULL on failure.
  858. *
  859. */
  860. RAND_DRBG *RAND_DRBG_get0_master(void)
  861. {
  862. if (!RUN_ONCE(&rand_drbg_init, do_rand_drbg_init))
  863. return NULL;
  864. return master_drbg;
  865. }
  866. /*
  867. * Get the public DRBG.
  868. * Returns pointer to the DRBG on success, NULL on failure.
  869. */
  870. RAND_DRBG *RAND_DRBG_get0_public(void)
  871. {
  872. RAND_DRBG *drbg;
  873. if (!RUN_ONCE(&rand_drbg_init, do_rand_drbg_init))
  874. return NULL;
  875. drbg = CRYPTO_THREAD_get_local(&public_drbg);
  876. if (drbg == NULL) {
  877. ossl_init_thread_start(OPENSSL_INIT_THREAD_RAND);
  878. drbg = drbg_setup(master_drbg);
  879. CRYPTO_THREAD_set_local(&public_drbg, drbg);
  880. }
  881. return drbg;
  882. }
  883. /*
  884. * Get the private DRBG.
  885. * Returns pointer to the DRBG on success, NULL on failure.
  886. */
  887. RAND_DRBG *RAND_DRBG_get0_private(void)
  888. {
  889. RAND_DRBG *drbg;
  890. if (!RUN_ONCE(&rand_drbg_init, do_rand_drbg_init))
  891. return NULL;
  892. drbg = CRYPTO_THREAD_get_local(&private_drbg);
  893. if (drbg == NULL) {
  894. ossl_init_thread_start(OPENSSL_INIT_THREAD_RAND);
  895. drbg = drbg_setup(master_drbg);
  896. CRYPTO_THREAD_set_local(&private_drbg, drbg);
  897. }
  898. return drbg;
  899. }
  900. RAND_METHOD rand_meth = {
  901. drbg_seed,
  902. drbg_bytes,
  903. NULL,
  904. drbg_add,
  905. drbg_bytes,
  906. drbg_status
  907. };
  908. RAND_METHOD *RAND_OpenSSL(void)
  909. {
  910. return &rand_meth;
  911. }