Coverage Report

Created: 2025-12-08 06:22

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
204k
{
26
204k
    if (ctx->digest != NULL) {
27
102k
        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
102k
        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
102k
    }
37
204k
}
38
39
void evp_md_ctx_clear_digest(EVP_MD_CTX *ctx, int force, int keep_fetched)
40
102k
{
41
102k
    if (ctx->algctx != NULL) {
42
102k
        if (ctx->digest != NULL && ctx->digest->freectx != NULL)
43
102k
            ctx->digest->freectx(ctx->algctx);
44
102k
        ctx->algctx = NULL;
45
102k
        EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
46
102k
    }
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
102k
    cleanup_old_md_data(ctx, force);
55
102k
    if (force)
56
0
        ctx->digest = NULL;
57
58
    /* Non legacy code, this has to be later than the ctx->digest cleaning */
59
102k
    if (!keep_fetched) {
60
102k
        EVP_MD_free(ctx->fetched_digest);
61
102k
        ctx->fetched_digest = NULL;
62
102k
        ctx->reqdigest = NULL;
63
102k
    }
64
102k
}
65
66
static int evp_md_ctx_reset_ex(EVP_MD_CTX *ctx, int keep_fetched)
67
102k
{
68
102k
    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
102k
    if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX)) {
76
102k
        EVP_PKEY_CTX_free(ctx->pctx);
77
102k
        ctx->pctx = NULL;
78
102k
    }
79
80
102k
    evp_md_ctx_clear_digest(ctx, 0, keep_fetched);
81
102k
    if (!keep_fetched)
82
102k
        OPENSSL_cleanse(ctx, sizeof(*ctx));
83
84
102k
    return 1;
85
102k
}
86
87
/* This call frees resources associated with the context */
88
int EVP_MD_CTX_reset(EVP_MD_CTX *ctx)
89
102k
{
90
102k
    return evp_md_ctx_reset_ex(ctx, 0);
91
102k
}
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
102k
{
121
102k
    return OPENSSL_zalloc(sizeof(EVP_MD_CTX));
122
102k
}
123
124
void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
125
102k
{
126
102k
    if (ctx == NULL)
127
0
        return;
128
129
102k
    EVP_MD_CTX_reset(ctx);
130
102k
    OPENSSL_free(ctx);
131
102k
}
132
133
int evp_md_ctx_free_algctx(EVP_MD_CTX *ctx)
134
102k
{
135
102k
    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
102k
    return 1;
145
102k
}
146
147
static int evp_md_init_internal(EVP_MD_CTX *ctx, const EVP_MD *type,
148
                                const OSSL_PARAM params[])
149
102k
{
150
102k
#if !defined(FIPS_MODULE)
151
102k
    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
102k
#endif
168
169
102k
    EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_CLEANED
170
102k
                                | EVP_MD_CTX_FLAG_FINALISED);
171
172
102k
    if (type != NULL) {
173
102k
        ctx->reqdigest = type;
174
102k
    } else {
175
0
        if (ossl_unlikely(ctx->digest == NULL)) {
176
0
            ERR_raise(ERR_LIB_EVP, EVP_R_NO_DIGEST_SET);
177
0
            return 0;
178
0
        }
179
0
        type = ctx->digest;
180
0
    }
181
182
    /*
183
     * If there is EVP_MD_CTX_FLAG_NO_INIT set then we
184
     * should use legacy handling for now.
185
     */
