drbg.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  1. /*
  2. * Copyright 2011-2024 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 "prov/fipscommon.h"
  24. #include "crypto/context.h"
  25. /*
  26. * Support framework for NIST SP 800-90A DRBG
  27. *
  28. * See manual page PROV_DRBG(7) for a general overview.
  29. *
  30. * The OpenSSL model is to have new and free functions, and that new
  31. * does all initialization. That is not the NIST model, which has
  32. * instantiation and un-instantiate, and reuse within a new/free
  33. * lifecycle. (No doubt this comes from the desire to support hardware
  34. * DRBG, where allocation of resources on something like an HSM is
  35. * a much bigger deal than just re-setting an allocated resource.)
  36. */
  37. /* NIST SP 800-90A DRBG recommends the use of a personalization string. */
  38. static const char ossl_pers_string[] = DRBG_DEFAULT_PERS_STRING;
  39. static const OSSL_DISPATCH *find_call(const OSSL_DISPATCH *dispatch,
  40. int function);
  41. static int rand_drbg_restart(PROV_DRBG *drbg);
  42. /*
  43. * We interpret a call to this function as a hint only and ignore it. This
  44. * occurs when the EVP layer thinks we should do some locking. In practice
  45. * however we manage for ourselves when we take a lock or not on the basis
  46. * of whether drbg->lock is present or not.
  47. */
  48. int ossl_drbg_lock(void *vctx)
  49. {
  50. return 1;
  51. }
  52. /* Interpreted as a hint only and ignored as for ossl_drbg_lock() */
  53. void ossl_drbg_unlock(void *vctx)
  54. {
  55. }
  56. static int ossl_drbg_lock_parent(PROV_DRBG *drbg)
  57. {
  58. void *parent = drbg->parent;
  59. if (parent != NULL
  60. && drbg->parent_lock != NULL
  61. && !drbg->parent_lock(parent)) {
  62. ERR_raise(ERR_LIB_PROV, PROV_R_PARENT_LOCKING_NOT_ENABLED);
  63. return 0;
  64. }
  65. return 1;
  66. }
  67. static void ossl_drbg_unlock_parent(PROV_DRBG *drbg)
  68. {
  69. void *parent = drbg->parent;
  70. if (parent != NULL && drbg->parent_unlock != NULL)
  71. drbg->parent_unlock(parent);
  72. }
  73. static int get_parent_strength(PROV_DRBG *drbg, unsigned int *str)
  74. {
  75. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  76. void *parent = drbg->parent;
  77. int res;
  78. if (drbg->parent_get_ctx_params == NULL) {
  79. ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_GET_PARENT_STRENGTH);
  80. return 0;
  81. }
  82. *params = OSSL_PARAM_construct_uint(OSSL_RAND_PARAM_STRENGTH, str);
  83. if (!ossl_drbg_lock_parent(drbg)) {
  84. ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_LOCK_PARENT);
  85. return 0;
  86. }
  87. res = drbg->parent_get_ctx_params(parent, params);
  88. ossl_drbg_unlock_parent(drbg);
  89. if (!res) {
  90. ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_GET_PARENT_STRENGTH);
  91. return 0;
  92. }
  93. return 1;
  94. }
  95. static unsigned int get_parent_reseed_count(PROV_DRBG *drbg)
  96. {
  97. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  98. void *parent = drbg->parent;
  99. unsigned int r = 0;
  100. *params = OSSL_PARAM_construct_uint(OSSL_DRBG_PARAM_RESEED_COUNTER, &r);
  101. if (!ossl_drbg_lock_parent(drbg)) {
  102. ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_LOCK_PARENT);
  103. goto err;
  104. }
  105. if (!drbg->parent_get_ctx_params(parent, params))
  106. r = 0;
  107. ossl_drbg_unlock_parent(drbg);
  108. return r;
  109. err:
  110. r = tsan_load(&drbg->reseed_counter) - 2;
  111. if (r == 0)
  112. r = UINT_MAX;
  113. return r;
  114. }
  115. /*
  116. * Implements the get_entropy() callback
  117. *
  118. * If the DRBG has a parent, then the required amount of entropy input
  119. * is fetched using the parent's ossl_prov_drbg_generate().
  120. *
  121. * Otherwise, the entropy is polled from the system entropy sources
  122. * using ossl_pool_acquire_entropy().
  123. *
  124. * If a random pool has been added to the DRBG using RAND_add(), then
  125. * its entropy will be used up first.
  126. */
  127. size_t ossl_drbg_get_seed(void *vdrbg, unsigned char **pout,
  128. int entropy, size_t min_len,
  129. size_t max_len, int prediction_resistance,
  130. const unsigned char *adin, size_t adin_len)
  131. {
  132. PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
  133. size_t bytes_needed;
  134. unsigned char *buffer;
  135. /* Figure out how many bytes we need */
  136. bytes_needed = entropy >= 0 ? (entropy + 7) / 8 : 0;
  137. if (bytes_needed < min_len)
  138. bytes_needed = min_len;
  139. if (bytes_needed > max_len)
  140. bytes_needed = max_len;
  141. /* Allocate storage */
  142. buffer = OPENSSL_secure_malloc(bytes_needed);
  143. if (buffer == NULL)
  144. return 0;
  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->parent, 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. if (!CRYPTO_atomic_add(&dngbl->rand_nonce_count, 1, &data.count,
  299. dngbl->rand_nonce_lock))
  300. return 0;
  301. return ossl_prov_get_nonce(drbg->provctx, pout, min_len, max_len,
  302. &data, sizeof(data));
  303. }
  304. #endif /* PROV_RAND_GET_RANDOM_NONCE */
  305. /*
  306. * Instantiate |drbg|, after it has been initialized. Use |pers| and
  307. * |perslen| as prediction-resistance input.
  308. *
  309. * Requires that drbg->lock is already locked for write, if non-null.
  310. *
  311. * Returns 1 on success, 0 on failure.
  312. */
  313. int ossl_prov_drbg_instantiate(PROV_DRBG *drbg, unsigned int strength,
  314. int prediction_resistance,
  315. const unsigned char *pers, size_t perslen)
  316. {
  317. unsigned char *nonce = NULL, *entropy = NULL;
  318. size_t noncelen = 0, entropylen = 0;
  319. size_t min_entropy, min_entropylen, max_entropylen;
  320. if (strength > drbg->strength) {
  321. ERR_raise(ERR_LIB_PROV, PROV_R_INSUFFICIENT_DRBG_STRENGTH);
  322. goto end;
  323. }
  324. min_entropy = drbg->strength;
  325. min_entropylen = drbg->min_entropylen;
  326. max_entropylen = drbg->max_entropylen;
  327. if (pers == NULL) {
  328. pers = (const unsigned char *)ossl_pers_string;
  329. perslen = sizeof(ossl_pers_string);
  330. }
  331. if (perslen > drbg->max_perslen) {
  332. ERR_raise(ERR_LIB_PROV, PROV_R_PERSONALISATION_STRING_TOO_LONG);
  333. goto end;
  334. }
  335. if (drbg->state != EVP_RAND_STATE_UNINITIALISED) {
  336. if (drbg->state == EVP_RAND_STATE_ERROR)
  337. ERR_raise(ERR_LIB_PROV, PROV_R_IN_ERROR_STATE);
  338. else
  339. ERR_raise(ERR_LIB_PROV, PROV_R_ALREADY_INSTANTIATED);
  340. goto end;
  341. }
  342. drbg->state = EVP_RAND_STATE_ERROR;
  343. if (drbg->min_noncelen > 0) {
  344. if (drbg->parent_nonce != NULL) {
  345. noncelen = drbg->parent_nonce(drbg->parent, NULL, drbg->strength,
  346. drbg->min_noncelen,
  347. drbg->max_noncelen);
  348. if (noncelen == 0) {
  349. ERR_raise(ERR_LIB_PROV, PROV_R_ERROR_RETRIEVING_NONCE);
  350. goto end;
  351. }
  352. nonce = OPENSSL_malloc(noncelen);
  353. if (nonce == NULL) {
  354. ERR_raise(ERR_LIB_PROV, PROV_R_ERROR_RETRIEVING_NONCE);
  355. goto end;
  356. }
  357. if (noncelen != drbg->parent_nonce(drbg->parent, nonce,
  358. drbg->strength,
  359. drbg->min_noncelen,
  360. drbg->max_noncelen)) {
  361. ERR_raise(ERR_LIB_PROV, PROV_R_ERROR_RETRIEVING_NONCE);
  362. goto end;
  363. }
  364. #ifndef PROV_RAND_GET_RANDOM_NONCE
  365. } else if (drbg->parent != NULL) {
  366. #endif
  367. /*
  368. * NIST SP800-90Ar1 section 9.1 says you can combine getting
  369. * the entropy and nonce in 1 call by increasing the entropy
  370. * with 50% and increasing the minimum length to accommodate
  371. * the length of the nonce. We do this in case a nonce is
  372. * required and there is no parental nonce capability.
  373. */
  374. min_entropy += drbg->strength / 2;
  375. min_entropylen += drbg->min_noncelen;
  376. max_entropylen += drbg->max_noncelen;
  377. }
  378. #ifndef PROV_RAND_GET_RANDOM_NONCE
  379. else { /* parent == NULL */
  380. noncelen = prov_drbg_get_nonce(drbg, &nonce, drbg->min_noncelen,
  381. drbg->max_noncelen);
  382. if (noncelen < drbg->min_noncelen
  383. || noncelen > drbg->max_noncelen) {
  384. ERR_raise(ERR_LIB_PROV, PROV_R_ERROR_RETRIEVING_NONCE);
  385. goto end;
  386. }
  387. }
  388. #endif
  389. }
  390. drbg->reseed_next_counter = tsan_load(&drbg->reseed_counter);
  391. if (drbg->reseed_next_counter) {
  392. drbg->reseed_next_counter++;
  393. if (!drbg->reseed_next_counter)
  394. drbg->reseed_next_counter = 1;
  395. }
  396. entropylen = get_entropy(drbg, &entropy, min_entropy,
  397. min_entropylen, max_entropylen,
  398. prediction_resistance);
  399. if (entropylen < min_entropylen
  400. || entropylen > max_entropylen) {
  401. ERR_raise(ERR_LIB_PROV, PROV_R_ERROR_RETRIEVING_ENTROPY);
  402. goto end;
  403. }
  404. if (!drbg->instantiate(drbg, entropy, entropylen, nonce, noncelen,
  405. pers, perslen)) {
  406. cleanup_entropy(drbg, entropy, entropylen);
  407. ERR_raise(ERR_LIB_PROV, PROV_R_ERROR_INSTANTIATING_DRBG);
  408. goto end;
  409. }
  410. cleanup_entropy(drbg, entropy, entropylen);
  411. drbg->state = EVP_RAND_STATE_READY;
  412. drbg->generate_counter = 1;
  413. drbg->reseed_time = time(NULL);
  414. tsan_store(&drbg->reseed_counter, drbg->reseed_next_counter);
  415. end:
  416. if (nonce != NULL)
  417. ossl_prov_cleanup_nonce(drbg->provctx, nonce, noncelen);
  418. if (drbg->state == EVP_RAND_STATE_READY)
  419. return 1;
  420. return 0;
  421. }
  422. /*
  423. * Uninstantiate |drbg|. Must be instantiated before it can be used.
  424. *
  425. * Requires that drbg->lock is already locked for write, if non-null.
  426. *
  427. * Returns 1 on success, 0 on failure.
  428. */
  429. int ossl_prov_drbg_uninstantiate(PROV_DRBG *drbg)
  430. {
  431. drbg->state = EVP_RAND_STATE_UNINITIALISED;
  432. return 1;
  433. }
  434. static int ossl_prov_drbg_reseed_unlocked(PROV_DRBG *drbg,
  435. int prediction_resistance,
  436. const unsigned char *ent,
  437. size_t ent_len,
  438. const unsigned char *adin,
  439. size_t adinlen)
  440. {
  441. unsigned char *entropy = NULL;
  442. size_t entropylen = 0;
  443. if (!ossl_prov_is_running())
  444. return 0;
  445. if (drbg->state != EVP_RAND_STATE_READY) {
  446. /* try to recover from previous errors */
  447. rand_drbg_restart(drbg);
  448. if (drbg->state == EVP_RAND_STATE_ERROR) {
  449. ERR_raise(ERR_LIB_PROV, PROV_R_IN_ERROR_STATE);
  450. return 0;
  451. }
  452. if (drbg->state == EVP_RAND_STATE_UNINITIALISED) {
  453. ERR_raise(ERR_LIB_PROV, PROV_R_NOT_INSTANTIATED);
  454. return 0;
  455. }
  456. }
  457. if (ent != NULL) {
  458. if (ent_len < drbg->min_entropylen) {
  459. ERR_raise(ERR_LIB_RAND, RAND_R_ENTROPY_OUT_OF_RANGE);
  460. drbg->state = EVP_RAND_STATE_ERROR;
  461. return 0;
  462. }
  463. if (ent_len > drbg->max_entropylen) {
  464. ERR_raise(ERR_LIB_RAND, RAND_R_ENTROPY_INPUT_TOO_LONG);
  465. drbg->state = EVP_RAND_STATE_ERROR;
  466. return 0;
  467. }
  468. }
  469. if (adin == NULL) {
  470. adinlen = 0;
  471. } else if (adinlen > drbg->max_adinlen) {
  472. ERR_raise(ERR_LIB_PROV, PROV_R_ADDITIONAL_INPUT_TOO_LONG);
  473. return 0;
  474. }
  475. drbg->state = EVP_RAND_STATE_ERROR;
  476. drbg->reseed_next_counter = tsan_load(&drbg->reseed_counter);
  477. if (drbg->reseed_next_counter) {
  478. drbg->reseed_next_counter++;
  479. if (!drbg->reseed_next_counter)
  480. drbg->reseed_next_counter = 1;
  481. }
  482. if (ent != NULL) {
  483. #ifdef FIPS_MODULE
  484. /*
  485. * NIST SP-800-90A mandates that entropy *shall not* be provided
  486. * by the consuming application. Instead the data is added as additional
  487. * input.
  488. *
  489. * (NIST SP-800-90Ar1, Sections 9.1 and 9.2)
  490. */
  491. if (!drbg->reseed(drbg, NULL, 0, ent, ent_len)) {
  492. ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_RESEED);
  493. return 0;
  494. }
  495. #else
  496. if (!drbg->reseed(drbg, ent, ent_len, adin, adinlen)) {
  497. ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_RESEED);
  498. return 0;
  499. }
  500. /* There isn't much point adding the same additional input twice */
  501. adin = NULL;
  502. adinlen = 0;
  503. #endif
  504. }
  505. /* Reseed using our sources in addition */
  506. entropylen = get_entropy(drbg, &entropy, drbg->strength,
  507. drbg->min_entropylen, drbg->max_entropylen,
  508. prediction_resistance);
  509. if (entropylen < drbg->min_entropylen
  510. || entropylen > drbg->max_entropylen) {
  511. ERR_raise(ERR_LIB_PROV, PROV_R_ERROR_RETRIEVING_ENTROPY);
  512. goto end;
  513. }
  514. if (!drbg->reseed(drbg, entropy, entropylen, adin, adinlen))
  515. goto end;
  516. drbg->state = EVP_RAND_STATE_READY;
  517. drbg->generate_counter = 1;
  518. drbg->reseed_time = time(NULL);
  519. tsan_store(&drbg->reseed_counter, drbg->reseed_next_counter);
  520. if (drbg->parent != NULL)
  521. drbg->parent_reseed_counter = get_parent_reseed_count(drbg);
  522. end:
  523. cleanup_entropy(drbg, entropy, entropylen);
  524. if (drbg->state == EVP_RAND_STATE_READY)
  525. return 1;
  526. return 0;
  527. }
  528. /*
  529. * Reseed |drbg|, mixing in the specified data
  530. *
  531. * Acquires the drbg->lock for writing, if non-null.
  532. *
  533. * Returns 1 on success, 0 on failure.
  534. */
  535. int ossl_prov_drbg_reseed(PROV_DRBG *drbg, int prediction_resistance,
  536. const unsigned char *ent, size_t ent_len,
  537. const unsigned char *adin, size_t adinlen)
  538. {
  539. int ret;
  540. if (drbg->lock != NULL && !CRYPTO_THREAD_write_lock(drbg->lock))
  541. return 0;
  542. ret = ossl_prov_drbg_reseed_unlocked(drbg, prediction_resistance, ent,
  543. ent_len, adin, adinlen);
  544. if (drbg->lock != NULL)
  545. CRYPTO_THREAD_unlock(drbg->lock);
  546. return ret;
  547. }
  548. /*
  549. * Generate |outlen| bytes into the buffer at |out|. Reseed if we need
  550. * to or if |prediction_resistance| is set. Additional input can be
  551. * sent in |adin| and |adinlen|.
  552. *
  553. * Acquires the drbg->lock for writing if available
  554. *
  555. * Returns 1 on success, 0 on failure.
  556. *
  557. */
  558. int ossl_prov_drbg_generate(PROV_DRBG *drbg, unsigned char *out, size_t outlen,
  559. unsigned int strength, int prediction_resistance,
  560. const unsigned char *adin, size_t adinlen)
  561. {
  562. int fork_id;
  563. int reseed_required = 0;
  564. int ret = 0;
  565. if (!ossl_prov_is_running())
  566. return 0;
  567. if (drbg->lock != NULL && !CRYPTO_THREAD_write_lock(drbg->lock))
  568. return 0;
  569. if (drbg->state != EVP_RAND_STATE_READY) {
  570. /* try to recover from previous errors */
  571. rand_drbg_restart(drbg);
  572. if (drbg->state == EVP_RAND_STATE_ERROR) {
  573. ERR_raise(ERR_LIB_PROV, PROV_R_IN_ERROR_STATE);
  574. goto err;
  575. }
  576. if (drbg->state == EVP_RAND_STATE_UNINITIALISED) {
  577. ERR_raise(ERR_LIB_PROV, PROV_R_NOT_INSTANTIATED);
  578. goto err;
  579. }
  580. }
  581. if (strength > drbg->strength) {
  582. ERR_raise(ERR_LIB_PROV, PROV_R_INSUFFICIENT_DRBG_STRENGTH);
  583. goto err;
  584. }
  585. if (outlen > drbg->max_request) {
  586. ERR_raise(ERR_LIB_PROV, PROV_R_REQUEST_TOO_LARGE_FOR_DRBG);
  587. goto err;
  588. }
  589. if (adinlen > drbg->max_adinlen) {
  590. ERR_raise(ERR_LIB_PROV, PROV_R_ADDITIONAL_INPUT_TOO_LONG);
  591. goto err;
  592. }
  593. fork_id = openssl_get_fork_id();
  594. if (drbg->fork_id != fork_id) {
  595. drbg->fork_id = fork_id;
  596. reseed_required = 1;
  597. }
  598. if (drbg->reseed_interval > 0) {
  599. if (drbg->generate_counter >= drbg->reseed_interval)
  600. reseed_required = 1;
  601. }
  602. if (drbg->reseed_time_interval > 0) {
  603. time_t now = time(NULL);
  604. if (now < drbg->reseed_time
  605. || now - drbg->reseed_time >= drbg->reseed_time_interval)
  606. reseed_required = 1;
  607. }
  608. if (drbg->parent != NULL
  609. && get_parent_reseed_count(drbg) != drbg->parent_reseed_counter)
  610. reseed_required = 1;
  611. if (reseed_required || prediction_resistance) {
  612. if (!ossl_prov_drbg_reseed_unlocked(drbg, prediction_resistance, NULL,
  613. 0, adin, adinlen)) {
  614. ERR_raise(ERR_LIB_PROV, PROV_R_RESEED_ERROR);
  615. goto err;
  616. }
  617. adin = NULL;
  618. adinlen = 0;
  619. }
  620. if (!drbg->generate(drbg, out, outlen, adin, adinlen)) {
  621. drbg->state = EVP_RAND_STATE_ERROR;
  622. ERR_raise(ERR_LIB_PROV, PROV_R_GENERATE_ERROR);
  623. goto err;
  624. }
  625. drbg->generate_counter++;
  626. ret = 1;
  627. err:
  628. if (drbg->lock != NULL)
  629. CRYPTO_THREAD_unlock(drbg->lock);
  630. return ret;
  631. }
  632. /*
  633. * Restart |drbg|, using the specified entropy or additional input
  634. *
  635. * Tries its best to get the drbg instantiated by all means,
  636. * regardless of its current state.
  637. *
  638. * Optionally, a |buffer| of |len| random bytes can be passed,
  639. * which is assumed to contain at least |entropy| bits of entropy.
  640. *
  641. * If |entropy| > 0, the buffer content is used as entropy input.
  642. *
  643. * If |entropy| == 0, the buffer content is used as additional input
  644. *
  645. * Returns 1 on success, 0 on failure.
  646. *
  647. * This function is used internally only.
  648. */
  649. static int rand_drbg_restart(PROV_DRBG *drbg)
  650. {
  651. /* repair error state */
  652. if (drbg->state == EVP_RAND_STATE_ERROR)
  653. drbg->uninstantiate(drbg);
  654. /* repair uninitialized state */
  655. if (drbg->state == EVP_RAND_STATE_UNINITIALISED)
  656. /* reinstantiate drbg */
  657. ossl_prov_drbg_instantiate(drbg, drbg->strength, 0, NULL, 0);
  658. return drbg->state == EVP_RAND_STATE_READY;
  659. }
  660. /* Provider support from here down */
  661. static const OSSL_DISPATCH *find_call(const OSSL_DISPATCH *dispatch,
  662. int function)
  663. {
  664. if (dispatch != NULL)
  665. while (dispatch->function_id != 0) {
  666. if (dispatch->function_id == function)
  667. return dispatch;
  668. dispatch++;
  669. }
  670. return NULL;
  671. }
  672. int ossl_drbg_enable_locking(void *vctx)
  673. {
  674. PROV_DRBG *drbg = vctx;
  675. if (drbg != NULL && drbg->lock == NULL) {
  676. if (drbg->parent_enable_locking != NULL)
  677. if (!drbg->parent_enable_locking(drbg->parent)) {
  678. ERR_raise(ERR_LIB_PROV, PROV_R_PARENT_LOCKING_NOT_ENABLED);
  679. return 0;
  680. }
  681. drbg->lock = CRYPTO_THREAD_lock_new();
  682. if (drbg->lock == NULL) {
  683. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_CREATE_LOCK);
  684. return 0;
  685. }
  686. }
  687. return 1;
  688. }
  689. /*
  690. * Allocate memory and initialize a new DRBG. The DRBG is allocated on
  691. * the secure heap if |secure| is nonzero and the secure heap is enabled.
  692. * The |parent|, if not NULL, will be used as random source for reseeding.
  693. * This also requires the parent's provider context and the parent's lock.
  694. *
  695. * Returns a pointer to the new DRBG instance on success, NULL on failure.
  696. */
  697. PROV_DRBG *ossl_rand_drbg_new
  698. (void *provctx, void *parent, const OSSL_DISPATCH *p_dispatch,
  699. int (*dnew)(PROV_DRBG *ctx),
  700. void (*dfree)(void *vctx),
  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;
  712. unsigned int p_str;
  713. const OSSL_DISPATCH *pfunc;
  714. if (!ossl_prov_is_running())
  715. return NULL;
  716. drbg = OPENSSL_zalloc(sizeof(*drbg));
  717. if (drbg == NULL)
  718. return NULL;
  719. drbg->provctx = provctx;
  720. drbg->instantiate = instantiate;
  721. drbg->uninstantiate = uninstantiate;
  722. drbg->reseed = reseed;
  723. drbg->generate = generate;
  724. drbg->fork_id = openssl_get_fork_id();
  725. /* Extract parent's functions */
  726. drbg->parent = parent;
  727. if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_ENABLE_LOCKING)) != NULL)
  728. drbg->parent_enable_locking = OSSL_FUNC_rand_enable_locking(pfunc);
  729. if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_LOCK)) != NULL)
  730. drbg->parent_lock = OSSL_FUNC_rand_lock(pfunc);
  731. if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_UNLOCK)) != NULL)
  732. drbg->parent_unlock = OSSL_FUNC_rand_unlock(pfunc);
  733. if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_GET_CTX_PARAMS)) != NULL)
  734. drbg->parent_get_ctx_params = OSSL_FUNC_rand_get_ctx_params(pfunc);
  735. if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_NONCE)) != NULL)
  736. drbg->parent_nonce = OSSL_FUNC_rand_nonce(pfunc);
  737. if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_GET_SEED)) != NULL)
  738. drbg->parent_get_seed = OSSL_FUNC_rand_get_seed(pfunc);
  739. if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_CLEAR_SEED)) != NULL)
  740. drbg->parent_clear_seed = OSSL_FUNC_rand_clear_seed(pfunc);
  741. /* Set some default maximums up */
  742. drbg->max_entropylen = DRBG_MAX_LENGTH;
  743. drbg->max_noncelen = DRBG_MAX_LENGTH;
  744. drbg->max_perslen = DRBG_MAX_LENGTH;
  745. drbg->max_adinlen = DRBG_MAX_LENGTH;
  746. drbg->generate_counter = 1;
  747. drbg->reseed_counter = 1;
  748. drbg->reseed_interval = RESEED_INTERVAL;
  749. drbg->reseed_time_interval = TIME_INTERVAL;
  750. if (!dnew(drbg))
  751. goto err;
  752. if (parent != NULL) {
  753. if (!get_parent_strength(drbg, &p_str))
  754. goto err;
  755. if (drbg->strength > p_str) {
  756. /*
  757. * We currently don't support the algorithm from NIST SP 800-90C
  758. * 10.1.2 to use a weaker DRBG as source
  759. */
  760. ERR_raise(ERR_LIB_PROV, PROV_R_PARENT_STRENGTH_TOO_WEAK);
  761. goto err;
  762. }
  763. }
  764. #ifdef TSAN_REQUIRES_LOCKING
  765. if (!ossl_drbg_enable_locking(drbg))
  766. goto err;
  767. #endif
  768. return drbg;
  769. err:
  770. dfree(drbg);
  771. return NULL;
  772. }
  773. void ossl_rand_drbg_free(PROV_DRBG *drbg)
  774. {
  775. if (drbg == NULL)
  776. return;
  777. CRYPTO_THREAD_lock_free(drbg->lock);
  778. OPENSSL_free(drbg);
  779. }
  780. /*
  781. * Helper function called by internal DRBG implementations. Assumes that at
  782. * least a read lock has been taken on drbg->lock
  783. */
  784. int ossl_drbg_get_ctx_params(PROV_DRBG *drbg, OSSL_PARAM params[])
  785. {
  786. OSSL_PARAM *p;
  787. p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STATE);
  788. if (p != NULL && !OSSL_PARAM_set_int(p, drbg->state))
  789. return 0;
  790. p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STRENGTH);
  791. if (p != NULL && !OSSL_PARAM_set_int(p, drbg->strength))
  792. return 0;
  793. p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MIN_ENTROPYLEN);
  794. if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->min_entropylen))
  795. return 0;
  796. p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MAX_ENTROPYLEN);
  797. if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->max_entropylen))
  798. return 0;
  799. p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MIN_NONCELEN);
  800. if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->min_noncelen))
  801. return 0;
  802. p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MAX_NONCELEN);
  803. if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->max_noncelen))
  804. return 0;
  805. p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MAX_PERSLEN);
  806. if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->max_perslen))
  807. return 0;
  808. p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MAX_ADINLEN);
  809. if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->max_adinlen))
  810. return 0;
  811. p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_RESEED_REQUESTS);
  812. if (p != NULL && !OSSL_PARAM_set_uint(p, drbg->reseed_interval))
  813. return 0;
  814. p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_RESEED_TIME);
  815. if (p != NULL && !OSSL_PARAM_set_time_t(p, drbg->reseed_time))
  816. return 0;
  817. p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL);
  818. if (p != NULL && !OSSL_PARAM_set_time_t(p, drbg->reseed_time_interval))
  819. return 0;
  820. return 1;
  821. }
  822. /*
  823. * Helper function to get certain params that require no lock to obtain. Sets
  824. * *complete to 1 if all the params were processed, or 0 otherwise
  825. */
  826. int ossl_drbg_get_ctx_params_no_lock(PROV_DRBG *drbg, OSSL_PARAM params[],
  827. int *complete)
  828. {
  829. size_t cnt = 0;
  830. OSSL_PARAM *p;
  831. /* This value never changes once set */
  832. p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_MAX_REQUEST);
  833. if (p != NULL) {
  834. if (!OSSL_PARAM_set_size_t(p, drbg->max_request))
  835. return 0;
  836. cnt++;
  837. }
  838. /*
  839. * Can be changed by multiple threads, but we tolerate inaccuracies in this
  840. * value.
  841. */
  842. p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_RESEED_COUNTER);
  843. if (p != NULL) {
  844. if (!OSSL_PARAM_set_uint(p, tsan_load(&drbg->reseed_counter)))
  845. return 0;
  846. cnt++;
  847. }
  848. if (params[cnt].key == NULL)
  849. *complete = 1;
  850. else
  851. *complete = 0;
  852. return 1;
  853. }
  854. int ossl_drbg_set_ctx_params(PROV_DRBG *drbg, const OSSL_PARAM params[])
  855. {
  856. const OSSL_PARAM *p;
  857. if (params == NULL)
  858. return 1;
  859. p = OSSL_PARAM_locate_const(params, OSSL_DRBG_PARAM_RESEED_REQUESTS);
  860. if (p != NULL && !OSSL_PARAM_get_uint(p, &drbg->reseed_interval))
  861. return 0;
  862. p = OSSL_PARAM_locate_const(params, OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL);
  863. if (p != NULL && !OSSL_PARAM_get_time_t(p, &drbg->reseed_time_interval))
  864. return 0;
  865. return 1;
  866. }
  867. /* Confirm digest is allowed to be used with a DRBG */
  868. int ossl_drbg_verify_digest(ossl_unused OSSL_LIB_CTX *libctx, const EVP_MD *md)
  869. {
  870. #ifdef FIPS_MODULE
  871. /* FIPS 140-3 IG D.R limited DRBG digests to a specific set */
  872. static const char *const allowed_digests[] = {
  873. "SHA1", /* SHA 1 allowed */
  874. "SHA2-256", "SHA2-512", /* non-truncated SHA2 allowed */
  875. "SHA3-256", "SHA3-512", /* non-truncated SHA3 allowed */
  876. };
  877. size_t i;
  878. if (FIPS_restricted_drbg_digests_enabled(libctx)) {
  879. for (i = 0; i < OSSL_NELEM(allowed_digests); i++)
  880. if (EVP_MD_is_a(md, allowed_digests[i]))
  881. return 1;
  882. ERR_raise(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED);
  883. return 0;
  884. }
  885. #endif
  886. /* Outside of FIPS, any digests that are not XOF are allowed */
  887. if ((EVP_MD_get_flags(md) & EVP_MD_FLAG_XOF) != 0) {
  888. ERR_raise(ERR_LIB_PROV, PROV_R_XOF_DIGESTS_NOT_ALLOWED);
  889. return 0;
  890. }
  891. return 1;
  892. }