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