drbg.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  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 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. /*
  182. * In normal use (i.e. OpenSSL's own uses), this is never called.
  183. * Outside of the FIPS provider, OpenSSL sets its DRBGs up so that
  184. * they always have a parent. This remains purely for legacy reasons.
  185. */
  186. return ossl_prov_get_entropy(drbg->provctx, pout, entropy, min_len,
  187. max_len);
  188. #endif
  189. if (drbg->parent_get_seed == NULL) {
  190. ERR_raise(ERR_LIB_PROV, PROV_R_PARENT_CANNOT_SUPPLY_ENTROPY_SEED);
  191. return 0;
  192. }
  193. if (!get_parent_strength(drbg, &p_str))
  194. return 0;
  195. if (drbg->strength > p_str) {
  196. /*
  197. * We currently don't support the algorithm from NIST SP 800-90C
  198. * 10.1.2 to use a weaker DRBG as source
  199. */
  200. ERR_raise(ERR_LIB_PROV, PROV_R_PARENT_STRENGTH_TOO_WEAK);
  201. return 0;
  202. }
  203. /*
  204. * Our lock is already held, but we need to lock our parent before
  205. * generating bits from it. Note: taking the lock will be a no-op
  206. * if locking is not required (while drbg->parent->lock == NULL).
  207. */
  208. if (!ossl_drbg_lock_parent(drbg))
  209. return 0;
  210. /*
  211. * Get random data from parent. Include our DRBG address as
  212. * additional input, in order to provide a distinction between
  213. * different DRBG child instances.
  214. *
  215. * Note: using the sizeof() operator on a pointer triggers
  216. * a warning in some static code analyzers, but it's
  217. * intentional and correct here.
  218. */
  219. bytes = drbg->parent_get_seed(drbg->parent, pout, drbg->strength,
  220. min_len, max_len, prediction_resistance,
  221. (unsigned char *)&drbg, sizeof(drbg));
  222. ossl_drbg_unlock_parent(drbg);
  223. return bytes;
  224. }
  225. static void cleanup_entropy(PROV_DRBG *drbg, unsigned char *out, size_t outlen)
  226. {
  227. if (drbg->parent == NULL) {
  228. #ifdef FIPS_MODULE
  229. ossl_crngt_cleanup_entropy(drbg, out, outlen);
  230. #else
  231. ossl_prov_cleanup_entropy(drbg->provctx, out, outlen);
  232. #endif
  233. } else if (drbg->parent_clear_seed != NULL) {
  234. if (!ossl_drbg_lock_parent(drbg))
  235. return;
  236. drbg->parent_clear_seed(drbg->parent, out, outlen);
  237. ossl_drbg_unlock_parent(drbg);
  238. }
  239. }
  240. #ifndef PROV_RAND_GET_RANDOM_NONCE
  241. typedef struct prov_drbg_nonce_global_st {
  242. CRYPTO_RWLOCK *rand_nonce_lock;
  243. int rand_nonce_count;
  244. } PROV_DRBG_NONCE_GLOBAL;
  245. /*
  246. * drbg_ossl_ctx_new() calls drgb_setup() which calls rand_drbg_get_nonce()
  247. * which needs to get the rand_nonce_lock out of the OSSL_LIB_CTX...but since
  248. * drbg_ossl_ctx_new() hasn't finished running yet we need the rand_nonce_lock
  249. * to be in a different global data object. Otherwise we will go into an
  250. * infinite recursion loop.
  251. */
  252. void *ossl_prov_drbg_nonce_ctx_new(OSSL_LIB_CTX *libctx)
  253. {
  254. PROV_DRBG_NONCE_GLOBAL *dngbl = OPENSSL_zalloc(sizeof(*dngbl));
  255. if (dngbl == NULL)
  256. return NULL;
  257. dngbl->rand_nonce_lock = CRYPTO_THREAD_lock_new();
  258. if (dngbl->rand_nonce_lock == NULL) {
  259. OPENSSL_free(dngbl);
  260. return NULL;
  261. }
  262. return dngbl;
  263. }
  264. void ossl_prov_drbg_nonce_ctx_free(void *vdngbl)
  265. {
  266. PROV_DRBG_NONCE_GLOBAL *dngbl = vdngbl;
  267. if (dngbl == NULL)
  268. return;
  269. CRYPTO_THREAD_lock_free(dngbl->rand_nonce_lock);
  270. OPENSSL_free(dngbl);
  271. }
  272. /* Get a nonce from the operating system */
  273. static size_t prov_drbg_get_nonce(PROV_DRBG *drbg, unsigned char **pout,
  274. size_t min_len, size_t max_len)
  275. {
  276. size_t ret = 0, n;
  277. unsigned char *buf = NULL;
  278. OSSL_LIB_CTX *libctx = ossl_prov_ctx_get0_libctx(drbg->provctx);
  279. PROV_DRBG_NONCE_GLOBAL *dngbl
  280. = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_DRBG_NONCE_INDEX);
  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. if (!CRYPTO_atomic_add(&dngbl->rand_nonce_count, 1, &data.count,
  304. dngbl->rand_nonce_lock))
  305. return 0;
  306. return ossl_prov_get_nonce(drbg->provctx, pout, min_len, max_len,
  307. &data, sizeof(data));
  308. }
  309. #endif /* PROV_RAND_GET_RANDOM_NONCE */
  310. /*
  311. * Instantiate |drbg|, after it has been initialized. Use |pers| and
  312. * |perslen| as prediction-resistance input.
  313. *
  314. * Requires that drbg->lock is already locked for write, if non-null.
  315. *
  316. * Returns 1 on success, 0 on failure.
  317. */
  318. int ossl_prov_drbg_instantiate(PROV_DRBG *drbg, unsigned int strength,
  319. int prediction_resistance,
  320. const unsigned char *pers, size_t perslen)
  321. {
  322. unsigned char *nonce = NULL, *entropy = NULL;
  323. size_t noncelen = 0, entropylen = 0;
  324. size_t min_entropy, min_entropylen, max_entropylen;
  325. if (strength > drbg->strength) {
  326. ERR_raise(ERR_LIB_PROV, PROV_R_INSUFFICIENT_DRBG_STRENGTH);
  327. goto end;
  328. }
  329. min_entropy = drbg->strength;
  330. min_entropylen = drbg->min_entropylen;
  331. max_entropylen = drbg->max_entropylen;
  332. if (pers == NULL) {
  333. pers = (const unsigned char *)ossl_pers_string;
  334. perslen = sizeof(ossl_pers_string);
  335. }
  336. if (perslen > drbg->max_perslen) {
  337. ERR_raise(ERR_LIB_PROV, PROV_R_PERSONALISATION_STRING_TOO_LONG);
  338. goto end;
  339. }
  340. if (drbg->state != EVP_RAND_STATE_UNINITIALISED) {
  341. if (drbg->state == EVP_RAND_STATE_ERROR)
  342. ERR_raise(ERR_LIB_PROV, PROV_R_IN_ERROR_STATE);
  343. else
  344. ERR_raise(ERR_LIB_PROV, PROV_R_ALREADY_INSTANTIATED);
  345. goto end;
  346. }
  347. drbg->state = EVP_RAND_STATE_ERROR;
  348. if (drbg->min_noncelen > 0) {
  349. if (drbg->parent_nonce != NULL) {
  350. noncelen = drbg->parent_nonce(drbg->parent, NULL, drbg->strength,
  351. drbg->min_noncelen,
  352. drbg->max_noncelen);
  353. if (noncelen == 0) {
  354. ERR_raise(ERR_LIB_PROV, PROV_R_ERROR_RETRIEVING_NONCE);
  355. goto end;
  356. }
  357. nonce = OPENSSL_malloc(noncelen);
  358. if (nonce == NULL) {
  359. ERR_raise(ERR_LIB_PROV, PROV_R_ERROR_RETRIEVING_NONCE);
  360. goto end;
  361. }
  362. if (noncelen != drbg->parent_nonce(drbg->parent, nonce,
  363. drbg->strength,
  364. drbg->min_noncelen,
  365. drbg->max_noncelen)) {
  366. ERR_raise(ERR_LIB_PROV, PROV_R_ERROR_RETRIEVING_NONCE);
  367. goto end;
  368. }
  369. #ifndef PROV_RAND_GET_RANDOM_NONCE
  370. } else if (drbg->parent != NULL) {
  371. #endif
  372. /*
  373. * NIST SP800-90Ar1 section 9.1 says you can combine getting
  374. * the entropy and nonce in 1 call by increasing the entropy
  375. * with 50% and increasing the minimum length to accommodate
  376. * the length of the nonce. We do this in case a nonce is
  377. * required and there is no parental nonce capability.
  378. */
  379. min_entropy += drbg->strength / 2;
  380. min_entropylen += drbg->min_noncelen;
  381. max_entropylen += drbg->max_noncelen;
  382. }
  383. #ifndef PROV_RAND_GET_RANDOM_NONCE
  384. else { /* parent == NULL */
  385. noncelen = prov_drbg_get_nonce(drbg, &nonce, drbg->min_noncelen,
  386. drbg->max_noncelen);
  387. if (noncelen < drbg->min_noncelen
  388. || noncelen > drbg->max_noncelen) {
  389. ERR_raise(ERR_LIB_PROV, PROV_R_ERROR_RETRIEVING_NONCE);
  390. goto end;
  391. }
  392. }
  393. #endif
  394. }
  395. drbg->reseed_next_counter = tsan_load(&drbg->reseed_counter);
  396. if (drbg->reseed_next_counter) {
  397. drbg->reseed_next_counter++;
  398. if (!drbg->reseed_next_counter)
  399. drbg->reseed_next_counter = 1;
  400. }
  401. entropylen = get_entropy(drbg, &entropy, min_entropy,
  402. min_entropylen, max_entropylen,
  403. prediction_resistance);
  404. if (entropylen < min_entropylen
  405. || entropylen > max_entropylen) {
  406. ERR_raise(ERR_LIB_PROV, PROV_R_ERROR_RETRIEVING_ENTROPY);
  407. goto end;
  408. }
  409. if (!drbg->instantiate(drbg, entropy, entropylen, nonce, noncelen,
  410. pers, perslen)) {
  411. cleanup_entropy(drbg, entropy, entropylen);
  412. ERR_raise(ERR_LIB_PROV, PROV_R_ERROR_INSTANTIATING_DRBG);
  413. goto end;
  414. }
  415. cleanup_entropy(drbg, entropy, entropylen);
  416. drbg->state = EVP_RAND_STATE_READY;
  417. drbg->generate_counter = 1;
  418. drbg->reseed_time = time(NULL);
  419. tsan_store(&drbg->reseed_counter, drbg->reseed_next_counter);
  420. end:
  421. if (nonce != NULL)
  422. ossl_prov_cleanup_nonce(drbg->provctx, nonce, noncelen);
  423. if (drbg->state == EVP_RAND_STATE_READY)
  424. return 1;
  425. return 0;
  426. }
  427. /*
  428. * Uninstantiate |drbg|. Must be instantiated before it can be used.
  429. *
  430. * Requires that drbg->lock is already locked for write, if non-null.
  431. *
  432. * Returns 1 on success, 0 on failure.
  433. */
  434. int ossl_prov_drbg_uninstantiate(PROV_DRBG *drbg)
  435. {
  436. drbg->state = EVP_RAND_STATE_UNINITIALISED;
  437. return 1;
  438. }
  439. static int ossl_prov_drbg_reseed_unlocked(PROV_DRBG *drbg,
  440. int prediction_resistance,
  441. const unsigned char *ent,
  442. size_t ent_len,
  443. const unsigned char *adin,
  444. size_t adinlen)
  445. {
  446. unsigned char *entropy = NULL;
  447. size_t entropylen = 0;
  448. if (!ossl_prov_is_running())
  449. return 0;
  450. if (drbg->state != EVP_RAND_STATE_READY) {
  451. /* try to recover from previous errors */
  452. rand_drbg_restart(drbg);
  453. if (drbg->state == EVP_RAND_STATE_ERROR) {
  454. ERR_raise(ERR_LIB_PROV, PROV_R_IN_ERROR_STATE);
  455. return 0;
  456. }
  457. if (drbg->state == EVP_RAND_STATE_UNINITIALISED) {
  458. ERR_raise(ERR_LIB_PROV, PROV_R_NOT_INSTANTIATED);
  459. return 0;
  460. }
  461. }
  462. if (ent != NULL) {
  463. if (ent_len < drbg->min_entropylen) {
  464. ERR_raise(ERR_LIB_RAND, RAND_R_ENTROPY_OUT_OF_RANGE);
  465. drbg->state = EVP_RAND_STATE_ERROR;
  466. return 0;
  467. }
  468. if (ent_len > drbg->max_entropylen) {
  469. ERR_raise(ERR_LIB_RAND, RAND_R_ENTROPY_INPUT_TOO_LONG);
  470. drbg->state = EVP_RAND_STATE_ERROR;
  471. return 0;
  472. }
  473. }
  474. if (adin == NULL) {
  475. adinlen = 0;
  476. } else if (adinlen > drbg->max_adinlen) {
  477. ERR_raise(ERR_LIB_PROV, PROV_R_ADDITIONAL_INPUT_TOO_LONG);
  478. return 0;
  479. }
  480. drbg->state = EVP_RAND_STATE_ERROR;
  481. drbg->reseed_next_counter = tsan_load(&drbg->reseed_counter);
  482. if (drbg->reseed_next_counter) {
  483. drbg->reseed_next_counter++;
  484. if (!drbg->reseed_next_counter)
  485. drbg->reseed_next_counter = 1;
  486. }
  487. if (ent != NULL) {
  488. #ifdef FIPS_MODULE
  489. /*
  490. * NIST SP-800-90A mandates that entropy *shall not* be provided
  491. * by the consuming application. Instead the data is added as additional
  492. * input.
  493. *
  494. * (NIST SP-800-90Ar1, Sections 9.1 and 9.2)
  495. */
  496. if (!drbg->reseed(drbg, NULL, 0, ent, ent_len)) {
  497. ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_RESEED);
  498. return 0;
  499. }
  500. #else
  501. if (!drbg->reseed(drbg, ent, ent_len, adin, adinlen)) {
  502. ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_RESEED);
  503. return 0;
  504. }
  505. /* There isn't much point adding the same additional input twice */
  506. adin = NULL;
  507. adinlen = 0;
  508. #endif
  509. }
  510. /* Reseed using our sources in addition */
  511. entropylen = get_entropy(drbg, &entropy, drbg->strength,
  512. drbg->min_entropylen, drbg->max_entropylen,
  513. prediction_resistance);
  514. if (entropylen < drbg->min_entropylen
  515. || entropylen > drbg->max_entropylen) {
  516. ERR_raise(ERR_LIB_PROV, PROV_R_ERROR_RETRIEVING_ENTROPY);
  517. goto end;
  518. }
  519. if (!drbg->reseed(drbg, entropy, entropylen, adin, adinlen))
  520. goto end;
  521. drbg->state = EVP_RAND_STATE_READY;
  522. drbg->generate_counter = 1;
  523. drbg->reseed_time = time(NULL);
  524. tsan_store(&drbg->reseed_counter, drbg->reseed_next_counter);
  525. if (drbg->parent != NULL)
  526. drbg->parent_reseed_counter = get_parent_reseed_count(drbg);
  527. end:
  528. cleanup_entropy(drbg, entropy, entropylen);
  529. if (drbg->state == EVP_RAND_STATE_READY)
  530. return 1;
  531. return 0;
  532. }
  533. /*
  534. * Reseed |drbg|, mixing in the specified data
  535. *
  536. * Acquires the drbg->lock for writing, if non-null.
  537. *
  538. * Returns 1 on success, 0 on failure.
  539. */
  540. int ossl_prov_drbg_reseed(PROV_DRBG *drbg, int prediction_resistance,
  541. const unsigned char *ent, size_t ent_len,
  542. const unsigned char *adin, size_t adinlen)
  543. {
  544. int ret;
  545. if (drbg->lock != NULL && !CRYPTO_THREAD_write_lock(drbg->lock))
  546. return 0;
  547. ret = ossl_prov_drbg_reseed_unlocked(drbg, prediction_resistance, ent,
  548. ent_len, adin, adinlen);
  549. if (drbg->lock != NULL)
  550. CRYPTO_THREAD_unlock(drbg->lock);
  551. return ret;
  552. }
  553. /*
  554. * Generate |outlen| bytes into the buffer at |out|. Reseed if we need
  555. * to or if |prediction_resistance| is set. Additional input can be
  556. * sent in |adin| and |adinlen|.
  557. *
  558. * Acquires the drbg->lock for writing if available
  559. *
  560. * Returns 1 on success, 0 on failure.
  561. *
  562. */
  563. int ossl_prov_drbg_generate(PROV_DRBG *drbg, unsigned char *out, size_t outlen,
  564. unsigned int strength, int prediction_resistance,
  565. const unsigned char *adin, size_t adinlen)
  566. {
  567. int fork_id;
  568. int reseed_required = 0;
  569. int ret = 0;
  570. if (!ossl_prov_is_running())
  571. return 0;
  572. if (drbg->lock != NULL && !CRYPTO_THREAD_write_lock(drbg->lock))
  573. return 0;
  574. if (drbg->state != EVP_RAND_STATE_READY) {
  575. /* try to recover from previous errors */
  576. rand_drbg_restart(drbg);
  577. if (drbg->state == EVP_RAND_STATE_ERROR) {
  578. ERR_raise(ERR_LIB_PROV, PROV_R_IN_ERROR_STATE);
  579. goto err;
  580. }
  581. if (drbg->state == EVP_RAND_STATE_UNINITIALISED) {
  582. ERR_raise(ERR_LIB_PROV, PROV_R_NOT_INSTANTIATED);
  583. goto err;
  584. }
  585. }
  586. if (strength > drbg->strength) {
  587. ERR_raise(ERR_LIB_PROV, PROV_R_INSUFFICIENT_DRBG_STRENGTH);
  588. goto err;
  589. }
  590. if (outlen > drbg->max_request) {
  591. ERR_raise(ERR_LIB_PROV, PROV_R_REQUEST_TOO_LARGE_FOR_DRBG);
  592. goto err;
  593. }
  594. if (adinlen > drbg->max_adinlen) {
  595. ERR_raise(ERR_LIB_PROV, PROV_R_ADDITIONAL_INPUT_TOO_LONG);
  596. goto err;
  597. }
  598. fork_id = openssl_get_fork_id();
  599. if (drbg->fork_id != fork_id) {
  600. drbg->fork_id = fork_id;
  601. reseed_required = 1;
  602. }
  603. if (drbg->reseed_interval > 0) {
  604. if (drbg->generate_counter >= drbg->reseed_interval)
  605. reseed_required = 1;
  606. }
  607. if (drbg->reseed_time_interval > 0) {
  608. time_t now = time(NULL);
  609. if (now < drbg->reseed_time
  610. || now - drbg->reseed_time >= drbg->reseed_time_interval)
  611. reseed_required = 1;
  612. }
  613. if (drbg->parent != NULL
  614. && get_parent_reseed_count(drbg) != drbg->parent_reseed_counter)
  615. reseed_required = 1;
  616. if (reseed_required || prediction_resistance) {
  617. if (!ossl_prov_drbg_reseed_unlocked(drbg, prediction_resistance, NULL,
  618. 0, adin, adinlen)) {
  619. ERR_raise(ERR_LIB_PROV, PROV_R_RESEED_ERROR);
  620. goto err;
  621. }
  622. adin = NULL;
  623. adinlen = 0;
  624. }
  625. if (!drbg->generate(drbg, out, outlen, adin, adinlen)) {
  626. drbg->state = EVP_RAND_STATE_ERROR;
  627. ERR_raise(ERR_LIB_PROV, PROV_R_GENERATE_ERROR);
  628. goto err;
  629. }
  630. drbg->generate_counter++;
  631. ret = 1;
  632. err:
  633. if (drbg->lock != NULL)
  634. CRYPTO_THREAD_unlock(drbg->lock);
  635. return ret;
  636. }
  637. /*
  638. * Restart |drbg|, using the specified entropy or additional input
  639. *
  640. * Tries its best to get the drbg instantiated by all means,
  641. * regardless of its current state.
  642. *
  643. * Optionally, a |buffer| of |len| random bytes can be passed,
  644. * which is assumed to contain at least |entropy| bits of entropy.
  645. *
  646. * If |entropy| > 0, the buffer content is used as entropy input.
  647. *
  648. * If |entropy| == 0, the buffer content is used as additional input
  649. *
  650. * Returns 1 on success, 0 on failure.
  651. *
  652. * This function is used internally only.
  653. */
  654. static int rand_drbg_restart(PROV_DRBG *drbg)
  655. {
  656. /* repair error state */
  657. if (drbg->state == EVP_RAND_STATE_ERROR)
  658. drbg->uninstantiate(drbg);
  659. /* repair uninitialized state */
  660. if (drbg->state == EVP_RAND_STATE_UNINITIALISED)
  661. /* reinstantiate drbg */
  662. ossl_prov_drbg_instantiate(drbg, drbg->strength, 0, NULL, 0);
  663. return drbg->state == EVP_RAND_STATE_READY;
  664. }
  665. /* Provider support from here down */
  666. static const OSSL_DISPATCH *find_call(const OSSL_DISPATCH *dispatch,
  667. int function)
  668. {
  669. if (dispatch != NULL)
  670. while (dispatch->function_id != 0) {
  671. if (dispatch->function_id == function)
  672. return dispatch;
  673. dispatch++;
  674. }
  675. return NULL;
  676. }
  677. int ossl_drbg_enable_locking(void *vctx)
  678. {
  679. PROV_DRBG *drbg = vctx;
  680. if (drbg != NULL && drbg->lock == NULL) {
  681. if (drbg->parent_enable_locking != NULL)
  682. if (!drbg->parent_enable_locking(drbg->parent)) {
  683. ERR_raise(ERR_LIB_PROV, PROV_R_PARENT_LOCKING_NOT_ENABLED);
  684. return 0;
  685. }
  686. drbg->lock = CRYPTO_THREAD_lock_new();
  687. if (drbg->lock == NULL) {
  688. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_CREATE_LOCK);
  689. return 0;
  690. }
  691. }
  692. return 1;
  693. }
  694. /*
  695. * Allocate memory and initialize a new DRBG. The DRBG is allocated on
  696. * the secure heap if |secure| is nonzero and the secure heap is enabled.
  697. * The |parent|, if not NULL, will be used as random source for reseeding.
  698. * This also requires the parent's provider context and the parent's lock.
  699. *
  700. * Returns a pointer to the new DRBG instance on success, NULL on failure.
  701. */
  702. PROV_DRBG *ossl_rand_drbg_new
  703. (void *provctx, void *parent, const OSSL_DISPATCH *p_dispatch,
  704. int (*dnew)(PROV_DRBG *ctx),
  705. void (*dfree)(void *vctx),
  706. int (*instantiate)(PROV_DRBG *drbg,
  707. const unsigned char *entropy, size_t entropylen,
  708. const unsigned char *nonce, size_t noncelen,
  709. const unsigned char *pers, size_t perslen),
  710. int (*uninstantiate)(PROV_DRBG *ctx),
  711. int (*reseed)(PROV_DRBG *drbg, const unsigned char *ent, size_t ent_len,
  712. const unsigned char *adin, size_t adin_len),
  713. int (*generate)(PROV_DRBG *, unsigned char *out, size_t outlen,
  714. const unsigned char *adin, size_t adin_len))
  715. {
  716. PROV_DRBG *drbg;
  717. unsigned int p_str;
  718. const OSSL_DISPATCH *pfunc;
  719. if (!ossl_prov_is_running())
  720. return NULL;
  721. drbg = OPENSSL_zalloc(sizeof(*drbg));
  722. if (drbg == NULL)
  723. return NULL;
  724. drbg->provctx = provctx;
  725. drbg->instantiate = instantiate;
  726. drbg->uninstantiate = uninstantiate;
  727. drbg->reseed = reseed;
  728. drbg->generate = generate;
  729. drbg->fork_id = openssl_get_fork_id();
  730. /* Extract parent's functions */
  731. drbg->parent = parent;
  732. if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_ENABLE_LOCKING)) != NULL)
  733. drbg->parent_enable_locking = OSSL_FUNC_rand_enable_locking(pfunc);
  734. if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_LOCK)) != NULL)
  735. drbg->parent_lock = OSSL_FUNC_rand_lock(pfunc);
  736. if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_UNLOCK)) != NULL)
  737. drbg->parent_unlock = OSSL_FUNC_rand_unlock(pfunc);
  738. if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_GET_CTX_PARAMS)) != NULL)
  739. drbg->parent_get_ctx_params = OSSL_FUNC_rand_get_ctx_params(pfunc);
  740. if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_NONCE)) != NULL)
  741. drbg->parent_nonce = OSSL_FUNC_rand_nonce(pfunc);
  742. if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_GET_SEED)) != NULL)
  743. drbg->parent_get_seed = OSSL_FUNC_rand_get_seed(pfunc);
  744. if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_CLEAR_SEED)) != NULL)
  745. drbg->parent_clear_seed = OSSL_FUNC_rand_clear_seed(pfunc);
  746. /* Set some default maximums up */
  747. drbg->max_entropylen = DRBG_MAX_LENGTH;
  748. drbg->max_noncelen = DRBG_MAX_LENGTH;
  749. drbg->max_perslen = DRBG_MAX_LENGTH;
  750. drbg->max_adinlen = DRBG_MAX_LENGTH;
  751. drbg->generate_counter = 1;
  752. drbg->reseed_counter = 1;
  753. drbg->reseed_interval = RESEED_INTERVAL;
  754. drbg->reseed_time_interval = TIME_INTERVAL;
  755. if (!dnew(drbg))
  756. goto err;
  757. if (parent != NULL) {
  758. if (!get_parent_strength(drbg, &p_str))
  759. goto err;
  760. if (drbg->strength > p_str) {
  761. /*
  762. * We currently don't support the algorithm from NIST SP 800-90C
  763. * 10.1.2 to use a weaker DRBG as source
  764. */
  765. ERR_raise(ERR_LIB_PROV, PROV_R_PARENT_STRENGTH_TOO_WEAK);
  766. goto err;
  767. }
  768. }
  769. #ifdef TSAN_REQUIRES_LOCKING
  770. if (!ossl_drbg_enable_locking(drbg))
  771. goto err;
  772. #endif
  773. return drbg;
  774. err:
  775. dfree(drbg);
  776. return NULL;
  777. }
  778. void ossl_rand_drbg_free(PROV_DRBG *drbg)
  779. {
  780. if (drbg == NULL)
  781. return;
  782. CRYPTO_THREAD_lock_free(drbg->lock);
  783. OPENSSL_free(drbg);
  784. }
  785. /*
  786. * Helper function called by internal DRBG implementations. Assumes that at
  787. * least a read lock has been taken on drbg->lock
  788. */
  789. int ossl_drbg_get_ctx_params(PROV_DRBG *drbg, OSSL_PARAM params[])
  790. {
  791. OSSL_PARAM *p;
  792. p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STATE);
  793. if (p != NULL && !OSSL_PARAM_set_int(p, drbg->state))
  794. return 0;
  795. p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STRENGTH);
  796. if (p != NULL && !OSSL_PARAM_set_int(p, drbg->strength))
  797. return 0;
  798. p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MIN_ENTROPYLEN);
  799. if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->min_entropylen))
  800. return 0;
  801. p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MAX_ENTROPYLEN);
  802. if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->max_entropylen))
  803. return 0;
  804. p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MIN_NONCELEN);
  805. if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->min_noncelen))
  806. return 0;
  807. p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MAX_NONCELEN);
  808. if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->max_noncelen))
  809. return 0;
  810. p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MAX_PERSLEN);
  811. if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->max_perslen))
  812. return 0;
  813. p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MAX_ADINLEN);
  814. if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->max_adinlen))
  815. return 0;
  816. p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_RESEED_REQUESTS);
  817. if (p != NULL && !OSSL_PARAM_set_uint(p, drbg->reseed_interval))
  818. return 0;
  819. p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_RESEED_TIME);
  820. if (p != NULL && !OSSL_PARAM_set_time_t(p, drbg->reseed_time))
  821. return 0;
  822. p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL);
  823. if (p != NULL && !OSSL_PARAM_set_time_t(p, drbg->reseed_time_interval))
  824. return 0;
  825. return 1;
  826. }
  827. /*
  828. * Helper function to get certain params that require no lock to obtain. Sets
  829. * *complete to 1 if all the params were processed, or 0 otherwise
  830. */
  831. int ossl_drbg_get_ctx_params_no_lock(PROV_DRBG *drbg, OSSL_PARAM params[],
  832. int *complete)
  833. {
  834. size_t cnt = 0;
  835. OSSL_PARAM *p;
  836. /* This value never changes once set */
  837. p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_MAX_REQUEST);
  838. if (p != NULL) {
  839. if (!OSSL_PARAM_set_size_t(p, drbg->max_request))
  840. return 0;
  841. cnt++;
  842. }
  843. /*
  844. * Can be changed by multiple threads, but we tolerate inaccuracies in this
  845. * value.
  846. */
  847. p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_RESEED_COUNTER);
  848. if (p != NULL) {
  849. if (!OSSL_PARAM_set_uint(p, tsan_load(&drbg->reseed_counter)))
  850. return 0;
  851. cnt++;
  852. }
  853. if (params[cnt].key == NULL)
  854. *complete = 1;
  855. else
  856. *complete = 0;
  857. return 1;
  858. }
  859. int ossl_drbg_set_ctx_params(PROV_DRBG *drbg, const OSSL_PARAM params[])
  860. {
  861. const OSSL_PARAM *p;
  862. if (params == NULL)
  863. return 1;
  864. p = OSSL_PARAM_locate_const(params, OSSL_DRBG_PARAM_RESEED_REQUESTS);
  865. if (p != NULL && !OSSL_PARAM_get_uint(p, &drbg->reseed_interval))
  866. return 0;
  867. p = OSSL_PARAM_locate_const(params, OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL);
  868. if (p != NULL && !OSSL_PARAM_get_time_t(p, &drbg->reseed_time_interval))
  869. return 0;
  870. return 1;
  871. }
  872. /* Confirm digest is allowed to be used with a DRBG */
  873. int ossl_drbg_verify_digest(ossl_unused OSSL_LIB_CTX *libctx, const EVP_MD *md)
  874. {
  875. #ifdef FIPS_MODULE
  876. /* FIPS 140-3 IG D.R limited DRBG digests to a specific set */
  877. static const char *const allowed_digests[] = {
  878. "SHA1", /* SHA 1 allowed */
  879. "SHA2-256", "SHA2-512", /* non-truncated SHA2 allowed */
  880. "SHA3-256", "SHA3-512", /* non-truncated SHA3 allowed */
  881. };
  882. size_t i;
  883. if (FIPS_restricted_drbg_digests_enabled(libctx)) {
  884. for (i = 0; i < OSSL_NELEM(allowed_digests); i++)
  885. if (EVP_MD_is_a(md, allowed_digests[i]))
  886. return 1;
  887. ERR_raise(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED);
  888. return 0;
  889. }
  890. #endif
  891. /* Outside of FIPS, any digests that are not XOF are allowed */
  892. if ((EVP_MD_get_flags(md) & EVP_MD_FLAG_XOF) != 0) {
  893. ERR_raise(ERR_LIB_PROV, PROV_R_XOF_DIGESTS_NOT_ALLOWED);
  894. return 0;
  895. }
  896. return 1;
  897. }