Coverage Report

Created: 2024-07-27 06:39

/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
    pt = EC_POINT_new(group);
334
0
    if (ctx == NULL || pt == NULL) {
335
0
        ERR_raise(ERR_LIB_SM2, ERR_R_MALLOC_FAILURE);
336
0
        goto done;
337
0
    }
338
339
0
    BN_CTX_start(ctx);
340
0
    t = BN_CTX_get(ctx);
341
0
    x1 = BN_CTX_get(ctx);
342
0
    if (x1 == NULL) {
343
0
        ERR_raise(ERR_LIB_SM2, ERR_R_MALLOC_FAILURE);
344
0
        goto done;
345
0
    }
346
347
    /*
348
     * B1: verify whether r' in [1,n-1], verification failed if not
349
     * B2: verify whether s' in [1,n-1], verification failed if not
350
     * B3: set M'~=ZA || M'
351
     * B4: calculate e'=Hv(M'~)
352
     * B5: calculate t = (r' + s') modn, verification failed if t=0
353
     * B6: calculate the point (x1', y1')=[s']G + [t]PA
354
     * B7: calculate R=(e'+x1') modn, verification pass if yes, otherwise failed
355
     */
356
357
0
    ECDSA_SIG_get0(sig, &r, &s);
358
359
0
    if (BN_cmp(r, BN_value_one()) < 0
360
0
            || BN_cmp(s, BN_value_one()) < 0
361
0
            || BN_cmp(order, r) <= 0
362
0
            || BN_cmp(order, s) <= 0) {
363
0
        ERR_raise(ERR_LIB_SM2, SM2_R_BAD_SIGNATURE);
364
0
        goto done;
365
0
    }
366
367
0
    if (!BN_mod_add(t, r, s, order, ctx)) {
368
0
        ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
369
0
        goto done;
370
0
    }
371
372
0
    if (BN_is_zero(t)) {
373
0
        ERR_raise(ERR_LIB_SM2, SM2_R_BAD_SIGNATURE);
374
0
        goto done;
375
0
    }
376
377
0
    if (!EC_POINT_mul(group, pt, s, EC_KEY_get0_public_key(key), t, ctx)
378
0
            || !EC_POINT_get_affine_coordinates(group, pt, x1, NULL, ctx)) {
379
0
        ERR_raise(ERR_LIB_SM2, ERR_R_EC_LIB);
380
0
        goto done;
381
0
    }
382
383
0
    if (!BN_mod_add(t, e, x1, order, ctx)) {
384
0
        ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
385
0
        goto done;
386
0
    }
387
388
0
    if (BN_cmp(r, t) == 0)
389
0
        ret = 1;
390
391
0
 done:
392
0
    EC_POINT_free(pt);
393
0
    BN_CTX_free(ctx);
394
0
    return ret;
395
0
}
396
397
ECDSA_SIG *ossl_sm2_do_sign(const EC_KEY *key,
398
                            const EVP_MD *digest,
399
                            const uint8_t *id,
400
                            const size_t id_len,
401
                            const uint8_t *msg, size_t msg_len)
402
0
{
403
0
    BIGNUM *e = NULL;
404
0
    ECDSA_SIG *sig = NULL;
405
406
0
    e = sm2_compute_msg_hash(digest, key, id, id_len, msg, msg_len);
407
0
    if (e == NULL) {
408
        /* SM2err already called */
409
0
        goto done;
410
0
    }
411
412
0
    sig = sm2_sig_gen(key, e);
413
414
0
 done:
415
0
    BN_free(e);
416
0
    return sig;
417
0
}
418
419
int ossl_sm2_do_verify(const EC_KEY *key,
420
                       const EVP_MD *digest,
421
                       const ECDSA_SIG *sig,
422
                       const uint8_t *id,
423
                       const size_t id_len,
424
                       const uint8_t *msg, size_t msg_len)
425
0
{
426
0
    BIGNUM *e = NULL;
427
0
    int ret = 0;
428
429
0
    e = sm2_compute_msg_hash(digest, key, id, id_len, msg, msg_len);
430
0
    if (e == NULL) {
431
        /* SM2err already called */
432
0
        goto done;
433
0
    }
434
435
0
    ret = sm2_sig_verify(key, sig, e);
436
437
0
 done:
438
0
    BN_free(e);
439
0
    return ret;
440
0
}
441
442
int ossl_sm2_internal_sign(const unsigned char *dgst, int dgstlen,
443
                           unsigned char *sig, unsigned int *siglen,
444
                           EC_KEY *eckey)
445
0
{
446
0
    BIGNUM *e = NULL;
447
0
    ECDSA_SIG *s = NULL;
448
0
    int sigleni;
449
0
    int ret = -1;
450
451
0
    if (sig == NULL) {
452
0
        ERR_raise(ERR_LIB_SM2, ERR_R_PASSED_NULL_PARAMETER);
453
0
        goto done;
454
0
    }
455
456
0
    e = BN_bin2bn(dgst, dgstlen, NULL);
457
0
    if (e == NULL) {
458
0
       ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
459
0
       goto done;
460
0
    }
461
462
0
    s = sm2_sig_gen(eckey, e);
463
0
    if (s == NULL) {
464
0
        ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
465
0
        goto done;
466
0
    }
467
468
0
    sigleni = i2d_ECDSA_SIG(s, &sig);
469
0
    if (sigleni < 0) {
470
0
       ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
471
0
       goto done;
472
0
    }
473
0
    *siglen = (unsigned int)sigleni;
474
475
0
    ret = 1;
476
477
0
 done:
478
0
    ECDSA_SIG_free(s);
479
0
    BN_free(e);
480
0
    return ret;
481
0
}
482
483
int ossl_sm2_internal_verify(const unsigned char *dgst, int dgstlen,
484
                             const unsigned char *sig, int sig_len,
485
                             EC_KEY *eckey)
486
0
{
487
0
    ECDSA_SIG *s = NULL;
488
0
    BIGNUM *e = NULL;
489
0
    const unsigned char *p = sig;
490
0
    unsigned char *der = NULL;
491
0
    int derlen = -1;
492
0
    int ret = -1;
493
494
0
    s = ECDSA_SIG_new();
495
0
    if (s == NULL) {
496
0
        ERR_raise(ERR_LIB_SM2, ERR_R_MALLOC_FAILURE);
497
0
        goto done;
498
0
    }
499
0
    if (d2i_ECDSA_SIG(&s, &p, sig_len) == NULL) {
500
0
        ERR_raise(ERR_LIB_SM2, SM2_R_INVALID_ENCODING);
501
0
        goto done;
502
0
    }
503
    /* Ensure signature uses DER and doesn't have trailing garbage */
504
0
    derlen = i2d_ECDSA_SIG(s, &der);
505
0
    if (derlen != sig_len || memcmp(sig, der, derlen) != 0) {
506
0
        ERR_raise(ERR_LIB_SM2, SM2_R_INVALID_ENCODING);
507
0
        goto done;
508
0
    }
509
510
0
    e = BN_bin2bn(dgst, dgstlen, NULL);
511
0
    if (e == NULL) {
512
0
        ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
513
0
        goto done;
514
0
    }
515
516
0
    ret = sm2_sig_verify(eckey, s, e);
517
518
0
 done:
519
0
    OPENSSL_free(der);
520
0
    BN_free(e);
521
0
    ECDSA_SIG_free(s);
522
0
    return ret;
523
0
}