]>
Commit | Line | Data |
---|---|---|
62867571 | 1 | /* |
d5e5e2ff | 2 | * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved. |
5ba372b1 | 3 | * |
4a8b0c55 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
62867571 RS |
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 | |
5ba372b1 | 8 | */ |
d02b48c6 RE |
9 | |
10 | #include <stdio.h> | |
ec577822 BM |
11 | #include <openssl/objects.h> |
12 | #include <openssl/evp.h> | |
3c27208f | 13 | #include <openssl/engine.h> |
d5e5e2ff SL |
14 | #include <openssl/params.h> |
15 | #include <openssl/core_names.h> | |
16 | #include "internal/cryptlib.h" | |
ab0a14bb | 17 | #include "internal/evp_int.h" |
3653d0c2 | 18 | #include "internal/provider.h" |
77a01145 | 19 | #include "evp_locl.h" |
d02b48c6 | 20 | |
74cabf3f | 21 | /* This call frees resources associated with the context */ |
959ed531 | 22 | int EVP_MD_CTX_reset(EVP_MD_CTX *ctx) |
0f113f3e | 23 | { |
74cabf3f RL |
24 | if (ctx == NULL) |
25 | return 1; | |
26 | ||
8c8cf0d9 MC |
27 | if (ctx->digest == NULL || ctx->digest->prov == NULL) |
28 | goto legacy; | |
29 | ||
30 | if (ctx->provctx != NULL) { | |
31 | if (ctx->digest->freectx != NULL) | |
32 | ctx->digest->freectx(ctx->provctx); | |
33 | ctx->provctx = NULL; | |
34 | EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED); | |
35 | } | |
36 | ||
37 | if (ctx->pctx != NULL) | |
38 | goto legacy; | |
39 | ||
40 | return 1; | |
41 | ||
42 | /* TODO(3.0): Remove legacy code below */ | |
43 | legacy: | |
44 | ||
74cabf3f RL |
45 | /* |
46 | * Don't assume ctx->md_data was cleaned in EVP_Digest_Final, because | |
47 | * sometimes only copies of the context are ever finalised. | |
48 | */ | |
49 | if (ctx->digest && ctx->digest->cleanup | |
50 | && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_CLEANED)) | |
51 | ctx->digest->cleanup(ctx); | |
52 | if (ctx->digest && ctx->digest->ctx_size && ctx->md_data | |
53 | && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_REUSE)) { | |
54 | OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size); | |
55 | } | |
00902d94 PY |
56 | /* |
57 | * pctx should be freed by the user of EVP_MD_CTX | |
4803717f | 58 | * if EVP_MD_CTX_FLAG_KEEP_PKEY_CTX is set |
00902d94 | 59 | */ |
319e518a MC |
60 | #ifndef FIPS_MODE |
61 | /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */ | |
4803717f | 62 | if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX)) |
00902d94 | 63 | EVP_PKEY_CTX_free(ctx->pctx); |
319e518a MC |
64 | |
65 | # ifndef OPENSSL_NO_ENGINE | |
7c96dbcd | 66 | ENGINE_finish(ctx->engine); |
319e518a | 67 | # endif |
74cabf3f | 68 | #endif |
3ce2fdab | 69 | OPENSSL_cleanse(ctx, sizeof(*ctx)); |
74cabf3f RL |
70 | |
71 | return 1; | |
0f113f3e | 72 | } |
dbad1690 | 73 | |
959ed531 | 74 | EVP_MD_CTX *EVP_MD_CTX_new(void) |
0f113f3e | 75 | { |
74cabf3f RL |
76 | return OPENSSL_zalloc(sizeof(EVP_MD_CTX)); |
77 | } | |
dbad1690 | 78 | |
959ed531 | 79 | void EVP_MD_CTX_free(EVP_MD_CTX *ctx) |
74cabf3f | 80 | { |
8c8cf0d9 MC |
81 | if (ctx == NULL) |
82 | return; | |
83 | ||
84 | if (ctx->digest == NULL || ctx->digest->prov == NULL) | |
85 | goto legacy; | |
86 | ||
87 | EVP_MD_CTX_reset(ctx); | |
88 | ||
89 | EVP_MD_meth_free(ctx->fetched_digest); | |
90 | ctx->fetched_digest = NULL; | |
91 | ctx->digest = NULL; | |
b7c913c8 | 92 | ctx->reqdigest = NULL; |
8c8cf0d9 MC |
93 | |
94 | OPENSSL_free(ctx); | |
95 | return; | |
96 | ||
97 | /* TODO(3.0): Remove legacy code below */ | |
98 | legacy: | |
959ed531 | 99 | EVP_MD_CTX_reset(ctx); |
74cabf3f | 100 | OPENSSL_free(ctx); |
0f113f3e | 101 | } |
dbad1690 | 102 | |
2dc769a1 | 103 | int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type) |
0f113f3e | 104 | { |
959ed531 | 105 | EVP_MD_CTX_reset(ctx); |
0f113f3e MC |
106 | return EVP_DigestInit_ex(ctx, type, NULL); |
107 | } | |
0fea7ed4 | 108 | |
11a57c7b | 109 | int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl) |
0f113f3e | 110 | { |
319e518a | 111 | #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE) |
8c8cf0d9 | 112 | ENGINE *tmpimpl = NULL; |
319e518a | 113 | #endif |
8c8cf0d9 | 114 | |
0f113f3e | 115 | EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_CLEANED); |
8c8cf0d9 | 116 | |
b7c913c8 MC |
117 | if (type != NULL) |
118 | ctx->reqdigest = type; | |
119 | ||
8c8cf0d9 | 120 | /* TODO(3.0): Legacy work around code below. Remove this */ |
319e518a | 121 | #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE) |
0f113f3e MC |
122 | /* |
123 | * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so | |
124 | * this context may already have an ENGINE! Try to avoid releasing the | |
125 | * previous handle, re-querying for an ENGINE, and having a | |
0d4fb843 | 126 | * reinitialisation, when it may all be unnecessary. |
0f113f3e | 127 | */ |
a93e0e78 MRA |
128 | if (ctx->engine && ctx->digest && |
129 | (type == NULL || (type->type == ctx->digest->type))) | |
0f113f3e | 130 | goto skip_to_init; |
8c8cf0d9 | 131 | |
98475995 RL |
132 | if (type != NULL) { |
133 | /* | |
134 | * Ensure an ENGINE left lying around from last time is cleared (the | |
135 | * previous check attempted to avoid this if the same ENGINE and | |
136 | * EVP_MD could be used). | |
137 | */ | |
138 | ENGINE_finish(ctx->engine); | |
139 | ctx->engine = NULL; | |
140 | } | |
141 | ||
8c8cf0d9 MC |
142 | if (type != NULL && impl == NULL) |
143 | tmpimpl = ENGINE_get_digest_engine(type->type); | |
144 | #endif | |
145 | ||
146 | /* | |
147 | * If there are engines involved or if we're being used as part of | |
148 | * EVP_DigestSignInit then we should use legacy handling for now. | |
149 | */ | |
150 | if (ctx->engine != NULL | |
151 | || impl != NULL | |
319e518a | 152 | #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE) |
8c8cf0d9 | 153 | || tmpimpl != NULL |
319e518a | 154 | #endif |
8c8cf0d9 MC |
155 | || ctx->pctx != NULL |
156 | || (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0) { | |
157 | if (ctx->digest == ctx->fetched_digest) | |
158 | ctx->digest = NULL; | |
159 | EVP_MD_meth_free(ctx->fetched_digest); | |
160 | ctx->fetched_digest = NULL; | |
161 | goto legacy; | |
162 | } | |
163 | ||
8c8cf0d9 MC |
164 | if (ctx->digest != NULL && ctx->digest->ctx_size > 0) { |
165 | OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size); | |
166 | ctx->md_data = NULL; | |
167 | } | |
168 | ||
169 | /* TODO(3.0): Start of non-legacy code below */ | |
170 | ||
171 | if (type->prov == NULL) { | |
319e518a MC |
172 | #ifdef FIPS_MODE |
173 | /* We only do explict fetches inside the FIPS module */ | |
174 | EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR); | |
175 | return 0; | |
176 | #else | |
177 | EVP_MD *provmd = EVP_MD_fetch(NULL, OBJ_nid2sn(type->type), ""); | |
178 | ||
8c8cf0d9 MC |
179 | if (provmd == NULL) { |
180 | EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR); | |
181 | return 0; | |
182 | } | |
183 | type = provmd; | |
184 | EVP_MD_meth_free(ctx->fetched_digest); | |
185 | ctx->fetched_digest = provmd; | |
319e518a | 186 | #endif |
8c8cf0d9 MC |
187 | } |
188 | ||
d5e5e2ff SL |
189 | if (ctx->provctx != NULL && ctx->digest != NULL && ctx->digest != type) { |
190 | if (ctx->digest->freectx != NULL) | |
191 | ctx->digest->freectx(ctx->provctx); | |
192 | ctx->provctx = NULL; | |
193 | } | |
8c8cf0d9 MC |
194 | ctx->digest = type; |
195 | if (ctx->provctx == NULL) { | |
a39eb840 | 196 | ctx->provctx = ctx->digest->newctx(ossl_provider_ctx(type->prov)); |
8c8cf0d9 MC |
197 | if (ctx->provctx == NULL) { |
198 | EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR); | |
199 | return 0; | |
200 | } | |
201 | } | |
202 | ||
203 | if (ctx->digest->dinit == NULL) { | |
204 | EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR); | |
205 | return 0; | |
206 | } | |
207 | ||
208 | return ctx->digest->dinit(ctx->provctx); | |
209 | ||
210 | /* TODO(3.0): Remove legacy code below */ | |
211 | legacy: | |
212 | ||
319e518a | 213 | #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE) |
0f113f3e | 214 | if (type) { |
7c96dbcd | 215 | if (impl != NULL) { |
0f113f3e MC |
216 | if (!ENGINE_init(impl)) { |
217 | EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR); | |
218 | return 0; | |
219 | } | |
7c96dbcd | 220 | } else { |
0f113f3e | 221 | /* Ask if an ENGINE is reserved for this job */ |
8c8cf0d9 | 222 | impl = tmpimpl; |
7c96dbcd RS |
223 | } |
224 | if (impl != NULL) { | |
0f113f3e MC |
225 | /* There's an ENGINE for this job ... (apparently) */ |
226 | const EVP_MD *d = ENGINE_get_digest(impl, type->type); | |
7c96dbcd RS |
227 | |
228 | if (d == NULL) { | |
0f113f3e MC |
229 | EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR); |
230 | ENGINE_finish(impl); | |
231 | return 0; | |
232 | } | |
233 | /* We'll use the ENGINE's private digest definition */ | |
234 | type = d; | |
235 | /* | |
236 | * Store the ENGINE functional reference so we know 'type' came | |
237 | * from an ENGINE and we need to release it when done. | |
238 | */ | |
239 | ctx->engine = impl; | |
240 | } else | |
241 | ctx->engine = NULL; | |
a0108702 MC |
242 | } else { |
243 | if (!ctx->digest) { | |
244 | EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_NO_DIGEST_SET); | |
245 | return 0; | |
246 | } | |
247 | type = ctx->digest; | |
0f113f3e | 248 | } |
90e8a310 | 249 | #endif |
0f113f3e | 250 | if (ctx->digest != type) { |
ffe9150b | 251 | if (ctx->digest && ctx->digest->ctx_size) { |
a93e0e78 | 252 | OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size); |
ffe9150b MC |
253 | ctx->md_data = NULL; |
254 | } | |
0f113f3e MC |
255 | ctx->digest = type; |
256 | if (!(ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) && type->ctx_size) { | |
257 | ctx->update = type->update; | |
84c15091 | 258 | ctx->md_data = OPENSSL_zalloc(type->ctx_size); |
0f113f3e MC |
259 | if (ctx->md_data == NULL) { |
260 | EVPerr(EVP_F_EVP_DIGESTINIT_EX, ERR_R_MALLOC_FAILURE); | |
261 | return 0; | |
262 | } | |
263 | } | |
264 | } | |
319e518a | 265 | #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE) |
0f113f3e | 266 | skip_to_init: |
0b13e9f0 | 267 | #endif |
319e518a MC |
268 | #ifndef FIPS_MODE |
269 | /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */ | |
270 | if (ctx->pctx != NULL) { | |
0f113f3e MC |
271 | int r; |
272 | r = EVP_PKEY_CTX_ctrl(ctx->pctx, -1, EVP_PKEY_OP_TYPE_SIG, | |
273 | EVP_PKEY_CTRL_DIGESTINIT, 0, ctx); | |
274 | if (r <= 0 && (r != -2)) | |
275 | return 0; | |
276 | } | |
319e518a | 277 | #endif |
0f113f3e MC |
278 | if (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) |
279 | return 1; | |
280 | return ctx->digest->init(ctx); | |
281 | } | |
d02b48c6 | 282 | |
f80921b6 | 283 | int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t count) |
0f113f3e | 284 | { |
a8274ea3 MC |
285 | if (count == 0) |
286 | return 1; | |
287 | ||
8c8cf0d9 MC |
288 | if (ctx->digest == NULL || ctx->digest->prov == NULL) |
289 | goto legacy; | |
290 | ||
291 | if (ctx->digest->dupdate == NULL) { | |
292 | EVPerr(EVP_F_EVP_DIGESTUPDATE, EVP_R_UPDATE_ERROR); | |
293 | return 0; | |
294 | } | |
295 | return ctx->digest->dupdate(ctx->provctx, data, count); | |
296 | ||
297 | /* TODO(3.0): Remove legacy code below */ | |
298 | legacy: | |
0f113f3e MC |
299 | return ctx->update(ctx, data, count); |
300 | } | |
d02b48c6 | 301 | |
dbad1690 | 302 | /* The caller can assume that this removes any secret data from the context */ |
2dc769a1 | 303 | int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size) |
0f113f3e MC |
304 | { |
305 | int ret; | |
306 | ret = EVP_DigestFinal_ex(ctx, md, size); | |
959ed531 | 307 | EVP_MD_CTX_reset(ctx); |
0f113f3e MC |
308 | return ret; |
309 | } | |
20d2186c DSH |
310 | |
311 | /* The caller can assume that this removes any secret data from the context */ | |
8c8cf0d9 | 312 | int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *isize) |
0f113f3e MC |
313 | { |
314 | int ret; | |
8c8cf0d9 | 315 | size_t size = 0; |
0ad50b4d | 316 | size_t mdsize = EVP_MD_size(ctx->digest); |
8c8cf0d9 MC |
317 | |
318 | if (ctx->digest == NULL || ctx->digest->prov == NULL) | |
319 | goto legacy; | |
54a656ef | 320 | |
8c8cf0d9 MC |
321 | if (ctx->digest->dfinal == NULL) { |
322 | EVPerr(EVP_F_EVP_DIGESTFINAL_EX, EVP_R_FINAL_ERROR); | |
323 | return 0; | |
324 | } | |
325 | ||
0ad50b4d | 326 | ret = ctx->digest->dfinal(ctx->provctx, md, &size, mdsize); |
8c8cf0d9 MC |
327 | |
328 | if (isize != NULL) { | |
329 | if (size <= UINT_MAX) { | |
330 | *isize = (int)size; | |
331 | } else { | |
332 | EVPerr(EVP_F_EVP_DIGESTFINAL_EX, EVP_R_FINAL_ERROR); | |
333 | ret = 0; | |
334 | } | |
335 | } | |
336 | ||
337 | EVP_MD_CTX_reset(ctx); | |
8c8cf0d9 MC |
338 | return ret; |
339 | ||
340 | /* TODO(3.0): Remove legacy code below */ | |
341 | legacy: | |
0ad50b4d | 342 | OPENSSL_assert(mdsize <= EVP_MAX_MD_SIZE); |
0f113f3e | 343 | ret = ctx->digest->final(ctx, md); |
8c8cf0d9 | 344 | if (isize != NULL) |
0ad50b4d | 345 | *isize = mdsize; |
0f113f3e MC |
346 | if (ctx->digest->cleanup) { |
347 | ctx->digest->cleanup(ctx); | |
348 | EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED); | |
349 | } | |
3ce2fdab | 350 | OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size); |
0f113f3e MC |
351 | return ret; |
352 | } | |
351d8998 | 353 | |
cd8d1456 AP |
354 | int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t size) |
355 | { | |
356 | int ret = 0; | |
d5e5e2ff SL |
357 | OSSL_PARAM params[2]; |
358 | size_t i = 0; | |
359 | ||
360 | if (ctx->digest == NULL || ctx->digest->prov == NULL) | |
361 | goto legacy; | |
cd8d1456 | 362 | |
d5e5e2ff SL |
363 | if (ctx->digest->dfinal == NULL) { |
364 | EVPerr(EVP_F_EVP_DIGESTFINALXOF, EVP_R_FINAL_ERROR); | |
365 | return 0; | |
366 | } | |
367 | ||
4e7991b4 | 368 | params[i++] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN, &size); |
d5e5e2ff SL |
369 | params[i++] = OSSL_PARAM_construct_end(); |
370 | ||
371 | if (EVP_MD_CTX_set_params(ctx, params) > 0) | |
372 | ret = ctx->digest->dfinal(ctx->provctx, md, &size, size); | |
373 | EVP_MD_CTX_reset(ctx); | |
374 | return ret; | |
375 | ||
376 | legacy: | |
cd8d1456 AP |
377 | if (ctx->digest->flags & EVP_MD_FLAG_XOF |
378 | && size <= INT_MAX | |
379 | && ctx->digest->md_ctrl(ctx, EVP_MD_CTRL_XOF_LEN, (int)size, NULL)) { | |
380 | ret = ctx->digest->final(ctx, md); | |
cd8d1456 AP |
381 | if (ctx->digest->cleanup != NULL) { |
382 | ctx->digest->cleanup(ctx); | |
383 | EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED); | |
384 | } | |
385 | OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size); | |
386 | } else { | |
387 | EVPerr(EVP_F_EVP_DIGESTFINALXOF, EVP_R_NOT_XOF_OR_INVALID_LENGTH); | |
388 | } | |
389 | ||
390 | return ret; | |
391 | } | |
392 | ||
dbad1690 | 393 | int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in) |
0f113f3e | 394 | { |
959ed531 | 395 | EVP_MD_CTX_reset(out); |
0f113f3e MC |
396 | return EVP_MD_CTX_copy_ex(out, in); |
397 | } | |
20d2186c DSH |
398 | |
399 | int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in) | |
0f113f3e MC |
400 | { |
401 | unsigned char *tmp_buf; | |
8c8cf0d9 MC |
402 | |
403 | if (in == NULL || in->digest == NULL) { | |
0f113f3e MC |
404 | EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_INPUT_NOT_INITIALIZED); |
405 | return 0; | |
406 | } | |
8c8cf0d9 MC |
407 | |
408 | if (in->digest->prov == NULL) | |
409 | goto legacy; | |
410 | ||
411 | if (in->digest->dupctx == NULL) { | |
412 | EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_NOT_ABLE_TO_COPY_CTX); | |
413 | return 0; | |
414 | } | |
415 | ||
416 | EVP_MD_CTX_reset(out); | |
417 | if (out->fetched_digest != NULL) | |
418 | EVP_MD_meth_free(out->fetched_digest); | |
419 | *out = *in; | |
420 | /* NULL out pointers in case of error */ | |
421 | out->pctx = NULL; | |
422 | out->provctx = NULL; | |
423 | ||
424 | if (in->fetched_digest != NULL) | |
70c35fd1 | 425 | EVP_MD_up_ref(in->fetched_digest); |
8c8cf0d9 MC |
426 | |
427 | out->provctx = in->digest->dupctx(in->provctx); | |
428 | if (out->provctx == NULL) { | |
429 | EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_NOT_ABLE_TO_COPY_CTX); | |
430 | return 0; | |
431 | } | |
432 | ||
433 | /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */ | |
434 | EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX); | |
319e518a MC |
435 | #ifndef FIPS_MODE |
436 | /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */ | |
8c8cf0d9 MC |
437 | if (in->pctx != NULL) { |
438 | out->pctx = EVP_PKEY_CTX_dup(in->pctx); | |
439 | if (out->pctx == NULL) { | |
440 | EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_NOT_ABLE_TO_COPY_CTX); | |
441 | EVP_MD_CTX_reset(out); | |
442 | return 0; | |
443 | } | |
444 | } | |
319e518a | 445 | #endif |
8c8cf0d9 MC |
446 | |
447 | return 1; | |
448 | ||
449 | /* TODO(3.0): Remove legacy code below */ | |
450 | legacy: | |
319e518a | 451 | #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE) |
0f113f3e MC |
452 | /* Make sure it's safe to copy a digest context using an ENGINE */ |
453 | if (in->engine && !ENGINE_init(in->engine)) { | |
454 | EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_ENGINE_LIB); | |
455 | return 0; | |
456 | } | |
0b13e9f0 | 457 | #endif |
26188931 | 458 | |
0f113f3e MC |
459 | if (out->digest == in->digest) { |
460 | tmp_buf = out->md_data; | |
461 | EVP_MD_CTX_set_flags(out, EVP_MD_CTX_FLAG_REUSE); | |
462 | } else | |
463 | tmp_buf = NULL; | |
959ed531 | 464 | EVP_MD_CTX_reset(out); |
b4faea50 | 465 | memcpy(out, in, sizeof(*out)); |
0f113f3e | 466 | |
4803717f PY |
467 | /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */ |
468 | EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX); | |
469 | ||
6aa0ba4b RL |
470 | /* Null these variables, since they are getting fixed up |
471 | * properly below. Anything else may cause a memleak and/or | |
472 | * double free if any of the memory allocations below fail | |
473 | */ | |
474 | out->md_data = NULL; | |
475 | out->pctx = NULL; | |
476 | ||
0f113f3e MC |
477 | if (in->md_data && out->digest->ctx_size) { |
478 | if (tmp_buf) | |
479 | out->md_data = tmp_buf; | |
480 | else { | |
481 | out->md_data = OPENSSL_malloc(out->digest->ctx_size); | |
90945fa3 | 482 | if (out->md_data == NULL) { |
0f113f3e MC |
483 | EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_MALLOC_FAILURE); |
484 | return 0; | |
485 | } | |
486 | } | |
487 | memcpy(out->md_data, in->md_data, out->digest->ctx_size); | |
488 | } | |
26188931 | 489 | |
0f113f3e | 490 | out->update = in->update; |
d4575825 | 491 | |
319e518a MC |
492 | #ifndef FIPS_MODE |
493 | /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */ | |
0f113f3e MC |
494 | if (in->pctx) { |
495 | out->pctx = EVP_PKEY_CTX_dup(in->pctx); | |
496 | if (!out->pctx) { | |
959ed531 | 497 | EVP_MD_CTX_reset(out); |
0f113f3e MC |
498 | return 0; |
499 | } | |
500 | } | |
319e518a | 501 | #endif |
18327cd0 | 502 | |
0f113f3e MC |
503 | if (out->digest->copy) |
504 | return out->digest->copy(out, in); | |
3a828611 | 505 | |
0f113f3e MC |
506 | return 1; |
507 | } | |
88ce56f8 | 508 | |
9e0aad9f | 509 | int EVP_Digest(const void *data, size_t count, |
0f113f3e MC |
510 | unsigned char *md, unsigned int *size, const EVP_MD *type, |
511 | ENGINE *impl) | |
512 | { | |
bfb0641f | 513 | EVP_MD_CTX *ctx = EVP_MD_CTX_new(); |
0f113f3e | 514 | int ret; |
dbad1690 | 515 | |
74cabf3f RL |
516 | if (ctx == NULL) |
517 | return 0; | |
518 | EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT); | |
519 | ret = EVP_DigestInit_ex(ctx, type, impl) | |
520 | && EVP_DigestUpdate(ctx, data, count) | |
521 | && EVP_DigestFinal_ex(ctx, md, size); | |
bfb0641f | 522 | EVP_MD_CTX_free(ctx); |
dbad1690 | 523 | |
0f113f3e MC |
524 | return ret; |
525 | } | |
dbad1690 | 526 | |
d5e5e2ff SL |
527 | int EVP_MD_CTX_set_params(EVP_MD_CTX *ctx, const OSSL_PARAM params[]) |
528 | { | |
529 | if (ctx->digest != NULL && ctx->digest->set_params != NULL) | |
530 | return ctx->digest->set_params(ctx->provctx, params); | |
531 | return 0; | |
532 | } | |
533 | ||
4e7991b4 | 534 | int EVP_MD_CTX_get_params(EVP_MD_CTX *ctx, OSSL_PARAM params[]) |
d5e5e2ff SL |
535 | { |
536 | if (ctx->digest != NULL && ctx->digest->get_params != NULL) | |
537 | return ctx->digest->get_params(ctx->provctx, params); | |
538 | return 0; | |
539 | } | |
540 | ||
83b4a243 | 541 | /* TODO(3.0): Remove legacy code below - only used by engines & DigestSign */ |
396d5fd0 DSH |
542 | int EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void *p2) |
543 | { | |
d5e5e2ff | 544 | if (ctx->digest != NULL) { |
83b4a243 SL |
545 | if (ctx->digest->prov != NULL) { |
546 | OSSL_PARAM params[2]; | |
4e7991b4 | 547 | size_t i, n = 0; |
83b4a243 SL |
548 | |
549 | switch (cmd) { | |
550 | case EVP_MD_CTRL_XOF_LEN: | |
551 | if (ctx->digest->set_params == NULL) | |
552 | break; | |
553 | i = (size_t)p1; | |
554 | params[n++] = | |
4e7991b4 | 555 | OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN, &i); |
83b4a243 SL |
556 | params[n++] = OSSL_PARAM_construct_end(); |
557 | return ctx->digest->set_params(ctx->provctx, params); | |
558 | case EVP_MD_CTRL_MICALG: | |
559 | if (ctx->digest->get_params == NULL) | |
560 | break; | |
561 | params[n++] = | |
562 | OSSL_PARAM_construct_utf8_string(OSSL_DIGEST_PARAM_MICALG, | |
4e7991b4 | 563 | p2, p1 ? p1 : 9999); |
83b4a243 SL |
564 | params[n++] = OSSL_PARAM_construct_end(); |
565 | return ctx->digest->get_params(ctx->provctx, params); | |
566 | } | |
567 | return 0; | |
d5e5e2ff SL |
568 | } |
569 | /* legacy code */ | |
570 | if (ctx->digest->md_ctrl != NULL) { | |
571 | int ret = ctx->digest->md_ctrl(ctx, cmd, p1, p2); | |
572 | if (ret <= 0) | |
573 | return 0; | |
574 | return 1; | |
575 | } | |
396d5fd0 DSH |
576 | } |
577 | return 0; | |
578 | } | |
3653d0c2 | 579 | |
0211740f RL |
580 | static void *evp_md_from_dispatch(const OSSL_DISPATCH *fns, |
581 | OSSL_PROVIDER *prov) | |
3653d0c2 MC |
582 | { |
583 | EVP_MD *md = NULL; | |
8c8cf0d9 | 584 | int fncnt = 0; |
3653d0c2 | 585 | |
0211740f RL |
586 | /* EVP_MD_fetch() will set the legacy NID if available */ |
587 | if ((md = EVP_MD_meth_new(NID_undef, NID_undef)) == NULL) | |
3653d0c2 MC |
588 | return NULL; |
589 | ||
590 | for (; fns->function_id != 0; fns++) { | |
3653d0c2 MC |
591 | switch (fns->function_id) { |
592 | case OSSL_FUNC_DIGEST_NEWCTX: | |
d5e5e2ff SL |
593 | if (md->newctx == NULL) { |
594 | md->newctx = OSSL_get_OP_digest_newctx(fns); | |
595 | fncnt++; | |
596 | } | |
3653d0c2 MC |
597 | break; |
598 | case OSSL_FUNC_DIGEST_INIT: | |
d5e5e2ff SL |
599 | if (md->dinit == NULL) { |
600 | md->dinit = OSSL_get_OP_digest_init(fns); | |
601 | fncnt++; | |
602 | } | |
3653d0c2 | 603 | break; |
df05f2ce | 604 | case OSSL_FUNC_DIGEST_UPDATE: |
d5e5e2ff SL |
605 | if (md->dupdate == NULL) { |
606 | md->dupdate = OSSL_get_OP_digest_update(fns); | |
607 | fncnt++; | |
608 | } | |
3653d0c2 MC |
609 | break; |
610 | case OSSL_FUNC_DIGEST_FINAL: | |
d5e5e2ff SL |
611 | if (md->dfinal == NULL) { |
612 | md->dfinal = OSSL_get_OP_digest_final(fns); | |
613 | fncnt++; | |
614 | } | |
3653d0c2 MC |
615 | break; |
616 | case OSSL_FUNC_DIGEST_DIGEST: | |
d5e5e2ff SL |
617 | if (md->digest == NULL) |
618 | md->digest = OSSL_get_OP_digest_digest(fns); | |
3653d0c2 MC |
619 | /* We don't increment fnct for this as it is stand alone */ |
620 | break; | |
3653d0c2 | 621 | case OSSL_FUNC_DIGEST_FREECTX: |
d5e5e2ff SL |
622 | if (md->freectx == NULL) { |
623 | md->freectx = OSSL_get_OP_digest_freectx(fns); | |
624 | fncnt++; | |
625 | } | |
3653d0c2 | 626 | break; |
8c8cf0d9 | 627 | case OSSL_FUNC_DIGEST_DUPCTX: |
d5e5e2ff SL |
628 | if (md->dupctx == NULL) |
629 | md->dupctx = OSSL_get_OP_digest_dupctx(fns); | |
8c8cf0d9 MC |
630 | break; |
631 | case OSSL_FUNC_DIGEST_SIZE: | |
d5e5e2ff SL |
632 | if (md->size == NULL) |
633 | md->size = OSSL_get_OP_digest_size(fns); | |
8c8cf0d9 | 634 | break; |
7556b9df | 635 | case OSSL_FUNC_DIGEST_BLOCK_SIZE: |
d5e5e2ff SL |
636 | if (md->dblock_size == NULL) |
637 | md->dblock_size = OSSL_get_OP_digest_block_size(fns); | |
638 | break; | |
639 | case OSSL_FUNC_DIGEST_SET_PARAMS: | |
640 | if (md->set_params == NULL) | |
641 | md->set_params = OSSL_get_OP_digest_set_params(fns); | |
642 | break; | |
643 | case OSSL_FUNC_DIGEST_GET_PARAMS: | |
644 | if (md->get_params == NULL) | |
645 | md->get_params = OSSL_get_OP_digest_get_params(fns); | |
7556b9df | 646 | break; |
3653d0c2 | 647 | } |
8c8cf0d9 MC |
648 | } |
649 | if ((fncnt != 0 && fncnt != 5) | |
650 | || (fncnt == 0 && md->digest == NULL) | |
651 | || md->size == NULL) { | |
652 | /* | |
653 | * In order to be a consistent set of functions we either need the | |
654 | * whole set of init/update/final etc functions or none of them. | |
655 | * The "digest" function can standalone. We at least need one way to | |
656 | * generate digests. | |
657 | */ | |
658 | EVP_MD_meth_free(md); | |
659 | return NULL; | |
3653d0c2 MC |
660 | } |
661 | md->prov = prov; | |
662 | if (prov != NULL) | |
663 | ossl_provider_upref(prov); | |
664 | ||
665 | return md; | |
666 | } | |
667 | ||
70c35fd1 | 668 | static int evp_md_up_ref(void *md) |
3653d0c2 | 669 | { |
70c35fd1 | 670 | return EVP_MD_up_ref(md); |
3653d0c2 MC |
671 | } |
672 | ||
673 | static void evp_md_free(void *md) | |
674 | { | |
675 | EVP_MD_meth_free(md); | |
676 | } | |
677 | ||
678 | EVP_MD *EVP_MD_fetch(OPENSSL_CTX *ctx, const char *algorithm, | |
679 | const char *properties) | |
680 | { | |
0211740f RL |
681 | EVP_MD *md = |
682 | evp_generic_fetch(ctx, OSSL_OP_DIGEST, algorithm, properties, | |
70c35fd1 | 683 | evp_md_from_dispatch, evp_md_up_ref, |
0211740f RL |
684 | evp_md_free); |
685 | ||
686 | #ifndef FIPS_MODE | |
687 | /* TODO(3.x) get rid of the need for legacy NIDs */ | |
688 | if (md != NULL) { | |
689 | /* | |
690 | * FIPS module note: since internal fetches will be entirely | |
691 | * provider based, we know that none of its code depends on legacy | |
692 | * NIDs or any functionality that use them. | |
693 | */ | |
694 | md->type = OBJ_sn2nid(algorithm); | |
695 | } | |
696 | #endif | |
697 | ||
698 | return md; | |
3653d0c2 | 699 | } |