Coverage Report

Created: 2025-12-14 06:48

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
233k
{
26
233k
    if (ctx->digest != NULL) {
27
116k
        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
116k
        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
116k
    }
37
233k
}
38
39
void evp_md_ctx_clear_digest(EVP_MD_CTX *ctx, int force, int keep_fetched)
40
116k
{
41
116k
    if (ctx->algctx != NULL) {
42
116k
        if (ctx->digest != NULL && ctx->digest->freectx != NULL)
43
116k
            ctx->digest->freectx(ctx->algctx);
44
116k
        ctx->algctx = NULL;
45
116k
        EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
46
116k
    }
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
116k
    cleanup_old_md_data(ctx, force);
55
116k
    if (force)
56
0
        ctx->digest = NULL;
57
58
    /* Non legacy code, this has to be later than the ctx->digest cleaning */
59
116k
    if (!keep_fetched) {
60
116k
        EVP_MD_free(ctx->fetched_digest);
61
116k
        ctx->fetched_digest = NULL;
62
116k
        ctx->reqdigest = NULL;
63
116k
    }
64
116k
}
65
66
static int evp_md_ctx_reset_ex(EVP_MD_CTX *ctx, int keep_fetched)
67
116k
{
68
116k
    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
116k
    if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX)) {
76
116k
        EVP_PKEY_CTX_free(ctx->pctx);
77
116k
        ctx->pctx = NULL;
78
116k
    }
79
80
116k
    evp_md_ctx_clear_digest(ctx, 0, keep_fetched);
81
116k
    if (!keep_fetched)
82
116k
        OPENSSL_cleanse(ctx, sizeof(*ctx));
83
84
116k
    return 1;
85
116k
}
86
87
/* This call frees resources associated with the context */
88
int EVP_MD_CTX_reset(EVP_MD_CTX *ctx)
89
116k
{
90
116k
    return evp_md_ctx_reset_ex(ctx, 0);
91
116k
}
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
116k
{
121
116k
    return OPENSSL_zalloc(sizeof(EVP_MD_CTX));
122
116k
}
123
124
void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
125
116k
{
126
116k
    if (ctx == NULL)
127
0
        return;
128
129
116k
    EVP_MD_CTX_reset(ctx);
130
116k
    OPENSSL_free(ctx);
131
116k
}
132
133
int evp_md_ctx_free_algctx(EVP_MD_CTX *ctx)
134
116k
{
135
116k
    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
116k
    return 1;
145
116k
}
146
147
static int evp_md_init_internal(EVP_MD_CTX *ctx, const EVP_MD *type,
148
    const OSSL_PARAM params[])
149
116k
{
150
116k
#if !defined(FIPS_MODULE)
151
116k
    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
116k
#endif
168
169
116k
    EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_CLEANED | EVP_MD_CTX_FLAG_FINALISED);
170
171
116k
    if (type != NULL) {
172
116k
        ctx->reqdigest = type;
173
116k
    } 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
