Coverage Report

Created: 2025-06-13 06:58

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