Coverage Report

Created: 2025-06-13 06:58

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