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;
86 ctx->reqdigest = NULL;
91 /* TODO(3.0): Remove legacy code below */
93 EVP_MD_CTX_reset(ctx);
97 int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type)
99 EVP_MD_CTX_reset(ctx);
100 return EVP_DigestInit_ex(ctx, type, NULL);
103 int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
106 ENGINE *tmpimpl = NULL;
108 EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
111 ctx->reqdigest = type;
113 /* TODO(3.0): Legacy work around code below. Remove this */
114 #ifndef OPENSSL_NO_ENGINE
116 * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
117 * this context may already have an ENGINE! Try to avoid releasing the
118 * previous handle, re-querying for an ENGINE, and having a
119 * reinitialisation, when it may all be unnecessary.
121 if (ctx->engine && ctx->digest &&
122 (type == NULL || (type->type == ctx->digest->type)))
125 if (type != NULL && impl == NULL)
126 tmpimpl = ENGINE_get_digest_engine(type->type);
130 * If there are engines involved or if we're being used as part of
131 * EVP_DigestSignInit then we should use legacy handling for now.
133 if (ctx->engine != NULL
137 || (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0) {
138 if (ctx->digest == ctx->fetched_digest)
140 EVP_MD_meth_free(ctx->fetched_digest);
141 ctx->fetched_digest = NULL;
145 if (type->prov == 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) {
162 provmd = EVP_MD_fetch(NULL, OBJ_nid2sn(type->type), "");
163 if (provmd == NULL) {
164 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
168 EVP_MD_meth_free(ctx->fetched_digest);
169 ctx->fetched_digest = provmd;
173 if (ctx->provctx == NULL) {
174 ctx->provctx = ctx->digest->newctx();
175 if (ctx->provctx == NULL) {
176 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
181 if (ctx->digest->dinit == NULL) {
182 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
186 return ctx->digest->dinit(ctx->provctx);
188 /* TODO(3.0): Remove legacy code below */
191 #ifndef OPENSSL_NO_ENGINE
194 * Ensure an ENGINE left lying around from last time is cleared (the
195 * previous check attempted to avoid this if the same ENGINE and
196 * EVP_MD could be used).
198 ENGINE_finish(ctx->engine);
200 if (!ENGINE_init(impl)) {
201 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
205 /* Ask if an ENGINE is reserved for this job */
209 /* There's an ENGINE for this job ... (apparently) */
210 const EVP_MD *d = ENGINE_get_digest(impl, type->type);
213 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
217 /* We'll use the ENGINE's private digest definition */
220 * Store the ENGINE functional reference so we know 'type' came
221 * from an ENGINE and we need to release it when done.
228 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_NO_DIGEST_SET);
234 if (ctx->digest != type) {
235 if (ctx->digest && ctx->digest->ctx_size) {
236 OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
240 if (!(ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) && type->ctx_size) {
241 ctx->update = type->update;
242 ctx->md_data = OPENSSL_zalloc(type->ctx_size);
243 if (ctx->md_data == NULL) {
244 EVPerr(EVP_F_EVP_DIGESTINIT_EX, ERR_R_MALLOC_FAILURE);
249 #ifndef OPENSSL_NO_ENGINE
254 r = EVP_PKEY_CTX_ctrl(ctx->pctx, -1, EVP_PKEY_OP_TYPE_SIG,
255 EVP_PKEY_CTRL_DIGESTINIT, 0, ctx);
256 if (r <= 0 && (r != -2))
259 if (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT)
261 return ctx->digest->init(ctx);
264 int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t count)
269 if (ctx->digest == NULL || ctx->digest->prov == NULL)
272 if (ctx->digest->dupdate == NULL) {
273 EVPerr(EVP_F_EVP_DIGESTUPDATE, EVP_R_UPDATE_ERROR);
276 return ctx->digest->dupdate(ctx->provctx, data, count);
278 /* TODO(3.0): Remove legacy code below */
280 return ctx->update(ctx, data, count);
283 /* The caller can assume that this removes any secret data from the context */
284 int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size)
287 ret = EVP_DigestFinal_ex(ctx, md, size);
288 EVP_MD_CTX_reset(ctx);
292 /* The caller can assume that this removes any secret data from the context */
293 int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *isize)
298 if (ctx->digest == NULL || ctx->digest->prov == NULL)
301 if (ctx->digest->dfinal == NULL) {
302 EVPerr(EVP_F_EVP_DIGESTFINAL_EX, EVP_R_FINAL_ERROR);
306 ret = ctx->digest->dfinal(ctx->provctx, md, &size);
309 if (size <= UINT_MAX) {
312 EVPerr(EVP_F_EVP_DIGESTFINAL_EX, EVP_R_FINAL_ERROR);
317 EVP_MD_CTX_reset(ctx);
321 /* TODO(3.0): Remove legacy code below */
323 OPENSSL_assert(ctx->digest->md_size <= EVP_MAX_MD_SIZE);
324 ret = ctx->digest->final(ctx, md);
326 *isize = ctx->digest->md_size;
327 if (ctx->digest->cleanup) {
328 ctx->digest->cleanup(ctx);
329 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
331 OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
335 int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t size)
339 if (ctx->digest->flags & EVP_MD_FLAG_XOF
341 && ctx->digest->md_ctrl(ctx, EVP_MD_CTRL_XOF_LEN, (int)size, NULL)) {
342 ret = ctx->digest->final(ctx, md);
344 if (ctx->digest->cleanup != NULL) {
345 ctx->digest->cleanup(ctx);
346 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
348 OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
350 EVPerr(EVP_F_EVP_DIGESTFINALXOF, EVP_R_NOT_XOF_OR_INVALID_LENGTH);
356 int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in)
358 EVP_MD_CTX_reset(out);
359 return EVP_MD_CTX_copy_ex(out, in);
362 int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in)
364 unsigned char *tmp_buf;
366 if (in == NULL || in->digest == NULL) {
367 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_INPUT_NOT_INITIALIZED);
371 if (in->digest->prov == NULL)
374 if (in->digest->dupctx == NULL) {
375 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_NOT_ABLE_TO_COPY_CTX);
379 EVP_MD_CTX_reset(out);
380 if (out->fetched_digest != NULL)
381 EVP_MD_meth_free(out->fetched_digest);
383 /* NULL out pointers in case of error */
387 if (in->fetched_digest != NULL)
388 EVP_MD_upref(in->fetched_digest);
390 out->provctx = in->digest->dupctx(in->provctx);
391 if (out->provctx == NULL) {
392 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_NOT_ABLE_TO_COPY_CTX);
396 /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
397 EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
398 if (in->pctx != NULL) {
399 out->pctx = EVP_PKEY_CTX_dup(in->pctx);
400 if (out->pctx == NULL) {
401 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_NOT_ABLE_TO_COPY_CTX);
402 EVP_MD_CTX_reset(out);
409 /* TODO(3.0): Remove legacy code below */
411 #ifndef OPENSSL_NO_ENGINE
412 /* Make sure it's safe to copy a digest context using an ENGINE */
413 if (in->engine && !ENGINE_init(in->engine)) {
414 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_ENGINE_LIB);
419 if (out->digest == in->digest) {
420 tmp_buf = out->md_data;
421 EVP_MD_CTX_set_flags(out, EVP_MD_CTX_FLAG_REUSE);
424 EVP_MD_CTX_reset(out);
425 memcpy(out, in, sizeof(*out));
427 /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
428 EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
430 /* Null these variables, since they are getting fixed up
431 * properly below. Anything else may cause a memleak and/or
432 * double free if any of the memory allocations below fail
437 if (in->md_data && out->digest->ctx_size) {
439 out->md_data = tmp_buf;
441 out->md_data = OPENSSL_malloc(out->digest->ctx_size);
442 if (out->md_data == NULL) {
443 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_MALLOC_FAILURE);
447 memcpy(out->md_data, in->md_data, out->digest->ctx_size);
450 out->update = in->update;
453 out->pctx = EVP_PKEY_CTX_dup(in->pctx);
455 EVP_MD_CTX_reset(out);
460 if (out->digest->copy)
461 return out->digest->copy(out, in);
466 int EVP_Digest(const void *data, size_t count,
467 unsigned char *md, unsigned int *size, const EVP_MD *type,
470 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
475 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT);
476 ret = EVP_DigestInit_ex(ctx, type, impl)
477 && EVP_DigestUpdate(ctx, data, count)
478 && EVP_DigestFinal_ex(ctx, md, size);
479 EVP_MD_CTX_free(ctx);
484 int EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void *p2)
486 if (ctx->digest && ctx->digest->md_ctrl) {
487 int ret = ctx->digest->md_ctrl(ctx, cmd, p1, p2);
495 static void *evp_md_from_dispatch(int mdtype, const OSSL_DISPATCH *fns,
501 if ((md = EVP_MD_meth_new(mdtype, NID_undef)) == NULL)
504 for (; fns->function_id != 0; fns++) {
505 switch (fns->function_id) {
506 case OSSL_FUNC_DIGEST_NEWCTX:
507 if (md->newctx != NULL)
509 md->newctx = OSSL_get_OP_digest_newctx(fns);
512 case OSSL_FUNC_DIGEST_INIT:
513 if (md->dinit != NULL)
515 md->dinit = OSSL_get_OP_digest_init(fns);
518 case OSSL_FUNC_DIGEST_UPDDATE:
519 if (md->dupdate != NULL)
521 md->dupdate = OSSL_get_OP_digest_update(fns);
524 case OSSL_FUNC_DIGEST_FINAL:
525 if (md->dfinal != NULL)
527 md->dfinal = OSSL_get_OP_digest_final(fns);
530 case OSSL_FUNC_DIGEST_DIGEST:
531 if (md->digest != NULL)
533 md->digest = OSSL_get_OP_digest_digest(fns);
534 /* We don't increment fnct for this as it is stand alone */
536 case OSSL_FUNC_DIGEST_FREECTX:
537 if (md->freectx != NULL)
539 md->freectx = OSSL_get_OP_digest_freectx(fns);
542 case OSSL_FUNC_DIGEST_DUPCTX:
543 if (md->dupctx != NULL)
545 md->dupctx = OSSL_get_OP_digest_dupctx(fns);
547 case OSSL_FUNC_DIGEST_SIZE:
548 if (md->size != NULL)
550 md->size = OSSL_get_OP_digest_size(fns);
552 case OSSL_FUNC_DIGEST_BLOCK_SIZE:
553 if (md->dblock_size != NULL)
555 md->dblock_size = OSSL_get_OP_digest_block_size(fns);
559 if ((fncnt != 0 && fncnt != 5)
560 || (fncnt == 0 && md->digest == NULL)
561 || md->size == NULL) {
563 * In order to be a consistent set of functions we either need the
564 * whole set of init/update/final etc functions or none of them.
565 * The "digest" function can standalone. We at least need one way to
568 EVP_MD_meth_free(md);
573 ossl_provider_upref(prov);
578 static int evp_md_upref(void *md)
580 return EVP_MD_upref(md);
583 static void evp_md_free(void *md)
585 EVP_MD_meth_free(md);
588 EVP_MD *EVP_MD_fetch(OPENSSL_CTX *ctx, const char *algorithm,
589 const char *properties)
591 return evp_generic_fetch(ctx, OSSL_OP_DIGEST, algorithm, properties,
592 evp_md_from_dispatch, evp_md_upref,