Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl34/crypto/evp/digest.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2024 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
/* We need to use some engine deprecated APIs */
11
#define OPENSSL_SUPPRESS_DEPRECATED
12
13
#include <stdio.h>
14
#include <openssl/objects.h>
15
#include <openssl/evp.h>
16
#include <openssl/ec.h>
17
#ifndef FIPS_MODULE
18
#include <openssl/engine.h>
19
#endif
20
#include <openssl/params.h>
21
#include <openssl/core_names.h>
22
#include "internal/cryptlib.h"
23
#include "internal/nelem.h"
24
#include "internal/provider.h"
25
#include "internal/core.h"
26
#include "crypto/evp.h"
27
#include "evp_local.h"
28
29
static void cleanup_old_md_data(EVP_MD_CTX *ctx, int force)
30
1.28G
{
31
1.28G
    if (ctx->digest != NULL) {
32
1.26G
        if (ctx->digest->cleanup != NULL
33
0
            && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_CLEANED))
34
0
            ctx->digest->cleanup(ctx);
35
1.26G
        if (ctx->md_data != NULL && ctx->digest->ctx_size > 0
36
0
            && (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_REUSE)
37
0
                || force)) {
38
0
            OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
39
0
            ctx->md_data = NULL;
40
0
        }
41
1.26G
    }
42
1.28G
}
43
44
void evp_md_ctx_clear_digest(EVP_MD_CTX *ctx, int force, int keep_fetched)
45
27.7M
{
46
27.7M
    if (ctx->algctx != NULL) {
47
11.0M
        if (ctx->digest != NULL && ctx->digest->freectx != NULL)
48
11.0M
            ctx->digest->freectx(ctx->algctx);
49
11.0M
        ctx->algctx = NULL;
50
11.0M
        EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
51
11.0M
    }
52
53
    /* Code below to be removed when legacy support is dropped. */
54
55
    /*
56
     * Don't assume ctx->md_data was cleaned in EVP_Digest_Final, because
57
     * sometimes only copies of the context are ever finalised.
58
     */
59
27.7M
    cleanup_old_md_data(ctx, force);
60
27.7M
    if (force)
61
100k
        ctx->digest = NULL;
62
63
27.7M
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
64
27.7M
    ENGINE_finish(ctx->engine);
65
27.7M
    ctx->engine = NULL;
66
27.7M
#endif
67
68
    /* Non legacy code, this has to be later than the ctx->digest cleaning */
69
27.7M
    if (!keep_fetched) {
70
20.3M
        EVP_MD_free(ctx->fetched_digest);
71
20.3M
        ctx->fetched_digest = NULL;
72
20.3M
        ctx->reqdigest = NULL;
73
20.3M
    }
74
27.7M
}
75
76
static int evp_md_ctx_reset_ex(EVP_MD_CTX *ctx, int keep_fetched)
77
36.4M
{
78
36.4M
    if (ctx == NULL)
79
8.82M
        return 1;
80
81
    /*
82
     * pctx should be freed by the user of EVP_MD_CTX
83
     * if EVP_MD_CTX_FLAG_KEEP_PKEY_CTX is set
84
     */
85
27.6M
    if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX)) {
86
27.6M
        EVP_PKEY_CTX_free(ctx->pctx);
87
27.6M
        ctx->pctx = NULL;
88
27.6M
    }
89
90
27.6M
    evp_md_ctx_clear_digest(ctx, 0, keep_fetched);
91
27.6M
    if (!keep_fetched)
92
20.2M
        OPENSSL_cleanse(ctx, sizeof(*ctx));
93
94
27.6M
    return 1;
95
36.4M
}
96
97
/* This call frees resources associated with the context */
98
int EVP_MD_CTX_reset(EVP_MD_CTX *ctx)
99
29.0M
{
100
29.0M
    return evp_md_ctx_reset_ex(ctx, 0);
101
29.0M
}
102
103
#ifndef FIPS_MODULE
104
EVP_MD_CTX *evp_md_ctx_new_ex(EVP_PKEY *pkey, const ASN1_OCTET_STRING *id,
105
    OSSL_LIB_CTX *libctx, const char *propq)
106
15.8k
{
107
15.8k
    EVP_MD_CTX *ctx;
108
15.8k
    EVP_PKEY_CTX *pctx = NULL;
109
110
15.8k
    if ((ctx = EVP_MD_CTX_new()) == NULL
111
15.8k
        || (pctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq)) == NULL) {
112
667
        ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
113
667
        goto err;
114
667
    }
115
116
15.2k
    if (id != NULL && EVP_PKEY_CTX_set1_id(pctx, id->data, id->length) <= 0)
117
0
        goto err;
118
119
15.2k
    EVP_MD_CTX_set_pkey_ctx(ctx, pctx);
120
15.2k
    return ctx;
121
122
667
err:
123
667
    EVP_PKEY_CTX_free(pctx);
124
667
    EVP_MD_CTX_free(ctx);
125
667
    return NULL;
126
15.2k
}
127
#endif
128
129
EVP_MD_CTX *EVP_MD_CTX_new(void)
130
10.7M
{
131
10.7M
    return OPENSSL_zalloc(sizeof(EVP_MD_CTX));
132
10.7M
}
133
134
void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
135
13.1M
{
136
13.1M
    if (ctx == NULL)
137
2.39M
        return;
138
139
10.7M
    EVP_MD_CTX_reset(ctx);
140
10.7M
    OPENSSL_free(ctx);
141
10.7M
}
142
143
int evp_md_ctx_free_algctx(EVP_MD_CTX *ctx)
144
4.24M
{
145
4.24M
    if (ctx->algctx != NULL) {
146
154k
        if (!ossl_assert(ctx->digest != NULL)) {
147
0
            ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
148
0
            return 0;
149
0
        }
150
154k
        if (ctx->digest->freectx != NULL)
151
154k
            ctx->digest->freectx(ctx->algctx);
152
154k
        ctx->algctx = NULL;
153
154k
    }
154
4.24M
    return 1;
155
4.24M
}
156
157
static int evp_md_init_internal(EVP_MD_CTX *ctx, const EVP_MD *type,
158
    const OSSL_PARAM params[], ENGINE *impl)
