Coverage Report

Created: 2025-08-28 07:07

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