186
102k
    if ((ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0
187
102k
            || (type != NULL && type->origin == EVP_ORIG_METH)
188
102k
            || (type == NULL && ctx->digest != NULL
189
0
                             && ctx->digest->origin == EVP_ORIG_METH)) {
190
        /* If we were using provided hash before, cleanup algctx */
191
0
        if (!evp_md_ctx_free_algctx(ctx))
192
0
            return 0;
193
0
        if (ctx->digest == ctx->fetched_digest)
194
0
            ctx->digest = NULL;
195
0
        EVP_MD_free(ctx->fetched_digest);
196
0
        ctx->fetched_digest = NULL;
197
0
        goto legacy;
198
0
    }
199
200
102k
    cleanup_old_md_data(ctx, 1);
201
202
    /* Start of non-legacy code below */
203
102k
    if (ossl_likely(ctx->digest == type)) {
204
0
        if (ossl_unlikely(!ossl_assert(type->prov != NULL))) {
205
0
            ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
206
0
            return 0;
207
0
        }
208
102k
    } else {
209
102k
        if (!evp_md_ctx_free_algctx(ctx))
210
0
            return 0;
211
102k
    }
212
213
102k
    if (ossl_unlikely(type->prov == NULL)) {
214
#ifdef FIPS_MODULE
215
        /* We only do explicit fetches inside the FIPS module */
216
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
217
        return 0;
218
#else
219
        /* The NULL digest is a special case */
220
0
        EVP_MD *provmd = EVP_MD_fetch(NULL,
221
0
                                      type->type != NID_undef ? OBJ_nid2sn(type->type)
222
0
                                                              : "NULL", "");
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
102k
    if (ossl_unlikely(type->prov != NULL && ctx->fetched_digest != type)) {
235
102k
        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
102k
        EVP_MD_free(ctx->fetched_digest);
240
102k
        ctx->fetched_digest = (EVP_MD *)type;
241
102k
    }
242
102k
    ctx->digest = type;
243
102k
    if (ctx->algctx == NULL) {
244
102k
        ctx->algctx = ctx->digest->newctx(ossl_provider_ctx(type->prov));
245
102k
        if (ctx->algctx == NULL) {
246
0
            ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
247
0
            return 0;
248
0
        }
249
102k
    }
250
251
102k
    if (ctx->digest->dinit == NULL) {
252
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
253
0
        return 0;
254
0
    }
255
256
102k
    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
102k
{
302
102k
    if (!ossl_assert(impl == NULL))
303
0
        return 0;
304
102k
    return evp_md_init_internal(ctx, type, NULL);
305
102k
}
306
307
int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t count)
308
102k
{
309
102k
    if (ossl_unlikely(count == 0))
310
0
        return 1;
311
312
102k
    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
102k
    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
102k
    if (ctx->digest == NULL
339
102k
            || ctx->digest->prov == NULL
340
102k
            || ossl_unlikely((ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0))
341
0
        goto legacy;
342
343
102k
    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
102k
    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
102k
}
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
102k
{
366
102k
    int ret, sz;
367
102k
    size_t size = 0;
368
102k
    size_t mdsize = 0;
369
370
102k
    if (ossl_unlikely(ctx->digest == NULL))
371
0
        return 0;
372
373
102k
    sz = EVP_MD_CTX_get_size(ctx);
374
102k
    if (ossl_unlikely(sz < 0))
375
0
        return 0;
376
102k
    mdsize = sz;
377
102k
    if (ossl_unlikely(ctx->digest->prov == NULL))
378
0
        goto legacy;
379
380
102k
    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
102k
    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
102k
    ret = ctx->digest->dfinal(ctx->algctx, md, &size, mdsize);
391
392
102k
    ctx->flags |= EVP_MD_CTX_FLAG_FINALISED;
393
394
102k
    if (isize != NULL) {
395
102k
        if (ossl_likely(size <= UINT_MAX)) {
396
102k
            *isize = (unsigned int)size;
397
102k
        } else {
398
0
            ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
399
0
            ret = 0;
400
0
        }
401
102k
    }
402
403
102k
    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
102k
}
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
102k
{
645
102k
    EVP_MD_CTX *ctx;
646
102k
    int ret;
647
648
102k
    if (!ossl_assert(impl == NULL))
649
0
        return 0;
650
651
102k
    ctx = EVP_MD_CTX_new();
652
102k
    if (ctx == NULL)
653
0
        return 0;
654
102k
    EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT);
655
102k
    ret = EVP_DigestInit_ex(ctx, type, NULL)
656
102k
        && EVP_DigestUpdate(ctx, data, count)
657
102k
        && EVP_DigestFinal_ex(ctx, md, size);
658
102k
    EVP_MD_CTX_free(ctx);
659
660
102k
    return ret;
661
102k
}
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
102k
{
667
102k
    EVP_MD *digest = EVP_MD_fetch(libctx, name, propq);
668
102k
    unsigned int temp = 0;
669
102k
    int ret = 0;
670
671
102k
    if (digest != NULL) {
672
102k
        ret = EVP_Digest(data, datalen, md, &temp, digest, NULL);
673
102k
        EVP_MD_free(digest);
674
102k
    }
675
102k
    if (mdlen != NULL)
676
0
        *mdlen = temp;
677
102k
    return ret;
678
102k
}
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
0
{
753
0
    EVP_PKEY_CTX *pctx = ctx->pctx;
754
755
    /* If we have a pctx then we should try that first */
756
0
    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
0
    if (ctx->digest != NULL && ctx->digest->get_ctx_params != NULL)
765
0
        return ctx->digest->get_ctx_params(ctx->algctx, params);
766
767
0
    return 0;
768
0
}
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
102k
{
783
102k
    EVP_PKEY_CTX *pctx;
784
102k
    void *provctx;
785
786
102k
    if (ossl_unlikely(ctx == NULL))
787
0
        return NULL;
788
789
    /* If we have a pctx then we should try that first */
790
102k
    pctx = ctx->pctx;
791
102k
    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
102k
    if (ossl_unlikely(ctx->digest != NULL
800
102k
                      && ctx->digest->gettable_ctx_params != NULL)) {
801
0
        provctx = ossl_provider_ctx(EVP_MD_get0_provider(ctx->digest));
802
0
        return ctx->digest->gettable_ctx_params(ctx->algctx, provctx);
803
0
    }
804
102k
    return NULL;
805
102k
}
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
847
    /* Code below to be removed when legacy support is dropped. */
848
0
 legacy:
849
0
    if (ctx->digest->md_ctrl == NULL) {
850
0
        ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_NOT_IMPLEMENTED);
851
0
        return 0;
852
0
    }
