Coverage Report

Created: 2023-06-08 06:40

/src/openssl/crypto/sm2/sm2_sign.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
3
 * Copyright 2017 Ribose Inc. All Rights Reserved.
4
 * Ported from Ribose contributions from Botan.
5
 *
6
 * Licensed under the Apache License 2.0 (the "License").  You may not use
7
 * this file except in compliance with the License.  You can obtain a copy
8
 * in the file LICENSE in the source distribution or at
9
 * https://www.openssl.org/source/license.html
10
 */
11
12
#include "internal/deprecated.h"
13
14
#include "crypto/sm2.h"
15
#include "crypto/sm2err.h"
16
#include "crypto/ec.h" /* ossl_ec_group_do_inverse_ord() */
17
#include "internal/numbers.h"
18
#include <openssl/err.h>
19
#include <openssl/evp.h>
20
#include <openssl/bn.h>
21
#include <string.h>
22
23
int ossl_sm2_compute_z_digest(uint8_t *out,
24
                              const EVP_MD *digest,
25
                              const uint8_t *id,
26
                              const size_t id_len,
27
                              const EC_KEY *key)
28
0
{
29
0
    int rc = 0;
30
0
    const EC_GROUP *group = EC_KEY_get0_group(key);
31
0
    BN_CTX *ctx = NULL;
32
0
    EVP_MD_CTX *hash = NULL;
33
0
    BIGNUM *p = NULL;
34
0
    BIGNUM *a = NULL;
35
0
    BIGNUM *b = NULL;
36
0
    BIGNUM *xG = NULL;
37
0
    BIGNUM *yG = NULL;
38
0
    BIGNUM *xA = NULL;
39
0
    BIGNUM *yA = NULL;
40
0
    int p_bytes = 0;
41
0
    uint8_t *buf = NULL;
42
0
    uint16_t entl = 0;
43
0
    uint8_t e_byte = 0;
44
45
0
    hash = EVP_MD_CTX_new();
46
0
    if (hash == NULL) {
47
0
        ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
48
0
        goto done;
49
0
    }
50
0
    ctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(key));
51
0
    if (ctx == NULL) {
52
0
        ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
53
0
        goto done;
54
0
    }
55
56
0
    p = BN_CTX_get(ctx);
57
0
    a = BN_CTX_get(ctx);
58
0
    b = BN_CTX_get(ctx);
59
0
    xG = BN_CTX_get(ctx);
60
0
    yG = BN_CTX_get(ctx);
61
0
    xA = BN_CTX_get(ctx);
62
0
    yA = BN_CTX_get(ctx);
63
64
0
    if (yA == NULL) {
65
0
        ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
66
0
        goto done;
67
0
    }
68
69
0
    if (!EVP_DigestInit(hash, digest)) {
70
0
        ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
71
0
        goto done;
72
0
    }
73
74
    /* Z = h(ENTL || ID || a || b || xG || yG || xA || yA) */
75
76
0
    if (id_len >= (UINT16_MAX / 8)) {
77
        /* too large */
78
0
        ERR_raise(ERR_LIB_SM2, SM2_R_ID_TOO_LARGE);
79
0
        goto done;
80
0
    }
81
82
0
    entl = (uint16_t)(8 * id_len);
83
84
0
    e_byte = entl >> 8;
85
0
    if (!EVP_DigestUpdate(hash, &e_byte, 1)) {
86
0
        ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
87
0
        goto done;
88
0
    }
89
0
    e_byte = entl & 0xFF;
90
0
    if (!EVP_DigestUpdate(hash, &e_byte, 1)) {
91
0
        ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
92
0
        goto done;
93
0
    }
94
95
0
    if (id_len > 0 && !EVP_DigestUpdate(hash, id, id_len)) {
96
0
        ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
97
0
        goto done;
98
0
    }
99
100
0
    if (!EC_GROUP_get_curve(group, p, a, b, ctx)) {
101
0
        ERR_raise(ERR_LIB_SM2, ERR_R_EC_LIB);
102
0
        goto done;
103
0
    }
104
105
0
    p_bytes = BN_num_bytes(p);
106
0
    buf = OPENSSL_zalloc(p_bytes);
107
0
    if (buf == NULL)
108
0
        goto done;
