Coverage Report

Created: 2025-07-01 06:23

/src/irssi/subprojects/openssl-1.1.1l/crypto/sm2/sm2_sign.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2017-2019 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 OpenSSL license (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 "crypto/sm2.h"
13
#include "crypto/sm2err.h"
14
#include "crypto/ec.h" /* ec_group_do_inverse_ord() */
15
#include "internal/numbers.h"
16
#include <openssl/err.h>
17
#include <openssl/evp.h>
18
#include <openssl/err.h>
19
#include <openssl/bn.h>
20
#include <string.h>
21
22
int sm2_compute_z_digest(uint8_t *out,
23
                         const EVP_MD *digest,
24
                         const uint8_t *id,
25
                         const size_t id_len,
26
                         const EC_KEY *key)
27
0
{
28
0
    int rc = 0;
29
0
    const EC_GROUP *group = EC_KEY_get0_group(key);
30
0
    BN_CTX *ctx = NULL;
31
0
    EVP_MD_CTX *hash = NULL;
32
0
    BIGNUM *p = NULL;
33
0
    BIGNUM *a = NULL;
34
0
    BIGNUM *b = NULL;
35
0
    BIGNUM *xG = NULL;
36
0
    BIGNUM *yG = NULL;
37
0
    BIGNUM *xA = NULL;
38
0
    BIGNUM *yA = NULL;
39
0
    int p_bytes = 0;
40
0
    uint8_t *buf = NULL;
41
0
    uint16_t entl = 0;
42
0
    uint8_t e_byte = 0;
43
44
0
    hash = EVP_MD_CTX_new();
45
0
    ctx = BN_CTX_new();
46
0
    if (hash == NULL || ctx == NULL) {
47
0
        SM2err(SM2_F_SM2_COMPUTE_Z_DIGEST, ERR_R_MALLOC_FAILURE);
48
0
        goto done;
49
0
    }
50
51
0
    p = BN_CTX_get(ctx);
52
0
    a = BN_CTX_get(ctx);
53
0
    b = BN_CTX_get(ctx);
54
0
    xG = BN_CTX_get(ctx);
55
0
    yG = BN_CTX_get(ctx);
56
0
    xA = BN_CTX_get(ctx);
57
0
    yA = BN_CTX_get(ctx);
58
59
0
    if (yA == NULL) {
60
0
        SM2err(SM2_F_SM2_COMPUTE_Z_DIGEST, ERR_R_MALLOC_FAILURE);
61
0
        goto done;
62
0
    }
63
64
0
    if (!EVP_DigestInit(hash, digest)) {
65
0
        SM2err(SM2_F_SM2_COMPUTE_Z_DIGEST, ERR_R_EVP_LIB);
66
0
        goto done;
67
0
    }
68
69
    /* Z = h(ENTL || ID || a || b || xG || yG || xA || yA) */
70
71
0
    if (id_len >= (UINT16_MAX / 8)) {
72
        /* too large */
73
0
        SM2err(SM2_F_SM2_COMPUTE_Z_DIGEST, SM2_R_ID_TOO_LARGE);
74
0
        goto done;
75
0
    }
76
77
0
    entl = (uint16_t)(8 * id_len);
78
79
0
    e_byte = entl >> 8;
80
0
    if (!EVP_DigestUpdate(hash, &e_byte, 1)) {
81
0
        SM2err(SM2_F_SM2_COMPUTE_Z_DIGEST, ERR_R_EVP_LIB);
82
0
        goto done;
83
0
    }
84
0
    e_byte = entl & 0xFF;
85
0
    if (!EVP_DigestUpdate(hash, &e_byte, 1)) {
86
0
        SM2err(SM2_F_SM2_COMPUTE_Z_DIGEST, ERR_R_EVP_LIB);
87
0
        goto done;
88
0
    }
89
90
0
    if (id_len > 0 && !EVP_DigestUpdate(hash, id, id_len)) {
91
0
        SM2err(SM2_F_SM2_COMPUTE_Z_DIGEST, ERR_R_EVP_LIB);
92
0
        goto done;
93
0
    }
94
95
0
    if (!EC_GROUP_get_curve(group, p, a, b, ctx)) {
96
0
        SM2err(SM2_F_SM2_COMPUTE_Z_DIGEST, ERR_R_EC_LIB);
97
0
        goto done;
98
0
    }
99
100
0
    p_bytes = BN_num_bytes(p);
101
0
    buf = OPENSSL_zalloc(p_bytes);
102
0
    if (buf == NULL) {
103
0
        SM2err(SM2_F_SM2_COMPUTE_Z_DIGEST, ERR_R_MALLOC_FAILURE);
104
0
        goto done;
105
0
    }
106
107
0
    if (BN_bn2binpad(a, buf, p_bytes) < 0
108
0
            || !EVP_DigestUpdate(hash, buf, p_bytes)
109
0
            || BN_bn2binpad(b, buf, p_bytes) < 0
110
0
            || !EVP_DigestUpdate(hash, buf, p_bytes)
111
0
            || !EC_POINT_get_affine_coordinates(group,
112
0
                                                EC_GROUP_get0_generator(group),
113
0
                                                xG, yG, ctx)
114
0
            || BN_bn2binpad(xG, buf, p_bytes) < 0
115
0
            || !EVP_DigestUpdate(hash, buf, p_bytes)
116
0
            || BN_bn2binpad(yG, buf, p_bytes) < 0
117
0
            || !EVP_DigestUpdate(hash, buf, p_bytes)
118
0
            || !EC_POINT_get_affine_coordinates(group,
119
0
                                                EC_KEY_get0_public_key(key),
120
0
                                                xA, yA, ctx)
121
0
            || BN_bn2binpad(xA, buf, p_bytes) < 0
122
0
            || !EVP_DigestUpdate(hash, buf, p_bytes)
123
0
            || BN_bn2binpad(yA, buf, p_bytes) < 0
124
0
            || !EVP_DigestUpdate(hash, buf, p_bytes)
125
0
            || !EVP_DigestFinal(hash, out, NULL)) {
126
0
        SM2err(SM2_F_SM2_COMPUTE_Z_DIGEST, ERR_R_INTERNAL_ERROR);
127
0
        goto done;
128
0
    }
129
130
0
    rc = 1;
131
132
0
 done:
133
0
    OPENSSL_free(buf);
134
0
    BN_CTX_free(ctx);
135
0
    EVP_MD_CTX_free(hash);
136
0
    return rc;
137
0
}
138
139
static BIGNUM *sm2_compute_msg_hash(const EVP_MD *digest,
140
                                    const EC_KEY *key,
141
                                    const uint8_t *id,
142
                                    const size_t id_len,
143
                                    const uint8_t *msg, size_t msg_len)
144
0
{
145
0
    EVP_MD_CTX *hash = EVP_MD_CTX_new();
146
0
    const int md_size = EVP_MD_size(digest);
147
0
    uint8_t *z = NULL;
148
0
    BIGNUM *e = NULL;
149
150
0
    if (md_size < 0) {
151
0
        SM2err(SM2_F_SM2_COMPUTE_MSG_HASH, SM2_R_INVALID_DIGEST);
152
0
        goto done;
153
0
    }
154
155
0
    z = OPENSSL_zalloc(md_size);
156
0
    if (hash == NULL || z == NULL) {
157
0
        SM2err(SM2_F_SM2_COMPUTE_MSG_HASH, ERR_R_MALLOC_FAILURE);
158
0
        goto done;
159
0
    }
160
161
0
    if (!sm2_compute_z_digest(z, digest, id, id_len, key)) {
162
        /* SM2err already called */
163
0
        goto done;
164
0
    }
165
166
0
    if (!EVP_DigestInit(hash, digest)
167
0
            || !EVP_DigestUpdate(hash, z, md_size)
168
0
            || !EVP_DigestUpdate(hash, msg, msg_len)
169
               /* reuse z buffer to hold H(Z || M) */
170
0
            || !EVP_DigestFinal(hash, z, NULL)) {
171
0
        SM2err(SM2_F_SM2_COMPUTE_MSG_HASH, ERR_R_EVP_LIB);
172
0
        goto done;
173
0
    }
174
175
0
    e = BN_bin2bn(z, md_size, NULL);
176
0
    if (e == NULL)
177
0
        SM2err(SM2_F_SM2_COMPUTE_MSG_HASH, ERR_R_INTERNAL_ERROR);
178
179
0
 done:
180
0
    OPENSSL_free(z);
181
0
    EVP_MD_CTX_free(hash);
182
0
    return e;
183
0
}
184
185
static ECDSA_SIG *sm2_sig_gen(const EC_KEY *key, const BIGNUM *e)
186
0
{
187
0
    const BIGNUM *dA = EC_KEY_get0_private_key(key);
188
0
    const EC_GROUP *group = EC_KEY_get0_group(key);
189
0
    const BIGNUM *order = EC_GROUP_get0_order(group);
190
0
    ECDSA_SIG *sig = NULL;
191
0
    EC_POINT *kG = NULL;
192
0
    BN_CTX *ctx = NULL;
193
0
    BIGNUM *k = NULL;
194
0
    BIGNUM *rk = NULL;
195
0
    BIGNUM *r = NULL;
196
0
    BIGNUM *s = NULL;
197
0
    BIGNUM *x1 = NULL;
198
0
    BIGNUM *tmp = NULL;
199
200
0
    kG = EC_POINT_new(group);
201
0
    ctx = BN_CTX_new();
202
0
    if (kG == NULL || ctx == NULL) {
203
0
        SM2err(SM2_F_SM2_SIG_GEN, ERR_R_MALLOC_FAILURE);
204
0
        goto done;
205
0
    }
206
207
0
    BN_CTX_start(ctx);
208
0
    k = BN_CTX_get(ctx);
209
0
    rk = BN_CTX_get(ctx);
210
0
    x1 = BN_CTX_get(ctx);
211
0
    tmp = BN_CTX_get(ctx);
212
0
    if (tmp == NULL) {
213
0
        SM2err(SM2_F_SM2_SIG_GEN, ERR_R_MALLOC_FAILURE);
214
0
        goto done;
215
0
    }
216
217
    /*
218
     * These values are returned and so should not be allocated out of the
219
     * context
220
     */
221
0
    r = BN_new();
222
0
    s = BN_new();
223
224
0
    if (r == NULL || s == NULL) {
225
0
        SM2err(SM2_F_SM2_SIG_GEN, ERR_R_MALLOC_FAILURE);
226
0
        goto done;
227
0
    }
228
229
0
    for (;;) {
230
0
        if (!BN_priv_rand_range(k, order)) {
231
0
            SM2err(SM2_F_SM2_SIG_GEN, ERR_R_INTERNAL_ERROR);
232
0
            goto done;
233
0
        }
234
235
0
        if (!EC_POINT_mul(group, kG, k, NULL, NULL, ctx)
236
0
                || !EC_POINT_get_affine_coordinates(group, kG, x1, NULL,
237
0
                                                    ctx)
238
0
                || !BN_mod_add(r, e, x1, order, ctx)) {
239
0
            SM2err(SM2_F_SM2_SIG_GEN, ERR_R_INTERNAL_ERROR);
240
0
            goto done;
241
0
        }
242
243
        /* try again if r == 0 or r+k == n */
244
0
        if (BN_is_zero(r))
245
0
            continue;
246
247
0
        if (!BN_add(rk, r, k)) {
248
0
            SM2err(SM2_F_SM2_SIG_GEN, ERR_R_INTERNAL_ERROR);
249
0
            goto done;
250
0
        }
251
252
0
        if (BN_cmp(rk, order) == 0)
253
0
            continue;
254
255
0
        if (!BN_add(s, dA, BN_value_one())
256
0
                || !ec_group_do_inverse_ord(group, s, s, ctx)
257
0
                || !BN_mod_mul(tmp, dA, r, order, ctx)
258
0
                || !BN_sub(tmp, k, tmp)
259
0
                || !BN_mod_mul(s, s, tmp, order, ctx)) {
260
0
            SM2err(SM2_F_SM2_SIG_GEN, ERR_R_BN_LIB);
261
0
            goto done;
262
0
        }
263
264
0
        sig = ECDSA_SIG_new();
265
0
        if (sig == NULL) {
266
0
            SM2err(SM2_F_SM2_SIG_GEN, ERR_R_MALLOC_FAILURE);
267
0
            goto done;
268
0
        }
269
270
         /* takes ownership of r and s */
271
0
        ECDSA_SIG_set0(sig, r, s);
272
0
        break;
273
0
    }
274
275
0
 done:
276
0
    if (sig == NULL) {
277
0
        BN_free(r);
278
0
        BN_free(s);
279
0
    }
280
281
0
    BN_CTX_free(ctx);
282
0
    EC_POINT_free(kG);
283
0
    return sig;
284
0
}
285
286
static int sm2_sig_verify(const EC_KEY *key, const ECDSA_SIG *sig,
287
                          const BIGNUM *e)
288
0
{
289
0
    int ret = 0;
290
0
    const EC_GROUP *group = EC_KEY_get0_group(key);
291
0
    const BIGNUM *order = EC_GROUP_get0_order(group);
292
0
    BN_CTX *ctx = NULL;
293
0
    EC_POINT *pt = NULL;
294
0
    BIGNUM *t = NULL;
295
0
    BIGNUM *x1 = NULL;
296
0
    const BIGNUM *r = NULL;
297
0
    const BIGNUM *s = NULL;
298
299
0
    ctx = BN_CTX_new();
300
0
    pt = EC_POINT_new(group);
301
0
    if (ctx == NULL || pt == NULL) {
302
0
        SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_MALLOC_FAILURE);
303
0
        goto done;
304
0
    }
305
306
0
    BN_CTX_start(ctx);
307
0
    t = BN_CTX_get(ctx);
308
0
    x1 = BN_CTX_get(ctx);
309
0
    if (x1 == NULL) {
310
0
        SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_MALLOC_FAILURE);
311
0
        goto done;
312
0
    }
