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 #if !defined(FIPS_MODULE)
144 if (ctx
->pctx
!= NULL
145 && EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
->pctx
)
146 && ctx
->pctx
->op
.sig
.sigprovctx
!= NULL
) {
148 * Prior to OpenSSL 3.0 calling EVP_DigestInit_ex() on an mdctx
149 * previously initialised with EVP_DigestSignInit() would retain
150 * information about the key, and re-initialise for another sign
151 * operation. So in that case we redirect to EVP_DigestSignInit()
153 if (ctx
->pctx
->operation
== EVP_PKEY_OP_SIGNCTX
)
154 return EVP_DigestSignInit(ctx
, NULL
, type
, impl
, NULL
);
155 if (ctx
->pctx
->operation
== EVP_PKEY_OP_VERIFYCTX
)
156 return EVP_DigestVerifyInit(ctx
, NULL
, type
, impl
, NULL
);
157 EVPerr(0, EVP_R_UPDATE_ERROR
);
162 EVP_MD_CTX_clear_flags(ctx
, EVP_MD_CTX_FLAG_CLEANED
);
164 if (ctx
->provctx
!= NULL
) {
165 if (!ossl_assert(ctx
->digest
!= NULL
)) {
166 EVPerr(EVP_F_EVP_DIGESTINIT_EX
, EVP_R_INITIALIZATION_ERROR
);
169 if (ctx
->digest
->freectx
!= NULL
)
170 ctx
->digest
->freectx(ctx
->provctx
);
175 ctx
->reqdigest
= type
;
177 /* TODO(3.0): Legacy work around code below. Remove this */
178 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
180 * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
181 * this context may already have an ENGINE! Try to avoid releasing the
182 * previous handle, re-querying for an ENGINE, and having a
183 * reinitialisation, when it may all be unnecessary.
185 if (ctx
->engine
&& ctx
->digest
&&
186 (type
== NULL
|| (type
->type
== ctx
->digest
->type
)))
191 * Ensure an ENGINE left lying around from last time is cleared (the
192 * previous check attempted to avoid this if the same ENGINE and
193 * EVP_MD could be used).
195 ENGINE_finish(ctx
->engine
);
199 if (type
!= NULL
&& impl
== NULL
)
200 tmpimpl
= ENGINE_get_digest_engine(type
->type
);
204 * If there are engines involved or EVP_MD_CTX_FLAG_NO_INIT is set then we
205 * should use legacy handling for now.
207 if (ctx
->engine
!= NULL
209 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
212 || (ctx
->flags
& EVP_MD_CTX_FLAG_NO_INIT
) != 0) {
213 if (ctx
->digest
== ctx
->fetched_digest
)
215 EVP_MD_free(ctx
->fetched_digest
);
216 ctx
->fetched_digest
= NULL
;
220 if (ctx
->digest
!= NULL
&& ctx
->digest
->ctx_size
> 0) {
221 OPENSSL_clear_free(ctx
->md_data
, ctx
->digest
->ctx_size
);
225 /* TODO(3.0): Start of non-legacy code below */
227 if (type
->prov
== NULL
) {
229 /* We only do explicit fetches inside the FIPS module */
230 EVPerr(EVP_F_EVP_DIGESTINIT_EX
, EVP_R_INITIALIZATION_ERROR
);
233 EVP_MD
*provmd
= EVP_MD_fetch(NULL
, OBJ_nid2sn(type
->type
), "");
238 EVP_MD_free(ctx
->fetched_digest
);
239 ctx
->fetched_digest
= provmd
;
243 if (ctx
->provctx
!= NULL
&& ctx
->digest
!= NULL
&& ctx
->digest
!= type
) {
244 if (ctx
->digest
->freectx
!= NULL
)
245 ctx
->digest
->freectx(ctx
->provctx
);
249 if (ctx
->provctx
== NULL
) {
250 ctx
->provctx
= ctx
->digest
->newctx(ossl_provider_ctx(type
->prov
));
251 if (ctx
->provctx
== NULL
) {
252 EVPerr(EVP_F_EVP_DIGESTINIT_EX
, EVP_R_INITIALIZATION_ERROR
);
257 if (ctx
->digest
->dinit
== NULL
) {
258 EVPerr(EVP_F_EVP_DIGESTINIT_EX
, EVP_R_INITIALIZATION_ERROR
);
262 return ctx
->digest
->dinit(ctx
->provctx
);
264 /* TODO(3.0): Remove legacy code below */
267 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
270 if (!ENGINE_init(impl
)) {
271 EVPerr(EVP_F_EVP_DIGESTINIT_EX
, EVP_R_INITIALIZATION_ERROR
);
275 /* Ask if an ENGINE is reserved for this job */
279 /* There's an ENGINE for this job ... (apparently) */
280 const EVP_MD
*d
= ENGINE_get_digest(impl
, type
->type
);
283 EVPerr(EVP_F_EVP_DIGESTINIT_EX
, EVP_R_INITIALIZATION_ERROR
);
287 /* We'll use the ENGINE's private digest definition */
290 * Store the ENGINE functional reference so we know 'type' came
291 * from an ENGINE and we need to release it when done.
298 EVPerr(EVP_F_EVP_DIGESTINIT_EX
, EVP_R_NO_DIGEST_SET
);
304 if (ctx
->digest
!= type
) {
305 if (ctx
->digest
&& ctx
->digest
->ctx_size
) {
306 OPENSSL_clear_free(ctx
->md_data
, ctx
->digest
->ctx_size
);
310 if (!(ctx
->flags
& EVP_MD_CTX_FLAG_NO_INIT
) && type
->ctx_size
) {
311 ctx
->update
= type
->update
;
312 ctx
->md_data
= OPENSSL_zalloc(type
->ctx_size
);
313 if (ctx
->md_data
== NULL
) {
314 EVPerr(EVP_F_EVP_DIGESTINIT_EX
, ERR_R_MALLOC_FAILURE
);
319 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
324 * TODO(3.0): Temporarily no support for EVP_DigestSign* inside FIPS module
325 * or when using providers.
327 if (ctx
->pctx
!= NULL
328 && (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
->pctx
)
329 || ctx
->pctx
->op
.sig
.signature
== NULL
)) {
331 r
= EVP_PKEY_CTX_ctrl(ctx
->pctx
, -1, EVP_PKEY_OP_TYPE_SIG
,
332 EVP_PKEY_CTRL_DIGESTINIT
, 0, ctx
);
333 if (r
<= 0 && (r
!= -2))
337 if (ctx
->flags
& EVP_MD_CTX_FLAG_NO_INIT
)
339 return ctx
->digest
->init(ctx
);
342 int EVP_DigestUpdate(EVP_MD_CTX
*ctx
, const void *data
, size_t count
)
347 if (ctx
->pctx
!= NULL
348 && EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
->pctx
)
349 && ctx
->pctx
->op
.sig
.sigprovctx
!= NULL
) {
351 * Prior to OpenSSL 3.0 EVP_DigestSignUpdate() and
352 * EVP_DigestVerifyUpdate() were just macros for EVP_DigestUpdate().
353 * Some code calls EVP_DigestUpdate() directly even when initialised
354 * with EVP_DigestSignInit_with_libctx() or
355 * EVP_DigestVerifyInit_with_libctx(), so we detect that and redirect to
356 * the correct EVP_Digest*Update() function
358 if (ctx
->pctx
->operation
== EVP_PKEY_OP_SIGNCTX
)
359 return EVP_DigestSignUpdate(ctx
, data
, count
);
360 if (ctx
->pctx
->operation
== EVP_PKEY_OP_VERIFYCTX
)
361 return EVP_DigestVerifyUpdate(ctx
, data
, count
);
362 EVPerr(EVP_F_EVP_DIGESTUPDATE
, EVP_R_UPDATE_ERROR
);
366 if (ctx
->digest
== NULL
367 || ctx
->digest
->prov
== NULL
368 || (ctx
->flags
& EVP_MD_CTX_FLAG_NO_INIT
) != 0)
371 if (ctx
->digest
->dupdate
== NULL
) {
372 EVPerr(EVP_F_EVP_DIGESTUPDATE
, EVP_R_UPDATE_ERROR
);
375 return ctx
->digest
->dupdate(ctx
->provctx
, data
, count
);
377 /* TODO(3.0): Remove legacy code below */
379 return ctx
->update(ctx
, data
, count
);
382 /* The caller can assume that this removes any secret data from the context */
383 int EVP_DigestFinal(EVP_MD_CTX
*ctx
, unsigned char *md
, unsigned int *size
)
386 ret
= EVP_DigestFinal_ex(ctx
, md
, size
);
387 EVP_MD_CTX_reset(ctx
);
391 /* The caller can assume that this removes any secret data from the context */
392 int EVP_DigestFinal_ex(EVP_MD_CTX
*ctx
, unsigned char *md
, unsigned int *isize
)
398 if (ctx
->digest
== NULL
)
401 sz
= EVP_MD_size(ctx
->digest
);
405 if (ctx
->digest
->prov
== NULL
)
408 if (ctx
->digest
->dfinal
== NULL
) {
409 EVPerr(EVP_F_EVP_DIGESTFINAL_EX
, EVP_R_FINAL_ERROR
);
413 ret
= ctx
->digest
->dfinal(ctx
->provctx
, md
, &size
, mdsize
);
416 if (size
<= UINT_MAX
) {
419 EVPerr(EVP_F_EVP_DIGESTFINAL_EX
, EVP_R_FINAL_ERROR
);
426 /* TODO(3.0): Remove legacy code below */
428 OPENSSL_assert(mdsize
<= EVP_MAX_MD_SIZE
);
429 ret
= ctx
->digest
->final(ctx
, md
);
432 if (ctx
->digest
->cleanup
) {
433 ctx
->digest
->cleanup(ctx
);
434 EVP_MD_CTX_set_flags(ctx
, EVP_MD_CTX_FLAG_CLEANED
);
436 OPENSSL_cleanse(ctx
->md_data
, ctx
->digest
->ctx_size
);
440 int EVP_DigestFinalXOF(EVP_MD_CTX
*ctx
, unsigned char *md
, size_t size
)
443 OSSL_PARAM params
[2];
446 if (ctx
->digest
== NULL
|| ctx
->digest
->prov
== NULL
)
449 if (ctx
->digest
->dfinal
== NULL
) {
450 EVPerr(EVP_F_EVP_DIGESTFINALXOF
, EVP_R_FINAL_ERROR
);
454 params
[i
++] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN
, &size
);
455 params
[i
++] = OSSL_PARAM_construct_end();
457 if (EVP_MD_CTX_set_params(ctx
, params
) > 0)
458 ret
= ctx
->digest
->dfinal(ctx
->provctx
, md
, &size
, size
);
459 EVP_MD_CTX_reset(ctx
);
463 if (ctx
->digest
->flags
& EVP_MD_FLAG_XOF
465 && ctx
->digest
->md_ctrl(ctx
, EVP_MD_CTRL_XOF_LEN
, (int)size
, NULL
)) {
466 ret
= ctx
->digest
->final(ctx
, md
);
467 if (ctx
->digest
->cleanup
!= NULL
) {
468 ctx
->digest
->cleanup(ctx
);
469 EVP_MD_CTX_set_flags(ctx
, EVP_MD_CTX_FLAG_CLEANED
);
471 OPENSSL_cleanse(ctx
->md_data
, ctx
->digest
->ctx_size
);
473 EVPerr(EVP_F_EVP_DIGESTFINALXOF
, EVP_R_NOT_XOF_OR_INVALID_LENGTH
);
479 int EVP_MD_CTX_copy(EVP_MD_CTX
*out
, const EVP_MD_CTX
*in
)
481 EVP_MD_CTX_reset(out
);
482 return EVP_MD_CTX_copy_ex(out
, in
);
485 int EVP_MD_CTX_copy_ex(EVP_MD_CTX
*out
, const EVP_MD_CTX
*in
)
487 unsigned char *tmp_buf
;
489 if (in
== NULL
|| in
->digest
== NULL
) {
490 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX
, EVP_R_INPUT_NOT_INITIALIZED
);
494 if (in
->digest
->prov
== NULL
495 || (in
->flags
& EVP_MD_CTX_FLAG_NO_INIT
) != 0)
498 if (in
->digest
->dupctx
== NULL
) {
499 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX
, EVP_R_NOT_ABLE_TO_COPY_CTX
);
503 EVP_MD_CTX_reset(out
);
504 if (out
->fetched_digest
!= NULL
)
505 EVP_MD_free(out
->fetched_digest
);
507 /* NULL out pointers in case of error */
511 if (in
->fetched_digest
!= NULL
)
512 EVP_MD_up_ref(in
->fetched_digest
);
514 if (in
->provctx
!= NULL
) {
515 out
->provctx
= in
->digest
->dupctx(in
->provctx
);
516 if (out
->provctx
== NULL
) {
517 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX
, EVP_R_NOT_ABLE_TO_COPY_CTX
);
522 /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
523 EVP_MD_CTX_clear_flags(out
, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX
);
525 /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
526 if (in
->pctx
!= NULL
) {
527 out
->pctx
= EVP_PKEY_CTX_dup(in
->pctx
);
528 if (out
->pctx
== NULL
) {
529 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX
, EVP_R_NOT_ABLE_TO_COPY_CTX
);
530 EVP_MD_CTX_reset(out
);
538 /* TODO(3.0): Remove legacy code below */
540 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
541 /* Make sure it's safe to copy a digest context using an ENGINE */
542 if (in
->engine
&& !ENGINE_init(in
->engine
)) {
543 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX
, ERR_R_ENGINE_LIB
);
548 if (out
->digest
== in
->digest
) {
549 tmp_buf
= out
->md_data
;
550 EVP_MD_CTX_set_flags(out
, EVP_MD_CTX_FLAG_REUSE
);
553 EVP_MD_CTX_reset(out
);
554 memcpy(out
, in
, sizeof(*out
));
556 /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
557 EVP_MD_CTX_clear_flags(out
, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX
);
559 /* Null these variables, since they are getting fixed up
560 * properly below. Anything else may cause a memleak and/or
561 * double free if any of the memory allocations below fail
566 if (in
->md_data
&& out
->digest
->ctx_size
) {
568 out
->md_data
= tmp_buf
;
570 out
->md_data
= OPENSSL_malloc(out
->digest
->ctx_size
);
571 if (out
->md_data
== NULL
) {
572 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX
, ERR_R_MALLOC_FAILURE
);
576 memcpy(out
->md_data
, in
->md_data
, out
->digest
->ctx_size
);
579 out
->update
= in
->update
;
582 /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
584 out
->pctx
= EVP_PKEY_CTX_dup(in
->pctx
);
586 EVP_MD_CTX_reset(out
);
592 if (out
->digest
->copy
)
593 return out
->digest
->copy(out
, in
);
598 int EVP_Digest(const void *data
, size_t count
,
599 unsigned char *md
, unsigned int *size
, const EVP_MD
*type
,
602 EVP_MD_CTX
*ctx
= EVP_MD_CTX_new();
607 EVP_MD_CTX_set_flags(ctx
, EVP_MD_CTX_FLAG_ONESHOT
);
608 ret
= EVP_DigestInit_ex(ctx
, type
, impl
)
609 && EVP_DigestUpdate(ctx
, data
, count
)
610 && EVP_DigestFinal_ex(ctx
, md
, size
);
611 EVP_MD_CTX_free(ctx
);
616 int EVP_MD_get_params(const EVP_MD
*digest
, OSSL_PARAM params
[])
618 if (digest
!= NULL
&& digest
->get_params
!= NULL
)
619 return digest
->get_params(params
);
623 const OSSL_PARAM
*EVP_MD_gettable_params(const EVP_MD
*digest
)
625 if (digest
!= NULL
&& digest
->gettable_params
!= NULL
)
626 return digest
->gettable_params(
627 ossl_provider_ctx(EVP_MD_provider(digest
)));
631 int EVP_MD_CTX_set_params(EVP_MD_CTX
*ctx
, const OSSL_PARAM params
[])
633 EVP_PKEY_CTX
*pctx
= ctx
->pctx
;
635 /* If we have a pctx then we should try that first */
637 && (pctx
->operation
== EVP_PKEY_OP_VERIFYCTX
638 || pctx
->operation
== EVP_PKEY_OP_SIGNCTX
)
639 && pctx
->op
.sig
.sigprovctx
!= NULL
640 && pctx
->op
.sig
.signature
->set_ctx_md_params
!= NULL
)
641 return pctx
->op
.sig
.signature
->set_ctx_md_params(pctx
->op
.sig
.sigprovctx
,
644 if (ctx
->digest
!= NULL
&& ctx
->digest
->set_ctx_params
!= NULL
)
645 return ctx
->digest
->set_ctx_params(ctx
->provctx
, params
);
650 const OSSL_PARAM
*EVP_MD_settable_ctx_params(const EVP_MD
*md
)
652 if (md
!= NULL
&& md
->settable_ctx_params
!= NULL
)
653 return md
->settable_ctx_params(ossl_provider_ctx(EVP_MD_provider(md
)));
657 const OSSL_PARAM
*EVP_MD_CTX_settable_params(EVP_MD_CTX
*ctx
)
664 /* If we have a pctx then we should try that first */
667 && (pctx
->operation
== EVP_PKEY_OP_VERIFYCTX
668 || pctx
->operation
== EVP_PKEY_OP_SIGNCTX
)
669 && pctx
->op
.sig
.sigprovctx
!= NULL
670 && pctx
->op
.sig
.signature
->settable_ctx_md_params
!= NULL
)
671 return pctx
->op
.sig
.signature
->settable_ctx_md_params(
672 pctx
->op
.sig
.sigprovctx
);
674 if (ctx
->digest
!= NULL
&& ctx
->digest
->settable_ctx_params
!= NULL
)
675 return ctx
->digest
->settable_ctx_params(
676 ossl_provider_ctx(EVP_MD_provider(ctx
->digest
)));
681 int EVP_MD_CTX_get_params(EVP_MD_CTX
*ctx
, OSSL_PARAM params
[])
683 EVP_PKEY_CTX
*pctx
= ctx
->pctx
;
685 /* 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
->get_ctx_md_params
!= NULL
)
691 return pctx
->op
.sig
.signature
->get_ctx_md_params(pctx
->op
.sig
.sigprovctx
,
694 if (ctx
->digest
!= NULL
&& ctx
->digest
->get_params
!= NULL
)
695 return ctx
->digest
->get_ctx_params(ctx
->provctx
, params
);
700 const OSSL_PARAM
*EVP_MD_gettable_ctx_params(const EVP_MD
*md
)
702 if (md
!= NULL
&& md
->gettable_ctx_params
!= NULL
)
703 return md
->gettable_ctx_params(ossl_provider_ctx(EVP_MD_provider(md
)));
707 const OSSL_PARAM
*EVP_MD_CTX_gettable_params(EVP_MD_CTX
*ctx
)
714 /* If we have a pctx then we should try that first */
717 && (pctx
->operation
== EVP_PKEY_OP_VERIFYCTX
718 || pctx
->operation
== EVP_PKEY_OP_SIGNCTX
)
719 && pctx
->op
.sig
.sigprovctx
!= NULL
720 && pctx
->op
.sig
.signature
->gettable_ctx_md_params
!= NULL
)
721 return pctx
->op
.sig
.signature
->gettable_ctx_md_params(
722 pctx
->op
.sig
.sigprovctx
);
724 if (ctx
->digest
!= NULL
725 && ctx
->digest
->gettable_ctx_params
!= NULL
)
726 return ctx
->digest
->gettable_ctx_params(
727 ossl_provider_ctx(EVP_MD_provider(ctx
->digest
)));
732 /* TODO(3.0): Remove legacy code below - only used by engines & DigestSign */
733 int EVP_MD_CTX_ctrl(EVP_MD_CTX
*ctx
, int cmd
, int p1
, void *p2
)
735 int ret
= EVP_CTRL_RET_UNSUPPORTED
;
738 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
741 ERR_raise(ERR_LIB_EVP
, ERR_R_PASSED_NULL_PARAMETER
);
745 if (ctx
->digest
!= NULL
&& ctx
->digest
->prov
== NULL
)
749 case EVP_MD_CTRL_XOF_LEN
:
751 params
[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN
, &sz
);
753 case EVP_MD_CTRL_MICALG
:
755 params
[0] = OSSL_PARAM_construct_utf8_string(OSSL_DIGEST_PARAM_MICALG
,
758 case EVP_CTRL_SSL3_MASTER_SECRET
:
759 params
[0] = OSSL_PARAM_construct_octet_string(OSSL_DIGEST_PARAM_SSL3_MS
,
767 ret
= EVP_MD_CTX_set_params(ctx
, params
);
769 ret
= EVP_MD_CTX_get_params(ctx
, params
);
773 /* TODO(3.0): Remove legacy code below */
775 if (ctx
->digest
->md_ctrl
== NULL
) {
776 ERR_raise(ERR_LIB_EVP
, EVP_R_CTRL_NOT_IMPLEMENTED
);
780 ret
= ctx
->digest
->md_ctrl(ctx
, cmd
, p1
, p2
);
787 EVP_MD
*evp_md_new(void)
789 EVP_MD
*md
= OPENSSL_zalloc(sizeof(*md
));
792 md
->lock
= CRYPTO_THREAD_lock_new();
793 if (md
->lock
== NULL
) {
803 * FIPS module note: since internal fetches will be entirely
804 * provider based, we know that none of its code depends on legacy
805 * NIDs or any functionality that use them.
808 /* TODO(3.x) get rid of the need for legacy NIDs */
809 static void set_legacy_nid(const char *name
, void *vlegacy_nid
)
812 int *legacy_nid
= vlegacy_nid
;
814 * We use lowest level function to get the associated method, because
815 * higher level functions such as EVP_get_digestbyname() have changed
816 * to look at providers too.
818 const void *legacy_method
= OBJ_NAME_get(name
, OBJ_NAME_TYPE_MD_METH
);
820 if (*legacy_nid
== -1) /* We found a clash already */
823 if (legacy_method
== NULL
)
825 nid
= EVP_MD_nid(legacy_method
);
826 if (*legacy_nid
!= NID_undef
&& *legacy_nid
!= nid
) {
834 static void *evp_md_from_dispatch(int name_id
,
835 const OSSL_DISPATCH
*fns
,
841 /* EVP_MD_fetch() will set the legacy NID if available */
842 if ((md
= evp_md_new()) == NULL
) {
843 EVPerr(0, ERR_R_MALLOC_FAILURE
);
848 /* TODO(3.x) get rid of the need for legacy NIDs */
849 md
->type
= NID_undef
;
850 evp_names_do_all(prov
, name_id
, set_legacy_nid
, &md
->type
);
851 if (md
->type
== -1) {
852 ERR_raise(ERR_LIB_EVP
, ERR_R_INTERNAL_ERROR
);
858 md
->name_id
= name_id
;
860 for (; fns
->function_id
!= 0; fns
++) {
861 switch (fns
->function_id
) {
862 case OSSL_FUNC_DIGEST_NEWCTX
:
863 if (md
->newctx
== NULL
) {
864 md
->newctx
= OSSL_FUNC_digest_newctx(fns
);
868 case OSSL_FUNC_DIGEST_INIT
:
869 if (md
->dinit
== NULL
) {
870 md
->dinit
= OSSL_FUNC_digest_init(fns
);
874 case OSSL_FUNC_DIGEST_UPDATE
:
875 if (md
->dupdate
== NULL
) {
876 md
->dupdate
= OSSL_FUNC_digest_update(fns
);
880 case OSSL_FUNC_DIGEST_FINAL
:
881 if (md
->dfinal
== NULL
) {
882 md
->dfinal
= OSSL_FUNC_digest_final(fns
);
886 case OSSL_FUNC_DIGEST_DIGEST
:
887 if (md
->digest
== NULL
)
888 md
->digest
= OSSL_FUNC_digest_digest(fns
);
889 /* We don't increment fnct for this as it is stand alone */
891 case OSSL_FUNC_DIGEST_FREECTX
:
892 if (md
->freectx
== NULL
) {
893 md
->freectx
= OSSL_FUNC_digest_freectx(fns
);
897 case OSSL_FUNC_DIGEST_DUPCTX
:
898 if (md
->dupctx
== NULL
)
899 md
->dupctx
= OSSL_FUNC_digest_dupctx(fns
);
901 case OSSL_FUNC_DIGEST_GET_PARAMS
:
902 if (md
->get_params
== NULL
)
903 md
->get_params
= OSSL_FUNC_digest_get_params(fns
);
905 case OSSL_FUNC_DIGEST_SET_CTX_PARAMS
:
906 if (md
->set_ctx_params
== NULL
)
907 md
->set_ctx_params
= OSSL_FUNC_digest_set_ctx_params(fns
);
909 case OSSL_FUNC_DIGEST_GET_CTX_PARAMS
:
910 if (md
->get_ctx_params
== NULL
)
911 md
->get_ctx_params
= OSSL_FUNC_digest_get_ctx_params(fns
);
913 case OSSL_FUNC_DIGEST_GETTABLE_PARAMS
:
914 if (md
->gettable_params
== NULL
)
915 md
->gettable_params
= OSSL_FUNC_digest_gettable_params(fns
);
917 case OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS
:
918 if (md
->settable_ctx_params
== NULL
)
919 md
->settable_ctx_params
=
920 OSSL_FUNC_digest_settable_ctx_params(fns
);
922 case OSSL_FUNC_DIGEST_GETTABLE_CTX_PARAMS
:
923 if (md
->gettable_ctx_params
== NULL
)
924 md
->gettable_ctx_params
=
925 OSSL_FUNC_digest_gettable_ctx_params(fns
);
929 if ((fncnt
!= 0 && fncnt
!= 5)
930 || (fncnt
== 0 && md
->digest
== NULL
)) {
932 * In order to be a consistent set of functions we either need the
933 * whole set of init/update/final etc functions or none of them.
934 * The "digest" function can standalone. We at least need one way to
938 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_PROVIDER_FUNCTIONS
);
943 ossl_provider_up_ref(prov
);
948 static int evp_md_up_ref(void *md
)
950 return EVP_MD_up_ref(md
);
953 static void evp_md_free(void *md
)
958 EVP_MD
*EVP_MD_fetch(OPENSSL_CTX
*ctx
, const char *algorithm
,
959 const char *properties
)
962 evp_generic_fetch(ctx
, OSSL_OP_DIGEST
, algorithm
, properties
,
963 evp_md_from_dispatch
, evp_md_up_ref
, evp_md_free
);
968 int EVP_MD_up_ref(EVP_MD
*md
)
972 CRYPTO_UP_REF(&md
->refcnt
, &ref
, md
->lock
);
976 void EVP_MD_free(EVP_MD
*md
)
983 CRYPTO_DOWN_REF(&md
->refcnt
, &i
, md
->lock
);
986 ossl_provider_free(md
->prov
);
987 CRYPTO_THREAD_lock_free(md
->lock
);
991 void EVP_MD_do_all_provided(OPENSSL_CTX
*libctx
,
992 void (*fn
)(EVP_MD
*mac
, void *arg
),
995 evp_generic_do_all(libctx
, OSSL_OP_DIGEST
,
996 (void (*)(void *, void *))fn
, arg
,
997 evp_md_from_dispatch
, evp_md_free
);