drbg_lib.c 35 KB

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