Coverage Report

Created: 2026-05-20 07:05

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