Coverage Report

Created: 2026-05-24 07:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl34/crypto/evp/digest.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2026 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
876M
{
31
876M
    if (ctx->digest != NULL) {
32
857M
        if (ctx->digest->cleanup != NULL
33
0
            && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_CLEANED))
34
0
            ctx->digest->cleanup(ctx);
35
857M
        if (ctx->md_data != NULL && ctx->digest->ctx_size > 0
36
0
            && (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_REUSE)
37
0
                || force)) {
38
0
            OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
39
0
            ctx->md_data = NULL;
40
0
        }
41
857M
    }
42
876M
}
43
44
void evp_md_ctx_clear_digest(EVP_MD_CTX *ctx, int force, int keep_fetched)
45
34.2M
{
46
34.2M
    if (ctx->algctx != NULL) {
47
13.1M
        if (ctx->digest != NULL && ctx->digest->freectx != NULL)
48
13.1M
            ctx->digest->freectx(ctx->algctx);
49
13.1M
        ctx->algctx = NULL;
50
13.1M
        EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
51
13.1M
    }
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
34.2M
    cleanup_old_md_data(ctx, force);
60
34.2M
    if (force)
61
109k
        ctx->digest = NULL;
62
63
34.2M
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
64
34.2M
    ENGINE_finish(ctx->engine);
65
34.2M
    ctx->engine = NULL;
66
34.2M
#endif
67
68
    /* Non legacy code, this has to be later than the ctx->digest cleaning */
69
34.2M
    if (!keep_fetched) {
70
24.7M
        EVP_MD_free(ctx->fetched_digest);
71
24.7M
        ctx->fetched_digest = NULL;
72
24.7M
        ctx->reqdigest = NULL;
73
24.7M
    }
74
34.2M
}
75
76
static int evp_md_ctx_reset_ex(EVP_MD_CTX *ctx, int keep_fetched)
77
44.7M
{
78
44.7M
    if (ctx == NULL)
79
10.6M
        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
34.0M
    if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX)) {
86
34.0M
        EVP_PKEY_CTX_free(ctx->pctx);
87
34.0M
        ctx->pctx = NULL;
88
34.0M
    }
89
90
34.0M
    evp_md_ctx_clear_digest(ctx, 0, keep_fetched);
91
34.0M
    if (!keep_fetched)
92
24.5M
        OPENSSL_cleanse(ctx, sizeof(*ctx));
93
94
34.0M
    return 1;
95
44.7M
}
96
97
/* This call frees resources associated with the context */
98
int EVP_MD_CTX_reset(EVP_MD_CTX *ctx)
99
35.2M
{
100
35.2M
    return evp_md_ctx_reset_ex(ctx, 0);
101
35.2M
}
102
103
#ifndef FIPS_MODULE
104
EVP_MD_CTX *evp_md_ctx_new_ex(EVP_PKEY *pkey, const ASN1_OCTET_STRING *id,
105
    OSSL_LIB_CTX *libctx, const char *propq)
106
15.9k
{
107
15.9k
    EVP_MD_CTX *ctx;
108
15.9k
    EVP_PKEY_CTX *pctx = NULL;
109
110
15.9k
    if ((ctx = EVP_MD_CTX_new()) == NULL
111
15.9k
        || (pctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq)) == NULL) {
112
700
        ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
113
700
        goto err;
114
700
    }
115
116
15.2k
    if (id != NULL && EVP_PKEY_CTX_set1_id(pctx, id->data, id->length) <= 0)
117
0
        goto err;
118
119
15.2k
    EVP_MD_CTX_set_pkey_ctx(ctx, pctx);
120
15.2k
    return ctx;
121
122
700
err:
123
700
    EVP_PKEY_CTX_free(pctx);
124
700
    EVP_MD_CTX_free(ctx);
125
700
    return NULL;
