drbg_lib.c 40 KB

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