drbg.c 30 KB

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