Coverage Report

Created: 2025-11-16 06:40

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-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/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
235
#define HASH_PRNG_MAX_SEEDLEN    (888/8)
45
46
/* 440 bits from SP800-90Ar1 10.1 table 2 */
47
76
#define HASH_PRNG_SMALL_SEEDLEN   (440/8)
48
49
/* Determine what seedlen to use based on the block length */
50
114
#define MAX_BLOCKLEN_USING_SMALL_SEEDLEN (256/8)
51
887
#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
720
{
78
720
    PROV_DRBG_HASH *hash = (PROV_DRBG_HASH *)drbg->data;
79
720
    EVP_MD_CTX *ctx = hash->ctx;
80
720
    unsigned char *vtmp = hash->vtmp;
81
    /* tmp = counter || num_bits_returned || [inbyte] */
82
720
    unsigned char tmp[1 + 4 + 1];
83
720
    int tmp_sz = 0;
84
720
    size_t outlen = drbg->seedlen;
85
720
    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
720
    tmp[tmp_sz++] = 1;
93
    /* tmp[1..4] is the fixed 32 bit no_of_bits_to_return */
94
720
    tmp[tmp_sz++] = (unsigned char)((num_bits_returned >> 24) & 0xff);
95
720
    tmp[tmp_sz++] = (unsigned char)((num_bits_returned >> 16) & 0xff);
96
720
    tmp[tmp_sz++] = (unsigned char)((num_bits_returned >> 8) & 0xff);
97
720
    tmp[tmp_sz++] = (unsigned char)(num_bits_returned & 0xff);
98
    /* Tack the additional input byte onto the end of tmp if it exists */
99
720
    if (inbyte != INBYTE_IGNORE)
100
553
        tmp[tmp_sz++] = inbyte;
101
102
    /* (Step 4) */
103
2.12k
    for (;;) {
104
        /*
105
         * (Step 4.1) out = out || Hash(tmp || in || [in2] || [in3])
106
         *            (where tmp = counter || num_bits_returned || [inbyte])
107
         */
108
2.12k
        if (!(EVP_DigestInit_ex(ctx, ossl_prov_digest_md(&hash->digest), NULL)
109
2.12k
                && EVP_DigestUpdate(ctx, tmp, tmp_sz)
110
2.12k
                && EVP_DigestUpdate(ctx, in, inlen)
111
2.12k
                && (in2 == NULL || EVP_DigestUpdate(ctx, in2, in2len))
112
2.12k
                && (in3 == NULL || EVP_DigestUpdate(ctx, in3, in3len))))
113
0
            return 0;
114
115
2.12k
        if (outlen < hash->blocklen) {
116
720
            if (!EVP_DigestFinal(ctx, vtmp, NULL))
117
0
                return 0;
118
720
            memcpy(out, vtmp, outlen);
119
720
            OPENSSL_cleanse(vtmp, hash->blocklen);
120
720
            break;
121
1.40k
        } else if (!EVP_DigestFinal(ctx, out, NULL)) {
122
0
            return 0;
123
0
        }
124
125
1.40k
        outlen -= hash->blocklen;
126
1.40k
        if (outlen == 0)
127
0
            break;
128
        /* (Step 4.2) counter++ */
129
1.40k
        tmp[0]++;
130
1.40k
        out += hash->blocklen;
131
1.40k
    }
132
720
    return 1;
133
720
}
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
360
{
140
360
    return hash_df(drbg, out, in_byte, in1, in1len, NULL, 0, NULL, 0);
141
360
}
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
26.9k
{
152
26.9k
    size_t i;
153
26.9k
    int result;
154
26.9k
    const unsigned char *add;
155
26.9k
    unsigned char carry = 0, *d;
156
157
26.9k
    assert(drbg->seedlen >= 1 && inlen >= 1 && inlen <= drbg->seedlen);
158
159
26.9k
    d = &dst[drbg->seedlen - 1];
160
26.9k
    add = &in[inlen - 1];
161
162
71.5k
    for (i = inlen; i > 0; i--, d--, add--) {
163
44.6k
        result = *d + *add + carry;
164
44.6k
        carry = (unsigned char)(result >> 8);
165
44.6k
        *d = (unsigned char)(result & 0xff);
166
44.6k
    }
167
168
26.9k
    if (carry != 0) {
169
        /* Add the carry to the top of the dst if inlen is not the same size */
170
273
        for (i = drbg->seedlen - inlen; i > 0; --i, d--) {
171
194
            *d += 1;     /* Carry can only be 1 */
172
194
            if (*d != 0) /* exit if carry doesn't propagate to the next byte */
173
191
                break;
174
194
        }
175
270
    }
176
26.9k
    return 1;
177
26.9k
}
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
167
{
183
167
    PROV_DRBG_HASH *hash = (PROV_DRBG_HASH *)drbg->data;
184
167
    EVP_MD_CTX *ctx = hash->ctx;
185
186
167
    return EVP_DigestInit_ex(ctx, ossl_prov_digest_md(&hash->digest), NULL)
187
167
           && EVP_DigestUpdate(ctx, &inbyte, 1)
188
167
           && EVP_DigestUpdate(ctx, hash->V, drbg->seedlen)
189
167
           && (adin == NULL || EVP_DigestUpdate(ctx, adin, adinlen))
190
167
           && EVP_DigestFinal(ctx, hash->vtmp, NULL)
191
167
           && add_bytes(drbg, hash->V, hash->vtmp, hash->blocklen);
192
167
}
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
167
{
214
167
    PROV_DRBG_HASH *hash = (PROV_DRBG_HASH *)drbg->data;
215
167
    unsigned char one = 1;
216
217
167
    if (outlen == 0)
218
0
        return 1;
219
167
    memcpy(hash->vtmp, hash->V, drbg->seedlen);
220
26.5k
    for (;;) {
221
26.5k
        if (!EVP_DigestInit_ex(hash->ctx, ossl_prov_digest_md(&hash->digest),
222
26.5k
                               NULL)
223
26.5k
                || !EVP_DigestUpdate(hash->ctx, hash->vtmp, drbg->seedlen))
224
0
            return 0;
225
226
26.5k
        if (outlen < hash->blocklen) {
227
85
            if (!EVP_DigestFinal(hash->ctx, hash->vtmp, NULL))
228
0
                return 0;
229
85
            memcpy(out, hash->vtmp, outlen);
230
85
            return 1;
231
26.4k
        } else {
232
26.4k
            if (!EVP_DigestFinal(hash->ctx, out, NULL))
233
0
                return 0;
234
26.4k
            outlen -= hash->blocklen;
235
26.4k
            if (outlen == 0)
236
82
                break;
237
26.4k
            out += hash->blocklen;
238
26.4k
        }
239
26.4k
        add_bytes(drbg, hash->vtmp, &one, 1);
240
26.4k
    }
241
82
    return 1;
242
167
}
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
167
{
258
167
    PROV_DRBG_HASH *hash = (PROV_DRBG_HASH *)drbg->data;
259
260
167
    EVP_MD_CTX_free(hash->ctx);
261
167
    hash->ctx = EVP_MD_CTX_new();
262
263
    /* (Step 1-3) V = Hash_df(entropy||nonce||pers, seedlen) */
264
167
    return hash->ctx != NULL
265
167
           && hash_df(drbg, hash->V, INBYTE_IGNORE,
266
167
                      ent, ent_len, nonce, nonce_len, pstr, pstr_len)
267
           /* (Step 4) C = Hash_df(0x00||V, seedlen) */
268
167
           && hash_df1(drbg, hash->C, 0x00, hash->V, drbg->seedlen);
269
167
}
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
193
{
306
193
    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
193
    if (!hash_df(drbg, hash->C, 0x01, hash->V, drbg->seedlen, ent, ent_len,
311
193
                 adin, adin_len))
312
0
        return 0;
313
193
    memcpy(hash->V, hash->C, drbg->seedlen);
314
    /* (Step 4) C = Hash_df(0x00||V, seedlen) */
315
193
    return hash_df1(drbg, hash->C, 0x00, hash->V, drbg->seedlen);
316
193
}
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
167
{
322
167
    PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
323
324
167
    return ossl_prov_drbg_reseed(drbg, prediction_resistance, ent, ent_len,
325
167
                                 adin, adin_len);
326
167
}
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
167
{
341
167
    PROV_DRBG_HASH *hash = (PROV_DRBG_HASH *)drbg->data;
342
167
    unsigned char counter[4];
343
167
    int reseed_counter = drbg->generate_counter;
344
345
167
    counter[0] = (unsigned char)((reseed_counter >> 24) & 0xff);
346
167
    counter[1] = (unsigned char)((reseed_counter >> 16) & 0xff);
347
167
    counter[2] = (unsigned char)((reseed_counter >> 8) & 0xff);
348
167
    counter[3] = (unsigned char)(reseed_counter & 0xff);
349
350
167
    return hash->ctx != NULL
351
167
           && (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
167
           && hash_gen(drbg, out, outlen)
357
           /* (Step 4/5) H = V = (V + Hash(0x03||V) mod (2^seedlen_bits) */
358
167
           && 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
167
           && add_bytes(drbg, hash->V, hash->C, drbg->seedlen)
362
           /* V = (V + reseed_counter) mod (2^seedlen_bits) */
363
167
           && add_bytes(drbg, hash->V, counter, 4);
364
167
}
365
366
static int drbg_hash_generate_wrapper
367
    (void *vdrbg, unsigned char *out, size_t outlen, unsigned int strength,
368
     int prediction_resistance, const unsigned char *adin, size_t adin_len)
369
167
{
370
167
    PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
371
372
167
    return ossl_prov_drbg_generate(drbg, out, outlen, strength,
373
167
                                   prediction_resistance, adin, adin_len);
374
167
}
375
376
static int drbg_hash_uninstantiate(PROV_DRBG *drbg)
377
0
{
378
0
    PROV_DRBG_HASH *hash = (PROV_DRBG_HASH *)drbg->data;
379
380
0
    OPENSSL_cleanse(hash->V, sizeof(hash->V));
381
0
    OPENSSL_cleanse(hash->C, sizeof(hash->C));
382
0
    OPENSSL_cleanse(hash->vtmp, sizeof(hash->vtmp));
383
0
    return ossl_prov_drbg_uninstantiate(drbg);
384
0
}
385
386
static int drbg_hash_uninstantiate_wrapper(void *vdrbg)
387
0
{
388
0
    PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
389
0
    int ret;
390
391
0
    if (drbg->lock != NULL && !CRYPTO_THREAD_write_lock(drbg->lock))
392
0
        return 0;
393
394
0
    ret = drbg_hash_uninstantiate(drbg);
395
396
0
    if (drbg->lock != NULL)
397
0
        CRYPTO_THREAD_unlock(drbg->lock);
398
399
0
    return ret;
400
0
}
401
402
static int drbg_hash_verify_zeroization(void *vdrbg)
403
0
{
404
0
    PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
405
0
    PROV_DRBG_HASH *hash = (PROV_DRBG_HASH *)drbg->data;
406
0
    int ret = 0;
407
408
0
    if (drbg->lock != NULL && !CRYPTO_THREAD_read_lock(drbg->lock))
409
0
        return 0;
410
411
0
    PROV_DRBG_VERIFY_ZEROIZATION(hash->V);
412
0
    PROV_DRBG_VERIFY_ZEROIZATION(hash->C);
413
0
    PROV_DRBG_VERIFY_ZEROIZATION(hash->vtmp);
414
415
0
    ret = 1;
416
0
 err:
417
0
    if (drbg->lock != NULL)
418
0
        CRYPTO_THREAD_unlock(drbg->lock);
419
0
    return ret;
420
0
}
421
422
static int drbg_hash_new(PROV_DRBG *ctx)
423
197
{
424
197
    PROV_DRBG_HASH *hash;
425
426
197
    hash = OPENSSL_secure_zalloc(sizeof(*hash));
427
197
    if (hash == NULL)
428
0
        return 0;
429
430
197
    OSSL_FIPS_IND_INIT(ctx)
431
432
197
    ctx->data = hash;
433
197
    ctx->seedlen = HASH_PRNG_MAX_SEEDLEN;
434
197
    ctx->max_entropylen = DRBG_MAX_LENGTH;
435
197
    ctx->max_noncelen = DRBG_MAX_LENGTH;
436
197
    ctx->max_perslen = DRBG_MAX_LENGTH;
437
197
    ctx->max_adinlen = DRBG_MAX_LENGTH;
438
439
    /* Maximum number of bits per request = 2^19  = 2^16 bytes */
440
197
    ctx->max_request = 1 << 16;
441
197
    return 1;
442
197
}
443
444
static void *drbg_hash_new_wrapper(void *provctx, void *parent,
445
                                   const OSSL_DISPATCH *parent_dispatch)
446
197
{
447
197
    return ossl_rand_drbg_new(provctx, parent, parent_dispatch,
448
197
                              &drbg_hash_new, &drbg_hash_free,
449
197
                              &drbg_hash_instantiate, &drbg_hash_uninstantiate,
450
197
                              &drbg_hash_reseed, &drbg_hash_generate);
451
197
}
452
453
static void drbg_hash_free(void *vdrbg)
454
197
{
455
197
    PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
456
197
    PROV_DRBG_HASH *hash;
457
458
197
    if (drbg != NULL && (hash = (PROV_DRBG_HASH *)drbg->data) != NULL) {
459
197
        EVP_MD_CTX_free(hash->ctx);
460
197
        ossl_prov_digest_reset(&hash->digest);
461
197
        OPENSSL_secure_clear_free(hash, sizeof(*hash));
462
197
    }
463
197
    ossl_rand_drbg_free(drbg);
464
197
}
465
466
static int drbg_hash_get_ctx_params(void *vdrbg, OSSL_PARAM params[])
467
102
{
468
102
    PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
469
102
    PROV_DRBG_HASH *hash = (PROV_DRBG_HASH *)drbg->data;
470
102
    const EVP_MD *md;
471
102
    OSSL_PARAM *p;
472
102
    int ret = 0, complete = 0;
473
474
102
    if (!ossl_drbg_get_ctx_params_no_lock(drbg, params, &complete))
475
0
        return 0;
476
477
102
    if (complete)
478
102
        return 1;
479
480
0
    if (drbg->lock != NULL && !CRYPTO_THREAD_read_lock(drbg->lock))
481
0
        return 0;
482
483
0
    p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_DIGEST);
484
0
    if (p != NULL) {
485
0
        md = ossl_prov_digest_md(&hash->digest);
486
0
        if (md == NULL || !OSSL_PARAM_set_utf8_string(p, EVP_MD_get0_name(md)))
487
0
            goto err;
488
0
    }
489
490
0
    ret = ossl_drbg_get_ctx_params(drbg, params);
491
0
 err:
492
0
    if (drbg->lock != NULL)
493
0
        CRYPTO_THREAD_unlock(drbg->lock);
494
495
0
    return ret;
496
0
}
497
498
static const OSSL_PARAM *drbg_hash_gettable_ctx_params(ossl_unused void *vctx,
499
                                                       ossl_unused void *p_ctx)