116k
    if ((ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0
186
116k
        || (type != NULL && type->origin == EVP_ORIG_METH)
187
116k
        || (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
116k
    cleanup_old_md_data(ctx, 1);
200
201
    /* Start of non-legacy code below */
202
116k
    if (ossl_likely(ctx->digest == type)) {
203
0
        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
116k
    } else {
208
116k
        if (!evp_md_ctx_free_algctx(ctx))
209
0
            return 0;
210
116k
    }
211
212
116k
    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
116k
    if (ossl_unlikely(type->prov != NULL && ctx->fetched_digest != type)) {
235
116k
        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
116k
        EVP_MD_free(ctx->fetched_digest);
240
116k
        ctx->fetched_digest = (EVP_MD *)type;
241
116k
    }
242
116k
    ctx->digest = type;
243
116k
    if (ctx->algctx == NULL) {
244
116k
        ctx->algctx = ctx->digest->newctx(ossl_provider_ctx(type->prov));
245
116k
        if (ctx->algctx == NULL) {
246
0
            ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
247
0
            return 0;
248
0
        }
249
116k
    }
250
251
116k
    if (ctx->digest->dinit == NULL) {
252
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
253
0
        return 0;
254
0
    }
255
256
116k
    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
116k
{
302
116k
    if (!ossl_assert(impl == NULL))
303
0
        return 0;
304
116k
    return evp_md_init_internal(ctx, type, NULL);
305
116k
}
306
307
int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t count)
308
116k
{
309
116k
    if (ossl_unlikely(count == 0))
310
0
        return 1;
311
312
116k
    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
116k
    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
116k
    if (ctx->digest == NULL
339
116k
        || ctx->digest->prov == NULL
340
116k
        || ossl_unlikely((ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0))
341
0
        goto legacy;
342
343
116k
    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
116k
    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
116k
}
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
116k
{
366
116k
    int ret, sz;
367
116k
    size_t size = 0;
368
116k
    size_t mdsize = 0;
369
370
116k
    if (ossl_unlikely(ctx->digest == NULL))
371
0
        return 0;
372
373
116k
    sz = EVP_MD_CTX_get_size(ctx);
374
116k
    if (ossl_unlikely(sz < 0))
375
0
        return 0;
376
116k
    mdsize = sz;
377
116k
    if (ossl_unlikely(ctx->digest->prov == NULL))
378
0
        goto legacy;
379
380
116k
    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
116k
    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
116k
    ret = ctx->digest->dfinal(ctx->algctx, md, &size, mdsize);
391
392
116k
    ctx->flags |= EVP_MD_CTX_FLAG_FINALISED;
393
394
116k
    if (isize != NULL) {
395
116k
        if (ossl_likely(size <= UINT_MAX)) {
396
116k
            *isize = (unsigned int)size;
397
116k
        } else {
398
0
            ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
399
0
            ret = 0;
400
0
        }
401
116k
    }
402
403
116k
    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
116k
}
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
int EVP_MD_CTX_serialize(EVP_MD_CTX *ctx, unsigned char *out, size_t *outlen)
498
0
{
499
0
    if (ctx->digest == NULL) {
500
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_NULL_ALGORITHM);
501
0
        return 0;
502
0
    }
503
504
0
    if (ctx->digest->prov == NULL) {
505
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
506
0
        return 0;
507
0
    }
508
509
0
    if (ctx->digest->serialize == NULL) {
510
0
        ERR_raise(ERR_LIB_EVP, EVP_R_METHOD_NOT_SUPPORTED);
511
0
        return 0;
512
0
    }
513
514
0
    if (ossl_unlikely((ctx->flags & EVP_MD_CTX_FLAG_FINALISED) != 0)) {
515
0
        ERR_raise(ERR_LIB_EVP, EVP_R_CONTEXT_FINALIZED);
516
0
        return 0;
517
0
    }
518
519
0
    return ctx->digest->serialize(ctx->algctx, out, outlen);
520
0
}
521
522
int EVP_MD_CTX_deserialize(EVP_MD_CTX *ctx, const unsigned char *in,
523
    size_t inlen)
524
0
{
525
0
    if (ctx->digest == NULL) {
526
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_NULL_ALGORITHM);
527
0
        return 0;
528
0
    }
529
530
0
    if (ctx->digest->prov == NULL) {
531
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
532
0
        return 0;
533
0
    }
534
535
0
    if (ctx->digest->deserialize == NULL) {
536
0
        ERR_raise(ERR_LIB_EVP, EVP_R_METHOD_NOT_SUPPORTED);
537
0
        return 0;
538
0
    }
539
540
0
    if (ossl_unlikely((ctx->flags & EVP_MD_CTX_FLAG_FINALISED) != 0)) {
541
0
        ERR_raise(ERR_LIB_EVP, EVP_R_CONTEXT_FINALIZED);
542
0
        return 0;
543
0
    }
544
545
0
    return ctx->digest->deserialize(ctx->algctx, in, inlen);
546
0
}
547
548
EVP_MD_CTX *EVP_MD_CTX_dup(const EVP_MD_CTX *in)
549
0
{
550
0
    EVP_MD_CTX *out = EVP_MD_CTX_new();
551
552
0
    if (out != NULL && !EVP_MD_CTX_copy_ex(out, in)) {
553
0
        EVP_MD_CTX_free(out);
554
0
        out = NULL;
555
0
    }
556
0
    return out;
557
0
}
558
559
int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in)
560
0
{
561
0
    EVP_MD_CTX_reset(out);
562
0
    return EVP_MD_CTX_copy_ex(out, in);
563
0
}
564
565
int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in)
566
0
{
567
0
    int digest_change = 0;
568
0
    unsigned char *tmp_buf;
569
570
0
    if (in == NULL) {
571
0
        ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
572
0
        return 0;
573
0
    }
574
575
0
    if (in->digest == NULL) {
576
        /* copying uninitialized digest context */
577
0
        EVP_MD_CTX_reset(out);
578
0
        if (out->fetched_digest != NULL)
579
0
            EVP_MD_free(out->fetched_digest);
580
0
        *out = *in;
581
0
        goto clone_pkey;
582
0
    }
583
584
0
    if (in->digest->prov == NULL
585
0
        || (in->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0)
586
0
        goto legacy;
587
588
0
    if (in->digest->dupctx == NULL) {
589
0
        ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX);
590
0
        return 0;
591
0
    }
592
593
0
    if (out->digest == in->digest && in->digest->copyctx != NULL) {
594
595
0
        in->digest->copyctx(out->algctx, in->algctx);
596
597
0
        EVP_PKEY_CTX_free(out->pctx);
598
0
        out->pctx = NULL;
599
0
        cleanup_old_md_data(out, 0);
600
601
0
        out->flags = in->flags;
602
0
        out->update = in->update;
603
0
    } else {
604
0
        evp_md_ctx_reset_ex(out, 1);
605
0
        digest_change = (out->fetched_digest != in->fetched_digest);
606
607
0
        if (digest_change && in->fetched_digest != NULL
608
0
            && !EVP_MD_up_ref(in->fetched_digest))
609
0
            return 0;
610
0
        if (digest_change && out->fetched_digest != NULL)
611
0
            EVP_MD_free(out->fetched_digest);
612
0
        *out = *in;
613
        /* NULL out pointers in case of error */
614
0
        out->pctx = NULL;
615
0
        out->algctx = NULL;
616
617
0
        if (in->algctx != NULL) {
618
0
            out->algctx = in->digest->dupctx(in->algctx);
619
0
            if (out->algctx == NULL) {
620
0
                ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX);
621
0
                return 0;
622
0
            }
623
0
        }
624
0
    }