126
15.2k
}
127
#endif
128
129
EVP_MD_CTX *EVP_MD_CTX_new(void)
130
12.9M
{
131
12.9M
    return OPENSSL_zalloc(sizeof(EVP_MD_CTX));
132
12.9M
}
133
134
void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
135
15.5M
{
136
15.5M
    if (ctx == NULL)
137
2.57M
        return;
138
139
12.9M
    EVP_MD_CTX_reset(ctx);
140
12.9M
    OPENSSL_free(ctx);
141
12.9M
}
142
143
int evp_md_ctx_free_algctx(EVP_MD_CTX *ctx)
144
4.52M
{
145
4.52M
    if (ctx->algctx != NULL) {
146
159k
        if (!ossl_assert(ctx->digest != NULL)) {
147
0
            ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
148
0
            return 0;
149
0
        }
150
159k
        if (ctx->digest->freectx != NULL)
151
159k
            ctx->digest->freectx(ctx->algctx);
152
159k
        ctx->algctx = NULL;
153
159k
    }
154
4.52M
    return 1;
155
4.52M
}
156
157
static int evp_md_init_internal(EVP_MD_CTX *ctx, const EVP_MD *type,
158
    const OSSL_PARAM params[], ENGINE *impl)
159
400M
{
160
400M
#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
161
400M
    ENGINE *tmpimpl = NULL;
162
400M
#endif
163
164
400M
#if !defined(FIPS_MODULE)
165
400M
    if (ctx->pctx != NULL
166
0
        && EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx->pctx)
167
0
        && ctx->pctx->op.sig.algctx != NULL) {
168
        /*
169
         * Prior to OpenSSL 3.0 calling EVP_DigestInit_ex() on an mdctx
170
         * previously initialised with EVP_DigestSignInit() would retain
171
         * information about the key, and re-initialise for another sign
172
         * operation. So in that case we redirect to EVP_DigestSignInit()
173
         */
174
0
        if (ctx->pctx->operation == EVP_PKEY_OP_SIGNCTX)
175
0
            return EVP_DigestSignInit(ctx, NULL, type, impl, NULL);
176
0
        if (ctx->pctx->operation == EVP_PKEY_OP_VERIFYCTX)
177
0
            return EVP_DigestVerifyInit(ctx, NULL, type, impl, NULL);
178
0
        ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
179
0
        return 0;
180
0
    }
181
400M
#endif
182
183
400M
    EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_CLEANED | EVP_MD_CTX_FLAG_FINALISED);
184
185
400M
    if (type != NULL) {
186
4.34M
        ctx->reqdigest = type;
187
396M
    } else {
188
396M
        if (ctx->digest == NULL) {
189
0
            ERR_raise(ERR_LIB_EVP, EVP_R_NO_DIGEST_SET);
190
0
            return 0;
191
0
        }
192
396M
        type = ctx->digest;
193
396M
    }
194
195
    /* Code below to be removed when legacy support is dropped. */
196
400M
#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
197
    /*
198
     * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
199
     * this context may already have an ENGINE! Try to avoid releasing the
200
     * previous handle, re-querying for an ENGINE, and having a
201
     * reinitialisation, when it may all be unnecessary.
202
     */
203
400M
    if (ctx->engine != NULL
204
0
        && ctx->digest != NULL
205
0
        && type->type == ctx->digest->type)
206
0
        goto skip_to_init;
207
208
    /*
209
     * Ensure an ENGINE left lying around from last time is cleared (the
210
     * previous check attempted to avoid this if the same ENGINE and
211
     * EVP_MD could be used).
212
     */
213
400M
    ENGINE_finish(ctx->engine);
214
400M
    ctx->engine = NULL;
215
216
400M
    if (impl == NULL)
217
400M
        tmpimpl = ENGINE_get_digest_engine(type->type);
218
400M
#endif
219
220
    /*
221
     * If there are engines involved or EVP_MD_CTX_FLAG_NO_INIT is set then we
222
     * should use legacy handling for now.
223
     */
