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