159
420M
{
160
420M
#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
161
420M
    ENGINE *tmpimpl = NULL;
162
420M
#endif
163
164
420M
#if !defined(FIPS_MODULE)
165
420M
    if (ctx->pctx != NULL
166
0
        && EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx->pctx)
167
0
        && ctx->pctx->op.sig.algctx != NULL) {
168
        /*
169
         * Prior to OpenSSL 3.0 calling EVP_DigestInit_ex() on an mdctx
170
         * previously initialised with EVP_DigestSignInit() would retain
171
         * information about the key, and re-initialise for another sign
172
         * operation. So in that case we redirect to EVP_DigestSignInit()
173
         */
174
0
        if (ctx->pctx->operation == EVP_PKEY_OP_SIGNCTX)
175
0
            return EVP_DigestSignInit(ctx, NULL, type, impl, NULL);
176
0
        if (ctx->pctx->operation == EVP_PKEY_OP_VERIFYCTX)
177
0
            return EVP_DigestVerifyInit(ctx, NULL, type, impl, NULL);
178
0
        ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
179
0
        return 0;
180
0
    }
181
420M
#endif
182
183
420M
    EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_CLEANED | EVP_MD_CTX_FLAG_FINALISED);
184
185
420M
    if (type != NULL) {
186
3.49M
        ctx->reqdigest = type;
187
416M
    } else {
188
416M
        if (ctx->digest == NULL) {
189
0
            ERR_raise(ERR_LIB_EVP, EVP_R_NO_DIGEST_SET);
190
0
            return 0;
191
0
        }
192
416M
        type = ctx->digest;
193
416M
    }
194
195
    /* Code below to be removed when legacy support is dropped. */
196
420M
#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
197
    /*
198
     * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
199
     * this context may already have an ENGINE! Try to avoid releasing the
200
     * previous handle, re-querying for an ENGINE, and having a
201
     * reinitialisation, when it may all be unnecessary.
202
     */
203
420M
    if (ctx->engine != NULL
204
0
        && ctx->digest != NULL
205
0
        && type->type == ctx->digest->type)
206
0
        goto skip_to_init;
207
208
    /*
209
     * Ensure an ENGINE left lying around from last time is cleared (the
210
     * previous check attempted to avoid this if the same ENGINE and
211
     * EVP_MD could be used).
212
     */
213
420M
    ENGINE_finish(ctx->engine);
214
420M
    ctx->engine = NULL;
215
216
420M
    if (impl == NULL)
217
420M
        tmpimpl = ENGINE_get_digest_engine(type->type);
218
420M
#endif
219
220
    /*
221
     * If there are engines involved or EVP_MD_CTX_FLAG_NO_INIT is set then we
222
     * should use legacy handling for now.
223
     */
224
420M
    if (impl != NULL
225
420M
#if !defined(OPENSSL_NO_ENGINE)
226
420M
        || ctx->engine != NULL
227
420M
#if !defined(FIPS_MODULE)
228
420M
        || tmpimpl != NULL
229
420M
#endif
230
420M
#endif
231
420M
        || (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0
232
420M
        || (type != NULL && type->origin == EVP_ORIG_METH)
233
420M
        || (type == NULL && ctx->digest != NULL
234
0
            && ctx->digest->origin == EVP_ORIG_METH)) {
235
        /* If we were using provided hash before, cleanup algctx */
236
0
        if (!evp_md_ctx_free_algctx(ctx))
237
0
            return 0;
238
0
        if (ctx->digest == ctx->fetched_digest)
239
0
            ctx->digest = NULL;
240
0
        EVP_MD_free(ctx->fetched_digest);
241
0
        ctx->fetched_digest = NULL;
242
0
        goto legacy;
243
0
    }
244
245
420M
    cleanup_old_md_data(ctx, 1);
246
247
    /* Start of non-legacy code below */
248
420M
    if (ctx->digest == type) {
249
418M
        if (!ossl_assert(type->prov != NULL)) {
250
0
            ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
251
0
            return 0;
252
0
        }
253
418M
    } else {
254
1.67M
        if (!evp_md_ctx_free_algctx(ctx))
255
0
            return 0;
256
1.67M
    }
257
258
420M
    if (type->prov == NULL) {
259
#ifdef FIPS_MODULE
260
        /* We only do explicit fetches inside the FIPS module */
261
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
262
        return 0;
263
#else
264
        /* The NULL digest is a special case */
265
151
        EVP_MD *provmd = EVP_MD_fetch(NULL,
266
151
            type->type != NID_undef ? OBJ_nid2sn(type->type)
267
151
                                    : "NULL",
268
151
            "");
269
270
151
        if (provmd == NULL) {
271
0
            ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
272
0
            return 0;
273
0
        }
274
151
        type = provmd;
275
151
        EVP_MD_free(ctx->fetched_digest);
276
151
        ctx->fetched_digest = provmd;
277
151
#endif
278
151
    }
279
280
420M
    if (type->prov != NULL && ctx->fetched_digest != type) {
281
1.67M
        if (!EVP_MD_up_ref((EVP_MD *)type)) {
282
0
            ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
283
0
            return 0;
284
0
        }
285
1.67M
        EVP_MD_free(ctx->fetched_digest);
286
1.67M
        ctx->fetched_digest = (EVP_MD *)type;
287
1.67M
    }
288
420M
    ctx->digest = type;
289
420M
    if (ctx->algctx == NULL) {
290
1.67M
        ctx->algctx = ctx->digest->newctx(ossl_provider_ctx(type->prov));
291
1.67M
        if (ctx->algctx == NULL) {
292
0
            ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
293
0
            return 0;
294
0
        }
295
1.67M
    }
296
297
420M
    if (ctx->digest->dinit == NULL) {
298
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
299
0
        return 0;
300
0
    }
301
302
420M
    return ctx->digest->dinit(ctx->algctx, params);
303
304
    /* Code below to be removed when legacy support is dropped. */
305
0
legacy:
306
307
0
#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
308
0
    if (type) {
309
0
        if (impl != NULL) {
310
0
            if (!ENGINE_init(impl)) {
311
0
                ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
312
0
                return 0;
313
0
            }
314
0
        } else {
315
            /* Ask if an ENGINE is reserved for this job */
316
0
            impl = tmpimpl;
317
0
        }
318
0
        if (impl != NULL) {
319
            /* There's an ENGINE for this job ... (apparently) */
320
0
            const EVP_MD *d = ENGINE_get_digest(impl, type->type);
321
322
0
            if (d == NULL) {
323
0
                ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
324
0
                ENGINE_finish(impl);
325
0
                return 0;
326
0
            }
327
            /* We'll use the ENGINE's private digest definition */
328
0
            type = d;
329
            /*
330
             * Store the ENGINE functional reference so we know 'type' came
331
             * from an ENGINE and we need to release it when done.
332
             */
333
0
            ctx->engine = impl;
334
0
        } else
335
0
            ctx->engine = NULL;
336
0
    }
337
0
#endif
338
0
    if (ctx->digest != type) {
339
0
        cleanup_old_md_data(ctx, 1);
340
341
0
        ctx->digest = type;
342
0
        if (!(ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) && type->ctx_size) {
343
0
            ctx->update = type->update;
344
0
            ctx->md_data = OPENSSL_zalloc(type->ctx_size);
345
0
            if (ctx->md_data == NULL)
346
0
                return 0;
347
0
        }
348
0
    }
349
0
#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
350
0
skip_to_init:
351
0
#endif
352
0
#ifndef FIPS_MODULE
353
0
    if (ctx->pctx != NULL
354
0
        && (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx->pctx)
355
0
            || ctx->pctx->op.sig.signature == NULL)) {
356
0
        int r;
357
0
        r = EVP_PKEY_CTX_ctrl(ctx->pctx, -1, EVP_PKEY_OP_TYPE_SIG,
358
0
            EVP_PKEY_CTRL_DIGESTINIT, 0, ctx);
359
0
        if (r <= 0 && (r != -2))
360
0
            return 0;
361
0
    }
