]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/evp/digest.c
Make some EVP code available from within the FIPS module
[thirdparty/openssl.git] / crypto / evp / digest.c
1 /*
2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (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
8 */
9
10 #include <stdio.h>
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"
16 #include "internal/provider.h"
17 #include "evp_locl.h"
18
19 /* This call frees resources associated with the context */
20 int EVP_MD_CTX_reset(EVP_MD_CTX *ctx)
21 {
22 if (ctx == NULL)
23 return 1;
24
25 if (ctx->digest == NULL || ctx->digest->prov == NULL)
26 goto legacy;
27
28 if (ctx->provctx != NULL) {
29 if (ctx->digest->freectx != NULL)
30 ctx->digest->freectx(ctx->provctx);
31 ctx->provctx = NULL;
32 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
33 }
34
35 if (ctx->pctx != NULL)
36 goto legacy;
37
38 return 1;
39
40 /* TODO(3.0): Remove legacy code below */
41 legacy:
42
43 /*
44 * Don't assume ctx->md_data was cleaned in EVP_Digest_Final, because
45 * sometimes only copies of the context are ever finalised.
46 */
47 if (ctx->digest && ctx->digest->cleanup
48 && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_CLEANED))
49 ctx->digest->cleanup(ctx);
50 if (ctx->digest && ctx->digest->ctx_size && ctx->md_data
51 && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_REUSE)) {
52 OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
53 }
54 /*
55 * pctx should be freed by the user of EVP_MD_CTX
56 * if EVP_MD_CTX_FLAG_KEEP_PKEY_CTX is set
57 */
58 #ifndef FIPS_MODE
59 /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
60 if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX))
61 EVP_PKEY_CTX_free(ctx->pctx);
62
63 # ifndef OPENSSL_NO_ENGINE
64 ENGINE_finish(ctx->engine);
65 # endif
66 #endif
67 OPENSSL_cleanse(ctx, sizeof(*ctx));
68
69 return 1;
70 }
71
72 EVP_MD_CTX *EVP_MD_CTX_new(void)
73 {
74 return OPENSSL_zalloc(sizeof(EVP_MD_CTX));
75 }
76
77 void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
78 {
79 if (ctx == NULL)
80 return;
81
82 if (ctx->digest == NULL || ctx->digest->prov == NULL)
83 goto legacy;
84
85 EVP_MD_CTX_reset(ctx);
86
87 EVP_MD_meth_free(ctx->fetched_digest);
88 ctx->fetched_digest = NULL;
89 ctx->digest = NULL;
90 ctx->reqdigest = NULL;
91
92 OPENSSL_free(ctx);
93 return;
94
95 /* TODO(3.0): Remove legacy code below */
96 legacy:
97 EVP_MD_CTX_reset(ctx);
98 OPENSSL_free(ctx);
99 }
100
101 int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type)
102 {
103 EVP_MD_CTX_reset(ctx);
104 return EVP_DigestInit_ex(ctx, type, NULL);
105 }
106
107 int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
108 {
109 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
110 ENGINE *tmpimpl = NULL;
111 #endif
112
113 EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
114
115 if (type != NULL)
116 ctx->reqdigest = type;
117
118 /* TODO(3.0): Legacy work around code below. Remove this */
119 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
120 /*
121 * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
122 * this context may already have an ENGINE! Try to avoid releasing the
123 * previous handle, re-querying for an ENGINE, and having a
124 * reinitialisation, when it may all be unnecessary.
125 */
126 if (ctx->engine && ctx->digest &&
127 (type == NULL || (type->type == ctx->digest->type)))
128 goto skip_to_init;
129
130 if (type != NULL && impl == NULL)
131 tmpimpl = ENGINE_get_digest_engine(type->type);
132 #endif
133
134 /*
135 * If there are engines involved or if we're being used as part of
136 * EVP_DigestSignInit then we should use legacy handling for now.
137 */
138 if (ctx->engine != NULL
139 || impl != NULL
140 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
141 || tmpimpl != NULL
142 #endif
143 || ctx->pctx != NULL
144 || (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0) {
145 if (ctx->digest == ctx->fetched_digest)
146 ctx->digest = NULL;
147 EVP_MD_meth_free(ctx->fetched_digest);
148 ctx->fetched_digest = NULL;
149 goto legacy;
150 }
151
152 if (type->prov == NULL) {
153 switch(type->type) {
154 case NID_sha256:
155 case NID_md2:
156 break;
157 default:
158 goto legacy;
159 }
160 }
161
162 if (ctx->digest != NULL && ctx->digest->ctx_size > 0) {
163 OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
164 ctx->md_data = NULL;
165 }
166
167 /* TODO(3.0): Start of non-legacy code below */
168
169 if (type->prov == NULL) {
170 #ifdef FIPS_MODE
171 /* We only do explict fetches inside the FIPS module */
172 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
173 return 0;
174 #else
175 EVP_MD *provmd = EVP_MD_fetch(NULL, OBJ_nid2sn(type->type), "");
176
177 if (provmd == NULL) {
178 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
179 return 0;
180 }
181 type = provmd;
182 EVP_MD_meth_free(ctx->fetched_digest);
183 ctx->fetched_digest = provmd;
184 #endif
185 }
186
187 ctx->digest = type;
188 if (ctx->provctx == NULL) {
189 ctx->provctx = ctx->digest->newctx(ossl_provider_ctx(type->prov));
190 if (ctx->provctx == NULL) {
191 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
192 return 0;
193 }
194 }
195
196 if (ctx->digest->dinit == NULL) {
197 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
198 return 0;
199 }
200
201 return ctx->digest->dinit(ctx->provctx);
202
203 /* TODO(3.0): Remove legacy code below */
204 legacy:
205
206 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
207 if (type) {
208 /*
209 * Ensure an ENGINE left lying around from last time is cleared (the
210 * previous check attempted to avoid this if the same ENGINE and
211 * EVP_MD could be used).
212 */
213 ENGINE_finish(ctx->engine);
214 if (impl != NULL) {
215 if (!ENGINE_init(impl)) {
216 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
217 return 0;
218 }
219 } else {
220 /* Ask if an ENGINE is reserved for this job */
221 impl = tmpimpl;
222 }
223 if (impl != NULL) {
224 /* There's an ENGINE for this job ... (apparently) */
225 const EVP_MD *d = ENGINE_get_digest(impl, type->type);
226
227 if (d == NULL) {
228 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
229 ENGINE_finish(impl);
230 return 0;
231 }
232 /* We'll use the ENGINE's private digest definition */
233 type = d;
234 /*
235 * Store the ENGINE functional reference so we know 'type' came
236 * from an ENGINE and we need to release it when done.
237 */
238 ctx->engine = impl;
239 } else
240 ctx->engine = NULL;
241 } else {
242 if (!ctx->digest) {
243 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_NO_DIGEST_SET);
244 return 0;
245 }
246 type = ctx->digest;
247 }
248 #endif
249 if (ctx->digest != type) {
250 if (ctx->digest && ctx->digest->ctx_size) {
251 OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
252 ctx->md_data = NULL;
253 }
254 ctx->digest = type;
255 if (!(ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) && type->ctx_size) {
256 ctx->update = type->update;
257 ctx->md_data = OPENSSL_zalloc(type->ctx_size);
258 if (ctx->md_data == NULL) {
259 EVPerr(EVP_F_EVP_DIGESTINIT_EX, ERR_R_MALLOC_FAILURE);
260 return 0;
261 }
262 }
263 }
264 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
265 skip_to_init:
266 #endif
267 #ifndef FIPS_MODE
268 /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
269 if (ctx->pctx != NULL) {
270 int r;
271 r = EVP_PKEY_CTX_ctrl(ctx->pctx, -1, EVP_PKEY_OP_TYPE_SIG,
272 EVP_PKEY_CTRL_DIGESTINIT, 0, ctx);
273 if (r <= 0 && (r != -2))
274 return 0;
275 }
276 #endif
277 if (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT)
278 return 1;
279 return ctx->digest->init(ctx);
280 }
281
282 int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t count)
283 {
284 if (count == 0)
285 return 1;
286
287 if (ctx->digest == NULL || ctx->digest->prov == NULL)
288 goto legacy;
289
290 if (ctx->digest->dupdate == NULL) {
291 EVPerr(EVP_F_EVP_DIGESTUPDATE, EVP_R_UPDATE_ERROR);
292 return 0;
293 }
294 return ctx->digest->dupdate(ctx->provctx, data, count);
295
296 /* TODO(3.0): Remove legacy code below */
297 legacy:
298 return ctx->update(ctx, data, count);
299 }
300
301 /* The caller can assume that this removes any secret data from the context */
302 int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size)
303 {
304 int ret;
305 ret = EVP_DigestFinal_ex(ctx, md, size);
306 EVP_MD_CTX_reset(ctx);
307 return ret;
308 }
309
310 /* The caller can assume that this removes any secret data from the context */
311 int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *isize)
312 {
313 int ret;
314 size_t size = 0;
315 size_t mdsize = EVP_MD_size(ctx->digest);
316
317 if (ctx->digest == NULL || ctx->digest->prov == NULL)
318 goto legacy;
319
320 if (ctx->digest->dfinal == NULL) {
321 EVPerr(EVP_F_EVP_DIGESTFINAL_EX, EVP_R_FINAL_ERROR);
322 return 0;
323 }
324
325 ret = ctx->digest->dfinal(ctx->provctx, md, &size, mdsize);
326
327 if (isize != NULL) {
328 if (size <= UINT_MAX) {
329 *isize = (int)size;
330 } else {
331 EVPerr(EVP_F_EVP_DIGESTFINAL_EX, EVP_R_FINAL_ERROR);
332 ret = 0;
333 }
334 }
335
336 EVP_MD_CTX_reset(ctx);
337
338 return ret;
339
340 /* TODO(3.0): Remove legacy code below */
341 legacy:
342 OPENSSL_assert(mdsize <= EVP_MAX_MD_SIZE);
343 ret = ctx->digest->final(ctx, md);
344 if (isize != NULL)
345 *isize = mdsize;
346 if (ctx->digest->cleanup) {
347 ctx->digest->cleanup(ctx);
348 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
349 }
350 OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
351 return ret;
352 }
353
354 int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t size)
355 {
356 int ret = 0;
357
358 if (ctx->digest->flags & EVP_MD_FLAG_XOF
359 && size <= INT_MAX
360 && ctx->digest->md_ctrl(ctx, EVP_MD_CTRL_XOF_LEN, (int)size, NULL)) {
361 ret = ctx->digest->final(ctx, md);
362
363 if (ctx->digest->cleanup != NULL) {
364 ctx->digest->cleanup(ctx);
365 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
366 }
367 OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
368 } else {
369 EVPerr(EVP_F_EVP_DIGESTFINALXOF, EVP_R_NOT_XOF_OR_INVALID_LENGTH);
370 }
371
372 return ret;
373 }
374
375 int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in)
376 {
377 EVP_MD_CTX_reset(out);
378 return EVP_MD_CTX_copy_ex(out, in);
379 }
380
381 int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in)
382 {
383 unsigned char *tmp_buf;
384
385 if (in == NULL || in->digest == NULL) {
386 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_INPUT_NOT_INITIALIZED);
387 return 0;
388 }
389
390 if (in->digest->prov == NULL)
391 goto legacy;
392
393 if (in->digest->dupctx == NULL) {
394 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_NOT_ABLE_TO_COPY_CTX);
395 return 0;
396 }
397
398 EVP_MD_CTX_reset(out);
399 if (out->fetched_digest != NULL)
400 EVP_MD_meth_free(out->fetched_digest);
401 *out = *in;
402 /* NULL out pointers in case of error */
403 out->pctx = NULL;
404 out->provctx = NULL;
405
406 if (in->fetched_digest != NULL)
407 EVP_MD_upref(in->fetched_digest);
408
409 out->provctx = in->digest->dupctx(in->provctx);
410 if (out->provctx == NULL) {
411 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_NOT_ABLE_TO_COPY_CTX);
412 return 0;
413 }
414
415 /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
416 EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
417 #ifndef FIPS_MODE
418 /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
419 if (in->pctx != NULL) {
420 out->pctx = EVP_PKEY_CTX_dup(in->pctx);
421 if (out->pctx == NULL) {
422 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_NOT_ABLE_TO_COPY_CTX);
423 EVP_MD_CTX_reset(out);
424 return 0;
425 }
426 }
427 #endif
428
429 return 1;
430
431 /* TODO(3.0): Remove legacy code below */
432 legacy:
433 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
434 /* Make sure it's safe to copy a digest context using an ENGINE */
435 if (in->engine && !ENGINE_init(in->engine)) {
436 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_ENGINE_LIB);
437 return 0;
438 }
439 #endif
440
441 if (out->digest == in->digest) {
442 tmp_buf = out->md_data;
443 EVP_MD_CTX_set_flags(out, EVP_MD_CTX_FLAG_REUSE);
444 } else
445 tmp_buf = NULL;
446 EVP_MD_CTX_reset(out);
447 memcpy(out, in, sizeof(*out));
448
449 /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
450 EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
451
452 /* Null these variables, since they are getting fixed up
453 * properly below. Anything else may cause a memleak and/or
454 * double free if any of the memory allocations below fail
455 */
456 out->md_data = NULL;
457 out->pctx = NULL;
458
459 if (in->md_data && out->digest->ctx_size) {
460 if (tmp_buf)
461 out->md_data = tmp_buf;
462 else {
463 out->md_data = OPENSSL_malloc(out->digest->ctx_size);
464 if (out->md_data == NULL) {
465 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_MALLOC_FAILURE);
466 return 0;
467 }
468 }
469 memcpy(out->md_data, in->md_data, out->digest->ctx_size);
470 }
471
472 out->update = in->update;
473
474 #ifndef FIPS_MODE
475 /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */
476 if (in->pctx) {
477 out->pctx = EVP_PKEY_CTX_dup(in->pctx);
478 if (!out->pctx) {
479 EVP_MD_CTX_reset(out);
480 return 0;
481 }
482 }
483 #endif
484
485 if (out->digest->copy)
486 return out->digest->copy(out, in);
487
488 return 1;
489 }
490
491 int EVP_Digest(const void *data, size_t count,
492 unsigned char *md, unsigned int *size, const EVP_MD *type,
493 ENGINE *impl)
494 {
495 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
496 int ret;
497
498 if (ctx == NULL)
499 return 0;
500 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT);
501 ret = EVP_DigestInit_ex(ctx, type, impl)
502 && EVP_DigestUpdate(ctx, data, count)
503 && EVP_DigestFinal_ex(ctx, md, size);
504 EVP_MD_CTX_free(ctx);
505
506 return ret;
507 }
508
509 int EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void *p2)
510 {
511 if (ctx->digest && ctx->digest->md_ctrl) {
512 int ret = ctx->digest->md_ctrl(ctx, cmd, p1, p2);
513 if (ret <= 0)
514 return 0;
515 return 1;
516 }
517 return 0;
518 }
519
520 static void *evp_md_from_dispatch(const OSSL_DISPATCH *fns,
521 OSSL_PROVIDER *prov)
522 {
523 EVP_MD *md = NULL;
524 int fncnt = 0;
525
526 /* EVP_MD_fetch() will set the legacy NID if available */
527 if ((md = EVP_MD_meth_new(NID_undef, NID_undef)) == NULL)
528 return NULL;
529
530 for (; fns->function_id != 0; fns++) {
531 switch (fns->function_id) {
532 case OSSL_FUNC_DIGEST_NEWCTX:
533 if (md->newctx != NULL)
534 break;
535 md->newctx = OSSL_get_OP_digest_newctx(fns);
536 fncnt++;
537 break;
538 case OSSL_FUNC_DIGEST_INIT:
539 if (md->dinit != NULL)
540 break;
541 md->dinit = OSSL_get_OP_digest_init(fns);
542 fncnt++;
543 break;
544 case OSSL_FUNC_DIGEST_UPDATE:
545 if (md->dupdate != NULL)
546 break;
547 md->dupdate = OSSL_get_OP_digest_update(fns);
548 fncnt++;
549 break;
550 case OSSL_FUNC_DIGEST_FINAL:
551 if (md->dfinal != NULL)
552 break;
553 md->dfinal = OSSL_get_OP_digest_final(fns);
554 fncnt++;
555 break;
556 case OSSL_FUNC_DIGEST_DIGEST:
557 if (md->digest != NULL)
558 break;
559 md->digest = OSSL_get_OP_digest_digest(fns);
560 /* We don't increment fnct for this as it is stand alone */
561 break;
562 case OSSL_FUNC_DIGEST_FREECTX:
563 if (md->freectx != NULL)
564 break;
565 md->freectx = OSSL_get_OP_digest_freectx(fns);
566 fncnt++;
567 break;
568 case OSSL_FUNC_DIGEST_DUPCTX:
569 if (md->dupctx != NULL)
570 break;
571 md->dupctx = OSSL_get_OP_digest_dupctx(fns);
572 break;
573 case OSSL_FUNC_DIGEST_SIZE:
574 if (md->size != NULL)
575 break;
576 md->size = OSSL_get_OP_digest_size(fns);
577 break;
578 case OSSL_FUNC_DIGEST_BLOCK_SIZE:
579 if (md->dblock_size != NULL)
580 break;
581 md->dblock_size = OSSL_get_OP_digest_block_size(fns);
582 break;
583 }
584 }
585 if ((fncnt != 0 && fncnt != 5)
586 || (fncnt == 0 && md->digest == NULL)
587 || md->size == NULL) {
588 /*
589 * In order to be a consistent set of functions we either need the
590 * whole set of init/update/final etc functions or none of them.
591 * The "digest" function can standalone. We at least need one way to
592 * generate digests.
593 */
594 EVP_MD_meth_free(md);
595 return NULL;
596 }
597 md->prov = prov;
598 if (prov != NULL)
599 ossl_provider_upref(prov);
600
601 return md;
602 }
603
604 static int evp_md_upref(void *md)
605 {
606 return EVP_MD_upref(md);
607 }
608
609 static void evp_md_free(void *md)
610 {
611 EVP_MD_meth_free(md);
612 }
613
614 EVP_MD *EVP_MD_fetch(OPENSSL_CTX *ctx, const char *algorithm,
615 const char *properties)
616 {
617 EVP_MD *md =
618 evp_generic_fetch(ctx, OSSL_OP_DIGEST, algorithm, properties,
619 evp_md_from_dispatch, evp_md_upref,
620 evp_md_free);
621
622 #ifndef FIPS_MODE
623 /* TODO(3.x) get rid of the need for legacy NIDs */
624 if (md != NULL) {
625 /*
626 * FIPS module note: since internal fetches will be entirely
627 * provider based, we know that none of its code depends on legacy
628 * NIDs or any functionality that use them.
629 */
630 md->type = OBJ_sn2nid(algorithm);
631 }
632 #endif
633
634 return md;
635 }