drbg_hash.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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 <assert.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <openssl/sha.h>
  13. #include <openssl/crypto.h>
  14. #include <openssl/err.h>
  15. #include <openssl/rand.h>
  16. #include <openssl/core_dispatch.h>
  17. #include <openssl/proverr.h>
  18. #include "internal/thread_once.h"
  19. #include "prov/providercommon.h"
  20. #include "prov/provider_ctx.h"
  21. #include "prov/provider_util.h"
  22. #include "prov/implementations.h"
  23. #include "drbg_local.h"
  24. static OSSL_FUNC_rand_newctx_fn drbg_hash_new_wrapper;
  25. static OSSL_FUNC_rand_freectx_fn drbg_hash_free;
  26. static OSSL_FUNC_rand_instantiate_fn drbg_hash_instantiate_wrapper;
  27. static OSSL_FUNC_rand_uninstantiate_fn drbg_hash_uninstantiate_wrapper;
  28. static OSSL_FUNC_rand_generate_fn drbg_hash_generate_wrapper;
  29. static OSSL_FUNC_rand_reseed_fn drbg_hash_reseed_wrapper;
  30. static OSSL_FUNC_rand_settable_ctx_params_fn drbg_hash_settable_ctx_params;
  31. static OSSL_FUNC_rand_set_ctx_params_fn drbg_hash_set_ctx_params;
  32. static OSSL_FUNC_rand_gettable_ctx_params_fn drbg_hash_gettable_ctx_params;
  33. static OSSL_FUNC_rand_get_ctx_params_fn drbg_hash_get_ctx_params;
  34. static OSSL_FUNC_rand_verify_zeroization_fn drbg_hash_verify_zeroization;
  35. static int drbg_hash_set_ctx_params_locked(void *vctx, const OSSL_PARAM params[]);
  36. /* 888 bits from SP800-90Ar1 10.1 table 2 */
  37. #define HASH_PRNG_MAX_SEEDLEN (888/8)
  38. /* 440 bits from SP800-90Ar1 10.1 table 2 */
  39. #define HASH_PRNG_SMALL_SEEDLEN (440/8)
  40. /* Determine what seedlen to use based on the block length */
  41. #define MAX_BLOCKLEN_USING_SMALL_SEEDLEN (256/8)
  42. #define INBYTE_IGNORE ((unsigned char)0xFF)
  43. typedef struct rand_drbg_hash_st {
  44. PROV_DIGEST digest;
  45. EVP_MD_CTX *ctx;
  46. size_t blocklen;
  47. unsigned char V[HASH_PRNG_MAX_SEEDLEN];
  48. unsigned char C[HASH_PRNG_MAX_SEEDLEN];
  49. /* Temporary value storage: should always exceed max digest length */
  50. unsigned char vtmp[HASH_PRNG_MAX_SEEDLEN];
  51. } PROV_DRBG_HASH;
  52. /*
  53. * SP800-90Ar1 10.3.1 Derivation function using a Hash Function (Hash_df).
  54. * The input string used is composed of:
  55. * inbyte - An optional leading byte (ignore if equal to INBYTE_IGNORE)
  56. * in - input string 1 (A Non NULL value).
  57. * in2 - optional input string (Can be NULL).
  58. * in3 - optional input string (Can be NULL).
  59. * These are concatenated as part of the DigestUpdate process.
  60. */
  61. static int hash_df(PROV_DRBG *drbg, unsigned char *out,
  62. const unsigned char inbyte,
  63. const unsigned char *in, size_t inlen,
  64. const unsigned char *in2, size_t in2len,
  65. const unsigned char *in3, size_t in3len)
  66. {
  67. PROV_DRBG_HASH *hash = (PROV_DRBG_HASH *)drbg->data;
  68. EVP_MD_CTX *ctx = hash->ctx;
  69. unsigned char *vtmp = hash->vtmp;
  70. /* tmp = counter || num_bits_returned || [inbyte] */
  71. unsigned char tmp[1 + 4 + 1];
  72. int tmp_sz = 0;
  73. size_t outlen = drbg->seedlen;
  74. size_t num_bits_returned = outlen * 8;
  75. /*
  76. * No need to check outlen size here, as the standard only ever needs
  77. * seedlen bytes which is always less than the maximum permitted.
  78. */
  79. /* (Step 3) counter = 1 (tmp[0] is the 8 bit counter) */
  80. tmp[tmp_sz++] = 1;
  81. /* tmp[1..4] is the fixed 32 bit no_of_bits_to_return */
  82. tmp[tmp_sz++] = (unsigned char)((num_bits_returned >> 24) & 0xff);
  83. tmp[tmp_sz++] = (unsigned char)((num_bits_returned >> 16) & 0xff);
  84. tmp[tmp_sz++] = (unsigned char)((num_bits_returned >> 8) & 0xff);
  85. tmp[tmp_sz++] = (unsigned char)(num_bits_returned & 0xff);
  86. /* Tack the additional input byte onto the end of tmp if it exists */
  87. if (inbyte != INBYTE_IGNORE)
  88. tmp[tmp_sz++] = inbyte;
  89. /* (Step 4) */
  90. for (;;) {
  91. /*
  92. * (Step 4.1) out = out || Hash(tmp || in || [in2] || [in3])
  93. * (where tmp = counter || num_bits_returned || [inbyte])
  94. */
  95. if (!(EVP_DigestInit_ex(ctx, ossl_prov_digest_md(&hash->digest), NULL)
  96. && EVP_DigestUpdate(ctx, tmp, tmp_sz)
  97. && EVP_DigestUpdate(ctx, in, inlen)
  98. && (in2 == NULL || EVP_DigestUpdate(ctx, in2, in2len))
  99. && (in3 == NULL || EVP_DigestUpdate(ctx, in3, in3len))))
  100. return 0;
  101. if (outlen < hash->blocklen) {
  102. if (!EVP_DigestFinal(ctx, vtmp, NULL))
  103. return 0;
  104. memcpy(out, vtmp, outlen);
  105. OPENSSL_cleanse(vtmp, hash->blocklen);
  106. break;
  107. } else if (!EVP_DigestFinal(ctx, out, NULL)) {
  108. return 0;
  109. }
  110. outlen -= hash->blocklen;
  111. if (outlen == 0)
  112. break;
  113. /* (Step 4.2) counter++ */
  114. tmp[0]++;
  115. out += hash->blocklen;
  116. }
  117. return 1;
  118. }
  119. /* Helper function that just passes 2 input parameters to hash_df() */
  120. static int hash_df1(PROV_DRBG *drbg, unsigned char *out,
  121. const unsigned char in_byte,
  122. const unsigned char *in1, size_t in1len)
  123. {
  124. return hash_df(drbg, out, in_byte, in1, in1len, NULL, 0, NULL, 0);
  125. }
  126. /*
  127. * Add 2 byte buffers together. The first elements in each buffer are the top
  128. * most bytes. The result is stored in the dst buffer.
  129. * The final carry is ignored i.e: dst = (dst + in) mod (2^seedlen_bits).
  130. * where dst size is drbg->seedlen, and inlen <= drbg->seedlen.
  131. */
  132. static int add_bytes(PROV_DRBG *drbg, unsigned char *dst,
  133. unsigned char *in, size_t inlen)
  134. {
  135. size_t i;
  136. int result;
  137. const unsigned char *add;
  138. unsigned char carry = 0, *d;
  139. assert(drbg->seedlen >= 1 && inlen >= 1 && inlen <= drbg->seedlen);
  140. d = &dst[drbg->seedlen - 1];
  141. add = &in[inlen - 1];
  142. for (i = inlen; i > 0; i--, d--, add--) {
  143. result = *d + *add + carry;
  144. carry = (unsigned char)(result >> 8);
  145. *d = (unsigned char)(result & 0xff);
  146. }
  147. if (carry != 0) {
  148. /* Add the carry to the top of the dst if inlen is not the same size */
  149. for (i = drbg->seedlen - inlen; i > 0; --i, d--) {
  150. *d += 1; /* Carry can only be 1 */
  151. if (*d != 0) /* exit if carry doesn't propagate to the next byte */
  152. break;
  153. }
  154. }
  155. return 1;
  156. }
  157. /* V = (V + Hash(inbyte || V || [additional_input]) mod (2^seedlen) */
  158. static int add_hash_to_v(PROV_DRBG *drbg, unsigned char inbyte,
  159. const unsigned char *adin, size_t adinlen)
  160. {
  161. PROV_DRBG_HASH *hash = (PROV_DRBG_HASH *)drbg->data;
  162. EVP_MD_CTX *ctx = hash->ctx;
  163. return EVP_DigestInit_ex(ctx, ossl_prov_digest_md(&hash->digest), NULL)
  164. && EVP_DigestUpdate(ctx, &inbyte, 1)
  165. && EVP_DigestUpdate(ctx, hash->V, drbg->seedlen)
  166. && (adin == NULL || EVP_DigestUpdate(ctx, adin, adinlen))
  167. && EVP_DigestFinal(ctx, hash->vtmp, NULL)
  168. && add_bytes(drbg, hash->V, hash->vtmp, hash->blocklen);
  169. }
  170. /*
  171. * The Hashgen() as listed in SP800-90Ar1 10.1.1.4 Hash_DRBG_Generate_Process.
  172. *
  173. * drbg contains the current value of V.
  174. * outlen is the requested number of bytes.
  175. * out is a buffer to return the generated bits.
  176. *
  177. * The algorithm to generate the bits is:
  178. * data = V
  179. * w = NULL
  180. * for (i = 1 to m) {
  181. * W = W || Hash(data)
  182. * data = (data + 1) mod (2^seedlen)
  183. * }
  184. * out = Leftmost(W, outlen)
  185. *
  186. * Returns zero if an error occurs otherwise it returns 1.
  187. */
  188. static int hash_gen(PROV_DRBG *drbg, unsigned char *out, size_t outlen)
  189. {
  190. PROV_DRBG_HASH *hash = (PROV_DRBG_HASH *)drbg->data;
  191. unsigned char one = 1;
  192. if (outlen == 0)
  193. return 1;
  194. memcpy(hash->vtmp, hash->V, drbg->seedlen);
  195. for (;;) {
  196. if (!EVP_DigestInit_ex(hash->ctx, ossl_prov_digest_md(&hash->digest),
  197. NULL)
  198. || !EVP_DigestUpdate(hash->ctx, hash->vtmp, drbg->seedlen))
  199. return 0;
  200. if (outlen < hash->blocklen) {
  201. if (!EVP_DigestFinal(hash->ctx, hash->vtmp, NULL))
  202. return 0;
  203. memcpy(out, hash->vtmp, outlen);
  204. return 1;
  205. } else {
  206. if (!EVP_DigestFinal(hash->ctx, out, NULL))
  207. return 0;
  208. outlen -= hash->blocklen;
  209. if (outlen == 0)
  210. break;
  211. out += hash->blocklen;
  212. }
  213. add_bytes(drbg, hash->vtmp, &one, 1);
  214. }
  215. return 1;
  216. }
  217. /*
  218. * SP800-90Ar1 10.1.1.2 Hash_DRBG_Instantiate_Process:
  219. *
  220. * ent is entropy input obtained from a randomness source of length ent_len.
  221. * nonce is a string of bytes of length nonce_len.
  222. * pstr is a personalization string received from an application. May be NULL.
  223. *
  224. * Returns zero if an error occurs otherwise it returns 1.
  225. */
  226. static int drbg_hash_instantiate(PROV_DRBG *drbg,
  227. const unsigned char *ent, size_t ent_len,
  228. const unsigned char *nonce, size_t nonce_len,
  229. const unsigned char *pstr, size_t pstr_len)
  230. {
  231. PROV_DRBG_HASH *hash = (PROV_DRBG_HASH *)drbg->data;
  232. EVP_MD_CTX_free(hash->ctx);
  233. hash->ctx = EVP_MD_CTX_new();
  234. /* (Step 1-3) V = Hash_df(entropy||nonce||pers, seedlen) */
  235. return hash->ctx != NULL
  236. && hash_df(drbg, hash->V, INBYTE_IGNORE,
  237. ent, ent_len, nonce, nonce_len, pstr, pstr_len)
  238. /* (Step 4) C = Hash_df(0x00||V, seedlen) */
  239. && hash_df1(drbg, hash->C, 0x00, hash->V, drbg->seedlen);
  240. }
  241. static int drbg_hash_instantiate_wrapper(void *vdrbg, unsigned int strength,
  242. int prediction_resistance,
  243. const unsigned char *pstr,
  244. size_t pstr_len,
  245. const OSSL_PARAM params[])
  246. {
  247. PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
  248. int ret = 0;
  249. if (drbg->lock != NULL && !CRYPTO_THREAD_write_lock(drbg->lock))
  250. return 0;
  251. if (!ossl_prov_is_running()
  252. || !drbg_hash_set_ctx_params_locked(drbg, params))
  253. goto err;
  254. ret = ossl_prov_drbg_instantiate(drbg, strength, prediction_resistance,
  255. pstr, pstr_len);
  256. err:
  257. if (drbg->lock != NULL)
  258. CRYPTO_THREAD_unlock(drbg->lock);
  259. return ret;
  260. }
  261. /*
  262. * SP800-90Ar1 10.1.1.3 Hash_DRBG_Reseed_Process:
  263. *
  264. * ent is entropy input bytes obtained from a randomness source.
  265. * addin is additional input received from an application. May be NULL.
  266. *
  267. * Returns zero if an error occurs otherwise it returns 1.
  268. */
  269. static int drbg_hash_reseed(PROV_DRBG *drbg,
  270. const unsigned char *ent, size_t ent_len,
  271. const unsigned char *adin, size_t adin_len)
  272. {
  273. PROV_DRBG_HASH *hash = (PROV_DRBG_HASH *)drbg->data;
  274. /* (Step 1-2) V = Hash_df(0x01 || V || entropy_input || additional_input) */
  275. /* V about to be updated so use C as output instead */
  276. if (!hash_df(drbg, hash->C, 0x01, hash->V, drbg->seedlen, ent, ent_len,
  277. adin, adin_len))
  278. return 0;
  279. memcpy(hash->V, hash->C, drbg->seedlen);
  280. /* (Step 4) C = Hash_df(0x00||V, seedlen) */
  281. return hash_df1(drbg, hash->C, 0x00, hash->V, drbg->seedlen);
  282. }
  283. static int drbg_hash_reseed_wrapper(void *vdrbg, int prediction_resistance,
  284. const unsigned char *ent, size_t ent_len,
  285. const unsigned char *adin, size_t adin_len)
  286. {
  287. PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
  288. return ossl_prov_drbg_reseed(drbg, prediction_resistance, ent, ent_len,
  289. adin, adin_len);
  290. }
  291. /*
  292. * SP800-90Ar1 10.1.1.4 Hash_DRBG_Generate_Process:
  293. *
  294. * Generates pseudo random bytes using the drbg.
  295. * out is a buffer to fill with outlen bytes of pseudo random data.
  296. * addin is additional input received from an application. May be NULL.
  297. *
  298. * Returns zero if an error occurs otherwise it returns 1.
  299. */
  300. static int drbg_hash_generate(PROV_DRBG *drbg,
  301. unsigned char *out, size_t outlen,
  302. const unsigned char *adin, size_t adin_len)
  303. {
  304. PROV_DRBG_HASH *hash = (PROV_DRBG_HASH *)drbg->data;
  305. unsigned char counter[4];
  306. int reseed_counter = drbg->generate_counter;
  307. counter[0] = (unsigned char)((reseed_counter >> 24) & 0xff);
  308. counter[1] = (unsigned char)((reseed_counter >> 16) & 0xff);
  309. counter[2] = (unsigned char)((reseed_counter >> 8) & 0xff);
  310. counter[3] = (unsigned char)(reseed_counter & 0xff);
  311. return hash->ctx != NULL
  312. && (adin == NULL
  313. /* (Step 2) if adin != NULL then V = V + Hash(0x02||V||adin) */
  314. || adin_len == 0
  315. || add_hash_to_v(drbg, 0x02, adin, adin_len))
  316. /* (Step 3) Hashgen(outlen, V) */
  317. && hash_gen(drbg, out, outlen)
  318. /* (Step 4/5) H = V = (V + Hash(0x03||V) mod (2^seedlen_bits) */
  319. && add_hash_to_v(drbg, 0x03, NULL, 0)
  320. /* (Step 5) V = (V + H + C + reseed_counter) mod (2^seedlen_bits) */
  321. /* V = (V + C) mod (2^seedlen_bits) */
  322. && add_bytes(drbg, hash->V, hash->C, drbg->seedlen)
  323. /* V = (V + reseed_counter) mod (2^seedlen_bits) */
  324. && add_bytes(drbg, hash->V, counter, 4);
  325. }
  326. static int drbg_hash_generate_wrapper
  327. (void *vdrbg, unsigned char *out, size_t outlen, unsigned int strength,
  328. int prediction_resistance, const unsigned char *adin, size_t adin_len)
  329. {
  330. PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
  331. return ossl_prov_drbg_generate(drbg, out, outlen, strength,
  332. prediction_resistance, adin, adin_len);
  333. }
  334. static int drbg_hash_uninstantiate(PROV_DRBG *drbg)
  335. {
  336. PROV_DRBG_HASH *hash = (PROV_DRBG_HASH *)drbg->data;
  337. OPENSSL_cleanse(hash->V, sizeof(hash->V));
  338. OPENSSL_cleanse(hash->C, sizeof(hash->C));
  339. OPENSSL_cleanse(hash->vtmp, sizeof(hash->vtmp));
  340. return ossl_prov_drbg_uninstantiate(drbg);
  341. }
  342. static int drbg_hash_uninstantiate_wrapper(void *vdrbg)
  343. {
  344. PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
  345. int ret;
  346. if (drbg->lock != NULL && !CRYPTO_THREAD_write_lock(drbg->lock))
  347. return 0;
  348. ret = drbg_hash_uninstantiate(drbg);
  349. if (drbg->lock != NULL)
  350. CRYPTO_THREAD_unlock(drbg->lock);
  351. return ret;
  352. }
  353. static int drbg_hash_verify_zeroization(void *vdrbg)
  354. {
  355. PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
  356. PROV_DRBG_HASH *hash = (PROV_DRBG_HASH *)drbg->data;
  357. int ret = 0;
  358. if (drbg->lock != NULL && !CRYPTO_THREAD_read_lock(drbg->lock))
  359. return 0;
  360. PROV_DRBG_VERIFY_ZEROIZATION(hash->V);
  361. PROV_DRBG_VERIFY_ZEROIZATION(hash->C);
  362. PROV_DRBG_VERIFY_ZEROIZATION(hash->vtmp);
  363. ret = 1;
  364. err:
  365. if (drbg->lock != NULL)
  366. CRYPTO_THREAD_unlock(drbg->lock);
  367. return ret;
  368. }
  369. static int drbg_hash_new(PROV_DRBG *ctx)
  370. {
  371. PROV_DRBG_HASH *hash;
  372. hash = OPENSSL_secure_zalloc(sizeof(*hash));
  373. if (hash == NULL)
  374. return 0;
  375. ctx->data = hash;
  376. ctx->seedlen = HASH_PRNG_MAX_SEEDLEN;
  377. ctx->max_entropylen = DRBG_MAX_LENGTH;
  378. ctx->max_noncelen = DRBG_MAX_LENGTH;
  379. ctx->max_perslen = DRBG_MAX_LENGTH;
  380. ctx->max_adinlen = DRBG_MAX_LENGTH;
  381. /* Maximum number of bits per request = 2^19 = 2^16 bytes */
  382. ctx->max_request = 1 << 16;
  383. return 1;
  384. }
  385. static void *drbg_hash_new_wrapper(void *provctx, void *parent,
  386. const OSSL_DISPATCH *parent_dispatch)
  387. {
  388. return ossl_rand_drbg_new(provctx, parent, parent_dispatch,
  389. &drbg_hash_new, &drbg_hash_free,
  390. &drbg_hash_instantiate, &drbg_hash_uninstantiate,
  391. &drbg_hash_reseed, &drbg_hash_generate);
  392. }
  393. static void drbg_hash_free(void *vdrbg)
  394. {
  395. PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
  396. PROV_DRBG_HASH *hash;
  397. if (drbg != NULL && (hash = (PROV_DRBG_HASH *)drbg->data) != NULL) {
  398. EVP_MD_CTX_free(hash->ctx);
  399. ossl_prov_digest_reset(&hash->digest);
  400. OPENSSL_secure_clear_free(hash, sizeof(*hash));
  401. }
  402. ossl_rand_drbg_free(drbg);
  403. }
  404. static int drbg_hash_get_ctx_params(void *vdrbg, OSSL_PARAM params[])
  405. {
  406. PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
  407. PROV_DRBG_HASH *hash = (PROV_DRBG_HASH *)drbg->data;
  408. const EVP_MD *md;
  409. OSSL_PARAM *p;
  410. int ret = 0, complete = 0;
  411. if (!ossl_drbg_get_ctx_params_no_lock(drbg, params, &complete))
  412. return 0;
  413. if (complete)
  414. return 1;
  415. if (drbg->lock != NULL && !CRYPTO_THREAD_read_lock(drbg->lock))
  416. return 0;
  417. p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_DIGEST);
  418. if (p != NULL) {
  419. md = ossl_prov_digest_md(&hash->digest);
  420. if (md == NULL || !OSSL_PARAM_set_utf8_string(p, EVP_MD_get0_name(md)))
  421. goto err;
  422. }
  423. ret = ossl_drbg_get_ctx_params(drbg, params);
  424. err:
  425. if (drbg->lock != NULL)
  426. CRYPTO_THREAD_unlock(drbg->lock);
  427. return ret;
  428. }
  429. static const OSSL_PARAM *drbg_hash_gettable_ctx_params(ossl_unused void *vctx,
  430. ossl_unused void *p_ctx)
  431. {
  432. static const OSSL_PARAM known_gettable_ctx_params[] = {
  433. OSSL_PARAM_utf8_string(OSSL_DRBG_PARAM_DIGEST, NULL, 0),
  434. OSSL_PARAM_DRBG_GETTABLE_CTX_COMMON,
  435. OSSL_PARAM_END
  436. };
  437. return known_gettable_ctx_params;
  438. }
  439. static int drbg_hash_set_ctx_params_locked(void *vctx, const OSSL_PARAM params[])
  440. {
  441. PROV_DRBG *ctx = (PROV_DRBG *)vctx;
  442. PROV_DRBG_HASH *hash = (PROV_DRBG_HASH *)ctx->data;
  443. OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
  444. const EVP_MD *md;
  445. int md_size;
  446. if (!ossl_prov_digest_load_from_params(&hash->digest, params, libctx))
  447. return 0;
  448. md = ossl_prov_digest_md(&hash->digest);
  449. if (md != NULL) {
  450. if (!ossl_drbg_verify_digest(libctx, md))
  451. return 0; /* Error already raised for us */
  452. /* These are taken from SP 800-90 10.1 Table 2 */
  453. md_size = EVP_MD_get_size(md);
  454. if (md_size <= 0)
  455. return 0;
  456. hash->blocklen = md_size;
  457. /* See SP800-57 Part1 Rev4 5.6.1 Table 3 */
  458. ctx->strength = 64 * (hash->blocklen >> 3);
  459. if (ctx->strength > 256)
  460. ctx->strength = 256;
  461. if (hash->blocklen > MAX_BLOCKLEN_USING_SMALL_SEEDLEN)
  462. ctx->seedlen = HASH_PRNG_MAX_SEEDLEN;
  463. else
  464. ctx->seedlen = HASH_PRNG_SMALL_SEEDLEN;
  465. ctx->min_entropylen = ctx->strength / 8;
  466. ctx->min_noncelen = ctx->min_entropylen / 2;
  467. }
  468. return ossl_drbg_set_ctx_params(ctx, params);
  469. }
  470. static int drbg_hash_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  471. {
  472. PROV_DRBG *drbg = (PROV_DRBG *)vctx;
  473. int ret;
  474. if (drbg->lock != NULL && !CRYPTO_THREAD_write_lock(drbg->lock))
  475. return 0;
  476. ret = drbg_hash_set_ctx_params_locked(vctx, params);
  477. if (drbg->lock != NULL)
  478. CRYPTO_THREAD_unlock(drbg->lock);
  479. return ret;
  480. }
  481. static const OSSL_PARAM *drbg_hash_settable_ctx_params(ossl_unused void *vctx,
  482. ossl_unused void *p_ctx)
  483. {
  484. static const OSSL_PARAM known_settable_ctx_params[] = {
  485. OSSL_PARAM_utf8_string(OSSL_DRBG_PARAM_PROPERTIES, NULL, 0),
  486. OSSL_PARAM_utf8_string(OSSL_DRBG_PARAM_DIGEST, NULL, 0),
  487. OSSL_PARAM_DRBG_SETTABLE_CTX_COMMON,
  488. OSSL_PARAM_END
  489. };
  490. return known_settable_ctx_params;
  491. }
  492. const OSSL_DISPATCH ossl_drbg_hash_functions[] = {
  493. { OSSL_FUNC_RAND_NEWCTX, (void(*)(void))drbg_hash_new_wrapper },
  494. { OSSL_FUNC_RAND_FREECTX, (void(*)(void))drbg_hash_free },
  495. { OSSL_FUNC_RAND_INSTANTIATE,
  496. (void(*)(void))drbg_hash_instantiate_wrapper },
  497. { OSSL_FUNC_RAND_UNINSTANTIATE,
  498. (void(*)(void))drbg_hash_uninstantiate_wrapper },
  499. { OSSL_FUNC_RAND_GENERATE, (void(*)(void))drbg_hash_generate_wrapper },
  500. { OSSL_FUNC_RAND_RESEED, (void(*)(void))drbg_hash_reseed_wrapper },
  501. { OSSL_FUNC_RAND_ENABLE_LOCKING, (void(*)(void))ossl_drbg_enable_locking },
  502. { OSSL_FUNC_RAND_LOCK, (void(*)(void))ossl_drbg_lock },
  503. { OSSL_FUNC_RAND_UNLOCK, (void(*)(void))ossl_drbg_unlock },
  504. { OSSL_FUNC_RAND_SETTABLE_CTX_PARAMS,
  505. (void(*)(void))drbg_hash_settable_ctx_params },
  506. { OSSL_FUNC_RAND_SET_CTX_PARAMS, (void(*)(void))drbg_hash_set_ctx_params },
  507. { OSSL_FUNC_RAND_GETTABLE_CTX_PARAMS,
  508. (void(*)(void))drbg_hash_gettable_ctx_params },
  509. { OSSL_FUNC_RAND_GET_CTX_PARAMS, (void(*)(void))drbg_hash_get_ctx_params },
  510. { OSSL_FUNC_RAND_VERIFY_ZEROIZATION,
  511. (void(*)(void))drbg_hash_verify_zeroization },
  512. { OSSL_FUNC_RAND_GET_SEED, (void(*)(void))ossl_drbg_get_seed },
  513. { OSSL_FUNC_RAND_CLEAR_SEED, (void(*)(void))ossl_drbg_clear_seed },
  514. OSSL_DISPATCH_END
  515. };