xref: /third_party/openssl/crypto/evp/m_sigver.c (revision e1051a39)
1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 2006-2022 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci#include <stdio.h>
11e1051a39Sopenharmony_ci#include "internal/cryptlib.h"
12e1051a39Sopenharmony_ci#include <openssl/evp.h>
13e1051a39Sopenharmony_ci#include <openssl/objects.h>
14e1051a39Sopenharmony_ci#include "crypto/evp.h"
15e1051a39Sopenharmony_ci#include "internal/provider.h"
16e1051a39Sopenharmony_ci#include "internal/numbers.h"   /* includes SIZE_MAX */
17e1051a39Sopenharmony_ci#include "evp_local.h"
18e1051a39Sopenharmony_ci
19e1051a39Sopenharmony_ci#ifndef FIPS_MODULE
20e1051a39Sopenharmony_ci
21e1051a39Sopenharmony_cistatic int update(EVP_MD_CTX *ctx, const void *data, size_t datalen)
22e1051a39Sopenharmony_ci{
23e1051a39Sopenharmony_ci    ERR_raise(ERR_LIB_EVP, EVP_R_ONLY_ONESHOT_SUPPORTED);
24e1051a39Sopenharmony_ci    return 0;
25e1051a39Sopenharmony_ci}
26e1051a39Sopenharmony_ci
27e1051a39Sopenharmony_ci/*
28e1051a39Sopenharmony_ci * If we get the "NULL" md then the name comes back as "UNDEF". We want to use
29e1051a39Sopenharmony_ci * NULL for this.
30e1051a39Sopenharmony_ci */
31e1051a39Sopenharmony_cistatic const char *canon_mdname(const char *mdname)
32e1051a39Sopenharmony_ci{
33e1051a39Sopenharmony_ci    if (mdname != NULL && strcmp(mdname, "UNDEF") == 0)
34e1051a39Sopenharmony_ci        return NULL;
35e1051a39Sopenharmony_ci
36e1051a39Sopenharmony_ci    return mdname;
37e1051a39Sopenharmony_ci}
38e1051a39Sopenharmony_ci
39e1051a39Sopenharmony_cistatic int do_sigver_init(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
40e1051a39Sopenharmony_ci                          const EVP_MD *type, const char *mdname,
41e1051a39Sopenharmony_ci                          OSSL_LIB_CTX *libctx, const char *props,
42e1051a39Sopenharmony_ci                          ENGINE *e, EVP_PKEY *pkey, int ver,
43e1051a39Sopenharmony_ci                          const OSSL_PARAM params[])
44e1051a39Sopenharmony_ci{
45e1051a39Sopenharmony_ci    EVP_PKEY_CTX *locpctx = NULL;
46e1051a39Sopenharmony_ci    EVP_SIGNATURE *signature = NULL;
47e1051a39Sopenharmony_ci    EVP_KEYMGMT *tmp_keymgmt = NULL;
48e1051a39Sopenharmony_ci    const OSSL_PROVIDER *tmp_prov = NULL;
49e1051a39Sopenharmony_ci    const char *supported_sig = NULL;
50e1051a39Sopenharmony_ci    char locmdname[80] = "";     /* 80 chars should be enough */
51e1051a39Sopenharmony_ci    void *provkey = NULL;
52e1051a39Sopenharmony_ci    int ret, iter, reinit = 1;
53e1051a39Sopenharmony_ci
54e1051a39Sopenharmony_ci    if (ctx->algctx != NULL) {
55e1051a39Sopenharmony_ci        if (!ossl_assert(ctx->digest != NULL)) {
56e1051a39Sopenharmony_ci            ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
57e1051a39Sopenharmony_ci            return 0;
58e1051a39Sopenharmony_ci        }
59e1051a39Sopenharmony_ci        if (ctx->digest->freectx != NULL)
60e1051a39Sopenharmony_ci            ctx->digest->freectx(ctx->algctx);
61e1051a39Sopenharmony_ci        ctx->algctx = NULL;
62e1051a39Sopenharmony_ci    }
63e1051a39Sopenharmony_ci
64e1051a39Sopenharmony_ci    if (ctx->pctx == NULL) {
65e1051a39Sopenharmony_ci        reinit = 0;
66e1051a39Sopenharmony_ci        if (e == NULL)
67e1051a39Sopenharmony_ci            ctx->pctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, props);
68e1051a39Sopenharmony_ci        else
69e1051a39Sopenharmony_ci            ctx->pctx = EVP_PKEY_CTX_new(pkey, e);
70e1051a39Sopenharmony_ci    }
71e1051a39Sopenharmony_ci    if (ctx->pctx == NULL)
72e1051a39Sopenharmony_ci        return 0;
73e1051a39Sopenharmony_ci
74e1051a39Sopenharmony_ci    locpctx = ctx->pctx;
75e1051a39Sopenharmony_ci    ERR_set_mark();
76e1051a39Sopenharmony_ci
77e1051a39Sopenharmony_ci    if (evp_pkey_ctx_is_legacy(locpctx))
78e1051a39Sopenharmony_ci        goto legacy;
79e1051a39Sopenharmony_ci
80e1051a39Sopenharmony_ci    /* do not reinitialize if pkey is set or operation is different */
81e1051a39Sopenharmony_ci    if (reinit
82e1051a39Sopenharmony_ci        && (pkey != NULL
83e1051a39Sopenharmony_ci            || locpctx->operation != (ver ? EVP_PKEY_OP_VERIFYCTX
84e1051a39Sopenharmony_ci                                          : EVP_PKEY_OP_SIGNCTX)
85e1051a39Sopenharmony_ci            || (signature = locpctx->op.sig.signature) == NULL
86e1051a39Sopenharmony_ci            || locpctx->op.sig.algctx == NULL))
87e1051a39Sopenharmony_ci        reinit = 0;
88e1051a39Sopenharmony_ci
89e1051a39Sopenharmony_ci    if (props == NULL)
90e1051a39Sopenharmony_ci        props = locpctx->propquery;
91e1051a39Sopenharmony_ci
92e1051a39Sopenharmony_ci    if (locpctx->pkey == NULL) {
93e1051a39Sopenharmony_ci        ERR_clear_last_mark();
94e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET);
95e1051a39Sopenharmony_ci        goto err;
96e1051a39Sopenharmony_ci    }
97e1051a39Sopenharmony_ci
98e1051a39Sopenharmony_ci    if (!reinit) {
99e1051a39Sopenharmony_ci        evp_pkey_ctx_free_old_ops(locpctx);
100e1051a39Sopenharmony_ci    } else {
101e1051a39Sopenharmony_ci        if (mdname == NULL && type == NULL)
102e1051a39Sopenharmony_ci            mdname = canon_mdname(EVP_MD_get0_name(ctx->reqdigest));
103e1051a39Sopenharmony_ci        goto reinitialize;
104e1051a39Sopenharmony_ci    }
105e1051a39Sopenharmony_ci
106e1051a39Sopenharmony_ci    /*
107e1051a39Sopenharmony_ci     * Try to derive the supported signature from |locpctx->keymgmt|.
108e1051a39Sopenharmony_ci     */
109e1051a39Sopenharmony_ci    if (!ossl_assert(locpctx->pkey->keymgmt == NULL
110e1051a39Sopenharmony_ci                     || locpctx->pkey->keymgmt == locpctx->keymgmt)) {
111e1051a39Sopenharmony_ci        ERR_clear_last_mark();
112e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
113e1051a39Sopenharmony_ci        goto err;
114e1051a39Sopenharmony_ci    }
115e1051a39Sopenharmony_ci    supported_sig = evp_keymgmt_util_query_operation_name(locpctx->keymgmt,
116e1051a39Sopenharmony_ci                                                          OSSL_OP_SIGNATURE);
117e1051a39Sopenharmony_ci    if (supported_sig == NULL) {
118e1051a39Sopenharmony_ci        ERR_clear_last_mark();
119e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
120e1051a39Sopenharmony_ci        goto err;
121e1051a39Sopenharmony_ci    }
122e1051a39Sopenharmony_ci
123e1051a39Sopenharmony_ci    /*
124e1051a39Sopenharmony_ci     * We perform two iterations:
125e1051a39Sopenharmony_ci     *
126e1051a39Sopenharmony_ci     * 1.  Do the normal signature fetch, using the fetching data given by
127e1051a39Sopenharmony_ci     *     the EVP_PKEY_CTX.
128e1051a39Sopenharmony_ci     * 2.  Do the provider specific signature fetch, from the same provider
129e1051a39Sopenharmony_ci     *     as |ctx->keymgmt|
130e1051a39Sopenharmony_ci     *
131e1051a39Sopenharmony_ci     * We then try to fetch the keymgmt from the same provider as the
132e1051a39Sopenharmony_ci     * signature, and try to export |ctx->pkey| to that keymgmt (when
133e1051a39Sopenharmony_ci     * this keymgmt happens to be the same as |ctx->keymgmt|, the export
134e1051a39Sopenharmony_ci     * is a no-op, but we call it anyway to not complicate the code even
135e1051a39Sopenharmony_ci     * more).
136e1051a39Sopenharmony_ci     * If the export call succeeds (returns a non-NULL provider key pointer),
137e1051a39Sopenharmony_ci     * we're done and can perform the operation itself.  If not, we perform
138e1051a39Sopenharmony_ci     * the second iteration, or jump to legacy.
139e1051a39Sopenharmony_ci     */
140e1051a39Sopenharmony_ci    for (iter = 1, provkey = NULL; iter < 3 && provkey == NULL; iter++) {
141e1051a39Sopenharmony_ci        EVP_KEYMGMT *tmp_keymgmt_tofree = NULL;
142e1051a39Sopenharmony_ci
143e1051a39Sopenharmony_ci        /*
144e1051a39Sopenharmony_ci         * If we're on the second iteration, free the results from the first.
145e1051a39Sopenharmony_ci         * They are NULL on the first iteration, so no need to check what
146e1051a39Sopenharmony_ci         * iteration we're on.
147e1051a39Sopenharmony_ci         */
148e1051a39Sopenharmony_ci        EVP_SIGNATURE_free(signature);
149e1051a39Sopenharmony_ci        EVP_KEYMGMT_free(tmp_keymgmt);
150e1051a39Sopenharmony_ci
151e1051a39Sopenharmony_ci        switch (iter) {
152e1051a39Sopenharmony_ci        case 1:
153e1051a39Sopenharmony_ci            signature = EVP_SIGNATURE_fetch(locpctx->libctx, supported_sig,
154e1051a39Sopenharmony_ci                                            locpctx->propquery);
155e1051a39Sopenharmony_ci            if (signature != NULL)
156e1051a39Sopenharmony_ci                tmp_prov = EVP_SIGNATURE_get0_provider(signature);
157e1051a39Sopenharmony_ci            break;
158e1051a39Sopenharmony_ci        case 2:
159e1051a39Sopenharmony_ci            tmp_prov = EVP_KEYMGMT_get0_provider(locpctx->keymgmt);
160e1051a39Sopenharmony_ci            signature =
161e1051a39Sopenharmony_ci                evp_signature_fetch_from_prov((OSSL_PROVIDER *)tmp_prov,
162e1051a39Sopenharmony_ci                                              supported_sig, locpctx->propquery);
163e1051a39Sopenharmony_ci            if (signature == NULL)
164e1051a39Sopenharmony_ci                goto legacy;
165e1051a39Sopenharmony_ci            break;
166e1051a39Sopenharmony_ci        }
167e1051a39Sopenharmony_ci        if (signature == NULL)
168e1051a39Sopenharmony_ci            continue;
169e1051a39Sopenharmony_ci
170e1051a39Sopenharmony_ci        /*
171e1051a39Sopenharmony_ci         * Ensure that the key is provided, either natively, or as a cached
172e1051a39Sopenharmony_ci         * export.  We start by fetching the keymgmt with the same name as
173e1051a39Sopenharmony_ci         * |locpctx->pkey|, but from the provider of the signature method, using
174e1051a39Sopenharmony_ci         * the same property query as when fetching the signature method.
175e1051a39Sopenharmony_ci         * With the keymgmt we found (if we did), we try to export |locpctx->pkey|
176e1051a39Sopenharmony_ci         * to it (evp_pkey_export_to_provider() is smart enough to only actually
177e1051a39Sopenharmony_ci
178e1051a39Sopenharmony_ci         * export it if |tmp_keymgmt| is different from |locpctx->pkey|'s keymgmt)
179e1051a39Sopenharmony_ci         */
180e1051a39Sopenharmony_ci        tmp_keymgmt_tofree = tmp_keymgmt =
181e1051a39Sopenharmony_ci            evp_keymgmt_fetch_from_prov((OSSL_PROVIDER *)tmp_prov,
182e1051a39Sopenharmony_ci                                        EVP_KEYMGMT_get0_name(locpctx->keymgmt),
183e1051a39Sopenharmony_ci                                        locpctx->propquery);
184e1051a39Sopenharmony_ci        if (tmp_keymgmt != NULL)
185e1051a39Sopenharmony_ci            provkey = evp_pkey_export_to_provider(locpctx->pkey, locpctx->libctx,
186e1051a39Sopenharmony_ci                                                  &tmp_keymgmt, locpctx->propquery);
187e1051a39Sopenharmony_ci        if (tmp_keymgmt == NULL)
188e1051a39Sopenharmony_ci            EVP_KEYMGMT_free(tmp_keymgmt_tofree);
189e1051a39Sopenharmony_ci    }
190e1051a39Sopenharmony_ci
191e1051a39Sopenharmony_ci    if (provkey == NULL) {
192e1051a39Sopenharmony_ci        EVP_SIGNATURE_free(signature);
193e1051a39Sopenharmony_ci        ERR_clear_last_mark();
194e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
195e1051a39Sopenharmony_ci        goto err;
196e1051a39Sopenharmony_ci    }
197e1051a39Sopenharmony_ci
198e1051a39Sopenharmony_ci    ERR_pop_to_mark();
199e1051a39Sopenharmony_ci
200e1051a39Sopenharmony_ci    /* No more legacy from here down to legacy: */
201e1051a39Sopenharmony_ci
202e1051a39Sopenharmony_ci    locpctx->op.sig.signature = signature;
203e1051a39Sopenharmony_ci    locpctx->operation = ver ? EVP_PKEY_OP_VERIFYCTX
204e1051a39Sopenharmony_ci                             : EVP_PKEY_OP_SIGNCTX;
205e1051a39Sopenharmony_ci    locpctx->op.sig.algctx
206e1051a39Sopenharmony_ci        = signature->newctx(ossl_provider_ctx(signature->prov), props);
207e1051a39Sopenharmony_ci    if (locpctx->op.sig.algctx == NULL) {
208e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_EVP,  EVP_R_INITIALIZATION_ERROR);
209e1051a39Sopenharmony_ci        goto err;
210e1051a39Sopenharmony_ci    }
211e1051a39Sopenharmony_ci
212e1051a39Sopenharmony_ci reinitialize:
213e1051a39Sopenharmony_ci    if (pctx != NULL)
214e1051a39Sopenharmony_ci        *pctx = locpctx;
215e1051a39Sopenharmony_ci
216e1051a39Sopenharmony_ci    if (type != NULL) {
217e1051a39Sopenharmony_ci        ctx->reqdigest = type;
218e1051a39Sopenharmony_ci        if (mdname == NULL)
219e1051a39Sopenharmony_ci            mdname = canon_mdname(EVP_MD_get0_name(type));
220e1051a39Sopenharmony_ci    } else {
221e1051a39Sopenharmony_ci        if (mdname == NULL && !reinit) {
222e1051a39Sopenharmony_ci            if (evp_keymgmt_util_get_deflt_digest_name(tmp_keymgmt, provkey,
223e1051a39Sopenharmony_ci                                                       locmdname,
224e1051a39Sopenharmony_ci                                                       sizeof(locmdname)) > 0) {
225e1051a39Sopenharmony_ci                mdname = canon_mdname(locmdname);
226e1051a39Sopenharmony_ci            }
227e1051a39Sopenharmony_ci        }
228e1051a39Sopenharmony_ci
229e1051a39Sopenharmony_ci        if (mdname != NULL) {
230e1051a39Sopenharmony_ci            /*
231e1051a39Sopenharmony_ci             * We're about to get a new digest so clear anything associated with
232e1051a39Sopenharmony_ci             * an old digest.
233e1051a39Sopenharmony_ci             */
234e1051a39Sopenharmony_ci            evp_md_ctx_clear_digest(ctx, 1, 0);
235e1051a39Sopenharmony_ci
236e1051a39Sopenharmony_ci            /* legacy code support for engines */
237e1051a39Sopenharmony_ci            ERR_set_mark();
238e1051a39Sopenharmony_ci            /*
239e1051a39Sopenharmony_ci             * This might be requested by a later call to EVP_MD_CTX_get0_md().
240e1051a39Sopenharmony_ci             * In that case the "explicit fetch" rules apply for that
241e1051a39Sopenharmony_ci             * function (as per man pages), i.e. the ref count is not updated
242e1051a39Sopenharmony_ci             * so the EVP_MD should not be used beyound the lifetime of the
243e1051a39Sopenharmony_ci             * EVP_MD_CTX.
244e1051a39Sopenharmony_ci             */
245e1051a39Sopenharmony_ci            ctx->fetched_digest = EVP_MD_fetch(locpctx->libctx, mdname, props);
246e1051a39Sopenharmony_ci            if (ctx->fetched_digest != NULL) {
247e1051a39Sopenharmony_ci                ctx->digest = ctx->reqdigest = ctx->fetched_digest;
248e1051a39Sopenharmony_ci            } else {
249e1051a39Sopenharmony_ci                /* legacy engine support : remove the mark when this is deleted */
250e1051a39Sopenharmony_ci                ctx->reqdigest = ctx->digest = EVP_get_digestbyname(mdname);
251e1051a39Sopenharmony_ci                if (ctx->digest == NULL) {
252e1051a39Sopenharmony_ci                    (void)ERR_clear_last_mark();
253e1051a39Sopenharmony_ci                    ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
254e1051a39Sopenharmony_ci                    goto err;
255e1051a39Sopenharmony_ci                }
256e1051a39Sopenharmony_ci            }
257e1051a39Sopenharmony_ci            (void)ERR_pop_to_mark();
258e1051a39Sopenharmony_ci        }
259e1051a39Sopenharmony_ci    }
260e1051a39Sopenharmony_ci
261e1051a39Sopenharmony_ci    if (ver) {
262e1051a39Sopenharmony_ci        if (signature->digest_verify_init == NULL) {
263e1051a39Sopenharmony_ci            ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
264e1051a39Sopenharmony_ci            goto err;
265e1051a39Sopenharmony_ci        }
266e1051a39Sopenharmony_ci        ret = signature->digest_verify_init(locpctx->op.sig.algctx,
267e1051a39Sopenharmony_ci                                            mdname, provkey, params);
268e1051a39Sopenharmony_ci    } else {
269e1051a39Sopenharmony_ci        if (signature->digest_sign_init == NULL) {
270e1051a39Sopenharmony_ci            ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
271e1051a39Sopenharmony_ci            goto err;
272e1051a39Sopenharmony_ci        }
273e1051a39Sopenharmony_ci        ret = signature->digest_sign_init(locpctx->op.sig.algctx,
274e1051a39Sopenharmony_ci                                          mdname, provkey, params);
275e1051a39Sopenharmony_ci    }
276e1051a39Sopenharmony_ci
277e1051a39Sopenharmony_ci    /*
278e1051a39Sopenharmony_ci     * If the operation was not a success and no digest was found, an error
279e1051a39Sopenharmony_ci     * needs to be raised.
280e1051a39Sopenharmony_ci     */
281e1051a39Sopenharmony_ci    if (ret > 0 || mdname != NULL)
282e1051a39Sopenharmony_ci        goto end;
283e1051a39Sopenharmony_ci    if (type == NULL)   /* This check is redundant but clarifies matters */
284e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_EVP, EVP_R_NO_DEFAULT_DIGEST);
285e1051a39Sopenharmony_ci
286e1051a39Sopenharmony_ci err:
287e1051a39Sopenharmony_ci    evp_pkey_ctx_free_old_ops(locpctx);
288e1051a39Sopenharmony_ci    locpctx->operation = EVP_PKEY_OP_UNDEFINED;
289e1051a39Sopenharmony_ci    EVP_KEYMGMT_free(tmp_keymgmt);
290e1051a39Sopenharmony_ci    return 0;
291e1051a39Sopenharmony_ci
292e1051a39Sopenharmony_ci legacy:
293e1051a39Sopenharmony_ci    /*
294e1051a39Sopenharmony_ci     * If we don't have the full support we need with provided methods,
295e1051a39Sopenharmony_ci     * let's go see if legacy does.
296e1051a39Sopenharmony_ci     */
297e1051a39Sopenharmony_ci    ERR_pop_to_mark();
298e1051a39Sopenharmony_ci    EVP_KEYMGMT_free(tmp_keymgmt);
299e1051a39Sopenharmony_ci    tmp_keymgmt = NULL;
300e1051a39Sopenharmony_ci
301e1051a39Sopenharmony_ci    if (type == NULL && mdname != NULL)
302e1051a39Sopenharmony_ci        type = evp_get_digestbyname_ex(locpctx->libctx, mdname);
303e1051a39Sopenharmony_ci
304e1051a39Sopenharmony_ci    if (ctx->pctx->pmeth == NULL) {
305e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
306e1051a39Sopenharmony_ci        return 0;
307e1051a39Sopenharmony_ci    }
308e1051a39Sopenharmony_ci
309e1051a39Sopenharmony_ci    if (!(ctx->pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM)) {
310e1051a39Sopenharmony_ci
311e1051a39Sopenharmony_ci        if (type == NULL) {
312e1051a39Sopenharmony_ci            int def_nid;
313e1051a39Sopenharmony_ci            if (EVP_PKEY_get_default_digest_nid(pkey, &def_nid) > 0)
314e1051a39Sopenharmony_ci                type = EVP_get_digestbynid(def_nid);
315e1051a39Sopenharmony_ci        }
316e1051a39Sopenharmony_ci
317e1051a39Sopenharmony_ci        if (type == NULL) {
318e1051a39Sopenharmony_ci            ERR_raise(ERR_LIB_EVP, EVP_R_NO_DEFAULT_DIGEST);
319e1051a39Sopenharmony_ci            return 0;
320e1051a39Sopenharmony_ci        }
321e1051a39Sopenharmony_ci    }
322e1051a39Sopenharmony_ci
323e1051a39Sopenharmony_ci    if (ver) {
324e1051a39Sopenharmony_ci        if (ctx->pctx->pmeth->verifyctx_init) {
325e1051a39Sopenharmony_ci            if (ctx->pctx->pmeth->verifyctx_init(ctx->pctx, ctx) <= 0)
326e1051a39Sopenharmony_ci                return 0;
327e1051a39Sopenharmony_ci            ctx->pctx->operation = EVP_PKEY_OP_VERIFYCTX;
328e1051a39Sopenharmony_ci        } else if (ctx->pctx->pmeth->digestverify != 0) {
329e1051a39Sopenharmony_ci            ctx->pctx->operation = EVP_PKEY_OP_VERIFY;
330e1051a39Sopenharmony_ci            ctx->update = update;
331e1051a39Sopenharmony_ci        } else if (EVP_PKEY_verify_init(ctx->pctx) <= 0) {
332e1051a39Sopenharmony_ci            return 0;
333e1051a39Sopenharmony_ci        }
334e1051a39Sopenharmony_ci    } else {
335e1051a39Sopenharmony_ci        if (ctx->pctx->pmeth->signctx_init) {
336e1051a39Sopenharmony_ci            if (ctx->pctx->pmeth->signctx_init(ctx->pctx, ctx) <= 0)
337e1051a39Sopenharmony_ci                return 0;
338e1051a39Sopenharmony_ci            ctx->pctx->operation = EVP_PKEY_OP_SIGNCTX;
339e1051a39Sopenharmony_ci        } else if (ctx->pctx->pmeth->digestsign != 0) {
340e1051a39Sopenharmony_ci            ctx->pctx->operation = EVP_PKEY_OP_SIGN;
341e1051a39Sopenharmony_ci            ctx->update = update;
342e1051a39Sopenharmony_ci        } else if (EVP_PKEY_sign_init(ctx->pctx) <= 0) {
343e1051a39Sopenharmony_ci            return 0;
344e1051a39Sopenharmony_ci        }
345e1051a39Sopenharmony_ci    }
346e1051a39Sopenharmony_ci    if (EVP_PKEY_CTX_set_signature_md(ctx->pctx, type) <= 0)
347e1051a39Sopenharmony_ci        return 0;
348e1051a39Sopenharmony_ci    if (pctx)
349e1051a39Sopenharmony_ci        *pctx = ctx->pctx;
350e1051a39Sopenharmony_ci    if (ctx->pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM)
351e1051a39Sopenharmony_ci        return 1;
352e1051a39Sopenharmony_ci    if (!EVP_DigestInit_ex(ctx, type, e))
353e1051a39Sopenharmony_ci        return 0;
354e1051a39Sopenharmony_ci    /*
355e1051a39Sopenharmony_ci     * This indicates the current algorithm requires
356e1051a39Sopenharmony_ci     * special treatment before hashing the tbs-message.
357e1051a39Sopenharmony_ci     */
358e1051a39Sopenharmony_ci    ctx->pctx->flag_call_digest_custom = 0;
359e1051a39Sopenharmony_ci    if (ctx->pctx->pmeth->digest_custom != NULL)
360e1051a39Sopenharmony_ci        ctx->pctx->flag_call_digest_custom = 1;
361e1051a39Sopenharmony_ci
362e1051a39Sopenharmony_ci    ret = 1;
363e1051a39Sopenharmony_ci
364e1051a39Sopenharmony_ci end:
365e1051a39Sopenharmony_ci#ifndef FIPS_MODULE
366e1051a39Sopenharmony_ci    if (ret > 0)
367e1051a39Sopenharmony_ci        ret = evp_pkey_ctx_use_cached_data(locpctx);
368e1051a39Sopenharmony_ci#endif
369e1051a39Sopenharmony_ci
370e1051a39Sopenharmony_ci    EVP_KEYMGMT_free(tmp_keymgmt);
371e1051a39Sopenharmony_ci    return ret > 0 ? 1 : 0;
372e1051a39Sopenharmony_ci}
373e1051a39Sopenharmony_ci
374e1051a39Sopenharmony_ciint EVP_DigestSignInit_ex(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
375e1051a39Sopenharmony_ci                          const char *mdname, OSSL_LIB_CTX *libctx,
376e1051a39Sopenharmony_ci                          const char *props, EVP_PKEY *pkey,
377e1051a39Sopenharmony_ci                          const OSSL_PARAM params[])
378e1051a39Sopenharmony_ci{
379e1051a39Sopenharmony_ci    return do_sigver_init(ctx, pctx, NULL, mdname, libctx, props, NULL, pkey, 0,
380e1051a39Sopenharmony_ci                          params);
381e1051a39Sopenharmony_ci}
382e1051a39Sopenharmony_ci
383e1051a39Sopenharmony_ciint EVP_DigestSignInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
384e1051a39Sopenharmony_ci                       const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey)
385e1051a39Sopenharmony_ci{
386e1051a39Sopenharmony_ci    return do_sigver_init(ctx, pctx, type, NULL, NULL, NULL, e, pkey, 0,
387e1051a39Sopenharmony_ci                          NULL);
388e1051a39Sopenharmony_ci}
389e1051a39Sopenharmony_ci
390e1051a39Sopenharmony_ciint EVP_DigestVerifyInit_ex(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
391e1051a39Sopenharmony_ci                            const char *mdname, OSSL_LIB_CTX *libctx,
392e1051a39Sopenharmony_ci                            const char *props, EVP_PKEY *pkey,
393e1051a39Sopenharmony_ci                            const OSSL_PARAM params[])
394e1051a39Sopenharmony_ci{
395e1051a39Sopenharmony_ci    return do_sigver_init(ctx, pctx, NULL, mdname, libctx, props, NULL, pkey, 1,
396e1051a39Sopenharmony_ci                          params);
397e1051a39Sopenharmony_ci}
398e1051a39Sopenharmony_ci
399e1051a39Sopenharmony_ciint EVP_DigestVerifyInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
400e1051a39Sopenharmony_ci                         const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey)
401e1051a39Sopenharmony_ci{
402e1051a39Sopenharmony_ci    return do_sigver_init(ctx, pctx, type, NULL, NULL, NULL, e, pkey, 1,
403e1051a39Sopenharmony_ci                          NULL);
404e1051a39Sopenharmony_ci}
405e1051a39Sopenharmony_ci#endif /* FIPS_MDOE */
406e1051a39Sopenharmony_ci
407e1051a39Sopenharmony_ciint EVP_DigestSignUpdate(EVP_MD_CTX *ctx, const void *data, size_t dsize)
408e1051a39Sopenharmony_ci{
409e1051a39Sopenharmony_ci    EVP_PKEY_CTX *pctx = ctx->pctx;
410e1051a39Sopenharmony_ci
411e1051a39Sopenharmony_ci    if (pctx == NULL
412e1051a39Sopenharmony_ci            || pctx->operation != EVP_PKEY_OP_SIGNCTX
413e1051a39Sopenharmony_ci            || pctx->op.sig.algctx == NULL
414e1051a39Sopenharmony_ci            || pctx->op.sig.signature == NULL)
415e1051a39Sopenharmony_ci        goto legacy;
416e1051a39Sopenharmony_ci
417e1051a39Sopenharmony_ci    if (pctx->op.sig.signature->digest_sign_update == NULL) {
418e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_EVP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
419e1051a39Sopenharmony_ci        return 0;
420e1051a39Sopenharmony_ci    }
421e1051a39Sopenharmony_ci
422e1051a39Sopenharmony_ci    return pctx->op.sig.signature->digest_sign_update(pctx->op.sig.algctx,
423e1051a39Sopenharmony_ci                                                      data, dsize);
424e1051a39Sopenharmony_ci
425e1051a39Sopenharmony_ci legacy:
426e1051a39Sopenharmony_ci    if (pctx != NULL) {
427e1051a39Sopenharmony_ci        /* do_sigver_init() checked that |digest_custom| is non-NULL */
428e1051a39Sopenharmony_ci        if (pctx->flag_call_digest_custom
429e1051a39Sopenharmony_ci            && !ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx))
430e1051a39Sopenharmony_ci            return 0;
431e1051a39Sopenharmony_ci        pctx->flag_call_digest_custom = 0;
432e1051a39Sopenharmony_ci    }
433e1051a39Sopenharmony_ci
434e1051a39Sopenharmony_ci    return EVP_DigestUpdate(ctx, data, dsize);
435e1051a39Sopenharmony_ci}
436e1051a39Sopenharmony_ci
437e1051a39Sopenharmony_ciint EVP_DigestVerifyUpdate(EVP_MD_CTX *ctx, const void *data, size_t dsize)
438e1051a39Sopenharmony_ci{
439e1051a39Sopenharmony_ci    EVP_PKEY_CTX *pctx = ctx->pctx;
440e1051a39Sopenharmony_ci
441e1051a39Sopenharmony_ci    if (pctx == NULL
442e1051a39Sopenharmony_ci            || pctx->operation != EVP_PKEY_OP_VERIFYCTX
443e1051a39Sopenharmony_ci            || pctx->op.sig.algctx == NULL
444e1051a39Sopenharmony_ci            || pctx->op.sig.signature == NULL)
445e1051a39Sopenharmony_ci        goto legacy;
446e1051a39Sopenharmony_ci
447e1051a39Sopenharmony_ci    if (pctx->op.sig.signature->digest_verify_update == NULL) {
448e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_EVP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
449e1051a39Sopenharmony_ci        return 0;
450e1051a39Sopenharmony_ci    }
451e1051a39Sopenharmony_ci
452e1051a39Sopenharmony_ci    return pctx->op.sig.signature->digest_verify_update(pctx->op.sig.algctx,
453e1051a39Sopenharmony_ci                                                        data, dsize);
454e1051a39Sopenharmony_ci
455e1051a39Sopenharmony_ci legacy:
456e1051a39Sopenharmony_ci    if (pctx != NULL) {
457e1051a39Sopenharmony_ci        /* do_sigver_init() checked that |digest_custom| is non-NULL */
458e1051a39Sopenharmony_ci        if (pctx->flag_call_digest_custom
459e1051a39Sopenharmony_ci            && !ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx))
460e1051a39Sopenharmony_ci            return 0;
461e1051a39Sopenharmony_ci        pctx->flag_call_digest_custom = 0;
462e1051a39Sopenharmony_ci    }
463e1051a39Sopenharmony_ci
464e1051a39Sopenharmony_ci    return EVP_DigestUpdate(ctx, data, dsize);
465e1051a39Sopenharmony_ci}
466e1051a39Sopenharmony_ci
467e1051a39Sopenharmony_ci#ifndef FIPS_MODULE
468e1051a39Sopenharmony_ciint EVP_DigestSignFinal(EVP_MD_CTX *ctx, unsigned char *sigret,
469e1051a39Sopenharmony_ci                        size_t *siglen)
470e1051a39Sopenharmony_ci{
471e1051a39Sopenharmony_ci    int sctx = 0, r = 0;
472e1051a39Sopenharmony_ci    EVP_PKEY_CTX *dctx, *pctx = ctx->pctx;
473e1051a39Sopenharmony_ci
474e1051a39Sopenharmony_ci    if (pctx == NULL
475e1051a39Sopenharmony_ci            || pctx->operation != EVP_PKEY_OP_SIGNCTX
476e1051a39Sopenharmony_ci            || pctx->op.sig.algctx == NULL
477e1051a39Sopenharmony_ci            || pctx->op.sig.signature == NULL)
478e1051a39Sopenharmony_ci        goto legacy;
479e1051a39Sopenharmony_ci
480e1051a39Sopenharmony_ci    if (sigret == NULL || (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) != 0)
481e1051a39Sopenharmony_ci        return pctx->op.sig.signature->digest_sign_final(pctx->op.sig.algctx,
482e1051a39Sopenharmony_ci                                                         sigret, siglen,
483e1051a39Sopenharmony_ci                                                         sigret == NULL ? 0 : *siglen);
484e1051a39Sopenharmony_ci    dctx = EVP_PKEY_CTX_dup(pctx);
485e1051a39Sopenharmony_ci    if (dctx == NULL)
486e1051a39Sopenharmony_ci        return 0;
487e1051a39Sopenharmony_ci
488e1051a39Sopenharmony_ci    r = dctx->op.sig.signature->digest_sign_final(dctx->op.sig.algctx,
489e1051a39Sopenharmony_ci                                                  sigret, siglen,
490e1051a39Sopenharmony_ci                                                  *siglen);
491e1051a39Sopenharmony_ci    EVP_PKEY_CTX_free(dctx);
492e1051a39Sopenharmony_ci    return r;
493e1051a39Sopenharmony_ci
494e1051a39Sopenharmony_ci legacy:
495e1051a39Sopenharmony_ci    if (pctx == NULL || pctx->pmeth == NULL) {
496e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
497e1051a39Sopenharmony_ci        return 0;
498e1051a39Sopenharmony_ci    }
499e1051a39Sopenharmony_ci
500e1051a39Sopenharmony_ci    /* do_sigver_init() checked that |digest_custom| is non-NULL */
501e1051a39Sopenharmony_ci    if (pctx->flag_call_digest_custom
502e1051a39Sopenharmony_ci        && !ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx))
503e1051a39Sopenharmony_ci        return 0;
504e1051a39Sopenharmony_ci    pctx->flag_call_digest_custom = 0;
505e1051a39Sopenharmony_ci
506e1051a39Sopenharmony_ci    if (pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM) {
507e1051a39Sopenharmony_ci        if (sigret == NULL)
508e1051a39Sopenharmony_ci            return pctx->pmeth->signctx(pctx, sigret, siglen, ctx);
509e1051a39Sopenharmony_ci        if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE)
510e1051a39Sopenharmony_ci            r = pctx->pmeth->signctx(pctx, sigret, siglen, ctx);
511e1051a39Sopenharmony_ci        else {
512e1051a39Sopenharmony_ci            dctx = EVP_PKEY_CTX_dup(pctx);
513e1051a39Sopenharmony_ci            if (dctx == NULL)
514e1051a39Sopenharmony_ci                return 0;
515e1051a39Sopenharmony_ci            r = dctx->pmeth->signctx(dctx, sigret, siglen, ctx);
516e1051a39Sopenharmony_ci            EVP_PKEY_CTX_free(dctx);
517e1051a39Sopenharmony_ci        }
518e1051a39Sopenharmony_ci        return r;
519e1051a39Sopenharmony_ci    }
520e1051a39Sopenharmony_ci    if (pctx->pmeth->signctx != NULL)
521e1051a39Sopenharmony_ci        sctx = 1;
522e1051a39Sopenharmony_ci    else
523e1051a39Sopenharmony_ci        sctx = 0;
524e1051a39Sopenharmony_ci    if (sigret != NULL) {
525e1051a39Sopenharmony_ci        unsigned char md[EVP_MAX_MD_SIZE];
526e1051a39Sopenharmony_ci        unsigned int mdlen = 0;
527e1051a39Sopenharmony_ci
528e1051a39Sopenharmony_ci        if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) {
529e1051a39Sopenharmony_ci            if (sctx)
530e1051a39Sopenharmony_ci                r = pctx->pmeth->signctx(pctx, sigret, siglen, ctx);
531e1051a39Sopenharmony_ci            else
532e1051a39Sopenharmony_ci                r = EVP_DigestFinal_ex(ctx, md, &mdlen);
533e1051a39Sopenharmony_ci        } else {
534e1051a39Sopenharmony_ci            EVP_MD_CTX *tmp_ctx = EVP_MD_CTX_new();
535e1051a39Sopenharmony_ci
536e1051a39Sopenharmony_ci            if (tmp_ctx == NULL)
537e1051a39Sopenharmony_ci                return 0;
538e1051a39Sopenharmony_ci            if (!EVP_MD_CTX_copy_ex(tmp_ctx, ctx)) {
539e1051a39Sopenharmony_ci                EVP_MD_CTX_free(tmp_ctx);
540e1051a39Sopenharmony_ci                return 0;
541e1051a39Sopenharmony_ci            }
542e1051a39Sopenharmony_ci            if (sctx)
543e1051a39Sopenharmony_ci                r = tmp_ctx->pctx->pmeth->signctx(tmp_ctx->pctx,
544e1051a39Sopenharmony_ci                                                  sigret, siglen, tmp_ctx);
545e1051a39Sopenharmony_ci            else
546e1051a39Sopenharmony_ci                r = EVP_DigestFinal_ex(tmp_ctx, md, &mdlen);
547e1051a39Sopenharmony_ci            EVP_MD_CTX_free(tmp_ctx);
548e1051a39Sopenharmony_ci        }
549e1051a39Sopenharmony_ci        if (sctx || !r)
550e1051a39Sopenharmony_ci            return r;
551e1051a39Sopenharmony_ci        if (EVP_PKEY_sign(pctx, sigret, siglen, md, mdlen) <= 0)
552e1051a39Sopenharmony_ci            return 0;
553e1051a39Sopenharmony_ci    } else {
554e1051a39Sopenharmony_ci        if (sctx) {
555e1051a39Sopenharmony_ci            if (pctx->pmeth->signctx(pctx, sigret, siglen, ctx) <= 0)
556e1051a39Sopenharmony_ci                return 0;
557e1051a39Sopenharmony_ci        } else {
558e1051a39Sopenharmony_ci            int s = EVP_MD_get_size(ctx->digest);
559e1051a39Sopenharmony_ci
560e1051a39Sopenharmony_ci            if (s < 0 || EVP_PKEY_sign(pctx, sigret, siglen, NULL, s) <= 0)
561e1051a39Sopenharmony_ci                return 0;
562e1051a39Sopenharmony_ci        }
563e1051a39Sopenharmony_ci    }
564e1051a39Sopenharmony_ci    return 1;
565e1051a39Sopenharmony_ci}
566e1051a39Sopenharmony_ci
567e1051a39Sopenharmony_ciint EVP_DigestSign(EVP_MD_CTX *ctx, unsigned char *sigret, size_t *siglen,
568e1051a39Sopenharmony_ci                   const unsigned char *tbs, size_t tbslen)
569e1051a39Sopenharmony_ci{
570e1051a39Sopenharmony_ci    EVP_PKEY_CTX *pctx = ctx->pctx;
571e1051a39Sopenharmony_ci
572e1051a39Sopenharmony_ci    if (pctx != NULL
573e1051a39Sopenharmony_ci            && pctx->operation == EVP_PKEY_OP_SIGNCTX
574e1051a39Sopenharmony_ci            && pctx->op.sig.algctx != NULL
575e1051a39Sopenharmony_ci            && pctx->op.sig.signature != NULL) {
576e1051a39Sopenharmony_ci        if (pctx->op.sig.signature->digest_sign != NULL)
577e1051a39Sopenharmony_ci            return pctx->op.sig.signature->digest_sign(pctx->op.sig.algctx,
578e1051a39Sopenharmony_ci                                                       sigret, siglen,
579e1051a39Sopenharmony_ci                                                       sigret == NULL ? 0 : *siglen,
580e1051a39Sopenharmony_ci                                                       tbs, tbslen);
581e1051a39Sopenharmony_ci    } else {
582e1051a39Sopenharmony_ci        /* legacy */
583e1051a39Sopenharmony_ci        if (ctx->pctx->pmeth != NULL && ctx->pctx->pmeth->digestsign != NULL)
584e1051a39Sopenharmony_ci            return ctx->pctx->pmeth->digestsign(ctx, sigret, siglen, tbs, tbslen);
585e1051a39Sopenharmony_ci    }
586e1051a39Sopenharmony_ci
587e1051a39Sopenharmony_ci    if (sigret != NULL && EVP_DigestSignUpdate(ctx, tbs, tbslen) <= 0)
588e1051a39Sopenharmony_ci        return 0;
589e1051a39Sopenharmony_ci    return EVP_DigestSignFinal(ctx, sigret, siglen);
590e1051a39Sopenharmony_ci}
591e1051a39Sopenharmony_ci
592e1051a39Sopenharmony_ciint EVP_DigestVerifyFinal(EVP_MD_CTX *ctx, const unsigned char *sig,
593e1051a39Sopenharmony_ci                          size_t siglen)
594e1051a39Sopenharmony_ci{
595e1051a39Sopenharmony_ci    unsigned char md[EVP_MAX_MD_SIZE];
596e1051a39Sopenharmony_ci    int r = 0;
597e1051a39Sopenharmony_ci    unsigned int mdlen = 0;
598e1051a39Sopenharmony_ci    int vctx = 0;
599e1051a39Sopenharmony_ci    EVP_PKEY_CTX *dctx, *pctx = ctx->pctx;
600e1051a39Sopenharmony_ci
601e1051a39Sopenharmony_ci    if (pctx == NULL
602e1051a39Sopenharmony_ci            || pctx->operation != EVP_PKEY_OP_VERIFYCTX
603e1051a39Sopenharmony_ci            || pctx->op.sig.algctx == NULL
604e1051a39Sopenharmony_ci            || pctx->op.sig.signature == NULL)
605e1051a39Sopenharmony_ci        goto legacy;
606e1051a39Sopenharmony_ci
607e1051a39Sopenharmony_ci    if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISE) != 0)
608e1051a39Sopenharmony_ci        return pctx->op.sig.signature->digest_verify_final(pctx->op.sig.algctx,
609e1051a39Sopenharmony_ci                                                           sig, siglen);
610e1051a39Sopenharmony_ci    dctx = EVP_PKEY_CTX_dup(pctx);
611e1051a39Sopenharmony_ci    if (dctx == NULL)
612e1051a39Sopenharmony_ci        return 0;
613e1051a39Sopenharmony_ci
614e1051a39Sopenharmony_ci    r = dctx->op.sig.signature->digest_verify_final(dctx->op.sig.algctx,
615e1051a39Sopenharmony_ci                                                    sig, siglen);
616e1051a39Sopenharmony_ci    EVP_PKEY_CTX_free(dctx);
617e1051a39Sopenharmony_ci    return r;
618e1051a39Sopenharmony_ci
619e1051a39Sopenharmony_ci legacy:
620e1051a39Sopenharmony_ci    if (pctx == NULL || pctx->pmeth == NULL) {
621e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
622e1051a39Sopenharmony_ci        return 0;
623e1051a39Sopenharmony_ci    }
624e1051a39Sopenharmony_ci
625e1051a39Sopenharmony_ci    /* do_sigver_init() checked that |digest_custom| is non-NULL */
626e1051a39Sopenharmony_ci    if (pctx->flag_call_digest_custom
627e1051a39Sopenharmony_ci        && !ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx))
628e1051a39Sopenharmony_ci        return 0;
629e1051a39Sopenharmony_ci    pctx->flag_call_digest_custom = 0;
630e1051a39Sopenharmony_ci
631e1051a39Sopenharmony_ci    if (pctx->pmeth->verifyctx != NULL)
632e1051a39Sopenharmony_ci        vctx = 1;
633e1051a39Sopenharmony_ci    else
634e1051a39Sopenharmony_ci        vctx = 0;
635e1051a39Sopenharmony_ci    if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) {
636e1051a39Sopenharmony_ci        if (vctx)
637e1051a39Sopenharmony_ci            r = pctx->pmeth->verifyctx(pctx, sig, siglen, ctx);
638e1051a39Sopenharmony_ci        else
639e1051a39Sopenharmony_ci            r = EVP_DigestFinal_ex(ctx, md, &mdlen);
640e1051a39Sopenharmony_ci    } else {
641e1051a39Sopenharmony_ci        EVP_MD_CTX *tmp_ctx = EVP_MD_CTX_new();
642e1051a39Sopenharmony_ci        if (tmp_ctx == NULL)
643e1051a39Sopenharmony_ci            return -1;
644e1051a39Sopenharmony_ci        if (!EVP_MD_CTX_copy_ex(tmp_ctx, ctx)) {
645e1051a39Sopenharmony_ci            EVP_MD_CTX_free(tmp_ctx);
646e1051a39Sopenharmony_ci            return -1;
647e1051a39Sopenharmony_ci        }
648e1051a39Sopenharmony_ci        if (vctx)
649e1051a39Sopenharmony_ci            r = tmp_ctx->pctx->pmeth->verifyctx(tmp_ctx->pctx,
650e1051a39Sopenharmony_ci                                                sig, siglen, tmp_ctx);
651e1051a39Sopenharmony_ci        else
652e1051a39Sopenharmony_ci            r = EVP_DigestFinal_ex(tmp_ctx, md, &mdlen);
653e1051a39Sopenharmony_ci        EVP_MD_CTX_free(tmp_ctx);
654e1051a39Sopenharmony_ci    }
655e1051a39Sopenharmony_ci    if (vctx || !r)
656e1051a39Sopenharmony_ci        return r;
657e1051a39Sopenharmony_ci    return EVP_PKEY_verify(pctx, sig, siglen, md, mdlen);
658e1051a39Sopenharmony_ci}
659e1051a39Sopenharmony_ci
660e1051a39Sopenharmony_ciint EVP_DigestVerify(EVP_MD_CTX *ctx, const unsigned char *sigret,
661e1051a39Sopenharmony_ci                     size_t siglen, const unsigned char *tbs, size_t tbslen)
662e1051a39Sopenharmony_ci{
663e1051a39Sopenharmony_ci    EVP_PKEY_CTX *pctx = ctx->pctx;
664e1051a39Sopenharmony_ci
665e1051a39Sopenharmony_ci    if (pctx != NULL
666e1051a39Sopenharmony_ci            && pctx->operation == EVP_PKEY_OP_VERIFYCTX
667e1051a39Sopenharmony_ci            && pctx->op.sig.algctx != NULL
668e1051a39Sopenharmony_ci            && pctx->op.sig.signature != NULL) {
669e1051a39Sopenharmony_ci        if (pctx->op.sig.signature->digest_verify != NULL)
670e1051a39Sopenharmony_ci            return pctx->op.sig.signature->digest_verify(pctx->op.sig.algctx,
671e1051a39Sopenharmony_ci                                                         sigret, siglen,
672e1051a39Sopenharmony_ci                                                         tbs, tbslen);
673e1051a39Sopenharmony_ci    } else {
674e1051a39Sopenharmony_ci        /* legacy */
675e1051a39Sopenharmony_ci        if (ctx->pctx->pmeth != NULL && ctx->pctx->pmeth->digestverify != NULL)
676e1051a39Sopenharmony_ci            return ctx->pctx->pmeth->digestverify(ctx, sigret, siglen, tbs, tbslen);
677e1051a39Sopenharmony_ci    }
678e1051a39Sopenharmony_ci
679e1051a39Sopenharmony_ci    if (EVP_DigestVerifyUpdate(ctx, tbs, tbslen) <= 0)
680e1051a39Sopenharmony_ci        return -1;
681e1051a39Sopenharmony_ci    return EVP_DigestVerifyFinal(ctx, sigret, siglen);
682e1051a39Sopenharmony_ci}
683e1051a39Sopenharmony_ci#endif /* FIPS_MODULE */
684