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
13296341Sdelphij *    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
64296341Sdelphijstatic ECDSA_SIG *ecdsa_do_sign(const unsigned char *dgst, int dlen,
65296341Sdelphij                                const BIGNUM *, const BIGNUM *,
66296341Sdelphij                                EC_KEY *eckey);
67296341Sdelphijstatic int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
68296341Sdelphij                            BIGNUM **rp);
69296341Sdelphijstatic int ecdsa_do_verify(const unsigned char *dgst, int dgst_len,
70296341Sdelphij                           const ECDSA_SIG *sig, EC_KEY *eckey);
71160814Ssimon
72160814Ssimonstatic ECDSA_METHOD openssl_ecdsa_meth = {
73296341Sdelphij    "OpenSSL ECDSA method",
74296341Sdelphij    ecdsa_do_sign,
75296341Sdelphij    ecdsa_sign_setup,
76296341Sdelphij    ecdsa_do_verify,
77160814Ssimon#if 0
78296341Sdelphij    NULL,                       /* init */
79296341Sdelphij    NULL,                       /* finish */
80160814Ssimon#endif
81296341Sdelphij    0,                          /* flags */
82296341Sdelphij    NULL                        /* app_data */
83160814Ssimon};
84160814Ssimon
85160814Ssimonconst ECDSA_METHOD *ECDSA_OpenSSL(void)
86160814Ssimon{
87296341Sdelphij    return &openssl_ecdsa_meth;
88160814Ssimon}
89160814Ssimon
90160814Ssimonstatic int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
91296341Sdelphij                            BIGNUM **rp)
92160814Ssimon{
93296341Sdelphij    BN_CTX *ctx = NULL;
94296341Sdelphij    BIGNUM *k = NULL, *r = NULL, *order = NULL, *X = NULL;
95296341Sdelphij    EC_POINT *tmp_point = NULL;
96296341Sdelphij    const EC_GROUP *group;
97296341Sdelphij    int ret = 0;
98160814Ssimon
99296341Sdelphij    if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL) {
100296341Sdelphij        ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_PASSED_NULL_PARAMETER);
101296341Sdelphij        return 0;
102296341Sdelphij    }
103160814Ssimon
104296341Sdelphij    if (ctx_in == NULL) {
105296341Sdelphij        if ((ctx = BN_CTX_new()) == NULL) {
106296341Sdelphij            ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);
107296341Sdelphij            return 0;
108296341Sdelphij        }
109296341Sdelphij    } else
110296341Sdelphij        ctx = ctx_in;
111160814Ssimon
112296341Sdelphij    k = BN_new();               /* this value is later returned in *kinvp */
113296341Sdelphij    r = BN_new();               /* this value is later returned in *rp */
114296341Sdelphij    order = BN_new();
115296341Sdelphij    X = BN_new();
116296341Sdelphij    if (!k || !r || !order || !X) {
117296341Sdelphij        ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);
118296341Sdelphij        goto err;
119296341Sdelphij    }
120296341Sdelphij    if ((tmp_point = EC_POINT_new(group)) == NULL) {
121296341Sdelphij        ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
122296341Sdelphij        goto err;
123296341Sdelphij    }
124296341Sdelphij    if (!EC_GROUP_get_order(group, order, ctx)) {
125296341Sdelphij        ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
126296341Sdelphij        goto err;
127296341Sdelphij    }
128160814Ssimon
129296341Sdelphij    do {
130296341Sdelphij        /* get random k */
131296341Sdelphij        do
132296341Sdelphij            if (!BN_rand_range(k, order)) {
133296341Sdelphij                ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP,
134296341Sdelphij                         ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED);
135296341Sdelphij                goto err;
136296341Sdelphij            }
137296341Sdelphij        while (BN_is_zero(k)) ;
138237657Sjkim
139296341Sdelphij        /*
140296341Sdelphij         * We do not want timing information to leak the length of k, so we
141296341Sdelphij         * compute G*k using an equivalent scalar of fixed bit-length.
142296341Sdelphij         */
143237657Sjkim
144296341Sdelphij        if (!BN_add(k, k, order))
145296341Sdelphij            goto err;
146296341Sdelphij        if (BN_num_bits(k) <= BN_num_bits(order))
147296341Sdelphij            if (!BN_add(k, k, order))
148296341Sdelphij                goto err;
149296341Sdelphij
150296341Sdelphij        /* compute r the x-coordinate of generator * k */
151296341Sdelphij        if (!EC_POINT_mul(group, tmp_point, k, NULL, NULL, ctx)) {
152296341Sdelphij            ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
153296341Sdelphij            goto err;
154296341Sdelphij        }
155296341Sdelphij        if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==
156296341Sdelphij            NID_X9_62_prime_field) {
157296341Sdelphij            if (!EC_POINT_get_affine_coordinates_GFp
158296341Sdelphij                (group, tmp_point, X, NULL, ctx)) {
159296341Sdelphij                ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
160296341Sdelphij                goto err;
161296341Sdelphij            }
162296341Sdelphij        }
163238405Sjkim#ifndef OPENSSL_NO_EC2M
164296341Sdelphij        else {                  /* NID_X9_62_characteristic_two_field */
165296341Sdelphij
166296341Sdelphij            if (!EC_POINT_get_affine_coordinates_GF2m(group,
167296341Sdelphij                                                      tmp_point, X, NULL,
168296341Sdelphij                                                      ctx)) {
169296341Sdelphij                ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
170296341Sdelphij                goto err;
171296341Sdelphij            }
172296341Sdelphij        }
173238405Sjkim#endif
174296341Sdelphij        if (!BN_nnmod(r, X, order, ctx)) {
175296341Sdelphij            ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
176296341Sdelphij            goto err;
177296341Sdelphij        }
178296341Sdelphij    }
179296341Sdelphij    while (BN_is_zero(r));
180160814Ssimon
181296341Sdelphij    /* compute the inverse of k */
182296341Sdelphij    if (!BN_mod_inverse(k, k, order, ctx)) {
183296341Sdelphij        ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
184296341Sdelphij        goto err;
185296341Sdelphij    }
186296341Sdelphij    /* clear old values if necessary */
187296341Sdelphij    if (*rp != NULL)
188296341Sdelphij        BN_clear_free(*rp);
189296341Sdelphij    if (*kinvp != NULL)
190296341Sdelphij        BN_clear_free(*kinvp);
191296341Sdelphij    /* save the pre-computed values  */
192296341Sdelphij    *rp = r;
193296341Sdelphij    *kinvp = k;
194296341Sdelphij    ret = 1;
195296341Sdelphij err:
196296341Sdelphij    if (!ret) {
197296341Sdelphij        if (k != NULL)
198296341Sdelphij            BN_clear_free(k);
199296341Sdelphij        if (r != NULL)
200296341Sdelphij            BN_clear_free(r);
201296341Sdelphij    }
202296341Sdelphij    if (ctx_in == NULL)
203296341Sdelphij        BN_CTX_free(ctx);
204296341Sdelphij    if (order != NULL)
205296341Sdelphij        BN_free(order);
206296341Sdelphij    if (tmp_point != NULL)
207296341Sdelphij        EC_POINT_free(tmp_point);
208296341Sdelphij    if (X)
209296341Sdelphij        BN_clear_free(X);
210296341Sdelphij    return (ret);
211160814Ssimon}
212160814Ssimon
213296341Sdelphijstatic ECDSA_SIG *ecdsa_do_sign(const unsigned char *dgst, int dgst_len,
214296341Sdelphij                                const BIGNUM *in_kinv, const BIGNUM *in_r,
215296341Sdelphij                                EC_KEY *eckey)
216160814Ssimon{
217296341Sdelphij    int ok = 0, i;
218296341Sdelphij    BIGNUM *kinv = NULL, *s, *m = NULL, *tmp = NULL, *order = NULL;
219296341Sdelphij    const BIGNUM *ckinv;
220296341Sdelphij    BN_CTX *ctx = NULL;
221296341Sdelphij    const EC_GROUP *group;
222296341Sdelphij    ECDSA_SIG *ret;
223296341Sdelphij    ECDSA_DATA *ecdsa;
224296341Sdelphij    const BIGNUM *priv_key;
225160814Ssimon
226296341Sdelphij    ecdsa = ecdsa_check(eckey);
227296341Sdelphij    group = EC_KEY_get0_group(eckey);
228296341Sdelphij    priv_key = EC_KEY_get0_private_key(eckey);
229160814Ssimon
230296341Sdelphij    if (group == NULL || priv_key == NULL || ecdsa == NULL) {
231296341Sdelphij        ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_PASSED_NULL_PARAMETER);
232296341Sdelphij        return NULL;
233296341Sdelphij    }
234160814Ssimon
235296341Sdelphij    ret = ECDSA_SIG_new();
236296341Sdelphij    if (!ret) {
237296341Sdelphij        ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_MALLOC_FAILURE);
238296341Sdelphij        return NULL;
239296341Sdelphij    }
240296341Sdelphij    s = ret->s;
241160814Ssimon
242296341Sdelphij    if ((ctx = BN_CTX_new()) == NULL || (order = BN_new()) == NULL ||
243296341Sdelphij        (tmp = BN_new()) == NULL || (m = BN_new()) == NULL) {
244296341Sdelphij        ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_MALLOC_FAILURE);
245296341Sdelphij        goto err;
246296341Sdelphij    }
247160814Ssimon
248296341Sdelphij    if (!EC_GROUP_get_order(group, order, ctx)) {
249296341Sdelphij        ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_EC_LIB);
250296341Sdelphij        goto err;
251296341Sdelphij    }
252296341Sdelphij    i = BN_num_bits(order);
253296341Sdelphij    /*
254296341Sdelphij     * Need to truncate digest if it is too long: first truncate whole bytes.
255296341Sdelphij     */
256296341Sdelphij    if (8 * dgst_len > i)
257296341Sdelphij        dgst_len = (i + 7) / 8;
258296341Sdelphij    if (!BN_bin2bn(dgst, dgst_len, m)) {
259296341Sdelphij        ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
260296341Sdelphij        goto err;
261296341Sdelphij    }
262296341Sdelphij    /* If still too long truncate remaining bits with a shift */
263296341Sdelphij    if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
264296341Sdelphij        ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
265296341Sdelphij        goto err;
266296341Sdelphij    }
267296341Sdelphij    do {
268296341Sdelphij        if (in_kinv == NULL || in_r == NULL) {
269296341Sdelphij            if (!ECDSA_sign_setup(eckey, ctx, &kinv, &ret->r)) {
270296341Sdelphij                ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_ECDSA_LIB);
271296341Sdelphij                goto err;
272296341Sdelphij            }
273296341Sdelphij            ckinv = kinv;
274296341Sdelphij        } else {
275296341Sdelphij            ckinv = in_kinv;
276296341Sdelphij            if (BN_copy(ret->r, in_r) == NULL) {
277296341Sdelphij                ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_MALLOC_FAILURE);
278296341Sdelphij                goto err;
279296341Sdelphij            }
280296341Sdelphij        }
281160814Ssimon
282296341Sdelphij        if (!BN_mod_mul(tmp, priv_key, ret->r, order, ctx)) {
283296341Sdelphij            ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
284296341Sdelphij            goto err;
285296341Sdelphij        }
286296341Sdelphij        if (!BN_mod_add_quick(s, tmp, m, order)) {
287296341Sdelphij            ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
288296341Sdelphij            goto err;
289296341Sdelphij        }
290296341Sdelphij        if (!BN_mod_mul(s, s, ckinv, order, ctx)) {
291296341Sdelphij            ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
292296341Sdelphij            goto err;
293296341Sdelphij        }
294296341Sdelphij        if (BN_is_zero(s)) {
295296341Sdelphij            /*
296296341Sdelphij             * if kinv and r have been supplied by the caller don't to
297296341Sdelphij             * generate new kinv and r values
298296341Sdelphij             */
299296341Sdelphij            if (in_kinv != NULL && in_r != NULL) {
300296341Sdelphij                ECDSAerr(ECDSA_F_ECDSA_DO_SIGN,
301296341Sdelphij                         ECDSA_R_NEED_NEW_SETUP_VALUES);
302296341Sdelphij                goto err;
303296341Sdelphij            }
304296341Sdelphij        } else
305296341Sdelphij            /* s != 0 => we have a valid signature */
306296341Sdelphij            break;
307296341Sdelphij    }
308296341Sdelphij    while (1);
309296341Sdelphij
310296341Sdelphij    ok = 1;
311296341Sdelphij err:
312296341Sdelphij    if (!ok) {
313296341Sdelphij        ECDSA_SIG_free(ret);
314296341Sdelphij        ret = NULL;
315296341Sdelphij    }
316296341Sdelphij    if (ctx)
317296341Sdelphij        BN_CTX_free(ctx);
318296341Sdelphij    if (m)
319296341Sdelphij        BN_clear_free(m);
320296341Sdelphij    if (tmp)
321296341Sdelphij        BN_clear_free(tmp);
322296341Sdelphij    if (order)
323296341Sdelphij        BN_free(order);
324296341Sdelphij    if (kinv)
325296341Sdelphij        BN_clear_free(kinv);
326296341Sdelphij    return ret;
327160814Ssimon}
328160814Ssimon
329160814Ssimonstatic int ecdsa_do_verify(const unsigned char *dgst, int dgst_len,
330296341Sdelphij                           const ECDSA_SIG *sig, EC_KEY *eckey)
331160814Ssimon{
332296341Sdelphij    int ret = -1, i;
333296341Sdelphij    BN_CTX *ctx;
334296341Sdelphij    BIGNUM *order, *u1, *u2, *m, *X;
335296341Sdelphij    EC_POINT *point = NULL;
336296341Sdelphij    const EC_GROUP *group;
337296341Sdelphij    const EC_POINT *pub_key;
338160814Ssimon
339296341Sdelphij    /* check input values */
340296341Sdelphij    if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL ||
341296341Sdelphij        (pub_key = EC_KEY_get0_public_key(eckey)) == NULL || sig == NULL) {
342296341Sdelphij        ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ECDSA_R_MISSING_PARAMETERS);
343296341Sdelphij        return -1;
344296341Sdelphij    }
345160814Ssimon
346296341Sdelphij    ctx = BN_CTX_new();
347296341Sdelphij    if (!ctx) {
348296341Sdelphij        ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_MALLOC_FAILURE);
349296341Sdelphij        return -1;
350296341Sdelphij    }
351296341Sdelphij    BN_CTX_start(ctx);
352296341Sdelphij    order = BN_CTX_get(ctx);
353296341Sdelphij    u1 = BN_CTX_get(ctx);
354296341Sdelphij    u2 = BN_CTX_get(ctx);
355296341Sdelphij    m = BN_CTX_get(ctx);
356296341Sdelphij    X = BN_CTX_get(ctx);
357296341Sdelphij    if (!X) {
358296341Sdelphij        ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
359296341Sdelphij        goto err;
360296341Sdelphij    }
361160814Ssimon
362296341Sdelphij    if (!EC_GROUP_get_order(group, order, ctx)) {
363296341Sdelphij        ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
364296341Sdelphij        goto err;
365296341Sdelphij    }
366160814Ssimon
367296341Sdelphij    if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||
368296341Sdelphij        BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s) ||
369296341Sdelphij        BN_is_negative(sig->s) || BN_ucmp(sig->s, order) >= 0) {
370296341Sdelphij        ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ECDSA_R_BAD_SIGNATURE);
371296341Sdelphij        ret = 0;                /* signature is invalid */
372296341Sdelphij        goto err;
373296341Sdelphij    }
374296341Sdelphij    /* calculate tmp1 = inv(S) mod order */
375296341Sdelphij    if (!BN_mod_inverse(u2, sig->s, order, ctx)) {
376296341Sdelphij        ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
377296341Sdelphij        goto err;
378296341Sdelphij    }
379296341Sdelphij    /* digest -> m */
380296341Sdelphij    i = BN_num_bits(order);
381296341Sdelphij    /*
382296341Sdelphij     * Need to truncate digest if it is too long: first truncate whole bytes.
383296341Sdelphij     */
384296341Sdelphij    if (8 * dgst_len > i)
385296341Sdelphij        dgst_len = (i + 7) / 8;
386296341Sdelphij    if (!BN_bin2bn(dgst, dgst_len, m)) {
387296341Sdelphij        ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
388296341Sdelphij        goto err;
389296341Sdelphij    }
390296341Sdelphij    /* If still too long truncate remaining bits with a shift */
391296341Sdelphij    if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
392296341Sdelphij        ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
393296341Sdelphij        goto err;
394296341Sdelphij    }
395296341Sdelphij    /* u1 = m * tmp mod order */
396296341Sdelphij    if (!BN_mod_mul(u1, m, u2, order, ctx)) {
397296341Sdelphij        ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
398296341Sdelphij        goto err;
399296341Sdelphij    }
400296341Sdelphij    /* u2 = r * w mod q */
401296341Sdelphij    if (!BN_mod_mul(u2, sig->r, u2, order, ctx)) {
402296341Sdelphij        ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
403296341Sdelphij        goto err;
404296341Sdelphij    }
405296341Sdelphij
406296341Sdelphij    if ((point = EC_POINT_new(group)) == NULL) {
407296341Sdelphij        ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_MALLOC_FAILURE);
408296341Sdelphij        goto err;
409296341Sdelphij    }
410296341Sdelphij    if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) {
411296341Sdelphij        ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
412296341Sdelphij        goto err;
413296341Sdelphij    }
414296341Sdelphij    if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==
415296341Sdelphij        NID_X9_62_prime_field) {
416296341Sdelphij        if (!EC_POINT_get_affine_coordinates_GFp(group, point, X, NULL, ctx)) {
417296341Sdelphij            ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
418296341Sdelphij            goto err;
419296341Sdelphij        }
420296341Sdelphij    }
421238405Sjkim#ifndef OPENSSL_NO_EC2M
422296341Sdelphij    else {                      /* NID_X9_62_characteristic_two_field */
423296341Sdelphij
424296341Sdelphij        if (!EC_POINT_get_affine_coordinates_GF2m(group, point, X, NULL, ctx)) {
425296341Sdelphij            ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
426296341Sdelphij            goto err;
427296341Sdelphij        }
428296341Sdelphij    }
429296341Sdelphij#endif
430296341Sdelphij    if (!BN_nnmod(u1, X, order, ctx)) {
431296341Sdelphij        ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
432296341Sdelphij        goto err;
433296341Sdelphij    }
434296341Sdelphij    /*  if the signature is correct u1 is equal to sig->r */
435296341Sdelphij    ret = (BN_ucmp(u1, sig->r) == 0);
436296341Sdelphij err:
437296341Sdelphij    BN_CTX_end(ctx);
438296341Sdelphij    BN_CTX_free(ctx);
439296341Sdelphij    if (point)
440296341Sdelphij        EC_POINT_free(point);
441296341Sdelphij    return ret;
442160814Ssimon}
443