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