Coverage Report

Created: 2025-12-10 06:24

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