Coverage Report

Created: 2025-08-28 07:07

/src/openssl35/providers/implementations/rands/drbg_hmac.c
Line
Count
Source (jump to first uncovered line)
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 <stdlib.h>
11
#include <string.h>
12
#include <openssl/crypto.h>
13
#include <openssl/err.h>
14
#include <openssl/rand.h>
15
#include <openssl/proverr.h>
16
#include "internal/thread_once.h"
17
#include "prov/providercommon.h"
18
#include "prov/implementations.h"
19
#include "prov/provider_ctx.h"
20
#include "prov/hmac_drbg.h"
21
#include "drbg_local.h"
22
#include "crypto/evp.h"
23
#include "crypto/evp/evp_local.h"
24
#include "internal/provider.h"
25
26
static OSSL_FUNC_rand_newctx_fn drbg_hmac_new_wrapper;
27
static OSSL_FUNC_rand_freectx_fn drbg_hmac_free;
28
static OSSL_FUNC_rand_instantiate_fn drbg_hmac_instantiate_wrapper;
29
static OSSL_FUNC_rand_uninstantiate_fn drbg_hmac_uninstantiate_wrapper;
30
static OSSL_FUNC_rand_generate_fn drbg_hmac_generate_wrapper;
31
static OSSL_FUNC_rand_reseed_fn drbg_hmac_reseed_wrapper;
32
static OSSL_FUNC_rand_settable_ctx_params_fn drbg_hmac_settable_ctx_params;
33
static OSSL_FUNC_rand_set_ctx_params_fn drbg_hmac_set_ctx_params;
34
static OSSL_FUNC_rand_gettable_ctx_params_fn drbg_hmac_gettable_ctx_params;
35
static OSSL_FUNC_rand_get_ctx_params_fn drbg_hmac_get_ctx_params;
36
static OSSL_FUNC_rand_verify_zeroization_fn drbg_hmac_verify_zeroization;
37
38
static int drbg_hmac_set_ctx_params_locked(void *vctx, const OSSL_PARAM params[]);
39
40
/*
41
 * Called twice by SP800-90Ar1 10.1.2.2 HMAC_DRBG_Update_Process.
42
 *
43
 * hmac is an object that holds the input/output Key and Value (K and V).
44
 * inbyte is 0x00 on the first call and 0x01 on the second call.
45
 * in1, in2, in3 are optional inputs that can be NULL.
46
 * in1len, in2len, in3len are the lengths of the input buffers.
47
 *
48
 * The returned K,V is:
49
 *   hmac->K = HMAC(hmac->K, hmac->V || inbyte || [in1] || [in2] || [in3])
50
 *   hmac->V = HMAC(hmac->K, hmac->V)
51
 *
52
 * Returns zero if an error occurs otherwise it returns 1.
53
 */
54
static int do_hmac(PROV_DRBG_HMAC *hmac, unsigned char inbyte,
55
                   const unsigned char *in1, size_t in1len,
56
                   const unsigned char *in2, size_t in2len,
57
                   const unsigned char *in3, size_t in3len)
58
1.26k
{
59
1.26k
    EVP_MAC_CTX *ctx = hmac->ctx;
60
61
1.26k
    if (!EVP_MAC_init(ctx, hmac->K, hmac->blocklen, NULL)
62
            /* K = HMAC(K, V || inbyte || [in1] || [in2] || [in3]) */
63
1.26k
            || !EVP_MAC_update(ctx, hmac->V, hmac->blocklen)
64
1.26k
            || !EVP_MAC_update(ctx, &inbyte, 1)
65
1.26k
            || !(in1 == NULL || in1len == 0 || EVP_MAC_update(ctx, in1, in1len))
66
1.26k
            || !(in2 == NULL || in2len == 0 || EVP_MAC_update(ctx, in2, in2len))
67
1.26k
            || !(in3 == NULL || in3len == 0 || EVP_MAC_update(ctx, in3, in3len))
68
1.26k
            || !EVP_MAC_final(ctx, hmac->K, NULL, sizeof(hmac->K)))
69
18
        return 0;
70
71
   /* V = HMAC(K, V) */
72
1.24k
    return EVP_MAC_init(ctx, hmac->K, hmac->blocklen, NULL)
73
1.24k
           && EVP_MAC_update(ctx, hmac->V, hmac->blocklen)
74
1.24k
           && EVP_MAC_final(ctx, hmac->V, NULL, sizeof(hmac->V));
75
1.26k
}
76
77
/*
78
 * SP800-90Ar1 10.1.2.2 HMAC_DRBG_Update_Process
79
 *
80
 *
81
 * Updates the drbg objects Key(K) and Value(V) using the following algorithm:
82
 *   K,V = do_hmac(hmac, 0, in1, in2, in3)
83
 *   if (any input is not NULL)
84
 *     K,V = do_hmac(hmac, 1, in1, in2, in3)
85
 *
86
 * where in1, in2, in3 are optional input buffers that can be NULL.
87
 *       in1len, in2len, in3len are the lengths of the input buffers.
88
 *
89
 * Returns zero if an error occurs otherwise it returns 1.
90
 */