625
626
0
clone_pkey:
627
    /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
628
0
    EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
629
0
#ifndef FIPS_MODULE
630
0
    if (in->pctx != NULL) {
631
0
        out->pctx = EVP_PKEY_CTX_dup(in->pctx);
632
0
        if (out->pctx == NULL) {
633
0
            ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX);
634
0
            EVP_MD_CTX_reset(out);
635
0
            return 0;
636
0
        }
637
0
    }
638
0
#endif
639
640
0
    return 1;
641
642
    /* Code below to be removed when legacy support is dropped. */
643
0
legacy:
644
645
0
    if (out->digest == in->digest) {
646
0
        tmp_buf = out->md_data;
647
0
        EVP_MD_CTX_set_flags(out, EVP_MD_CTX_FLAG_REUSE);
648
0
    } else
649
0
        tmp_buf = NULL;
650
0
    EVP_MD_CTX_reset(out);
651
0
    memcpy(out, in, sizeof(*out));
652
653
    /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
654
0
    EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
655
656
    /* Null these variables, since they are getting fixed up
657
     * properly below.  Anything else may cause a memleak and/or
658
     * double free if any of the memory allocations below fail
659
     */
660
0
    out->md_data = NULL;
661
0
    out->pctx = NULL;
662
663
0
    if (in->md_data && out->digest->ctx_size) {
664
0
        if (tmp_buf)
665
0
            out->md_data = tmp_buf;
666
0
        else {
667
0
            out->md_data = OPENSSL_malloc(out->digest->ctx_size);
668
0
            if (out->md_data == NULL)
669
0
                return 0;
670
0
        }
671
0
        memcpy(out->md_data, in->md_data, out->digest->ctx_size);
672
0
    }
673
674
0
    out->update = in->update;
675
676
0
#ifndef FIPS_MODULE
677
0
    if (in->pctx) {
678
0
        out->pctx = EVP_PKEY_CTX_dup(in->pctx);
679
0
        if (!out->pctx) {
680
0
            EVP_MD_CTX_reset(out);
681
0
            return 0;
682
0
        }
683
0
    }
684
0
#endif
685
686
0
    if (out->digest->copy)
687
0
        return out->digest->copy(out, in);
688
689
0
    return 1;
690
0
}
691
692
int EVP_Digest(const void *data, size_t count,
693
    unsigned char *md, unsigned int *size, const EVP_MD *type,
694
    ENGINE *impl)
695
116k
{
696
116k
    EVP_MD_CTX *ctx;
697
116k
    int ret;
698
699
116k
    if (!ossl_assert(impl == NULL))
700
0
        return 0;
701
702
116k
    ctx = EVP_MD_CTX_new();
703
116k
    if (ctx == NULL)
704
0
        return 0;
705
116k
    EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT);
706
116k
    ret = EVP_DigestInit_ex(ctx, type, NULL)
707
116k
        && EVP_DigestUpdate(ctx, data, count)
708
116k
        && EVP_DigestFinal_ex(ctx, md, size);
709
116k
    EVP_MD_CTX_free(ctx);
710
711
116k
    return ret;
712
116k
}
713
714
int EVP_Q_digest(OSSL_LIB_CTX *libctx, const char *name, const char *propq,
715
    const void *data, size_t datalen,
716
    unsigned char *md, size_t *mdlen)
