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/engine.h>
17 #include <openssl/params.h>
18 #include <openssl/core_names.h>
19 #include "internal/cryptlib.h"
20 #include "crypto/evp.h"
21 #include "internal/provider.h"
22 #include "evp_local.h"
24 /* This call frees resources associated with the context */
25 int EVP_MD_CTX_reset(EVP_MD_CTX
*ctx
)
31 /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
33 * pctx should be freed by the user of EVP_MD_CTX
34 * if EVP_MD_CTX_FLAG_KEEP_PKEY_CTX is set
36 if (!EVP_MD_CTX_test_flags(ctx
, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX
))
37 EVP_PKEY_CTX_free(ctx
->pctx
);
40 EVP_MD_free(ctx
->fetched_digest
);
41 ctx
->fetched_digest
= NULL
;
42 ctx
->reqdigest
= NULL
;
44 if (ctx
->provctx
!= NULL
) {
45 if (ctx
->digest
->freectx
!= NULL
)
46 ctx
->digest
->freectx(ctx
->provctx
);
48 EVP_MD_CTX_set_flags(ctx
, EVP_MD_CTX_FLAG_CLEANED
);
51 /* TODO(3.0): Remove legacy code below */
54 * Don't assume ctx->md_data was cleaned in EVP_Digest_Final, because
55 * sometimes only copies of the context are ever finalised.
57 if (ctx
->digest
&& ctx
->digest
->cleanup
58 && !EVP_MD_CTX_test_flags(ctx
, EVP_MD_CTX_FLAG_CLEANED
))
59 ctx
->digest
->cleanup(ctx
);
60 if (ctx
->digest
&& ctx
->digest
->ctx_size
&& ctx
->md_data
61 && !EVP_MD_CTX_test_flags(ctx
, EVP_MD_CTX_FLAG_REUSE
)) {
62 OPENSSL_clear_free(ctx
->md_data
, ctx
->digest
->ctx_size
);
65 #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
66 ENGINE_finish(ctx
->engine
);
69 /* TODO(3.0): End of legacy code */
71 OPENSSL_cleanse(ctx
, sizeof(*ctx
));
76 EVP_MD_CTX
*EVP_MD_CTX_new(void)
78 return OPENSSL_zalloc(sizeof(EVP_MD_CTX
));
81 void EVP_MD_CTX_free(EVP_MD_CTX
*ctx
)
86 EVP_MD_CTX_reset(ctx
);
92 int EVP_DigestInit(EVP_MD_CTX
*ctx
, const EVP_MD
*type
)
94 EVP_MD_CTX_reset(ctx
);
95 return EVP_DigestInit_ex(ctx
, type
, NULL
);
98 int EVP_DigestInit_ex(EVP_MD_CTX
*ctx
, const EVP_MD
*type
, ENGINE
*impl
)
100 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
101 ENGINE
*tmpimpl
= NULL
;
104 EVP_MD_CTX_clear_flags(ctx
, EVP_MD_CTX_FLAG_CLEANED
);
106 if (ctx
->provctx
!= NULL
) {
107 if (!ossl_assert(ctx
->digest
!= NULL
)) {
108 EVPerr(EVP_F_EVP_DIGESTINIT_EX
, EVP_R_INITIALIZATION_ERROR
);
111 if (ctx
->digest
->freectx
!= NULL
)
112 ctx
->digest
->freectx(ctx
->provctx
);
117 ctx
->reqdigest
= type
;
119 /* TODO(3.0): Legacy work around code below. Remove this */
120 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
122 * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
123 * this context may already have an ENGINE! Try to avoid releasing the
124 * previous handle, re-querying for an ENGINE, and having a
125 * reinitialisation, when it may all be unnecessary.
127 if (ctx
->engine
&& ctx
->digest
&&
128 (type
== NULL
|| (type
->type
== ctx
->digest
->type
)))
133 * Ensure an ENGINE left lying around from last time is cleared (the
134 * previous check attempted to avoid this if the same ENGINE and
135 * EVP_MD could be used).
137 ENGINE_finish(ctx
->engine
);
141 if (type
!= NULL
&& impl
== NULL
)
142 tmpimpl
= ENGINE_get_digest_engine(type
->type
);
146 * If there are engines involved or EVP_MD_CTX_FLAG_NO_INIT is set then we
147 * should use legacy handling for now.
149 if (ctx
->engine
!= NULL
151 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
154 || (ctx
->flags
& EVP_MD_CTX_FLAG_NO_INIT
) != 0) {
155 if (ctx
->digest
== ctx
->fetched_digest
)
157 EVP_MD_free(ctx
->fetched_digest
);
158 ctx
->fetched_digest
= NULL
;
162 if (ctx
->digest
!= NULL
&& ctx
->digest
->ctx_size
> 0) {
163 OPENSSL_clear_free(ctx
->md_data
, ctx
->digest
->ctx_size
);
167 /* TODO(3.0): Start of non-legacy code below */
169 if (type
->prov
== NULL
) {
171 /* We only do explicit fetches inside the FIPS module */
172 EVPerr(EVP_F_EVP_DIGESTINIT_EX
, EVP_R_INITIALIZATION_ERROR
);
175 EVP_MD
*provmd
= EVP_MD_fetch(NULL
, OBJ_nid2sn(type
->type
), "");
177 if (provmd
== NULL
) {
178 EVPerr(EVP_F_EVP_DIGESTINIT_EX
, EVP_R_INITIALIZATION_ERROR
);
182 EVP_MD_free(ctx
->fetched_digest
);
183 ctx
->fetched_digest
= provmd
;
187 if (ctx
->provctx
!= NULL
&& ctx
->digest
!= NULL
&& ctx
->digest
!= type
) {
188 if (ctx
->digest
->freectx
!= NULL
)
189 ctx
->digest
->freectx(ctx
->provctx
);
193 if (ctx
->provctx
== NULL
) {
194 ctx
->provctx
= ctx
->digest
->newctx(ossl_provider_ctx(type
->prov
));
195 if (ctx
->provctx
== NULL
) {
196 EVPerr(EVP_F_EVP_DIGESTINIT_EX
, EVP_R_INITIALIZATION_ERROR
);
201 if (ctx
->digest
->dinit
== NULL
) {
202 EVPerr(EVP_F_EVP_DIGESTINIT_EX
, EVP_R_INITIALIZATION_ERROR
);
206 return ctx
->digest
->dinit(ctx
->provctx
);
208 /* TODO(3.0): Remove legacy code below */
211 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
214 if (!ENGINE_init(impl
)) {
215 EVPerr(EVP_F_EVP_DIGESTINIT_EX
, EVP_R_INITIALIZATION_ERROR
);
219 /* Ask if an ENGINE is reserved for this job */
223 /* There's an ENGINE for this job ... (apparently) */
224 const EVP_MD
*d
= ENGINE_get_digest(impl
, type
->type
);
227 EVPerr(EVP_F_EVP_DIGESTINIT_EX
, EVP_R_INITIALIZATION_ERROR
);
231 /* We'll use the ENGINE's private digest definition */
234 * Store the ENGINE functional reference so we know 'type' came
235 * from an ENGINE and we need to release it when done.
242 EVPerr(EVP_F_EVP_DIGESTINIT_EX
, EVP_R_NO_DIGEST_SET
);
248 if (ctx
->digest
!= type
) {
249 if (ctx
->digest
&& ctx
->digest
->ctx_size
) {
250 OPENSSL_clear_free(ctx
->md_data
, ctx
->digest
->ctx_size
);
254 if (!(ctx
->flags
& EVP_MD_CTX_FLAG_NO_INIT
) && type
->ctx_size
) {
255 ctx
->update
= type
->update
;
256 ctx
->md_data
= OPENSSL_zalloc(type
->ctx_size
);
257 if (ctx
->md_data
== NULL
) {
258 EVPerr(EVP_F_EVP_DIGESTINIT_EX
, ERR_R_MALLOC_FAILURE
);
263 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
268 * TODO(3.0): Temporarily no support for EVP_DigestSign* inside FIPS module
269 * or when using providers.
271 if (ctx
->pctx
!= NULL
272 && (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
->pctx
)
273 || ctx
->pctx
->op
.sig
.signature
== NULL
)) {
275 r
= EVP_PKEY_CTX_ctrl(ctx
->pctx
, -1, EVP_PKEY_OP_TYPE_SIG
,
276 EVP_PKEY_CTRL_DIGESTINIT
, 0, ctx
);
277 if (r
<= 0 && (r
!= -2))
281 if (ctx
->flags
& EVP_MD_CTX_FLAG_NO_INIT
)
283 return ctx
->digest
->init(ctx
);
286 int EVP_DigestUpdate(EVP_MD_CTX
*ctx
, const void *data
, size_t count
)
291 if (ctx
->pctx
!= NULL
292 && EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
->pctx
)
293 && ctx
->pctx
->op
.sig
.sigprovctx
!= NULL
) {
295 * Prior to OpenSSL 3.0 EVP_DigestSignUpdate() and
296 * EVP_DigestVerifyUpdate() were just macros for EVP_DigestUpdate().
297 * Some code calls EVP_DigestUpdate() directly even when initialised
298 * with EVP_DigestSignInit_ex() or EVP_DigestVerifyInit_ex(), so we
299 * detect that and redirect to the correct EVP_Digest*Update() function
301 if (ctx
->pctx
->operation
== EVP_PKEY_OP_SIGNCTX
)
302 return EVP_DigestSignUpdate(ctx
, data
, count
);
303 if (ctx
->pctx
->operation
== EVP_PKEY_OP_VERIFYCTX
)
304 return EVP_DigestVerifyUpdate(ctx
, data
, count
);
305 EVPerr(EVP_F_EVP_DIGESTUPDATE
, EVP_R_UPDATE_ERROR
);
309 if (ctx
->digest
== NULL
310 || ctx
->digest
->prov
== NULL
311 || (ctx
->flags
& EVP_MD_CTX_FLAG_NO_INIT
) != 0)
314 if (ctx
->digest
->dupdate
== NULL
) {
315 EVPerr(EVP_F_EVP_DIGESTUPDATE
, EVP_R_UPDATE_ERROR
);
318 return ctx
->digest
->dupdate(ctx
->provctx
, data
, count
);
320 /* TODO(3.0): Remove legacy code below */
322 return ctx
->update(ctx
, data
, count
);
325 /* The caller can assume that this removes any secret data from the context */
326 int EVP_DigestFinal(EVP_MD_CTX
*ctx
, unsigned char *md
, unsigned int *size
)
329 ret
= EVP_DigestFinal_ex(ctx
, md
, size
);
330 EVP_MD_CTX_reset(ctx
);
334 /* The caller can assume that this removes any secret data from the context */
335 int EVP_DigestFinal_ex(EVP_MD_CTX
*ctx
, unsigned char *md
, unsigned int *isize
)
339 size_t mdsize
= EVP_MD_size(ctx
->digest
);
341 if (ctx
->digest
== NULL
|| ctx
->digest
->prov
== NULL
)
344 if (ctx
->digest
->dfinal
== NULL
) {
345 EVPerr(EVP_F_EVP_DIGESTFINAL_EX
, EVP_R_FINAL_ERROR
);
349 ret
= ctx
->digest
->dfinal(ctx
->provctx
, md
, &size
, mdsize
);
352 if (size
<= UINT_MAX
) {
355 EVPerr(EVP_F_EVP_DIGESTFINAL_EX
, EVP_R_FINAL_ERROR
);
362 /* TODO(3.0): Remove legacy code below */
364 OPENSSL_assert(mdsize
<= EVP_MAX_MD_SIZE
);
365 ret
= ctx
->digest
->final(ctx
, md
);
368 if (ctx
->digest
->cleanup
) {
369 ctx
->digest
->cleanup(ctx
);
370 EVP_MD_CTX_set_flags(ctx
, EVP_MD_CTX_FLAG_CLEANED
);
372 OPENSSL_cleanse(ctx
->md_data
, ctx
->digest
->ctx_size
);
376 int EVP_DigestFinalXOF(EVP_MD_CTX
*ctx
, unsigned char *md
, size_t size
)
379 OSSL_PARAM params
[2];
382 if (ctx
->digest
== NULL
|| ctx
->digest
->prov
== NULL
)
385 if (ctx
->digest
->dfinal
== NULL
) {
386 EVPerr(EVP_F_EVP_DIGESTFINALXOF
, EVP_R_FINAL_ERROR
);
390 params
[i
++] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN
, &size
);
391 params
[i
++] = OSSL_PARAM_construct_end();
393 if (EVP_MD_CTX_set_params(ctx
, params
) > 0)
394 ret
= ctx
->digest
->dfinal(ctx
->provctx
, md
, &size
, size
);
395 EVP_MD_CTX_reset(ctx
);
399 if (ctx
->digest
->flags
& EVP_MD_FLAG_XOF
401 && ctx
->digest
->md_ctrl(ctx
, EVP_MD_CTRL_XOF_LEN
, (int)size
, NULL
)) {
402 ret
= ctx
->digest
->final(ctx
, md
);
403 if (ctx
->digest
->cleanup
!= NULL
) {
404 ctx
->digest
->cleanup(ctx
);
405 EVP_MD_CTX_set_flags(ctx
, EVP_MD_CTX_FLAG_CLEANED
);
407 OPENSSL_cleanse(ctx
->md_data
, ctx
->digest
->ctx_size
);
409 EVPerr(EVP_F_EVP_DIGESTFINALXOF
, EVP_R_NOT_XOF_OR_INVALID_LENGTH
);
415 int EVP_MD_CTX_copy(EVP_MD_CTX
*out
, const EVP_MD_CTX
*in
)
417 EVP_MD_CTX_reset(out
);
418 return EVP_MD_CTX_copy_ex(out
, in
);
421 int EVP_MD_CTX_copy_ex(EVP_MD_CTX
*out
, const EVP_MD_CTX
*in
)
423 unsigned char *tmp_buf
;
425 if (in
== NULL
|| in
->digest
== NULL
) {
426 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX
, EVP_R_INPUT_NOT_INITIALIZED
);
430 if (in
->digest
->prov
== NULL
431 || (in
->flags
& EVP_MD_CTX_FLAG_NO_INIT
) != 0)
434 if (in
->digest
->dupctx
== NULL
) {
435 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX
, EVP_R_NOT_ABLE_TO_COPY_CTX
);
439 EVP_MD_CTX_reset(out
);
440 if (out
->fetched_digest
!= NULL
)
441 EVP_MD_free(out
->fetched_digest
);
443 /* NULL out pointers in case of error */
447 if (in
->fetched_digest
!= NULL
)
448 EVP_MD_up_ref(in
->fetched_digest
);
450 out
->provctx
= in
->digest
->dupctx(in
->provctx
);
451 if (out
->provctx
== NULL
) {
452 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX
, EVP_R_NOT_ABLE_TO_COPY_CTX
);
456 /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
457 EVP_MD_CTX_clear_flags(out
, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX
);
459 /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
460 if (in
->pctx
!= NULL
) {
461 out
->pctx
= EVP_PKEY_CTX_dup(in
->pctx
);
462 if (out
->pctx
== NULL
) {
463 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX
, EVP_R_NOT_ABLE_TO_COPY_CTX
);
464 EVP_MD_CTX_reset(out
);
472 /* TODO(3.0): Remove legacy code below */
474 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
475 /* Make sure it's safe to copy a digest context using an ENGINE */
476 if (in
->engine
&& !ENGINE_init(in
->engine
)) {
477 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX
, ERR_R_ENGINE_LIB
);
482 if (out
->digest
== in
->digest
) {
483 tmp_buf
= out
->md_data
;
484 EVP_MD_CTX_set_flags(out
, EVP_MD_CTX_FLAG_REUSE
);
487 EVP_MD_CTX_reset(out
);
488 memcpy(out
, in
, sizeof(*out
));
490 /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
491 EVP_MD_CTX_clear_flags(out
, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX
);
493 /* Null these variables, since they are getting fixed up
494 * properly below. Anything else may cause a memleak and/or
495 * double free if any of the memory allocations below fail
500 if (in
->md_data
&& out
->digest
->ctx_size
) {
502 out
->md_data
= tmp_buf
;
504 out
->md_data
= OPENSSL_malloc(out
->digest
->ctx_size
);
505 if (out
->md_data
== NULL
) {
506 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX
, ERR_R_MALLOC_FAILURE
);
510 memcpy(out
->md_data
, in
->md_data
, out
->digest
->ctx_size
);
513 out
->update
= in
->update
;
516 /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
518 out
->pctx
= EVP_PKEY_CTX_dup(in
->pctx
);
520 EVP_MD_CTX_reset(out
);
526 if (out
->digest
->copy
)
527 return out
->digest
->copy(out
, in
);
532 int EVP_Digest(const void *data
, size_t count
,
533 unsigned char *md
, unsigned int *size
, const EVP_MD
*type
,
536 EVP_MD_CTX
*ctx
= EVP_MD_CTX_new();
541 EVP_MD_CTX_set_flags(ctx
, EVP_MD_CTX_FLAG_ONESHOT
);
542 ret
= EVP_DigestInit_ex(ctx
, type
, impl
)
543 && EVP_DigestUpdate(ctx
, data
, count
)
544 && EVP_DigestFinal_ex(ctx
, md
, size
);
545 EVP_MD_CTX_free(ctx
);
550 int EVP_MD_get_params(const EVP_MD
*digest
, OSSL_PARAM params
[])
552 if (digest
!= NULL
&& digest
->get_params
!= NULL
)
553 return digest
->get_params(params
);
557 const OSSL_PARAM
*EVP_MD_gettable_params(const EVP_MD
*digest
)
559 if (digest
!= NULL
&& digest
->gettable_params
!= NULL
)
560 return digest
->gettable_params();
564 int EVP_MD_CTX_set_params(EVP_MD_CTX
*ctx
, const OSSL_PARAM params
[])
566 EVP_PKEY_CTX
*pctx
= ctx
->pctx
;
568 if (ctx
->digest
!= NULL
&& ctx
->digest
->set_ctx_params
!= NULL
)
569 return ctx
->digest
->set_ctx_params(ctx
->provctx
, params
);
572 && (pctx
->operation
== EVP_PKEY_OP_VERIFYCTX
573 || pctx
->operation
== EVP_PKEY_OP_SIGNCTX
)
574 && pctx
->op
.sig
.sigprovctx
!= NULL
575 && pctx
->op
.sig
.signature
->set_ctx_md_params
!= NULL
)
576 return pctx
->op
.sig
.signature
->set_ctx_md_params(pctx
->op
.sig
.sigprovctx
,
581 const OSSL_PARAM
*EVP_MD_settable_ctx_params(const EVP_MD
*md
)
583 if (md
!= NULL
&& md
->settable_ctx_params
!= NULL
)
584 return md
->settable_ctx_params();
588 const OSSL_PARAM
*EVP_MD_CTX_settable_params(EVP_MD_CTX
*ctx
)
593 && ctx
->digest
!= NULL
594 && ctx
->digest
->settable_ctx_params
!= NULL
)
595 return ctx
->digest
->settable_ctx_params();
599 && (pctx
->operation
== EVP_PKEY_OP_VERIFYCTX
600 || pctx
->operation
== EVP_PKEY_OP_SIGNCTX
)
601 && pctx
->op
.sig
.sigprovctx
!= NULL
602 && pctx
->op
.sig
.signature
->settable_ctx_md_params
!= NULL
)
603 return pctx
->op
.sig
.signature
->settable_ctx_md_params(
604 pctx
->op
.sig
.sigprovctx
);
609 int EVP_MD_CTX_get_params(EVP_MD_CTX
*ctx
, OSSL_PARAM params
[])
611 EVP_PKEY_CTX
*pctx
= ctx
->pctx
;
613 if (ctx
->digest
!= NULL
&& ctx
->digest
->get_params
!= NULL
)
614 return ctx
->digest
->get_ctx_params(ctx
->provctx
, params
);
617 && (pctx
->operation
== EVP_PKEY_OP_VERIFYCTX
618 || pctx
->operation
== EVP_PKEY_OP_SIGNCTX
)
619 && pctx
->op
.sig
.sigprovctx
!= NULL
620 && pctx
->op
.sig
.signature
->get_ctx_md_params
!= NULL
)
621 return pctx
->op
.sig
.signature
->get_ctx_md_params(pctx
->op
.sig
.sigprovctx
,
627 const OSSL_PARAM
*EVP_MD_gettable_ctx_params(const EVP_MD
*md
)
629 if (md
!= NULL
&& md
->gettable_ctx_params
!= NULL
)
630 return md
->gettable_ctx_params();
634 const OSSL_PARAM
*EVP_MD_CTX_gettable_params(EVP_MD_CTX
*ctx
)
639 && ctx
->digest
!= NULL
640 && ctx
->digest
->gettable_ctx_params
!= NULL
)
641 return ctx
->digest
->gettable_ctx_params();
645 && (pctx
->operation
== EVP_PKEY_OP_VERIFYCTX
646 || pctx
->operation
== EVP_PKEY_OP_SIGNCTX
)
647 && pctx
->op
.sig
.sigprovctx
!= NULL
648 && pctx
->op
.sig
.signature
->gettable_ctx_md_params
!= NULL
)
649 return pctx
->op
.sig
.signature
->gettable_ctx_md_params(
650 pctx
->op
.sig
.sigprovctx
);
655 /* TODO(3.0): Remove legacy code below - only used by engines & DigestSign */
656 int EVP_MD_CTX_ctrl(EVP_MD_CTX
*ctx
, int cmd
, int p1
, void *p2
)
658 int ret
= EVP_CTRL_RET_UNSUPPORTED
;
661 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
664 ERR_raise(ERR_LIB_EVP
, ERR_R_PASSED_NULL_PARAMETER
);
668 if (ctx
->digest
!= NULL
&& ctx
->digest
->prov
== NULL
)
672 case EVP_MD_CTRL_XOF_LEN
:
674 params
[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN
, &sz
);
676 case EVP_MD_CTRL_MICALG
:
678 params
[0] = OSSL_PARAM_construct_utf8_string(OSSL_DIGEST_PARAM_MICALG
,
681 case EVP_CTRL_SSL3_MASTER_SECRET
:
682 params
[0] = OSSL_PARAM_construct_octet_string(OSSL_DIGEST_PARAM_SSL3_MS
,
690 ret
= EVP_MD_CTX_set_params(ctx
, params
);
692 ret
= EVP_MD_CTX_get_params(ctx
, params
);
696 /* TODO(3.0): Remove legacy code below */
698 if (ctx
->digest
->md_ctrl
== NULL
) {
699 ERR_raise(ERR_LIB_EVP
, EVP_R_CTRL_NOT_IMPLEMENTED
);
703 ret
= ctx
->digest
->md_ctrl(ctx
, cmd
, p1
, p2
);
710 EVP_MD
*evp_md_new(void)
712 EVP_MD
*md
= OPENSSL_zalloc(sizeof(*md
));
715 md
->lock
= CRYPTO_THREAD_lock_new();
716 if (md
->lock
== NULL
) {
726 * FIPS module note: since internal fetches will be entirely
727 * provider based, we know that none of its code depends on legacy
728 * NIDs or any functionality that use them.
731 /* TODO(3.x) get rid of the need for legacy NIDs */
732 static void set_legacy_nid(const char *name
, void *vlegacy_nid
)
735 int *legacy_nid
= vlegacy_nid
;
737 * We use lowest level function to get the associated method, because
738 * higher level functions such as EVP_get_digestbyname() have changed
739 * to look at providers too.
741 const void *legacy_method
= OBJ_NAME_get(name
, OBJ_NAME_TYPE_MD_METH
);
743 if (*legacy_nid
== -1) /* We found a clash already */
746 if (legacy_method
== NULL
)
748 nid
= EVP_MD_nid(legacy_method
);
749 if (*legacy_nid
!= NID_undef
&& *legacy_nid
!= nid
) {
757 static void *evp_md_from_dispatch(int name_id
,
758 const OSSL_DISPATCH
*fns
,
764 /* EVP_MD_fetch() will set the legacy NID if available */
765 if ((md
= evp_md_new()) == NULL
) {
766 EVPerr(0, ERR_R_MALLOC_FAILURE
);
771 /* TODO(3.x) get rid of the need for legacy NIDs */
772 md
->type
= NID_undef
;
773 evp_names_do_all(prov
, name_id
, set_legacy_nid
, &md
->type
);
774 if (md
->type
== -1) {
775 ERR_raise(ERR_LIB_EVP
, ERR_R_INTERNAL_ERROR
);
781 md
->name_id
= name_id
;
783 for (; fns
->function_id
!= 0; fns
++) {
784 switch (fns
->function_id
) {
785 case OSSL_FUNC_DIGEST_NEWCTX
:
786 if (md
->newctx
== NULL
) {
787 md
->newctx
= OSSL_FUNC_digest_newctx(fns
);
791 case OSSL_FUNC_DIGEST_INIT
:
792 if (md
->dinit
== NULL
) {
793 md
->dinit
= OSSL_FUNC_digest_init(fns
);
797 case OSSL_FUNC_DIGEST_UPDATE
:
798 if (md
->dupdate
== NULL
) {
799 md
->dupdate
= OSSL_FUNC_digest_update(fns
);
803 case OSSL_FUNC_DIGEST_FINAL
:
804 if (md
->dfinal
== NULL
) {
805 md
->dfinal
= OSSL_FUNC_digest_final(fns
);
809 case OSSL_FUNC_DIGEST_DIGEST
:
810 if (md
->digest
== NULL
)
811 md
->digest
= OSSL_FUNC_digest_digest(fns
);
812 /* We don't increment fnct for this as it is stand alone */
814 case OSSL_FUNC_DIGEST_FREECTX
:
815 if (md
->freectx
== NULL
) {
816 md
->freectx
= OSSL_FUNC_digest_freectx(fns
);
820 case OSSL_FUNC_DIGEST_DUPCTX
:
821 if (md
->dupctx
== NULL
)
822 md
->dupctx
= OSSL_FUNC_digest_dupctx(fns
);
824 case OSSL_FUNC_DIGEST_GET_PARAMS
:
825 if (md
->get_params
== NULL
)
826 md
->get_params
= OSSL_FUNC_digest_get_params(fns
);
828 case OSSL_FUNC_DIGEST_SET_CTX_PARAMS
:
829 if (md
->set_ctx_params
== NULL
)
830 md
->set_ctx_params
= OSSL_FUNC_digest_set_ctx_params(fns
);
832 case OSSL_FUNC_DIGEST_GET_CTX_PARAMS
:
833 if (md
->get_ctx_params
== NULL
)
834 md
->get_ctx_params
= OSSL_FUNC_digest_get_ctx_params(fns
);
836 case OSSL_FUNC_DIGEST_GETTABLE_PARAMS
:
837 if (md
->gettable_params
== NULL
)
838 md
->gettable_params
= OSSL_FUNC_digest_gettable_params(fns
);
840 case OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS
:
841 if (md
->settable_ctx_params
== NULL
)
842 md
->settable_ctx_params
=
843 OSSL_FUNC_digest_settable_ctx_params(fns
);
845 case OSSL_FUNC_DIGEST_GETTABLE_CTX_PARAMS
:
846 if (md
->gettable_ctx_params
== NULL
)
847 md
->gettable_ctx_params
=
848 OSSL_FUNC_digest_gettable_ctx_params(fns
);
852 if ((fncnt
!= 0 && fncnt
!= 5)
853 || (fncnt
== 0 && md
->digest
== NULL
)) {
855 * In order to be a consistent set of functions we either need the
856 * whole set of init/update/final etc functions or none of them.
857 * The "digest" function can standalone. We at least need one way to
861 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_PROVIDER_FUNCTIONS
);
866 ossl_provider_up_ref(prov
);
871 static int evp_md_up_ref(void *md
)
873 return EVP_MD_up_ref(md
);
876 static void evp_md_free(void *md
)
881 EVP_MD
*EVP_MD_fetch(OPENSSL_CTX
*ctx
, const char *algorithm
,
882 const char *properties
)
885 evp_generic_fetch(ctx
, OSSL_OP_DIGEST
, algorithm
, properties
,
886 evp_md_from_dispatch
, evp_md_up_ref
, evp_md_free
);
891 int EVP_MD_up_ref(EVP_MD
*md
)
895 CRYPTO_UP_REF(&md
->refcnt
, &ref
, md
->lock
);
899 void EVP_MD_free(EVP_MD
*md
)
906 CRYPTO_DOWN_REF(&md
->refcnt
, &i
, md
->lock
);
909 ossl_provider_free(md
->prov
);
910 CRYPTO_THREAD_lock_free(md
->lock
);
914 void EVP_MD_do_all_provided(OPENSSL_CTX
*libctx
,
915 void (*fn
)(EVP_MD
*mac
, void *arg
),
918 evp_generic_do_all(libctx
, OSSL_OP_DIGEST
,
919 (void (*)(void *, void *))fn
, arg
,
920 evp_md_from_dispatch
, evp_md_free
);