drbg.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  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. #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 re-use 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. int (*instantiate)(PROV_DRBG *drbg,
  701. const unsigned char *entropy, size_t entropylen,
  702. const unsigned char *nonce, size_t noncelen,
  703. const unsigned char *pers, size_t perslen),
  704. int (*uninstantiate)(PROV_DRBG *ctx),
  705. int (*reseed)(PROV_DRBG *drbg, const unsigned char *ent, size_t ent_len,
  706. const unsigned char *adin, size_t adin_len),
  707. int (*generate)(PROV_DRBG *, unsigned char *out, size_t outlen,
  708. const unsigned char *adin, size_t adin_len))
  709. {
  710. PROV_DRBG *drbg;
  711. unsigned int p_str;
  712. const OSSL_DISPATCH *pfunc;
  713. if (!ossl_prov_is_running())
  714. return NULL;
  715. drbg = OPENSSL_zalloc(sizeof(*drbg));
  716. if (drbg == NULL)
  717. return NULL;
  718. drbg->provctx = provctx;
  719. drbg->instantiate = instantiate;
  720. drbg->uninstantiate = uninstantiate;
  721. drbg->reseed = reseed;
  722. drbg->generate = generate;
  723. drbg->fork_id = openssl_get_fork_id();
  724. /* Extract parent's functions */
  725. drbg->parent = parent;
  726. if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_ENABLE_LOCKING)) != NULL)
  727. drbg->parent_enable_locking = OSSL_FUNC_rand_enable_locking(pfunc);
  728. if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_LOCK)) != NULL)
  729. drbg->parent_lock = OSSL_FUNC_rand_lock(pfunc);
  730. if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_UNLOCK)) != NULL)
  731. drbg->parent_unlock = OSSL_FUNC_rand_unlock(pfunc);
  732. if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_GET_CTX_PARAMS)) != NULL)
  733. drbg->parent_get_ctx_params = OSSL_FUNC_rand_get_ctx_params(pfunc);
  734. if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_NONCE)) != NULL)
  735. drbg->parent_nonce = OSSL_FUNC_rand_nonce(pfunc);
  736. if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_GET_SEED)) != NULL)
  737. drbg->parent_get_seed = OSSL_FUNC_rand_get_seed(pfunc);
  738. if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_CLEAR_SEED)) != NULL)
  739. drbg->parent_clear_seed = OSSL_FUNC_rand_clear_seed(pfunc);
  740. /* Set some default maximums up */
  741. drbg->max_entropylen = DRBG_MAX_LENGTH;
  742. drbg->max_noncelen = DRBG_MAX_LENGTH;
  743. drbg->max_perslen = DRBG_MAX_LENGTH;
  744. drbg->max_adinlen = DRBG_MAX_LENGTH;
  745. drbg->generate_counter = 1;
  746. drbg->reseed_counter = 1;
  747. drbg->reseed_interval = RESEED_INTERVAL;
  748. drbg->reseed_time_interval = TIME_INTERVAL;
  749. if (!dnew(drbg))
  750. goto err;
  751. if (parent != NULL) {
  752. if (!get_parent_strength(drbg, &p_str))
  753. goto err;
  754. if (drbg->strength > p_str) {
  755. /*
  756. * We currently don't support the algorithm from NIST SP 800-90C
  757. * 10.1.2 to use a weaker DRBG as source
  758. */
  759. ERR_raise(ERR_LIB_PROV, PROV_R_PARENT_STRENGTH_TOO_WEAK);
  760. goto err;
  761. }
  762. }
  763. #ifdef TSAN_REQUIRES_LOCKING
  764. if (!ossl_drbg_enable_locking(drbg))
  765. goto err;
  766. #endif
  767. return drbg;
  768. err:
  769. ossl_rand_drbg_free(drbg);
  770. return NULL;
  771. }
  772. void ossl_rand_drbg_free(PROV_DRBG *drbg)
  773. {
  774. if (drbg == NULL)
  775. return;
  776. CRYPTO_THREAD_lock_free(drbg->lock);
  777. OPENSSL_free(drbg);
  778. }
  779. /*
  780. * Helper function called by internal DRBG implementations. Assumes that at
  781. * least a read lock has been taken on drbg->lock
  782. */
  783. int ossl_drbg_get_ctx_params(PROV_DRBG *drbg, OSSL_PARAM params[])
  784. {
  785. OSSL_PARAM *p;
  786. p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STATE);
  787. if (p != NULL && !OSSL_PARAM_set_int(p, drbg->state))
  788. return 0;
  789. p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STRENGTH);
  790. if (p != NULL && !OSSL_PARAM_set_int(p, drbg->strength))
  791. return 0;
  792. p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MIN_ENTROPYLEN);
  793. if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->min_entropylen))
  794. return 0;
  795. p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MAX_ENTROPYLEN);
  796. if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->max_entropylen))
  797. return 0;
  798. p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MIN_NONCELEN);
  799. if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->min_noncelen))
  800. return 0;
  801. p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MAX_NONCELEN);
  802. if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->max_noncelen))
  803. return 0;
  804. p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MAX_PERSLEN);
  805. if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->max_perslen))
  806. return 0;
  807. p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MAX_ADINLEN);
  808. if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->max_adinlen))
  809. return 0;
  810. p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_RESEED_REQUESTS);
  811. if (p != NULL && !OSSL_PARAM_set_uint(p, drbg->reseed_interval))
  812. return 0;
  813. p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_RESEED_TIME);
  814. if (p != NULL && !OSSL_PARAM_set_time_t(p, drbg->reseed_time))
  815. return 0;
  816. p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL);
  817. if (p != NULL && !OSSL_PARAM_set_time_t(p, drbg->reseed_time_interval))
  818. return 0;
  819. return 1;
  820. }
  821. /*
  822. * Helper function to get certain params that require no lock to obtain. Sets
  823. * *complete to 1 if all the params were processed, or 0 otherwise
  824. */
  825. int ossl_drbg_get_ctx_params_no_lock(PROV_DRBG *drbg, OSSL_PARAM params[],
  826. int *complete)
  827. {
  828. size_t cnt = 0;
  829. OSSL_PARAM *p;
  830. /* This value never changes once set */
  831. p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_MAX_REQUEST);
  832. if (p != NULL) {
  833. if (!OSSL_PARAM_set_size_t(p, drbg->max_request))
  834. return 0;
  835. cnt++;
  836. }
  837. /*
  838. * Can be changed by multiple threads, but we tolerate inaccuracies in this
  839. * value.
  840. */
  841. p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_RESEED_COUNTER);
  842. if (p != NULL) {
  843. if (!OSSL_PARAM_set_uint(p, tsan_load(&drbg->reseed_counter)))
  844. return 0;
  845. cnt++;
  846. }
  847. if (params[cnt].key == NULL)
  848. *complete = 1;
  849. else
  850. *complete = 0;
  851. return 1;
  852. }
  853. int ossl_drbg_set_ctx_params(PROV_DRBG *drbg, const OSSL_PARAM params[])
  854. {
  855. const OSSL_PARAM *p;
  856. if (params == NULL)
  857. return 1;
  858. p = OSSL_PARAM_locate_const(params, OSSL_DRBG_PARAM_RESEED_REQUESTS);
  859. if (p != NULL && !OSSL_PARAM_get_uint(p, &drbg->reseed_interval))
  860. return 0;
  861. p = OSSL_PARAM_locate_const(params, OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL);
  862. if (p != NULL && !OSSL_PARAM_get_time_t(p, &drbg->reseed_time_interval))
  863. return 0;
  864. return 1;
  865. }
  866. /* Confirm digest is allowed to be used with a DRBG */
  867. int ossl_drbg_verify_digest(ossl_unused OSSL_LIB_CTX *libctx, const EVP_MD *md)
  868. {
  869. #ifdef FIPS_MODULE
  870. /* FIPS 140-3 IG D.R limited DRBG digests to a specific set */
  871. static const char *const allowed_digests[] = {
  872. "SHA1", /* SHA 1 allowed */
  873. "SHA2-256", "SHA2-512", /* non-truncated SHA2 allowed */
  874. "SHA3-256", "SHA3-512", /* non-truncated SHA3 allowed */
  875. };
  876. size_t i;
  877. if (FIPS_restricted_drbg_digests_enabled(libctx)) {
  878. for (i = 0; i < OSSL_NELEM(allowed_digests); i++)
  879. if (EVP_MD_is_a(md, allowed_digests[i]))
  880. return 1;
  881. ERR_raise(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED);
  882. return 0;
  883. }
  884. #endif
  885. /* Outside of FIPS, any digests that are not XOF are allowed */
  886. if ((EVP_MD_get_flags(md) & EVP_MD_FLAG_XOF) != 0) {
  887. ERR_raise(ERR_LIB_PROV, PROV_R_XOF_DIGESTS_NOT_ALLOWED);
  888. return 0;
  889. }
  890. return 1;
  891. }