500
0
{
501
0
    static const OSSL_PARAM known_gettable_ctx_params[] = {
502
0
        OSSL_PARAM_utf8_string(OSSL_DRBG_PARAM_DIGEST, NULL, 0),
503
0
        OSSL_PARAM_DRBG_GETTABLE_CTX_COMMON,
504
0
        OSSL_FIPS_IND_GETTABLE_CTX_PARAM()
505
0
        OSSL_PARAM_END
506
0
    };
507
0
    return known_gettable_ctx_params;
508
0
}
509
510
static int drbg_fetch_digest_from_prov(const OSSL_PARAM params[],
511
                                       OSSL_LIB_CTX *libctx,
512
                                       EVP_MD **digest)
513
56
{
514
56
    OSSL_PROVIDER *prov = NULL;
515
56
    const OSSL_PARAM *p;
516
56
    EVP_MD *md = NULL;
517
56
    int ret = 0;
518
519
56
    if (digest == NULL)
520
0
        return 0;
521
522
56
    if ((p = OSSL_PARAM_locate_const(params,
523
56
                                     OSSL_PROV_PARAM_CORE_PROV_NAME)) == NULL)
524
56
        return 0;
525
0
    if (p->data_type != OSSL_PARAM_UTF8_STRING)
526
0
        return 0;
527
0
    if ((prov = ossl_provider_find(libctx, (const char *)p->data, 1)) == NULL)
528
0
        return 0;
529
530
0
    p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_DIGEST);
