Coverage Report

Created: 2026-03-03 06:43

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