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