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