2 * Copyright 1995-2021 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"
29 void evp_md_ctx_clear_digest(EVP_MD_CTX *ctx, int force)
31 if (ctx->algctx != NULL) {
32 if (ctx->digest != NULL && ctx->digest->freectx != NULL)
33 ctx->digest->freectx(ctx->algctx);
35 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
38 /* Code below to be removed when legacy support is dropped. */
41 * Don't assume ctx->md_data was cleaned in EVP_Digest_Final, because
42 * sometimes only copies of the context are ever finalised.
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);
53 #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
54 ENGINE_finish(ctx->engine);
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;
64 /* This call frees resources associated with the context */
65 int EVP_MD_CTX_reset(EVP_MD_CTX *ctx)
72 * pctx should be freed by the user of EVP_MD_CTX
73 * if EVP_MD_CTX_FLAG_KEEP_PKEY_CTX is set
75 if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX)) {
76 EVP_PKEY_CTX_free(ctx->pctx);
81 evp_md_ctx_clear_digest(ctx, 0);
82 OPENSSL_cleanse(ctx, sizeof(*ctx));
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)
92 EVP_PKEY_CTX *pctx = NULL;
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);
100 if (id != NULL && EVP_PKEY_CTX_set1_id(pctx, id->data, id->length) <= 0)
103 EVP_MD_CTX_set_pkey_ctx(ctx, pctx);
107 EVP_PKEY_CTX_free(pctx);
108 EVP_MD_CTX_free(ctx);
113 EVP_MD_CTX *EVP_MD_CTX_new(void)
115 return OPENSSL_zalloc(sizeof(EVP_MD_CTX));
118 void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
123 EVP_MD_CTX_reset(ctx);
127 static int evp_md_init_internal(EVP_MD_CTX *ctx, const EVP_MD *type,
128 const OSSL_PARAM params[], ENGINE *impl)
130 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
131 ENGINE *tmpimpl = NULL;
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) {
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()
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);
153 EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
155 if (ctx->algctx != NULL) {
156 if (!ossl_assert(ctx->digest != NULL)) {
157 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
160 if (ctx->digest->freectx != NULL)
161 ctx->digest->freectx(ctx->algctx);
166 ctx->reqdigest = type;
168 if (ctx->digest == NULL) {
169 ERR_raise(ERR_LIB_EVP, EVP_R_NO_DIGEST_SET);
175 /* Code below to be removed when legacy support is dropped. */
176 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
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.
183 if (ctx->engine && ctx->digest &&
184 (type == NULL || (type->type == ctx->digest->type)))
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).
193 ENGINE_finish(ctx->engine);
197 if (type != NULL && impl == NULL)
198 tmpimpl = ENGINE_get_digest_engine(type->type);
202 * If there are engines involved or EVP_MD_CTX_FLAG_NO_INIT is set then we
203 * should use legacy handling for now.
205 if (ctx->engine != NULL
207 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
210 || (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0) {
211 if (ctx->digest == ctx->fetched_digest)
213 EVP_MD_free(ctx->fetched_digest);
214 ctx->fetched_digest = NULL;
218 if (ctx->digest != NULL && ctx->digest->ctx_size > 0) {
219 OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
223 /* Start of non-legacy code below */
225 if (type->prov == NULL) {
227 /* We only do explicit fetches inside the FIPS module */
228 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
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)
236 if (provmd == NULL) {
237 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
241 EVP_MD_free(ctx->fetched_digest);
242 ctx->fetched_digest = provmd;
246 if (ctx->algctx != NULL && ctx->digest != NULL && ctx->digest != type) {
247 if (ctx->digest->freectx != NULL)
248 ctx->digest->freectx(ctx->algctx);
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);
256 EVP_MD_free(ctx->fetched_digest);
257 ctx->fetched_digest = (EVP_MD *)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);
268 if (ctx->digest->dinit == NULL) {
269 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
273 return ctx->digest->dinit(ctx->algctx, params);
275 /* Code below to be removed when legacy support is dropped. */
278 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
281 if (!ENGINE_init(impl)) {
282 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
286 /* Ask if an ENGINE is reserved for this job */
290 /* There's an ENGINE for this job ... (apparently) */
291 const EVP_MD *d = ENGINE_get_digest(impl, type->type);
294 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
298 /* We'll use the ENGINE's private digest definition */
301 * Store the ENGINE functional reference so we know 'type' came
302 * from an ENGINE and we need to release it when done.
309 if (ctx->digest != type) {
310 if (ctx->digest && ctx->digest->ctx_size) {
311 OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
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);
324 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
328 if (ctx->pctx != NULL
329 && (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx->pctx)
330 || ctx->pctx->op.sig.signature == NULL)) {
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))
338 if (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT)
340 return ctx->digest->init(ctx);
343 int EVP_DigestInit_ex2(EVP_MD_CTX *ctx, const EVP_MD *type,
344 const OSSL_PARAM params[])
346 return evp_md_init_internal(ctx, type, params, NULL);
349 int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type)
351 EVP_MD_CTX_reset(ctx);
352 return evp_md_init_internal(ctx, type, NULL, NULL);
355 int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
357 return evp_md_init_internal(ctx, type, NULL, impl);
360 int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t count)
365 if (ctx->pctx != NULL
366 && EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx->pctx)
367 && ctx->pctx->op.sig.algctx != NULL) {
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
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);
384 if (ctx->digest == NULL
385 || ctx->digest->prov == NULL
386 || (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0)
389 if (ctx->digest->dupdate == NULL) {
390 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
393 return ctx->digest->dupdate(ctx->algctx, data, count);
395 /* Code below to be removed when legacy support is dropped. */
397 return ctx->update(ctx, data, count);
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)
404 ret = EVP_DigestFinal_ex(ctx, md, size);
405 EVP_MD_CTX_reset(ctx);
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)
416 if (ctx->digest == NULL)
419 sz = EVP_MD_get_size(ctx->digest);
423 if (ctx->digest->prov == NULL)
426 if (ctx->digest->dfinal == NULL) {
427 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
431 ret = ctx->digest->dfinal(ctx->algctx, md, &size, mdsize);
434 if (size <= UINT_MAX) {
437 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
444 /* Code below to be removed when legacy support is dropped. */
446 OPENSSL_assert(mdsize <= EVP_MAX_MD_SIZE);
447 ret = ctx->digest->final(ctx, md);
450 if (ctx->digest->cleanup) {
451 ctx->digest->cleanup(ctx);
452 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
454 OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
458 int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t size)
461 OSSL_PARAM params[2];
464 if (ctx->digest == NULL) {
465 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_NULL_ALGORITHM);
469 if (ctx->digest->prov == NULL)
472 if (ctx->digest->dfinal == NULL) {
473 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
477 params[i++] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN, &size);
478 params[i++] = OSSL_PARAM_construct_end();
480 if (EVP_MD_CTX_set_params(ctx, params) > 0)
481 ret = ctx->digest->dfinal(ctx->algctx, md, &size, size);
486 if (ctx->digest->flags & EVP_MD_FLAG_XOF
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);
494 OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
496 ERR_raise(ERR_LIB_EVP, EVP_R_NOT_XOF_OR_INVALID_LENGTH);
502 int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in)
504 EVP_MD_CTX_reset(out);
505 return EVP_MD_CTX_copy_ex(out, in);
508 int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in)
510 unsigned char *tmp_buf;
512 if (in == NULL || in->digest == NULL) {
513 ERR_raise(ERR_LIB_EVP, EVP_R_INPUT_NOT_INITIALIZED);
517 if (in->digest->prov == NULL
518 || (in->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0)
521 if (in->digest->dupctx == NULL) {
522 ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX);
526 EVP_MD_CTX_reset(out);
527 if (out->fetched_digest != NULL)
528 EVP_MD_free(out->fetched_digest);
530 /* NULL out pointers in case of error */
534 if (in->fetched_digest != NULL)
535 EVP_MD_up_ref(in->fetched_digest);
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);
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);
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);
560 /* Code below to be removed when legacy support is dropped. */
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);
570 if (out->digest == in->digest) {
571 tmp_buf = out->md_data;
572 EVP_MD_CTX_set_flags(out, EVP_MD_CTX_FLAG_REUSE);
575 EVP_MD_CTX_reset(out);
576 memcpy(out, in, sizeof(*out));
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);
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
588 if (in->md_data && out->digest->ctx_size) {
590 out->md_data = tmp_buf;
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);
598 memcpy(out->md_data, in->md_data, out->digest->ctx_size);
601 out->update = in->update;
605 out->pctx = EVP_PKEY_CTX_dup(in->pctx);
607 EVP_MD_CTX_reset(out);
613 if (out->digest->copy)
614 return out->digest->copy(out, in);
619 int EVP_Digest(const void *data, size_t count,
620 unsigned char *md, unsigned int *size, const EVP_MD *type,
623 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
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);
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)
641 EVP_MD *digest = EVP_MD_fetch(libctx, name, propq);
642 unsigned int temp = 0;
645 if (digest != NULL) {
646 ret = EVP_Digest(data, datalen, md, &temp, digest, NULL);
654 int EVP_MD_get_params(const EVP_MD *digest, OSSL_PARAM params[])
656 if (digest != NULL && digest->get_params != NULL)
657 return digest->get_params(params);
661 const OSSL_PARAM *EVP_MD_gettable_params(const EVP_MD *digest)
663 if (digest != NULL && digest->gettable_params != NULL)
664 return digest->gettable_params(
665 ossl_provider_ctx(EVP_MD_get0_provider(digest)));
669 int EVP_MD_CTX_set_params(EVP_MD_CTX *ctx, const OSSL_PARAM params[])
671 EVP_PKEY_CTX *pctx = ctx->pctx;
673 /* If we have a pctx then we should try that first */
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,
682 if (ctx->digest != NULL && ctx->digest->set_ctx_params != NULL)
683 return ctx->digest->set_ctx_params(ctx->algctx, params);
688 const OSSL_PARAM *EVP_MD_settable_ctx_params(const EVP_MD *md)
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);
699 const OSSL_PARAM *EVP_MD_CTX_settable_params(EVP_MD_CTX *ctx)
707 /* If we have a pctx then we should try that first */
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);
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);
725 int EVP_MD_CTX_get_params(EVP_MD_CTX *ctx, OSSL_PARAM params[])
727 EVP_PKEY_CTX *pctx = ctx->pctx;
729 /* If we have a pctx then we should try that first */
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,
738 if (ctx->digest != NULL && ctx->digest->get_params != NULL)
739 return ctx->digest->get_ctx_params(ctx->algctx, params);
744 const OSSL_PARAM *EVP_MD_gettable_ctx_params(const EVP_MD *md)
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);
755 const OSSL_PARAM *EVP_MD_CTX_gettable_params(EVP_MD_CTX *ctx)
763 /* If we have a pctx then we should try that first */
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);
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);
780 int EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void *p2)
782 int ret = EVP_CTRL_RET_UNSUPPORTED;
785 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
788 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
792 if (ctx->digest != NULL && ctx->digest->prov == NULL)
796 case EVP_MD_CTRL_XOF_LEN:
798 params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN, &sz);
800 case EVP_MD_CTRL_MICALG:
802 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_DIGEST_PARAM_MICALG,
805 case EVP_CTRL_SSL3_MASTER_SECRET:
806 params[0] = OSSL_PARAM_construct_octet_string(OSSL_DIGEST_PARAM_SSL3_MS,
814 ret = EVP_MD_CTX_set_params(ctx, params);
816 ret = EVP_MD_CTX_get_params(ctx, params);
820 /* Code below to be removed when legacy support is dropped. */
822 if (ctx->digest->md_ctrl == NULL) {
823 ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_NOT_IMPLEMENTED);
827 ret = ctx->digest->md_ctrl(ctx, cmd, p1, p2);
834 EVP_MD *evp_md_new(void)
836 EVP_MD *md = OPENSSL_zalloc(sizeof(*md));
839 md->lock = CRYPTO_THREAD_lock_new();
840 if (md->lock == NULL) {
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.
855 static void set_legacy_nid(const char *name, void *vlegacy_nid)
858 int *legacy_nid = vlegacy_nid;
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.
864 const void *legacy_method = OBJ_NAME_get(name, OBJ_NAME_TYPE_MD_METH);
866 if (*legacy_nid == -1) /* We found a clash already */
869 if (legacy_method == NULL)
871 nid = EVP_MD_nid(legacy_method);
872 if (*legacy_nid != NID_undef && *legacy_nid != nid) {
880 static int evp_md_cache_constants(EVP_MD *md)
882 int ok, xof = 0, algid_absent = 0;
885 OSSL_PARAM params[5];
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,
892 params[4] = OSSL_PARAM_construct_end();
893 ok = evp_do_md_getparams(md, params) > 0;
894 if (mdsize > INT_MAX || blksz > INT_MAX)
897 md->block_size = (int)blksz;
898 md->md_size = (int)mdsize;
900 md->flags |= EVP_MD_FLAG_XOF;
902 md->flags |= EVP_MD_FLAG_DIGALGID_ABSENT;
907 static void *evp_md_from_algorithm(int name_id,
908 const OSSL_ALGORITHM *algodef,
911 const OSSL_DISPATCH *fns = algodef->implementation;
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);
922 md->type = NID_undef;
923 if (!evp_names_do_all(prov, name_id, set_legacy_nid, &md->type)
925 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
931 md->name_id = name_id;
932 if ((md->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL) {
936 md->description = algodef->algorithm_description;
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);
946 case OSSL_FUNC_DIGEST_INIT:
947 if (md->dinit == NULL) {
948 md->dinit = OSSL_FUNC_digest_init(fns);
952 case OSSL_FUNC_DIGEST_UPDATE:
953 if (md->dupdate == NULL) {
954 md->dupdate = OSSL_FUNC_digest_update(fns);
958 case OSSL_FUNC_DIGEST_FINAL:
959 if (md->dfinal == NULL) {
960 md->dfinal = OSSL_FUNC_digest_final(fns);
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 */
969 case OSSL_FUNC_DIGEST_FREECTX:
970 if (md->freectx == NULL) {
971 md->freectx = OSSL_FUNC_digest_freectx(fns);
975 case OSSL_FUNC_DIGEST_DUPCTX:
976 if (md->dupctx == NULL)
977 md->dupctx = OSSL_FUNC_digest_dupctx(fns);
979 case OSSL_FUNC_DIGEST_GET_PARAMS:
980 if (md->get_params == NULL)
981 md->get_params = OSSL_FUNC_digest_get_params(fns);
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);
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);
991 case OSSL_FUNC_DIGEST_GETTABLE_PARAMS:
992 if (md->gettable_params == NULL)
993 md->gettable_params = OSSL_FUNC_digest_gettable_params(fns);
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);
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);
1007 if ((fncnt != 0 && fncnt != 5)
1008 || (fncnt == 0 && md->digest == NULL)) {
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
1016 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
1021 ossl_provider_up_ref(prov);
1023 if (!evp_md_cache_constants(md)) {
1025 ERR_raise(ERR_LIB_EVP, EVP_R_CACHE_CONSTANTS_FAILED);
1032 static int evp_md_up_ref(void *md)
1034 return EVP_MD_up_ref(md);
1037 static void evp_md_free(void *md)
1042 EVP_MD *EVP_MD_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
1043 const char *properties)
1046 evp_generic_fetch(ctx, OSSL_OP_DIGEST, algorithm, properties,
1047 evp_md_from_algorithm, evp_md_up_ref, evp_md_free);
1052 int EVP_MD_up_ref(EVP_MD *md)
1056 if (md->origin == EVP_ORIG_DYNAMIC)
1057 CRYPTO_UP_REF(&md->refcnt, &ref, md->lock);
1061 void EVP_MD_free(EVP_MD *md)
1065 if (md == NULL || md->origin != EVP_ORIG_DYNAMIC)
1068 CRYPTO_DOWN_REF(&md->refcnt, &i, md->lock);
1071 evp_md_free_int(md);
1074 void EVP_MD_do_all_provided(OSSL_LIB_CTX *libctx,
1075 void (*fn)(EVP_MD *mac, void *arg),
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);