2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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"
18 /* This call frees resources associated with the context */
19 int EVP_MD_CTX_reset(EVP_MD_CTX *ctx)
25 * Don't assume ctx->md_data was cleaned in EVP_Digest_Final, because
26 * sometimes only copies of the context are ever finalised.
28 if (ctx->digest && ctx->digest->cleanup
29 && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_CLEANED))
30 ctx->digest->cleanup(ctx);
31 if (ctx->digest && ctx->digest->ctx_size && ctx->md_data
32 && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_REUSE)) {
33 OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
36 * pctx should be freed by the user of EVP_MD_CTX
37 * if EVP_MD_CTX_FLAG_KEEP_PKEY_CTX is set
39 if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX))
40 EVP_PKEY_CTX_free(ctx->pctx);
41 #ifndef OPENSSL_NO_ENGINE
42 ENGINE_finish(ctx->engine);
44 OPENSSL_cleanse(ctx, sizeof(*ctx));
49 EVP_MD_CTX *EVP_MD_CTX_new(void)
51 return OPENSSL_zalloc(sizeof(EVP_MD_CTX));
54 void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
56 EVP_MD_CTX_reset(ctx);
60 int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type)
62 EVP_MD_CTX_reset(ctx);
63 return EVP_DigestInit_ex(ctx, type, NULL);
66 int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
68 EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
69 #ifndef OPENSSL_NO_ENGINE
71 * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
72 * this context may already have an ENGINE! Try to avoid releasing the
73 * previous handle, re-querying for an ENGINE, and having a
74 * reinitialisation, when it may all be unnecessary.
76 if (ctx->engine && ctx->digest &&
77 (type == NULL || (type->type == ctx->digest->type)))
81 * Ensure an ENGINE left lying around from last time is cleared (the
82 * previous check attempted to avoid this if the same ENGINE and
83 * EVP_MD could be used).
85 ENGINE_finish(ctx->engine);
87 if (!ENGINE_init(impl)) {
88 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
92 /* Ask if an ENGINE is reserved for this job */
93 impl = ENGINE_get_digest_engine(type->type);
96 /* There's an ENGINE for this job ... (apparently) */
97 const EVP_MD *d = ENGINE_get_digest(impl, type->type);
100 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
104 /* We'll use the ENGINE's private digest definition */
107 * Store the ENGINE functional reference so we know 'type' came
108 * from an ENGINE and we need to release it when done.
115 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_NO_DIGEST_SET);
121 if (ctx->digest != type) {
122 if (ctx->digest && ctx->digest->ctx_size) {
123 OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
127 if (!(ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) && type->ctx_size) {
128 ctx->update = type->update;
129 ctx->md_data = OPENSSL_zalloc(type->ctx_size);
130 if (ctx->md_data == NULL) {
131 EVPerr(EVP_F_EVP_DIGESTINIT_EX, ERR_R_MALLOC_FAILURE);
136 #ifndef OPENSSL_NO_ENGINE
141 r = EVP_PKEY_CTX_ctrl(ctx->pctx, -1, EVP_PKEY_OP_TYPE_SIG,
142 EVP_PKEY_CTRL_DIGESTINIT, 0, ctx);
143 if (r <= 0 && (r != -2))
146 if (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT)
148 return ctx->digest->init(ctx);
151 int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t count)
153 return ctx->update(ctx, data, count);
156 /* The caller can assume that this removes any secret data from the context */
157 int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size)
160 ret = EVP_DigestFinal_ex(ctx, md, size);
161 EVP_MD_CTX_reset(ctx);
165 /* The caller can assume that this removes any secret data from the context */
166 int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size)
170 OPENSSL_assert(ctx->digest->md_size <= EVP_MAX_MD_SIZE);
171 ret = ctx->digest->final(ctx, md);
173 *size = ctx->digest->md_size;
174 if (ctx->digest->cleanup) {
175 ctx->digest->cleanup(ctx);
176 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
178 OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
182 int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t size)
186 if (ctx->digest->flags & EVP_MD_FLAG_XOF
188 && ctx->digest->md_ctrl(ctx, EVP_MD_CTRL_XOF_LEN, (int)size, NULL)) {
189 ret = ctx->digest->final(ctx, md);
191 if (ctx->digest->cleanup != NULL) {
192 ctx->digest->cleanup(ctx);
193 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
195 OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
197 EVPerr(EVP_F_EVP_DIGESTFINALXOF, EVP_R_NOT_XOF_OR_INVALID_LENGTH);
203 int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in)
205 EVP_MD_CTX_reset(out);
206 return EVP_MD_CTX_copy_ex(out, in);
209 int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in)
211 unsigned char *tmp_buf;
212 if ((in == NULL) || (in->digest == NULL)) {
213 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_INPUT_NOT_INITIALIZED);
216 #ifndef OPENSSL_NO_ENGINE
217 /* Make sure it's safe to copy a digest context using an ENGINE */
218 if (in->engine && !ENGINE_init(in->engine)) {
219 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_ENGINE_LIB);
224 if (out->digest == in->digest) {
225 tmp_buf = out->md_data;
226 EVP_MD_CTX_set_flags(out, EVP_MD_CTX_FLAG_REUSE);
229 EVP_MD_CTX_reset(out);
230 memcpy(out, in, sizeof(*out));
232 /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
233 EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
235 /* Null these variables, since they are getting fixed up
236 * properly below. Anything else may cause a memleak and/or
237 * double free if any of the memory allocations below fail
242 if (in->md_data && out->digest->ctx_size) {
244 out->md_data = tmp_buf;
246 out->md_data = OPENSSL_malloc(out->digest->ctx_size);
247 if (out->md_data == NULL) {
248 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_MALLOC_FAILURE);
252 memcpy(out->md_data, in->md_data, out->digest->ctx_size);
255 out->update = in->update;
258 out->pctx = EVP_PKEY_CTX_dup(in->pctx);
260 EVP_MD_CTX_reset(out);
265 if (out->digest->copy)
266 return out->digest->copy(out, in);
271 int EVP_Digest(const void *data, size_t count,
272 unsigned char *md, unsigned int *size, const EVP_MD *type,
275 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
280 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT);
281 ret = EVP_DigestInit_ex(ctx, type, impl)
282 && EVP_DigestUpdate(ctx, data, count)
283 && EVP_DigestFinal_ex(ctx, md, size);
284 EVP_MD_CTX_free(ctx);
289 int EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void *p2)
291 if (ctx->digest && ctx->digest->md_ctrl) {
292 int ret = ctx->digest->md_ctrl(ctx, cmd, p1, p2);