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 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 /* TODO(3.0): Remove legacy code below */
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
)
68 /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
70 * pctx should be freed by the user of EVP_MD_CTX
71 * if EVP_MD_CTX_FLAG_KEEP_PKEY_CTX is set
73 if (!EVP_MD_CTX_test_flags(ctx
, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX
)) {
74 EVP_PKEY_CTX_free(ctx
->pctx
);
79 evp_md_ctx_clear_digest(ctx
, 0);
80 OPENSSL_cleanse(ctx
, sizeof(*ctx
));
86 EVP_MD_CTX
*evp_md_ctx_new_ex(EVP_PKEY
*pkey
, const ASN1_OCTET_STRING
*id
,
87 OSSL_LIB_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 ERR_raise(ERR_LIB_ASN1
, ERR_R_MALLOC_FAILURE
);
98 if (id
!= NULL
&& EVP_PKEY_CTX_set1_id(pctx
, id
->data
, id
->length
) <= 0)
101 EVP_MD_CTX_set_pkey_ctx(ctx
, pctx
);
105 EVP_PKEY_CTX_free(pctx
);
106 EVP_MD_CTX_free(ctx
);
111 EVP_MD_CTX
*EVP_MD_CTX_new(void)
113 return OPENSSL_zalloc(sizeof(EVP_MD_CTX
));
116 void EVP_MD_CTX_free(EVP_MD_CTX
*ctx
)
121 EVP_MD_CTX_reset(ctx
);
127 int EVP_DigestInit(EVP_MD_CTX
*ctx
, const EVP_MD
*type
)
129 EVP_MD_CTX_reset(ctx
);
130 return EVP_DigestInit_ex(ctx
, type
, NULL
);
133 int EVP_DigestInit_ex(EVP_MD_CTX
*ctx
, const EVP_MD
*type
, ENGINE
*impl
)
135 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
136 ENGINE
*tmpimpl
= NULL
;
139 #if !defined(FIPS_MODULE)
140 if (ctx
->pctx
!= NULL
141 && EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
->pctx
)
142 && ctx
->pctx
->op
.sig
.sigprovctx
!= NULL
) {
144 * Prior to OpenSSL 3.0 calling EVP_DigestInit_ex() on an mdctx
145 * previously initialised with EVP_DigestSignInit() would retain
146 * information about the key, and re-initialise for another sign
147 * operation. So in that case we redirect to EVP_DigestSignInit()
149 if (ctx
->pctx
->operation
== EVP_PKEY_OP_SIGNCTX
)
150 return EVP_DigestSignInit(ctx
, NULL
, type
, impl
, NULL
);
151 if (ctx
->pctx
->operation
== EVP_PKEY_OP_VERIFYCTX
)
152 return EVP_DigestVerifyInit(ctx
, NULL
, type
, impl
, NULL
);
153 ERR_raise(ERR_LIB_EVP
, EVP_R_UPDATE_ERROR
);
158 EVP_MD_CTX_clear_flags(ctx
, EVP_MD_CTX_FLAG_CLEANED
);
160 if (ctx
->provctx
!= NULL
) {
161 if (!ossl_assert(ctx
->digest
!= NULL
)) {
162 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
165 if (ctx
->digest
->freectx
!= NULL
)
166 ctx
->digest
->freectx(ctx
->provctx
);
171 ctx
->reqdigest
= type
;
173 if (ctx
->digest
== NULL
) {
174 ERR_raise(ERR_LIB_EVP
, EVP_R_NO_DIGEST_SET
);
180 /* TODO(3.0): Legacy work around code below. Remove this */
181 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
183 * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
184 * this context may already have an ENGINE! Try to avoid releasing the
185 * previous handle, re-querying for an ENGINE, and having a
186 * reinitialisation, when it may all be unnecessary.
188 if (ctx
->engine
&& ctx
->digest
&&
189 (type
== NULL
|| (type
->type
== ctx
->digest
->type
)))
194 * Ensure an ENGINE left lying around from last time is cleared (the
195 * previous check attempted to avoid this if the same ENGINE and
196 * EVP_MD could be used).
198 ENGINE_finish(ctx
->engine
);
202 if (type
!= NULL
&& impl
== NULL
)
203 tmpimpl
= ENGINE_get_digest_engine(type
->type
);
207 * If there are engines involved or EVP_MD_CTX_FLAG_NO_INIT is set then we
208 * should use legacy handling for now.
210 if (ctx
->engine
!= NULL
212 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
215 || (ctx
->flags
& EVP_MD_CTX_FLAG_NO_INIT
) != 0) {
216 if (ctx
->digest
== ctx
->fetched_digest
)
218 EVP_MD_free(ctx
->fetched_digest
);
219 ctx
->fetched_digest
= NULL
;
223 if (ctx
->digest
!= NULL
&& ctx
->digest
->ctx_size
> 0) {
224 OPENSSL_clear_free(ctx
->md_data
, ctx
->digest
->ctx_size
);
228 /* TODO(3.0): Start of non-legacy code below */
230 if (type
->prov
== NULL
) {
232 /* We only do explicit fetches inside the FIPS module */
233 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
236 EVP_MD
*provmd
= EVP_MD_fetch(NULL
, OBJ_nid2sn(type
->type
), "");
241 EVP_MD_free(ctx
->fetched_digest
);
242 ctx
->fetched_digest
= provmd
;
246 if (ctx
->provctx
!= NULL
&& ctx
->digest
!= NULL
&& ctx
->digest
!= type
) {
247 if (ctx
->digest
->freectx
!= NULL
)
248 ctx
->digest
->freectx(ctx
->provctx
);
252 if (ctx
->provctx
== NULL
) {
253 ctx
->provctx
= ctx
->digest
->newctx(ossl_provider_ctx(type
->prov
));
254 if (ctx
->provctx
== NULL
) {
255 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
260 if (ctx
->digest
->dinit
== NULL
) {
261 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
265 return ctx
->digest
->dinit(ctx
->provctx
);
267 /* TODO(3.0): Remove legacy code below */
270 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
273 if (!ENGINE_init(impl
)) {
274 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
278 /* Ask if an ENGINE is reserved for this job */
282 /* There's an ENGINE for this job ... (apparently) */
283 const EVP_MD
*d
= ENGINE_get_digest(impl
, type
->type
);
286 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
290 /* We'll use the ENGINE's private digest definition */
293 * Store the ENGINE functional reference so we know 'type' came
294 * from an ENGINE and we need to release it when done.
301 if (ctx
->digest
!= type
) {
302 if (ctx
->digest
&& ctx
->digest
->ctx_size
) {
303 OPENSSL_clear_free(ctx
->md_data
, ctx
->digest
->ctx_size
);
307 if (!(ctx
->flags
& EVP_MD_CTX_FLAG_NO_INIT
) && type
->ctx_size
) {
308 ctx
->update
= type
->update
;
309 ctx
->md_data
= OPENSSL_zalloc(type
->ctx_size
);
310 if (ctx
->md_data
== NULL
) {
311 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
316 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
321 * TODO(3.0): Temporarily no support for EVP_DigestSign* inside FIPS module
322 * or when using providers.
324 if (ctx
->pctx
!= NULL
325 && (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
->pctx
)
326 || ctx
->pctx
->op
.sig
.signature
== NULL
)) {
328 r
= EVP_PKEY_CTX_ctrl(ctx
->pctx
, -1, EVP_PKEY_OP_TYPE_SIG
,
329 EVP_PKEY_CTRL_DIGESTINIT
, 0, ctx
);
330 if (r
<= 0 && (r
!= -2))
334 if (ctx
->flags
& EVP_MD_CTX_FLAG_NO_INIT
)
336 return ctx
->digest
->init(ctx
);
339 int EVP_DigestUpdate(EVP_MD_CTX
*ctx
, const void *data
, size_t count
)
344 if (ctx
->pctx
!= NULL
345 && EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
->pctx
)
346 && ctx
->pctx
->op
.sig
.sigprovctx
!= NULL
) {
348 * Prior to OpenSSL 3.0 EVP_DigestSignUpdate() and
349 * EVP_DigestVerifyUpdate() were just macros for EVP_DigestUpdate().
350 * Some code calls EVP_DigestUpdate() directly even when initialised
351 * with EVP_DigestSignInit_ex() or
352 * EVP_DigestVerifyInit_ex(), so we detect that and redirect to
353 * the correct EVP_Digest*Update() function
355 if (ctx
->pctx
->operation
== EVP_PKEY_OP_SIGNCTX
)
356 return EVP_DigestSignUpdate(ctx
, data
, count
);
357 if (ctx
->pctx
->operation
== EVP_PKEY_OP_VERIFYCTX
)
358 return EVP_DigestVerifyUpdate(ctx
, data
, count
);
359 ERR_raise(ERR_LIB_EVP
, EVP_R_UPDATE_ERROR
);
363 if (ctx
->digest
== NULL
364 || ctx
->digest
->prov
== NULL
365 || (ctx
->flags
& EVP_MD_CTX_FLAG_NO_INIT
) != 0)
368 if (ctx
->digest
->dupdate
== NULL
) {
369 ERR_raise(ERR_LIB_EVP
, EVP_R_UPDATE_ERROR
);
372 return ctx
->digest
->dupdate(ctx
->provctx
, data
, count
);
374 /* TODO(3.0): Remove legacy code below */
376 return ctx
->update(ctx
, data
, count
);
379 /* The caller can assume that this removes any secret data from the context */
380 int EVP_DigestFinal(EVP_MD_CTX
*ctx
, unsigned char *md
, unsigned int *size
)
383 ret
= EVP_DigestFinal_ex(ctx
, md
, size
);
384 EVP_MD_CTX_reset(ctx
);
388 /* The caller can assume that this removes any secret data from the context */
389 int EVP_DigestFinal_ex(EVP_MD_CTX
*ctx
, unsigned char *md
, unsigned int *isize
)
395 if (ctx
->digest
== NULL
)
398 sz
= EVP_MD_size(ctx
->digest
);
402 if (ctx
->digest
->prov
== NULL
)
405 if (ctx
->digest
->dfinal
== NULL
) {
406 ERR_raise(ERR_LIB_EVP
, EVP_R_FINAL_ERROR
);
410 ret
= ctx
->digest
->dfinal(ctx
->provctx
, md
, &size
, mdsize
);
413 if (size
<= UINT_MAX
) {
416 ERR_raise(ERR_LIB_EVP
, EVP_R_FINAL_ERROR
);
423 /* TODO(3.0): Remove legacy code below */
425 OPENSSL_assert(mdsize
<= EVP_MAX_MD_SIZE
);
426 ret
= ctx
->digest
->final(ctx
, md
);
429 if (ctx
->digest
->cleanup
) {
430 ctx
->digest
->cleanup(ctx
);
431 EVP_MD_CTX_set_flags(ctx
, EVP_MD_CTX_FLAG_CLEANED
);
433 OPENSSL_cleanse(ctx
->md_data
, ctx
->digest
->ctx_size
);
437 int EVP_DigestFinalXOF(EVP_MD_CTX
*ctx
, unsigned char *md
, size_t size
)
440 OSSL_PARAM params
[2];
443 if (ctx
->digest
== NULL
|| ctx
->digest
->prov
== NULL
)
446 if (ctx
->digest
->dfinal
== NULL
) {
447 ERR_raise(ERR_LIB_EVP
, EVP_R_FINAL_ERROR
);
451 params
[i
++] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN
, &size
);
452 params
[i
++] = OSSL_PARAM_construct_end();
454 if (EVP_MD_CTX_set_params(ctx
, params
) > 0)
455 ret
= ctx
->digest
->dfinal(ctx
->provctx
, md
, &size
, size
);
460 if (ctx
->digest
->flags
& EVP_MD_FLAG_XOF
462 && ctx
->digest
->md_ctrl(ctx
, EVP_MD_CTRL_XOF_LEN
, (int)size
, NULL
)) {
463 ret
= ctx
->digest
->final(ctx
, md
);
464 if (ctx
->digest
->cleanup
!= NULL
) {
465 ctx
->digest
->cleanup(ctx
);
466 EVP_MD_CTX_set_flags(ctx
, EVP_MD_CTX_FLAG_CLEANED
);
468 OPENSSL_cleanse(ctx
->md_data
, ctx
->digest
->ctx_size
);
470 ERR_raise(ERR_LIB_EVP
, EVP_R_NOT_XOF_OR_INVALID_LENGTH
);
476 int EVP_MD_CTX_copy(EVP_MD_CTX
*out
, const EVP_MD_CTX
*in
)
478 EVP_MD_CTX_reset(out
);
479 return EVP_MD_CTX_copy_ex(out
, in
);
482 int EVP_MD_CTX_copy_ex(EVP_MD_CTX
*out
, const EVP_MD_CTX
*in
)
484 unsigned char *tmp_buf
;
486 if (in
== NULL
|| in
->digest
== NULL
) {
487 ERR_raise(ERR_LIB_EVP
, EVP_R_INPUT_NOT_INITIALIZED
);
491 if (in
->digest
->prov
== NULL
492 || (in
->flags
& EVP_MD_CTX_FLAG_NO_INIT
) != 0)
495 if (in
->digest
->dupctx
== NULL
) {
496 ERR_raise(ERR_LIB_EVP
, EVP_R_NOT_ABLE_TO_COPY_CTX
);
500 EVP_MD_CTX_reset(out
);
501 if (out
->fetched_digest
!= NULL
)
502 EVP_MD_free(out
->fetched_digest
);
504 /* NULL out pointers in case of error */
508 if (in
->fetched_digest
!= NULL
)
509 EVP_MD_up_ref(in
->fetched_digest
);
511 if (in
->provctx
!= NULL
) {
512 out
->provctx
= in
->digest
->dupctx(in
->provctx
);
513 if (out
->provctx
== NULL
) {
514 ERR_raise(ERR_LIB_EVP
, EVP_R_NOT_ABLE_TO_COPY_CTX
);
519 /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
520 EVP_MD_CTX_clear_flags(out
, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX
);
522 /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
523 if (in
->pctx
!= NULL
) {
524 out
->pctx
= EVP_PKEY_CTX_dup(in
->pctx
);
525 if (out
->pctx
== NULL
) {
526 ERR_raise(ERR_LIB_EVP
, EVP_R_NOT_ABLE_TO_COPY_CTX
);
527 EVP_MD_CTX_reset(out
);
535 /* TODO(3.0): Remove legacy code below */
537 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
538 /* Make sure it's safe to copy a digest context using an ENGINE */
539 if (in
->engine
&& !ENGINE_init(in
->engine
)) {
540 ERR_raise(ERR_LIB_EVP
, ERR_R_ENGINE_LIB
);
545 if (out
->digest
== in
->digest
) {
546 tmp_buf
= out
->md_data
;
547 EVP_MD_CTX_set_flags(out
, EVP_MD_CTX_FLAG_REUSE
);
550 EVP_MD_CTX_reset(out
);
551 memcpy(out
, in
, sizeof(*out
));
553 /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
554 EVP_MD_CTX_clear_flags(out
, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX
);
556 /* Null these variables, since they are getting fixed up
557 * properly below. Anything else may cause a memleak and/or
558 * double free if any of the memory allocations below fail
563 if (in
->md_data
&& out
->digest
->ctx_size
) {
565 out
->md_data
= tmp_buf
;
567 out
->md_data
= OPENSSL_malloc(out
->digest
->ctx_size
);
568 if (out
->md_data
== NULL
) {
569 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
573 memcpy(out
->md_data
, in
->md_data
, out
->digest
->ctx_size
);
576 out
->update
= in
->update
;
579 /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
581 out
->pctx
= EVP_PKEY_CTX_dup(in
->pctx
);
583 EVP_MD_CTX_reset(out
);
589 if (out
->digest
->copy
)
590 return out
->digest
->copy(out
, in
);
595 int EVP_Digest(const void *data
, size_t count
,
596 unsigned char *md
, unsigned int *size
, const EVP_MD
*type
,
599 EVP_MD_CTX
*ctx
= EVP_MD_CTX_new();
604 EVP_MD_CTX_set_flags(ctx
, EVP_MD_CTX_FLAG_ONESHOT
);
605 ret
= EVP_DigestInit_ex(ctx
, type
, impl
)
606 && EVP_DigestUpdate(ctx
, data
, count
)
607 && EVP_DigestFinal_ex(ctx
, md
, size
);
608 EVP_MD_CTX_free(ctx
);
613 int EVP_MD_get_params(const EVP_MD
*digest
, OSSL_PARAM params
[])
615 if (digest
!= NULL
&& digest
->get_params
!= NULL
)
616 return digest
->get_params(params
);
620 const OSSL_PARAM
*EVP_MD_gettable_params(const EVP_MD
*digest
)
622 if (digest
!= NULL
&& digest
->gettable_params
!= NULL
)
623 return digest
->gettable_params(
624 ossl_provider_ctx(EVP_MD_provider(digest
)));
628 int EVP_MD_CTX_set_params(EVP_MD_CTX
*ctx
, const OSSL_PARAM params
[])
630 EVP_PKEY_CTX
*pctx
= ctx
->pctx
;
632 /* If we have a pctx then we should try that first */
634 && (pctx
->operation
== EVP_PKEY_OP_VERIFYCTX
635 || pctx
->operation
== EVP_PKEY_OP_SIGNCTX
)
636 && pctx
->op
.sig
.sigprovctx
!= NULL
637 && pctx
->op
.sig
.signature
->set_ctx_md_params
!= NULL
)
638 return pctx
->op
.sig
.signature
->set_ctx_md_params(pctx
->op
.sig
.sigprovctx
,
641 if (ctx
->digest
!= NULL
&& ctx
->digest
->set_ctx_params
!= NULL
)
642 return ctx
->digest
->set_ctx_params(ctx
->provctx
, params
);
647 const OSSL_PARAM
*EVP_MD_settable_ctx_params(const EVP_MD
*md
)
649 if (md
!= NULL
&& md
->settable_ctx_params
!= NULL
)
650 return md
->settable_ctx_params(ossl_provider_ctx(EVP_MD_provider(md
)));
654 const OSSL_PARAM
*EVP_MD_CTX_settable_params(EVP_MD_CTX
*ctx
)
661 /* If we have a pctx then we should try that first */
664 && (pctx
->operation
== EVP_PKEY_OP_VERIFYCTX
665 || pctx
->operation
== EVP_PKEY_OP_SIGNCTX
)
666 && pctx
->op
.sig
.sigprovctx
!= NULL
667 && pctx
->op
.sig
.signature
->settable_ctx_md_params
!= NULL
)
668 return pctx
->op
.sig
.signature
->settable_ctx_md_params(
669 pctx
->op
.sig
.sigprovctx
);
671 if (ctx
->digest
!= NULL
&& ctx
->digest
->settable_ctx_params
!= NULL
)
672 return ctx
->digest
->settable_ctx_params(
673 ossl_provider_ctx(EVP_MD_provider(ctx
->digest
)));
678 int EVP_MD_CTX_get_params(EVP_MD_CTX
*ctx
, OSSL_PARAM params
[])
680 EVP_PKEY_CTX
*pctx
= ctx
->pctx
;
682 /* If we have a pctx then we should try that first */
684 && (pctx
->operation
== EVP_PKEY_OP_VERIFYCTX
685 || pctx
->operation
== EVP_PKEY_OP_SIGNCTX
)
686 && pctx
->op
.sig
.sigprovctx
!= NULL
687 && pctx
->op
.sig
.signature
->get_ctx_md_params
!= NULL
)
688 return pctx
->op
.sig
.signature
->get_ctx_md_params(pctx
->op
.sig
.sigprovctx
,
691 if (ctx
->digest
!= NULL
&& ctx
->digest
->get_params
!= NULL
)
692 return ctx
->digest
->get_ctx_params(ctx
->provctx
, params
);
697 const OSSL_PARAM
*EVP_MD_gettable_ctx_params(const EVP_MD
*md
)
699 if (md
!= NULL
&& md
->gettable_ctx_params
!= NULL
)
700 return md
->gettable_ctx_params(ossl_provider_ctx(EVP_MD_provider(md
)));
704 const OSSL_PARAM
*EVP_MD_CTX_gettable_params(EVP_MD_CTX
*ctx
)
711 /* If we have a pctx then we should try that first */
714 && (pctx
->operation
== EVP_PKEY_OP_VERIFYCTX
715 || pctx
->operation
== EVP_PKEY_OP_SIGNCTX
)
716 && pctx
->op
.sig
.sigprovctx
!= NULL
717 && pctx
->op
.sig
.signature
->gettable_ctx_md_params
!= NULL
)
718 return pctx
->op
.sig
.signature
->gettable_ctx_md_params(
719 pctx
->op
.sig
.sigprovctx
);
721 if (ctx
->digest
!= NULL
722 && ctx
->digest
->gettable_ctx_params
!= NULL
)
723 return ctx
->digest
->gettable_ctx_params(
724 ossl_provider_ctx(EVP_MD_provider(ctx
->digest
)));
729 /* TODO(3.0): Remove legacy code below - only used by engines & DigestSign */
730 int EVP_MD_CTX_ctrl(EVP_MD_CTX
*ctx
, int cmd
, int p1
, void *p2
)
732 int ret
= EVP_CTRL_RET_UNSUPPORTED
;
735 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
738 ERR_raise(ERR_LIB_EVP
, ERR_R_PASSED_NULL_PARAMETER
);
742 if (ctx
->digest
!= NULL
&& ctx
->digest
->prov
== NULL
)
746 case EVP_MD_CTRL_XOF_LEN
:
748 params
[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN
, &sz
);
750 case EVP_MD_CTRL_MICALG
:
752 params
[0] = OSSL_PARAM_construct_utf8_string(OSSL_DIGEST_PARAM_MICALG
,
755 case EVP_CTRL_SSL3_MASTER_SECRET
:
756 params
[0] = OSSL_PARAM_construct_octet_string(OSSL_DIGEST_PARAM_SSL3_MS
,
764 ret
= EVP_MD_CTX_set_params(ctx
, params
);
766 ret
= EVP_MD_CTX_get_params(ctx
, params
);
770 /* TODO(3.0): Remove legacy code below */
772 if (ctx
->digest
->md_ctrl
== NULL
) {
773 ERR_raise(ERR_LIB_EVP
, EVP_R_CTRL_NOT_IMPLEMENTED
);
777 ret
= ctx
->digest
->md_ctrl(ctx
, cmd
, p1
, p2
);
784 EVP_MD
*evp_md_new(void)
786 EVP_MD
*md
= OPENSSL_zalloc(sizeof(*md
));
789 md
->lock
= CRYPTO_THREAD_lock_new();
790 if (md
->lock
== NULL
) {
800 * FIPS module note: since internal fetches will be entirely
801 * provider based, we know that none of its code depends on legacy
802 * NIDs or any functionality that use them.
805 /* TODO(3.x) get rid of the need for legacy NIDs */
806 static void set_legacy_nid(const char *name
, void *vlegacy_nid
)
809 int *legacy_nid
= vlegacy_nid
;
811 * We use lowest level function to get the associated method, because
812 * higher level functions such as EVP_get_digestbyname() have changed
813 * to look at providers too.
815 const void *legacy_method
= OBJ_NAME_get(name
, OBJ_NAME_TYPE_MD_METH
);
817 if (*legacy_nid
== -1) /* We found a clash already */
820 if (legacy_method
== NULL
)
822 nid
= EVP_MD_nid(legacy_method
);
823 if (*legacy_nid
!= NID_undef
&& *legacy_nid
!= nid
) {
831 static int evp_md_cache_constants(EVP_MD
*md
)
833 int ok
, xof
= 0, algid_absent
= 0;
836 OSSL_PARAM params
[5];
838 params
[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_BLOCK_SIZE
, &blksz
);
839 params
[1] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_SIZE
, &mdsize
);
840 params
[2] = OSSL_PARAM_construct_int(OSSL_DIGEST_PARAM_XOF
, &xof
);
841 params
[3] = OSSL_PARAM_construct_int(OSSL_DIGEST_PARAM_ALGID_ABSENT
,
843 params
[4] = OSSL_PARAM_construct_end();
844 ok
= evp_do_md_getparams(md
, params
);
845 if (mdsize
> INT_MAX
|| blksz
> INT_MAX
)
848 md
->block_size
= (int)blksz
;
849 md
->md_size
= (int)mdsize
;
851 md
->flags
|= EVP_MD_FLAG_XOF
;
853 md
->flags
|= EVP_MD_FLAG_DIGALGID_ABSENT
;
858 static void *evp_md_from_dispatch(int name_id
,
859 const OSSL_DISPATCH
*fns
,
865 /* EVP_MD_fetch() will set the legacy NID if available */
866 if ((md
= evp_md_new()) == NULL
) {
867 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
872 /* TODO(3.x) get rid of the need for legacy NIDs */
873 md
->type
= NID_undef
;
874 evp_names_do_all(prov
, name_id
, set_legacy_nid
, &md
->type
);
875 if (md
->type
== -1) {
876 ERR_raise(ERR_LIB_EVP
, ERR_R_INTERNAL_ERROR
);
882 md
->name_id
= name_id
;
884 for (; fns
->function_id
!= 0; fns
++) {
885 switch (fns
->function_id
) {
886 case OSSL_FUNC_DIGEST_NEWCTX
:
887 if (md
->newctx
== NULL
) {
888 md
->newctx
= OSSL_FUNC_digest_newctx(fns
);
892 case OSSL_FUNC_DIGEST_INIT
:
893 if (md
->dinit
== NULL
) {
894 md
->dinit
= OSSL_FUNC_digest_init(fns
);
898 case OSSL_FUNC_DIGEST_UPDATE
:
899 if (md
->dupdate
== NULL
) {
900 md
->dupdate
= OSSL_FUNC_digest_update(fns
);
904 case OSSL_FUNC_DIGEST_FINAL
:
905 if (md
->dfinal
== NULL
) {
906 md
->dfinal
= OSSL_FUNC_digest_final(fns
);
910 case OSSL_FUNC_DIGEST_DIGEST
:
911 if (md
->digest
== NULL
)
912 md
->digest
= OSSL_FUNC_digest_digest(fns
);
913 /* We don't increment fnct for this as it is stand alone */
915 case OSSL_FUNC_DIGEST_FREECTX
:
916 if (md
->freectx
== NULL
) {
917 md
->freectx
= OSSL_FUNC_digest_freectx(fns
);
921 case OSSL_FUNC_DIGEST_DUPCTX
:
922 if (md
->dupctx
== NULL
)
923 md
->dupctx
= OSSL_FUNC_digest_dupctx(fns
);
925 case OSSL_FUNC_DIGEST_GET_PARAMS
:
926 if (md
->get_params
== NULL
)
927 md
->get_params
= OSSL_FUNC_digest_get_params(fns
);
929 case OSSL_FUNC_DIGEST_SET_CTX_PARAMS
:
930 if (md
->set_ctx_params
== NULL
)
931 md
->set_ctx_params
= OSSL_FUNC_digest_set_ctx_params(fns
);
933 case OSSL_FUNC_DIGEST_GET_CTX_PARAMS
:
934 if (md
->get_ctx_params
== NULL
)
935 md
->get_ctx_params
= OSSL_FUNC_digest_get_ctx_params(fns
);
937 case OSSL_FUNC_DIGEST_GETTABLE_PARAMS
:
938 if (md
->gettable_params
== NULL
)
939 md
->gettable_params
= OSSL_FUNC_digest_gettable_params(fns
);
941 case OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS
:
942 if (md
->settable_ctx_params
== NULL
)
943 md
->settable_ctx_params
=
944 OSSL_FUNC_digest_settable_ctx_params(fns
);
946 case OSSL_FUNC_DIGEST_GETTABLE_CTX_PARAMS
:
947 if (md
->gettable_ctx_params
== NULL
)
948 md
->gettable_ctx_params
=
949 OSSL_FUNC_digest_gettable_ctx_params(fns
);
953 if ((fncnt
!= 0 && fncnt
!= 5)
954 || (fncnt
== 0 && md
->digest
== NULL
)) {
956 * In order to be a consistent set of functions we either need the
957 * whole set of init/update/final etc functions or none of them.
958 * The "digest" function can standalone. We at least need one way to
962 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_PROVIDER_FUNCTIONS
);
967 ossl_provider_up_ref(prov
);
969 if (!evp_md_cache_constants(md
)) {
971 ERR_raise(ERR_LIB_EVP
, EVP_R_CACHE_CONSTANTS_FAILED
);
978 static int evp_md_up_ref(void *md
)
980 return EVP_MD_up_ref(md
);
983 static void evp_md_free(void *md
)
988 EVP_MD
*EVP_MD_fetch(OSSL_LIB_CTX
*ctx
, const char *algorithm
,
989 const char *properties
)
992 evp_generic_fetch(ctx
, OSSL_OP_DIGEST
, algorithm
, properties
,
993 evp_md_from_dispatch
, evp_md_up_ref
, evp_md_free
);
998 int EVP_MD_up_ref(EVP_MD
*md
)
1002 CRYPTO_UP_REF(&md
->refcnt
, &ref
, md
->lock
);
1006 void EVP_MD_free(EVP_MD
*md
)
1013 CRYPTO_DOWN_REF(&md
->refcnt
, &i
, md
->lock
);
1016 ossl_provider_free(md
->prov
);
1017 CRYPTO_THREAD_lock_free(md
->lock
);
1021 void EVP_MD_do_all_provided(OSSL_LIB_CTX
*libctx
,
1022 void (*fn
)(EVP_MD
*mac
, void *arg
),
1025 evp_generic_do_all(libctx
, OSSL_OP_DIGEST
,
1026 (void (*)(void *, void *))fn
, arg
,
1027 evp_md_from_dispatch
, evp_md_free
);