717
116k
{
718
116k
    EVP_MD *digest = EVP_MD_fetch(libctx, name, propq);
719
116k
    unsigned int temp = 0;
720
116k
    int ret = 0;
721
722
116k
    if (digest != NULL) {
723
116k
        ret = EVP_Digest(data, datalen, md, &temp, digest, NULL);
724
116k
        EVP_MD_free(digest);
725
116k
    }
726
116k
    if (mdlen != NULL)
727
0
        *mdlen = temp;
728
116k
    return ret;
729
116k
}
730
731
int EVP_MD_get_params(const EVP_MD *digest, OSSL_PARAM params[])
732
0
{
733
0
    if (digest != NULL && digest->get_params != NULL)
734
0
        return digest->get_params(params);
735
0
    return 0;
736
0
}
737
738
const OSSL_PARAM *EVP_MD_gettable_params(const EVP_MD *digest)
739
0
{
740
0
    if (digest != NULL && digest->gettable_params != NULL)
741
0
        return digest->gettable_params(
742
0
            ossl_provider_ctx(EVP_MD_get0_provider(digest)));
743
0
    return NULL;
744
0
}
745
746
int EVP_MD_CTX_set_params(EVP_MD_CTX *ctx, const OSSL_PARAM params[])
747
0
{
748
0
    EVP_PKEY_CTX *pctx = ctx->pctx;
749
750
    /* If we have a pctx then we should try that first */
751
0
    if (ossl_unlikely(pctx != NULL)
752
0
        && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
753
0
            || pctx->operation == EVP_PKEY_OP_SIGNCTX)
754
0
        && pctx->op.sig.algctx != NULL
755
0
        && pctx->op.sig.signature->set_ctx_md_params != NULL)
756
0
        return pctx->op.sig.signature->set_ctx_md_params(pctx->op.sig.algctx,
757
0
            params);
758
759
0
    if (ossl_likely(ctx->digest != NULL && ctx->digest->set_ctx_params != NULL))
760
0
        return ctx->digest->set_ctx_params(ctx->algctx, params);
761
762
0
    return 0;
763
0
}
764
765
const OSSL_PARAM *EVP_MD_settable_ctx_params(const EVP_MD *md)
766
0
{
767
0
    void *provctx;
768
769
0
    if (md != NULL && md->settable_ctx_params != NULL) {
770
0
        provctx = ossl_provider_ctx(EVP_MD_get0_provider(md));
771
0
        return md->settable_ctx_params(NULL, provctx);
772
0
    }
773
0
    return NULL;
774
0
}
775
776
const OSSL_PARAM *EVP_MD_CTX_settable_params(EVP_MD_CTX *ctx)
777
0
{
778
0
    EVP_PKEY_CTX *pctx;
779
0
    void *alg;
780
781
0
    if (ctx == NULL)
782
0
        return NULL;
783
784
    /* If we have a pctx then we should try that first */
785
0
    pctx = ctx->pctx;
786
0
    if (pctx != NULL
787
0
        && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
788
0
            || pctx->operation == EVP_PKEY_OP_SIGNCTX)
789
0
        && pctx->op.sig.algctx != NULL
790
0
        && pctx->op.sig.signature->settable_ctx_md_params != NULL)
791
0
        return pctx->op.sig.signature->settable_ctx_md_params(
792
0
            pctx->op.sig.algctx);
793
794
0
    if (ctx->digest != NULL && ctx->digest->settable_ctx_params != NULL) {
795
0
        alg = ossl_provider_ctx(EVP_MD_get0_provider(ctx->digest));
796
0
        return ctx->digest->settable_ctx_params(ctx->algctx, alg);
797
0
    }
798
799
0
    return NULL;
800
0
}
801
802
int EVP_MD_CTX_get_params(EVP_MD_CTX *ctx, OSSL_PARAM params[])
803
0
{
804
0
    EVP_PKEY_CTX *pctx = ctx->pctx;
805
806
    /* If we have a pctx then we should try that first */
807
0
    if (pctx != NULL
808
0
        && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
809
0
            || pctx->operation == EVP_PKEY_OP_SIGNCTX)
810
0
        && pctx->op.sig.algctx != NULL
811
0
        && pctx->op.sig.signature->get_ctx_md_params != NULL)
812
0
        return pctx->op.sig.signature->get_ctx_md_params(pctx->op.sig.algctx,
813
0
            params);
814
815
0
    if (ctx->digest != NULL && ctx->digest->get_ctx_params != NULL)
816
0
        return ctx->digest->get_ctx_params(ctx->algctx, params);
817
818
0
    return 0;
