]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/evp/digest.c
Clear md_data only when necessary
[thirdparty/openssl.git] / crypto / evp / digest.c
CommitLineData
62867571 1/*
a28d06f3 2 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
5ba372b1 3 *
4a8b0c55 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
62867571
RS
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
5ba372b1 8 */
d02b48c6 9
e4468e6d
P
10/* We need to use some engine deprecated APIs */
11#define OPENSSL_SUPPRESS_DEPRECATED
12
d02b48c6 13#include <stdio.h>
ec577822
BM
14#include <openssl/objects.h>
15#include <openssl/evp.h>
ded346fa 16#include <openssl/ec.h>
3f773c91
TM
17#ifndef FIPS_MODULE
18# include <openssl/engine.h>
19#endif
d5e5e2ff
SL
20#include <openssl/params.h>
21#include <openssl/core_names.h>
22#include "internal/cryptlib.h"
3653d0c2 23#include "internal/provider.h"
6c9bc258
TM
24#include "internal/core.h"
25#include "crypto/evp.h"
706457b7 26#include "evp_local.h"
d02b48c6 27
357bccc8
MC
28static void cleanup_old_md_data(EVP_MD_CTX *ctx, int force)
29{
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)
8086b267 36 || force)) {
357bccc8 37 OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
8086b267
MC
38 ctx->md_data = NULL;
39 }
357bccc8
MC
40 }
41}
aa64cf24 42
3101ab60
MC
43void evp_md_ctx_clear_digest(EVP_MD_CTX *ctx, int force)
44{
7c14d0c1 45 if (ctx->algctx != NULL) {
8549b972 46 if (ctx->digest != NULL && ctx->digest->freectx != NULL)
7c14d0c1
SL
47 ctx->digest->freectx(ctx->algctx);
48 ctx->algctx = NULL;
8c8cf0d9
MC
49 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
50 }
51
0be6cf0c 52 /* Code below to be removed when legacy support is dropped. */
8c8cf0d9 53
74cabf3f
RL
54 /*
55 * Don't assume ctx->md_data was cleaned in EVP_Digest_Final, because
56 * sometimes only copies of the context are ever finalised.
57 */
357bccc8 58 cleanup_old_md_data(ctx, force);
3101ab60
MC
59 if (force)
60 ctx->digest = NULL;
319e518a 61
f844f9eb 62#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
7c96dbcd 63 ENGINE_finish(ctx->engine);
3101ab60 64 ctx->engine = NULL;
74cabf3f 65#endif
8549b972
P
66
67 /* Non legacy code, this has to be later than the ctx->digest cleaning */
68 EVP_MD_free(ctx->fetched_digest);
69 ctx->fetched_digest = NULL;
70 ctx->reqdigest = NULL;
3101ab60 71}
14bec2c4 72
3101ab60
MC
73/* This call frees resources associated with the context */
74int EVP_MD_CTX_reset(EVP_MD_CTX *ctx)
75{
76 if (ctx == NULL)
77 return 1;
78
79#ifndef FIPS_MODULE
3101ab60
MC
80 /*
81 * pctx should be freed by the user of EVP_MD_CTX
82 * if EVP_MD_CTX_FLAG_KEEP_PKEY_CTX is set
83 */
84 if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX)) {
85 EVP_PKEY_CTX_free(ctx->pctx);
86 ctx->pctx = NULL;
87 }
88#endif
14bec2c4 89
3101ab60 90 evp_md_ctx_clear_digest(ctx, 0);
3ce2fdab 91 OPENSSL_cleanse(ctx, sizeof(*ctx));
74cabf3f
RL
92
93 return 1;
0f113f3e 94}
dbad1690 95
ded346fa 96#ifndef FIPS_MODULE
d8652be0 97EVP_MD_CTX *evp_md_ctx_new_ex(EVP_PKEY *pkey, const ASN1_OCTET_STRING *id,
b4250010 98 OSSL_LIB_CTX *libctx, const char *propq)
ded346fa
DDO
99{
100 EVP_MD_CTX *ctx;
101 EVP_PKEY_CTX *pctx = NULL;
102
103 if ((ctx = EVP_MD_CTX_new()) == NULL
104 || (pctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq)) == NULL) {
9311d0c4 105 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
ded346fa
DDO
106 goto err;
107 }
108
c2403f36 109 if (id != NULL && EVP_PKEY_CTX_set1_id(pctx, id->data, id->length) <= 0)
ded346fa 110 goto err;
ded346fa
DDO
111
112 EVP_MD_CTX_set_pkey_ctx(ctx, pctx);
113 return ctx;
114
115 err:
116 EVP_PKEY_CTX_free(pctx);
117 EVP_MD_CTX_free(ctx);
118 return NULL;
119}
120#endif
121
959ed531 122EVP_MD_CTX *EVP_MD_CTX_new(void)
0f113f3e 123{
74cabf3f
RL
124 return OPENSSL_zalloc(sizeof(EVP_MD_CTX));
125}
dbad1690 126
959ed531 127void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
74cabf3f 128{
8c8cf0d9
MC
129 if (ctx == NULL)
130 return;
131
8c8cf0d9 132 EVP_MD_CTX_reset(ctx);
8c8cf0d9 133 OPENSSL_free(ctx);
0f113f3e 134}
dbad1690 135
4b58d9b4
P
136static int evp_md_init_internal(EVP_MD_CTX *ctx, const EVP_MD *type,
137 const OSSL_PARAM params[], ENGINE *impl)
0f113f3e 138{
f844f9eb 139#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
8c8cf0d9 140 ENGINE *tmpimpl = NULL;
319e518a 141#endif
8c8cf0d9 142
b0002eb0
MC
143#if !defined(FIPS_MODULE)
144 if (ctx->pctx != NULL
145 && EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx->pctx)
7c14d0c1 146 && ctx->pctx->op.sig.algctx != NULL) {
b0002eb0
MC
147 /*
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()
152 */
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);
9311d0c4 157 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
b0002eb0
MC
158 return 0;
159 }
160#endif
161
0f113f3e 162 EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
8c8cf0d9 163
7c14d0c1 164 if (ctx->algctx != NULL) {
15de965f 165 if (!ossl_assert(ctx->digest != NULL)) {
9311d0c4 166 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
15de965f
MC
167 return 0;
168 }
169 if (ctx->digest->freectx != NULL)
7c14d0c1
SL
170 ctx->digest->freectx(ctx->algctx);
171 ctx->algctx = NULL;
15de965f
MC
172 }
173
5de9863b 174 if (type != NULL) {
b7c913c8 175 ctx->reqdigest = type;
5de9863b
TM
176 } else {
177 if (ctx->digest == NULL) {
178 ERR_raise(ERR_LIB_EVP, EVP_R_NO_DIGEST_SET);
179 return 0;
180 }
181 type = ctx->digest;
182 }
b7c913c8 183
0be6cf0c 184 /* Code below to be removed when legacy support is dropped. */
f844f9eb 185#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
0f113f3e
MC
186 /*
187 * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
188 * this context may already have an ENGINE! Try to avoid releasing the
189 * previous handle, re-querying for an ENGINE, and having a
0d4fb843 190 * reinitialisation, when it may all be unnecessary.
0f113f3e 191 */
10cf46c4
P
192 if (ctx->engine != NULL
193 && ctx->digest != NULL
194 && type->type == ctx->digest->type)
0f113f3e 195 goto skip_to_init;
8c8cf0d9 196
10cf46c4
P
197 /*
198 * Ensure an ENGINE left lying around from last time is cleared (the
199 * previous check attempted to avoid this if the same ENGINE and
200 * EVP_MD could be used).
201 */
202 ENGINE_finish(ctx->engine);
203 ctx->engine = NULL;
98475995 204
10cf46c4 205 if (impl == NULL)
8c8cf0d9
MC
206 tmpimpl = ENGINE_get_digest_engine(type->type);
207#endif
208
209 /*
19cfe784
MC
210 * If there are engines involved or EVP_MD_CTX_FLAG_NO_INIT is set then we
211 * should use legacy handling for now.
8c8cf0d9 212 */
10cf46c4
P
213 if (impl != NULL
214#if !defined(OPENSSL_NO_ENGINE)
215 || ctx->engine != NULL
216# if !defined(FIPS_MODULE)
8c8cf0d9 217 || tmpimpl != NULL
10cf46c4 218# endif
319e518a 219#endif
d9ad5b16
MC
220 || (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0
221 || type->origin == EVP_ORIG_METH) {
8c8cf0d9
MC
222 if (ctx->digest == ctx->fetched_digest)
223 ctx->digest = NULL;
3fd70262 224 EVP_MD_free(ctx->fetched_digest);
8c8cf0d9
MC
225 ctx->fetched_digest = NULL;
226 goto legacy;
227 }
228
357bccc8 229 cleanup_old_md_data(ctx, 1);
8c8cf0d9 230
0be6cf0c 231 /* Start of non-legacy code below */
8c8cf0d9
MC
232
233 if (type->prov == NULL) {
f844f9eb 234#ifdef FIPS_MODULE
79c44b4e 235 /* We only do explicit fetches inside the FIPS module */
9311d0c4 236 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
319e518a
MC
237 return 0;
238#else
bef9b48e
TM
239 /* The NULL digest is a special case */
240 EVP_MD *provmd = EVP_MD_fetch(NULL,
241 type->type != NID_undef ? OBJ_nid2sn(type->type)
242 : "NULL", "");
319e518a 243
31f7ff37
P
244 if (provmd == NULL) {
245 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
8c8cf0d9 246 return 0;
31f7ff37 247 }
8c8cf0d9 248 type = provmd;
3fd70262 249 EVP_MD_free(ctx->fetched_digest);
8c8cf0d9 250 ctx->fetched_digest = provmd;
319e518a 251#endif
8c8cf0d9
MC
252 }
253
7c14d0c1 254 if (ctx->algctx != NULL && ctx->digest != NULL && ctx->digest != type) {
d5e5e2ff 255 if (ctx->digest->freectx != NULL)
7c14d0c1
SL
256 ctx->digest->freectx(ctx->algctx);
257 ctx->algctx = NULL;
d5e5e2ff 258 }
31f7ff37
P
259 if (type->prov != NULL && ctx->fetched_digest != type) {
260 if (!EVP_MD_up_ref((EVP_MD *)type)) {
261 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
262 return 0;
263 }
264 EVP_MD_free(ctx->fetched_digest);
265 ctx->fetched_digest = (EVP_MD *)type;
266 }
8c8cf0d9 267 ctx->digest = type;
7c14d0c1
SL
268 if (ctx->algctx == NULL) {
269 ctx->algctx = ctx->digest->newctx(ossl_provider_ctx(type->prov));
270 if (ctx->algctx == NULL) {
9311d0c4 271 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
8c8cf0d9
MC
272 return 0;
273 }
274 }
275
276 if (ctx->digest->dinit == NULL) {
9311d0c4 277 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
8c8cf0d9
MC
278 return 0;
279 }
280
7c14d0c1 281 return ctx->digest->dinit(ctx->algctx, params);
8c8cf0d9 282
0be6cf0c 283 /* Code below to be removed when legacy support is dropped. */
8c8cf0d9
MC
284 legacy:
285
f844f9eb 286#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
0f113f3e 287 if (type) {
7c96dbcd 288 if (impl != NULL) {
0f113f3e 289 if (!ENGINE_init(impl)) {
9311d0c4 290 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
0f113f3e
MC
291 return 0;
292 }
7c96dbcd 293 } else {
0f113f3e 294 /* Ask if an ENGINE is reserved for this job */
8c8cf0d9 295 impl = tmpimpl;
7c96dbcd
RS
296 }
297 if (impl != NULL) {
0f113f3e
MC
298 /* There's an ENGINE for this job ... (apparently) */
299 const EVP_MD *d = ENGINE_get_digest(impl, type->type);
7c96dbcd
RS
300
301 if (d == NULL) {
9311d0c4 302 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
0f113f3e
MC
303 ENGINE_finish(impl);
304 return 0;
305 }
306 /* We'll use the ENGINE's private digest definition */
307 type = d;
308 /*
309 * Store the ENGINE functional reference so we know 'type' came
310 * from an ENGINE and we need to release it when done.
311 */
312 ctx->engine = impl;
313 } else
314 ctx->engine = NULL;
0f113f3e 315 }
90e8a310 316#endif
0f113f3e 317 if (ctx->digest != type) {
357bccc8
MC
318 cleanup_old_md_data(ctx, 1);
319
0f113f3e
MC
320 ctx->digest = type;
321 if (!(ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) && type->ctx_size) {
322 ctx->update = type->update;
84c15091 323 ctx->md_data = OPENSSL_zalloc(type->ctx_size);
0f113f3e 324 if (ctx->md_data == NULL) {
9311d0c4 325 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
326 return 0;
327 }
328 }
329 }
f844f9eb 330#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
0f113f3e 331 skip_to_init:
0b13e9f0 332#endif
f844f9eb 333#ifndef FIPS_MODULE
864b89ce
MC
334 if (ctx->pctx != NULL
335 && (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx->pctx)
336 || ctx->pctx->op.sig.signature == NULL)) {
0f113f3e
MC
337 int r;
338 r = EVP_PKEY_CTX_ctrl(ctx->pctx, -1, EVP_PKEY_OP_TYPE_SIG,
339 EVP_PKEY_CTRL_DIGESTINIT, 0, ctx);
340 if (r <= 0 && (r != -2))
341 return 0;
342 }
319e518a 343#endif
0f113f3e
MC
344 if (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT)
345 return 1;
346 return ctx->digest->init(ctx);
347}
d02b48c6 348
4b58d9b4
P
349int EVP_DigestInit_ex2(EVP_MD_CTX *ctx, const EVP_MD *type,
350 const OSSL_PARAM params[])
351{
352 return evp_md_init_internal(ctx, type, params, NULL);
353}
354
355int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type)
356{
357 EVP_MD_CTX_reset(ctx);
358 return evp_md_init_internal(ctx, type, NULL, NULL);
359}
360
361int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
362{
363 return evp_md_init_internal(ctx, type, NULL, impl);
364}
365
f80921b6 366int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t count)
0f113f3e 367{
a8274ea3
MC
368 if (count == 0)
369 return 1;
370
72df8f88
MC
371 if (ctx->pctx != NULL
372 && EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx->pctx)
7c14d0c1 373 && ctx->pctx->op.sig.algctx != NULL) {
72df8f88
MC
374 /*
375 * Prior to OpenSSL 3.0 EVP_DigestSignUpdate() and
376 * EVP_DigestVerifyUpdate() were just macros for EVP_DigestUpdate().
377 * Some code calls EVP_DigestUpdate() directly even when initialised
d8652be0
MC
378 * with EVP_DigestSignInit_ex() or
379 * EVP_DigestVerifyInit_ex(), so we detect that and redirect to
0ab18e79 380 * the correct EVP_Digest*Update() function
72df8f88
MC
381 */
382 if (ctx->pctx->operation == EVP_PKEY_OP_SIGNCTX)
383 return EVP_DigestSignUpdate(ctx, data, count);
384 if (ctx->pctx->operation == EVP_PKEY_OP_VERIFYCTX)
385 return EVP_DigestVerifyUpdate(ctx, data, count);
9311d0c4 386 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
72df8f88
MC
387 return 0;
388 }
389
557d6737
MC
390 if (ctx->digest == NULL
391 || ctx->digest->prov == NULL
392 || (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0)
8c8cf0d9
MC
393 goto legacy;
394
395 if (ctx->digest->dupdate == NULL) {
9311d0c4 396 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
8c8cf0d9
MC
397 return 0;
398 }
7c14d0c1 399 return ctx->digest->dupdate(ctx->algctx, data, count);
8c8cf0d9 400
0be6cf0c 401 /* Code below to be removed when legacy support is dropped. */
8c8cf0d9 402 legacy:
0f113f3e
MC
403 return ctx->update(ctx, data, count);
404}
d02b48c6 405
dbad1690 406/* The caller can assume that this removes any secret data from the context */
2dc769a1 407int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size)
0f113f3e
MC
408{
409 int ret;
410 ret = EVP_DigestFinal_ex(ctx, md, size);
959ed531 411 EVP_MD_CTX_reset(ctx);
0f113f3e
MC
412 return ret;
413}
20d2186c
DSH
414
415/* The caller can assume that this removes any secret data from the context */
8c8cf0d9 416int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *isize)
0f113f3e 417{
4bec3f6d 418 int ret, sz;
8c8cf0d9 419 size_t size = 0;
4bec3f6d
SL
420 size_t mdsize = 0;
421
422 if (ctx->digest == NULL)
423 return 0;
8c8cf0d9 424
ed576acd 425 sz = EVP_MD_get_size(ctx->digest);
4bec3f6d
SL
426 if (sz < 0)
427 return 0;
428 mdsize = sz;
429 if (ctx->digest->prov == NULL)
8c8cf0d9 430 goto legacy;
54a656ef 431
8c8cf0d9 432 if (ctx->digest->dfinal == NULL) {
9311d0c4 433 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
8c8cf0d9
MC
434 return 0;
435 }
436
7c14d0c1 437 ret = ctx->digest->dfinal(ctx->algctx, md, &size, mdsize);
8c8cf0d9
MC
438
439 if (isize != NULL) {
440 if (size <= UINT_MAX) {
441 *isize = (int)size;
442 } else {
9311d0c4 443 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
8c8cf0d9
MC
444 ret = 0;
445 }
446 }
447
8c8cf0d9
MC
448 return ret;
449
0be6cf0c 450 /* Code below to be removed when legacy support is dropped. */
8c8cf0d9 451 legacy:
0ad50b4d 452 OPENSSL_assert(mdsize <= EVP_MAX_MD_SIZE);
0f113f3e 453 ret = ctx->digest->final(ctx, md);
8c8cf0d9 454 if (isize != NULL)
0ad50b4d 455 *isize = mdsize;
0f113f3e
MC
456 if (ctx->digest->cleanup) {
457 ctx->digest->cleanup(ctx);
458 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
459 }
3ce2fdab 460 OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
0f113f3e
MC
461 return ret;
462}
351d8998 463
cd8d1456
AP
464int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t size)
465{
466 int ret = 0;
d5e5e2ff
SL
467 OSSL_PARAM params[2];
468 size_t i = 0;
469
28c21fa0
P
470 if (ctx->digest == NULL) {
471 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_NULL_ALGORITHM);
472 return 0;
473 }
474
475 if (ctx->digest->prov == NULL)
d5e5e2ff 476 goto legacy;
cd8d1456 477
d5e5e2ff 478 if (ctx->digest->dfinal == NULL) {
9311d0c4 479 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
d5e5e2ff
SL
480 return 0;
481 }
482
4e7991b4 483 params[i++] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN, &size);
d5e5e2ff
SL
484 params[i++] = OSSL_PARAM_construct_end();
485
486 if (EVP_MD_CTX_set_params(ctx, params) > 0)
7c14d0c1 487 ret = ctx->digest->dfinal(ctx->algctx, md, &size, size);
39fde64a 488
d5e5e2ff
SL
489 return ret;
490
491legacy:
cd8d1456
AP
492 if (ctx->digest->flags & EVP_MD_FLAG_XOF
493 && size <= INT_MAX
494 && ctx->digest->md_ctrl(ctx, EVP_MD_CTRL_XOF_LEN, (int)size, NULL)) {
495 ret = ctx->digest->final(ctx, md);
cd8d1456
AP
496 if (ctx->digest->cleanup != NULL) {
497 ctx->digest->cleanup(ctx);
498 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
499 }
500 OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
501 } else {
9311d0c4 502 ERR_raise(ERR_LIB_EVP, EVP_R_NOT_XOF_OR_INVALID_LENGTH);
cd8d1456
AP
503 }
504
505 return ret;
506}
507
dbad1690 508int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in)
0f113f3e 509{
959ed531 510 EVP_MD_CTX_reset(out);
0f113f3e
MC
511 return EVP_MD_CTX_copy_ex(out, in);
512}
20d2186c
DSH
513
514int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in)
0f113f3e
MC
515{
516 unsigned char *tmp_buf;
8c8cf0d9 517
9ece8323
TM
518 if (in == NULL) {
519 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
0f113f3e
MC
520 return 0;
521 }
8c8cf0d9 522
9ece8323
TM
523 if (in->digest == NULL) {
524 /* copying uninitialized digest context */
525 EVP_MD_CTX_reset(out);
526 if (out->fetched_digest != NULL)
527 EVP_MD_free(out->fetched_digest);
528 *out = *in;
ad2fcee1 529 goto clone_pkey;
9ece8323
TM
530 }
531
557d6737
MC
532 if (in->digest->prov == NULL
533 || (in->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0)
8c8cf0d9
MC
534 goto legacy;
535
536 if (in->digest->dupctx == NULL) {
9311d0c4 537 ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX);
8c8cf0d9
MC
538 return 0;
539 }
540
541 EVP_MD_CTX_reset(out);
542 if (out->fetched_digest != NULL)
3fd70262 543 EVP_MD_free(out->fetched_digest);
8c8cf0d9
MC
544 *out = *in;
545 /* NULL out pointers in case of error */
546 out->pctx = NULL;
7c14d0c1 547 out->algctx = NULL;
8c8cf0d9
MC
548
549 if (in->fetched_digest != NULL)
70c35fd1 550 EVP_MD_up_ref(in->fetched_digest);
8c8cf0d9 551
7c14d0c1
SL
552 if (in->algctx != NULL) {
553 out->algctx = in->digest->dupctx(in->algctx);
554 if (out->algctx == NULL) {
9311d0c4 555 ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX);
ada0670b
MC
556 return 0;
557 }
8c8cf0d9
MC
558 }
559
ad2fcee1 560 clone_pkey:
8c8cf0d9
MC
561 /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
562 EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
f844f9eb 563#ifndef FIPS_MODULE
8c8cf0d9
MC
564 if (in->pctx != NULL) {
565 out->pctx = EVP_PKEY_CTX_dup(in->pctx);
566 if (out->pctx == NULL) {
9311d0c4 567 ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX);
8c8cf0d9
MC
568 EVP_MD_CTX_reset(out);
569 return 0;
570 }
571 }
319e518a 572#endif
8c8cf0d9
MC
573
574 return 1;
575
0be6cf0c 576 /* Code below to be removed when legacy support is dropped. */
8c8cf0d9 577 legacy:
f844f9eb 578#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
0f113f3e
MC
579 /* Make sure it's safe to copy a digest context using an ENGINE */
580 if (in->engine && !ENGINE_init(in->engine)) {
9311d0c4 581 ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB);
0f113f3e
MC
582 return 0;
583 }
0b13e9f0 584#endif
26188931 585
0f113f3e
MC
586 if (out->digest == in->digest) {
587 tmp_buf = out->md_data;
588 EVP_MD_CTX_set_flags(out, EVP_MD_CTX_FLAG_REUSE);
589 } else
590 tmp_buf = NULL;
959ed531 591 EVP_MD_CTX_reset(out);
b4faea50 592 memcpy(out, in, sizeof(*out));
0f113f3e 593
4803717f
PY
594 /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
595 EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
596
6aa0ba4b
RL
597 /* Null these variables, since they are getting fixed up
598 * properly below. Anything else may cause a memleak and/or
599 * double free if any of the memory allocations below fail
600 */
601 out->md_data = NULL;
602 out->pctx = NULL;
603
0f113f3e
MC
604 if (in->md_data && out->digest->ctx_size) {
605 if (tmp_buf)
606 out->md_data = tmp_buf;
607 else {
608 out->md_data = OPENSSL_malloc(out->digest->ctx_size);
90945fa3 609 if (out->md_data == NULL) {
9311d0c4 610 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
611 return 0;
612 }
613 }
614 memcpy(out->md_data, in->md_data, out->digest->ctx_size);
615 }
26188931 616
0f113f3e 617 out->update = in->update;
d4575825 618
f844f9eb 619#ifndef FIPS_MODULE
0f113f3e
MC
620 if (in->pctx) {
621 out->pctx = EVP_PKEY_CTX_dup(in->pctx);
622 if (!out->pctx) {
959ed531 623 EVP_MD_CTX_reset(out);
0f113f3e
MC
624 return 0;
625 }
626 }
319e518a 627#endif
18327cd0 628
0f113f3e
MC
629 if (out->digest->copy)
630 return out->digest->copy(out, in);
3a828611 631
0f113f3e
MC
632 return 1;
633}
88ce56f8 634
9e0aad9f 635int EVP_Digest(const void *data, size_t count,
0f113f3e
MC
636 unsigned char *md, unsigned int *size, const EVP_MD *type,
637 ENGINE *impl)
638{
bfb0641f 639 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
0f113f3e 640 int ret;
dbad1690 641
74cabf3f
RL
642 if (ctx == NULL)
643 return 0;
644 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT);
645 ret = EVP_DigestInit_ex(ctx, type, impl)
646 && EVP_DigestUpdate(ctx, data, count)
647 && EVP_DigestFinal_ex(ctx, md, size);
bfb0641f 648 EVP_MD_CTX_free(ctx);
dbad1690 649
0f113f3e
MC
650 return ret;
651}
dbad1690 652
4d49b685 653int EVP_Q_digest(OSSL_LIB_CTX *libctx, const char *name, const char *propq,
006de767
RL
654 const void *data, size_t datalen,
655 unsigned char *md, size_t *mdlen)
4d49b685
DDO
656{
657 EVP_MD *digest = EVP_MD_fetch(libctx, name, propq);
006de767 658 unsigned int temp = 0;
4d49b685
DDO
659 int ret = 0;
660
661 if (digest != NULL) {
006de767 662 ret = EVP_Digest(data, datalen, md, &temp, digest, NULL);
4d49b685
DDO
663 EVP_MD_free(digest);
664 }
006de767
RL
665 if (mdlen != NULL)
666 *mdlen = temp;
4d49b685
DDO
667 return ret;
668}
669
ae3ff60e
RL
670int EVP_MD_get_params(const EVP_MD *digest, OSSL_PARAM params[])
671{
672 if (digest != NULL && digest->get_params != NULL)
673 return digest->get_params(params);
674 return 0;
675}
676
677const OSSL_PARAM *EVP_MD_gettable_params(const EVP_MD *digest)
678{
679 if (digest != NULL && digest->gettable_params != NULL)
18ec26ba 680 return digest->gettable_params(
ed576acd 681 ossl_provider_ctx(EVP_MD_get0_provider(digest)));
ae3ff60e
RL
682 return NULL;
683}
684
d5e5e2ff
SL
685int EVP_MD_CTX_set_params(EVP_MD_CTX *ctx, const OSSL_PARAM params[])
686{
5f5c3b4f
MC
687 EVP_PKEY_CTX *pctx = ctx->pctx;
688
ada0670b 689 /* If we have a pctx then we should try that first */
5f5c3b4f
MC
690 if (pctx != NULL
691 && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
692 || pctx->operation == EVP_PKEY_OP_SIGNCTX)
7c14d0c1 693 && pctx->op.sig.algctx != NULL
5f5c3b4f 694 && pctx->op.sig.signature->set_ctx_md_params != NULL)
7c14d0c1 695 return pctx->op.sig.signature->set_ctx_md_params(pctx->op.sig.algctx,
5f5c3b4f 696 params);
ada0670b
MC
697
698 if (ctx->digest != NULL && ctx->digest->set_ctx_params != NULL)
7c14d0c1 699 return ctx->digest->set_ctx_params(ctx->algctx, params);
ada0670b 700
d5e5e2ff
SL
701 return 0;
702}
703
e6879a31 704const OSSL_PARAM *EVP_MD_settable_ctx_params(const EVP_MD *md)
ae3ff60e 705{
7c14d0c1 706 void *provctx;
6de3a06d
P
707
708 if (md != NULL && md->settable_ctx_params != NULL) {
ed576acd 709 provctx = ossl_provider_ctx(EVP_MD_get0_provider(md));
7c14d0c1 710 return md->settable_ctx_params(NULL, provctx);
6de3a06d 711 }
e6879a31
MC
712 return NULL;
713}
714
715const OSSL_PARAM *EVP_MD_CTX_settable_params(EVP_MD_CTX *ctx)
716{
5f5c3b4f 717 EVP_PKEY_CTX *pctx;
6de3a06d 718 void *alg;
5f5c3b4f 719
18ec26ba
P
720 if (ctx == NULL)
721 return NULL;
722
ada0670b 723 /* If we have a pctx then we should try that first */
5f5c3b4f
MC
724 pctx = ctx->pctx;
725 if (pctx != NULL
726 && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
727 || pctx->operation == EVP_PKEY_OP_SIGNCTX)
7c14d0c1 728 && pctx->op.sig.algctx != NULL
5f5c3b4f
MC
729 && pctx->op.sig.signature->settable_ctx_md_params != NULL)
730 return pctx->op.sig.signature->settable_ctx_md_params(
7c14d0c1 731 pctx->op.sig.algctx);
5f5c3b4f 732
6de3a06d 733 if (ctx->digest != NULL && ctx->digest->settable_ctx_params != NULL) {
ed576acd 734 alg = ossl_provider_ctx(EVP_MD_get0_provider(ctx->digest));
7c14d0c1 735 return ctx->digest->settable_ctx_params(ctx->algctx, alg);
6de3a06d 736 }
ada0670b 737
ae3ff60e
RL
738 return NULL;
739}
740
4e7991b4 741int EVP_MD_CTX_get_params(EVP_MD_CTX *ctx, OSSL_PARAM params[])
d5e5e2ff 742{
5f5c3b4f
MC
743 EVP_PKEY_CTX *pctx = ctx->pctx;
744
ada0670b 745 /* If we have a pctx then we should try that first */
5f5c3b4f
MC
746 if (pctx != NULL
747 && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
748 || pctx->operation == EVP_PKEY_OP_SIGNCTX)
7c14d0c1 749 && pctx->op.sig.algctx != NULL
5f5c3b4f 750 && pctx->op.sig.signature->get_ctx_md_params != NULL)
7c14d0c1 751 return pctx->op.sig.signature->get_ctx_md_params(pctx->op.sig.algctx,
5f5c3b4f
MC
752 params);
753
ada0670b 754 if (ctx->digest != NULL && ctx->digest->get_params != NULL)
7c14d0c1 755 return ctx->digest->get_ctx_params(ctx->algctx, params);
ada0670b 756
d5e5e2ff
SL
757 return 0;
758}
759
e6879a31 760const OSSL_PARAM *EVP_MD_gettable_ctx_params(const EVP_MD *md)
ae3ff60e 761{
7c14d0c1 762 void *provctx;
6de3a06d
P
763
764 if (md != NULL && md->gettable_ctx_params != NULL) {
ed576acd 765 provctx = ossl_provider_ctx(EVP_MD_get0_provider(md));
7c14d0c1 766 return md->gettable_ctx_params(NULL, provctx);
6de3a06d 767 }
e6879a31
MC
768 return NULL;
769}
770
771const OSSL_PARAM *EVP_MD_CTX_gettable_params(EVP_MD_CTX *ctx)
772{
5f5c3b4f 773 EVP_PKEY_CTX *pctx;
7c14d0c1 774 void *provctx;
5f5c3b4f 775
825ccf51
SL
776 if (ctx == NULL)
777 return NULL;
778
ada0670b 779 /* If we have a pctx then we should try that first */
5f5c3b4f
MC
780 pctx = ctx->pctx;
781 if (pctx != NULL
782 && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
783 || pctx->operation == EVP_PKEY_OP_SIGNCTX)
7c14d0c1 784 && pctx->op.sig.algctx != NULL
5f5c3b4f
MC
785 && pctx->op.sig.signature->gettable_ctx_md_params != NULL)
786 return pctx->op.sig.signature->gettable_ctx_md_params(
7c14d0c1 787 pctx->op.sig.algctx);
5f5c3b4f 788
6de3a06d 789 if (ctx->digest != NULL && ctx->digest->gettable_ctx_params != NULL) {
ed576acd 790 provctx = ossl_provider_ctx(EVP_MD_get0_provider(ctx->digest));
7c14d0c1 791 return ctx->digest->gettable_ctx_params(ctx->algctx, provctx);
6de3a06d 792 }
ae3ff60e
RL
793 return NULL;
794}
795
396d5fd0
DSH
796int EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void *p2)
797{
6a3b7c68
RL
798 int ret = EVP_CTRL_RET_UNSUPPORTED;
799 int set_params = 1;
800 size_t sz;
801 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
802
a2b62316
MC
803 if (ctx == NULL) {
804 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
6a3b7c68
RL
805 return 0;
806 }
807
a2b62316 808 if (ctx->digest != NULL && ctx->digest->prov == NULL)
6a3b7c68
RL
809 goto legacy;
810
811 switch (cmd) {
812 case EVP_MD_CTRL_XOF_LEN:
813 sz = (size_t)p1;
814 params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN, &sz);
815 break;
816 case EVP_MD_CTRL_MICALG:
817 set_params = 0;
818 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_DIGEST_PARAM_MICALG,
4e7991b4 819 p2, p1 ? p1 : 9999);
6a3b7c68 820 break;
c3885102
MC
821 case EVP_CTRL_SSL3_MASTER_SECRET:
822 params[0] = OSSL_PARAM_construct_octet_string(OSSL_DIGEST_PARAM_SSL3_MS,
823 p2, p1);
824 break;
6a3b7c68 825 default:
6a36f209 826 goto conclude;
396d5fd0 827 }
6a3b7c68
RL
828
829 if (set_params)
5f5c3b4f 830 ret = EVP_MD_CTX_set_params(ctx, params);
6a3b7c68 831 else
5f5c3b4f 832 ret = EVP_MD_CTX_get_params(ctx, params);
552be00d 833 goto conclude;
6a3b7c68
RL
834
835
0be6cf0c 836 /* Code below to be removed when legacy support is dropped. */
6a3b7c68
RL
837 legacy:
838 if (ctx->digest->md_ctrl == NULL) {
839 ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_NOT_IMPLEMENTED);
840 return 0;
841 }
842
843 ret = ctx->digest->md_ctrl(ctx, cmd, p1, p2);
552be00d 844 conclude:
6a3b7c68
RL
845 if (ret <= 0)
846 return 0;
847 return ret;
396d5fd0 848}
3653d0c2 849
3fd70262
RL
850EVP_MD *evp_md_new(void)
851{
852 EVP_MD *md = OPENSSL_zalloc(sizeof(*md));
853
854 if (md != NULL) {
855 md->lock = CRYPTO_THREAD_lock_new();
856 if (md->lock == NULL) {
857 OPENSSL_free(md);
858 return NULL;
859 }
860 md->refcnt = 1;
861 }
862 return md;
863}
864
32040838
RL
865/*
866 * FIPS module note: since internal fetches will be entirely
867 * provider based, we know that none of its code depends on legacy
868 * NIDs or any functionality that use them.
869 */
f844f9eb 870#ifndef FIPS_MODULE
32040838
RL
871static void set_legacy_nid(const char *name, void *vlegacy_nid)
872{
873 int nid;
874 int *legacy_nid = vlegacy_nid;
6a835fcf
RL
875 /*
876 * We use lowest level function to get the associated method, because
877 * higher level functions such as EVP_get_digestbyname() have changed
878 * to look at providers too.
879 */
880 const void *legacy_method = OBJ_NAME_get(name, OBJ_NAME_TYPE_MD_METH);
32040838
RL
881
882 if (*legacy_nid == -1) /* We found a clash already */
883 return;
6a835fcf
RL
884
885 if (legacy_method == NULL)
32040838 886 return;
6a835fcf 887 nid = EVP_MD_nid(legacy_method);
32040838
RL
888 if (*legacy_nid != NID_undef && *legacy_nid != nid) {
889 *legacy_nid = -1;
890 return;
891 }
892 *legacy_nid = nid;
893}
894#endif
895
38f79314
MC
896static int evp_md_cache_constants(EVP_MD *md)
897{
af53092c 898 int ok, xof = 0, algid_absent = 0;
38f79314
MC
899 size_t blksz = 0;
900 size_t mdsize = 0;
af53092c 901 OSSL_PARAM params[5];
38f79314
MC
902
903 params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_BLOCK_SIZE, &blksz);
904 params[1] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_SIZE, &mdsize);
af53092c
SL
905 params[2] = OSSL_PARAM_construct_int(OSSL_DIGEST_PARAM_XOF, &xof);
906 params[3] = OSSL_PARAM_construct_int(OSSL_DIGEST_PARAM_ALGID_ABSENT,
907 &algid_absent);
908 params[4] = OSSL_PARAM_construct_end();
0df56c30 909 ok = evp_do_md_getparams(md, params) > 0;
38f79314
MC
910 if (mdsize > INT_MAX || blksz > INT_MAX)
911 ok = 0;
912 if (ok) {
913 md->block_size = (int)blksz;
914 md->md_size = (int)mdsize;
af53092c
SL
915 if (xof)
916 md->flags |= EVP_MD_FLAG_XOF;
917 if (algid_absent)
918 md->flags |= EVP_MD_FLAG_DIGALGID_ABSENT;
38f79314
MC
919 }
920 return ok;
921}
922
309a78aa
RL
923static void *evp_md_from_algorithm(int name_id,
924 const OSSL_ALGORITHM *algodef,
925 OSSL_PROVIDER *prov)
3653d0c2 926{
309a78aa 927 const OSSL_DISPATCH *fns = algodef->implementation;
3653d0c2 928 EVP_MD *md = NULL;
8c8cf0d9 929 int fncnt = 0;
3653d0c2 930
0211740f 931 /* EVP_MD_fetch() will set the legacy NID if available */
f7c16d48 932 if ((md = evp_md_new()) == NULL) {
9311d0c4 933 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
3653d0c2 934 return NULL;
6b9e3724 935 }
3653d0c2 936
f844f9eb 937#ifndef FIPS_MODULE
32040838 938 md->type = NID_undef;
d84f5515
MC
939 if (!evp_names_do_all(prov, name_id, set_legacy_nid, &md->type)
940 || md->type == -1) {
32040838
RL
941 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
942 EVP_MD_free(md);
943 return NULL;
f7c16d48 944 }
ed71e917
MC
945#endif
946
32040838 947 md->name_id = name_id;
6c9bc258
TM
948 if ((md->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL) {
949 EVP_MD_free(md);
950 return NULL;
951 }
309a78aa 952 md->description = algodef->algorithm_description;
32040838 953
3653d0c2 954 for (; fns->function_id != 0; fns++) {
3653d0c2
MC
955 switch (fns->function_id) {
956 case OSSL_FUNC_DIGEST_NEWCTX:
d5e5e2ff 957 if (md->newctx == NULL) {
363b1e5d 958 md->newctx = OSSL_FUNC_digest_newctx(fns);
d5e5e2ff
SL
959 fncnt++;
960 }
3653d0c2
MC
961 break;
962 case OSSL_FUNC_DIGEST_INIT:
d5e5e2ff 963 if (md->dinit == NULL) {
363b1e5d 964 md->dinit = OSSL_FUNC_digest_init(fns);
d5e5e2ff
SL
965 fncnt++;
966 }
3653d0c2 967 break;
df05f2ce 968 case OSSL_FUNC_DIGEST_UPDATE:
d5e5e2ff 969 if (md->dupdate == NULL) {
363b1e5d 970 md->dupdate = OSSL_FUNC_digest_update(fns);
d5e5e2ff
SL
971 fncnt++;
972 }
3653d0c2
MC
973 break;
974 case OSSL_FUNC_DIGEST_FINAL:
d5e5e2ff 975 if (md->dfinal == NULL) {
363b1e5d 976 md->dfinal = OSSL_FUNC_digest_final(fns);
d5e5e2ff
SL
977 fncnt++;
978 }
3653d0c2
MC
979 break;
980 case OSSL_FUNC_DIGEST_DIGEST:
d5e5e2ff 981 if (md->digest == NULL)
363b1e5d 982 md->digest = OSSL_FUNC_digest_digest(fns);
3653d0c2
MC
983 /* We don't increment fnct for this as it is stand alone */
984 break;
3653d0c2 985 case OSSL_FUNC_DIGEST_FREECTX:
d5e5e2ff 986 if (md->freectx == NULL) {
363b1e5d 987 md->freectx = OSSL_FUNC_digest_freectx(fns);
d5e5e2ff
SL
988 fncnt++;
989 }
3653d0c2 990 break;
8c8cf0d9 991 case OSSL_FUNC_DIGEST_DUPCTX:
d5e5e2ff 992 if (md->dupctx == NULL)
363b1e5d 993 md->dupctx = OSSL_FUNC_digest_dupctx(fns);
8c8cf0d9 994 break;
d5e5e2ff
SL
995 case OSSL_FUNC_DIGEST_GET_PARAMS:
996 if (md->get_params == NULL)
363b1e5d 997 md->get_params = OSSL_FUNC_digest_get_params(fns);
7556b9df 998 break;
92d9d0ae
RL
999 case OSSL_FUNC_DIGEST_SET_CTX_PARAMS:
1000 if (md->set_ctx_params == NULL)
363b1e5d 1001 md->set_ctx_params = OSSL_FUNC_digest_set_ctx_params(fns);
6a3b7c68 1002 break;
92d9d0ae
RL
1003 case OSSL_FUNC_DIGEST_GET_CTX_PARAMS:
1004 if (md->get_ctx_params == NULL)
363b1e5d 1005 md->get_ctx_params = OSSL_FUNC_digest_get_ctx_params(fns);
6a3b7c68 1006 break;
ae3ff60e
RL
1007 case OSSL_FUNC_DIGEST_GETTABLE_PARAMS:
1008 if (md->gettable_params == NULL)
363b1e5d 1009 md->gettable_params = OSSL_FUNC_digest_gettable_params(fns);
ae3ff60e
RL
1010 break;
1011 case OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS:
1012 if (md->settable_ctx_params == NULL)
1013 md->settable_ctx_params =
363b1e5d 1014 OSSL_FUNC_digest_settable_ctx_params(fns);
ae3ff60e
RL
1015 break;
1016 case OSSL_FUNC_DIGEST_GETTABLE_CTX_PARAMS:
1017 if (md->gettable_ctx_params == NULL)
1018 md->gettable_ctx_params =
363b1e5d 1019 OSSL_FUNC_digest_gettable_ctx_params(fns);
ae3ff60e 1020 break;
3653d0c2 1021 }
8c8cf0d9
MC
1022 }
1023 if ((fncnt != 0 && fncnt != 5)
6a3b7c68 1024 || (fncnt == 0 && md->digest == NULL)) {
8c8cf0d9
MC
1025 /*
1026 * In order to be a consistent set of functions we either need the
1027 * whole set of init/update/final etc functions or none of them.
1028 * The "digest" function can standalone. We at least need one way to
1029 * generate digests.
1030 */
3fd70262 1031 EVP_MD_free(md);
6a3b7c68 1032 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
8c8cf0d9 1033 return NULL;
3653d0c2
MC
1034 }
1035 md->prov = prov;
1036 if (prov != NULL)
7c95390e 1037 ossl_provider_up_ref(prov);
3653d0c2 1038
38f79314
MC
1039 if (!evp_md_cache_constants(md)) {
1040 EVP_MD_free(md);
1041 ERR_raise(ERR_LIB_EVP, EVP_R_CACHE_CONSTANTS_FAILED);
1042 md = NULL;
1043 }
1044
3653d0c2
MC
1045 return md;
1046}
1047
70c35fd1 1048static int evp_md_up_ref(void *md)
3653d0c2 1049{
70c35fd1 1050 return EVP_MD_up_ref(md);
3653d0c2
MC
1051}
1052
1053static void evp_md_free(void *md)
1054{
3fd70262 1055 EVP_MD_free(md);
3653d0c2
MC
1056}
1057
b4250010 1058EVP_MD *EVP_MD_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
3653d0c2
MC
1059 const char *properties)
1060{
0211740f
RL
1061 EVP_MD *md =
1062 evp_generic_fetch(ctx, OSSL_OP_DIGEST, algorithm, properties,
309a78aa 1063 evp_md_from_algorithm, evp_md_up_ref, evp_md_free);
0211740f 1064
0211740f 1065 return md;
3653d0c2 1066}
c540f00f 1067
3fd70262
RL
1068int EVP_MD_up_ref(EVP_MD *md)
1069{
1070 int ref = 0;
1071
f6c95e46
RS
1072 if (md->origin == EVP_ORIG_DYNAMIC)
1073 CRYPTO_UP_REF(&md->refcnt, &ref, md->lock);
3fd70262
RL
1074 return 1;
1075}
1076
1077void EVP_MD_free(EVP_MD *md)
1078{
1079 int i;
1080
f6c95e46 1081 if (md == NULL || md->origin != EVP_ORIG_DYNAMIC)
3fd70262
RL
1082 return;
1083
1084 CRYPTO_DOWN_REF(&md->refcnt, &i, md->lock);
1085 if (i > 0)
1086 return;
f6c95e46 1087 evp_md_free_int(md);
3fd70262
RL
1088}
1089
b4250010 1090void EVP_MD_do_all_provided(OSSL_LIB_CTX *libctx,
251e610c
RL
1091 void (*fn)(EVP_MD *mac, void *arg),
1092 void *arg)
c540f00f
RL
1093{
1094 evp_generic_do_all(libctx, OSSL_OP_DIGEST,
1095 (void (*)(void *, void *))fn, arg,
cd770738 1096 evp_md_from_algorithm, evp_md_up_ref, evp_md_free);
c540f00f 1097}