Coverage Report

Created: 2025-06-13 06:58

/src/openssl32/crypto/ec/ecdsa_ossl.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2002-2024 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
/*
11
 * ECDSA low level APIs are deprecated for public use, but still ok for
12
 * internal use.
13
 */
14
#include "internal/deprecated.h"
15
16
#include <string.h>
17
#include <openssl/err.h>
18
#include <openssl/obj_mac.h>
19
#include <openssl/rand.h>
20
#include "crypto/bn.h"
21
#include "ec_local.h"
22
#include "internal/deterministic_nonce.h"
23
24
3.52k
#define MIN_ECDSA_SIGN_ORDERBITS 64
25
/*
26
 * It is highly unlikely that a retry will happen,
27
 * Multiple retries would indicate that something is wrong
28
 * with the group parameters (which would normally only happen
29
 * with a bad custom group).
30
 */
31
0
#define MAX_ECDSA_SIGN_RETRIES 8
32
33
static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in,
34
                            BIGNUM **kinvp, BIGNUM **rp,
35
                            const unsigned char *dgst, int dlen,
36
                            unsigned int nonce_type, const char *digestname,
37
                            OSSL_LIB_CTX *libctx, const char *propq);
38
39
int ossl_ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
40
                          BIGNUM **rp)
41
0
{
42
0
    if (eckey->group->meth->ecdsa_sign_setup == NULL) {
43
0
        ERR_raise(ERR_LIB_EC, EC_R_CURVE_DOES_NOT_SUPPORT_ECDSA);
44
0
        return 0;
45
0
    }
46
47
0
    return eckey->group->meth->ecdsa_sign_setup(eckey, ctx_in, kinvp, rp);
48
0
}
49
50
ECDSA_SIG *ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len,
51
                               const BIGNUM *in_kinv, const BIGNUM *in_r,
52
                               EC_KEY *eckey)
53
2.93k
{
54
2.93k
    if (eckey->group->meth->ecdsa_sign_sig == NULL) {
55
0
        ERR_raise(ERR_LIB_EC, EC_R_CURVE_DOES_NOT_SUPPORT_ECDSA);
56
0
        return NULL;
57
0
    }
58
59
2.93k
    return eckey->group->meth->ecdsa_sign_sig(dgst, dgst_len,
60
2.93k
                                              in_kinv, in_r, eckey);
61
2.93k
}
62
63
int ossl_ecdsa_verify_sig(const unsigned char *dgst, int dgst_len,
64
                          const ECDSA_SIG *sig, EC_KEY *eckey)
65
586
{
66
586
    if (eckey->group->meth->ecdsa_verify_sig == NULL) {
67
0
        ERR_raise(ERR_LIB_EC, EC_R_CURVE_DOES_NOT_SUPPORT_ECDSA);
68
0
        return 0;
69
0
    }
70
71
586
    return eckey->group->meth->ecdsa_verify_sig(dgst, dgst_len, sig, eckey);
72
586
}
73
74
int ossl_ecdsa_sign(int type, const unsigned char *dgst, int dlen,
75
                    unsigned char *sig, unsigned int *siglen,
76
                    const BIGNUM *kinv, const BIGNUM *r, EC_KEY *eckey)
77
2.93k
{
78
2.93k
    ECDSA_SIG *s;
79
80
2.93k
    if (sig == NULL && (kinv == NULL || r == NULL)) {
81
0
        *siglen = ECDSA_size(eckey);
82
0
        return 1;
83
0
    }
84
85
2.93k
    s = ECDSA_do_sign_ex(dgst, dlen, kinv, r, eckey);
86
2.93k
    if (s == NULL) {
87
0
        *siglen = 0;
88
0
        return 0;
89
0
    }
90
2.93k
    *siglen = i2d_ECDSA_SIG(s, sig != NULL ? &sig : NULL);
91
2.93k
    ECDSA_SIG_free(s);
92
2.93k
    return 1;
93
2.93k
}
94
95
int ossl_ecdsa_deterministic_sign(const unsigned char *dgst, int dlen,
96
                                  unsigned char *sig, unsigned int *siglen,
97
                                  EC_KEY *eckey, unsigned int nonce_type,
98
                                  const char *digestname,
99
                                  OSSL_LIB_CTX *libctx, const char *propq)