819
0
}
820
821
const OSSL_PARAM *EVP_MD_gettable_ctx_params(const EVP_MD *md)
822
0
{
823
0
    void *provctx;
824
825
0
    if (md != NULL && md->gettable_ctx_params != NULL) {
826
0
        provctx = ossl_provider_ctx(EVP_MD_get0_provider(md));
827
0
        return md->gettable_ctx_params(NULL, provctx);
828
0
    }
829
0
    return NULL;
830
0
}
831
832
const OSSL_PARAM *EVP_MD_CTX_gettable_params(EVP_MD_CTX *ctx)
833
116k
{
834
116k
    EVP_PKEY_CTX *pctx;
835
116k
    void *provctx;
836
837
116k
    if (ossl_unlikely(ctx == NULL))
838
0
        return NULL;
839
840
    /* If we have a pctx then we should try that first */
841
116k
    pctx = ctx->pctx;
842
116k
    if (ossl_unlikely(pctx != NULL)
843
0
        && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
844
0
            || pctx->operation == EVP_PKEY_OP_SIGNCTX)
845
0
        && pctx->op.sig.algctx != NULL
846
0
        && pctx->op.sig.signature->gettable_ctx_md_params != NULL)
847
0
        return pctx->op.sig.signature->gettable_ctx_md_params(
848
0
            pctx->op.sig.algctx);
849
850
116k
    if (ossl_unlikely(ctx->digest != NULL
851
116k
            && ctx->digest->gettable_ctx_params != NULL)) {
852
0
        provctx = ossl_provider_ctx(EVP_MD_get0_provider(ctx->digest));
853
0
        return ctx->digest->gettable_ctx_params(ctx->algctx, provctx);
854
0
    }
855
116k
    return NULL;
856
116k
}
857
858
int EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void *p2)
859
0
{
860
0
    int ret = EVP_CTRL_RET_UNSUPPORTED;
861
0
    int set_params = 1;
862
0
    size_t sz;
863
0
    OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
864
865
0
    if (ctx == NULL) {
866
0
        ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
867
0
        return 0;
868
0
    }
869
870
0
    if (ctx->digest != NULL && ctx->digest->prov == NULL)
871
0
        goto legacy;
872
873
0
    switch (cmd) {
874
0
    case EVP_MD_CTRL_XOF_LEN:
875
0
        sz = (size_t)p1;
876
0
        params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN, &sz);
877
0
        break;
878
0
    case EVP_MD_CTRL_MICALG:
879
0
        set_params = 0;
880
0
        params[0] = OSSL_PARAM_construct_utf8_string(OSSL_DIGEST_PARAM_MICALG,
881
0
            p2, p1 ? p1 : 9999);
882
0
        break;
883
0
    case EVP_CTRL_SSL3_MASTER_SECRET:
884
0
        params[0] = OSSL_PARAM_construct_octet_string(OSSL_DIGEST_PARAM_SSL3_MS,
885
0
            p2, p1);
886
0
        break;
887
0
    default:
888
0
        goto conclude;
889
0
    }
890
891
0
    if (set_params)
892
0
        ret = EVP_MD_CTX_set_params(ctx, params);
893
0
    else
894
0
        ret = EVP_MD_CTX_get_params(ctx, params);
895
0
    goto conclude;
896
897
    /* Code below to be removed when legacy support is dropped. */
898
0
legacy:
899
0
    if (ctx->digest->md_ctrl == NULL) {
900
0
        ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_NOT_IMPLEMENTED);
901
0
        return 0;
902
0
    }
903
904
0
    ret = ctx->digest->md_ctrl(ctx, cmd, p1, p2);
905
0
conclude:
906
0
    if (ret <= 0)
907
0
        return 0;
908
0
    return ret;
909
0
}
910
911
EVP_MD *evp_md_new(void)
912
432
{
913
432
    EVP_MD *md = OPENSSL_zalloc(sizeof(*md));
914
915
432
    if (md != NULL && !CRYPTO_NEW_REF(&md->refcnt, 1)) {
916
0
        OPENSSL_free(md);
917
0
        return NULL;
918
0
    }
919
432
    return md;
920
432
}
921
922
/*
923
 * FIPS module note: since internal fetches will be entirely
924
 * provider based, we know that none of its code depends on legacy
925
 * NIDs or any functionality that use them.
926
 */