362
0
#endif
363
0
    if (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT)
364
0
        return 1;
365
0
    return ctx->digest->init(ctx);
366
0
}
367
368
int EVP_DigestInit_ex2(EVP_MD_CTX *ctx, const EVP_MD *type,
369
    const OSSL_PARAM params[])
370
1.24G
{
371
1.24G
    return evp_md_init_internal(ctx, type, params, NULL);
372
1.24G
}
373
374
int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type)
375
23
{
376
23
    EVP_MD_CTX_reset(ctx);
377
23
    return evp_md_init_internal(ctx, type, NULL, NULL);
378
23
}
379
380
int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
381
7.65M
{
382
7.65M
    return evp_md_init_internal(ctx, type, NULL, impl);
383
7.65M
}
384
385
int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t count)
386
1.54G
{
387
1.54G
    if (count == 0)
388
30.3k
        return 1;
389
390
1.54G
    if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISED) != 0) {
391
0
        ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
392
0
        return 0;
393
0
    }
394
395
1.54G
    if (ctx->pctx != NULL
396
0
        && EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx->pctx)
397
0
        && ctx->pctx->op.sig.algctx != NULL) {
398
        /*
399
         * Prior to OpenSSL 3.0 EVP_DigestSignUpdate() and
400
         * EVP_DigestVerifyUpdate() were just macros for EVP_DigestUpdate().
401
         * Some code calls EVP_DigestUpdate() directly even when initialised
402
         * with EVP_DigestSignInit_ex() or
403
         * EVP_DigestVerifyInit_ex(), so we detect that and redirect to
404
         * the correct EVP_Digest*Update() function
405
         */
406
0
        if (ctx->pctx->operation == EVP_PKEY_OP_SIGNCTX)
407
0
            return EVP_DigestSignUpdate(ctx, data, count);
408
0
        if (ctx->pctx->operation == EVP_PKEY_OP_VERIFYCTX)
409
0
            return EVP_DigestVerifyUpdate(ctx, data, count);
410
0
        ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
411
0
        return 0;
412
0
    }
413
414
1.54G
    if (ctx->digest == NULL
415
1.54G
        || ctx->digest->prov == NULL
416
1.54G
        || (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0)
417
8
        goto legacy;
418
419
1.54G
    if (ctx->digest->dupdate == NULL) {
420
0
        ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
421
0
        return 0;
422
0
    }
423
1.54G
    return ctx->digest->dupdate(ctx->algctx, data, count);
424
425
    /* Code below to be removed when legacy support is dropped. */
426
8
legacy:
427
8
    return ctx->update != NULL ? ctx->update(ctx, data, count) : 0;
428
1.54G
}
429
430
/* The caller can assume that this removes any secret data from the context */
431
int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size)
432
206k
{
433
206k
    int ret;
434
206k
    ret = EVP_DigestFinal_ex(ctx, md, size);
435
206k
    EVP_MD_CTX_reset(ctx);
436
206k
    return ret;
437
206k
}
438
439
/* The caller can assume that this removes any secret data from the context */
440
int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *isize)
441
283M
{
442
283M
    int ret, sz;
443
283M
    size_t size = 0;
444
283M
    size_t mdsize = 0;
445
446
283M
    if (ctx->digest == NULL)
447
0
        return 0;
448
449
283M
    sz = EVP_MD_CTX_get_size(ctx);
450
283M
    if (sz < 0)
451
13
        return 0;
452
283M
    mdsize = sz;
453
283M
    if (ctx->digest->prov == NULL)
454
0
        goto legacy;
455
456
283M
    if (ctx->digest->dfinal == NULL) {
457
0
        ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
458
0
        return 0;
459
0
    }
460
461
283M
    if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISED) != 0) {
462
0
        ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
463
0
        return 0;
464
0
    }
465
466
283M
    ret = ctx->digest->dfinal(ctx->algctx, md, &size, mdsize);
467
468
283M
    ctx->flags |= EVP_MD_CTX_FLAG_FINALISED;
469
470
283M
    if (isize != NULL) {
471
2.90M
        if (size <= UINT_MAX) {
472
2.90M
            *isize = (unsigned int)size;
473
2.90M
        } else {
474
0
            ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
475
0
            ret = 0;
476
0
        }
477
2.90M
    }
478
479
283M
    return ret;
480
481
    /* Code below to be removed when legacy support is dropped. */
482
0
legacy:
483
0
    OPENSSL_assert(mdsize <= EVP_MAX_MD_SIZE);
484
0
    ret = ctx->digest->final(ctx, md);
485
0
    if (isize != NULL)
486
0
        *isize = mdsize;
