EVP_DigestInit_ex(): drop previous context engine earlier
[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) {
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_meth_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_meth_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     /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
270     if (ctx->pctx != NULL) {
271         int r;
272         r = EVP_PKEY_CTX_ctrl(ctx->pctx, -1, EVP_PKEY_OP_TYPE_SIG,
273                               EVP_PKEY_CTRL_DIGESTINIT, 0, ctx);
274         if (r <= 0 && (r != -2))
275             return 0;
276     }
277 #endif
278     if (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT)
279         return 1;
280     return ctx->digest->init(ctx);
281 }
282
283 int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t count)
284 {
285     if (count == 0)
286         return 1;
287
288     if (ctx->digest == NULL || ctx->digest->prov == NULL)
289         goto legacy;
290
291     if (ctx->digest->dupdate == NULL) {
292         EVPerr(EVP_F_EVP_DIGESTUPDATE, EVP_R_UPDATE_ERROR);
293         return 0;
294     }
295     return ctx->digest->dupdate(ctx->provctx, data, count);
296
297     /* TODO(3.0): Remove legacy code below */
298  legacy:
299     return ctx->update(ctx, data, count);
300 }
301
302 /* The caller can assume that this removes any secret data from the context */
303 int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size)
304 {
305     int ret;
306     ret = EVP_DigestFinal_ex(ctx, md, size);
307     EVP_MD_CTX_reset(ctx);
308     return ret;
309 }
310
311 /* The caller can assume that this removes any secret data from the context */
312 int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *isize)
313 {
314     int ret;
315     size_t size = 0;
316     size_t mdsize = EVP_MD_size(ctx->digest);
317
318     if (ctx->digest == NULL || ctx->digest->prov == NULL)
319         goto legacy;
320
321     if (ctx->digest->dfinal == NULL) {
322         EVPerr(EVP_F_EVP_DIGESTFINAL_EX, EVP_R_FINAL_ERROR);
323         return 0;
324     }
325
326     ret = ctx->digest->dfinal(ctx->provctx, md, &size, mdsize);
327
328     if (isize != NULL) {
329         if (size <= UINT_MAX) {
330             *isize = (int)size;
331         } else {
332             EVPerr(EVP_F_EVP_DIGESTFINAL_EX, EVP_R_FINAL_ERROR);
333             ret = 0;
334         }
335     }
336
337     EVP_MD_CTX_reset(ctx);
338     return ret;
339
340     /* TODO(3.0): Remove legacy code below */
341  legacy:
342     OPENSSL_assert(mdsize <= EVP_MAX_MD_SIZE);
343     ret = ctx->digest->final(ctx, md);
344     if (isize != NULL)
345         *isize = mdsize;
346     if (ctx->digest->cleanup) {
347         ctx->digest->cleanup(ctx);
348         EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
349     }
350     OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
351     return ret;
352 }
353
354 int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t size)
355 {
356     int ret = 0;
357     OSSL_PARAM params[2];
358     size_t i = 0;
359
360     if (ctx->digest == NULL || ctx->digest->prov == NULL)
361         goto legacy;
362
363     if (ctx->digest->dfinal == NULL) {
364         EVPerr(EVP_F_EVP_DIGESTFINALXOF, EVP_R_FINAL_ERROR);
365         return 0;
366     }
367
368     params[i++] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN,
369                                               &size, NULL);
370     params[i++] = OSSL_PARAM_construct_end();
371
372     if (EVP_MD_CTX_set_params(ctx, params) > 0)
373         ret = ctx->digest->dfinal(ctx->provctx, md, &size, size);
374     EVP_MD_CTX_reset(ctx);
375     return ret;
376
377 legacy:
378     if (ctx->digest->flags & EVP_MD_FLAG_XOF
379         && size <= INT_MAX
380         && ctx->digest->md_ctrl(ctx, EVP_MD_CTRL_XOF_LEN, (int)size, NULL)) {
381         ret = ctx->digest->final(ctx, md);
382         if (ctx->digest->cleanup != NULL) {
383             ctx->digest->cleanup(ctx);
384             EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
385         }
386         OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
387     } else {
388         EVPerr(EVP_F_EVP_DIGESTFINALXOF, EVP_R_NOT_XOF_OR_INVALID_LENGTH);
389     }
390
391     return ret;
392 }
393
394 int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in)
395 {
396     EVP_MD_CTX_reset(out);
397     return EVP_MD_CTX_copy_ex(out, in);
398 }
399
400 int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in)
401 {
402     unsigned char *tmp_buf;
403
404     if (in == NULL || in->digest == NULL) {
405         EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_INPUT_NOT_INITIALIZED);
406         return 0;
407     }
408
409     if (in->digest->prov == NULL)
410         goto legacy;
411
412     if (in->digest->dupctx == NULL) {
413         EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_NOT_ABLE_TO_COPY_CTX);
414         return 0;
415     }
416
417     EVP_MD_CTX_reset(out);
418     if (out->fetched_digest != NULL)
419         EVP_MD_meth_free(out->fetched_digest);
420     *out = *in;
421     /* NULL out pointers in case of error */
422     out->pctx = NULL;
423     out->provctx = NULL;
424
425     if (in->fetched_digest != NULL)
426         EVP_MD_upref(in->fetched_digest);
427
428     out->provctx = in->digest->dupctx(in->provctx);
429     if (out->provctx == NULL) {
430         EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_NOT_ABLE_TO_COPY_CTX);
431         return 0;
432     }
433
434     /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
435     EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
436 #ifndef FIPS_MODE
437     /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
438     if (in->pctx != NULL) {
439         out->pctx = EVP_PKEY_CTX_dup(in->pctx);
440         if (out->pctx == NULL) {
441             EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_NOT_ABLE_TO_COPY_CTX);
442             EVP_MD_CTX_reset(out);
443             return 0;
444         }
445     }
446 #endif
447
448     return 1;
449
450     /* TODO(3.0): Remove legacy code below */
451  legacy:
452 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
453     /* Make sure it's safe to copy a digest context using an ENGINE */
454     if (in->engine && !ENGINE_init(in->engine)) {
455         EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_ENGINE_LIB);
456         return 0;
457     }
458 #endif
459
460     if (out->digest == in->digest) {
461         tmp_buf = out->md_data;
462         EVP_MD_CTX_set_flags(out, EVP_MD_CTX_FLAG_REUSE);
463     } else
464         tmp_buf = NULL;
465     EVP_MD_CTX_reset(out);
466     memcpy(out, in, sizeof(*out));
467
468     /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
469     EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
470
471     /* Null these variables, since they are getting fixed up
472      * properly below.  Anything else may cause a memleak and/or
473      * double free if any of the memory allocations below fail
474      */
475     out->md_data = NULL;
476     out->pctx = NULL;
477
478     if (in->md_data && out->digest->ctx_size) {
479         if (tmp_buf)
480             out->md_data = tmp_buf;
481         else {
482             out->md_data = OPENSSL_malloc(out->digest->ctx_size);
483             if (out->md_data == NULL) {
484                 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_MALLOC_FAILURE);
485                 return 0;
486             }
487         }
488         memcpy(out->md_data, in->md_data, out->digest->ctx_size);
489     }
490
491     out->update = in->update;
492
493 #ifndef FIPS_MODE
494     /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
495     if (in->pctx) {
496         out->pctx = EVP_PKEY_CTX_dup(in->pctx);
497         if (!out->pctx) {
498             EVP_MD_CTX_reset(out);
499             return 0;
500         }
501     }
502 #endif
503
504     if (out->digest->copy)
505         return out->digest->copy(out, in);
506
507     return 1;
508 }
509
510 int EVP_Digest(const void *data, size_t count,
511                unsigned char *md, unsigned int *size, const EVP_MD *type,
512                ENGINE *impl)
513 {
514     EVP_MD_CTX *ctx = EVP_MD_CTX_new();
515     int ret;
516
517     if (ctx == NULL)
518         return 0;
519     EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT);
520     ret = EVP_DigestInit_ex(ctx, type, impl)
521         && EVP_DigestUpdate(ctx, data, count)
522         && EVP_DigestFinal_ex(ctx, md, size);
523     EVP_MD_CTX_free(ctx);
524
525     return ret;
526 }
527
528 int EVP_MD_CTX_set_params(EVP_MD_CTX *ctx, const OSSL_PARAM params[])
529 {
530     if (ctx->digest != NULL && ctx->digest->set_params != NULL)
531         return ctx->digest->set_params(ctx->provctx, params);
532     return 0;
533 }
534
535 int EVP_MD_CTX_get_params(EVP_MD_CTX *ctx, const OSSL_PARAM params[])
536 {
537     if (ctx->digest != NULL && ctx->digest->get_params != NULL)
538         return ctx->digest->get_params(ctx->provctx, params);
539     return 0;
540 }
541
542 #if !OPENSSL_API_3
543 int EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void *p2)
544 {
545     if (ctx->digest != NULL) {
546         OSSL_PARAM params[2];
547         size_t i, sz, n = 0;
548
549         switch (cmd) {
550         case EVP_MD_CTRL_XOF_LEN:
551             if (ctx->digest->set_params == NULL)
552                 break;
553             i = (size_t)p1;
554             params[n++] = OSSL_PARAM_construct_size_t(
555                               OSSL_DIGEST_PARAM_XOFLEN, &i, &sz);
556             params[n++] = OSSL_PARAM_construct_end();
557             return ctx->digest->set_params(ctx->provctx, params) > 0;
558         case EVP_MD_CTRL_MICALG:
559             if (ctx->digest->get_params == NULL)
560                 break;
561             params[n++] = OSSL_PARAM_construct_utf8_string(
562                               OSSL_DIGEST_PARAM_MICALG, p2, p1 ? p1 : 9999,
563                               &sz);
564             params[n++] = OSSL_PARAM_construct_end();
565             return ctx->digest->get_params(ctx->provctx, params);
566         }
567         /* legacy code */
568         if (ctx->digest->md_ctrl != NULL) {
569             int ret = ctx->digest->md_ctrl(ctx, cmd, p1, p2);
570             if (ret <= 0)
571                 return 0;
572             return 1;
573         }
574     }
575     return 0;
576 }
577 #endif
578
579 static void *evp_md_from_dispatch(const OSSL_DISPATCH *fns,
580                                   OSSL_PROVIDER *prov)
581 {
582     EVP_MD *md = NULL;
583     int fncnt = 0;
584
585     /* EVP_MD_fetch() will set the legacy NID if available */
586     if ((md = EVP_MD_meth_new(NID_undef, NID_undef)) == NULL)
587         return NULL;
588
589     for (; fns->function_id != 0; fns++) {
590         switch (fns->function_id) {
591         case OSSL_FUNC_DIGEST_NEWCTX:
592             if (md->newctx == NULL) {
593                 md->newctx = OSSL_get_OP_digest_newctx(fns);
594                 fncnt++;
595             }
596             break;
597         case OSSL_FUNC_DIGEST_INIT:
598             if (md->dinit == NULL) {
599                 md->dinit = OSSL_get_OP_digest_init(fns);
600                 fncnt++;
601             }
602             break;
603         case OSSL_FUNC_DIGEST_UPDATE:
604             if (md->dupdate == NULL) {
605                 md->dupdate = OSSL_get_OP_digest_update(fns);
606                 fncnt++;
607             }
608             break;
609         case OSSL_FUNC_DIGEST_FINAL:
610             if (md->dfinal == NULL) {
611                 md->dfinal = OSSL_get_OP_digest_final(fns);
612                 fncnt++;
613             }
614             break;
615         case OSSL_FUNC_DIGEST_DIGEST:
616             if (md->digest == NULL)
617                 md->digest = OSSL_get_OP_digest_digest(fns);
618             /* We don't increment fnct for this as it is stand alone */
619             break;
620         case OSSL_FUNC_DIGEST_FREECTX:
621             if (md->freectx == NULL) {
622                 md->freectx = OSSL_get_OP_digest_freectx(fns);
623                 fncnt++;
624             }
625             break;
626         case OSSL_FUNC_DIGEST_DUPCTX:
627             if (md->dupctx == NULL)
628                 md->dupctx = OSSL_get_OP_digest_dupctx(fns);
629             break;
630         case OSSL_FUNC_DIGEST_SIZE:
631             if (md->size == NULL)
632                 md->size = OSSL_get_OP_digest_size(fns);
633             break;
634         case OSSL_FUNC_DIGEST_BLOCK_SIZE:
635             if (md->dblock_size == NULL)
636                 md->dblock_size = OSSL_get_OP_digest_block_size(fns);
637             break;
638         case OSSL_FUNC_DIGEST_SET_PARAMS:
639             if (md->set_params == NULL)
640                 md->set_params = OSSL_get_OP_digest_set_params(fns);
641             break;
642         case OSSL_FUNC_DIGEST_GET_PARAMS:
643             if (md->get_params == NULL)
644                 md->get_params = OSSL_get_OP_digest_get_params(fns);
645             break;
646         }
647     }
648     if ((fncnt != 0 && fncnt != 5)
649         || (fncnt == 0 && md->digest == NULL)
650         || md->size == NULL) {
651         /*
652          * In order to be a consistent set of functions we either need the
653          * whole set of init/update/final etc functions or none of them.
654          * The "digest" function can standalone. We at least need one way to
655          * generate digests.
656          */
657         EVP_MD_meth_free(md);
658         return NULL;
659     }
660     md->prov = prov;
661     if (prov != NULL)
662         ossl_provider_upref(prov);
663
664     return md;
665 }
666
667 static int evp_md_upref(void *md)
668 {
669     return EVP_MD_upref(md);
670 }
671
672 static void evp_md_free(void *md)
673 {
674     EVP_MD_meth_free(md);
675 }
676
677 EVP_MD *EVP_MD_fetch(OPENSSL_CTX *ctx, const char *algorithm,
678                      const char *properties)
679 {
680     EVP_MD *md =
681         evp_generic_fetch(ctx, OSSL_OP_DIGEST, algorithm, properties,
682                           evp_md_from_dispatch, evp_md_upref,
683                           evp_md_free);
684
685 #ifndef FIPS_MODE
686     /* TODO(3.x) get rid of the need for legacy NIDs */
687     if (md != NULL) {
688         /*
689          * FIPS module note: since internal fetches will be entirely
690          * provider based, we know that none of its code depends on legacy
691          * NIDs or any functionality that use them.
692          */
693         md->type = OBJ_sn2nid(algorithm);
694     }
695 #endif
696
697     return md;
698 }