224
400M
    if (impl != NULL
225
400M
#if !defined(OPENSSL_NO_ENGINE)
226
400M
        || ctx->engine != NULL
227
400M
#if !defined(FIPS_MODULE)
228
400M
        || tmpimpl != NULL
229
400M
#endif
230
400M
#endif
231
400M
        || (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0
232
400M
        || (type != NULL && type->origin == EVP_ORIG_METH)
233
400M
        || (type == NULL && ctx->digest != NULL
234
0
            && ctx->digest->origin == EVP_ORIG_METH)) {
235
        /* If we were using provided hash before, cleanup algctx */
236
0
        if (!evp_md_ctx_free_algctx(ctx))
237
0
            return 0;
238
0
        if (ctx->digest == ctx->fetched_digest)
239
0
            ctx->digest = NULL;
240
0
        EVP_MD_free(ctx->fetched_digest);
241
0
        ctx->fetched_digest = NULL;
242
0
        goto legacy;
243
0
    }
244
245
400M
    cleanup_old_md_data(ctx, 1);
246
247
    /* Start of non-legacy code below */
248
400M
    if (ctx->digest == type) {
249
398M
        if (!ossl_assert(type->prov != NULL)) {
250
0
            ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
251
0
            return 0;
252
0
        }
253
398M
    } else {
254
1.82M
        if (!evp_md_ctx_free_algctx(ctx))
255
0
            return 0;
256
1.82M
    }
257
258
400M
    if (type->prov == NULL) {
259
#ifdef FIPS_MODULE
260
        /* We only do explicit fetches inside the FIPS module */
261
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
262
        return 0;
263
#else
264
        /* The NULL digest is a special case */
265
174
        EVP_MD *provmd = EVP_MD_fetch(NULL,
266
174
            type->type != NID_undef ? OBJ_nid2sn(type->type)
267
174
                                    : "NULL",
268
174
            "");
269
270
174
        if (provmd == NULL) {
271
0
            ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
272
0
            return 0;
273
0
        }
274
174
        type = provmd;
275
174
        EVP_MD_free(ctx->fetched_digest);
276
174
        ctx->fetched_digest = provmd;
277
174
#endif
278
174
    }
279
280
400M
    if (type->prov != NULL && ctx->fetched_digest != type) {
281
1.82M
        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.82M
        EVP_MD_free(ctx->fetched_digest);
286
1.82M
        ctx->fetched_digest = (EVP_MD *)type;
287
1.82M
    }
288
400M
    ctx->digest = type;
289
400M
    if (ctx->algctx == NULL) {
290
1.82M
        ctx->algctx = ctx->digest->newctx(ossl_provider_ctx(type->prov));
291
1.82M
        if (ctx->algctx == NULL) {
292
0
            ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
293
0
            return 0;
294
0
        }
295
1.82M
    }
296
297
400M
    if (ctx->digest->dinit == NULL) {
298
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
299
0
        return 0;
300
0
    }
301
302
400M
    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
839M
{
371
839M
    return evp_md_init_internal(ctx, type, params, NULL);
372
839M
}
373
374
int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type)
375
40
{
376
40
    EVP_MD_CTX_reset(ctx);
377
40
    return evp_md_init_internal(ctx, type, NULL, NULL);
378
40
}
379
380
int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
381
8.92M
{
382
8.92M
    return evp_md_init_internal(ctx, type, NULL, impl);
383
8.92M
}
384
385
int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t count)
386
1.43G
{
387
1.43G
    if (count == 0)
388
61.1k
        return 1;
389
390
1.43G
    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.43G
    if (ctx->pctx != NULL
396
0
        && EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx->pctx)
397
0
        && ctx->pctx->op.sig.algctx != NULL) {
398
        /*
399
         * Prior to OpenSSL 3.0 EVP_DigestSignUpdate() and
400
         * EVP_DigestVerifyUpdate() were just macros for EVP_DigestUpdate().
401
         * Some code calls EVP_DigestUpdate() directly even when initialised
402
         * with EVP_DigestSignInit_ex() or
403
         * EVP_DigestVerifyInit_ex(), so we detect that and redirect to
404
         * the correct EVP_Digest*Update() function
405
         */
406
0
        if (ctx->pctx->operation == EVP_PKEY_OP_SIGNCTX)
407
0
            return EVP_DigestSignUpdate(ctx, data, count);
408
0
        if (ctx->pctx->operation == EVP_PKEY_OP_VERIFYCTX)
409
0
            return EVP_DigestVerifyUpdate(ctx, data, count);
410
0
        ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
411
0
        return 0;
412
0
    }
