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)
26 * Don't assume ctx->md_data was cleaned in EVP_Digest_Final, because
27 * sometimes only copies of the context are ever finalised.
29 if (ctx->digest && ctx->digest->cleanup
30 && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_CLEANED))
31 ctx->digest->cleanup(ctx);
32 if (ctx->digest && ctx->digest->ctx_size && ctx->md_data
33 && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_REUSE)) {
34 OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
37 * pctx should be freed by the user of EVP_MD_CTX
38 * if EVP_MD_CTX_FLAG_KEEP_PKEY_CTX is set
40 if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX))
41 EVP_PKEY_CTX_free(ctx->pctx);
42 #ifndef OPENSSL_NO_ENGINE
43 ENGINE_finish(ctx->engine);
45 OPENSSL_cleanse(ctx, sizeof(*ctx));
50 EVP_MD_CTX *EVP_MD_CTX_new(void)
52 return OPENSSL_zalloc(sizeof(EVP_MD_CTX));
55 void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
57 EVP_MD_CTX_reset(ctx);
61 int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type)
63 EVP_MD_CTX_reset(ctx);
64 return EVP_DigestInit_ex(ctx, type, NULL);
67 int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
69 EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
70 #ifndef OPENSSL_NO_ENGINE
72 * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
73 * this context may already have an ENGINE! Try to avoid releasing the
74 * previous handle, re-querying for an ENGINE, and having a
75 * reinitialisation, when it may all be unnecessary.
77 if (ctx->engine && ctx->digest &&
78 (type == NULL || (type->type == ctx->digest->type)))
82 * Ensure an ENGINE left lying around from last time is cleared (the
83 * previous check attempted to avoid this if the same ENGINE and
84 * EVP_MD could be used).
86 ENGINE_finish(ctx->engine);
88 if (!ENGINE_init(impl)) {
89 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
93 /* Ask if an ENGINE is reserved for this job */
94 impl = ENGINE_get_digest_engine(type->type);
97 /* There's an ENGINE for this job ... (apparently) */
98 const EVP_MD *d = ENGINE_get_digest(impl, type->type);
101 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
105 /* We'll use the ENGINE's private digest definition */
108 * Store the ENGINE functional reference so we know 'type' came
109 * from an ENGINE and we need to release it when done.
116 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_NO_DIGEST_SET);
122 if (ctx->digest != type) {
123 if (ctx->digest && ctx->digest->ctx_size) {
124 OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
128 if (!(ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) && type->ctx_size) {
129 ctx->update = type->update;
130 ctx->md_data = OPENSSL_zalloc(type->ctx_size);
131 if (ctx->md_data == NULL) {
132 EVPerr(EVP_F_EVP_DIGESTINIT_EX, ERR_R_MALLOC_FAILURE);
137 #ifndef OPENSSL_NO_ENGINE
142 r = EVP_PKEY_CTX_ctrl(ctx->pctx, -1, EVP_PKEY_OP_TYPE_SIG,
143 EVP_PKEY_CTRL_DIGESTINIT, 0, ctx);
144 if (r <= 0 && (r != -2))
147 if (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT)
149 return ctx->digest->init(ctx);
152 int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t count)
154 return ctx->update(ctx, data, count);
157 /* The caller can assume that this removes any secret data from the context */
158 int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size)
161 ret = EVP_DigestFinal_ex(ctx, md, size);
162 EVP_MD_CTX_reset(ctx);
166 /* The caller can assume that this removes any secret data from the context */
167 int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size)
171 OPENSSL_assert(ctx->digest->md_size <= EVP_MAX_MD_SIZE);
172 ret = ctx->digest->final(ctx, md);
174 *size = ctx->digest->md_size;
175 if (ctx->digest->cleanup) {
176 ctx->digest->cleanup(ctx);
177 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
179 OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
183 int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t size)
187 if (ctx->digest->flags & EVP_MD_FLAG_XOF
189 && ctx->digest->md_ctrl(ctx, EVP_MD_CTRL_XOF_LEN, (int)size, NULL)) {
190 ret = ctx->digest->final(ctx, md);
192 if (ctx->digest->cleanup != NULL) {
193 ctx->digest->cleanup(ctx);
194 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
196 OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
198 EVPerr(EVP_F_EVP_DIGESTFINALXOF, EVP_R_NOT_XOF_OR_INVALID_LENGTH);
204 int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in)
206 EVP_MD_CTX_reset(out);
207 return EVP_MD_CTX_copy_ex(out, in);
210 int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in)
212 unsigned char *tmp_buf;
213 if ((in == NULL) || (in->digest == NULL)) {
214 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_INPUT_NOT_INITIALIZED);
217 #ifndef OPENSSL_NO_ENGINE
218 /* Make sure it's safe to copy a digest context using an ENGINE */
219 if (in->engine && !ENGINE_init(in->engine)) {
220 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_ENGINE_LIB);
225 if (out->digest == in->digest) {
226 tmp_buf = out->md_data;
227 EVP_MD_CTX_set_flags(out, EVP_MD_CTX_FLAG_REUSE);
230 EVP_MD_CTX_reset(out);
231 memcpy(out, in, sizeof(*out));
233 /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
234 EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
236 /* Null these variables, since they are getting fixed up
237 * properly below. Anything else may cause a memleak and/or
238 * double free if any of the memory allocations below fail
243 if (in->md_data && out->digest->ctx_size) {
245 out->md_data = tmp_buf;
247 out->md_data = OPENSSL_malloc(out->digest->ctx_size);
248 if (out->md_data == NULL) {
249 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_MALLOC_FAILURE);
253 memcpy(out->md_data, in->md_data, out->digest->ctx_size);
256 out->update = in->update;
259 out->pctx = EVP_PKEY_CTX_dup(in->pctx);
261 EVP_MD_CTX_reset(out);
266 if (out->digest->copy)
267 return out->digest->copy(out, in);
272 int EVP_Digest(const void *data, size_t count,
273 unsigned char *md, unsigned int *size, const EVP_MD *type,
276 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
281 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT);
282 ret = EVP_DigestInit_ex(ctx, type, impl)
283 && EVP_DigestUpdate(ctx, data, count)
284 && EVP_DigestFinal_ex(ctx, md, size);
285 EVP_MD_CTX_free(ctx);
290 int EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void *p2)
292 if (ctx->digest && ctx->digest->md_ctrl) {
293 int ret = ctx->digest->md_ctrl(ctx, cmd, p1, p2);
301 static void *evp_md_from_dispatch(int mdtype, const OSSL_DISPATCH *fns,
306 if ((md = EVP_MD_meth_new(mdtype, NID_undef)) == NULL)
309 for (; fns->function_id != 0; fns++) {
312 switch (fns->function_id) {
313 case OSSL_FUNC_DIGEST_NEWCTX:
314 if (md->newctx != NULL)
316 md->newctx = OSSL_get_OP_digest_newctx(fns);
319 case OSSL_FUNC_DIGEST_INIT:
320 if (md->dinit != NULL)
322 md->dinit = OSSL_get_OP_digest_init(fns);
325 case OSSL_FUNC_DIGEST_UPDDATE:
326 if (md->dupdate != NULL)
328 md->dupdate = OSSL_get_OP_digest_update(fns);
331 case OSSL_FUNC_DIGEST_FINAL:
332 if (md->dfinal != NULL)
334 md->dfinal = OSSL_get_OP_digest_final(fns);
337 case OSSL_FUNC_DIGEST_DIGEST:
338 if (md->digest != NULL)
340 md->digest = OSSL_get_OP_digest_digest(fns);
341 /* We don't increment fnct for this as it is stand alone */
343 case OSSL_FUNC_DIGEST_CLEANCTX:
344 if (md->cleanctx != NULL)
346 md->cleanctx = OSSL_get_OP_digest_cleanctx(fns);
349 case OSSL_FUNC_DIGEST_FREECTX:
350 if (md->freectx != NULL)
352 md->freectx = OSSL_get_OP_digest_freectx(fns);
356 if ((fncnt != 0 && fncnt != 6) || (fncnt == 0 && md->digest == NULL)) {
358 * In order to be a consistent set of functions we either need the
359 * whole set of init/update/final etc functions or none of them.
360 * The "digest" function can standalone. We at least need one way to
363 EVP_MD_meth_free(md);
369 ossl_provider_upref(prov);
374 static int evp_md_upref(void *md)
376 return EVP_MD_upref(md);
379 static void evp_md_free(void *md)
381 EVP_MD_meth_free(md);
384 EVP_MD *EVP_MD_fetch(OPENSSL_CTX *ctx, const char *algorithm,
385 const char *properties)
387 return evp_generic_fetch(ctx, OSSL_OP_DIGEST, algorithm, properties,
388 evp_md_from_dispatch, evp_md_upref,