Coverage Report

Created: 2025-08-25 06:30

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