2 * Copyright 1995-2020 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
11 #include <openssl/objects.h>
12 #include <openssl/evp.h>
13 #include <openssl/engine.h>
14 #include <openssl/params.h>
15 #include <openssl/core_names.h>
16 #include "internal/cryptlib.h"
17 #include "crypto/evp.h"
18 #include "internal/provider.h"
19 #include "evp_local.h"
21 /* This call frees resources associated with the context */
22 int EVP_MD_CTX_reset(EVP_MD_CTX *ctx)
28 /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
30 * pctx should be freed by the user of EVP_MD_CTX
31 * if EVP_MD_CTX_FLAG_KEEP_PKEY_CTX is set
33 if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX))
34 EVP_PKEY_CTX_free(ctx->pctx);
37 EVP_MD_free(ctx->fetched_digest);
38 ctx->fetched_digest = NULL;
39 ctx->reqdigest = NULL;
41 if (ctx->provctx != NULL) {
42 if (ctx->digest->freectx != NULL)
43 ctx->digest->freectx(ctx->provctx);
45 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
48 /* TODO(3.0): Remove legacy code below */
51 * Don't assume ctx->md_data was cleaned in EVP_Digest_Final, because
52 * sometimes only copies of the context are ever finalised.
54 if (ctx->digest && ctx->digest->cleanup
55 && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_CLEANED))
56 ctx->digest->cleanup(ctx);
57 if (ctx->digest && ctx->digest->ctx_size && ctx->md_data
58 && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_REUSE)) {
59 OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
62 #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
63 ENGINE_finish(ctx->engine);
66 /* TODO(3.0): End of legacy code */
68 OPENSSL_cleanse(ctx, sizeof(*ctx));
73 EVP_MD_CTX *EVP_MD_CTX_new(void)
75 return OPENSSL_zalloc(sizeof(EVP_MD_CTX));
78 void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
83 EVP_MD_CTX_reset(ctx);
89 int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type)
91 EVP_MD_CTX_reset(ctx);
92 return EVP_DigestInit_ex(ctx, type, NULL);
95 int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
97 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
98 ENGINE *tmpimpl = NULL;
101 EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
103 if (ctx->provctx != NULL) {
104 if (!ossl_assert(ctx->digest != NULL)) {
105 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
108 if (ctx->digest->freectx != NULL)
109 ctx->digest->freectx(ctx->provctx);
114 ctx->reqdigest = type;
116 /* TODO(3.0): Legacy work around code below. Remove this */
117 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
119 * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
120 * this context may already have an ENGINE! Try to avoid releasing the
121 * previous handle, re-querying for an ENGINE, and having a
122 * reinitialisation, when it may all be unnecessary.
124 if (ctx->engine && ctx->digest &&
125 (type == NULL || (type->type == ctx->digest->type)))
130 * Ensure an ENGINE left lying around from last time is cleared (the
131 * previous check attempted to avoid this if the same ENGINE and
132 * EVP_MD could be used).
134 ENGINE_finish(ctx->engine);
138 if (type != NULL && impl == NULL)
139 tmpimpl = ENGINE_get_digest_engine(type->type);
143 * If there are engines involved or EVP_MD_CTX_FLAG_NO_INIT is set then we
144 * should use legacy handling for now.
146 if (ctx->engine != NULL
148 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
151 || (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0) {
152 if (ctx->digest == ctx->fetched_digest)
154 EVP_MD_free(ctx->fetched_digest);
155 ctx->fetched_digest = NULL;
159 if (ctx->digest != NULL && ctx->digest->ctx_size > 0) {
160 OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
164 /* TODO(3.0): Start of non-legacy code below */
166 if (type->prov == NULL) {
168 /* We only do explicit fetches inside the FIPS module */
169 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
172 EVP_MD *provmd = EVP_MD_fetch(NULL, OBJ_nid2sn(type->type), "");
174 if (provmd == NULL) {
175 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
179 EVP_MD_free(ctx->fetched_digest);
180 ctx->fetched_digest = provmd;
184 if (ctx->provctx != NULL && ctx->digest != NULL && ctx->digest != type) {
185 if (ctx->digest->freectx != NULL)
186 ctx->digest->freectx(ctx->provctx);
190 if (ctx->provctx == NULL) {
191 ctx->provctx = ctx->digest->newctx(ossl_provider_ctx(type->prov));
192 if (ctx->provctx == NULL) {
193 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
198 if (ctx->digest->dinit == NULL) {
199 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
203 return ctx->digest->dinit(ctx->provctx);
205 /* TODO(3.0): Remove legacy code below */
208 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
211 if (!ENGINE_init(impl)) {
212 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
216 /* Ask if an ENGINE is reserved for this job */
220 /* There's an ENGINE for this job ... (apparently) */
221 const EVP_MD *d = ENGINE_get_digest(impl, type->type);
224 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
228 /* We'll use the ENGINE's private digest definition */
231 * Store the ENGINE functional reference so we know 'type' came
232 * from an ENGINE and we need to release it when done.
239 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_NO_DIGEST_SET);
245 if (ctx->digest != type) {
246 if (ctx->digest && ctx->digest->ctx_size) {
247 OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
251 if (!(ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) && type->ctx_size) {
252 ctx->update = type->update;
253 ctx->md_data = OPENSSL_zalloc(type->ctx_size);
254 if (ctx->md_data == NULL) {
255 EVPerr(EVP_F_EVP_DIGESTINIT_EX, ERR_R_MALLOC_FAILURE);
260 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
265 * TODO(3.0): Temporarily no support for EVP_DigestSign* inside FIPS module
266 * or when using providers.
268 if (ctx->pctx != NULL
269 && (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx->pctx)
270 || ctx->pctx->op.sig.signature == NULL)) {
272 r = EVP_PKEY_CTX_ctrl(ctx->pctx, -1, EVP_PKEY_OP_TYPE_SIG,
273 EVP_PKEY_CTRL_DIGESTINIT, 0, ctx);
274 if (r <= 0 && (r != -2))
278 if (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT)
280 return ctx->digest->init(ctx);
283 int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t count)
288 if (ctx->pctx != NULL
289 && EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx->pctx)
290 && ctx->pctx->op.sig.sigprovctx != NULL) {
292 * Prior to OpenSSL 3.0 EVP_DigestSignUpdate() and
293 * EVP_DigestVerifyUpdate() were just macros for EVP_DigestUpdate().
294 * Some code calls EVP_DigestUpdate() directly even when initialised
295 * with EVP_DigestSignInit_ex() or EVP_DigestVerifyInit_ex(), so we
296 * detect that and redirect to the correct EVP_Digest*Update() function
298 if (ctx->pctx->operation == EVP_PKEY_OP_SIGNCTX)
299 return EVP_DigestSignUpdate(ctx, data, count);
300 if (ctx->pctx->operation == EVP_PKEY_OP_VERIFYCTX)
301 return EVP_DigestVerifyUpdate(ctx, data, count);
302 EVPerr(EVP_F_EVP_DIGESTUPDATE, EVP_R_UPDATE_ERROR);
306 if (ctx->digest == NULL
307 || ctx->digest->prov == NULL
308 || (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0)
311 if (ctx->digest->dupdate == NULL) {
312 EVPerr(EVP_F_EVP_DIGESTUPDATE, EVP_R_UPDATE_ERROR);
315 return ctx->digest->dupdate(ctx->provctx, data, count);
317 /* TODO(3.0): Remove legacy code below */
319 return ctx->update(ctx, data, count);
322 /* The caller can assume that this removes any secret data from the context */
323 int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size)
326 ret = EVP_DigestFinal_ex(ctx, md, size);
327 EVP_MD_CTX_reset(ctx);
331 /* The caller can assume that this removes any secret data from the context */
332 int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *isize)
336 size_t mdsize = EVP_MD_size(ctx->digest);
338 if (ctx->digest == NULL || ctx->digest->prov == NULL)
341 if (ctx->digest->dfinal == NULL) {
342 EVPerr(EVP_F_EVP_DIGESTFINAL_EX, EVP_R_FINAL_ERROR);
346 ret = ctx->digest->dfinal(ctx->provctx, md, &size, mdsize);
349 if (size <= UINT_MAX) {
352 EVPerr(EVP_F_EVP_DIGESTFINAL_EX, EVP_R_FINAL_ERROR);
359 /* TODO(3.0): Remove legacy code below */
361 OPENSSL_assert(mdsize <= EVP_MAX_MD_SIZE);
362 ret = ctx->digest->final(ctx, md);
365 if (ctx->digest->cleanup) {
366 ctx->digest->cleanup(ctx);
367 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
369 OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
373 int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t size)
376 OSSL_PARAM params[2];
379 if (ctx->digest == NULL || ctx->digest->prov == NULL)
382 if (ctx->digest->dfinal == NULL) {
383 EVPerr(EVP_F_EVP_DIGESTFINALXOF, EVP_R_FINAL_ERROR);
387 params[i++] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN, &size);
388 params[i++] = OSSL_PARAM_construct_end();
390 if (EVP_MD_CTX_set_params(ctx, params) > 0)
391 ret = ctx->digest->dfinal(ctx->provctx, md, &size, size);
392 EVP_MD_CTX_reset(ctx);
396 if (ctx->digest->flags & EVP_MD_FLAG_XOF
398 && ctx->digest->md_ctrl(ctx, EVP_MD_CTRL_XOF_LEN, (int)size, NULL)) {
399 ret = ctx->digest->final(ctx, md);
400 if (ctx->digest->cleanup != NULL) {
401 ctx->digest->cleanup(ctx);
402 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
404 OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
406 EVPerr(EVP_F_EVP_DIGESTFINALXOF, EVP_R_NOT_XOF_OR_INVALID_LENGTH);
412 int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in)
414 EVP_MD_CTX_reset(out);
415 return EVP_MD_CTX_copy_ex(out, in);
418 int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in)
420 unsigned char *tmp_buf;
422 if (in == NULL || in->digest == NULL) {
423 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_INPUT_NOT_INITIALIZED);
427 if (in->digest->prov == NULL
428 || (in->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0)
431 if (in->digest->dupctx == NULL) {
432 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_NOT_ABLE_TO_COPY_CTX);
436 EVP_MD_CTX_reset(out);
437 if (out->fetched_digest != NULL)
438 EVP_MD_free(out->fetched_digest);
440 /* NULL out pointers in case of error */
444 if (in->fetched_digest != NULL)
445 EVP_MD_up_ref(in->fetched_digest);
447 out->provctx = in->digest->dupctx(in->provctx);
448 if (out->provctx == NULL) {
449 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_NOT_ABLE_TO_COPY_CTX);
453 /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
454 EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
456 /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
457 if (in->pctx != NULL) {
458 out->pctx = EVP_PKEY_CTX_dup(in->pctx);
459 if (out->pctx == NULL) {
460 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_NOT_ABLE_TO_COPY_CTX);
461 EVP_MD_CTX_reset(out);
469 /* TODO(3.0): Remove legacy code below */
471 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
472 /* Make sure it's safe to copy a digest context using an ENGINE */
473 if (in->engine && !ENGINE_init(in->engine)) {
474 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_ENGINE_LIB);
479 if (out->digest == in->digest) {
480 tmp_buf = out->md_data;
481 EVP_MD_CTX_set_flags(out, EVP_MD_CTX_FLAG_REUSE);
484 EVP_MD_CTX_reset(out);
485 memcpy(out, in, sizeof(*out));
487 /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
488 EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
490 /* Null these variables, since they are getting fixed up
491 * properly below. Anything else may cause a memleak and/or
492 * double free if any of the memory allocations below fail
497 if (in->md_data && out->digest->ctx_size) {
499 out->md_data = tmp_buf;
501 out->md_data = OPENSSL_malloc(out->digest->ctx_size);
502 if (out->md_data == NULL) {
503 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_MALLOC_FAILURE);
507 memcpy(out->md_data, in->md_data, out->digest->ctx_size);
510 out->update = in->update;
513 /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
515 out->pctx = EVP_PKEY_CTX_dup(in->pctx);
517 EVP_MD_CTX_reset(out);
523 if (out->digest->copy)
524 return out->digest->copy(out, in);
529 int EVP_Digest(const void *data, size_t count,
530 unsigned char *md, unsigned int *size, const EVP_MD *type,
533 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
538 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT);
539 ret = EVP_DigestInit_ex(ctx, type, impl)
540 && EVP_DigestUpdate(ctx, data, count)
541 && EVP_DigestFinal_ex(ctx, md, size);
542 EVP_MD_CTX_free(ctx);
547 int EVP_MD_get_params(const EVP_MD *digest, OSSL_PARAM params[])
549 if (digest != NULL && digest->get_params != NULL)
550 return digest->get_params(params);
554 const OSSL_PARAM *EVP_MD_gettable_params(const EVP_MD *digest)
556 if (digest != NULL && digest->gettable_params != NULL)
557 return digest->gettable_params();
561 int EVP_MD_CTX_set_params(EVP_MD_CTX *ctx, const OSSL_PARAM params[])
563 EVP_PKEY_CTX *pctx = ctx->pctx;
565 if (ctx->digest != NULL && ctx->digest->set_ctx_params != NULL)
566 return ctx->digest->set_ctx_params(ctx->provctx, params);
569 && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
570 || pctx->operation == EVP_PKEY_OP_SIGNCTX)
571 && pctx->op.sig.sigprovctx != NULL
572 && pctx->op.sig.signature->set_ctx_md_params != NULL)
573 return pctx->op.sig.signature->set_ctx_md_params(pctx->op.sig.sigprovctx,
578 const OSSL_PARAM *EVP_MD_settable_ctx_params(const EVP_MD *md)
580 if (md != NULL && md->settable_ctx_params != NULL)
581 return md->settable_ctx_params();
585 const OSSL_PARAM *EVP_MD_CTX_settable_params(EVP_MD_CTX *ctx)
590 && ctx->digest != NULL
591 && ctx->digest->settable_ctx_params != NULL)
592 return ctx->digest->settable_ctx_params();
596 && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
597 || pctx->operation == EVP_PKEY_OP_SIGNCTX)
598 && pctx->op.sig.sigprovctx != NULL
599 && pctx->op.sig.signature->settable_ctx_md_params != NULL)
600 return pctx->op.sig.signature->settable_ctx_md_params(
601 pctx->op.sig.sigprovctx);
606 int EVP_MD_CTX_get_params(EVP_MD_CTX *ctx, OSSL_PARAM params[])
608 EVP_PKEY_CTX *pctx = ctx->pctx;
610 if (ctx->digest != NULL && ctx->digest->get_params != NULL)
611 return ctx->digest->get_ctx_params(ctx->provctx, params);
614 && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
615 || pctx->operation == EVP_PKEY_OP_SIGNCTX)
616 && pctx->op.sig.sigprovctx != NULL
617 && pctx->op.sig.signature->get_ctx_md_params != NULL)
618 return pctx->op.sig.signature->get_ctx_md_params(pctx->op.sig.sigprovctx,
624 const OSSL_PARAM *EVP_MD_gettable_ctx_params(const EVP_MD *md)
626 if (md != NULL && md->gettable_ctx_params != NULL)
627 return md->gettable_ctx_params();
631 const OSSL_PARAM *EVP_MD_CTX_gettable_params(EVP_MD_CTX *ctx)
636 && ctx->digest != NULL
637 && ctx->digest->gettable_ctx_params != NULL)
638 return ctx->digest->gettable_ctx_params();
642 && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
643 || pctx->operation == EVP_PKEY_OP_SIGNCTX)
644 && pctx->op.sig.sigprovctx != NULL
645 && pctx->op.sig.signature->gettable_ctx_md_params != NULL)
646 return pctx->op.sig.signature->gettable_ctx_md_params(
647 pctx->op.sig.sigprovctx);
652 /* TODO(3.0): Remove legacy code below - only used by engines & DigestSign */
653 int EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void *p2)
655 int ret = EVP_CTRL_RET_UNSUPPORTED;
658 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
661 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
665 if (ctx->digest != NULL && ctx->digest->prov == NULL)
669 case EVP_MD_CTRL_XOF_LEN:
671 params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN, &sz);
673 case EVP_MD_CTRL_MICALG:
675 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_DIGEST_PARAM_MICALG,
678 case EVP_CTRL_SSL3_MASTER_SECRET:
679 params[0] = OSSL_PARAM_construct_octet_string(OSSL_DIGEST_PARAM_SSL3_MS,
687 ret = EVP_MD_CTX_set_params(ctx, params);
689 ret = EVP_MD_CTX_get_params(ctx, params);
693 /* TODO(3.0): Remove legacy code below */
695 if (ctx->digest->md_ctrl == NULL) {
696 ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_NOT_IMPLEMENTED);
700 ret = ctx->digest->md_ctrl(ctx, cmd, p1, p2);
707 EVP_MD *evp_md_new(void)
709 EVP_MD *md = OPENSSL_zalloc(sizeof(*md));
712 md->lock = CRYPTO_THREAD_lock_new();
713 if (md->lock == NULL) {
723 * FIPS module note: since internal fetches will be entirely
724 * provider based, we know that none of its code depends on legacy
725 * NIDs or any functionality that use them.
728 /* TODO(3.x) get rid of the need for legacy NIDs */
729 static void set_legacy_nid(const char *name, void *vlegacy_nid)
732 int *legacy_nid = vlegacy_nid;
734 * We use lowest level function to get the associated method, because
735 * higher level functions such as EVP_get_digestbyname() have changed
736 * to look at providers too.
738 const void *legacy_method = OBJ_NAME_get(name, OBJ_NAME_TYPE_MD_METH);
740 if (*legacy_nid == -1) /* We found a clash already */
743 if (legacy_method == NULL)
745 nid = EVP_MD_nid(legacy_method);
746 if (*legacy_nid != NID_undef && *legacy_nid != nid) {
754 static void *evp_md_from_dispatch(int name_id,
755 const OSSL_DISPATCH *fns,
761 /* EVP_MD_fetch() will set the legacy NID if available */
762 if ((md = evp_md_new()) == NULL) {
763 EVPerr(0, ERR_R_MALLOC_FAILURE);
768 /* TODO(3.x) get rid of the need for legacy NIDs */
769 md->type = NID_undef;
770 evp_names_do_all(prov, name_id, set_legacy_nid, &md->type);
771 if (md->type == -1) {
772 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
778 md->name_id = name_id;
780 for (; fns->function_id != 0; fns++) {
781 switch (fns->function_id) {
782 case OSSL_FUNC_DIGEST_NEWCTX:
783 if (md->newctx == NULL) {
784 md->newctx = OSSL_get_OP_digest_newctx(fns);
788 case OSSL_FUNC_DIGEST_INIT:
789 if (md->dinit == NULL) {
790 md->dinit = OSSL_get_OP_digest_init(fns);
794 case OSSL_FUNC_DIGEST_UPDATE:
795 if (md->dupdate == NULL) {
796 md->dupdate = OSSL_get_OP_digest_update(fns);
800 case OSSL_FUNC_DIGEST_FINAL:
801 if (md->dfinal == NULL) {
802 md->dfinal = OSSL_get_OP_digest_final(fns);
806 case OSSL_FUNC_DIGEST_DIGEST:
807 if (md->digest == NULL)
808 md->digest = OSSL_get_OP_digest_digest(fns);
809 /* We don't increment fnct for this as it is stand alone */
811 case OSSL_FUNC_DIGEST_FREECTX:
812 if (md->freectx == NULL) {
813 md->freectx = OSSL_get_OP_digest_freectx(fns);
817 case OSSL_FUNC_DIGEST_DUPCTX:
818 if (md->dupctx == NULL)
819 md->dupctx = OSSL_get_OP_digest_dupctx(fns);
821 case OSSL_FUNC_DIGEST_GET_PARAMS:
822 if (md->get_params == NULL)
823 md->get_params = OSSL_get_OP_digest_get_params(fns);
825 case OSSL_FUNC_DIGEST_SET_CTX_PARAMS:
826 if (md->set_ctx_params == NULL)
827 md->set_ctx_params = OSSL_get_OP_digest_set_ctx_params(fns);
829 case OSSL_FUNC_DIGEST_GET_CTX_PARAMS:
830 if (md->get_ctx_params == NULL)
831 md->get_ctx_params = OSSL_get_OP_digest_get_ctx_params(fns);
833 case OSSL_FUNC_DIGEST_GETTABLE_PARAMS:
834 if (md->gettable_params == NULL)
835 md->gettable_params = OSSL_get_OP_digest_gettable_params(fns);
837 case OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS:
838 if (md->settable_ctx_params == NULL)
839 md->settable_ctx_params =
840 OSSL_get_OP_digest_settable_ctx_params(fns);
842 case OSSL_FUNC_DIGEST_GETTABLE_CTX_PARAMS:
843 if (md->gettable_ctx_params == NULL)
844 md->gettable_ctx_params =
845 OSSL_get_OP_digest_gettable_ctx_params(fns);
849 if ((fncnt != 0 && fncnt != 5)
850 || (fncnt == 0 && md->digest == NULL)) {
852 * In order to be a consistent set of functions we either need the
853 * whole set of init/update/final etc functions or none of them.
854 * The "digest" function can standalone. We at least need one way to
858 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
863 ossl_provider_up_ref(prov);
868 static int evp_md_up_ref(void *md)
870 return EVP_MD_up_ref(md);
873 static void evp_md_free(void *md)
878 EVP_MD *EVP_MD_fetch(OPENSSL_CTX *ctx, const char *algorithm,
879 const char *properties)
882 evp_generic_fetch(ctx, OSSL_OP_DIGEST, algorithm, properties,
883 evp_md_from_dispatch, evp_md_up_ref, evp_md_free);
888 int EVP_MD_up_ref(EVP_MD *md)
892 CRYPTO_UP_REF(&md->refcnt, &ref, md->lock);
896 void EVP_MD_free(EVP_MD *md)
903 CRYPTO_DOWN_REF(&md->refcnt, &i, md->lock);
906 ossl_provider_free(md->prov);
907 CRYPTO_THREAD_lock_free(md->lock);
911 void EVP_MD_do_all_provided(OPENSSL_CTX *libctx,
912 void (*fn)(EVP_MD *mac, void *arg),
915 evp_generic_do_all(libctx, OSSL_OP_DIGEST,
916 (void (*)(void *, void *))fn, arg,
917 evp_md_from_dispatch, evp_md_free);