2 * Copyright 1995-2022 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>
18 # include <openssl/engine.h>
20 #include <openssl/params.h>
21 #include <openssl/core_names.h>
22 #include "internal/cryptlib.h"
23 #include "internal/provider.h"
24 #include "internal/core.h"
25 #include "crypto/evp.h"
26 #include "evp_local.h"
28 static void cleanup_old_md_data(EVP_MD_CTX
*ctx
, int force
)
30 if (ctx
->digest
!= NULL
) {
31 if (ctx
->digest
->cleanup
!= NULL
32 && !EVP_MD_CTX_test_flags(ctx
, EVP_MD_CTX_FLAG_CLEANED
))
33 ctx
->digest
->cleanup(ctx
);
34 if (ctx
->md_data
!= NULL
&& ctx
->digest
->ctx_size
> 0
35 && (!EVP_MD_CTX_test_flags(ctx
, EVP_MD_CTX_FLAG_REUSE
)
37 OPENSSL_clear_free(ctx
->md_data
, ctx
->digest
->ctx_size
);
43 void evp_md_ctx_clear_digest(EVP_MD_CTX
*ctx
, int force
, int keep_fetched
)
45 if (ctx
->algctx
!= NULL
) {
46 if (ctx
->digest
!= NULL
&& ctx
->digest
->freectx
!= NULL
)
47 ctx
->digest
->freectx(ctx
->algctx
);
49 EVP_MD_CTX_set_flags(ctx
, EVP_MD_CTX_FLAG_CLEANED
);
52 /* Code below to be removed when legacy support is dropped. */
55 * Don't assume ctx->md_data was cleaned in EVP_Digest_Final, because
56 * sometimes only copies of the context are ever finalised.
58 cleanup_old_md_data(ctx
, force
);
62 #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
63 ENGINE_finish(ctx
->engine
);
67 /* Non legacy code, this has to be later than the ctx->digest cleaning */
69 EVP_MD_free(ctx
->fetched_digest
);
70 ctx
->fetched_digest
= NULL
;
71 ctx
->reqdigest
= NULL
;
75 static int evp_md_ctx_reset_ex(EVP_MD_CTX
*ctx
, int keep_fetched
)
82 * pctx should be freed by the user of EVP_MD_CTX
83 * if EVP_MD_CTX_FLAG_KEEP_PKEY_CTX is set
85 if (!EVP_MD_CTX_test_flags(ctx
, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX
)) {
86 EVP_PKEY_CTX_free(ctx
->pctx
);
91 evp_md_ctx_clear_digest(ctx
, 0, keep_fetched
);
93 OPENSSL_cleanse(ctx
, sizeof(*ctx
));
98 /* This call frees resources associated with the context */
99 int EVP_MD_CTX_reset(EVP_MD_CTX
*ctx
)
101 return evp_md_ctx_reset_ex(ctx
, 0);
105 EVP_MD_CTX
*evp_md_ctx_new_ex(EVP_PKEY
*pkey
, const ASN1_OCTET_STRING
*id
,
106 OSSL_LIB_CTX
*libctx
, const char *propq
)
109 EVP_PKEY_CTX
*pctx
= NULL
;
111 if ((ctx
= EVP_MD_CTX_new()) == NULL
112 || (pctx
= EVP_PKEY_CTX_new_from_pkey(libctx
, pkey
, propq
)) == NULL
) {
113 ERR_raise(ERR_LIB_ASN1
, ERR_R_EVP_LIB
);
117 if (id
!= NULL
&& EVP_PKEY_CTX_set1_id(pctx
, id
->data
, id
->length
) <= 0)
120 EVP_MD_CTX_set_pkey_ctx(ctx
, pctx
);
124 EVP_PKEY_CTX_free(pctx
);
125 EVP_MD_CTX_free(ctx
);
130 EVP_MD_CTX
*EVP_MD_CTX_new(void)
132 return OPENSSL_zalloc(sizeof(EVP_MD_CTX
));
135 void EVP_MD_CTX_free(EVP_MD_CTX
*ctx
)
140 EVP_MD_CTX_reset(ctx
);
144 int evp_md_ctx_free_algctx(EVP_MD_CTX
*ctx
)
146 if (ctx
->algctx
!= NULL
) {
147 if (!ossl_assert(ctx
->digest
!= NULL
)) {
148 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
151 if (ctx
->digest
->freectx
!= NULL
)
152 ctx
->digest
->freectx(ctx
->algctx
);
158 static int evp_md_init_internal(EVP_MD_CTX
*ctx
, const EVP_MD
*type
,
159 const OSSL_PARAM params
[], ENGINE
*impl
)
161 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
162 ENGINE
*tmpimpl
= NULL
;
165 #if !defined(FIPS_MODULE)
166 if (ctx
->pctx
!= NULL
167 && EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
->pctx
)
168 && ctx
->pctx
->op
.sig
.algctx
!= NULL
) {
170 * Prior to OpenSSL 3.0 calling EVP_DigestInit_ex() on an mdctx
171 * previously initialised with EVP_DigestSignInit() would retain
172 * information about the key, and re-initialise for another sign
173 * operation. So in that case we redirect to EVP_DigestSignInit()
175 if (ctx
->pctx
->operation
== EVP_PKEY_OP_SIGNCTX
)
176 return EVP_DigestSignInit(ctx
, NULL
, type
, impl
, NULL
);
177 if (ctx
->pctx
->operation
== EVP_PKEY_OP_VERIFYCTX
)
178 return EVP_DigestVerifyInit(ctx
, NULL
, type
, impl
, NULL
);
179 ERR_raise(ERR_LIB_EVP
, EVP_R_UPDATE_ERROR
);
184 EVP_MD_CTX_clear_flags(ctx
, EVP_MD_CTX_FLAG_CLEANED
);
187 ctx
->reqdigest
= type
;
189 if (ctx
->digest
== NULL
) {
190 ERR_raise(ERR_LIB_EVP
, EVP_R_NO_DIGEST_SET
);
196 /* Code below to be removed when legacy support is dropped. */
197 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
199 * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
200 * this context may already have an ENGINE! Try to avoid releasing the
201 * previous handle, re-querying for an ENGINE, and having a
202 * reinitialisation, when it may all be unnecessary.
204 if (ctx
->engine
!= NULL
205 && ctx
->digest
!= NULL
206 && type
->type
== ctx
->digest
->type
)
210 * Ensure an ENGINE left lying around from last time is cleared (the
211 * previous check attempted to avoid this if the same ENGINE and
212 * EVP_MD could be used).
214 ENGINE_finish(ctx
->engine
);
218 tmpimpl
= ENGINE_get_digest_engine(type
->type
);
222 * If there are engines involved or EVP_MD_CTX_FLAG_NO_INIT is set then we
223 * should use legacy handling for now.
226 #if !defined(OPENSSL_NO_ENGINE)
227 || ctx
->engine
!= NULL
228 # if !defined(FIPS_MODULE)
232 || (ctx
->flags
& EVP_MD_CTX_FLAG_NO_INIT
) != 0
233 || (type
!= NULL
&& type
->origin
== EVP_ORIG_METH
)
234 || (type
== NULL
&& ctx
->digest
!= NULL
235 && ctx
->digest
->origin
== EVP_ORIG_METH
)) {
236 /* If we were using provided hash before, cleanup algctx */
237 if (!evp_md_ctx_free_algctx(ctx
))
239 if (ctx
->digest
== ctx
->fetched_digest
)
241 EVP_MD_free(ctx
->fetched_digest
);
242 ctx
->fetched_digest
= NULL
;
246 cleanup_old_md_data(ctx
, 1);
248 /* Start of non-legacy code below */
249 if (ctx
->digest
== type
) {
250 if (!ossl_assert(type
->prov
!= NULL
)) {
251 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
255 if (!evp_md_ctx_free_algctx(ctx
))
259 if (type
->prov
== NULL
) {
261 /* We only do explicit fetches inside the FIPS module */
262 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
265 /* The NULL digest is a special case */
266 EVP_MD
*provmd
= EVP_MD_fetch(NULL
,
267 type
->type
!= NID_undef
? OBJ_nid2sn(type
->type
)
270 if (provmd
== NULL
) {
271 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
275 EVP_MD_free(ctx
->fetched_digest
);
276 ctx
->fetched_digest
= provmd
;
280 if (type
->prov
!= NULL
&& ctx
->fetched_digest
!= type
) {
281 if (!EVP_MD_up_ref((EVP_MD
*)type
)) {
282 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
285 EVP_MD_free(ctx
->fetched_digest
);
286 ctx
->fetched_digest
= (EVP_MD
*)type
;
289 if (ctx
->algctx
== NULL
) {
290 ctx
->algctx
= ctx
->digest
->newctx(ossl_provider_ctx(type
->prov
));
291 if (ctx
->algctx
== NULL
) {
292 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
297 if (ctx
->digest
->dinit
== NULL
) {
298 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
302 return ctx
->digest
->dinit(ctx
->algctx
, params
);
304 /* Code below to be removed when legacy support is dropped. */
307 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
310 if (!ENGINE_init(impl
)) {
311 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
315 /* Ask if an ENGINE is reserved for this job */
319 /* There's an ENGINE for this job ... (apparently) */
320 const EVP_MD
*d
= ENGINE_get_digest(impl
, type
->type
);
323 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
327 /* We'll use the ENGINE's private digest definition */
330 * Store the ENGINE functional reference so we know 'type' came
331 * from an ENGINE and we need to release it when done.
338 if (ctx
->digest
!= type
) {
339 cleanup_old_md_data(ctx
, 1);
342 if (!(ctx
->flags
& EVP_MD_CTX_FLAG_NO_INIT
) && type
->ctx_size
) {
343 ctx
->update
= type
->update
;
344 ctx
->md_data
= OPENSSL_zalloc(type
->ctx_size
);
345 if (ctx
->md_data
== NULL
)
349 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
353 if (ctx
->pctx
!= NULL
354 && (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
->pctx
)
355 || ctx
->pctx
->op
.sig
.signature
== NULL
)) {
357 r
= EVP_PKEY_CTX_ctrl(ctx
->pctx
, -1, EVP_PKEY_OP_TYPE_SIG
,
358 EVP_PKEY_CTRL_DIGESTINIT
, 0, ctx
);
359 if (r
<= 0 && (r
!= -2))
363 if (ctx
->flags
& EVP_MD_CTX_FLAG_NO_INIT
)
365 return ctx
->digest
->init(ctx
);
368 int EVP_DigestInit_ex2(EVP_MD_CTX
*ctx
, const EVP_MD
*type
,
369 const OSSL_PARAM params
[])
371 return evp_md_init_internal(ctx
, type
, params
, NULL
);
374 int EVP_DigestInit(EVP_MD_CTX
*ctx
, const EVP_MD
*type
)
376 EVP_MD_CTX_reset(ctx
);
377 return evp_md_init_internal(ctx
, type
, NULL
, NULL
);
380 int EVP_DigestInit_ex(EVP_MD_CTX
*ctx
, const EVP_MD
*type
, ENGINE
*impl
)
382 return evp_md_init_internal(ctx
, type
, NULL
, impl
);
385 int EVP_DigestUpdate(EVP_MD_CTX
*ctx
, const void *data
, size_t count
)
390 if (ctx
->pctx
!= NULL
391 && EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx
->pctx
)
392 && ctx
->pctx
->op
.sig
.algctx
!= NULL
) {
394 * Prior to OpenSSL 3.0 EVP_DigestSignUpdate() and
395 * EVP_DigestVerifyUpdate() were just macros for EVP_DigestUpdate().
396 * Some code calls EVP_DigestUpdate() directly even when initialised
397 * with EVP_DigestSignInit_ex() or
398 * EVP_DigestVerifyInit_ex(), so we detect that and redirect to
399 * the correct EVP_Digest*Update() function
401 if (ctx
->pctx
->operation
== EVP_PKEY_OP_SIGNCTX
)
402 return EVP_DigestSignUpdate(ctx
, data
, count
);
403 if (ctx
->pctx
->operation
== EVP_PKEY_OP_VERIFYCTX
)
404 return EVP_DigestVerifyUpdate(ctx
, data
, count
);
405 ERR_raise(ERR_LIB_EVP
, EVP_R_UPDATE_ERROR
);
409 if (ctx
->digest
== NULL
410 || ctx
->digest
->prov
== NULL
411 || (ctx
->flags
& EVP_MD_CTX_FLAG_NO_INIT
) != 0)
414 if (ctx
->digest
->dupdate
== NULL
) {
415 ERR_raise(ERR_LIB_EVP
, EVP_R_UPDATE_ERROR
);
418 return ctx
->digest
->dupdate(ctx
->algctx
, data
, count
);
420 /* Code below to be removed when legacy support is dropped. */
422 return ctx
->update(ctx
, data
, count
);
425 /* The caller can assume that this removes any secret data from the context */
426 int EVP_DigestFinal(EVP_MD_CTX
*ctx
, unsigned char *md
, unsigned int *size
)
429 ret
= EVP_DigestFinal_ex(ctx
, md
, size
);
430 EVP_MD_CTX_reset(ctx
);
434 /* The caller can assume that this removes any secret data from the context */
435 int EVP_DigestFinal_ex(EVP_MD_CTX
*ctx
, unsigned char *md
, unsigned int *isize
)
441 if (ctx
->digest
== NULL
)
444 sz
= EVP_MD_get_size(ctx
->digest
);
448 if (ctx
->digest
->prov
== NULL
)
451 if (ctx
->digest
->dfinal
== NULL
) {
452 ERR_raise(ERR_LIB_EVP
, EVP_R_FINAL_ERROR
);
456 ret
= ctx
->digest
->dfinal(ctx
->algctx
, md
, &size
, mdsize
);
459 if (size
<= UINT_MAX
) {
462 ERR_raise(ERR_LIB_EVP
, EVP_R_FINAL_ERROR
);
469 /* Code below to be removed when legacy support is dropped. */
471 OPENSSL_assert(mdsize
<= EVP_MAX_MD_SIZE
);
472 ret
= ctx
->digest
->final(ctx
, md
);
475 if (ctx
->digest
->cleanup
) {
476 ctx
->digest
->cleanup(ctx
);
477 EVP_MD_CTX_set_flags(ctx
, EVP_MD_CTX_FLAG_CLEANED
);
479 OPENSSL_cleanse(ctx
->md_data
, ctx
->digest
->ctx_size
);
483 int EVP_DigestFinalXOF(EVP_MD_CTX
*ctx
, unsigned char *md
, size_t size
)
486 OSSL_PARAM params
[2];
489 if (ctx
->digest
== NULL
) {
490 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_NULL_ALGORITHM
);
494 if (ctx
->digest
->prov
== NULL
)
497 if (ctx
->digest
->dfinal
== NULL
) {
498 ERR_raise(ERR_LIB_EVP
, EVP_R_FINAL_ERROR
);
502 params
[i
++] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN
, &size
);
503 params
[i
++] = OSSL_PARAM_construct_end();
505 if (EVP_MD_CTX_set_params(ctx
, params
) > 0)
506 ret
= ctx
->digest
->dfinal(ctx
->algctx
, md
, &size
, size
);
511 if (ctx
->digest
->flags
& EVP_MD_FLAG_XOF
513 && ctx
->digest
->md_ctrl(ctx
, EVP_MD_CTRL_XOF_LEN
, (int)size
, NULL
)) {
514 ret
= ctx
->digest
->final(ctx
, md
);
515 if (ctx
->digest
->cleanup
!= NULL
) {
516 ctx
->digest
->cleanup(ctx
);
517 EVP_MD_CTX_set_flags(ctx
, EVP_MD_CTX_FLAG_CLEANED
);
519 OPENSSL_cleanse(ctx
->md_data
, ctx
->digest
->ctx_size
);
521 ERR_raise(ERR_LIB_EVP
, EVP_R_NOT_XOF_OR_INVALID_LENGTH
);
527 EVP_MD_CTX
*EVP_MD_CTX_dup(const EVP_MD_CTX
*in
)
529 EVP_MD_CTX
*out
= EVP_MD_CTX_new();
531 if (out
!= NULL
&& !EVP_MD_CTX_copy_ex(out
, in
)) {
532 EVP_MD_CTX_free(out
);
538 int EVP_MD_CTX_copy(EVP_MD_CTX
*out
, const EVP_MD_CTX
*in
)
540 EVP_MD_CTX_reset(out
);
541 return EVP_MD_CTX_copy_ex(out
, in
);
544 int EVP_MD_CTX_copy_ex(EVP_MD_CTX
*out
, const EVP_MD_CTX
*in
)
546 int digest_change
= 0;
547 unsigned char *tmp_buf
;
550 ERR_raise(ERR_LIB_EVP
, ERR_R_PASSED_NULL_PARAMETER
);
554 if (in
->digest
== NULL
) {
555 /* copying uninitialized digest context */
556 EVP_MD_CTX_reset(out
);
557 if (out
->fetched_digest
!= NULL
)
558 EVP_MD_free(out
->fetched_digest
);
563 if (in
->digest
->prov
== NULL
564 || (in
->flags
& EVP_MD_CTX_FLAG_NO_INIT
) != 0)
567 if (in
->digest
->dupctx
== NULL
) {
568 ERR_raise(ERR_LIB_EVP
, EVP_R_NOT_ABLE_TO_COPY_CTX
);
572 evp_md_ctx_reset_ex(out
, 1);
573 digest_change
= (out
->fetched_digest
!= in
->fetched_digest
);
574 if (digest_change
&& out
->fetched_digest
!= NULL
)
575 EVP_MD_free(out
->fetched_digest
);
577 /* NULL out pointers in case of error */
581 if (digest_change
&& in
->fetched_digest
!= NULL
)
582 EVP_MD_up_ref(in
->fetched_digest
);
584 if (in
->algctx
!= NULL
) {
585 out
->algctx
= in
->digest
->dupctx(in
->algctx
);
586 if (out
->algctx
== NULL
) {
587 ERR_raise(ERR_LIB_EVP
, EVP_R_NOT_ABLE_TO_COPY_CTX
);
593 /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
594 EVP_MD_CTX_clear_flags(out
, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX
);
596 if (in
->pctx
!= NULL
) {
597 out
->pctx
= EVP_PKEY_CTX_dup(in
->pctx
);
598 if (out
->pctx
== NULL
) {
599 ERR_raise(ERR_LIB_EVP
, EVP_R_NOT_ABLE_TO_COPY_CTX
);
600 EVP_MD_CTX_reset(out
);
608 /* Code below to be removed when legacy support is dropped. */
610 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
611 /* Make sure it's safe to copy a digest context using an ENGINE */
612 if (in
->engine
&& !ENGINE_init(in
->engine
)) {
613 ERR_raise(ERR_LIB_EVP
, ERR_R_ENGINE_LIB
);
618 if (out
->digest
== in
->digest
) {
619 tmp_buf
= out
->md_data
;
620 EVP_MD_CTX_set_flags(out
, EVP_MD_CTX_FLAG_REUSE
);
623 EVP_MD_CTX_reset(out
);
624 memcpy(out
, in
, sizeof(*out
));
626 /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
627 EVP_MD_CTX_clear_flags(out
, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX
);
629 /* Null these variables, since they are getting fixed up
630 * properly below. Anything else may cause a memleak and/or
631 * double free if any of the memory allocations below fail
636 if (in
->md_data
&& out
->digest
->ctx_size
) {
638 out
->md_data
= tmp_buf
;
640 out
->md_data
= OPENSSL_malloc(out
->digest
->ctx_size
);
641 if (out
->md_data
== NULL
)
644 memcpy(out
->md_data
, in
->md_data
, out
->digest
->ctx_size
);
647 out
->update
= in
->update
;
651 out
->pctx
= EVP_PKEY_CTX_dup(in
->pctx
);
653 EVP_MD_CTX_reset(out
);
659 if (out
->digest
->copy
)
660 return out
->digest
->copy(out
, in
);
665 int EVP_Digest(const void *data
, size_t count
,
666 unsigned char *md
, unsigned int *size
, const EVP_MD
*type
,
669 EVP_MD_CTX
*ctx
= EVP_MD_CTX_new();
674 EVP_MD_CTX_set_flags(ctx
, EVP_MD_CTX_FLAG_ONESHOT
);
675 ret
= EVP_DigestInit_ex(ctx
, type
, impl
)
676 && EVP_DigestUpdate(ctx
, data
, count
)
677 && EVP_DigestFinal_ex(ctx
, md
, size
);
678 EVP_MD_CTX_free(ctx
);
683 int EVP_Q_digest(OSSL_LIB_CTX
*libctx
, const char *name
, const char *propq
,
684 const void *data
, size_t datalen
,
685 unsigned char *md
, size_t *mdlen
)
687 EVP_MD
*digest
= EVP_MD_fetch(libctx
, name
, propq
);
688 unsigned int temp
= 0;
691 if (digest
!= NULL
) {
692 ret
= EVP_Digest(data
, datalen
, md
, &temp
, digest
, NULL
);
700 int EVP_MD_get_params(const EVP_MD
*digest
, OSSL_PARAM params
[])
702 if (digest
!= NULL
&& digest
->get_params
!= NULL
)
703 return digest
->get_params(params
);
707 const OSSL_PARAM
*EVP_MD_gettable_params(const EVP_MD
*digest
)
709 if (digest
!= NULL
&& digest
->gettable_params
!= NULL
)
710 return digest
->gettable_params(
711 ossl_provider_ctx(EVP_MD_get0_provider(digest
)));
715 int EVP_MD_CTX_set_params(EVP_MD_CTX
*ctx
, const OSSL_PARAM params
[])
717 EVP_PKEY_CTX
*pctx
= ctx
->pctx
;
719 /* If we have a pctx then we should try that first */
721 && (pctx
->operation
== EVP_PKEY_OP_VERIFYCTX
722 || pctx
->operation
== EVP_PKEY_OP_SIGNCTX
)
723 && pctx
->op
.sig
.algctx
!= NULL
724 && pctx
->op
.sig
.signature
->set_ctx_md_params
!= NULL
)
725 return pctx
->op
.sig
.signature
->set_ctx_md_params(pctx
->op
.sig
.algctx
,
728 if (ctx
->digest
!= NULL
&& ctx
->digest
->set_ctx_params
!= NULL
)
729 return ctx
->digest
->set_ctx_params(ctx
->algctx
, params
);
734 const OSSL_PARAM
*EVP_MD_settable_ctx_params(const EVP_MD
*md
)
738 if (md
!= NULL
&& md
->settable_ctx_params
!= NULL
) {
739 provctx
= ossl_provider_ctx(EVP_MD_get0_provider(md
));
740 return md
->settable_ctx_params(NULL
, provctx
);
745 const OSSL_PARAM
*EVP_MD_CTX_settable_params(EVP_MD_CTX
*ctx
)
753 /* If we have a pctx then we should try that first */
756 && (pctx
->operation
== EVP_PKEY_OP_VERIFYCTX
757 || pctx
->operation
== EVP_PKEY_OP_SIGNCTX
)
758 && pctx
->op
.sig
.algctx
!= NULL
759 && pctx
->op
.sig
.signature
->settable_ctx_md_params
!= NULL
)
760 return pctx
->op
.sig
.signature
->settable_ctx_md_params(
761 pctx
->op
.sig
.algctx
);
763 if (ctx
->digest
!= NULL
&& ctx
->digest
->settable_ctx_params
!= NULL
) {
764 alg
= ossl_provider_ctx(EVP_MD_get0_provider(ctx
->digest
));
765 return ctx
->digest
->settable_ctx_params(ctx
->algctx
, alg
);
771 int EVP_MD_CTX_get_params(EVP_MD_CTX
*ctx
, OSSL_PARAM params
[])
773 EVP_PKEY_CTX
*pctx
= ctx
->pctx
;
775 /* If we have a pctx then we should try that first */
777 && (pctx
->operation
== EVP_PKEY_OP_VERIFYCTX
778 || pctx
->operation
== EVP_PKEY_OP_SIGNCTX
)
779 && pctx
->op
.sig
.algctx
!= NULL
780 && pctx
->op
.sig
.signature
->get_ctx_md_params
!= NULL
)
781 return pctx
->op
.sig
.signature
->get_ctx_md_params(pctx
->op
.sig
.algctx
,
784 if (ctx
->digest
!= NULL
&& ctx
->digest
->get_params
!= NULL
)
785 return ctx
->digest
->get_ctx_params(ctx
->algctx
, params
);
790 const OSSL_PARAM
*EVP_MD_gettable_ctx_params(const EVP_MD
*md
)
794 if (md
!= NULL
&& md
->gettable_ctx_params
!= NULL
) {
795 provctx
= ossl_provider_ctx(EVP_MD_get0_provider(md
));
796 return md
->gettable_ctx_params(NULL
, provctx
);
801 const OSSL_PARAM
*EVP_MD_CTX_gettable_params(EVP_MD_CTX
*ctx
)
809 /* If we have a pctx then we should try that first */
812 && (pctx
->operation
== EVP_PKEY_OP_VERIFYCTX
813 || pctx
->operation
== EVP_PKEY_OP_SIGNCTX
)
814 && pctx
->op
.sig
.algctx
!= NULL
815 && pctx
->op
.sig
.signature
->gettable_ctx_md_params
!= NULL
)
816 return pctx
->op
.sig
.signature
->gettable_ctx_md_params(
817 pctx
->op
.sig
.algctx
);
819 if (ctx
->digest
!= NULL
&& ctx
->digest
->gettable_ctx_params
!= NULL
) {
820 provctx
= ossl_provider_ctx(EVP_MD_get0_provider(ctx
->digest
));
821 return ctx
->digest
->gettable_ctx_params(ctx
->algctx
, provctx
);
826 int EVP_MD_CTX_ctrl(EVP_MD_CTX
*ctx
, int cmd
, int p1
, void *p2
)
828 int ret
= EVP_CTRL_RET_UNSUPPORTED
;
831 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
834 ERR_raise(ERR_LIB_EVP
, ERR_R_PASSED_NULL_PARAMETER
);
838 if (ctx
->digest
!= NULL
&& ctx
->digest
->prov
== NULL
)
842 case EVP_MD_CTRL_XOF_LEN
:
844 params
[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN
, &sz
);
846 case EVP_MD_CTRL_MICALG
:
848 params
[0] = OSSL_PARAM_construct_utf8_string(OSSL_DIGEST_PARAM_MICALG
,
851 case EVP_CTRL_SSL3_MASTER_SECRET
:
852 params
[0] = OSSL_PARAM_construct_octet_string(OSSL_DIGEST_PARAM_SSL3_MS
,
860 ret
= EVP_MD_CTX_set_params(ctx
, params
);
862 ret
= EVP_MD_CTX_get_params(ctx
, params
);
866 /* Code below to be removed when legacy support is dropped. */
868 if (ctx
->digest
->md_ctrl
== NULL
) {
869 ERR_raise(ERR_LIB_EVP
, EVP_R_CTRL_NOT_IMPLEMENTED
);
873 ret
= ctx
->digest
->md_ctrl(ctx
, cmd
, p1
, p2
);
880 EVP_MD
*evp_md_new(void)
882 EVP_MD
*md
= OPENSSL_zalloc(sizeof(*md
));
885 md
->lock
= CRYPTO_THREAD_lock_new();
886 if (md
->lock
== NULL
) {
896 * FIPS module note: since internal fetches will be entirely
897 * provider based, we know that none of its code depends on legacy
898 * NIDs or any functionality that use them.
901 static void set_legacy_nid(const char *name
, void *vlegacy_nid
)
904 int *legacy_nid
= vlegacy_nid
;
906 * We use lowest level function to get the associated method, because
907 * higher level functions such as EVP_get_digestbyname() have changed
908 * to look at providers too.
910 const void *legacy_method
= OBJ_NAME_get(name
, OBJ_NAME_TYPE_MD_METH
);
912 if (*legacy_nid
== -1) /* We found a clash already */
915 if (legacy_method
== NULL
)
917 nid
= EVP_MD_nid(legacy_method
);
918 if (*legacy_nid
!= NID_undef
&& *legacy_nid
!= nid
) {
926 static int evp_md_cache_constants(EVP_MD
*md
)
928 int ok
, xof
= 0, algid_absent
= 0;
931 OSSL_PARAM params
[5];
933 params
[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_BLOCK_SIZE
, &blksz
);
934 params
[1] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_SIZE
, &mdsize
);
935 params
[2] = OSSL_PARAM_construct_int(OSSL_DIGEST_PARAM_XOF
, &xof
);
936 params
[3] = OSSL_PARAM_construct_int(OSSL_DIGEST_PARAM_ALGID_ABSENT
,
938 params
[4] = OSSL_PARAM_construct_end();
939 ok
= evp_do_md_getparams(md
, params
) > 0;
940 if (mdsize
> INT_MAX
|| blksz
> INT_MAX
)
943 md
->block_size
= (int)blksz
;
944 md
->md_size
= (int)mdsize
;
946 md
->flags
|= EVP_MD_FLAG_XOF
;
948 md
->flags
|= EVP_MD_FLAG_DIGALGID_ABSENT
;
953 static void *evp_md_from_algorithm(int name_id
,
954 const OSSL_ALGORITHM
*algodef
,
957 const OSSL_DISPATCH
*fns
= algodef
->implementation
;
961 /* EVP_MD_fetch() will set the legacy NID if available */
962 if ((md
= evp_md_new()) == NULL
) {
963 ERR_raise(ERR_LIB_EVP
, ERR_R_EVP_LIB
);
968 md
->type
= NID_undef
;
969 if (!evp_names_do_all(prov
, name_id
, set_legacy_nid
, &md
->type
)
971 ERR_raise(ERR_LIB_EVP
, ERR_R_INTERNAL_ERROR
);
977 md
->name_id
= name_id
;
978 if ((md
->type_name
= ossl_algorithm_get1_first_name(algodef
)) == NULL
) {
982 md
->description
= algodef
->algorithm_description
;
984 for (; fns
->function_id
!= 0; fns
++) {
985 switch (fns
->function_id
) {
986 case OSSL_FUNC_DIGEST_NEWCTX
:
987 if (md
->newctx
== NULL
) {
988 md
->newctx
= OSSL_FUNC_digest_newctx(fns
);
992 case OSSL_FUNC_DIGEST_INIT
:
993 if (md
->dinit
== NULL
) {
994 md
->dinit
= OSSL_FUNC_digest_init(fns
);
998 case OSSL_FUNC_DIGEST_UPDATE
:
999 if (md
->dupdate
== NULL
) {
1000 md
->dupdate
= OSSL_FUNC_digest_update(fns
);
1004 case OSSL_FUNC_DIGEST_FINAL
:
1005 if (md
->dfinal
== NULL
) {
1006 md
->dfinal
= OSSL_FUNC_digest_final(fns
);
1010 case OSSL_FUNC_DIGEST_DIGEST
:
1011 if (md
->digest
== NULL
)
1012 md
->digest
= OSSL_FUNC_digest_digest(fns
);
1013 /* We don't increment fnct for this as it is stand alone */
1015 case OSSL_FUNC_DIGEST_FREECTX
:
1016 if (md
->freectx
== NULL
) {
1017 md
->freectx
= OSSL_FUNC_digest_freectx(fns
);
1021 case OSSL_FUNC_DIGEST_DUPCTX
:
1022 if (md
->dupctx
== NULL
)
1023 md
->dupctx
= OSSL_FUNC_digest_dupctx(fns
);
1025 case OSSL_FUNC_DIGEST_GET_PARAMS
:
1026 if (md
->get_params
== NULL
)
1027 md
->get_params
= OSSL_FUNC_digest_get_params(fns
);
1029 case OSSL_FUNC_DIGEST_SET_CTX_PARAMS
:
1030 if (md
->set_ctx_params
== NULL
)
1031 md
->set_ctx_params
= OSSL_FUNC_digest_set_ctx_params(fns
);
1033 case OSSL_FUNC_DIGEST_GET_CTX_PARAMS
:
1034 if (md
->get_ctx_params
== NULL
)
1035 md
->get_ctx_params
= OSSL_FUNC_digest_get_ctx_params(fns
);
1037 case OSSL_FUNC_DIGEST_GETTABLE_PARAMS
:
1038 if (md
->gettable_params
== NULL
)
1039 md
->gettable_params
= OSSL_FUNC_digest_gettable_params(fns
);
1041 case OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS
:
1042 if (md
->settable_ctx_params
== NULL
)
1043 md
->settable_ctx_params
=
1044 OSSL_FUNC_digest_settable_ctx_params(fns
);
1046 case OSSL_FUNC_DIGEST_GETTABLE_CTX_PARAMS
:
1047 if (md
->gettable_ctx_params
== NULL
)
1048 md
->gettable_ctx_params
=
1049 OSSL_FUNC_digest_gettable_ctx_params(fns
);
1053 if ((fncnt
!= 0 && fncnt
!= 5)
1054 || (fncnt
== 0 && md
->digest
== NULL
)) {
1056 * In order to be a consistent set of functions we either need the
1057 * whole set of init/update/final etc functions or none of them.
1058 * The "digest" function can standalone. We at least need one way to
1062 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_PROVIDER_FUNCTIONS
);
1067 ossl_provider_up_ref(prov
);
1069 if (!evp_md_cache_constants(md
)) {
1071 ERR_raise(ERR_LIB_EVP
, EVP_R_CACHE_CONSTANTS_FAILED
);
1078 static int evp_md_up_ref(void *md
)
1080 return EVP_MD_up_ref(md
);
1083 static void evp_md_free(void *md
)
1088 EVP_MD
*EVP_MD_fetch(OSSL_LIB_CTX
*ctx
, const char *algorithm
,
1089 const char *properties
)
1092 evp_generic_fetch(ctx
, OSSL_OP_DIGEST
, algorithm
, properties
,
1093 evp_md_from_algorithm
, evp_md_up_ref
, evp_md_free
);
1098 int EVP_MD_up_ref(EVP_MD
*md
)
1102 if (md
->origin
== EVP_ORIG_DYNAMIC
)
1103 CRYPTO_UP_REF(&md
->refcnt
, &ref
, md
->lock
);
1107 void EVP_MD_free(EVP_MD
*md
)
1111 if (md
== NULL
|| md
->origin
!= EVP_ORIG_DYNAMIC
)
1114 CRYPTO_DOWN_REF(&md
->refcnt
, &i
, md
->lock
);
1117 evp_md_free_int(md
);
1120 void EVP_MD_do_all_provided(OSSL_LIB_CTX
*libctx
,
1121 void (*fn
)(EVP_MD
*mac
, void *arg
),
1124 evp_generic_do_all(libctx
, OSSL_OP_DIGEST
,
1125 (void (*)(void *, void *))fn
, arg
,
1126 evp_md_from_algorithm
, evp_md_up_ref
, evp_md_free
);