313
314
    /*
315
     * B1: verify whether r' in [1,n-1], verification failed if not
316
     * B2: verify whether s' in [1,n-1], verification failed if not
317
     * B3: set M'~=ZA || M'
318
     * B4: calculate e'=Hv(M'~)
319
     * B5: calculate t = (r' + s') modn, verification failed if t=0
320
     * B6: calculate the point (x1', y1')=[s']G + [t]PA
321
     * B7: calculate R=(e'+x1') modn, verification pass if yes, otherwise failed
322
     */
323
324
0
    ECDSA_SIG_get0(sig, &r, &s);
325
326
0
    if (BN_cmp(r, BN_value_one()) < 0
327
0
            || BN_cmp(s, BN_value_one()) < 0
328
0
            || BN_cmp(order, r) <= 0
329
0
            || BN_cmp(order, s) <= 0) {
330
0
        SM2err(SM2_F_SM2_SIG_VERIFY, SM2_R_BAD_SIGNATURE);
331
0
        goto done;
332
0
    }
333
334
0
    if (!BN_mod_add(t, r, s, order, ctx)) {
335
0
        SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_BN_LIB);
336
0
        goto done;
337
0
    }
338
339
0
    if (BN_is_zero(t)) {
340
0
        SM2err(SM2_F_SM2_SIG_VERIFY, SM2_R_BAD_SIGNATURE);
341
0
        goto done;
342
0
    }