531
0
    if (p == NULL) {
532
0
        ret = 1;
533
0
        goto done;
534
0
    }
535
536
0
    if (p->data_type != OSSL_PARAM_UTF8_STRING)
537
0
        goto done;
538
539
0
    md = evp_digest_fetch_from_prov(prov, (const char *)p->data, NULL);
540
0
    if (md) {
541
0
        EVP_MD_free(*digest);
542
0
        *digest = md;
543
0
        ret = 1;
544
0
    }
545
546
0
done:
547
0
    ossl_provider_free(prov);
548
0
    return ret;
549
0
}
550
551
static int drbg_hash_set_ctx_params_locked(void *vctx, const OSSL_PARAM params[])
552
133
{
553
133
    PROV_DRBG *ctx = (PROV_DRBG *)vctx;
554
133
    PROV_DRBG_HASH *hash = (PROV_DRBG_HASH *)ctx->data;
555
133
    OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
556
133
    EVP_MD *prov_md = NULL;
557
133
    const EVP_MD *md;
558
133
    int md_size;
559
560
133
    if (!OSSL_FIPS_IND_SET_CTX_PARAM(ctx, OSSL_FIPS_IND_SETTABLE0, params,
561
133
                                     OSSL_DRBG_PARAM_FIPS_DIGEST_CHECK))
562
0
        return 0;
563
564
    /* try to fetch digest from provider */
565
133
    (void)ERR_set_mark();
566
133
    if (!drbg_fetch_digest_from_prov(params, libctx, &prov_md)) {
567
132
        (void)ERR_pop_to_mark();
568
        /* fall back to full implementation search */
569
132
        if (!ossl_prov_digest_load_from_params(&hash->digest, params, libctx))
570
15
            return 0;
571
132
    } else {
572
1
        (void)ERR_clear_last_mark();
573
1
        if (prov_md)
574
1
            ossl_prov_digest_set_md(&hash->digest, prov_md);
575
1
    }
576
577
118
    md = ossl_prov_digest_md(&hash->digest);
578
118
    if (md != NULL) {
579
118
        if (!ossl_drbg_verify_digest(ctx, libctx, md))
580
2
            return 0;   /* Error already raised for us */
581
582
        /* These are taken from SP 800-90 10.1 Table 2 */
583
116
        md_size = EVP_MD_get_size(md);
584
116
        if (md_size <= 0)
585
2
            return 0;
586
114
        hash->blocklen = md_size;
587
        /* See SP800-57 Part1 Rev4 5.6.1 Table 3 */
588
114
        ctx->strength = 64 * (hash->blocklen >> 3);
589
114
        if (ctx->strength > 256)
590
27
            ctx->strength = 256;
591
114
        if (hash->blocklen > MAX_BLOCKLEN_USING_SMALL_SEEDLEN)
592
38
            ctx->seedlen = HASH_PRNG_MAX_SEEDLEN;
593
76
        else
594
76
            ctx->seedlen = HASH_PRNG_SMALL_SEEDLEN;
595
596
114
        ctx->min_entropylen = ctx->strength / 8;
597
114
        ctx->min_noncelen = ctx->min_entropylen / 2;
598
114
    }
599
600
114
    return ossl_drbg_set_ctx_params(ctx, params);
601
118
}
602
603
static int drbg_hash_set_ctx_params(void *vctx, const OSSL_PARAM params[])
604
120
{
605
120
    PROV_DRBG *drbg = (PROV_DRBG *)vctx;
606
120
    int ret;
607
608
120
    if (drbg->lock != NULL && !CRYPTO_THREAD_write_lock(drbg->lock))
609
0
        return 0;
610
611
120
    ret = drbg_hash_set_ctx_params_locked(vctx, params);
612
613
120
    if (drbg->lock != NULL)
614
0
        CRYPTO_THREAD_unlock(drbg->lock);
615
616
120
    return ret;
617
120
}
618
619
static const OSSL_PARAM *drbg_hash_settable_ctx_params(ossl_unused void *vctx,
620
                                                       ossl_unused void *p_ctx)
