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 ASN1err(0, ERR_R_MALLOC_FAILURE
);
97 # ifndef OPENSSL_NO_EC
98 if (id
!= NULL
&& EVP_PKEY_CTX_set1_id(pctx
, id
->data
, id
->length
) <= 0) {
99 ASN1err(0, ERR_R_MALLOC_FAILURE
);
104 EVP_MD_CTX_set_pkey_ctx(ctx
, pctx
);
108 EVP_PKEY_CTX_free(pctx
);
109 EVP_MD_CTX_free(ctx
);
114 EVP_MD_CTX
*EVP_MD_CTX_new(void)
116 return OPENSSL_zalloc(sizeof(EVP_MD_CTX
));
119 void EVP_MD_CTX_free(EVP_MD_CTX
*ctx
)
124 EVP_MD_CTX_reset(ctx
);
130 int EVP_DigestInit(EVP_MD_CTX
*ctx
, const EVP_MD
*type
)
132 EVP_MD_CTX_reset(ctx
);
133 return EVP_DigestInit_ex(ctx
, type
, NULL
);
136 int EVP_DigestInit_ex(EVP_MD_CTX
*ctx
, const EVP_MD
*type
, ENGINE
*impl
)
138 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
139 ENGINE
*tmpimpl
= NULL
;
142 #if !defined(FIPS_MODULE)
143 if (ctx
->pctx
!= NULL
144 && EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
->pctx
)
145 && ctx
->pctx
->op
.sig
.sigprovctx
!= NULL
) {
147 * Prior to OpenSSL 3.0 calling EVP_DigestInit_ex() on an mdctx
148 * previously initialised with EVP_DigestSignInit() would retain
149 * information about the key, and re-initialise for another sign
150 * operation. So in that case we redirect to EVP_DigestSignInit()
152 if (ctx
->pctx
->operation
== EVP_PKEY_OP_SIGNCTX
)
153 return EVP_DigestSignInit(ctx
, NULL
, type
, impl
, NULL
);
154 if (ctx
->pctx
->operation
== EVP_PKEY_OP_VERIFYCTX
)
155 return EVP_DigestVerifyInit(ctx
, NULL
, type
, impl
, NULL
);
156 EVPerr(0, EVP_R_UPDATE_ERROR
);
161 EVP_MD_CTX_clear_flags(ctx
, EVP_MD_CTX_FLAG_CLEANED
);
163 if (ctx
->provctx
!= NULL
) {
164 if (!ossl_assert(ctx
->digest
!= NULL
)) {
165 EVPerr(EVP_F_EVP_DIGESTINIT_EX
, EVP_R_INITIALIZATION_ERROR
);
168 if (ctx
->digest
->freectx
!= NULL
)
169 ctx
->digest
->freectx(ctx
->provctx
);
174 ctx
->reqdigest
= type
;
176 /* TODO(3.0): Legacy work around code below. Remove this */
177 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
179 * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
180 * this context may already have an ENGINE! Try to avoid releasing the
181 * previous handle, re-querying for an ENGINE, and having a
182 * reinitialisation, when it may all be unnecessary.
184 if (ctx
->engine
&& ctx
->digest
&&
185 (type
== NULL
|| (type
->type
== ctx
->digest
->type
)))
190 * Ensure an ENGINE left lying around from last time is cleared (the
191 * previous check attempted to avoid this if the same ENGINE and
192 * EVP_MD could be used).
194 ENGINE_finish(ctx
->engine
);
198 if (type
!= NULL
&& impl
== NULL
)
199 tmpimpl
= ENGINE_get_digest_engine(type
->type
);
203 * If there are engines involved or EVP_MD_CTX_FLAG_NO_INIT is set then we
204 * should use legacy handling for now.
206 if (ctx
->engine
!= NULL
208 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
211 || (ctx
->flags
& EVP_MD_CTX_FLAG_NO_INIT
) != 0) {
212 if (ctx
->digest
== ctx
->fetched_digest
)
214 EVP_MD_free(ctx
->fetched_digest
);
215 ctx
->fetched_digest
= NULL
;
219 if (ctx
->digest
!= NULL
&& ctx
->digest
->ctx_size
> 0) {
220 OPENSSL_clear_free(ctx
->md_data
, ctx
->digest
->ctx_size
);
224 /* TODO(3.0): Start of non-legacy code below */
226 if (type
->prov
== NULL
) {
228 /* We only do explicit fetches inside the FIPS module */
229 EVPerr(EVP_F_EVP_DIGESTINIT_EX
, EVP_R_INITIALIZATION_ERROR
);
232 EVP_MD
*provmd
= EVP_MD_fetch(NULL
, OBJ_nid2sn(type
->type
), "");
237 EVP_MD_free(ctx
->fetched_digest
);
238 ctx
->fetched_digest
= provmd
;
242 if (ctx
->provctx
!= NULL
&& ctx
->digest
!= NULL
&& ctx
->digest
!= type
) {
243 if (ctx
->digest
->freectx
!= NULL
)
244 ctx
->digest
->freectx(ctx
->provctx
);
248 if (ctx
->provctx
== NULL
) {
249 ctx
->provctx
= ctx
->digest
->newctx(ossl_provider_ctx(type
->prov
));
250 if (ctx
->provctx
== NULL
) {
251 EVPerr(EVP_F_EVP_DIGESTINIT_EX
, EVP_R_INITIALIZATION_ERROR
);
256 if (ctx
->digest
->dinit
== NULL
) {
257 EVPerr(EVP_F_EVP_DIGESTINIT_EX
, EVP_R_INITIALIZATION_ERROR
);
261 return ctx
->digest
->dinit(ctx
->provctx
);
263 /* TODO(3.0): Remove legacy code below */
266 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
269 if (!ENGINE_init(impl
)) {
270 EVPerr(EVP_F_EVP_DIGESTINIT_EX
, EVP_R_INITIALIZATION_ERROR
);
274 /* Ask if an ENGINE is reserved for this job */
278 /* There's an ENGINE for this job ... (apparently) */
279 const EVP_MD
*d
= ENGINE_get_digest(impl
, type
->type
);
282 EVPerr(EVP_F_EVP_DIGESTINIT_EX
, EVP_R_INITIALIZATION_ERROR
);
286 /* We'll use the ENGINE's private digest definition */
289 * Store the ENGINE functional reference so we know 'type' came
290 * from an ENGINE and we need to release it when done.
297 EVPerr(EVP_F_EVP_DIGESTINIT_EX
, EVP_R_NO_DIGEST_SET
);
303 if (ctx
->digest
!= type
) {
304 if (ctx
->digest
&& ctx
->digest
->ctx_size
) {
305 OPENSSL_clear_free(ctx
->md_data
, ctx
->digest
->ctx_size
);
309 if (!(ctx
->flags
& EVP_MD_CTX_FLAG_NO_INIT
) && type
->ctx_size
) {
310 ctx
->update
= type
->update
;
311 ctx
->md_data
= OPENSSL_zalloc(type
->ctx_size
);
312 if (ctx
->md_data
== NULL
) {
313 EVPerr(EVP_F_EVP_DIGESTINIT_EX
, ERR_R_MALLOC_FAILURE
);
318 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
323 * TODO(3.0): Temporarily no support for EVP_DigestSign* inside FIPS module
324 * or when using providers.
326 if (ctx
->pctx
!= NULL
327 && (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
->pctx
)
328 || ctx
->pctx
->op
.sig
.signature
== NULL
)) {
330 r
= EVP_PKEY_CTX_ctrl(ctx
->pctx
, -1, EVP_PKEY_OP_TYPE_SIG
,
331 EVP_PKEY_CTRL_DIGESTINIT
, 0, ctx
);
332 if (r
<= 0 && (r
!= -2))
336 if (ctx
->flags
& EVP_MD_CTX_FLAG_NO_INIT
)
338 return ctx
->digest
->init(ctx
);
341 int EVP_DigestUpdate(EVP_MD_CTX
*ctx
, const void *data
, size_t count
)
346 if (ctx
->pctx
!= NULL
347 && EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
->pctx
)
348 && ctx
->pctx
->op
.sig
.sigprovctx
!= NULL
) {
350 * Prior to OpenSSL 3.0 EVP_DigestSignUpdate() and
351 * EVP_DigestVerifyUpdate() were just macros for EVP_DigestUpdate().
352 * Some code calls EVP_DigestUpdate() directly even when initialised
353 * with EVP_DigestSignInit_ex() or
354 * EVP_DigestVerifyInit_ex(), so we detect that and redirect to
355 * the correct EVP_Digest*Update() function
357 if (ctx
->pctx
->operation
== EVP_PKEY_OP_SIGNCTX
)
358 return EVP_DigestSignUpdate(ctx
, data
, count
);
359 if (ctx
->pctx
->operation
== EVP_PKEY_OP_VERIFYCTX
)
360 return EVP_DigestVerifyUpdate(ctx
, data
, count
);
361 EVPerr(EVP_F_EVP_DIGESTUPDATE
, EVP_R_UPDATE_ERROR
);
365 if (ctx
->digest
== NULL
366 || ctx
->digest
->prov
== NULL
367 || (ctx
->flags
& EVP_MD_CTX_FLAG_NO_INIT
) != 0)
370 if (ctx
->digest
->dupdate
== NULL
) {
371 EVPerr(EVP_F_EVP_DIGESTUPDATE
, EVP_R_UPDATE_ERROR
);
374 return ctx
->digest
->dupdate(ctx
->provctx
, data
, count
);
376 /* TODO(3.0): Remove legacy code below */
378 return ctx
->update(ctx
, data
, count
);
381 /* The caller can assume that this removes any secret data from the context */
382 int EVP_DigestFinal(EVP_MD_CTX
*ctx
, unsigned char *md
, unsigned int *size
)
385 ret
= EVP_DigestFinal_ex(ctx
, md
, size
);
386 EVP_MD_CTX_reset(ctx
);
390 /* The caller can assume that this removes any secret data from the context */
391 int EVP_DigestFinal_ex(EVP_MD_CTX
*ctx
, unsigned char *md
, unsigned int *isize
)
397 if (ctx
->digest
== NULL
)
400 sz
= EVP_MD_size(ctx
->digest
);
404 if (ctx
->digest
->prov
== NULL
)
407 if (ctx
->digest
->dfinal
== NULL
) {
408 EVPerr(EVP_F_EVP_DIGESTFINAL_EX
, EVP_R_FINAL_ERROR
);
412 ret
= ctx
->digest
->dfinal(ctx
->provctx
, md
, &size
, mdsize
);
415 if (size
<= UINT_MAX
) {
418 EVPerr(EVP_F_EVP_DIGESTFINAL_EX
, EVP_R_FINAL_ERROR
);
425 /* TODO(3.0): Remove legacy code below */
427 OPENSSL_assert(mdsize
<= EVP_MAX_MD_SIZE
);
428 ret
= ctx
->digest
->final(ctx
, md
);
431 if (ctx
->digest
->cleanup
) {
432 ctx
->digest
->cleanup(ctx
);
433 EVP_MD_CTX_set_flags(ctx
, EVP_MD_CTX_FLAG_CLEANED
);
435 OPENSSL_cleanse(ctx
->md_data
, ctx
->digest
->ctx_size
);
439 int EVP_DigestFinalXOF(EVP_MD_CTX
*ctx
, unsigned char *md
, size_t size
)
442 OSSL_PARAM params
[2];
445 if (ctx
->digest
== NULL
|| ctx
->digest
->prov
== NULL
)
448 if (ctx
->digest
->dfinal
== NULL
) {
449 EVPerr(EVP_F_EVP_DIGESTFINALXOF
, EVP_R_FINAL_ERROR
);
453 params
[i
++] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN
, &size
);
454 params
[i
++] = OSSL_PARAM_construct_end();
456 if (EVP_MD_CTX_set_params(ctx
, params
) > 0)
457 ret
= ctx
->digest
->dfinal(ctx
->provctx
, md
, &size
, size
);
458 EVP_MD_CTX_reset(ctx
);
462 if (ctx
->digest
->flags
& EVP_MD_FLAG_XOF
464 && ctx
->digest
->md_ctrl(ctx
, EVP_MD_CTRL_XOF_LEN
, (int)size
, NULL
)) {
465 ret
= ctx
->digest
->final(ctx
, md
);
466 if (ctx
->digest
->cleanup
!= NULL
) {
467 ctx
->digest
->cleanup(ctx
);
468 EVP_MD_CTX_set_flags(ctx
, EVP_MD_CTX_FLAG_CLEANED
);
470 OPENSSL_cleanse(ctx
->md_data
, ctx
->digest
->ctx_size
);
472 EVPerr(EVP_F_EVP_DIGESTFINALXOF
, EVP_R_NOT_XOF_OR_INVALID_LENGTH
);
478 int EVP_MD_CTX_copy(EVP_MD_CTX
*out
, const EVP_MD_CTX
*in
)
480 EVP_MD_CTX_reset(out
);
481 return EVP_MD_CTX_copy_ex(out
, in
);
484 int EVP_MD_CTX_copy_ex(EVP_MD_CTX
*out
, const EVP_MD_CTX
*in
)
486 unsigned char *tmp_buf
;
488 if (in
== NULL
|| in
->digest
== NULL
) {
489 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX
, EVP_R_INPUT_NOT_INITIALIZED
);
493 if (in
->digest
->prov
== NULL
494 || (in
->flags
& EVP_MD_CTX_FLAG_NO_INIT
) != 0)
497 if (in
->digest
->dupctx
== NULL
) {
498 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX
, EVP_R_NOT_ABLE_TO_COPY_CTX
);
502 EVP_MD_CTX_reset(out
);
503 if (out
->fetched_digest
!= NULL
)
504 EVP_MD_free(out
->fetched_digest
);
506 /* NULL out pointers in case of error */
510 if (in
->fetched_digest
!= NULL
)
511 EVP_MD_up_ref(in
->fetched_digest
);
513 if (in
->provctx
!= NULL
) {
514 out
->provctx
= in
->digest
->dupctx(in
->provctx
);
515 if (out
->provctx
== NULL
) {
516 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX
, EVP_R_NOT_ABLE_TO_COPY_CTX
);
521 /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
522 EVP_MD_CTX_clear_flags(out
, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX
);
524 /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
525 if (in
->pctx
!= NULL
) {
526 out
->pctx
= EVP_PKEY_CTX_dup(in
->pctx
);
527 if (out
->pctx
== NULL
) {
528 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX
, EVP_R_NOT_ABLE_TO_COPY_CTX
);
529 EVP_MD_CTX_reset(out
);
537 /* TODO(3.0): Remove legacy code below */
539 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
540 /* Make sure it's safe to copy a digest context using an ENGINE */
541 if (in
->engine
&& !ENGINE_init(in
->engine
)) {
542 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX
, ERR_R_ENGINE_LIB
);
547 if (out
->digest
== in
->digest
) {
548 tmp_buf
= out
->md_data
;
549 EVP_MD_CTX_set_flags(out
, EVP_MD_CTX_FLAG_REUSE
);
552 EVP_MD_CTX_reset(out
);
553 memcpy(out
, in
, sizeof(*out
));
555 /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
556 EVP_MD_CTX_clear_flags(out
, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX
);
558 /* Null these variables, since they are getting fixed up
559 * properly below. Anything else may cause a memleak and/or
560 * double free if any of the memory allocations below fail
565 if (in
->md_data
&& out
->digest
->ctx_size
) {
567 out
->md_data
= tmp_buf
;
569 out
->md_data
= OPENSSL_malloc(out
->digest
->ctx_size
);
570 if (out
->md_data
== NULL
) {
571 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX
, ERR_R_MALLOC_FAILURE
);
575 memcpy(out
->md_data
, in
->md_data
, out
->digest
->ctx_size
);
578 out
->update
= in
->update
;
581 /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
583 out
->pctx
= EVP_PKEY_CTX_dup(in
->pctx
);
585 EVP_MD_CTX_reset(out
);
591 if (out
->digest
->copy
)
592 return out
->digest
->copy(out
, in
);
597 int EVP_Digest(const void *data
, size_t count
,
598 unsigned char *md
, unsigned int *size
, const EVP_MD
*type
,
601 EVP_MD_CTX
*ctx
= EVP_MD_CTX_new();
606 EVP_MD_CTX_set_flags(ctx
, EVP_MD_CTX_FLAG_ONESHOT
);
607 ret
= EVP_DigestInit_ex(ctx
, type
, impl
)
608 && EVP_DigestUpdate(ctx
, data
, count
)
609 && EVP_DigestFinal_ex(ctx
, md
, size
);
610 EVP_MD_CTX_free(ctx
);
615 int EVP_MD_get_params(const EVP_MD
*digest
, OSSL_PARAM params
[])
617 if (digest
!= NULL
&& digest
->get_params
!= NULL
)
618 return digest
->get_params(params
);
622 const OSSL_PARAM
*EVP_MD_gettable_params(const EVP_MD
*digest
)
624 if (digest
!= NULL
&& digest
->gettable_params
!= NULL
)
625 return digest
->gettable_params(
626 ossl_provider_ctx(EVP_MD_provider(digest
)));
630 int EVP_MD_CTX_set_params(EVP_MD_CTX
*ctx
, const OSSL_PARAM params
[])
632 EVP_PKEY_CTX
*pctx
= ctx
->pctx
;
634 /* If we have a pctx then we should try that first */
636 && (pctx
->operation
== EVP_PKEY_OP_VERIFYCTX
637 || pctx
->operation
== EVP_PKEY_OP_SIGNCTX
)
638 && pctx
->op
.sig
.sigprovctx
!= NULL
639 && pctx
->op
.sig
.signature
->set_ctx_md_params
!= NULL
)
640 return pctx
->op
.sig
.signature
->set_ctx_md_params(pctx
->op
.sig
.sigprovctx
,
643 if (ctx
->digest
!= NULL
&& ctx
->digest
->set_ctx_params
!= NULL
)
644 return ctx
->digest
->set_ctx_params(ctx
->provctx
, params
);
649 const OSSL_PARAM
*EVP_MD_settable_ctx_params(const EVP_MD
*md
)
651 if (md
!= NULL
&& md
->settable_ctx_params
!= NULL
)
652 return md
->settable_ctx_params(ossl_provider_ctx(EVP_MD_provider(md
)));
656 const OSSL_PARAM
*EVP_MD_CTX_settable_params(EVP_MD_CTX
*ctx
)
663 /* If we have a pctx then we should try that first */
666 && (pctx
->operation
== EVP_PKEY_OP_VERIFYCTX
667 || pctx
->operation
== EVP_PKEY_OP_SIGNCTX
)
668 && pctx
->op
.sig
.sigprovctx
!= NULL
669 && pctx
->op
.sig
.signature
->settable_ctx_md_params
!= NULL
)
670 return pctx
->op
.sig
.signature
->settable_ctx_md_params(
671 pctx
->op
.sig
.sigprovctx
);
673 if (ctx
->digest
!= NULL
&& ctx
->digest
->settable_ctx_params
!= NULL
)
674 return ctx
->digest
->settable_ctx_params(
675 ossl_provider_ctx(EVP_MD_provider(ctx
->digest
)));
680 int EVP_MD_CTX_get_params(EVP_MD_CTX
*ctx
, OSSL_PARAM params
[])
682 EVP_PKEY_CTX
*pctx
= ctx
->pctx
;
684 /* If we have a pctx then we should try that first */
686 && (pctx
->operation
== EVP_PKEY_OP_VERIFYCTX
687 || pctx
->operation
== EVP_PKEY_OP_SIGNCTX
)
688 && pctx
->op
.sig
.sigprovctx
!= NULL
689 && pctx
->op
.sig
.signature
->get_ctx_md_params
!= NULL
)
690 return pctx
->op
.sig
.signature
->get_ctx_md_params(pctx
->op
.sig
.sigprovctx
,
693 if (ctx
->digest
!= NULL
&& ctx
->digest
->get_params
!= NULL
)
694 return ctx
->digest
->get_ctx_params(ctx
->provctx
, params
);
699 const OSSL_PARAM
*EVP_MD_gettable_ctx_params(const EVP_MD
*md
)
701 if (md
!= NULL
&& md
->gettable_ctx_params
!= NULL
)
702 return md
->gettable_ctx_params(ossl_provider_ctx(EVP_MD_provider(md
)));
706 const OSSL_PARAM
*EVP_MD_CTX_gettable_params(EVP_MD_CTX
*ctx
)
713 /* If we have a pctx then we should try that first */
716 && (pctx
->operation
== EVP_PKEY_OP_VERIFYCTX
717 || pctx
->operation
== EVP_PKEY_OP_SIGNCTX
)
718 && pctx
->op
.sig
.sigprovctx
!= NULL
719 && pctx
->op
.sig
.signature
->gettable_ctx_md_params
!= NULL
)
720 return pctx
->op
.sig
.signature
->gettable_ctx_md_params(
721 pctx
->op
.sig
.sigprovctx
);
723 if (ctx
->digest
!= NULL
724 && ctx
->digest
->gettable_ctx_params
!= NULL
)
725 return ctx
->digest
->gettable_ctx_params(
726 ossl_provider_ctx(EVP_MD_provider(ctx
->digest
)));
731 /* TODO(3.0): Remove legacy code below - only used by engines & DigestSign */
732 int EVP_MD_CTX_ctrl(EVP_MD_CTX
*ctx
, int cmd
, int p1
, void *p2
)
734 int ret
= EVP_CTRL_RET_UNSUPPORTED
;
737 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
740 ERR_raise(ERR_LIB_EVP
, ERR_R_PASSED_NULL_PARAMETER
);
744 if (ctx
->digest
!= NULL
&& ctx
->digest
->prov
== NULL
)
748 case EVP_MD_CTRL_XOF_LEN
:
750 params
[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN
, &sz
);
752 case EVP_MD_CTRL_MICALG
:
754 params
[0] = OSSL_PARAM_construct_utf8_string(OSSL_DIGEST_PARAM_MICALG
,
757 case EVP_CTRL_SSL3_MASTER_SECRET
:
758 params
[0] = OSSL_PARAM_construct_octet_string(OSSL_DIGEST_PARAM_SSL3_MS
,
766 ret
= EVP_MD_CTX_set_params(ctx
, params
);
768 ret
= EVP_MD_CTX_get_params(ctx
, params
);
772 /* TODO(3.0): Remove legacy code below */
774 if (ctx
->digest
->md_ctrl
== NULL
) {
775 ERR_raise(ERR_LIB_EVP
, EVP_R_CTRL_NOT_IMPLEMENTED
);
779 ret
= ctx
->digest
->md_ctrl(ctx
, cmd
, p1
, p2
);
786 EVP_MD
*evp_md_new(void)
788 EVP_MD
*md
= OPENSSL_zalloc(sizeof(*md
));
791 md
->lock
= CRYPTO_THREAD_lock_new();
792 if (md
->lock
== NULL
) {
802 * FIPS module note: since internal fetches will be entirely
803 * provider based, we know that none of its code depends on legacy
804 * NIDs or any functionality that use them.
807 /* TODO(3.x) get rid of the need for legacy NIDs */
808 static void set_legacy_nid(const char *name
, void *vlegacy_nid
)
811 int *legacy_nid
= vlegacy_nid
;
813 * We use lowest level function to get the associated method, because
814 * higher level functions such as EVP_get_digestbyname() have changed
815 * to look at providers too.
817 const void *legacy_method
= OBJ_NAME_get(name
, OBJ_NAME_TYPE_MD_METH
);
819 if (*legacy_nid
== -1) /* We found a clash already */
822 if (legacy_method
== NULL
)
824 nid
= EVP_MD_nid(legacy_method
);
825 if (*legacy_nid
!= NID_undef
&& *legacy_nid
!= nid
) {
833 static void *evp_md_from_dispatch(int name_id
,
834 const OSSL_DISPATCH
*fns
,
840 /* EVP_MD_fetch() will set the legacy NID if available */
841 if ((md
= evp_md_new()) == NULL
) {
842 EVPerr(0, ERR_R_MALLOC_FAILURE
);
847 /* TODO(3.x) get rid of the need for legacy NIDs */
848 md
->type
= NID_undef
;
849 evp_names_do_all(prov
, name_id
, set_legacy_nid
, &md
->type
);
850 if (md
->type
== -1) {
851 ERR_raise(ERR_LIB_EVP
, ERR_R_INTERNAL_ERROR
);
857 md
->name_id
= name_id
;
859 for (; fns
->function_id
!= 0; fns
++) {
860 switch (fns
->function_id
) {
861 case OSSL_FUNC_DIGEST_NEWCTX
:
862 if (md
->newctx
== NULL
) {
863 md
->newctx
= OSSL_FUNC_digest_newctx(fns
);
867 case OSSL_FUNC_DIGEST_INIT
:
868 if (md
->dinit
== NULL
) {
869 md
->dinit
= OSSL_FUNC_digest_init(fns
);
873 case OSSL_FUNC_DIGEST_UPDATE
:
874 if (md
->dupdate
== NULL
) {
875 md
->dupdate
= OSSL_FUNC_digest_update(fns
);
879 case OSSL_FUNC_DIGEST_FINAL
:
880 if (md
->dfinal
== NULL
) {
881 md
->dfinal
= OSSL_FUNC_digest_final(fns
);
885 case OSSL_FUNC_DIGEST_DIGEST
:
886 if (md
->digest
== NULL
)
887 md
->digest
= OSSL_FUNC_digest_digest(fns
);
888 /* We don't increment fnct for this as it is stand alone */
890 case OSSL_FUNC_DIGEST_FREECTX
:
891 if (md
->freectx
== NULL
) {
892 md
->freectx
= OSSL_FUNC_digest_freectx(fns
);
896 case OSSL_FUNC_DIGEST_DUPCTX
:
897 if (md
->dupctx
== NULL
)
898 md
->dupctx
= OSSL_FUNC_digest_dupctx(fns
);
900 case OSSL_FUNC_DIGEST_GET_PARAMS
:
901 if (md
->get_params
== NULL
)
902 md
->get_params
= OSSL_FUNC_digest_get_params(fns
);
904 case OSSL_FUNC_DIGEST_SET_CTX_PARAMS
:
905 if (md
->set_ctx_params
== NULL
)
906 md
->set_ctx_params
= OSSL_FUNC_digest_set_ctx_params(fns
);
908 case OSSL_FUNC_DIGEST_GET_CTX_PARAMS
:
909 if (md
->get_ctx_params
== NULL
)
910 md
->get_ctx_params
= OSSL_FUNC_digest_get_ctx_params(fns
);
912 case OSSL_FUNC_DIGEST_GETTABLE_PARAMS
:
913 if (md
->gettable_params
== NULL
)
914 md
->gettable_params
= OSSL_FUNC_digest_gettable_params(fns
);
916 case OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS
:
917 if (md
->settable_ctx_params
== NULL
)
918 md
->settable_ctx_params
=
919 OSSL_FUNC_digest_settable_ctx_params(fns
);
921 case OSSL_FUNC_DIGEST_GETTABLE_CTX_PARAMS
:
922 if (md
->gettable_ctx_params
== NULL
)
923 md
->gettable_ctx_params
=
924 OSSL_FUNC_digest_gettable_ctx_params(fns
);
928 if ((fncnt
!= 0 && fncnt
!= 5)
929 || (fncnt
== 0 && md
->digest
== NULL
)) {
931 * In order to be a consistent set of functions we either need the
932 * whole set of init/update/final etc functions or none of them.
933 * The "digest" function can standalone. We at least need one way to
937 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_PROVIDER_FUNCTIONS
);
942 ossl_provider_up_ref(prov
);
947 static int evp_md_up_ref(void *md
)
949 return EVP_MD_up_ref(md
);
952 static void evp_md_free(void *md
)
957 EVP_MD
*EVP_MD_fetch(OSSL_LIB_CTX
*ctx
, const char *algorithm
,
958 const char *properties
)
961 evp_generic_fetch(ctx
, OSSL_OP_DIGEST
, algorithm
, properties
,
962 evp_md_from_dispatch
, evp_md_up_ref
, evp_md_free
);
967 int EVP_MD_up_ref(EVP_MD
*md
)
971 CRYPTO_UP_REF(&md
->refcnt
, &ref
, md
->lock
);
975 void EVP_MD_free(EVP_MD
*md
)
982 CRYPTO_DOWN_REF(&md
->refcnt
, &i
, md
->lock
);
985 ossl_provider_free(md
->prov
);
986 CRYPTO_THREAD_lock_free(md
->lock
);
990 void EVP_MD_do_all_provided(OSSL_LIB_CTX
*libctx
,
991 void (*fn
)(EVP_MD
*mac
, void *arg
),
994 evp_generic_do_all(libctx
, OSSL_OP_DIGEST
,
995 (void (*)(void *, void *))fn
, arg
,
996 evp_md_from_dispatch
, evp_md_free
);