487
0
    if (ctx->digest->cleanup) {
488
0
        ctx->digest->cleanup(ctx);
489
0
        EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
490
0
    }
491
0
    OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
492
0
    return ret;
493
283M
}
494
495
/* This is a one shot operation */
496
int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t size)
497
137M
{
498
137M
    int ret = 0;
499
137M
    OSSL_PARAM params[2];
500
137M
    size_t i = 0;
501
502
137M
    if (ctx->digest == NULL) {
503
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_NULL_ALGORITHM);
504
0
        return 0;
505
0
    }
506
507
137M
    if (ctx->digest->prov == NULL)
508
0
        goto legacy;
509
510
137M
    if (ctx->digest->dfinal == NULL) {
511
0
        ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
512
0
        return 0;
513
0
    }
514
515
137M
    if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISED) != 0) {
516
0
        ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
517
0
        return 0;
518
0
    }
519
520
    /*
521
     * For backward compatibility we pass the XOFLEN via a param here so that
522
     * older providers can use the supplied value. Ideally we should have just
523
     * used the size passed into ctx->digest->dfinal().
524
     */
525
137M
    params[i++] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN, &size);
526
137M
    params[i++] = OSSL_PARAM_construct_end();
527
528
137M
    if (EVP_MD_CTX_set_params(ctx, params) >= 0)
529
137M
        ret = ctx->digest->dfinal(ctx->algctx, md, &size, size);
530
531
137M
    ctx->flags |= EVP_MD_CTX_FLAG_FINALISED;
532
533
137M
    return ret;
534
535
0
legacy:
536
0
    if (EVP_MD_xof(ctx->digest)
537
0
        && size <= INT_MAX
538
0
        && ctx->digest->md_ctrl(ctx, EVP_MD_CTRL_XOF_LEN, (int)size, NULL)) {
539
0
        ret = ctx->digest->final(ctx, md);
540
0
        if (ctx->digest->cleanup != NULL) {
541
0
            ctx->digest->cleanup(ctx);
542
0
            EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
543
0
        }
544
0
        OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
545
0
    } else {
546
0
        ERR_raise(ERR_LIB_EVP, EVP_R_NOT_XOF_OR_INVALID_LENGTH);
547
0
    }
548
549
0
    return ret;
550
137M
}
551
552
/* EVP_DigestSqueeze() can be called multiple times */
553
int EVP_DigestSqueeze(EVP_MD_CTX *ctx, unsigned char *md, size_t size)
554
1.82M
{
555
1.82M
    if (ctx->digest == NULL) {
556
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_NULL_ALGORITHM);
557
0
        return 0;
558
0
    }
559
560
1.82M
    if (ctx->digest->prov == NULL) {
561
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
562
0
        return 0;
563
0
    }
564
565
1.82M
    if (ctx->digest->dsqueeze == NULL) {
566
0
        ERR_raise(ERR_LIB_EVP, EVP_R_METHOD_NOT_SUPPORTED);
567
0
        return 0;
568
0
    }
569
570
1.82M
    return ctx->digest->dsqueeze(ctx->algctx, md, &size, size);
571
1.82M
}
572
573
EVP_MD_CTX *EVP_MD_CTX_dup(const EVP_MD_CTX *in)
574
0
{
575
0
    EVP_MD_CTX *out = EVP_MD_CTX_new();
576
577
0
    if (out != NULL && !EVP_MD_CTX_copy_ex(out, in)) {
578
0
        EVP_MD_CTX_free(out);
579
0
        out = NULL;
580
0
    }
581
0
    return out;
582
0
}
583
584
int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in)
585
470k
{
586
470k
    EVP_MD_CTX_reset(out);
587
470k
    return EVP_MD_CTX_copy_ex(out, in);
588
470k
}
589
590
int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in)
591
3.43M
{
592
3.43M
    int digest_change = 0;
593
3.43M
    unsigned char *tmp_buf;
594
595
3.43M
    if (in == NULL) {
596
0
        ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
597
0
        return 0;
598
0
    }
599
600
3.43M
    if (in->digest == NULL) {
601
        /* copying uninitialized digest context */
602
0
        EVP_MD_CTX_reset(out);
603
0
        if (out->fetched_digest != NULL)
604
0
            EVP_MD_free(out->fetched_digest);
605
0
        *out = *in;
606
0
        goto clone_pkey;
607
0
    }
608
609
3.43M
    if (in->digest->prov == NULL
610
3.43M
        || (in->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0)
611
0
        goto legacy;
612
613
3.43M
    if (in->digest->dupctx == NULL) {
614
0
        ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX);
615
0
        return 0;
616
0
    }
617
618
3.43M
    evp_md_ctx_reset_ex(out, 1);
619
3.43M
    digest_change = (out->fetched_digest != in->fetched_digest);
620
3.43M
    if (digest_change && out->fetched_digest != NULL)
621
0
        EVP_MD_free(out->fetched_digest);
622
3.43M
    *out = *in;
623
    /* NULL out pointers in case of error */
624
3.43M
    out->pctx = NULL;
625
3.43M
    out->algctx = NULL;
626
627
3.43M
    if (digest_change && in->fetched_digest != NULL)
628
2.50M
        EVP_MD_up_ref(in->fetched_digest);
629
630
3.43M
    if (in->algctx != NULL) {
631
3.27M
        out->algctx = in->digest->dupctx(in->algctx);
632
3.27M
        if (out->algctx == NULL) {
633
0
            ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX);
634
0
            return 0;
635
0
        }
636
3.27M
    }
637
638
3.43M
clone_pkey:
639
    /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
640
3.43M
    EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
641
3.43M
#ifndef FIPS_MODULE
642
3.43M
    if (in->pctx != NULL) {
643
158k
        out->pctx = EVP_PKEY_CTX_dup(in->pctx);
644
158k
        if (out->pctx == NULL) {
645
0
            ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX);
646
0
            EVP_MD_CTX_reset(out);
647
0
            return 0;
648
0
        }
649
158k
    }
650
3.43M
#endif
651
652
3.43M
    return 1;
653
654
    /* Code below to be removed when legacy support is dropped. */
655
0
legacy:
656
0
#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
657
    /* Make sure it's safe to copy a digest context using an ENGINE */
658
0
    if (in->engine && !ENGINE_init(in->engine)) {
659
0
        ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB);
