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>
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 if (ctx
->provctx
!= NULL
) {
29 if (ctx
->digest
!= NULL
&& ctx
->digest
->freectx
!= NULL
)
30 ctx
->digest
->freectx(ctx
->provctx
);
32 EVP_MD_CTX_set_flags(ctx
, EVP_MD_CTX_FLAG_CLEANED
);
35 /* Code below to be removed when legacy support is dropped. */
38 * Don't assume ctx->md_data was cleaned in EVP_Digest_Final, because
39 * sometimes only copies of the context are ever finalised.
41 if (ctx
->digest
&& ctx
->digest
->cleanup
42 && !EVP_MD_CTX_test_flags(ctx
, EVP_MD_CTX_FLAG_CLEANED
))
43 ctx
->digest
->cleanup(ctx
);
44 if (ctx
->digest
&& ctx
->digest
->ctx_size
&& ctx
->md_data
45 && (!EVP_MD_CTX_test_flags(ctx
, EVP_MD_CTX_FLAG_REUSE
) || force
))
46 OPENSSL_clear_free(ctx
->md_data
, ctx
->digest
->ctx_size
);
50 #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
51 ENGINE_finish(ctx
->engine
);
55 /* Non legacy code, this has to be later than the ctx->digest cleaning */
56 EVP_MD_free(ctx
->fetched_digest
);
57 ctx
->fetched_digest
= NULL
;
58 ctx
->reqdigest
= NULL
;
61 /* This call frees resources associated with the context */
62 int EVP_MD_CTX_reset(EVP_MD_CTX
*ctx
)
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
);
124 static int evp_md_init_internal(EVP_MD_CTX
*ctx
, const EVP_MD
*type
,
125 const OSSL_PARAM params
[], ENGINE
*impl
)
127 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
128 ENGINE
*tmpimpl
= NULL
;
131 #if !defined(FIPS_MODULE)
132 if (ctx
->pctx
!= NULL
133 && EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
->pctx
)
134 && ctx
->pctx
->op
.sig
.sigprovctx
!= NULL
) {
136 * Prior to OpenSSL 3.0 calling EVP_DigestInit_ex() on an mdctx
137 * previously initialised with EVP_DigestSignInit() would retain
138 * information about the key, and re-initialise for another sign
139 * operation. So in that case we redirect to EVP_DigestSignInit()
141 if (ctx
->pctx
->operation
== EVP_PKEY_OP_SIGNCTX
)
142 return EVP_DigestSignInit(ctx
, NULL
, type
, impl
, NULL
);
143 if (ctx
->pctx
->operation
== EVP_PKEY_OP_VERIFYCTX
)
144 return EVP_DigestVerifyInit(ctx
, NULL
, type
, impl
, NULL
);
145 ERR_raise(ERR_LIB_EVP
, EVP_R_UPDATE_ERROR
);
150 EVP_MD_CTX_clear_flags(ctx
, EVP_MD_CTX_FLAG_CLEANED
);
152 if (ctx
->provctx
!= NULL
) {
153 if (!ossl_assert(ctx
->digest
!= NULL
)) {
154 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
157 if (ctx
->digest
->freectx
!= NULL
)
158 ctx
->digest
->freectx(ctx
->provctx
);
163 ctx
->reqdigest
= type
;
165 if (ctx
->digest
== NULL
) {
166 ERR_raise(ERR_LIB_EVP
, EVP_R_NO_DIGEST_SET
);
172 /* Code below to be removed when legacy support is dropped. */
173 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
175 * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
176 * this context may already have an ENGINE! Try to avoid releasing the
177 * previous handle, re-querying for an ENGINE, and having a
178 * reinitialisation, when it may all be unnecessary.
180 if (ctx
->engine
&& ctx
->digest
&&
181 (type
== NULL
|| (type
->type
== ctx
->digest
->type
)))
186 * Ensure an ENGINE left lying around from last time is cleared (the
187 * previous check attempted to avoid this if the same ENGINE and
188 * EVP_MD could be used).
190 ENGINE_finish(ctx
->engine
);
194 if (type
!= NULL
&& impl
== NULL
)
195 tmpimpl
= ENGINE_get_digest_engine(type
->type
);
199 * If there are engines involved or EVP_MD_CTX_FLAG_NO_INIT is set then we
200 * should use legacy handling for now.
202 if (ctx
->engine
!= NULL
204 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
207 || (ctx
->flags
& EVP_MD_CTX_FLAG_NO_INIT
) != 0) {
208 if (ctx
->digest
== ctx
->fetched_digest
)
210 EVP_MD_free(ctx
->fetched_digest
);
211 ctx
->fetched_digest
= NULL
;
215 if (ctx
->digest
!= NULL
&& ctx
->digest
->ctx_size
> 0) {
216 OPENSSL_clear_free(ctx
->md_data
, ctx
->digest
->ctx_size
);
220 /* Start of non-legacy code below */
222 if (type
->prov
== NULL
) {
224 /* We only do explicit fetches inside the FIPS module */
225 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
228 EVP_MD
*provmd
= EVP_MD_fetch(NULL
, OBJ_nid2sn(type
->type
), "");
230 if (provmd
== NULL
) {
231 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
235 EVP_MD_free(ctx
->fetched_digest
);
236 ctx
->fetched_digest
= provmd
;
240 if (ctx
->provctx
!= NULL
&& ctx
->digest
!= NULL
&& ctx
->digest
!= type
) {
241 if (ctx
->digest
->freectx
!= NULL
)
242 ctx
->digest
->freectx(ctx
->provctx
);
245 if (type
->prov
!= NULL
&& ctx
->fetched_digest
!= type
) {
246 if (!EVP_MD_up_ref((EVP_MD
*)type
)) {
247 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
250 EVP_MD_free(ctx
->fetched_digest
);
251 ctx
->fetched_digest
= (EVP_MD
*)type
;
254 if (ctx
->provctx
== NULL
) {
255 ctx
->provctx
= ctx
->digest
->newctx(ossl_provider_ctx(type
->prov
));
256 if (ctx
->provctx
== NULL
) {
257 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
262 if (ctx
->digest
->dinit
== NULL
) {
263 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
267 return ctx
->digest
->dinit(ctx
->provctx
, params
);
269 /* Code below to be removed when legacy support is dropped. */
272 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
275 if (!ENGINE_init(impl
)) {
276 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
280 /* Ask if an ENGINE is reserved for this job */
284 /* There's an ENGINE for this job ... (apparently) */
285 const EVP_MD
*d
= ENGINE_get_digest(impl
, type
->type
);
288 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
292 /* We'll use the ENGINE's private digest definition */
295 * Store the ENGINE functional reference so we know 'type' came
296 * from an ENGINE and we need to release it when done.
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 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
318 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
322 if (ctx
->pctx
!= NULL
323 && (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
->pctx
)
324 || ctx
->pctx
->op
.sig
.signature
== NULL
)) {
326 r
= EVP_PKEY_CTX_ctrl(ctx
->pctx
, -1, EVP_PKEY_OP_TYPE_SIG
,
327 EVP_PKEY_CTRL_DIGESTINIT
, 0, ctx
);
328 if (r
<= 0 && (r
!= -2))
332 if (ctx
->flags
& EVP_MD_CTX_FLAG_NO_INIT
)
334 return ctx
->digest
->init(ctx
);
337 int EVP_DigestInit_ex2(EVP_MD_CTX
*ctx
, const EVP_MD
*type
,
338 const OSSL_PARAM params
[])
340 return evp_md_init_internal(ctx
, type
, params
, NULL
);
343 int EVP_DigestInit(EVP_MD_CTX
*ctx
, const EVP_MD
*type
)
345 EVP_MD_CTX_reset(ctx
);
346 return evp_md_init_internal(ctx
, type
, NULL
, NULL
);
349 int EVP_DigestInit_ex(EVP_MD_CTX
*ctx
, const EVP_MD
*type
, ENGINE
*impl
)
351 return evp_md_init_internal(ctx
, type
, NULL
, impl
);
354 int EVP_DigestUpdate(EVP_MD_CTX
*ctx
, const void *data
, size_t count
)
359 if (ctx
->pctx
!= NULL
360 && EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
->pctx
)
361 && ctx
->pctx
->op
.sig
.sigprovctx
!= NULL
) {
363 * Prior to OpenSSL 3.0 EVP_DigestSignUpdate() and
364 * EVP_DigestVerifyUpdate() were just macros for EVP_DigestUpdate().
365 * Some code calls EVP_DigestUpdate() directly even when initialised
366 * with EVP_DigestSignInit_ex() or
367 * EVP_DigestVerifyInit_ex(), so we detect that and redirect to
368 * the correct EVP_Digest*Update() function
370 if (ctx
->pctx
->operation
== EVP_PKEY_OP_SIGNCTX
)
371 return EVP_DigestSignUpdate(ctx
, data
, count
);
372 if (ctx
->pctx
->operation
== EVP_PKEY_OP_VERIFYCTX
)
373 return EVP_DigestVerifyUpdate(ctx
, data
, count
);
374 ERR_raise(ERR_LIB_EVP
, EVP_R_UPDATE_ERROR
);
378 if (ctx
->digest
== NULL
379 || ctx
->digest
->prov
== NULL
380 || (ctx
->flags
& EVP_MD_CTX_FLAG_NO_INIT
) != 0)
383 if (ctx
->digest
->dupdate
== NULL
) {
384 ERR_raise(ERR_LIB_EVP
, EVP_R_UPDATE_ERROR
);
387 return ctx
->digest
->dupdate(ctx
->provctx
, data
, count
);
389 /* Code below to be removed when legacy support is dropped. */
391 return ctx
->update(ctx
, data
, count
);
394 /* The caller can assume that this removes any secret data from the context */
395 int EVP_DigestFinal(EVP_MD_CTX
*ctx
, unsigned char *md
, unsigned int *size
)
398 ret
= EVP_DigestFinal_ex(ctx
, md
, size
);
399 EVP_MD_CTX_reset(ctx
);
403 /* The caller can assume that this removes any secret data from the context */
404 int EVP_DigestFinal_ex(EVP_MD_CTX
*ctx
, unsigned char *md
, unsigned int *isize
)
410 if (ctx
->digest
== NULL
)
413 sz
= EVP_MD_size(ctx
->digest
);
417 if (ctx
->digest
->prov
== NULL
)
420 if (ctx
->digest
->dfinal
== NULL
) {
421 ERR_raise(ERR_LIB_EVP
, EVP_R_FINAL_ERROR
);
425 ret
= ctx
->digest
->dfinal(ctx
->provctx
, md
, &size
, mdsize
);
428 if (size
<= UINT_MAX
) {
431 ERR_raise(ERR_LIB_EVP
, EVP_R_FINAL_ERROR
);
438 /* Code below to be removed when legacy support is dropped. */
440 OPENSSL_assert(mdsize
<= EVP_MAX_MD_SIZE
);
441 ret
= ctx
->digest
->final(ctx
, md
);
444 if (ctx
->digest
->cleanup
) {
445 ctx
->digest
->cleanup(ctx
);
446 EVP_MD_CTX_set_flags(ctx
, EVP_MD_CTX_FLAG_CLEANED
);
448 OPENSSL_cleanse(ctx
->md_data
, ctx
->digest
->ctx_size
);
452 int EVP_DigestFinalXOF(EVP_MD_CTX
*ctx
, unsigned char *md
, size_t size
)
455 OSSL_PARAM params
[2];
458 if (ctx
->digest
== NULL
) {
459 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_NULL_ALGORITHM
);
463 if (ctx
->digest
->prov
== NULL
)
466 if (ctx
->digest
->dfinal
== NULL
) {
467 ERR_raise(ERR_LIB_EVP
, EVP_R_FINAL_ERROR
);
471 params
[i
++] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN
, &size
);
472 params
[i
++] = OSSL_PARAM_construct_end();
474 if (EVP_MD_CTX_set_params(ctx
, params
) > 0)
475 ret
= ctx
->digest
->dfinal(ctx
->provctx
, md
, &size
, size
);
480 if (ctx
->digest
->flags
& EVP_MD_FLAG_XOF
482 && ctx
->digest
->md_ctrl(ctx
, EVP_MD_CTRL_XOF_LEN
, (int)size
, NULL
)) {
483 ret
= ctx
->digest
->final(ctx
, md
);
484 if (ctx
->digest
->cleanup
!= NULL
) {
485 ctx
->digest
->cleanup(ctx
);
486 EVP_MD_CTX_set_flags(ctx
, EVP_MD_CTX_FLAG_CLEANED
);
488 OPENSSL_cleanse(ctx
->md_data
, ctx
->digest
->ctx_size
);
490 ERR_raise(ERR_LIB_EVP
, EVP_R_NOT_XOF_OR_INVALID_LENGTH
);
496 int EVP_MD_CTX_copy(EVP_MD_CTX
*out
, const EVP_MD_CTX
*in
)
498 EVP_MD_CTX_reset(out
);
499 return EVP_MD_CTX_copy_ex(out
, in
);
502 int EVP_MD_CTX_copy_ex(EVP_MD_CTX
*out
, const EVP_MD_CTX
*in
)
504 unsigned char *tmp_buf
;
506 if (in
== NULL
|| in
->digest
== NULL
) {
507 ERR_raise(ERR_LIB_EVP
, EVP_R_INPUT_NOT_INITIALIZED
);
511 if (in
->digest
->prov
== NULL
512 || (in
->flags
& EVP_MD_CTX_FLAG_NO_INIT
) != 0)
515 if (in
->digest
->dupctx
== NULL
) {
516 ERR_raise(ERR_LIB_EVP
, EVP_R_NOT_ABLE_TO_COPY_CTX
);
520 EVP_MD_CTX_reset(out
);
521 if (out
->fetched_digest
!= NULL
)
522 EVP_MD_free(out
->fetched_digest
);
524 /* NULL out pointers in case of error */
528 if (in
->fetched_digest
!= NULL
)
529 EVP_MD_up_ref(in
->fetched_digest
);
531 if (in
->provctx
!= NULL
) {
532 out
->provctx
= in
->digest
->dupctx(in
->provctx
);
533 if (out
->provctx
== NULL
) {
534 ERR_raise(ERR_LIB_EVP
, EVP_R_NOT_ABLE_TO_COPY_CTX
);
539 /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
540 EVP_MD_CTX_clear_flags(out
, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX
);
542 if (in
->pctx
!= NULL
) {
543 out
->pctx
= EVP_PKEY_CTX_dup(in
->pctx
);
544 if (out
->pctx
== NULL
) {
545 ERR_raise(ERR_LIB_EVP
, EVP_R_NOT_ABLE_TO_COPY_CTX
);
546 EVP_MD_CTX_reset(out
);
554 /* Code below to be removed when legacy support is dropped. */
556 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
557 /* Make sure it's safe to copy a digest context using an ENGINE */
558 if (in
->engine
&& !ENGINE_init(in
->engine
)) {
559 ERR_raise(ERR_LIB_EVP
, ERR_R_ENGINE_LIB
);
564 if (out
->digest
== in
->digest
) {
565 tmp_buf
= out
->md_data
;
566 EVP_MD_CTX_set_flags(out
, EVP_MD_CTX_FLAG_REUSE
);
569 EVP_MD_CTX_reset(out
);
570 memcpy(out
, in
, sizeof(*out
));
572 /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
573 EVP_MD_CTX_clear_flags(out
, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX
);
575 /* Null these variables, since they are getting fixed up
576 * properly below. Anything else may cause a memleak and/or
577 * double free if any of the memory allocations below fail
582 if (in
->md_data
&& out
->digest
->ctx_size
) {
584 out
->md_data
= tmp_buf
;
586 out
->md_data
= OPENSSL_malloc(out
->digest
->ctx_size
);
587 if (out
->md_data
== NULL
) {
588 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
592 memcpy(out
->md_data
, in
->md_data
, out
->digest
->ctx_size
);
595 out
->update
= in
->update
;
599 out
->pctx
= EVP_PKEY_CTX_dup(in
->pctx
);
601 EVP_MD_CTX_reset(out
);
607 if (out
->digest
->copy
)
608 return out
->digest
->copy(out
, in
);
613 int EVP_Digest(const void *data
, size_t count
,
614 unsigned char *md
, unsigned int *size
, const EVP_MD
*type
,
617 EVP_MD_CTX
*ctx
= EVP_MD_CTX_new();
622 EVP_MD_CTX_set_flags(ctx
, EVP_MD_CTX_FLAG_ONESHOT
);
623 ret
= EVP_DigestInit_ex(ctx
, type
, impl
)
624 && EVP_DigestUpdate(ctx
, data
, count
)
625 && EVP_DigestFinal_ex(ctx
, md
, size
);
626 EVP_MD_CTX_free(ctx
);
631 int EVP_MD_get_params(const EVP_MD
*digest
, OSSL_PARAM params
[])
633 if (digest
!= NULL
&& digest
->get_params
!= NULL
)
634 return digest
->get_params(params
);
638 const OSSL_PARAM
*EVP_MD_gettable_params(const EVP_MD
*digest
)
640 if (digest
!= NULL
&& digest
->gettable_params
!= NULL
)
641 return digest
->gettable_params(
642 ossl_provider_ctx(EVP_MD_provider(digest
)));
646 int EVP_MD_CTX_set_params(EVP_MD_CTX
*ctx
, const OSSL_PARAM params
[])
648 EVP_PKEY_CTX
*pctx
= ctx
->pctx
;
650 /* If we have a pctx then we should try that first */
652 && (pctx
->operation
== EVP_PKEY_OP_VERIFYCTX
653 || pctx
->operation
== EVP_PKEY_OP_SIGNCTX
)
654 && pctx
->op
.sig
.sigprovctx
!= NULL
655 && pctx
->op
.sig
.signature
->set_ctx_md_params
!= NULL
)
656 return pctx
->op
.sig
.signature
->set_ctx_md_params(pctx
->op
.sig
.sigprovctx
,
659 if (ctx
->digest
!= NULL
&& ctx
->digest
->set_ctx_params
!= NULL
)
660 return ctx
->digest
->set_ctx_params(ctx
->provctx
, params
);
665 const OSSL_PARAM
*EVP_MD_settable_ctx_params(const EVP_MD
*md
)
669 if (md
!= NULL
&& md
->settable_ctx_params
!= NULL
) {
670 alg
= ossl_provider_ctx(EVP_MD_provider(md
));
671 return md
->settable_ctx_params(NULL
, alg
);
676 const OSSL_PARAM
*EVP_MD_CTX_settable_params(EVP_MD_CTX
*ctx
)
684 /* If we have a pctx then we should try that first */
687 && (pctx
->operation
== EVP_PKEY_OP_VERIFYCTX
688 || pctx
->operation
== EVP_PKEY_OP_SIGNCTX
)
689 && pctx
->op
.sig
.sigprovctx
!= NULL
690 && pctx
->op
.sig
.signature
->settable_ctx_md_params
!= NULL
)
691 return pctx
->op
.sig
.signature
->settable_ctx_md_params(
692 pctx
->op
.sig
.sigprovctx
);
694 if (ctx
->digest
!= NULL
&& ctx
->digest
->settable_ctx_params
!= NULL
) {
695 alg
= ossl_provider_ctx(EVP_MD_provider(ctx
->digest
));
696 return ctx
->digest
->settable_ctx_params(ctx
->provctx
, alg
);
702 int EVP_MD_CTX_get_params(EVP_MD_CTX
*ctx
, OSSL_PARAM params
[])
704 EVP_PKEY_CTX
*pctx
= ctx
->pctx
;
706 /* If we have a pctx then we should try that first */
708 && (pctx
->operation
== EVP_PKEY_OP_VERIFYCTX
709 || pctx
->operation
== EVP_PKEY_OP_SIGNCTX
)
710 && pctx
->op
.sig
.sigprovctx
!= NULL
711 && pctx
->op
.sig
.signature
->get_ctx_md_params
!= NULL
)
712 return pctx
->op
.sig
.signature
->get_ctx_md_params(pctx
->op
.sig
.sigprovctx
,
715 if (ctx
->digest
!= NULL
&& ctx
->digest
->get_params
!= NULL
)
716 return ctx
->digest
->get_ctx_params(ctx
->provctx
, params
);
721 const OSSL_PARAM
*EVP_MD_gettable_ctx_params(const EVP_MD
*md
)
725 if (md
!= NULL
&& md
->gettable_ctx_params
!= NULL
) {
726 alg
= ossl_provider_ctx(EVP_MD_provider(md
));
727 return md
->gettable_ctx_params(NULL
, alg
);
732 const OSSL_PARAM
*EVP_MD_CTX_gettable_params(EVP_MD_CTX
*ctx
)
740 /* If we have a pctx then we should try that first */
743 && (pctx
->operation
== EVP_PKEY_OP_VERIFYCTX
744 || pctx
->operation
== EVP_PKEY_OP_SIGNCTX
)
745 && pctx
->op
.sig
.sigprovctx
!= NULL
746 && pctx
->op
.sig
.signature
->gettable_ctx_md_params
!= NULL
)
747 return pctx
->op
.sig
.signature
->gettable_ctx_md_params(
748 pctx
->op
.sig
.sigprovctx
);
750 if (ctx
->digest
!= NULL
&& ctx
->digest
->gettable_ctx_params
!= NULL
) {
751 alg
= ossl_provider_ctx(EVP_MD_provider(ctx
->digest
));
752 return ctx
->digest
->gettable_ctx_params(ctx
->provctx
, alg
);
757 int EVP_MD_CTX_ctrl(EVP_MD_CTX
*ctx
, int cmd
, int p1
, void *p2
)
759 int ret
= EVP_CTRL_RET_UNSUPPORTED
;
762 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
765 ERR_raise(ERR_LIB_EVP
, ERR_R_PASSED_NULL_PARAMETER
);
769 if (ctx
->digest
!= NULL
&& ctx
->digest
->prov
== NULL
)
773 case EVP_MD_CTRL_XOF_LEN
:
775 params
[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN
, &sz
);
777 case EVP_MD_CTRL_MICALG
:
779 params
[0] = OSSL_PARAM_construct_utf8_string(OSSL_DIGEST_PARAM_MICALG
,
782 case EVP_CTRL_SSL3_MASTER_SECRET
:
783 params
[0] = OSSL_PARAM_construct_octet_string(OSSL_DIGEST_PARAM_SSL3_MS
,
791 ret
= EVP_MD_CTX_set_params(ctx
, params
);
793 ret
= EVP_MD_CTX_get_params(ctx
, params
);
797 /* Code below to be removed when legacy support is dropped. */
799 if (ctx
->digest
->md_ctrl
== NULL
) {
800 ERR_raise(ERR_LIB_EVP
, EVP_R_CTRL_NOT_IMPLEMENTED
);
804 ret
= ctx
->digest
->md_ctrl(ctx
, cmd
, p1
, p2
);
811 EVP_MD
*evp_md_new(void)
813 EVP_MD
*md
= OPENSSL_zalloc(sizeof(*md
));
816 md
->lock
= CRYPTO_THREAD_lock_new();
817 if (md
->lock
== NULL
) {
827 * FIPS module note: since internal fetches will be entirely
828 * provider based, we know that none of its code depends on legacy
829 * NIDs or any functionality that use them.
832 static void set_legacy_nid(const char *name
, void *vlegacy_nid
)
835 int *legacy_nid
= vlegacy_nid
;
837 * We use lowest level function to get the associated method, because
838 * higher level functions such as EVP_get_digestbyname() have changed
839 * to look at providers too.
841 const void *legacy_method
= OBJ_NAME_get(name
, OBJ_NAME_TYPE_MD_METH
);
843 if (*legacy_nid
== -1) /* We found a clash already */
846 if (legacy_method
== NULL
)
848 nid
= EVP_MD_nid(legacy_method
);
849 if (*legacy_nid
!= NID_undef
&& *legacy_nid
!= nid
) {
857 static int evp_md_cache_constants(EVP_MD
*md
)
859 int ok
, xof
= 0, algid_absent
= 0;
862 OSSL_PARAM params
[5];
864 params
[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_BLOCK_SIZE
, &blksz
);
865 params
[1] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_SIZE
, &mdsize
);
866 params
[2] = OSSL_PARAM_construct_int(OSSL_DIGEST_PARAM_XOF
, &xof
);
867 params
[3] = OSSL_PARAM_construct_int(OSSL_DIGEST_PARAM_ALGID_ABSENT
,
869 params
[4] = OSSL_PARAM_construct_end();
870 ok
= evp_do_md_getparams(md
, params
);
871 if (mdsize
> INT_MAX
|| blksz
> INT_MAX
)
874 md
->block_size
= (int)blksz
;
875 md
->md_size
= (int)mdsize
;
877 md
->flags
|= EVP_MD_FLAG_XOF
;
879 md
->flags
|= EVP_MD_FLAG_DIGALGID_ABSENT
;
884 static void *evp_md_from_algorithm(int name_id
,
885 const OSSL_ALGORITHM
*algodef
,
888 const OSSL_DISPATCH
*fns
= algodef
->implementation
;
892 /* EVP_MD_fetch() will set the legacy NID if available */
893 if ((md
= evp_md_new()) == NULL
) {
894 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
899 md
->type
= NID_undef
;
900 if (!evp_names_do_all(prov
, name_id
, set_legacy_nid
, &md
->type
)
902 ERR_raise(ERR_LIB_EVP
, ERR_R_INTERNAL_ERROR
);
908 md
->name_id
= name_id
;
909 md
->description
= algodef
->algorithm_description
;
911 for (; fns
->function_id
!= 0; fns
++) {
912 switch (fns
->function_id
) {
913 case OSSL_FUNC_DIGEST_NEWCTX
:
914 if (md
->newctx
== NULL
) {
915 md
->newctx
= OSSL_FUNC_digest_newctx(fns
);
919 case OSSL_FUNC_DIGEST_INIT
:
920 if (md
->dinit
== NULL
) {
921 md
->dinit
= OSSL_FUNC_digest_init(fns
);
925 case OSSL_FUNC_DIGEST_UPDATE
:
926 if (md
->dupdate
== NULL
) {
927 md
->dupdate
= OSSL_FUNC_digest_update(fns
);
931 case OSSL_FUNC_DIGEST_FINAL
:
932 if (md
->dfinal
== NULL
) {
933 md
->dfinal
= OSSL_FUNC_digest_final(fns
);
937 case OSSL_FUNC_DIGEST_DIGEST
:
938 if (md
->digest
== NULL
)
939 md
->digest
= OSSL_FUNC_digest_digest(fns
);
940 /* We don't increment fnct for this as it is stand alone */
942 case OSSL_FUNC_DIGEST_FREECTX
:
943 if (md
->freectx
== NULL
) {
944 md
->freectx
= OSSL_FUNC_digest_freectx(fns
);
948 case OSSL_FUNC_DIGEST_DUPCTX
:
949 if (md
->dupctx
== NULL
)
950 md
->dupctx
= OSSL_FUNC_digest_dupctx(fns
);
952 case OSSL_FUNC_DIGEST_GET_PARAMS
:
953 if (md
->get_params
== NULL
)
954 md
->get_params
= OSSL_FUNC_digest_get_params(fns
);
956 case OSSL_FUNC_DIGEST_SET_CTX_PARAMS
:
957 if (md
->set_ctx_params
== NULL
)
958 md
->set_ctx_params
= OSSL_FUNC_digest_set_ctx_params(fns
);
960 case OSSL_FUNC_DIGEST_GET_CTX_PARAMS
:
961 if (md
->get_ctx_params
== NULL
)
962 md
->get_ctx_params
= OSSL_FUNC_digest_get_ctx_params(fns
);
964 case OSSL_FUNC_DIGEST_GETTABLE_PARAMS
:
965 if (md
->gettable_params
== NULL
)
966 md
->gettable_params
= OSSL_FUNC_digest_gettable_params(fns
);
968 case OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS
:
969 if (md
->settable_ctx_params
== NULL
)
970 md
->settable_ctx_params
=
971 OSSL_FUNC_digest_settable_ctx_params(fns
);
973 case OSSL_FUNC_DIGEST_GETTABLE_CTX_PARAMS
:
974 if (md
->gettable_ctx_params
== NULL
)
975 md
->gettable_ctx_params
=
976 OSSL_FUNC_digest_gettable_ctx_params(fns
);
980 if ((fncnt
!= 0 && fncnt
!= 5)
981 || (fncnt
== 0 && md
->digest
== NULL
)) {
983 * In order to be a consistent set of functions we either need the
984 * whole set of init/update/final etc functions or none of them.
985 * The "digest" function can standalone. We at least need one way to
989 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_PROVIDER_FUNCTIONS
);
994 ossl_provider_up_ref(prov
);
996 if (!evp_md_cache_constants(md
)) {
998 ERR_raise(ERR_LIB_EVP
, EVP_R_CACHE_CONSTANTS_FAILED
);
1005 static int evp_md_up_ref(void *md
)
1007 return EVP_MD_up_ref(md
);
1010 static void evp_md_free(void *md
)
1015 EVP_MD
*EVP_MD_fetch(OSSL_LIB_CTX
*ctx
, const char *algorithm
,
1016 const char *properties
)
1019 evp_generic_fetch(ctx
, OSSL_OP_DIGEST
, algorithm
, properties
,
1020 evp_md_from_algorithm
, evp_md_up_ref
, evp_md_free
);
1025 int EVP_MD_up_ref(EVP_MD
*md
)
1029 if (md
->origin
== EVP_ORIG_DYNAMIC
)
1030 CRYPTO_UP_REF(&md
->refcnt
, &ref
, md
->lock
);
1034 void EVP_MD_free(EVP_MD
*md
)
1038 if (md
== NULL
|| md
->origin
!= EVP_ORIG_DYNAMIC
)
1041 CRYPTO_DOWN_REF(&md
->refcnt
, &i
, md
->lock
);
1044 evp_md_free_int(md
);
1047 void EVP_MD_do_all_provided(OSSL_LIB_CTX
*libctx
,
1048 void (*fn
)(EVP_MD
*mac
, void *arg
),
1051 evp_generic_do_all(libctx
, OSSL_OP_DIGEST
,
1052 (void (*)(void *, void *))fn
, arg
,
1053 evp_md_from_algorithm
, evp_md_free
);