91
static int drbg_hmac_update(PROV_DRBG_HMAC *hmac,
92
                            const unsigned char *in1, size_t in1len,
93
                            const unsigned char *in2, size_t in2len,
94
                            const unsigned char *in3, size_t in3len)
95
792
{
96
    /* (Steps 1-2) K = HMAC(K, V||0x00||provided_data). V = HMAC(K,V) */
97
792
    if (!do_hmac(hmac, 0x00, in1, in1len, in2, in2len, in3, in3len))
98
18
        return 0;
99
    /* (Step 3) If provided_data == NULL then return (K,V) */
100
774
    if (in1len == 0 && in2len == 0 && in3len == 0)
101
301
        return 1;
102
    /* (Steps 4-5) K = HMAC(K, V||0x01||provided_data). V = HMAC(K,V) */
103
473
    return do_hmac(hmac, 0x01, in1, in1len, in2, in2len, in3, in3len);
104
774
}
105
106
/*
107
 * SP800-90Ar1 10.1.2.3 HMAC_DRBG_Instantiate_Process:
108
 *
109
 * This sets the drbg Key (K) to all zeros, and Value (V) to all 1's.
110
 * and then calls (K,V) = drbg_hmac_update() with input parameters:
111
 *   ent = entropy data (Can be NULL) of length ent_len.
112
 *   nonce = nonce data (Can be NULL) of length nonce_len.
113
 *   pstr = personalization data (Can be NULL) of length pstr_len.
114
 *
115
 * Returns zero if an error occurs otherwise it returns 1.
116
 */
117
int ossl_drbg_hmac_init(PROV_DRBG_HMAC *hmac,
118
                        const unsigned char *ent, size_t ent_len,
119
                        const unsigned char *nonce, size_t nonce_len,
120
                        const unsigned char *pstr, size_t pstr_len)
121
341
{
122
341
    if (hmac->ctx == NULL) {
123
0
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MAC);
124
0
        return 0;
125
0
    }
126
127
    /* (Step 2) Key = 0x00 00...00 */
128
341
    memset(hmac->K, 0x00, hmac->blocklen);
129
    /* (Step 3) V = 0x01 01...01 */
130
341
    memset(hmac->V, 0x01, hmac->blocklen);
131
    /* (Step 4) (K,V) = HMAC_DRBG_Update(entropy||nonce||pers string, K, V) */
132
341
    return drbg_hmac_update(hmac, ent, ent_len, nonce, nonce_len, pstr,
133
341
                            pstr_len);
134
341
}
135
static int drbg_hmac_instantiate(PROV_DRBG *drbg,
136
                                 const unsigned char *ent, size_t ent_len,
137
                                 const unsigned char *nonce, size_t nonce_len,
138
                                 const unsigned char *pstr, size_t pstr_len)
139
156
{
140
156
    return ossl_drbg_hmac_init((PROV_DRBG_HMAC *)drbg->data, ent, ent_len,
141
156
                               nonce, nonce_len, pstr, pstr_len);
142
156
}
143
144
static int drbg_hmac_instantiate_wrapper(void *vdrbg, unsigned int strength,
145
                                         int prediction_resistance,
146
                                         const unsigned char *pstr,
147
                                         size_t pstr_len,
148
                                         const OSSL_PARAM params[])
