Coverage Report

Created: 2026-10-28 06:28

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