927
#ifndef FIPS_MODULE
928
static void set_legacy_nid(const char *name, void *vlegacy_nid)
929
1.13k
{
930
1.13k
    int nid;
931
1.13k
    int *legacy_nid = vlegacy_nid;
932
    /*
933
     * We use lowest level function to get the associated method, because
934
     * higher level functions such as EVP_get_digestbyname() have changed
935
     * to look at providers too.
936
     */
937
1.13k
    const void *legacy_method = OBJ_NAME_get(name, OBJ_NAME_TYPE_MD_METH);
938
939
1.13k
    if (*legacy_nid == -1) /* We found a clash already */
940
0
        return;
941
942
1.13k
    if (legacy_method == NULL)
943
768
        return;
944
368
    nid = EVP_MD_nid(legacy_method);
945
368
    if (*legacy_nid != NID_undef && *legacy_nid != nid) {
946
0
        *legacy_nid = -1;
947
0
        return;
948
0
    }
949
368
    *legacy_nid = nid;
950
368
}
951
#endif
952
953
static int evp_md_cache_constants(EVP_MD *md)
954
432
{
955
432
    int ok, xof = 0, algid_absent = 0;
956
432
    size_t blksz = 0;
957
432
    size_t mdsize = 0;
958
432
    OSSL_PARAM params[5];
959
960
    /*
961
     * Note that these parameters are 'constants' that are only set up
962
     * during the EVP_MD_fetch(). For this reason the XOF functions set the
963
     * md_size to 0, since the output size is unknown.
964
     */
965
432
    params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_BLOCK_SIZE, &blksz);
966
432
    params[1] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_SIZE, &mdsize);
967
432
    params[2] = OSSL_PARAM_construct_int(OSSL_DIGEST_PARAM_XOF, &xof);
968
432
    params[3] = OSSL_PARAM_construct_int(OSSL_DIGEST_PARAM_ALGID_ABSENT,
969
432
        &algid_absent);
970
432
    params[4] = OSSL_PARAM_construct_end();
971
432
    ok = evp_do_md_getparams(md, params) > 0;
972
432
    if (mdsize > INT_MAX || blksz > INT_MAX)
973
0
        ok = 0;
974
432
    if (ok) {
975
432
        md->block_size = (int)blksz;
976
432
        md->md_size = (int)mdsize;
977
432
        if (xof)
978
64
            md->flags |= EVP_MD_FLAG_XOF;
979
432
        if (algid_absent)
980
288
            md->flags |= EVP_MD_FLAG_DIGALGID_ABSENT;
981
432
    }
982
432
    return ok;
983
432
}
984
985
static void *evp_md_from_algorithm(int name_id,
986
    const OSSL_ALGORITHM *algodef,
987
    OSSL_PROVIDER *prov)
