]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/evp/digest.c
Final cleanup after move to leaner EVP_PKEY methods
[thirdparty/openssl.git] / crypto / evp / digest.c
CommitLineData
62867571 1/*
d5e5e2ff 2 * Copyright 1995-2019 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
RE
9
10#include <stdio.h>
ec577822
BM
11#include <openssl/objects.h>
12#include <openssl/evp.h>
3c27208f 13#include <openssl/engine.h>
d5e5e2ff
SL
14#include <openssl/params.h>
15#include <openssl/core_names.h>
16#include "internal/cryptlib.h"
25f2138b 17#include "crypto/evp.h"
3653d0c2 18#include "internal/provider.h"
706457b7 19#include "evp_local.h"
d02b48c6 20
74cabf3f 21/* This call frees resources associated with the context */
959ed531 22int EVP_MD_CTX_reset(EVP_MD_CTX *ctx)
0f113f3e 23{
74cabf3f
RL
24 if (ctx == NULL)
25 return 1;
26
aa64cf24
MC
27#ifndef FIPS_MODE
28 /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
29 /*
30 * pctx should be freed by the user of EVP_MD_CTX
31 * if EVP_MD_CTX_FLAG_KEEP_PKEY_CTX is set
32 */
33 if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX))
34 EVP_PKEY_CTX_free(ctx->pctx);
35#endif
36
14bec2c4
MC
37 EVP_MD_free(ctx->fetched_digest);
38 ctx->fetched_digest = NULL;
39 ctx->reqdigest = NULL;
8c8cf0d9
MC
40
41 if (ctx->provctx != NULL) {
42 if (ctx->digest->freectx != NULL)
43 ctx->digest->freectx(ctx->provctx);
44 ctx->provctx = NULL;
45 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
46 }
47
8c8cf0d9 48 /* TODO(3.0): Remove legacy code below */
8c8cf0d9 49
74cabf3f
RL
50 /*
51 * Don't assume ctx->md_data was cleaned in EVP_Digest_Final, because
52 * sometimes only copies of the context are ever finalised.
53 */
54 if (ctx->digest && ctx->digest->cleanup
55 && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_CLEANED))
56 ctx->digest->cleanup(ctx);
57 if (ctx->digest && ctx->digest->ctx_size && ctx->md_data
58 && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_REUSE)) {
59 OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
60 }
319e518a 61
aa64cf24 62#if !defined(FIPS_MODE) && !defined(OPENSSL_NO_ENGINE)
7c96dbcd 63 ENGINE_finish(ctx->engine);
74cabf3f 64#endif
14bec2c4
MC
65
66 /* TODO(3.0): End of legacy code */
67
3ce2fdab 68 OPENSSL_cleanse(ctx, sizeof(*ctx));
74cabf3f
RL
69
70 return 1;
0f113f3e 71}
dbad1690 72
959ed531 73EVP_MD_CTX *EVP_MD_CTX_new(void)
0f113f3e 74{
74cabf3f
RL
75 return OPENSSL_zalloc(sizeof(EVP_MD_CTX));
76}
dbad1690 77
959ed531 78void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
74cabf3f 79{
8c8cf0d9
MC
80 if (ctx == NULL)
81 return;
82
8c8cf0d9
MC
83 EVP_MD_CTX_reset(ctx);
84
8c8cf0d9
MC
85 OPENSSL_free(ctx);
86 return;
0f113f3e 87}
dbad1690 88
2dc769a1 89int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type)
0f113f3e 90{
959ed531 91 EVP_MD_CTX_reset(ctx);
0f113f3e
MC
92 return EVP_DigestInit_ex(ctx, type, NULL);
93}
0fea7ed4 94
11a57c7b 95int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
0f113f3e 96{
319e518a 97#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
8c8cf0d9 98 ENGINE *tmpimpl = NULL;
319e518a 99#endif
8c8cf0d9 100
0f113f3e 101 EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
8c8cf0d9 102
15de965f
MC
103 if (ctx->provctx != NULL) {
104 if (!ossl_assert(ctx->digest != NULL)) {
105 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
106 return 0;
107 }
108 if (ctx->digest->freectx != NULL)
109 ctx->digest->freectx(ctx->provctx);
110 ctx->provctx = NULL;
111 }
112
b7c913c8
MC
113 if (type != NULL)
114 ctx->reqdigest = type;
115
8c8cf0d9 116 /* TODO(3.0): Legacy work around code below. Remove this */
319e518a 117#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
0f113f3e
MC
118 /*
119 * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
120 * this context may already have an ENGINE! Try to avoid releasing the
121 * previous handle, re-querying for an ENGINE, and having a
0d4fb843 122 * reinitialisation, when it may all be unnecessary.
0f113f3e 123 */
a93e0e78
MRA
124 if (ctx->engine && ctx->digest &&
125 (type == NULL || (type->type == ctx->digest->type)))
0f113f3e 126 goto skip_to_init;
8c8cf0d9 127
98475995
RL
128 if (type != NULL) {
129 /*
130 * Ensure an ENGINE left lying around from last time is cleared (the
131 * previous check attempted to avoid this if the same ENGINE and
132 * EVP_MD could be used).
133 */
134 ENGINE_finish(ctx->engine);
135 ctx->engine = NULL;
136 }
137
8c8cf0d9
MC
138 if (type != NULL && impl == NULL)
139 tmpimpl = ENGINE_get_digest_engine(type->type);
140#endif
141
142 /*
19cfe784
MC
143 * If there are engines involved or EVP_MD_CTX_FLAG_NO_INIT is set then we
144 * should use legacy handling for now.
8c8cf0d9
MC
145 */
146 if (ctx->engine != NULL
147 || impl != NULL
319e518a 148#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
8c8cf0d9 149 || tmpimpl != NULL
319e518a 150#endif
8c8cf0d9
MC
151 || (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0) {
152 if (ctx->digest == ctx->fetched_digest)
153 ctx->digest = NULL;
3fd70262 154 EVP_MD_free(ctx->fetched_digest);
8c8cf0d9
MC
155 ctx->fetched_digest = NULL;
156 goto legacy;
157 }
158
8c8cf0d9
MC
159 if (ctx->digest != NULL && ctx->digest->ctx_size > 0) {
160 OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
161 ctx->md_data = NULL;
162 }
163
164 /* TODO(3.0): Start of non-legacy code below */
165
166 if (type->prov == NULL) {
319e518a
MC
167#ifdef FIPS_MODE
168 /* We only do explict fetches inside the FIPS module */
169 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
170 return 0;
171#else
172 EVP_MD *provmd = EVP_MD_fetch(NULL, OBJ_nid2sn(type->type), "");
173
8c8cf0d9
MC
174 if (provmd == NULL) {
175 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
176 return 0;
177 }
178 type = provmd;
3fd70262 179 EVP_MD_free(ctx->fetched_digest);
8c8cf0d9 180 ctx->fetched_digest = provmd;
319e518a 181#endif
8c8cf0d9
MC
182 }
183
d5e5e2ff
SL
184 if (ctx->provctx != NULL && ctx->digest != NULL && ctx->digest != type) {
185 if (ctx->digest->freectx != NULL)
186 ctx->digest->freectx(ctx->provctx);
187 ctx->provctx = NULL;
188 }
8c8cf0d9
MC
189 ctx->digest = type;
190 if (ctx->provctx == NULL) {
a39eb840 191 ctx->provctx = ctx->digest->newctx(ossl_provider_ctx(type->prov));
8c8cf0d9
MC
192 if (ctx->provctx == NULL) {
193 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
194 return 0;
195 }
196 }
197
198 if (ctx->digest->dinit == NULL) {
199 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
200 return 0;
201 }
202
203 return ctx->digest->dinit(ctx->provctx);
204
205 /* TODO(3.0): Remove legacy code below */
206 legacy:
207
319e518a 208#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
0f113f3e 209 if (type) {
7c96dbcd 210 if (impl != NULL) {
0f113f3e
MC
211 if (!ENGINE_init(impl)) {
212 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
213 return 0;
214 }
7c96dbcd 215 } else {
0f113f3e 216 /* Ask if an ENGINE is reserved for this job */
8c8cf0d9 217 impl = tmpimpl;
7c96dbcd
RS
218 }
219 if (impl != NULL) {
0f113f3e
MC
220 /* There's an ENGINE for this job ... (apparently) */
221 const EVP_MD *d = ENGINE_get_digest(impl, type->type);
7c96dbcd
RS
222
223 if (d == NULL) {
0f113f3e
MC
224 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
225 ENGINE_finish(impl);
226 return 0;
227 }
228 /* We'll use the ENGINE's private digest definition */
229 type = d;
230 /*
231 * Store the ENGINE functional reference so we know 'type' came
232 * from an ENGINE and we need to release it when done.
233 */
234 ctx->engine = impl;
235 } else
236 ctx->engine = NULL;
a0108702
MC
237 } else {
238 if (!ctx->digest) {
239 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_NO_DIGEST_SET);
240 return 0;
241 }
242 type = ctx->digest;
0f113f3e 243 }
90e8a310 244#endif
0f113f3e 245 if (ctx->digest != type) {
ffe9150b 246 if (ctx->digest && ctx->digest->ctx_size) {
a93e0e78 247 OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
ffe9150b
MC
248 ctx->md_data = NULL;
249 }
0f113f3e
MC
250 ctx->digest = type;
251 if (!(ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) && type->ctx_size) {
252 ctx->update = type->update;
84c15091 253 ctx->md_data = OPENSSL_zalloc(type->ctx_size);
0f113f3e
MC
254 if (ctx->md_data == NULL) {
255 EVPerr(EVP_F_EVP_DIGESTINIT_EX, ERR_R_MALLOC_FAILURE);
256 return 0;
257 }
258 }
259 }
319e518a 260#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
0f113f3e 261 skip_to_init:
0b13e9f0 262#endif
319e518a 263#ifndef FIPS_MODE
dfcb5d29
MC
264 /*
265 * TODO(3.0): Temporarily no support for EVP_DigestSign* inside FIPS module
266 * or when using providers.
267 */
864b89ce
MC
268 if (ctx->pctx != NULL
269 && (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx->pctx)
270 || ctx->pctx->op.sig.signature == NULL)) {
0f113f3e
MC
271 int r;
272 r = EVP_PKEY_CTX_ctrl(ctx->pctx, -1, EVP_PKEY_OP_TYPE_SIG,
273 EVP_PKEY_CTRL_DIGESTINIT, 0, ctx);
274 if (r <= 0 && (r != -2))
275 return 0;
276 }
319e518a 277#endif
0f113f3e
MC
278 if (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT)
279 return 1;
280 return ctx->digest->init(ctx);
281}
d02b48c6 282
f80921b6 283int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t count)
0f113f3e 284{
a8274ea3
MC
285 if (count == 0)
286 return 1;
287
72df8f88
MC
288 if (ctx->pctx != NULL
289 && EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx->pctx)
290 && ctx->pctx->op.sig.sigprovctx != NULL) {
291 /*
292 * Prior to OpenSSL 3.0 EVP_DigestSignUpdate() and
293 * EVP_DigestVerifyUpdate() were just macros for EVP_DigestUpdate().
294 * Some code calls EVP_DigestUpdate() directly even when initialised
295 * with EVP_DigestSignInit_ex() or EVP_DigestVerifyInit_ex(), so we
296 * detect that and redirect to the correct EVP_Digest*Update() function
297 */
298 if (ctx->pctx->operation == EVP_PKEY_OP_SIGNCTX)
299 return EVP_DigestSignUpdate(ctx, data, count);
300 if (ctx->pctx->operation == EVP_PKEY_OP_VERIFYCTX)
301 return EVP_DigestVerifyUpdate(ctx, data, count);
302 EVPerr(EVP_F_EVP_DIGESTUPDATE, EVP_R_UPDATE_ERROR);
303 return 0;
304 }
305
8c8cf0d9
MC
306 if (ctx->digest == NULL || ctx->digest->prov == NULL)
307 goto legacy;
308
309 if (ctx->digest->dupdate == NULL) {
310 EVPerr(EVP_F_EVP_DIGESTUPDATE, EVP_R_UPDATE_ERROR);
311 return 0;
312 }
313 return ctx->digest->dupdate(ctx->provctx, data, count);
314
315 /* TODO(3.0): Remove legacy code below */
316 legacy:
0f113f3e
MC
317 return ctx->update(ctx, data, count);
318}
d02b48c6 319
dbad1690 320/* The caller can assume that this removes any secret data from the context */
2dc769a1 321int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size)
0f113f3e
MC
322{
323 int ret;
324 ret = EVP_DigestFinal_ex(ctx, md, size);
959ed531 325 EVP_MD_CTX_reset(ctx);
0f113f3e
MC
326 return ret;
327}
20d2186c
DSH
328
329/* The caller can assume that this removes any secret data from the context */
8c8cf0d9 330int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *isize)
0f113f3e
MC
331{
332 int ret;
8c8cf0d9 333 size_t size = 0;
0ad50b4d 334 size_t mdsize = EVP_MD_size(ctx->digest);
8c8cf0d9
MC
335
336 if (ctx->digest == NULL || ctx->digest->prov == NULL)
337 goto legacy;
54a656ef 338
8c8cf0d9
MC
339 if (ctx->digest->dfinal == NULL) {
340 EVPerr(EVP_F_EVP_DIGESTFINAL_EX, EVP_R_FINAL_ERROR);
341 return 0;
342 }
343
0ad50b4d 344 ret = ctx->digest->dfinal(ctx->provctx, md, &size, mdsize);
8c8cf0d9
MC
345
346 if (isize != NULL) {
347 if (size <= UINT_MAX) {
348 *isize = (int)size;
349 } else {
350 EVPerr(EVP_F_EVP_DIGESTFINAL_EX, EVP_R_FINAL_ERROR);
351 ret = 0;
352 }
353 }
354
8c8cf0d9
MC
355 return ret;
356
357 /* TODO(3.0): Remove legacy code below */
358 legacy:
0ad50b4d 359 OPENSSL_assert(mdsize <= EVP_MAX_MD_SIZE);
0f113f3e 360 ret = ctx->digest->final(ctx, md);
8c8cf0d9 361 if (isize != NULL)
0ad50b4d 362 *isize = mdsize;
0f113f3e
MC
363 if (ctx->digest->cleanup) {
364 ctx->digest->cleanup(ctx);
365 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
366 }
3ce2fdab 367 OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
0f113f3e
MC
368 return ret;
369}
351d8998 370
cd8d1456
AP
371int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t size)
372{
373 int ret = 0;
d5e5e2ff
SL
374 OSSL_PARAM params[2];
375 size_t i = 0;
376
377 if (ctx->digest == NULL || ctx->digest->prov == NULL)
378 goto legacy;
cd8d1456 379
d5e5e2ff
SL
380 if (ctx->digest->dfinal == NULL) {
381 EVPerr(EVP_F_EVP_DIGESTFINALXOF, EVP_R_FINAL_ERROR);
382 return 0;
383 }
384
4e7991b4 385 params[i++] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN, &size);
d5e5e2ff
SL
386 params[i++] = OSSL_PARAM_construct_end();
387
388 if (EVP_MD_CTX_set_params(ctx, params) > 0)
389 ret = ctx->digest->dfinal(ctx->provctx, md, &size, size);
390 EVP_MD_CTX_reset(ctx);
391 return ret;
392
393legacy:
cd8d1456
AP
394 if (ctx->digest->flags & EVP_MD_FLAG_XOF
395 && size <= INT_MAX
396 && ctx->digest->md_ctrl(ctx, EVP_MD_CTRL_XOF_LEN, (int)size, NULL)) {
397 ret = ctx->digest->final(ctx, md);
cd8d1456
AP
398 if (ctx->digest->cleanup != NULL) {
399 ctx->digest->cleanup(ctx);
400 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
401 }
402 OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
403 } else {
404 EVPerr(EVP_F_EVP_DIGESTFINALXOF, EVP_R_NOT_XOF_OR_INVALID_LENGTH);
405 }
406
407 return ret;
408}
409
dbad1690 410int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in)
0f113f3e 411{
959ed531 412 EVP_MD_CTX_reset(out);
0f113f3e
MC
413 return EVP_MD_CTX_copy_ex(out, in);
414}
20d2186c
DSH
415
416int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in)
0f113f3e
MC
417{
418 unsigned char *tmp_buf;
8c8cf0d9
MC
419
420 if (in == NULL || in->digest == NULL) {
0f113f3e
MC
421 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_INPUT_NOT_INITIALIZED);
422 return 0;
423 }
8c8cf0d9
MC
424
425 if (in->digest->prov == NULL)
426 goto legacy;
427
428 if (in->digest->dupctx == NULL) {
429 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_NOT_ABLE_TO_COPY_CTX);
430 return 0;
431 }
432
433 EVP_MD_CTX_reset(out);
434 if (out->fetched_digest != NULL)
3fd70262 435 EVP_MD_free(out->fetched_digest);
8c8cf0d9
MC
436 *out = *in;
437 /* NULL out pointers in case of error */
438 out->pctx = NULL;
439 out->provctx = NULL;
440
441 if (in->fetched_digest != NULL)
70c35fd1 442 EVP_MD_up_ref(in->fetched_digest);
8c8cf0d9
MC
443
444 out->provctx = in->digest->dupctx(in->provctx);
445 if (out->provctx == NULL) {
446 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_NOT_ABLE_TO_COPY_CTX);
447 return 0;
448 }
449
450 /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
451 EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
319e518a
MC
452#ifndef FIPS_MODE
453 /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
8c8cf0d9
MC
454 if (in->pctx != NULL) {
455 out->pctx = EVP_PKEY_CTX_dup(in->pctx);
456 if (out->pctx == NULL) {
457 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_NOT_ABLE_TO_COPY_CTX);
458 EVP_MD_CTX_reset(out);
459 return 0;
460 }
461 }
319e518a 462#endif
8c8cf0d9
MC
463
464 return 1;
465
466 /* TODO(3.0): Remove legacy code below */
467 legacy:
319e518a 468#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
0f113f3e
MC
469 /* Make sure it's safe to copy a digest context using an ENGINE */
470 if (in->engine && !ENGINE_init(in->engine)) {
471 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_ENGINE_LIB);
472 return 0;
473 }
0b13e9f0 474#endif
26188931 475
0f113f3e
MC
476 if (out->digest == in->digest) {
477 tmp_buf = out->md_data;
478 EVP_MD_CTX_set_flags(out, EVP_MD_CTX_FLAG_REUSE);
479 } else
480 tmp_buf = NULL;
959ed531 481 EVP_MD_CTX_reset(out);
b4faea50 482 memcpy(out, in, sizeof(*out));
0f113f3e 483
4803717f
PY
484 /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
485 EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
486
6aa0ba4b
RL
487 /* Null these variables, since they are getting fixed up
488 * properly below. Anything else may cause a memleak and/or
489 * double free if any of the memory allocations below fail
490 */
491 out->md_data = NULL;
492 out->pctx = NULL;
493
0f113f3e
MC
494 if (in->md_data && out->digest->ctx_size) {
495 if (tmp_buf)
496 out->md_data = tmp_buf;
497 else {
498 out->md_data = OPENSSL_malloc(out->digest->ctx_size);
90945fa3 499 if (out->md_data == NULL) {
0f113f3e
MC
500 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_MALLOC_FAILURE);
501 return 0;
502 }
503 }
504 memcpy(out->md_data, in->md_data, out->digest->ctx_size);
505 }
26188931 506
0f113f3e 507 out->update = in->update;
d4575825 508
319e518a
MC
509#ifndef FIPS_MODE
510 /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
0f113f3e
MC
511 if (in->pctx) {
512 out->pctx = EVP_PKEY_CTX_dup(in->pctx);
513 if (!out->pctx) {
959ed531 514 EVP_MD_CTX_reset(out);
0f113f3e
MC
515 return 0;
516 }
517 }
319e518a 518#endif
18327cd0 519
0f113f3e
MC
520 if (out->digest->copy)
521 return out->digest->copy(out, in);
3a828611 522
0f113f3e
MC
523 return 1;
524}
88ce56f8 525
9e0aad9f 526int EVP_Digest(const void *data, size_t count,
0f113f3e
MC
527 unsigned char *md, unsigned int *size, const EVP_MD *type,
528 ENGINE *impl)
529{
bfb0641f 530 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
0f113f3e 531 int ret;
dbad1690 532
74cabf3f
RL
533 if (ctx == NULL)
534 return 0;
535 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT);
536 ret = EVP_DigestInit_ex(ctx, type, impl)
537 && EVP_DigestUpdate(ctx, data, count)
538 && EVP_DigestFinal_ex(ctx, md, size);
bfb0641f 539 EVP_MD_CTX_free(ctx);
dbad1690 540
0f113f3e
MC
541 return ret;
542}
dbad1690 543
ae3ff60e
RL
544int EVP_MD_get_params(const EVP_MD *digest, OSSL_PARAM params[])
545{
546 if (digest != NULL && digest->get_params != NULL)
547 return digest->get_params(params);
548 return 0;
549}
550
551const OSSL_PARAM *EVP_MD_gettable_params(const EVP_MD *digest)
552{
553 if (digest != NULL && digest->gettable_params != NULL)
554 return digest->gettable_params();
555 return NULL;
556}
557
d5e5e2ff
SL
558int EVP_MD_CTX_set_params(EVP_MD_CTX *ctx, const OSSL_PARAM params[])
559{
5f5c3b4f
MC
560 EVP_PKEY_CTX *pctx = ctx->pctx;
561
92d9d0ae
RL
562 if (ctx->digest != NULL && ctx->digest->set_ctx_params != NULL)
563 return ctx->digest->set_ctx_params(ctx->provctx, params);
5f5c3b4f
MC
564
565 if (pctx != NULL
566 && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
567 || pctx->operation == EVP_PKEY_OP_SIGNCTX)
568 && pctx->op.sig.sigprovctx != NULL
569 && pctx->op.sig.signature->set_ctx_md_params != NULL)
570 return pctx->op.sig.signature->set_ctx_md_params(pctx->op.sig.sigprovctx,
571 params);
d5e5e2ff
SL
572 return 0;
573}
574
e6879a31 575const OSSL_PARAM *EVP_MD_settable_ctx_params(const EVP_MD *md)
ae3ff60e 576{
e6879a31
MC
577 if (md != NULL && md->settable_ctx_params != NULL)
578 return md->settable_ctx_params();
579 return NULL;
580}
581
582const OSSL_PARAM *EVP_MD_CTX_settable_params(EVP_MD_CTX *ctx)
583{
5f5c3b4f
MC
584 EVP_PKEY_CTX *pctx;
585
e6879a31
MC
586 if (ctx != NULL
587 && ctx->digest != NULL
588 && ctx->digest->settable_ctx_params != NULL)
589 return ctx->digest->settable_ctx_params();
590
5f5c3b4f
MC
591 pctx = ctx->pctx;
592 if (pctx != NULL
593 && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
594 || pctx->operation == EVP_PKEY_OP_SIGNCTX)
595 && pctx->op.sig.sigprovctx != NULL
596 && pctx->op.sig.signature->settable_ctx_md_params != NULL)
597 return pctx->op.sig.signature->settable_ctx_md_params(
598 pctx->op.sig.sigprovctx);
599
ae3ff60e
RL
600 return NULL;
601}
602
4e7991b4 603int EVP_MD_CTX_get_params(EVP_MD_CTX *ctx, OSSL_PARAM params[])
d5e5e2ff 604{
5f5c3b4f
MC
605 EVP_PKEY_CTX *pctx = ctx->pctx;
606
d5e5e2ff 607 if (ctx->digest != NULL && ctx->digest->get_params != NULL)
92d9d0ae 608 return ctx->digest->get_ctx_params(ctx->provctx, params);
5f5c3b4f
MC
609
610 if (pctx != NULL
611 && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
612 || pctx->operation == EVP_PKEY_OP_SIGNCTX)
613 && pctx->op.sig.sigprovctx != NULL
614 && pctx->op.sig.signature->get_ctx_md_params != NULL)
615 return pctx->op.sig.signature->get_ctx_md_params(pctx->op.sig.sigprovctx,
616 params);
617
d5e5e2ff
SL
618 return 0;
619}
620
e6879a31 621const OSSL_PARAM *EVP_MD_gettable_ctx_params(const EVP_MD *md)
ae3ff60e 622{
e6879a31
MC
623 if (md != NULL && md->gettable_ctx_params != NULL)
624 return md->gettable_ctx_params();
625 return NULL;
626}
627
628const OSSL_PARAM *EVP_MD_CTX_gettable_params(EVP_MD_CTX *ctx)
629{
5f5c3b4f
MC
630 EVP_PKEY_CTX *pctx;
631
e6879a31
MC
632 if (ctx != NULL
633 && ctx->digest != NULL
634 && ctx->digest->gettable_ctx_params != NULL)
635 return ctx->digest->gettable_ctx_params();
636
5f5c3b4f
MC
637 pctx = ctx->pctx;
638 if (pctx != NULL
639 && (pctx->operation == EVP_PKEY_OP_VERIFYCTX
640 || pctx->operation == EVP_PKEY_OP_SIGNCTX)
641 && pctx->op.sig.sigprovctx != NULL
642 && pctx->op.sig.signature->gettable_ctx_md_params != NULL)
643 return pctx->op.sig.signature->gettable_ctx_md_params(
644 pctx->op.sig.sigprovctx);
645
ae3ff60e
RL
646 return NULL;
647}
648
83b4a243 649/* TODO(3.0): Remove legacy code below - only used by engines & DigestSign */
396d5fd0
DSH
650int EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void *p2)
651{
6a3b7c68
RL
652 int ret = EVP_CTRL_RET_UNSUPPORTED;
653 int set_params = 1;
654 size_t sz;
655 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
656
657 if (ctx == NULL || ctx->digest == NULL) {
658 ERR_raise(ERR_LIB_EVP, EVP_R_MESSAGE_DIGEST_IS_NULL);
659 return 0;
660 }
661
5f5c3b4f
MC
662 if (ctx->digest->prov == NULL
663 && (ctx->pctx == NULL
664 || (ctx->pctx->operation != EVP_PKEY_OP_VERIFYCTX
665 && ctx->pctx->operation != EVP_PKEY_OP_SIGNCTX)))
6a3b7c68
RL
666 goto legacy;
667
668 switch (cmd) {
669 case EVP_MD_CTRL_XOF_LEN:
670 sz = (size_t)p1;
671 params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN, &sz);
672 break;
673 case EVP_MD_CTRL_MICALG:
674 set_params = 0;
675 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_DIGEST_PARAM_MICALG,
4e7991b4 676 p2, p1 ? p1 : 9999);
6a3b7c68 677 break;
c3885102
MC
678 case EVP_CTRL_SSL3_MASTER_SECRET:
679 params[0] = OSSL_PARAM_construct_octet_string(OSSL_DIGEST_PARAM_SSL3_MS,
680 p2, p1);
681 break;
6a3b7c68 682 default:
6a36f209 683 goto conclude;
396d5fd0 684 }
6a3b7c68
RL
685
686 if (set_params)
5f5c3b4f 687 ret = EVP_MD_CTX_set_params(ctx, params);
6a3b7c68 688 else
5f5c3b4f 689 ret = EVP_MD_CTX_get_params(ctx, params);
552be00d 690 goto conclude;
6a3b7c68
RL
691
692
693/* TODO(3.0): Remove legacy code below */
694 legacy:
695 if (ctx->digest->md_ctrl == NULL) {
696 ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_NOT_IMPLEMENTED);
697 return 0;
698 }
699
700 ret = ctx->digest->md_ctrl(ctx, cmd, p1, p2);
552be00d 701 conclude:
6a3b7c68
RL
702 if (ret <= 0)
703 return 0;
704 return ret;
396d5fd0 705}
3653d0c2 706
3fd70262
RL
707EVP_MD *evp_md_new(void)
708{
709 EVP_MD *md = OPENSSL_zalloc(sizeof(*md));
710
711 if (md != NULL) {
712 md->lock = CRYPTO_THREAD_lock_new();
713 if (md->lock == NULL) {
714 OPENSSL_free(md);
715 return NULL;
716 }
717 md->refcnt = 1;
718 }
719 return md;
720}
721
32040838
RL
722/*
723 * FIPS module note: since internal fetches will be entirely
724 * provider based, we know that none of its code depends on legacy
725 * NIDs or any functionality that use them.
726 */
727#ifndef FIPS_MODE
728/* TODO(3.x) get rid of the need for legacy NIDs */
729static void set_legacy_nid(const char *name, void *vlegacy_nid)
730{
731 int nid;
732 int *legacy_nid = vlegacy_nid;
733
734 if (*legacy_nid == -1) /* We found a clash already */
735 return;
736 if ((nid = OBJ_sn2nid(name)) == NID_undef
737 && (nid = OBJ_ln2nid(name)) == NID_undef)
738 return;
739 if (*legacy_nid != NID_undef && *legacy_nid != nid) {
740 *legacy_nid = -1;
741 return;
742 }
743 *legacy_nid = nid;
744}
745#endif
746
f7c16d48
RL
747static void *evp_md_from_dispatch(int name_id,
748 const OSSL_DISPATCH *fns,
0ddf74bf 749 OSSL_PROVIDER *prov)
3653d0c2
MC
750{
751 EVP_MD *md = NULL;
8c8cf0d9 752 int fncnt = 0;
3653d0c2 753
0211740f 754 /* EVP_MD_fetch() will set the legacy NID if available */
f7c16d48 755 if ((md = evp_md_new()) == NULL) {
6b9e3724 756 EVPerr(0, ERR_R_MALLOC_FAILURE);
3653d0c2 757 return NULL;
6b9e3724 758 }
3653d0c2 759
ed71e917 760#ifndef FIPS_MODE
32040838
RL
761 /* TODO(3.x) get rid of the need for legacy NIDs */
762 md->type = NID_undef;
f651c727 763 evp_names_do_all(prov, name_id, set_legacy_nid, &md->type);
32040838
RL
764 if (md->type == -1) {
765 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
766 EVP_MD_free(md);
767 return NULL;
f7c16d48 768 }
ed71e917
MC
769#endif
770
32040838
RL
771 md->name_id = name_id;
772
3653d0c2 773 for (; fns->function_id != 0; fns++) {
3653d0c2
MC
774 switch (fns->function_id) {
775 case OSSL_FUNC_DIGEST_NEWCTX:
d5e5e2ff
SL
776 if (md->newctx == NULL) {
777 md->newctx = OSSL_get_OP_digest_newctx(fns);
778 fncnt++;
779 }
3653d0c2
MC
780 break;
781 case OSSL_FUNC_DIGEST_INIT:
d5e5e2ff
SL
782 if (md->dinit == NULL) {
783 md->dinit = OSSL_get_OP_digest_init(fns);
784 fncnt++;
785 }
3653d0c2 786 break;
df05f2ce 787 case OSSL_FUNC_DIGEST_UPDATE:
d5e5e2ff
SL
788 if (md->dupdate == NULL) {
789 md->dupdate = OSSL_get_OP_digest_update(fns);
790 fncnt++;
791 }
3653d0c2
MC
792 break;
793 case OSSL_FUNC_DIGEST_FINAL:
d5e5e2ff
SL
794 if (md->dfinal == NULL) {
795 md->dfinal = OSSL_get_OP_digest_final(fns);
796 fncnt++;
797 }
3653d0c2
MC
798 break;
799 case OSSL_FUNC_DIGEST_DIGEST:
d5e5e2ff
SL
800 if (md->digest == NULL)
801 md->digest = OSSL_get_OP_digest_digest(fns);
3653d0c2
MC
802 /* We don't increment fnct for this as it is stand alone */
803 break;
3653d0c2 804 case OSSL_FUNC_DIGEST_FREECTX:
d5e5e2ff
SL
805 if (md->freectx == NULL) {
806 md->freectx = OSSL_get_OP_digest_freectx(fns);
807 fncnt++;
808 }
3653d0c2 809 break;
8c8cf0d9 810 case OSSL_FUNC_DIGEST_DUPCTX:
d5e5e2ff
SL
811 if (md->dupctx == NULL)
812 md->dupctx = OSSL_get_OP_digest_dupctx(fns);
8c8cf0d9 813 break;
d5e5e2ff
SL
814 case OSSL_FUNC_DIGEST_GET_PARAMS:
815 if (md->get_params == NULL)
816 md->get_params = OSSL_get_OP_digest_get_params(fns);
7556b9df 817 break;
92d9d0ae
RL
818 case OSSL_FUNC_DIGEST_SET_CTX_PARAMS:
819 if (md->set_ctx_params == NULL)
820 md->set_ctx_params = OSSL_get_OP_digest_set_ctx_params(fns);
6a3b7c68 821 break;
92d9d0ae
RL
822 case OSSL_FUNC_DIGEST_GET_CTX_PARAMS:
823 if (md->get_ctx_params == NULL)
824 md->get_ctx_params = OSSL_get_OP_digest_get_ctx_params(fns);
6a3b7c68 825 break;
ae3ff60e
RL
826 case OSSL_FUNC_DIGEST_GETTABLE_PARAMS:
827 if (md->gettable_params == NULL)
828 md->gettable_params = OSSL_get_OP_digest_gettable_params(fns);
829 break;
830 case OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS:
831 if (md->settable_ctx_params == NULL)
832 md->settable_ctx_params =
833 OSSL_get_OP_digest_settable_ctx_params(fns);
834 break;
835 case OSSL_FUNC_DIGEST_GETTABLE_CTX_PARAMS:
836 if (md->gettable_ctx_params == NULL)
837 md->gettable_ctx_params =
838 OSSL_get_OP_digest_gettable_ctx_params(fns);
839 break;
3653d0c2 840 }
8c8cf0d9
MC
841 }
842 if ((fncnt != 0 && fncnt != 5)
6a3b7c68 843 || (fncnt == 0 && md->digest == NULL)) {
8c8cf0d9
MC
844 /*
845 * In order to be a consistent set of functions we either need the
846 * whole set of init/update/final etc functions or none of them.
847 * The "digest" function can standalone. We at least need one way to
848 * generate digests.
849 */
3fd70262 850 EVP_MD_free(md);
6a3b7c68 851 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
8c8cf0d9 852 return NULL;
3653d0c2
MC
853 }
854 md->prov = prov;
855 if (prov != NULL)
7c95390e 856 ossl_provider_up_ref(prov);
3653d0c2
MC
857
858 return md;
859}
860
70c35fd1 861static int evp_md_up_ref(void *md)
3653d0c2 862{
70c35fd1 863 return EVP_MD_up_ref(md);
3653d0c2
MC
864}
865
866static void evp_md_free(void *md)
867{
3fd70262 868 EVP_MD_free(md);
3653d0c2
MC
869}
870
871EVP_MD *EVP_MD_fetch(OPENSSL_CTX *ctx, const char *algorithm,
872 const char *properties)
873{
0211740f
RL
874 EVP_MD *md =
875 evp_generic_fetch(ctx, OSSL_OP_DIGEST, algorithm, properties,
0ddf74bf 876 evp_md_from_dispatch, evp_md_up_ref, evp_md_free);
0211740f 877
0211740f 878 return md;
3653d0c2 879}
c540f00f 880
3fd70262
RL
881int EVP_MD_up_ref(EVP_MD *md)
882{
883 int ref = 0;
884
885 CRYPTO_UP_REF(&md->refcnt, &ref, md->lock);
886 return 1;
887}
888
889void EVP_MD_free(EVP_MD *md)
890{
891 int i;
892
893 if (md == NULL)
894 return;
895
896 CRYPTO_DOWN_REF(&md->refcnt, &i, md->lock);
897 if (i > 0)
898 return;
899 ossl_provider_free(md->prov);
3fd70262
RL
900 CRYPTO_THREAD_lock_free(md->lock);
901 OPENSSL_free(md);
902}
903
251e610c
RL
904void EVP_MD_do_all_provided(OPENSSL_CTX *libctx,
905 void (*fn)(EVP_MD *mac, void *arg),
906 void *arg)
c540f00f
RL
907{
908 evp_generic_do_all(libctx, OSSL_OP_DIGEST,
909 (void (*)(void *, void *))fn, arg,
0ddf74bf 910 evp_md_from_dispatch, evp_md_free);
c540f00f 911}