]>
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 | ||
3fd70262 | 89 | EVP_MD_free(ctx->fetched_digest); |
8c8cf0d9 MC |
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; | |
3fd70262 | 159 | EVP_MD_free(ctx->fetched_digest); |
8c8cf0d9 MC |
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; | |
3fd70262 | 184 | EVP_MD_free(ctx->fetched_digest); |
8c8cf0d9 | 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 | 268 | #ifndef FIPS_MODE |
dfcb5d29 MC |
269 | /* |
270 | * TODO(3.0): Temporarily no support for EVP_DigestSign* inside FIPS module | |
271 | * or when using providers. | |
272 | */ | |
864b89ce MC |
273 | if (ctx->pctx != NULL |
274 | && (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx->pctx) | |
275 | || ctx->pctx->op.sig.signature == NULL)) { | |
0f113f3e MC |
276 | int r; |
277 | r = EVP_PKEY_CTX_ctrl(ctx->pctx, -1, EVP_PKEY_OP_TYPE_SIG, | |
278 | EVP_PKEY_CTRL_DIGESTINIT, 0, ctx); | |
279 | if (r <= 0 && (r != -2)) | |
280 | return 0; | |
281 | } | |
319e518a | 282 | #endif |
0f113f3e MC |
283 | if (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) |
284 | return 1; | |
285 | return ctx->digest->init(ctx); | |
286 | } | |
d02b48c6 | 287 | |
f80921b6 | 288 | int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t count) |
0f113f3e | 289 | { |
a8274ea3 MC |
290 | if (count == 0) |
291 | return 1; | |
292 | ||
8c8cf0d9 MC |
293 | if (ctx->digest == NULL || ctx->digest->prov == NULL) |
294 | goto legacy; | |
295 | ||
296 | if (ctx->digest->dupdate == NULL) { | |
297 | EVPerr(EVP_F_EVP_DIGESTUPDATE, EVP_R_UPDATE_ERROR); | |
298 | return 0; | |
299 | } | |
300 | return ctx->digest->dupdate(ctx->provctx, data, count); | |
301 | ||
302 | /* TODO(3.0): Remove legacy code below */ | |
303 | legacy: | |
0f113f3e MC |
304 | return ctx->update(ctx, data, count); |
305 | } | |
d02b48c6 | 306 | |
dbad1690 | 307 | /* The caller can assume that this removes any secret data from the context */ |
2dc769a1 | 308 | int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size) |
0f113f3e MC |
309 | { |
310 | int ret; | |
311 | ret = EVP_DigestFinal_ex(ctx, md, size); | |
959ed531 | 312 | EVP_MD_CTX_reset(ctx); |
0f113f3e MC |
313 | return ret; |
314 | } | |
20d2186c DSH |
315 | |
316 | /* The caller can assume that this removes any secret data from the context */ | |
8c8cf0d9 | 317 | int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *isize) |
0f113f3e MC |
318 | { |
319 | int ret; | |
8c8cf0d9 | 320 | size_t size = 0; |
0ad50b4d | 321 | size_t mdsize = EVP_MD_size(ctx->digest); |
8c8cf0d9 MC |
322 | |
323 | if (ctx->digest == NULL || ctx->digest->prov == NULL) | |
324 | goto legacy; | |
54a656ef | 325 | |
8c8cf0d9 MC |
326 | if (ctx->digest->dfinal == NULL) { |
327 | EVPerr(EVP_F_EVP_DIGESTFINAL_EX, EVP_R_FINAL_ERROR); | |
328 | return 0; | |
329 | } | |
330 | ||
0ad50b4d | 331 | ret = ctx->digest->dfinal(ctx->provctx, md, &size, mdsize); |
8c8cf0d9 MC |
332 | |
333 | if (isize != NULL) { | |
334 | if (size <= UINT_MAX) { | |
335 | *isize = (int)size; | |
336 | } else { | |
337 | EVPerr(EVP_F_EVP_DIGESTFINAL_EX, EVP_R_FINAL_ERROR); | |
338 | ret = 0; | |
339 | } | |
340 | } | |
341 | ||
342 | EVP_MD_CTX_reset(ctx); | |
8c8cf0d9 MC |
343 | return ret; |
344 | ||
345 | /* TODO(3.0): Remove legacy code below */ | |
346 | legacy: | |
0ad50b4d | 347 | OPENSSL_assert(mdsize <= EVP_MAX_MD_SIZE); |
0f113f3e | 348 | ret = ctx->digest->final(ctx, md); |
8c8cf0d9 | 349 | if (isize != NULL) |
0ad50b4d | 350 | *isize = mdsize; |
0f113f3e MC |
351 | if (ctx->digest->cleanup) { |
352 | ctx->digest->cleanup(ctx); | |
353 | EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED); | |
354 | } | |
3ce2fdab | 355 | OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size); |
0f113f3e MC |
356 | return ret; |
357 | } | |
351d8998 | 358 | |
cd8d1456 AP |
359 | int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t size) |
360 | { | |
361 | int ret = 0; | |
d5e5e2ff SL |
362 | OSSL_PARAM params[2]; |
363 | size_t i = 0; | |
364 | ||
365 | if (ctx->digest == NULL || ctx->digest->prov == NULL) | |
366 | goto legacy; | |
cd8d1456 | 367 | |
d5e5e2ff SL |
368 | if (ctx->digest->dfinal == NULL) { |
369 | EVPerr(EVP_F_EVP_DIGESTFINALXOF, EVP_R_FINAL_ERROR); | |
370 | return 0; | |
371 | } | |
372 | ||
4e7991b4 | 373 | params[i++] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN, &size); |
d5e5e2ff SL |
374 | params[i++] = OSSL_PARAM_construct_end(); |
375 | ||
376 | if (EVP_MD_CTX_set_params(ctx, params) > 0) | |
377 | ret = ctx->digest->dfinal(ctx->provctx, md, &size, size); | |
378 | EVP_MD_CTX_reset(ctx); | |
379 | return ret; | |
380 | ||
381 | legacy: | |
cd8d1456 AP |
382 | if (ctx->digest->flags & EVP_MD_FLAG_XOF |
383 | && size <= INT_MAX | |
384 | && ctx->digest->md_ctrl(ctx, EVP_MD_CTRL_XOF_LEN, (int)size, NULL)) { | |
385 | ret = ctx->digest->final(ctx, md); | |
cd8d1456 AP |
386 | if (ctx->digest->cleanup != NULL) { |
387 | ctx->digest->cleanup(ctx); | |
388 | EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED); | |
389 | } | |
390 | OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size); | |
391 | } else { | |
392 | EVPerr(EVP_F_EVP_DIGESTFINALXOF, EVP_R_NOT_XOF_OR_INVALID_LENGTH); | |
393 | } | |
394 | ||
395 | return ret; | |
396 | } | |
397 | ||
dbad1690 | 398 | int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in) |
0f113f3e | 399 | { |
959ed531 | 400 | EVP_MD_CTX_reset(out); |
0f113f3e MC |
401 | return EVP_MD_CTX_copy_ex(out, in); |
402 | } | |
20d2186c DSH |
403 | |
404 | int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in) | |
0f113f3e MC |
405 | { |
406 | unsigned char *tmp_buf; | |
8c8cf0d9 MC |
407 | |
408 | if (in == NULL || in->digest == NULL) { | |
0f113f3e MC |
409 | EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_INPUT_NOT_INITIALIZED); |
410 | return 0; | |
411 | } | |
8c8cf0d9 MC |
412 | |
413 | if (in->digest->prov == NULL) | |
414 | goto legacy; | |
415 | ||
416 | if (in->digest->dupctx == NULL) { | |
417 | EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_NOT_ABLE_TO_COPY_CTX); | |
418 | return 0; | |
419 | } | |
420 | ||
421 | EVP_MD_CTX_reset(out); | |
422 | if (out->fetched_digest != NULL) | |
3fd70262 | 423 | EVP_MD_free(out->fetched_digest); |
8c8cf0d9 MC |
424 | *out = *in; |
425 | /* NULL out pointers in case of error */ | |
426 | out->pctx = NULL; | |
427 | out->provctx = NULL; | |
428 | ||
429 | if (in->fetched_digest != NULL) | |
70c35fd1 | 430 | EVP_MD_up_ref(in->fetched_digest); |
8c8cf0d9 MC |
431 | |
432 | out->provctx = in->digest->dupctx(in->provctx); | |
433 | if (out->provctx == NULL) { | |
434 | EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_NOT_ABLE_TO_COPY_CTX); | |
435 | return 0; | |
436 | } | |
437 | ||
438 | /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */ | |
439 | EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX); | |
319e518a MC |
440 | #ifndef FIPS_MODE |
441 | /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */ | |
8c8cf0d9 MC |
442 | if (in->pctx != NULL) { |
443 | out->pctx = EVP_PKEY_CTX_dup(in->pctx); | |
444 | if (out->pctx == NULL) { | |
445 | EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_NOT_ABLE_TO_COPY_CTX); | |
446 | EVP_MD_CTX_reset(out); | |
447 | return 0; | |
448 | } | |
449 | } | |
319e518a | 450 | #endif |
8c8cf0d9 MC |
451 | |
452 | return 1; | |
453 | ||
454 | /* TODO(3.0): Remove legacy code below */ | |
455 | legacy: | |
319e518a | 456 | #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE) |
0f113f3e MC |
457 | /* Make sure it's safe to copy a digest context using an ENGINE */ |
458 | if (in->engine && !ENGINE_init(in->engine)) { | |
459 | EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_ENGINE_LIB); | |
460 | return 0; | |
461 | } | |
0b13e9f0 | 462 | #endif |
26188931 | 463 | |
0f113f3e MC |
464 | if (out->digest == in->digest) { |
465 | tmp_buf = out->md_data; | |
466 | EVP_MD_CTX_set_flags(out, EVP_MD_CTX_FLAG_REUSE); | |
467 | } else | |
468 | tmp_buf = NULL; | |
959ed531 | 469 | EVP_MD_CTX_reset(out); |
b4faea50 | 470 | memcpy(out, in, sizeof(*out)); |
0f113f3e | 471 | |
4803717f PY |
472 | /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */ |
473 | EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX); | |
474 | ||
6aa0ba4b RL |
475 | /* Null these variables, since they are getting fixed up |
476 | * properly below. Anything else may cause a memleak and/or | |
477 | * double free if any of the memory allocations below fail | |
478 | */ | |
479 | out->md_data = NULL; | |
480 | out->pctx = NULL; | |
481 | ||
0f113f3e MC |
482 | if (in->md_data && out->digest->ctx_size) { |
483 | if (tmp_buf) | |
484 | out->md_data = tmp_buf; | |
485 | else { | |
486 | out->md_data = OPENSSL_malloc(out->digest->ctx_size); | |
90945fa3 | 487 | if (out->md_data == NULL) { |
0f113f3e MC |
488 | EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_MALLOC_FAILURE); |
489 | return 0; | |
490 | } | |
491 | } | |
492 | memcpy(out->md_data, in->md_data, out->digest->ctx_size); | |
493 | } | |
26188931 | 494 | |
0f113f3e | 495 | out->update = in->update; |
d4575825 | 496 | |
319e518a MC |
497 | #ifndef FIPS_MODE |
498 | /* TODO(3.0): Temporarily no support for EVP_DigestSign* in FIPS module */ | |
0f113f3e MC |
499 | if (in->pctx) { |
500 | out->pctx = EVP_PKEY_CTX_dup(in->pctx); | |
501 | if (!out->pctx) { | |
959ed531 | 502 | EVP_MD_CTX_reset(out); |
0f113f3e MC |
503 | return 0; |
504 | } | |
505 | } | |
319e518a | 506 | #endif |
18327cd0 | 507 | |
0f113f3e MC |
508 | if (out->digest->copy) |
509 | return out->digest->copy(out, in); | |
3a828611 | 510 | |
0f113f3e MC |
511 | return 1; |
512 | } | |
88ce56f8 | 513 | |
9e0aad9f | 514 | int EVP_Digest(const void *data, size_t count, |
0f113f3e MC |
515 | unsigned char *md, unsigned int *size, const EVP_MD *type, |
516 | ENGINE *impl) | |
517 | { | |
bfb0641f | 518 | EVP_MD_CTX *ctx = EVP_MD_CTX_new(); |
0f113f3e | 519 | int ret; |
dbad1690 | 520 | |
74cabf3f RL |
521 | if (ctx == NULL) |
522 | return 0; | |
523 | EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT); | |
524 | ret = EVP_DigestInit_ex(ctx, type, impl) | |
525 | && EVP_DigestUpdate(ctx, data, count) | |
526 | && EVP_DigestFinal_ex(ctx, md, size); | |
bfb0641f | 527 | EVP_MD_CTX_free(ctx); |
dbad1690 | 528 | |
0f113f3e MC |
529 | return ret; |
530 | } | |
dbad1690 | 531 | |
ae3ff60e RL |
532 | int EVP_MD_get_params(const EVP_MD *digest, OSSL_PARAM params[]) |
533 | { | |
534 | if (digest != NULL && digest->get_params != NULL) | |
535 | return digest->get_params(params); | |
536 | return 0; | |
537 | } | |
538 | ||
539 | const OSSL_PARAM *EVP_MD_gettable_params(const EVP_MD *digest) | |
540 | { | |
541 | if (digest != NULL && digest->gettable_params != NULL) | |
542 | return digest->gettable_params(); | |
543 | return NULL; | |
544 | } | |
545 | ||
d5e5e2ff SL |
546 | int EVP_MD_CTX_set_params(EVP_MD_CTX *ctx, const OSSL_PARAM params[]) |
547 | { | |
92d9d0ae RL |
548 | if (ctx->digest != NULL && ctx->digest->set_ctx_params != NULL) |
549 | return ctx->digest->set_ctx_params(ctx->provctx, params); | |
d5e5e2ff SL |
550 | return 0; |
551 | } | |
552 | ||
ae3ff60e RL |
553 | const OSSL_PARAM *EVP_MD_CTX_settable_params(const EVP_MD *digest) |
554 | { | |
555 | if (digest != NULL && digest->settable_ctx_params != NULL) | |
556 | return digest->settable_ctx_params(); | |
557 | return NULL; | |
558 | } | |
559 | ||
4e7991b4 | 560 | int EVP_MD_CTX_get_params(EVP_MD_CTX *ctx, OSSL_PARAM params[]) |
d5e5e2ff SL |
561 | { |
562 | if (ctx->digest != NULL && ctx->digest->get_params != NULL) | |
92d9d0ae | 563 | return ctx->digest->get_ctx_params(ctx->provctx, params); |
d5e5e2ff SL |
564 | return 0; |
565 | } | |
566 | ||
ae3ff60e RL |
567 | const OSSL_PARAM *EVP_MD_CTX_gettable_params(const EVP_MD *digest) |
568 | { | |
569 | if (digest != NULL && digest->gettable_ctx_params != NULL) | |
570 | return digest->gettable_ctx_params(); | |
571 | return NULL; | |
572 | } | |
573 | ||
83b4a243 | 574 | /* TODO(3.0): Remove legacy code below - only used by engines & DigestSign */ |
396d5fd0 DSH |
575 | int EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void *p2) |
576 | { | |
6a3b7c68 RL |
577 | int ret = EVP_CTRL_RET_UNSUPPORTED; |
578 | int set_params = 1; | |
579 | size_t sz; | |
580 | OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; | |
581 | ||
582 | if (ctx == NULL || ctx->digest == NULL) { | |
583 | ERR_raise(ERR_LIB_EVP, EVP_R_MESSAGE_DIGEST_IS_NULL); | |
584 | return 0; | |
585 | } | |
586 | ||
587 | if (ctx->digest->prov == NULL) | |
588 | goto legacy; | |
589 | ||
590 | switch (cmd) { | |
591 | case EVP_MD_CTRL_XOF_LEN: | |
592 | sz = (size_t)p1; | |
593 | params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN, &sz); | |
594 | break; | |
595 | case EVP_MD_CTRL_MICALG: | |
596 | set_params = 0; | |
597 | params[0] = OSSL_PARAM_construct_utf8_string(OSSL_DIGEST_PARAM_MICALG, | |
4e7991b4 | 598 | p2, p1 ? p1 : 9999); |
6a3b7c68 RL |
599 | break; |
600 | default: | |
601 | return EVP_CTRL_RET_UNSUPPORTED; | |
396d5fd0 | 602 | } |
6a3b7c68 RL |
603 | |
604 | if (set_params) | |
605 | ret = evp_do_md_ctx_setparams(ctx->digest, ctx->provctx, params); | |
606 | else | |
607 | ret = evp_do_md_ctx_getparams(ctx->digest, ctx->provctx, params); | |
608 | return ret; | |
609 | ||
610 | ||
611 | /* TODO(3.0): Remove legacy code below */ | |
612 | legacy: | |
613 | if (ctx->digest->md_ctrl == NULL) { | |
614 | ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_NOT_IMPLEMENTED); | |
615 | return 0; | |
616 | } | |
617 | ||
618 | ret = ctx->digest->md_ctrl(ctx, cmd, p1, p2); | |
619 | if (ret <= 0) | |
620 | return 0; | |
621 | return ret; | |
396d5fd0 | 622 | } |
3653d0c2 | 623 | |
3fd70262 RL |
624 | EVP_MD *evp_md_new(void) |
625 | { | |
626 | EVP_MD *md = OPENSSL_zalloc(sizeof(*md)); | |
627 | ||
628 | if (md != NULL) { | |
629 | md->lock = CRYPTO_THREAD_lock_new(); | |
630 | if (md->lock == NULL) { | |
631 | OPENSSL_free(md); | |
632 | return NULL; | |
633 | } | |
634 | md->refcnt = 1; | |
635 | } | |
636 | return md; | |
637 | } | |
638 | ||
f7c16d48 RL |
639 | static void *evp_md_from_dispatch(int name_id, |
640 | const OSSL_DISPATCH *fns, | |
3ca9d210 | 641 | OSSL_PROVIDER *prov, void *unused) |
3653d0c2 MC |
642 | { |
643 | EVP_MD *md = NULL; | |
8c8cf0d9 | 644 | int fncnt = 0; |
3653d0c2 | 645 | |
0211740f | 646 | /* EVP_MD_fetch() will set the legacy NID if available */ |
f7c16d48 | 647 | if ((md = evp_md_new()) == NULL) { |
6b9e3724 | 648 | EVPerr(0, ERR_R_MALLOC_FAILURE); |
3653d0c2 | 649 | return NULL; |
6b9e3724 | 650 | } |
f7c16d48 | 651 | md->name_id = name_id; |
3653d0c2 | 652 | |
ed71e917 | 653 | #ifndef FIPS_MODE |
f7c16d48 RL |
654 | { |
655 | /* | |
656 | * FIPS module note: since internal fetches will be entirely | |
657 | * provider based, we know that none of its code depends on legacy | |
658 | * NIDs or any functionality that use them. | |
659 | * | |
660 | * TODO(3.x) get rid of the need for legacy NIDs | |
661 | */ | |
662 | md->type = OBJ_sn2nid(evp_first_name(prov, name_id)); | |
663 | } | |
ed71e917 MC |
664 | #endif |
665 | ||
3653d0c2 | 666 | for (; fns->function_id != 0; fns++) { |
3653d0c2 MC |
667 | switch (fns->function_id) { |
668 | case OSSL_FUNC_DIGEST_NEWCTX: | |
d5e5e2ff SL |
669 | if (md->newctx == NULL) { |
670 | md->newctx = OSSL_get_OP_digest_newctx(fns); | |
671 | fncnt++; | |
672 | } | |
3653d0c2 MC |
673 | break; |
674 | case OSSL_FUNC_DIGEST_INIT: | |
d5e5e2ff SL |
675 | if (md->dinit == NULL) { |
676 | md->dinit = OSSL_get_OP_digest_init(fns); | |
677 | fncnt++; | |
678 | } | |
3653d0c2 | 679 | break; |
df05f2ce | 680 | case OSSL_FUNC_DIGEST_UPDATE: |
d5e5e2ff SL |
681 | if (md->dupdate == NULL) { |
682 | md->dupdate = OSSL_get_OP_digest_update(fns); | |
683 | fncnt++; | |
684 | } | |
3653d0c2 MC |
685 | break; |
686 | case OSSL_FUNC_DIGEST_FINAL: | |
d5e5e2ff SL |
687 | if (md->dfinal == NULL) { |
688 | md->dfinal = OSSL_get_OP_digest_final(fns); | |
689 | fncnt++; | |
690 | } | |
3653d0c2 MC |
691 | break; |
692 | case OSSL_FUNC_DIGEST_DIGEST: | |
d5e5e2ff SL |
693 | if (md->digest == NULL) |
694 | md->digest = OSSL_get_OP_digest_digest(fns); | |
3653d0c2 MC |
695 | /* We don't increment fnct for this as it is stand alone */ |
696 | break; | |
3653d0c2 | 697 | case OSSL_FUNC_DIGEST_FREECTX: |
d5e5e2ff SL |
698 | if (md->freectx == NULL) { |
699 | md->freectx = OSSL_get_OP_digest_freectx(fns); | |
700 | fncnt++; | |
701 | } | |
3653d0c2 | 702 | break; |
8c8cf0d9 | 703 | case OSSL_FUNC_DIGEST_DUPCTX: |
d5e5e2ff SL |
704 | if (md->dupctx == NULL) |
705 | md->dupctx = OSSL_get_OP_digest_dupctx(fns); | |
8c8cf0d9 | 706 | break; |
d5e5e2ff SL |
707 | case OSSL_FUNC_DIGEST_GET_PARAMS: |
708 | if (md->get_params == NULL) | |
709 | md->get_params = OSSL_get_OP_digest_get_params(fns); | |
7556b9df | 710 | break; |
92d9d0ae RL |
711 | case OSSL_FUNC_DIGEST_SET_CTX_PARAMS: |
712 | if (md->set_ctx_params == NULL) | |
713 | md->set_ctx_params = OSSL_get_OP_digest_set_ctx_params(fns); | |
6a3b7c68 | 714 | break; |
92d9d0ae RL |
715 | case OSSL_FUNC_DIGEST_GET_CTX_PARAMS: |
716 | if (md->get_ctx_params == NULL) | |
717 | md->get_ctx_params = OSSL_get_OP_digest_get_ctx_params(fns); | |
6a3b7c68 | 718 | break; |
ae3ff60e RL |
719 | case OSSL_FUNC_DIGEST_GETTABLE_PARAMS: |
720 | if (md->gettable_params == NULL) | |
721 | md->gettable_params = OSSL_get_OP_digest_gettable_params(fns); | |
722 | break; | |
723 | case OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS: | |
724 | if (md->settable_ctx_params == NULL) | |
725 | md->settable_ctx_params = | |
726 | OSSL_get_OP_digest_settable_ctx_params(fns); | |
727 | break; | |
728 | case OSSL_FUNC_DIGEST_GETTABLE_CTX_PARAMS: | |
729 | if (md->gettable_ctx_params == NULL) | |
730 | md->gettable_ctx_params = | |
731 | OSSL_get_OP_digest_gettable_ctx_params(fns); | |
732 | break; | |
3653d0c2 | 733 | } |
8c8cf0d9 MC |
734 | } |
735 | if ((fncnt != 0 && fncnt != 5) | |
6a3b7c68 | 736 | || (fncnt == 0 && md->digest == NULL)) { |
8c8cf0d9 MC |
737 | /* |
738 | * In order to be a consistent set of functions we either need the | |
739 | * whole set of init/update/final etc functions or none of them. | |
740 | * The "digest" function can standalone. We at least need one way to | |
741 | * generate digests. | |
742 | */ | |
3fd70262 | 743 | EVP_MD_free(md); |
6a3b7c68 | 744 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS); |
8c8cf0d9 | 745 | return NULL; |
3653d0c2 MC |
746 | } |
747 | md->prov = prov; | |
748 | if (prov != NULL) | |
7c95390e | 749 | ossl_provider_up_ref(prov); |
3653d0c2 MC |
750 | |
751 | return md; | |
752 | } | |
753 | ||
70c35fd1 | 754 | static int evp_md_up_ref(void *md) |
3653d0c2 | 755 | { |
70c35fd1 | 756 | return EVP_MD_up_ref(md); |
3653d0c2 MC |
757 | } |
758 | ||
759 | static void evp_md_free(void *md) | |
760 | { | |
3fd70262 | 761 | EVP_MD_free(md); |
3653d0c2 MC |
762 | } |
763 | ||
764 | EVP_MD *EVP_MD_fetch(OPENSSL_CTX *ctx, const char *algorithm, | |
765 | const char *properties) | |
766 | { | |
0211740f RL |
767 | EVP_MD *md = |
768 | evp_generic_fetch(ctx, OSSL_OP_DIGEST, algorithm, properties, | |
3ca9d210 | 769 | evp_md_from_dispatch, NULL, evp_md_up_ref, |
0211740f RL |
770 | evp_md_free); |
771 | ||
0211740f | 772 | return md; |
3653d0c2 | 773 | } |
c540f00f | 774 | |
3fd70262 RL |
775 | int EVP_MD_up_ref(EVP_MD *md) |
776 | { | |
777 | int ref = 0; | |
778 | ||
779 | CRYPTO_UP_REF(&md->refcnt, &ref, md->lock); | |
780 | return 1; | |
781 | } | |
782 | ||
783 | void EVP_MD_free(EVP_MD *md) | |
784 | { | |
785 | int i; | |
786 | ||
787 | if (md == NULL) | |
788 | return; | |
789 | ||
790 | CRYPTO_DOWN_REF(&md->refcnt, &i, md->lock); | |
791 | if (i > 0) | |
792 | return; | |
793 | ossl_provider_free(md->prov); | |
3fd70262 RL |
794 | CRYPTO_THREAD_lock_free(md->lock); |
795 | OPENSSL_free(md); | |
796 | } | |
797 | ||
c540f00f RL |
798 | void EVP_MD_do_all_ex(OPENSSL_CTX *libctx, |
799 | void (*fn)(EVP_MD *mac, void *arg), | |
800 | void *arg) | |
801 | { | |
802 | evp_generic_do_all(libctx, OSSL_OP_DIGEST, | |
803 | (void (*)(void *, void *))fn, arg, | |
3ca9d210 | 804 | evp_md_from_dispatch, NULL, evp_md_free); |
c540f00f | 805 | } |