Coverage Report

Created: 2026-02-22 06:11

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