109
110
0
    if (BN_bn2binpad(a, buf, p_bytes) < 0
111
0
            || !EVP_DigestUpdate(hash, buf, p_bytes)
112
0
            || BN_bn2binpad(b, buf, p_bytes) < 0
113
0
            || !EVP_DigestUpdate(hash, buf, p_bytes)
114
0
            || !EC_POINT_get_affine_coordinates(group,
115
0
                                                EC_GROUP_get0_generator(group),
116
0
                                                xG, yG, ctx)
117
0
            || BN_bn2binpad(xG, buf, p_bytes) < 0
118
0
            || !EVP_DigestUpdate(hash, buf, p_bytes)
119
0
            || BN_bn2binpad(yG, buf, p_bytes) < 0
120
0
            || !EVP_DigestUpdate(hash, buf, p_bytes)
121
0
            || !EC_POINT_get_affine_coordinates(group,
122
0
                                                EC_KEY_get0_public_key(key),
123
0
                                                xA, yA, ctx)
124
0
            || BN_bn2binpad(xA, buf, p_bytes) < 0
125
0
            || !EVP_DigestUpdate(hash, buf, p_bytes)
126
0
            || BN_bn2binpad(yA, buf, p_bytes) < 0
127
0
            || !EVP_DigestUpdate(hash, buf, p_bytes)
128
0
            || !EVP_DigestFinal(hash, out, NULL)) {
129
0
        ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
130
0
        goto done;
131
0
    }
132
133
0
    rc = 1;
134
135
0
 done:
136
0
    OPENSSL_free(buf);
137
0
    BN_CTX_free(ctx);
138
0
    EVP_MD_CTX_free(hash);
139
0
    return rc;
140
0
}
141
142
static BIGNUM *sm2_compute_msg_hash(const EVP_MD *digest,
143
                                    const EC_KEY *key,
144
                                    const uint8_t *id,
145
                                    const size_t id_len,
146
                                    const uint8_t *msg, size_t msg_len)
