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
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>
17 #include <openssl/engine.h>
18 #include <openssl/params.h>
19 #include <openssl/core_names.h>
20 #include "internal/cryptlib.h"
21 #include "crypto/evp.h"
22 #include "internal/provider.h"
23 #include "evp_local.h"
26 void evp_md_ctx_clear_digest(EVP_MD_CTX *ctx, int force)
28 EVP_MD_free(ctx->fetched_digest);
29 ctx->fetched_digest = NULL;
30 ctx->reqdigest = NULL;
32 if (ctx->provctx != NULL) {
33 if (ctx->digest->freectx != NULL)
34 ctx->digest->freectx(ctx->provctx);
36 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
39 /* TODO(3.0): Remove legacy code below */
42 * Don't assume ctx->md_data was cleaned in EVP_Digest_Final, because
43 * sometimes only copies of the context are ever finalised.
45 if (ctx->digest && ctx->digest->cleanup
46 && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_CLEANED))
47 ctx->digest->cleanup(ctx);
48 if (ctx->digest && ctx->digest->ctx_size && ctx->md_data
49 && (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_REUSE) || force))
50 OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
54 #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
55 ENGINE_finish(ctx->engine);
60 /* This call frees resources associated with the context */
61 int EVP_MD_CTX_reset(EVP_MD_CTX *ctx)
67 /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
69 * pctx should be freed by the user of EVP_MD_CTX
70 * if EVP_MD_CTX_FLAG_KEEP_PKEY_CTX is set
72 if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX)) {
73 EVP_PKEY_CTX_free(ctx->pctx);
78 evp_md_ctx_clear_digest(ctx, 0);
79 OPENSSL_cleanse(ctx, sizeof(*ctx));
85 EVP_MD_CTX *evp_md_ctx_new_ex(EVP_PKEY *pkey, const ASN1_OCTET_STRING *id,
86 OSSL_LIB_CTX *libctx, const char *propq)
89 EVP_PKEY_CTX *pctx = NULL;
91 if ((ctx = EVP_MD_CTX_new()) == NULL
92 || (pctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq)) == NULL) {
93 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
97 if (id != NULL && EVP_PKEY_CTX_set1_id(pctx, id->data, id->length) <= 0)
100 EVP_MD_CTX_set_pkey_ctx(ctx, pctx);
104 EVP_PKEY_CTX_free(pctx);
105 EVP_MD_CTX_free(ctx);
110 EVP_MD_CTX *EVP_MD_CTX_new(void)
112 return OPENSSL_zalloc(sizeof(EVP_MD_CTX));
115 void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
120 EVP_MD_CTX_reset(ctx);
126 int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type)
128 EVP_MD_CTX_reset(ctx);
129 return EVP_DigestInit_ex(ctx, type, NULL);
132 int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
134 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
135 ENGINE *tmpimpl = NULL;
138 #if !defined(FIPS_MODULE)
139 if (ctx->pctx != NULL
140 && EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx->pctx)
141 && ctx->pctx->op.sig.sigprovctx != NULL) {
143 * Prior to OpenSSL 3.0 calling EVP_DigestInit_ex() on an mdctx
144 * previously initialised with EVP_DigestSignInit() would retain
145 * information about the key, and re-initialise for another sign
146 * operation. So in that case we redirect to EVP_DigestSignInit()
148 if (ctx->pctx->operation == EVP_PKEY_OP_SIGNCTX)
149 return EVP_DigestSignInit(ctx, NULL, type, impl, NULL);
150 if (ctx->pctx->operation == EVP_PKEY_OP_VERIFYCTX)
151 return EVP_DigestVerifyInit(ctx, NULL, type, impl, NULL);
152 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
157 EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
159 if (ctx->provctx != NULL) {
160 if (!ossl_assert(ctx->digest != NULL)) {
161 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
164 if (ctx->digest->freectx != NULL)
165 ctx->digest->freectx(ctx->provctx);
170 ctx->reqdigest = type;
172 if (ctx->digest == NULL) {
173 ERR_raise(ERR_LIB_EVP, EVP_R_NO_DIGEST_SET);
179 /* TODO(3.0): Legacy work around code below. Remove this */
180 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
182 * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
183 * this context may already have an ENGINE! Try to avoid releasing the
184 * previous handle, re-querying for an ENGINE, and having a
185 * reinitialisation, when it may all be unnecessary.
187 if (ctx->engine && ctx->digest &&
188 (type == NULL || (type->type == ctx->digest->type)))
193 * Ensure an ENGINE left lying around from last time is cleared (the
194 * previous check attempted to avoid this if the same ENGINE and
195 * EVP_MD could be used).
197 ENGINE_finish(ctx->engine);
201 if (type != NULL && impl == NULL)
202 tmpimpl = ENGINE_get_digest_engine(type->type);
206 * If there are engines involved or EVP_MD_CTX_FLAG_NO_INIT is set then we
207 * should use legacy handling for now.
209 if (ctx->engine != NULL
211 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
214 || (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0) {
215 if (ctx->digest == ctx->fetched_digest)
217 EVP_MD_free(ctx->fetched_digest);
218 ctx->fetched_digest = NULL;
222 if (ctx->digest != NULL && ctx->digest->ctx_size > 0) {
223 OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
227 /* TODO(3.0): Start of non-legacy code below */
229 if (type->prov == NULL) {
231 /* We only do explicit fetches inside the FIPS module */
232 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
235 EVP_MD *provmd = EVP_MD_fetch(NULL, OBJ_nid2sn(type->type), "");
240 EVP_MD_free(ctx->fetched_digest);
241 ctx->fetched_digest = provmd;
245 if (ctx->provctx != NULL && ctx->digest != NULL && ctx->digest != type) {
246 if (ctx->digest->freectx != NULL)
247 ctx->digest->freectx(ctx->provctx);
251 if (ctx->provctx == NULL) {
252 ctx->provctx = ctx->digest->newctx(ossl_provider_ctx(type->prov));
253 if (ctx->provctx == NULL) {
254 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
259 if (ctx->digest->dinit == NULL) {
260 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
264 return ctx->digest->dinit(ctx->provctx);
266 /* TODO(3.0): Remove legacy code below */
269 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
272 if (!ENGINE_init(impl)) {
273 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
277 /* Ask if an ENGINE is reserved for this job */
281 /* There's an ENGINE for this job ... (apparently) */
282 const EVP_MD *d = ENGINE_get_digest(impl, type->type);
285 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
289 /* We'll use the ENGINE's private digest definition */
292 * Store the ENGINE functional reference so we know 'type' came
293 * from an ENGINE and we need to release it when done.
300 if (ctx->digest != type) {
301 if (ctx->digest && ctx->digest->ctx_size) {
302 OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
306 if (!(ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) && type->ctx_size) {
307 ctx->update = type->update;
308 ctx->md_data = OPENSSL_zalloc(type->ctx_size);
309 if (ctx->md_data == NULL) {
310 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
315 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
320 * TODO(3.0): Temporarily no support for EVP_DigestSign* inside FIPS module
321 * or when using providers.
323 if (ctx->pctx != NULL
324 && (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx->pctx)
325 || ctx->pctx->op.sig.signature == NULL)) {
327 r = EVP_PKEY_CTX_ctrl(ctx->pctx, -1, EVP_PKEY_OP_TYPE_SIG,
328 EVP_PKEY_CTRL_DIGESTINIT, 0, ctx);
329 if (r <= 0 && (r != -2))
333 if (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT)
335 return ctx->digest->init(ctx);
338 int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t count)
343 if (ctx->pctx != NULL
344 && EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx->pctx)
345 && ctx->pctx->op.sig.sigprovctx != NULL) {
347 * Prior to OpenSSL 3.0 EVP_DigestSignUpdate() and
348 * EVP_DigestVerifyUpdate() were just macros for EVP_DigestUpdate().
349 * Some code calls EVP_DigestUpdate() directly even when initialised
350 * with EVP_DigestSignInit_ex() or
351 * EVP_DigestVerifyInit_ex(), so we detect that and redirect to
352 * the correct EVP_Digest*Update() function
354 if (ctx->pctx->operation == EVP_PKEY_OP_SIGNCTX)
355 return EVP_DigestSignUpdate(ctx, data, count);
356 if (ctx->pctx->operation == EVP_PKEY_OP_VERIFYCTX)
357 return EVP_DigestVerifyUpdate(ctx, data, count);
358 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
362 if (ctx->digest == NULL
363 || ctx->digest->prov == NULL
364 || (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0)
367 if (ctx->digest->dupdate == NULL) {
368 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
371 return ctx->digest->dupdate(ctx->provctx, data, count);
373 /* TODO(3.0): Remove legacy code below */
375 return ctx->update(ctx, data, count);
378 /* The caller can assume that this removes any secret data from the context */
379 int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size)
382 ret = EVP_DigestFinal_ex(ctx, md, size);
383 EVP_MD_CTX_reset(ctx);
387 /* The caller can assume that this removes any secret data from the context */
388 int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *isize)
394 if (ctx->digest == NULL)
397 sz = EVP_MD_size(ctx->digest);
401 if (ctx->digest->prov == NULL)
404 if (ctx->digest->dfinal == NULL) {
405 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
409 ret = ctx->digest->dfinal(ctx->provctx, md, &size, mdsize);
412 if (size <= UINT_MAX) {
415 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
422 /* TODO(3.0): Remove legacy code below */
424 OPENSSL_assert(mdsize <= EVP_MAX_MD_SIZE);
425 ret = ctx->digest->final(ctx, md);
428 if (ctx->digest->cleanup) {
429 ctx->digest->cleanup(ctx);
430 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
432 OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
436 int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t size)
439 OSSL_PARAM params[2];
442 if (ctx->digest == NULL || ctx->digest->prov == NULL)
445 if (ctx->digest->dfinal == NULL) {
446 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
450 params[i++] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN, &size);
451 params[i++] = OSSL_PARAM_construct_end();
453 if (EVP_MD_CTX_set_params(ctx, params) > 0)
454 ret = ctx->digest->dfinal(ctx->provctx, md, &size, size);
459 if (ctx->digest->flags & EVP_MD_FLAG_XOF
461 && ctx->digest->md_ctrl(ctx, EVP_MD_CTRL_XOF_LEN, (int)size, NULL)) {
462 ret = ctx->digest->final(ctx, md);
463 if (ctx->digest->cleanup != NULL) {
464 ctx->digest->cleanup(ctx);
465 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
467 OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
469 ERR_raise(ERR_LIB_EVP, EVP_R_NOT_XOF_OR_INVALID_LENGTH);
475 int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in)
477 EVP_MD_CTX_reset(out);
478 return EVP_MD_CTX_copy_ex(out, in);
481 int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in)
483 unsigned char *tmp_buf;
485 if (in == NULL || in->digest == NULL) {
486 ERR_raise(ERR_LIB_EVP, EVP_R_INPUT_NOT_INITIALIZED);
490 if (in->digest->prov == NULL
491 || (in->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0)
494 if (in->digest->dupctx == NULL) {
495 ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX);
499 EVP_MD_CTX_reset(out);
500 if (out->fetched_digest != NULL)
501 EVP_MD_free(out->fetched_digest);
503 /* NULL out pointers in case of error */
507 if (in->fetched_digest != NULL)
508 EVP_MD_up_ref(in->fetched_digest);
510 if (in->provctx != NULL) {
511 out->provctx = in->digest->dupctx(in->provctx);
512 if (out->provctx == NULL) {
513 ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX);
518 /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
519 EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
521 /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
522 if (in->pctx != NULL) {
523 out->pctx = EVP_PKEY_CTX_dup(in->pctx);
524 if (out->pctx == NULL) {
525 ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX);
526 EVP_MD_CTX_reset(out);
534 /* TODO(3.0): Remove legacy code below */
536 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
537 /* Make sure it's safe to copy a digest context using an ENGINE */
538 if (in->engine && !ENGINE_init(in->engine)) {
539 ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB);
544 if (out->digest == in->digest) {
545 tmp_buf = out->md_data;
546 EVP_MD_CTX_set_flags(out, EVP_MD_CTX_FLAG_REUSE);
549 EVP_MD_CTX_reset(out);
550 memcpy(out, in, sizeof(*out));
552 /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
553 EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
555 /* Null these variables, since they are getting fixed up
556 * properly below. Anything else may cause a memleak and/or
557 * double free if any of the memory allocations below fail
562 if (in->md_data && out->digest->ctx_size) {
564 out->md_data = tmp_buf;
566 out->md_data = OPENSSL_malloc(out->digest->ctx_size);
567 if (out->md_data == NULL) {
568 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
572 memcpy(out->md_data, in->md_data, out->digest->ctx_size);
575 out->update = in->update;
578 /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
580 out->pctx = EVP_PKEY_CTX_dup(in->pctx);
582 EVP_MD_CTX_reset(out);
588 if (out->digest->copy)
589 return out->digest->copy(out, in);
594 int EVP_Digest(const void *data, size_t count,
595 unsigned char *md, unsigned int *size, const EVP_MD *type,
598 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
603 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT);
604 ret = EVP_DigestInit_ex(ctx, type, impl)
605 && EVP_DigestUpdate(ctx, data, count)
606 && EVP_DigestFinal_ex(ctx, md, size);
607 EVP_MD_CTX_free(ctx);
612 int EVP_MD_get_params(const EVP_MD *digest, OSSL_PARAM params[])
614 if (digest != NULL && digest->get_params != NULL)
615 return digest->get_params(params);
619 const OSSL_PARAM *EVP_MD_gettable_params(const EVP_MD *digest)
621 if (digest != NULL && digest->gettable_params != NULL)
622 return digest->gettable_params(
623 ossl_provider_ctx(EVP_MD_provider(digest)));
627 int EVP_MD_CTX_set_params(EVP_MD_CTX *ctx, const OSSL_PARAM params[])
629 EVP_PKEY_CTX *pctx = ctx->pctx;
631 /* If we have a pctx then we should try that first */
633 && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
634 || pctx->operation == EVP_PKEY_OP_SIGNCTX)
635 && pctx->op.sig.sigprovctx != NULL
636 && pctx->op.sig.signature->set_ctx_md_params != NULL)
637 return pctx->op.sig.signature->set_ctx_md_params(pctx->op.sig.sigprovctx,
640 if (ctx->digest != NULL && ctx->digest->set_ctx_params != NULL)
641 return ctx->digest->set_ctx_params(ctx->provctx, params);
646 const OSSL_PARAM *EVP_MD_settable_ctx_params(const EVP_MD *md)
648 if (md != NULL && md->settable_ctx_params != NULL)
649 return md->settable_ctx_params(ossl_provider_ctx(EVP_MD_provider(md)));
653 const OSSL_PARAM *EVP_MD_CTX_settable_params(EVP_MD_CTX *ctx)
660 /* If we have a pctx then we should try that first */
663 && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
664 || pctx->operation == EVP_PKEY_OP_SIGNCTX)
665 && pctx->op.sig.sigprovctx != NULL
666 && pctx->op.sig.signature->settable_ctx_md_params != NULL)
667 return pctx->op.sig.signature->settable_ctx_md_params(
668 pctx->op.sig.sigprovctx);
670 if (ctx->digest != NULL && ctx->digest->settable_ctx_params != NULL)
671 return ctx->digest->settable_ctx_params(
672 ossl_provider_ctx(EVP_MD_provider(ctx->digest)));
677 int EVP_MD_CTX_get_params(EVP_MD_CTX *ctx, OSSL_PARAM params[])
679 EVP_PKEY_CTX *pctx = ctx->pctx;
681 /* If we have a pctx then we should try that first */
683 && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
684 || pctx->operation == EVP_PKEY_OP_SIGNCTX)
685 && pctx->op.sig.sigprovctx != NULL
686 && pctx->op.sig.signature->get_ctx_md_params != NULL)
687 return pctx->op.sig.signature->get_ctx_md_params(pctx->op.sig.sigprovctx,
690 if (ctx->digest != NULL && ctx->digest->get_params != NULL)
691 return ctx->digest->get_ctx_params(ctx->provctx, params);
696 const OSSL_PARAM *EVP_MD_gettable_ctx_params(const EVP_MD *md)
698 if (md != NULL && md->gettable_ctx_params != NULL)
699 return md->gettable_ctx_params(ossl_provider_ctx(EVP_MD_provider(md)));
703 const OSSL_PARAM *EVP_MD_CTX_gettable_params(EVP_MD_CTX *ctx)
710 /* If we have a pctx then we should try that first */
713 && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
714 || pctx->operation == EVP_PKEY_OP_SIGNCTX)
715 && pctx->op.sig.sigprovctx != NULL
716 && pctx->op.sig.signature->gettable_ctx_md_params != NULL)
717 return pctx->op.sig.signature->gettable_ctx_md_params(
718 pctx->op.sig.sigprovctx);
720 if (ctx->digest != NULL
721 && ctx->digest->gettable_ctx_params != NULL)
722 return ctx->digest->gettable_ctx_params(
723 ossl_provider_ctx(EVP_MD_provider(ctx->digest)));
728 /* TODO(3.0): Remove legacy code below - only used by engines & DigestSign */
729 int EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void *p2)
731 int ret = EVP_CTRL_RET_UNSUPPORTED;
734 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
737 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
741 if (ctx->digest != NULL && ctx->digest->prov == NULL)
745 case EVP_MD_CTRL_XOF_LEN:
747 params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN, &sz);
749 case EVP_MD_CTRL_MICALG:
751 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_DIGEST_PARAM_MICALG,
754 case EVP_CTRL_SSL3_MASTER_SECRET:
755 params[0] = OSSL_PARAM_construct_octet_string(OSSL_DIGEST_PARAM_SSL3_MS,
763 ret = EVP_MD_CTX_set_params(ctx, params);
765 ret = EVP_MD_CTX_get_params(ctx, params);
769 /* TODO(3.0): Remove legacy code below */
771 if (ctx->digest->md_ctrl == NULL) {
772 ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_NOT_IMPLEMENTED);
776 ret = ctx->digest->md_ctrl(ctx, cmd, p1, p2);
783 EVP_MD *evp_md_new(void)
785 EVP_MD *md = OPENSSL_zalloc(sizeof(*md));
788 md->lock = CRYPTO_THREAD_lock_new();
789 if (md->lock == NULL) {
799 * FIPS module note: since internal fetches will be entirely
800 * provider based, we know that none of its code depends on legacy
801 * NIDs or any functionality that use them.
804 /* TODO(3.x) get rid of the need for legacy NIDs */
805 static void set_legacy_nid(const char *name, void *vlegacy_nid)
808 int *legacy_nid = vlegacy_nid;
810 * We use lowest level function to get the associated method, because
811 * higher level functions such as EVP_get_digestbyname() have changed
812 * to look at providers too.
814 const void *legacy_method = OBJ_NAME_get(name, OBJ_NAME_TYPE_MD_METH);
816 if (*legacy_nid == -1) /* We found a clash already */
819 if (legacy_method == NULL)
821 nid = EVP_MD_nid(legacy_method);
822 if (*legacy_nid != NID_undef && *legacy_nid != nid) {
830 static int evp_md_cache_constants(EVP_MD *md)
835 unsigned long flags = 0;
836 OSSL_PARAM params[4];
838 params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_BLOCK_SIZE, &blksz);
839 params[1] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_SIZE, &mdsize);
840 params[2] = OSSL_PARAM_construct_ulong(OSSL_DIGEST_PARAM_FLAGS, &flags);
841 params[3] = OSSL_PARAM_construct_end();
842 ok = evp_do_md_getparams(md, params);
843 if (mdsize > INT_MAX || blksz > INT_MAX)
846 md->block_size = (int)blksz;
847 md->md_size = (int)mdsize;
853 static void *evp_md_from_dispatch(int name_id,
854 const OSSL_DISPATCH *fns,
860 /* EVP_MD_fetch() will set the legacy NID if available */
861 if ((md = evp_md_new()) == NULL) {
862 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
867 /* TODO(3.x) get rid of the need for legacy NIDs */
868 md->type = NID_undef;
869 evp_names_do_all(prov, name_id, set_legacy_nid, &md->type);
870 if (md->type == -1) {
871 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
877 md->name_id = name_id;
879 for (; fns->function_id != 0; fns++) {
880 switch (fns->function_id) {
881 case OSSL_FUNC_DIGEST_NEWCTX:
882 if (md->newctx == NULL) {
883 md->newctx = OSSL_FUNC_digest_newctx(fns);
887 case OSSL_FUNC_DIGEST_INIT:
888 if (md->dinit == NULL) {
889 md->dinit = OSSL_FUNC_digest_init(fns);
893 case OSSL_FUNC_DIGEST_UPDATE:
894 if (md->dupdate == NULL) {
895 md->dupdate = OSSL_FUNC_digest_update(fns);
899 case OSSL_FUNC_DIGEST_FINAL:
900 if (md->dfinal == NULL) {
901 md->dfinal = OSSL_FUNC_digest_final(fns);
905 case OSSL_FUNC_DIGEST_DIGEST:
906 if (md->digest == NULL)
907 md->digest = OSSL_FUNC_digest_digest(fns);
908 /* We don't increment fnct for this as it is stand alone */
910 case OSSL_FUNC_DIGEST_FREECTX:
911 if (md->freectx == NULL) {
912 md->freectx = OSSL_FUNC_digest_freectx(fns);
916 case OSSL_FUNC_DIGEST_DUPCTX:
917 if (md->dupctx == NULL)
918 md->dupctx = OSSL_FUNC_digest_dupctx(fns);
920 case OSSL_FUNC_DIGEST_GET_PARAMS:
921 if (md->get_params == NULL)
922 md->get_params = OSSL_FUNC_digest_get_params(fns);
924 case OSSL_FUNC_DIGEST_SET_CTX_PARAMS:
925 if (md->set_ctx_params == NULL)
926 md->set_ctx_params = OSSL_FUNC_digest_set_ctx_params(fns);
928 case OSSL_FUNC_DIGEST_GET_CTX_PARAMS:
929 if (md->get_ctx_params == NULL)
930 md->get_ctx_params = OSSL_FUNC_digest_get_ctx_params(fns);
932 case OSSL_FUNC_DIGEST_GETTABLE_PARAMS:
933 if (md->gettable_params == NULL)
934 md->gettable_params = OSSL_FUNC_digest_gettable_params(fns);
936 case OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS:
937 if (md->settable_ctx_params == NULL)
938 md->settable_ctx_params =
939 OSSL_FUNC_digest_settable_ctx_params(fns);
941 case OSSL_FUNC_DIGEST_GETTABLE_CTX_PARAMS:
942 if (md->gettable_ctx_params == NULL)
943 md->gettable_ctx_params =
944 OSSL_FUNC_digest_gettable_ctx_params(fns);
948 if ((fncnt != 0 && fncnt != 5)
949 || (fncnt == 0 && md->digest == NULL)) {
951 * In order to be a consistent set of functions we either need the
952 * whole set of init/update/final etc functions or none of them.
953 * The "digest" function can standalone. We at least need one way to
957 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
962 ossl_provider_up_ref(prov);
964 if (!evp_md_cache_constants(md)) {
966 ERR_raise(ERR_LIB_EVP, EVP_R_CACHE_CONSTANTS_FAILED);
973 static int evp_md_up_ref(void *md)
975 return EVP_MD_up_ref(md);
978 static void evp_md_free(void *md)
983 EVP_MD *EVP_MD_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
984 const char *properties)
987 evp_generic_fetch(ctx, OSSL_OP_DIGEST, algorithm, properties,
988 evp_md_from_dispatch, evp_md_up_ref, evp_md_free);
993 int EVP_MD_up_ref(EVP_MD *md)
997 CRYPTO_UP_REF(&md->refcnt, &ref, md->lock);
1001 void EVP_MD_free(EVP_MD *md)
1008 CRYPTO_DOWN_REF(&md->refcnt, &i, md->lock);
1011 ossl_provider_free(md->prov);
1012 CRYPTO_THREAD_lock_free(md->lock);
1016 void EVP_MD_do_all_provided(OSSL_LIB_CTX *libctx,
1017 void (*fn)(EVP_MD *mac, void *arg),
1020 evp_generic_do_all(libctx, OSSL_OP_DIGEST,
1021 (void (*)(void *, void *))fn, arg,
1022 evp_md_from_dispatch, evp_md_free);