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 EVP_MD *provmd = EVP_MD_fetch(NULL, OBJ_nid2sn(type->type), "");
233 if (provmd == NULL) {
234 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
238 EVP_MD_free(ctx->fetched_digest);
239 ctx->fetched_digest = provmd;
243 if (ctx->algctx != NULL && ctx->digest != NULL && ctx->digest != type) {
244 if (ctx->digest->freectx != NULL)
245 ctx->digest->freectx(ctx->algctx);
248 if (type->prov != NULL && ctx->fetched_digest != type) {
249 if (!EVP_MD_up_ref((EVP_MD *)type)) {
250 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
253 EVP_MD_free(ctx->fetched_digest);
254 ctx->fetched_digest = (EVP_MD *)type;
257 if (ctx->algctx == NULL) {
258 ctx->algctx = ctx->digest->newctx(ossl_provider_ctx(type->prov));
259 if (ctx->algctx == NULL) {
260 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
265 if (ctx->digest->dinit == NULL) {
266 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
270 return ctx->digest->dinit(ctx->algctx, params);
272 /* Code below to be removed when legacy support is dropped. */
275 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
278 if (!ENGINE_init(impl)) {
279 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
283 /* Ask if an ENGINE is reserved for this job */
287 /* There's an ENGINE for this job ... (apparently) */
288 const EVP_MD *d = ENGINE_get_digest(impl, type->type);
291 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
295 /* We'll use the ENGINE's private digest definition */
298 * Store the ENGINE functional reference so we know 'type' came
299 * from an ENGINE and we need to release it when done.
306 if (ctx->digest != type) {
307 if (ctx->digest && ctx->digest->ctx_size) {
308 OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
312 if (!(ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) && type->ctx_size) {
313 ctx->update = type->update;
314 ctx->md_data = OPENSSL_zalloc(type->ctx_size);
315 if (ctx->md_data == NULL) {
316 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
321 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
325 if (ctx->pctx != NULL
326 && (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx->pctx)
327 || ctx->pctx->op.sig.signature == NULL)) {
329 r = EVP_PKEY_CTX_ctrl(ctx->pctx, -1, EVP_PKEY_OP_TYPE_SIG,
330 EVP_PKEY_CTRL_DIGESTINIT, 0, ctx);
331 if (r <= 0 && (r != -2))
335 if (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT)
337 return ctx->digest->init(ctx);
340 int EVP_DigestInit_ex2(EVP_MD_CTX *ctx, const EVP_MD *type,
341 const OSSL_PARAM params[])
343 return evp_md_init_internal(ctx, type, params, NULL);
346 int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type)
348 EVP_MD_CTX_reset(ctx);
349 return evp_md_init_internal(ctx, type, NULL, NULL);
352 int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
354 return evp_md_init_internal(ctx, type, NULL, impl);
357 int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t count)
362 if (ctx->pctx != NULL
363 && EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx->pctx)
364 && ctx->pctx->op.sig.algctx != NULL) {
366 * Prior to OpenSSL 3.0 EVP_DigestSignUpdate() and
367 * EVP_DigestVerifyUpdate() were just macros for EVP_DigestUpdate().
368 * Some code calls EVP_DigestUpdate() directly even when initialised
369 * with EVP_DigestSignInit_ex() or
370 * EVP_DigestVerifyInit_ex(), so we detect that and redirect to
371 * the correct EVP_Digest*Update() function
373 if (ctx->pctx->operation == EVP_PKEY_OP_SIGNCTX)
374 return EVP_DigestSignUpdate(ctx, data, count);
375 if (ctx->pctx->operation == EVP_PKEY_OP_VERIFYCTX)
376 return EVP_DigestVerifyUpdate(ctx, data, count);
377 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
381 if (ctx->digest == NULL
382 || ctx->digest->prov == NULL
383 || (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0)
386 if (ctx->digest->dupdate == NULL) {
387 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
390 return ctx->digest->dupdate(ctx->algctx, data, count);
392 /* Code below to be removed when legacy support is dropped. */
394 return ctx->update(ctx, data, count);
397 /* The caller can assume that this removes any secret data from the context */
398 int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size)
401 ret = EVP_DigestFinal_ex(ctx, md, size);
402 EVP_MD_CTX_reset(ctx);
406 /* The caller can assume that this removes any secret data from the context */
407 int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *isize)
413 if (ctx->digest == NULL)
416 sz = EVP_MD_get_size(ctx->digest);
420 if (ctx->digest->prov == NULL)
423 if (ctx->digest->dfinal == NULL) {
424 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
428 ret = ctx->digest->dfinal(ctx->algctx, md, &size, mdsize);
431 if (size <= UINT_MAX) {
434 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
441 /* Code below to be removed when legacy support is dropped. */
443 OPENSSL_assert(mdsize <= EVP_MAX_MD_SIZE);
444 ret = ctx->digest->final(ctx, md);
447 if (ctx->digest->cleanup) {
448 ctx->digest->cleanup(ctx);
449 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
451 OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
455 int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t size)
458 OSSL_PARAM params[2];
461 if (ctx->digest == NULL) {
462 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_NULL_ALGORITHM);
466 if (ctx->digest->prov == NULL)
469 if (ctx->digest->dfinal == NULL) {
470 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
474 params[i++] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN, &size);
475 params[i++] = OSSL_PARAM_construct_end();
477 if (EVP_MD_CTX_set_params(ctx, params) > 0)
478 ret = ctx->digest->dfinal(ctx->algctx, md, &size, size);
483 if (ctx->digest->flags & EVP_MD_FLAG_XOF
485 && ctx->digest->md_ctrl(ctx, EVP_MD_CTRL_XOF_LEN, (int)size, NULL)) {
486 ret = ctx->digest->final(ctx, md);
487 if (ctx->digest->cleanup != NULL) {
488 ctx->digest->cleanup(ctx);
489 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
491 OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
493 ERR_raise(ERR_LIB_EVP, EVP_R_NOT_XOF_OR_INVALID_LENGTH);
499 int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in)
501 EVP_MD_CTX_reset(out);
502 return EVP_MD_CTX_copy_ex(out, in);
505 int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in)
507 unsigned char *tmp_buf;
509 if (in == NULL || in->digest == NULL) {
510 ERR_raise(ERR_LIB_EVP, EVP_R_INPUT_NOT_INITIALIZED);
514 if (in->digest->prov == NULL
515 || (in->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0)
518 if (in->digest->dupctx == NULL) {
519 ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX);
523 EVP_MD_CTX_reset(out);
524 if (out->fetched_digest != NULL)
525 EVP_MD_free(out->fetched_digest);
527 /* NULL out pointers in case of error */
531 if (in->fetched_digest != NULL)
532 EVP_MD_up_ref(in->fetched_digest);
534 if (in->algctx != NULL) {
535 out->algctx = in->digest->dupctx(in->algctx);
536 if (out->algctx == NULL) {
537 ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX);
542 /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
543 EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
545 if (in->pctx != NULL) {
546 out->pctx = EVP_PKEY_CTX_dup(in->pctx);
547 if (out->pctx == NULL) {
548 ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX);
549 EVP_MD_CTX_reset(out);
557 /* Code below to be removed when legacy support is dropped. */
559 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
560 /* Make sure it's safe to copy a digest context using an ENGINE */
561 if (in->engine && !ENGINE_init(in->engine)) {
562 ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB);
567 if (out->digest == in->digest) {
568 tmp_buf = out->md_data;
569 EVP_MD_CTX_set_flags(out, EVP_MD_CTX_FLAG_REUSE);
572 EVP_MD_CTX_reset(out);
573 memcpy(out, in, sizeof(*out));
575 /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
576 EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
578 /* Null these variables, since they are getting fixed up
579 * properly below. Anything else may cause a memleak and/or
580 * double free if any of the memory allocations below fail
585 if (in->md_data && out->digest->ctx_size) {
587 out->md_data = tmp_buf;
589 out->md_data = OPENSSL_malloc(out->digest->ctx_size);
590 if (out->md_data == NULL) {
591 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
595 memcpy(out->md_data, in->md_data, out->digest->ctx_size);
598 out->update = in->update;
602 out->pctx = EVP_PKEY_CTX_dup(in->pctx);
604 EVP_MD_CTX_reset(out);
610 if (out->digest->copy)
611 return out->digest->copy(out, in);
616 int EVP_Digest(const void *data, size_t count,
617 unsigned char *md, unsigned int *size, const EVP_MD *type,
620 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
625 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT);
626 ret = EVP_DigestInit_ex(ctx, type, impl)
627 && EVP_DigestUpdate(ctx, data, count)
628 && EVP_DigestFinal_ex(ctx, md, size);
629 EVP_MD_CTX_free(ctx);
634 int EVP_Q_digest(OSSL_LIB_CTX *libctx, const char *name, const char *propq,
635 const void *data, size_t datalen,
636 unsigned char *md, size_t *mdlen)
638 EVP_MD *digest = EVP_MD_fetch(libctx, name, propq);
639 unsigned int temp = 0;
642 if (digest != NULL) {
643 ret = EVP_Digest(data, datalen, md, &temp, digest, NULL);
651 int EVP_MD_get_params(const EVP_MD *digest, OSSL_PARAM params[])
653 if (digest != NULL && digest->get_params != NULL)
654 return digest->get_params(params);
658 const OSSL_PARAM *EVP_MD_gettable_params(const EVP_MD *digest)
660 if (digest != NULL && digest->gettable_params != NULL)
661 return digest->gettable_params(
662 ossl_provider_ctx(EVP_MD_get0_provider(digest)));
666 int EVP_MD_CTX_set_params(EVP_MD_CTX *ctx, const OSSL_PARAM params[])
668 EVP_PKEY_CTX *pctx = ctx->pctx;
670 /* If we have a pctx then we should try that first */
672 && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
673 || pctx->operation == EVP_PKEY_OP_SIGNCTX)
674 && pctx->op.sig.algctx != NULL
675 && pctx->op.sig.signature->set_ctx_md_params != NULL)
676 return pctx->op.sig.signature->set_ctx_md_params(pctx->op.sig.algctx,
679 if (ctx->digest != NULL && ctx->digest->set_ctx_params != NULL)
680 return ctx->digest->set_ctx_params(ctx->algctx, params);
685 const OSSL_PARAM *EVP_MD_settable_ctx_params(const EVP_MD *md)
689 if (md != NULL && md->settable_ctx_params != NULL) {
690 provctx = ossl_provider_ctx(EVP_MD_get0_provider(md));
691 return md->settable_ctx_params(NULL, provctx);
696 const OSSL_PARAM *EVP_MD_CTX_settable_params(EVP_MD_CTX *ctx)
704 /* If we have a pctx then we should try that first */
707 && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
708 || pctx->operation == EVP_PKEY_OP_SIGNCTX)
709 && pctx->op.sig.algctx != NULL
710 && pctx->op.sig.signature->settable_ctx_md_params != NULL)
711 return pctx->op.sig.signature->settable_ctx_md_params(
712 pctx->op.sig.algctx);
714 if (ctx->digest != NULL && ctx->digest->settable_ctx_params != NULL) {
715 alg = ossl_provider_ctx(EVP_MD_get0_provider(ctx->digest));
716 return ctx->digest->settable_ctx_params(ctx->algctx, alg);
722 int EVP_MD_CTX_get_params(EVP_MD_CTX *ctx, OSSL_PARAM params[])
724 EVP_PKEY_CTX *pctx = ctx->pctx;
726 /* If we have a pctx then we should try that first */
728 && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
729 || pctx->operation == EVP_PKEY_OP_SIGNCTX)
730 && pctx->op.sig.algctx != NULL
731 && pctx->op.sig.signature->get_ctx_md_params != NULL)
732 return pctx->op.sig.signature->get_ctx_md_params(pctx->op.sig.algctx,
735 if (ctx->digest != NULL && ctx->digest->get_params != NULL)
736 return ctx->digest->get_ctx_params(ctx->algctx, params);
741 const OSSL_PARAM *EVP_MD_gettable_ctx_params(const EVP_MD *md)
745 if (md != NULL && md->gettable_ctx_params != NULL) {
746 provctx = ossl_provider_ctx(EVP_MD_get0_provider(md));
747 return md->gettable_ctx_params(NULL, provctx);
752 const OSSL_PARAM *EVP_MD_CTX_gettable_params(EVP_MD_CTX *ctx)
760 /* If we have a pctx then we should try that first */
763 && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
764 || pctx->operation == EVP_PKEY_OP_SIGNCTX)
765 && pctx->op.sig.algctx != NULL
766 && pctx->op.sig.signature->gettable_ctx_md_params != NULL)
767 return pctx->op.sig.signature->gettable_ctx_md_params(
768 pctx->op.sig.algctx);
770 if (ctx->digest != NULL && ctx->digest->gettable_ctx_params != NULL) {
771 provctx = ossl_provider_ctx(EVP_MD_get0_provider(ctx->digest));
772 return ctx->digest->gettable_ctx_params(ctx->algctx, provctx);
777 int EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void *p2)
779 int ret = EVP_CTRL_RET_UNSUPPORTED;
782 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
785 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
789 if (ctx->digest != NULL && ctx->digest->prov == NULL)
793 case EVP_MD_CTRL_XOF_LEN:
795 params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN, &sz);
797 case EVP_MD_CTRL_MICALG:
799 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_DIGEST_PARAM_MICALG,
802 case EVP_CTRL_SSL3_MASTER_SECRET:
803 params[0] = OSSL_PARAM_construct_octet_string(OSSL_DIGEST_PARAM_SSL3_MS,
811 ret = EVP_MD_CTX_set_params(ctx, params);
813 ret = EVP_MD_CTX_get_params(ctx, params);
817 /* Code below to be removed when legacy support is dropped. */
819 if (ctx->digest->md_ctrl == NULL) {
820 ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_NOT_IMPLEMENTED);
824 ret = ctx->digest->md_ctrl(ctx, cmd, p1, p2);
831 EVP_MD *evp_md_new(void)
833 EVP_MD *md = OPENSSL_zalloc(sizeof(*md));
836 md->lock = CRYPTO_THREAD_lock_new();
837 if (md->lock == NULL) {
847 * FIPS module note: since internal fetches will be entirely
848 * provider based, we know that none of its code depends on legacy
849 * NIDs or any functionality that use them.
852 static void set_legacy_nid(const char *name, void *vlegacy_nid)
855 int *legacy_nid = vlegacy_nid;
857 * We use lowest level function to get the associated method, because
858 * higher level functions such as EVP_get_digestbyname() have changed
859 * to look at providers too.
861 const void *legacy_method = OBJ_NAME_get(name, OBJ_NAME_TYPE_MD_METH);
863 if (*legacy_nid == -1) /* We found a clash already */
866 if (legacy_method == NULL)
868 nid = EVP_MD_nid(legacy_method);
869 if (*legacy_nid != NID_undef && *legacy_nid != nid) {
877 static int evp_md_cache_constants(EVP_MD *md)
879 int ok, xof = 0, algid_absent = 0;
882 OSSL_PARAM params[5];
884 params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_BLOCK_SIZE, &blksz);
885 params[1] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_SIZE, &mdsize);
886 params[2] = OSSL_PARAM_construct_int(OSSL_DIGEST_PARAM_XOF, &xof);
887 params[3] = OSSL_PARAM_construct_int(OSSL_DIGEST_PARAM_ALGID_ABSENT,
889 params[4] = OSSL_PARAM_construct_end();
890 ok = evp_do_md_getparams(md, params) > 0;
891 if (mdsize > INT_MAX || blksz > INT_MAX)
894 md->block_size = (int)blksz;
895 md->md_size = (int)mdsize;
897 md->flags |= EVP_MD_FLAG_XOF;
899 md->flags |= EVP_MD_FLAG_DIGALGID_ABSENT;
904 static void *evp_md_from_algorithm(int name_id,
905 const OSSL_ALGORITHM *algodef,
908 const OSSL_DISPATCH *fns = algodef->implementation;
912 /* EVP_MD_fetch() will set the legacy NID if available */
913 if ((md = evp_md_new()) == NULL) {
914 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
919 md->type = NID_undef;
920 if (!evp_names_do_all(prov, name_id, set_legacy_nid, &md->type)
922 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
928 md->name_id = name_id;
929 if ((md->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL) {
933 md->description = algodef->algorithm_description;
935 for (; fns->function_id != 0; fns++) {
936 switch (fns->function_id) {
937 case OSSL_FUNC_DIGEST_NEWCTX:
938 if (md->newctx == NULL) {
939 md->newctx = OSSL_FUNC_digest_newctx(fns);
943 case OSSL_FUNC_DIGEST_INIT:
944 if (md->dinit == NULL) {
945 md->dinit = OSSL_FUNC_digest_init(fns);
949 case OSSL_FUNC_DIGEST_UPDATE:
950 if (md->dupdate == NULL) {
951 md->dupdate = OSSL_FUNC_digest_update(fns);
955 case OSSL_FUNC_DIGEST_FINAL:
956 if (md->dfinal == NULL) {
957 md->dfinal = OSSL_FUNC_digest_final(fns);
961 case OSSL_FUNC_DIGEST_DIGEST:
962 if (md->digest == NULL)
963 md->digest = OSSL_FUNC_digest_digest(fns);
964 /* We don't increment fnct for this as it is stand alone */
966 case OSSL_FUNC_DIGEST_FREECTX:
967 if (md->freectx == NULL) {
968 md->freectx = OSSL_FUNC_digest_freectx(fns);
972 case OSSL_FUNC_DIGEST_DUPCTX:
973 if (md->dupctx == NULL)
974 md->dupctx = OSSL_FUNC_digest_dupctx(fns);
976 case OSSL_FUNC_DIGEST_GET_PARAMS:
977 if (md->get_params == NULL)
978 md->get_params = OSSL_FUNC_digest_get_params(fns);
980 case OSSL_FUNC_DIGEST_SET_CTX_PARAMS:
981 if (md->set_ctx_params == NULL)
982 md->set_ctx_params = OSSL_FUNC_digest_set_ctx_params(fns);
984 case OSSL_FUNC_DIGEST_GET_CTX_PARAMS:
985 if (md->get_ctx_params == NULL)
986 md->get_ctx_params = OSSL_FUNC_digest_get_ctx_params(fns);
988 case OSSL_FUNC_DIGEST_GETTABLE_PARAMS:
989 if (md->gettable_params == NULL)
990 md->gettable_params = OSSL_FUNC_digest_gettable_params(fns);
992 case OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS:
993 if (md->settable_ctx_params == NULL)
994 md->settable_ctx_params =
995 OSSL_FUNC_digest_settable_ctx_params(fns);
997 case OSSL_FUNC_DIGEST_GETTABLE_CTX_PARAMS:
998 if (md->gettable_ctx_params == NULL)
999 md->gettable_ctx_params =
1000 OSSL_FUNC_digest_gettable_ctx_params(fns);
1004 if ((fncnt != 0 && fncnt != 5)
1005 || (fncnt == 0 && md->digest == NULL)) {
1007 * In order to be a consistent set of functions we either need the
1008 * whole set of init/update/final etc functions or none of them.
1009 * The "digest" function can standalone. We at least need one way to
1013 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
1018 ossl_provider_up_ref(prov);
1020 if (!evp_md_cache_constants(md)) {
1022 ERR_raise(ERR_LIB_EVP, EVP_R_CACHE_CONSTANTS_FAILED);
1029 static int evp_md_up_ref(void *md)
1031 return EVP_MD_up_ref(md);
1034 static void evp_md_free(void *md)
1039 EVP_MD *EVP_MD_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
1040 const char *properties)
1043 evp_generic_fetch(ctx, OSSL_OP_DIGEST, algorithm, properties,
1044 evp_md_from_algorithm, evp_md_up_ref, evp_md_free);
1049 int EVP_MD_up_ref(EVP_MD *md)
1053 if (md->origin == EVP_ORIG_DYNAMIC)
1054 CRYPTO_UP_REF(&md->refcnt, &ref, md->lock);
1058 void EVP_MD_free(EVP_MD *md)
1062 if (md == NULL || md->origin != EVP_ORIG_DYNAMIC)
1065 CRYPTO_DOWN_REF(&md->refcnt, &i, md->lock);
1068 evp_md_free_int(md);
1071 void EVP_MD_do_all_provided(OSSL_LIB_CTX *libctx,
1072 void (*fn)(EVP_MD *mac, void *arg),
1075 evp_generic_do_all(libctx, OSSL_OP_DIGEST,
1076 (void (*)(void *, void *))fn, arg,
1077 evp_md_from_algorithm, evp_md_up_ref, evp_md_free);