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_with_libctx(EVP_PKEY
*pkey
,
86 const ASN1_OCTET_STRING
*id
,
87 OPENSSL_CTX
*libctx
, const char *propq
)
90 EVP_PKEY_CTX
*pctx
= NULL
;
92 if ((ctx
= EVP_MD_CTX_new()) == NULL
93 || (pctx
= EVP_PKEY_CTX_new_from_pkey(libctx
, pkey
, propq
)) == NULL
) {
94 ASN1err(0, ERR_R_MALLOC_FAILURE
);
98 # ifndef OPENSSL_NO_EC
99 if (id
!= NULL
&& EVP_PKEY_CTX_set1_id(pctx
, id
->data
, id
->length
) <= 0) {
100 ASN1err(0, ERR_R_MALLOC_FAILURE
);
105 EVP_MD_CTX_set_pkey_ctx(ctx
, pctx
);
109 EVP_PKEY_CTX_free(pctx
);
110 EVP_MD_CTX_free(ctx
);
115 EVP_MD_CTX
*EVP_MD_CTX_new(void)
117 return OPENSSL_zalloc(sizeof(EVP_MD_CTX
));
120 void EVP_MD_CTX_free(EVP_MD_CTX
*ctx
)
125 EVP_MD_CTX_reset(ctx
);
131 int EVP_DigestInit(EVP_MD_CTX
*ctx
, const EVP_MD
*type
)
133 EVP_MD_CTX_reset(ctx
);
134 return EVP_DigestInit_ex(ctx
, type
, NULL
);
137 int EVP_DigestInit_ex(EVP_MD_CTX
*ctx
, const EVP_MD
*type
, ENGINE
*impl
)
139 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
140 ENGINE
*tmpimpl
= NULL
;
143 EVP_MD_CTX_clear_flags(ctx
, EVP_MD_CTX_FLAG_CLEANED
);
145 if (ctx
->provctx
!= NULL
) {
146 if (!ossl_assert(ctx
->digest
!= NULL
)) {
147 EVPerr(EVP_F_EVP_DIGESTINIT_EX
, EVP_R_INITIALIZATION_ERROR
);
150 if (ctx
->digest
->freectx
!= NULL
)
151 ctx
->digest
->freectx(ctx
->provctx
);
156 ctx
->reqdigest
= type
;
158 /* TODO(3.0): Legacy work around code below. Remove this */
159 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
161 * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
162 * this context may already have an ENGINE! Try to avoid releasing the
163 * previous handle, re-querying for an ENGINE, and having a
164 * reinitialisation, when it may all be unnecessary.
166 if (ctx
->engine
&& ctx
->digest
&&
167 (type
== NULL
|| (type
->type
== ctx
->digest
->type
)))
172 * Ensure an ENGINE left lying around from last time is cleared (the
173 * previous check attempted to avoid this if the same ENGINE and
174 * EVP_MD could be used).
176 ENGINE_finish(ctx
->engine
);
180 if (type
!= NULL
&& impl
== NULL
)
181 tmpimpl
= ENGINE_get_digest_engine(type
->type
);
185 * If there are engines involved or EVP_MD_CTX_FLAG_NO_INIT is set then we
186 * should use legacy handling for now.
188 if (ctx
->engine
!= NULL
190 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
193 || (ctx
->flags
& EVP_MD_CTX_FLAG_NO_INIT
) != 0) {
194 if (ctx
->digest
== ctx
->fetched_digest
)
196 EVP_MD_free(ctx
->fetched_digest
);
197 ctx
->fetched_digest
= NULL
;
201 if (ctx
->digest
!= NULL
&& ctx
->digest
->ctx_size
> 0) {
202 OPENSSL_clear_free(ctx
->md_data
, ctx
->digest
->ctx_size
);
206 /* TODO(3.0): Start of non-legacy code below */
208 if (type
->prov
== NULL
) {
210 /* We only do explicit fetches inside the FIPS module */
211 EVPerr(EVP_F_EVP_DIGESTINIT_EX
, EVP_R_INITIALIZATION_ERROR
);
214 EVP_MD
*provmd
= EVP_MD_fetch(NULL
, OBJ_nid2sn(type
->type
), "");
219 EVP_MD_free(ctx
->fetched_digest
);
220 ctx
->fetched_digest
= provmd
;
224 if (ctx
->provctx
!= NULL
&& ctx
->digest
!= NULL
&& ctx
->digest
!= type
) {
225 if (ctx
->digest
->freectx
!= NULL
)
226 ctx
->digest
->freectx(ctx
->provctx
);
230 if (ctx
->provctx
== NULL
) {
231 ctx
->provctx
= ctx
->digest
->newctx(ossl_provider_ctx(type
->prov
));
232 if (ctx
->provctx
== NULL
) {
233 EVPerr(EVP_F_EVP_DIGESTINIT_EX
, EVP_R_INITIALIZATION_ERROR
);
238 if (ctx
->digest
->dinit
== NULL
) {
239 EVPerr(EVP_F_EVP_DIGESTINIT_EX
, EVP_R_INITIALIZATION_ERROR
);
243 return ctx
->digest
->dinit(ctx
->provctx
);
245 /* TODO(3.0): Remove legacy code below */
248 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
251 if (!ENGINE_init(impl
)) {
252 EVPerr(EVP_F_EVP_DIGESTINIT_EX
, EVP_R_INITIALIZATION_ERROR
);
256 /* Ask if an ENGINE is reserved for this job */
260 /* There's an ENGINE for this job ... (apparently) */
261 const EVP_MD
*d
= ENGINE_get_digest(impl
, type
->type
);
264 EVPerr(EVP_F_EVP_DIGESTINIT_EX
, EVP_R_INITIALIZATION_ERROR
);
268 /* We'll use the ENGINE's private digest definition */
271 * Store the ENGINE functional reference so we know 'type' came
272 * from an ENGINE and we need to release it when done.
279 EVPerr(EVP_F_EVP_DIGESTINIT_EX
, EVP_R_NO_DIGEST_SET
);
285 if (ctx
->digest
!= type
) {
286 if (ctx
->digest
&& ctx
->digest
->ctx_size
) {
287 OPENSSL_clear_free(ctx
->md_data
, ctx
->digest
->ctx_size
);
291 if (!(ctx
->flags
& EVP_MD_CTX_FLAG_NO_INIT
) && type
->ctx_size
) {
292 ctx
->update
= type
->update
;
293 ctx
->md_data
= OPENSSL_zalloc(type
->ctx_size
);
294 if (ctx
->md_data
== NULL
) {
295 EVPerr(EVP_F_EVP_DIGESTINIT_EX
, ERR_R_MALLOC_FAILURE
);
300 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
305 * TODO(3.0): Temporarily no support for EVP_DigestSign* inside FIPS module
306 * or when using providers.
308 if (ctx
->pctx
!= NULL
309 && (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
->pctx
)
310 || ctx
->pctx
->op
.sig
.signature
== NULL
)) {
312 r
= EVP_PKEY_CTX_ctrl(ctx
->pctx
, -1, EVP_PKEY_OP_TYPE_SIG
,
313 EVP_PKEY_CTRL_DIGESTINIT
, 0, ctx
);
314 if (r
<= 0 && (r
!= -2))
318 if (ctx
->flags
& EVP_MD_CTX_FLAG_NO_INIT
)
320 return ctx
->digest
->init(ctx
);
323 int EVP_DigestUpdate(EVP_MD_CTX
*ctx
, const void *data
, size_t count
)
328 if (ctx
->pctx
!= NULL
329 && EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
->pctx
)
330 && ctx
->pctx
->op
.sig
.sigprovctx
!= NULL
) {
332 * Prior to OpenSSL 3.0 EVP_DigestSignUpdate() and
333 * EVP_DigestVerifyUpdate() were just macros for EVP_DigestUpdate().
334 * Some code calls EVP_DigestUpdate() directly even when initialised
335 * with EVP_DigestSignInit_with_libctx() or
336 * EVP_DigestVerifyInit_with_libctx(), so we detect that and redirect to
337 * the correct EVP_Digest*Update() function
339 if (ctx
->pctx
->operation
== EVP_PKEY_OP_SIGNCTX
)
340 return EVP_DigestSignUpdate(ctx
, data
, count
);
341 if (ctx
->pctx
->operation
== EVP_PKEY_OP_VERIFYCTX
)
342 return EVP_DigestVerifyUpdate(ctx
, data
, count
);
343 EVPerr(EVP_F_EVP_DIGESTUPDATE
, EVP_R_UPDATE_ERROR
);
347 if (ctx
->digest
== NULL
348 || ctx
->digest
->prov
== NULL
349 || (ctx
->flags
& EVP_MD_CTX_FLAG_NO_INIT
) != 0)
352 if (ctx
->digest
->dupdate
== NULL
) {
353 EVPerr(EVP_F_EVP_DIGESTUPDATE
, EVP_R_UPDATE_ERROR
);
356 return ctx
->digest
->dupdate(ctx
->provctx
, data
, count
);
358 /* TODO(3.0): Remove legacy code below */
360 return ctx
->update(ctx
, data
, count
);
363 /* The caller can assume that this removes any secret data from the context */
364 int EVP_DigestFinal(EVP_MD_CTX
*ctx
, unsigned char *md
, unsigned int *size
)
367 ret
= EVP_DigestFinal_ex(ctx
, md
, size
);
368 EVP_MD_CTX_reset(ctx
);
372 /* The caller can assume that this removes any secret data from the context */
373 int EVP_DigestFinal_ex(EVP_MD_CTX
*ctx
, unsigned char *md
, unsigned int *isize
)
379 if (ctx
->digest
== NULL
)
382 sz
= EVP_MD_size(ctx
->digest
);
386 if (ctx
->digest
->prov
== NULL
)
389 if (ctx
->digest
->dfinal
== NULL
) {
390 EVPerr(EVP_F_EVP_DIGESTFINAL_EX
, EVP_R_FINAL_ERROR
);
394 ret
= ctx
->digest
->dfinal(ctx
->provctx
, md
, &size
, mdsize
);
397 if (size
<= UINT_MAX
) {
400 EVPerr(EVP_F_EVP_DIGESTFINAL_EX
, EVP_R_FINAL_ERROR
);
407 /* TODO(3.0): Remove legacy code below */
409 OPENSSL_assert(mdsize
<= EVP_MAX_MD_SIZE
);
410 ret
= ctx
->digest
->final(ctx
, md
);
413 if (ctx
->digest
->cleanup
) {
414 ctx
->digest
->cleanup(ctx
);
415 EVP_MD_CTX_set_flags(ctx
, EVP_MD_CTX_FLAG_CLEANED
);
417 OPENSSL_cleanse(ctx
->md_data
, ctx
->digest
->ctx_size
);
421 int EVP_DigestFinalXOF(EVP_MD_CTX
*ctx
, unsigned char *md
, size_t size
)
424 OSSL_PARAM params
[2];
427 if (ctx
->digest
== NULL
|| ctx
->digest
->prov
== NULL
)
430 if (ctx
->digest
->dfinal
== NULL
) {
431 EVPerr(EVP_F_EVP_DIGESTFINALXOF
, EVP_R_FINAL_ERROR
);
435 params
[i
++] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN
, &size
);
436 params
[i
++] = OSSL_PARAM_construct_end();
438 if (EVP_MD_CTX_set_params(ctx
, params
) > 0)
439 ret
= ctx
->digest
->dfinal(ctx
->provctx
, md
, &size
, size
);
440 EVP_MD_CTX_reset(ctx
);
444 if (ctx
->digest
->flags
& EVP_MD_FLAG_XOF
446 && ctx
->digest
->md_ctrl(ctx
, EVP_MD_CTRL_XOF_LEN
, (int)size
, NULL
)) {
447 ret
= ctx
->digest
->final(ctx
, md
);
448 if (ctx
->digest
->cleanup
!= NULL
) {
449 ctx
->digest
->cleanup(ctx
);
450 EVP_MD_CTX_set_flags(ctx
, EVP_MD_CTX_FLAG_CLEANED
);
452 OPENSSL_cleanse(ctx
->md_data
, ctx
->digest
->ctx_size
);
454 EVPerr(EVP_F_EVP_DIGESTFINALXOF
, EVP_R_NOT_XOF_OR_INVALID_LENGTH
);
460 int EVP_MD_CTX_copy(EVP_MD_CTX
*out
, const EVP_MD_CTX
*in
)
462 EVP_MD_CTX_reset(out
);
463 return EVP_MD_CTX_copy_ex(out
, in
);
466 int EVP_MD_CTX_copy_ex(EVP_MD_CTX
*out
, const EVP_MD_CTX
*in
)
468 unsigned char *tmp_buf
;
470 if (in
== NULL
|| in
->digest
== NULL
) {
471 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX
, EVP_R_INPUT_NOT_INITIALIZED
);
475 if (in
->digest
->prov
== NULL
476 || (in
->flags
& EVP_MD_CTX_FLAG_NO_INIT
) != 0)
479 if (in
->digest
->dupctx
== NULL
) {
480 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX
, EVP_R_NOT_ABLE_TO_COPY_CTX
);
484 EVP_MD_CTX_reset(out
);
485 if (out
->fetched_digest
!= NULL
)
486 EVP_MD_free(out
->fetched_digest
);
488 /* NULL out pointers in case of error */
492 if (in
->fetched_digest
!= NULL
)
493 EVP_MD_up_ref(in
->fetched_digest
);
495 if (in
->provctx
!= NULL
) {
496 out
->provctx
= in
->digest
->dupctx(in
->provctx
);
497 if (out
->provctx
== NULL
) {
498 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX
, EVP_R_NOT_ABLE_TO_COPY_CTX
);
503 /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
504 EVP_MD_CTX_clear_flags(out
, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX
);
506 /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
507 if (in
->pctx
!= NULL
) {
508 out
->pctx
= EVP_PKEY_CTX_dup(in
->pctx
);
509 if (out
->pctx
== NULL
) {
510 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX
, EVP_R_NOT_ABLE_TO_COPY_CTX
);
511 EVP_MD_CTX_reset(out
);
519 /* TODO(3.0): Remove legacy code below */
521 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
522 /* Make sure it's safe to copy a digest context using an ENGINE */
523 if (in
->engine
&& !ENGINE_init(in
->engine
)) {
524 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX
, ERR_R_ENGINE_LIB
);
529 if (out
->digest
== in
->digest
) {
530 tmp_buf
= out
->md_data
;
531 EVP_MD_CTX_set_flags(out
, EVP_MD_CTX_FLAG_REUSE
);
534 EVP_MD_CTX_reset(out
);
535 memcpy(out
, in
, sizeof(*out
));
537 /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
538 EVP_MD_CTX_clear_flags(out
, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX
);
540 /* Null these variables, since they are getting fixed up
541 * properly below. Anything else may cause a memleak and/or
542 * double free if any of the memory allocations below fail
547 if (in
->md_data
&& out
->digest
->ctx_size
) {
549 out
->md_data
= tmp_buf
;
551 out
->md_data
= OPENSSL_malloc(out
->digest
->ctx_size
);
552 if (out
->md_data
== NULL
) {
553 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX
, ERR_R_MALLOC_FAILURE
);
557 memcpy(out
->md_data
, in
->md_data
, out
->digest
->ctx_size
);
560 out
->update
= in
->update
;
563 /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
565 out
->pctx
= EVP_PKEY_CTX_dup(in
->pctx
);
567 EVP_MD_CTX_reset(out
);
573 if (out
->digest
->copy
)
574 return out
->digest
->copy(out
, in
);
579 int EVP_Digest(const void *data
, size_t count
,
580 unsigned char *md
, unsigned int *size
, const EVP_MD
*type
,
583 EVP_MD_CTX
*ctx
= EVP_MD_CTX_new();
588 EVP_MD_CTX_set_flags(ctx
, EVP_MD_CTX_FLAG_ONESHOT
);
589 ret
= EVP_DigestInit_ex(ctx
, type
, impl
)
590 && EVP_DigestUpdate(ctx
, data
, count
)
591 && EVP_DigestFinal_ex(ctx
, md
, size
);
592 EVP_MD_CTX_free(ctx
);
597 int EVP_MD_get_params(const EVP_MD
*digest
, OSSL_PARAM params
[])
599 if (digest
!= NULL
&& digest
->get_params
!= NULL
)
600 return digest
->get_params(params
);
604 const OSSL_PARAM
*EVP_MD_gettable_params(const EVP_MD
*digest
)
606 if (digest
!= NULL
&& digest
->gettable_params
!= NULL
)
607 return digest
->gettable_params(
608 ossl_provider_ctx(EVP_MD_provider(digest
)));
612 int EVP_MD_CTX_set_params(EVP_MD_CTX
*ctx
, const OSSL_PARAM params
[])
614 EVP_PKEY_CTX
*pctx
= ctx
->pctx
;
616 /* If we have a pctx then we should try that first */
618 && (pctx
->operation
== EVP_PKEY_OP_VERIFYCTX
619 || pctx
->operation
== EVP_PKEY_OP_SIGNCTX
)
620 && pctx
->op
.sig
.sigprovctx
!= NULL
621 && pctx
->op
.sig
.signature
->set_ctx_md_params
!= NULL
)
622 return pctx
->op
.sig
.signature
->set_ctx_md_params(pctx
->op
.sig
.sigprovctx
,
625 if (ctx
->digest
!= NULL
&& ctx
->digest
->set_ctx_params
!= NULL
)
626 return ctx
->digest
->set_ctx_params(ctx
->provctx
, params
);
631 const OSSL_PARAM
*EVP_MD_settable_ctx_params(const EVP_MD
*md
)
633 if (md
!= NULL
&& md
->settable_ctx_params
!= NULL
)
634 return md
->settable_ctx_params(ossl_provider_ctx(EVP_MD_provider(md
)));
638 const OSSL_PARAM
*EVP_MD_CTX_settable_params(EVP_MD_CTX
*ctx
)
645 /* If we have a pctx then we should try that first */
648 && (pctx
->operation
== EVP_PKEY_OP_VERIFYCTX
649 || pctx
->operation
== EVP_PKEY_OP_SIGNCTX
)
650 && pctx
->op
.sig
.sigprovctx
!= NULL
651 && pctx
->op
.sig
.signature
->settable_ctx_md_params
!= NULL
)
652 return pctx
->op
.sig
.signature
->settable_ctx_md_params(
653 pctx
->op
.sig
.sigprovctx
);
655 if (ctx
->digest
!= NULL
&& ctx
->digest
->settable_ctx_params
!= NULL
)
656 return ctx
->digest
->settable_ctx_params(
657 ossl_provider_ctx(EVP_MD_provider(ctx
->digest
)));
662 int EVP_MD_CTX_get_params(EVP_MD_CTX
*ctx
, OSSL_PARAM params
[])
664 EVP_PKEY_CTX
*pctx
= ctx
->pctx
;
666 /* If we have a pctx then we should try that first */
668 && (pctx
->operation
== EVP_PKEY_OP_VERIFYCTX
669 || pctx
->operation
== EVP_PKEY_OP_SIGNCTX
)
670 && pctx
->op
.sig
.sigprovctx
!= NULL
671 && pctx
->op
.sig
.signature
->get_ctx_md_params
!= NULL
)
672 return pctx
->op
.sig
.signature
->get_ctx_md_params(pctx
->op
.sig
.sigprovctx
,
675 if (ctx
->digest
!= NULL
&& ctx
->digest
->get_params
!= NULL
)
676 return ctx
->digest
->get_ctx_params(ctx
->provctx
, params
);
681 const OSSL_PARAM
*EVP_MD_gettable_ctx_params(const EVP_MD
*md
)
683 if (md
!= NULL
&& md
->gettable_ctx_params
!= NULL
)
684 return md
->gettable_ctx_params(ossl_provider_ctx(EVP_MD_provider(md
)));
688 const OSSL_PARAM
*EVP_MD_CTX_gettable_params(EVP_MD_CTX
*ctx
)
695 /* If we have a pctx then we should try that first */
698 && (pctx
->operation
== EVP_PKEY_OP_VERIFYCTX
699 || pctx
->operation
== EVP_PKEY_OP_SIGNCTX
)
700 && pctx
->op
.sig
.sigprovctx
!= NULL
701 && pctx
->op
.sig
.signature
->gettable_ctx_md_params
!= NULL
)
702 return pctx
->op
.sig
.signature
->gettable_ctx_md_params(
703 pctx
->op
.sig
.sigprovctx
);
705 if (ctx
->digest
!= NULL
706 && ctx
->digest
->gettable_ctx_params
!= NULL
)
707 return ctx
->digest
->gettable_ctx_params(
708 ossl_provider_ctx(EVP_MD_provider(ctx
->digest
)));
713 /* TODO(3.0): Remove legacy code below - only used by engines & DigestSign */
714 int EVP_MD_CTX_ctrl(EVP_MD_CTX
*ctx
, int cmd
, int p1
, void *p2
)
716 int ret
= EVP_CTRL_RET_UNSUPPORTED
;
719 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
722 ERR_raise(ERR_LIB_EVP
, ERR_R_PASSED_NULL_PARAMETER
);
726 if (ctx
->digest
!= NULL
&& ctx
->digest
->prov
== NULL
)
730 case EVP_MD_CTRL_XOF_LEN
:
732 params
[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN
, &sz
);
734 case EVP_MD_CTRL_MICALG
:
736 params
[0] = OSSL_PARAM_construct_utf8_string(OSSL_DIGEST_PARAM_MICALG
,
739 case EVP_CTRL_SSL3_MASTER_SECRET
:
740 params
[0] = OSSL_PARAM_construct_octet_string(OSSL_DIGEST_PARAM_SSL3_MS
,
748 ret
= EVP_MD_CTX_set_params(ctx
, params
);
750 ret
= EVP_MD_CTX_get_params(ctx
, params
);
754 /* TODO(3.0): Remove legacy code below */
756 if (ctx
->digest
->md_ctrl
== NULL
) {
757 ERR_raise(ERR_LIB_EVP
, EVP_R_CTRL_NOT_IMPLEMENTED
);
761 ret
= ctx
->digest
->md_ctrl(ctx
, cmd
, p1
, p2
);
768 EVP_MD
*evp_md_new(void)
770 EVP_MD
*md
= OPENSSL_zalloc(sizeof(*md
));
773 md
->lock
= CRYPTO_THREAD_lock_new();
774 if (md
->lock
== NULL
) {
784 * FIPS module note: since internal fetches will be entirely
785 * provider based, we know that none of its code depends on legacy
786 * NIDs or any functionality that use them.
789 /* TODO(3.x) get rid of the need for legacy NIDs */
790 static void set_legacy_nid(const char *name
, void *vlegacy_nid
)
793 int *legacy_nid
= vlegacy_nid
;
795 * We use lowest level function to get the associated method, because
796 * higher level functions such as EVP_get_digestbyname() have changed
797 * to look at providers too.
799 const void *legacy_method
= OBJ_NAME_get(name
, OBJ_NAME_TYPE_MD_METH
);
801 if (*legacy_nid
== -1) /* We found a clash already */
804 if (legacy_method
== NULL
)
806 nid
= EVP_MD_nid(legacy_method
);
807 if (*legacy_nid
!= NID_undef
&& *legacy_nid
!= nid
) {
815 static void *evp_md_from_dispatch(int name_id
,
816 const OSSL_DISPATCH
*fns
,
822 /* EVP_MD_fetch() will set the legacy NID if available */
823 if ((md
= evp_md_new()) == NULL
) {
824 EVPerr(0, ERR_R_MALLOC_FAILURE
);
829 /* TODO(3.x) get rid of the need for legacy NIDs */
830 md
->type
= NID_undef
;
831 evp_names_do_all(prov
, name_id
, set_legacy_nid
, &md
->type
);
832 if (md
->type
== -1) {
833 ERR_raise(ERR_LIB_EVP
, ERR_R_INTERNAL_ERROR
);
839 md
->name_id
= name_id
;
841 for (; fns
->function_id
!= 0; fns
++) {
842 switch (fns
->function_id
) {
843 case OSSL_FUNC_DIGEST_NEWCTX
:
844 if (md
->newctx
== NULL
) {
845 md
->newctx
= OSSL_FUNC_digest_newctx(fns
);
849 case OSSL_FUNC_DIGEST_INIT
:
850 if (md
->dinit
== NULL
) {
851 md
->dinit
= OSSL_FUNC_digest_init(fns
);
855 case OSSL_FUNC_DIGEST_UPDATE
:
856 if (md
->dupdate
== NULL
) {
857 md
->dupdate
= OSSL_FUNC_digest_update(fns
);
861 case OSSL_FUNC_DIGEST_FINAL
:
862 if (md
->dfinal
== NULL
) {
863 md
->dfinal
= OSSL_FUNC_digest_final(fns
);
867 case OSSL_FUNC_DIGEST_DIGEST
:
868 if (md
->digest
== NULL
)
869 md
->digest
= OSSL_FUNC_digest_digest(fns
);
870 /* We don't increment fnct for this as it is stand alone */
872 case OSSL_FUNC_DIGEST_FREECTX
:
873 if (md
->freectx
== NULL
) {
874 md
->freectx
= OSSL_FUNC_digest_freectx(fns
);
878 case OSSL_FUNC_DIGEST_DUPCTX
:
879 if (md
->dupctx
== NULL
)
880 md
->dupctx
= OSSL_FUNC_digest_dupctx(fns
);
882 case OSSL_FUNC_DIGEST_GET_PARAMS
:
883 if (md
->get_params
== NULL
)
884 md
->get_params
= OSSL_FUNC_digest_get_params(fns
);
886 case OSSL_FUNC_DIGEST_SET_CTX_PARAMS
:
887 if (md
->set_ctx_params
== NULL
)
888 md
->set_ctx_params
= OSSL_FUNC_digest_set_ctx_params(fns
);
890 case OSSL_FUNC_DIGEST_GET_CTX_PARAMS
:
891 if (md
->get_ctx_params
== NULL
)
892 md
->get_ctx_params
= OSSL_FUNC_digest_get_ctx_params(fns
);
894 case OSSL_FUNC_DIGEST_GETTABLE_PARAMS
:
895 if (md
->gettable_params
== NULL
)
896 md
->gettable_params
= OSSL_FUNC_digest_gettable_params(fns
);
898 case OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS
:
899 if (md
->settable_ctx_params
== NULL
)
900 md
->settable_ctx_params
=
901 OSSL_FUNC_digest_settable_ctx_params(fns
);
903 case OSSL_FUNC_DIGEST_GETTABLE_CTX_PARAMS
:
904 if (md
->gettable_ctx_params
== NULL
)
905 md
->gettable_ctx_params
=
906 OSSL_FUNC_digest_gettable_ctx_params(fns
);
910 if ((fncnt
!= 0 && fncnt
!= 5)
911 || (fncnt
== 0 && md
->digest
== NULL
)) {
913 * In order to be a consistent set of functions we either need the
914 * whole set of init/update/final etc functions or none of them.
915 * The "digest" function can standalone. We at least need one way to
919 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_PROVIDER_FUNCTIONS
);
924 ossl_provider_up_ref(prov
);
929 static int evp_md_up_ref(void *md
)
931 return EVP_MD_up_ref(md
);
934 static void evp_md_free(void *md
)
939 EVP_MD
*EVP_MD_fetch(OPENSSL_CTX
*ctx
, const char *algorithm
,
940 const char *properties
)
943 evp_generic_fetch(ctx
, OSSL_OP_DIGEST
, algorithm
, properties
,
944 evp_md_from_dispatch
, evp_md_up_ref
, evp_md_free
);
949 int EVP_MD_up_ref(EVP_MD
*md
)
953 CRYPTO_UP_REF(&md
->refcnt
, &ref
, md
->lock
);
957 void EVP_MD_free(EVP_MD
*md
)
964 CRYPTO_DOWN_REF(&md
->refcnt
, &i
, md
->lock
);
967 ossl_provider_free(md
->prov
);
968 CRYPTO_THREAD_lock_free(md
->lock
);
972 void EVP_MD_do_all_provided(OPENSSL_CTX
*libctx
,
973 void (*fn
)(EVP_MD
*mac
, void *arg
),
976 evp_generic_do_all(libctx
, OSSL_OP_DIGEST
,
977 (void (*)(void *, void *))fn
, arg
,
978 evp_md_from_dispatch
, evp_md_free
);