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"
25 /* This call frees resources associated with the context */
26 int EVP_MD_CTX_reset(EVP_MD_CTX
*ctx
)
32 /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
34 * pctx should be freed by the user of EVP_MD_CTX
35 * if EVP_MD_CTX_FLAG_KEEP_PKEY_CTX is set
37 if (!EVP_MD_CTX_test_flags(ctx
, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX
)) {
38 EVP_PKEY_CTX_free(ctx
->pctx
);
43 EVP_MD_free(ctx
->fetched_digest
);
44 ctx
->fetched_digest
= NULL
;
45 ctx
->reqdigest
= NULL
;
47 if (ctx
->provctx
!= NULL
) {
48 if (ctx
->digest
->freectx
!= NULL
)
49 ctx
->digest
->freectx(ctx
->provctx
);
51 EVP_MD_CTX_set_flags(ctx
, EVP_MD_CTX_FLAG_CLEANED
);
54 /* TODO(3.0): Remove legacy code below */
57 * Don't assume ctx->md_data was cleaned in EVP_Digest_Final, because
58 * sometimes only copies of the context are ever finalised.
60 if (ctx
->digest
&& ctx
->digest
->cleanup
61 && !EVP_MD_CTX_test_flags(ctx
, EVP_MD_CTX_FLAG_CLEANED
))
62 ctx
->digest
->cleanup(ctx
);
63 if (ctx
->digest
&& ctx
->digest
->ctx_size
&& ctx
->md_data
64 && !EVP_MD_CTX_test_flags(ctx
, EVP_MD_CTX_FLAG_REUSE
)) {
65 OPENSSL_clear_free(ctx
->md_data
, ctx
->digest
->ctx_size
);
68 #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
69 ENGINE_finish(ctx
->engine
);
72 /* TODO(3.0): End of legacy code */
74 OPENSSL_cleanse(ctx
, sizeof(*ctx
));
80 EVP_MD_CTX
*evp_md_ctx_new_with_libctx(EVP_PKEY
*pkey
,
81 const ASN1_OCTET_STRING
*id
,
82 OPENSSL_CTX
*libctx
, const char *propq
)
85 EVP_PKEY_CTX
*pctx
= NULL
;
87 if ((ctx
= EVP_MD_CTX_new()) == NULL
88 || (pctx
= EVP_PKEY_CTX_new_from_pkey(libctx
, pkey
, propq
)) == NULL
) {
89 ASN1err(0, ERR_R_MALLOC_FAILURE
);
93 # ifndef OPENSSL_NO_EC
94 if (id
!= NULL
&& EVP_PKEY_CTX_set1_id(pctx
, id
->data
, id
->length
) <= 0) {
95 ASN1err(0, ERR_R_MALLOC_FAILURE
);
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 EVP_MD_CTX_clear_flags(ctx
, EVP_MD_CTX_FLAG_CLEANED
);
140 if (ctx
->provctx
!= NULL
) {
141 if (!ossl_assert(ctx
->digest
!= NULL
)) {
142 EVPerr(EVP_F_EVP_DIGESTINIT_EX
, EVP_R_INITIALIZATION_ERROR
);
145 if (ctx
->digest
->freectx
!= NULL
)
146 ctx
->digest
->freectx(ctx
->provctx
);
151 ctx
->reqdigest
= type
;
153 /* TODO(3.0): Legacy work around code below. Remove this */
154 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
156 * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
157 * this context may already have an ENGINE! Try to avoid releasing the
158 * previous handle, re-querying for an ENGINE, and having a
159 * reinitialisation, when it may all be unnecessary.
161 if (ctx
->engine
&& ctx
->digest
&&
162 (type
== NULL
|| (type
->type
== ctx
->digest
->type
)))
167 * Ensure an ENGINE left lying around from last time is cleared (the
168 * previous check attempted to avoid this if the same ENGINE and
169 * EVP_MD could be used).
171 ENGINE_finish(ctx
->engine
);
175 if (type
!= NULL
&& impl
== NULL
)
176 tmpimpl
= ENGINE_get_digest_engine(type
->type
);
180 * If there are engines involved or EVP_MD_CTX_FLAG_NO_INIT is set then we
181 * should use legacy handling for now.
183 if (ctx
->engine
!= NULL
185 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
188 || (ctx
->flags
& EVP_MD_CTX_FLAG_NO_INIT
) != 0) {
189 if (ctx
->digest
== ctx
->fetched_digest
)
191 EVP_MD_free(ctx
->fetched_digest
);
192 ctx
->fetched_digest
= NULL
;
196 if (ctx
->digest
!= NULL
&& ctx
->digest
->ctx_size
> 0) {
197 OPENSSL_clear_free(ctx
->md_data
, ctx
->digest
->ctx_size
);
201 /* TODO(3.0): Start of non-legacy code below */
203 if (type
->prov
== NULL
) {
205 /* We only do explicit fetches inside the FIPS module */
206 EVPerr(EVP_F_EVP_DIGESTINIT_EX
, EVP_R_INITIALIZATION_ERROR
);
209 EVP_MD
*provmd
= EVP_MD_fetch(NULL
, OBJ_nid2sn(type
->type
), "");
211 if (provmd
== NULL
) {
212 EVPerr(EVP_F_EVP_DIGESTINIT_EX
, EVP_R_INITIALIZATION_ERROR
);
216 EVP_MD_free(ctx
->fetched_digest
);
217 ctx
->fetched_digest
= provmd
;
221 if (ctx
->provctx
!= NULL
&& ctx
->digest
!= NULL
&& ctx
->digest
!= type
) {
222 if (ctx
->digest
->freectx
!= NULL
)
223 ctx
->digest
->freectx(ctx
->provctx
);
227 if (ctx
->provctx
== NULL
) {
228 ctx
->provctx
= ctx
->digest
->newctx(ossl_provider_ctx(type
->prov
));
229 if (ctx
->provctx
== NULL
) {
230 EVPerr(EVP_F_EVP_DIGESTINIT_EX
, EVP_R_INITIALIZATION_ERROR
);
235 if (ctx
->digest
->dinit
== NULL
) {
236 EVPerr(EVP_F_EVP_DIGESTINIT_EX
, EVP_R_INITIALIZATION_ERROR
);
240 return ctx
->digest
->dinit(ctx
->provctx
);
242 /* TODO(3.0): Remove legacy code below */
245 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
248 if (!ENGINE_init(impl
)) {
249 EVPerr(EVP_F_EVP_DIGESTINIT_EX
, EVP_R_INITIALIZATION_ERROR
);
253 /* Ask if an ENGINE is reserved for this job */
257 /* There's an ENGINE for this job ... (apparently) */
258 const EVP_MD
*d
= ENGINE_get_digest(impl
, type
->type
);
261 EVPerr(EVP_F_EVP_DIGESTINIT_EX
, EVP_R_INITIALIZATION_ERROR
);
265 /* We'll use the ENGINE's private digest definition */
268 * Store the ENGINE functional reference so we know 'type' came
269 * from an ENGINE and we need to release it when done.
276 EVPerr(EVP_F_EVP_DIGESTINIT_EX
, EVP_R_NO_DIGEST_SET
);
282 if (ctx
->digest
!= type
) {
283 if (ctx
->digest
&& ctx
->digest
->ctx_size
) {
284 OPENSSL_clear_free(ctx
->md_data
, ctx
->digest
->ctx_size
);
288 if (!(ctx
->flags
& EVP_MD_CTX_FLAG_NO_INIT
) && type
->ctx_size
) {
289 ctx
->update
= type
->update
;
290 ctx
->md_data
= OPENSSL_zalloc(type
->ctx_size
);
291 if (ctx
->md_data
== NULL
) {
292 EVPerr(EVP_F_EVP_DIGESTINIT_EX
, ERR_R_MALLOC_FAILURE
);
297 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
302 * TODO(3.0): Temporarily no support for EVP_DigestSign* inside FIPS module
303 * or when using providers.
305 if (ctx
->pctx
!= NULL
306 && (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
->pctx
)
307 || ctx
->pctx
->op
.sig
.signature
== NULL
)) {
309 r
= EVP_PKEY_CTX_ctrl(ctx
->pctx
, -1, EVP_PKEY_OP_TYPE_SIG
,
310 EVP_PKEY_CTRL_DIGESTINIT
, 0, ctx
);
311 if (r
<= 0 && (r
!= -2))
315 if (ctx
->flags
& EVP_MD_CTX_FLAG_NO_INIT
)
317 return ctx
->digest
->init(ctx
);
320 int EVP_DigestUpdate(EVP_MD_CTX
*ctx
, const void *data
, size_t count
)
325 if (ctx
->pctx
!= NULL
326 && EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
->pctx
)
327 && ctx
->pctx
->op
.sig
.sigprovctx
!= NULL
) {
329 * Prior to OpenSSL 3.0 EVP_DigestSignUpdate() and
330 * EVP_DigestVerifyUpdate() were just macros for EVP_DigestUpdate().
331 * Some code calls EVP_DigestUpdate() directly even when initialised
332 * with EVP_DigestSignInit_with_libctx() or
333 * EVP_DigestVerifyInit_with_libctx(), so we detect that and redirect to
334 * the correct EVP_Digest*Update() function
336 if (ctx
->pctx
->operation
== EVP_PKEY_OP_SIGNCTX
)
337 return EVP_DigestSignUpdate(ctx
, data
, count
);
338 if (ctx
->pctx
->operation
== EVP_PKEY_OP_VERIFYCTX
)
339 return EVP_DigestVerifyUpdate(ctx
, data
, count
);
340 EVPerr(EVP_F_EVP_DIGESTUPDATE
, EVP_R_UPDATE_ERROR
);
344 if (ctx
->digest
== NULL
345 || ctx
->digest
->prov
== NULL
346 || (ctx
->flags
& EVP_MD_CTX_FLAG_NO_INIT
) != 0)
349 if (ctx
->digest
->dupdate
== NULL
) {
350 EVPerr(EVP_F_EVP_DIGESTUPDATE
, EVP_R_UPDATE_ERROR
);
353 return ctx
->digest
->dupdate(ctx
->provctx
, data
, count
);
355 /* TODO(3.0): Remove legacy code below */
357 return ctx
->update(ctx
, data
, count
);
360 /* The caller can assume that this removes any secret data from the context */
361 int EVP_DigestFinal(EVP_MD_CTX
*ctx
, unsigned char *md
, unsigned int *size
)
364 ret
= EVP_DigestFinal_ex(ctx
, md
, size
);
365 EVP_MD_CTX_reset(ctx
);
369 /* The caller can assume that this removes any secret data from the context */
370 int EVP_DigestFinal_ex(EVP_MD_CTX
*ctx
, unsigned char *md
, unsigned int *isize
)
376 if (ctx
->digest
== NULL
)
379 sz
= EVP_MD_size(ctx
->digest
);
383 if (ctx
->digest
->prov
== NULL
)
386 if (ctx
->digest
->dfinal
== NULL
) {
387 EVPerr(EVP_F_EVP_DIGESTFINAL_EX
, EVP_R_FINAL_ERROR
);
391 ret
= ctx
->digest
->dfinal(ctx
->provctx
, md
, &size
, mdsize
);
394 if (size
<= UINT_MAX
) {
397 EVPerr(EVP_F_EVP_DIGESTFINAL_EX
, EVP_R_FINAL_ERROR
);
404 /* TODO(3.0): Remove legacy code below */
406 OPENSSL_assert(mdsize
<= EVP_MAX_MD_SIZE
);
407 ret
= ctx
->digest
->final(ctx
, md
);
410 if (ctx
->digest
->cleanup
) {
411 ctx
->digest
->cleanup(ctx
);
412 EVP_MD_CTX_set_flags(ctx
, EVP_MD_CTX_FLAG_CLEANED
);
414 OPENSSL_cleanse(ctx
->md_data
, ctx
->digest
->ctx_size
);
418 int EVP_DigestFinalXOF(EVP_MD_CTX
*ctx
, unsigned char *md
, size_t size
)
421 OSSL_PARAM params
[2];
424 if (ctx
->digest
== NULL
|| ctx
->digest
->prov
== NULL
)
427 if (ctx
->digest
->dfinal
== NULL
) {
428 EVPerr(EVP_F_EVP_DIGESTFINALXOF
, EVP_R_FINAL_ERROR
);
432 params
[i
++] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN
, &size
);
433 params
[i
++] = OSSL_PARAM_construct_end();
435 if (EVP_MD_CTX_set_params(ctx
, params
) > 0)
436 ret
= ctx
->digest
->dfinal(ctx
->provctx
, md
, &size
, size
);
437 EVP_MD_CTX_reset(ctx
);
441 if (ctx
->digest
->flags
& EVP_MD_FLAG_XOF
443 && ctx
->digest
->md_ctrl(ctx
, EVP_MD_CTRL_XOF_LEN
, (int)size
, NULL
)) {
444 ret
= ctx
->digest
->final(ctx
, md
);
445 if (ctx
->digest
->cleanup
!= NULL
) {
446 ctx
->digest
->cleanup(ctx
);
447 EVP_MD_CTX_set_flags(ctx
, EVP_MD_CTX_FLAG_CLEANED
);
449 OPENSSL_cleanse(ctx
->md_data
, ctx
->digest
->ctx_size
);
451 EVPerr(EVP_F_EVP_DIGESTFINALXOF
, EVP_R_NOT_XOF_OR_INVALID_LENGTH
);
457 int EVP_MD_CTX_copy(EVP_MD_CTX
*out
, const EVP_MD_CTX
*in
)
459 EVP_MD_CTX_reset(out
);
460 return EVP_MD_CTX_copy_ex(out
, in
);
463 int EVP_MD_CTX_copy_ex(EVP_MD_CTX
*out
, const EVP_MD_CTX
*in
)
465 unsigned char *tmp_buf
;
467 if (in
== NULL
|| in
->digest
== NULL
) {
468 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX
, EVP_R_INPUT_NOT_INITIALIZED
);
472 if (in
->digest
->prov
== NULL
473 || (in
->flags
& EVP_MD_CTX_FLAG_NO_INIT
) != 0)
476 if (in
->digest
->dupctx
== NULL
) {
477 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX
, EVP_R_NOT_ABLE_TO_COPY_CTX
);
481 EVP_MD_CTX_reset(out
);
482 if (out
->fetched_digest
!= NULL
)
483 EVP_MD_free(out
->fetched_digest
);
485 /* NULL out pointers in case of error */
489 if (in
->fetched_digest
!= NULL
)
490 EVP_MD_up_ref(in
->fetched_digest
);
492 if (in
->provctx
!= NULL
) {
493 out
->provctx
= in
->digest
->dupctx(in
->provctx
);
494 if (out
->provctx
== NULL
) {
495 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX
, EVP_R_NOT_ABLE_TO_COPY_CTX
);
500 /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
501 EVP_MD_CTX_clear_flags(out
, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX
);
503 /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
504 if (in
->pctx
!= NULL
) {
505 out
->pctx
= EVP_PKEY_CTX_dup(in
->pctx
);
506 if (out
->pctx
== NULL
) {
507 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX
, EVP_R_NOT_ABLE_TO_COPY_CTX
);
508 EVP_MD_CTX_reset(out
);
516 /* TODO(3.0): Remove legacy code below */
518 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
519 /* Make sure it's safe to copy a digest context using an ENGINE */
520 if (in
->engine
&& !ENGINE_init(in
->engine
)) {
521 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX
, ERR_R_ENGINE_LIB
);
526 if (out
->digest
== in
->digest
) {
527 tmp_buf
= out
->md_data
;
528 EVP_MD_CTX_set_flags(out
, EVP_MD_CTX_FLAG_REUSE
);
531 EVP_MD_CTX_reset(out
);
532 memcpy(out
, in
, sizeof(*out
));
534 /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
535 EVP_MD_CTX_clear_flags(out
, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX
);
537 /* Null these variables, since they are getting fixed up
538 * properly below. Anything else may cause a memleak and/or
539 * double free if any of the memory allocations below fail
544 if (in
->md_data
&& out
->digest
->ctx_size
) {
546 out
->md_data
= tmp_buf
;
548 out
->md_data
= OPENSSL_malloc(out
->digest
->ctx_size
);
549 if (out
->md_data
== NULL
) {
550 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX
, ERR_R_MALLOC_FAILURE
);
554 memcpy(out
->md_data
, in
->md_data
, out
->digest
->ctx_size
);
557 out
->update
= in
->update
;
560 /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
562 out
->pctx
= EVP_PKEY_CTX_dup(in
->pctx
);
564 EVP_MD_CTX_reset(out
);
570 if (out
->digest
->copy
)
571 return out
->digest
->copy(out
, in
);
576 int EVP_Digest(const void *data
, size_t count
,
577 unsigned char *md
, unsigned int *size
, const EVP_MD
*type
,
580 EVP_MD_CTX
*ctx
= EVP_MD_CTX_new();
585 EVP_MD_CTX_set_flags(ctx
, EVP_MD_CTX_FLAG_ONESHOT
);
586 ret
= EVP_DigestInit_ex(ctx
, type
, impl
)
587 && EVP_DigestUpdate(ctx
, data
, count
)
588 && EVP_DigestFinal_ex(ctx
, md
, size
);
589 EVP_MD_CTX_free(ctx
);
594 int EVP_MD_get_params(const EVP_MD
*digest
, OSSL_PARAM params
[])
596 if (digest
!= NULL
&& digest
->get_params
!= NULL
)
597 return digest
->get_params(params
);
601 const OSSL_PARAM
*EVP_MD_gettable_params(const EVP_MD
*digest
)
603 if (digest
!= NULL
&& digest
->gettable_params
!= NULL
)
604 return digest
->gettable_params(
605 ossl_provider_ctx(EVP_MD_provider(digest
)));
609 int EVP_MD_CTX_set_params(EVP_MD_CTX
*ctx
, const OSSL_PARAM params
[])
611 EVP_PKEY_CTX
*pctx
= ctx
->pctx
;
613 /* If we have a pctx then we should try that first */
615 && (pctx
->operation
== EVP_PKEY_OP_VERIFYCTX
616 || pctx
->operation
== EVP_PKEY_OP_SIGNCTX
)
617 && pctx
->op
.sig
.sigprovctx
!= NULL
618 && pctx
->op
.sig
.signature
->set_ctx_md_params
!= NULL
)
619 return pctx
->op
.sig
.signature
->set_ctx_md_params(pctx
->op
.sig
.sigprovctx
,
622 if (ctx
->digest
!= NULL
&& ctx
->digest
->set_ctx_params
!= NULL
)
623 return ctx
->digest
->set_ctx_params(ctx
->provctx
, params
);
628 const OSSL_PARAM
*EVP_MD_settable_ctx_params(const EVP_MD
*md
)
630 if (md
!= NULL
&& md
->settable_ctx_params
!= NULL
)
631 return md
->settable_ctx_params(ossl_provider_ctx(EVP_MD_provider(md
)));
635 const OSSL_PARAM
*EVP_MD_CTX_settable_params(EVP_MD_CTX
*ctx
)
642 /* If we have a pctx then we should try that first */
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
->settable_ctx_md_params
!= NULL
)
649 return pctx
->op
.sig
.signature
->settable_ctx_md_params(
650 pctx
->op
.sig
.sigprovctx
);
652 if (ctx
->digest
!= NULL
&& ctx
->digest
->settable_ctx_params
!= NULL
)
653 return ctx
->digest
->settable_ctx_params(
654 ossl_provider_ctx(EVP_MD_provider(ctx
->digest
)));
659 int EVP_MD_CTX_get_params(EVP_MD_CTX
*ctx
, OSSL_PARAM params
[])
661 EVP_PKEY_CTX
*pctx
= ctx
->pctx
;
663 /* If we have a pctx then we should try that first */
665 && (pctx
->operation
== EVP_PKEY_OP_VERIFYCTX
666 || pctx
->operation
== EVP_PKEY_OP_SIGNCTX
)
667 && pctx
->op
.sig
.sigprovctx
!= NULL
668 && pctx
->op
.sig
.signature
->get_ctx_md_params
!= NULL
)
669 return pctx
->op
.sig
.signature
->get_ctx_md_params(pctx
->op
.sig
.sigprovctx
,
672 if (ctx
->digest
!= NULL
&& ctx
->digest
->get_params
!= NULL
)
673 return ctx
->digest
->get_ctx_params(ctx
->provctx
, params
);
678 const OSSL_PARAM
*EVP_MD_gettable_ctx_params(const EVP_MD
*md
)
680 if (md
!= NULL
&& md
->gettable_ctx_params
!= NULL
)
681 return md
->gettable_ctx_params(ossl_provider_ctx(EVP_MD_provider(md
)));
685 const OSSL_PARAM
*EVP_MD_CTX_gettable_params(EVP_MD_CTX
*ctx
)
692 /* If we have a pctx then we should try that first */
695 && (pctx
->operation
== EVP_PKEY_OP_VERIFYCTX
696 || pctx
->operation
== EVP_PKEY_OP_SIGNCTX
)
697 && pctx
->op
.sig
.sigprovctx
!= NULL
698 && pctx
->op
.sig
.signature
->gettable_ctx_md_params
!= NULL
)
699 return pctx
->op
.sig
.signature
->gettable_ctx_md_params(
700 pctx
->op
.sig
.sigprovctx
);
702 if (ctx
->digest
!= NULL
703 && ctx
->digest
->gettable_ctx_params
!= NULL
)
704 return ctx
->digest
->gettable_ctx_params(
705 ossl_provider_ctx(EVP_MD_provider(ctx
->digest
)));
710 /* TODO(3.0): Remove legacy code below - only used by engines & DigestSign */
711 int EVP_MD_CTX_ctrl(EVP_MD_CTX
*ctx
, int cmd
, int p1
, void *p2
)
713 int ret
= EVP_CTRL_RET_UNSUPPORTED
;
716 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
719 ERR_raise(ERR_LIB_EVP
, ERR_R_PASSED_NULL_PARAMETER
);
723 if (ctx
->digest
!= NULL
&& ctx
->digest
->prov
== NULL
)
727 case EVP_MD_CTRL_XOF_LEN
:
729 params
[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN
, &sz
);
731 case EVP_MD_CTRL_MICALG
:
733 params
[0] = OSSL_PARAM_construct_utf8_string(OSSL_DIGEST_PARAM_MICALG
,
736 case EVP_CTRL_SSL3_MASTER_SECRET
:
737 params
[0] = OSSL_PARAM_construct_octet_string(OSSL_DIGEST_PARAM_SSL3_MS
,
745 ret
= EVP_MD_CTX_set_params(ctx
, params
);
747 ret
= EVP_MD_CTX_get_params(ctx
, params
);
751 /* TODO(3.0): Remove legacy code below */
753 if (ctx
->digest
->md_ctrl
== NULL
) {
754 ERR_raise(ERR_LIB_EVP
, EVP_R_CTRL_NOT_IMPLEMENTED
);
758 ret
= ctx
->digest
->md_ctrl(ctx
, cmd
, p1
, p2
);
765 EVP_MD
*evp_md_new(void)
767 EVP_MD
*md
= OPENSSL_zalloc(sizeof(*md
));
770 md
->lock
= CRYPTO_THREAD_lock_new();
771 if (md
->lock
== NULL
) {
781 * FIPS module note: since internal fetches will be entirely
782 * provider based, we know that none of its code depends on legacy
783 * NIDs or any functionality that use them.
786 /* TODO(3.x) get rid of the need for legacy NIDs */
787 static void set_legacy_nid(const char *name
, void *vlegacy_nid
)
790 int *legacy_nid
= vlegacy_nid
;
792 * We use lowest level function to get the associated method, because
793 * higher level functions such as EVP_get_digestbyname() have changed
794 * to look at providers too.
796 const void *legacy_method
= OBJ_NAME_get(name
, OBJ_NAME_TYPE_MD_METH
);
798 if (*legacy_nid
== -1) /* We found a clash already */
801 if (legacy_method
== NULL
)
803 nid
= EVP_MD_nid(legacy_method
);
804 if (*legacy_nid
!= NID_undef
&& *legacy_nid
!= nid
) {
812 static void *evp_md_from_dispatch(int name_id
,
813 const OSSL_DISPATCH
*fns
,
819 /* EVP_MD_fetch() will set the legacy NID if available */
820 if ((md
= evp_md_new()) == NULL
) {
821 EVPerr(0, ERR_R_MALLOC_FAILURE
);
826 /* TODO(3.x) get rid of the need for legacy NIDs */
827 md
->type
= NID_undef
;
828 evp_names_do_all(prov
, name_id
, set_legacy_nid
, &md
->type
);
829 if (md
->type
== -1) {
830 ERR_raise(ERR_LIB_EVP
, ERR_R_INTERNAL_ERROR
);
836 md
->name_id
= name_id
;
838 for (; fns
->function_id
!= 0; fns
++) {
839 switch (fns
->function_id
) {
840 case OSSL_FUNC_DIGEST_NEWCTX
:
841 if (md
->newctx
== NULL
) {
842 md
->newctx
= OSSL_FUNC_digest_newctx(fns
);
846 case OSSL_FUNC_DIGEST_INIT
:
847 if (md
->dinit
== NULL
) {
848 md
->dinit
= OSSL_FUNC_digest_init(fns
);
852 case OSSL_FUNC_DIGEST_UPDATE
:
853 if (md
->dupdate
== NULL
) {
854 md
->dupdate
= OSSL_FUNC_digest_update(fns
);
858 case OSSL_FUNC_DIGEST_FINAL
:
859 if (md
->dfinal
== NULL
) {
860 md
->dfinal
= OSSL_FUNC_digest_final(fns
);
864 case OSSL_FUNC_DIGEST_DIGEST
:
865 if (md
->digest
== NULL
)
866 md
->digest
= OSSL_FUNC_digest_digest(fns
);
867 /* We don't increment fnct for this as it is stand alone */
869 case OSSL_FUNC_DIGEST_FREECTX
:
870 if (md
->freectx
== NULL
) {
871 md
->freectx
= OSSL_FUNC_digest_freectx(fns
);
875 case OSSL_FUNC_DIGEST_DUPCTX
:
876 if (md
->dupctx
== NULL
)
877 md
->dupctx
= OSSL_FUNC_digest_dupctx(fns
);
879 case OSSL_FUNC_DIGEST_GET_PARAMS
:
880 if (md
->get_params
== NULL
)
881 md
->get_params
= OSSL_FUNC_digest_get_params(fns
);
883 case OSSL_FUNC_DIGEST_SET_CTX_PARAMS
:
884 if (md
->set_ctx_params
== NULL
)
885 md
->set_ctx_params
= OSSL_FUNC_digest_set_ctx_params(fns
);
887 case OSSL_FUNC_DIGEST_GET_CTX_PARAMS
:
888 if (md
->get_ctx_params
== NULL
)
889 md
->get_ctx_params
= OSSL_FUNC_digest_get_ctx_params(fns
);
891 case OSSL_FUNC_DIGEST_GETTABLE_PARAMS
:
892 if (md
->gettable_params
== NULL
)
893 md
->gettable_params
= OSSL_FUNC_digest_gettable_params(fns
);
895 case OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS
:
896 if (md
->settable_ctx_params
== NULL
)
897 md
->settable_ctx_params
=
898 OSSL_FUNC_digest_settable_ctx_params(fns
);
900 case OSSL_FUNC_DIGEST_GETTABLE_CTX_PARAMS
:
901 if (md
->gettable_ctx_params
== NULL
)
902 md
->gettable_ctx_params
=
903 OSSL_FUNC_digest_gettable_ctx_params(fns
);
907 if ((fncnt
!= 0 && fncnt
!= 5)
908 || (fncnt
== 0 && md
->digest
== NULL
)) {
910 * In order to be a consistent set of functions we either need the
911 * whole set of init/update/final etc functions or none of them.
912 * The "digest" function can standalone. We at least need one way to
916 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_PROVIDER_FUNCTIONS
);
921 ossl_provider_up_ref(prov
);
926 static int evp_md_up_ref(void *md
)
928 return EVP_MD_up_ref(md
);
931 static void evp_md_free(void *md
)
936 EVP_MD
*EVP_MD_fetch(OPENSSL_CTX
*ctx
, const char *algorithm
,
937 const char *properties
)
940 evp_generic_fetch(ctx
, OSSL_OP_DIGEST
, algorithm
, properties
,
941 evp_md_from_dispatch
, evp_md_up_ref
, evp_md_free
);
946 int EVP_MD_up_ref(EVP_MD
*md
)
950 CRYPTO_UP_REF(&md
->refcnt
, &ref
, md
->lock
);
954 void EVP_MD_free(EVP_MD
*md
)
961 CRYPTO_DOWN_REF(&md
->refcnt
, &i
, md
->lock
);
964 ossl_provider_free(md
->prov
);
965 CRYPTO_THREAD_lock_free(md
->lock
);
969 void EVP_MD_do_all_provided(OPENSSL_CTX
*libctx
,
970 void (*fn
)(EVP_MD
*mac
, void *arg
),
973 evp_generic_do_all(libctx
, OSSL_OP_DIGEST
,
974 (void (*)(void *, void *))fn
, arg
,
975 evp_md_from_dispatch
, evp_md_free
);