147
0
{
148
0
    EVP_MD_CTX *hash = EVP_MD_CTX_new();
149
0
    const int md_size = EVP_MD_get_size(digest);
150
0
    uint8_t *z = NULL;
151
0
    BIGNUM *e = NULL;
152
0
    EVP_MD *fetched_digest = NULL;
153
0
    OSSL_LIB_CTX *libctx = ossl_ec_key_get_libctx(key);
154
0
    const char *propq = ossl_ec_key_get0_propq(key);
155
156
0
    if (md_size < 0) {
157
0
        ERR_raise(ERR_LIB_SM2, SM2_R_INVALID_DIGEST);
158
0
        goto done;
159
0
    }
160
0
    if (hash == NULL) {
161
0
        ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
162
0
        goto done;
163
0
    }
164
165
0
    z = OPENSSL_zalloc(md_size);
166
0
    if (z == NULL)
167
0
        goto done;
168
169
0
    fetched_digest = EVP_MD_fetch(libctx, EVP_MD_get0_name(digest), propq);
170
0
    if (fetched_digest == NULL) {
171
0
        ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
172
0
        goto done;
173
0
    }
174
175
0
    if (!ossl_sm2_compute_z_digest(z, fetched_digest, id, id_len, key)) {
176
        /* SM2err already called */
177
0
        goto done;
178
0
    }
179
180
0
    if (!EVP_DigestInit(hash, fetched_digest)
181
0
            || !EVP_DigestUpdate(hash, z, md_size)
182
0
            || !EVP_DigestUpdate(hash, msg, msg_len)
183
               /* reuse z buffer to hold H(Z || M) */
184
0
            || !EVP_DigestFinal(hash, z, NULL)) {
185
0
        ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
186
0
        goto done;
187
0
    }
188
189
0
    e = BN_bin2bn(z, md_size, NULL);
190
0
    if (e == NULL)
191
0
        ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
192
193
0
 done:
194
0
    EVP_MD_free(fetched_digest);
195
0
    OPENSSL_free(z);
196
0
    EVP_MD_CTX_free(hash);
197
0
    return e;
198
0
}
199
200
static ECDSA_SIG *sm2_sig_gen(const EC_KEY *key, const BIGNUM *e)
201
0
{
202
0
    const BIGNUM *dA = EC_KEY_get0_private_key(key);
203
0
    const EC_GROUP *group = EC_KEY_get0_group(key);
204
0
    const BIGNUM *order = EC_GROUP_get0_order(group);
205
0
    ECDSA_SIG *sig = NULL;
206
0
    EC_POINT *kG = NULL;
207
0
    BN_CTX *ctx = NULL;
208
0
    BIGNUM *k = NULL;
209
0
    BIGNUM *rk = NULL;
210
0
    BIGNUM *r = NULL;
211
0
    BIGNUM *s = NULL;
212
0
    BIGNUM *x1 = NULL;
213
0
    BIGNUM *tmp = NULL;
214
0
    OSSL_LIB_CTX *libctx = ossl_ec_key_get_libctx(key);
215
216
0
    kG = EC_POINT_new(group);
217
0
    if (kG == NULL) {
218
0
        ERR_raise(ERR_LIB_SM2, ERR_R_EC_LIB);
219
0
        goto done;
220
0
    }
221
0
    ctx = BN_CTX_new_ex(libctx);
222
0
    if (ctx == NULL) {
223
0
        ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
224
0
        goto done;
225
0
    }
226
227
0
    BN_CTX_start(ctx);
228
0
    k = BN_CTX_get(ctx);
229
0
    rk = BN_CTX_get(ctx);
230
0
    x1 = BN_CTX_get(ctx);
231
0
    tmp = BN_CTX_get(ctx);
232
0
    if (tmp == NULL) {
233
0
        ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
234
0
        goto done;
235
0
    }
236
237
    /*
238
     * These values are returned and so should not be allocated out of the
239
     * context
240
     */
241
0
    r = BN_new();
242
0
    s = BN_new();
243
244
0
    if (r == NULL || s == NULL) {
245
0
        ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
246
0
        goto done;
247
0
    }
248
249
    /*
250
     * A3: Generate a random number k in [1,n-1] using random number generators;
251
     * A4: Compute (x1,y1)=[k]G, and convert the type of data x1 to be integer
252
     *     as specified in clause 4.2.8 of GM/T 0003.1-2012;
253
     * A5: Compute r=(e+x1) mod n. If r=0 or r+k=n, then go to A3;
254
     * A6: Compute s=(1/(1+dA)*(k-r*dA)) mod n. If s=0, then go to A3;
255
     * A7: Convert the type of data (r,s) to be bit strings according to the details
256
     *     in clause 4.2.2 of GM/T 0003.1-2012. Then the signature of message M is (r,s).
257
     */
258
0
    for (;;) {
259
0
        if (!BN_priv_rand_range_ex(k, order, 0, ctx)) {
260
0
            ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
261
0
            goto done;
262
0
        }
263
264
0
        if (!EC_POINT_mul(group, kG, k, NULL, NULL, ctx)
265
0
                || !EC_POINT_get_affine_coordinates(group, kG, x1, NULL,
266
0
                                                    ctx)
267
0
                || !BN_mod_add(r, e, x1, order, ctx)) {
268
0
            ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
269
0
            goto done;
270
0
        }
271
272
        /* try again if r == 0 or r+k == n */
273
0
        if (BN_is_zero(r))
274
0
            continue;
275
276
0
        if (!BN_add(rk, r, k)) {
277
0
            ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
278
0
            goto done;
279
0
        }
280
281
0
        if (BN_cmp(rk, order) == 0)
282
0
            continue;
283
284
0
        if (!BN_add(s, dA, BN_value_one())
285
0
                || !ossl_ec_group_do_inverse_ord(group, s, s, ctx)
286
0
                || !BN_mod_mul(tmp, dA, r, order, ctx)
287
0
                || !BN_sub(tmp, k, tmp)
288
0
                || !BN_mod_mul(s, s, tmp, order, ctx)) {
289
0
            ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
290
0
            goto done;
291
0
        }
292
293
        /* try again if s == 0 */
294
0
        if (BN_is_zero(s))
295
0
            continue;
296
297
0
        sig = ECDSA_SIG_new();
298
0
        if (sig == NULL) {
299
0
            ERR_raise(ERR_LIB_SM2, ERR_R_ECDSA_LIB);
300
0
            goto done;
301
0
        }
302
303
         /* takes ownership of r and s */
304
0
        ECDSA_SIG_set0(sig, r, s);
305
0
        break;
306
0
    }
307
308
0
 done:
309
0
    if (sig == NULL) {
310
0
        BN_free(r);
311
0
        BN_free(s);
312
0
    }
313
314
0
    BN_CTX_free(ctx);
315
0
    EC_POINT_free(kG);
316
0
    return sig;
317
0
}
318
319
static int sm2_sig_verify(const EC_KEY *key, const ECDSA_SIG *sig,
320
                          const BIGNUM *e)