343
344
0
    if (!EC_POINT_mul(group, pt, s, EC_KEY_get0_public_key(key), t, ctx)
345
0
            || !EC_POINT_get_affine_coordinates(group, pt, x1, NULL, ctx)) {
346
0
        SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_EC_LIB);
347
0
        goto done;
348
0
    }
349
350
0
    if (!BN_mod_add(t, e, x1, order, ctx)) {
351
0
        SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_BN_LIB);
352
0
        goto done;
353
0
    }
354
355
0
    if (BN_cmp(r, t) == 0)
356
0
        ret = 1;
357
358
0
 done:
359
0
    EC_POINT_free(pt);
360
0
    BN_CTX_free(ctx);
361
0
    return ret;
362
0
}
363
364
ECDSA_SIG *sm2_do_sign(const EC_KEY *key,
365
                       const EVP_MD *digest,
366
                       const uint8_t *id,
367
                       const size_t id_len,
368
                       const uint8_t *msg, size_t msg_len)
369
0
{
370
0
    BIGNUM *e = NULL;
371
0
    ECDSA_SIG *sig = NULL;
372
373
0
    e = sm2_compute_msg_hash(digest, key, id, id_len, msg, msg_len);
374
0
    if (e == NULL) {
375
        /* SM2err already called */
376
0
        goto done;
377
0
    }
378
379
0
    sig = sm2_sig_gen(key, e);
380
381
0
 done:
382
0
    BN_free(e);
383
0
    return sig;
384
0
}
385
386
int sm2_do_verify(const EC_KEY *key,
387
                  const EVP_MD *digest,
388
                  const ECDSA_SIG *sig,
389
                  const uint8_t *id,
390
                  const size_t id_len,
391
                  const uint8_t *msg, size_t msg_len)
