2 * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
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
11 #include <openssl/objects.h>
12 #include <openssl/evp.h>
13 #include <openssl/engine.h>
14 #include <openssl/params.h>
15 #include <openssl/core_names.h>
16 #include "internal/cryptlib.h"
17 #include "internal/evp_int.h"
18 #include "internal/provider.h"
21 /* This call frees resources associated with the context */
22 int EVP_MD_CTX_reset(EVP_MD_CTX *ctx)
27 if (ctx->digest == NULL || ctx->digest->prov == NULL)
30 if (ctx->provctx != NULL) {
31 if (ctx->digest->freectx != NULL)
32 ctx->digest->freectx(ctx->provctx);
34 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
37 if (ctx->pctx != NULL)
42 /* TODO(3.0): Remove legacy code below */
46 * Don't assume ctx->md_data was cleaned in EVP_Digest_Final, because
47 * sometimes only copies of the context are ever finalised.
49 if (ctx->digest && ctx->digest->cleanup
50 && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_CLEANED))
51 ctx->digest->cleanup(ctx);
52 if (ctx->digest && ctx->digest->ctx_size && ctx->md_data
53 && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_REUSE)) {
54 OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
57 * pctx should be freed by the user of EVP_MD_CTX
58 * if EVP_MD_CTX_FLAG_KEEP_PKEY_CTX is set
61 /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
62 if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX))
63 EVP_PKEY_CTX_free(ctx->pctx);
65 # ifndef OPENSSL_NO_ENGINE
66 ENGINE_finish(ctx->engine);
69 OPENSSL_cleanse(ctx, sizeof(*ctx));
74 EVP_MD_CTX *EVP_MD_CTX_new(void)
76 return OPENSSL_zalloc(sizeof(EVP_MD_CTX));
79 void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
84 if (ctx->digest == NULL || ctx->digest->prov == NULL)
87 EVP_MD_CTX_reset(ctx);
89 EVP_MD_meth_free(ctx->fetched_digest);
90 ctx->fetched_digest = NULL;
92 ctx->reqdigest = NULL;
97 /* TODO(3.0): Remove legacy code below */
99 EVP_MD_CTX_reset(ctx);
103 int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type)
105 EVP_MD_CTX_reset(ctx);
106 return EVP_DigestInit_ex(ctx, type, NULL);
109 int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
111 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
112 ENGINE *tmpimpl = NULL;
115 EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
118 ctx->reqdigest = type;
120 /* TODO(3.0): Legacy work around code below. Remove this */
121 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
123 * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
124 * this context may already have an ENGINE! Try to avoid releasing the
125 * previous handle, re-querying for an ENGINE, and having a
126 * reinitialisation, when it may all be unnecessary.
128 if (ctx->engine && ctx->digest &&
129 (type == NULL || (type->type == ctx->digest->type)))
132 if (type != NULL && impl == NULL)
133 tmpimpl = ENGINE_get_digest_engine(type->type);
137 * If there are engines involved or if we're being used as part of
138 * EVP_DigestSignInit then we should use legacy handling for now.
140 if (ctx->engine != NULL
142 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
146 || (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0) {
147 if (ctx->digest == ctx->fetched_digest)
149 EVP_MD_meth_free(ctx->fetched_digest);
150 ctx->fetched_digest = NULL;
154 if (ctx->digest != NULL && ctx->digest->ctx_size > 0) {
155 OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
159 /* TODO(3.0): Start of non-legacy code below */
161 if (type->prov == NULL) {
163 /* We only do explict fetches inside the FIPS module */
164 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
167 EVP_MD *provmd = EVP_MD_fetch(NULL, OBJ_nid2sn(type->type), "");
169 if (provmd == NULL) {
170 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
174 EVP_MD_meth_free(ctx->fetched_digest);
175 ctx->fetched_digest = provmd;
179 if (ctx->provctx != NULL && ctx->digest != NULL && ctx->digest != type) {
180 if (ctx->digest->freectx != NULL)
181 ctx->digest->freectx(ctx->provctx);
185 if (ctx->provctx == NULL) {
186 ctx->provctx = ctx->digest->newctx(ossl_provider_ctx(type->prov));
187 if (ctx->provctx == NULL) {
188 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
193 if (ctx->digest->dinit == NULL) {
194 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
198 return ctx->digest->dinit(ctx->provctx);
200 /* TODO(3.0): Remove legacy code below */
203 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
206 * Ensure an ENGINE left lying around from last time is cleared (the
207 * previous check attempted to avoid this if the same ENGINE and
208 * EVP_MD could be used).
210 ENGINE_finish(ctx->engine);
212 if (!ENGINE_init(impl)) {
213 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
217 /* Ask if an ENGINE is reserved for this job */
221 /* There's an ENGINE for this job ... (apparently) */
222 const EVP_MD *d = ENGINE_get_digest(impl, type->type);
225 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
229 /* We'll use the ENGINE's private digest definition */
232 * Store the ENGINE functional reference so we know 'type' came
233 * from an ENGINE and we need to release it when done.
240 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_NO_DIGEST_SET);
246 if (ctx->digest != type) {
247 if (ctx->digest && ctx->digest->ctx_size) {
248 OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
252 if (!(ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) && type->ctx_size) {
253 ctx->update = type->update;
254 ctx->md_data = OPENSSL_zalloc(type->ctx_size);
255 if (ctx->md_data == NULL) {
256 EVPerr(EVP_F_EVP_DIGESTINIT_EX, ERR_R_MALLOC_FAILURE);
261 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
265 /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
266 if (ctx->pctx != NULL) {
268 r = EVP_PKEY_CTX_ctrl(ctx->pctx, -1, EVP_PKEY_OP_TYPE_SIG,
269 EVP_PKEY_CTRL_DIGESTINIT, 0, ctx);
270 if (r <= 0 && (r != -2))
274 if (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT)
276 return ctx->digest->init(ctx);
279 int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t count)
284 if (ctx->digest == NULL || ctx->digest->prov == NULL)
287 if (ctx->digest->dupdate == NULL) {
288 EVPerr(EVP_F_EVP_DIGESTUPDATE, EVP_R_UPDATE_ERROR);
291 return ctx->digest->dupdate(ctx->provctx, data, count);
293 /* TODO(3.0): Remove legacy code below */
295 return ctx->update(ctx, data, count);
298 /* The caller can assume that this removes any secret data from the context */
299 int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size)
302 ret = EVP_DigestFinal_ex(ctx, md, size);
303 EVP_MD_CTX_reset(ctx);
307 /* The caller can assume that this removes any secret data from the context */
308 int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *isize)
312 size_t mdsize = EVP_MD_size(ctx->digest);
314 if (ctx->digest == NULL || ctx->digest->prov == NULL)
317 if (ctx->digest->dfinal == NULL) {
318 EVPerr(EVP_F_EVP_DIGESTFINAL_EX, EVP_R_FINAL_ERROR);
322 ret = ctx->digest->dfinal(ctx->provctx, md, &size, mdsize);
325 if (size <= UINT_MAX) {
328 EVPerr(EVP_F_EVP_DIGESTFINAL_EX, EVP_R_FINAL_ERROR);
333 EVP_MD_CTX_reset(ctx);
336 /* TODO(3.0): Remove legacy code below */
338 OPENSSL_assert(mdsize <= EVP_MAX_MD_SIZE);
339 ret = ctx->digest->final(ctx, md);
342 if (ctx->digest->cleanup) {
343 ctx->digest->cleanup(ctx);
344 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
346 OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
350 int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t size)
353 OSSL_PARAM params[2];
356 if (ctx->digest == NULL || ctx->digest->prov == NULL)
359 if (ctx->digest->dfinal == NULL) {
360 EVPerr(EVP_F_EVP_DIGESTFINALXOF, EVP_R_FINAL_ERROR);
364 params[i++] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN,
366 params[i++] = OSSL_PARAM_construct_end();
368 if (EVP_MD_CTX_set_params(ctx, params) > 0)
369 ret = ctx->digest->dfinal(ctx->provctx, md, &size, size);
370 EVP_MD_CTX_reset(ctx);
374 if (ctx->digest->flags & EVP_MD_FLAG_XOF
376 && ctx->digest->md_ctrl(ctx, EVP_MD_CTRL_XOF_LEN, (int)size, NULL)) {
377 ret = ctx->digest->final(ctx, md);
378 if (ctx->digest->cleanup != NULL) {
379 ctx->digest->cleanup(ctx);
380 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
382 OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
384 EVPerr(EVP_F_EVP_DIGESTFINALXOF, EVP_R_NOT_XOF_OR_INVALID_LENGTH);
390 int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in)
392 EVP_MD_CTX_reset(out);
393 return EVP_MD_CTX_copy_ex(out, in);
396 int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in)
398 unsigned char *tmp_buf;
400 if (in == NULL || in->digest == NULL) {
401 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_INPUT_NOT_INITIALIZED);
405 if (in->digest->prov == NULL)
408 if (in->digest->dupctx == NULL) {
409 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_NOT_ABLE_TO_COPY_CTX);
413 EVP_MD_CTX_reset(out);
414 if (out->fetched_digest != NULL)
415 EVP_MD_meth_free(out->fetched_digest);
417 /* NULL out pointers in case of error */
421 if (in->fetched_digest != NULL)
422 EVP_MD_upref(in->fetched_digest);
424 out->provctx = in->digest->dupctx(in->provctx);
425 if (out->provctx == NULL) {
426 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_NOT_ABLE_TO_COPY_CTX);
430 /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
431 EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
433 /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
434 if (in->pctx != NULL) {
435 out->pctx = EVP_PKEY_CTX_dup(in->pctx);
436 if (out->pctx == NULL) {
437 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_NOT_ABLE_TO_COPY_CTX);
438 EVP_MD_CTX_reset(out);
446 /* TODO(3.0): Remove legacy code below */
448 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
449 /* Make sure it's safe to copy a digest context using an ENGINE */
450 if (in->engine && !ENGINE_init(in->engine)) {
451 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_ENGINE_LIB);
456 if (out->digest == in->digest) {
457 tmp_buf = out->md_data;
458 EVP_MD_CTX_set_flags(out, EVP_MD_CTX_FLAG_REUSE);
461 EVP_MD_CTX_reset(out);
462 memcpy(out, in, sizeof(*out));
464 /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
465 EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
467 /* Null these variables, since they are getting fixed up
468 * properly below. Anything else may cause a memleak and/or
469 * double free if any of the memory allocations below fail
474 if (in->md_data && out->digest->ctx_size) {
476 out->md_data = tmp_buf;
478 out->md_data = OPENSSL_malloc(out->digest->ctx_size);
479 if (out->md_data == NULL) {
480 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_MALLOC_FAILURE);
484 memcpy(out->md_data, in->md_data, out->digest->ctx_size);
487 out->update = in->update;
490 /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
492 out->pctx = EVP_PKEY_CTX_dup(in->pctx);
494 EVP_MD_CTX_reset(out);
500 if (out->digest->copy)
501 return out->digest->copy(out, in);
506 int EVP_Digest(const void *data, size_t count,
507 unsigned char *md, unsigned int *size, const EVP_MD *type,
510 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
515 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT);
516 ret = EVP_DigestInit_ex(ctx, type, impl)
517 && EVP_DigestUpdate(ctx, data, count)
518 && EVP_DigestFinal_ex(ctx, md, size);
519 EVP_MD_CTX_free(ctx);
524 int EVP_MD_CTX_set_params(EVP_MD_CTX *ctx, const OSSL_PARAM params[])
526 if (ctx->digest != NULL && ctx->digest->set_params != NULL)
527 return ctx->digest->set_params(ctx->provctx, params);
531 int EVP_MD_CTX_get_params(EVP_MD_CTX *ctx, const OSSL_PARAM params[])
533 if (ctx->digest != NULL && ctx->digest->get_params != NULL)
534 return ctx->digest->get_params(ctx->provctx, params);
539 int EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void *p2)
541 if (ctx->digest != NULL) {
542 OSSL_PARAM params[2];
546 case EVP_MD_CTRL_XOF_LEN:
547 if (ctx->digest->set_params == NULL)
550 params[n++] = OSSL_PARAM_construct_size_t(
551 OSSL_DIGEST_PARAM_XOFLEN, &i, &sz);
552 params[n++] = OSSL_PARAM_construct_end();
553 return ctx->digest->set_params(ctx->provctx, params) > 0;
554 case EVP_MD_CTRL_MICALG:
555 if (ctx->digest->get_params == NULL)
557 params[n++] = OSSL_PARAM_construct_utf8_string(
558 OSSL_DIGEST_PARAM_MICALG, p2, p1 ? p1 : 9999,
560 params[n++] = OSSL_PARAM_construct_end();
561 return ctx->digest->get_params(ctx->provctx, params);
564 if (ctx->digest->md_ctrl != NULL) {
565 int ret = ctx->digest->md_ctrl(ctx, cmd, p1, p2);
575 static void *evp_md_from_dispatch(const OSSL_DISPATCH *fns,
581 /* EVP_MD_fetch() will set the legacy NID if available */
582 if ((md = EVP_MD_meth_new(NID_undef, NID_undef)) == NULL)
585 for (; fns->function_id != 0; fns++) {
586 switch (fns->function_id) {
587 case OSSL_FUNC_DIGEST_NEWCTX:
588 if (md->newctx == NULL) {
589 md->newctx = OSSL_get_OP_digest_newctx(fns);
593 case OSSL_FUNC_DIGEST_INIT:
594 if (md->dinit == NULL) {
595 md->dinit = OSSL_get_OP_digest_init(fns);
599 case OSSL_FUNC_DIGEST_UPDATE:
600 if (md->dupdate == NULL) {
601 md->dupdate = OSSL_get_OP_digest_update(fns);
605 case OSSL_FUNC_DIGEST_FINAL:
606 if (md->dfinal == NULL) {
607 md->dfinal = OSSL_get_OP_digest_final(fns);
611 case OSSL_FUNC_DIGEST_DIGEST:
612 if (md->digest == NULL)
613 md->digest = OSSL_get_OP_digest_digest(fns);
614 /* We don't increment fnct for this as it is stand alone */
616 case OSSL_FUNC_DIGEST_FREECTX:
617 if (md->freectx == NULL) {
618 md->freectx = OSSL_get_OP_digest_freectx(fns);
622 case OSSL_FUNC_DIGEST_DUPCTX:
623 if (md->dupctx == NULL)
624 md->dupctx = OSSL_get_OP_digest_dupctx(fns);
626 case OSSL_FUNC_DIGEST_SIZE:
627 if (md->size == NULL)
628 md->size = OSSL_get_OP_digest_size(fns);
630 case OSSL_FUNC_DIGEST_BLOCK_SIZE:
631 if (md->dblock_size == NULL)
632 md->dblock_size = OSSL_get_OP_digest_block_size(fns);
634 case OSSL_FUNC_DIGEST_SET_PARAMS:
635 if (md->set_params == NULL)
636 md->set_params = OSSL_get_OP_digest_set_params(fns);
638 case OSSL_FUNC_DIGEST_GET_PARAMS:
639 if (md->get_params == NULL)
640 md->get_params = OSSL_get_OP_digest_get_params(fns);
644 if ((fncnt != 0 && fncnt != 5)
645 || (fncnt == 0 && md->digest == NULL)
646 || md->size == NULL) {
648 * In order to be a consistent set of functions we either need the
649 * whole set of init/update/final etc functions or none of them.
650 * The "digest" function can standalone. We at least need one way to
653 EVP_MD_meth_free(md);
658 ossl_provider_upref(prov);
663 static int evp_md_upref(void *md)
665 return EVP_MD_upref(md);
668 static void evp_md_free(void *md)
670 EVP_MD_meth_free(md);
673 EVP_MD *EVP_MD_fetch(OPENSSL_CTX *ctx, const char *algorithm,
674 const char *properties)
677 evp_generic_fetch(ctx, OSSL_OP_DIGEST, algorithm, properties,
678 evp_md_from_dispatch, evp_md_upref,
682 /* TODO(3.x) get rid of the need for legacy NIDs */
685 * FIPS module note: since internal fetches will be entirely
686 * provider based, we know that none of its code depends on legacy
687 * NIDs or any functionality that use them.
689 md->type = OBJ_sn2nid(algorithm);