621
197
{
622
197
    static const OSSL_PARAM known_settable_ctx_params[] = {
623
197
        OSSL_PARAM_utf8_string(OSSL_DRBG_PARAM_PROPERTIES, NULL, 0),
624
197
        OSSL_PARAM_utf8_string(OSSL_DRBG_PARAM_DIGEST, NULL, 0),
625
197
        OSSL_PARAM_DRBG_SETTABLE_CTX_COMMON,
626
197
        OSSL_FIPS_IND_SETTABLE_CTX_PARAM(OSSL_DRBG_PARAM_FIPS_DIGEST_CHECK)
627
        OSSL_PARAM_END
628
197
    };
629
197
    return known_settable_ctx_params;
630
197
}
631
632
const OSSL_DISPATCH ossl_drbg_hash_functions[] = {
633
    { OSSL_FUNC_RAND_NEWCTX, (void(*)(void))drbg_hash_new_wrapper },
634
    { OSSL_FUNC_RAND_FREECTX, (void(*)(void))drbg_hash_free },
635
    { OSSL_FUNC_RAND_INSTANTIATE,
636
      (void(*)(void))drbg_hash_instantiate_wrapper },
637
    { OSSL_FUNC_RAND_UNINSTANTIATE,
638
      (void(*)(void))drbg_hash_uninstantiate_wrapper },
639
    { OSSL_FUNC_RAND_GENERATE, (void(*)(void))drbg_hash_generate_wrapper },
640
    { OSSL_FUNC_RAND_RESEED, (void(*)(void))drbg_hash_reseed_wrapper },
641
    { OSSL_FUNC_RAND_ENABLE_LOCKING, (void(*)(void))ossl_drbg_enable_locking },
642
    { OSSL_FUNC_RAND_LOCK, (void(*)(void))ossl_drbg_lock },
643
    { OSSL_FUNC_RAND_UNLOCK, (void(*)(void))ossl_drbg_unlock },
644
    { OSSL_FUNC_RAND_SETTABLE_CTX_PARAMS,
645
      (void(*)(void))drbg_hash_settable_ctx_params },
646
    { OSSL_FUNC_RAND_SET_CTX_PARAMS, (void(*)(void))drbg_hash_set_ctx_params },
647
    { OSSL_FUNC_RAND_GETTABLE_CTX_PARAMS,
648
      (void(*)(void))drbg_hash_gettable_ctx_params },
649
    { OSSL_FUNC_RAND_GET_CTX_PARAMS, (void(*)(void))drbg_hash_get_ctx_params },
650
    { OSSL_FUNC_RAND_VERIFY_ZEROIZATION,
651
      (void(*)(void))drbg_hash_verify_zeroization },
652
    { OSSL_FUNC_RAND_GET_SEED, (void(*)(void))ossl_drbg_get_seed },
653
    { OSSL_FUNC_RAND_CLEAR_SEED, (void(*)(void))ossl_drbg_clear_seed },
654
    OSSL_DISPATCH_END
655
};