1*2139Sjp161948 /* crypto/ecdsa/ecs_ossl.c */
2*2139Sjp161948 /*
3*2139Sjp161948 * Written by Nils Larsch for the OpenSSL project
4*2139Sjp161948 */
5*2139Sjp161948 /* ====================================================================
6*2139Sjp161948 * Copyright (c) 1998-2004 The OpenSSL Project. All rights reserved.
7*2139Sjp161948 *
8*2139Sjp161948 * Redistribution and use in source and binary forms, with or without
9*2139Sjp161948 * modification, are permitted provided that the following conditions
10*2139Sjp161948 * are met:
11*2139Sjp161948 *
12*2139Sjp161948 * 1. Redistributions of source code must retain the above copyright
13*2139Sjp161948 * notice, this list of conditions and the following disclaimer.
14*2139Sjp161948 *
15*2139Sjp161948 * 2. Redistributions in binary form must reproduce the above copyright
16*2139Sjp161948 * notice, this list of conditions and the following disclaimer in
17*2139Sjp161948 * the documentation and/or other materials provided with the
18*2139Sjp161948 * distribution.
19*2139Sjp161948 *
20*2139Sjp161948 * 3. All advertising materials mentioning features or use of this
21*2139Sjp161948 * software must display the following acknowledgment:
22*2139Sjp161948 * "This product includes software developed by the OpenSSL Project
23*2139Sjp161948 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24*2139Sjp161948 *
25*2139Sjp161948 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26*2139Sjp161948 * endorse or promote products derived from this software without
27*2139Sjp161948 * prior written permission. For written permission, please contact
28*2139Sjp161948 * openssl-core@OpenSSL.org.
29*2139Sjp161948 *
30*2139Sjp161948 * 5. Products derived from this software may not be called "OpenSSL"
31*2139Sjp161948 * nor may "OpenSSL" appear in their names without prior written
32*2139Sjp161948 * permission of the OpenSSL Project.
33*2139Sjp161948 *
34*2139Sjp161948 * 6. Redistributions of any form whatsoever must retain the following
35*2139Sjp161948 * acknowledgment:
36*2139Sjp161948 * "This product includes software developed by the OpenSSL Project
37*2139Sjp161948 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38*2139Sjp161948 *
39*2139Sjp161948 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40*2139Sjp161948 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41*2139Sjp161948 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42*2139Sjp161948 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43*2139Sjp161948 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44*2139Sjp161948 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45*2139Sjp161948 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46*2139Sjp161948 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47*2139Sjp161948 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48*2139Sjp161948 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49*2139Sjp161948 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50*2139Sjp161948 * OF THE POSSIBILITY OF SUCH DAMAGE.
51*2139Sjp161948 * ====================================================================
52*2139Sjp161948 *
53*2139Sjp161948 * This product includes cryptographic software written by Eric Young
54*2139Sjp161948 * (eay@cryptsoft.com). This product includes software written by Tim
55*2139Sjp161948 * Hudson (tjh@cryptsoft.com).
56*2139Sjp161948 *
57*2139Sjp161948 */
58*2139Sjp161948
59*2139Sjp161948 #include "ecs_locl.h"
60*2139Sjp161948 #include <openssl/err.h>
61*2139Sjp161948 #include <openssl/obj_mac.h>
62*2139Sjp161948 #include <openssl/bn.h>
63*2139Sjp161948
64*2139Sjp161948 static ECDSA_SIG *ecdsa_do_sign(const unsigned char *dgst, int dlen,
65*2139Sjp161948 const BIGNUM *, const BIGNUM *, EC_KEY *eckey);
66*2139Sjp161948 static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
67*2139Sjp161948 BIGNUM **rp);
68*2139Sjp161948 static int ecdsa_do_verify(const unsigned char *dgst, int dgst_len,
69*2139Sjp161948 const ECDSA_SIG *sig, EC_KEY *eckey);
70*2139Sjp161948
71*2139Sjp161948 static ECDSA_METHOD openssl_ecdsa_meth = {
72*2139Sjp161948 "OpenSSL ECDSA method",
73*2139Sjp161948 ecdsa_do_sign,
74*2139Sjp161948 ecdsa_sign_setup,
75*2139Sjp161948 ecdsa_do_verify,
76*2139Sjp161948 #if 0
77*2139Sjp161948 NULL, /* init */
78*2139Sjp161948 NULL, /* finish */
79*2139Sjp161948 #endif
80*2139Sjp161948 0, /* flags */
81*2139Sjp161948 NULL /* app_data */
82*2139Sjp161948 };
83*2139Sjp161948
ECDSA_OpenSSL(void)84*2139Sjp161948 const ECDSA_METHOD *ECDSA_OpenSSL(void)
85*2139Sjp161948 {
86*2139Sjp161948 return &openssl_ecdsa_meth;
87*2139Sjp161948 }
88*2139Sjp161948
ecdsa_sign_setup(EC_KEY * eckey,BN_CTX * ctx_in,BIGNUM ** kinvp,BIGNUM ** rp)89*2139Sjp161948 static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
90*2139Sjp161948 BIGNUM **rp)
91*2139Sjp161948 {
92*2139Sjp161948 BN_CTX *ctx = NULL;
93*2139Sjp161948 BIGNUM *k = NULL, *r = NULL, *order = NULL, *X = NULL;
94*2139Sjp161948 EC_POINT *tmp_point=NULL;
95*2139Sjp161948 const EC_GROUP *group;
96*2139Sjp161948 int ret = 0;
97*2139Sjp161948
98*2139Sjp161948 if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL)
99*2139Sjp161948 {
100*2139Sjp161948 ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_PASSED_NULL_PARAMETER);
101*2139Sjp161948 return 0;
102*2139Sjp161948 }
103*2139Sjp161948
104*2139Sjp161948 if (ctx_in == NULL)
105*2139Sjp161948 {
106*2139Sjp161948 if ((ctx = BN_CTX_new()) == NULL)
107*2139Sjp161948 {
108*2139Sjp161948 ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP,ERR_R_MALLOC_FAILURE);
109*2139Sjp161948 return 0;
110*2139Sjp161948 }
111*2139Sjp161948 }
112*2139Sjp161948 else
113*2139Sjp161948 ctx = ctx_in;
114*2139Sjp161948
115*2139Sjp161948 k = BN_new(); /* this value is later returned in *kinvp */
116*2139Sjp161948 r = BN_new(); /* this value is later returned in *rp */
117*2139Sjp161948 order = BN_new();
118*2139Sjp161948 X = BN_new();
119*2139Sjp161948 if (!k || !r || !order || !X)
120*2139Sjp161948 {
121*2139Sjp161948 ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);
122*2139Sjp161948 goto err;
123*2139Sjp161948 }
124*2139Sjp161948 if ((tmp_point = EC_POINT_new(group)) == NULL)
125*2139Sjp161948 {
126*2139Sjp161948 ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
127*2139Sjp161948 goto err;
128*2139Sjp161948 }
129*2139Sjp161948 if (!EC_GROUP_get_order(group, order, ctx))
130*2139Sjp161948 {
131*2139Sjp161948 ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
132*2139Sjp161948 goto err;
133*2139Sjp161948 }
134*2139Sjp161948
135*2139Sjp161948 do
136*2139Sjp161948 {
137*2139Sjp161948 /* get random k */
138*2139Sjp161948 do
139*2139Sjp161948 if (!BN_rand_range(k, order))
140*2139Sjp161948 {
141*2139Sjp161948 ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP,
142*2139Sjp161948 ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED);
143*2139Sjp161948 goto err;
144*2139Sjp161948 }
145*2139Sjp161948 while (BN_is_zero(k));
146*2139Sjp161948
147*2139Sjp161948 /* compute r the x-coordinate of generator * k */
148*2139Sjp161948 if (!EC_POINT_mul(group, tmp_point, k, NULL, NULL, ctx))
149*2139Sjp161948 {
150*2139Sjp161948 ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
151*2139Sjp161948 goto err;
152*2139Sjp161948 }
153*2139Sjp161948 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field)
154*2139Sjp161948 {
155*2139Sjp161948 if (!EC_POINT_get_affine_coordinates_GFp(group,
156*2139Sjp161948 tmp_point, X, NULL, ctx))
157*2139Sjp161948 {
158*2139Sjp161948 ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP,ERR_R_EC_LIB);
159*2139Sjp161948 goto err;
160*2139Sjp161948 }
161*2139Sjp161948 }
162*2139Sjp161948 else /* NID_X9_62_characteristic_two_field */
163*2139Sjp161948 {
164*2139Sjp161948 if (!EC_POINT_get_affine_coordinates_GF2m(group,
165*2139Sjp161948 tmp_point, X, NULL, ctx))
166*2139Sjp161948 {
167*2139Sjp161948 ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP,ERR_R_EC_LIB);
168*2139Sjp161948 goto err;
169*2139Sjp161948 }
170*2139Sjp161948 }
171*2139Sjp161948 if (!BN_nnmod(r, X, order, ctx))
172*2139Sjp161948 {
173*2139Sjp161948 ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
174*2139Sjp161948 goto err;
175*2139Sjp161948 }
176*2139Sjp161948 }
177*2139Sjp161948 while (BN_is_zero(r));
178*2139Sjp161948
179*2139Sjp161948 /* compute the inverse of k */
180*2139Sjp161948 if (!BN_mod_inverse(k, k, order, ctx))
181*2139Sjp161948 {
182*2139Sjp161948 ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
183*2139Sjp161948 goto err;
184*2139Sjp161948 }
185*2139Sjp161948 /* clear old values if necessary */
186*2139Sjp161948 if (*rp != NULL)
187*2139Sjp161948 BN_clear_free(*rp);
188*2139Sjp161948 if (*kinvp != NULL)
189*2139Sjp161948 BN_clear_free(*kinvp);
190*2139Sjp161948 /* save the pre-computed values */
191*2139Sjp161948 *rp = r;
192*2139Sjp161948 *kinvp = k;
193*2139Sjp161948 ret = 1;
194*2139Sjp161948 err:
195*2139Sjp161948 if (!ret)
196*2139Sjp161948 {
197*2139Sjp161948 if (k != NULL) BN_clear_free(k);
198*2139Sjp161948 if (r != NULL) BN_clear_free(r);
199*2139Sjp161948 }
200*2139Sjp161948 if (ctx_in == NULL)
201*2139Sjp161948 BN_CTX_free(ctx);
202*2139Sjp161948 if (order != NULL)
203*2139Sjp161948 BN_free(order);
204*2139Sjp161948 if (tmp_point != NULL)
205*2139Sjp161948 EC_POINT_free(tmp_point);
206*2139Sjp161948 if (X)
207*2139Sjp161948 BN_clear_free(X);
208*2139Sjp161948 return(ret);
209*2139Sjp161948 }
210*2139Sjp161948
211*2139Sjp161948
ecdsa_do_sign(const unsigned char * dgst,int dgst_len,const BIGNUM * in_kinv,const BIGNUM * in_r,EC_KEY * eckey)212*2139Sjp161948 static ECDSA_SIG *ecdsa_do_sign(const unsigned char *dgst, int dgst_len,
213*2139Sjp161948 const BIGNUM *in_kinv, const BIGNUM *in_r, EC_KEY *eckey)
214*2139Sjp161948 {
215*2139Sjp161948 int ok = 0;
216*2139Sjp161948 BIGNUM *kinv=NULL, *s, *m=NULL,*tmp=NULL,*order=NULL;
217*2139Sjp161948 const BIGNUM *ckinv;
218*2139Sjp161948 BN_CTX *ctx = NULL;
219*2139Sjp161948 const EC_GROUP *group;
220*2139Sjp161948 ECDSA_SIG *ret;
221*2139Sjp161948 ECDSA_DATA *ecdsa;
222*2139Sjp161948 const BIGNUM *priv_key;
223*2139Sjp161948
224*2139Sjp161948 ecdsa = ecdsa_check(eckey);
225*2139Sjp161948 group = EC_KEY_get0_group(eckey);
226*2139Sjp161948 priv_key = EC_KEY_get0_private_key(eckey);
227*2139Sjp161948
228*2139Sjp161948 if (group == NULL || priv_key == NULL || ecdsa == NULL)
229*2139Sjp161948 {
230*2139Sjp161948 ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_PASSED_NULL_PARAMETER);
231*2139Sjp161948 return NULL;
232*2139Sjp161948 }
233*2139Sjp161948
234*2139Sjp161948 ret = ECDSA_SIG_new();
235*2139Sjp161948 if (!ret)
236*2139Sjp161948 {
237*2139Sjp161948 ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_MALLOC_FAILURE);
238*2139Sjp161948 return NULL;
239*2139Sjp161948 }
240*2139Sjp161948 s = ret->s;
241*2139Sjp161948
242*2139Sjp161948 if ((ctx = BN_CTX_new()) == NULL || (order = BN_new()) == NULL ||
243*2139Sjp161948 (tmp = BN_new()) == NULL || (m = BN_new()) == NULL)
244*2139Sjp161948 {
245*2139Sjp161948 ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_MALLOC_FAILURE);
246*2139Sjp161948 goto err;
247*2139Sjp161948 }
248*2139Sjp161948
249*2139Sjp161948 if (!EC_GROUP_get_order(group, order, ctx))
250*2139Sjp161948 {
251*2139Sjp161948 ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_EC_LIB);
252*2139Sjp161948 goto err;
253*2139Sjp161948 }
254*2139Sjp161948 if (dgst_len > BN_num_bytes(order))
255*2139Sjp161948 {
256*2139Sjp161948 ECDSAerr(ECDSA_F_ECDSA_DO_SIGN,
257*2139Sjp161948 ECDSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
258*2139Sjp161948 goto err;
259*2139Sjp161948 }
260*2139Sjp161948
261*2139Sjp161948 if (!BN_bin2bn(dgst, dgst_len, m))
262*2139Sjp161948 {
263*2139Sjp161948 ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
264*2139Sjp161948 goto err;
265*2139Sjp161948 }
266*2139Sjp161948 do
267*2139Sjp161948 {
268*2139Sjp161948 if (in_kinv == NULL || in_r == NULL)
269*2139Sjp161948 {
270*2139Sjp161948 if (!ECDSA_sign_setup(eckey, ctx, &kinv, &ret->r))
271*2139Sjp161948 {
272*2139Sjp161948 ECDSAerr(ECDSA_F_ECDSA_DO_SIGN,ERR_R_ECDSA_LIB);
273*2139Sjp161948 goto err;
274*2139Sjp161948 }
275*2139Sjp161948 ckinv = kinv;
276*2139Sjp161948 }
277*2139Sjp161948 else
278*2139Sjp161948 {
279*2139Sjp161948 ckinv = in_kinv;
280*2139Sjp161948 if (BN_copy(ret->r, in_r) == NULL)
281*2139Sjp161948 {
282*2139Sjp161948 ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_MALLOC_FAILURE);
283*2139Sjp161948 goto err;
284*2139Sjp161948 }
285*2139Sjp161948 }
286*2139Sjp161948
287*2139Sjp161948 if (!BN_mod_mul(tmp, priv_key, ret->r, order, ctx))
288*2139Sjp161948 {
289*2139Sjp161948 ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
290*2139Sjp161948 goto err;
291*2139Sjp161948 }
292*2139Sjp161948 if (!BN_mod_add_quick(s, tmp, m, order))
293*2139Sjp161948 {
294*2139Sjp161948 ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
295*2139Sjp161948 goto err;
296*2139Sjp161948 }
297*2139Sjp161948 if (!BN_mod_mul(s, s, ckinv, order, ctx))
298*2139Sjp161948 {
299*2139Sjp161948 ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
300*2139Sjp161948 goto err;
301*2139Sjp161948 }
302*2139Sjp161948 }
303*2139Sjp161948 while (BN_is_zero(s));
304*2139Sjp161948
305*2139Sjp161948 ok = 1;
306*2139Sjp161948 err:
307*2139Sjp161948 if (!ok)
308*2139Sjp161948 {
309*2139Sjp161948 ECDSA_SIG_free(ret);
310*2139Sjp161948 ret = NULL;
311*2139Sjp161948 }
312*2139Sjp161948 if (ctx)
313*2139Sjp161948 BN_CTX_free(ctx);
314*2139Sjp161948 if (m)
315*2139Sjp161948 BN_clear_free(m);
316*2139Sjp161948 if (tmp)
317*2139Sjp161948 BN_clear_free(tmp);
318*2139Sjp161948 if (order)
319*2139Sjp161948 BN_free(order);
320*2139Sjp161948 if (kinv)
321*2139Sjp161948 BN_clear_free(kinv);
322*2139Sjp161948 return ret;
323*2139Sjp161948 }
324*2139Sjp161948
ecdsa_do_verify(const unsigned char * dgst,int dgst_len,const ECDSA_SIG * sig,EC_KEY * eckey)325*2139Sjp161948 static int ecdsa_do_verify(const unsigned char *dgst, int dgst_len,
326*2139Sjp161948 const ECDSA_SIG *sig, EC_KEY *eckey)
327*2139Sjp161948 {
328*2139Sjp161948 int ret = -1;
329*2139Sjp161948 BN_CTX *ctx;
330*2139Sjp161948 BIGNUM *order, *u1, *u2, *m, *X;
331*2139Sjp161948 EC_POINT *point = NULL;
332*2139Sjp161948 const EC_GROUP *group;
333*2139Sjp161948 const EC_POINT *pub_key;
334*2139Sjp161948
335*2139Sjp161948 /* check input values */
336*2139Sjp161948 if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL ||
337*2139Sjp161948 (pub_key = EC_KEY_get0_public_key(eckey)) == NULL || sig == NULL)
338*2139Sjp161948 {
339*2139Sjp161948 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ECDSA_R_MISSING_PARAMETERS);
340*2139Sjp161948 return -1;
341*2139Sjp161948 }
342*2139Sjp161948
343*2139Sjp161948 ctx = BN_CTX_new();
344*2139Sjp161948 if (!ctx)
345*2139Sjp161948 {
346*2139Sjp161948 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_MALLOC_FAILURE);
347*2139Sjp161948 return -1;
348*2139Sjp161948 }
349*2139Sjp161948 BN_CTX_start(ctx);
350*2139Sjp161948 order = BN_CTX_get(ctx);
351*2139Sjp161948 u1 = BN_CTX_get(ctx);
352*2139Sjp161948 u2 = BN_CTX_get(ctx);
353*2139Sjp161948 m = BN_CTX_get(ctx);
354*2139Sjp161948 X = BN_CTX_get(ctx);
355*2139Sjp161948 if (!X)
356*2139Sjp161948 {
357*2139Sjp161948 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
358*2139Sjp161948 goto err;
359*2139Sjp161948 }
360*2139Sjp161948
361*2139Sjp161948 if (!EC_GROUP_get_order(group, order, ctx))
362*2139Sjp161948 {
363*2139Sjp161948 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
364*2139Sjp161948 goto err;
365*2139Sjp161948 }
366*2139Sjp161948
367*2139Sjp161948 if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||
368*2139Sjp161948 BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s) ||
369*2139Sjp161948 BN_is_negative(sig->s) || BN_ucmp(sig->s, order) >= 0)
370*2139Sjp161948 {
371*2139Sjp161948 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ECDSA_R_BAD_SIGNATURE);
372*2139Sjp161948 ret = 0; /* signature is invalid */
373*2139Sjp161948 goto err;
374*2139Sjp161948 }
375*2139Sjp161948 /* calculate tmp1 = inv(S) mod order */
376*2139Sjp161948 if (!BN_mod_inverse(u2, sig->s, order, ctx))
377*2139Sjp161948 {
378*2139Sjp161948 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
379*2139Sjp161948 goto err;
380*2139Sjp161948 }
381*2139Sjp161948 /* digest -> m */
382*2139Sjp161948 if (!BN_bin2bn(dgst, dgst_len, m))
383*2139Sjp161948 {
384*2139Sjp161948 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
385*2139Sjp161948 goto err;
386*2139Sjp161948 }
387*2139Sjp161948 /* u1 = m * tmp mod order */
388*2139Sjp161948 if (!BN_mod_mul(u1, m, u2, order, ctx))
389*2139Sjp161948 {
390*2139Sjp161948 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
391*2139Sjp161948 goto err;
392*2139Sjp161948 }
393*2139Sjp161948 /* u2 = r * w mod q */
394*2139Sjp161948 if (!BN_mod_mul(u2, sig->r, u2, order, ctx))
395*2139Sjp161948 {
396*2139Sjp161948 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
397*2139Sjp161948 goto err;
398*2139Sjp161948 }
399*2139Sjp161948
400*2139Sjp161948 if ((point = EC_POINT_new(group)) == NULL)
401*2139Sjp161948 {
402*2139Sjp161948 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_MALLOC_FAILURE);
403*2139Sjp161948 goto err;
404*2139Sjp161948 }
405*2139Sjp161948 if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx))
406*2139Sjp161948 {
407*2139Sjp161948 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
408*2139Sjp161948 goto err;
409*2139Sjp161948 }
410*2139Sjp161948 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field)
411*2139Sjp161948 {
412*2139Sjp161948 if (!EC_POINT_get_affine_coordinates_GFp(group,
413*2139Sjp161948 point, X, NULL, ctx))
414*2139Sjp161948 {
415*2139Sjp161948 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
416*2139Sjp161948 goto err;
417*2139Sjp161948 }
418*2139Sjp161948 }
419*2139Sjp161948 else /* NID_X9_62_characteristic_two_field */
420*2139Sjp161948 {
421*2139Sjp161948 if (!EC_POINT_get_affine_coordinates_GF2m(group,
422*2139Sjp161948 point, X, NULL, ctx))
423*2139Sjp161948 {
424*2139Sjp161948 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
425*2139Sjp161948 goto err;
426*2139Sjp161948 }
427*2139Sjp161948 }
428*2139Sjp161948
429*2139Sjp161948 if (!BN_nnmod(u1, X, order, ctx))
430*2139Sjp161948 {
431*2139Sjp161948 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
432*2139Sjp161948 goto err;
433*2139Sjp161948 }
434*2139Sjp161948 /* if the signature is correct u1 is equal to sig->r */
435*2139Sjp161948 ret = (BN_ucmp(u1, sig->r) == 0);
436*2139Sjp161948 err:
437*2139Sjp161948 BN_CTX_end(ctx);
438*2139Sjp161948 BN_CTX_free(ctx);
439*2139Sjp161948 if (point)
440*2139Sjp161948 EC_POINT_free(point);
441*2139Sjp161948 return ret;
442*2139Sjp161948 }
443