413
414
1.43G
    if (ctx->digest == NULL
415
1.43G
        || ctx->digest->prov == NULL
416
1.43G
        || (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0)
417
9
        goto legacy;
418
419
1.43G
    if (ctx->digest->dupdate == NULL) {
420
0
        ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
421
0
        return 0;
422
0
    }
423
1.43G
    return ctx->digest->dupdate(ctx->algctx, data, count);
424
425
    /* Code below to be removed when legacy support is dropped. */
426
9
legacy:
427
9
    return ctx->update != NULL ? ctx->update(ctx, data, count) : 0;
428
1.43G
}
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
260k
{
433
260k
    int ret;
434
260k
    ret = EVP_DigestFinal_ex(ctx, md, size);
435
260k
    EVP_MD_CTX_reset(ctx);
436
260k
    return ret;
437
260k
}
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
230M
{
442
230M
    int ret, sz;
443
230M
    size_t size = 0;
444
230M
    size_t mdsize = 0;
445
446
230M
    if (ctx->digest == NULL)
447
0
        return 0;
448
449
230M
    sz = EVP_MD_CTX_get_size(ctx);
450
230M
    if (sz < 0)
451
12
        return 0;
452
230M
    mdsize = sz;
453
230M
    if (ctx->digest->prov == NULL)
454
0
        goto legacy;
455
456
230M
    if (ctx->digest->dfinal == NULL) {
457
0
        ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
458
0
        return 0;
459
0
    }
460
461
230M
    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
230M
    ret = ctx->digest->dfinal(ctx->algctx, md, &size, mdsize);
467
468
230M
    ctx->flags |= EVP_MD_CTX_FLAG_FINALISED;
469
470
230M
    if (isize != NULL) {
471
3.97M
        if (size <= UINT_MAX) {
472
3.97M
            *isize = (unsigned int)size;
473
3.97M
        } else {
474
0
            ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
475
0
            ret = 0;
476
0
        }
477
3.97M
    }
478
479
230M
    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
230M
}
494
495
/* This is a one shot operation */
496
int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t size)
497
170M
{
498
170M
    int ret = 0;
499
170M
    OSSL_PARAM params[2];
500
170M
    size_t i = 0;
501
502
170M
    if (ctx->digest == NULL) {
503
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_NULL_ALGORITHM);
504
0
        return 0;
505
0
    }
506
507
170M
    if (ctx->digest->prov == NULL)
508
0
        goto legacy;
509
510
170M
    if (ctx->digest->dfinal == NULL) {
511
0
        ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
512
0
        return 0;
513
0
    }
514
515
170M
    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
170M
    params[i++] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN, &size);
526
170M
    params[i++] = OSSL_PARAM_construct_end();
527
528
170M
    if (EVP_MD_CTX_set_params(ctx, params) >= 0)
529
170M
        ret = ctx->digest->dfinal(ctx->algctx, md, &size, size);
530
531
170M
    ctx->flags |= EVP_MD_CTX_FLAG_FINALISED;
532
533
170M
    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
170M
}
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.96M
{
555
1.96M
    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.96M
    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.96M
    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.96M
    return ctx->digest->dsqueeze(ctx->algctx, md, &size, size);
571
1.96M
}
572
573
EVP_MD_CTX *EVP_MD_CTX_dup(const EVP_MD_CTX *in)
574
2.44k
{
575
2.44k
    EVP_MD_CTX *out = EVP_MD_CTX_new();
576
577
2.44k
    if (out != NULL && !EVP_MD_CTX_copy_ex(out, in)) {
578
0
        EVP_MD_CTX_free(out);
579
0
        out = NULL;
580
0
    }
581
2.44k
    return out;
582
2.44k
}
583
584
int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in)
585
697k
{
586
697k
    EVP_MD_CTX_reset(out);
587
697k
    return EVP_MD_CTX_copy_ex(out, in);
588
697k
}
589
590
int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in)
591
4.43M
{
592
4.43M
    int digest_change = 0;
593
4.43M
    unsigned char *tmp_buf;
594
595
4.43M
    if (in == NULL) {
596
0
        ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
597
0
        return 0;
598
0
    }
599
600
4.43M
    if (in->digest == NULL) {
601
        /* copying uninitialized digest context */
602
0
        EVP_MD_CTX_reset(out);
603
0
        if (out->fetched_digest != NULL)
604
0
            EVP_MD_free(out->fetched_digest);
605
0
        *out = *in;
606
0
        goto clone_pkey;
607
0
    }
608
609
4.43M
    if (in->digest->prov == NULL
610
4.43M
        || (in->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0)
611
0
        goto legacy;
612
613
4.43M
    if (in->digest->dupctx == NULL) {
614
0
        ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX);
615
0
        return 0;
616
0
    }
617
618
4.43M
    evp_md_ctx_reset_ex(out, 1);
619
4.43M
    digest_change = (out->fetched_digest != in->fetched_digest);
620
4.43M
    if (digest_change && out->fetched_digest != NULL)
621
0
        EVP_MD_free(out->fetched_digest);
622
4.43M
    *out = *in;
623
    /* NULL out pointers in case of error */
624
4.43M
    out->pctx = NULL;
625
4.43M
    out->algctx = NULL;
626
627
4.43M
    if (digest_change && in->fetched_digest != NULL)
628
3.36M
        EVP_MD_up_ref(in->fetched_digest);
629
630
4.43M
    if (in->algctx != NULL) {
631
4.19M
        out->algctx = in->digest->dupctx(in->algctx);
632
4.19M
        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
4.19M
    }
637
638
4.43M
clone_pkey:
639
    /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
640
4.43M
    EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
641
4.43M
#ifndef FIPS_MODULE
642
4.43M
    if (in->pctx != NULL) {
643
247k
        out->pctx = EVP_PKEY_CTX_dup(in->pctx);
644
247k
        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
247k
    }
650
4.43M
#endif
651
652
4.43M
    return 1;
653
654
    /* Code below to be removed when legacy support is dropped. */
655
0
legacy:
656
0
#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
657
    /* Make sure it's safe to copy a digest context using an ENGINE */
658
0
    if (in->engine && !ENGINE_init(in->engine)) {
659
0
        ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB);
660
0
        return 0;
661
0
    }
