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