321
0
{
322
0
    int ret = 0;
323
0
    const EC_GROUP *group = EC_KEY_get0_group(key);
324
0
    const BIGNUM *order = EC_GROUP_get0_order(group);
325
0
    BN_CTX *ctx = NULL;
326
0
    EC_POINT *pt = NULL;
327
0
    BIGNUM *t = NULL;
328
0
    BIGNUM *x1 = NULL;
329
0
    const BIGNUM *r = NULL;
330
0
    const BIGNUM *s = NULL;
331
0
    OSSL_LIB_CTX *libctx = ossl_ec_key_get_libctx(key);
332
333
0
    ctx = BN_CTX_new_ex(libctx);
334
0
    pt = EC_POINT_new(group);
335
0
    if (ctx == NULL || pt == NULL) {
336
0
        ERR_raise(ERR_LIB_SM2, ERR_R_EC_LIB);
337
0
        goto done;
338
0
    }
339
340
0
    BN_CTX_start(ctx);
341
0
    t = BN_CTX_get(ctx);
342
0
    x1 = BN_CTX_get(ctx);
343
0
    if (x1 == NULL) {
344
0
        ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
345
0
        goto done;
346
0
    }
347
348
    /*
349
     * B1: verify whether r' in [1,n-1], verification failed if not
350
     * B2: verify whether s' in [1,n-1], verification failed if not
351
     * B3: set M'~=ZA || M'
352
     * B4: calculate e'=Hv(M'~)
353
     * B5: calculate t = (r' + s') modn, verification failed if t=0
354
     * B6: calculate the point (x1', y1')=[s']G + [t]PA
355
     * B7: calculate R=(e'+x1') modn, verification pass if yes, otherwise failed
356
     */
357
358
0
    ECDSA_SIG_get0(sig, &r, &s);
359
360
0
    if (BN_cmp(r, BN_value_one()) < 0
361
0
            || BN_cmp(s, BN_value_one()) < 0
362
0
            || BN_cmp(order, r) <= 0
363
0
            || BN_cmp(order, s) <= 0) {
364
0
        ERR_raise(ERR_LIB_SM2, SM2_R_BAD_SIGNATURE);
365
0
        goto done;
366
0
    }
367
368
0
    if (!BN_mod_add(t, r, s, order, ctx)) {
369
0
        ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
370
0
        goto done;
371
0
    }
372
373
0
    if (BN_is_zero(t)) {
374
0
        ERR_raise(ERR_LIB_SM2, SM2_R_BAD_SIGNATURE);
375
0
        goto done;
376
0
    }
377
378
0
    if (!EC_POINT_mul(group, pt, s, EC_KEY_get0_public_key(key), t, ctx)
379
0
            || !EC_POINT_get_affine_coordinates(group, pt, x1, NULL, ctx)) {
380
0
        ERR_raise(ERR_LIB_SM2, ERR_R_EC_LIB);
381
0
        goto done;
382
0
    }
383
384
0
    if (!BN_mod_add(t, e, x1, order, ctx)) {
385
0
        ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
386
0
        goto done;
387
0
    }
388
389
0
    if (BN_cmp(r, t) == 0)
390
0
        ret = 1;
391
392
0
 done:
393
0
    EC_POINT_free(pt);
394
0
    BN_CTX_free(ctx);
395
0
    return ret;
396
0
}
397
398
ECDSA_SIG *ossl_sm2_do_sign(const EC_KEY *key,
399
                            const EVP_MD *digest,
400
                            const uint8_t *id,
401
                            const size_t id_len,
402
                            const uint8_t *msg, size_t msg_len)
