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