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