2 * Copyright 1995-2016 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
);
35 EVP_PKEY_CTX_free(ctx
->pctx
);
36 #ifndef OPENSSL_NO_ENGINE
37 ENGINE_finish(ctx
->engine
);
39 memset(ctx
, 0, sizeof(*ctx
));
44 EVP_MD_CTX
*EVP_MD_CTX_new(void)
46 return OPENSSL_zalloc(sizeof(EVP_MD_CTX
));
49 void EVP_MD_CTX_free(EVP_MD_CTX
*ctx
)
51 EVP_MD_CTX_reset(ctx
);
55 int EVP_DigestInit(EVP_MD_CTX
*ctx
, const EVP_MD
*type
)
57 EVP_MD_CTX_reset(ctx
);
58 return EVP_DigestInit_ex(ctx
, type
, NULL
);
61 int EVP_DigestInit_ex(EVP_MD_CTX
*ctx
, const EVP_MD
*type
, ENGINE
*impl
)
63 EVP_MD_CTX_clear_flags(ctx
, EVP_MD_CTX_FLAG_CLEANED
);
64 #ifndef OPENSSL_NO_ENGINE
66 * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
67 * this context may already have an ENGINE! Try to avoid releasing the
68 * previous handle, re-querying for an ENGINE, and having a
69 * reinitialisation, when it may all be unnecessary.
71 if (ctx
->engine
&& ctx
->digest
&& (!type
||
78 * Ensure an ENGINE left lying around from last time is cleared (the
79 * previous check attempted to avoid this if the same ENGINE and
80 * EVP_MD could be used).
82 ENGINE_finish(ctx
->engine
);
84 if (!ENGINE_init(impl
)) {
85 EVPerr(EVP_F_EVP_DIGESTINIT_EX
, EVP_R_INITIALIZATION_ERROR
);
89 /* Ask if an ENGINE is reserved for this job */
90 impl
= ENGINE_get_digest_engine(type
->type
);
93 /* There's an ENGINE for this job ... (apparently) */
94 const EVP_MD
*d
= ENGINE_get_digest(impl
, type
->type
);
97 EVPerr(EVP_F_EVP_DIGESTINIT_EX
, EVP_R_INITIALIZATION_ERROR
);
101 /* We'll use the ENGINE's private digest definition */
104 * Store the ENGINE functional reference so we know 'type' came
105 * from an ENGINE and we need to release it when done.
112 EVPerr(EVP_F_EVP_DIGESTINIT_EX
, EVP_R_NO_DIGEST_SET
);
118 if (ctx
->digest
!= type
) {
119 if (ctx
->digest
&& ctx
->digest
->ctx_size
) {
120 OPENSSL_free(ctx
->md_data
);
124 if (!(ctx
->flags
& EVP_MD_CTX_FLAG_NO_INIT
) && type
->ctx_size
) {
125 ctx
->update
= type
->update
;
126 ctx
->md_data
= OPENSSL_zalloc(type
->ctx_size
);
127 if (ctx
->md_data
== NULL
) {
128 EVPerr(EVP_F_EVP_DIGESTINIT_EX
, ERR_R_MALLOC_FAILURE
);
133 #ifndef OPENSSL_NO_ENGINE
138 r
= EVP_PKEY_CTX_ctrl(ctx
->pctx
, -1, EVP_PKEY_OP_TYPE_SIG
,
139 EVP_PKEY_CTRL_DIGESTINIT
, 0, ctx
);
140 if (r
<= 0 && (r
!= -2))
143 if (ctx
->flags
& EVP_MD_CTX_FLAG_NO_INIT
)
145 return ctx
->digest
->init(ctx
);
148 int EVP_DigestUpdate(EVP_MD_CTX
*ctx
, const void *data
, size_t count
)
150 return ctx
->update(ctx
, data
, count
);
153 /* The caller can assume that this removes any secret data from the context */
154 int EVP_DigestFinal(EVP_MD_CTX
*ctx
, unsigned char *md
, unsigned int *size
)
157 ret
= EVP_DigestFinal_ex(ctx
, md
, size
);
158 EVP_MD_CTX_reset(ctx
);
162 /* The caller can assume that this removes any secret data from the context */
163 int EVP_DigestFinal_ex(EVP_MD_CTX
*ctx
, unsigned char *md
, unsigned int *size
)
167 OPENSSL_assert(ctx
->digest
->md_size
<= EVP_MAX_MD_SIZE
);
168 ret
= ctx
->digest
->final(ctx
, md
);
170 *size
= ctx
->digest
->md_size
;
171 if (ctx
->digest
->cleanup
) {
172 ctx
->digest
->cleanup(ctx
);
173 EVP_MD_CTX_set_flags(ctx
, EVP_MD_CTX_FLAG_CLEANED
);
175 memset(ctx
->md_data
, 0, ctx
->digest
->ctx_size
);
179 int EVP_MD_CTX_copy(EVP_MD_CTX
*out
, const EVP_MD_CTX
*in
)
181 EVP_MD_CTX_reset(out
);
182 return EVP_MD_CTX_copy_ex(out
, in
);
185 int EVP_MD_CTX_copy_ex(EVP_MD_CTX
*out
, const EVP_MD_CTX
*in
)
187 unsigned char *tmp_buf
;
188 if ((in
== NULL
) || (in
->digest
== NULL
)) {
189 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX
, EVP_R_INPUT_NOT_INITIALIZED
);
192 #ifndef OPENSSL_NO_ENGINE
193 /* Make sure it's safe to copy a digest context using an ENGINE */
194 if (in
->engine
&& !ENGINE_init(in
->engine
)) {
195 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX
, ERR_R_ENGINE_LIB
);
200 if (out
->digest
== in
->digest
) {
201 tmp_buf
= out
->md_data
;
202 EVP_MD_CTX_set_flags(out
, EVP_MD_CTX_FLAG_REUSE
);
205 EVP_MD_CTX_reset(out
);
206 memcpy(out
, in
, sizeof(*out
));
208 /* Null these variables, since they are getting fixed up
209 * properly below. Anything else may cause a memleak and/or
210 * double free if any of the memory allocations below fail
215 if (in
->md_data
&& out
->digest
->ctx_size
) {
217 out
->md_data
= tmp_buf
;
219 out
->md_data
= OPENSSL_malloc(out
->digest
->ctx_size
);
220 if (out
->md_data
== NULL
) {
221 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX
, ERR_R_MALLOC_FAILURE
);
225 memcpy(out
->md_data
, in
->md_data
, out
->digest
->ctx_size
);
228 out
->update
= in
->update
;
231 out
->pctx
= EVP_PKEY_CTX_dup(in
->pctx
);
233 EVP_MD_CTX_reset(out
);
238 if (out
->digest
->copy
)
239 return out
->digest
->copy(out
, in
);
244 int EVP_Digest(const void *data
, size_t count
,
245 unsigned char *md
, unsigned int *size
, const EVP_MD
*type
,
248 EVP_MD_CTX
*ctx
= EVP_MD_CTX_new();
253 EVP_MD_CTX_set_flags(ctx
, EVP_MD_CTX_FLAG_ONESHOT
);
254 ret
= EVP_DigestInit_ex(ctx
, type
, impl
)
255 && EVP_DigestUpdate(ctx
, data
, count
)
256 && EVP_DigestFinal_ex(ctx
, md
, size
);
257 EVP_MD_CTX_free(ctx
);
262 int EVP_MD_CTX_ctrl(EVP_MD_CTX
*ctx
, int cmd
, int p1
, void *p2
)
264 if (ctx
->digest
&& ctx
->digest
->md_ctrl
) {
265 int ret
= ctx
->digest
->md_ctrl(ctx
, cmd
, p1
, p2
);