988
432
{
989
432
    const OSSL_DISPATCH *fns = algodef->implementation;
990
432
    EVP_MD *md = NULL;
991
432
    int fncnt = 0;
992
993
    /* EVP_MD_fetch() will set the legacy NID if available */
994
432
    if ((md = evp_md_new()) == NULL) {
995
0
        ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
996
0
        return NULL;
997
0
    }
998
999
432
#ifndef FIPS_MODULE
1000
432
    md->type = NID_undef;
1001
432
    if (!evp_names_do_all(prov, name_id, set_legacy_nid, &md->type)
1002
432
        || md->type == -1) {
1003
0
        ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
1004
0
        goto err;
1005
0
    }
1006
432
#endif
1007
1008
432
    md->name_id = name_id;
1009
432
    if ((md->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL)
1010
0
        goto err;
1011
1012
432
    md->description = algodef->algorithm_description;
1013
1014
5.44k
    for (; fns->function_id != 0; fns++) {
1015
5.00k
        switch (fns->function_id) {
1016
432
        case OSSL_FUNC_DIGEST_NEWCTX:
1017
432
            if (md->newctx == NULL) {
1018
432
                md->newctx = OSSL_FUNC_digest_newctx(fns);
1019
432
                fncnt++;
1020
432
            }
1021
432
            break;
1022
432
        case OSSL_FUNC_DIGEST_INIT:
1023
432
            if (md->dinit == NULL) {
1024
432
                md->dinit = OSSL_FUNC_digest_init(fns);
1025
432
                fncnt++;
1026
432
            }
1027
432
            break;
1028
432
        case OSSL_FUNC_DIGEST_UPDATE:
1029
432
            if (md->dupdate == NULL) {
1030
432
                md->dupdate = OSSL_FUNC_digest_update(fns);
1031
432
                fncnt++;
1032
432
            }
1033
432
            break;
1034
432
        case OSSL_FUNC_DIGEST_FINAL:
1035
432
            if (md->dfinal == NULL) {
1036
432
                md->dfinal = OSSL_FUNC_digest_final(fns);
1037
432
                fncnt++;
1038
432
            }
1039
432
            break;
1040
64
        case OSSL_FUNC_DIGEST_SQUEEZE:
1041
64
            if (md->dsqueeze == NULL) {
1042
64
                md->dsqueeze = OSSL_FUNC_digest_squeeze(fns);
1043
64
                fncnt++;
1044
64
            }
1045
64
            break;
1046
0
        case OSSL_FUNC_DIGEST_DIGEST:
1047
0
            if (md->digest == NULL)
1048
0
                md->digest = OSSL_FUNC_digest_digest(fns);
1049
            /* We don't increment fnct for this as it is stand alone */
1050
0
            break;
1051
432
        case OSSL_FUNC_DIGEST_FREECTX:
1052
432
            if (md->freectx == NULL) {
1053
432
                md->freectx = OSSL_FUNC_digest_freectx(fns);
1054
432
                fncnt++;
1055
432
            }
1056
432
            break;
1057
432
        case OSSL_FUNC_DIGEST_DUPCTX:
1058
432
            if (md->dupctx == NULL)
1059
432
                md->dupctx = OSSL_FUNC_digest_dupctx(fns);
1060
432
            break;
1061
432
        case OSSL_FUNC_DIGEST_GET_PARAMS:
1062
432
            if (md->get_params == NULL)
1063
432
                md->get_params = OSSL_FUNC_digest_get_params(fns);
1064
432
            break;
1065
128
        case OSSL_FUNC_DIGEST_SET_CTX_PARAMS:
1066
128
            if (md->set_ctx_params == NULL)
1067
128
                md->set_ctx_params = OSSL_FUNC_digest_set_ctx_params(fns);
1068
128
            break;
1069
96
        case OSSL_FUNC_DIGEST_GET_CTX_PARAMS:
1070
96
            if (md->get_ctx_params == NULL)
1071
96
                md->get_ctx_params = OSSL_FUNC_digest_get_ctx_params(fns);
1072
96
            break;
1073
432
        case OSSL_FUNC_DIGEST_GETTABLE_PARAMS:
1074
432
            if (md->gettable_params == NULL)
1075
432
                md->gettable_params = OSSL_FUNC_digest_gettable_params(fns);
1076
432
            break;
1077
128
        case OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS:
1078
128
            if (md->settable_ctx_params == NULL)
1079
128
                md->settable_ctx_params = OSSL_FUNC_digest_settable_ctx_params(fns);
1080
128
            break;
1081
96
        case OSSL_FUNC_DIGEST_GETTABLE_CTX_PARAMS:
1082
96
            if (md->gettable_ctx_params == NULL)
1083
96
                md->gettable_ctx_params = OSSL_FUNC_digest_gettable_ctx_params(fns);
1084
96
            break;
1085
432
        case OSSL_FUNC_DIGEST_COPYCTX:
1086
432
            if (md->copyctx == NULL)
1087
432
                md->copyctx = OSSL_FUNC_digest_copyctx(fns);
1088
432
            break;
1089
304
        case OSSL_FUNC_DIGEST_SERIALIZE:
1090
304
            if (md->serialize == NULL)
1091
304
                md->serialize = OSSL_FUNC_digest_serialize(fns);
1092
304
            break;
1093
304
        case OSSL_FUNC_DIGEST_DESERIALIZE:
1094
304
            if (md->deserialize == NULL)
1095
304
                md->deserialize = OSSL_FUNC_digest_deserialize(fns);
1096
304
            break;
1097
5.00k
        }
1098
5.00k
    }
1099
432
    if ((fncnt != 0 && fncnt != 5 && fncnt != 6)
1100
432
        || (fncnt == 0 && md->digest == NULL)) {
1101
        /*
1102
         * In order to be a consistent set of functions we either need the
1103
         * whole set of init/update/final etc functions or none of them.
1104
         * The "digest" function can standalone. We at least need one way to
1105
         * generate digests.
1106
         */
1107
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
1108
0
        goto err;
1109
0
    }
1110
432
    if (prov != NULL && !ossl_provider_up_ref(prov))
1111
0
        goto err;
1112
1113
432
    md->prov = prov;
1114
1115
432
    if (!evp_md_cache_constants(md)) {
1116
0
        ERR_raise(ERR_LIB_EVP, EVP_R_CACHE_CONSTANTS_FAILED);
1117
0
        goto err;
1118
0
    }
1119
1120
432
    return md;
1121
1122
0
err:
1123
0
    EVP_MD_free(md);
1124
0
    return NULL;
1125
432
}
1126
1127
static int evp_md_up_ref(void *md)
1128
117k
{
1129
117k
    return EVP_MD_up_ref(md);
1130
117k
}
1131
1132
static void evp_md_free(void *md)
1133
885
{
1134
885
    EVP_MD_free(md);
1135
885
}
1136
1137
EVP_MD *EVP_MD_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
1138
    const char *properties)
