Coverage Report

Created: 2023-06-08 06:40

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