662
0
#endif
663
664
0
    if (out->digest == in->digest) {
665
0
        tmp_buf = out->md_data;
666
0
        EVP_MD_CTX_set_flags(out, EVP_MD_CTX_FLAG_REUSE);
667
0
    } else
668
0
        tmp_buf = NULL;
669
0
    EVP_MD_CTX_reset(out);
670
0
    memcpy(out, in, sizeof(*out));
671
672
    /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
673
0
    EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
674
675
    /* Null these variables, since they are getting fixed up
676
     * properly below.  Anything else may cause a memleak and/or
677
     * double free if any of the memory allocations below fail
678
     */
679
0
    out->md_data = NULL;
680
0
    out->pctx = NULL;
681
682
0
    if (in->md_data && out->digest->ctx_size) {
683
0
        if (tmp_buf)
684
0
            out->md_data = tmp_buf;
685
0
        else {
686
0
            out->md_data = OPENSSL_malloc(out->digest->ctx_size);
687
0
            if (out->md_data == NULL)
688
0
                return 0;
689
0
        }
690
0
        memcpy(out->md_data, in->md_data, out->digest->ctx_size);
691
0
    }
692
693
0
    out->update = in->update;
694
695
0
#ifndef FIPS_MODULE
696
0
    if (in->pctx) {
697
0
        out->pctx = EVP_PKEY_CTX_dup(in->pctx);
698
0
        if (!out->pctx) {
699
0
            EVP_MD_CTX_reset(out);
700
0
            return 0;
701
0
        }
702
0
    }
703
0
#endif
704
705
0
    if (out->digest->copy)
706
0
        return out->digest->copy(out, in);
707
708
0
    return 1;
709
0
}
710
711
int EVP_Digest(const void *data, size_t count,
712
    unsigned char *md, unsigned int *size, const EVP_MD *type,
713
    ENGINE *impl)
714
377k
{
715
377k
    EVP_MD_CTX *ctx = EVP_MD_CTX_new();
716
377k
    int ret;
717
718
377k
    if (ctx == NULL)
719
0
        return 0;
720
377k
    EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT);
721
377k
    ret = EVP_DigestInit_ex(ctx, type, impl)
722
377k
        && EVP_DigestUpdate(ctx, data, count)
723
377k
        && EVP_DigestFinal_ex(ctx, md, size);
724
377k
    EVP_MD_CTX_free(ctx);
725
726
377k
    return ret;
727
377k
}
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
170M
{
763
170M
    EVP_PKEY_CTX *pctx = ctx->pctx;
764
765
    /* If we have a pctx then we should try that first */
766
170M
    if (pctx != NULL
767
0
        && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
768
0
            || pctx->operation == EVP_PKEY_OP_SIGNCTX)
769
0
        && pctx->op.sig.algctx != NULL
770
0
        && pctx->op.sig.signature->set_ctx_md_params != NULL)
771
0
        return pctx->op.sig.signature->set_ctx_md_params(pctx->op.sig.algctx,
772
0
            params);