660
0
        return 0;
661
0
    }
662
0
#endif
663
664
0
    if (out->digest == in->digest) {
665
0
        tmp_buf = out->md_data;
666
0
        EVP_MD_CTX_set_flags(out, EVP_MD_CTX_FLAG_REUSE);
667
0
    } else
668
0
        tmp_buf = NULL;
669
0
    EVP_MD_CTX_reset(out);
670
0
    memcpy(out, in, sizeof(*out));
671
672
    /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
673
0
    EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
674
675
    /* Null these variables, since they are getting fixed up
676
     * properly below.  Anything else may cause a memleak and/or
677
     * double free if any of the memory allocations below fail
678
     */
679
0
    out->md_data = NULL;
680
0
    out->pctx = NULL;
681
682
0
    if (in->md_data && out->digest->ctx_size) {
683
0
        if (tmp_buf)
684
0
            out->md_data = tmp_buf;
685
0
        else {
686
0
            out->md_data = OPENSSL_malloc(out->digest->ctx_size);
687
0
            if (out->md_data == NULL)
688
0
                return 0;
689
0
        }
690
0
        memcpy(out->md_data, in->md_data, out->digest->ctx_size);
691
0
    }
692
693
0
    out->update = in->update;
694
695
0
#ifndef FIPS_MODULE
696
0
    if (in->pctx) {
697
0
        out->pctx = EVP_PKEY_CTX_dup(in->pctx);
698
0
        if (!out->pctx) {
699
0
            EVP_MD_CTX_reset(out);
700
0
            return 0;
701
0
        }
702
0
    }
703
0
#endif
704
705
0
    if (out->digest->copy)
706
0
        return out->digest->copy(out, in);
707
708
0
    return 1;
709
0
}
710
711
int EVP_Digest(const void *data, size_t count,
712
    unsigned char *md, unsigned int *size, const EVP_MD *type,
713
    ENGINE *impl)
714
333k
{
715
333k
    EVP_MD_CTX *ctx = EVP_MD_CTX_new();
716
333k
    int ret;
717
718
333k
    if (ctx == NULL)
719
0
        return 0;
720
333k
    EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT);
721
333k
    ret = EVP_DigestInit_ex(ctx, type, impl)
722
333k
        && EVP_DigestUpdate(ctx, data, count)
723
333k
        && EVP_DigestFinal_ex(ctx, md, size);
724
333k
    EVP_MD_CTX_free(ctx);
725
726
333k
    return ret;
727
333k
}
728
729
int EVP_Q_digest(OSSL_LIB_CTX *libctx, const char *name, const char *propq,
730
    const void *data, size_t datalen,
731
    unsigned char *md, size_t *mdlen)
732
0
{
733
0
    EVP_MD *digest = EVP_MD_fetch(libctx, name, propq);
734
0
    unsigned int temp = 0;
735
0
    int ret = 0;
736
737
0
    if (digest != NULL) {
738
0
        ret = EVP_Digest(data, datalen, md, &temp, digest, NULL);
739
0
        EVP_MD_free(digest);
740
0
    }
741
0
    if (mdlen != NULL)
742
0
        *mdlen = temp;
743
0
    return ret;
744
0
}
745
746
int EVP_MD_get_params(const EVP_MD *digest, OSSL_PARAM params[])
747
0
{
748
0
    if (digest != NULL && digest->get_params != NULL)
749
0
        return digest->get_params(params);
750
0
    return 0;
751
0
}
752
753
const OSSL_PARAM *EVP_MD_gettable_params(const EVP_MD *digest)
754
0
{
755
0
    if (digest != NULL && digest->gettable_params != NULL)
756
0
        return digest->gettable_params(
757
0
            ossl_provider_ctx(EVP_MD_get0_provider(digest)));
758
0
    return NULL;
759
0
}
760
761
int EVP_MD_CTX_set_params(EVP_MD_CTX *ctx, const OSSL_PARAM params[])
762
137M
{
763
137M
    EVP_PKEY_CTX *pctx = ctx->pctx;
764
765
    /* If we have a pctx then we should try that first */
766
137M
    if (pctx != NULL
767
0
        && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
768
0
            || pctx->operation == EVP_PKEY_OP_SIGNCTX)
769
0
        && pctx->op.sig.algctx != NULL
770
0
        && pctx->op.sig.signature->set_ctx_md_params != NULL)
771
0
        return pctx->op.sig.signature->set_ctx_md_params(pctx->op.sig.algctx,
772
0
            params);
773
774
137M
    if (ctx->digest != NULL && ctx->digest->set_ctx_params != NULL)
775
137M
        return ctx->digest->set_ctx_params(ctx->algctx, params);
776
777
38
    return 0;
778
137M
}
779
780
const OSSL_PARAM *EVP_MD_settable_ctx_params(const EVP_MD *md)
781
89
{
782
89
    void *provctx;
783
784
89
    if (md != NULL && md->settable_ctx_params != NULL) {
785
75
        provctx = ossl_provider_ctx(EVP_MD_get0_provider(md));
786
75
        return md->settable_ctx_params(NULL, provctx);
787
75
    }
788
14
    return NULL;
789
89
}
790
791
const OSSL_PARAM *EVP_MD_CTX_settable_params(EVP_MD_CTX *ctx)
792
0
{
793
0
    EVP_PKEY_CTX *pctx;
794
0
    void *alg;
795
796
0
    if (ctx == NULL)
797
0
        return NULL;
798
799
    /* If we have a pctx then we should try that first */
800
0
    pctx = ctx->pctx;
801
0
    if (pctx != NULL
802
0
        && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
803
0
            || pctx->operation == EVP_PKEY_OP_SIGNCTX)
804
0
        && pctx->op.sig.algctx != NULL
805
0
        && pctx->op.sig.signature->settable_ctx_md_params != NULL)
806
0
        return pctx->op.sig.signature->settable_ctx_md_params(
807
0
            pctx->op.sig.algctx);
808
809
0
    if (ctx->digest != NULL && ctx->digest->settable_ctx_params != NULL) {
810
0
        alg = ossl_provider_ctx(EVP_MD_get0_provider(ctx->digest));
811
0
        return ctx->digest->settable_ctx_params(ctx->algctx, alg);
812
0
    }
