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