Coverage Report

Created: 2023-06-08 06:41

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