813
814
0
    return NULL;
815
0
}
816
817
int EVP_MD_CTX_get_params(EVP_MD_CTX *ctx, OSSL_PARAM params[])
818
158k
{
819
158k
    EVP_PKEY_CTX *pctx = ctx->pctx;
820
821
    /* If we have a pctx then we should try that first */
822
158k
    if (pctx != NULL
823
0
        && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
824
0
            || pctx->operation == EVP_PKEY_OP_SIGNCTX)
825
0
        && pctx->op.sig.algctx != NULL
826
0
        && pctx->op.sig.signature->get_ctx_md_params != NULL)
827
0
        return pctx->op.sig.signature->get_ctx_md_params(pctx->op.sig.algctx,
828
0
            params);
829
830
158k
    if (ctx->digest != NULL && ctx->digest->get_ctx_params != NULL)
831
158k
        return ctx->digest->get_ctx_params(ctx->algctx, params);
832
833
0
    return 0;
834
158k
}
835
836
const OSSL_PARAM *EVP_MD_gettable_ctx_params(const EVP_MD *md)
837
0
{
838
0
    void *provctx;
839
840
0
    if (md != NULL && md->gettable_ctx_params != NULL) {
841
0
        provctx = ossl_provider_ctx(EVP_MD_get0_provider(md));
842
0
        return md->gettable_ctx_params(NULL, provctx);
843
0
    }
844
0
    return NULL;
845
0
}
846
847
const OSSL_PARAM *EVP_MD_CTX_gettable_params(EVP_MD_CTX *ctx)
848
283M
{
849
283M
    EVP_PKEY_CTX *pctx;
850
283M
    void *provctx;
851
852
283M
    if (ctx == NULL)
853
0
        return NULL;
854
855
    /* If we have a pctx then we should try that first */
856
283M
    pctx = ctx->pctx;
857
283M
    if (pctx != NULL
858
455k
        && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
859
455k
            || pctx->operation == EVP_PKEY_OP_SIGNCTX)
860
455k
        && pctx->op.sig.algctx != NULL
861
455k
        && pctx->op.sig.signature->gettable_ctx_md_params != NULL)
862
0
        return pctx->op.sig.signature->gettable_ctx_md_params(
863
0
            pctx->op.sig.algctx);
864
865
283M
    if (ctx->digest != NULL && ctx->digest->gettable_ctx_params != NULL) {
866
102k
        provctx = ossl_provider_ctx(EVP_MD_get0_provider(ctx->digest));
867
102k
        return ctx->digest->gettable_ctx_params(ctx->algctx, provctx);
868
102k
    }
869
283M
    return NULL;
870
283M
}
871
872
int EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void *p2)
873
0
{
874
0
    int ret = EVP_CTRL_RET_UNSUPPORTED;
875
0
    int set_params = 1;
876
0
    size_t sz;
877
0
    OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
878
879
0
    if (ctx == NULL) {
880
0
        ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
881
0
        return 0;
882
0
    }
883
884
0
    if (ctx->digest != NULL && ctx->digest->prov == NULL)
885
0
        goto legacy;
886
887
0
    switch (cmd) {
888
0
    case EVP_MD_CTRL_XOF_LEN:
889
0
        sz = (size_t)p1;
890
0
        params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN, &sz);
891
0
        break;
892
0
    case EVP_MD_CTRL_MICALG:
893
0
        set_params = 0;
894
0
        params[0] = OSSL_PARAM_construct_utf8_string(OSSL_DIGEST_PARAM_MICALG,
895
0
            p2, p1 ? p1 : 9999);
896
0
        break;
897
0
    case EVP_CTRL_SSL3_MASTER_SECRET:
898
0
        params[0] = OSSL_PARAM_construct_octet_string(OSSL_DIGEST_PARAM_SSL3_MS,
899
0
            p2, p1);
900
0
        break;
901
0
    default:
902
0
        goto conclude;
903
0
    }
904
905
0
    if (set_params)
906
0
        ret = EVP_MD_CTX_set_params(ctx, params);
907
0
    else
908
0
        ret = EVP_MD_CTX_get_params(ctx, params);
909
0
    goto conclude;
910
911
    /* Code below to be removed when legacy support is dropped. */
912
0
legacy:
913
0
    if (ctx->digest->md_ctrl == NULL) {
914
0
        ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_NOT_IMPLEMENTED);
915
0
        return 0;
916
0
    }
917
918
0
    ret = ctx->digest->md_ctrl(ctx, cmd, p1, p2);
919
0
conclude:
920
0
    if (ret <= 0)
921
0
        return 0;
922
0
    return ret;
923
0
}
924
925
EVP_MD *evp_md_new(void)
926
2.26k
{
927
2.26k
    EVP_MD *md = OPENSSL_zalloc(sizeof(*md));
928
929
2.26k
    if (md != NULL && !CRYPTO_NEW_REF(&md->refcnt, 1)) {
930
0
        OPENSSL_free(md);
931
0
        return NULL;
932
0
    }
933
2.26k
    return md;
934
2.26k
}
935
936
/*
937
 * FIPS module note: since internal fetches will be entirely
938
 * provider based, we know that none of its code depends on legacy
939
 * NIDs or any functionality that use them.
940
 */
941
#ifndef FIPS_MODULE
942
static void set_legacy_nid(const char *name, void *vlegacy_nid)
943
6.41k
{
944
6.41k
    int nid;
945
6.41k
    int *legacy_nid = vlegacy_nid;
946
    /*
947
     * We use lowest level function to get the associated method, because
948
     * higher level functions such as EVP_get_digestbyname() have changed
949
     * to look at providers too.
950
     */
951
6.41k
    const void *legacy_method = OBJ_NAME_get(name, OBJ_NAME_TYPE_MD_METH);
952
953
6.41k
    if (*legacy_nid == -1) /* We found a clash already */
954
0
        return;
955
956
6.41k
    if (legacy_method == NULL)
957
4.31k
        return;
958
2.09k
    nid = EVP_MD_nid(legacy_method);
959
2.09k
    if (*legacy_nid != NID_undef && *legacy_nid != nid) {
960
0
        *legacy_nid = -1;
961
0
        return;
962
0
    }
963
2.09k
    *legacy_nid = nid;
964
2.09k
}
965
#endif
966
967
static int evp_md_cache_constants(EVP_MD *md)
968
2.42k
{
969
2.42k
    int ok, xof = 0, algid_absent = 0;
970
2.42k
    size_t blksz = 0;
971
2.42k
    size_t mdsize = 0;
972
2.42k
    OSSL_PARAM params[5];
973
974
    /*
975
     * Note that these parameters are 'constants' that are only set up
976
     * during the EVP_MD_fetch(). For this reason the XOF functions set the
977
     * md_size to 0, since the output size is unknown.
978
     */
979
2.42k
    params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_BLOCK_SIZE, &blksz);