853
854
0
    ret = ctx->digest->md_ctrl(ctx, cmd, p1, p2);
855
0
 conclude:
856
0
    if (ret <= 0)
857
0
        return 0;
858
0
    return ret;
859
0
}
860
861
EVP_MD *evp_md_new(void)
862
432
{
863
432
    EVP_MD *md = OPENSSL_zalloc(sizeof(*md));
864
865
432
    if (md != NULL && !CRYPTO_NEW_REF(&md->refcnt, 1)) {
866
0
        OPENSSL_free(md);
867
0
        return NULL;
868
0
    }
869
432
    return md;
870
432
}
871
872
/*
873
 * FIPS module note: since internal fetches will be entirely
874
 * provider based, we know that none of its code depends on legacy
875
 * NIDs or any functionality that use them.
876
 */
877
#ifndef FIPS_MODULE
878
static void set_legacy_nid(const char *name, void *vlegacy_nid)
879
1.13k
{
880
1.13k
    int nid;
881
1.13k
    int *legacy_nid = vlegacy_nid;
882
    /*
883
     * We use lowest level function to get the associated method, because
884
     * higher level functions such as EVP_get_digestbyname() have changed
885
     * to look at providers too.
886
     */
887
1.13k
    const void *legacy_method = OBJ_NAME_get(name, OBJ_NAME_TYPE_MD_METH);
888
889
1.13k
    if (*legacy_nid == -1)       /* We found a clash already */
890
0
        return;
891
892
1.13k
    if (legacy_method == NULL)
893
768
        return;
894
368
    nid = EVP_MD_nid(legacy_method);
895
368
    if (*legacy_nid != NID_undef && *legacy_nid != nid) {
896
0
        *legacy_nid = -1;
897
0
        return;
898
0
    }
899
368
    *legacy_nid = nid;
900
368
}
901
#endif
902
903
static int evp_md_cache_constants(EVP_MD *md)
904
432
{
905
432
    int ok, xof = 0, algid_absent = 0;
906
432
    size_t blksz = 0;
907
432
    size_t mdsize = 0;
908
432
    OSSL_PARAM params[5];
909
910
    /*
911
     * Note that these parameters are 'constants' that are only set up
912
     * during the EVP_MD_fetch(). For this reason the XOF functions set the
913
     * md_size to 0, since the output size is unknown.
914
     */
915
432
    params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_BLOCK_SIZE, &blksz);
