2 * Copyright 1995-2018 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 "internal/cryptlib.h"
12 #include <openssl/objects.h>
13 #include <openssl/evp.h>
14 #include <openssl/engine.h>
15 #include "internal/evp_int.h"
16 #include "internal/provider.h"
19 /* This call frees resources associated with the context */
20 int EVP_MD_CTX_reset(EVP_MD_CTX *ctx)
25 if (ctx->digest == NULL || ctx->digest->prov == NULL)
28 if (ctx->provctx != NULL) {
29 if (ctx->digest->freectx != NULL)
30 ctx->digest->freectx(ctx->provctx);
32 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
35 if (ctx->pctx != NULL)
40 /* TODO(3.0): Remove legacy code below */
44 * Don't assume ctx->md_data was cleaned in EVP_Digest_Final, because
45 * sometimes only copies of the context are ever finalised.
47 if (ctx->digest && ctx->digest->cleanup
48 && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_CLEANED))
49 ctx->digest->cleanup(ctx);
50 if (ctx->digest && ctx->digest->ctx_size && ctx->md_data
51 && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_REUSE)) {
52 OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
55 * pctx should be freed by the user of EVP_MD_CTX
56 * if EVP_MD_CTX_FLAG_KEEP_PKEY_CTX is set
59 /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
60 if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX))
61 EVP_PKEY_CTX_free(ctx->pctx);
63 # ifndef OPENSSL_NO_ENGINE
64 ENGINE_finish(ctx->engine);
67 OPENSSL_cleanse(ctx, sizeof(*ctx));
72 EVP_MD_CTX *EVP_MD_CTX_new(void)
74 return OPENSSL_zalloc(sizeof(EVP_MD_CTX));
77 void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
82 if (ctx->digest == NULL || ctx->digest->prov == NULL)
85 EVP_MD_CTX_reset(ctx);
87 EVP_MD_meth_free(ctx->fetched_digest);
88 ctx->fetched_digest = NULL;
90 ctx->reqdigest = NULL;
95 /* TODO(3.0): Remove legacy code below */
97 EVP_MD_CTX_reset(ctx);
101 int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type)
103 EVP_MD_CTX_reset(ctx);
104 return EVP_DigestInit_ex(ctx, type, NULL);
107 int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
109 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
110 ENGINE *tmpimpl = NULL;
113 EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
116 ctx->reqdigest = type;
118 /* TODO(3.0): Legacy work around code below. Remove this */
119 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
121 * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
122 * this context may already have an ENGINE! Try to avoid releasing the
123 * previous handle, re-querying for an ENGINE, and having a
124 * reinitialisation, when it may all be unnecessary.
126 if (ctx->engine && ctx->digest &&
127 (type == NULL || (type->type == ctx->digest->type)))
130 if (type != NULL && impl == NULL)
131 tmpimpl = ENGINE_get_digest_engine(type->type);
135 * If there are engines involved or if we're being used as part of
136 * EVP_DigestSignInit then we should use legacy handling for now.
138 if (ctx->engine != NULL
140 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
144 || (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0) {
145 if (ctx->digest == ctx->fetched_digest)
147 EVP_MD_meth_free(ctx->fetched_digest);
148 ctx->fetched_digest = NULL;
152 if (type->prov == NULL) {
162 if (ctx->digest != NULL && ctx->digest->ctx_size > 0) {
163 OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
167 /* TODO(3.0): Start of non-legacy code below */
169 if (type->prov == NULL) {
171 /* We only do explict fetches inside the FIPS module */
172 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
175 EVP_MD *provmd = EVP_MD_fetch(NULL, OBJ_nid2sn(type->type), "");
177 if (provmd == NULL) {
178 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
182 EVP_MD_meth_free(ctx->fetched_digest);
183 ctx->fetched_digest = provmd;
188 if (ctx->provctx == NULL) {
189 ctx->provctx = ctx->digest->newctx(ossl_provider_ctx(type->prov));
190 if (ctx->provctx == NULL) {
191 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
196 if (ctx->digest->dinit == NULL) {
197 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
201 return ctx->digest->dinit(ctx->provctx);
203 /* TODO(3.0): Remove legacy code below */
206 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
209 * Ensure an ENGINE left lying around from last time is cleared (the
210 * previous check attempted to avoid this if the same ENGINE and
211 * EVP_MD could be used).
213 ENGINE_finish(ctx->engine);
215 if (!ENGINE_init(impl)) {
216 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
220 /* Ask if an ENGINE is reserved for this job */
224 /* There's an ENGINE for this job ... (apparently) */
225 const EVP_MD *d = ENGINE_get_digest(impl, type->type);
228 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
232 /* We'll use the ENGINE's private digest definition */
235 * Store the ENGINE functional reference so we know 'type' came
236 * from an ENGINE and we need to release it when done.
243 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_NO_DIGEST_SET);
249 if (ctx->digest != type) {
250 if (ctx->digest && ctx->digest->ctx_size) {
251 OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
255 if (!(ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) && type->ctx_size) {
256 ctx->update = type->update;
257 ctx->md_data = OPENSSL_zalloc(type->ctx_size);
258 if (ctx->md_data == NULL) {
259 EVPerr(EVP_F_EVP_DIGESTINIT_EX, ERR_R_MALLOC_FAILURE);
264 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
268 /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
269 if (ctx->pctx != NULL) {
271 r = EVP_PKEY_CTX_ctrl(ctx->pctx, -1, EVP_PKEY_OP_TYPE_SIG,
272 EVP_PKEY_CTRL_DIGESTINIT, 0, ctx);
273 if (r <= 0 && (r != -2))
277 if (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT)
279 return ctx->digest->init(ctx);
282 int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t count)
287 if (ctx->digest == NULL || ctx->digest->prov == NULL)
290 if (ctx->digest->dupdate == NULL) {
291 EVPerr(EVP_F_EVP_DIGESTUPDATE, EVP_R_UPDATE_ERROR);
294 return ctx->digest->dupdate(ctx->provctx, data, count);
296 /* TODO(3.0): Remove legacy code below */
298 return ctx->update(ctx, data, count);
301 /* The caller can assume that this removes any secret data from the context */
302 int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size)
305 ret = EVP_DigestFinal_ex(ctx, md, size);
306 EVP_MD_CTX_reset(ctx);
310 /* The caller can assume that this removes any secret data from the context */
311 int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *isize)
315 size_t mdsize = EVP_MD_size(ctx->digest);
317 if (ctx->digest == NULL || ctx->digest->prov == NULL)
320 if (ctx->digest->dfinal == NULL) {
321 EVPerr(EVP_F_EVP_DIGESTFINAL_EX, EVP_R_FINAL_ERROR);
325 ret = ctx->digest->dfinal(ctx->provctx, md, &size, mdsize);
328 if (size <= UINT_MAX) {
331 EVPerr(EVP_F_EVP_DIGESTFINAL_EX, EVP_R_FINAL_ERROR);
336 EVP_MD_CTX_reset(ctx);
340 /* TODO(3.0): Remove legacy code below */
342 OPENSSL_assert(mdsize <= EVP_MAX_MD_SIZE);
343 ret = ctx->digest->final(ctx, md);
346 if (ctx->digest->cleanup) {
347 ctx->digest->cleanup(ctx);
348 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
350 OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
354 int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t size)
358 if (ctx->digest->flags & EVP_MD_FLAG_XOF
360 && ctx->digest->md_ctrl(ctx, EVP_MD_CTRL_XOF_LEN, (int)size, NULL)) {
361 ret = ctx->digest->final(ctx, md);
363 if (ctx->digest->cleanup != NULL) {
364 ctx->digest->cleanup(ctx);
365 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
367 OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
369 EVPerr(EVP_F_EVP_DIGESTFINALXOF, EVP_R_NOT_XOF_OR_INVALID_LENGTH);
375 int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in)
377 EVP_MD_CTX_reset(out);
378 return EVP_MD_CTX_copy_ex(out, in);
381 int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in)
383 unsigned char *tmp_buf;
385 if (in == NULL || in->digest == NULL) {
386 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_INPUT_NOT_INITIALIZED);
390 if (in->digest->prov == NULL)
393 if (in->digest->dupctx == NULL) {
394 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_NOT_ABLE_TO_COPY_CTX);
398 EVP_MD_CTX_reset(out);
399 if (out->fetched_digest != NULL)
400 EVP_MD_meth_free(out->fetched_digest);
402 /* NULL out pointers in case of error */
406 if (in->fetched_digest != NULL)
407 EVP_MD_upref(in->fetched_digest);
409 out->provctx = in->digest->dupctx(in->provctx);
410 if (out->provctx == NULL) {
411 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_NOT_ABLE_TO_COPY_CTX);
415 /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
416 EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
418 /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
419 if (in->pctx != NULL) {
420 out->pctx = EVP_PKEY_CTX_dup(in->pctx);
421 if (out->pctx == NULL) {
422 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_NOT_ABLE_TO_COPY_CTX);
423 EVP_MD_CTX_reset(out);
431 /* TODO(3.0): Remove legacy code below */
433 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
434 /* Make sure it's safe to copy a digest context using an ENGINE */
435 if (in->engine && !ENGINE_init(in->engine)) {
436 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_ENGINE_LIB);
441 if (out->digest == in->digest) {
442 tmp_buf = out->md_data;
443 EVP_MD_CTX_set_flags(out, EVP_MD_CTX_FLAG_REUSE);
446 EVP_MD_CTX_reset(out);
447 memcpy(out, in, sizeof(*out));
449 /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
450 EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
452 /* Null these variables, since they are getting fixed up
453 * properly below. Anything else may cause a memleak and/or
454 * double free if any of the memory allocations below fail
459 if (in->md_data && out->digest->ctx_size) {
461 out->md_data = tmp_buf;
463 out->md_data = OPENSSL_malloc(out->digest->ctx_size);
464 if (out->md_data == NULL) {
465 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_MALLOC_FAILURE);
469 memcpy(out->md_data, in->md_data, out->digest->ctx_size);
472 out->update = in->update;
475 /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
477 out->pctx = EVP_PKEY_CTX_dup(in->pctx);
479 EVP_MD_CTX_reset(out);
485 if (out->digest->copy)
486 return out->digest->copy(out, in);
491 int EVP_Digest(const void *data, size_t count,
492 unsigned char *md, unsigned int *size, const EVP_MD *type,
495 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
500 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT);
501 ret = EVP_DigestInit_ex(ctx, type, impl)
502 && EVP_DigestUpdate(ctx, data, count)
503 && EVP_DigestFinal_ex(ctx, md, size);
504 EVP_MD_CTX_free(ctx);
509 int EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void *p2)
511 if (ctx->digest && ctx->digest->md_ctrl) {
512 int ret = ctx->digest->md_ctrl(ctx, cmd, p1, p2);
520 static void *evp_md_from_dispatch(const OSSL_DISPATCH *fns,
526 /* EVP_MD_fetch() will set the legacy NID if available */
527 if ((md = EVP_MD_meth_new(NID_undef, NID_undef)) == NULL)
530 for (; fns->function_id != 0; fns++) {
531 switch (fns->function_id) {
532 case OSSL_FUNC_DIGEST_NEWCTX:
533 if (md->newctx != NULL)
535 md->newctx = OSSL_get_OP_digest_newctx(fns);
538 case OSSL_FUNC_DIGEST_INIT:
539 if (md->dinit != NULL)
541 md->dinit = OSSL_get_OP_digest_init(fns);
544 case OSSL_FUNC_DIGEST_UPDATE:
545 if (md->dupdate != NULL)
547 md->dupdate = OSSL_get_OP_digest_update(fns);
550 case OSSL_FUNC_DIGEST_FINAL:
551 if (md->dfinal != NULL)
553 md->dfinal = OSSL_get_OP_digest_final(fns);
556 case OSSL_FUNC_DIGEST_DIGEST:
557 if (md->digest != NULL)
559 md->digest = OSSL_get_OP_digest_digest(fns);
560 /* We don't increment fnct for this as it is stand alone */
562 case OSSL_FUNC_DIGEST_FREECTX:
563 if (md->freectx != NULL)
565 md->freectx = OSSL_get_OP_digest_freectx(fns);
568 case OSSL_FUNC_DIGEST_DUPCTX:
569 if (md->dupctx != NULL)
571 md->dupctx = OSSL_get_OP_digest_dupctx(fns);
573 case OSSL_FUNC_DIGEST_SIZE:
574 if (md->size != NULL)
576 md->size = OSSL_get_OP_digest_size(fns);
578 case OSSL_FUNC_DIGEST_BLOCK_SIZE:
579 if (md->dblock_size != NULL)
581 md->dblock_size = OSSL_get_OP_digest_block_size(fns);
585 if ((fncnt != 0 && fncnt != 5)
586 || (fncnt == 0 && md->digest == NULL)
587 || md->size == NULL) {
589 * In order to be a consistent set of functions we either need the
590 * whole set of init/update/final etc functions or none of them.
591 * The "digest" function can standalone. We at least need one way to
594 EVP_MD_meth_free(md);
599 ossl_provider_upref(prov);
604 static int evp_md_upref(void *md)
606 return EVP_MD_upref(md);
609 static void evp_md_free(void *md)
611 EVP_MD_meth_free(md);
614 EVP_MD *EVP_MD_fetch(OPENSSL_CTX *ctx, const char *algorithm,
615 const char *properties)
618 evp_generic_fetch(ctx, OSSL_OP_DIGEST, algorithm, properties,
619 evp_md_from_dispatch, evp_md_upref,
623 /* TODO(3.x) get rid of the need for legacy NIDs */
626 * FIPS module note: since internal fetches will be entirely
627 * provider based, we know that none of its code depends on legacy
628 * NIDs or any functionality that use them.
630 md->type = OBJ_sn2nid(algorithm);