149
0
{
150
0
    PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
151
0
    int ret = 0;
152
153
0
    if (drbg->lock != NULL && !CRYPTO_THREAD_write_lock(drbg->lock))
154
0
        return 0;
155
156
0
    if (!ossl_prov_is_running()
157
0
            || !drbg_hmac_set_ctx_params_locked(drbg, params))
158
0
        goto err;
159
0
    ret = ossl_prov_drbg_instantiate(drbg, strength, prediction_resistance,
160
0
                                     pstr, pstr_len);
161
0
 err:
162
0
    if (drbg->lock != NULL)
163
0
        CRYPTO_THREAD_unlock(drbg->lock);
164
0
    return ret;
165
0
}
166
167
168
/*
169
 * SP800-90Ar1 10.1.2.4 HMAC_DRBG_Reseed_Process:
170
 *
171
 * Reseeds the drbg's Key (K) and Value (V) by calling
172
 * (K,V) = drbg_hmac_update() with the following input parameters:
173
 *   ent = entropy input data (Can be NULL) of length ent_len.
174
 *   adin = additional input data (Can be NULL) of length adin_len.
175
 *
176
 * Returns zero if an error occurs otherwise it returns 1.
177
 */
178
static int drbg_hmac_reseed(PROV_DRBG *drbg,
179
                            const unsigned char *ent, size_t ent_len,
180
                            const unsigned char *adin, size_t adin_len)
181
150
{
182
150
    PROV_DRBG_HMAC *hmac = (PROV_DRBG_HMAC *)drbg->data;
183
184
    /* (Step 2) (K,V) = HMAC_DRBG_Update(entropy||additional_input, K, V) */
185
150
    return drbg_hmac_update(hmac, ent, ent_len, adin, adin_len, NULL, 0);
186
150
}
187
188
static int drbg_hmac_reseed_wrapper(void *vdrbg, int prediction_resistance,
189
                                    const unsigned char *ent, size_t ent_len,
190
                                    const unsigned char *adin, size_t adin_len)
191
116
{
192
116
    PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
193
194
116
    return ossl_prov_drbg_reseed(drbg, prediction_resistance, ent, ent_len,
195
116
                                 adin, adin_len);
196
116
}
197
198
/*
199
 * SP800-90Ar1 10.1.2.5 HMAC_DRBG_Generate_Process:
200
 *
201
 * Generates pseudo random bytes and updates the internal K,V for the drbg.
202
 * out is a buffer to fill with outlen bytes of pseudo random data.
203
 * adin is an additional_input string of size adin_len that may be NULL.
204
 *
205
 * Returns zero if an error occurs otherwise it returns 1.
206
 */
207
int ossl_drbg_hmac_generate(PROV_DRBG_HMAC *hmac,
208
                            unsigned char *out, size_t outlen,
209
                            const unsigned char *adin, size_t adin_len)
210
323
{
211
323
    EVP_MAC_CTX *ctx = hmac->ctx;
212
323
    const unsigned char *temp = hmac->V;
213
214
    /* (Step 2) if adin != NULL then (K,V) = HMAC_DRBG_Update(adin, K, V) */
215
323
    if (adin != NULL
216
323
            && adin_len > 0
217
323
            && !drbg_hmac_update(hmac, adin, adin_len, NULL, 0, NULL, 0))
218
0
        return 0;
219
220
    /*
221
     * (Steps 3-5) temp = NULL
222
     *             while (len(temp) < outlen) {
223
     *                 V = HMAC(K, V)
224
     *                 temp = temp || V
225
     *             }
226
     */
227
23.3k
    for (;;) {
228
23.3k
        if (!EVP_MAC_init(ctx, hmac->K, hmac->blocklen, NULL)
229
23.3k
            || !EVP_MAC_update(ctx, temp, hmac->blocklen))
230
0
            return 0;
231
232
23.3k
        if (outlen > hmac->blocklen) {
233
23.0k
            if (!EVP_MAC_final(ctx, out, NULL, outlen))
234
22
                return 0;
235
23.0k
            temp = out;
236
23.0k
        } else {
237
301
            if (!EVP_MAC_final(ctx, hmac->V, NULL, sizeof(hmac->V)))
238
0
                return 0;
239
301
            memcpy(out, hmac->V, outlen);
240
301
            break;
241
301
        }
242
23.0k
        out += hmac->blocklen;
243
23.0k
        outlen -= hmac->blocklen;
244
23.0k
    }
245
    /* (Step 6) (K,V) = HMAC_DRBG_Update(adin, K, V) */
246
301
    if (!drbg_hmac_update(hmac, adin, adin_len, NULL, 0, NULL, 0))
247
0
        return 0;
248
249
301
    return 1;
250
301
}
251
252
static int drbg_hmac_generate(PROV_DRBG *drbg,
253
                              unsigned char *out, size_t outlen,
254
                              const unsigned char *adin, size_t adin_len)