773
774
170M
    if (ctx->digest != NULL && ctx->digest->set_ctx_params != NULL)
775
170M
        return ctx->digest->set_ctx_params(ctx->algctx, params);
776
777
35
    return 0;
778
170M
}
779
780
const OSSL_PARAM *EVP_MD_settable_ctx_params(const EVP_MD *md)
781
169
{
782
169
    void *provctx;
783
784
169
    if (md != NULL && md->settable_ctx_params != NULL) {
785
140
        provctx = ossl_provider_ctx(EVP_MD_get0_provider(md));
786
140
        return md->settable_ctx_params(NULL, provctx);
787
140
    }
788
29
    return NULL;
789
169
}
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
188k
{
819
188k
    EVP_PKEY_CTX *pctx = ctx->pctx;
820
821
    /* If we have a pctx then we should try that first */
822
188k
    if (pctx != NULL
823
0
        && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
824
0
            || pctx->operation == EVP_PKEY_OP_SIGNCTX)
825
0
        && pctx->op.sig.algctx != NULL
826
0
        && pctx->op.sig.signature->get_ctx_md_params != NULL)
827
0
        return pctx->op.sig.signature->get_ctx_md_params(pctx->op.sig.algctx,
828
0
            params);
829
830
188k
    if (ctx->digest != NULL && ctx->digest->get_ctx_params != NULL)
831
188k
        return ctx->digest->get_ctx_params(ctx->algctx, params);
832
833
0
    return 0;
834
188k
}
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
231M
{
849
231M
    EVP_PKEY_CTX *pctx;
850
231M
    void *provctx;
851
852
231M
    if (ctx == NULL)
853
0
        return NULL;
854
855
    /* If we have a pctx then we should try that first */
856
231M
    pctx = ctx->pctx;
857
231M
    if (pctx != NULL
858
774k
        && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
859
774k
            || pctx->operation == EVP_PKEY_OP_SIGNCTX)
860
774k
        && pctx->op.sig.signature != NULL
861
774k
        && pctx->op.sig.signature->gettable_ctx_md_params != NULL
862
0
        && pctx->op.sig.algctx != NULL)
863
0
        return pctx->op.sig.signature->gettable_ctx_md_params(
864
0
            pctx->op.sig.algctx);
865
866
231M
    if (ctx->digest != NULL && ctx->digest->gettable_ctx_params != NULL) {
867
95.1k
        provctx = ossl_provider_ctx(EVP_MD_get0_provider(ctx->digest));
868
95.1k
        return ctx->digest->gettable_ctx_params(ctx->algctx, provctx);
869
95.1k
    }
870
231M
    return NULL;
871
231M
}
872
873
int EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void *p2)
874
0
{
875
0
    int ret = EVP_CTRL_RET_UNSUPPORTED;
876
0
    int set_params = 1;
877
0
    size_t sz;
878
0
    OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
879
880
0
    if (ctx == NULL) {
881
0
        ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
882
0
        return 0;
883
0
    }
884
885
0
    if (ctx->digest != NULL && ctx->digest->prov == NULL)
886
0
        goto legacy;
887
888
0
    switch (cmd) {
889
0
    case EVP_MD_CTRL_XOF_LEN:
890
0
        sz = (size_t)p1;
891
0
        params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN, &sz);
892
0
        break;
893
0
    case EVP_MD_CTRL_MICALG:
894
0
        set_params = 0;
895
0
        params[0] = OSSL_PARAM_construct_utf8_string(OSSL_DIGEST_PARAM_MICALG,
896
0
            p2, p1 ? p1 : 9999);
897
0
        break;
898
0
    case EVP_CTRL_SSL3_MASTER_SECRET:
899
0
        params[0] = OSSL_PARAM_construct_octet_string(OSSL_DIGEST_PARAM_SSL3_MS,
900
0
            p2, p1);
901
0
        break;
902
0
    default:
903
0
        goto conclude;
904
0
    }
905
906
0
    if (set_params)
907
0
        ret = EVP_MD_CTX_set_params(ctx, params);
908
0
    else
909
0
        ret = EVP_MD_CTX_get_params(ctx, params);