100
0
{
101
0
    ECDSA_SIG *s;
102
0
    BIGNUM *kinv = NULL, *r = NULL;
103
0
    int ret = 0;
104
105
0
    if (sig == NULL) {
106
0
        ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
107
0
        return 0;
108
0
    }
109
0
    if (digestname == NULL) {
110
0
        ERR_raise(ERR_LIB_EC, EC_R_INVALID_DIGEST);
111
0
        return 0;
112
0
    }
113
114
0
    *siglen = 0;
115
0
    if (!ecdsa_sign_setup(eckey, NULL, &kinv, &r, dgst, dlen,
116
0
                          nonce_type, digestname, libctx, propq))
117
0
        return 0;
118
119
0
    s = ECDSA_do_sign_ex(dgst, dlen, kinv, r, eckey);
120
0
    if (s == NULL)
121
0
        goto end;
122
123
0
    *siglen = i2d_ECDSA_SIG(s, &sig);
124
0
    ECDSA_SIG_free(s);
125
0
    ret = 1;
126
0
end:
127
0
    BN_clear_free(kinv);
128
0
    BN_clear_free(r);
129
0
    return ret;
130
0
}
131
132
static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in,
133
                            BIGNUM **kinvp, BIGNUM **rp,
134
                            const unsigned char *dgst, int dlen,
135
                            unsigned int nonce_type, const char *digestname,
136
                            OSSL_LIB_CTX *libctx, const char *propq)
137
1.76k
{
138
1.76k
    BN_CTX *ctx = NULL;
139
1.76k
    BIGNUM *k = NULL, *r = NULL, *X = NULL;
140
1.76k
    const BIGNUM *order;
141
1.76k
    EC_POINT *tmp_point = NULL;
142
1.76k
    const EC_GROUP *group;
143
1.76k
    int ret = 0;
144
1.76k
    int order_bits;
145
1.76k
    const BIGNUM *priv_key;
146
147
1.76k
    if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL) {
148
0
        ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
149
0
        return 0;
150
0
    }
151
1.76k
    if ((priv_key = EC_KEY_get0_private_key(eckey)) == NULL) {
152
0
        ERR_raise(ERR_LIB_EC, EC_R_MISSING_PRIVATE_KEY);
153
0
        return 0;
154
0
    }
155
156
1.76k
    if (!EC_KEY_can_sign(eckey)) {
157
0
        ERR_raise(ERR_LIB_EC, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
158
0
        return 0;
159
0
    }
160
161
1.76k
    if ((ctx = ctx_in) == NULL) {
162
0
        if ((ctx = BN_CTX_new_ex(eckey->libctx)) == NULL) {
163
0
            ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
164
0
            return 0;
165
0
        }
166
0
    }
167
168
1.76k
    k = BN_secure_new();        /* this value is later returned in *kinvp */
169
1.76k
    r = BN_new();               /* this value is later returned in *rp */
170
1.76k
    X = BN_new();
171
1.76k
    if (k == NULL || r == NULL || X == NULL) {
172
0
        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
173
0
        goto err;
174
0
    }
175
1.76k
    if ((tmp_point = EC_POINT_new(group)) == NULL) {
176
0
        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
177
0
        goto err;
178
0
    }
179
180
1.76k
    if ((order = EC_GROUP_get0_order(group)) == NULL) {
181
0
        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
182
0
        goto err;
183
0
    }
184
185
    /* Preallocate space */
186
1.76k
    order_bits = BN_num_bits(order);
187
    /* Check the number of bits here so that an infinite loop is not possible */
188
1.76k
    if (order_bits < MIN_ECDSA_SIGN_ORDERBITS
189
1.76k
        || !BN_set_bit(k, order_bits)
190
1.76k
        || !BN_set_bit(r, order_bits)
191
1.76k
        || !BN_set_bit(X, order_bits))
192
0
        goto err;
193
194
1.76k
    do {
195
        /* get random or deterministic value of k */
196
1.76k
        do {
197
1.76k
            int res = 0;
198
199
1.76k
            if (dgst != NULL) {
200
1.76k
                if (nonce_type == 1) {
201
0
#ifndef FIPS_MODULE
202
0
                    res = ossl_gen_deterministic_nonce_rfc6979(k, order,
203
0
                                                               priv_key,
204
0
                                                               dgst, dlen,
205
0
                                                               digestname,
206
0
                                                               libctx, propq);
207
0
#endif
208
1.76k
                } else {
209
1.76k
                    res = ossl_bn_gen_dsa_nonce_fixed_top(k, order, priv_key,
210
1.76k
                                                          dgst, dlen, ctx);
211
1.76k
                }
212
1.76k
            } else {
213
0
                res = ossl_bn_priv_rand_range_fixed_top(k, order, 0, ctx);
214
0
            }
215
1.76k
            if (!res) {
216
0
                ERR_raise(ERR_LIB_EC, EC_R_RANDOM_NUMBER_GENERATION_FAILED);
217
0
                goto err;
218
0
            }
219
1.76k
        } while (ossl_bn_is_word_fixed_top(k, 0));
220
221
        /* compute r the x-coordinate of generator * k */
222
1.76k
        if (!EC_POINT_mul(group, tmp_point, k, NULL, NULL, ctx)) {
223
0
            ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
224
0
            goto err;
225
0
        }
226
227
1.76k
        if (!EC_POINT_get_affine_coordinates(group, tmp_point, X, NULL, ctx)) {
228
0
            ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
229
0
            goto err;
230
0
        }
231
232
1.76k
        if (!BN_nnmod(r, X, order, ctx)) {
233
0
            ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
234
0
            goto err;
235
0
        }
236
1.76k
    } while (BN_is_zero(r));
237
238
    /* compute the inverse of k */
239
1.76k
    if (!ossl_ec_group_do_inverse_ord(group, k, k, ctx)) {
240
0
        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
241
0
        goto err;
242
0
    }
243
244
    /* clear old values if necessary */
245
1.76k
    BN_clear_free(*rp);
246
1.76k
    BN_clear_free(*kinvp);
247
    /* save the pre-computed values  */
248
1.76k
    *rp = r;
249
1.76k
    *kinvp = k;
250
1.76k
    ret = 1;
251
1.76k
 err:
252
1.76k
    if (!ret) {
253
0
        BN_clear_free(k);
254
0
        BN_clear_free(r);
255
0
    }
256
1.76k
    if (ctx != ctx_in)
257
0
        BN_CTX_free(ctx);
258
1.76k
    EC_POINT_free(tmp_point);
259
1.76k
    BN_clear_free(X);
260
1.76k
    return ret;
261
1.76k
}
262
263
int ossl_ecdsa_simple_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
264
                                 BIGNUM **rp)