1139
116k
{
1140
116k
    EVP_MD *md = evp_generic_fetch(ctx, OSSL_OP_DIGEST, algorithm, properties,
1141
116k
        evp_md_from_algorithm, evp_md_up_ref, evp_md_free);
1142
1143
116k
    return md;
1144
116k
}
1145
1146
int EVP_MD_up_ref(EVP_MD *md)
1147
233k
{
1148
233k
    int ref = 0;
1149
1150
233k
    if (md->origin == EVP_ORIG_DYNAMIC)
1151
233k
        CRYPTO_UP_REF(&md->refcnt, &ref);
1152
233k
    return 1;
1153
233k
}
1154
1155
void EVP_MD_free(EVP_MD *md)
1156
350k
{
1157
350k
    int i;
1158
1159
350k
    if (md == NULL || md->origin != EVP_ORIG_DYNAMIC)
1160
116k
        return;
1161
1162
234k
    CRYPTO_DOWN_REF(&md->refcnt, &i);
1163
234k
    if (i > 0)
1164
233k
        return;
1165
432
    evp_md_free_int(md);
1166
432
}
1167
1168
void EVP_MD_do_all_provided(OSSL_LIB_CTX *libctx,
1169
    void (*fn)(EVP_MD *mac, void *arg),
1170
    void *arg)
1171
0
{
1172
0
    evp_generic_do_all(libctx, OSSL_OP_DIGEST,
1173
0
        (void (*)(void *, void *))fn, arg,
1174
0
        evp_md_from_algorithm, evp_md_up_ref, evp_md_free);
1175
0
}
1176
1177
EVP_MD *evp_digest_fetch_from_prov(OSSL_PROVIDER *prov,
1178
    const char *algorithm,
1179
    const char *properties)
1180
0
{
1181
0
    return evp_generic_fetch_from_prov(prov, OSSL_OP_DIGEST,
1182
0
        algorithm, properties,
1183
0
        evp_md_from_algorithm,
1184
0
        evp_md_up_ref,
1185
0
        evp_md_free);
1186
0
}
1187
1188
typedef struct {
1189
    int md_nid;
1190
    int hmac_nid;
1191
} ossl_hmacmd_pair;
1192
1193
static const ossl_hmacmd_pair ossl_hmacmd_pairs[] = {
1194
    { NID_sha1, NID_hmacWithSHA1 },
1195
    { NID_md5, NID_hmacWithMD5 },
1196
    { NID_sha224, NID_hmacWithSHA224 },
1197
    { NID_sha256, NID_hmacWithSHA256 },
1198
    { NID_sha384, NID_hmacWithSHA384 },
1199
    { NID_sha512, NID_hmacWithSHA512 },
1200
    { NID_id_GostR3411_94, NID_id_HMACGostR3411_94 },
1201
    { NID_id_GostR3411_2012_256, NID_id_tc26_hmac_gost_3411_2012_256 },
1202
    { NID_id_GostR3411_2012_512, NID_id_tc26_hmac_gost_3411_2012_512 },
1203
    { NID_sha3_224, NID_hmac_sha3_224 },
1204
    { NID_sha3_256, NID_hmac_sha3_256 },
1205
    { NID_sha3_384, NID_hmac_sha3_384 },
1206
    { NID_sha3_512, NID_hmac_sha3_512 },
1207
    { NID_sha512_224, NID_hmacWithSHA512_224 },
1208
    { NID_sha512_256, NID_hmacWithSHA512_256 }
1209
};
1210
1211
int ossl_hmac2mdnid(int hmac_nid)
1212
0
{
1213
0
    int md_nid = NID_undef;
1214
0
    size_t i;
1215
1216
0
    for (i = 0; i < OSSL_NELEM(ossl_hmacmd_pairs); i++) {
1217
0
        if (ossl_hmacmd_pairs[i].hmac_nid == hmac_nid) {
1218
0
            md_nid = ossl_hmacmd_pairs[i].md_nid;
1219
0
            break;
1220
0
        }
1221
0
    }
1222
1223
0
    return md_nid;
1224
0
}
1225
1226
int ossl_md2hmacnid(int md_nid)
1227
0
{
1228
0
    int hmac_nid = NID_undef;
1229
0
    size_t i;
1230
1231
0
    for (i = 0; i < OSSL_NELEM(ossl_hmacmd_pairs); i++) {
1232
0
        if (ossl_hmacmd_pairs[i].md_nid == md_nid) {
1233
0
            hmac_nid = ossl_hmacmd_pairs[i].hmac_nid;
1234
0
            break;
1235
0
        }
1236
0
    }
1237
1238
0
    return hmac_nid;
1239
0
}