/src/openssl/crypto/evp/digest.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2023 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 | 57.4k | { |
30 | 57.4k | if (ctx->digest != NULL) { |
31 | 38.1k | if (ctx->digest->cleanup != NULL |
32 | 38.1k | && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_CLEANED)) |
33 | 0 | ctx->digest->cleanup(ctx); |
34 | 38.1k | if (ctx->md_data != NULL && ctx->digest->ctx_size > 0 |
35 | 38.1k | && (!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 | 38.1k | } |
41 | 57.4k | } |
42 | | |
43 | | void evp_md_ctx_clear_digest(EVP_MD_CTX *ctx, int force, int keep_fetched) |
44 | 19.2k | { |
45 | 19.2k | if (ctx->algctx != NULL) { |
46 | 19.2k | if (ctx->digest != NULL && ctx->digest->freectx != NULL) |
47 | 19.2k | ctx->digest->freectx(ctx->algctx); |
48 | 19.2k | ctx->algctx = NULL; |
49 | 19.2k | EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED); |
50 | 19.2k | } |
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 | 19.2k | cleanup_old_md_data(ctx, force); |
59 | 19.2k | if (force) |
60 | 0 | ctx->digest = NULL; |
61 | | |
62 | 19.2k | #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE) |
63 | 19.2k | ENGINE_finish(ctx->engine); |
64 | 19.2k | ctx->engine = NULL; |
65 | 19.2k | #endif |
66 | | |
67 | | /* Non legacy code, this has to be later than the ctx->digest cleaning */ |
68 | 19.2k | if (!keep_fetched) { |
69 | 19.2k | EVP_MD_free(ctx->fetched_digest); |
70 | 19.2k | ctx->fetched_digest = NULL; |
71 | 19.2k | ctx->reqdigest = NULL; |
72 | 19.2k | } |
73 | 19.2k | } |
74 | | |
75 | | static int evp_md_ctx_reset_ex(EVP_MD_CTX *ctx, int keep_fetched) |
76 | 19.2k | { |
77 | 19.2k | if (ctx == NULL) |
78 | 0 | return 1; |
79 | | |
80 | 19.2k | #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 | 19.2k | if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX)) { |
86 | 19.2k | EVP_PKEY_CTX_free(ctx->pctx); |
87 | 19.2k | ctx->pctx = NULL; |
88 | 19.2k | } |
89 | 19.2k | #endif |
90 | | |
91 | 19.2k | evp_md_ctx_clear_digest(ctx, 0, keep_fetched); |
92 | 19.2k | if (!keep_fetched) |
93 | 19.2k | OPENSSL_cleanse(ctx, sizeof(*ctx)); |
94 | | |
95 | 19.2k | return 1; |
96 | 19.2k | } |
97 | | |
98 | | /* This call frees resources associated with the context */ |
99 | | int EVP_MD_CTX_reset(EVP_MD_CTX *ctx) |
100 | 19.2k | { |
101 | 19.2k | return evp_md_ctx_reset_ex(ctx, 0); |
102 | 19.2k | } |
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 | 0 | { |
108 | 0 | EVP_MD_CTX *ctx; |
109 | 0 | EVP_PKEY_CTX *pctx = NULL; |
110 | |
|
111 | 0 | if ((ctx = EVP_MD_CTX_new()) == NULL |
112 | 0 | || (pctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq)) == NULL) { |
113 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB); |
114 | 0 | goto err; |
115 | 0 | } |
116 | | |
117 | 0 | if (id != NULL && EVP_PKEY_CTX_set1_id(pctx, id->data, id->length) <= 0) |
118 | 0 | goto err; |
119 | | |
120 | 0 | EVP_MD_CTX_set_pkey_ctx(ctx, pctx); |
121 | 0 | return ctx; |
122 | | |
123 | 0 | err: |
124 | 0 | EVP_PKEY_CTX_free(pctx); |
125 | 0 | EVP_MD_CTX_free(ctx); |
126 | 0 | return NULL; |
127 | 0 | } |
128 | | #endif |
129 | | |
130 | | EVP_MD_CTX *EVP_MD_CTX_new(void) |
131 | 19.2k | { |
132 | 19.2k | return OPENSSL_zalloc(sizeof(EVP_MD_CTX)); |
133 | 19.2k | } |
134 | | |
135 | | void EVP_MD_CTX_free(EVP_MD_CTX *ctx) |
136 | 19.2k | { |
137 | 19.2k | if (ctx == NULL) |
138 | 0 | return; |
139 | | |
140 | 19.2k | EVP_MD_CTX_reset(ctx); |
141 | 19.2k | OPENSSL_free(ctx); |
142 | 19.2k | } |
143 | | |
144 | | int evp_md_ctx_free_algctx(EVP_MD_CTX *ctx) |
145 | 19.2k | { |
146 | 19.2k | if (ctx->algctx != NULL) { |
147 | 0 | if (!ossl_assert(ctx->digest != NULL)) { |
148 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
149 | 0 | return 0; |
150 | 0 | } |
151 | 0 | if (ctx->digest->freectx != NULL) |
152 | 0 | ctx->digest->freectx(ctx->algctx); |
153 | 0 | ctx->algctx = NULL; |
154 | 0 | } |
155 | 19.2k | return 1; |
156 | 19.2k | } |
157 | | |
158 | | static int evp_md_init_internal(EVP_MD_CTX *ctx, const EVP_MD *type, |
159 | | const OSSL_PARAM params[], ENGINE *impl) |
160 | 38.1k | { |
161 | 38.1k | #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE) |
162 | 38.1k | ENGINE *tmpimpl = NULL; |
163 | 38.1k | #endif |
164 | | |
165 | 38.1k | #if !defined(FIPS_MODULE) |
166 | 38.1k | if (ctx->pctx != NULL |
167 | 38.1k | && EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx->pctx) |
168 | 38.1k | && 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 | 38.1k | #endif |
183 | | |
184 | 38.1k | EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_CLEANED |
185 | 38.1k | | EVP_MD_CTX_FLAG_FINALISED); |
186 | | |
187 | 38.1k | if (type != NULL) { |
188 | 38.1k | ctx->reqdigest = type; |
189 | 38.1k | } 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 | 38.1k | #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 | 38.1k | if (ctx->engine != NULL |
206 | 38.1k | && ctx->digest != NULL |
207 | 38.1k | && 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 | 38.1k | ENGINE_finish(ctx->engine); |
216 | 38.1k | ctx->engine = NULL; |
217 | | |
218 | 38.1k | if (impl == NULL) |
219 | 38.1k | tmpimpl = ENGINE_get_digest_engine(type->type); |
220 | 38.1k | #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 | 38.1k | if (impl != NULL |
227 | 38.1k | #if !defined(OPENSSL_NO_ENGINE) |
228 | 38.1k | || ctx->engine != NULL |
229 | 38.1k | # if !defined(FIPS_MODULE) |
230 | 38.1k | || tmpimpl != NULL |
231 | 38.1k | # endif |
232 | 38.1k | #endif |
233 | 38.1k | || (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0 |
234 | 38.1k | || (type != NULL && type->origin == EVP_ORIG_METH) |
235 | 38.1k | || (type == NULL && ctx->digest != NULL |
236 | 38.1k | && 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 | 38.1k | cleanup_old_md_data(ctx, 1); |
248 | | |
249 | | /* Start of non-legacy code below */ |
250 | 38.1k | if (ctx->digest == type) { |
251 | 18.8k | if (!ossl_assert(type->prov != NULL)) { |
252 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
253 | 0 | return 0; |
254 | 0 | } |
255 | 19.2k | } else { |
256 | 19.2k | if (!evp_md_ctx_free_algctx(ctx)) |
257 | 0 | return 0; |
258 | 19.2k | } |
259 | | |
260 | 38.1k | 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 | 0 | EVP_MD *provmd = EVP_MD_fetch(NULL, |
268 | 0 | type->type != NID_undef ? OBJ_nid2sn(type->type) |
269 | 0 | : "NULL", ""); |
270 | |
|
271 | 0 | if (provmd == NULL) { |
272 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
273 | 0 | return 0; |
274 | 0 | } |
275 | 0 | type = provmd; |
276 | 0 | EVP_MD_free(ctx->fetched_digest); |
277 | 0 | ctx->fetched_digest = provmd; |
278 | 0 | #endif |
279 | 0 | } |
280 | | |
281 | 38.1k | if (type->prov != NULL && ctx->fetched_digest != type) { |
282 | 19.2k | 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 | 19.2k | EVP_MD_free(ctx->fetched_digest); |
287 | 19.2k | ctx->fetched_digest = (EVP_MD *)type; |
288 | 19.2k | } |
289 | 38.1k | ctx->digest = type; |
290 | 38.1k | if (ctx->algctx == NULL) { |
291 | 19.2k | ctx->algctx = ctx->digest->newctx(ossl_provider_ctx(type->prov)); |
292 | 19.2k | if (ctx->algctx == NULL) { |
293 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
294 | 0 | return 0; |
295 | 0 | } |
296 | 19.2k | } |
297 | | |
298 | 38.1k | if (ctx->digest->dinit == NULL) { |
299 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
300 | 0 | return 0; |
301 | 0 | } |
302 | | |
303 | 38.1k | 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 | 0 | { |
372 | 0 | return evp_md_init_internal(ctx, type, params, NULL); |
373 | 0 | } |
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 | 38.1k | { |
383 | 38.1k | return evp_md_init_internal(ctx, type, NULL, impl); |
384 | 38.1k | } |
385 | | |
386 | | int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t count) |
387 | 191M | { |
388 | 191M | if (count == 0) |
389 | 0 | return 1; |
390 | | |
391 | 191M | 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 | 191M | if (ctx->pctx != NULL |
397 | 191M | && EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx->pctx) |
398 | 191M | && 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 | 191M | if (ctx->digest == NULL |
416 | 191M | || ctx->digest->prov == NULL |
417 | 191M | || (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0) |
418 | 0 | goto legacy; |
419 | | |
420 | 191M | if (ctx->digest->dupdate == NULL) { |
421 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR); |
422 | 0 | return 0; |
423 | 0 | } |
424 | 191M | 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(ctx, data, count); |
429 | 191M | } |
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 | 0 | { |
434 | 0 | int ret; |
435 | 0 | ret = EVP_DigestFinal_ex(ctx, md, size); |
436 | 0 | EVP_MD_CTX_reset(ctx); |
437 | 0 | return ret; |
438 | 0 | } |
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 | 18.8k | { |
443 | 18.8k | int ret, sz; |
444 | 18.8k | size_t size = 0; |
445 | 18.8k | size_t mdsize = 0; |
446 | | |
447 | 18.8k | if (ctx->digest == NULL) |
448 | 0 | return 0; |
449 | | |
450 | 18.8k | sz = EVP_MD_get_size(ctx->digest); |
451 | 18.8k | if (sz < 0) |
452 | 0 | return 0; |
453 | 18.8k | mdsize = sz; |
454 | 18.8k | if (ctx->digest->prov == NULL) |
455 | 0 | goto legacy; |
456 | | |
457 | 18.8k | if (sz == 0) /* Assuming a xoflen must have been set. */ |
458 | 0 | mdsize = SIZE_MAX; |
459 | 18.8k | if (ctx->digest->gettable_ctx_params != NULL) { |
460 | 173 | OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END }; |
461 | | |
462 | 173 | params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_SIZE, |
463 | 173 | &mdsize); |
464 | 173 | if (!EVP_MD_CTX_get_params(ctx, params)) |
465 | 0 | return 0; |
466 | 173 | } |
467 | | |
468 | 18.8k | if (ctx->digest->dfinal == NULL) { |
469 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR); |
470 | 0 | return 0; |
471 | 0 | } |
472 | | |
473 | 18.8k | if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISED) != 0) { |
474 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR); |
475 | 0 | return 0; |
476 | 0 | } |
477 | | |
478 | 18.8k | ret = ctx->digest->dfinal(ctx->algctx, md, &size, mdsize); |
479 | | |
480 | 18.8k | ctx->flags |= EVP_MD_CTX_FLAG_FINALISED; |
481 | | |
482 | 18.8k | if (isize != NULL) { |
483 | 18.8k | if (size <= UINT_MAX) { |
484 | 18.8k | *isize = (unsigned int)size; |
485 | 18.8k | } else { |
486 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR); |
487 | 0 | ret = 0; |
488 | 0 | } |
489 | 18.8k | } |
490 | | |
491 | 18.8k | return ret; |
492 | | |
493 | | /* Code below to be removed when legacy support is dropped. */ |
494 | 0 | legacy: |
495 | 0 | OPENSSL_assert(mdsize <= EVP_MAX_MD_SIZE); |
496 | 0 | ret = ctx->digest->final(ctx, md); |
497 | 0 | if (isize != NULL) |
498 | 0 | *isize = mdsize; |
499 | 0 | if (ctx->digest->cleanup) { |
500 | 0 | ctx->digest->cleanup(ctx); |
501 | 0 | EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED); |
502 | 0 | } |
503 | 0 | OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size); |
504 | 0 | return ret; |
505 | 18.8k | } |
506 | | |
507 | | /* This is a one shot operation */ |
508 | | int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t size) |
509 | 0 | { |
510 | 0 | int ret = 0; |
511 | 0 | OSSL_PARAM params[2]; |
512 | 0 | size_t i = 0; |
513 | |
|
514 | 0 | if (ctx->digest == NULL) { |
515 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_NULL_ALGORITHM); |
516 | 0 | return 0; |
517 | 0 | } |
518 | | |
519 | 0 | if (ctx->digest->prov == NULL) |
520 | 0 | goto legacy; |
521 | | |
522 | 0 | if (ctx->digest->dfinal == NULL) { |
523 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR); |
524 | 0 | return 0; |
525 | 0 | } |
526 | | |
527 | 0 | if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISED) != 0) { |
528 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR); |
529 | 0 | return 0; |
530 | 0 | } |
531 | | |
532 | | /* |
533 | | * For backward compatibility we pass the XOFLEN via a param here so that |
534 | | * older providers can use the supplied value. Ideally we should have just |
535 | | * used the size passed into ctx->digest->dfinal(). |
536 | | */ |
537 | 0 | params[i++] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN, &size); |
538 | 0 | params[i++] = OSSL_PARAM_construct_end(); |
539 | |
|
540 | 0 | if (EVP_MD_CTX_set_params(ctx, params) >= 0) |
541 | 0 | ret = ctx->digest->dfinal(ctx->algctx, md, &size, size); |
542 | |
|
543 | 0 | ctx->flags |= EVP_MD_CTX_FLAG_FINALISED; |
544 | |
|
545 | 0 | return ret; |
546 | | |
547 | 0 | legacy: |
548 | 0 | if (ctx->digest->flags & EVP_MD_FLAG_XOF |
549 | 0 | && size <= INT_MAX |
550 | 0 | && ctx->digest->md_ctrl(ctx, EVP_MD_CTRL_XOF_LEN, (int)size, NULL)) { |
551 | 0 | ret = ctx->digest->final(ctx, md); |
552 | 0 | if (ctx->digest->cleanup != NULL) { |
553 | 0 | ctx->digest->cleanup(ctx); |
554 | 0 | EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED); |
555 | 0 | } |
556 | 0 | OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size); |
557 | 0 | } else { |
558 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_NOT_XOF_OR_INVALID_LENGTH); |
559 | 0 | } |
560 | |
|
561 | 0 | return ret; |
562 | 0 | } |
563 | | |
564 | | /* EVP_DigestSqueeze() can be called multiple times */ |
565 | | int EVP_DigestSqueeze(EVP_MD_CTX *ctx, unsigned char *md, size_t size) |
566 | 0 | { |
567 | 0 | if (ctx->digest == NULL) { |
568 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_NULL_ALGORITHM); |
569 | 0 | return 0; |
570 | 0 | } |
571 | | |
572 | 0 | if (ctx->digest->prov == NULL) { |
573 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION); |
574 | 0 | return 0; |
575 | 0 | } |
576 | | |
577 | 0 | if (ctx->digest->dsqueeze == NULL) { |
578 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_METHOD_NOT_SUPPORTED); |
579 | 0 | return 0; |
580 | 0 | } |
581 | | |
582 | 0 | return ctx->digest->dsqueeze(ctx->algctx, md, &size, size); |
583 | 0 | } |
584 | | |
585 | | EVP_MD_CTX *EVP_MD_CTX_dup(const EVP_MD_CTX *in) |
586 | 0 | { |
587 | 0 | EVP_MD_CTX *out = EVP_MD_CTX_new(); |
588 | |
|
589 | 0 | if (out != NULL && !EVP_MD_CTX_copy_ex(out, in)) { |
590 | 0 | EVP_MD_CTX_free(out); |
591 | 0 | out = NULL; |
592 | 0 | } |
593 | 0 | return out; |
594 | 0 | } |
595 | | |
596 | | int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in) |
597 | 0 | { |
598 | 0 | EVP_MD_CTX_reset(out); |
599 | 0 | return EVP_MD_CTX_copy_ex(out, in); |
600 | 0 | } |
601 | | |
602 | | int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in) |
603 | 0 | { |
604 | 0 | int digest_change = 0; |
605 | 0 | unsigned char *tmp_buf; |
606 | |
|
607 | 0 | if (in == NULL) { |
608 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER); |
609 | 0 | return 0; |
610 | 0 | } |
611 | | |
612 | 0 | if (in->digest == NULL) { |
613 | | /* copying uninitialized digest context */ |
614 | 0 | EVP_MD_CTX_reset(out); |
615 | 0 | if (out->fetched_digest != NULL) |
616 | 0 | EVP_MD_free(out->fetched_digest); |
617 | 0 | *out = *in; |
618 | 0 | goto clone_pkey; |
619 | 0 | } |
620 | | |
621 | 0 | if (in->digest->prov == NULL |
622 | 0 | || (in->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0) |
623 | 0 | goto legacy; |
624 | | |
625 | 0 | if (in->digest->dupctx == NULL) { |
626 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX); |
627 | 0 | return 0; |
628 | 0 | } |
629 | | |
630 | 0 | evp_md_ctx_reset_ex(out, 1); |
631 | 0 | digest_change = (out->fetched_digest != in->fetched_digest); |
632 | 0 | if (digest_change && out->fetched_digest != NULL) |
633 | 0 | EVP_MD_free(out->fetched_digest); |
634 | 0 | *out = *in; |
635 | | /* NULL out pointers in case of error */ |
636 | 0 | out->pctx = NULL; |
637 | 0 | out->algctx = NULL; |
638 | |
|
639 | 0 | if (digest_change && in->fetched_digest != NULL) |
640 | 0 | EVP_MD_up_ref(in->fetched_digest); |
641 | |
|
642 | 0 | if (in->algctx != NULL) { |
643 | 0 | out->algctx = in->digest->dupctx(in->algctx); |
644 | 0 | if (out->algctx == NULL) { |
645 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX); |
646 | 0 | return 0; |
647 | 0 | } |
648 | 0 | } |
649 | | |
650 | 0 | clone_pkey: |
651 | | /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */ |
652 | 0 | EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX); |
653 | 0 | #ifndef FIPS_MODULE |
654 | 0 | if (in->pctx != NULL) { |
655 | 0 | out->pctx = EVP_PKEY_CTX_dup(in->pctx); |
656 | 0 | if (out->pctx == NULL) { |
657 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX); |
658 | 0 | EVP_MD_CTX_reset(out); |
659 | 0 | return 0; |
660 | 0 | } |
661 | 0 | } |
662 | 0 | #endif |
663 | | |
664 | 0 | return 1; |
665 | | |
666 | | /* Code below to be removed when legacy support is dropped. */ |
667 | 0 | legacy: |
668 | 0 | #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE) |
669 | | /* Make sure it's safe to copy a digest context using an ENGINE */ |
670 | 0 | if (in->engine && !ENGINE_init(in->engine)) { |
671 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB); |
672 | 0 | return 0; |
673 | 0 | } |
674 | 0 | #endif |
675 | | |
676 | 0 | if (out->digest == in->digest) { |
677 | 0 | tmp_buf = out->md_data; |
678 | 0 | EVP_MD_CTX_set_flags(out, EVP_MD_CTX_FLAG_REUSE); |
679 | 0 | } else |
680 | 0 | tmp_buf = NULL; |
681 | 0 | EVP_MD_CTX_reset(out); |
682 | 0 | memcpy(out, in, sizeof(*out)); |
683 | | |
684 | | /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */ |
685 | 0 | EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX); |
686 | | |
687 | | /* Null these variables, since they are getting fixed up |
688 | | * properly below. Anything else may cause a memleak and/or |
689 | | * double free if any of the memory allocations below fail |
690 | | */ |
691 | 0 | out->md_data = NULL; |
692 | 0 | out->pctx = NULL; |
693 | |
|
694 | 0 | if (in->md_data && out->digest->ctx_size) { |
695 | 0 | if (tmp_buf) |
696 | 0 | out->md_data = tmp_buf; |
697 | 0 | else { |
698 | 0 | out->md_data = OPENSSL_malloc(out->digest->ctx_size); |
699 | 0 | if (out->md_data == NULL) |
700 | 0 | return 0; |
701 | 0 | } |
702 | 0 | memcpy(out->md_data, in->md_data, out->digest->ctx_size); |
703 | 0 | } |
704 | | |
705 | 0 | out->update = in->update; |
706 | |
|
707 | 0 | #ifndef FIPS_MODULE |
708 | 0 | if (in->pctx) { |
709 | 0 | out->pctx = EVP_PKEY_CTX_dup(in->pctx); |
710 | 0 | if (!out->pctx) { |
711 | 0 | EVP_MD_CTX_reset(out); |
712 | 0 | return 0; |
713 | 0 | } |
714 | 0 | } |
715 | 0 | #endif |
716 | | |
717 | 0 | if (out->digest->copy) |
718 | 0 | return out->digest->copy(out, in); |
719 | | |
720 | 0 | return 1; |
721 | 0 | } |
722 | | |
723 | | int EVP_Digest(const void *data, size_t count, |
724 | | unsigned char *md, unsigned int *size, const EVP_MD *type, |
725 | | ENGINE *impl) |
726 | 0 | { |
727 | 0 | EVP_MD_CTX *ctx = EVP_MD_CTX_new(); |
728 | 0 | int ret; |
729 | |
|
730 | 0 | if (ctx == NULL) |
731 | 0 | return 0; |
732 | 0 | EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT); |
733 | 0 | ret = EVP_DigestInit_ex(ctx, type, impl) |
734 | 0 | && EVP_DigestUpdate(ctx, data, count) |
735 | 0 | && EVP_DigestFinal_ex(ctx, md, size); |
736 | 0 | EVP_MD_CTX_free(ctx); |
737 | |
|
738 | 0 | return ret; |
739 | 0 | } |
740 | | |
741 | | int EVP_Q_digest(OSSL_LIB_CTX *libctx, const char *name, const char *propq, |
742 | | const void *data, size_t datalen, |
743 | | unsigned char *md, size_t *mdlen) |
744 | 0 | { |
745 | 0 | EVP_MD *digest = EVP_MD_fetch(libctx, name, propq); |
746 | 0 | unsigned int temp = 0; |
747 | 0 | int ret = 0; |
748 | |
|
749 | 0 | if (digest != NULL) { |
750 | 0 | ret = EVP_Digest(data, datalen, md, &temp, digest, NULL); |
751 | 0 | EVP_MD_free(digest); |
752 | 0 | } |
753 | 0 | if (mdlen != NULL) |
754 | 0 | *mdlen = temp; |
755 | 0 | return ret; |
756 | 0 | } |
757 | | |
758 | | int EVP_MD_get_params(const EVP_MD *digest, OSSL_PARAM params[]) |
759 | 0 | { |
760 | 0 | if (digest != NULL && digest->get_params != NULL) |
761 | 0 | return digest->get_params(params); |
762 | 0 | return 0; |
763 | 0 | } |
764 | | |
765 | | const OSSL_PARAM *EVP_MD_gettable_params(const EVP_MD *digest) |
766 | 0 | { |
767 | 0 | if (digest != NULL && digest->gettable_params != NULL) |
768 | 0 | return digest->gettable_params( |
769 | 0 | ossl_provider_ctx(EVP_MD_get0_provider(digest))); |
770 | 0 | return NULL; |
771 | 0 | } |
772 | | |
773 | | int EVP_MD_CTX_set_params(EVP_MD_CTX *ctx, const OSSL_PARAM params[]) |
774 | 0 | { |
775 | 0 | EVP_PKEY_CTX *pctx = ctx->pctx; |
776 | | |
777 | | /* If we have a pctx then we should try that first */ |
778 | 0 | if (pctx != NULL |
779 | 0 | && (pctx->operation == EVP_PKEY_OP_VERIFYCTX |
780 | 0 | || pctx->operation == EVP_PKEY_OP_SIGNCTX) |
781 | 0 | && pctx->op.sig.algctx != NULL |
782 | 0 | && pctx->op.sig.signature->set_ctx_md_params != NULL) |
783 | 0 | return pctx->op.sig.signature->set_ctx_md_params(pctx->op.sig.algctx, |
784 | 0 | params); |
785 | | |
786 | 0 | if (ctx->digest != NULL && ctx->digest->set_ctx_params != NULL) |
787 | 0 | return ctx->digest->set_ctx_params(ctx->algctx, params); |
788 | | |
789 | 0 | return 0; |
790 | 0 | } |
791 | | |
792 | | const OSSL_PARAM *EVP_MD_settable_ctx_params(const EVP_MD *md) |
793 | 0 | { |
794 | 0 | void *provctx; |
795 | |
|
796 | 0 | if (md != NULL && md->settable_ctx_params != NULL) { |
797 | 0 | provctx = ossl_provider_ctx(EVP_MD_get0_provider(md)); |
798 | 0 | return md->settable_ctx_params(NULL, provctx); |
799 | 0 | } |
800 | 0 | return NULL; |
801 | 0 | } |
802 | | |
803 | | const OSSL_PARAM *EVP_MD_CTX_settable_params(EVP_MD_CTX *ctx) |
804 | 0 | { |
805 | 0 | EVP_PKEY_CTX *pctx; |
806 | 0 | void *alg; |
807 | |
|
808 | 0 | if (ctx == NULL) |
809 | 0 | return NULL; |
810 | | |
811 | | /* If we have a pctx then we should try that first */ |
812 | 0 | pctx = ctx->pctx; |
813 | 0 | if (pctx != NULL |
814 | 0 | && (pctx->operation == EVP_PKEY_OP_VERIFYCTX |
815 | 0 | || pctx->operation == EVP_PKEY_OP_SIGNCTX) |
816 | 0 | && pctx->op.sig.algctx != NULL |
817 | 0 | && pctx->op.sig.signature->settable_ctx_md_params != NULL) |
818 | 0 | return pctx->op.sig.signature->settable_ctx_md_params( |
819 | 0 | pctx->op.sig.algctx); |
820 | | |
821 | 0 | if (ctx->digest != NULL && ctx->digest->settable_ctx_params != NULL) { |
822 | 0 | alg = ossl_provider_ctx(EVP_MD_get0_provider(ctx->digest)); |
823 | 0 | return ctx->digest->settable_ctx_params(ctx->algctx, alg); |
824 | 0 | } |
825 | | |
826 | 0 | return NULL; |
827 | 0 | } |
828 | | |
829 | | int EVP_MD_CTX_get_params(EVP_MD_CTX *ctx, OSSL_PARAM params[]) |
830 | 173 | { |
831 | 173 | EVP_PKEY_CTX *pctx = ctx->pctx; |
832 | | |
833 | | /* If we have a pctx then we should try that first */ |
834 | 173 | if (pctx != NULL |
835 | 173 | && (pctx->operation == EVP_PKEY_OP_VERIFYCTX |
836 | 0 | || pctx->operation == EVP_PKEY_OP_SIGNCTX) |
837 | 173 | && pctx->op.sig.algctx != NULL |
838 | 173 | && pctx->op.sig.signature->get_ctx_md_params != NULL) |
839 | 0 | return pctx->op.sig.signature->get_ctx_md_params(pctx->op.sig.algctx, |
840 | 0 | params); |
841 | | |
842 | 173 | if (ctx->digest != NULL && ctx->digest->get_ctx_params != NULL) |
843 | 173 | return ctx->digest->get_ctx_params(ctx->algctx, params); |
844 | | |
845 | 0 | return 0; |
846 | 173 | } |
847 | | |
848 | | const OSSL_PARAM *EVP_MD_gettable_ctx_params(const EVP_MD *md) |
849 | 0 | { |
850 | 0 | void *provctx; |
851 | |
|
852 | 0 | if (md != NULL && md->gettable_ctx_params != NULL) { |
853 | 0 | provctx = ossl_provider_ctx(EVP_MD_get0_provider(md)); |
854 | 0 | return md->gettable_ctx_params(NULL, provctx); |
855 | 0 | } |
856 | 0 | return NULL; |
857 | 0 | } |
858 | | |
859 | | const OSSL_PARAM *EVP_MD_CTX_gettable_params(EVP_MD_CTX *ctx) |
860 | 0 | { |
861 | 0 | EVP_PKEY_CTX *pctx; |
862 | 0 | void *provctx; |
863 | |
|
864 | 0 | if (ctx == NULL) |
865 | 0 | return NULL; |
866 | | |
867 | | /* If we have a pctx then we should try that first */ |
868 | 0 | pctx = ctx->pctx; |
869 | 0 | if (pctx != NULL |
870 | 0 | && (pctx->operation == EVP_PKEY_OP_VERIFYCTX |
871 | 0 | || pctx->operation == EVP_PKEY_OP_SIGNCTX) |
872 | 0 | && pctx->op.sig.algctx != NULL |
873 | 0 | && pctx->op.sig.signature->gettable_ctx_md_params != NULL) |
874 | 0 | return pctx->op.sig.signature->gettable_ctx_md_params( |
875 | 0 | pctx->op.sig.algctx); |
876 | | |
877 | 0 | if (ctx->digest != NULL && ctx->digest->gettable_ctx_params != NULL) { |
878 | 0 | provctx = ossl_provider_ctx(EVP_MD_get0_provider(ctx->digest)); |
879 | 0 | return ctx->digest->gettable_ctx_params(ctx->algctx, provctx); |
880 | 0 | } |
881 | 0 | return NULL; |
882 | 0 | } |
883 | | |
884 | | int EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void *p2) |
885 | 0 | { |
886 | 0 | int ret = EVP_CTRL_RET_UNSUPPORTED; |
887 | 0 | int set_params = 1; |
888 | 0 | size_t sz; |
889 | 0 | OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; |
890 | |
|
891 | 0 | if (ctx == NULL) { |
892 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER); |
893 | 0 | return 0; |
894 | 0 | } |
895 | | |
896 | 0 | if (ctx->digest != NULL && ctx->digest->prov == NULL) |
897 | 0 | goto legacy; |
898 | | |
899 | 0 | switch (cmd) { |
900 | 0 | case EVP_MD_CTRL_XOF_LEN: |
901 | 0 | sz = (size_t)p1; |
902 | 0 | params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN, &sz); |
903 | 0 | break; |
904 | 0 | case EVP_MD_CTRL_MICALG: |
905 | 0 | set_params = 0; |
906 | 0 | params[0] = OSSL_PARAM_construct_utf8_string(OSSL_DIGEST_PARAM_MICALG, |
907 | 0 | p2, p1 ? p1 : 9999); |
908 | 0 | break; |
909 | 0 | case EVP_CTRL_SSL3_MASTER_SECRET: |
910 | 0 | params[0] = OSSL_PARAM_construct_octet_string(OSSL_DIGEST_PARAM_SSL3_MS, |
911 | 0 | p2, p1); |
912 | 0 | break; |
913 | 0 | default: |
914 | 0 | goto conclude; |
915 | 0 | } |
916 | | |
917 | 0 | if (set_params) |
918 | 0 | ret = EVP_MD_CTX_set_params(ctx, params); |
919 | 0 | else |
920 | 0 | ret = EVP_MD_CTX_get_params(ctx, params); |
921 | 0 | goto conclude; |
922 | | |
923 | | |
924 | | /* Code below to be removed when legacy support is dropped. */ |
925 | 0 | legacy: |
926 | 0 | if (ctx->digest->md_ctrl == NULL) { |
927 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_NOT_IMPLEMENTED); |
928 | 0 | return 0; |
929 | 0 | } |
930 | | |
931 | 0 | ret = ctx->digest->md_ctrl(ctx, cmd, p1, p2); |
932 | 0 | conclude: |
933 | 0 | if (ret <= 0) |
934 | 0 | return 0; |
935 | 0 | return ret; |
936 | 0 | } |
937 | | |
938 | | EVP_MD *evp_md_new(void) |
939 | 120 | { |
940 | 120 | EVP_MD *md = OPENSSL_zalloc(sizeof(*md)); |
941 | | |
942 | 120 | if (md != NULL && !CRYPTO_NEW_REF(&md->refcnt, 1)) { |
943 | 0 | OPENSSL_free(md); |
944 | 0 | return NULL; |
945 | 0 | } |
946 | 120 | return md; |
947 | 120 | } |
948 | | |
949 | | /* |
950 | | * FIPS module note: since internal fetches will be entirely |
951 | | * provider based, we know that none of its code depends on legacy |
952 | | * NIDs or any functionality that use them. |
953 | | */ |
954 | | #ifndef FIPS_MODULE |
955 | | static void set_legacy_nid(const char *name, void *vlegacy_nid) |
956 | 317 | { |
957 | 317 | int nid; |
958 | 317 | int *legacy_nid = vlegacy_nid; |
959 | | /* |
960 | | * We use lowest level function to get the associated method, because |
961 | | * higher level functions such as EVP_get_digestbyname() have changed |
962 | | * to look at providers too. |
963 | | */ |
964 | 317 | const void *legacy_method = OBJ_NAME_get(name, OBJ_NAME_TYPE_MD_METH); |
965 | | |
966 | 317 | if (*legacy_nid == -1) /* We found a clash already */ |
967 | 0 | return; |
968 | | |
969 | 317 | if (legacy_method == NULL) |
970 | 207 | return; |
971 | 110 | nid = EVP_MD_nid(legacy_method); |
972 | 110 | if (*legacy_nid != NID_undef && *legacy_nid != nid) { |
973 | 0 | *legacy_nid = -1; |
974 | 0 | return; |
975 | 0 | } |
976 | 110 | *legacy_nid = nid; |
977 | 110 | } |
978 | | #endif |
979 | | |
980 | | static int evp_md_cache_constants(EVP_MD *md) |
981 | 120 | { |
982 | 120 | int ok, xof = 0, algid_absent = 0; |
983 | 120 | size_t blksz = 0; |
984 | 120 | size_t mdsize = 0; |
985 | 120 | OSSL_PARAM params[5]; |
986 | | |
987 | 120 | params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_BLOCK_SIZE, &blksz); |
988 | 120 | params[1] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_SIZE, &mdsize); |
989 | 120 | params[2] = OSSL_PARAM_construct_int(OSSL_DIGEST_PARAM_XOF, &xof); |
990 | 120 | params[3] = OSSL_PARAM_construct_int(OSSL_DIGEST_PARAM_ALGID_ABSENT, |
991 | 120 | &algid_absent); |
992 | 120 | params[4] = OSSL_PARAM_construct_end(); |
993 | 120 | ok = evp_do_md_getparams(md, params) > 0; |
994 | 120 | if (mdsize > INT_MAX || blksz > INT_MAX) |
995 | 0 | ok = 0; |
996 | 120 | if (ok) { |
997 | 120 | md->block_size = (int)blksz; |
998 | 120 | md->md_size = (int)mdsize; |
999 | 120 | if (xof) |
1000 | 16 | md->flags |= EVP_MD_FLAG_XOF; |
1001 | 120 | if (algid_absent) |
1002 | 64 | md->flags |= EVP_MD_FLAG_DIGALGID_ABSENT; |
1003 | 120 | } |
1004 | 120 | return ok; |
1005 | 120 | } |
1006 | | |
1007 | | static void *evp_md_from_algorithm(int name_id, |
1008 | | const OSSL_ALGORITHM *algodef, |
1009 | | OSSL_PROVIDER *prov) |
1010 | 120 | { |
1011 | 120 | const OSSL_DISPATCH *fns = algodef->implementation; |
1012 | 120 | EVP_MD *md = NULL; |
1013 | 120 | int fncnt = 0; |
1014 | | |
1015 | | /* EVP_MD_fetch() will set the legacy NID if available */ |
1016 | 120 | if ((md = evp_md_new()) == NULL) { |
1017 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB); |
1018 | 0 | return NULL; |
1019 | 0 | } |
1020 | | |
1021 | 120 | #ifndef FIPS_MODULE |
1022 | 120 | md->type = NID_undef; |
1023 | 120 | if (!evp_names_do_all(prov, name_id, set_legacy_nid, &md->type) |
1024 | 120 | || md->type == -1) { |
1025 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR); |
1026 | 0 | EVP_MD_free(md); |
1027 | 0 | return NULL; |
1028 | 0 | } |
1029 | 120 | #endif |
1030 | | |
1031 | 120 | md->name_id = name_id; |
1032 | 120 | if ((md->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL) { |
1033 | 0 | EVP_MD_free(md); |
1034 | 0 | return NULL; |
1035 | 0 | } |
1036 | 120 | md->description = algodef->algorithm_description; |
1037 | | |
1038 | 1.18k | for (; fns->function_id != 0; fns++) { |
1039 | 1.06k | switch (fns->function_id) { |
1040 | 120 | case OSSL_FUNC_DIGEST_NEWCTX: |
1041 | 120 | if (md->newctx == NULL) { |
1042 | 120 | md->newctx = OSSL_FUNC_digest_newctx(fns); |
1043 | 120 | fncnt++; |
1044 | 120 | } |
1045 | 120 | break; |
1046 | 120 | case OSSL_FUNC_DIGEST_INIT: |
1047 | 120 | if (md->dinit == NULL) { |
1048 | 120 | md->dinit = OSSL_FUNC_digest_init(fns); |
1049 | 120 | fncnt++; |
1050 | 120 | } |
1051 | 120 | break; |
1052 | 120 | case OSSL_FUNC_DIGEST_UPDATE: |
1053 | 120 | if (md->dupdate == NULL) { |
1054 | 120 | md->dupdate = OSSL_FUNC_digest_update(fns); |
1055 | 120 | fncnt++; |
1056 | 120 | } |
1057 | 120 | break; |
1058 | 120 | case OSSL_FUNC_DIGEST_FINAL: |
1059 | 120 | if (md->dfinal == NULL) { |
1060 | 120 | md->dfinal = OSSL_FUNC_digest_final(fns); |
1061 | 120 | fncnt++; |
1062 | 120 | } |
1063 | 120 | break; |
1064 | 16 | case OSSL_FUNC_DIGEST_SQUEEZE: |
1065 | 16 | if (md->dsqueeze == NULL) { |
1066 | 16 | md->dsqueeze = OSSL_FUNC_digest_squeeze(fns); |
1067 | 16 | fncnt++; |
1068 | 16 | } |
1069 | 16 | break; |
1070 | 0 | case OSSL_FUNC_DIGEST_DIGEST: |
1071 | 0 | if (md->digest == NULL) |
1072 | 0 | md->digest = OSSL_FUNC_digest_digest(fns); |
1073 | | /* We don't increment fnct for this as it is stand alone */ |
1074 | 0 | break; |
1075 | 120 | case OSSL_FUNC_DIGEST_FREECTX: |
1076 | 120 | if (md->freectx == NULL) { |
1077 | 120 | md->freectx = OSSL_FUNC_digest_freectx(fns); |
1078 | 120 | fncnt++; |
1079 | 120 | } |
1080 | 120 | break; |
1081 | 120 | case OSSL_FUNC_DIGEST_DUPCTX: |
1082 | 120 | if (md->dupctx == NULL) |
1083 | 120 | md->dupctx = OSSL_FUNC_digest_dupctx(fns); |
1084 | 120 | break; |
1085 | 120 | case OSSL_FUNC_DIGEST_GET_PARAMS: |
1086 | 120 | if (md->get_params == NULL) |
1087 | 120 | md->get_params = OSSL_FUNC_digest_get_params(fns); |
1088 | 120 | break; |
1089 | 35 | case OSSL_FUNC_DIGEST_SET_CTX_PARAMS: |
1090 | 35 | if (md->set_ctx_params == NULL) |
1091 | 35 | md->set_ctx_params = OSSL_FUNC_digest_set_ctx_params(fns); |
1092 | 35 | break; |
1093 | 8 | case OSSL_FUNC_DIGEST_GET_CTX_PARAMS: |
1094 | 8 | if (md->get_ctx_params == NULL) |
1095 | 8 | md->get_ctx_params = OSSL_FUNC_digest_get_ctx_params(fns); |
1096 | 8 | break; |
1097 | 120 | case OSSL_FUNC_DIGEST_GETTABLE_PARAMS: |
1098 | 120 | if (md->gettable_params == NULL) |
1099 | 120 | md->gettable_params = OSSL_FUNC_digest_gettable_params(fns); |
1100 | 120 | break; |
1101 | 35 | case OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS: |
1102 | 35 | if (md->settable_ctx_params == NULL) |
1103 | 35 | md->settable_ctx_params = |
1104 | 35 | OSSL_FUNC_digest_settable_ctx_params(fns); |
1105 | 35 | break; |
1106 | 8 | case OSSL_FUNC_DIGEST_GETTABLE_CTX_PARAMS: |
1107 | 8 | if (md->gettable_ctx_params == NULL) |
1108 | 8 | md->gettable_ctx_params = |
1109 | 8 | OSSL_FUNC_digest_gettable_ctx_params(fns); |
1110 | 8 | break; |
1111 | 1.06k | } |
1112 | 1.06k | } |
1113 | 120 | if ((fncnt != 0 && fncnt != 5 && fncnt != 6) |
1114 | 120 | || (fncnt == 0 && md->digest == NULL)) { |
1115 | | /* |
1116 | | * In order to be a consistent set of functions we either need the |
1117 | | * whole set of init/update/final etc functions or none of them. |
1118 | | * The "digest" function can standalone. We at least need one way to |
1119 | | * generate digests. |
1120 | | */ |
1121 | 0 | EVP_MD_free(md); |
1122 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS); |
1123 | 0 | return NULL; |
1124 | 0 | } |
1125 | 120 | md->prov = prov; |
1126 | 120 | if (prov != NULL) |
1127 | 120 | ossl_provider_up_ref(prov); |
1128 | | |
1129 | 120 | if (!evp_md_cache_constants(md)) { |
1130 | 0 | EVP_MD_free(md); |
1131 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_CACHE_CONSTANTS_FAILED); |
1132 | 0 | md = NULL; |
1133 | 0 | } |
1134 | | |
1135 | 120 | return md; |
1136 | 120 | } |
1137 | | |
1138 | | static int evp_md_up_ref(void *md) |
1139 | 1.08M | { |
1140 | 1.08M | return EVP_MD_up_ref(md); |
1141 | 1.08M | } |
1142 | | |
1143 | | static void evp_md_free(void *md) |
1144 | 197 | { |
1145 | 197 | EVP_MD_free(md); |
1146 | 197 | } |
1147 | | |
1148 | | EVP_MD *EVP_MD_fetch(OSSL_LIB_CTX *ctx, const char *algorithm, |
1149 | | const char *properties) |
1150 | 1.08M | { |
1151 | 1.08M | EVP_MD *md = |
1152 | 1.08M | evp_generic_fetch(ctx, OSSL_OP_DIGEST, algorithm, properties, |
1153 | 1.08M | evp_md_from_algorithm, evp_md_up_ref, evp_md_free); |
1154 | | |
1155 | 1.08M | return md; |
1156 | 1.08M | } |
1157 | | |
1158 | | int EVP_MD_up_ref(EVP_MD *md) |
1159 | 1.09M | { |
1160 | 1.09M | int ref = 0; |
1161 | | |
1162 | 1.09M | if (md->origin == EVP_ORIG_DYNAMIC) |
1163 | 1.09M | CRYPTO_UP_REF(&md->refcnt, &ref); |
1164 | 1.09M | return 1; |
1165 | 1.09M | } |
1166 | | |
1167 | | void EVP_MD_free(EVP_MD *md) |
1168 | 1.11M | { |
1169 | 1.11M | int i; |
1170 | | |
1171 | 1.11M | if (md == NULL || md->origin != EVP_ORIG_DYNAMIC) |
1172 | 19.2k | return; |
1173 | | |
1174 | 1.09M | CRYPTO_DOWN_REF(&md->refcnt, &i); |
1175 | 1.09M | if (i > 0) |
1176 | 1.09M | return; |
1177 | 27 | evp_md_free_int(md); |
1178 | 27 | } |
1179 | | |
1180 | | void EVP_MD_do_all_provided(OSSL_LIB_CTX *libctx, |
1181 | | void (*fn)(EVP_MD *mac, void *arg), |
1182 | | void *arg) |
1183 | 0 | { |
1184 | 0 | evp_generic_do_all(libctx, OSSL_OP_DIGEST, |
1185 | 0 | (void (*)(void *, void *))fn, arg, |
1186 | 0 | evp_md_from_algorithm, evp_md_up_ref, evp_md_free); |
1187 | 0 | } |