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