2 * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
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
10 /* We need to use some engine deprecated APIs */
11 #define OPENSSL_SUPPRESS_DEPRECATED
14 #include <openssl/objects.h>
15 #include <openssl/evp.h>
16 #include <openssl/ec.h>
18 # include <openssl/engine.h>
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"
28 static void cleanup_old_md_data(EVP_MD_CTX *ctx, int force)
30 if (ctx->digest != NULL) {
31 if (ctx->digest->cleanup != NULL
32 && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_CLEANED))
33 ctx->digest->cleanup(ctx);
34 if (ctx->md_data != NULL && ctx->digest->ctx_size > 0
35 && (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_REUSE)
37 OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
43 void evp_md_ctx_clear_digest(EVP_MD_CTX *ctx, int force, int keep_fetched)
45 if (ctx->algctx != NULL) {
46 if (ctx->digest != NULL && ctx->digest->freectx != NULL)
47 ctx->digest->freectx(ctx->algctx);
49 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
52 /* Code below to be removed when legacy support is dropped. */
55 * Don't assume ctx->md_data was cleaned in EVP_Digest_Final, because
56 * sometimes only copies of the context are ever finalised.
58 cleanup_old_md_data(ctx, force);
62 #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
63 ENGINE_finish(ctx->engine);
67 /* Non legacy code, this has to be later than the ctx->digest cleaning */
69 EVP_MD_free(ctx->fetched_digest);
70 ctx->fetched_digest = NULL;
71 ctx->reqdigest = NULL;
75 static int evp_md_ctx_reset_ex(EVP_MD_CTX *ctx, int keep_fetched)
82 * pctx should be freed by the user of EVP_MD_CTX
83 * if EVP_MD_CTX_FLAG_KEEP_PKEY_CTX is set
85 if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX)) {
86 EVP_PKEY_CTX_free(ctx->pctx);
91 evp_md_ctx_clear_digest(ctx, 0, keep_fetched);
93 OPENSSL_cleanse(ctx, sizeof(*ctx));
98 /* This call frees resources associated with the context */
99 int EVP_MD_CTX_reset(EVP_MD_CTX *ctx)
101 return evp_md_ctx_reset_ex(ctx, 0);
105 EVP_MD_CTX *evp_md_ctx_new_ex(EVP_PKEY *pkey, const ASN1_OCTET_STRING *id,
106 OSSL_LIB_CTX *libctx, const char *propq)
109 EVP_PKEY_CTX *pctx = NULL;
111 if ((ctx = EVP_MD_CTX_new()) == NULL
112 || (pctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq)) == NULL) {
113 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
117 if (id != NULL && EVP_PKEY_CTX_set1_id(pctx, id->data, id->length) <= 0)
120 EVP_MD_CTX_set_pkey_ctx(ctx, pctx);
124 EVP_PKEY_CTX_free(pctx);
125 EVP_MD_CTX_free(ctx);
130 EVP_MD_CTX *EVP_MD_CTX_new(void)
132 return OPENSSL_zalloc(sizeof(EVP_MD_CTX));
135 void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
140 EVP_MD_CTX_reset(ctx);
144 static int evp_md_init_internal(EVP_MD_CTX *ctx, const EVP_MD *type,
145 const OSSL_PARAM params[], ENGINE *impl)
147 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
148 ENGINE *tmpimpl = NULL;
151 #if !defined(FIPS_MODULE)
152 if (ctx->pctx != NULL
153 && EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx->pctx)
154 && ctx->pctx->op.sig.algctx != NULL) {
156 * Prior to OpenSSL 3.0 calling EVP_DigestInit_ex() on an mdctx
157 * previously initialised with EVP_DigestSignInit() would retain
158 * information about the key, and re-initialise for another sign
159 * operation. So in that case we redirect to EVP_DigestSignInit()
161 if (ctx->pctx->operation == EVP_PKEY_OP_SIGNCTX)
162 return EVP_DigestSignInit(ctx, NULL, type, impl, NULL);
163 if (ctx->pctx->operation == EVP_PKEY_OP_VERIFYCTX)
164 return EVP_DigestVerifyInit(ctx, NULL, type, impl, NULL);
165 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
170 EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
172 if (ctx->algctx != NULL) {
173 if (!ossl_assert(ctx->digest != NULL)) {
174 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
177 if (ctx->digest->freectx != NULL)
178 ctx->digest->freectx(ctx->algctx);
183 ctx->reqdigest = type;
185 if (ctx->digest == NULL) {
186 ERR_raise(ERR_LIB_EVP, EVP_R_NO_DIGEST_SET);
192 /* Code below to be removed when legacy support is dropped. */
193 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
195 * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
196 * this context may already have an ENGINE! Try to avoid releasing the
197 * previous handle, re-querying for an ENGINE, and having a
198 * reinitialisation, when it may all be unnecessary.
200 if (ctx->engine && ctx->digest &&
201 (type == NULL || (type->type == ctx->digest->type)))
206 * Ensure an ENGINE left lying around from last time is cleared (the
207 * previous check attempted to avoid this if the same ENGINE and
208 * EVP_MD could be used).
210 ENGINE_finish(ctx->engine);
214 if (type != NULL && impl == NULL)
215 tmpimpl = ENGINE_get_digest_engine(type->type);
219 * If there are engines involved or EVP_MD_CTX_FLAG_NO_INIT is set then we
220 * should use legacy handling for now.
222 if (ctx->engine != NULL
224 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
227 || (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0
228 || (type != NULL && type->origin == EVP_ORIG_METH)
229 || (type == NULL && ctx->digest != NULL
230 && ctx->digest->origin == EVP_ORIG_METH)) {
231 if (ctx->digest == ctx->fetched_digest)
233 EVP_MD_free(ctx->fetched_digest);
234 ctx->fetched_digest = NULL;
238 cleanup_old_md_data(ctx, 1);
240 /* Start of non-legacy code below */
242 if (type->prov == NULL) {
244 /* We only do explicit fetches inside the FIPS module */
245 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
248 /* The NULL digest is a special case */
249 EVP_MD *provmd = EVP_MD_fetch(NULL,
250 type->type != NID_undef ? OBJ_nid2sn(type->type)
253 if (provmd == NULL) {
254 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
258 EVP_MD_free(ctx->fetched_digest);
259 ctx->fetched_digest = provmd;
263 if (ctx->algctx != NULL && ctx->digest != NULL && ctx->digest != type) {
264 if (ctx->digest->freectx != NULL)
265 ctx->digest->freectx(ctx->algctx);
268 if (type->prov != NULL && ctx->fetched_digest != type) {
269 if (!EVP_MD_up_ref((EVP_MD *)type)) {
270 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
273 EVP_MD_free(ctx->fetched_digest);
274 ctx->fetched_digest = (EVP_MD *)type;
277 if (ctx->algctx == NULL) {
278 ctx->algctx = ctx->digest->newctx(ossl_provider_ctx(type->prov));
279 if (ctx->algctx == NULL) {
280 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
285 if (ctx->digest->dinit == NULL) {
286 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
290 return ctx->digest->dinit(ctx->algctx, params);
292 /* Code below to be removed when legacy support is dropped. */
295 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
298 if (!ENGINE_init(impl)) {
299 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
303 /* Ask if an ENGINE is reserved for this job */
307 /* There's an ENGINE for this job ... (apparently) */
308 const EVP_MD *d = ENGINE_get_digest(impl, type->type);
311 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
315 /* We'll use the ENGINE's private digest definition */
318 * Store the ENGINE functional reference so we know 'type' came
319 * from an ENGINE and we need to release it when done.
326 if (ctx->digest != type) {
327 cleanup_old_md_data(ctx, 1);
330 if (!(ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) && type->ctx_size) {
331 ctx->update = type->update;
332 ctx->md_data = OPENSSL_zalloc(type->ctx_size);
333 if (ctx->md_data == NULL) {
334 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
339 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
343 if (ctx->pctx != NULL
344 && (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx->pctx)
345 || ctx->pctx->op.sig.signature == NULL)) {
347 r = EVP_PKEY_CTX_ctrl(ctx->pctx, -1, EVP_PKEY_OP_TYPE_SIG,
348 EVP_PKEY_CTRL_DIGESTINIT, 0, ctx);
349 if (r <= 0 && (r != -2))
353 if (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT)
355 return ctx->digest->init(ctx);
358 int EVP_DigestInit_ex2(EVP_MD_CTX *ctx, const EVP_MD *type,
359 const OSSL_PARAM params[])
361 return evp_md_init_internal(ctx, type, params, NULL);
364 int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type)
366 EVP_MD_CTX_reset(ctx);
367 return evp_md_init_internal(ctx, type, NULL, NULL);
370 int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
372 return evp_md_init_internal(ctx, type, NULL, impl);
375 int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t count)
380 if (ctx->pctx != NULL
381 && EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx->pctx)
382 && ctx->pctx->op.sig.algctx != NULL) {
384 * Prior to OpenSSL 3.0 EVP_DigestSignUpdate() and
385 * EVP_DigestVerifyUpdate() were just macros for EVP_DigestUpdate().
386 * Some code calls EVP_DigestUpdate() directly even when initialised
387 * with EVP_DigestSignInit_ex() or
388 * EVP_DigestVerifyInit_ex(), so we detect that and redirect to
389 * the correct EVP_Digest*Update() function
391 if (ctx->pctx->operation == EVP_PKEY_OP_SIGNCTX)
392 return EVP_DigestSignUpdate(ctx, data, count);
393 if (ctx->pctx->operation == EVP_PKEY_OP_VERIFYCTX)
394 return EVP_DigestVerifyUpdate(ctx, data, count);
395 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
399 if (ctx->digest == NULL
400 || ctx->digest->prov == NULL
401 || (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0)
404 if (ctx->digest->dupdate == NULL) {
405 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
408 return ctx->digest->dupdate(ctx->algctx, data, count);
410 /* Code below to be removed when legacy support is dropped. */
412 return ctx->update(ctx, data, count);
415 /* The caller can assume that this removes any secret data from the context */
416 int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size)
419 ret = EVP_DigestFinal_ex(ctx, md, size);
420 EVP_MD_CTX_reset(ctx);
424 /* The caller can assume that this removes any secret data from the context */
425 int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *isize)
431 if (ctx->digest == NULL)
434 sz = EVP_MD_get_size(ctx->digest);
438 if (ctx->digest->prov == NULL)
441 if (ctx->digest->dfinal == NULL) {
442 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
446 ret = ctx->digest->dfinal(ctx->algctx, md, &size, mdsize);
449 if (size <= UINT_MAX) {
450 *isize = (unsigned int)size;
452 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
459 /* Code below to be removed when legacy support is dropped. */
461 OPENSSL_assert(mdsize <= EVP_MAX_MD_SIZE);
462 ret = ctx->digest->final(ctx, md);
465 if (ctx->digest->cleanup) {
466 ctx->digest->cleanup(ctx);
467 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
469 OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
473 int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t size)
476 OSSL_PARAM params[2];
479 if (ctx->digest == NULL) {
480 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_NULL_ALGORITHM);
484 if (ctx->digest->prov == NULL)
487 if (ctx->digest->dfinal == NULL) {
488 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
492 params[i++] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN, &size);
493 params[i++] = OSSL_PARAM_construct_end();
495 if (EVP_MD_CTX_set_params(ctx, params) > 0)
496 ret = ctx->digest->dfinal(ctx->algctx, md, &size, size);
501 if (ctx->digest->flags & EVP_MD_FLAG_XOF
503 && ctx->digest->md_ctrl(ctx, EVP_MD_CTRL_XOF_LEN, (int)size, NULL)) {
504 ret = ctx->digest->final(ctx, md);
505 if (ctx->digest->cleanup != NULL) {
506 ctx->digest->cleanup(ctx);
507 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
509 OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
511 ERR_raise(ERR_LIB_EVP, EVP_R_NOT_XOF_OR_INVALID_LENGTH);
517 int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in)
519 EVP_MD_CTX_reset(out);
520 return EVP_MD_CTX_copy_ex(out, in);
523 int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in)
525 int digest_change = 0;
526 unsigned char *tmp_buf;
529 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
533 if (in->digest == NULL) {
534 /* copying uninitialized digest context */
535 EVP_MD_CTX_reset(out);
536 if (out->fetched_digest != NULL)
537 EVP_MD_free(out->fetched_digest);
542 if (in->digest->prov == NULL
543 || (in->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0)
546 if (in->digest->dupctx == NULL) {
547 ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX);
551 evp_md_ctx_reset_ex(out, 1);
552 digest_change = (out->fetched_digest != in->fetched_digest);
553 if (digest_change && out->fetched_digest != NULL)
554 EVP_MD_free(out->fetched_digest);
556 /* NULL out pointers in case of error */
560 if (digest_change && in->fetched_digest != NULL)
561 EVP_MD_up_ref(in->fetched_digest);
563 if (in->algctx != NULL) {
564 out->algctx = in->digest->dupctx(in->algctx);
565 if (out->algctx == NULL) {
566 ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX);
572 /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
573 EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
575 if (in->pctx != NULL) {
576 out->pctx = EVP_PKEY_CTX_dup(in->pctx);
577 if (out->pctx == NULL) {
578 ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX);
579 EVP_MD_CTX_reset(out);
587 /* Code below to be removed when legacy support is dropped. */
589 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
590 /* Make sure it's safe to copy a digest context using an ENGINE */
591 if (in->engine && !ENGINE_init(in->engine)) {
592 ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB);
597 if (out->digest == in->digest) {
598 tmp_buf = out->md_data;
599 EVP_MD_CTX_set_flags(out, EVP_MD_CTX_FLAG_REUSE);
602 EVP_MD_CTX_reset(out);
603 memcpy(out, in, sizeof(*out));
605 /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
606 EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
608 /* Null these variables, since they are getting fixed up
609 * properly below. Anything else may cause a memleak and/or
610 * double free if any of the memory allocations below fail
615 if (in->md_data && out->digest->ctx_size) {
617 out->md_data = tmp_buf;
619 out->md_data = OPENSSL_malloc(out->digest->ctx_size);
620 if (out->md_data == NULL) {
621 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
625 memcpy(out->md_data, in->md_data, out->digest->ctx_size);
628 out->update = in->update;
632 out->pctx = EVP_PKEY_CTX_dup(in->pctx);
634 EVP_MD_CTX_reset(out);
640 if (out->digest->copy)
641 return out->digest->copy(out, in);
646 int EVP_Digest(const void *data, size_t count,
647 unsigned char *md, unsigned int *size, const EVP_MD *type,
650 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
655 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT);
656 ret = EVP_DigestInit_ex(ctx, type, impl)
657 && EVP_DigestUpdate(ctx, data, count)
658 && EVP_DigestFinal_ex(ctx, md, size);
659 EVP_MD_CTX_free(ctx);
664 int EVP_Q_digest(OSSL_LIB_CTX *libctx, const char *name, const char *propq,
665 const void *data, size_t datalen,
666 unsigned char *md, size_t *mdlen)
668 EVP_MD *digest = EVP_MD_fetch(libctx, name, propq);
669 unsigned int temp = 0;
672 if (digest != NULL) {
673 ret = EVP_Digest(data, datalen, md, &temp, digest, NULL);
681 int EVP_MD_get_params(const EVP_MD *digest, OSSL_PARAM params[])
683 if (digest != NULL && digest->get_params != NULL)
684 return digest->get_params(params);
688 const OSSL_PARAM *EVP_MD_gettable_params(const EVP_MD *digest)
690 if (digest != NULL && digest->gettable_params != NULL)
691 return digest->gettable_params(
692 ossl_provider_ctx(EVP_MD_get0_provider(digest)));
696 int EVP_MD_CTX_set_params(EVP_MD_CTX *ctx, const OSSL_PARAM params[])
698 EVP_PKEY_CTX *pctx = ctx->pctx;
700 /* If we have a pctx then we should try that first */
702 && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
703 || pctx->operation == EVP_PKEY_OP_SIGNCTX)
704 && pctx->op.sig.algctx != NULL
705 && pctx->op.sig.signature->set_ctx_md_params != NULL)
706 return pctx->op.sig.signature->set_ctx_md_params(pctx->op.sig.algctx,
709 if (ctx->digest != NULL && ctx->digest->set_ctx_params != NULL)
710 return ctx->digest->set_ctx_params(ctx->algctx, params);
715 const OSSL_PARAM *EVP_MD_settable_ctx_params(const EVP_MD *md)
719 if (md != NULL && md->settable_ctx_params != NULL) {
720 provctx = ossl_provider_ctx(EVP_MD_get0_provider(md));
721 return md->settable_ctx_params(NULL, provctx);
726 const OSSL_PARAM *EVP_MD_CTX_settable_params(EVP_MD_CTX *ctx)
734 /* If we have a pctx then we should try that first */
737 && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
738 || pctx->operation == EVP_PKEY_OP_SIGNCTX)
739 && pctx->op.sig.algctx != NULL
740 && pctx->op.sig.signature->settable_ctx_md_params != NULL)
741 return pctx->op.sig.signature->settable_ctx_md_params(
742 pctx->op.sig.algctx);
744 if (ctx->digest != NULL && ctx->digest->settable_ctx_params != NULL) {
745 alg = ossl_provider_ctx(EVP_MD_get0_provider(ctx->digest));
746 return ctx->digest->settable_ctx_params(ctx->algctx, alg);
752 int EVP_MD_CTX_get_params(EVP_MD_CTX *ctx, OSSL_PARAM params[])
754 EVP_PKEY_CTX *pctx = ctx->pctx;
756 /* If we have a pctx then we should try that first */
758 && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
759 || pctx->operation == EVP_PKEY_OP_SIGNCTX)
760 && pctx->op.sig.algctx != NULL
761 && pctx->op.sig.signature->get_ctx_md_params != NULL)
762 return pctx->op.sig.signature->get_ctx_md_params(pctx->op.sig.algctx,
765 if (ctx->digest != NULL && ctx->digest->get_ctx_params != NULL)
766 return ctx->digest->get_ctx_params(ctx->algctx, params);
771 const OSSL_PARAM *EVP_MD_gettable_ctx_params(const EVP_MD *md)
775 if (md != NULL && md->gettable_ctx_params != NULL) {
776 provctx = ossl_provider_ctx(EVP_MD_get0_provider(md));
777 return md->gettable_ctx_params(NULL, provctx);
782 const OSSL_PARAM *EVP_MD_CTX_gettable_params(EVP_MD_CTX *ctx)
790 /* If we have a pctx then we should try that first */
793 && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
794 || pctx->operation == EVP_PKEY_OP_SIGNCTX)
795 && pctx->op.sig.algctx != NULL
796 && pctx->op.sig.signature->gettable_ctx_md_params != NULL)
797 return pctx->op.sig.signature->gettable_ctx_md_params(
798 pctx->op.sig.algctx);
800 if (ctx->digest != NULL && ctx->digest->gettable_ctx_params != NULL) {
801 provctx = ossl_provider_ctx(EVP_MD_get0_provider(ctx->digest));
802 return ctx->digest->gettable_ctx_params(ctx->algctx, provctx);
807 int EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void *p2)
809 int ret = EVP_CTRL_RET_UNSUPPORTED;
812 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
815 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
819 if (ctx->digest != NULL && ctx->digest->prov == NULL)
823 case EVP_MD_CTRL_XOF_LEN:
825 params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN, &sz);
827 case EVP_MD_CTRL_MICALG:
829 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_DIGEST_PARAM_MICALG,
832 case EVP_CTRL_SSL3_MASTER_SECRET:
833 params[0] = OSSL_PARAM_construct_octet_string(OSSL_DIGEST_PARAM_SSL3_MS,
841 ret = EVP_MD_CTX_set_params(ctx, params);
843 ret = EVP_MD_CTX_get_params(ctx, params);
847 /* Code below to be removed when legacy support is dropped. */
849 if (ctx->digest->md_ctrl == NULL) {
850 ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_NOT_IMPLEMENTED);
854 ret = ctx->digest->md_ctrl(ctx, cmd, p1, p2);
861 EVP_MD *evp_md_new(void)
863 EVP_MD *md = OPENSSL_zalloc(sizeof(*md));
866 md->lock = CRYPTO_THREAD_lock_new();
867 if (md->lock == NULL) {
877 * FIPS module note: since internal fetches will be entirely
878 * provider based, we know that none of its code depends on legacy
879 * NIDs or any functionality that use them.
882 static void set_legacy_nid(const char *name, void *vlegacy_nid)
885 int *legacy_nid = vlegacy_nid;
887 * We use lowest level function to get the associated method, because
888 * higher level functions such as EVP_get_digestbyname() have changed
889 * to look at providers too.
891 const void *legacy_method = OBJ_NAME_get(name, OBJ_NAME_TYPE_MD_METH);
893 if (*legacy_nid == -1) /* We found a clash already */
896 if (legacy_method == NULL)
898 nid = EVP_MD_nid(legacy_method);
899 if (*legacy_nid != NID_undef && *legacy_nid != nid) {
907 static int evp_md_cache_constants(EVP_MD *md)
909 int ok, xof = 0, algid_absent = 0;
912 OSSL_PARAM params[5];
914 params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_BLOCK_SIZE, &blksz);
915 params[1] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_SIZE, &mdsize);
916 params[2] = OSSL_PARAM_construct_int(OSSL_DIGEST_PARAM_XOF, &xof);
917 params[3] = OSSL_PARAM_construct_int(OSSL_DIGEST_PARAM_ALGID_ABSENT,
919 params[4] = OSSL_PARAM_construct_end();
920 ok = evp_do_md_getparams(md, params) > 0;
921 if (mdsize > INT_MAX || blksz > INT_MAX)
924 md->block_size = (int)blksz;
925 md->md_size = (int)mdsize;
927 md->flags |= EVP_MD_FLAG_XOF;
929 md->flags |= EVP_MD_FLAG_DIGALGID_ABSENT;
934 static void *evp_md_from_algorithm(int name_id,
935 const OSSL_ALGORITHM *algodef,
938 const OSSL_DISPATCH *fns = algodef->implementation;
942 /* EVP_MD_fetch() will set the legacy NID if available */
943 if ((md = evp_md_new()) == NULL) {
944 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
949 md->type = NID_undef;
950 if (!evp_names_do_all(prov, name_id, set_legacy_nid, &md->type)
952 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
958 md->name_id = name_id;
959 if ((md->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL) {
963 md->description = algodef->algorithm_description;
965 for (; fns->function_id != 0; fns++) {
966 switch (fns->function_id) {
967 case OSSL_FUNC_DIGEST_NEWCTX:
968 if (md->newctx == NULL) {
969 md->newctx = OSSL_FUNC_digest_newctx(fns);
973 case OSSL_FUNC_DIGEST_INIT:
974 if (md->dinit == NULL) {
975 md->dinit = OSSL_FUNC_digest_init(fns);
979 case OSSL_FUNC_DIGEST_UPDATE:
980 if (md->dupdate == NULL) {
981 md->dupdate = OSSL_FUNC_digest_update(fns);
985 case OSSL_FUNC_DIGEST_FINAL:
986 if (md->dfinal == NULL) {
987 md->dfinal = OSSL_FUNC_digest_final(fns);
991 case OSSL_FUNC_DIGEST_DIGEST:
992 if (md->digest == NULL)
993 md->digest = OSSL_FUNC_digest_digest(fns);
994 /* We don't increment fnct for this as it is stand alone */
996 case OSSL_FUNC_DIGEST_FREECTX:
997 if (md->freectx == NULL) {
998 md->freectx = OSSL_FUNC_digest_freectx(fns);
1002 case OSSL_FUNC_DIGEST_DUPCTX:
1003 if (md->dupctx == NULL)
1004 md->dupctx = OSSL_FUNC_digest_dupctx(fns);
1006 case OSSL_FUNC_DIGEST_GET_PARAMS:
1007 if (md->get_params == NULL)
1008 md->get_params = OSSL_FUNC_digest_get_params(fns);
1010 case OSSL_FUNC_DIGEST_SET_CTX_PARAMS:
1011 if (md->set_ctx_params == NULL)
1012 md->set_ctx_params = OSSL_FUNC_digest_set_ctx_params(fns);
1014 case OSSL_FUNC_DIGEST_GET_CTX_PARAMS:
1015 if (md->get_ctx_params == NULL)
1016 md->get_ctx_params = OSSL_FUNC_digest_get_ctx_params(fns);
1018 case OSSL_FUNC_DIGEST_GETTABLE_PARAMS:
1019 if (md->gettable_params == NULL)
1020 md->gettable_params = OSSL_FUNC_digest_gettable_params(fns);
1022 case OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS:
1023 if (md->settable_ctx_params == NULL)
1024 md->settable_ctx_params =
1025 OSSL_FUNC_digest_settable_ctx_params(fns);
1027 case OSSL_FUNC_DIGEST_GETTABLE_CTX_PARAMS:
1028 if (md->gettable_ctx_params == NULL)
1029 md->gettable_ctx_params =
1030 OSSL_FUNC_digest_gettable_ctx_params(fns);
1034 if ((fncnt != 0 && fncnt != 5)
1035 || (fncnt == 0 && md->digest == NULL)) {
1037 * In order to be a consistent set of functions we either need the
1038 * whole set of init/update/final etc functions or none of them.
1039 * The "digest" function can standalone. We at least need one way to
1043 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
1048 ossl_provider_up_ref(prov);
1050 if (!evp_md_cache_constants(md)) {
1052 ERR_raise(ERR_LIB_EVP, EVP_R_CACHE_CONSTANTS_FAILED);
1059 static int evp_md_up_ref(void *md)
1061 return EVP_MD_up_ref(md);
1064 static void evp_md_free(void *md)
1069 EVP_MD *EVP_MD_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
1070 const char *properties)
1073 evp_generic_fetch(ctx, OSSL_OP_DIGEST, algorithm, properties,
1074 evp_md_from_algorithm, evp_md_up_ref, evp_md_free);
1079 int EVP_MD_up_ref(EVP_MD *md)
1083 if (md->origin == EVP_ORIG_DYNAMIC)
1084 CRYPTO_UP_REF(&md->refcnt, &ref, md->lock);
1088 void EVP_MD_free(EVP_MD *md)
1092 if (md == NULL || md->origin != EVP_ORIG_DYNAMIC)
1095 CRYPTO_DOWN_REF(&md->refcnt, &i, md->lock);
1098 evp_md_free_int(md);
1101 void EVP_MD_do_all_provided(OSSL_LIB_CTX *libctx,
1102 void (*fn)(EVP_MD *mac, void *arg),
1105 evp_generic_do_all(libctx, OSSL_OP_DIGEST,
1106 (void (*)(void *, void *))fn, arg,
1107 evp_md_from_algorithm, evp_md_up_ref, evp_md_free);