summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/evp/digest.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/evp/digest.c')
-rw-r--r--src/lib/libcrypto/evp/digest.c17
1 files changed, 15 insertions, 2 deletions
diff --git a/src/lib/libcrypto/evp/digest.c b/src/lib/libcrypto/evp/digest.c
index a969ac69ed..b22eed4421 100644
--- a/src/lib/libcrypto/evp/digest.c
+++ b/src/lib/libcrypto/evp/digest.c
@@ -113,7 +113,9 @@
113#include "cryptlib.h" 113#include "cryptlib.h"
114#include <openssl/objects.h> 114#include <openssl/objects.h>
115#include <openssl/evp.h> 115#include <openssl/evp.h>
116#ifndef OPENSSL_NO_ENGINE
116#include <openssl/engine.h> 117#include <openssl/engine.h>
118#endif
117 119
118void EVP_MD_CTX_init(EVP_MD_CTX *ctx) 120void EVP_MD_CTX_init(EVP_MD_CTX *ctx)
119 { 121 {
@@ -138,6 +140,7 @@ int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type)
138int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl) 140int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
139 { 141 {
140 EVP_MD_CTX_clear_flags(ctx,EVP_MD_CTX_FLAG_CLEANED); 142 EVP_MD_CTX_clear_flags(ctx,EVP_MD_CTX_FLAG_CLEANED);
143#ifndef OPENSSL_NO_ENGINE
141 /* Whether it's nice or not, "Inits" can be used on "Final"'d contexts 144 /* Whether it's nice or not, "Inits" can be used on "Final"'d contexts
142 * so this context may already have an ENGINE! Try to avoid releasing 145 * so this context may already have an ENGINE! Try to avoid releasing
143 * the previous handle, re-querying for an ENGINE, and having a 146 * the previous handle, re-querying for an ENGINE, and having a
@@ -183,11 +186,13 @@ int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
183 else 186 else
184 ctx->engine = NULL; 187 ctx->engine = NULL;
185 } 188 }
186 else if(!ctx->digest) 189 else
190 if(!ctx->digest)
187 { 191 {
188 EVPerr(EVP_F_EVP_DIGESTINIT, EVP_R_NO_DIGEST_SET); 192 EVPerr(EVP_F_EVP_DIGESTINIT, EVP_R_NO_DIGEST_SET);
189 return 0; 193 return 0;
190 } 194 }
195#endif
191 if (ctx->digest != type) 196 if (ctx->digest != type)
192 { 197 {
193 if (ctx->digest && ctx->digest->ctx_size) 198 if (ctx->digest && ctx->digest->ctx_size)
@@ -196,7 +201,9 @@ int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
196 if (type->ctx_size) 201 if (type->ctx_size)
197 ctx->md_data=OPENSSL_malloc(type->ctx_size); 202 ctx->md_data=OPENSSL_malloc(type->ctx_size);
198 } 203 }
204#ifndef OPENSSL_NO_ENGINE
199skip_to_init: 205skip_to_init:
206#endif
200 return ctx->digest->init(ctx); 207 return ctx->digest->init(ctx);
201 } 208 }
202 209
@@ -219,6 +226,8 @@ int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size)
219int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size) 226int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size)
220 { 227 {
221 int ret; 228 int ret;
229
230 OPENSSL_assert(ctx->digest->md_size <= EVP_MAX_MD_SIZE);
222 ret=ctx->digest->final(ctx,md); 231 ret=ctx->digest->final(ctx,md);
223 if (size != NULL) 232 if (size != NULL)
224 *size=ctx->digest->md_size; 233 *size=ctx->digest->md_size;
@@ -244,12 +253,14 @@ int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in)
244 EVPerr(EVP_F_EVP_MD_CTX_COPY,EVP_R_INPUT_NOT_INITIALIZED); 253 EVPerr(EVP_F_EVP_MD_CTX_COPY,EVP_R_INPUT_NOT_INITIALIZED);
245 return 0; 254 return 0;
246 } 255 }
256#ifndef OPENSSL_NO_ENGINE
247 /* Make sure it's safe to copy a digest context using an ENGINE */ 257 /* Make sure it's safe to copy a digest context using an ENGINE */
248 if (in->engine && !ENGINE_init(in->engine)) 258 if (in->engine && !ENGINE_init(in->engine))
249 { 259 {
250 EVPerr(EVP_F_EVP_MD_CTX_COPY,ERR_R_ENGINE_LIB); 260 EVPerr(EVP_F_EVP_MD_CTX_COPY,ERR_R_ENGINE_LIB);
251 return 0; 261 return 0;
252 } 262 }
263#endif
253 264
254 EVP_MD_CTX_cleanup(out); 265 EVP_MD_CTX_cleanup(out);
255 memcpy(out,in,sizeof *out); 266 memcpy(out,in,sizeof *out);
@@ -299,13 +310,15 @@ int EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx)
299 ctx->digest->cleanup(ctx); 310 ctx->digest->cleanup(ctx);
300 if (ctx->digest && ctx->digest->ctx_size && ctx->md_data) 311 if (ctx->digest && ctx->digest->ctx_size && ctx->md_data)
301 { 312 {
302 memset(ctx->md_data,0,ctx->digest->ctx_size); 313 OPENSSL_cleanse(ctx->md_data,ctx->digest->ctx_size);
303 OPENSSL_free(ctx->md_data); 314 OPENSSL_free(ctx->md_data);
304 } 315 }
316#ifndef OPENSSL_NO_ENGINE
305 if(ctx->engine) 317 if(ctx->engine)
306 /* The EVP_MD we used belongs to an ENGINE, release the 318 /* The EVP_MD we used belongs to an ENGINE, release the
307 * functional reference we held for this reason. */ 319 * functional reference we held for this reason. */
308 ENGINE_finish(ctx->engine); 320 ENGINE_finish(ctx->engine);
321#endif
309 memset(ctx,'\0',sizeof *ctx); 322 memset(ctx,'\0',sizeof *ctx);
310 323
311 return 1; 324 return 1;