403
0
{
404
0
    BIGNUM *e = NULL;
405
0
    ECDSA_SIG *sig = NULL;
406
407
0
    e = sm2_compute_msg_hash(digest, key, id, id_len, msg, msg_len);
408
0
    if (e == NULL) {
409
        /* SM2err already called */
410
0
        goto done;
411
0
    }
412
413
0
    sig = sm2_sig_gen(key, e);
414
415
0
 done:
416
0
    BN_free(e);
417
0
    return sig;
418
0
}
419
420
int ossl_sm2_do_verify(const EC_KEY *key,
421
                       const EVP_MD *digest,
422
                       const ECDSA_SIG *sig,
423
                       const uint8_t *id,
424
                       const size_t id_len,
425
                       const uint8_t *msg, size_t msg_len)
426
0
{
427
0
    BIGNUM *e = NULL;
428
0
    int ret = 0;
429
430
0
    e = sm2_compute_msg_hash(digest, key, id, id_len, msg, msg_len);
431
0
    if (e == NULL) {
432
        /* SM2err already called */
433
0
        goto done;
434
0
    }
435
436
0
    ret = sm2_sig_verify(key, sig, e);
437
438
0
 done:
439
0
    BN_free(e);
440
0
    return ret;
441
0
}
442
443
int ossl_sm2_internal_sign(const unsigned char *dgst, int dgstlen,
444
                           unsigned char *sig, unsigned int *siglen,
445
                           EC_KEY *eckey)
446
0
{
447
0
    BIGNUM *e = NULL;
448
0
    ECDSA_SIG *s = NULL;
449
0
    int sigleni;
450
0
    int ret = -1;
451
452
0
    e = BN_bin2bn(dgst, dgstlen, NULL);
453
0
    if (e == NULL) {
454
0
       ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
455
0
       goto done;
456
0
    }
457
458
0
    s = sm2_sig_gen(eckey, e);
459
0
    if (s == NULL) {
460
0
        ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
461
0
        goto done;
462
0
    }
463
464
0
    sigleni = i2d_ECDSA_SIG(s, sig != NULL ? &sig : NULL);
465
0
    if (sigleni < 0) {
466
0
       ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
467
0
       goto done;
468
0
    }
469
0
    *siglen = (unsigned int)sigleni;
470
471
0
    ret = 1;
472
473
0
 done:
474
0
    ECDSA_SIG_free(s);
475
0
    BN_free(e);
476
0
    return ret;
477
0
}
478
479
int ossl_sm2_internal_verify(const unsigned char *dgst, int dgstlen,
480
                             const unsigned char *sig, int sig_len,
481
                             EC_KEY *eckey)
482
0
{
483
0
    ECDSA_SIG *s = NULL;
484
0
    BIGNUM *e = NULL;
485
0
    const unsigned char *p = sig;
486
0
    unsigned char *der = NULL;
487
0
    int derlen = -1;
488
0
    int ret = -1;
489
490
0
    s = ECDSA_SIG_new();
491
0
    if (s == NULL) {
492
0
        ERR_raise(ERR_LIB_SM2, ERR_R_ECDSA_LIB);
493
0
        goto done;
494
0
    }
495
0
    if (d2i_ECDSA_SIG(&s, &p, sig_len) == NULL) {
496
0
        ERR_raise(ERR_LIB_SM2, SM2_R_INVALID_ENCODING);
497
0
        goto done;
498
0
    }
499
    /* Ensure signature uses DER and doesn't have trailing garbage */
500
0
    derlen = i2d_ECDSA_SIG(s, &der);
501
0
    if (derlen != sig_len || memcmp(sig, der, derlen) != 0) {
502
0
        ERR_raise(ERR_LIB_SM2, SM2_R_INVALID_ENCODING);
503
0
        goto done;
504
0
    }
505
506
0
    e = BN_bin2bn(dgst, dgstlen, NULL);
507
0
    if (e == NULL) {
508
0
        ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
509
0
        goto done;
510
0
    }
511
512
0
    ret = sm2_sig_verify(eckey, s, e);
513
514
0
 done:
515
0
    OPENSSL_free(der);
516
0
    BN_free(e);
517
0
    ECDSA_SIG_free(s);
518
0
    return ret;
519
0
}