xref: /netbsd-src/crypto/external/bsd/openssl/dist/providers/implementations/signature/ecdsa_sig.c (revision b0d1725196a7921d003d2c66a14f186abda4176b)
1*b0d17251Schristos /*
2*b0d17251Schristos  * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
3*b0d17251Schristos  *
4*b0d17251Schristos  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*b0d17251Schristos  * this file except in compliance with the License.  You can obtain a copy
6*b0d17251Schristos  * in the file LICENSE in the source distribution or at
7*b0d17251Schristos  * https://www.openssl.org/source/license.html
8*b0d17251Schristos  */
9*b0d17251Schristos 
10*b0d17251Schristos /*
11*b0d17251Schristos  * ECDSA low level APIs are deprecated for public use, but still ok for
12*b0d17251Schristos  * internal use.
13*b0d17251Schristos  */
14*b0d17251Schristos #include "internal/deprecated.h"
15*b0d17251Schristos 
16*b0d17251Schristos #include <string.h> /* memcpy */
17*b0d17251Schristos #include <openssl/crypto.h>
18*b0d17251Schristos #include <openssl/core_dispatch.h>
19*b0d17251Schristos #include <openssl/core_names.h>
20*b0d17251Schristos #include <openssl/dsa.h>
21*b0d17251Schristos #include <openssl/params.h>
22*b0d17251Schristos #include <openssl/evp.h>
23*b0d17251Schristos #include <openssl/err.h>
24*b0d17251Schristos #include <openssl/proverr.h>
25*b0d17251Schristos #include "internal/nelem.h"
26*b0d17251Schristos #include "internal/sizes.h"
27*b0d17251Schristos #include "internal/cryptlib.h"
28*b0d17251Schristos #include "prov/providercommon.h"
29*b0d17251Schristos #include "prov/implementations.h"
30*b0d17251Schristos #include "prov/provider_ctx.h"
31*b0d17251Schristos #include "prov/securitycheck.h"
32*b0d17251Schristos #include "crypto/ec.h"
33*b0d17251Schristos #include "prov/der_ec.h"
34*b0d17251Schristos 
35*b0d17251Schristos static OSSL_FUNC_signature_newctx_fn ecdsa_newctx;
36*b0d17251Schristos static OSSL_FUNC_signature_sign_init_fn ecdsa_sign_init;
37*b0d17251Schristos static OSSL_FUNC_signature_verify_init_fn ecdsa_verify_init;
38*b0d17251Schristos static OSSL_FUNC_signature_sign_fn ecdsa_sign;
39*b0d17251Schristos static OSSL_FUNC_signature_verify_fn ecdsa_verify;
40*b0d17251Schristos static OSSL_FUNC_signature_digest_sign_init_fn ecdsa_digest_sign_init;
41*b0d17251Schristos static OSSL_FUNC_signature_digest_sign_update_fn ecdsa_digest_signverify_update;
42*b0d17251Schristos static OSSL_FUNC_signature_digest_sign_final_fn ecdsa_digest_sign_final;
43*b0d17251Schristos static OSSL_FUNC_signature_digest_verify_init_fn ecdsa_digest_verify_init;
44*b0d17251Schristos static OSSL_FUNC_signature_digest_verify_update_fn ecdsa_digest_signverify_update;
45*b0d17251Schristos static OSSL_FUNC_signature_digest_verify_final_fn ecdsa_digest_verify_final;
46*b0d17251Schristos static OSSL_FUNC_signature_freectx_fn ecdsa_freectx;
47*b0d17251Schristos static OSSL_FUNC_signature_dupctx_fn ecdsa_dupctx;
48*b0d17251Schristos static OSSL_FUNC_signature_get_ctx_params_fn ecdsa_get_ctx_params;
49*b0d17251Schristos static OSSL_FUNC_signature_gettable_ctx_params_fn ecdsa_gettable_ctx_params;
50*b0d17251Schristos static OSSL_FUNC_signature_set_ctx_params_fn ecdsa_set_ctx_params;
51*b0d17251Schristos static OSSL_FUNC_signature_settable_ctx_params_fn ecdsa_settable_ctx_params;
52*b0d17251Schristos static OSSL_FUNC_signature_get_ctx_md_params_fn ecdsa_get_ctx_md_params;
53*b0d17251Schristos static OSSL_FUNC_signature_gettable_ctx_md_params_fn ecdsa_gettable_ctx_md_params;
54*b0d17251Schristos static OSSL_FUNC_signature_set_ctx_md_params_fn ecdsa_set_ctx_md_params;
55*b0d17251Schristos static OSSL_FUNC_signature_settable_ctx_md_params_fn ecdsa_settable_ctx_md_params;
56*b0d17251Schristos 
57*b0d17251Schristos /*
58*b0d17251Schristos  * What's passed as an actual key is defined by the KEYMGMT interface.
59*b0d17251Schristos  * We happen to know that our KEYMGMT simply passes DSA structures, so
60*b0d17251Schristos  * we use that here too.
61*b0d17251Schristos  */
62*b0d17251Schristos 
63*b0d17251Schristos typedef struct {
64*b0d17251Schristos     OSSL_LIB_CTX *libctx;
65*b0d17251Schristos     char *propq;
66*b0d17251Schristos     EC_KEY *ec;
67*b0d17251Schristos     char mdname[OSSL_MAX_NAME_SIZE];
68*b0d17251Schristos 
69*b0d17251Schristos     /*
70*b0d17251Schristos      * Flag to determine if the hash function can be changed (1) or not (0)
71*b0d17251Schristos      * Because it's dangerous to change during a DigestSign or DigestVerify
72*b0d17251Schristos      * operation, this flag is cleared by their Init function, and set again
73*b0d17251Schristos      * by their Final function.
74*b0d17251Schristos      */
75*b0d17251Schristos     unsigned int flag_allow_md : 1;
76*b0d17251Schristos 
77*b0d17251Schristos     /* The Algorithm Identifier of the combined signature algorithm */
78*b0d17251Schristos     unsigned char aid_buf[OSSL_MAX_ALGORITHM_ID_SIZE];
79*b0d17251Schristos     unsigned char *aid;
80*b0d17251Schristos     size_t  aid_len;
81*b0d17251Schristos     size_t mdsize;
82*b0d17251Schristos     int operation;
83*b0d17251Schristos 
84*b0d17251Schristos     EVP_MD *md;
85*b0d17251Schristos     EVP_MD_CTX *mdctx;
86*b0d17251Schristos     /*
87*b0d17251Schristos      * Internally used to cache the results of calling the EC group
88*b0d17251Schristos      * sign_setup() methods which are then passed to the sign operation.
89*b0d17251Schristos      * This is used by CAVS failure tests to terminate a loop if the signature
90*b0d17251Schristos      * is not valid.
91*b0d17251Schristos      * This could of also been done with a simple flag.
92*b0d17251Schristos      */
93*b0d17251Schristos     BIGNUM *kinv;
94*b0d17251Schristos     BIGNUM *r;
95*b0d17251Schristos #if !defined(OPENSSL_NO_ACVP_TESTS)
96*b0d17251Schristos     /*
97*b0d17251Schristos      * This indicates that KAT (CAVS) test is running. Externally an app will
98*b0d17251Schristos      * override the random callback such that the generated private key and k
99*b0d17251Schristos      * are known.
100*b0d17251Schristos      * Normal operation will loop to choose a new k if the signature is not
101*b0d17251Schristos      * valid - but for this mode of operation it forces a failure instead.
102*b0d17251Schristos      */
103*b0d17251Schristos     unsigned int kattest;
104*b0d17251Schristos #endif
105*b0d17251Schristos } PROV_ECDSA_CTX;
106*b0d17251Schristos 
ecdsa_newctx(void * provctx,const char * propq)107*b0d17251Schristos static void *ecdsa_newctx(void *provctx, const char *propq)
108*b0d17251Schristos {
109*b0d17251Schristos     PROV_ECDSA_CTX *ctx;
110*b0d17251Schristos 
111*b0d17251Schristos     if (!ossl_prov_is_running())
112*b0d17251Schristos         return NULL;
113*b0d17251Schristos 
114*b0d17251Schristos     ctx = OPENSSL_zalloc(sizeof(PROV_ECDSA_CTX));
115*b0d17251Schristos     if (ctx == NULL)
116*b0d17251Schristos         return NULL;
117*b0d17251Schristos 
118*b0d17251Schristos     ctx->flag_allow_md = 1;
119*b0d17251Schristos     ctx->libctx = PROV_LIBCTX_OF(provctx);
120*b0d17251Schristos     if (propq != NULL && (ctx->propq = OPENSSL_strdup(propq)) == NULL) {
121*b0d17251Schristos         OPENSSL_free(ctx);
122*b0d17251Schristos         ctx = NULL;
123*b0d17251Schristos         ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
124*b0d17251Schristos     }
125*b0d17251Schristos     return ctx;
126*b0d17251Schristos }
127*b0d17251Schristos 
ecdsa_signverify_init(void * vctx,void * ec,const OSSL_PARAM params[],int operation)128*b0d17251Schristos static int ecdsa_signverify_init(void *vctx, void *ec,
129*b0d17251Schristos                                  const OSSL_PARAM params[], int operation)
130*b0d17251Schristos {
131*b0d17251Schristos     PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
132*b0d17251Schristos 
133*b0d17251Schristos     if (!ossl_prov_is_running()
134*b0d17251Schristos             || ctx == NULL)
135*b0d17251Schristos         return 0;
136*b0d17251Schristos 
137*b0d17251Schristos     if (ec == NULL && ctx->ec == NULL) {
138*b0d17251Schristos         ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
139*b0d17251Schristos         return 0;
140*b0d17251Schristos     }
141*b0d17251Schristos 
142*b0d17251Schristos     if (ec != NULL) {
143*b0d17251Schristos         if (!ossl_ec_check_key(ctx->libctx, ec, operation == EVP_PKEY_OP_SIGN))
144*b0d17251Schristos             return 0;
145*b0d17251Schristos         if (!EC_KEY_up_ref(ec))
146*b0d17251Schristos             return 0;
147*b0d17251Schristos         EC_KEY_free(ctx->ec);
148*b0d17251Schristos         ctx->ec = ec;
149*b0d17251Schristos     }
150*b0d17251Schristos 
151*b0d17251Schristos     ctx->operation = operation;
152*b0d17251Schristos 
153*b0d17251Schristos     if (!ecdsa_set_ctx_params(ctx, params))
154*b0d17251Schristos         return 0;
155*b0d17251Schristos 
156*b0d17251Schristos     return 1;
157*b0d17251Schristos }
158*b0d17251Schristos 
ecdsa_sign_init(void * vctx,void * ec,const OSSL_PARAM params[])159*b0d17251Schristos static int ecdsa_sign_init(void *vctx, void *ec, const OSSL_PARAM params[])
160*b0d17251Schristos {
161*b0d17251Schristos     return ecdsa_signverify_init(vctx, ec, params, EVP_PKEY_OP_SIGN);
162*b0d17251Schristos }
163*b0d17251Schristos 
ecdsa_verify_init(void * vctx,void * ec,const OSSL_PARAM params[])164*b0d17251Schristos static int ecdsa_verify_init(void *vctx, void *ec, const OSSL_PARAM params[])
165*b0d17251Schristos {
166*b0d17251Schristos     return ecdsa_signverify_init(vctx, ec, params, EVP_PKEY_OP_VERIFY);
167*b0d17251Schristos }
168*b0d17251Schristos 
ecdsa_sign(void * vctx,unsigned char * sig,size_t * siglen,size_t sigsize,const unsigned char * tbs,size_t tbslen)169*b0d17251Schristos static int ecdsa_sign(void *vctx, unsigned char *sig, size_t *siglen,
170*b0d17251Schristos                       size_t sigsize, const unsigned char *tbs, size_t tbslen)
171*b0d17251Schristos {
172*b0d17251Schristos     PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
173*b0d17251Schristos     int ret;
174*b0d17251Schristos     unsigned int sltmp;
175*b0d17251Schristos     size_t ecsize = ECDSA_size(ctx->ec);
176*b0d17251Schristos 
177*b0d17251Schristos     if (!ossl_prov_is_running())
178*b0d17251Schristos         return 0;
179*b0d17251Schristos 
180*b0d17251Schristos     if (sig == NULL) {
181*b0d17251Schristos         *siglen = ecsize;
182*b0d17251Schristos         return 1;
183*b0d17251Schristos     }
184*b0d17251Schristos 
185*b0d17251Schristos #if !defined(OPENSSL_NO_ACVP_TESTS)
186*b0d17251Schristos     if (ctx->kattest && !ECDSA_sign_setup(ctx->ec, NULL, &ctx->kinv, &ctx->r))
187*b0d17251Schristos         return 0;
188*b0d17251Schristos #endif
189*b0d17251Schristos 
190*b0d17251Schristos     if (sigsize < (size_t)ecsize)
191*b0d17251Schristos         return 0;
192*b0d17251Schristos 
193*b0d17251Schristos     if (ctx->mdsize != 0 && tbslen != ctx->mdsize)
194*b0d17251Schristos         return 0;
195*b0d17251Schristos 
196*b0d17251Schristos     ret = ECDSA_sign_ex(0, tbs, tbslen, sig, &sltmp, ctx->kinv, ctx->r, ctx->ec);
197*b0d17251Schristos     if (ret <= 0)
198*b0d17251Schristos         return 0;
199*b0d17251Schristos 
200*b0d17251Schristos     *siglen = sltmp;
201*b0d17251Schristos     return 1;
202*b0d17251Schristos }
203*b0d17251Schristos 
ecdsa_verify(void * vctx,const unsigned char * sig,size_t siglen,const unsigned char * tbs,size_t tbslen)204*b0d17251Schristos static int ecdsa_verify(void *vctx, const unsigned char *sig, size_t siglen,
205*b0d17251Schristos                         const unsigned char *tbs, size_t tbslen)
206*b0d17251Schristos {
207*b0d17251Schristos     PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
208*b0d17251Schristos 
209*b0d17251Schristos     if (!ossl_prov_is_running() || (ctx->mdsize != 0 && tbslen != ctx->mdsize))
210*b0d17251Schristos         return 0;
211*b0d17251Schristos 
212*b0d17251Schristos     return ECDSA_verify(0, tbs, tbslen, sig, siglen, ctx->ec);
213*b0d17251Schristos }
214*b0d17251Schristos 
ecdsa_setup_md(PROV_ECDSA_CTX * ctx,const char * mdname,const char * mdprops)215*b0d17251Schristos static int ecdsa_setup_md(PROV_ECDSA_CTX *ctx, const char *mdname,
216*b0d17251Schristos                           const char *mdprops)
217*b0d17251Schristos {
218*b0d17251Schristos     EVP_MD *md = NULL;
219*b0d17251Schristos     size_t mdname_len;
220*b0d17251Schristos     int md_nid, sha1_allowed;
221*b0d17251Schristos     WPACKET pkt;
222*b0d17251Schristos 
223*b0d17251Schristos     if (mdname == NULL)
224*b0d17251Schristos         return 1;
225*b0d17251Schristos 
226*b0d17251Schristos     mdname_len = strlen(mdname);
227*b0d17251Schristos     if (mdname_len >= sizeof(ctx->mdname)) {
228*b0d17251Schristos         ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
229*b0d17251Schristos                        "%s exceeds name buffer length", mdname);
230*b0d17251Schristos         return 0;
231*b0d17251Schristos     }
232*b0d17251Schristos     if (mdprops == NULL)
233*b0d17251Schristos         mdprops = ctx->propq;
234*b0d17251Schristos     md = EVP_MD_fetch(ctx->libctx, mdname, mdprops);
235*b0d17251Schristos     if (md == NULL) {
236*b0d17251Schristos         ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
237*b0d17251Schristos                        "%s could not be fetched", mdname);
238*b0d17251Schristos         return 0;
239*b0d17251Schristos     }
240*b0d17251Schristos     sha1_allowed = (ctx->operation != EVP_PKEY_OP_SIGN);
241*b0d17251Schristos     md_nid = ossl_digest_get_approved_nid_with_sha1(ctx->libctx, md,
242*b0d17251Schristos                                                     sha1_allowed);
243*b0d17251Schristos     if (md_nid < 0) {
244*b0d17251Schristos         ERR_raise_data(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED,
245*b0d17251Schristos                        "digest=%s", mdname);
246*b0d17251Schristos         EVP_MD_free(md);
247*b0d17251Schristos         return 0;
248*b0d17251Schristos     }
249*b0d17251Schristos 
250*b0d17251Schristos     if (!ctx->flag_allow_md) {
251*b0d17251Schristos         if (ctx->mdname[0] != '\0' && !EVP_MD_is_a(md, ctx->mdname)) {
252*b0d17251Schristos             ERR_raise_data(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED,
253*b0d17251Schristos                            "digest %s != %s", mdname, ctx->mdname);
254*b0d17251Schristos             EVP_MD_free(md);
255*b0d17251Schristos             return 0;
256*b0d17251Schristos         }
257*b0d17251Schristos         EVP_MD_free(md);
258*b0d17251Schristos         return 1;
259*b0d17251Schristos     }
260*b0d17251Schristos 
261*b0d17251Schristos     EVP_MD_CTX_free(ctx->mdctx);
262*b0d17251Schristos     EVP_MD_free(ctx->md);
263*b0d17251Schristos 
264*b0d17251Schristos     ctx->aid_len = 0;
265*b0d17251Schristos     if (WPACKET_init_der(&pkt, ctx->aid_buf, sizeof(ctx->aid_buf))
266*b0d17251Schristos         && ossl_DER_w_algorithmIdentifier_ECDSA_with_MD(&pkt, -1, ctx->ec,
267*b0d17251Schristos                                                         md_nid)
268*b0d17251Schristos         && WPACKET_finish(&pkt)) {
269*b0d17251Schristos         WPACKET_get_total_written(&pkt, &ctx->aid_len);
270*b0d17251Schristos         ctx->aid = WPACKET_get_curr(&pkt);
271*b0d17251Schristos     }
272*b0d17251Schristos     WPACKET_cleanup(&pkt);
273*b0d17251Schristos     ctx->mdctx = NULL;
274*b0d17251Schristos     ctx->md = md;
275*b0d17251Schristos     ctx->mdsize = EVP_MD_get_size(ctx->md);
276*b0d17251Schristos     OPENSSL_strlcpy(ctx->mdname, mdname, sizeof(ctx->mdname));
277*b0d17251Schristos 
278*b0d17251Schristos     return 1;
279*b0d17251Schristos }
280*b0d17251Schristos 
ecdsa_digest_signverify_init(void * vctx,const char * mdname,void * ec,const OSSL_PARAM params[],int operation)281*b0d17251Schristos static int ecdsa_digest_signverify_init(void *vctx, const char *mdname,
282*b0d17251Schristos                                         void *ec, const OSSL_PARAM params[],
283*b0d17251Schristos                                         int operation)
284*b0d17251Schristos {
285*b0d17251Schristos     PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
286*b0d17251Schristos 
287*b0d17251Schristos     if (!ossl_prov_is_running())
288*b0d17251Schristos         return 0;
289*b0d17251Schristos 
290*b0d17251Schristos     if (!ecdsa_signverify_init(vctx, ec, params, operation)
291*b0d17251Schristos         || !ecdsa_setup_md(ctx, mdname, NULL))
292*b0d17251Schristos         return 0;
293*b0d17251Schristos 
294*b0d17251Schristos     ctx->flag_allow_md = 0;
295*b0d17251Schristos 
296*b0d17251Schristos     if (ctx->mdctx == NULL) {
297*b0d17251Schristos         ctx->mdctx = EVP_MD_CTX_new();
298*b0d17251Schristos         if (ctx->mdctx == NULL)
299*b0d17251Schristos             goto error;
300*b0d17251Schristos     }
301*b0d17251Schristos 
302*b0d17251Schristos     if (!EVP_DigestInit_ex2(ctx->mdctx, ctx->md, params))
303*b0d17251Schristos         goto error;
304*b0d17251Schristos     return 1;
305*b0d17251Schristos error:
306*b0d17251Schristos     EVP_MD_CTX_free(ctx->mdctx);
307*b0d17251Schristos     ctx->mdctx = NULL;
308*b0d17251Schristos     return 0;
309*b0d17251Schristos }
310*b0d17251Schristos 
ecdsa_digest_sign_init(void * vctx,const char * mdname,void * ec,const OSSL_PARAM params[])311*b0d17251Schristos static int ecdsa_digest_sign_init(void *vctx, const char *mdname, void *ec,
312*b0d17251Schristos                                   const OSSL_PARAM params[])
313*b0d17251Schristos {
314*b0d17251Schristos     return ecdsa_digest_signverify_init(vctx, mdname, ec, params,
315*b0d17251Schristos                                         EVP_PKEY_OP_SIGN);
316*b0d17251Schristos }
317*b0d17251Schristos 
ecdsa_digest_verify_init(void * vctx,const char * mdname,void * ec,const OSSL_PARAM params[])318*b0d17251Schristos static int ecdsa_digest_verify_init(void *vctx, const char *mdname, void *ec,
319*b0d17251Schristos                                     const OSSL_PARAM params[])
320*b0d17251Schristos {
321*b0d17251Schristos     return ecdsa_digest_signverify_init(vctx, mdname, ec, params,
322*b0d17251Schristos                                         EVP_PKEY_OP_VERIFY);
323*b0d17251Schristos }
324*b0d17251Schristos 
ecdsa_digest_signverify_update(void * vctx,const unsigned char * data,size_t datalen)325*b0d17251Schristos int ecdsa_digest_signverify_update(void *vctx, const unsigned char *data,
326*b0d17251Schristos                                    size_t datalen)
327*b0d17251Schristos {
328*b0d17251Schristos     PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
329*b0d17251Schristos 
330*b0d17251Schristos     if (ctx == NULL || ctx->mdctx == NULL)
331*b0d17251Schristos         return 0;
332*b0d17251Schristos 
333*b0d17251Schristos     return EVP_DigestUpdate(ctx->mdctx, data, datalen);
334*b0d17251Schristos }
335*b0d17251Schristos 
ecdsa_digest_sign_final(void * vctx,unsigned char * sig,size_t * siglen,size_t sigsize)336*b0d17251Schristos int ecdsa_digest_sign_final(void *vctx, unsigned char *sig, size_t *siglen,
337*b0d17251Schristos                             size_t sigsize)
338*b0d17251Schristos {
339*b0d17251Schristos     PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
340*b0d17251Schristos     unsigned char digest[EVP_MAX_MD_SIZE];
341*b0d17251Schristos     unsigned int dlen = 0;
342*b0d17251Schristos 
343*b0d17251Schristos     if (!ossl_prov_is_running() || ctx == NULL || ctx->mdctx == NULL)
344*b0d17251Schristos         return 0;
345*b0d17251Schristos 
346*b0d17251Schristos     /*
347*b0d17251Schristos      * If sig is NULL then we're just finding out the sig size. Other fields
348*b0d17251Schristos      * are ignored. Defer to ecdsa_sign.
349*b0d17251Schristos      */
350*b0d17251Schristos     if (sig != NULL
351*b0d17251Schristos         && !EVP_DigestFinal_ex(ctx->mdctx, digest, &dlen))
352*b0d17251Schristos         return 0;
353*b0d17251Schristos     ctx->flag_allow_md = 1;
354*b0d17251Schristos     return ecdsa_sign(vctx, sig, siglen, sigsize, digest, (size_t)dlen);
355*b0d17251Schristos }
356*b0d17251Schristos 
ecdsa_digest_verify_final(void * vctx,const unsigned char * sig,size_t siglen)357*b0d17251Schristos int ecdsa_digest_verify_final(void *vctx, const unsigned char *sig,
358*b0d17251Schristos                               size_t siglen)
359*b0d17251Schristos {
360*b0d17251Schristos     PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
361*b0d17251Schristos     unsigned char digest[EVP_MAX_MD_SIZE];
362*b0d17251Schristos     unsigned int dlen = 0;
363*b0d17251Schristos 
364*b0d17251Schristos     if (!ossl_prov_is_running() || ctx == NULL || ctx->mdctx == NULL)
365*b0d17251Schristos         return 0;
366*b0d17251Schristos 
367*b0d17251Schristos     if (!EVP_DigestFinal_ex(ctx->mdctx, digest, &dlen))
368*b0d17251Schristos         return 0;
369*b0d17251Schristos     ctx->flag_allow_md = 1;
370*b0d17251Schristos     return ecdsa_verify(ctx, sig, siglen, digest, (size_t)dlen);
371*b0d17251Schristos }
372*b0d17251Schristos 
ecdsa_freectx(void * vctx)373*b0d17251Schristos static void ecdsa_freectx(void *vctx)
374*b0d17251Schristos {
375*b0d17251Schristos     PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
376*b0d17251Schristos 
377*b0d17251Schristos     OPENSSL_free(ctx->propq);
378*b0d17251Schristos     EVP_MD_CTX_free(ctx->mdctx);
379*b0d17251Schristos     EVP_MD_free(ctx->md);
380*b0d17251Schristos     ctx->propq = NULL;
381*b0d17251Schristos     ctx->mdctx = NULL;
382*b0d17251Schristos     ctx->md = NULL;
383*b0d17251Schristos     ctx->mdsize = 0;
384*b0d17251Schristos     EC_KEY_free(ctx->ec);
385*b0d17251Schristos     BN_clear_free(ctx->kinv);
386*b0d17251Schristos     BN_clear_free(ctx->r);
387*b0d17251Schristos     OPENSSL_free(ctx);
388*b0d17251Schristos }
389*b0d17251Schristos 
ecdsa_dupctx(void * vctx)390*b0d17251Schristos static void *ecdsa_dupctx(void *vctx)
391*b0d17251Schristos {
392*b0d17251Schristos     PROV_ECDSA_CTX *srcctx = (PROV_ECDSA_CTX *)vctx;
393*b0d17251Schristos     PROV_ECDSA_CTX *dstctx;
394*b0d17251Schristos 
395*b0d17251Schristos     if (!ossl_prov_is_running())
396*b0d17251Schristos         return NULL;
397*b0d17251Schristos 
398*b0d17251Schristos     dstctx = OPENSSL_zalloc(sizeof(*srcctx));
399*b0d17251Schristos     if (dstctx == NULL)
400*b0d17251Schristos         return NULL;
401*b0d17251Schristos 
402*b0d17251Schristos     *dstctx = *srcctx;
403*b0d17251Schristos     dstctx->ec = NULL;
404*b0d17251Schristos     dstctx->md = NULL;
405*b0d17251Schristos     dstctx->mdctx = NULL;
406*b0d17251Schristos     dstctx->propq = NULL;
407*b0d17251Schristos 
408*b0d17251Schristos     if (srcctx->ec != NULL && !EC_KEY_up_ref(srcctx->ec))
409*b0d17251Schristos         goto err;
410*b0d17251Schristos     /* Test KATS should not need to be supported */
411*b0d17251Schristos     if (srcctx->kinv != NULL || srcctx->r != NULL)
412*b0d17251Schristos         goto err;
413*b0d17251Schristos     dstctx->ec = srcctx->ec;
414*b0d17251Schristos 
415*b0d17251Schristos     if (srcctx->md != NULL && !EVP_MD_up_ref(srcctx->md))
416*b0d17251Schristos         goto err;
417*b0d17251Schristos     dstctx->md = srcctx->md;
418*b0d17251Schristos 
419*b0d17251Schristos     if (srcctx->mdctx != NULL) {
420*b0d17251Schristos         dstctx->mdctx = EVP_MD_CTX_new();
421*b0d17251Schristos         if (dstctx->mdctx == NULL
422*b0d17251Schristos                 || !EVP_MD_CTX_copy_ex(dstctx->mdctx, srcctx->mdctx))
423*b0d17251Schristos             goto err;
424*b0d17251Schristos     }
425*b0d17251Schristos 
426*b0d17251Schristos     if (srcctx->propq != NULL) {
427*b0d17251Schristos         dstctx->propq = OPENSSL_strdup(srcctx->propq);
428*b0d17251Schristos         if (dstctx->propq == NULL)
429*b0d17251Schristos             goto err;
430*b0d17251Schristos     }
431*b0d17251Schristos 
432*b0d17251Schristos     return dstctx;
433*b0d17251Schristos  err:
434*b0d17251Schristos     ecdsa_freectx(dstctx);
435*b0d17251Schristos     return NULL;
436*b0d17251Schristos }
437*b0d17251Schristos 
ecdsa_get_ctx_params(void * vctx,OSSL_PARAM * params)438*b0d17251Schristos static int ecdsa_get_ctx_params(void *vctx, OSSL_PARAM *params)
439*b0d17251Schristos {
440*b0d17251Schristos     PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
441*b0d17251Schristos     OSSL_PARAM *p;
442*b0d17251Schristos 
443*b0d17251Schristos     if (ctx == NULL)
444*b0d17251Schristos         return 0;
445*b0d17251Schristos 
446*b0d17251Schristos     p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_ALGORITHM_ID);
447*b0d17251Schristos     if (p != NULL && !OSSL_PARAM_set_octet_string(p, ctx->aid, ctx->aid_len))
448*b0d17251Schristos         return 0;
449*b0d17251Schristos 
450*b0d17251Schristos     p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_DIGEST_SIZE);
451*b0d17251Schristos     if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->mdsize))
452*b0d17251Schristos         return 0;
453*b0d17251Schristos 
454*b0d17251Schristos     p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_DIGEST);
455*b0d17251Schristos     if (p != NULL && !OSSL_PARAM_set_utf8_string(p, ctx->md == NULL
456*b0d17251Schristos                                                     ? ctx->mdname
457*b0d17251Schristos                                                     : EVP_MD_get0_name(ctx->md)))
458*b0d17251Schristos         return 0;
459*b0d17251Schristos 
460*b0d17251Schristos     return 1;
461*b0d17251Schristos }
462*b0d17251Schristos 
463*b0d17251Schristos static const OSSL_PARAM known_gettable_ctx_params[] = {
464*b0d17251Schristos     OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_ALGORITHM_ID, NULL, 0),
465*b0d17251Schristos     OSSL_PARAM_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE, NULL),
466*b0d17251Schristos     OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),
467*b0d17251Schristos     OSSL_PARAM_END
468*b0d17251Schristos };
469*b0d17251Schristos 
ecdsa_gettable_ctx_params(ossl_unused void * vctx,ossl_unused void * provctx)470*b0d17251Schristos static const OSSL_PARAM *ecdsa_gettable_ctx_params(ossl_unused void *vctx,
471*b0d17251Schristos                                                    ossl_unused void *provctx)
472*b0d17251Schristos {
473*b0d17251Schristos     return known_gettable_ctx_params;
474*b0d17251Schristos }
475*b0d17251Schristos 
ecdsa_set_ctx_params(void * vctx,const OSSL_PARAM params[])476*b0d17251Schristos static int ecdsa_set_ctx_params(void *vctx, const OSSL_PARAM params[])
477*b0d17251Schristos {
478*b0d17251Schristos     PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
479*b0d17251Schristos     const OSSL_PARAM *p;
480*b0d17251Schristos     size_t mdsize = 0;
481*b0d17251Schristos 
482*b0d17251Schristos     if (ctx == NULL)
483*b0d17251Schristos         return 0;
484*b0d17251Schristos     if (params == NULL)
485*b0d17251Schristos         return 1;
486*b0d17251Schristos 
487*b0d17251Schristos #if !defined(OPENSSL_NO_ACVP_TESTS)
488*b0d17251Schristos     p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_KAT);
489*b0d17251Schristos     if (p != NULL && !OSSL_PARAM_get_uint(p, &ctx->kattest))
490*b0d17251Schristos         return 0;
491*b0d17251Schristos #endif
492*b0d17251Schristos 
493*b0d17251Schristos     p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_DIGEST);
494*b0d17251Schristos     if (p != NULL) {
495*b0d17251Schristos         char mdname[OSSL_MAX_NAME_SIZE] = "", *pmdname = mdname;
496*b0d17251Schristos         char mdprops[OSSL_MAX_PROPQUERY_SIZE] = "", *pmdprops = mdprops;
497*b0d17251Schristos         const OSSL_PARAM *propsp =
498*b0d17251Schristos             OSSL_PARAM_locate_const(params,
499*b0d17251Schristos                                     OSSL_SIGNATURE_PARAM_PROPERTIES);
500*b0d17251Schristos 
501*b0d17251Schristos         if (!OSSL_PARAM_get_utf8_string(p, &pmdname, sizeof(mdname)))
502*b0d17251Schristos             return 0;
503*b0d17251Schristos         if (propsp != NULL
504*b0d17251Schristos             && !OSSL_PARAM_get_utf8_string(propsp, &pmdprops, sizeof(mdprops)))
505*b0d17251Schristos             return 0;
506*b0d17251Schristos         if (!ecdsa_setup_md(ctx, mdname, mdprops))
507*b0d17251Schristos             return 0;
508*b0d17251Schristos     }
509*b0d17251Schristos 
510*b0d17251Schristos     p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_DIGEST_SIZE);
511*b0d17251Schristos     if (p != NULL) {
512*b0d17251Schristos         if (!OSSL_PARAM_get_size_t(p, &mdsize)
513*b0d17251Schristos             || (!ctx->flag_allow_md && mdsize != ctx->mdsize))
514*b0d17251Schristos             return 0;
515*b0d17251Schristos         ctx->mdsize = mdsize;
516*b0d17251Schristos     }
517*b0d17251Schristos 
518*b0d17251Schristos     return 1;
519*b0d17251Schristos }
520*b0d17251Schristos 
521*b0d17251Schristos static const OSSL_PARAM settable_ctx_params[] = {
522*b0d17251Schristos     OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),
523*b0d17251Schristos     OSSL_PARAM_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE, NULL),
524*b0d17251Schristos     OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PROPERTIES, NULL, 0),
525*b0d17251Schristos     OSSL_PARAM_uint(OSSL_SIGNATURE_PARAM_KAT, NULL),
526*b0d17251Schristos     OSSL_PARAM_END
527*b0d17251Schristos };
528*b0d17251Schristos 
529*b0d17251Schristos static const OSSL_PARAM settable_ctx_params_no_digest[] = {
530*b0d17251Schristos     OSSL_PARAM_uint(OSSL_SIGNATURE_PARAM_KAT, NULL),
531*b0d17251Schristos     OSSL_PARAM_END
532*b0d17251Schristos };
533*b0d17251Schristos 
ecdsa_settable_ctx_params(void * vctx,ossl_unused void * provctx)534*b0d17251Schristos static const OSSL_PARAM *ecdsa_settable_ctx_params(void *vctx,
535*b0d17251Schristos                                                    ossl_unused void *provctx)
536*b0d17251Schristos {
537*b0d17251Schristos     PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
538*b0d17251Schristos 
539*b0d17251Schristos     if (ctx != NULL && !ctx->flag_allow_md)
540*b0d17251Schristos         return settable_ctx_params_no_digest;
541*b0d17251Schristos     return settable_ctx_params;
542*b0d17251Schristos }
543*b0d17251Schristos 
ecdsa_get_ctx_md_params(void * vctx,OSSL_PARAM * params)544*b0d17251Schristos static int ecdsa_get_ctx_md_params(void *vctx, OSSL_PARAM *params)
545*b0d17251Schristos {
546*b0d17251Schristos     PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
547*b0d17251Schristos 
548*b0d17251Schristos     if (ctx->mdctx == NULL)
549*b0d17251Schristos         return 0;
550*b0d17251Schristos 
551*b0d17251Schristos     return EVP_MD_CTX_get_params(ctx->mdctx, params);
552*b0d17251Schristos }
553*b0d17251Schristos 
ecdsa_gettable_ctx_md_params(void * vctx)554*b0d17251Schristos static const OSSL_PARAM *ecdsa_gettable_ctx_md_params(void *vctx)
555*b0d17251Schristos {
556*b0d17251Schristos     PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
557*b0d17251Schristos 
558*b0d17251Schristos     if (ctx->md == NULL)
559*b0d17251Schristos         return 0;
560*b0d17251Schristos 
561*b0d17251Schristos     return EVP_MD_gettable_ctx_params(ctx->md);
562*b0d17251Schristos }
563*b0d17251Schristos 
ecdsa_set_ctx_md_params(void * vctx,const OSSL_PARAM params[])564*b0d17251Schristos static int ecdsa_set_ctx_md_params(void *vctx, const OSSL_PARAM params[])
565*b0d17251Schristos {
566*b0d17251Schristos     PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
567*b0d17251Schristos 
568*b0d17251Schristos     if (ctx->mdctx == NULL)
569*b0d17251Schristos         return 0;
570*b0d17251Schristos 
571*b0d17251Schristos     return EVP_MD_CTX_set_params(ctx->mdctx, params);
572*b0d17251Schristos }
573*b0d17251Schristos 
ecdsa_settable_ctx_md_params(void * vctx)574*b0d17251Schristos static const OSSL_PARAM *ecdsa_settable_ctx_md_params(void *vctx)
575*b0d17251Schristos {
576*b0d17251Schristos     PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
577*b0d17251Schristos 
578*b0d17251Schristos     if (ctx->md == NULL)
579*b0d17251Schristos         return 0;
580*b0d17251Schristos 
581*b0d17251Schristos     return EVP_MD_settable_ctx_params(ctx->md);
582*b0d17251Schristos }
583*b0d17251Schristos 
584*b0d17251Schristos const OSSL_DISPATCH ossl_ecdsa_signature_functions[] = {
585*b0d17251Schristos     { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))ecdsa_newctx },
586*b0d17251Schristos     { OSSL_FUNC_SIGNATURE_SIGN_INIT, (void (*)(void))ecdsa_sign_init },
587*b0d17251Schristos     { OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))ecdsa_sign },
588*b0d17251Schristos     { OSSL_FUNC_SIGNATURE_VERIFY_INIT, (void (*)(void))ecdsa_verify_init },
589*b0d17251Schristos     { OSSL_FUNC_SIGNATURE_VERIFY, (void (*)(void))ecdsa_verify },
590*b0d17251Schristos     { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT,
591*b0d17251Schristos       (void (*)(void))ecdsa_digest_sign_init },
592*b0d17251Schristos     { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE,
593*b0d17251Schristos       (void (*)(void))ecdsa_digest_signverify_update },
594*b0d17251Schristos     { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL,
595*b0d17251Schristos       (void (*)(void))ecdsa_digest_sign_final },
596*b0d17251Schristos     { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT,
597*b0d17251Schristos       (void (*)(void))ecdsa_digest_verify_init },
598*b0d17251Schristos     { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE,
599*b0d17251Schristos       (void (*)(void))ecdsa_digest_signverify_update },
600*b0d17251Schristos     { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL,
601*b0d17251Schristos       (void (*)(void))ecdsa_digest_verify_final },
602*b0d17251Schristos     { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))ecdsa_freectx },
603*b0d17251Schristos     { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))ecdsa_dupctx },
604*b0d17251Schristos     { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, (void (*)(void))ecdsa_get_ctx_params },
605*b0d17251Schristos     { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS,
606*b0d17251Schristos       (void (*)(void))ecdsa_gettable_ctx_params },
607*b0d17251Schristos     { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, (void (*)(void))ecdsa_set_ctx_params },
608*b0d17251Schristos     { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS,
609*b0d17251Schristos       (void (*)(void))ecdsa_settable_ctx_params },
610*b0d17251Schristos     { OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS,
611*b0d17251Schristos       (void (*)(void))ecdsa_get_ctx_md_params },
612*b0d17251Schristos     { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS,
613*b0d17251Schristos       (void (*)(void))ecdsa_gettable_ctx_md_params },
614*b0d17251Schristos     { OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS,
615*b0d17251Schristos       (void (*)(void))ecdsa_set_ctx_md_params },
616*b0d17251Schristos     { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS,
617*b0d17251Schristos       (void (*)(void))ecdsa_settable_ctx_md_params },
618*b0d17251Schristos     { 0, NULL }
619*b0d17251Schristos };
620