392
0
{
393
0
    BIGNUM *e = NULL;
394
0
    int ret = 0;
395
396
0
    e = sm2_compute_msg_hash(digest, key, id, id_len, msg, msg_len);
397
0
    if (e == NULL) {
398
        /* SM2err already called */
399
0
        goto done;
400
0
    }
401
402
0
    ret = sm2_sig_verify(key, sig, e);
403
404
0
 done:
405
0
    BN_free(e);
406
0
    return ret;
407
0
}
408
409
int sm2_sign(const unsigned char *dgst, int dgstlen,
410
             unsigned char *sig, unsigned int *siglen, EC_KEY *eckey)
411
0
{
412
0
    BIGNUM *e = NULL;
413
0
    ECDSA_SIG *s = NULL;
414
0
    int sigleni;
415
0
    int ret = -1;
416
417
0
    e = BN_bin2bn(dgst, dgstlen, NULL);
418
0
    if (e == NULL) {
419
0
       SM2err(SM2_F_SM2_SIGN, ERR_R_BN_LIB);
420
0
       goto done;
421
0
    }
422
423
0
    s = sm2_sig_gen(eckey, e);
424
425
0
    sigleni = i2d_ECDSA_SIG(s, &sig);
426
0
    if (sigleni < 0) {
427
0
       SM2err(SM2_F_SM2_SIGN, ERR_R_INTERNAL_ERROR);
428
0
       goto done;
429
0
    }
430
0
    *siglen = (unsigned int)sigleni;
431
432
0
    ret = 1;
433
434
0
 done:
435
0
    ECDSA_SIG_free(s);
436
0
    BN_free(e);
437
0
    return ret;
438
0
}
439
440
int sm2_verify(const unsigned char *dgst, int dgstlen,
441
               const unsigned char *sig, int sig_len, EC_KEY *eckey)
