Coverage Report

Created: 2026-01-09 07:00

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