Coverage Report

Created: 2025-08-28 07:07

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