drbg.c 30 KB

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