diff options
Diffstat (limited to 'src/lib/libcrypto/evp/digest.c')
-rw-r--r-- | src/lib/libcrypto/evp/digest.c | 211 |
1 files changed, 74 insertions, 137 deletions
diff --git a/src/lib/libcrypto/evp/digest.c b/src/lib/libcrypto/evp/digest.c index 3bc2d1295c..982ba2b136 100644 --- a/src/lib/libcrypto/evp/digest.c +++ b/src/lib/libcrypto/evp/digest.c | |||
@@ -116,7 +116,6 @@ | |||
116 | #ifndef OPENSSL_NO_ENGINE | 116 | #ifndef OPENSSL_NO_ENGINE |
117 | #include <openssl/engine.h> | 117 | #include <openssl/engine.h> |
118 | #endif | 118 | #endif |
119 | #include "evp_locl.h" | ||
120 | 119 | ||
121 | void EVP_MD_CTX_init(EVP_MD_CTX *ctx) | 120 | void EVP_MD_CTX_init(EVP_MD_CTX *ctx) |
122 | { | 121 | { |
@@ -127,7 +126,8 @@ EVP_MD_CTX *EVP_MD_CTX_create(void) | |||
127 | { | 126 | { |
128 | EVP_MD_CTX *ctx=OPENSSL_malloc(sizeof *ctx); | 127 | EVP_MD_CTX *ctx=OPENSSL_malloc(sizeof *ctx); |
129 | 128 | ||
130 | EVP_MD_CTX_init(ctx); | 129 | if (ctx) |
130 | EVP_MD_CTX_init(ctx); | ||
131 | 131 | ||
132 | return ctx; | 132 | return ctx; |
133 | } | 133 | } |
@@ -138,77 +138,18 @@ int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type) | |||
138 | return EVP_DigestInit_ex(ctx, type, NULL); | 138 | return EVP_DigestInit_ex(ctx, type, NULL); |
139 | } | 139 | } |
140 | 140 | ||
141 | #ifdef OPENSSL_FIPS | 141 | int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl) |
142 | |||
143 | /* The purpose of these is to trap programs that attempt to use non FIPS | ||
144 | * algorithms in FIPS mode and ignore the errors. | ||
145 | */ | ||
146 | |||
147 | static int bad_init(EVP_MD_CTX *ctx) | ||
148 | { FIPS_ERROR_IGNORED("Digest init"); return 0;} | ||
149 | |||
150 | static int bad_update(EVP_MD_CTX *ctx,const void *data,size_t count) | ||
151 | { FIPS_ERROR_IGNORED("Digest update"); return 0;} | ||
152 | |||
153 | static int bad_final(EVP_MD_CTX *ctx,unsigned char *md) | ||
154 | { FIPS_ERROR_IGNORED("Digest Final"); return 0;} | ||
155 | |||
156 | static const EVP_MD bad_md = | ||
157 | { | 142 | { |
158 | 0, | 143 | EVP_MD_CTX_clear_flags(ctx,EVP_MD_CTX_FLAG_CLEANED); |
159 | 0, | ||
160 | 0, | ||
161 | 0, | ||
162 | bad_init, | ||
163 | bad_update, | ||
164 | bad_final, | ||
165 | NULL, | ||
166 | NULL, | ||
167 | NULL, | ||
168 | 0, | ||
169 | {0,0,0,0}, | ||
170 | }; | ||
171 | |||
172 | #endif | ||
173 | |||
174 | #ifndef OPENSSL_NO_ENGINE | 144 | #ifndef OPENSSL_NO_ENGINE |
175 | 145 | /* Whether it's nice or not, "Inits" can be used on "Final"'d contexts | |
176 | #ifdef OPENSSL_FIPS | 146 | * so this context may already have an ENGINE! Try to avoid releasing |
177 | 147 | * the previous handle, re-querying for an ENGINE, and having a | |
178 | static int do_engine_null(ENGINE *impl) { return 0;} | 148 | * reinitialisation, when it may all be unecessary. */ |
179 | static int do_evp_md_engine_null(EVP_MD_CTX *ctx, | 149 | if (ctx->engine && ctx->digest && (!type || |
180 | const EVP_MD **ptype, ENGINE *impl) | 150 | (type && (type->type == ctx->digest->type)))) |
181 | { return 1; } | 151 | goto skip_to_init; |
182 | 152 | if (type) | |
183 | static int (*do_engine_init)(ENGINE *impl) | ||
184 | = do_engine_null; | ||
185 | |||
186 | static int (*do_engine_finish)(ENGINE *impl) | ||
187 | = do_engine_null; | ||
188 | |||
189 | static int (*do_evp_md_engine) | ||
190 | (EVP_MD_CTX *ctx, const EVP_MD **ptype, ENGINE *impl) | ||
191 | = do_evp_md_engine_null; | ||
192 | |||
193 | void int_EVP_MD_set_engine_callbacks( | ||
194 | int (*eng_md_init)(ENGINE *impl), | ||
195 | int (*eng_md_fin)(ENGINE *impl), | ||
196 | int (*eng_md_evp) | ||
197 | (EVP_MD_CTX *ctx, const EVP_MD **ptype, ENGINE *impl)) | ||
198 | { | ||
199 | do_engine_init = eng_md_init; | ||
200 | do_engine_finish = eng_md_fin; | ||
201 | do_evp_md_engine = eng_md_evp; | ||
202 | } | ||
203 | |||
204 | #else | ||
205 | |||
206 | #define do_engine_init ENGINE_init | ||
207 | #define do_engine_finish ENGINE_finish | ||
208 | |||
209 | static int do_evp_md_engine(EVP_MD_CTX *ctx, const EVP_MD **ptype, ENGINE *impl) | ||
210 | { | ||
211 | if (*ptype) | ||
212 | { | 153 | { |
213 | /* Ensure an ENGINE left lying around from last time is cleared | 154 | /* Ensure an ENGINE left lying around from last time is cleared |
214 | * (the previous check attempted to avoid this if the same | 155 | * (the previous check attempted to avoid this if the same |
@@ -219,25 +160,26 @@ static int do_evp_md_engine(EVP_MD_CTX *ctx, const EVP_MD **ptype, ENGINE *impl) | |||
219 | { | 160 | { |
220 | if (!ENGINE_init(impl)) | 161 | if (!ENGINE_init(impl)) |
221 | { | 162 | { |
222 | EVPerr(EVP_F_DO_EVP_MD_ENGINE,EVP_R_INITIALIZATION_ERROR); | 163 | EVPerr(EVP_F_EVP_DIGESTINIT_EX,EVP_R_INITIALIZATION_ERROR); |
223 | return 0; | 164 | return 0; |
224 | } | 165 | } |
225 | } | 166 | } |
226 | else | 167 | else |
227 | /* Ask if an ENGINE is reserved for this job */ | 168 | /* Ask if an ENGINE is reserved for this job */ |
228 | impl = ENGINE_get_digest_engine((*ptype)->type); | 169 | impl = ENGINE_get_digest_engine(type->type); |
229 | if(impl) | 170 | if(impl) |
230 | { | 171 | { |
231 | /* There's an ENGINE for this job ... (apparently) */ | 172 | /* There's an ENGINE for this job ... (apparently) */ |
232 | const EVP_MD *d = ENGINE_get_digest(impl, (*ptype)->type); | 173 | const EVP_MD *d = ENGINE_get_digest(impl, type->type); |
233 | if(!d) | 174 | if(!d) |
234 | { | 175 | { |
235 | /* Same comment from evp_enc.c */ | 176 | /* Same comment from evp_enc.c */ |
236 | EVPerr(EVP_F_DO_EVP_MD_ENGINE,EVP_R_INITIALIZATION_ERROR); | 177 | EVPerr(EVP_F_EVP_DIGESTINIT_EX,EVP_R_INITIALIZATION_ERROR); |
178 | ENGINE_finish(impl); | ||
237 | return 0; | 179 | return 0; |
238 | } | 180 | } |
239 | /* We'll use the ENGINE's private digest definition */ | 181 | /* We'll use the ENGINE's private digest definition */ |
240 | *ptype = d; | 182 | type = d; |
241 | /* Store the ENGINE functional reference so we know | 183 | /* Store the ENGINE functional reference so we know |
242 | * 'type' came from an ENGINE and we need to release | 184 | * 'type' came from an ENGINE and we need to release |
243 | * it when done. */ | 185 | * it when done. */ |
@@ -249,71 +191,46 @@ static int do_evp_md_engine(EVP_MD_CTX *ctx, const EVP_MD **ptype, ENGINE *impl) | |||
249 | else | 191 | else |
250 | if(!ctx->digest) | 192 | if(!ctx->digest) |
251 | { | 193 | { |
252 | EVPerr(EVP_F_DO_EVP_MD_ENGINE,EVP_R_NO_DIGEST_SET); | 194 | EVPerr(EVP_F_EVP_DIGESTINIT_EX,EVP_R_NO_DIGEST_SET); |
253 | return 0; | 195 | return 0; |
254 | } | 196 | } |
255 | return 1; | ||
256 | } | ||
257 | |||
258 | #endif | ||
259 | |||
260 | #endif | ||
261 | |||
262 | int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl) | ||
263 | { | ||
264 | M_EVP_MD_CTX_clear_flags(ctx,EVP_MD_CTX_FLAG_CLEANED); | ||
265 | #ifdef OPENSSL_FIPS | ||
266 | if(FIPS_selftest_failed()) | ||
267 | { | ||
268 | FIPSerr(FIPS_F_EVP_DIGESTINIT_EX,FIPS_R_FIPS_SELFTEST_FAILED); | ||
269 | ctx->digest = &bad_md; | ||
270 | return 0; | ||
271 | } | ||
272 | #endif | ||
273 | #ifndef OPENSSL_NO_ENGINE | ||
274 | /* Whether it's nice or not, "Inits" can be used on "Final"'d contexts | ||
275 | * so this context may already have an ENGINE! Try to avoid releasing | ||
276 | * the previous handle, re-querying for an ENGINE, and having a | ||
277 | * reinitialisation, when it may all be unecessary. */ | ||
278 | if (ctx->engine && ctx->digest && (!type || | ||
279 | (type && (type->type == ctx->digest->type)))) | ||
280 | goto skip_to_init; | ||
281 | if (!do_evp_md_engine(ctx, &type, impl)) | ||
282 | return 0; | ||
283 | #endif | 197 | #endif |
284 | if (ctx->digest != type) | 198 | if (ctx->digest != type) |
285 | { | 199 | { |
286 | #ifdef OPENSSL_FIPS | 200 | if (ctx->digest && ctx->digest->ctx_size) |
287 | if (FIPS_mode()) | 201 | OPENSSL_free(ctx->md_data); |
202 | ctx->digest=type; | ||
203 | if (!(ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) && type->ctx_size) | ||
288 | { | 204 | { |
289 | if (!(type->flags & EVP_MD_FLAG_FIPS) | 205 | ctx->update = type->update; |
290 | && !(ctx->flags & EVP_MD_CTX_FLAG_NON_FIPS_ALLOW)) | 206 | ctx->md_data=OPENSSL_malloc(type->ctx_size); |
207 | if (ctx->md_data == NULL) | ||
291 | { | 208 | { |
292 | EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_DISABLED_FOR_FIPS); | 209 | EVPerr(EVP_F_EVP_DIGESTINIT_EX, |
293 | ctx->digest = &bad_md; | 210 | ERR_R_MALLOC_FAILURE); |
294 | return 0; | 211 | return 0; |
295 | } | 212 | } |
296 | } | 213 | } |
297 | #endif | ||
298 | if (ctx->digest && ctx->digest->ctx_size) | ||
299 | OPENSSL_free(ctx->md_data); | ||
300 | ctx->digest=type; | ||
301 | if (type->ctx_size) | ||
302 | ctx->md_data=OPENSSL_malloc(type->ctx_size); | ||
303 | } | 214 | } |
304 | #ifndef OPENSSL_NO_ENGINE | 215 | #ifndef OPENSSL_NO_ENGINE |
305 | skip_to_init: | 216 | skip_to_init: |
306 | #endif | 217 | #endif |
218 | if (ctx->pctx) | ||
219 | { | ||
220 | int r; | ||
221 | r = EVP_PKEY_CTX_ctrl(ctx->pctx, -1, EVP_PKEY_OP_TYPE_SIG, | ||
222 | EVP_PKEY_CTRL_DIGESTINIT, 0, ctx); | ||
223 | if (r <= 0 && (r != -2)) | ||
224 | return 0; | ||
225 | } | ||
226 | if (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) | ||
227 | return 1; | ||
307 | return ctx->digest->init(ctx); | 228 | return ctx->digest->init(ctx); |
308 | } | 229 | } |
309 | 230 | ||
310 | int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, | 231 | int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t count) |
311 | size_t count) | ||
312 | { | 232 | { |
313 | #ifdef OPENSSL_FIPS | 233 | return ctx->update(ctx,data,count); |
314 | FIPS_selftest_check(); | ||
315 | #endif | ||
316 | return ctx->digest->update(ctx,data,count); | ||
317 | } | 234 | } |
318 | 235 | ||
319 | /* The caller can assume that this removes any secret data from the context */ | 236 | /* The caller can assume that this removes any secret data from the context */ |
@@ -329,9 +246,6 @@ int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size) | |||
329 | int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size) | 246 | int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size) |
330 | { | 247 | { |
331 | int ret; | 248 | int ret; |
332 | #ifdef OPENSSL_FIPS | ||
333 | FIPS_selftest_check(); | ||
334 | #endif | ||
335 | 249 | ||
336 | OPENSSL_assert(ctx->digest->md_size <= EVP_MAX_MD_SIZE); | 250 | OPENSSL_assert(ctx->digest->md_size <= EVP_MAX_MD_SIZE); |
337 | ret=ctx->digest->final(ctx,md); | 251 | ret=ctx->digest->final(ctx,md); |
@@ -340,7 +254,7 @@ int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size) | |||
340 | if (ctx->digest->cleanup) | 254 | if (ctx->digest->cleanup) |
341 | { | 255 | { |
342 | ctx->digest->cleanup(ctx); | 256 | ctx->digest->cleanup(ctx); |
343 | M_EVP_MD_CTX_set_flags(ctx,EVP_MD_CTX_FLAG_CLEANED); | 257 | EVP_MD_CTX_set_flags(ctx,EVP_MD_CTX_FLAG_CLEANED); |
344 | } | 258 | } |
345 | memset(ctx->md_data,0,ctx->digest->ctx_size); | 259 | memset(ctx->md_data,0,ctx->digest->ctx_size); |
346 | return ret; | 260 | return ret; |
@@ -362,7 +276,7 @@ int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in) | |||
362 | } | 276 | } |
363 | #ifndef OPENSSL_NO_ENGINE | 277 | #ifndef OPENSSL_NO_ENGINE |
364 | /* Make sure it's safe to copy a digest context using an ENGINE */ | 278 | /* Make sure it's safe to copy a digest context using an ENGINE */ |
365 | if (in->engine && !do_engine_init(in->engine)) | 279 | if (in->engine && !ENGINE_init(in->engine)) |
366 | { | 280 | { |
367 | EVPerr(EVP_F_EVP_MD_CTX_COPY_EX,ERR_R_ENGINE_LIB); | 281 | EVPerr(EVP_F_EVP_MD_CTX_COPY_EX,ERR_R_ENGINE_LIB); |
368 | return 0; | 282 | return 0; |
@@ -372,19 +286,40 @@ int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in) | |||
372 | if (out->digest == in->digest) | 286 | if (out->digest == in->digest) |
373 | { | 287 | { |
374 | tmp_buf = out->md_data; | 288 | tmp_buf = out->md_data; |
375 | M_EVP_MD_CTX_set_flags(out,EVP_MD_CTX_FLAG_REUSE); | 289 | EVP_MD_CTX_set_flags(out,EVP_MD_CTX_FLAG_REUSE); |
376 | } | 290 | } |
377 | else tmp_buf = NULL; | 291 | else tmp_buf = NULL; |
378 | EVP_MD_CTX_cleanup(out); | 292 | EVP_MD_CTX_cleanup(out); |
379 | memcpy(out,in,sizeof *out); | 293 | memcpy(out,in,sizeof *out); |
380 | 294 | ||
381 | if (out->digest->ctx_size) | 295 | if (in->md_data && out->digest->ctx_size) |
382 | { | 296 | { |
383 | if (tmp_buf) out->md_data = tmp_buf; | 297 | if (tmp_buf) |
384 | else out->md_data=OPENSSL_malloc(out->digest->ctx_size); | 298 | out->md_data = tmp_buf; |
299 | else | ||
300 | { | ||
301 | out->md_data=OPENSSL_malloc(out->digest->ctx_size); | ||
302 | if (!out->md_data) | ||
303 | { | ||
304 | EVPerr(EVP_F_EVP_MD_CTX_COPY_EX,ERR_R_MALLOC_FAILURE); | ||
305 | return 0; | ||
306 | } | ||
307 | } | ||
385 | memcpy(out->md_data,in->md_data,out->digest->ctx_size); | 308 | memcpy(out->md_data,in->md_data,out->digest->ctx_size); |
386 | } | 309 | } |
387 | 310 | ||
311 | out->update = in->update; | ||
312 | |||
313 | if (in->pctx) | ||
314 | { | ||
315 | out->pctx = EVP_PKEY_CTX_dup(in->pctx); | ||
316 | if (!out->pctx) | ||
317 | { | ||
318 | EVP_MD_CTX_cleanup(out); | ||
319 | return 0; | ||
320 | } | ||
321 | } | ||
322 | |||
388 | if (out->digest->copy) | 323 | if (out->digest->copy) |
389 | return out->digest->copy(out,in); | 324 | return out->digest->copy(out,in); |
390 | 325 | ||
@@ -398,7 +333,7 @@ int EVP_Digest(const void *data, size_t count, | |||
398 | int ret; | 333 | int ret; |
399 | 334 | ||
400 | EVP_MD_CTX_init(&ctx); | 335 | EVP_MD_CTX_init(&ctx); |
401 | M_EVP_MD_CTX_set_flags(&ctx,EVP_MD_CTX_FLAG_ONESHOT); | 336 | EVP_MD_CTX_set_flags(&ctx,EVP_MD_CTX_FLAG_ONESHOT); |
402 | ret=EVP_DigestInit_ex(&ctx, type, impl) | 337 | ret=EVP_DigestInit_ex(&ctx, type, impl) |
403 | && EVP_DigestUpdate(&ctx, data, count) | 338 | && EVP_DigestUpdate(&ctx, data, count) |
404 | && EVP_DigestFinal_ex(&ctx, md, size); | 339 | && EVP_DigestFinal_ex(&ctx, md, size); |
@@ -420,19 +355,21 @@ int EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx) | |||
420 | * because sometimes only copies of the context are ever finalised. | 355 | * because sometimes only copies of the context are ever finalised. |
421 | */ | 356 | */ |
422 | if (ctx->digest && ctx->digest->cleanup | 357 | if (ctx->digest && ctx->digest->cleanup |
423 | && !M_EVP_MD_CTX_test_flags(ctx,EVP_MD_CTX_FLAG_CLEANED)) | 358 | && !EVP_MD_CTX_test_flags(ctx,EVP_MD_CTX_FLAG_CLEANED)) |
424 | ctx->digest->cleanup(ctx); | 359 | ctx->digest->cleanup(ctx); |
425 | if (ctx->digest && ctx->digest->ctx_size && ctx->md_data | 360 | if (ctx->digest && ctx->digest->ctx_size && ctx->md_data |
426 | && !M_EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_REUSE)) | 361 | && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_REUSE)) |
427 | { | 362 | { |
428 | OPENSSL_cleanse(ctx->md_data,ctx->digest->ctx_size); | 363 | OPENSSL_cleanse(ctx->md_data,ctx->digest->ctx_size); |
429 | OPENSSL_free(ctx->md_data); | 364 | OPENSSL_free(ctx->md_data); |
430 | } | 365 | } |
366 | if (ctx->pctx) | ||
367 | EVP_PKEY_CTX_free(ctx->pctx); | ||
431 | #ifndef OPENSSL_NO_ENGINE | 368 | #ifndef OPENSSL_NO_ENGINE |
432 | if(ctx->engine) | 369 | if(ctx->engine) |
433 | /* The EVP_MD we used belongs to an ENGINE, release the | 370 | /* The EVP_MD we used belongs to an ENGINE, release the |
434 | * functional reference we held for this reason. */ | 371 | * functional reference we held for this reason. */ |
435 | do_engine_finish(ctx->engine); | 372 | ENGINE_finish(ctx->engine); |
436 | #endif | 373 | #endif |
437 | memset(ctx,'\0',sizeof *ctx); | 374 | memset(ctx,'\0',sizeof *ctx); |
438 | 375 | ||