Coverage Report

Created: 2023-06-07 07:24

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