/src/openssl32/crypto/evp/digest.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2024 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 | | /* We need to use some engine deprecated APIs */ |
11 | | #define OPENSSL_SUPPRESS_DEPRECATED |
12 | | |
13 | | #include <stdio.h> |
14 | | #include <openssl/objects.h> |
15 | | #include <openssl/evp.h> |
16 | | #include <openssl/ec.h> |
17 | | #ifndef FIPS_MODULE |
18 | | # include <openssl/engine.h> |
19 | | #endif |
20 | | #include <openssl/params.h> |
21 | | #include <openssl/core_names.h> |
22 | | #include "internal/cryptlib.h" |
23 | | #include "internal/provider.h" |
24 | | #include "internal/core.h" |
25 | | #include "crypto/evp.h" |
26 | | #include "evp_local.h" |
27 | | |
28 | | static void cleanup_old_md_data(EVP_MD_CTX *ctx, int force) |
29 | 420M | { |
30 | 420M | if (ctx->digest != NULL) { |
31 | 411M | if (ctx->digest->cleanup != NULL |
32 | 411M | && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_CLEANED)) |
33 | 0 | ctx->digest->cleanup(ctx); |
34 | 411M | if (ctx->md_data != NULL && ctx->digest->ctx_size > 0 |
35 | 411M | && (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_REUSE) |
36 | 0 | || force)) { |
37 | 0 | OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size); |
38 | 0 | ctx->md_data = NULL; |
39 | 0 | } |
40 | 411M | } |
41 | 420M | } |
42 | | |
43 | | void evp_md_ctx_clear_digest(EVP_MD_CTX *ctx, int force, int keep_fetched) |
44 | 11.5M | { |
45 | 11.5M | if (ctx->algctx != NULL) { |
46 | 4.78M | if (ctx->digest != NULL && ctx->digest->freectx != NULL) |
47 | 4.78M | ctx->digest->freectx(ctx->algctx); |
48 | 4.78M | ctx->algctx = NULL; |
49 | 4.78M | EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED); |
50 | 4.78M | } |
51 | | |
52 | | /* Code below to be removed when legacy support is dropped. */ |
53 | | |
54 | | /* |
55 | | * Don't assume ctx->md_data was cleaned in EVP_Digest_Final, because |
56 | | * sometimes only copies of the context are ever finalised. |
57 | | */ |
58 | 11.5M | cleanup_old_md_data(ctx, force); |
59 | 11.5M | if (force) |
60 | 62.9k | ctx->digest = NULL; |
61 | | |
62 | 11.5M | #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE) |
63 | 11.5M | ENGINE_finish(ctx->engine); |
64 | 11.5M | ctx->engine = NULL; |
65 | 11.5M | #endif |
66 | | |
67 | | /* Non legacy code, this has to be later than the ctx->digest cleaning */ |
68 | 11.5M | if (!keep_fetched) { |
69 | 8.45M | EVP_MD_free(ctx->fetched_digest); |
70 | 8.45M | ctx->fetched_digest = NULL; |
71 | 8.45M | ctx->reqdigest = NULL; |
72 | 8.45M | } |
73 | 11.5M | } |
74 | | |
75 | | static int evp_md_ctx_reset_ex(EVP_MD_CTX *ctx, int keep_fetched) |
76 | 15.0M | { |
77 | 15.0M | if (ctx == NULL) |
78 | 3.60M | return 1; |
79 | | |
80 | 11.4M | #ifndef FIPS_MODULE |
81 | | /* |
82 | | * pctx should be freed by the user of EVP_MD_CTX |
83 | | * if EVP_MD_CTX_FLAG_KEEP_PKEY_CTX is set |
84 | | */ |
85 | 11.4M | if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX)) { |
86 | 11.4M | EVP_PKEY_CTX_free(ctx->pctx); |
87 | 11.4M | ctx->pctx = NULL; |
88 | 11.4M | } |
89 | 11.4M | #endif |
90 | | |
91 | 11.4M | evp_md_ctx_clear_digest(ctx, 0, keep_fetched); |
92 | 11.4M | if (!keep_fetched) |
93 | 8.39M | OPENSSL_cleanse(ctx, sizeof(*ctx)); |
94 | | |
95 | 11.4M | return 1; |
96 | 15.0M | } |
97 | | |
98 | | /* This call frees resources associated with the context */ |
99 | | int EVP_MD_CTX_reset(EVP_MD_CTX *ctx) |
100 | 12.0M | { |
101 | 12.0M | return evp_md_ctx_reset_ex(ctx, 0); |
102 | 12.0M | } |
103 | | |
104 | | #ifndef FIPS_MODULE |
105 | | EVP_MD_CTX *evp_md_ctx_new_ex(EVP_PKEY *pkey, const ASN1_OCTET_STRING *id, |
106 | | OSSL_LIB_CTX *libctx, const char *propq) |
107 | 8.66k | { |
108 | 8.66k | EVP_MD_CTX *ctx; |
109 | 8.66k | EVP_PKEY_CTX *pctx = NULL; |
110 | | |
111 | 8.66k | if ((ctx = EVP_MD_CTX_new()) == NULL |
112 | 8.66k | || (pctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq)) == NULL) { |
113 | 258 | ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB); |
114 | 258 | goto err; |
115 | 258 | } |
116 | | |
117 | 8.40k | if (id != NULL && EVP_PKEY_CTX_set1_id(pctx, id->data, id->length) <= 0) |
118 | 0 | goto err; |
119 | | |
120 | 8.40k | EVP_MD_CTX_set_pkey_ctx(ctx, pctx); |
121 | 8.40k | return ctx; |
122 | | |
123 | 258 | err: |
124 | 258 | EVP_PKEY_CTX_free(pctx); |
125 | 258 | EVP_MD_CTX_free(ctx); |
126 | 258 | return NULL; |
127 | 8.40k | } |
128 | | #endif |
129 | | |
130 | | EVP_MD_CTX *EVP_MD_CTX_new(void) |
131 | 4.55M | { |
132 | 4.55M | return OPENSSL_zalloc(sizeof(EVP_MD_CTX)); |
133 | 4.55M | } |
134 | | |
135 | | void EVP_MD_CTX_free(EVP_MD_CTX *ctx) |
136 | 5.82M | { |
137 | 5.82M | if (ctx == NULL) |
138 | 1.26M | return; |
139 | | |
140 | 4.55M | EVP_MD_CTX_reset(ctx); |
141 | 4.55M | OPENSSL_free(ctx); |
142 | 4.55M | } |
143 | | |
144 | | int evp_md_ctx_free_algctx(EVP_MD_CTX *ctx) |
145 | 1.90M | { |
146 | 1.90M | if (ctx->algctx != NULL) { |
147 | 70.1k | if (!ossl_assert(ctx->digest != NULL)) { |
148 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
149 | 0 | return 0; |
150 | 0 | } |
151 | 70.1k | if (ctx->digest->freectx != NULL) |
152 | 70.1k | ctx->digest->freectx(ctx->algctx); |
153 | 70.1k | ctx->algctx = NULL; |
154 | 70.1k | } |
155 | 1.90M | return 1; |
156 | 1.90M | } |
157 | | |
158 | | static int evp_md_init_internal(EVP_MD_CTX *ctx, const EVP_MD *type, |
159 | | const OSSL_PARAM params[], ENGINE *impl) |
160 | 2.10M | { |
161 | 2.10M | #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE) |
162 | 2.10M | ENGINE *tmpimpl = NULL; |
163 | 2.10M | #endif |
164 | | |
165 | 2.10M | #if !defined(FIPS_MODULE) |
166 | 2.10M | if (ctx->pctx != NULL |
167 | 2.10M | && EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx->pctx) |
168 | 2.10M | && ctx->pctx->op.sig.algctx != NULL) { |
169 | | /* |
170 | | * Prior to OpenSSL 3.0 calling EVP_DigestInit_ex() on an mdctx |
171 | | * previously initialised with EVP_DigestSignInit() would retain |
172 | | * information about the key, and re-initialise for another sign |
173 | | * operation. So in that case we redirect to EVP_DigestSignInit() |
174 | | */ |
175 | 0 | if (ctx->pctx->operation == EVP_PKEY_OP_SIGNCTX) |
176 | 0 | return EVP_DigestSignInit(ctx, NULL, type, impl, NULL); |
177 | 0 | if (ctx->pctx->operation == EVP_PKEY_OP_VERIFYCTX) |
178 | 0 | return EVP_DigestVerifyInit(ctx, NULL, type, impl, NULL); |
179 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR); |
180 | 0 | return 0; |
181 | 0 | } |
182 | 2.10M | #endif |
183 | | |
184 | 2.10M | EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_CLEANED |
185 | 2.10M | | EVP_MD_CTX_FLAG_FINALISED); |
186 | | |
187 | 2.10M | if (type != NULL) { |
188 | 2.10M | ctx->reqdigest = type; |
189 | 2.10M | } else { |
190 | 0 | if (ctx->digest == NULL) { |
191 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_NO_DIGEST_SET); |
192 | 0 | return 0; |
193 | 0 | } |
194 | 0 | type = ctx->digest; |
195 | 0 | } |
196 | | |
197 | | /* Code below to be removed when legacy support is dropped. */ |
198 | 2.10M | #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE) |
199 | | /* |
200 | | * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so |
201 | | * this context may already have an ENGINE! Try to avoid releasing the |
202 | | * previous handle, re-querying for an ENGINE, and having a |
203 | | * reinitialisation, when it may all be unnecessary. |
204 | | */ |
205 | 2.10M | if (ctx->engine != NULL |
206 | 2.10M | && ctx->digest != NULL |
207 | 2.10M | && type->type == ctx->digest->type) |
208 | 0 | goto skip_to_init; |
209 | | |
210 | | /* |
211 | | * Ensure an ENGINE left lying around from last time is cleared (the |
212 | | * previous check attempted to avoid this if the same ENGINE and |
213 | | * EVP_MD could be used). |
214 | | */ |
215 | 2.10M | ENGINE_finish(ctx->engine); |
216 | 2.10M | ctx->engine = NULL; |
217 | | |
218 | 2.10M | if (impl == NULL) |
219 | 2.10M | tmpimpl = ENGINE_get_digest_engine(type->type); |
220 | 2.10M | #endif |
221 | | |
222 | | /* |
223 | | * If there are engines involved or EVP_MD_CTX_FLAG_NO_INIT is set then we |
224 | | * should use legacy handling for now. |
225 | | */ |
226 | 2.10M | if (impl != NULL |
227 | 2.10M | #if !defined(OPENSSL_NO_ENGINE) |
228 | 2.10M | || ctx->engine != NULL |
229 | 2.10M | # if !defined(FIPS_MODULE) |
230 | 2.10M | || tmpimpl != NULL |
231 | 2.10M | # endif |
232 | 2.10M | #endif |
233 | 2.10M | || (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0 |
234 | 2.10M | || (type != NULL && type->origin == EVP_ORIG_METH) |
235 | 2.10M | || (type == NULL && ctx->digest != NULL |
236 | 2.10M | && ctx->digest->origin == EVP_ORIG_METH)) { |
237 | | /* If we were using provided hash before, cleanup algctx */ |
238 | 0 | if (!evp_md_ctx_free_algctx(ctx)) |
239 | 0 | return 0; |
240 | 0 | if (ctx->digest == ctx->fetched_digest) |
241 | 0 | ctx->digest = NULL; |
242 | 0 | EVP_MD_free(ctx->fetched_digest); |
243 | 0 | ctx->fetched_digest = NULL; |
244 | 0 | goto legacy; |
245 | 0 | } |
246 | | |
247 | 2.10M | cleanup_old_md_data(ctx, 1); |
248 | | |
249 | | /* Start of non-legacy code below */ |
250 | 2.10M | if (ctx->digest == type) { |
251 | 1.19M | if (!ossl_assert(type->prov != NULL)) { |
252 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
253 | 0 | return 0; |
254 | 0 | } |
255 | 1.19M | } else { |
256 | 904k | if (!evp_md_ctx_free_algctx(ctx)) |
257 | 0 | return 0; |
258 | 904k | } |
259 | | |
260 | 2.10M | if (type->prov == NULL) { |
261 | | #ifdef FIPS_MODULE |
262 | | /* We only do explicit fetches inside the FIPS module */ |
263 | | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
264 | | return 0; |
265 | | #else |
266 | | /* The NULL digest is a special case */ |
267 | 145 | EVP_MD *provmd = EVP_MD_fetch(NULL, |
268 | 145 | type->type != NID_undef ? OBJ_nid2sn(type->type) |
269 | 145 | : "NULL", ""); |
270 | | |
271 | 145 | if (provmd == NULL) { |
272 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
273 | 0 | return 0; |
274 | 0 | } |
275 | 145 | type = provmd; |
276 | 145 | EVP_MD_free(ctx->fetched_digest); |
277 | 145 | ctx->fetched_digest = provmd; |
278 | 145 | #endif |
279 | 145 | } |
280 | | |
281 | 2.10M | if (type->prov != NULL && ctx->fetched_digest != type) { |
282 | 904k | if (!EVP_MD_up_ref((EVP_MD *)type)) { |
283 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
284 | 0 | return 0; |
285 | 0 | } |
286 | 904k | EVP_MD_free(ctx->fetched_digest); |
287 | 904k | ctx->fetched_digest = (EVP_MD *)type; |
288 | 904k | } |
289 | 2.10M | ctx->digest = type; |
290 | 2.10M | if (ctx->algctx == NULL) { |
291 | 904k | ctx->algctx = ctx->digest->newctx(ossl_provider_ctx(type->prov)); |
292 | 904k | if (ctx->algctx == NULL) { |
293 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
294 | 0 | return 0; |
295 | 0 | } |
296 | 904k | } |
297 | | |
298 | 2.10M | if (ctx->digest->dinit == NULL) { |
299 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
300 | 0 | return 0; |
301 | 0 | } |
302 | | |
303 | 2.10M | return ctx->digest->dinit(ctx->algctx, params); |
304 | | |
305 | | /* Code below to be removed when legacy support is dropped. */ |
306 | 0 | legacy: |
307 | |
|
308 | 0 | #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE) |
309 | 0 | if (type) { |
310 | 0 | if (impl != NULL) { |
311 | 0 | if (!ENGINE_init(impl)) { |
312 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
313 | 0 | return 0; |
314 | 0 | } |
315 | 0 | } else { |
316 | | /* Ask if an ENGINE is reserved for this job */ |
317 | 0 | impl = tmpimpl; |
318 | 0 | } |
319 | 0 | if (impl != NULL) { |
320 | | /* There's an ENGINE for this job ... (apparently) */ |
321 | 0 | const EVP_MD *d = ENGINE_get_digest(impl, type->type); |
322 | |
|
323 | 0 | if (d == NULL) { |
324 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
325 | 0 | ENGINE_finish(impl); |
326 | 0 | return 0; |
327 | 0 | } |
328 | | /* We'll use the ENGINE's private digest definition */ |
329 | 0 | type = d; |
330 | | /* |
331 | | * Store the ENGINE functional reference so we know 'type' came |
332 | | * from an ENGINE and we need to release it when done. |
333 | | */ |
334 | 0 | ctx->engine = impl; |
335 | 0 | } else |
336 | 0 | ctx->engine = NULL; |
337 | 0 | } |
338 | 0 | #endif |
339 | 0 | if (ctx->digest != type) { |
340 | 0 | cleanup_old_md_data(ctx, 1); |
341 | |
|
342 | 0 | ctx->digest = type; |
343 | 0 | if (!(ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) && type->ctx_size) { |
344 | 0 | ctx->update = type->update; |
345 | 0 | ctx->md_data = OPENSSL_zalloc(type->ctx_size); |
346 | 0 | if (ctx->md_data == NULL) |
347 | 0 | return 0; |
348 | 0 | } |
349 | 0 | } |
350 | 0 | #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE) |
351 | 0 | skip_to_init: |
352 | 0 | #endif |
353 | 0 | #ifndef FIPS_MODULE |
354 | 0 | if (ctx->pctx != NULL |
355 | 0 | && (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx->pctx) |
356 | 0 | || ctx->pctx->op.sig.signature == NULL)) { |
357 | 0 | int r; |
358 | 0 | r = EVP_PKEY_CTX_ctrl(ctx->pctx, -1, EVP_PKEY_OP_TYPE_SIG, |
359 | 0 | EVP_PKEY_CTRL_DIGESTINIT, 0, ctx); |
360 | 0 | if (r <= 0 && (r != -2)) |
361 | 0 | return 0; |
362 | 0 | } |
363 | 0 | #endif |
364 | 0 | if (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) |
365 | 0 | return 1; |
366 | 0 | return ctx->digest->init(ctx); |
367 | 0 | } |
368 | | |
369 | | int EVP_DigestInit_ex2(EVP_MD_CTX *ctx, const EVP_MD *type, |
370 | | const OSSL_PARAM params[]) |
371 | 403M | { |
372 | 403M | return evp_md_init_internal(ctx, type, params, NULL); |
373 | 403M | } |
374 | | |
375 | | int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type) |
376 | 0 | { |
377 | 0 | EVP_MD_CTX_reset(ctx); |
378 | 0 | return evp_md_init_internal(ctx, type, NULL, NULL); |
379 | 0 | } |
380 | | |
381 | | int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl) |
382 | 4.71M | { |
383 | 4.71M | return evp_md_init_internal(ctx, type, NULL, impl); |
384 | 4.71M | } |
385 | | |
386 | | int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t count) |
387 | 2.70M | { |
388 | 2.70M | if (count == 0) |
389 | 9.41k | return 1; |
390 | | |
391 | 2.69M | if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISED) != 0) { |
392 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR); |
393 | 0 | return 0; |
394 | 0 | } |
395 | | |
396 | 2.69M | if (ctx->pctx != NULL |
397 | 2.69M | && EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx->pctx) |
398 | 2.69M | && ctx->pctx->op.sig.algctx != NULL) { |
399 | | /* |
400 | | * Prior to OpenSSL 3.0 EVP_DigestSignUpdate() and |
401 | | * EVP_DigestVerifyUpdate() were just macros for EVP_DigestUpdate(). |
402 | | * Some code calls EVP_DigestUpdate() directly even when initialised |
403 | | * with EVP_DigestSignInit_ex() or |
404 | | * EVP_DigestVerifyInit_ex(), so we detect that and redirect to |
405 | | * the correct EVP_Digest*Update() function |
406 | | */ |
407 | 0 | if (ctx->pctx->operation == EVP_PKEY_OP_SIGNCTX) |
408 | 0 | return EVP_DigestSignUpdate(ctx, data, count); |
409 | 0 | if (ctx->pctx->operation == EVP_PKEY_OP_VERIFYCTX) |
410 | 0 | return EVP_DigestVerifyUpdate(ctx, data, count); |
411 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR); |
412 | 0 | return 0; |
413 | 0 | } |
414 | | |
415 | 2.69M | if (ctx->digest == NULL |
416 | 2.69M | || ctx->digest->prov == NULL |
417 | 2.69M | || (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0) |
418 | 0 | goto legacy; |
419 | | |
420 | 2.69M | if (ctx->digest->dupdate == NULL) { |
421 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR); |
422 | 0 | return 0; |
423 | 0 | } |
424 | 2.69M | return ctx->digest->dupdate(ctx->algctx, data, count); |
425 | | |
426 | | /* Code below to be removed when legacy support is dropped. */ |
427 | 0 | legacy: |
428 | 0 | return ctx->update != NULL ? ctx->update(ctx, data, count) : 0; |
429 | 2.69M | } |
430 | | |
431 | | /* The caller can assume that this removes any secret data from the context */ |
432 | | int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size) |
433 | 54.9k | { |
434 | 54.9k | int ret; |
435 | 54.9k | ret = EVP_DigestFinal_ex(ctx, md, size); |
436 | 54.9k | EVP_MD_CTX_reset(ctx); |
437 | 54.9k | return ret; |
438 | 54.9k | } |
439 | | |
440 | | /* The caller can assume that this removes any secret data from the context */ |
441 | | int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *isize) |
442 | 1.59M | { |
443 | 1.59M | int ret, sz; |
444 | 1.59M | size_t size = 0; |
445 | 1.59M | size_t mdsize = 0; |
446 | | |
447 | 1.59M | if (ctx->digest == NULL) |
448 | 0 | return 0; |
449 | | |
450 | 1.59M | sz = EVP_MD_get_size(ctx->digest); |
451 | 1.59M | if (sz < 0) |
452 | 0 | return 0; |
453 | 1.59M | mdsize = sz; |
454 | 1.59M | if (ctx->digest->prov == NULL) |
455 | 0 | goto legacy; |
456 | | |
457 | 1.59M | if (ctx->digest->gettable_ctx_params != NULL) { |
458 | 0 | OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END }; |
459 | |
|
460 | 0 | params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_SIZE, |
461 | 0 | &mdsize); |
462 | 0 | if (!EVP_MD_CTX_get_params(ctx, params)) |
463 | 0 | return 0; |
464 | 0 | } |
465 | | |
466 | 1.59M | if (ctx->digest->dfinal == NULL) { |
467 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR); |
468 | 0 | return 0; |
469 | 0 | } |
470 | | |
471 | 1.59M | if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISED) != 0) { |
472 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR); |
473 | 0 | return 0; |
474 | 0 | } |
475 | | |
476 | 1.59M | ret = ctx->digest->dfinal(ctx->algctx, md, &size, mdsize); |
477 | | |
478 | 1.59M | ctx->flags |= EVP_MD_CTX_FLAG_FINALISED; |
479 | | |
480 | 1.59M | if (isize != NULL) { |
481 | 1.18M | if (size <= UINT_MAX) { |
482 | 1.18M | *isize = (unsigned int)size; |
483 | 1.18M | } else { |
484 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR); |
485 | 0 | ret = 0; |
486 | 0 | } |
487 | 1.18M | } |
488 | | |
489 | 1.59M | return ret; |
490 | | |
491 | | /* Code below to be removed when legacy support is dropped. */ |
492 | 0 | legacy: |
493 | 0 | OPENSSL_assert(mdsize <= EVP_MAX_MD_SIZE); |
494 | 0 | ret = ctx->digest->final(ctx, md); |
495 | 0 | if (isize != NULL) |
496 | 0 | *isize = mdsize; |
497 | 0 | if (ctx->digest->cleanup) { |
498 | 0 | ctx->digest->cleanup(ctx); |
499 | 0 | EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED); |
500 | 0 | } |
501 | 0 | OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size); |
502 | 0 | return ret; |
503 | 1.59M | } |
504 | | |
505 | | int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t size) |
506 | 67 | { |
507 | 67 | int ret = 0; |
508 | 67 | OSSL_PARAM params[2]; |
509 | 67 | size_t i = 0; |
510 | | |
511 | 67 | if (ctx->digest == NULL) { |
512 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_NULL_ALGORITHM); |
513 | 0 | return 0; |
514 | 0 | } |
515 | | |
516 | 67 | if (ctx->digest->prov == NULL) |
517 | 0 | goto legacy; |
518 | | |
519 | 67 | if (ctx->digest->dfinal == NULL) { |
520 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR); |
521 | 0 | return 0; |
522 | 0 | } |
523 | | |
524 | 67 | if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISED) != 0) { |
525 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR); |
526 | 0 | return 0; |
527 | 0 | } |
528 | | |
529 | 67 | params[i++] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN, &size); |
530 | 67 | params[i++] = OSSL_PARAM_construct_end(); |
531 | | |
532 | 67 | if (EVP_MD_CTX_set_params(ctx, params) > 0) |
533 | 67 | ret = ctx->digest->dfinal(ctx->algctx, md, &size, size); |
534 | | |
535 | 67 | ctx->flags |= EVP_MD_CTX_FLAG_FINALISED; |
536 | | |
537 | 67 | return ret; |
538 | | |
539 | 0 | legacy: |
540 | 0 | if (ctx->digest->flags & EVP_MD_FLAG_XOF |
541 | 0 | && size <= INT_MAX |
542 | 0 | && ctx->digest->md_ctrl(ctx, EVP_MD_CTRL_XOF_LEN, (int)size, NULL)) { |
543 | 0 | ret = ctx->digest->final(ctx, md); |
544 | 0 | if (ctx->digest->cleanup != NULL) { |
545 | 0 | ctx->digest->cleanup(ctx); |
546 | 0 | EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED); |
547 | 0 | } |
548 | 0 | OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size); |
549 | 0 | } else { |
550 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_NOT_XOF_OR_INVALID_LENGTH); |
551 | 0 | } |
552 | |
|
553 | 0 | return ret; |
554 | 67 | } |
555 | | |
556 | | EVP_MD_CTX *EVP_MD_CTX_dup(const EVP_MD_CTX *in) |
557 | 0 | { |
558 | 0 | EVP_MD_CTX *out = EVP_MD_CTX_new(); |
559 | |
|
560 | 0 | if (out != NULL && !EVP_MD_CTX_copy_ex(out, in)) { |
561 | 0 | EVP_MD_CTX_free(out); |
562 | 0 | out = NULL; |
563 | 0 | } |
564 | 0 | return out; |
565 | 0 | } |
566 | | |
567 | | int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in) |
568 | 175k | { |
569 | 175k | EVP_MD_CTX_reset(out); |
570 | 175k | return EVP_MD_CTX_copy_ex(out, in); |
571 | 175k | } |
572 | | |
573 | | int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in) |
574 | 1.58M | { |
575 | 1.58M | int digest_change = 0; |
576 | 1.58M | unsigned char *tmp_buf; |
577 | | |
578 | 1.58M | if (in == NULL) { |
579 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER); |
580 | 0 | return 0; |
581 | 0 | } |
582 | | |
583 | 1.58M | if (in->digest == NULL) { |
584 | | /* copying uninitialized digest context */ |
585 | 0 | EVP_MD_CTX_reset(out); |
586 | 0 | if (out->fetched_digest != NULL) |
587 | 0 | EVP_MD_free(out->fetched_digest); |
588 | 0 | *out = *in; |
589 | 0 | goto clone_pkey; |
590 | 0 | } |
591 | | |
592 | 1.58M | if (in->digest->prov == NULL |
593 | 1.58M | || (in->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0) |
594 | 0 | goto legacy; |
595 | | |
596 | 1.58M | if (in->digest->dupctx == NULL) { |
597 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX); |
598 | 0 | return 0; |
599 | 0 | } |
600 | | |
601 | 1.58M | evp_md_ctx_reset_ex(out, 1); |
602 | 1.58M | digest_change = (out->fetched_digest != in->fetched_digest); |
603 | 1.58M | if (digest_change && out->fetched_digest != NULL) |
604 | 0 | EVP_MD_free(out->fetched_digest); |
605 | 1.58M | *out = *in; |
606 | | /* NULL out pointers in case of error */ |
607 | 1.58M | out->pctx = NULL; |
608 | 1.58M | out->algctx = NULL; |
609 | | |
610 | 1.58M | if (digest_change && in->fetched_digest != NULL) |
611 | 1.08M | EVP_MD_up_ref(in->fetched_digest); |
612 | | |
613 | 1.58M | if (in->algctx != NULL) { |
614 | 1.52M | out->algctx = in->digest->dupctx(in->algctx); |
615 | 1.52M | if (out->algctx == NULL) { |
616 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX); |
617 | 0 | return 0; |
618 | 0 | } |
619 | 1.52M | } |
620 | | |
621 | 1.58M | clone_pkey: |
622 | | /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */ |
623 | 1.58M | EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX); |
624 | 1.58M | #ifndef FIPS_MODULE |
625 | 1.58M | if (in->pctx != NULL) { |
626 | 52.2k | out->pctx = EVP_PKEY_CTX_dup(in->pctx); |
627 | 52.2k | if (out->pctx == NULL) { |
628 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX); |
629 | 0 | EVP_MD_CTX_reset(out); |
630 | 0 | return 0; |
631 | 0 | } |
632 | 52.2k | } |
633 | 1.58M | #endif |
634 | | |
635 | 1.58M | return 1; |
636 | | |
637 | | /* Code below to be removed when legacy support is dropped. */ |
638 | 0 | legacy: |
639 | 0 | #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE) |
640 | | /* Make sure it's safe to copy a digest context using an ENGINE */ |
641 | 0 | if (in->engine && !ENGINE_init(in->engine)) { |
642 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB); |
643 | 0 | return 0; |
644 | 0 | } |
645 | 0 | #endif |
646 | | |
647 | 0 | if (out->digest == in->digest) { |
648 | 0 | tmp_buf = out->md_data; |
649 | 0 | EVP_MD_CTX_set_flags(out, EVP_MD_CTX_FLAG_REUSE); |
650 | 0 | } else |
651 | 0 | tmp_buf = NULL; |
652 | 0 | EVP_MD_CTX_reset(out); |
653 | 0 | memcpy(out, in, sizeof(*out)); |
654 | | |
655 | | /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */ |
656 | 0 | EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX); |
657 | | |
658 | | /* Null these variables, since they are getting fixed up |
659 | | * properly below. Anything else may cause a memleak and/or |
660 | | * double free if any of the memory allocations below fail |
661 | | */ |
662 | 0 | out->md_data = NULL; |
663 | 0 | out->pctx = NULL; |
664 | |
|
665 | 0 | if (in->md_data && out->digest->ctx_size) { |
666 | 0 | if (tmp_buf) |
667 | 0 | out->md_data = tmp_buf; |
668 | 0 | else { |
669 | 0 | out->md_data = OPENSSL_malloc(out->digest->ctx_size); |
670 | 0 | if (out->md_data == NULL) |
671 | 0 | return 0; |
672 | 0 | } |
673 | 0 | memcpy(out->md_data, in->md_data, out->digest->ctx_size); |
674 | 0 | } |
675 | | |
676 | 0 | out->update = in->update; |
677 | |
|
678 | 0 | #ifndef FIPS_MODULE |
679 | 0 | if (in->pctx) { |
680 | 0 | out->pctx = EVP_PKEY_CTX_dup(in->pctx); |
681 | 0 | if (!out->pctx) { |
682 | 0 | EVP_MD_CTX_reset(out); |
683 | 0 | return 0; |
684 | 0 | } |
685 | 0 | } |
686 | 0 | #endif |
687 | | |
688 | 0 | if (out->digest->copy) |
689 | 0 | return out->digest->copy(out, in); |
690 | | |
691 | 0 | return 1; |
692 | 0 | } |
693 | | |
694 | | int EVP_Digest(const void *data, size_t count, |
695 | | unsigned char *md, unsigned int *size, const EVP_MD *type, |
696 | | ENGINE *impl) |
697 | 287k | { |
698 | 287k | EVP_MD_CTX *ctx = EVP_MD_CTX_new(); |
699 | 287k | int ret; |
700 | | |
701 | 287k | if (ctx == NULL) |
702 | 0 | return 0; |
703 | 287k | EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT); |
704 | 287k | ret = EVP_DigestInit_ex(ctx, type, impl) |
705 | 287k | && EVP_DigestUpdate(ctx, data, count) |
706 | 287k | && EVP_DigestFinal_ex(ctx, md, size); |
707 | 287k | EVP_MD_CTX_free(ctx); |
708 | | |
709 | 287k | return ret; |
710 | 287k | } |
711 | | |
712 | | int EVP_Q_digest(OSSL_LIB_CTX *libctx, const char *name, const char *propq, |
713 | | const void *data, size_t datalen, |
714 | | unsigned char *md, size_t *mdlen) |
715 | 0 | { |
716 | 0 | EVP_MD *digest = EVP_MD_fetch(libctx, name, propq); |
717 | 0 | unsigned int temp = 0; |
718 | 0 | int ret = 0; |
719 | |
|
720 | 0 | if (digest != NULL) { |
721 | 0 | ret = EVP_Digest(data, datalen, md, &temp, digest, NULL); |
722 | 0 | EVP_MD_free(digest); |
723 | 0 | } |
724 | 0 | if (mdlen != NULL) |
725 | 0 | *mdlen = temp; |
726 | 0 | return ret; |
727 | 0 | } |
728 | | |
729 | | int EVP_MD_get_params(const EVP_MD *digest, OSSL_PARAM params[]) |
730 | 0 | { |
731 | 0 | if (digest != NULL && digest->get_params != NULL) |
732 | 0 | return digest->get_params(params); |
733 | 0 | return 0; |
734 | 0 | } |
735 | | |
736 | | const OSSL_PARAM *EVP_MD_gettable_params(const EVP_MD *digest) |
737 | 0 | { |
738 | 0 | if (digest != NULL && digest->gettable_params != NULL) |
739 | 0 | return digest->gettable_params( |
740 | 0 | ossl_provider_ctx(EVP_MD_get0_provider(digest))); |
741 | 0 | return NULL; |
742 | 0 | } |
743 | | |
744 | | int EVP_MD_CTX_set_params(EVP_MD_CTX *ctx, const OSSL_PARAM params[]) |
745 | 160M | { |
746 | 160M | EVP_PKEY_CTX *pctx = ctx->pctx; |
747 | | |
748 | | /* If we have a pctx then we should try that first */ |
749 | 160M | if (pctx != NULL |
750 | 160M | && (pctx->operation == EVP_PKEY_OP_VERIFYCTX |
751 | 0 | || pctx->operation == EVP_PKEY_OP_SIGNCTX) |
752 | 160M | && pctx->op.sig.algctx != NULL |
753 | 160M | && pctx->op.sig.signature->set_ctx_md_params != NULL) |
754 | 0 | return pctx->op.sig.signature->set_ctx_md_params(pctx->op.sig.algctx, |
755 | 0 | params); |
756 | | |
757 | 160M | if (ctx->digest != NULL && ctx->digest->set_ctx_params != NULL) |
758 | 160M | return ctx->digest->set_ctx_params(ctx->algctx, params); |
759 | | |
760 | 14 | return 0; |
761 | 160M | } |
762 | | |
763 | | const OSSL_PARAM *EVP_MD_settable_ctx_params(const EVP_MD *md) |
764 | 14 | { |
765 | 14 | void *provctx; |
766 | | |
767 | 14 | if (md != NULL && md->settable_ctx_params != NULL) { |
768 | 13 | provctx = ossl_provider_ctx(EVP_MD_get0_provider(md)); |
769 | 13 | return md->settable_ctx_params(NULL, provctx); |
770 | 13 | } |
771 | 1 | return NULL; |
772 | 14 | } |
773 | | |
774 | | const OSSL_PARAM *EVP_MD_CTX_settable_params(EVP_MD_CTX *ctx) |
775 | 0 | { |
776 | 0 | EVP_PKEY_CTX *pctx; |
777 | 0 | void *alg; |
778 | |
|
779 | 0 | if (ctx == NULL) |
780 | 0 | return NULL; |
781 | | |
782 | | /* If we have a pctx then we should try that first */ |
783 | 0 | pctx = ctx->pctx; |
784 | 0 | if (pctx != NULL |
785 | 0 | && (pctx->operation == EVP_PKEY_OP_VERIFYCTX |
786 | 0 | || pctx->operation == EVP_PKEY_OP_SIGNCTX) |
787 | 0 | && pctx->op.sig.algctx != NULL |
788 | 0 | && pctx->op.sig.signature->settable_ctx_md_params != NULL) |
789 | 0 | return pctx->op.sig.signature->settable_ctx_md_params( |
790 | 0 | pctx->op.sig.algctx); |
791 | | |
792 | 0 | if (ctx->digest != NULL && ctx->digest->settable_ctx_params != NULL) { |
793 | 0 | alg = ossl_provider_ctx(EVP_MD_get0_provider(ctx->digest)); |
794 | 0 | return ctx->digest->settable_ctx_params(ctx->algctx, alg); |
795 | 0 | } |
796 | | |
797 | 0 | return NULL; |
798 | 0 | } |
799 | | |
800 | | int EVP_MD_CTX_get_params(EVP_MD_CTX *ctx, OSSL_PARAM params[]) |
801 | 28.2k | { |
802 | 28.2k | EVP_PKEY_CTX *pctx = ctx->pctx; |
803 | | |
804 | | /* If we have a pctx then we should try that first */ |
805 | 28.2k | if (pctx != NULL |
806 | 28.2k | && (pctx->operation == EVP_PKEY_OP_VERIFYCTX |
807 | 0 | || pctx->operation == EVP_PKEY_OP_SIGNCTX) |
808 | 28.2k | && pctx->op.sig.algctx != NULL |
809 | 28.2k | && pctx->op.sig.signature->get_ctx_md_params != NULL) |
810 | 0 | return pctx->op.sig.signature->get_ctx_md_params(pctx->op.sig.algctx, |
811 | 0 | params); |
812 | | |
813 | 28.2k | if (ctx->digest != NULL && ctx->digest->get_ctx_params != NULL) |
814 | 28.2k | return ctx->digest->get_ctx_params(ctx->algctx, params); |
815 | | |
816 | 0 | return 0; |
817 | 28.2k | } |
818 | | |
819 | | const OSSL_PARAM *EVP_MD_gettable_ctx_params(const EVP_MD *md) |
820 | 0 | { |
821 | 0 | void *provctx; |
822 | |
|
823 | 0 | if (md != NULL && md->gettable_ctx_params != NULL) { |
824 | 0 | provctx = ossl_provider_ctx(EVP_MD_get0_provider(md)); |
825 | 0 | return md->gettable_ctx_params(NULL, provctx); |
826 | 0 | } |
827 | 0 | return NULL; |
828 | 0 | } |
829 | | |
830 | | const OSSL_PARAM *EVP_MD_CTX_gettable_params(EVP_MD_CTX *ctx) |
831 | 245M | { |
832 | 245M | EVP_PKEY_CTX *pctx; |
833 | 245M | void *provctx; |
834 | | |
835 | 245M | if (ctx == NULL) |
836 | 0 | return NULL; |
837 | | |
838 | | /* If we have a pctx then we should try that first */ |
839 | 245M | pctx = ctx->pctx; |
840 | 245M | if (pctx != NULL |
841 | 245M | && (pctx->operation == EVP_PKEY_OP_VERIFYCTX |
842 | 307k | || pctx->operation == EVP_PKEY_OP_SIGNCTX) |
843 | 245M | && pctx->op.sig.algctx != NULL |
844 | 245M | && pctx->op.sig.signature->gettable_ctx_md_params != NULL) |
845 | 0 | return pctx->op.sig.signature->gettable_ctx_md_params( |
846 | 0 | pctx->op.sig.algctx); |
847 | | |
848 | 245M | if (ctx->digest != NULL && ctx->digest->gettable_ctx_params != NULL) { |
849 | 28.2k | provctx = ossl_provider_ctx(EVP_MD_get0_provider(ctx->digest)); |
850 | 28.2k | return ctx->digest->gettable_ctx_params(ctx->algctx, provctx); |
851 | 28.2k | } |
852 | 245M | return NULL; |
853 | 245M | } |
854 | | |
855 | | int EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void *p2) |
856 | 0 | { |
857 | 0 | int ret = EVP_CTRL_RET_UNSUPPORTED; |
858 | 0 | int set_params = 1; |
859 | 0 | size_t sz; |
860 | 0 | OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; |
861 | |
|
862 | 0 | if (ctx == NULL) { |
863 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER); |
864 | 0 | return 0; |
865 | 0 | } |
866 | | |
867 | 0 | if (ctx->digest != NULL && ctx->digest->prov == NULL) |
868 | 0 | goto legacy; |
869 | | |
870 | 0 | switch (cmd) { |
871 | 0 | case EVP_MD_CTRL_XOF_LEN: |
872 | 0 | sz = (size_t)p1; |
873 | 0 | params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN, &sz); |
874 | 0 | break; |
875 | 0 | case EVP_MD_CTRL_MICALG: |
876 | 0 | set_params = 0; |
877 | 0 | params[0] = OSSL_PARAM_construct_utf8_string(OSSL_DIGEST_PARAM_MICALG, |
878 | 0 | p2, p1 ? p1 : 9999); |
879 | 0 | break; |
880 | 0 | case EVP_CTRL_SSL3_MASTER_SECRET: |
881 | 0 | params[0] = OSSL_PARAM_construct_octet_string(OSSL_DIGEST_PARAM_SSL3_MS, |
882 | 0 | p2, p1); |
883 | 0 | break; |
884 | 0 | default: |
885 | 0 | goto conclude; |
886 | 0 | } |
887 | | |
888 | 0 | if (set_params) |
889 | 0 | ret = EVP_MD_CTX_set_params(ctx, params); |
890 | 0 | else |
891 | 0 | ret = EVP_MD_CTX_get_params(ctx, params); |
892 | 0 | goto conclude; |
893 | | |
894 | | |
895 | | /* Code below to be removed when legacy support is dropped. */ |
896 | 0 | legacy: |
897 | 0 | if (ctx->digest->md_ctrl == NULL) { |
898 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_NOT_IMPLEMENTED); |
899 | 0 | return 0; |
900 | 0 | } |
901 | | |
902 | 0 | ret = ctx->digest->md_ctrl(ctx, cmd, p1, p2); |
903 | 0 | conclude: |
904 | 0 | if (ret <= 0) |
905 | 0 | return 0; |
906 | 0 | return ret; |
907 | 0 | } |
908 | | |
909 | | EVP_MD *evp_md_new(void) |
910 | 783 | { |
911 | 783 | EVP_MD *md = OPENSSL_zalloc(sizeof(*md)); |
912 | | |
913 | 783 | if (md != NULL && !CRYPTO_NEW_REF(&md->refcnt, 1)) { |
914 | 0 | OPENSSL_free(md); |
915 | 0 | return NULL; |
916 | 0 | } |
917 | 783 | return md; |
918 | 783 | } |
919 | | |
920 | | /* |
921 | | * FIPS module note: since internal fetches will be entirely |
922 | | * provider based, we know that none of its code depends on legacy |
923 | | * NIDs or any functionality that use them. |
924 | | */ |
925 | | #ifndef FIPS_MODULE |
926 | | static void set_legacy_nid(const char *name, void *vlegacy_nid) |
927 | 2.95k | { |
928 | 2.95k | int nid; |
929 | 2.95k | int *legacy_nid = vlegacy_nid; |
930 | | /* |
931 | | * We use lowest level function to get the associated method, because |
932 | | * higher level functions such as EVP_get_digestbyname() have changed |
933 | | * to look at providers too. |
934 | | */ |
935 | 2.95k | const void *legacy_method = OBJ_NAME_get(name, OBJ_NAME_TYPE_MD_METH); |
936 | | |
937 | 2.95k | if (*legacy_nid == -1) /* We found a clash already */ |
938 | 0 | return; |
939 | | |
940 | 2.95k | if (legacy_method == NULL) |
941 | 1.96k | return; |
942 | 989 | nid = EVP_MD_nid(legacy_method); |
943 | 989 | if (*legacy_nid != NID_undef && *legacy_nid != nid) { |
944 | 0 | *legacy_nid = -1; |
945 | 0 | return; |
946 | 0 | } |
947 | 989 | *legacy_nid = nid; |
948 | 989 | } |
949 | | #endif |
950 | | |
951 | | static int evp_md_cache_constants(EVP_MD *md) |
952 | 1.09k | { |
953 | 1.09k | int ok, xof = 0, algid_absent = 0; |
954 | 1.09k | size_t blksz = 0; |
955 | 1.09k | size_t mdsize = 0; |
956 | 1.09k | OSSL_PARAM params[5]; |
957 | | |
958 | 1.09k | params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_BLOCK_SIZE, &blksz); |
959 | 1.09k | params[1] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_SIZE, &mdsize); |
960 | 1.09k | params[2] = OSSL_PARAM_construct_int(OSSL_DIGEST_PARAM_XOF, &xof); |
961 | 1.09k | params[3] = OSSL_PARAM_construct_int(OSSL_DIGEST_PARAM_ALGID_ABSENT, |
962 | 1.09k | &algid_absent); |
963 | 1.09k | params[4] = OSSL_PARAM_construct_end(); |
964 | 1.09k | ok = evp_do_md_getparams(md, params) > 0; |
965 | 1.09k | if (mdsize > INT_MAX || blksz > INT_MAX) |
966 | 0 | ok = 0; |
967 | 1.09k | if (ok) { |
968 | 1.09k | md->block_size = (int)blksz; |
969 | 1.09k | md->md_size = (int)mdsize; |
970 | 1.09k | if (xof) |
971 | 172 | md->flags |= EVP_MD_FLAG_XOF; |
972 | 1.09k | if (algid_absent) |
973 | 690 | md->flags |= EVP_MD_FLAG_DIGALGID_ABSENT; |
974 | 1.09k | } |
975 | 1.09k | return ok; |
976 | 1.09k | } |
977 | | |
978 | | static void *evp_md_from_algorithm(int name_id, |
979 | | const OSSL_ALGORITHM *algodef, |
980 | | OSSL_PROVIDER *prov) |
981 | 578 | { |
982 | 578 | const OSSL_DISPATCH *fns = algodef->implementation; |
983 | 578 | EVP_MD *md = NULL; |
984 | 578 | int fncnt = 0; |
985 | | |
986 | | /* EVP_MD_fetch() will set the legacy NID if available */ |
987 | 578 | if ((md = evp_md_new()) == NULL) { |
988 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB); |
989 | 0 | return NULL; |
990 | 0 | } |
991 | | |
992 | 578 | #ifndef FIPS_MODULE |
993 | 578 | md->type = NID_undef; |
994 | 578 | if (!evp_names_do_all(prov, name_id, set_legacy_nid, &md->type) |
995 | 578 | || md->type == -1) { |
996 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR); |
997 | 0 | EVP_MD_free(md); |
998 | 0 | return NULL; |
999 | 0 | } |
1000 | 578 | #endif |
1001 | | |
1002 | 578 | md->name_id = name_id; |
1003 | 578 | if ((md->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL) { |
1004 | 0 | EVP_MD_free(md); |
1005 | 0 | return NULL; |
1006 | 0 | } |
1007 | 578 | md->description = algodef->algorithm_description; |
1008 | | |
1009 | 5.53k | for (; fns->function_id != 0; fns++) { |
1010 | 4.95k | switch (fns->function_id) { |
1011 | 578 | case OSSL_FUNC_DIGEST_NEWCTX: |
1012 | 578 | if (md->newctx == NULL) { |
1013 | 578 | md->newctx = OSSL_FUNC_digest_newctx(fns); |
1014 | 578 | fncnt++; |
1015 | 578 | } |
1016 | 578 | break; |
1017 | 578 | case OSSL_FUNC_DIGEST_INIT: |
1018 | 578 | if (md->dinit == NULL) { |
1019 | 578 | md->dinit = OSSL_FUNC_digest_init(fns); |
1020 | 578 | fncnt++; |
1021 | 578 | } |
1022 | 578 | break; |
1023 | 578 | case OSSL_FUNC_DIGEST_UPDATE: |
1024 | 578 | if (md->dupdate == NULL) { |
1025 | 578 | md->dupdate = OSSL_FUNC_digest_update(fns); |
1026 | 578 | fncnt++; |
1027 | 578 | } |
1028 | 578 | break; |
1029 | 578 | case OSSL_FUNC_DIGEST_FINAL: |
1030 | 578 | if (md->dfinal == NULL) { |
1031 | 578 | md->dfinal = OSSL_FUNC_digest_final(fns); |
1032 | 578 | fncnt++; |
1033 | 578 | } |
1034 | 578 | break; |
1035 | 0 | case OSSL_FUNC_DIGEST_DIGEST: |
1036 | 0 | if (md->digest == NULL) |
1037 | 0 | md->digest = OSSL_FUNC_digest_digest(fns); |
1038 | | /* We don't increment fnct for this as it is stand alone */ |
1039 | 0 | break; |
1040 | 578 | case OSSL_FUNC_DIGEST_FREECTX: |
1041 | 578 | if (md->freectx == NULL) { |
1042 | 578 | md->freectx = OSSL_FUNC_digest_freectx(fns); |
1043 | 578 | fncnt++; |
1044 | 578 | } |
1045 | 578 | break; |
1046 | 578 | case OSSL_FUNC_DIGEST_DUPCTX: |
1047 | 578 | if (md->dupctx == NULL) |
1048 | 578 | md->dupctx = OSSL_FUNC_digest_dupctx(fns); |
1049 | 578 | break; |
1050 | 578 | case OSSL_FUNC_DIGEST_GET_PARAMS: |
1051 | 578 | if (md->get_params == NULL) |
1052 | 578 | md->get_params = OSSL_FUNC_digest_get_params(fns); |
1053 | 578 | break; |
1054 | 154 | case OSSL_FUNC_DIGEST_SET_CTX_PARAMS: |
1055 | 154 | if (md->set_ctx_params == NULL) |
1056 | 154 | md->set_ctx_params = OSSL_FUNC_digest_set_ctx_params(fns); |
1057 | 154 | break; |
1058 | 10 | case OSSL_FUNC_DIGEST_GET_CTX_PARAMS: |
1059 | 10 | if (md->get_ctx_params == NULL) |
1060 | 10 | md->get_ctx_params = OSSL_FUNC_digest_get_ctx_params(fns); |
1061 | 10 | break; |
1062 | 578 | case OSSL_FUNC_DIGEST_GETTABLE_PARAMS: |
1063 | 578 | if (md->gettable_params == NULL) |
1064 | 578 | md->gettable_params = OSSL_FUNC_digest_gettable_params(fns); |
1065 | 578 | break; |
1066 | 154 | case OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS: |
1067 | 154 | if (md->settable_ctx_params == NULL) |
1068 | 154 | md->settable_ctx_params = |
1069 | 154 | OSSL_FUNC_digest_settable_ctx_params(fns); |
1070 | 154 | break; |
1071 | 10 | case OSSL_FUNC_DIGEST_GETTABLE_CTX_PARAMS: |
1072 | 10 | if (md->gettable_ctx_params == NULL) |
1073 | 10 | md->gettable_ctx_params = |
1074 | 10 | OSSL_FUNC_digest_gettable_ctx_params(fns); |
1075 | 10 | break; |
1076 | 4.95k | } |
1077 | 4.95k | } |
1078 | 578 | if ((fncnt != 0 && fncnt != 5) |
1079 | 578 | || (fncnt == 0 && md->digest == NULL)) { |
1080 | | /* |
1081 | | * In order to be a consistent set of functions we either need the |
1082 | | * whole set of init/update/final etc functions or none of them. |
1083 | | * The "digest" function can standalone. We at least need one way to |
1084 | | * generate digests. |
1085 | | */ |
1086 | 0 | EVP_MD_free(md); |
1087 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS); |
1088 | 0 | return NULL; |
1089 | 0 | } |
1090 | 578 | md->prov = prov; |
1091 | 578 | if (prov != NULL) |
1092 | 578 | ossl_provider_up_ref(prov); |
1093 | | |
1094 | 578 | if (!evp_md_cache_constants(md)) { |
1095 | 0 | EVP_MD_free(md); |
1096 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_CACHE_CONSTANTS_FAILED); |
1097 | 0 | md = NULL; |
1098 | 0 | } |
1099 | | |
1100 | 578 | return md; |
1101 | 578 | } |
1102 | | |
1103 | | static int evp_md_up_ref(void *md) |
1104 | 2.21M | { |
1105 | 2.21M | return EVP_MD_up_ref(md); |
1106 | 2.21M | } |
1107 | | |
1108 | | static void evp_md_free(void *md) |
1109 | 3.17k | { |
1110 | 3.17k | EVP_MD_free(md); |
1111 | 3.17k | } |
1112 | | |
1113 | | EVP_MD *EVP_MD_fetch(OSSL_LIB_CTX *ctx, const char *algorithm, |
1114 | | const char *properties) |
1115 | 2.76M | { |
1116 | 2.76M | EVP_MD *md = |
1117 | 2.76M | evp_generic_fetch(ctx, OSSL_OP_DIGEST, algorithm, properties, |
1118 | 2.76M | evp_md_from_algorithm, evp_md_up_ref, evp_md_free); |
1119 | | |
1120 | 2.76M | return md; |
1121 | 2.76M | } |
1122 | | |
1123 | | int EVP_MD_up_ref(EVP_MD *md) |
1124 | 7.43M | { |
1125 | 7.43M | int ref = 0; |
1126 | | |
1127 | 7.43M | if (md->origin == EVP_ORIG_DYNAMIC) |
1128 | 7.43M | CRYPTO_UP_REF(&md->refcnt, &ref); |
1129 | 7.43M | return 1; |
1130 | 7.43M | } |
1131 | | |
1132 | | void EVP_MD_free(EVP_MD *md) |
1133 | 14.3M | { |
1134 | 14.3M | int i; |
1135 | | |
1136 | 14.3M | if (md == NULL || md->origin != EVP_ORIG_DYNAMIC) |
1137 | 6.95M | return; |
1138 | | |
1139 | 7.44M | CRYPTO_DOWN_REF(&md->refcnt, &i); |
1140 | 7.44M | if (i > 0) |
1141 | 7.43M | return; |
1142 | 1.03k | evp_md_free_int(md); |
1143 | 1.03k | } |
1144 | | |
1145 | | void EVP_MD_do_all_provided(OSSL_LIB_CTX *libctx, |
1146 | | void (*fn)(EVP_MD *mac, void *arg), |
1147 | | void *arg) |
1148 | 2 | { |
1149 | 2 | evp_generic_do_all(libctx, OSSL_OP_DIGEST, |
1150 | 2 | (void (*)(void *, void *))fn, arg, |
1151 | 2 | evp_md_from_algorithm, evp_md_up_ref, evp_md_free); |
1152 | 2 | } |