265
0
{
266
0
    return ecdsa_sign_setup(eckey, ctx_in, kinvp, rp, NULL, 0,
267
0
                            0, NULL, NULL, NULL);
268
0
}
269
270
ECDSA_SIG *ossl_ecdsa_simple_sign_sig(const unsigned char *dgst, int dgst_len,
271
                                      const BIGNUM *in_kinv, const BIGNUM *in_r,
272
                                      EC_KEY *eckey)
273
2.93k
{
274
2.93k
    int ok = 0, i;
275
2.93k
    int retries = 0;
276
2.93k
    BIGNUM *kinv = NULL, *s, *m = NULL;
277
2.93k
    const BIGNUM *order, *ckinv;
278
2.93k
    BN_CTX *ctx = NULL;
279
2.93k
    const EC_GROUP *group;
280
2.93k
    ECDSA_SIG *ret;
281
2.93k
    const BIGNUM *priv_key;
282
283
2.93k
    group = EC_KEY_get0_group(eckey);
284
2.93k
    priv_key = EC_KEY_get0_private_key(eckey);
285
286
2.93k
    if (group == NULL) {
287
0
        ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
288
0
        return NULL;
289
0
    }
290
2.93k
    if (priv_key == NULL) {
291
0
        ERR_raise(ERR_LIB_EC, EC_R_MISSING_PRIVATE_KEY);
292
0
        return NULL;
293
0
    }
294
295
2.93k
    if (!EC_KEY_can_sign(eckey)) {
296
0
        ERR_raise(ERR_LIB_EC, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
297
0
        return NULL;
298
0
    }
299
300
2.93k
    ret = ECDSA_SIG_new();
301
2.93k
    if (ret == NULL) {
302
0
        ERR_raise(ERR_LIB_EC, ERR_R_ECDSA_LIB);
303
0
        return NULL;
304
0
    }
305
2.93k
    ret->r = BN_new();
306
2.93k
    ret->s = BN_new();
307
2.93k
    if (ret->r == NULL || ret->s == NULL) {
308
0
        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
309
0
        goto err;
310
0
    }
311
2.93k
    s = ret->s;
312
313
2.93k
    if ((ctx = BN_CTX_new_ex(eckey->libctx)) == NULL
314
2.93k
        || (m = BN_new()) == NULL) {
315
0
        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
316
0
        goto err;
317
0
    }
318
319
2.93k
    if ((order = EC_GROUP_get0_order(group)) == NULL) {
320
0
        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
321
0
        goto err;
322
0
    }
323
324
2.93k
    i = BN_num_bits(order);
325
    /*
326
     * Need to truncate digest if it is too long: first truncate whole bytes.
327
     */
328
2.93k
    if (8 * dgst_len > i)
329
150
        dgst_len = (i + 7) / 8;
330
2.93k
    if (!BN_bin2bn(dgst, dgst_len, m)) {
331
0
        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
332
0
        goto err;
333
0
    }
334
    /* If still too long, truncate remaining bits with a shift */
335
2.93k
    if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
336
0
        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
337
0
        goto err;
338
0
    }
339
2.93k
    do {
340
2.93k
        if (in_kinv == NULL || in_r == NULL) {
341
2.93k
            if (!ecdsa_sign_setup(eckey, ctx, &kinv, &ret->r, dgst, dgst_len,
342
2.93k
                                  0, NULL, NULL, NULL)) {
343
0
                ERR_raise(ERR_LIB_EC, ERR_R_ECDSA_LIB);
344
0
                goto err;
345
0
            }
346
2.93k
            ckinv = kinv;
347
2.93k
        } else {
348
0
            ckinv = in_kinv;
349
0
            if (BN_copy(ret->r, in_r) == NULL) {
350
0
                ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
351
0
                goto err;
352
0
            }
353
0
        }
354
355
        /*
356
         * With only one multiplicant being in Montgomery domain
357
         * multiplication yields real result without post-conversion.
358
         * Also note that all operations but last are performed with
359
         * zero-padded vectors. Last operation, BN_mod_mul_montgomery
360
         * below, returns user-visible value with removed zero padding.
361
         */
362
2.93k
        if (!bn_to_mont_fixed_top(s, ret->r, group->mont_data, ctx)
363
2.93k
            || !bn_mul_mont_fixed_top(s, s, priv_key, group->mont_data, ctx)) {
364
0
            ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
365
0
            goto err;
366
0
        }
367
2.93k
        if (!bn_mod_add_fixed_top(s, s, m, order)) {
368
0
            ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
369
0
            goto err;
370
0
        }
371
        /*
372
         * |s| can still be larger than modulus, because |m| can be. In
373
         * such case we count on Montgomery reduction to tie it up.
374
         */
375
2.93k
        if (!bn_to_mont_fixed_top(s, s, group->mont_data, ctx)
376
2.93k
            || !BN_mod_mul_montgomery(s, s, ckinv, group->mont_data, ctx)) {
377
0
            ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
378
0
            goto err;
379
0
        }
380
381
2.93k
        if (BN_is_zero(s)) {
382
            /*
383
             * if kinv and r have been supplied by the caller, don't
384
             * generate new kinv and r values
385
             */
386
0
            if (in_kinv != NULL && in_r != NULL) {
387
0
                ERR_raise(ERR_LIB_EC, EC_R_NEED_NEW_SETUP_VALUES);
388
0
                goto err;
389
0
            }
390
            /* Avoid infinite loops cause by invalid group parameters */
391
0
            if (retries++ > MAX_ECDSA_SIGN_RETRIES) {
392
0
                ERR_raise(ERR_LIB_EC, EC_R_TOO_MANY_RETRIES);
393
0
                goto err;
394
0
            }
395
2.93k
        } else {
396
            /* s != 0 => we have a valid signature */
397
2.93k
            break;
398
2.93k
        }
399
2.93k
    } while (1);
400
401
2.93k
    ok = 1;
402
2.93k
 err:
403
2.93k
    if (!ok) {
404
0
        ECDSA_SIG_free(ret);
405
0
        ret = NULL;
406
0
    }
407
2.93k
    BN_CTX_free(ctx);
408
2.93k
    BN_clear_free(m);
409
2.93k
    BN_clear_free(kinv);
410
2.93k
    return ret;
411
2.93k
}
412
413
/*-
414
 * returns
415
 *      1: correct signature
416
 *      0: incorrect signature
417
 *     -1: error
418
 */
