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