1160814Ssimon/* crypto/ecdsa/ecs_ossl.c */
2160814Ssimon/*
3160814Ssimon * Written by Nils Larsch for the OpenSSL project
4160814Ssimon */
5160814Ssimon/* ====================================================================
6160814Ssimon * Copyright (c) 1998-2004 The OpenSSL Project.  All rights reserved.
7160814Ssimon *
8160814Ssimon * Redistribution and use in source and binary forms, with or without
9160814Ssimon * modification, are permitted provided that the following conditions
10160814Ssimon * are met:
11160814Ssimon *
12160814Ssimon * 1. Redistributions of source code must retain the above copyright
13280304Sjkim *    notice, this list of conditions and the following disclaimer.
14160814Ssimon *
15160814Ssimon * 2. Redistributions in binary form must reproduce the above copyright
16160814Ssimon *    notice, this list of conditions and the following disclaimer in
17160814Ssimon *    the documentation and/or other materials provided with the
18160814Ssimon *    distribution.
19160814Ssimon *
20160814Ssimon * 3. All advertising materials mentioning features or use of this
21160814Ssimon *    software must display the following acknowledgment:
22160814Ssimon *    "This product includes software developed by the OpenSSL Project
23160814Ssimon *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24160814Ssimon *
25160814Ssimon * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26160814Ssimon *    endorse or promote products derived from this software without
27160814Ssimon *    prior written permission. For written permission, please contact
28160814Ssimon *    openssl-core@OpenSSL.org.
29160814Ssimon *
30160814Ssimon * 5. Products derived from this software may not be called "OpenSSL"
31160814Ssimon *    nor may "OpenSSL" appear in their names without prior written
32160814Ssimon *    permission of the OpenSSL Project.
33160814Ssimon *
34160814Ssimon * 6. Redistributions of any form whatsoever must retain the following
35160814Ssimon *    acknowledgment:
36160814Ssimon *    "This product includes software developed by the OpenSSL Project
37160814Ssimon *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38160814Ssimon *
39160814Ssimon * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40160814Ssimon * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41160814Ssimon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42160814Ssimon * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43160814Ssimon * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44160814Ssimon * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45160814Ssimon * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46160814Ssimon * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47160814Ssimon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48160814Ssimon * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49160814Ssimon * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50160814Ssimon * OF THE POSSIBILITY OF SUCH DAMAGE.
51160814Ssimon * ====================================================================
52160814Ssimon *
53160814Ssimon * This product includes cryptographic software written by Eric Young
54160814Ssimon * (eay@cryptsoft.com).  This product includes software written by Tim
55160814Ssimon * Hudson (tjh@cryptsoft.com).
56160814Ssimon *
57160814Ssimon */
58160814Ssimon
59160814Ssimon#include "ecs_locl.h"
60160814Ssimon#include <openssl/err.h>
61160814Ssimon#include <openssl/obj_mac.h>
62160814Ssimon#include <openssl/bn.h>
63160814Ssimon
64280304Sjkimstatic ECDSA_SIG *ecdsa_do_sign(const unsigned char *dgst, int dlen,
65280304Sjkim                                const BIGNUM *, const BIGNUM *,
66280304Sjkim                                EC_KEY *eckey);
67280304Sjkimstatic int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
68280304Sjkim                            BIGNUM **rp);
69280304Sjkimstatic int ecdsa_do_verify(const unsigned char *dgst, int dgst_len,
70280304Sjkim                           const ECDSA_SIG *sig, EC_KEY *eckey);
71160814Ssimon
72160814Ssimonstatic ECDSA_METHOD openssl_ecdsa_meth = {
73280304Sjkim    "OpenSSL ECDSA method",
74280304Sjkim    ecdsa_do_sign,
75280304Sjkim    ecdsa_sign_setup,
76280304Sjkim    ecdsa_do_verify,
77160814Ssimon#if 0
78280304Sjkim    NULL,                       /* init */
79280304Sjkim    NULL,                       /* finish */
80160814Ssimon#endif
81280304Sjkim    0,                          /* flags */
82280304Sjkim    NULL                        /* app_data */
83160814Ssimon};
84160814Ssimon
85160814Ssimonconst ECDSA_METHOD *ECDSA_OpenSSL(void)
86160814Ssimon{
87280304Sjkim    return &openssl_ecdsa_meth;
88160814Ssimon}
89160814Ssimon
90160814Ssimonstatic int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
91280304Sjkim                            BIGNUM **rp)
92160814Ssimon{
93280304Sjkim    BN_CTX *ctx = NULL;
94280304Sjkim    BIGNUM *k = NULL, *r = NULL, *order = NULL, *X = NULL;
95280304Sjkim    EC_POINT *tmp_point = NULL;
96280304Sjkim    const EC_GROUP *group;
97280304Sjkim    int ret = 0;
98160814Ssimon
99280304Sjkim    if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL) {
100280304Sjkim        ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_PASSED_NULL_PARAMETER);
101280304Sjkim        return 0;
102280304Sjkim    }
103160814Ssimon
104280304Sjkim    if (ctx_in == NULL) {
105280304Sjkim        if ((ctx = BN_CTX_new()) == NULL) {
106280304Sjkim            ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);
107280304Sjkim            return 0;
108280304Sjkim        }
109280304Sjkim    } else
110280304Sjkim        ctx = ctx_in;
111160814Ssimon
112280304Sjkim    k = BN_new();               /* this value is later returned in *kinvp */
113280304Sjkim    r = BN_new();               /* this value is later returned in *rp */
114280304Sjkim    order = BN_new();
115280304Sjkim    X = BN_new();
116280304Sjkim    if (!k || !r || !order || !X) {
117280304Sjkim        ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);
118280304Sjkim        goto err;
119280304Sjkim    }
120280304Sjkim    if ((tmp_point = EC_POINT_new(group)) == NULL) {
121280304Sjkim        ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
122280304Sjkim        goto err;
123280304Sjkim    }
124280304Sjkim    if (!EC_GROUP_get_order(group, order, ctx)) {
125280304Sjkim        ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
126280304Sjkim        goto err;
127280304Sjkim    }
128160814Ssimon
129280304Sjkim    do {
130280304Sjkim        /* get random k */
131280304Sjkim        do
132280304Sjkim            if (!BN_rand_range(k, order)) {
133280304Sjkim                ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP,
134280304Sjkim                         ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED);
135280304Sjkim                goto err;
136280304Sjkim            }
137280304Sjkim        while (BN_is_zero(k)) ;
138237657Sjkim
139280304Sjkim        /*
140280304Sjkim         * We do not want timing information to leak the length of k, so we
141280304Sjkim         * compute G*k using an equivalent scalar of fixed bit-length.
142280304Sjkim         */
143237657Sjkim
144280304Sjkim        if (!BN_add(k, k, order))
145280304Sjkim            goto err;
146280304Sjkim        if (BN_num_bits(k) <= BN_num_bits(order))
147280304Sjkim            if (!BN_add(k, k, order))
148280304Sjkim                goto err;
149280304Sjkim
150280304Sjkim        /* compute r the x-coordinate of generator * k */
151280304Sjkim        if (!EC_POINT_mul(group, tmp_point, k, NULL, NULL, ctx)) {
152280304Sjkim            ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
153280304Sjkim            goto err;
154280304Sjkim        }
155280304Sjkim        if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==
156280304Sjkim            NID_X9_62_prime_field) {
157280304Sjkim            if (!EC_POINT_get_affine_coordinates_GFp
158280304Sjkim                (group, tmp_point, X, NULL, ctx)) {
159280304Sjkim                ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
160280304Sjkim                goto err;
161280304Sjkim            }
162280304Sjkim        }
163238405Sjkim#ifndef OPENSSL_NO_EC2M
164280304Sjkim        else {                  /* NID_X9_62_characteristic_two_field */
165280304Sjkim
166280304Sjkim            if (!EC_POINT_get_affine_coordinates_GF2m(group,
167280304Sjkim                                                      tmp_point, X, NULL,
168280304Sjkim                                                      ctx)) {
169280304Sjkim                ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
170280304Sjkim                goto err;
171280304Sjkim            }
172280304Sjkim        }
173238405Sjkim#endif
174280304Sjkim        if (!BN_nnmod(r, X, order, ctx)) {
175280304Sjkim            ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
176280304Sjkim            goto err;
177280304Sjkim        }
178280304Sjkim    }
179280304Sjkim    while (BN_is_zero(r));
180160814Ssimon
181280304Sjkim    /* compute the inverse of k */
182280304Sjkim    if (!BN_mod_inverse(k, k, order, ctx)) {
183280304Sjkim        ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
184280304Sjkim        goto err;
185280304Sjkim    }
186280304Sjkim    /* clear old values if necessary */
187280304Sjkim    if (*rp != NULL)
188280304Sjkim        BN_clear_free(*rp);
189280304Sjkim    if (*kinvp != NULL)
190280304Sjkim        BN_clear_free(*kinvp);
191280304Sjkim    /* save the pre-computed values  */
192280304Sjkim    *rp = r;
193280304Sjkim    *kinvp = k;
194280304Sjkim    ret = 1;
195280304Sjkim err:
196280304Sjkim    if (!ret) {
197280304Sjkim        if (k != NULL)
198280304Sjkim            BN_clear_free(k);
199280304Sjkim        if (r != NULL)
200280304Sjkim            BN_clear_free(r);
201280304Sjkim    }
202280304Sjkim    if (ctx_in == NULL)
203280304Sjkim        BN_CTX_free(ctx);
204280304Sjkim    if (order != NULL)
205280304Sjkim        BN_free(order);
206280304Sjkim    if (tmp_point != NULL)
207280304Sjkim        EC_POINT_free(tmp_point);
208280304Sjkim    if (X)
209280304Sjkim        BN_clear_free(X);
210280304Sjkim    return (ret);
211160814Ssimon}
212160814Ssimon
213280304Sjkimstatic ECDSA_SIG *ecdsa_do_sign(const unsigned char *dgst, int dgst_len,
214280304Sjkim                                const BIGNUM *in_kinv, const BIGNUM *in_r,
215280304Sjkim                                EC_KEY *eckey)
216160814Ssimon{
217280304Sjkim    int ok = 0, i;
218280304Sjkim    BIGNUM *kinv = NULL, *s, *m = NULL, *tmp = NULL, *order = NULL;
219280304Sjkim    const BIGNUM *ckinv;
220280304Sjkim    BN_CTX *ctx = NULL;
221280304Sjkim    const EC_GROUP *group;
222280304Sjkim    ECDSA_SIG *ret;
223280304Sjkim    ECDSA_DATA *ecdsa;
224280304Sjkim    const BIGNUM *priv_key;
225160814Ssimon
226280304Sjkim    ecdsa = ecdsa_check(eckey);
227280304Sjkim    group = EC_KEY_get0_group(eckey);
228280304Sjkim    priv_key = EC_KEY_get0_private_key(eckey);
229160814Ssimon
230280304Sjkim    if (group == NULL || priv_key == NULL || ecdsa == NULL) {
231280304Sjkim        ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_PASSED_NULL_PARAMETER);
232280304Sjkim        return NULL;
233280304Sjkim    }
234160814Ssimon
235280304Sjkim    ret = ECDSA_SIG_new();
236280304Sjkim    if (!ret) {
237280304Sjkim        ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_MALLOC_FAILURE);
238280304Sjkim        return NULL;
239280304Sjkim    }
240280304Sjkim    s = ret->s;
241160814Ssimon
242280304Sjkim    if ((ctx = BN_CTX_new()) == NULL || (order = BN_new()) == NULL ||
243280304Sjkim        (tmp = BN_new()) == NULL || (m = BN_new()) == NULL) {
244280304Sjkim        ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_MALLOC_FAILURE);
245280304Sjkim        goto err;
246280304Sjkim    }
247160814Ssimon
248280304Sjkim    if (!EC_GROUP_get_order(group, order, ctx)) {
249280304Sjkim        ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_EC_LIB);
250280304Sjkim        goto err;
251280304Sjkim    }
252280304Sjkim    i = BN_num_bits(order);
253280304Sjkim    /*
254280304Sjkim     * Need to truncate digest if it is too long: first truncate whole bytes.
255280304Sjkim     */
256280304Sjkim    if (8 * dgst_len > i)
257280304Sjkim        dgst_len = (i + 7) / 8;
258280304Sjkim    if (!BN_bin2bn(dgst, dgst_len, m)) {
259280304Sjkim        ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
260280304Sjkim        goto err;
261280304Sjkim    }
262280304Sjkim    /* If still too long truncate remaining bits with a shift */
263280304Sjkim    if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
264280304Sjkim        ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
265280304Sjkim        goto err;
266280304Sjkim    }
267280304Sjkim    do {
268280304Sjkim        if (in_kinv == NULL || in_r == NULL) {
269280304Sjkim            if (!ECDSA_sign_setup(eckey, ctx, &kinv, &ret->r)) {
270280304Sjkim                ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_ECDSA_LIB);
271280304Sjkim                goto err;
272280304Sjkim            }
273280304Sjkim            ckinv = kinv;
274280304Sjkim        } else {
275280304Sjkim            ckinv = in_kinv;
276280304Sjkim            if (BN_copy(ret->r, in_r) == NULL) {
277280304Sjkim                ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_MALLOC_FAILURE);
278280304Sjkim                goto err;
279280304Sjkim            }
280280304Sjkim        }
281160814Ssimon
282280304Sjkim        if (!BN_mod_mul(tmp, priv_key, ret->r, order, ctx)) {
283280304Sjkim            ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
284280304Sjkim            goto err;
285280304Sjkim        }
286280304Sjkim        if (!BN_mod_add_quick(s, tmp, m, order)) {
287280304Sjkim            ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
288280304Sjkim            goto err;
289280304Sjkim        }
290280304Sjkim        if (!BN_mod_mul(s, s, ckinv, order, ctx)) {
291280304Sjkim            ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
292280304Sjkim            goto err;
293280304Sjkim        }
294280304Sjkim        if (BN_is_zero(s)) {
295280304Sjkim            /*
296280304Sjkim             * if kinv and r have been supplied by the caller don't to
297280304Sjkim             * generate new kinv and r values
298280304Sjkim             */
299280304Sjkim            if (in_kinv != NULL && in_r != NULL) {
300280304Sjkim                ECDSAerr(ECDSA_F_ECDSA_DO_SIGN,
301280304Sjkim                         ECDSA_R_NEED_NEW_SETUP_VALUES);
302280304Sjkim                goto err;
303280304Sjkim            }
304280304Sjkim        } else
305280304Sjkim            /* s != 0 => we have a valid signature */
306280304Sjkim            break;
307280304Sjkim    }
308280304Sjkim    while (1);
309280304Sjkim
310280304Sjkim    ok = 1;
311280304Sjkim err:
312280304Sjkim    if (!ok) {
313280304Sjkim        ECDSA_SIG_free(ret);
314280304Sjkim        ret = NULL;
315280304Sjkim    }
316280304Sjkim    if (ctx)
317280304Sjkim        BN_CTX_free(ctx);
318280304Sjkim    if (m)
319280304Sjkim        BN_clear_free(m);
320280304Sjkim    if (tmp)
321280304Sjkim        BN_clear_free(tmp);
322280304Sjkim    if (order)
323280304Sjkim        BN_free(order);
324280304Sjkim    if (kinv)
325280304Sjkim        BN_clear_free(kinv);
326280304Sjkim    return ret;
327160814Ssimon}
328160814Ssimon
329160814Ssimonstatic int ecdsa_do_verify(const unsigned char *dgst, int dgst_len,
330280304Sjkim                           const ECDSA_SIG *sig, EC_KEY *eckey)
331160814Ssimon{
332280304Sjkim    int ret = -1, i;
333280304Sjkim    BN_CTX *ctx;
334280304Sjkim    BIGNUM *order, *u1, *u2, *m, *X;
335280304Sjkim    EC_POINT *point = NULL;
336280304Sjkim    const EC_GROUP *group;
337280304Sjkim    const EC_POINT *pub_key;
338160814Ssimon
339280304Sjkim    /* check input values */
340280304Sjkim    if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL ||
341280304Sjkim        (pub_key = EC_KEY_get0_public_key(eckey)) == NULL || sig == NULL) {
342280304Sjkim        ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ECDSA_R_MISSING_PARAMETERS);
343280304Sjkim        return -1;
344280304Sjkim    }
345160814Ssimon
346280304Sjkim    ctx = BN_CTX_new();
347280304Sjkim    if (!ctx) {
348280304Sjkim        ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_MALLOC_FAILURE);
349280304Sjkim        return -1;
350280304Sjkim    }
351280304Sjkim    BN_CTX_start(ctx);
352280304Sjkim    order = BN_CTX_get(ctx);
353280304Sjkim    u1 = BN_CTX_get(ctx);
354280304Sjkim    u2 = BN_CTX_get(ctx);
355280304Sjkim    m = BN_CTX_get(ctx);
356280304Sjkim    X = BN_CTX_get(ctx);
357280304Sjkim    if (!X) {
358280304Sjkim        ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
359280304Sjkim        goto err;
360280304Sjkim    }
361160814Ssimon
362280304Sjkim    if (!EC_GROUP_get_order(group, order, ctx)) {
363280304Sjkim        ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
364280304Sjkim        goto err;
365280304Sjkim    }
366160814Ssimon
367280304Sjkim    if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||
368280304Sjkim        BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s) ||
369280304Sjkim        BN_is_negative(sig->s) || BN_ucmp(sig->s, order) >= 0) {
370280304Sjkim        ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ECDSA_R_BAD_SIGNATURE);
371280304Sjkim        ret = 0;                /* signature is invalid */
372280304Sjkim        goto err;
373280304Sjkim    }
374280304Sjkim    /* calculate tmp1 = inv(S) mod order */
375280304Sjkim    if (!BN_mod_inverse(u2, sig->s, order, ctx)) {
376280304Sjkim        ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
377280304Sjkim        goto err;
378280304Sjkim    }
379280304Sjkim    /* digest -> m */
380280304Sjkim    i = BN_num_bits(order);
381280304Sjkim    /*
382280304Sjkim     * Need to truncate digest if it is too long: first truncate whole bytes.
383280304Sjkim     */
384280304Sjkim    if (8 * dgst_len > i)
385280304Sjkim        dgst_len = (i + 7) / 8;
386280304Sjkim    if (!BN_bin2bn(dgst, dgst_len, m)) {
387280304Sjkim        ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
388280304Sjkim        goto err;
389280304Sjkim    }
390280304Sjkim    /* If still too long truncate remaining bits with a shift */
391280304Sjkim    if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
392280304Sjkim        ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
393280304Sjkim        goto err;
394280304Sjkim    }
395280304Sjkim    /* u1 = m * tmp mod order */
396280304Sjkim    if (!BN_mod_mul(u1, m, u2, order, ctx)) {
397280304Sjkim        ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
398280304Sjkim        goto err;
399280304Sjkim    }
400280304Sjkim    /* u2 = r * w mod q */
401280304Sjkim    if (!BN_mod_mul(u2, sig->r, u2, order, ctx)) {
402280304Sjkim        ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
403280304Sjkim        goto err;
404280304Sjkim    }
405280304Sjkim
406280304Sjkim    if ((point = EC_POINT_new(group)) == NULL) {
407280304Sjkim        ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_MALLOC_FAILURE);
408280304Sjkim        goto err;
409280304Sjkim    }
410280304Sjkim    if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) {
411280304Sjkim        ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
412280304Sjkim        goto err;
413280304Sjkim    }
414280304Sjkim    if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==
415280304Sjkim        NID_X9_62_prime_field) {
416280304Sjkim        if (!EC_POINT_get_affine_coordinates_GFp(group, point, X, NULL, ctx)) {
417280304Sjkim            ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
418280304Sjkim            goto err;
419280304Sjkim        }
420280304Sjkim    }
421238405Sjkim#ifndef OPENSSL_NO_EC2M
422280304Sjkim    else {                      /* NID_X9_62_characteristic_two_field */
423280304Sjkim
424280304Sjkim        if (!EC_POINT_get_affine_coordinates_GF2m(group, point, X, NULL, ctx)) {
425280304Sjkim            ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
426280304Sjkim            goto err;
427280304Sjkim        }
428280304Sjkim    }
429280304Sjkim#endif
430280304Sjkim    if (!BN_nnmod(u1, X, order, ctx)) {
431280304Sjkim        ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
432280304Sjkim        goto err;
433280304Sjkim    }
434280304Sjkim    /*  if the signature is correct u1 is equal to sig->r */
435280304Sjkim    ret = (BN_ucmp(u1, sig->r) == 0);
436280304Sjkim err:
437280304Sjkim    BN_CTX_end(ctx);
438280304Sjkim    BN_CTX_free(ctx);
439280304Sjkim    if (point)
440280304Sjkim        EC_POINT_free(point);
441280304Sjkim    return ret;
442160814Ssimon}
443