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.c154
1 files changed, 24 insertions, 130 deletions
diff --git a/src/lib/libcrypto/evp/digest.c b/src/lib/libcrypto/evp/digest.c
index 3bc2d1295c..762e6d3450 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
121void EVP_MD_CTX_init(EVP_MD_CTX *ctx) 120void EVP_MD_CTX_init(EVP_MD_CTX *ctx)
122 { 121 {
@@ -138,77 +137,18 @@ int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type)
138 return EVP_DigestInit_ex(ctx, type, NULL); 137 return EVP_DigestInit_ex(ctx, type, NULL);
139 } 138 }
140 139
141#ifdef OPENSSL_FIPS 140int 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
147static int bad_init(EVP_MD_CTX *ctx)
148 { FIPS_ERROR_IGNORED("Digest init"); return 0;}
149
150static int bad_update(EVP_MD_CTX *ctx,const void *data,size_t count)
151 { FIPS_ERROR_IGNORED("Digest update"); return 0;}
152
153static int bad_final(EVP_MD_CTX *ctx,unsigned char *md)
154 { FIPS_ERROR_IGNORED("Digest Final"); return 0;}
155
156static const EVP_MD bad_md =
157 { 141 {
158 0, 142 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 143#ifndef OPENSSL_NO_ENGINE
175 144 /* Whether it's nice or not, "Inits" can be used on "Final"'d contexts
176#ifdef OPENSSL_FIPS 145 * so this context may already have an ENGINE! Try to avoid releasing
177 146 * the previous handle, re-querying for an ENGINE, and having a
178static int do_engine_null(ENGINE *impl) { return 0;} 147 * reinitialisation, when it may all be unecessary. */
179static int do_evp_md_engine_null(EVP_MD_CTX *ctx, 148 if (ctx->engine && ctx->digest && (!type ||
180 const EVP_MD **ptype, ENGINE *impl) 149 (type && (type->type == ctx->digest->type))))
181 { return 1; } 150 goto skip_to_init;
182 151 if (type)
183static int (*do_engine_init)(ENGINE *impl)
184 = do_engine_null;
185
186static int (*do_engine_finish)(ENGINE *impl)
187 = do_engine_null;
188
189static int (*do_evp_md_engine)
190 (EVP_MD_CTX *ctx, const EVP_MD **ptype, ENGINE *impl)
191 = do_evp_md_engine_null;
192
193void 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
209static int do_evp_md_engine(EVP_MD_CTX *ctx, const EVP_MD **ptype, ENGINE *impl)
210 {
211 if (*ptype)
212 { 152 {
213 /* Ensure an ENGINE left lying around from last time is cleared 153 /* Ensure an ENGINE left lying around from last time is cleared
214 * (the previous check attempted to avoid this if the same 154 * (the previous check attempted to avoid this if the same
@@ -219,25 +159,25 @@ static int do_evp_md_engine(EVP_MD_CTX *ctx, const EVP_MD **ptype, ENGINE *impl)
219 { 159 {
220 if (!ENGINE_init(impl)) 160 if (!ENGINE_init(impl))
221 { 161 {
222 EVPerr(EVP_F_DO_EVP_MD_ENGINE,EVP_R_INITIALIZATION_ERROR); 162 EVPerr(EVP_F_EVP_DIGESTINIT_EX,EVP_R_INITIALIZATION_ERROR);
223 return 0; 163 return 0;
224 } 164 }
225 } 165 }
226 else 166 else
227 /* Ask if an ENGINE is reserved for this job */ 167 /* Ask if an ENGINE is reserved for this job */
228 impl = ENGINE_get_digest_engine((*ptype)->type); 168 impl = ENGINE_get_digest_engine(type->type);
229 if(impl) 169 if(impl)
230 { 170 {
231 /* There's an ENGINE for this job ... (apparently) */ 171 /* There's an ENGINE for this job ... (apparently) */
232 const EVP_MD *d = ENGINE_get_digest(impl, (*ptype)->type); 172 const EVP_MD *d = ENGINE_get_digest(impl, type->type);
233 if(!d) 173 if(!d)
234 { 174 {
235 /* Same comment from evp_enc.c */ 175 /* Same comment from evp_enc.c */
236 EVPerr(EVP_F_DO_EVP_MD_ENGINE,EVP_R_INITIALIZATION_ERROR); 176 EVPerr(EVP_F_EVP_DIGESTINIT_EX,EVP_R_INITIALIZATION_ERROR);
237 return 0; 177 return 0;
238 } 178 }
239 /* We'll use the ENGINE's private digest definition */ 179 /* We'll use the ENGINE's private digest definition */
240 *ptype = d; 180 type = d;
241 /* Store the ENGINE functional reference so we know 181 /* Store the ENGINE functional reference so we know
242 * 'type' came from an ENGINE and we need to release 182 * 'type' came from an ENGINE and we need to release
243 * it when done. */ 183 * it when done. */
@@ -249,52 +189,12 @@ static int do_evp_md_engine(EVP_MD_CTX *ctx, const EVP_MD **ptype, ENGINE *impl)
249 else 189 else
250 if(!ctx->digest) 190 if(!ctx->digest)
251 { 191 {
252 EVPerr(EVP_F_DO_EVP_MD_ENGINE,EVP_R_NO_DIGEST_SET); 192 EVPerr(EVP_F_EVP_DIGESTINIT_EX,EVP_R_NO_DIGEST_SET);
253 return 0; 193 return 0;
254 } 194 }
255 return 1;
256 }
257
258#endif
259
260#endif
261
262int 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 195#endif
284 if (ctx->digest != type) 196 if (ctx->digest != type)
285 { 197 {
286#ifdef OPENSSL_FIPS
287 if (FIPS_mode())
288 {
289 if (!(type->flags & EVP_MD_FLAG_FIPS)
290 && !(ctx->flags & EVP_MD_CTX_FLAG_NON_FIPS_ALLOW))
291 {
292 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_DISABLED_FOR_FIPS);
293 ctx->digest = &bad_md;
294 return 0;
295 }
296 }
297#endif
298 if (ctx->digest && ctx->digest->ctx_size) 198 if (ctx->digest && ctx->digest->ctx_size)
299 OPENSSL_free(ctx->md_data); 199 OPENSSL_free(ctx->md_data);
300 ctx->digest=type; 200 ctx->digest=type;
@@ -302,7 +202,7 @@ int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
302 ctx->md_data=OPENSSL_malloc(type->ctx_size); 202 ctx->md_data=OPENSSL_malloc(type->ctx_size);
303 } 203 }
304#ifndef OPENSSL_NO_ENGINE 204#ifndef OPENSSL_NO_ENGINE
305 skip_to_init: 205skip_to_init:
306#endif 206#endif
307 return ctx->digest->init(ctx); 207 return ctx->digest->init(ctx);
308 } 208 }
@@ -310,9 +210,6 @@ int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
310int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, 210int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data,
311 size_t count) 211 size_t count)
312 { 212 {
313#ifdef OPENSSL_FIPS
314 FIPS_selftest_check();
315#endif
316 return ctx->digest->update(ctx,data,count); 213 return ctx->digest->update(ctx,data,count);
317 } 214 }
318 215
@@ -329,9 +226,6 @@ int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size)
329int 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)
330 { 227 {
331 int ret; 228 int ret;
332#ifdef OPENSSL_FIPS
333 FIPS_selftest_check();
334#endif
335 229
336 OPENSSL_assert(ctx->digest->md_size <= EVP_MAX_MD_SIZE); 230 OPENSSL_assert(ctx->digest->md_size <= EVP_MAX_MD_SIZE);
337 ret=ctx->digest->final(ctx,md); 231 ret=ctx->digest->final(ctx,md);
@@ -340,7 +234,7 @@ int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size)
340 if (ctx->digest->cleanup) 234 if (ctx->digest->cleanup)
341 { 235 {
342 ctx->digest->cleanup(ctx); 236 ctx->digest->cleanup(ctx);
343 M_EVP_MD_CTX_set_flags(ctx,EVP_MD_CTX_FLAG_CLEANED); 237 EVP_MD_CTX_set_flags(ctx,EVP_MD_CTX_FLAG_CLEANED);
344 } 238 }
345 memset(ctx->md_data,0,ctx->digest->ctx_size); 239 memset(ctx->md_data,0,ctx->digest->ctx_size);
346 return ret; 240 return ret;
@@ -362,7 +256,7 @@ int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in)
362 } 256 }
363#ifndef OPENSSL_NO_ENGINE 257#ifndef OPENSSL_NO_ENGINE
364 /* Make sure it's safe to copy a digest context using an ENGINE */ 258 /* Make sure it's safe to copy a digest context using an ENGINE */
365 if (in->engine && !do_engine_init(in->engine)) 259 if (in->engine && !ENGINE_init(in->engine))
366 { 260 {
367 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX,ERR_R_ENGINE_LIB); 261 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX,ERR_R_ENGINE_LIB);
368 return 0; 262 return 0;
@@ -372,7 +266,7 @@ int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in)
372 if (out->digest == in->digest) 266 if (out->digest == in->digest)
373 { 267 {
374 tmp_buf = out->md_data; 268 tmp_buf = out->md_data;
375 M_EVP_MD_CTX_set_flags(out,EVP_MD_CTX_FLAG_REUSE); 269 EVP_MD_CTX_set_flags(out,EVP_MD_CTX_FLAG_REUSE);
376 } 270 }
377 else tmp_buf = NULL; 271 else tmp_buf = NULL;
378 EVP_MD_CTX_cleanup(out); 272 EVP_MD_CTX_cleanup(out);
@@ -398,7 +292,7 @@ int EVP_Digest(const void *data, size_t count,
398 int ret; 292 int ret;
399 293
400 EVP_MD_CTX_init(&ctx); 294 EVP_MD_CTX_init(&ctx);
401 M_EVP_MD_CTX_set_flags(&ctx,EVP_MD_CTX_FLAG_ONESHOT); 295 EVP_MD_CTX_set_flags(&ctx,EVP_MD_CTX_FLAG_ONESHOT);
402 ret=EVP_DigestInit_ex(&ctx, type, impl) 296 ret=EVP_DigestInit_ex(&ctx, type, impl)
403 && EVP_DigestUpdate(&ctx, data, count) 297 && EVP_DigestUpdate(&ctx, data, count)
404 && EVP_DigestFinal_ex(&ctx, md, size); 298 && EVP_DigestFinal_ex(&ctx, md, size);
@@ -420,10 +314,10 @@ int EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx)
420 * because sometimes only copies of the context are ever finalised. 314 * because sometimes only copies of the context are ever finalised.
421 */ 315 */
422 if (ctx->digest && ctx->digest->cleanup 316 if (ctx->digest && ctx->digest->cleanup
423 && !M_EVP_MD_CTX_test_flags(ctx,EVP_MD_CTX_FLAG_CLEANED)) 317 && !EVP_MD_CTX_test_flags(ctx,EVP_MD_CTX_FLAG_CLEANED))
424 ctx->digest->cleanup(ctx); 318 ctx->digest->cleanup(ctx);
425 if (ctx->digest && ctx->digest->ctx_size && ctx->md_data 319 if (ctx->digest && ctx->digest->ctx_size && ctx->md_data
426 && !M_EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_REUSE)) 320 && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_REUSE))
427 { 321 {
428 OPENSSL_cleanse(ctx->md_data,ctx->digest->ctx_size); 322 OPENSSL_cleanse(ctx->md_data,ctx->digest->ctx_size);
429 OPENSSL_free(ctx->md_data); 323 OPENSSL_free(ctx->md_data);
@@ -432,7 +326,7 @@ int EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx)
432 if(ctx->engine) 326 if(ctx->engine)
433 /* The EVP_MD we used belongs to an ENGINE, release the 327 /* The EVP_MD we used belongs to an ENGINE, release the
434 * functional reference we held for this reason. */ 328 * functional reference we held for this reason. */
435 do_engine_finish(ctx->engine); 329 ENGINE_finish(ctx->engine);
436#endif 330#endif
437 memset(ctx,'\0',sizeof *ctx); 331 memset(ctx,'\0',sizeof *ctx);
438 332