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
13296465Sdelphij *    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
64296465Sdelphijstatic ECDSA_SIG *ecdsa_do_sign(const unsigned char *dgst, int dlen,
65296465Sdelphij                                const BIGNUM *, const BIGNUM *,
66296465Sdelphij                                EC_KEY *eckey);
67296465Sdelphijstatic int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
68296465Sdelphij                            BIGNUM **rp);
69296465Sdelphijstatic int ecdsa_do_verify(const unsigned char *dgst, int dgst_len,
70296465Sdelphij                           const ECDSA_SIG *sig, EC_KEY *eckey);
71160814Ssimon
72160814Ssimonstatic ECDSA_METHOD openssl_ecdsa_meth = {
73296465Sdelphij    "OpenSSL ECDSA method",
74296465Sdelphij    ecdsa_do_sign,
75296465Sdelphij    ecdsa_sign_setup,
76296465Sdelphij    ecdsa_do_verify,
77160814Ssimon#if 0
78296465Sdelphij    NULL,                       /* init */
79296465Sdelphij    NULL,                       /* finish */
80160814Ssimon#endif
81296465Sdelphij    0,                          /* flags */
82296465Sdelphij    NULL                        /* app_data */
83160814Ssimon};
84160814Ssimon
85160814Ssimonconst ECDSA_METHOD *ECDSA_OpenSSL(void)
86160814Ssimon{
87296465Sdelphij    return &openssl_ecdsa_meth;
88160814Ssimon}
89160814Ssimon
90160814Ssimonstatic int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
91296465Sdelphij                            BIGNUM **rp)
92160814Ssimon{
93296465Sdelphij    BN_CTX *ctx = NULL;
94296465Sdelphij    BIGNUM *k = NULL, *r = NULL, *order = NULL, *X = NULL;
95296465Sdelphij    EC_POINT *tmp_point = NULL;
96296465Sdelphij    const EC_GROUP *group;
97296465Sdelphij    int ret = 0;
98160814Ssimon
99296465Sdelphij    if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL) {
100296465Sdelphij        ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_PASSED_NULL_PARAMETER);
101296465Sdelphij        return 0;
102296465Sdelphij    }
103160814Ssimon
104296465Sdelphij    if (ctx_in == NULL) {
105296465Sdelphij        if ((ctx = BN_CTX_new()) == NULL) {
106296465Sdelphij            ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);
107296465Sdelphij            return 0;
108296465Sdelphij        }
109296465Sdelphij    } else
110296465Sdelphij        ctx = ctx_in;
111160814Ssimon
112296465Sdelphij    k = BN_new();               /* this value is later returned in *kinvp */
113296465Sdelphij    r = BN_new();               /* this value is later returned in *rp */
114296465Sdelphij    order = BN_new();
115296465Sdelphij    X = BN_new();
116296465Sdelphij    if (!k || !r || !order || !X) {
117296465Sdelphij        ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);
118296465Sdelphij        goto err;
119296465Sdelphij    }
120296465Sdelphij    if ((tmp_point = EC_POINT_new(group)) == NULL) {
121296465Sdelphij        ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
122296465Sdelphij        goto err;
123296465Sdelphij    }
124296465Sdelphij    if (!EC_GROUP_get_order(group, order, ctx)) {
125296465Sdelphij        ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
126296465Sdelphij        goto err;
127296465Sdelphij    }
128160814Ssimon
129296465Sdelphij    do {
130296465Sdelphij        /* get random k */
131296465Sdelphij        do
132296465Sdelphij            if (!BN_rand_range(k, order)) {
133296465Sdelphij                ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP,
134296465Sdelphij                         ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED);
135296465Sdelphij                goto err;
136296465Sdelphij            }
137296465Sdelphij        while (BN_is_zero(k)) ;
138237998Sjkim
139296465Sdelphij        /*
140296465Sdelphij         * We do not want timing information to leak the length of k, so we
141296465Sdelphij         * compute G*k using an equivalent scalar of fixed bit-length.
142296465Sdelphij         */
143237998Sjkim
144296465Sdelphij        if (!BN_add(k, k, order))
145296465Sdelphij            goto err;
146296465Sdelphij        if (BN_num_bits(k) <= BN_num_bits(order))
147296465Sdelphij            if (!BN_add(k, k, order))
148296465Sdelphij                goto err;
149160814Ssimon
150296465Sdelphij        /* compute r the x-coordinate of generator * k */
151296465Sdelphij        if (!EC_POINT_mul(group, tmp_point, k, NULL, NULL, ctx)) {
152296465Sdelphij            ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
153296465Sdelphij            goto err;
154296465Sdelphij        }
155296465Sdelphij        if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==
156296465Sdelphij            NID_X9_62_prime_field) {
157296465Sdelphij            if (!EC_POINT_get_affine_coordinates_GFp
158296465Sdelphij                (group, tmp_point, X, NULL, ctx)) {
159296465Sdelphij                ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
160296465Sdelphij                goto err;
161296465Sdelphij            }
162296465Sdelphij        } else {                /* NID_X9_62_characteristic_two_field */
163296465Sdelphij
164296465Sdelphij            if (!EC_POINT_get_affine_coordinates_GF2m(group,
165296465Sdelphij                                                      tmp_point, X, NULL,
166296465Sdelphij                                                      ctx)) {
167296465Sdelphij                ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
168296465Sdelphij                goto err;
169296465Sdelphij            }
170296465Sdelphij        }
171296465Sdelphij        if (!BN_nnmod(r, X, order, ctx)) {
172296465Sdelphij            ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
173296465Sdelphij            goto err;
174296465Sdelphij        }
175296465Sdelphij    }
176296465Sdelphij    while (BN_is_zero(r));
177296465Sdelphij
178296465Sdelphij    /* compute the inverse of k */
179296465Sdelphij    if (!BN_mod_inverse(k, k, order, ctx)) {
180296465Sdelphij        ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
181296465Sdelphij        goto err;
182296465Sdelphij    }
183296465Sdelphij    /* clear old values if necessary */
184296465Sdelphij    if (*rp != NULL)
185296465Sdelphij        BN_clear_free(*rp);
186296465Sdelphij    if (*kinvp != NULL)
187296465Sdelphij        BN_clear_free(*kinvp);
188296465Sdelphij    /* save the pre-computed values  */
189296465Sdelphij    *rp = r;
190296465Sdelphij    *kinvp = k;
191296465Sdelphij    ret = 1;
192296465Sdelphij err:
193296465Sdelphij    if (!ret) {
194296465Sdelphij        if (k != NULL)
195296465Sdelphij            BN_clear_free(k);
196296465Sdelphij        if (r != NULL)
197296465Sdelphij            BN_clear_free(r);
198296465Sdelphij    }
199296465Sdelphij    if (ctx_in == NULL)
200296465Sdelphij        BN_CTX_free(ctx);
201296465Sdelphij    if (order != NULL)
202296465Sdelphij        BN_free(order);
203296465Sdelphij    if (tmp_point != NULL)
204296465Sdelphij        EC_POINT_free(tmp_point);
205296465Sdelphij    if (X)
206296465Sdelphij        BN_clear_free(X);
207296465Sdelphij    return (ret);
208160814Ssimon}
209160814Ssimon
210296465Sdelphijstatic ECDSA_SIG *ecdsa_do_sign(const unsigned char *dgst, int dgst_len,
211296465Sdelphij                                const BIGNUM *in_kinv, const BIGNUM *in_r,
212296465Sdelphij                                EC_KEY *eckey)
213160814Ssimon{
214296465Sdelphij    int ok = 0, i;
215296465Sdelphij    BIGNUM *kinv = NULL, *s, *m = NULL, *tmp = NULL, *order = NULL;
216296465Sdelphij    const BIGNUM *ckinv;
217296465Sdelphij    BN_CTX *ctx = NULL;
218296465Sdelphij    const EC_GROUP *group;
219296465Sdelphij    ECDSA_SIG *ret;
220296465Sdelphij    ECDSA_DATA *ecdsa;
221296465Sdelphij    const BIGNUM *priv_key;
222160814Ssimon
223296465Sdelphij    ecdsa = ecdsa_check(eckey);
224296465Sdelphij    group = EC_KEY_get0_group(eckey);
225296465Sdelphij    priv_key = EC_KEY_get0_private_key(eckey);
226160814Ssimon
227296465Sdelphij    if (group == NULL || priv_key == NULL || ecdsa == NULL) {
228296465Sdelphij        ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_PASSED_NULL_PARAMETER);
229296465Sdelphij        return NULL;
230296465Sdelphij    }
231160814Ssimon
232296465Sdelphij    ret = ECDSA_SIG_new();
233296465Sdelphij    if (!ret) {
234296465Sdelphij        ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_MALLOC_FAILURE);
235296465Sdelphij        return NULL;
236296465Sdelphij    }
237296465Sdelphij    s = ret->s;
238160814Ssimon
239296465Sdelphij    if ((ctx = BN_CTX_new()) == NULL || (order = BN_new()) == NULL ||
240296465Sdelphij        (tmp = BN_new()) == NULL || (m = BN_new()) == NULL) {
241296465Sdelphij        ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_MALLOC_FAILURE);
242296465Sdelphij        goto err;
243296465Sdelphij    }
244160814Ssimon
245296465Sdelphij    if (!EC_GROUP_get_order(group, order, ctx)) {
246296465Sdelphij        ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_EC_LIB);
247296465Sdelphij        goto err;
248296465Sdelphij    }
249296465Sdelphij    i = BN_num_bits(order);
250296465Sdelphij    /*
251296465Sdelphij     * Need to truncate digest if it is too long: first truncate whole bytes.
252296465Sdelphij     */
253296465Sdelphij    if (8 * dgst_len > i)
254296465Sdelphij        dgst_len = (i + 7) / 8;
255296465Sdelphij    if (!BN_bin2bn(dgst, dgst_len, m)) {
256296465Sdelphij        ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
257296465Sdelphij        goto err;
258296465Sdelphij    }
259296465Sdelphij    /* If still too long truncate remaining bits with a shift */
260296465Sdelphij    if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
261296465Sdelphij        ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
262296465Sdelphij        goto err;
263296465Sdelphij    }
264296465Sdelphij    do {
265296465Sdelphij        if (in_kinv == NULL || in_r == NULL) {
266296465Sdelphij            if (!ECDSA_sign_setup(eckey, ctx, &kinv, &ret->r)) {
267296465Sdelphij                ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_ECDSA_LIB);
268296465Sdelphij                goto err;
269296465Sdelphij            }
270296465Sdelphij            ckinv = kinv;
271296465Sdelphij        } else {
272296465Sdelphij            ckinv = in_kinv;
273296465Sdelphij            if (BN_copy(ret->r, in_r) == NULL) {
274296465Sdelphij                ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_MALLOC_FAILURE);
275296465Sdelphij                goto err;
276296465Sdelphij            }
277296465Sdelphij        }
278160814Ssimon
279296465Sdelphij        if (!BN_mod_mul(tmp, priv_key, ret->r, order, ctx)) {
280296465Sdelphij            ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
281296465Sdelphij            goto err;
282296465Sdelphij        }
283296465Sdelphij        if (!BN_mod_add_quick(s, tmp, m, order)) {
284296465Sdelphij            ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
285296465Sdelphij            goto err;
286296465Sdelphij        }
287296465Sdelphij        if (!BN_mod_mul(s, s, ckinv, order, ctx)) {
288296465Sdelphij            ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
289296465Sdelphij            goto err;
290296465Sdelphij        }
291296465Sdelphij        if (BN_is_zero(s)) {
292296465Sdelphij            /*
293296465Sdelphij             * if kinv and r have been supplied by the caller don't to
294296465Sdelphij             * generate new kinv and r values
295296465Sdelphij             */
296296465Sdelphij            if (in_kinv != NULL && in_r != NULL) {
297296465Sdelphij                ECDSAerr(ECDSA_F_ECDSA_DO_SIGN,
298296465Sdelphij                         ECDSA_R_NEED_NEW_SETUP_VALUES);
299296465Sdelphij                goto err;
300296465Sdelphij            }
301296465Sdelphij        } else
302296465Sdelphij            /* s != 0 => we have a valid signature */
303296465Sdelphij            break;
304296465Sdelphij    }
305296465Sdelphij    while (1);
306296465Sdelphij
307296465Sdelphij    ok = 1;
308296465Sdelphij err:
309296465Sdelphij    if (!ok) {
310296465Sdelphij        ECDSA_SIG_free(ret);
311296465Sdelphij        ret = NULL;
312296465Sdelphij    }
313296465Sdelphij    if (ctx)
314296465Sdelphij        BN_CTX_free(ctx);
315296465Sdelphij    if (m)
316296465Sdelphij        BN_clear_free(m);
317296465Sdelphij    if (tmp)
318296465Sdelphij        BN_clear_free(tmp);
319296465Sdelphij    if (order)
320296465Sdelphij        BN_free(order);
321296465Sdelphij    if (kinv)
322296465Sdelphij        BN_clear_free(kinv);
323296465Sdelphij    return ret;
324160814Ssimon}
325160814Ssimon
326160814Ssimonstatic int ecdsa_do_verify(const unsigned char *dgst, int dgst_len,
327296465Sdelphij                           const ECDSA_SIG *sig, EC_KEY *eckey)
328160814Ssimon{
329296465Sdelphij    int ret = -1, i;
330296465Sdelphij    BN_CTX *ctx;
331296465Sdelphij    BIGNUM *order, *u1, *u2, *m, *X;
332296465Sdelphij    EC_POINT *point = NULL;
333296465Sdelphij    const EC_GROUP *group;
334296465Sdelphij    const EC_POINT *pub_key;
335160814Ssimon
336296465Sdelphij    /* check input values */
337296465Sdelphij    if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL ||
338296465Sdelphij        (pub_key = EC_KEY_get0_public_key(eckey)) == NULL || sig == NULL) {
339296465Sdelphij        ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ECDSA_R_MISSING_PARAMETERS);
340296465Sdelphij        return -1;
341296465Sdelphij    }
342160814Ssimon
343296465Sdelphij    ctx = BN_CTX_new();
344296465Sdelphij    if (!ctx) {
345296465Sdelphij        ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_MALLOC_FAILURE);
346296465Sdelphij        return -1;
347296465Sdelphij    }
348296465Sdelphij    BN_CTX_start(ctx);
349296465Sdelphij    order = BN_CTX_get(ctx);
350296465Sdelphij    u1 = BN_CTX_get(ctx);
351296465Sdelphij    u2 = BN_CTX_get(ctx);
352296465Sdelphij    m = BN_CTX_get(ctx);
353296465Sdelphij    X = BN_CTX_get(ctx);
354296465Sdelphij    if (!X) {
355296465Sdelphij        ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
356296465Sdelphij        goto err;
357296465Sdelphij    }
358160814Ssimon
359296465Sdelphij    if (!EC_GROUP_get_order(group, order, ctx)) {
360296465Sdelphij        ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
361296465Sdelphij        goto err;
362296465Sdelphij    }
363160814Ssimon
364296465Sdelphij    if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||
365296465Sdelphij        BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s) ||
366296465Sdelphij        BN_is_negative(sig->s) || BN_ucmp(sig->s, order) >= 0) {
367296465Sdelphij        ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ECDSA_R_BAD_SIGNATURE);
368296465Sdelphij        ret = 0;                /* signature is invalid */
369296465Sdelphij        goto err;
370296465Sdelphij    }
371296465Sdelphij    /* calculate tmp1 = inv(S) mod order */
372296465Sdelphij    if (!BN_mod_inverse(u2, sig->s, order, ctx)) {
373296465Sdelphij        ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
374296465Sdelphij        goto err;
375296465Sdelphij    }
376296465Sdelphij    /* digest -> m */
377296465Sdelphij    i = BN_num_bits(order);
378296465Sdelphij    /*
379296465Sdelphij     * Need to truncate digest if it is too long: first truncate whole bytes.
380296465Sdelphij     */
381296465Sdelphij    if (8 * dgst_len > i)
382296465Sdelphij        dgst_len = (i + 7) / 8;
383296465Sdelphij    if (!BN_bin2bn(dgst, dgst_len, m)) {
384296465Sdelphij        ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
385296465Sdelphij        goto err;
386296465Sdelphij    }
387296465Sdelphij    /* If still too long truncate remaining bits with a shift */
388296465Sdelphij    if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
389296465Sdelphij        ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
390296465Sdelphij        goto err;
391296465Sdelphij    }
392296465Sdelphij    /* u1 = m * tmp mod order */
393296465Sdelphij    if (!BN_mod_mul(u1, m, u2, order, ctx)) {
394296465Sdelphij        ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
395296465Sdelphij        goto err;
396296465Sdelphij    }
397296465Sdelphij    /* u2 = r * w mod q */
398296465Sdelphij    if (!BN_mod_mul(u2, sig->r, u2, order, ctx)) {
399296465Sdelphij        ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
400296465Sdelphij        goto err;
401296465Sdelphij    }
402296465Sdelphij
403296465Sdelphij    if ((point = EC_POINT_new(group)) == NULL) {
404296465Sdelphij        ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_MALLOC_FAILURE);
405296465Sdelphij        goto err;
406296465Sdelphij    }
407296465Sdelphij    if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) {
408296465Sdelphij        ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
409296465Sdelphij        goto err;
410296465Sdelphij    }
411296465Sdelphij    if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==
412296465Sdelphij        NID_X9_62_prime_field) {
413296465Sdelphij        if (!EC_POINT_get_affine_coordinates_GFp(group, point, X, NULL, ctx)) {
414296465Sdelphij            ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
415296465Sdelphij            goto err;
416296465Sdelphij        }
417296465Sdelphij    } else {                    /* NID_X9_62_characteristic_two_field */
418296465Sdelphij
419296465Sdelphij        if (!EC_POINT_get_affine_coordinates_GF2m(group, point, X, NULL, ctx)) {
420296465Sdelphij            ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
421296465Sdelphij            goto err;
422296465Sdelphij        }
423296465Sdelphij    }
424296465Sdelphij
425296465Sdelphij    if (!BN_nnmod(u1, X, order, ctx)) {
426296465Sdelphij        ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
427296465Sdelphij        goto err;
428296465Sdelphij    }
429296465Sdelphij    /*  if the signature is correct u1 is equal to sig->r */
430296465Sdelphij    ret = (BN_ucmp(u1, sig->r) == 0);
431296465Sdelphij err:
432296465Sdelphij    BN_CTX_end(ctx);
433296465Sdelphij    BN_CTX_free(ctx);
434296465Sdelphij    if (point)
435296465Sdelphij        EC_POINT_free(point);
436296465Sdelphij    return ret;
437160814Ssimon}
438