4b9395d58bf6ae37c1652f969c84900bd2c8aac5
[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 "crypto/evp.h"
18 #include "internal/provider.h"
19 #include "evp_local.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     EVP_MD_CTX_reset(ctx);
85
86     EVP_MD_free(ctx->fetched_digest);
87     ctx->fetched_digest = NULL;
88     ctx->digest = NULL;
89     ctx->reqdigest = NULL;
90
91     OPENSSL_free(ctx);
92     return;
93 }
94
95 int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type)
96 {
97     EVP_MD_CTX_reset(ctx);
98     return EVP_DigestInit_ex(ctx, type, NULL);
99 }
100
101 int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
102 {
103 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
104     ENGINE *tmpimpl = NULL;
105 #endif
106
107     EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
108
109     if (type != NULL)
110         ctx->reqdigest = type;
111
112     /* TODO(3.0): Legacy work around code below. Remove this */
113 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
114     /*
115      * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
116      * this context may already have an ENGINE! Try to avoid releasing the
117      * previous handle, re-querying for an ENGINE, and having a
118      * reinitialisation, when it may all be unnecessary.
119      */
120     if (ctx->engine && ctx->digest &&
121         (type == NULL || (type->type == ctx->digest->type)))
122         goto skip_to_init;
123
124     if (type != NULL) {
125         /*
126          * Ensure an ENGINE left lying around from last time is cleared (the
127          * previous check attempted to avoid this if the same ENGINE and
128          * EVP_MD could be used).
129          */
130         ENGINE_finish(ctx->engine);
131         ctx->engine = NULL;
132     }
133
134     if (type != NULL && impl == NULL)
135         tmpimpl = ENGINE_get_digest_engine(type->type);
136 #endif
137
138     /*
139      * If there are engines involved or if we're being used as part of
140      * EVP_DigestSignInit then we should use legacy handling for now.
141      */
142     if (ctx->engine != NULL
143             || impl != NULL
144 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
145             || tmpimpl != NULL
146 #endif
147             || ctx->pctx != NULL
148             || (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0) {
149         if (ctx->digest == ctx->fetched_digest)
150             ctx->digest = NULL;
151         EVP_MD_free(ctx->fetched_digest);
152         ctx->fetched_digest = NULL;
153         goto legacy;
154     }
155
156     if (ctx->digest != NULL && ctx->digest->ctx_size > 0) {
157         OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
158         ctx->md_data = NULL;
159     }
160
161     /* TODO(3.0): Start of non-legacy code below */
162
163     if (type->prov == NULL) {
164 #ifdef FIPS_MODE
165         /* We only do explict fetches inside the FIPS module */
166         EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
167         return 0;
168 #else
169         EVP_MD *provmd = EVP_MD_fetch(NULL, OBJ_nid2sn(type->type), "");
170
171         if (provmd == NULL) {
172             EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
173             return 0;
174         }
175         type = provmd;
176         EVP_MD_free(ctx->fetched_digest);
177         ctx->fetched_digest = provmd;
178 #endif
179     }
180
181     if (ctx->provctx != NULL && ctx->digest != NULL && ctx->digest != type) {
182         if (ctx->digest->freectx != NULL)
183             ctx->digest->freectx(ctx->provctx);
184         ctx->provctx = NULL;
185     }
186     ctx->digest = type;
187     if (ctx->provctx == NULL) {
188         ctx->provctx = ctx->digest->newctx(ossl_provider_ctx(type->prov));
189         if (ctx->provctx == NULL) {
190             EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
191             return 0;
192         }
193     }
194
195     if (ctx->digest->dinit == NULL) {
196         EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
197         return 0;
198     }
199
200     return ctx->digest->dinit(ctx->provctx);
201
202     /* TODO(3.0): Remove legacy code below */
203  legacy:
204
205 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
206     if (type) {
207         if (impl != NULL) {
208             if (!ENGINE_init(impl)) {
209                 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
210                 return 0;
211             }
212         } else {
213             /* Ask if an ENGINE is reserved for this job */
214             impl = tmpimpl;
215         }
216         if (impl != NULL) {
217             /* There's an ENGINE for this job ... (apparently) */
218             const EVP_MD *d = ENGINE_get_digest(impl, type->type);
219
220             if (d == NULL) {
221                 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
222                 ENGINE_finish(impl);
223                 return 0;
224             }
225             /* We'll use the ENGINE's private digest definition */
226             type = d;
227             /*
228              * Store the ENGINE functional reference so we know 'type' came
229              * from an ENGINE and we need to release it when done.
230              */
231             ctx->engine = impl;
232         } else
233             ctx->engine = NULL;
234     } else {
235         if (!ctx->digest) {
236             EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_NO_DIGEST_SET);
237             return 0;
238         }
239         type = ctx->digest;
240     }
241 #endif
242     if (ctx->digest != type) {
243         if (ctx->digest && ctx->digest->ctx_size) {
244             OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
245             ctx->md_data = NULL;
246         }
247         ctx->digest = type;
248         if (!(ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) && type->ctx_size) {
249             ctx->update = type->update;
250             ctx->md_data = OPENSSL_zalloc(type->ctx_size);
251             if (ctx->md_data == NULL) {
252                 EVPerr(EVP_F_EVP_DIGESTINIT_EX, ERR_R_MALLOC_FAILURE);
253                 return 0;
254             }
255         }
256     }
257 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
258  skip_to_init:
259 #endif
260 #ifndef FIPS_MODE
261     /*
262      * TODO(3.0): Temporarily no support for EVP_DigestSign* inside FIPS module
263      * or when using providers.
264      */
265     if (ctx->pctx != NULL
266             && (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx->pctx)
267                  || ctx->pctx->op.sig.signature == NULL)) {
268         int r;
269         r = EVP_PKEY_CTX_ctrl(ctx->pctx, -1, EVP_PKEY_OP_TYPE_SIG,
270                               EVP_PKEY_CTRL_DIGESTINIT, 0, ctx);
271         if (r <= 0 && (r != -2))
272             return 0;
273     }
274 #endif
275     if (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT)
276         return 1;
277     return ctx->digest->init(ctx);
278 }
279
280 int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t count)
281 {
282     if (count == 0)
283         return 1;
284
285     if (ctx->digest == NULL || ctx->digest->prov == NULL)
286         goto legacy;
287
288     if (ctx->digest->dupdate == NULL) {
289         EVPerr(EVP_F_EVP_DIGESTUPDATE, EVP_R_UPDATE_ERROR);
290         return 0;
291     }
292     return ctx->digest->dupdate(ctx->provctx, data, count);
293
294     /* TODO(3.0): Remove legacy code below */
295  legacy:
296     return ctx->update(ctx, data, count);
297 }
298
299 /* The caller can assume that this removes any secret data from the context */
300 int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size)
301 {
302     int ret;
303     ret = EVP_DigestFinal_ex(ctx, md, size);
304     EVP_MD_CTX_reset(ctx);
305     return ret;
306 }
307
308 /* The caller can assume that this removes any secret data from the context */
309 int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *isize)
310 {
311     int ret;
312     size_t size = 0;
313     size_t mdsize = EVP_MD_size(ctx->digest);
314
315     if (ctx->digest == NULL || ctx->digest->prov == NULL)
316         goto legacy;
317
318     if (ctx->digest->dfinal == NULL) {
319         EVPerr(EVP_F_EVP_DIGESTFINAL_EX, EVP_R_FINAL_ERROR);
320         return 0;
321     }
322
323     ret = ctx->digest->dfinal(ctx->provctx, md, &size, mdsize);
324
325     if (isize != NULL) {
326         if (size <= UINT_MAX) {
327             *isize = (int)size;
328         } else {
329             EVPerr(EVP_F_EVP_DIGESTFINAL_EX, EVP_R_FINAL_ERROR);
330             ret = 0;
331         }
332     }
333
334     EVP_MD_CTX_reset(ctx);
335     return ret;
336
337     /* TODO(3.0): Remove legacy code below */
338  legacy:
339     OPENSSL_assert(mdsize <= EVP_MAX_MD_SIZE);
340     ret = ctx->digest->final(ctx, md);
341     if (isize != NULL)
342         *isize = mdsize;
343     if (ctx->digest->cleanup) {
344         ctx->digest->cleanup(ctx);
345         EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
346     }
347     OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
348     return ret;
349 }
350
351 int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t size)
352 {
353     int ret = 0;
354     OSSL_PARAM params[2];
355     size_t i = 0;
356
357     if (ctx->digest == NULL || ctx->digest->prov == NULL)
358         goto legacy;
359
360     if (ctx->digest->dfinal == NULL) {
361         EVPerr(EVP_F_EVP_DIGESTFINALXOF, EVP_R_FINAL_ERROR);
362         return 0;
363     }
364
365     params[i++] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN, &size);
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_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_up_ref(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_get_params(const EVP_MD *digest, OSSL_PARAM params[])
525 {
526     if (digest != NULL && digest->get_params != NULL)
527         return digest->get_params(params);
528     return 0;
529 }
530
531 const OSSL_PARAM *EVP_MD_gettable_params(const EVP_MD *digest)
532 {
533     if (digest != NULL && digest->gettable_params != NULL)
534         return digest->gettable_params();
535     return NULL;
536 }
537
538 int EVP_MD_CTX_set_params(EVP_MD_CTX *ctx, const OSSL_PARAM params[])
539 {
540     if (ctx->digest != NULL && ctx->digest->set_ctx_params != NULL)
541         return ctx->digest->set_ctx_params(ctx->provctx, params);
542     return 0;
543 }
544
545 const OSSL_PARAM *EVP_MD_settable_ctx_params(const EVP_MD *md)
546 {
547     if (md != NULL && md->settable_ctx_params != NULL)
548         return md->settable_ctx_params();
549     return NULL;
550 }
551
552 const OSSL_PARAM *EVP_MD_CTX_settable_params(EVP_MD_CTX *ctx)
553 {
554     if (ctx != NULL
555             && ctx->digest != NULL
556             && ctx->digest->settable_ctx_params != NULL)
557         return ctx->digest->settable_ctx_params();
558
559     return NULL;
560 }
561
562 int EVP_MD_CTX_get_params(EVP_MD_CTX *ctx, OSSL_PARAM params[])
563 {
564     if (ctx->digest != NULL && ctx->digest->get_params != NULL)
565         return ctx->digest->get_ctx_params(ctx->provctx, params);
566     return 0;
567 }
568
569 const OSSL_PARAM *EVP_MD_gettable_ctx_params(const EVP_MD *md)
570 {
571     if (md != NULL && md->gettable_ctx_params != NULL)
572         return md->gettable_ctx_params();
573     return NULL;
574 }
575
576 const OSSL_PARAM *EVP_MD_CTX_gettable_params(EVP_MD_CTX *ctx)
577 {
578     if (ctx != NULL
579             && ctx->digest != NULL
580             && ctx->digest->gettable_ctx_params != NULL)
581         return ctx->digest->gettable_ctx_params();
582
583     return NULL;
584 }
585
586 /* TODO(3.0): Remove legacy code below - only used by engines & DigestSign */
587 int EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void *p2)
588 {
589     int ret = EVP_CTRL_RET_UNSUPPORTED;
590     int set_params = 1;
591     size_t sz;
592     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
593
594     if (ctx == NULL || ctx->digest == NULL) {
595         ERR_raise(ERR_LIB_EVP, EVP_R_MESSAGE_DIGEST_IS_NULL);
596         return 0;
597     }
598
599     if (ctx->digest->prov == NULL)
600         goto legacy;
601
602     switch (cmd) {
603     case EVP_MD_CTRL_XOF_LEN:
604         sz = (size_t)p1;
605         params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN, &sz);
606         break;
607     case EVP_MD_CTRL_MICALG:
608         set_params = 0;
609         params[0] = OSSL_PARAM_construct_utf8_string(OSSL_DIGEST_PARAM_MICALG,
610                                                      p2, p1 ? p1 : 9999);
611         break;
612     default:
613         return EVP_CTRL_RET_UNSUPPORTED;
614     }
615
616     if (set_params)
617         ret = evp_do_md_ctx_setparams(ctx->digest, ctx->provctx, params);
618     else
619         ret = evp_do_md_ctx_getparams(ctx->digest, ctx->provctx, params);
620     return ret;
621
622
623 /* TODO(3.0): Remove legacy code below */
624  legacy:
625     if (ctx->digest->md_ctrl == NULL) {
626         ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_NOT_IMPLEMENTED);
627         return 0;
628     }
629
630     ret = ctx->digest->md_ctrl(ctx, cmd, p1, p2);
631     if (ret <= 0)
632         return 0;
633     return ret;
634 }
635
636 EVP_MD *evp_md_new(void)
637 {
638     EVP_MD *md = OPENSSL_zalloc(sizeof(*md));
639
640     if (md != NULL) {
641         md->lock = CRYPTO_THREAD_lock_new();
642         if (md->lock == NULL) {
643             OPENSSL_free(md);
644             return NULL;
645         }
646         md->refcnt = 1;
647     }
648     return md;
649 }
650
651 /*
652  * FIPS module note: since internal fetches will be entirely
653  * provider based, we know that none of its code depends on legacy
654  * NIDs or any functionality that use them.
655  */
656 #ifndef FIPS_MODE
657 /* TODO(3.x) get rid of the need for legacy NIDs */
658 static void set_legacy_nid(const char *name, void *vlegacy_nid)
659 {
660     int nid;
661     int *legacy_nid = vlegacy_nid;
662
663     if (*legacy_nid == -1)       /* We found a clash already */
664         return;
665     if ((nid = OBJ_sn2nid(name)) == NID_undef
666         && (nid = OBJ_ln2nid(name)) == NID_undef)
667         return;
668     if (*legacy_nid != NID_undef && *legacy_nid != nid) {
669         *legacy_nid = -1;
670         return;
671     }
672     *legacy_nid = nid;
673 }
674 #endif
675
676 static void *evp_md_from_dispatch(int name_id,
677                                   const OSSL_DISPATCH *fns,
678                                   OSSL_PROVIDER *prov, void *unused)
679 {
680     EVP_MD *md = NULL;
681     int fncnt = 0;
682
683     /* EVP_MD_fetch() will set the legacy NID if available */
684     if ((md = evp_md_new()) == NULL) {
685         EVPerr(0, ERR_R_MALLOC_FAILURE);
686         return NULL;
687     }
688
689 #ifndef FIPS_MODE
690     /* TODO(3.x) get rid of the need for legacy NIDs */
691     md->type = NID_undef;
692     evp_doall_names(prov, name_id, set_legacy_nid, &md->type);
693     if (md->type == -1) {
694         ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
695         EVP_MD_free(md);
696         return NULL;
697     }
698 #endif
699
700     md->name_id = name_id;
701
702     for (; fns->function_id != 0; fns++) {
703         switch (fns->function_id) {
704         case OSSL_FUNC_DIGEST_NEWCTX:
705             if (md->newctx == NULL) {
706                 md->newctx = OSSL_get_OP_digest_newctx(fns);
707                 fncnt++;
708             }
709             break;
710         case OSSL_FUNC_DIGEST_INIT:
711             if (md->dinit == NULL) {
712                 md->dinit = OSSL_get_OP_digest_init(fns);
713                 fncnt++;
714             }
715             break;
716         case OSSL_FUNC_DIGEST_UPDATE:
717             if (md->dupdate == NULL) {
718                 md->dupdate = OSSL_get_OP_digest_update(fns);
719                 fncnt++;
720             }
721             break;
722         case OSSL_FUNC_DIGEST_FINAL:
723             if (md->dfinal == NULL) {
724                 md->dfinal = OSSL_get_OP_digest_final(fns);
725                 fncnt++;
726             }
727             break;
728         case OSSL_FUNC_DIGEST_DIGEST:
729             if (md->digest == NULL)
730                 md->digest = OSSL_get_OP_digest_digest(fns);
731             /* We don't increment fnct for this as it is stand alone */
732             break;
733         case OSSL_FUNC_DIGEST_FREECTX:
734             if (md->freectx == NULL) {
735                 md->freectx = OSSL_get_OP_digest_freectx(fns);
736                 fncnt++;
737             }
738             break;
739         case OSSL_FUNC_DIGEST_DUPCTX:
740             if (md->dupctx == NULL)
741                 md->dupctx = OSSL_get_OP_digest_dupctx(fns);
742             break;
743         case OSSL_FUNC_DIGEST_GET_PARAMS:
744             if (md->get_params == NULL)
745                 md->get_params = OSSL_get_OP_digest_get_params(fns);
746             break;
747         case OSSL_FUNC_DIGEST_SET_CTX_PARAMS:
748             if (md->set_ctx_params == NULL)
749                 md->set_ctx_params = OSSL_get_OP_digest_set_ctx_params(fns);
750             break;
751         case OSSL_FUNC_DIGEST_GET_CTX_PARAMS:
752             if (md->get_ctx_params == NULL)
753                 md->get_ctx_params = OSSL_get_OP_digest_get_ctx_params(fns);
754             break;
755         case OSSL_FUNC_DIGEST_GETTABLE_PARAMS:
756             if (md->gettable_params == NULL)
757                 md->gettable_params = OSSL_get_OP_digest_gettable_params(fns);
758             break;
759         case OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS:
760             if (md->settable_ctx_params == NULL)
761                 md->settable_ctx_params =
762                     OSSL_get_OP_digest_settable_ctx_params(fns);
763             break;
764         case OSSL_FUNC_DIGEST_GETTABLE_CTX_PARAMS:
765             if (md->gettable_ctx_params == NULL)
766                 md->gettable_ctx_params =
767                     OSSL_get_OP_digest_gettable_ctx_params(fns);
768             break;
769         }
770     }
771     if ((fncnt != 0 && fncnt != 5)
772         || (fncnt == 0 && md->digest == NULL)) {
773         /*
774          * In order to be a consistent set of functions we either need the
775          * whole set of init/update/final etc functions or none of them.
776          * The "digest" function can standalone. We at least need one way to
777          * generate digests.
778          */
779         EVP_MD_free(md);
780         ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
781         return NULL;
782     }
783     md->prov = prov;
784     if (prov != NULL)
785         ossl_provider_up_ref(prov);
786
787     return md;
788 }
789
790 static int evp_md_up_ref(void *md)
791 {
792     return EVP_MD_up_ref(md);
793 }
794
795 static void evp_md_free(void *md)
796 {
797     EVP_MD_free(md);
798 }
799
800 EVP_MD *EVP_MD_fetch(OPENSSL_CTX *ctx, const char *algorithm,
801                      const char *properties)
802 {
803     EVP_MD *md =
804         evp_generic_fetch(ctx, OSSL_OP_DIGEST, algorithm, properties,
805                           evp_md_from_dispatch, NULL, evp_md_up_ref,
806                           evp_md_free);
807
808     return md;
809 }
810
811 int EVP_MD_up_ref(EVP_MD *md)
812 {
813     int ref = 0;
814
815     CRYPTO_UP_REF(&md->refcnt, &ref, md->lock);
816     return 1;
817 }
818
819 void EVP_MD_free(EVP_MD *md)
820 {
821     int i;
822
823     if (md == NULL)
824         return;
825
826     CRYPTO_DOWN_REF(&md->refcnt, &i, md->lock);
827     if (i > 0)
828         return;
829     ossl_provider_free(md->prov);
830     CRYPTO_THREAD_lock_free(md->lock);
831     OPENSSL_free(md);
832 }
833
834 void EVP_MD_do_all_ex(OPENSSL_CTX *libctx,
835                           void (*fn)(EVP_MD *mac, void *arg),
836                           void *arg)
837 {
838     evp_generic_do_all(libctx, OSSL_OP_DIGEST,
839                        (void (*)(void *, void *))fn, arg,
840                        evp_md_from_dispatch, NULL, evp_md_free);
841 }