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