Coverage Report

Created: 2026-02-22 06:11

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