Coverage Report

Created: 2026-05-24 07:14

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