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