Coverage Report

Created: 2024-05-21 06:33

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