910
0
    goto conclude;
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.32k
{
928
2.32k
    EVP_MD *md = OPENSSL_zalloc(sizeof(*md));
929
930
2.32k
    if (md != NULL && !CRYPTO_NEW_REF(&md->refcnt, 1)) {
931
0
        OPENSSL_free(md);
932
0
        return NULL;
933
0
    }
934
2.32k
    return md;
935
2.32k
}
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
6.54k
{
945
6.54k
    int nid;
946
6.54k
    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
6.54k
    const void *legacy_method = OBJ_NAME_get(name, OBJ_NAME_TYPE_MD_METH);
953
954
6.54k
    if (*legacy_nid == -1) /* We found a clash already */
955
0
        return;
956
957
6.54k
    if (legacy_method == NULL)
958
4.45k
        return;
959
2.09k
    nid = EVP_MD_nid(legacy_method);
960
2.09k
    if (*legacy_nid != NID_undef && *legacy_nid != nid) {
961
0
        *legacy_nid = -1;
962
0
        return;
963
0
    }
964
2.09k
    *legacy_nid = nid;
965
2.09k
}
966
#endif
967
968
static int evp_md_cache_constants(EVP_MD *md)
969
2.47k
{
970
2.47k
    int ok, xof = 0, algid_absent = 0;
971
2.47k
    size_t blksz = 0;
972
2.47k
    size_t mdsize = 0;
973
2.47k
    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.47k
    params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_BLOCK_SIZE, &blksz);
981
2.47k
    params[1] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_SIZE, &mdsize);
982
2.47k
    params[2] = OSSL_PARAM_construct_int(OSSL_DIGEST_PARAM_XOF, &xof);
983
2.47k
    params[3] = OSSL_PARAM_construct_int(OSSL_DIGEST_PARAM_ALGID_ABSENT,
984
2.47k
        &algid_absent);
985
2.47k
    params[4] = OSSL_PARAM_construct_end();
986
2.47k
    ok = evp_do_md_getparams(md, params) > 0;
987
2.47k
    if (mdsize > INT_MAX || blksz > INT_MAX)
988
0
        ok = 0;
989
2.47k
    if (ok) {
990
2.47k
        md->block_size = (int)blksz;
991
2.47k
        md->md_size = (int)mdsize;
992
2.47k
        if (xof)
993
402
            md->flags |= EVP_MD_FLAG_XOF;
994
2.47k
        if (algid_absent)
995
1.66k
            md->flags |= EVP_MD_FLAG_DIGALGID_ABSENT;
996
2.47k
    }
997
2.47k
    return ok;
998
2.47k
}
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 = OSSL_FUNC_digest_settable_ctx_params(fns);
1097
216
            break;
1098
114
        case OSSL_FUNC_DIGEST_GETTABLE_CTX_PARAMS:
1099
114
            if (md->gettable_ctx_params == NULL)
1100
114
                md->gettable_ctx_params = OSSL_FUNC_digest_gettable_ctx_params(fns);
1101
114
            break;
1102
6.60k
        }
1103
6.60k
    }
1104
729
    if ((fncnt != 0 && fncnt != 5 && fncnt != 6)
1105
729
        || (fncnt == 0 && md->digest == NULL)) {
1106
        /*
1107
         * In order to be a consistent set of functions we either need the
1108
         * whole set of init/update/final etc functions or none of them.
1109
         * The "digest" function can standalone. We at least need one way to
1110
         * generate digests.
1111
         */
1112
0
        EVP_MD_free(md);
1113
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
1114
0
        return NULL;
1115
0
    }
1116
729
    md->prov = prov;
1117
729
    if (prov != NULL)
1118
729
        ossl_provider_up_ref(prov);
1119
1120
729
    if (!evp_md_cache_constants(md)) {
1121
0
        EVP_MD_free(md);
1122
0
        ERR_raise(ERR_LIB_EVP, EVP_R_CACHE_CONSTANTS_FAILED);
1123
0
        md = NULL;
1124
0
    }
1125
1126
729
    return md;
1127
729
}
1128
1129
static int evp_md_up_ref(void *md)
1130
4.77M
{
1131
4.77M
    return EVP_MD_up_ref(md);
1132
4.77M
}
1133
1134
static void evp_md_free(void *md)
1135
8.39k
{
1136
8.39k
    EVP_MD_free(md);
1137
8.39k
}
1138
1139
EVP_MD *EVP_MD_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
1140
    const char *properties)
