VirtualBox

source: vbox/trunk/src/libs/openssl-3.0.1/crypto/evp/digest.c@ 94083

Last change on this file since 94083 was 94082, checked in by vboxsync, 3 years ago

libs/openssl-3.0.1: started applying and adjusting our OpenSSL changes to 3.0.1. bugref:10128

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette