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