1141
5.99M
{
1142
5.99M
    EVP_MD *md = evp_generic_fetch(ctx, OSSL_OP_DIGEST, algorithm, properties,
1143
5.99M
        evp_md_from_algorithm, evp_md_up_ref, evp_md_free);
1144
1145
5.99M
    return md;
1146
5.99M
}
1147
1148
int EVP_MD_up_ref(EVP_MD *md)
1149
20.0M
{
1150
20.0M
    int ref = 0;
1151
1152
20.0M
    if (md->origin == EVP_ORIG_DYNAMIC)
1153
20.0M
        CRYPTO_UP_REF(&md->refcnt, &ref);
1154
20.0M
    return 1;
1155
20.0M
}
1156
1157
void EVP_MD_free(EVP_MD *md)
1158
38.7M
{
1159
38.7M
    int i;
1160
1161
38.7M
    if (md == NULL || md->origin != EVP_ORIG_DYNAMIC)
1162
18.7M
        return;
1163
1164
20.0M
    CRYPTO_DOWN_REF(&md->refcnt, &i);
1165
20.0M
    if (i > 0)
1166
20.0M
        return;
1167
1.74k
    evp_md_free_int(md);
1168
1.74k
}
1169
1170
void EVP_MD_do_all_provided(OSSL_LIB_CTX *libctx,
1171
    void (*fn)(EVP_MD *mac, void *arg),
1172
    void *arg)
1173
8
{
1174
8
    evp_generic_do_all(libctx, OSSL_OP_DIGEST,
1175
8
        (void (*)(void *, void *))fn, arg,
1176
8
        evp_md_from_algorithm, evp_md_up_ref, evp_md_free);
1177
8
}
1178
1179
typedef struct {
1180
    int md_nid;
1181
    int hmac_nid;
1182
} ossl_hmacmd_pair;
1183
1184
static const ossl_hmacmd_pair ossl_hmacmd_pairs[] = {
1185
    { NID_sha1, NID_hmacWithSHA1 },
1186
    { NID_md5, NID_hmacWithMD5 },
1187
    { NID_sha224, NID_hmacWithSHA224 },
1188
    { NID_sha256, NID_hmacWithSHA256 },
1189
    { NID_sha384, NID_hmacWithSHA384 },
1190
    { NID_sha512, NID_hmacWithSHA512 },
1191
    { NID_id_GostR3411_94, NID_id_HMACGostR3411_94 },
1192
    { NID_id_GostR3411_2012_256, NID_id_tc26_hmac_gost_3411_2012_256 },
1193
    { NID_id_GostR3411_2012_512, NID_id_tc26_hmac_gost_3411_2012_512 },
1194
    { NID_sha3_224, NID_hmac_sha3_224 },
1195
    { NID_sha3_256, NID_hmac_sha3_256 },
1196
    { NID_sha3_384, NID_hmac_sha3_384 },
1197
    { NID_sha3_512, NID_hmac_sha3_512 },
1198
    { NID_sha512_224, NID_hmacWithSHA512_224 },
1199
    { NID_sha512_256, NID_hmacWithSHA512_256 }
1200
};
1201
1202
int ossl_hmac2mdnid(int hmac_nid)
1203
382
{
1204
382
    int md_nid = NID_undef;
1205
382
    size_t i;
1206
1207
1.54k
    for (i = 0; i < OSSL_NELEM(ossl_hmacmd_pairs); i++) {
1208
1.52k
        if (ossl_hmacmd_pairs[i].hmac_nid == hmac_nid) {
1209
364
            md_nid = ossl_hmacmd_pairs[i].md_nid;
1210
364
            break;
1211
364
        }
1212
1.52k
    }
1213
1214
382
    return md_nid;
1215
382
}
1216
1217
int ossl_md2hmacnid(int md_nid)
1218
0
{
1219
0
    int hmac_nid = NID_undef;
1220
0
    size_t i;
1221
1222
0
    for (i = 0; i < OSSL_NELEM(ossl_hmacmd_pairs); i++) {
1223
0
        if (ossl_hmacmd_pairs[i].md_nid == md_nid) {
1224
0
            hmac_nid = ossl_hmacmd_pairs[i].hmac_nid;
1225
0
            break;
1226
0
        }
1227
0
    }
1228
1229
0
    return hmac_nid;
1230
0
}