419
int ossl_ecdsa_verify(int type, const unsigned char *dgst, int dgst_len,
420
                      const unsigned char *sigbuf, int sig_len, EC_KEY *eckey)
421
1.28k
{
422
1.28k
    ECDSA_SIG *s;
423
1.28k
    const unsigned char *p = sigbuf;
424
1.28k
    unsigned char *der = NULL;
425
1.28k
    int derlen = -1;
426
1.28k
    int ret = -1;
427
428
1.28k
    s = ECDSA_SIG_new();
429
1.28k
    if (s == NULL)
430
0
        return ret;
431
1.28k
    if (d2i_ECDSA_SIG(&s, &p, sig_len) == NULL)
432
660
        goto err;
433
    /* Ensure signature uses DER and doesn't have trailing garbage */
434
623
    derlen = i2d_ECDSA_SIG(s, &der);
435
623
    if (derlen != sig_len || memcmp(sigbuf, der, derlen) != 0)
436
37
        goto err;
437
586
    ret = ECDSA_do_verify(dgst, dgst_len, s, eckey);
438
1.28k
 err:
439
1.28k
    OPENSSL_free(der);
440
1.28k
    ECDSA_SIG_free(s);
441
1.28k
    return ret;
442
586
}
443
444
int ossl_ecdsa_simple_verify_sig(const unsigned char *dgst, int dgst_len,
445
                                 const ECDSA_SIG *sig, EC_KEY *eckey)
