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