255
138
{
256
138
    return ossl_drbg_hmac_generate((PROV_DRBG_HMAC *)drbg->data, out, outlen,
257
138
                                    adin, adin_len);
258
138
}
259
260
static int drbg_hmac_generate_wrapper(void *vdrbg,
261
     unsigned char *out, size_t outlen, unsigned int strength,
262
     int prediction_resistance, const unsigned char *adin, size_t adin_len)
263
156
{
264
156
    PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
265
266
156
    return ossl_prov_drbg_generate(drbg, out, outlen, strength,
267
156
                                   prediction_resistance, adin, adin_len);
268
156
}
269
270
static int drbg_hmac_uninstantiate(PROV_DRBG *drbg)
271
0
{
272
0
    PROV_DRBG_HMAC *hmac = (PROV_DRBG_HMAC *)drbg->data;
273
274
0
    OPENSSL_cleanse(hmac->K, sizeof(hmac->K));
275
0
    OPENSSL_cleanse(hmac->V, sizeof(hmac->V));
276
0
    return ossl_prov_drbg_uninstantiate(drbg);
277
0
}
278
279
static int drbg_hmac_uninstantiate_wrapper(void *vdrbg)
280
0
{
281
0
    PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
282
0
    int ret;
283
284
0
    if (drbg->lock != NULL && !CRYPTO_THREAD_write_lock(drbg->lock))
285
0
        return 0;
286
287
0
    ret = drbg_hmac_uninstantiate(drbg);
288
289
0
    if (drbg->lock != NULL)
290
0
        CRYPTO_THREAD_unlock(drbg->lock);
291
292
0
    return ret;
293
0
}
294
295
static int drbg_hmac_verify_zeroization(void *vdrbg)
296
0
{
297
0
    PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
298
0
    PROV_DRBG_HMAC *hmac = (PROV_DRBG_HMAC *)drbg->data;
299
0
    int ret = 0;
300
301
0
    if (drbg->lock != NULL && !CRYPTO_THREAD_read_lock(drbg->lock))
302
0
        return 0;
303
304
0
    PROV_DRBG_VERIFY_ZEROIZATION(hmac->K);
305
0
    PROV_DRBG_VERIFY_ZEROIZATION(hmac->V);
306
307
0
    ret = 1;
308
0
 err:
309
0
    if (drbg->lock != NULL)
310
0
        CRYPTO_THREAD_unlock(drbg->lock);
311
0
    return ret;
312
0
}
313
314
static int drbg_hmac_new(PROV_DRBG *drbg)
315
240
{
316
240
    PROV_DRBG_HMAC *hmac;
317
318
240
    hmac = OPENSSL_secure_zalloc(sizeof(*hmac));
319
240
    if (hmac == NULL)
320
0
        return 0;
321
322
240
    OSSL_FIPS_IND_INIT(drbg)
323
324
240
    drbg->data = hmac;
325
    /* See SP800-57 Part1 Rev4 5.6.1 Table 3 */
326
240
    drbg->max_entropylen = DRBG_MAX_LENGTH;
327
240
    drbg->max_noncelen = DRBG_MAX_LENGTH;
328
240
    drbg->max_perslen = DRBG_MAX_LENGTH;
329
240
    drbg->max_adinlen = DRBG_MAX_LENGTH;
330
331
    /* Maximum number of bits per request = 2^19  = 2^16 bytes */
332
240
    drbg->max_request = 1 << 16;
333
240
    return 1;
334
240
}
335
336
static void *drbg_hmac_new_wrapper(void *provctx, void *parent,
337
                                   const OSSL_DISPATCH *parent_dispatch)