446
586
{
447
586
    int ret = -1, i;
448
586
    BN_CTX *ctx;
449
586
    const BIGNUM *order;
450
586
    BIGNUM *u1, *u2, *m, *X;
451
586
    EC_POINT *point = NULL;
452
586
    const EC_GROUP *group;
453
586
    const EC_POINT *pub_key;
454
455
    /* check input values */
456
586
    if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL ||
457
586
        (pub_key = EC_KEY_get0_public_key(eckey)) == NULL || sig == NULL) {
458
0
        ERR_raise(ERR_LIB_EC, EC_R_MISSING_PARAMETERS);
459
0
        return -1;
460
0
    }
461
462
586
    if (!EC_KEY_can_sign(eckey)) {
463
0
        ERR_raise(ERR_LIB_EC, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
464
0
        return -1;
465
0
    }
466
467
586
    ctx = BN_CTX_new_ex(eckey->libctx);
468
586
    if (ctx == NULL) {
469
0
        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
470
0
        return -1;
471
0
    }
472
586
    BN_CTX_start(ctx);
473
586
    u1 = BN_CTX_get(ctx);
474
586
    u2 = BN_CTX_get(ctx);
475
586
    m = BN_CTX_get(ctx);
476
586
    X = BN_CTX_get(ctx);
477
586
    if (X == NULL) {
478
0
        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
479
0
        goto err;
480
0
    }
481
482
586
    order = EC_GROUP_get0_order(group);
483
586
    if (order == NULL) {
484
0
        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
485
0
        goto err;
486
0
    }
487
488
586
    if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||
489
586
        BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s) ||
490
586
        BN_is_negative(sig->s) || BN_ucmp(sig->s, order) >= 0) {
491
31
        ERR_raise(ERR_LIB_EC, EC_R_BAD_SIGNATURE);
492
31
        ret = 0;                /* signature is invalid */
493
31
        goto err;
494
31
    }
495
    /* calculate tmp1 = inv(S) mod order */
496
555
    if (!ossl_ec_group_do_inverse_ord(group, u2, sig->s, ctx)) {
497
0
        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
498
0
        goto err;
499
0
    }
500
    /* digest -> m */
501
555
    i = BN_num_bits(order);
502
    /*
503
     * Need to truncate digest if it is too long: first truncate whole bytes.
504
     */
505
555
    if (8 * dgst_len > i)
506
77
        dgst_len = (i + 7) / 8;
507
555
    if (!BN_bin2bn(dgst, dgst_len, m)) {
508
0
        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
509
0
        goto err;
510
0
    }
511
    /* If still too long truncate remaining bits with a shift */
512
555
    if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
513
0
        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
514
0
        goto err;
515
0
    }
516
    /* u1 = m * tmp mod order */
517
555
    if (!BN_mod_mul(u1, m, u2, order, ctx)) {
518
0
        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
519
0
        goto err;
520
0
    }
521
    /* u2 = r * w mod q */
522
555
    if (!BN_mod_mul(u2, sig->r, u2, order, ctx)) {
523
0
        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
524
0
        goto err;
525
0
    }
526
527
555
    if ((point = EC_POINT_new(group)) == NULL) {
528
0
        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
529
0
        goto err;
530
0
    }
531
555
    if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) {
532
0
        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
533
0
        goto err;
534
0
    }
535
536
555
    if (!EC_POINT_get_affine_coordinates(group, point, X, NULL, ctx)) {
537
0
        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
538
0
        goto err;
539
0
    }
540
541
555
    if (!BN_nnmod(u1, X, order, ctx)) {
542
0
        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
543
0
        goto err;
544
0
    }
545
    /*  if the signature is correct u1 is equal to sig->r */
546
555
    ret = (BN_ucmp(u1, sig->r) == 0);
547
586
 err:
548
586
    BN_CTX_end(ctx);
549
586
    BN_CTX_free(ctx);
550
586
    EC_POINT_free(point);
551
586
    return ret;
552
555
}