Coverage Report

Created: 2025-06-13 06:58

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