drbg.c 29 KB

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