Coverage Report

Created: 2026-05-24 07:14

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