338
240
{
339
240
    return ossl_rand_drbg_new(provctx, parent, parent_dispatch,
340
240
                              &drbg_hmac_new, &drbg_hmac_free,
341
240
                              &drbg_hmac_instantiate, &drbg_hmac_uninstantiate,
342
240
                              &drbg_hmac_reseed, &drbg_hmac_generate);
343
240
}
344
345
static void drbg_hmac_free(void *vdrbg)
346
240
{
347
240
    PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
348
240
    PROV_DRBG_HMAC *hmac;
349
350
240
    if (drbg != NULL && (hmac = (PROV_DRBG_HMAC *)drbg->data) != NULL) {
351
240
        EVP_MAC_CTX_free(hmac->ctx);
352
240
        ossl_prov_digest_reset(&hmac->digest);
353
240
        OPENSSL_secure_clear_free(hmac, sizeof(*hmac));
354
240
    }
355
240
    ossl_rand_drbg_free(drbg);
356
240
}
357
358
static int drbg_hmac_get_ctx_params(void *vdrbg, OSSL_PARAM params[])
359
92
{
360
92
    PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
361
92
    PROV_DRBG_HMAC *hmac = (PROV_DRBG_HMAC *)drbg->data;
362
92
    const char *name;
363
92
    const EVP_MD *md;
364
92
    OSSL_PARAM *p;
365
92
    int ret = 0, complete = 0;
366
367
92
    if (!ossl_drbg_get_ctx_params_no_lock(drbg, params, &complete))
368
0
        return 0;
369
370
92
    if (complete)
371
92
        return 1;
372
373
0
    if (drbg->lock != NULL && !CRYPTO_THREAD_read_lock(drbg->lock))
374
0
        return 0;
375
376
0
    p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MAC);
377
0
    if (p != NULL) {
378
0
        if (hmac->ctx == NULL)
379
0
            goto err;
380
0
        name = EVP_MAC_get0_name(EVP_MAC_CTX_get0_mac(hmac->ctx));
381
0
        if (!OSSL_PARAM_set_utf8_string(p, name))
382
0
            goto err;
383
0
    }
384
385
0
    p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_DIGEST);
386
0
    if (p != NULL) {
387
0
        md = ossl_prov_digest_md(&hmac->digest);
388
0
        if (md == NULL || !OSSL_PARAM_set_utf8_string(p, EVP_MD_get0_name(md)))
389
0
            goto err;
390
0
    }
391
392
0
    ret = ossl_drbg_get_ctx_params(drbg, params);
393
0
 err:
394
0
    if (drbg->lock != NULL)
395
0
        CRYPTO_THREAD_unlock(drbg->lock);
396
397
0
    return ret;
398
0
}
399
400
static const OSSL_PARAM *drbg_hmac_gettable_ctx_params(ossl_unused void *vctx,
401
                                                       ossl_unused void *p_ctx)
402
0
{
403
0
    static const OSSL_PARAM known_gettable_ctx_params[] = {
404
0
        OSSL_PARAM_utf8_string(OSSL_DRBG_PARAM_MAC, NULL, 0),
405
0
        OSSL_PARAM_utf8_string(OSSL_DRBG_PARAM_DIGEST, NULL, 0),
406
0
        OSSL_PARAM_DRBG_GETTABLE_CTX_COMMON,
407
0
        OSSL_FIPS_IND_GETTABLE_CTX_PARAM()
408
0
        OSSL_PARAM_END
409
0
    };
410
0
    return known_gettable_ctx_params;
411
0
}
412
413
static int drbg_fetch_algs_from_prov(const OSSL_PARAM params[],
414
                                     OSSL_LIB_CTX *libctx,
415
                                     EVP_MAC_CTX **macctx,
416
                                     EVP_MD **digest)