980
2.42k
    params[1] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_SIZE, &mdsize);
981
2.42k
    params[2] = OSSL_PARAM_construct_int(OSSL_DIGEST_PARAM_XOF, &xof);
982
2.42k
    params[3] = OSSL_PARAM_construct_int(OSSL_DIGEST_PARAM_ALGID_ABSENT,
983
2.42k
        &algid_absent);
984
2.42k
    params[4] = OSSL_PARAM_construct_end();
985
2.42k
    ok = evp_do_md_getparams(md, params) > 0;
986
2.42k
    if (mdsize > INT_MAX || blksz > INT_MAX)
987
0
        ok = 0;
988
2.42k
    if (ok) {
989
2.42k
        md->block_size = (int)blksz;
990
2.42k
        md->md_size = (int)mdsize;
991
2.42k
        if (xof)
992
364
            md->flags |= EVP_MD_FLAG_XOF;
993
2.42k
        if (algid_absent)
994
1.60k
            md->flags |= EVP_MD_FLAG_DIGALGID_ABSENT;
995
2.42k
    }
996
2.42k
    return ok;
997
2.42k
}
998
999
static void *evp_md_from_algorithm(int name_id,
1000
    const OSSL_ALGORITHM *algodef,
1001
    OSSL_PROVIDER *prov)
1002
729
{
1003
729
    const OSSL_DISPATCH *fns = algodef->implementation;
1004
729
    EVP_MD *md = NULL;
1005
729
    int fncnt = 0;
1006
1007
    /* EVP_MD_fetch() will set the legacy NID if available */
1008
729
    if ((md = evp_md_new()) == NULL) {
1009
0
        ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
1010
0
        return NULL;
1011
0
    }
1012
1013
729
#ifndef FIPS_MODULE
1014
729
    md->type = NID_undef;
1015
729
    if (!evp_names_do_all(prov, name_id, set_legacy_nid, &md->type)
1016
729
        || md->type == -1) {
1017
0
        ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
1018
0
        EVP_MD_free(md);
1019
0
        return NULL;
1020
0
    }
1021
729
#endif
1022
1023
729
    md->name_id = name_id;
1024
729
    if ((md->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL) {
1025
0
        EVP_MD_free(md);
1026
0
        return NULL;
1027
0
    }
1028
729
    md->description = algodef->algorithm_description;
1029
1030
7.32k
    for (; fns->function_id != 0; fns++) {
1031
6.60k
        switch (fns->function_id) {
1032
729
        case OSSL_FUNC_DIGEST_NEWCTX:
1033
729
            if (md->newctx == NULL) {
1034
729
                md->newctx = OSSL_FUNC_digest_newctx(fns);
1035
729
                fncnt++;
1036
729
            }
1037
729
            break;
1038
729
        case OSSL_FUNC_DIGEST_INIT:
1039
729
            if (md->dinit == NULL) {
1040
729
                md->dinit = OSSL_FUNC_digest_init(fns);
1041
729
                fncnt++;
1042
729
            }
1043
729
            break;
1044
729
        case OSSL_FUNC_DIGEST_UPDATE:
1045
729
            if (md->dupdate == NULL) {
1046
729
                md->dupdate = OSSL_FUNC_digest_update(fns);
1047
729
                fncnt++;
1048
729
            }
1049
729
            break;
1050
729
        case OSSL_FUNC_DIGEST_FINAL:
1051
729
            if (md->dfinal == NULL) {
1052
729
                md->dfinal = OSSL_FUNC_digest_final(fns);
1053
729
                fncnt++;
1054
729
            }
1055
729
            break;
1056
108
        case OSSL_FUNC_DIGEST_SQUEEZE:
1057
108
            if (md->dsqueeze == NULL) {
1058
108
                md->dsqueeze = OSSL_FUNC_digest_squeeze(fns);
1059
108
                fncnt++;
1060
108
            }
1061
108
            break;
1062
0
        case OSSL_FUNC_DIGEST_DIGEST:
1063
0
            if (md->digest == NULL)
1064
0
                md->digest = OSSL_FUNC_digest_digest(fns);
1065
            /* We don't increment fnct for this as it is stand alone */
1066
0
            break;
1067
729
        case OSSL_FUNC_DIGEST_FREECTX:
1068
729
            if (md->freectx == NULL) {
1069
729
                md->freectx = OSSL_FUNC_digest_freectx(fns);
1070
729
                fncnt++;
1071
729
            }
1072
729
            break;
1073
729
        case OSSL_FUNC_DIGEST_DUPCTX:
1074
729
            if (md->dupctx == NULL)
1075
729
                md->dupctx = OSSL_FUNC_digest_dupctx(fns);
1076
729
            break;
1077
729
        case OSSL_FUNC_DIGEST_GET_PARAMS:
1078
729
            if (md->get_params == NULL)
1079
729
                md->get_params = OSSL_FUNC_digest_get_params(fns);
1080
729
            break;
1081
216
        case OSSL_FUNC_DIGEST_SET_CTX_PARAMS:
1082
216
            if (md->set_ctx_params == NULL)
1083
216
                md->set_ctx_params = OSSL_FUNC_digest_set_ctx_params(fns);
1084
216
            break;
1085
114
        case OSSL_FUNC_DIGEST_GET_CTX_PARAMS:
1086
114
            if (md->get_ctx_params == NULL)
1087
114
                md->get_ctx_params = OSSL_FUNC_digest_get_ctx_params(fns);
1088
114
            break;
1089
729
        case OSSL_FUNC_DIGEST_GETTABLE_PARAMS:
1090
729
            if (md->gettable_params == NULL)
1091
729
                md->gettable_params = OSSL_FUNC_digest_gettable_params(fns);
1092
729
            break;
1093
216
        case OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS:
1094
216
            if (md->settable_ctx_params == NULL)
1095
216
                md->settable_ctx_params = OSSL_FUNC_digest_settable_ctx_params(fns);
1096
216
            break;
1097
114
        case OSSL_FUNC_DIGEST_GETTABLE_CTX_PARAMS:
1098
114
            if (md->gettable_ctx_params == NULL)
1099
114
                md->gettable_ctx_params = OSSL_FUNC_digest_gettable_ctx_params(fns);
1100
114
            break;
1101
6.60k
        }
1102
6.60k
    }
1103
729
    if ((fncnt != 0 && fncnt != 5 && fncnt != 6)
1104
729
        || (fncnt == 0 && md->digest == NULL)) {
1105
        /*
1106
         * In order to be a consistent set of functions we either need the
1107
         * whole set of init/update/final etc functions or none of them.
1108
         * The "digest" function can standalone. We at least need one way to
1109
         * generate digests.
1110
         */
1111
0
        EVP_MD_free(md);
1112
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
1113
0
        return NULL;
1114
0
    }
1115
729
    md->prov = prov;
1116
729
    if (prov != NULL)
1117
729
        ossl_provider_up_ref(prov);
1118
1119
729
    if (!evp_md_cache_constants(md)) {
1120
0
        EVP_MD_free(md);
1121
0
        ERR_raise(ERR_LIB_EVP, EVP_R_CACHE_CONSTANTS_FAILED);
1122
0
        md = NULL;
1123
0
    }
1124
1125
729
    return md;
1126
729
}
1127
1128
static int evp_md_up_ref(void *md)
1129
4.48M
{
1130
4.48M
    return EVP_MD_up_ref(md);
1131
4.48M
}
1132
1133
static void evp_md_free(void *md)
1134
7.17k
{
1135
7.17k
    EVP_MD_free(md);
1136
7.17k
}
1137
1138
EVP_MD *EVP_MD_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
1139
    const char *properties)
