drbg.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  1. /*
  2. * Copyright 2011-2020 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 <openssl/evp.h>
  14. #include "crypto/rand.h"
  15. #include "drbg_local.h"
  16. #include "internal/thread_once.h"
  17. #include "crypto/cryptlib.h"
  18. #include "prov/seeding.h"
  19. #include "prov/rand_pool.h"
  20. #include "prov/provider_ctx.h"
  21. #include "prov/providercommonerr.h"
  22. /*
  23. * Support framework for NIST SP 800-90A DRBG
  24. *
  25. * See manual page PROV_DRBG(7) for a general overview.
  26. *
  27. * The OpenSSL model is to have new and free functions, and that new
  28. * does all initialization. That is not the NIST model, which has
  29. * instantiation and un-instantiate, and re-use within a new/free
  30. * lifecycle. (No doubt this comes from the desire to support hardware
  31. * DRBG, where allocation of resources on something like an HSM is
  32. * a much bigger deal than just re-setting an allocated resource.)
  33. */
  34. /* NIST SP 800-90A DRBG recommends the use of a personalization string. */
  35. static const char ossl_pers_string[] = DRBG_DEFAULT_PERS_STRING;
  36. static const OSSL_DISPATCH *find_call(const OSSL_DISPATCH *dispatch,
  37. int function);
  38. static int rand_drbg_restart(PROV_DRBG *drbg);
  39. int drbg_lock(void *vctx)
  40. {
  41. PROV_DRBG *drbg = vctx;
  42. if (drbg == NULL || drbg->lock == NULL)
  43. return 1;
  44. return CRYPTO_THREAD_write_lock(drbg->lock);
  45. }
  46. void drbg_unlock(void *vctx)
  47. {
  48. PROV_DRBG *drbg = vctx;
  49. if (drbg != NULL && drbg->lock != NULL)
  50. CRYPTO_THREAD_unlock(drbg->lock);
  51. }
  52. static int drbg_lock_parent(PROV_DRBG *drbg)
  53. {
  54. void *parent = drbg->parent;
  55. if (parent != NULL
  56. && drbg->parent_lock != NULL
  57. && !drbg->parent_lock(parent)) {
  58. ERR_raise(ERR_LIB_PROV, PROV_R_PARENT_LOCKING_NOT_ENABLED);
  59. return 0;
  60. }
  61. return 1;
  62. }
  63. static void drbg_unlock_parent(PROV_DRBG *drbg)
  64. {
  65. void *parent = drbg->parent;
  66. if (parent != NULL && drbg->parent_unlock != NULL)
  67. drbg->parent_unlock(parent);
  68. }
  69. static int get_parent_strength(PROV_DRBG *drbg, unsigned int *str)
  70. {
  71. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  72. void *parent = drbg->parent;
  73. int res;
  74. if (drbg->parent_get_ctx_params == NULL) {
  75. ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_GET_PARENT_STRENGTH);
  76. return 0;
  77. }
  78. *params = OSSL_PARAM_construct_uint(OSSL_RAND_PARAM_STRENGTH, str);
  79. if (!drbg_lock_parent(drbg)) {
  80. ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_LOCK_PARENT);
  81. return 0;
  82. }
  83. res = drbg->parent_get_ctx_params(parent, params);
  84. drbg_unlock_parent(drbg);
  85. if (!res) {
  86. ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_GET_PARENT_STRENGTH);
  87. return 0;
  88. }
  89. return 1;
  90. }
  91. static unsigned int get_parent_reseed_count(PROV_DRBG *drbg)
  92. {
  93. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  94. void *parent = drbg->parent;
  95. unsigned int r;
  96. *params = OSSL_PARAM_construct_uint(OSSL_DRBG_PARAM_RESEED_CTR, &r);
  97. if (!drbg_lock_parent(drbg)) {
  98. ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_LOCK_PARENT);
  99. goto err;
  100. }
  101. if (!drbg->parent_get_ctx_params(parent, params)) {
  102. drbg_unlock_parent(drbg);
  103. ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_GET_RESEED_PROP_CTR);
  104. goto err;
  105. }
  106. drbg_unlock_parent(drbg);
  107. return r;
  108. err:
  109. r = tsan_load(&drbg->reseed_counter) - 2;
  110. if (r == 0)
  111. r = UINT_MAX;
  112. return r;
  113. }
  114. /*
  115. * Implements the get_entropy() callback (see RAND_DRBG_set_callbacks())
  116. *
  117. * If the DRBG has a parent, then the required amount of entropy input
  118. * is fetched using the parent's RAND_DRBG_generate().
  119. *
  120. * Otherwise, the entropy is polled from the system entropy sources
  121. * using prov_pool_acquire_entropy().
  122. *
  123. * If a random pool has been added to the DRBG using RAND_add(), then
  124. * its entropy will be used up first.
  125. */
  126. static size_t prov_drbg_get_entropy(PROV_DRBG *drbg, unsigned char **pout,
  127. int entropy, size_t min_len,
  128. size_t max_len, int prediction_resistance)
  129. {
  130. size_t ret = 0;
  131. size_t entropy_available = 0;
  132. RAND_POOL *pool;
  133. unsigned int p_str;
  134. if (drbg->parent != NULL) {
  135. if (!get_parent_strength(drbg, &p_str))
  136. return 0;
  137. if (drbg->strength > p_str) {
  138. /*
  139. * We currently don't support the algorithm from NIST SP 800-90C
  140. * 10.1.2 to use a weaker DRBG as source
  141. */
  142. RANDerr(0, PROV_R_PARENT_STRENGTH_TOO_WEAK);
  143. return 0;
  144. }
  145. }
  146. if (drbg->seed_pool != NULL) {
  147. pool = drbg->seed_pool;
  148. pool->entropy_requested = entropy;
  149. } else {
  150. pool = rand_pool_new(entropy, 1, min_len, max_len);
  151. if (pool == NULL) {
  152. ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
  153. return 0;
  154. }
  155. }
  156. if (drbg->parent != NULL) {
  157. size_t bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
  158. unsigned char *buffer = rand_pool_add_begin(pool, bytes_needed);
  159. if (buffer != NULL) {
  160. size_t bytes = 0;
  161. /*
  162. * Get random data from parent. Include our address as additional input,
  163. * in order to provide some additional distinction between different
  164. * DRBG child instances.
  165. * Our lock is already held, but we need to lock our parent before
  166. * generating bits from it. (Note: taking the lock will be a no-op
  167. * if locking if drbg->parent->lock == NULL.)
  168. */
  169. if (drbg->parent_generate == NULL)
  170. goto err;
  171. drbg_lock_parent(drbg);
  172. if (drbg->parent_generate(drbg->parent, buffer, bytes_needed,
  173. drbg->strength, prediction_resistance,
  174. (unsigned char *)&drbg,
  175. sizeof(drbg)) != 0)
  176. bytes = bytes_needed;
  177. drbg_unlock_parent(drbg);
  178. drbg->parent_reseed_counter = get_parent_reseed_count(drbg);
  179. rand_pool_add_end(pool, bytes, 8 * bytes);
  180. entropy_available = rand_pool_entropy_available(pool);
  181. }
  182. } else {
  183. /* Get entropy by polling system entropy sources. */
  184. entropy_available = prov_pool_acquire_entropy(pool);
  185. }
  186. if (entropy_available > 0) {
  187. ret = rand_pool_length(pool);
  188. *pout = rand_pool_detach(pool);
  189. }
  190. err:
  191. if (drbg->seed_pool == NULL)
  192. rand_pool_free(pool);
  193. return ret;
  194. }
  195. /*
  196. * Implements the cleanup_entropy() callback (see RAND_DRBG_set_callbacks())
  197. *
  198. */
  199. static void prov_drbg_cleanup_entropy(PROV_DRBG *drbg,
  200. unsigned char *out, size_t outlen)
  201. {
  202. if (drbg->seed_pool == NULL) {
  203. OPENSSL_secure_clear_free(out, outlen);
  204. }
  205. }
  206. static size_t get_entropy(PROV_DRBG *drbg, unsigned char **pout, int entropy,
  207. size_t min_len, size_t max_len,
  208. int prediction_resistance)
  209. {
  210. #ifdef FIPS_MODULE
  211. if (drbg->parent == NULL)
  212. return prov_crngt_get_entropy(drbg, pout, entropy, min_len, max_len,
  213. prediction_resistance);
  214. #endif
  215. return prov_drbg_get_entropy(drbg, pout, entropy, min_len, max_len,
  216. prediction_resistance);
  217. }
  218. static void cleanup_entropy(PROV_DRBG *drbg, unsigned char *out, size_t outlen)
  219. {
  220. #ifdef FIPS_MODULE
  221. if (drbg->parent == NULL)
  222. prov_crngt_cleanup_entropy(drbg, out, outlen);
  223. else
  224. #endif
  225. prov_drbg_cleanup_entropy(drbg, out, outlen);
  226. }
  227. #ifndef PROV_RAND_GET_RANDOM_NONCE
  228. typedef struct prov_drbg_nonce_global_st {
  229. CRYPTO_RWLOCK *rand_nonce_lock;
  230. int rand_nonce_count;
  231. } PROV_DRBG_NONCE_GLOBAL;
  232. /*
  233. * drbg_ossl_ctx_new() calls drgb_setup() which calls rand_drbg_get_nonce()
  234. * which needs to get the rand_nonce_lock out of the OPENSSL_CTX...but since
  235. * drbg_ossl_ctx_new() hasn't finished running yet we need the rand_nonce_lock
  236. * to be in a different global data object. Otherwise we will go into an
  237. * infinite recursion loop.
  238. */
  239. static void *prov_drbg_nonce_ossl_ctx_new(OPENSSL_CTX *libctx)
  240. {
  241. PROV_DRBG_NONCE_GLOBAL *dngbl = OPENSSL_zalloc(sizeof(*dngbl));
  242. if (dngbl == NULL)
  243. return NULL;
  244. dngbl->rand_nonce_lock = CRYPTO_THREAD_lock_new();
  245. if (dngbl->rand_nonce_lock == NULL) {
  246. OPENSSL_free(dngbl);
  247. return NULL;
  248. }
  249. return dngbl;
  250. }
  251. static void prov_drbg_nonce_ossl_ctx_free(void *vdngbl)
  252. {
  253. PROV_DRBG_NONCE_GLOBAL *dngbl = vdngbl;
  254. if (dngbl == NULL)
  255. return;
  256. CRYPTO_THREAD_lock_free(dngbl->rand_nonce_lock);
  257. OPENSSL_free(dngbl);
  258. }
  259. static const OPENSSL_CTX_METHOD drbg_nonce_ossl_ctx_method = {
  260. prov_drbg_nonce_ossl_ctx_new,
  261. prov_drbg_nonce_ossl_ctx_free,
  262. };
  263. /* Get a nonce from the operating system */
  264. static size_t prov_drbg_get_nonce(PROV_DRBG *drbg,
  265. unsigned char **pout,
  266. int entropy, size_t min_len, size_t max_len)
  267. {
  268. size_t ret = 0, n;
  269. RAND_POOL *pool;
  270. unsigned char *buf = NULL;
  271. OPENSSL_CTX *libctx = PROV_LIBRARY_CONTEXT_OF(drbg->provctx);
  272. PROV_DRBG_NONCE_GLOBAL *dngbl
  273. = openssl_ctx_get_data(libctx, OPENSSL_CTX_DRBG_NONCE_INDEX,
  274. &drbg_nonce_ossl_ctx_method);
  275. struct {
  276. void *instance;
  277. int count;
  278. } data;
  279. if (dngbl == NULL)
  280. return 0;
  281. if (drbg->parent != NULL) {
  282. if (drbg->parent_nonce != NULL) {
  283. n = drbg->parent_nonce(drbg->parent, NULL, 0, drbg->min_noncelen,
  284. drbg->max_noncelen);
  285. if (n > 0 && (buf = OPENSSL_malloc(n)) != NULL) {
  286. ret = drbg->parent_nonce(drbg->parent, buf, 0,
  287. drbg->min_noncelen,
  288. drbg->max_noncelen);
  289. if (ret == n) {
  290. *pout = buf;
  291. return ret;
  292. }
  293. OPENSSL_free(buf);
  294. }
  295. }
  296. }
  297. /* Use the built in nonce source */
  298. memset(&data, 0, sizeof(data));
  299. pool = rand_pool_new(0, 0, min_len, max_len);
  300. if (pool == NULL)
  301. return 0;
  302. if (prov_pool_add_nonce_data(pool) == 0)
  303. goto err;
  304. data.instance = drbg;
  305. CRYPTO_atomic_add(&dngbl->rand_nonce_count, 1, &data.count,
  306. dngbl->rand_nonce_lock);
  307. if (rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0) == 0)
  308. goto err;
  309. ret = rand_pool_length(pool);
  310. *pout = rand_pool_detach(pool);
  311. err:
  312. rand_pool_free(pool);
  313. return ret;
  314. }
  315. static void prov_drbg_clear_nonce(PROV_DRBG *drbg, unsigned char *nonce,
  316. size_t noncelen)
  317. {
  318. OPENSSL_clear_free(nonce, noncelen);
  319. }
  320. #else
  321. # define prov_drbg_clear_nonce(drbg, nonce, len) \
  322. OPENSSL_clear_free((nonce), (len))
  323. #endif /* PROV_RAND_GET_RANDOM_NONCE */
  324. /*
  325. * Instantiate |drbg|, after it has been initialized. Use |pers| and
  326. * |perslen| as prediction-resistance input.
  327. *
  328. * Requires that drbg->lock is already locked for write, if non-null.
  329. *
  330. * Returns 1 on success, 0 on failure.
  331. */
  332. int PROV_DRBG_instantiate(PROV_DRBG *drbg, unsigned int strength,
  333. int prediction_resistance,
  334. const unsigned char *pers, size_t perslen)
  335. {
  336. unsigned char *nonce = NULL, *entropy = NULL;
  337. size_t noncelen = 0, entropylen = 0;
  338. size_t min_entropy, min_entropylen, max_entropylen;
  339. if (strength > drbg->strength) {
  340. PROVerr(0, PROV_R_INSUFFICIENT_DRBG_STRENGTH);
  341. goto end;
  342. }
  343. min_entropy = drbg->strength;
  344. min_entropylen = drbg->min_entropylen;
  345. max_entropylen = drbg->max_entropylen;
  346. if (pers == NULL) {
  347. pers = (const unsigned char *)ossl_pers_string;
  348. perslen = sizeof(ossl_pers_string);
  349. }
  350. if (perslen > drbg->max_perslen) {
  351. PROVerr(0, PROV_R_PERSONALISATION_STRING_TOO_LONG);
  352. goto end;
  353. }
  354. if (drbg->state != EVP_RAND_STATE_UNINITIALISED) {
  355. if (drbg->state == EVP_RAND_STATE_ERROR)
  356. PROVerr(0, PROV_R_IN_ERROR_STATE);
  357. else
  358. PROVerr(0, PROV_R_ALREADY_INSTANTIATED);
  359. goto end;
  360. }
  361. drbg->state = EVP_RAND_STATE_ERROR;
  362. if (drbg->min_noncelen > 0) {
  363. if (drbg->parent_nonce != NULL) {
  364. noncelen = drbg->parent_nonce(drbg->parent, NULL, drbg->strength,
  365. drbg->min_noncelen,
  366. drbg->max_noncelen);
  367. if (noncelen == 0) {
  368. PROVerr(0, PROV_R_ERROR_RETRIEVING_NONCE);
  369. goto end;
  370. }
  371. nonce = OPENSSL_malloc(noncelen);
  372. if (nonce == NULL) {
  373. PROVerr(0, PROV_R_ERROR_RETRIEVING_NONCE);
  374. goto end;
  375. }
  376. if (noncelen != drbg->parent_nonce(drbg->parent, nonce,
  377. drbg->strength,
  378. drbg->min_noncelen,
  379. drbg->max_noncelen)) {
  380. PROVerr(0, PROV_R_ERROR_RETRIEVING_NONCE);
  381. goto end;
  382. }
  383. #ifndef PROV_RAND_GET_RANDOM_NONCE
  384. } else if (drbg->parent != NULL) {
  385. #endif
  386. /*
  387. * NIST SP800-90Ar1 section 9.1 says you can combine getting
  388. * the entropy and nonce in 1 call by increasing the entropy
  389. * with 50% and increasing the minimum length to accommodate
  390. * the length of the nonce. We do this in case a nonce is
  391. * required and there is no parental nonce capability.
  392. */
  393. min_entropy += drbg->strength / 2;
  394. min_entropylen += drbg->min_noncelen;
  395. max_entropylen += drbg->max_noncelen;
  396. }
  397. #ifndef PROV_RAND_GET_RANDOM_NONCE
  398. else { /* parent == NULL */
  399. noncelen = prov_drbg_get_nonce(drbg, &nonce, drbg->strength / 2,
  400. drbg->min_noncelen,
  401. drbg->max_noncelen);
  402. if (noncelen < drbg->min_noncelen
  403. || noncelen > drbg->max_noncelen) {
  404. PROVerr(0, PROV_R_ERROR_RETRIEVING_NONCE);
  405. goto end;
  406. }
  407. }
  408. #endif
  409. }
  410. drbg->reseed_next_counter = tsan_load(&drbg->reseed_counter);
  411. if (drbg->reseed_next_counter) {
  412. drbg->reseed_next_counter++;
  413. if (!drbg->reseed_next_counter)
  414. drbg->reseed_next_counter = 1;
  415. }
  416. entropylen = get_entropy(drbg, &entropy, min_entropy,
  417. min_entropylen, max_entropylen,
  418. prediction_resistance);
  419. if (entropylen < min_entropylen
  420. || entropylen > max_entropylen) {
  421. PROVerr(0, PROV_R_ERROR_RETRIEVING_ENTROPY);
  422. goto end;
  423. }
  424. if (!drbg->instantiate(drbg, entropy, entropylen, nonce, noncelen,
  425. pers, perslen)) {
  426. PROVerr(0, PROV_R_ERROR_INSTANTIATING_DRBG);
  427. goto end;
  428. }
  429. drbg->state = EVP_RAND_STATE_READY;
  430. drbg->reseed_gen_counter = 1;
  431. drbg->reseed_time = time(NULL);
  432. tsan_store(&drbg->reseed_counter, drbg->reseed_next_counter);
  433. end:
  434. if (entropy != NULL)
  435. cleanup_entropy(drbg, entropy, entropylen);
  436. prov_drbg_clear_nonce(drbg, nonce, noncelen);
  437. if (drbg->state == EVP_RAND_STATE_READY)
  438. return 1;
  439. return 0;
  440. }
  441. /*
  442. * Uninstantiate |drbg|. Must be instantiated before it can be used.
  443. *
  444. * Requires that drbg->lock is already locked for write, if non-null.
  445. *
  446. * Returns 1 on success, 0 on failure.
  447. */
  448. int PROV_DRBG_uninstantiate(PROV_DRBG *drbg)
  449. {
  450. drbg->state = EVP_RAND_STATE_UNINITIALISED;
  451. return 1;
  452. }
  453. /*
  454. * Reseed |drbg|, mixing in the specified data
  455. *
  456. * Requires that drbg->lock is already locked for write, if non-null.
  457. *
  458. * Returns 1 on success, 0 on failure.
  459. */
  460. int PROV_DRBG_reseed(PROV_DRBG *drbg, int prediction_resistance,
  461. const unsigned char *ent, size_t ent_len,
  462. const unsigned char *adin, size_t adinlen)
  463. {
  464. unsigned char *entropy = NULL;
  465. size_t entropylen = 0;
  466. if (drbg->state != EVP_RAND_STATE_READY) {
  467. /* try to recover from previous errors */
  468. rand_drbg_restart(drbg);
  469. if (drbg->state == EVP_RAND_STATE_ERROR) {
  470. PROVerr(0, PROV_R_IN_ERROR_STATE);
  471. return 0;
  472. }
  473. if (drbg->state == EVP_RAND_STATE_UNINITIALISED) {
  474. PROVerr(0, PROV_R_NOT_INSTANTIATED);
  475. return 0;
  476. }
  477. }
  478. if (ent != NULL) {
  479. if (ent_len < drbg->min_entropylen) {
  480. RANDerr(0, RAND_R_ENTROPY_OUT_OF_RANGE);
  481. drbg->state = EVP_RAND_STATE_ERROR;
  482. return 0;
  483. }
  484. if (ent_len > drbg->max_entropylen) {
  485. RANDerr(0, RAND_R_ENTROPY_INPUT_TOO_LONG);
  486. drbg->state = EVP_RAND_STATE_ERROR;
  487. return 0;
  488. }
  489. }
  490. if (adin == NULL) {
  491. adinlen = 0;
  492. } else if (adinlen > drbg->max_adinlen) {
  493. PROVerr(0, PROV_R_ADDITIONAL_INPUT_TOO_LONG);
  494. return 0;
  495. }
  496. drbg->state = EVP_RAND_STATE_ERROR;
  497. drbg->reseed_next_counter = tsan_load(&drbg->reseed_counter);
  498. if (drbg->reseed_next_counter) {
  499. drbg->reseed_next_counter++;
  500. if (!drbg->reseed_next_counter)
  501. drbg->reseed_next_counter = 1;
  502. }
  503. if (ent != NULL) {
  504. #ifdef FIP_MODULE
  505. /*
  506. * NIST SP-800-90A mandates that entropy *shall not* be provided
  507. * by the consuming application. Instead the data is added as additional
  508. * input.
  509. *
  510. * (NIST SP-800-90Ar1, Sections 9.1 and 9.2)
  511. */
  512. if (!drbg->reseed(drbg, NULL, 0, ent, ent_len)) {
  513. ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_RESEED);
  514. return 0;
  515. }
  516. #else
  517. if (!drbg->reseed(drbg, ent, ent_len, adin, adinlen)) {
  518. ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_RESEED);
  519. return 0;
  520. }
  521. /* There isn't much point adding the same additional input twice */
  522. adin = NULL;
  523. adinlen = 0;
  524. #endif
  525. }
  526. /* Reseed using our sources in addition */
  527. entropylen = get_entropy(drbg, &entropy, drbg->strength,
  528. drbg->min_entropylen, drbg->max_entropylen,
  529. prediction_resistance);
  530. if (entropylen < drbg->min_entropylen
  531. || entropylen > drbg->max_entropylen) {
  532. PROVerr(0, PROV_R_ERROR_RETRIEVING_ENTROPY);
  533. goto end;
  534. }
  535. if (!drbg->reseed(drbg, entropy, entropylen, adin, adinlen))
  536. goto end;
  537. drbg->state = EVP_RAND_STATE_READY;
  538. drbg->reseed_gen_counter = 1;
  539. drbg->reseed_time = time(NULL);
  540. tsan_store(&drbg->reseed_counter, drbg->reseed_next_counter);
  541. if (drbg->parent != NULL)
  542. drbg->parent_reseed_counter = get_parent_reseed_count(drbg);
  543. end:
  544. cleanup_entropy(drbg, entropy, entropylen);
  545. if (drbg->state == EVP_RAND_STATE_READY)
  546. return 1;
  547. return 0;
  548. }
  549. /*
  550. * Generate |outlen| bytes into the buffer at |out|. Reseed if we need
  551. * to or if |prediction_resistance| is set. Additional input can be
  552. * sent in |adin| and |adinlen|.
  553. *
  554. * Requires that drbg->lock is already locked for write, if non-null.
  555. *
  556. * Returns 1 on success, 0 on failure.
  557. *
  558. */
  559. int PROV_DRBG_generate(PROV_DRBG *drbg, unsigned char *out, size_t outlen,
  560. unsigned int strength, int prediction_resistance,
  561. const unsigned char *adin, size_t adinlen)
  562. {
  563. int fork_id;
  564. int reseed_required = 0;
  565. if (drbg->state != EVP_RAND_STATE_READY) {
  566. /* try to recover from previous errors */
  567. rand_drbg_restart(drbg);
  568. if (drbg->state == EVP_RAND_STATE_ERROR) {
  569. PROVerr(0, PROV_R_IN_ERROR_STATE);
  570. return 0;
  571. }
  572. if (drbg->state == EVP_RAND_STATE_UNINITIALISED) {
  573. PROVerr(0, PROV_R_NOT_INSTANTIATED);
  574. return 0;
  575. }
  576. }
  577. if (strength > drbg->strength) {
  578. PROVerr(0, PROV_R_INSUFFICIENT_DRBG_STRENGTH);
  579. return 0;
  580. }
  581. if (outlen > drbg->max_request) {
  582. PROVerr(0, PROV_R_REQUEST_TOO_LARGE_FOR_DRBG);
  583. return 0;
  584. }
  585. if (adinlen > drbg->max_adinlen) {
  586. PROVerr(0, PROV_R_ADDITIONAL_INPUT_TOO_LONG);
  587. return 0;
  588. }
  589. fork_id = openssl_get_fork_id();
  590. if (drbg->fork_id != fork_id) {
  591. drbg->fork_id = fork_id;
  592. reseed_required = 1;
  593. }
  594. if (drbg->reseed_interval > 0) {
  595. if (drbg->reseed_gen_counter >= drbg->reseed_interval)
  596. reseed_required = 1;
  597. }
  598. if (drbg->reseed_time_interval > 0) {
  599. time_t now = time(NULL);
  600. if (now < drbg->reseed_time
  601. || now - drbg->reseed_time >= drbg->reseed_time_interval)
  602. reseed_required = 1;
  603. }
  604. if (drbg->parent != NULL
  605. && get_parent_reseed_count(drbg) != drbg->parent_reseed_counter)
  606. reseed_required = 1;
  607. if (reseed_required || prediction_resistance) {
  608. if (!PROV_DRBG_reseed(drbg, prediction_resistance, NULL, 0,
  609. adin, adinlen)) {
  610. PROVerr(0, PROV_R_RESEED_ERROR);
  611. return 0;
  612. }
  613. adin = NULL;
  614. adinlen = 0;
  615. }
  616. if (!drbg->generate(drbg, out, outlen, adin, adinlen)) {
  617. drbg->state = EVP_RAND_STATE_ERROR;
  618. PROVerr(0, PROV_R_GENERATE_ERROR);
  619. return 0;
  620. }
  621. drbg->reseed_gen_counter++;
  622. return 1;
  623. }
  624. /*
  625. * Restart |drbg|, using the specified entropy or additional input
  626. *
  627. * Tries its best to get the drbg instantiated by all means,
  628. * regardless of its current state.
  629. *
  630. * Optionally, a |buffer| of |len| random bytes can be passed,
  631. * which is assumed to contain at least |entropy| bits of entropy.
  632. *
  633. * If |entropy| > 0, the buffer content is used as entropy input.
  634. *
  635. * If |entropy| == 0, the buffer content is used as additional input
  636. *
  637. * Returns 1 on success, 0 on failure.
  638. *
  639. * This function is used internally only.
  640. */
  641. static int rand_drbg_restart(PROV_DRBG *drbg)
  642. {
  643. if (drbg->seed_pool != NULL) {
  644. drbg->state = EVP_RAND_STATE_ERROR;
  645. rand_pool_free(drbg->seed_pool);
  646. drbg->seed_pool = NULL;
  647. RANDerr(0, ERR_R_INTERNAL_ERROR);
  648. return 0;
  649. }
  650. /* repair error state */
  651. if (drbg->state == EVP_RAND_STATE_ERROR)
  652. drbg->uninstantiate(drbg);
  653. /* repair uninitialized state */
  654. if (drbg->state == EVP_RAND_STATE_UNINITIALISED)
  655. /* reinstantiate drbg */
  656. PROV_DRBG_instantiate(drbg, drbg->strength, 0, NULL, 0);
  657. rand_pool_free(drbg->seed_pool);
  658. drbg->seed_pool = NULL;
  659. return drbg->state == EVP_RAND_STATE_READY;
  660. }
  661. /* Provider support from here down */
  662. static const OSSL_DISPATCH *find_call(const OSSL_DISPATCH *dispatch,
  663. int function)
  664. {
  665. if (dispatch != NULL)
  666. while (dispatch->function_id != 0) {
  667. if (dispatch->function_id == function)
  668. return dispatch;
  669. dispatch++;
  670. }
  671. return NULL;
  672. }
  673. int drbg_enable_locking(void *vctx)
  674. {
  675. PROV_DRBG *drbg = vctx;
  676. if (drbg != NULL && drbg->lock == NULL) {
  677. if (drbg->parent_enable_locking != NULL)
  678. if (!drbg->parent_enable_locking(drbg->parent)) {
  679. ERR_raise(ERR_LIB_PROV, PROV_R_PARENT_LOCKING_NOT_ENABLED);
  680. return 0;
  681. }
  682. drbg->lock = CRYPTO_THREAD_lock_new();
  683. if (drbg->lock == NULL) {
  684. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_CREATE_LOCK);
  685. return 0;
  686. }
  687. }
  688. return 1;
  689. }
  690. /*
  691. * Allocate memory and initialize a new DRBG. The DRBG is allocated on
  692. * the secure heap if |secure| is nonzero and the secure heap is enabled.
  693. * The |parent|, if not NULL, will be used as random source for reseeding.
  694. * This also requires the parent's provider context and the parent's lock.
  695. *
  696. * Returns a pointer to the new DRBG instance on success, NULL on failure.
  697. */
  698. PROV_DRBG *prov_rand_drbg_new
  699. (void *provctx, void *parent, const OSSL_DISPATCH *p_dispatch,
  700. int (*dnew)(PROV_DRBG *ctx),
  701. int (*instantiate)(PROV_DRBG *drbg,
  702. const unsigned char *entropy, size_t entropylen,
  703. const unsigned char *nonce, size_t noncelen,
  704. const unsigned char *pers, size_t perslen),
  705. int (*uninstantiate)(PROV_DRBG *ctx),
  706. int (*reseed)(PROV_DRBG *drbg, const unsigned char *ent, size_t ent_len,
  707. const unsigned char *adin, size_t adin_len),
  708. int (*generate)(PROV_DRBG *, unsigned char *out, size_t outlen,
  709. const unsigned char *adin, size_t adin_len))
  710. {
  711. PROV_DRBG *drbg = OPENSSL_zalloc(sizeof(*drbg));
  712. unsigned int p_str;
  713. const OSSL_DISPATCH *pfunc;
  714. if (drbg == NULL) {
  715. ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
  716. return NULL;
  717. }
  718. drbg->provctx = provctx;
  719. drbg->instantiate = instantiate;
  720. drbg->uninstantiate = uninstantiate;
  721. drbg->reseed = reseed;
  722. drbg->generate = generate;
  723. drbg->fork_id = openssl_get_fork_id();
  724. /* Extract parent's functions */
  725. drbg->parent = parent;
  726. if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_ENABLE_LOCKING)) != NULL)
  727. drbg->parent_enable_locking = OSSL_FUNC_rand_enable_locking(pfunc);
  728. if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_LOCK)) != NULL)
  729. drbg->parent_lock = OSSL_FUNC_rand_lock(pfunc);
  730. if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_UNLOCK)) != NULL)
  731. drbg->parent_unlock = OSSL_FUNC_rand_unlock(pfunc);
  732. if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_GET_CTX_PARAMS)) != NULL)
  733. drbg->parent_get_ctx_params = OSSL_FUNC_rand_get_ctx_params(pfunc);
  734. if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_GENERATE)) != NULL)
  735. drbg->parent_generate = OSSL_FUNC_rand_generate(pfunc);
  736. if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_NONCE)) != NULL)
  737. drbg->parent_nonce = OSSL_FUNC_rand_nonce(pfunc);
  738. /* Set some default maximums up */
  739. drbg->max_entropylen = DRBG_MAX_LENGTH;
  740. drbg->max_noncelen = DRBG_MAX_LENGTH;
  741. drbg->max_perslen = DRBG_MAX_LENGTH;
  742. drbg->max_adinlen = DRBG_MAX_LENGTH;
  743. drbg->reseed_gen_counter = 1;
  744. drbg->reseed_counter = 1;
  745. drbg->reseed_interval = RESEED_INTERVAL;
  746. drbg->reseed_time_interval = TIME_INTERVAL;
  747. if (!dnew(drbg))
  748. goto err;
  749. if (parent != NULL) {
  750. if (!get_parent_strength(drbg, &p_str))
  751. goto err;
  752. if (drbg->strength > p_str) {
  753. /*
  754. * We currently don't support the algorithm from NIST SP 800-90C
  755. * 10.1.2 to use a weaker DRBG as source
  756. */
  757. ERR_raise(ERR_LIB_PROV, PROV_R_PARENT_STRENGTH_TOO_WEAK);
  758. goto err;
  759. }
  760. }
  761. return drbg;
  762. err:
  763. prov_rand_drbg_free(drbg);
  764. return NULL;
  765. }
  766. void prov_rand_drbg_free(PROV_DRBG *drbg)
  767. {
  768. if (drbg == NULL)
  769. return;
  770. rand_pool_free(drbg->adin_pool);
  771. CRYPTO_THREAD_lock_free(drbg->lock);
  772. OPENSSL_free(drbg);
  773. }
  774. int drbg_get_ctx_params(PROV_DRBG *drbg, OSSL_PARAM params[])
  775. {
  776. OSSL_PARAM *p;
  777. p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STATE);
  778. if (p != NULL && !OSSL_PARAM_set_int(p, drbg->state))
  779. return 0;
  780. p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STRENGTH);
  781. if (p != NULL && !OSSL_PARAM_set_int(p, drbg->strength))
  782. return 0;
  783. p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MAX_REQUEST);
  784. if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->max_request))
  785. return 0;
  786. p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MIN_ENTROPYLEN);
  787. if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->min_entropylen))
  788. return 0;
  789. p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MAX_ENTROPYLEN);
  790. if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->max_entropylen))
  791. return 0;
  792. p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MIN_NONCELEN);
  793. if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->min_noncelen))
  794. return 0;
  795. p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MAX_NONCELEN);
  796. if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->max_noncelen))
  797. return 0;
  798. p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MAX_PERSLEN);
  799. if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->max_perslen))
  800. return 0;
  801. p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MAX_ADINLEN);
  802. if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->max_adinlen))
  803. return 0;
  804. p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_RESEED_REQUESTS);
  805. if (p != NULL && !OSSL_PARAM_set_uint(p, drbg->reseed_interval))
  806. return 0;
  807. p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_RESEED_TIME);
  808. if (p != NULL && !OSSL_PARAM_set_time_t(p, drbg->reseed_time))
  809. return 0;
  810. p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL);
  811. if (p != NULL && !OSSL_PARAM_set_time_t(p, drbg->reseed_time_interval))
  812. return 0;
  813. p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_RESEED_CTR);
  814. if (p != NULL
  815. && !OSSL_PARAM_set_uint(p, tsan_load(&drbg->reseed_counter)))
  816. return 0;
  817. return 1;
  818. }
  819. int drbg_set_ctx_params(PROV_DRBG *drbg, const OSSL_PARAM params[])
  820. {
  821. const OSSL_PARAM *p;
  822. p = OSSL_PARAM_locate_const(params, OSSL_DRBG_PARAM_RESEED_REQUESTS);
  823. if (p != NULL && !OSSL_PARAM_get_uint(p, &drbg->reseed_interval))
  824. return 0;
  825. p = OSSL_PARAM_locate_const(params, OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL);
  826. if (p != NULL && !OSSL_PARAM_get_time_t(p, &drbg->reseed_time_interval))
  827. return 0;
  828. return 1;
  829. }