Move digests to providers
[oweals/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_meth_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 && impl == NULL)
133         tmpimpl = ENGINE_get_digest_engine(type->type);
134 #endif
135
136     /*
137      * If there are engines involved or if we're being used as part of
138      * EVP_DigestSignInit then we should use legacy handling for now.
139      */
140     if (ctx->engine != NULL
141             || impl != NULL
142 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
143             || tmpimpl != NULL
144 #endif
145             || ctx->pctx != NULL
146             || (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0) {
147         if (ctx->digest == ctx->fetched_digest)
148             ctx->digest = NULL;
149         EVP_MD_meth_free(ctx->fetched_digest);
150         ctx->fetched_digest = NULL;
151         goto legacy;
152     }
153
154     if (ctx->digest != NULL && ctx->digest->ctx_size > 0) {
155         OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
156         ctx->md_data = NULL;
157     }
158
159     /* TODO(3.0): Start of non-legacy code below */
160
161     if (type->prov == NULL) {
162 #ifdef FIPS_MODE
163         /* We only do explict fetches inside the FIPS module */
164         EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
165         return 0;
166 #else
167         EVP_MD *provmd = EVP_MD_fetch(NULL, OBJ_nid2sn(type->type), "");
168
169         if (provmd == NULL) {
170             EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
171             return 0;
172         }
173         type = provmd;
174         EVP_MD_meth_free(ctx->fetched_digest);
175         ctx->fetched_digest = provmd;
176 #endif
177     }
178
179     if (ctx->provctx != NULL && ctx->digest != NULL && ctx->digest != type) {
180         if (ctx->digest->freectx != NULL)
181             ctx->digest->freectx(ctx->provctx);
182         ctx->provctx = NULL;
183     }
184     ctx->digest = type;
185     if (ctx->provctx == NULL) {
186         ctx->provctx = ctx->digest->newctx(ossl_provider_ctx(type->prov));
187         if (ctx->provctx == NULL) {
188             EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
189             return 0;
190         }
191     }
192
193     if (ctx->digest->dinit == NULL) {
194         EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
195         return 0;
196     }
197
198     return ctx->digest->dinit(ctx->provctx);
199
200     /* TODO(3.0): Remove legacy code below */
201  legacy:
202
203 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
204     if (type) {
205         /*
206          * Ensure an ENGINE left lying around from last time is cleared (the
207          * previous check attempted to avoid this if the same ENGINE and
208          * EVP_MD could be used).
209          */
210         ENGINE_finish(ctx->engine);
211         if (impl != NULL) {
212             if (!ENGINE_init(impl)) {
213                 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
214                 return 0;
215             }
216         } else {
217             /* Ask if an ENGINE is reserved for this job */
218             impl = tmpimpl;
219         }
220         if (impl != NULL) {
221             /* There's an ENGINE for this job ... (apparently) */
222             const EVP_MD *d = ENGINE_get_digest(impl, type->type);
223
224             if (d == NULL) {
225                 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
226                 ENGINE_finish(impl);
227                 return 0;
228             }
229             /* We'll use the ENGINE's private digest definition */
230             type = d;
231             /*
232              * Store the ENGINE functional reference so we know 'type' came
233              * from an ENGINE and we need to release it when done.
234              */
235             ctx->engine = impl;
236         } else
237             ctx->engine = NULL;
238     } else {
239         if (!ctx->digest) {
240             EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_NO_DIGEST_SET);
241             return 0;
242         }
243         type = ctx->digest;
244     }
245 #endif
246     if (ctx->digest != type) {
247         if (ctx->digest && ctx->digest->ctx_size) {
248             OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
249             ctx->md_data = NULL;
250         }
251         ctx->digest = type;
252         if (!(ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) && type->ctx_size) {
253             ctx->update = type->update;
254             ctx->md_data = OPENSSL_zalloc(type->ctx_size);
255             if (ctx->md_data == NULL) {
256                 EVPerr(EVP_F_EVP_DIGESTINIT_EX, ERR_R_MALLOC_FAILURE);
257                 return 0;
258             }
259         }
260     }
261 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
262  skip_to_init:
263 #endif
264 #ifndef FIPS_MODE
265     /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
266     if (ctx->pctx != NULL) {
267         int r;
268         r = EVP_PKEY_CTX_ctrl(ctx->pctx, -1, EVP_PKEY_OP_TYPE_SIG,
269                               EVP_PKEY_CTRL_DIGESTINIT, 0, ctx);
270         if (r <= 0 && (r != -2))
271             return 0;
272     }
273 #endif
274     if (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT)
275         return 1;
276     return ctx->digest->init(ctx);
277 }
278
279 int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t count)
280 {
281     if (count == 0)
282         return 1;
283
284     if (ctx->digest == NULL || ctx->digest->prov == NULL)
285         goto legacy;
286
287     if (ctx->digest->dupdate == NULL) {
288         EVPerr(EVP_F_EVP_DIGESTUPDATE, EVP_R_UPDATE_ERROR);
289         return 0;
290     }
291     return ctx->digest->dupdate(ctx->provctx, data, count);
292
293     /* TODO(3.0): Remove legacy code below */
294  legacy:
295     return ctx->update(ctx, data, count);
296 }
297
298 /* The caller can assume that this removes any secret data from the context */
299 int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size)
300 {
301     int ret;
302     ret = EVP_DigestFinal_ex(ctx, md, size);
303     EVP_MD_CTX_reset(ctx);
304     return ret;
305 }
306
307 /* The caller can assume that this removes any secret data from the context */
308 int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *isize)
309 {
310     int ret;
311     size_t size = 0;
312     size_t mdsize = EVP_MD_size(ctx->digest);
313
314     if (ctx->digest == NULL || ctx->digest->prov == NULL)
315         goto legacy;
316
317     if (ctx->digest->dfinal == NULL) {
318         EVPerr(EVP_F_EVP_DIGESTFINAL_EX, EVP_R_FINAL_ERROR);
319         return 0;
320     }
321
322     ret = ctx->digest->dfinal(ctx->provctx, md, &size, mdsize);
323
324     if (isize != NULL) {
325         if (size <= UINT_MAX) {
326             *isize = (int)size;
327         } else {
328             EVPerr(EVP_F_EVP_DIGESTFINAL_EX, EVP_R_FINAL_ERROR);
329             ret = 0;
330         }
331     }
332
333     EVP_MD_CTX_reset(ctx);
334     return ret;
335
336     /* TODO(3.0): Remove legacy code below */
337  legacy:
338     OPENSSL_assert(mdsize <= EVP_MAX_MD_SIZE);
339     ret = ctx->digest->final(ctx, md);
340     if (isize != NULL)
341         *isize = mdsize;
342     if (ctx->digest->cleanup) {
343         ctx->digest->cleanup(ctx);
344         EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
345     }
346     OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
347     return ret;
348 }
349
350 int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t size)
351 {
352     int ret = 0;
353     OSSL_PARAM params[2];
354     size_t i = 0;
355
356     if (ctx->digest == NULL || ctx->digest->prov == NULL)
357         goto legacy;
358
359     if (ctx->digest->dfinal == NULL) {
360         EVPerr(EVP_F_EVP_DIGESTFINALXOF, EVP_R_FINAL_ERROR);
361         return 0;
362     }
363
364     params[i++] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN,
365                                               &size, NULL);
366     params[i++] = OSSL_PARAM_construct_end();
367
368     if (EVP_MD_CTX_set_params(ctx, params) > 0)
369         ret = ctx->digest->dfinal(ctx->provctx, md, &size, size);
370     EVP_MD_CTX_reset(ctx);
371     return ret;
372
373 legacy:
374     if (ctx->digest->flags & EVP_MD_FLAG_XOF
375         && size <= INT_MAX
376         && ctx->digest->md_ctrl(ctx, EVP_MD_CTRL_XOF_LEN, (int)size, NULL)) {
377         ret = ctx->digest->final(ctx, md);
378         if (ctx->digest->cleanup != NULL) {
379             ctx->digest->cleanup(ctx);
380             EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
381         }
382         OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
383     } else {
384         EVPerr(EVP_F_EVP_DIGESTFINALXOF, EVP_R_NOT_XOF_OR_INVALID_LENGTH);
385     }
386
387     return ret;
388 }
389
390 int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in)
391 {
392     EVP_MD_CTX_reset(out);
393     return EVP_MD_CTX_copy_ex(out, in);
394 }
395
396 int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in)
397 {
398     unsigned char *tmp_buf;
399
400     if (in == NULL || in->digest == NULL) {
401         EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_INPUT_NOT_INITIALIZED);
402         return 0;
403     }
404
405     if (in->digest->prov == NULL)
406         goto legacy;
407
408     if (in->digest->dupctx == NULL) {
409         EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_NOT_ABLE_TO_COPY_CTX);
410         return 0;
411     }
412
413     EVP_MD_CTX_reset(out);
414     if (out->fetched_digest != NULL)
415         EVP_MD_meth_free(out->fetched_digest);
416     *out = *in;
417     /* NULL out pointers in case of error */
418     out->pctx = NULL;
419     out->provctx = NULL;
420
421     if (in->fetched_digest != NULL)
422         EVP_MD_upref(in->fetched_digest);
423
424     out->provctx = in->digest->dupctx(in->provctx);
425     if (out->provctx == NULL) {
426         EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_NOT_ABLE_TO_COPY_CTX);
427         return 0;
428     }
429
430     /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
431     EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
432 #ifndef FIPS_MODE
433     /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
434     if (in->pctx != NULL) {
435         out->pctx = EVP_PKEY_CTX_dup(in->pctx);
436         if (out->pctx == NULL) {
437             EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_NOT_ABLE_TO_COPY_CTX);
438             EVP_MD_CTX_reset(out);
439             return 0;
440         }
441     }
442 #endif
443
444     return 1;
445
446     /* TODO(3.0): Remove legacy code below */
447  legacy:
448 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
449     /* Make sure it's safe to copy a digest context using an ENGINE */
450     if (in->engine && !ENGINE_init(in->engine)) {
451         EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_ENGINE_LIB);
452         return 0;
453     }
454 #endif
455
456     if (out->digest == in->digest) {
457         tmp_buf = out->md_data;
458         EVP_MD_CTX_set_flags(out, EVP_MD_CTX_FLAG_REUSE);
459     } else
460         tmp_buf = NULL;
461     EVP_MD_CTX_reset(out);
462     memcpy(out, in, sizeof(*out));
463
464     /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
465     EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
466
467     /* Null these variables, since they are getting fixed up
468      * properly below.  Anything else may cause a memleak and/or
469      * double free if any of the memory allocations below fail
470      */
471     out->md_data = NULL;
472     out->pctx = NULL;
473
474     if (in->md_data && out->digest->ctx_size) {
475         if (tmp_buf)
476             out->md_data = tmp_buf;
477         else {
478             out->md_data = OPENSSL_malloc(out->digest->ctx_size);
479             if (out->md_data == NULL) {
480                 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_MALLOC_FAILURE);
481                 return 0;
482             }
483         }
484         memcpy(out->md_data, in->md_data, out->digest->ctx_size);
485     }
486
487     out->update = in->update;
488
489 #ifndef FIPS_MODE
490     /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
491     if (in->pctx) {
492         out->pctx = EVP_PKEY_CTX_dup(in->pctx);
493         if (!out->pctx) {
494             EVP_MD_CTX_reset(out);
495             return 0;
496         }
497     }
498 #endif
499
500     if (out->digest->copy)
501         return out->digest->copy(out, in);
502
503     return 1;
504 }
505
506 int EVP_Digest(const void *data, size_t count,
507                unsigned char *md, unsigned int *size, const EVP_MD *type,
508                ENGINE *impl)
509 {
510     EVP_MD_CTX *ctx = EVP_MD_CTX_new();
511     int ret;
512
513     if (ctx == NULL)
514         return 0;
515     EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT);
516     ret = EVP_DigestInit_ex(ctx, type, impl)
517         && EVP_DigestUpdate(ctx, data, count)
518         && EVP_DigestFinal_ex(ctx, md, size);
519     EVP_MD_CTX_free(ctx);
520
521     return ret;
522 }
523
524 int EVP_MD_CTX_set_params(EVP_MD_CTX *ctx, const OSSL_PARAM params[])
525 {
526     if (ctx->digest != NULL && ctx->digest->set_params != NULL)
527         return ctx->digest->set_params(ctx->provctx, params);
528     return 0;
529 }
530
531 int EVP_MD_CTX_get_params(EVP_MD_CTX *ctx, const OSSL_PARAM params[])
532 {
533     if (ctx->digest != NULL && ctx->digest->get_params != NULL)
534         return ctx->digest->get_params(ctx->provctx, params);
535     return 0;
536 }
537
538 #if !OPENSSL_API_3
539 int EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void *p2)
540 {
541     if (ctx->digest != NULL) {
542         OSSL_PARAM params[2];
543         size_t i, sz, n = 0;
544
545         switch (cmd) {
546         case EVP_MD_CTRL_XOF_LEN:
547             if (ctx->digest->set_params == NULL)
548                 break;
549             i = (size_t)p1;
550             params[n++] = OSSL_PARAM_construct_size_t(
551                               OSSL_DIGEST_PARAM_XOFLEN, &i, &sz);
552             params[n++] = OSSL_PARAM_construct_end();
553             return ctx->digest->set_params(ctx->provctx, params) > 0;
554         case EVP_MD_CTRL_MICALG:
555             if (ctx->digest->get_params == NULL)
556                 break;
557             params[n++] = OSSL_PARAM_construct_utf8_string(
558                               OSSL_DIGEST_PARAM_MICALG, p2, p1 ? p1 : 9999,
559                               &sz);
560             params[n++] = OSSL_PARAM_construct_end();
561             return ctx->digest->get_params(ctx->provctx, params);
562         }
563         /* legacy code */
564         if (ctx->digest->md_ctrl != NULL) {
565             int ret = ctx->digest->md_ctrl(ctx, cmd, p1, p2);
566             if (ret <= 0)
567                 return 0;
568             return 1;
569         }
570     }
571     return 0;
572 }
573 #endif
574
575 static void *evp_md_from_dispatch(const OSSL_DISPATCH *fns,
576                                   OSSL_PROVIDER *prov)
577 {
578     EVP_MD *md = NULL;
579     int fncnt = 0;
580
581     /* EVP_MD_fetch() will set the legacy NID if available */
582     if ((md = EVP_MD_meth_new(NID_undef, NID_undef)) == NULL)
583         return NULL;
584
585     for (; fns->function_id != 0; fns++) {
586         switch (fns->function_id) {
587         case OSSL_FUNC_DIGEST_NEWCTX:
588             if (md->newctx == NULL) {
589                 md->newctx = OSSL_get_OP_digest_newctx(fns);
590                 fncnt++;
591             }
592             break;
593         case OSSL_FUNC_DIGEST_INIT:
594             if (md->dinit == NULL) {
595                 md->dinit = OSSL_get_OP_digest_init(fns);
596                 fncnt++;
597             }
598             break;
599         case OSSL_FUNC_DIGEST_UPDATE:
600             if (md->dupdate == NULL) {
601                 md->dupdate = OSSL_get_OP_digest_update(fns);
602                 fncnt++;
603             }
604             break;
605         case OSSL_FUNC_DIGEST_FINAL:
606             if (md->dfinal == NULL) {
607                 md->dfinal = OSSL_get_OP_digest_final(fns);
608                 fncnt++;
609             }
610             break;
611         case OSSL_FUNC_DIGEST_DIGEST:
612             if (md->digest == NULL)
613                 md->digest = OSSL_get_OP_digest_digest(fns);
614             /* We don't increment fnct for this as it is stand alone */
615             break;
616         case OSSL_FUNC_DIGEST_FREECTX:
617             if (md->freectx == NULL) {
618                 md->freectx = OSSL_get_OP_digest_freectx(fns);
619                 fncnt++;
620             }
621             break;
622         case OSSL_FUNC_DIGEST_DUPCTX:
623             if (md->dupctx == NULL)
624                 md->dupctx = OSSL_get_OP_digest_dupctx(fns);
625             break;
626         case OSSL_FUNC_DIGEST_SIZE:
627             if (md->size == NULL)
628                 md->size = OSSL_get_OP_digest_size(fns);
629             break;
630         case OSSL_FUNC_DIGEST_BLOCK_SIZE:
631             if (md->dblock_size == NULL)
632                 md->dblock_size = OSSL_get_OP_digest_block_size(fns);
633             break;
634         case OSSL_FUNC_DIGEST_SET_PARAMS:
635             if (md->set_params == NULL)
636                 md->set_params = OSSL_get_OP_digest_set_params(fns);
637             break;
638         case OSSL_FUNC_DIGEST_GET_PARAMS:
639             if (md->get_params == NULL)
640                 md->get_params = OSSL_get_OP_digest_get_params(fns);
641             break;
642         }
643     }
644     if ((fncnt != 0 && fncnt != 5)
645         || (fncnt == 0 && md->digest == NULL)
646         || md->size == NULL) {
647         /*
648          * In order to be a consistent set of functions we either need the
649          * whole set of init/update/final etc functions or none of them.
650          * The "digest" function can standalone. We at least need one way to
651          * generate digests.
652          */
653         EVP_MD_meth_free(md);
654         return NULL;
655     }
656     md->prov = prov;
657     if (prov != NULL)
658         ossl_provider_upref(prov);
659
660     return md;
661 }
662
663 static int evp_md_upref(void *md)
664 {
665     return EVP_MD_upref(md);
666 }
667
668 static void evp_md_free(void *md)
669 {
670     EVP_MD_meth_free(md);
671 }
672
673 EVP_MD *EVP_MD_fetch(OPENSSL_CTX *ctx, const char *algorithm,
674                      const char *properties)
675 {
676     EVP_MD *md =
677         evp_generic_fetch(ctx, OSSL_OP_DIGEST, algorithm, properties,
678                           evp_md_from_dispatch, evp_md_upref,
679                           evp_md_free);
680
681 #ifndef FIPS_MODE
682     /* TODO(3.x) get rid of the need for legacy NIDs */
683     if (md != NULL) {
684         /*
685          * FIPS module note: since internal fetches will be entirely
686          * provider based, we know that none of its code depends on legacy
687          * NIDs or any functionality that use them.
688          */
689         md->type = OBJ_sn2nid(algorithm);
690     }
691 #endif
692
693     return md;
694 }