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