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
58 if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX))
59 EVP_PKEY_CTX_free(ctx->pctx);
60 #ifndef OPENSSL_NO_ENGINE
61 ENGINE_finish(ctx->engine);
63 OPENSSL_cleanse(ctx, sizeof(*ctx));
68 EVP_MD_CTX *EVP_MD_CTX_new(void)
70 return OPENSSL_zalloc(sizeof(EVP_MD_CTX));
73 void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
78 if (ctx->digest == NULL || ctx->digest->prov == NULL)
81 EVP_MD_CTX_reset(ctx);
83 EVP_MD_meth_free(ctx->fetched_digest);
84 ctx->fetched_digest = NULL;
90 /* TODO(3.0): Remove legacy code below */
92 EVP_MD_CTX_reset(ctx);
96 int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type)
98 EVP_MD_CTX_reset(ctx);
99 return EVP_DigestInit_ex(ctx, type, NULL);
102 int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
105 ENGINE *tmpimpl = NULL;
107 EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
109 /* TODO(3.0): Legacy work around code below. Remove this */
110 #ifndef OPENSSL_NO_ENGINE
112 * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
113 * this context may already have an ENGINE! Try to avoid releasing the
114 * previous handle, re-querying for an ENGINE, and having a
115 * reinitialisation, when it may all be unnecessary.
117 if (ctx->engine && ctx->digest &&
118 (type == NULL || (type->type == ctx->digest->type)))
121 if (type != NULL && impl == NULL)
122 tmpimpl = ENGINE_get_digest_engine(type->type);
126 * If there are engines involved or if we're being used as part of
127 * EVP_DigestSignInit then we should use legacy handling for now.
129 if (ctx->engine != NULL
133 || (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0) {
134 if (ctx->digest == ctx->fetched_digest)
136 EVP_MD_meth_free(ctx->fetched_digest);
137 ctx->fetched_digest = NULL;
141 if (type->prov == NULL) {
148 if (ctx->digest != NULL && ctx->digest->ctx_size > 0) {
149 OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
153 /* TODO(3.0): Start of non-legacy code below */
155 if (type->prov == NULL) {
156 provmd = EVP_MD_fetch(NULL, OBJ_nid2sn(type->type), "");
157 if (provmd == NULL) {
158 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
162 EVP_MD_meth_free(ctx->fetched_digest);
163 ctx->fetched_digest = provmd;
167 if (ctx->provctx == NULL) {
168 ctx->provctx = ctx->digest->newctx();
169 if (ctx->provctx == NULL) {
170 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
175 if (ctx->digest->dinit == NULL) {
176 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
180 return ctx->digest->dinit(ctx->provctx);
182 /* TODO(3.0): Remove legacy code below */
185 #ifndef OPENSSL_NO_ENGINE
188 * Ensure an ENGINE left lying around from last time is cleared (the
189 * previous check attempted to avoid this if the same ENGINE and
190 * EVP_MD could be used).
192 ENGINE_finish(ctx->engine);
194 if (!ENGINE_init(impl)) {
195 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
199 /* Ask if an ENGINE is reserved for this job */
203 /* There's an ENGINE for this job ... (apparently) */
204 const EVP_MD *d = ENGINE_get_digest(impl, type->type);
207 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
211 /* We'll use the ENGINE's private digest definition */
214 * Store the ENGINE functional reference so we know 'type' came
215 * from an ENGINE and we need to release it when done.
222 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_NO_DIGEST_SET);
228 if (ctx->digest != type) {
229 if (ctx->digest && ctx->digest->ctx_size) {
230 OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
234 if (!(ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) && type->ctx_size) {
235 ctx->update = type->update;
236 ctx->md_data = OPENSSL_zalloc(type->ctx_size);
237 if (ctx->md_data == NULL) {
238 EVPerr(EVP_F_EVP_DIGESTINIT_EX, ERR_R_MALLOC_FAILURE);
243 #ifndef OPENSSL_NO_ENGINE
248 r = EVP_PKEY_CTX_ctrl(ctx->pctx, -1, EVP_PKEY_OP_TYPE_SIG,
249 EVP_PKEY_CTRL_DIGESTINIT, 0, ctx);
250 if (r <= 0 && (r != -2))
253 if (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT)
255 return ctx->digest->init(ctx);
258 int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t count)
260 if (ctx->digest == NULL || ctx->digest->prov == NULL)
263 if (ctx->digest->dupdate == NULL) {
264 EVPerr(EVP_F_EVP_DIGESTUPDATE, EVP_R_UPDATE_ERROR);
267 return ctx->digest->dupdate(ctx->provctx, data, count);
269 /* TODO(3.0): Remove legacy code below */
271 return ctx->update(ctx, data, count);
274 /* The caller can assume that this removes any secret data from the context */
275 int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size)
278 ret = EVP_DigestFinal_ex(ctx, md, size);
279 EVP_MD_CTX_reset(ctx);
283 /* The caller can assume that this removes any secret data from the context */
284 int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *isize)
289 if (ctx->digest == NULL || ctx->digest->prov == NULL)
292 if (ctx->digest->dfinal == NULL) {
293 EVPerr(EVP_F_EVP_DIGESTFINAL_EX, EVP_R_FINAL_ERROR);
297 ret = ctx->digest->dfinal(ctx->provctx, md, &size);
300 if (size <= UINT_MAX) {
303 EVPerr(EVP_F_EVP_DIGESTFINAL_EX, EVP_R_FINAL_ERROR);
308 EVP_MD_CTX_reset(ctx);
312 /* TODO(3.0): Remove legacy code below */
314 OPENSSL_assert(ctx->digest->md_size <= EVP_MAX_MD_SIZE);
315 ret = ctx->digest->final(ctx, md);
317 *isize = ctx->digest->md_size;
318 if (ctx->digest->cleanup) {
319 ctx->digest->cleanup(ctx);
320 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
322 OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
326 int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t size)
330 if (ctx->digest->flags & EVP_MD_FLAG_XOF
332 && ctx->digest->md_ctrl(ctx, EVP_MD_CTRL_XOF_LEN, (int)size, NULL)) {
333 ret = ctx->digest->final(ctx, md);
335 if (ctx->digest->cleanup != NULL) {
336 ctx->digest->cleanup(ctx);
337 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
339 OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
341 EVPerr(EVP_F_EVP_DIGESTFINALXOF, EVP_R_NOT_XOF_OR_INVALID_LENGTH);
347 int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in)
349 EVP_MD_CTX_reset(out);
350 return EVP_MD_CTX_copy_ex(out, in);
353 int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in)
355 unsigned char *tmp_buf;
357 if (in == NULL || in->digest == NULL) {
358 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_INPUT_NOT_INITIALIZED);
362 if (in->digest->prov == NULL)
365 if (in->digest->dupctx == NULL) {
366 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_NOT_ABLE_TO_COPY_CTX);
370 EVP_MD_CTX_reset(out);
371 if (out->fetched_digest != NULL)
372 EVP_MD_meth_free(out->fetched_digest);
374 /* NULL out pointers in case of error */
378 if (in->fetched_digest != NULL)
379 EVP_MD_upref(in->fetched_digest);
381 out->provctx = in->digest->dupctx(in->provctx);
382 if (out->provctx == NULL) {
383 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_NOT_ABLE_TO_COPY_CTX);
387 /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
388 EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
389 if (in->pctx != NULL) {
390 out->pctx = EVP_PKEY_CTX_dup(in->pctx);
391 if (out->pctx == NULL) {
392 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_NOT_ABLE_TO_COPY_CTX);
393 EVP_MD_CTX_reset(out);
400 /* TODO(3.0): Remove legacy code below */
402 #ifndef OPENSSL_NO_ENGINE
403 /* Make sure it's safe to copy a digest context using an ENGINE */
404 if (in->engine && !ENGINE_init(in->engine)) {
405 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_ENGINE_LIB);
410 if (out->digest == in->digest) {
411 tmp_buf = out->md_data;
412 EVP_MD_CTX_set_flags(out, EVP_MD_CTX_FLAG_REUSE);
415 EVP_MD_CTX_reset(out);
416 memcpy(out, in, sizeof(*out));
418 /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
419 EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
421 /* Null these variables, since they are getting fixed up
422 * properly below. Anything else may cause a memleak and/or
423 * double free if any of the memory allocations below fail
428 if (in->md_data && out->digest->ctx_size) {
430 out->md_data = tmp_buf;
432 out->md_data = OPENSSL_malloc(out->digest->ctx_size);
433 if (out->md_data == NULL) {
434 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_MALLOC_FAILURE);
438 memcpy(out->md_data, in->md_data, out->digest->ctx_size);
441 out->update = in->update;
444 out->pctx = EVP_PKEY_CTX_dup(in->pctx);
446 EVP_MD_CTX_reset(out);
451 if (out->digest->copy)
452 return out->digest->copy(out, in);
457 int EVP_Digest(const void *data, size_t count,
458 unsigned char *md, unsigned int *size, const EVP_MD *type,
461 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
466 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT);
467 ret = EVP_DigestInit_ex(ctx, type, impl)
468 && EVP_DigestUpdate(ctx, data, count)
469 && EVP_DigestFinal_ex(ctx, md, size);
470 EVP_MD_CTX_free(ctx);
475 int EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void *p2)
477 if (ctx->digest && ctx->digest->md_ctrl) {
478 int ret = ctx->digest->md_ctrl(ctx, cmd, p1, p2);
486 static void *evp_md_from_dispatch(int mdtype, const OSSL_DISPATCH *fns,
492 if ((md = EVP_MD_meth_new(mdtype, NID_undef)) == NULL)
495 for (; fns->function_id != 0; fns++) {
496 switch (fns->function_id) {
497 case OSSL_FUNC_DIGEST_NEWCTX:
498 if (md->newctx != NULL)
500 md->newctx = OSSL_get_OP_digest_newctx(fns);
503 case OSSL_FUNC_DIGEST_INIT:
504 if (md->dinit != NULL)
506 md->dinit = OSSL_get_OP_digest_init(fns);
509 case OSSL_FUNC_DIGEST_UPDDATE:
510 if (md->dupdate != NULL)
512 md->dupdate = OSSL_get_OP_digest_update(fns);
515 case OSSL_FUNC_DIGEST_FINAL:
516 if (md->dfinal != NULL)
518 md->dfinal = OSSL_get_OP_digest_final(fns);
521 case OSSL_FUNC_DIGEST_DIGEST:
522 if (md->digest != NULL)
524 md->digest = OSSL_get_OP_digest_digest(fns);
525 /* We don't increment fnct for this as it is stand alone */
527 case OSSL_FUNC_DIGEST_FREECTX:
528 if (md->freectx != NULL)
530 md->freectx = OSSL_get_OP_digest_freectx(fns);
533 case OSSL_FUNC_DIGEST_DUPCTX:
534 if (md->dupctx != NULL)
536 md->dupctx = OSSL_get_OP_digest_dupctx(fns);
538 case OSSL_FUNC_DIGEST_SIZE:
539 if (md->size != NULL)
541 md->size = OSSL_get_OP_digest_size(fns);
545 if ((fncnt != 0 && fncnt != 5)
546 || (fncnt == 0 && md->digest == NULL)
547 || md->size == NULL) {
549 * In order to be a consistent set of functions we either need the
550 * whole set of init/update/final etc functions or none of them.
551 * The "digest" function can standalone. We at least need one way to
554 EVP_MD_meth_free(md);
559 ossl_provider_upref(prov);
564 static int evp_md_upref(void *md)
566 return EVP_MD_upref(md);
569 static void evp_md_free(void *md)
571 EVP_MD_meth_free(md);
574 EVP_MD *EVP_MD_fetch(OPENSSL_CTX *ctx, const char *algorithm,
575 const char *properties)
577 return evp_generic_fetch(ctx, OSSL_OP_DIGEST, algorithm, properties,
578 evp_md_from_dispatch, evp_md_upref,