916
432
    params[1] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_SIZE, &mdsize);
917
432
    params[2] = OSSL_PARAM_construct_int(OSSL_DIGEST_PARAM_XOF, &xof);
918
432
    params[3] = OSSL_PARAM_construct_int(OSSL_DIGEST_PARAM_ALGID_ABSENT,
919
432
                                         &algid_absent);
920
432
    params[4] = OSSL_PARAM_construct_end();
921
432
    ok = evp_do_md_getparams(md, params) > 0;
922
432
    if (mdsize > INT_MAX || blksz > INT_MAX)
923
0
        ok = 0;
924
432
    if (ok) {
925
432
        md->block_size = (int)blksz;
926
432
        md->md_size = (int)mdsize;
927
432
        if (xof)
928
64
            md->flags |= EVP_MD_FLAG_XOF;
929
432
        if (algid_absent)
930
288
            md->flags |= EVP_MD_FLAG_DIGALGID_ABSENT;
931
432
    }
932
432
    return ok;
933
432
}
934
935
static void *evp_md_from_algorithm(int name_id,
936
                                   const OSSL_ALGORITHM *algodef,
937
                                   OSSL_PROVIDER *prov)
938
432
{
939
432
    const OSSL_DISPATCH *fns = algodef->implementation;
940
432
    EVP_MD *md = NULL;
941
432
    int fncnt = 0;
942
943
    /* EVP_MD_fetch() will set the legacy NID if available */
944
432
    if ((md = evp_md_new()) == NULL) {
945
0
        ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
946
0
        return NULL;
947
0
    }
948
949
432
#ifndef FIPS_MODULE
950
432
    md->type = NID_undef;
951
432
    if (!evp_names_do_all(prov, name_id, set_legacy_nid, &md->type)
952
432
            || md->type == -1) {
953
0
        ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
954
0
        goto err;
955
0
    }
956
432
#endif
957
958
432
    md->name_id = name_id;
959
432
    if ((md->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL)
960
0
        goto err;
961
962
432
    md->description = algodef->algorithm_description;
963
964
4.83k
    for (; fns->function_id != 0; fns++) {
965
4.40k
        switch (fns->function_id) {
966
432
        case OSSL_FUNC_DIGEST_NEWCTX:
967
432
            if (md->newctx == NULL) {
968
432
                md->newctx = OSSL_FUNC_digest_newctx(fns);
969
432
                fncnt++;
970
432
            }
971
432
            break;
972
432
        case OSSL_FUNC_DIGEST_INIT:
973
432
            if (md->dinit == NULL) {
974
432
                md->dinit = OSSL_FUNC_digest_init(fns);
975
432
                fncnt++;
976
432
            }
977
432
            break;
978
432
        case OSSL_FUNC_DIGEST_UPDATE:
979
432
            if (md->dupdate == NULL) {
980
432
                md->dupdate = OSSL_FUNC_digest_update(fns);
981
432
                fncnt++;
982
432
            }
983
432
            break;
984
432
        case OSSL_FUNC_DIGEST_FINAL:
985
432
            if (md->dfinal == NULL) {
986
432
                md->dfinal = OSSL_FUNC_digest_final(fns);
987
432
                fncnt++;
988
432
            }
989
432
            break;
990
64
        case OSSL_FUNC_DIGEST_SQUEEZE:
991
64
            if (md->dsqueeze == NULL) {
992
64
                md->dsqueeze = OSSL_FUNC_digest_squeeze(fns);
993
64
                fncnt++;
994
64
            }
995
64
            break;
996
0
        case OSSL_FUNC_DIGEST_DIGEST:
997
0
            if (md->digest == NULL)
998
0
                md->digest = OSSL_FUNC_digest_digest(fns);
999
            /* We don't increment fnct for this as it is stand alone */
1000
0
            break;
1001
432
        case OSSL_FUNC_DIGEST_FREECTX:
1002
432
            if (md->freectx == NULL) {
1003
432
                md->freectx = OSSL_FUNC_digest_freectx(fns);
1004
432
                fncnt++;
1005
432
            }
1006
432
            break;
1007
432
        case OSSL_FUNC_DIGEST_DUPCTX:
1008
432
            if (md->dupctx == NULL)
1009
432
                md->dupctx = OSSL_FUNC_digest_dupctx(fns);
1010
432
            break;
1011
432
        case OSSL_FUNC_DIGEST_GET_PARAMS:
1012
432
            if (md->get_params == NULL)
1013
432
                md->get_params = OSSL_FUNC_digest_get_params(fns);
1014
432
            break;
1015
128
        case OSSL_FUNC_DIGEST_SET_CTX_PARAMS:
1016
128
            if (md->set_ctx_params == NULL)
1017
128
                md->set_ctx_params = OSSL_FUNC_digest_set_ctx_params(fns);
1018
128
            break;
1019
96
        case OSSL_FUNC_DIGEST_GET_CTX_PARAMS:
1020
96
            if (md->get_ctx_params == NULL)
1021
96
                md->get_ctx_params = OSSL_FUNC_digest_get_ctx_params(fns);
1022
96
            break;
1023
432
        case OSSL_FUNC_DIGEST_GETTABLE_PARAMS:
1024
432
            if (md->gettable_params == NULL)
1025
432
                md->gettable_params = OSSL_FUNC_digest_gettable_params(fns);
1026
432
            break;
1027
128
        case OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS:
1028
128
            if (md->settable_ctx_params == NULL)
1029
128
                md->settable_ctx_params =
1030
128
                    OSSL_FUNC_digest_settable_ctx_params(fns);
1031
128
            break;
1032
96
        case OSSL_FUNC_DIGEST_GETTABLE_CTX_PARAMS:
1033
96
            if (md->gettable_ctx_params == NULL)
1034
96
                md->gettable_ctx_params =
1035
96
                    OSSL_FUNC_digest_gettable_ctx_params(fns);
1036
96
            break;
1037
432
        case OSSL_FUNC_DIGEST_COPYCTX:
1038
432
            if (md->copyctx == NULL)
1039
432
                md->copyctx =
1040
432
                    OSSL_FUNC_digest_copyctx(fns);
1041
432
            break;
1042
4.40k
        }
1043
4.40k
    }
1044
432
    if ((fncnt != 0 && fncnt != 5 && fncnt != 6)
1045
432
        || (fncnt == 0 && md->digest == NULL)) {
1046
        /*
1047
         * In order to be a consistent set of functions we either need the
1048
         * whole set of init/update/final etc functions or none of them.
1049
         * The "digest" function can standalone. We at least need one way to
1050
         * generate digests.
1051
         */
1052
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
1053
0
        goto err;
1054
0
    }
1055
432
    if (prov != NULL && !ossl_provider_up_ref(prov))
1056
0
        goto err;
1057
1058
432
    md->prov = prov;
1059
1060
432
    if (!evp_md_cache_constants(md)) {
1061
0
        ERR_raise(ERR_LIB_EVP, EVP_R_CACHE_CONSTANTS_FAILED);
1062
0
        goto err;
1063
0
    }
1064
1065
432
    return md;
1066
1067
0
err:
1068
0
    EVP_MD_free(md);
1069
0
    return NULL;
1070
432
}
1071
1072
static int evp_md_up_ref(void *md)
1073
102k
{
1074
102k
    return EVP_MD_up_ref(md);
1075
102k
}
1076
1077
static void evp_md_free(void *md)
1078
885
{
1079
885
    EVP_MD_free(md);
1080
885
}
1081
1082
EVP_MD *EVP_MD_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
1083
                     const char *properties)