417
73
{
418
73
    OSSL_PROVIDER *prov = NULL;
419
73
    const OSSL_PARAM *p;
420
73
    EVP_MD *md = NULL;
421
73
    EVP_MAC *mac = NULL;
422
73
    int ret = 0;
423
424
73
    if (macctx == NULL || digest == NULL)
425
0
        return 0;
426
427
73
    if ((p = OSSL_PARAM_locate_const(params,
428
73
                                     OSSL_PROV_PARAM_CORE_PROV_NAME)) == NULL)
429
73
        return 0;
430
0
    if (p->data_type != OSSL_PARAM_UTF8_STRING)
431
0
        return 0;
432
0
    if ((prov = ossl_provider_find(libctx, (const char *)p->data, 1)) == NULL)
433
0
        return 0;
434
435
0
    p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_DIGEST);
436
0
    if (p) {
437
0
        if (p->data_type != OSSL_PARAM_UTF8_STRING)
438
0
            goto done;
439
440
0
        md = evp_digest_fetch_from_prov(prov, (const char *)p->data, NULL);
441
0
        if (md) {
442
0
            EVP_MD_free(*digest);
443
0
            *digest = md;
444
0
        } else {
445
0
            goto done;
446
0
        }
447
0
    }
448
449
0
    p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_MAC);
450
0
    if (p == NULL) {
451
0
        ret = 1;
452
0
        goto done;
453
0
    }
454
455
0
    if (p->data_type != OSSL_PARAM_UTF8_STRING)
456
0
        goto done;
457
458
0
    EVP_MAC_CTX_free(*macctx);
459
0
    *macctx = NULL;
460
461
0
    mac = evp_mac_fetch_from_prov(prov, (const char *)p->data, NULL);
462
0
    if (mac) {
463
0
        *macctx = EVP_MAC_CTX_new(mac);
464
        /* The context holds on to the MAC */
465
0
        EVP_MAC_free(mac);
466
0
        ret = 1;
467
0
    }
468
469
0
done:
470
0
    ossl_provider_free(prov);
471
0
    return ret;
472
0
}
473
474
static int drbg_hmac_set_ctx_params_locked(void *vctx, const OSSL_PARAM params[])
475
169
{
476
169
    PROV_DRBG *ctx = (PROV_DRBG *)vctx;
477
169
    PROV_DRBG_HMAC *hmac = (PROV_DRBG_HMAC *)ctx->data;
478
169
    OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
479
169
    EVP_MD *prov_md = NULL;
480
169
    const EVP_MD *md;
481
169
    int md_size;
482
483
169
    if (!OSSL_FIPS_IND_SET_CTX_PARAM(ctx, OSSL_FIPS_IND_SETTABLE0, params,
484
169
                                     OSSL_DRBG_PARAM_FIPS_DIGEST_CHECK))
485
0
        return 0;
486
487
    /* try to fetch mac and digest from provider */
488
169
    (void)ERR_set_mark();
489
169
    if (!drbg_fetch_algs_from_prov(params, libctx, &hmac->ctx, &prov_md)) {
490
167
        (void)ERR_pop_to_mark();
491
        /* fall back to full implementation search */
492
167
        if (!ossl_prov_digest_load_from_params(&hmac->digest, params, libctx))
493
26
            return 0;
494
495
141
        if (!ossl_prov_macctx_load_from_params(&hmac->ctx, params,
496
141
                                               NULL, NULL, NULL, libctx))
497
21
            return 0;
498
141
    } else {
499
2
        (void)ERR_clear_last_mark();
500
2
        if (prov_md)
501
2
            ossl_prov_digest_set_md(&hmac->digest, prov_md);
502
2
    }
503
504
122
    md = ossl_prov_digest_md(&hmac->digest);
505
122
    if (md != NULL && !ossl_drbg_verify_digest(ctx, libctx, md))
506
4
        return 0;   /* Error already raised for us */
507
508
118
    if (md != NULL && hmac->ctx != NULL) {
509
        /* These are taken from SP 800-90 10.1 Table 2 */
510
118
        md_size = EVP_MD_get_size(md);
511
118
        if (md_size <= 0)
512
3
            return 0;
513
115
        hmac->blocklen = (size_t)md_size;
514
        /* See SP800-57 Part1 Rev4 5.6.1 Table 3 */
515
115
        ctx->strength = 64 * (int)(hmac->blocklen >> 3);
516
115
        if (ctx->strength > 256)
517
12
            ctx->strength = 256;
518
115
        ctx->seedlen = hmac->blocklen;
519
115
        ctx->min_entropylen = ctx->strength / 8;
520
115
        ctx->min_noncelen = ctx->min_entropylen / 2;
521
115
    }
522
523
115
    return ossl_drbg_set_ctx_params(ctx, params);
524
118
}
525
526
static int drbg_hmac_set_ctx_params(void *vctx, const OSSL_PARAM params[])
527
144
{
528
144
    PROV_DRBG *drbg = (PROV_DRBG *)vctx;
529
144
    int ret;
530
531
144
    if (drbg->lock != NULL && !CRYPTO_THREAD_write_lock(drbg->lock))
532
0
        return 0;
533
534
144
    ret = drbg_hmac_set_ctx_params_locked(vctx, params);
535
536
144
    if (drbg->lock != NULL)
537
0
        CRYPTO_THREAD_unlock(drbg->lock);
538
539
144
    return ret;
540
144
}
541
542
static const OSSL_PARAM *drbg_hmac_settable_ctx_params(ossl_unused void *vctx,
543
                                                       ossl_unused void *p_ctx)
