/src/openssl/crypto/evp/digest.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 1995-2025 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 | | #include <stdio.h> |
11 | | #include <openssl/objects.h> |
12 | | #include <openssl/evp.h> |
13 | | #include <openssl/ec.h> |
14 | | #include <openssl/params.h> |
15 | | #include <openssl/core_names.h> |
16 | | #include "internal/cryptlib.h" |
17 | | #include "internal/nelem.h" |
18 | | #include "internal/provider.h" |
19 | | #include "internal/core.h" |
20 | | #include "internal/common.h" |
21 | | #include "crypto/evp.h" |
22 | | #include "evp_local.h" |
23 | | |
24 | | void evp_md_ctx_clear_digest(EVP_MD_CTX *ctx, int force, int keep_fetched) |
25 | 16.4k | { |
26 | 16.4k | if (ctx->algctx != NULL) { |
27 | 16.4k | if (ctx->digest != NULL && ctx->digest->freectx != NULL) |
28 | 16.4k | ctx->digest->freectx(ctx->algctx); |
29 | 16.4k | ctx->algctx = NULL; |
30 | 16.4k | EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED); |
31 | 16.4k | } |
32 | | |
33 | | /* Code below to be removed when legacy support is dropped. */ |
34 | | |
35 | | /* |
36 | | * Don't assume ctx->md_data was cleaned in EVP_Digest_Final, because |
37 | | * sometimes only copies of the context are ever finalised. |
38 | | */ |
39 | 16.4k | if (force) |
40 | 0 | ctx->digest = NULL; |
41 | | |
42 | | /* Non legacy code, this has to be later than the ctx->digest cleaning */ |
43 | 16.4k | if (!keep_fetched) { |
44 | 16.4k | EVP_MD_free(ctx->fetched_digest); |
45 | 16.4k | ctx->fetched_digest = NULL; |
46 | 16.4k | ctx->reqdigest = NULL; |
47 | 16.4k | } |
48 | 16.4k | } |
49 | | |
50 | | static int evp_md_ctx_reset_ex(EVP_MD_CTX *ctx, int keep_fetched) |
51 | 16.4k | { |
52 | 16.4k | if (ctx == NULL) |
53 | 0 | return 1; |
54 | | |
55 | | /* |
56 | | * pctx should be freed by the user of EVP_MD_CTX |
57 | | * if EVP_MD_CTX_FLAG_KEEP_PKEY_CTX is set |
58 | | */ |
59 | 16.4k | if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX)) { |
60 | 16.4k | EVP_PKEY_CTX_free(ctx->pctx); |
61 | 16.4k | ctx->pctx = NULL; |
62 | 16.4k | } |
63 | | |
64 | 16.4k | evp_md_ctx_clear_digest(ctx, 0, keep_fetched); |
65 | 16.4k | if (!keep_fetched) |
66 | 16.4k | OPENSSL_cleanse(ctx, sizeof(*ctx)); |
67 | | |
68 | 16.4k | return 1; |
69 | 16.4k | } |
70 | | |
71 | | /* This call frees resources associated with the context */ |
72 | | int EVP_MD_CTX_reset(EVP_MD_CTX *ctx) |
73 | 16.4k | { |
74 | 16.4k | return evp_md_ctx_reset_ex(ctx, 0); |
75 | 16.4k | } |
76 | | |
77 | | #ifndef FIPS_MODULE |
78 | | EVP_MD_CTX *evp_md_ctx_new_ex(EVP_PKEY *pkey, const ASN1_OCTET_STRING *id, |
79 | | OSSL_LIB_CTX *libctx, const char *propq) |
80 | 0 | { |
81 | 0 | EVP_MD_CTX *ctx; |
82 | 0 | EVP_PKEY_CTX *pctx = NULL; |
83 | |
|
84 | 0 | if ((ctx = EVP_MD_CTX_new()) == NULL |
85 | 0 | || (pctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq)) == NULL) { |
86 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB); |
87 | 0 | goto err; |
88 | 0 | } |
89 | | |
90 | 0 | if (id != NULL && EVP_PKEY_CTX_set1_id(pctx, id->data, id->length) <= 0) |
91 | 0 | goto err; |
92 | | |
93 | 0 | EVP_MD_CTX_set_pkey_ctx(ctx, pctx); |
94 | 0 | return ctx; |
95 | | |
96 | 0 | err: |
97 | 0 | EVP_PKEY_CTX_free(pctx); |
98 | 0 | EVP_MD_CTX_free(ctx); |
99 | 0 | return NULL; |
100 | 0 | } |
101 | | #endif |
102 | | |
103 | | EVP_MD_CTX *EVP_MD_CTX_new(void) |
104 | 16.4k | { |
105 | 16.4k | return OPENSSL_zalloc(sizeof(EVP_MD_CTX)); |
106 | 16.4k | } |
107 | | |
108 | | void EVP_MD_CTX_free(EVP_MD_CTX *ctx) |
109 | 16.4k | { |
110 | 16.4k | if (ctx == NULL) |
111 | 0 | return; |
112 | | |
113 | 16.4k | EVP_MD_CTX_reset(ctx); |
114 | 16.4k | OPENSSL_free(ctx); |
115 | 16.4k | } |
116 | | |
117 | | int evp_md_ctx_free_algctx(EVP_MD_CTX *ctx) |
118 | 16.4k | { |
119 | 16.4k | if (ctx->algctx != NULL) { |
120 | 0 | if (!ossl_assert(ctx->digest != NULL)) { |
121 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
122 | 0 | return 0; |
123 | 0 | } |
124 | 0 | if (ctx->digest->freectx != NULL) |
125 | 0 | ctx->digest->freectx(ctx->algctx); |
126 | 0 | ctx->algctx = NULL; |
127 | 0 | } |
128 | 16.4k | return 1; |
129 | 16.4k | } |
130 | | |
131 | | static int evp_md_init_internal(EVP_MD_CTX *ctx, const EVP_MD *type, |
132 | | const OSSL_PARAM params[]) |
133 | 32.5k | { |
134 | 32.5k | #if !defined(FIPS_MODULE) |
135 | 32.5k | if (ctx->pctx != NULL |
136 | 0 | && EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx->pctx) |
137 | 0 | && ctx->pctx->op.sig.algctx != NULL) { |
138 | | /* |
139 | | * Prior to OpenSSL 3.0 calling EVP_DigestInit_ex() on an mdctx |
140 | | * previously initialised with EVP_DigestSignInit() would retain |
141 | | * information about the key, and re-initialise for another sign |
142 | | * operation. So in that case we redirect to EVP_DigestSignInit() |
143 | | */ |
144 | 0 | if (ctx->pctx->operation == EVP_PKEY_OP_SIGNCTX) |
145 | 0 | return EVP_DigestSignInit(ctx, NULL, type, NULL, NULL); |
146 | 0 | if (ctx->pctx->operation == EVP_PKEY_OP_VERIFYCTX) |
147 | 0 | return EVP_DigestVerifyInit(ctx, NULL, type, NULL, NULL); |
148 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR); |
149 | 0 | return 0; |
150 | 0 | } |
151 | 32.5k | #endif |
152 | | |
153 | 32.5k | EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_CLEANED | EVP_MD_CTX_FLAG_FINALISED); |
154 | | |
155 | 32.5k | if (type != NULL) { |
156 | 32.5k | ctx->reqdigest = type; |
157 | 32.5k | } else { |
158 | 0 | if (ossl_unlikely(ctx->digest == NULL)) { |
159 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_NO_DIGEST_SET); |
160 | 0 | return 0; |
161 | 0 | } |
162 | 0 | type = ctx->digest; |
163 | 0 | } |
164 | | |
165 | 32.5k | if (ossl_likely(ctx->digest == type)) { |
166 | 16.0k | if (ossl_unlikely(!ossl_assert(type->prov != NULL))) { |
167 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
168 | 0 | return 0; |
169 | 0 | } |
170 | 16.4k | } else { |
171 | 16.4k | if (!evp_md_ctx_free_algctx(ctx)) |
172 | 0 | return 0; |
173 | 16.4k | } |
174 | | |
175 | 32.5k | if (ossl_unlikely(type->prov == NULL)) { |
176 | | #ifdef FIPS_MODULE |
177 | | /* We only do explicit fetches inside the FIPS module */ |
178 | | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
179 | | return 0; |
180 | | #else |
181 | | /* The NULL digest is a special case */ |
182 | 0 | EVP_MD *provmd = EVP_MD_fetch(NULL, |
183 | 0 | type->type != NID_undef ? OBJ_nid2sn(type->type) |
184 | 0 | : "NULL", |
185 | 0 | ""); |
186 | |
|
187 | 0 | if (provmd == NULL) { |
188 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
189 | 0 | return 0; |
190 | 0 | } |
191 | 0 | type = provmd; |
192 | 0 | EVP_MD_free(ctx->fetched_digest); |
193 | 0 | ctx->fetched_digest = provmd; |
194 | 0 | #endif |
195 | 0 | } |
196 | | |
197 | 32.5k | if (ossl_unlikely(type->prov != NULL && ctx->fetched_digest != type)) { |
198 | 16.4k | if (ossl_unlikely(!EVP_MD_up_ref((EVP_MD *)type))) { |
199 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
200 | 0 | return 0; |
201 | 0 | } |
202 | 16.4k | EVP_MD_free(ctx->fetched_digest); |
203 | 16.4k | ctx->fetched_digest = (EVP_MD *)type; |
204 | 16.4k | } |
205 | 32.5k | ctx->digest = type; |
206 | 32.5k | if (ctx->algctx == NULL) { |
207 | 16.4k | ctx->algctx = ctx->digest->newctx(ossl_provider_ctx(type->prov)); |
208 | 16.4k | if (ctx->algctx == NULL) { |
209 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
210 | 0 | return 0; |
211 | 0 | } |
212 | 16.4k | } |
213 | | |
214 | 32.5k | if (ctx->digest->dinit == NULL) { |
215 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
216 | 0 | return 0; |
217 | 0 | } |
218 | | |
219 | 32.5k | return ctx->digest->dinit(ctx->algctx, params); |
220 | 32.5k | } |
221 | | |
222 | | int EVP_DigestInit_ex2(EVP_MD_CTX *ctx, const EVP_MD *type, |
223 | | const OSSL_PARAM params[]) |
224 | 4 | { |
225 | 4 | return evp_md_init_internal(ctx, type, params); |
226 | 4 | } |
227 | | |
228 | | int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type) |
229 | 0 | { |
230 | 0 | EVP_MD_CTX_reset(ctx); |
231 | 0 | return evp_md_init_internal(ctx, type, NULL); |
232 | 0 | } |
233 | | |
234 | | int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl) |
235 | 32.5k | { |
236 | 32.5k | if (!ossl_assert(impl == NULL)) |
237 | 0 | return 0; |
238 | 32.5k | return evp_md_init_internal(ctx, type, NULL); |
239 | 32.5k | } |
240 | | |
241 | | int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t count) |
242 | 163M | { |
243 | 163M | if (ossl_unlikely(count == 0)) |
244 | 0 | return 1; |
245 | | |
246 | 163M | if (ossl_unlikely((ctx->flags & EVP_MD_CTX_FLAG_FINALISED) != 0)) { |
247 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR); |
248 | 0 | return 0; |
249 | 0 | } |
250 | | |
251 | 163M | if (ossl_unlikely(ctx->pctx != NULL) |
252 | 0 | && EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx->pctx) |
253 | 0 | && ctx->pctx->op.sig.algctx != NULL) { |
254 | 0 | #ifndef FIPS_MODULE |
255 | | /* |
256 | | * Prior to OpenSSL 3.0 EVP_DigestSignUpdate() and |
257 | | * EVP_DigestVerifyUpdate() were just macros for EVP_DigestUpdate(). |
258 | | * Some code calls EVP_DigestUpdate() directly even when initialised |
259 | | * with EVP_DigestSignInit_ex() or |
260 | | * EVP_DigestVerifyInit_ex(), so we detect that and redirect to |
261 | | * the correct EVP_Digest*Update() function |
262 | | */ |
263 | 0 | if (ctx->pctx->operation == EVP_PKEY_OP_SIGNCTX) |
264 | 0 | return EVP_DigestSignUpdate(ctx, data, count); |
265 | 0 | if (ctx->pctx->operation == EVP_PKEY_OP_VERIFYCTX) |
266 | 0 | return EVP_DigestVerifyUpdate(ctx, data, count); |
267 | 0 | #endif |
268 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR); |
269 | 0 | return 0; |
270 | 0 | } |
271 | | |
272 | 163M | if (ctx->digest == NULL || ctx->digest->prov == NULL) { |
273 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR); |
274 | 0 | return 0; |
275 | 0 | } |
276 | | |
277 | 163M | if (ossl_unlikely(ctx->digest->dupdate == NULL)) { |
278 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR); |
279 | 0 | return 0; |
280 | 0 | } |
281 | 163M | return ctx->digest->dupdate(ctx->algctx, data, count); |
282 | 163M | } |
283 | | |
284 | | /* The caller can assume that this removes any secret data from the context */ |
285 | | int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size) |
286 | 0 | { |
287 | 0 | int ret; |
288 | 0 | ret = EVP_DigestFinal_ex(ctx, md, size); |
289 | 0 | EVP_MD_CTX_reset(ctx); |
290 | 0 | return ret; |
291 | 0 | } |
292 | | |
293 | | /* The caller can assume that this removes any secret data from the context */ |
294 | | int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *isize) |
295 | 16.1k | { |
296 | 16.1k | int ret, sz; |
297 | 16.1k | size_t size = 0; |
298 | 16.1k | size_t mdsize = 0; |
299 | | |
300 | 16.1k | if (ossl_unlikely(ctx->digest == NULL)) |
301 | 0 | return 0; |
302 | | |
303 | 16.1k | sz = EVP_MD_CTX_get_size(ctx); |
304 | 16.1k | if (ossl_unlikely(sz < 0)) |
305 | 0 | return 0; |
306 | 16.1k | mdsize = sz; |
307 | 16.1k | if (ossl_unlikely(ctx->digest->prov == NULL)) { |
308 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR); |
309 | 0 | return 0; |
310 | 0 | } |
311 | | |
312 | 16.1k | if (ossl_unlikely(ctx->digest->dfinal == NULL)) { |
313 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR); |
314 | 0 | return 0; |
315 | 0 | } |
316 | | |
317 | 16.1k | if (ossl_unlikely((ctx->flags & EVP_MD_CTX_FLAG_FINALISED) != 0)) { |
318 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR); |
319 | 0 | return 0; |
320 | 0 | } |
321 | | |
322 | 16.1k | ret = ctx->digest->dfinal(ctx->algctx, md, &size, mdsize); |
323 | | |
324 | 16.1k | ctx->flags |= EVP_MD_CTX_FLAG_FINALISED; |
325 | | |
326 | 16.1k | if (isize != NULL) { |
327 | 16.1k | if (ossl_likely(size <= UINT_MAX)) { |
328 | 16.1k | *isize = (unsigned int)size; |
329 | 16.1k | } else { |
330 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR); |
331 | 0 | ret = 0; |
332 | 0 | } |
333 | 16.1k | } |
334 | | |
335 | 16.1k | return ret; |
336 | 16.1k | } |
337 | | |
338 | | /* This is a one shot operation */ |
339 | | int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t size) |
340 | 0 | { |
341 | 0 | int ret = 0; |
342 | 0 | OSSL_PARAM params[2]; |
343 | 0 | size_t i = 0; |
344 | |
|
345 | 0 | if (ossl_unlikely(ctx->digest == NULL)) { |
346 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_NULL_ALGORITHM); |
347 | 0 | return 0; |
348 | 0 | } |
349 | | |
350 | 0 | if (ossl_unlikely(ctx->digest->prov == NULL)) { |
351 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR); |
352 | 0 | return 0; |
353 | 0 | } |
354 | | |
355 | 0 | if (ossl_unlikely(ctx->digest->dfinal == NULL)) { |
356 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR); |
357 | 0 | return 0; |
358 | 0 | } |
359 | | |
360 | 0 | if (ossl_unlikely((ctx->flags & EVP_MD_CTX_FLAG_FINALISED) != 0)) { |
361 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR); |
362 | 0 | return 0; |
363 | 0 | } |
364 | | |
365 | | /* |
366 | | * For backward compatibility we pass the XOFLEN via a param here so that |
367 | | * older providers can use the supplied value. Ideally we should have just |
368 | | * used the size passed into ctx->digest->dfinal(). |
369 | | */ |
370 | 0 | params[i++] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN, &size); |
371 | 0 | params[i++] = OSSL_PARAM_construct_end(); |
372 | |
|
373 | 0 | if (ossl_likely(EVP_MD_CTX_set_params(ctx, params) >= 0)) |
374 | 0 | ret = ctx->digest->dfinal(ctx->algctx, md, &size, size); |
375 | |
|
376 | 0 | ctx->flags |= EVP_MD_CTX_FLAG_FINALISED; |
377 | |
|
378 | 0 | return ret; |
379 | 0 | } |
380 | | |
381 | | /* EVP_DigestSqueeze() can be called multiple times */ |
382 | | int EVP_DigestSqueeze(EVP_MD_CTX *ctx, unsigned char *md, size_t size) |
383 | 0 | { |
384 | 0 | if (ctx->digest == NULL) { |
385 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_NULL_ALGORITHM); |
386 | 0 | return 0; |
387 | 0 | } |
388 | | |
389 | 0 | if (ctx->digest->prov == NULL) { |
390 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION); |
391 | 0 | return 0; |
392 | 0 | } |
393 | | |
394 | 0 | if (ctx->digest->dsqueeze == NULL) { |
395 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_METHOD_NOT_SUPPORTED); |
396 | 0 | return 0; |
397 | 0 | } |
398 | | |
399 | 0 | return ctx->digest->dsqueeze(ctx->algctx, md, &size, size); |
400 | 0 | } |
401 | | |
402 | | int EVP_MD_CTX_serialize(EVP_MD_CTX *ctx, unsigned char *out, size_t *outlen) |
403 | 0 | { |
404 | 0 | if (ctx->digest == NULL) { |
405 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_NULL_ALGORITHM); |
406 | 0 | return 0; |
407 | 0 | } |
408 | | |
409 | 0 | if (ctx->digest->prov == NULL) { |
410 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION); |
411 | 0 | return 0; |
412 | 0 | } |
413 | | |
414 | 0 | if (ctx->digest->serialize == NULL) { |
415 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_METHOD_NOT_SUPPORTED); |
416 | 0 | return 0; |
417 | 0 | } |
418 | | |
419 | 0 | if (ossl_unlikely((ctx->flags & EVP_MD_CTX_FLAG_FINALISED) != 0)) { |
420 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_CONTEXT_FINALIZED); |
421 | 0 | return 0; |
422 | 0 | } |
423 | | |
424 | 0 | return ctx->digest->serialize(ctx->algctx, out, outlen); |
425 | 0 | } |
426 | | |
427 | | int EVP_MD_CTX_deserialize(EVP_MD_CTX *ctx, const unsigned char *in, |
428 | | size_t inlen) |
429 | 0 | { |
430 | 0 | if (ctx->digest == NULL) { |
431 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_NULL_ALGORITHM); |
432 | 0 | return 0; |
433 | 0 | } |
434 | | |
435 | 0 | if (ctx->digest->prov == NULL) { |
436 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION); |
437 | 0 | return 0; |
438 | 0 | } |
439 | | |
440 | 0 | if (ctx->digest->deserialize == NULL) { |
441 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_METHOD_NOT_SUPPORTED); |
442 | 0 | return 0; |
443 | 0 | } |
444 | | |
445 | 0 | if (ossl_unlikely((ctx->flags & EVP_MD_CTX_FLAG_FINALISED) != 0)) { |
446 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_CONTEXT_FINALIZED); |
447 | 0 | return 0; |
448 | 0 | } |
449 | | |
450 | 0 | return ctx->digest->deserialize(ctx->algctx, in, inlen); |
451 | 0 | } |
452 | | |
453 | | EVP_MD_CTX *EVP_MD_CTX_dup(const EVP_MD_CTX *in) |
454 | 0 | { |
455 | 0 | EVP_MD_CTX *out = EVP_MD_CTX_new(); |
456 | |
|
457 | 0 | if (out != NULL && !EVP_MD_CTX_copy_ex(out, in)) { |
458 | 0 | EVP_MD_CTX_free(out); |
459 | 0 | out = NULL; |
460 | 0 | } |
461 | 0 | return out; |
462 | 0 | } |
463 | | |
464 | | int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in) |
465 | 0 | { |
466 | 0 | EVP_MD_CTX_reset(out); |
467 | 0 | return EVP_MD_CTX_copy_ex(out, in); |
468 | 0 | } |
469 | | |
470 | | int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in) |
471 | 0 | { |
472 | 0 | int digest_change = 0; |
473 | |
|
474 | 0 | if (in == NULL) { |
475 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER); |
476 | 0 | return 0; |
477 | 0 | } |
478 | | |
479 | 0 | if (in->digest == NULL) { |
480 | | /* copying uninitialized digest context */ |
481 | 0 | EVP_MD_CTX_reset(out); |
482 | 0 | if (out->fetched_digest != NULL) |
483 | 0 | EVP_MD_free(out->fetched_digest); |
484 | 0 | *out = *in; |
485 | 0 | goto clone_pkey; |
486 | 0 | } |
487 | | |
488 | 0 | if (in->digest->prov == NULL || in->digest->dupctx == NULL) { |
489 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX); |
490 | 0 | return 0; |
491 | 0 | } |
492 | | |
493 | 0 | if (out->digest == in->digest && in->digest->copyctx != NULL) { |
494 | |
|
495 | 0 | in->digest->copyctx(out->algctx, in->algctx); |
496 | |
|
497 | 0 | EVP_PKEY_CTX_free(out->pctx); |
498 | 0 | out->pctx = NULL; |
499 | |
|
500 | 0 | out->flags = in->flags; |
501 | 0 | } else { |
502 | 0 | evp_md_ctx_reset_ex(out, 1); |
503 | 0 | digest_change = (out->fetched_digest != in->fetched_digest); |
504 | |
|
505 | 0 | if (digest_change && in->fetched_digest != NULL |
506 | 0 | && !EVP_MD_up_ref(in->fetched_digest)) |
507 | 0 | return 0; |
508 | 0 | if (digest_change && out->fetched_digest != NULL) |
509 | 0 | EVP_MD_free(out->fetched_digest); |
510 | 0 | *out = *in; |
511 | | /* NULL out pointers in case of error */ |
512 | 0 | out->pctx = NULL; |
513 | 0 | out->algctx = NULL; |
514 | |
|
515 | 0 | if (in->algctx != NULL) { |
516 | 0 | out->algctx = in->digest->dupctx(in->algctx); |
517 | 0 | if (out->algctx == NULL) { |
518 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX); |
519 | 0 | return 0; |
520 | 0 | } |
521 | 0 | } |
522 | 0 | } |
523 | | |
524 | 0 | clone_pkey: |
525 | | /* copied EVP_MD_CTX should free the copied EVP_PKEY_CTX */ |
526 | 0 | EVP_MD_CTX_clear_flags(out, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX); |
527 | 0 | #ifndef FIPS_MODULE |
528 | 0 | if (in->pctx != NULL) { |
529 | 0 | out->pctx = EVP_PKEY_CTX_dup(in->pctx); |
530 | 0 | if (out->pctx == NULL) { |
531 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX); |
532 | 0 | EVP_MD_CTX_reset(out); |
533 | 0 | return 0; |
534 | 0 | } |
535 | 0 | } |
536 | 0 | #endif |
537 | | |
538 | 0 | return 1; |
539 | 0 | } |
540 | | |
541 | | int EVP_Digest(const void *data, size_t count, |
542 | | unsigned char *md, unsigned int *size, const EVP_MD *type, |
543 | | ENGINE *impl) |
544 | 0 | { |
545 | 0 | EVP_MD_CTX *ctx; |
546 | 0 | int ret; |
547 | |
|
548 | 0 | if (!ossl_assert(impl == NULL)) |
549 | 0 | return 0; |
550 | | |
551 | 0 | ctx = EVP_MD_CTX_new(); |
552 | 0 | if (ctx == NULL) |
553 | 0 | return 0; |
554 | 0 | EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT); |
555 | 0 | ret = EVP_DigestInit_ex(ctx, type, NULL) |
556 | 0 | && EVP_DigestUpdate(ctx, data, count) |
557 | 0 | && EVP_DigestFinal_ex(ctx, md, size); |
558 | 0 | EVP_MD_CTX_free(ctx); |
559 | |
|
560 | 0 | return ret; |
561 | 0 | } |
562 | | |
563 | | int EVP_Q_digest(OSSL_LIB_CTX *libctx, const char *name, const char *propq, |
564 | | const void *data, size_t datalen, |
565 | | unsigned char *md, size_t *mdlen) |
566 | 0 | { |
567 | 0 | EVP_MD *digest = EVP_MD_fetch(libctx, name, propq); |
568 | 0 | unsigned int temp = 0; |
569 | 0 | int ret = 0; |
570 | |
|
571 | 0 | if (digest != NULL) { |
572 | 0 | ret = EVP_Digest(data, datalen, md, &temp, digest, NULL); |
573 | 0 | EVP_MD_free(digest); |
574 | 0 | } |
575 | 0 | if (mdlen != NULL) |
576 | 0 | *mdlen = temp; |
577 | 0 | return ret; |
578 | 0 | } |
579 | | |
580 | | int EVP_MD_get_params(const EVP_MD *digest, OSSL_PARAM params[]) |
581 | 0 | { |
582 | 0 | if (digest != NULL && digest->get_params != NULL) |
583 | 0 | return digest->get_params(params); |
584 | 0 | return 0; |
585 | 0 | } |
586 | | |
587 | | const OSSL_PARAM *EVP_MD_gettable_params(const EVP_MD *digest) |
588 | 0 | { |
589 | 0 | if (digest != NULL && digest->gettable_params != NULL) |
590 | 0 | return digest->gettable_params( |
591 | 0 | ossl_provider_ctx(EVP_MD_get0_provider(digest))); |
592 | 0 | return NULL; |
593 | 0 | } |
594 | | |
595 | | int EVP_MD_CTX_set_params(EVP_MD_CTX *ctx, const OSSL_PARAM params[]) |
596 | 0 | { |
597 | 0 | EVP_PKEY_CTX *pctx = ctx->pctx; |
598 | | |
599 | | /* If we have a pctx then we should try that first */ |
600 | 0 | if (ossl_unlikely(pctx != NULL) |
601 | 0 | && (pctx->operation == EVP_PKEY_OP_VERIFYCTX |
602 | 0 | || pctx->operation == EVP_PKEY_OP_SIGNCTX) |
603 | 0 | && pctx->op.sig.algctx != NULL |
604 | 0 | && pctx->op.sig.signature->set_ctx_md_params != NULL) |
605 | 0 | return pctx->op.sig.signature->set_ctx_md_params(pctx->op.sig.algctx, |
606 | 0 | params); |
607 | | |
608 | 0 | if (ossl_likely(ctx->digest != NULL && ctx->digest->set_ctx_params != NULL)) |
609 | 0 | return ctx->digest->set_ctx_params(ctx->algctx, params); |
610 | | |
611 | 0 | return 0; |
612 | 0 | } |
613 | | |
614 | | const OSSL_PARAM *EVP_MD_settable_ctx_params(const EVP_MD *md) |
615 | 0 | { |
616 | 0 | void *provctx; |
617 | |
|
618 | 0 | if (md != NULL && md->settable_ctx_params != NULL) { |
619 | 0 | provctx = ossl_provider_ctx(EVP_MD_get0_provider(md)); |
620 | 0 | return md->settable_ctx_params(NULL, provctx); |
621 | 0 | } |
622 | 0 | return NULL; |
623 | 0 | } |
624 | | |
625 | | const OSSL_PARAM *EVP_MD_CTX_settable_params(EVP_MD_CTX *ctx) |
626 | 0 | { |
627 | 0 | EVP_PKEY_CTX *pctx; |
628 | 0 | void *alg; |
629 | |
|
630 | 0 | if (ctx == NULL) |
631 | 0 | return NULL; |
632 | | |
633 | | /* If we have a pctx then we should try that first */ |
634 | 0 | pctx = ctx->pctx; |
635 | 0 | if (pctx != NULL |
636 | 0 | && (pctx->operation == EVP_PKEY_OP_VERIFYCTX |
637 | 0 | || pctx->operation == EVP_PKEY_OP_SIGNCTX) |
638 | 0 | && pctx->op.sig.algctx != NULL |
639 | 0 | && pctx->op.sig.signature->settable_ctx_md_params != NULL) |
640 | 0 | return pctx->op.sig.signature->settable_ctx_md_params( |
641 | 0 | pctx->op.sig.algctx); |
642 | | |
643 | 0 | if (ctx->digest != NULL && ctx->digest->settable_ctx_params != NULL) { |
644 | 0 | alg = ossl_provider_ctx(EVP_MD_get0_provider(ctx->digest)); |
645 | 0 | return ctx->digest->settable_ctx_params(ctx->algctx, alg); |
646 | 0 | } |
647 | | |
648 | 0 | return NULL; |
649 | 0 | } |
650 | | |
651 | | int EVP_MD_CTX_get_params(EVP_MD_CTX *ctx, OSSL_PARAM params[]) |
652 | 143 | { |
653 | 143 | EVP_PKEY_CTX *pctx = ctx->pctx; |
654 | | |
655 | | /* If we have a pctx then we should try that first */ |
656 | 143 | if (pctx != NULL |
657 | 0 | && (pctx->operation == EVP_PKEY_OP_VERIFYCTX |
658 | 0 | || pctx->operation == EVP_PKEY_OP_SIGNCTX) |
659 | 0 | && pctx->op.sig.algctx != NULL |
660 | 0 | && pctx->op.sig.signature->get_ctx_md_params != NULL) |
661 | 0 | return pctx->op.sig.signature->get_ctx_md_params(pctx->op.sig.algctx, |
662 | 0 | params); |
663 | | |
664 | 143 | if (ctx->digest != NULL && ctx->digest->get_ctx_params != NULL) |
665 | 143 | return ctx->digest->get_ctx_params(ctx->algctx, params); |
666 | | |
667 | 0 | return 0; |
668 | 143 | } |
669 | | |
670 | | const OSSL_PARAM *EVP_MD_gettable_ctx_params(const EVP_MD *md) |
671 | 0 | { |
672 | 0 | void *provctx; |
673 | |
|
674 | 0 | if (md != NULL && md->gettable_ctx_params != NULL) { |
675 | 0 | provctx = ossl_provider_ctx(EVP_MD_get0_provider(md)); |
676 | 0 | return md->gettable_ctx_params(NULL, provctx); |
677 | 0 | } |
678 | 0 | return NULL; |
679 | 0 | } |
680 | | |
681 | | const OSSL_PARAM *EVP_MD_CTX_gettable_params(EVP_MD_CTX *ctx) |
682 | 16.1k | { |
683 | 16.1k | EVP_PKEY_CTX *pctx; |
684 | 16.1k | void *provctx; |
685 | | |
686 | 16.1k | if (ossl_unlikely(ctx == NULL)) |
687 | 0 | return NULL; |
688 | | |
689 | | /* If we have a pctx then we should try that first */ |
690 | 16.1k | pctx = ctx->pctx; |
691 | 16.1k | if (ossl_unlikely(pctx != NULL) |
692 | 0 | && (pctx->operation == EVP_PKEY_OP_VERIFYCTX |
693 | 0 | || pctx->operation == EVP_PKEY_OP_SIGNCTX) |
694 | 0 | && pctx->op.sig.algctx != NULL |
695 | 0 | && pctx->op.sig.signature->gettable_ctx_md_params != NULL) |
696 | 0 | return pctx->op.sig.signature->gettable_ctx_md_params( |
697 | 0 | pctx->op.sig.algctx); |
698 | | |
699 | 16.1k | if (ossl_unlikely(ctx->digest != NULL |
700 | 16.1k | && ctx->digest->gettable_ctx_params != NULL)) { |
701 | 143 | provctx = ossl_provider_ctx(EVP_MD_get0_provider(ctx->digest)); |
702 | 143 | return ctx->digest->gettable_ctx_params(ctx->algctx, provctx); |
703 | 143 | } |
704 | 15.9k | return NULL; |
705 | 16.1k | } |
706 | | |
707 | | int EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void *p2) |
708 | 0 | { |
709 | 0 | int ret = EVP_CTRL_RET_UNSUPPORTED; |
710 | 0 | int set_params = 1; |
711 | 0 | size_t sz; |
712 | 0 | OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; |
713 | |
|
714 | 0 | if (ctx == NULL) { |
715 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER); |
716 | 0 | return 0; |
717 | 0 | } |
718 | | |
719 | 0 | if (ctx->digest != NULL && ctx->digest->prov == NULL) { |
720 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_NOT_IMPLEMENTED); |
721 | 0 | return 0; |
722 | 0 | } |
723 | | |
724 | 0 | switch (cmd) { |
725 | 0 | case EVP_MD_CTRL_XOF_LEN: |
726 | 0 | sz = (size_t)p1; |
727 | 0 | params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN, &sz); |
728 | 0 | break; |
729 | 0 | case EVP_MD_CTRL_MICALG: |
730 | 0 | set_params = 0; |
731 | 0 | params[0] = OSSL_PARAM_construct_utf8_string(OSSL_DIGEST_PARAM_MICALG, |
732 | 0 | p2, p1 ? p1 : 9999); |
733 | 0 | break; |
734 | 0 | case EVP_CTRL_SSL3_MASTER_SECRET: |
735 | 0 | params[0] = OSSL_PARAM_construct_octet_string(OSSL_DIGEST_PARAM_SSL3_MS, |
736 | 0 | p2, p1); |
737 | 0 | break; |
738 | 0 | default: |
739 | 0 | goto conclude; |
740 | 0 | } |
741 | | |
742 | 0 | if (set_params) |
743 | 0 | ret = EVP_MD_CTX_set_params(ctx, params); |
744 | 0 | else |
745 | 0 | ret = EVP_MD_CTX_get_params(ctx, params); |
746 | |
|
747 | 0 | conclude: |
748 | 0 | if (ret <= 0) |
749 | 0 | return 0; |
750 | 0 | return ret; |
751 | 0 | } |
752 | | |
753 | | EVP_MD *evp_md_new(void) |
754 | 132 | { |
755 | 132 | EVP_MD *md = OPENSSL_zalloc(sizeof(*md)); |
756 | | |
757 | 132 | if (md != NULL && !CRYPTO_NEW_REF(&md->refcnt, 1)) { |
758 | 0 | OPENSSL_free(md); |
759 | 0 | return NULL; |
760 | 0 | } |
761 | 132 | return md; |
762 | 132 | } |
763 | | |
764 | | /* |
765 | | * FIPS module note: since internal fetches will be entirely |
766 | | * provider based, we know that none of its code depends on legacy |
767 | | * NIDs or any functionality that use them. |
768 | | */ |
769 | | #ifndef FIPS_MODULE |
770 | | static void set_legacy_nid(const char *name, void *vlegacy_nid) |
771 | 345 | { |
772 | 345 | int nid; |
773 | 345 | int *legacy_nid = vlegacy_nid; |
774 | | /* |
775 | | * We use lowest level function to get the associated method, because |
776 | | * higher level functions such as EVP_get_digestbyname() have changed |
777 | | * to look at providers too. |
778 | | */ |
779 | 345 | const void *legacy_method = OBJ_NAME_get(name, OBJ_NAME_TYPE_MD_METH); |
780 | | |
781 | 345 | if (*legacy_nid == -1) /* We found a clash already */ |
782 | 0 | return; |
783 | | |
784 | 345 | if (legacy_method == NULL) |
785 | 235 | return; |
786 | 110 | nid = EVP_MD_nid(legacy_method); |
787 | 110 | if (*legacy_nid != NID_undef && *legacy_nid != nid) { |
788 | 0 | *legacy_nid = -1; |
789 | 0 | return; |
790 | 0 | } |
791 | 110 | *legacy_nid = nid; |
792 | 110 | } |
793 | | #endif |
794 | | |
795 | | static int evp_md_cache_constants(EVP_MD *md) |
796 | 132 | { |
797 | 132 | int ok, xof = 0, algid_absent = 0; |
798 | 132 | size_t blksz = 0; |
799 | 132 | size_t mdsize = 0; |
800 | 132 | OSSL_PARAM params[5]; |
801 | | |
802 | | /* |
803 | | * Note that these parameters are 'constants' that are only set up |
804 | | * during the EVP_MD_fetch(). For this reason the XOF functions set the |
805 | | * md_size to 0, since the output size is unknown. |
806 | | */ |
807 | 132 | params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_BLOCK_SIZE, &blksz); |
808 | 132 | params[1] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_SIZE, &mdsize); |
809 | 132 | params[2] = OSSL_PARAM_construct_int(OSSL_DIGEST_PARAM_XOF, &xof); |
810 | 132 | params[3] = OSSL_PARAM_construct_int(OSSL_DIGEST_PARAM_ALGID_ABSENT, |
811 | 132 | &algid_absent); |
812 | 132 | params[4] = OSSL_PARAM_construct_end(); |
813 | 132 | ok = evp_do_md_getparams(md, params) > 0; |
814 | 132 | if (mdsize > INT_MAX || blksz > INT_MAX) |
815 | 0 | ok = 0; |
816 | 132 | if (ok) { |
817 | 132 | md->block_size = (int)blksz; |
818 | 132 | md->md_size = (int)mdsize; |
819 | 132 | if (xof) |
820 | 24 | md->flags |= EVP_MD_FLAG_XOF; |
821 | 132 | if (algid_absent) |
822 | 84 | md->flags |= EVP_MD_FLAG_DIGALGID_ABSENT; |
823 | 132 | } |
824 | 132 | return ok; |
825 | 132 | } |
826 | | |
827 | | static void *evp_md_from_algorithm(int name_id, |
828 | | const OSSL_ALGORITHM *algodef, |
829 | | OSSL_PROVIDER *prov) |
830 | 132 | { |
831 | 132 | const OSSL_DISPATCH *fns = algodef->implementation; |
832 | 132 | EVP_MD *md = NULL; |
833 | 132 | int fncnt = 0; |
834 | | |
835 | | /* EVP_MD_fetch() will set the legacy NID if available */ |
836 | 132 | if ((md = evp_md_new()) == NULL) { |
837 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB); |
838 | 0 | return NULL; |
839 | 0 | } |
840 | | |
841 | 132 | #ifndef FIPS_MODULE |
842 | 132 | md->type = NID_undef; |
843 | 132 | if (!evp_names_do_all(prov, name_id, set_legacy_nid, &md->type) |
844 | 132 | || md->type == -1) { |
845 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR); |
846 | 0 | goto err; |
847 | 0 | } |
848 | 132 | #endif |
849 | | |
850 | 132 | md->name_id = name_id; |
851 | 132 | if ((md->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL) |
852 | 0 | goto err; |
853 | | |
854 | 132 | md->description = algodef->algorithm_description; |
855 | | |
856 | 1.65k | for (; fns->function_id != 0; fns++) { |
857 | 1.51k | switch (fns->function_id) { |
858 | 132 | case OSSL_FUNC_DIGEST_NEWCTX: |
859 | 132 | if (md->newctx == NULL) { |
860 | 132 | md->newctx = OSSL_FUNC_digest_newctx(fns); |
861 | 132 | fncnt++; |
862 | 132 | } |
863 | 132 | break; |
864 | 132 | case OSSL_FUNC_DIGEST_INIT: |
865 | 132 | if (md->dinit == NULL) { |
866 | 132 | md->dinit = OSSL_FUNC_digest_init(fns); |
867 | 132 | fncnt++; |
868 | 132 | } |
869 | 132 | break; |
870 | 132 | case OSSL_FUNC_DIGEST_UPDATE: |
871 | 132 | if (md->dupdate == NULL) { |
872 | 132 | md->dupdate = OSSL_FUNC_digest_update(fns); |
873 | 132 | fncnt++; |
874 | 132 | } |
875 | 132 | break; |
876 | 132 | case OSSL_FUNC_DIGEST_FINAL: |
877 | 132 | if (md->dfinal == NULL) { |
878 | 132 | md->dfinal = OSSL_FUNC_digest_final(fns); |
879 | 132 | fncnt++; |
880 | 132 | } |
881 | 132 | break; |
882 | 24 | case OSSL_FUNC_DIGEST_SQUEEZE: |
883 | 24 | if (md->dsqueeze == NULL) { |
884 | 24 | md->dsqueeze = OSSL_FUNC_digest_squeeze(fns); |
885 | 24 | fncnt++; |
886 | 24 | } |
887 | 24 | break; |
888 | 0 | case OSSL_FUNC_DIGEST_DIGEST: |
889 | 0 | if (md->digest == NULL) |
890 | 0 | md->digest = OSSL_FUNC_digest_digest(fns); |
891 | | /* We don't increment fnct for this as it is stand alone */ |
892 | 0 | break; |
893 | 132 | case OSSL_FUNC_DIGEST_FREECTX: |
894 | 132 | if (md->freectx == NULL) { |
895 | 132 | md->freectx = OSSL_FUNC_digest_freectx(fns); |
896 | 132 | fncnt++; |
897 | 132 | } |
898 | 132 | break; |
899 | 132 | case OSSL_FUNC_DIGEST_DUPCTX: |
900 | 132 | if (md->dupctx == NULL) |
901 | 132 | md->dupctx = OSSL_FUNC_digest_dupctx(fns); |
902 | 132 | break; |
903 | 132 | case OSSL_FUNC_DIGEST_GET_PARAMS: |
904 | 132 | if (md->get_params == NULL) |
905 | 132 | md->get_params = OSSL_FUNC_digest_get_params(fns); |
906 | 132 | break; |
907 | 47 | case OSSL_FUNC_DIGEST_SET_CTX_PARAMS: |
908 | 47 | if (md->set_ctx_params == NULL) |
909 | 47 | md->set_ctx_params = OSSL_FUNC_digest_set_ctx_params(fns); |
910 | 47 | break; |
911 | 36 | case OSSL_FUNC_DIGEST_GET_CTX_PARAMS: |
912 | 36 | if (md->get_ctx_params == NULL) |
913 | 36 | md->get_ctx_params = OSSL_FUNC_digest_get_ctx_params(fns); |
914 | 36 | break; |
915 | 132 | case OSSL_FUNC_DIGEST_GETTABLE_PARAMS: |
916 | 132 | if (md->gettable_params == NULL) |
917 | 132 | md->gettable_params = OSSL_FUNC_digest_gettable_params(fns); |
918 | 132 | break; |
919 | 47 | case OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS: |
920 | 47 | if (md->settable_ctx_params == NULL) |
921 | 47 | md->settable_ctx_params = OSSL_FUNC_digest_settable_ctx_params(fns); |
922 | 47 | break; |
923 | 36 | case OSSL_FUNC_DIGEST_GETTABLE_CTX_PARAMS: |
924 | 36 | if (md->gettable_ctx_params == NULL) |
925 | 36 | md->gettable_ctx_params = OSSL_FUNC_digest_gettable_ctx_params(fns); |
926 | 36 | break; |
927 | 120 | case OSSL_FUNC_DIGEST_COPYCTX: |
928 | 120 | if (md->copyctx == NULL) |
929 | 120 | md->copyctx = OSSL_FUNC_digest_copyctx(fns); |
930 | 120 | break; |
931 | 76 | case OSSL_FUNC_DIGEST_SERIALIZE: |
932 | 76 | if (md->serialize == NULL) |
933 | 76 | md->serialize = OSSL_FUNC_digest_serialize(fns); |
934 | 76 | break; |
935 | 76 | case OSSL_FUNC_DIGEST_DESERIALIZE: |
936 | 76 | if (md->deserialize == NULL) |
937 | 76 | md->deserialize = OSSL_FUNC_digest_deserialize(fns); |
938 | 76 | break; |
939 | 1.51k | } |
940 | 1.51k | } |
941 | 132 | if ((fncnt != 0 && fncnt != 5 && fncnt != 6) |
942 | 132 | || (fncnt == 0 && md->digest == NULL)) { |
943 | | /* |
944 | | * In order to be a consistent set of functions we either need the |
945 | | * whole set of init/update/final etc functions or none of them. |
946 | | * The "digest" function can standalone. We at least need one way to |
947 | | * generate digests. |
948 | | */ |
949 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS); |
950 | 0 | goto err; |
951 | 0 | } |
952 | 132 | if (prov != NULL && !ossl_provider_up_ref(prov)) |
953 | 0 | goto err; |
954 | | |
955 | 132 | md->prov = prov; |
956 | | |
957 | 132 | if (!evp_md_cache_constants(md)) { |
958 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_CACHE_CONSTANTS_FAILED); |
959 | 0 | goto err; |
960 | 0 | } |
961 | | |
962 | 132 | return md; |
963 | | |
964 | 0 | err: |
965 | 0 | EVP_MD_free(md); |
966 | 0 | return NULL; |
967 | 132 | } |
968 | | |
969 | | static int evp_md_up_ref(void *md) |
970 | 895k | { |
971 | 895k | return EVP_MD_up_ref(md); |
972 | 895k | } |
973 | | |
974 | | static void evp_md_free(void *md) |
975 | 337 | { |
976 | 337 | EVP_MD_free(md); |
977 | 337 | } |
978 | | |
979 | | EVP_MD *EVP_MD_fetch(OSSL_LIB_CTX *ctx, const char *algorithm, |
980 | | const char *properties) |
981 | 895k | { |
982 | 895k | EVP_MD *md = evp_generic_fetch(ctx, OSSL_OP_DIGEST, algorithm, properties, |
983 | 895k | evp_md_from_algorithm, evp_md_up_ref, evp_md_free); |
984 | | |
985 | 895k | return md; |
986 | 895k | } |
987 | | |
988 | | int EVP_MD_up_ref(EVP_MD *md) |
989 | 911k | { |
990 | 911k | int ref = 0; |
991 | | |
992 | 911k | if (md->origin == EVP_ORIG_DYNAMIC) |
993 | 911k | CRYPTO_UP_REF(&md->refcnt, &ref); |
994 | 911k | return 1; |
995 | 911k | } |
996 | | |
997 | | void EVP_MD_free(EVP_MD *md) |
998 | 928k | { |
999 | 928k | int i; |
1000 | | |
1001 | 928k | if (md == NULL || md->origin != EVP_ORIG_DYNAMIC) |
1002 | 16.4k | return; |
1003 | | |
1004 | 911k | CRYPTO_DOWN_REF(&md->refcnt, &i); |
1005 | 911k | if (i > 0) |
1006 | 911k | return; |
1007 | | |
1008 | 0 | OPENSSL_free(md->type_name); |
1009 | 0 | ossl_provider_free(md->prov); |
1010 | 0 | CRYPTO_FREE_REF(&md->refcnt); |
1011 | 0 | OPENSSL_free(md); |
1012 | 0 | } |
1013 | | |
1014 | | void EVP_MD_do_all_provided(OSSL_LIB_CTX *libctx, |
1015 | | void (*fn)(EVP_MD *mac, void *arg), |
1016 | | void *arg) |
1017 | 0 | { |
1018 | 0 | evp_generic_do_all(libctx, OSSL_OP_DIGEST, |
1019 | 0 | (void (*)(void *, void *))fn, arg, |
1020 | 0 | evp_md_from_algorithm, evp_md_up_ref, evp_md_free); |
1021 | 0 | } |
1022 | | |
1023 | | EVP_MD *evp_digest_fetch_from_prov(OSSL_PROVIDER *prov, |
1024 | | const char *algorithm, |
1025 | | const char *properties) |
1026 | 0 | { |
1027 | 0 | return evp_generic_fetch_from_prov(prov, OSSL_OP_DIGEST, |
1028 | 0 | algorithm, properties, |
1029 | 0 | evp_md_from_algorithm, |
1030 | 0 | evp_md_up_ref, |
1031 | 0 | evp_md_free); |
1032 | 0 | } |
1033 | | |
1034 | | typedef struct { |
1035 | | int md_nid; |
1036 | | int hmac_nid; |
1037 | | } ossl_hmacmd_pair; |
1038 | | |
1039 | | static const ossl_hmacmd_pair ossl_hmacmd_pairs[] = { |
1040 | | { NID_sha1, NID_hmacWithSHA1 }, |
1041 | | { NID_md5, NID_hmacWithMD5 }, |
1042 | | { NID_sha224, NID_hmacWithSHA224 }, |
1043 | | { NID_sha256, NID_hmacWithSHA256 }, |
1044 | | { NID_sha384, NID_hmacWithSHA384 }, |
1045 | | { NID_sha512, NID_hmacWithSHA512 }, |
1046 | | { NID_id_GostR3411_94, NID_id_HMACGostR3411_94 }, |
1047 | | { NID_id_GostR3411_2012_256, NID_id_tc26_hmac_gost_3411_2012_256 }, |
1048 | | { NID_id_GostR3411_2012_512, NID_id_tc26_hmac_gost_3411_2012_512 }, |
1049 | | { NID_sha3_224, NID_hmac_sha3_224 }, |
1050 | | { NID_sha3_256, NID_hmac_sha3_256 }, |
1051 | | { NID_sha3_384, NID_hmac_sha3_384 }, |
1052 | | { NID_sha3_512, NID_hmac_sha3_512 }, |
1053 | | { NID_sha512_224, NID_hmacWithSHA512_224 }, |
1054 | | { NID_sha512_256, NID_hmacWithSHA512_256 } |
1055 | | }; |
1056 | | |
1057 | | int ossl_hmac2mdnid(int hmac_nid) |
1058 | 0 | { |
1059 | 0 | int md_nid = NID_undef; |
1060 | 0 | size_t i; |
1061 | |
|
1062 | 0 | for (i = 0; i < OSSL_NELEM(ossl_hmacmd_pairs); i++) { |
1063 | 0 | if (ossl_hmacmd_pairs[i].hmac_nid == hmac_nid) { |
1064 | 0 | md_nid = ossl_hmacmd_pairs[i].md_nid; |
1065 | 0 | break; |
1066 | 0 | } |
1067 | 0 | } |
1068 | |
|
1069 | 0 | return md_nid; |
1070 | 0 | } |
1071 | | |
1072 | | int ossl_md2hmacnid(int md_nid) |
1073 | 0 | { |
1074 | 0 | int hmac_nid = NID_undef; |
1075 | 0 | size_t i; |
1076 | |
|
1077 | 0 | for (i = 0; i < OSSL_NELEM(ossl_hmacmd_pairs); i++) { |
1078 | 0 | if (ossl_hmacmd_pairs[i].md_nid == md_nid) { |
1079 | 0 | hmac_nid = ossl_hmacmd_pairs[i].hmac_nid; |
1080 | 0 | break; |
1081 | 0 | } |
1082 | 0 | } |
1083 | |
|
1084 | 0 | return hmac_nid; |
1085 | 0 | } |