1084
102k
{
1085
102k
    EVP_MD *md =
1086
102k
        evp_generic_fetch(ctx, OSSL_OP_DIGEST, algorithm, properties,
1087
102k
                          evp_md_from_algorithm, evp_md_up_ref, evp_md_free);
1088
1089
102k
    return md;
1090
102k
}
1091
1092
int EVP_MD_up_ref(EVP_MD *md)
1093
204k
{
1094
204k
    int ref = 0;
1095
1096
204k
    if (md->origin == EVP_ORIG_DYNAMIC)
1097
204k
        CRYPTO_UP_REF(&md->refcnt, &ref);
1098
204k
    return 1;
1099
204k
}
1100
1101
void EVP_MD_free(EVP_MD *md)
1102
306k
{
1103
306k
    int i;
1104
1105
306k
    if (md == NULL || md->origin != EVP_ORIG_DYNAMIC)
1106
102k
        return;
1107
1108
204k
    CRYPTO_DOWN_REF(&md->refcnt, &i);
1109
204k
    if (i > 0)
1110
204k
        return;
1111
432
    evp_md_free_int(md);
1112
432
}
1113
1114
void EVP_MD_do_all_provided(OSSL_LIB_CTX *libctx,
1115
                            void (*fn)(EVP_MD *mac, void *arg),
1116
                            void *arg)
