Implement EVP_MD_fetch()
[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     /*
26      * Don't assume ctx->md_data was cleaned in EVP_Digest_Final, because
27      * sometimes only copies of the context are ever finalised.
28      */
29     if (ctx->digest && ctx->digest->cleanup
30         && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_CLEANED))
31         ctx->digest->cleanup(ctx);
32     if (ctx->digest && ctx->digest->ctx_size && ctx->md_data
33         && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_REUSE)) {
34         OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
35     }
36     /*
37      * pctx should be freed by the user of EVP_MD_CTX
38      * if EVP_MD_CTX_FLAG_KEEP_PKEY_CTX is set
39      */
40     if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX))
41         EVP_PKEY_CTX_free(ctx->pctx);
42 #ifndef OPENSSL_NO_ENGINE
43     ENGINE_finish(ctx->engine);
44 #endif
45     OPENSSL_cleanse(ctx, sizeof(*ctx));
46
47     return 1;
48 }
49
50 EVP_MD_CTX *EVP_MD_CTX_new(void)
51 {
52     return OPENSSL_zalloc(sizeof(EVP_MD_CTX));
53 }
54
55 void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
56 {
57     EVP_MD_CTX_reset(ctx);
58     OPENSSL_free(ctx);
59 }
60
61 int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type)
62 {
63     EVP_MD_CTX_reset(ctx);
64     return EVP_DigestInit_ex(ctx, type, NULL);
65 }
66
67 int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
68 {
69     EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
70 #ifndef OPENSSL_NO_ENGINE
71     /*
72      * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
73      * this context may already have an ENGINE! Try to avoid releasing the
74      * previous handle, re-querying for an ENGINE, and having a
75      * reinitialisation, when it may all be unnecessary.
76      */
77     if (ctx->engine && ctx->digest &&
78         (type == NULL || (type->type == ctx->digest->type)))
79         goto skip_to_init;
80     if (type) {
81         /*
82          * Ensure an ENGINE left lying around from last time is cleared (the
83          * previous check attempted to avoid this if the same ENGINE and
84          * EVP_MD could be used).
85          */
86         ENGINE_finish(ctx->engine);
87         if (impl != NULL) {
88             if (!ENGINE_init(impl)) {
89                 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
90                 return 0;
91             }
92         } else {
93             /* Ask if an ENGINE is reserved for this job */
94             impl = ENGINE_get_digest_engine(type->type);
95         }
96         if (impl != NULL) {
97             /* There's an ENGINE for this job ... (apparently) */
98             const EVP_MD *d = ENGINE_get_digest(impl, type->type);
99
100             if (d == NULL) {
101                 EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);
102                 ENGINE_finish(impl);
103                 return 0;
104             }
105             /* We'll use the ENGINE's private digest definition */
106             type = d;
107             /*
108              * Store the ENGINE functional reference so we know 'type' came
109              * from an ENGINE and we need to release it when done.
110              */
111             ctx->engine = impl;
112         } else
113             ctx->engine = NULL;
114     } else {
115         if (!ctx->digest) {
116             EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_NO_DIGEST_SET);
117             return 0;
118         }
119         type = ctx->digest;
120     }
121 #endif
122     if (ctx->digest != type) {
123         if (ctx->digest && ctx->digest->ctx_size) {
124             OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
125             ctx->md_data = NULL;
126         }
127         ctx->digest = type;
128         if (!(ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) && type->ctx_size) {
129             ctx->update = type->update;
130             ctx->md_data = OPENSSL_zalloc(type->ctx_size);
131             if (ctx->md_data == NULL) {
132                 EVPerr(EVP_F_EVP_DIGESTINIT_EX, ERR_R_MALLOC_FAILURE);
133                 return 0;
134             }
135         }
136     }
137 #ifndef OPENSSL_NO_ENGINE
138  skip_to_init:
139 #endif
140     if (ctx->pctx) {
141         int r;
142         r = EVP_PKEY_CTX_ctrl(ctx->pctx, -1, EVP_PKEY_OP_TYPE_SIG,
143                               EVP_PKEY_CTRL_DIGESTINIT, 0, ctx);
144         if (r <= 0 && (r != -2))
145             return 0;
146     }
147     if (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT)
148         return 1;
149     return ctx->digest->init(ctx);
150 }
151
152 int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t count)
153 {
154     return ctx->update(ctx, data, count);
155 }
156
157 /* The caller can assume that this removes any secret data from the context */
158 int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size)
159 {
160     int ret;
161     ret = EVP_DigestFinal_ex(ctx, md, size);
162     EVP_MD_CTX_reset(ctx);
163     return ret;
164 }
165
166 /* The caller can assume that this removes any secret data from the context */
167 int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size)
168 {
169     int ret;
170
171     OPENSSL_assert(ctx->digest->md_size <= EVP_MAX_MD_SIZE);
172     ret = ctx->digest->final(ctx, md);
173     if (size != NULL)
174         *size = ctx->digest->md_size;
175     if (ctx->digest->cleanup) {
176         ctx->digest->cleanup(ctx);
177         EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
178     }
179     OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
180     return ret;
181 }
182
183 int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t size)
184 {
185     int ret = 0;
186
187     if (ctx->digest->flags & EVP_MD_FLAG_XOF
188         && size <= INT_MAX
189         && ctx->digest->md_ctrl(ctx, EVP_MD_CTRL_XOF_LEN, (int)size, NULL)) {
190         ret = ctx->digest->final(ctx, md);
191
192         if (ctx->digest->cleanup != NULL) {
193             ctx->digest->cleanup(ctx);
194             EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
195         }
196         OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
197     } else {
198         EVPerr(EVP_F_EVP_DIGESTFINALXOF, EVP_R_NOT_XOF_OR_INVALID_LENGTH);
199     }
200
201     return ret;
202 }
203
204 int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in)
205 {
206     EVP_MD_CTX_reset(out);
207     return EVP_MD_CTX_copy_ex(out, in);
208 }
209
210 int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in)
211 {
212     unsigned char *tmp_buf;
213     if ((in == NULL) || (in->digest == NULL)) {
214         EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_INPUT_NOT_INITIALIZED);
215         return 0;
216     }
217 #ifndef OPENSSL_NO_ENGINE
218     /* Make sure it's safe to copy a digest context using an ENGINE */
219     if (in->engine && !ENGINE_init(in->engine)) {
220         EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_ENGINE_LIB);
221         return 0;
222     }
223 #endif
224
225     if (out->digest == in->digest) {
226         tmp_buf = out->md_data;
227         EVP_MD_CTX_set_flags(out, EVP_MD_CTX_FLAG_REUSE);
228     } else
229         tmp_buf = NULL;
230     EVP_MD_CTX_reset(out);
231     memcpy(out, in, sizeof(*out));
232
233     /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */
234     EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
235
236     /* Null these variables, since they are getting fixed up
237      * properly below.  Anything else may cause a memleak and/or
238      * double free if any of the memory allocations below fail
239      */
240     out->md_data = NULL;
241     out->pctx = NULL;
242
243     if (in->md_data && out->digest->ctx_size) {
244         if (tmp_buf)
245             out->md_data = tmp_buf;
246         else {
247             out->md_data = OPENSSL_malloc(out->digest->ctx_size);
248             if (out->md_data == NULL) {
249                 EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_MALLOC_FAILURE);
250                 return 0;
251             }
252         }
253         memcpy(out->md_data, in->md_data, out->digest->ctx_size);
254     }
255
256     out->update = in->update;
257
258     if (in->pctx) {
259         out->pctx = EVP_PKEY_CTX_dup(in->pctx);
260         if (!out->pctx) {
261             EVP_MD_CTX_reset(out);
262             return 0;
263         }
264     }
265
266     if (out->digest->copy)
267         return out->digest->copy(out, in);
268
269     return 1;
270 }
271
272 int EVP_Digest(const void *data, size_t count,
273                unsigned char *md, unsigned int *size, const EVP_MD *type,
274                ENGINE *impl)
275 {
276     EVP_MD_CTX *ctx = EVP_MD_CTX_new();
277     int ret;
278
279     if (ctx == NULL)
280         return 0;
281     EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT);
282     ret = EVP_DigestInit_ex(ctx, type, impl)
283         && EVP_DigestUpdate(ctx, data, count)
284         && EVP_DigestFinal_ex(ctx, md, size);
285     EVP_MD_CTX_free(ctx);
286
287     return ret;
288 }
289
290 int EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void *p2)
291 {
292     if (ctx->digest && ctx->digest->md_ctrl) {
293         int ret = ctx->digest->md_ctrl(ctx, cmd, p1, p2);
294         if (ret <= 0)
295             return 0;
296         return 1;
297     }
298     return 0;
299 }
300
301 static void *evp_md_from_dispatch(int mdtype, const OSSL_DISPATCH *fns,
302                                     OSSL_PROVIDER *prov)
303 {
304     EVP_MD *md = NULL;
305
306     if ((md = EVP_MD_meth_new(mdtype, NID_undef)) == NULL)
307         return NULL;
308
309     for (; fns->function_id != 0; fns++) {
310         int fncnt = 0;
311
312         switch (fns->function_id) {
313         case OSSL_FUNC_DIGEST_NEWCTX:
314             if (md->newctx != NULL)
315                 break;
316             md->newctx = OSSL_get_OP_digest_newctx(fns);
317             fncnt++;
318             break;
319         case OSSL_FUNC_DIGEST_INIT:
320             if (md->dinit != NULL)
321                 break;
322             md->dinit = OSSL_get_OP_digest_init(fns);
323             fncnt++;
324             break;
325         case OSSL_FUNC_DIGEST_UPDDATE:
326             if (md->dupdate != NULL)
327                 break;
328             md->dupdate = OSSL_get_OP_digest_update(fns);
329             fncnt++;
330             break;
331         case OSSL_FUNC_DIGEST_FINAL:
332             if (md->dfinal != NULL)
333                 break;
334             md->dfinal = OSSL_get_OP_digest_final(fns);
335             fncnt++;
336             break;
337         case OSSL_FUNC_DIGEST_DIGEST:
338             if (md->digest != NULL)
339                 break;
340             md->digest = OSSL_get_OP_digest_digest(fns);
341             /* We don't increment fnct for this as it is stand alone */
342             break;
343         case OSSL_FUNC_DIGEST_CLEANCTX:
344             if (md->cleanctx != NULL)
345                 break;
346             md->cleanctx = OSSL_get_OP_digest_cleanctx(fns);
347             fncnt++;
348             break;
349         case OSSL_FUNC_DIGEST_FREECTX:
350             if (md->freectx != NULL)
351                 break;
352             md->freectx = OSSL_get_OP_digest_freectx(fns);
353             fncnt++;
354             break;
355         }
356         if ((fncnt != 0 && fncnt != 6) || (fncnt == 0 && md->digest == NULL)) {
357             /*
358              * In order to be a consistent set of functions we either need the
359              * whole set of init/update/final etc functions or none of them.
360              * The "digest" function can standalone. We at least need one way to
361              * generate digests.
362              */
363             EVP_MD_meth_free(md);
364             return NULL;
365         }
366     }
367     md->prov = prov;
368     if (prov != NULL)
369         ossl_provider_upref(prov);
370
371     return md;
372 }
373
374 static int evp_md_upref(void *md)
375 {
376     return EVP_MD_upref(md);
377 }
378
379 static void evp_md_free(void *md)
380 {
381     EVP_MD_meth_free(md);
382 }
383
384 EVP_MD *EVP_MD_fetch(OPENSSL_CTX *ctx, const char *algorithm,
385                      const char *properties)
386 {
387     return evp_generic_fetch(ctx, OSSL_OP_DIGEST, algorithm, properties,
388                              evp_md_from_dispatch, evp_md_upref,
389                              evp_md_free);
390 }