Coverage Report

Created: 2023-06-08 06:41

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