1117
0
{
1118
0
    evp_generic_do_all(libctx, OSSL_OP_DIGEST,
1119
0
                       (void (*)(void *, void *))fn, arg,
1120
0
                       evp_md_from_algorithm, evp_md_up_ref, evp_md_free);
1121
0
}
1122
1123
EVP_MD *evp_digest_fetch_from_prov(OSSL_PROVIDER *prov,
1124
                                   const char *algorithm,
1125
                                   const char *properties)
1126
0
{
1127
0
    return evp_generic_fetch_from_prov(prov, OSSL_OP_DIGEST,
1128
0
                                       algorithm, properties,
1129
0
                                       evp_md_from_algorithm,
1130
0
                                       evp_md_up_ref,
1131
0
                                       evp_md_free);
1132
0
}
1133
1134
typedef struct {
1135
    int md_nid;
1136
    int hmac_nid;
1137
} ossl_hmacmd_pair;
1138
1139
static const ossl_hmacmd_pair ossl_hmacmd_pairs[] = {
1140
    {NID_sha1, NID_hmacWithSHA1},
1141
    {NID_md5, NID_hmacWithMD5},
1142
    {NID_sha224, NID_hmacWithSHA224},
1143
    {NID_sha256, NID_hmacWithSHA256},
1144
    {NID_sha384, NID_hmacWithSHA384},
1145
    {NID_sha512, NID_hmacWithSHA512},
1146
    {NID_id_GostR3411_94, NID_id_HMACGostR3411_94},
1147
    {NID_id_GostR3411_2012_256, NID_id_tc26_hmac_gost_3411_2012_256},
1148
    {NID_id_GostR3411_2012_512, NID_id_tc26_hmac_gost_3411_2012_512},
1149
    {NID_sha3_224, NID_hmac_sha3_224},
1150
    {NID_sha3_256, NID_hmac_sha3_256},
1151
    {NID_sha3_384, NID_hmac_sha3_384},
1152
    {NID_sha3_512, NID_hmac_sha3_512},
1153
    {NID_sha512_224, NID_hmacWithSHA512_224},
1154
    {NID_sha512_256, NID_hmacWithSHA512_256}
1155
};
1156
1157
int ossl_hmac2mdnid(int hmac_nid)
1158
0
{
1159
0
    int md_nid = NID_undef;
1160
0
    size_t i;
1161
1162
0
    for (i = 0; i < OSSL_NELEM(ossl_hmacmd_pairs); i++) {
1163
0
        if (ossl_hmacmd_pairs[i].hmac_nid == hmac_nid) {
1164
0
            md_nid = ossl_hmacmd_pairs[i].md_nid;
1165
0
            break;
1166
0
        }
1167
0
    }
1168
1169
0
    return md_nid;
1170
0
}
1171
1172
int ossl_md2hmacnid(int md_nid)
1173
0
{
1174
0
    int hmac_nid = NID_undef;
1175
0
    size_t i;
1176
1177
0
    for (i = 0; i < OSSL_NELEM(ossl_hmacmd_pairs); i++) {
1178
0
        if (ossl_hmacmd_pairs[i].md_nid == md_nid) {
1179
0
            hmac_nid = ossl_hmacmd_pairs[i].hmac_nid;
1180
0
            break;
1181
0
        }
1182
0
    }
1183
1184
0
    return hmac_nid;
1185
0
}