442
0
{
443
0
    ECDSA_SIG *s = NULL;
444
0
    BIGNUM *e = NULL;
445
0
    const unsigned char *p = sig;
446
0
    unsigned char *der = NULL;
447
0
    int derlen = -1;
448
0
    int ret = -1;
449
450
0
    s = ECDSA_SIG_new();
451
0
    if (s == NULL) {
452
0
        SM2err(SM2_F_SM2_VERIFY, ERR_R_MALLOC_FAILURE);
453
0
        goto done;
454
0
    }
455
0
    if (d2i_ECDSA_SIG(&s, &p, sig_len) == NULL) {
456
0
        SM2err(SM2_F_SM2_VERIFY, SM2_R_INVALID_ENCODING);
457
0
        goto done;
458
0
    }
459
    /* Ensure signature uses DER and doesn't have trailing garbage */
460
0
    derlen = i2d_ECDSA_SIG(s, &der);
461
0
    if (derlen != sig_len || memcmp(sig, der, derlen) != 0) {
462
0
        SM2err(SM2_F_SM2_VERIFY, SM2_R_INVALID_ENCODING);
463
0
        goto done;
464
0
    }
465
466
0
    e = BN_bin2bn(dgst, dgstlen, NULL);
467
0
    if (e == NULL) {
468
0
        SM2err(SM2_F_SM2_VERIFY, ERR_R_BN_LIB);
469
0
        goto done;
470
0
    }
471
472
0
    ret = sm2_sig_verify(eckey, s, e);
473
474
0
 done:
475
0
    OPENSSL_free(der);
476
0
    BN_free(e);
477
0
    ECDSA_SIG_free(s);
478
0
    return ret;
479
0
}