1140
5.63M
{
1141
5.63M
    EVP_MD *md = evp_generic_fetch(ctx, OSSL_OP_DIGEST, algorithm, properties,
1142
5.63M
        evp_md_from_algorithm, evp_md_up_ref, evp_md_free);
1143
1144
5.63M
    return md;
1145
5.63M
}
1146
1147
int EVP_MD_up_ref(EVP_MD *md)
1148
16.9M
{
1149
16.9M
    int ref = 0;
1150
1151
16.9M
    if (md->origin == EVP_ORIG_DYNAMIC)
1152
16.9M
        CRYPTO_UP_REF(&md->refcnt, &ref);
1153
16.9M
    return 1;
1154
16.9M
}
1155
1156
void EVP_MD_free(EVP_MD *md)
1157
33.1M
{
1158
33.1M
    int i;
1159
1160
33.1M
    if (md == NULL || md->origin != EVP_ORIG_DYNAMIC)
1161
16.1M
        return;
1162
1163
16.9M
    CRYPTO_DOWN_REF(&md->refcnt, &i);
1164
16.9M
    if (i > 0)
1165
16.9M
        return;
1166
1.74k
    evp_md_free_int(md);
1167
1.74k
}
1168
1169
void EVP_MD_do_all_provided(OSSL_LIB_CTX *libctx,
1170
    void (*fn)(EVP_MD *mac, void *arg),
1171
    void *arg)
1172
8
{
1173
8
    evp_generic_do_all(libctx, OSSL_OP_DIGEST,
1174
8
        (void (*)(void *, void *))fn, arg,
1175
8
        evp_md_from_algorithm, evp_md_up_ref, evp_md_free);
1176
8
}
1177
1178
typedef struct {
1179
    int md_nid;
1180
    int hmac_nid;
1181
} ossl_hmacmd_pair;
1182
1183
static const ossl_hmacmd_pair ossl_hmacmd_pairs[] = {
1184
    { NID_sha1, NID_hmacWithSHA1 },
1185
    { NID_md5, NID_hmacWithMD5 },
1186
    { NID_sha224, NID_hmacWithSHA224 },
1187
    { NID_sha256, NID_hmacWithSHA256 },
1188
    { NID_sha384, NID_hmacWithSHA384 },
1189
    { NID_sha512, NID_hmacWithSHA512 },
1190
    { NID_id_GostR3411_94, NID_id_HMACGostR3411_94 },
1191
    { NID_id_GostR3411_2012_256, NID_id_tc26_hmac_gost_3411_2012_256 },
1192
    { NID_id_GostR3411_2012_512, NID_id_tc26_hmac_gost_3411_2012_512 },
1193
    { NID_sha3_224, NID_hmac_sha3_224 },
1194
    { NID_sha3_256, NID_hmac_sha3_256 },
1195
    { NID_sha3_384, NID_hmac_sha3_384 },
1196
    { NID_sha3_512, NID_hmac_sha3_512 },
1197
    { NID_sha512_224, NID_hmacWithSHA512_224 },
1198
    { NID_sha512_256, NID_hmacWithSHA512_256 }
1199
};
1200
1201
int ossl_hmac2mdnid(int hmac_nid)
1202
0
{
1203
0
    int md_nid = NID_undef;
1204
0
    size_t i;
1205
1206
0
    for (i = 0; i < OSSL_NELEM(ossl_hmacmd_pairs); i++) {
1207
0
        if (ossl_hmacmd_pairs[i].hmac_nid == hmac_nid) {
1208
0
            md_nid = ossl_hmacmd_pairs[i].md_nid;
1209
0
            break;
1210
0
        }
1211
0
    }
1212
1213
0
    return md_nid;
1214
0
}
1215
1216
int ossl_md2hmacnid(int md_nid)
1217
0
{
1218
0
    int hmac_nid = NID_undef;
1219
0
    size_t i;
1220
1221
0
    for (i = 0; i < OSSL_NELEM(ossl_hmacmd_pairs); i++) {
1222
0
        if (ossl_hmacmd_pairs[i].md_nid == md_nid) {
1223
0
            hmac_nid = ossl_hmacmd_pairs[i].hmac_nid;
1224
0
            break;
1225
0
        }
1226
0
    }
1227
1228
0
    return hmac_nid;
1229
0
}