Coverage Report

Created: 2023-06-08 06:41

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