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