544
240
{
545
240
    static const OSSL_PARAM known_settable_ctx_params[] = {
546
240
        OSSL_PARAM_utf8_string(OSSL_DRBG_PARAM_PROPERTIES, NULL, 0),
547
240
        OSSL_PARAM_utf8_string(OSSL_DRBG_PARAM_DIGEST, NULL, 0),
548
240
        OSSL_PARAM_utf8_string(OSSL_DRBG_PARAM_MAC, NULL, 0),
549
240
        OSSL_PARAM_DRBG_SETTABLE_CTX_COMMON,
550
240
        OSSL_FIPS_IND_SETTABLE_CTX_PARAM(OSSL_DRBG_PARAM_FIPS_DIGEST_CHECK)
551
240
        OSSL_PARAM_END
552
240
    };
553
240
    return known_settable_ctx_params;
554
240
}
555
556
const OSSL_DISPATCH ossl_drbg_ossl_hmac_functions[] = {
557
    { OSSL_FUNC_RAND_NEWCTX, (void(*)(void))drbg_hmac_new_wrapper },
558
    { OSSL_FUNC_RAND_FREECTX, (void(*)(void))drbg_hmac_free },
559
    { OSSL_FUNC_RAND_INSTANTIATE,
560
      (void(*)(void))drbg_hmac_instantiate_wrapper },
561
    { OSSL_FUNC_RAND_UNINSTANTIATE,
562
      (void(*)(void))drbg_hmac_uninstantiate_wrapper },
563
    { OSSL_FUNC_RAND_GENERATE, (void(*)(void))drbg_hmac_generate_wrapper },
564
    { OSSL_FUNC_RAND_RESEED, (void(*)(void))drbg_hmac_reseed_wrapper },
565
    { OSSL_FUNC_RAND_ENABLE_LOCKING, (void(*)(void))ossl_drbg_enable_locking },
566
    { OSSL_FUNC_RAND_LOCK, (void(*)(void))ossl_drbg_lock },
567
    { OSSL_FUNC_RAND_UNLOCK, (void(*)(void))ossl_drbg_unlock },
568
    { OSSL_FUNC_RAND_SETTABLE_CTX_PARAMS,
569
      (void(*)(void))drbg_hmac_settable_ctx_params },
570
    { OSSL_FUNC_RAND_SET_CTX_PARAMS, (void(*)(void))drbg_hmac_set_ctx_params },
571
    { OSSL_FUNC_RAND_GETTABLE_CTX_PARAMS,
572
      (void(*)(void))drbg_hmac_gettable_ctx_params },
573
    { OSSL_FUNC_RAND_GET_CTX_PARAMS, (void(*)(void))drbg_hmac_get_ctx_params },
574
    { OSSL_FUNC_RAND_VERIFY_ZEROIZATION,
575
      (void(*)(void))drbg_hmac_verify_zeroization },
576
    { OSSL_FUNC_RAND_GET_SEED, (void(*)(void))ossl_drbg_get_seed },
577
    { OSSL_FUNC_RAND_CLEAR_SEED, (void(*)(void))ossl_drbg_clear_seed },
578
    OSSL_DISPATCH_END
579
};