e4787e6256ebb8acfa8836e9b368d7d6a4996360
[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     size_t mdsize = EVP_MD_size(ctx->digest);
299
300     if (ctx->digest == NULL || ctx->digest->prov == NULL)
301         goto legacy;
302
303     if (ctx->digest->dfinal == NULL) {
304         EVPerr(EVP_F_EVP_DIGESTFINAL_EX, EVP_R_FINAL_ERROR);
305         return 0;
306     }
307
308     ret = ctx->digest->dfinal(ctx->provctx, md, &size, mdsize);
309
310     if (isize != NULL) {
311         if (size <= UINT_MAX) {
312             *isize = (int)size;
313         } else {
314             EVPerr(EVP_F_EVP_DIGESTFINAL_EX, EVP_R_FINAL_ERROR);
315             ret = 0;
316         }
317     }
318
319     EVP_MD_CTX_reset(ctx);
320
321     return ret;
322
323     /* TODO(3.0): Remove legacy code below */
324  legacy:
325     OPENSSL_assert(mdsize <= EVP_MAX_MD_SIZE);
326     ret = ctx->digest->final(ctx, md);
327     if (isize != NULL)
328         *isize = mdsize;
329     if (ctx->digest->cleanup) {
330         ctx->digest->cleanup(ctx);
331         EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
332     }
333     OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
334     return ret;
335 }
336
337 int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t size)
338 {
339     int ret = 0;
340
341     if (ctx->digest->flags & EVP_MD_FLAG_XOF
342         && size <= INT_MAX
343         && ctx->digest->md_ctrl(ctx, EVP_MD_CTRL_XOF_LEN, (int)size, NULL)) {
344         ret = ctx->digest->final(ctx, md);
345
346         if (ctx->digest->cleanup != NULL) {
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     } else {
352         EVPerr(EVP_F_EVP_DIGESTFINALXOF, EVP_R_NOT_XOF_OR_INVALID_LENGTH);
353     }
354
355     return ret;
356 }
357
358 int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in)
359 {
360     EVP_MD_CTX_reset(out);
361     return EVP_MD_CTX_copy_ex(out, in);
362 }
363
364 int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in)
365 {
366     unsigned char *tmp_buf;
367
368     if (in == NULL || in->digest == NULL) {
369         EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_INPUT_NOT_INITIALIZED);
370         return 0;
371     }
372
373     if (in->digest->prov == NULL)
374         goto legacy;
375
376     if (in->digest->dupctx == NULL) {
377         EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_NOT_ABLE_TO_COPY_CTX);
378         return 0;
379     }
380
381     EVP_MD_CTX_reset(out);
382     if (out->fetched_digest != NULL)
383         EVP_MD_meth_free(out->fetched_digest);
384     *out = *in;
385     /* NULL out pointers in case of error */
386     out->pctx = NULL;
387     out->provctx = NULL;
388
389     if (in->fetched_digest != NULL)
390         EVP_MD_upref(in->fetched_digest);
391
392     out->provctx = in->digest->dupctx(in->provctx);
393     if (out->provctx == NULL) {
394         EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_NOT_ABLE_TO_COPY_CTX);
395         return 0;
396     }
397
398     /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
399     EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
400     if (in->pctx != NULL) {
401         out->pctx = EVP_PKEY_CTX_dup(in->pctx);
402         if (out->pctx == NULL) {
403             EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_NOT_ABLE_TO_COPY_CTX);
404             EVP_MD_CTX_reset(out);
405             return 0;
406         }
407     }
408
409     return 1;
410
411     /* TODO(3.0): Remove legacy code below */
412  legacy:
413 #ifndef OPENSSL_NO_ENGINE
414     /* Make sure it's safe to copy a digest context using an ENGINE */
415     if (in->engine && !ENGINE_init(in->engine)) {
416         EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_ENGINE_LIB);
417         return 0;
418     }
419 #endif
420
421     if (out->digest == in->digest) {
422         tmp_buf = out->md_data;
423         EVP_MD_CTX_set_flags(out, EVP_MD_CTX_FLAG_REUSE);
424     } else
425         tmp_buf = NULL;
426     EVP_MD_CTX_reset(out);
427     memcpy(out, in, sizeof(*out));
428
429     /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
430     EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
431
432     /* Null these variables, since they are getting fixed up
433      * properly below.  Anything else may cause a memleak and/or
434      * double free if any of the memory allocations below fail
435      */
436     out->md_data = NULL;
437     out->pctx = NULL;
438
439     if (in->md_data && out->digest->ctx_size) {
440         if (tmp_buf)
441             out->md_data = tmp_buf;
442         else {
443             out->md_data = OPENSSL_malloc(out->digest->ctx_size);
444             if (out->md_data == NULL) {
445                 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_MALLOC_FAILURE);
446                 return 0;
447             }
448         }
449         memcpy(out->md_data, in->md_data, out->digest->ctx_size);
450     }
451
452     out->update = in->update;
453
454     if (in->pctx) {
455         out->pctx = EVP_PKEY_CTX_dup(in->pctx);
456         if (!out->pctx) {
457             EVP_MD_CTX_reset(out);
458             return 0;
459         }
460     }
461
462     if (out->digest->copy)
463         return out->digest->copy(out, in);
464
465     return 1;
466 }
467
468 int EVP_Digest(const void *data, size_t count,
469                unsigned char *md, unsigned int *size, const EVP_MD *type,
470                ENGINE *impl)
471 {
472     EVP_MD_CTX *ctx = EVP_MD_CTX_new();
473     int ret;
474
475     if (ctx == NULL)
476         return 0;
477     EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT);
478     ret = EVP_DigestInit_ex(ctx, type, impl)
479         && EVP_DigestUpdate(ctx, data, count)
480         && EVP_DigestFinal_ex(ctx, md, size);
481     EVP_MD_CTX_free(ctx);
482
483     return ret;
484 }
485
486 int EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void *p2)
487 {
488     if (ctx->digest && ctx->digest->md_ctrl) {
489         int ret = ctx->digest->md_ctrl(ctx, cmd, p1, p2);
490         if (ret <= 0)
491             return 0;
492         return 1;
493     }
494     return 0;
495 }
496
497 static void *evp_md_from_dispatch(int mdtype, const OSSL_DISPATCH *fns,
498                                     OSSL_PROVIDER *prov)
499 {
500     EVP_MD *md = NULL;
501     int fncnt = 0;
502
503     if ((md = EVP_MD_meth_new(mdtype, NID_undef)) == NULL)
504         return NULL;
505
506     for (; fns->function_id != 0; fns++) {
507         switch (fns->function_id) {
508         case OSSL_FUNC_DIGEST_NEWCTX:
509             if (md->newctx != NULL)
510                 break;
511             md->newctx = OSSL_get_OP_digest_newctx(fns);
512             fncnt++;
513             break;
514         case OSSL_FUNC_DIGEST_INIT:
515             if (md->dinit != NULL)
516                 break;
517             md->dinit = OSSL_get_OP_digest_init(fns);
518             fncnt++;
519             break;
520         case OSSL_FUNC_DIGEST_UPDDATE:
521             if (md->dupdate != NULL)
522                 break;
523             md->dupdate = OSSL_get_OP_digest_update(fns);
524             fncnt++;
525             break;
526         case OSSL_FUNC_DIGEST_FINAL:
527             if (md->dfinal != NULL)
528                 break;
529             md->dfinal = OSSL_get_OP_digest_final(fns);
530             fncnt++;
531             break;
532         case OSSL_FUNC_DIGEST_DIGEST:
533             if (md->digest != NULL)
534                 break;
535             md->digest = OSSL_get_OP_digest_digest(fns);
536             /* We don't increment fnct for this as it is stand alone */
537             break;
538         case OSSL_FUNC_DIGEST_FREECTX:
539             if (md->freectx != NULL)
540                 break;
541             md->freectx = OSSL_get_OP_digest_freectx(fns);
542             fncnt++;
543             break;
544         case OSSL_FUNC_DIGEST_DUPCTX:
545             if (md->dupctx != NULL)
546                 break;
547             md->dupctx = OSSL_get_OP_digest_dupctx(fns);
548             break;
549         case OSSL_FUNC_DIGEST_SIZE:
550             if (md->size != NULL)
551                 break;
552             md->size = OSSL_get_OP_digest_size(fns);
553             break;
554         case OSSL_FUNC_DIGEST_BLOCK_SIZE:
555             if (md->dblock_size != NULL)
556                 break;
557             md->dblock_size = OSSL_get_OP_digest_block_size(fns);
558             break;
559         }
560     }
561     if ((fncnt != 0 && fncnt != 5)
562         || (fncnt == 0 && md->digest == NULL)
563         || md->size == NULL) {
564         /*
565          * In order to be a consistent set of functions we either need the
566          * whole set of init/update/final etc functions or none of them.
567          * The "digest" function can standalone. We at least need one way to
568          * generate digests.
569          */
570         EVP_MD_meth_free(md);
571         return NULL;
572     }
573     md->prov = prov;
574     if (prov != NULL)
575         ossl_provider_upref(prov);
576
577     return md;
578 }
579
580 static int evp_md_upref(void *md)
581 {
582     return EVP_MD_upref(md);
583 }
584
585 static void evp_md_free(void *md)
586 {
587     EVP_MD_meth_free(md);
588 }
589
590 static int evp_md_nid(void *vmd)
591 {
592     EVP_MD *md = vmd;
593
594     return md->type;
595 }
596
597 EVP_MD *EVP_MD_fetch(OPENSSL_CTX *ctx, const char *algorithm,
598                      const char *properties)
599 {
600     return evp_generic_fetch(ctx, OSSL_OP_DIGEST, algorithm, properties,
601                              evp_md_from_dispatch, evp_md_upref,
602                              evp_md_free, evp_md_nid);
603 }