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