5ef745225fdf30e940f39ccbf526100d24aae7ee
[openssl.git] / crypto / evp / digest.c
1 /*
2  * Copyright 1995-2019 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/engine.h>
14 #include <openssl/params.h>
15 #include <openssl/core_names.h>
16 #include "internal/cryptlib.h"
17 #include "internal/evp_int.h"
18 #include "internal/provider.h"
19 #include "evp_locl.h"
20
21 /* This call frees resources associated with the context */
22 int EVP_MD_CTX_reset(EVP_MD_CTX *ctx)
23 {
24     if (ctx == NULL)
25         return 1;
26
27     if (ctx->digest == NULL || ctx->digest->prov == NULL)
28         goto legacy;
29
30     if (ctx->provctx != NULL) {
31         if (ctx->digest->freectx != NULL)
32             ctx->digest->freectx(ctx->provctx);
33         ctx->provctx = NULL;
34         EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
35     }
36
37     if (ctx->pctx != NULL)
38         goto legacy;
39
40     return 1;
41
42     /* TODO(3.0): Remove legacy code below */
43  legacy:
44
45     /*
46      * Don't assume ctx->md_data was cleaned in EVP_Digest_Final, because
47      * sometimes only copies of the context are ever finalised.
48      */
49     if (ctx->digest && ctx->digest->cleanup
50         && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_CLEANED))
51         ctx->digest->cleanup(ctx);
52     if (ctx->digest && ctx->digest->ctx_size && ctx->md_data
53         && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_REUSE)) {
54         OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
55     }
56     /*
57      * pctx should be freed by the user of EVP_MD_CTX
58      * if EVP_MD_CTX_FLAG_KEEP_PKEY_CTX is set
59      */
60 #ifndef FIPS_MODE
61     /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
62     if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX))
63         EVP_PKEY_CTX_free(ctx->pctx);
64
65 # ifndef OPENSSL_NO_ENGINE
66     ENGINE_finish(ctx->engine);
67 # endif
68 #endif
69     OPENSSL_cleanse(ctx, sizeof(*ctx));
70
71     return 1;
72 }
73
74 EVP_MD_CTX *EVP_MD_CTX_new(void)
75 {
76     return OPENSSL_zalloc(sizeof(EVP_MD_CTX));
77 }
78
79 void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
80 {
81     if (ctx == NULL)
82         return;
83
84     if (ctx->digest == NULL || ctx->digest->prov == NULL)
85         goto legacy;
86
87     EVP_MD_CTX_reset(ctx);
88
89     EVP_MD_free(ctx->fetched_digest);
90     ctx->fetched_digest = NULL;
91     ctx->digest = NULL;
92     ctx->reqdigest = NULL;
93
94     OPENSSL_free(ctx);
95     return;
96
97     /* TODO(3.0): Remove legacy code below */
98  legacy:
99     EVP_MD_CTX_reset(ctx);
100     OPENSSL_free(ctx);
101 }
102
103 int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type)
104 {
105     EVP_MD_CTX_reset(ctx);
106     return EVP_DigestInit_ex(ctx, type, NULL);
107 }
108
109 int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
110 {
111 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
112     ENGINE *tmpimpl = NULL;
113 #endif
114
115     EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
116
117     if (type != NULL)
118         ctx->reqdigest = type;
119
120     /* TODO(3.0): Legacy work around code below. Remove this */
121 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
122     /*
123      * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
124      * this context may already have an ENGINE! Try to avoid releasing the
125      * previous handle, re-querying for an ENGINE, and having a
126      * reinitialisation, when it may all be unnecessary.
127      */
128     if (ctx->engine && ctx->digest &&
129         (type == NULL || (type->type == ctx->digest->type)))
130         goto skip_to_init;
131
132     if (type != NULL) {
133         /*
134          * Ensure an ENGINE left lying around from last time is cleared (the
135          * previous check attempted to avoid this if the same ENGINE and
136          * EVP_MD could be used).
137          */
138         ENGINE_finish(ctx->engine);
139         ctx->engine = NULL;
140     }
141
142     if (type != NULL && impl == NULL)
143         tmpimpl = ENGINE_get_digest_engine(type->type);
144 #endif
145
146     /*
147      * If there are engines involved or if we're being used as part of
148      * EVP_DigestSignInit then we should use legacy handling for now.
149      */
150     if (ctx->engine != NULL
151             || impl != NULL
152 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
153             || tmpimpl != NULL
154 #endif
155             || ctx->pctx != NULL
156             || (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0) {
157         if (ctx->digest == ctx->fetched_digest)
158             ctx->digest = NULL;
159         EVP_MD_free(ctx->fetched_digest);
160         ctx->fetched_digest = NULL;
161         goto legacy;
162     }
163
164     if (ctx->digest != NULL && ctx->digest->ctx_size > 0) {
165         OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
166         ctx->md_data = NULL;
167     }
168
169     /* TODO(3.0): Start of non-legacy code below */
170
171     if (type->prov == NULL) {
172 #ifdef FIPS_MODE
173         /* We only do explict fetches inside the FIPS module */
174         EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
175         return 0;
176 #else
177         EVP_MD *provmd = EVP_MD_fetch(NULL, OBJ_nid2sn(type->type), "");
178
179         if (provmd == NULL) {
180             EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
181             return 0;
182         }
183         type = provmd;
184         EVP_MD_free(ctx->fetched_digest);
185         ctx->fetched_digest = provmd;
186 #endif
187     }
188
189     if (ctx->provctx != NULL && ctx->digest != NULL && ctx->digest != type) {
190         if (ctx->digest->freectx != NULL)
191             ctx->digest->freectx(ctx->provctx);
192         ctx->provctx = NULL;
193     }
194     ctx->digest = type;
195     if (ctx->provctx == NULL) {
196         ctx->provctx = ctx->digest->newctx(ossl_provider_ctx(type->prov));
197         if (ctx->provctx == NULL) {
198             EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
199             return 0;
200         }
201     }
202
203     if (ctx->digest->dinit == NULL) {
204         EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
205         return 0;
206     }
207
208     return ctx->digest->dinit(ctx->provctx);
209
210     /* TODO(3.0): Remove legacy code below */
211  legacy:
212
213 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
214     if (type) {
215         if (impl != NULL) {
216             if (!ENGINE_init(impl)) {
217                 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
218                 return 0;
219             }
220         } else {
221             /* Ask if an ENGINE is reserved for this job */
222             impl = tmpimpl;
223         }
224         if (impl != NULL) {
225             /* There's an ENGINE for this job ... (apparently) */
226             const EVP_MD *d = ENGINE_get_digest(impl, type->type);
227
228             if (d == NULL) {
229                 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
230                 ENGINE_finish(impl);
231                 return 0;
232             }
233             /* We'll use the ENGINE's private digest definition */
234             type = d;
235             /*
236              * Store the ENGINE functional reference so we know 'type' came
237              * from an ENGINE and we need to release it when done.
238              */
239             ctx->engine = impl;
240         } else
241             ctx->engine = NULL;
242     } else {
243         if (!ctx->digest) {
244             EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_NO_DIGEST_SET);
245             return 0;
246         }
247         type = ctx->digest;
248     }
249 #endif
250     if (ctx->digest != type) {
251         if (ctx->digest && ctx->digest->ctx_size) {
252             OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
253             ctx->md_data = NULL;
254         }
255         ctx->digest = type;
256         if (!(ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) && type->ctx_size) {
257             ctx->update = type->update;
258             ctx->md_data = OPENSSL_zalloc(type->ctx_size);
259             if (ctx->md_data == NULL) {
260                 EVPerr(EVP_F_EVP_DIGESTINIT_EX, ERR_R_MALLOC_FAILURE);
261                 return 0;
262             }
263         }
264     }
265 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
266  skip_to_init:
267 #endif
268 #ifndef FIPS_MODE
269     /*
270      * TODO(3.0): Temporarily no support for EVP_DigestSign* inside FIPS module
271      * or when using providers.
272      */
273     if (ctx->pctx != NULL && ctx->pctx->signature == NULL) {
274         int r;
275         r = EVP_PKEY_CTX_ctrl(ctx->pctx, -1, EVP_PKEY_OP_TYPE_SIG,
276                               EVP_PKEY_CTRL_DIGESTINIT, 0, ctx);
277         if (r <= 0 && (r != -2))
278             return 0;
279     }
280 #endif
281     if (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT)
282         return 1;
283     return ctx->digest->init(ctx);
284 }
285
286 int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t count)
287 {
288     if (count == 0)
289         return 1;
290
291     if (ctx->digest == NULL || ctx->digest->prov == NULL)
292         goto legacy;
293
294     if (ctx->digest->dupdate == NULL) {
295         EVPerr(EVP_F_EVP_DIGESTUPDATE, EVP_R_UPDATE_ERROR);
296         return 0;
297     }
298     return ctx->digest->dupdate(ctx->provctx, data, count);
299
300     /* TODO(3.0): Remove legacy code below */
301  legacy:
302     return ctx->update(ctx, data, count);
303 }
304
305 /* The caller can assume that this removes any secret data from the context */
306 int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size)
307 {
308     int ret;
309     ret = EVP_DigestFinal_ex(ctx, md, size);
310     EVP_MD_CTX_reset(ctx);
311     return ret;
312 }
313
314 /* The caller can assume that this removes any secret data from the context */
315 int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *isize)
316 {
317     int ret;
318     size_t size = 0;
319     size_t mdsize = EVP_MD_size(ctx->digest);
320
321     if (ctx->digest == NULL || ctx->digest->prov == NULL)
322         goto legacy;
323
324     if (ctx->digest->dfinal == NULL) {
325         EVPerr(EVP_F_EVP_DIGESTFINAL_EX, EVP_R_FINAL_ERROR);
326         return 0;
327     }
328
329     ret = ctx->digest->dfinal(ctx->provctx, md, &size, mdsize);
330
331     if (isize != NULL) {
332         if (size <= UINT_MAX) {
333             *isize = (int)size;
334         } else {
335             EVPerr(EVP_F_EVP_DIGESTFINAL_EX, EVP_R_FINAL_ERROR);
336             ret = 0;
337         }
338     }
339
340     EVP_MD_CTX_reset(ctx);
341     return ret;
342
343     /* TODO(3.0): Remove legacy code below */
344  legacy:
345     OPENSSL_assert(mdsize <= EVP_MAX_MD_SIZE);
346     ret = ctx->digest->final(ctx, md);
347     if (isize != NULL)
348         *isize = mdsize;
349     if (ctx->digest->cleanup) {
350         ctx->digest->cleanup(ctx);
351         EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
352     }
353     OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
354     return ret;
355 }
356
357 int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t size)
358 {
359     int ret = 0;
360     OSSL_PARAM params[2];
361     size_t i = 0;
362
363     if (ctx->digest == NULL || ctx->digest->prov == NULL)
364         goto legacy;
365
366     if (ctx->digest->dfinal == NULL) {
367         EVPerr(EVP_F_EVP_DIGESTFINALXOF, EVP_R_FINAL_ERROR);
368         return 0;
369     }
370
371     params[i++] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN, &size);
372     params[i++] = OSSL_PARAM_construct_end();
373
374     if (EVP_MD_CTX_set_params(ctx, params) > 0)
375         ret = ctx->digest->dfinal(ctx->provctx, md, &size, size);
376     EVP_MD_CTX_reset(ctx);
377     return ret;
378
379 legacy:
380     if (ctx->digest->flags & EVP_MD_FLAG_XOF
381         && size <= INT_MAX
382         && ctx->digest->md_ctrl(ctx, EVP_MD_CTRL_XOF_LEN, (int)size, NULL)) {
383         ret = ctx->digest->final(ctx, md);
384         if (ctx->digest->cleanup != NULL) {
385             ctx->digest->cleanup(ctx);
386             EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
387         }
388         OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
389     } else {
390         EVPerr(EVP_F_EVP_DIGESTFINALXOF, EVP_R_NOT_XOF_OR_INVALID_LENGTH);
391     }
392
393     return ret;
394 }
395
396 int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in)
397 {
398     EVP_MD_CTX_reset(out);
399     return EVP_MD_CTX_copy_ex(out, in);
400 }
401
402 int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in)
403 {
404     unsigned char *tmp_buf;
405
406     if (in == NULL || in->digest == NULL) {
407         EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_INPUT_NOT_INITIALIZED);
408         return 0;
409     }
410
411     if (in->digest->prov == NULL)
412         goto legacy;
413
414     if (in->digest->dupctx == NULL) {
415         EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_NOT_ABLE_TO_COPY_CTX);
416         return 0;
417     }
418
419     EVP_MD_CTX_reset(out);
420     if (out->fetched_digest != NULL)
421         EVP_MD_free(out->fetched_digest);
422     *out = *in;
423     /* NULL out pointers in case of error */
424     out->pctx = NULL;
425     out->provctx = NULL;
426
427     if (in->fetched_digest != NULL)
428         EVP_MD_up_ref(in->fetched_digest);
429
430     out->provctx = in->digest->dupctx(in->provctx);
431     if (out->provctx == NULL) {
432         EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_NOT_ABLE_TO_COPY_CTX);
433         return 0;
434     }
435
436     /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
437     EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
438 #ifndef FIPS_MODE
439     /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
440     if (in->pctx != NULL) {
441         out->pctx = EVP_PKEY_CTX_dup(in->pctx);
442         if (out->pctx == NULL) {
443             EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_NOT_ABLE_TO_COPY_CTX);
444             EVP_MD_CTX_reset(out);
445             return 0;
446         }
447     }
448 #endif
449
450     return 1;
451
452     /* TODO(3.0): Remove legacy code below */
453  legacy:
454 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
455     /* Make sure it's safe to copy a digest context using an ENGINE */
456     if (in->engine && !ENGINE_init(in->engine)) {
457         EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_ENGINE_LIB);
458         return 0;
459     }
460 #endif
461
462     if (out->digest == in->digest) {
463         tmp_buf = out->md_data;
464         EVP_MD_CTX_set_flags(out, EVP_MD_CTX_FLAG_REUSE);
465     } else
466         tmp_buf = NULL;
467     EVP_MD_CTX_reset(out);
468     memcpy(out, in, sizeof(*out));
469
470     /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
471     EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
472
473     /* Null these variables, since they are getting fixed up
474      * properly below.  Anything else may cause a memleak and/or
475      * double free if any of the memory allocations below fail
476      */
477     out->md_data = NULL;
478     out->pctx = NULL;
479
480     if (in->md_data && out->digest->ctx_size) {
481         if (tmp_buf)
482             out->md_data = tmp_buf;
483         else {
484             out->md_data = OPENSSL_malloc(out->digest->ctx_size);
485             if (out->md_data == NULL) {
486                 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_MALLOC_FAILURE);
487                 return 0;
488             }
489         }
490         memcpy(out->md_data, in->md_data, out->digest->ctx_size);
491     }
492
493     out->update = in->update;
494
495 #ifndef FIPS_MODE
496     /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
497     if (in->pctx) {
498         out->pctx = EVP_PKEY_CTX_dup(in->pctx);
499         if (!out->pctx) {
500             EVP_MD_CTX_reset(out);
501             return 0;
502         }
503     }
504 #endif
505
506     if (out->digest->copy)
507         return out->digest->copy(out, in);
508
509     return 1;
510 }
511
512 int EVP_Digest(const void *data, size_t count,
513                unsigned char *md, unsigned int *size, const EVP_MD *type,
514                ENGINE *impl)
515 {
516     EVP_MD_CTX *ctx = EVP_MD_CTX_new();
517     int ret;
518
519     if (ctx == NULL)
520         return 0;
521     EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT);
522     ret = EVP_DigestInit_ex(ctx, type, impl)
523         && EVP_DigestUpdate(ctx, data, count)
524         && EVP_DigestFinal_ex(ctx, md, size);
525     EVP_MD_CTX_free(ctx);
526
527     return ret;
528 }
529
530 int EVP_MD_get_params(const EVP_MD *digest, OSSL_PARAM params[])
531 {
532     if (digest != NULL && digest->get_params != NULL)
533         return digest->get_params(params);
534     return 0;
535 }
536
537 const OSSL_PARAM *EVP_MD_gettable_params(const EVP_MD *digest)
538 {
539     if (digest != NULL && digest->gettable_params != NULL)
540         return digest->gettable_params();
541     return NULL;
542 }
543
544 int EVP_MD_CTX_set_params(EVP_MD_CTX *ctx, const OSSL_PARAM params[])
545 {
546     if (ctx->digest != NULL && ctx->digest->set_ctx_params != NULL)
547         return ctx->digest->set_ctx_params(ctx->provctx, params);
548     return 0;
549 }
550
551 const OSSL_PARAM *EVP_MD_CTX_settable_params(const EVP_MD *digest)
552 {
553     if (digest != NULL && digest->settable_ctx_params != NULL)
554         return digest->settable_ctx_params();
555     return NULL;
556 }
557
558 int EVP_MD_CTX_get_params(EVP_MD_CTX *ctx, OSSL_PARAM params[])
559 {
560     if (ctx->digest != NULL && ctx->digest->get_params != NULL)
561         return ctx->digest->get_ctx_params(ctx->provctx, params);
562     return 0;
563 }
564
565 const OSSL_PARAM *EVP_MD_CTX_gettable_params(const EVP_MD *digest)
566 {
567     if (digest != NULL && digest->gettable_ctx_params != NULL)
568         return digest->gettable_ctx_params();
569     return NULL;
570 }
571
572 /* TODO(3.0): Remove legacy code below - only used by engines & DigestSign */
573 int EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void *p2)
574 {
575     int ret = EVP_CTRL_RET_UNSUPPORTED;
576     int set_params = 1;
577     size_t sz;
578     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
579
580     if (ctx == NULL || ctx->digest == NULL) {
581         ERR_raise(ERR_LIB_EVP, EVP_R_MESSAGE_DIGEST_IS_NULL);
582         return 0;
583     }
584
585     if (ctx->digest->prov == NULL)
586         goto legacy;
587
588     switch (cmd) {
589     case EVP_MD_CTRL_XOF_LEN:
590         sz = (size_t)p1;
591         params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN, &sz);
592         break;
593     case EVP_MD_CTRL_MICALG:
594         set_params = 0;
595         params[0] = OSSL_PARAM_construct_utf8_string(OSSL_DIGEST_PARAM_MICALG,
596                                                      p2, p1 ? p1 : 9999);
597         break;
598     default:
599         return EVP_CTRL_RET_UNSUPPORTED;
600     }
601
602     if (set_params)
603         ret = evp_do_md_ctx_setparams(ctx->digest, ctx->provctx, params);
604     else
605         ret = evp_do_md_ctx_getparams(ctx->digest, ctx->provctx, params);
606     return ret;
607
608
609 /* TODO(3.0): Remove legacy code below */
610  legacy:
611     if (ctx->digest->md_ctrl == NULL) {
612         ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_NOT_IMPLEMENTED);
613         return 0;
614     }
615
616     ret = ctx->digest->md_ctrl(ctx, cmd, p1, p2);
617     if (ret <= 0)
618         return 0;
619     return ret;
620 }
621
622 EVP_MD *evp_md_new(void)
623 {
624     EVP_MD *md = OPENSSL_zalloc(sizeof(*md));
625
626     if (md != NULL) {
627         md->lock = CRYPTO_THREAD_lock_new();
628         if (md->lock == NULL) {
629             OPENSSL_free(md);
630             return NULL;
631         }
632         md->refcnt = 1;
633     }
634     return md;
635 }
636
637 static void *evp_md_from_dispatch(const char *name, const OSSL_DISPATCH *fns,
638                                   OSSL_PROVIDER *prov, void *unused)
639 {
640     EVP_MD *md = NULL;
641     int fncnt = 0;
642
643     /* EVP_MD_fetch() will set the legacy NID if available */
644     if ((md = evp_md_new()) == NULL
645         || (md->name = OPENSSL_strdup(name)) == NULL) {
646         EVP_MD_free(md);
647         EVPerr(0, ERR_R_MALLOC_FAILURE);
648         return NULL;
649     }
650
651 #ifndef FIPS_MODE
652     /*
653      * FIPS module note: since internal fetches will be entirely
654      * provider based, we know that none of its code depends on legacy
655      * NIDs or any functionality that use them.
656      *
657      * TODO(3.x) get rid of the need for legacy NIDs
658      */
659     md->type = OBJ_sn2nid(name);
660 #endif
661
662     for (; fns->function_id != 0; fns++) {
663         switch (fns->function_id) {
664         case OSSL_FUNC_DIGEST_NEWCTX:
665             if (md->newctx == NULL) {
666                 md->newctx = OSSL_get_OP_digest_newctx(fns);
667                 fncnt++;
668             }
669             break;
670         case OSSL_FUNC_DIGEST_INIT:
671             if (md->dinit == NULL) {
672                 md->dinit = OSSL_get_OP_digest_init(fns);
673                 fncnt++;
674             }
675             break;
676         case OSSL_FUNC_DIGEST_UPDATE:
677             if (md->dupdate == NULL) {
678                 md->dupdate = OSSL_get_OP_digest_update(fns);
679                 fncnt++;
680             }
681             break;
682         case OSSL_FUNC_DIGEST_FINAL:
683             if (md->dfinal == NULL) {
684                 md->dfinal = OSSL_get_OP_digest_final(fns);
685                 fncnt++;
686             }
687             break;
688         case OSSL_FUNC_DIGEST_DIGEST:
689             if (md->digest == NULL)
690                 md->digest = OSSL_get_OP_digest_digest(fns);
691             /* We don't increment fnct for this as it is stand alone */
692             break;
693         case OSSL_FUNC_DIGEST_FREECTX:
694             if (md->freectx == NULL) {
695                 md->freectx = OSSL_get_OP_digest_freectx(fns);
696                 fncnt++;
697             }
698             break;
699         case OSSL_FUNC_DIGEST_DUPCTX:
700             if (md->dupctx == NULL)
701                 md->dupctx = OSSL_get_OP_digest_dupctx(fns);
702             break;
703         case OSSL_FUNC_DIGEST_GET_PARAMS:
704             if (md->get_params == NULL)
705                 md->get_params = OSSL_get_OP_digest_get_params(fns);
706             break;
707         case OSSL_FUNC_DIGEST_SET_CTX_PARAMS:
708             if (md->set_ctx_params == NULL)
709                 md->set_ctx_params = OSSL_get_OP_digest_set_ctx_params(fns);
710             break;
711         case OSSL_FUNC_DIGEST_GET_CTX_PARAMS:
712             if (md->get_ctx_params == NULL)
713                 md->get_ctx_params = OSSL_get_OP_digest_get_ctx_params(fns);
714             break;
715         case OSSL_FUNC_DIGEST_GETTABLE_PARAMS:
716             if (md->gettable_params == NULL)
717                 md->gettable_params = OSSL_get_OP_digest_gettable_params(fns);
718             break;
719         case OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS:
720             if (md->settable_ctx_params == NULL)
721                 md->settable_ctx_params =
722                     OSSL_get_OP_digest_settable_ctx_params(fns);
723             break;
724         case OSSL_FUNC_DIGEST_GETTABLE_CTX_PARAMS:
725             if (md->gettable_ctx_params == NULL)
726                 md->gettable_ctx_params =
727                     OSSL_get_OP_digest_gettable_ctx_params(fns);
728             break;
729         }
730     }
731     if ((fncnt != 0 && fncnt != 5)
732         || (fncnt == 0 && md->digest == NULL)) {
733         /*
734          * In order to be a consistent set of functions we either need the
735          * whole set of init/update/final etc functions or none of them.
736          * The "digest" function can standalone. We at least need one way to
737          * generate digests.
738          */
739         EVP_MD_free(md);
740         ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
741         return NULL;
742     }
743     md->prov = prov;
744     if (prov != NULL)
745         ossl_provider_up_ref(prov);
746
747     return md;
748 }
749
750 static int evp_md_up_ref(void *md)
751 {
752     return EVP_MD_up_ref(md);
753 }
754
755 static void evp_md_free(void *md)
756 {
757     EVP_MD_free(md);
758 }
759
760 EVP_MD *EVP_MD_fetch(OPENSSL_CTX *ctx, const char *algorithm,
761                      const char *properties)
762 {
763     EVP_MD *md =
764         evp_generic_fetch(ctx, OSSL_OP_DIGEST, algorithm, properties,
765                           evp_md_from_dispatch, NULL, evp_md_up_ref,
766                           evp_md_free);
767
768     return md;
769 }
770
771 int EVP_MD_up_ref(EVP_MD *md)
772 {
773     int ref = 0;
774
775     CRYPTO_UP_REF(&md->refcnt, &ref, md->lock);
776     return 1;
777 }
778
779 void EVP_MD_free(EVP_MD *md)
780 {
781     int i;
782
783     if (md == NULL)
784         return;
785
786     CRYPTO_DOWN_REF(&md->refcnt, &i, md->lock);
787     if (i > 0)
788         return;
789     ossl_provider_free(md->prov);
790     OPENSSL_free(md->name);
791     CRYPTO_THREAD_lock_free(md->lock);
792     OPENSSL_free(md);
793 }
794
795 void EVP_MD_do_all_ex(OPENSSL_CTX *libctx,
796                           void (*fn)(EVP_MD *mac, void *arg),
797                           void *arg)
798 {
799     evp_generic_do_all(libctx, OSSL_OP_DIGEST,
800                        (void (*)(void *, void *))fn, arg,
801                        evp_md_from_dispatch, NULL, evp_md_free);
802 }