Coverage Report

Created: 2026-10-16 07:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/sm2/sm2_sign.c
Line
Count
Source
1
/*
2
 * Copyright 2017-2026 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 "crypto/bn.h" /* fixed-top / Montgomery constant-time BN helpers */
18
#include "internal/numbers.h"
19
#include <openssl/err.h>
20
#include <openssl/evp.h>
21
#include <openssl/bn.h>
22
#include <string.h>
23
24
/*
25
 * [SM2 Signature Scheme]
26
 * (https://datatracker.ietf.org/doc/html/rfc8998#section-3.2.1)
27
 *
28
 * If either a client or a server needs to verify the peer's SM2 certificate
29
 * contained in the Certificate message, then the following ASCII string value
30
 * MUST be used as the SM2 identifier according to [GMT.0009-2012]:
31
 *
32
 * 1234567812345678
33
 */
34
static const uint8_t default_sm2_id[] = {
35
    0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
36
    0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38
37
};
38
39
int ossl_sm2_compute_z_digest(uint8_t *out,
40
    const EVP_MD *digest,
41
    const uint8_t *id,
42
    size_t id_len,
43
    const EC_KEY *key)
44
0
{
45
0
    int rc = 0;
46
0
    const EC_GROUP *group = EC_KEY_get0_group(key);
47
0
    const EC_POINT *pubkey = EC_KEY_get0_public_key(key);
48
0
    BN_CTX *ctx = NULL;
49
0
    EVP_MD_CTX *hash = NULL;
50
0
    BIGNUM *p = NULL;
51
0
    BIGNUM *a = NULL;
52
0
    BIGNUM *b = NULL;
53
0
    BIGNUM *xG = NULL;
54
0
    BIGNUM *yG = NULL;
55
0
    BIGNUM *xA = NULL;
56
0
    BIGNUM *yA = NULL;
57
0
    int p_bytes = 0;
58
0
    uint8_t *buf = NULL;
59
0
    uint16_t entl = 0;
60
0
    uint8_t e_byte = 0;
61
62
    /* SM2 Signatures require a public key, check for it */
63
0
    if (pubkey == NULL) {
64
0
        ERR_raise(ERR_LIB_SM2, ERR_R_PASSED_NULL_PARAMETER);
65
0
        goto done;
66
0
    }
67
68
0
    hash = EVP_MD_CTX_new();
69
0
    if (hash == NULL) {
70
0
        ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
71
0
        goto done;
72
0
    }
73
0
    ctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(key));
74
0
    if (ctx == NULL) {
75
0
        ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
76
0
        goto done;
77
0
    }
78
79
0
    BN_CTX_start(ctx);
80
0
    p = BN_CTX_get(ctx);
81
0
    a = BN_CTX_get(ctx);
82
0
    b = BN_CTX_get(ctx);
83
0
    xG = BN_CTX_get(ctx);
84
0
    yG = BN_CTX_get(ctx);
85
0
    xA = BN_CTX_get(ctx);
86
0
    yA = BN_CTX_get(ctx);
87
88
0
    if (yA == NULL) {
89
0
        ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
90
0
        goto done;
91
0
    }
92
93
0
    if (!EVP_DigestInit(hash, digest)) {
94
0
        ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
95
0
        goto done;
96
0
    }
97
98
    /* Z = h(ENTL || ID || a || b || xG || yG || xA || yA) */
99
100
0
    if (id == NULL) {
101
0
        id = default_sm2_id;
102
0
        id_len = sizeof(default_sm2_id);
103
0
    }
104
105
0
    if (id_len >= (UINT16_MAX / 8)) {
106
        /* too large */
107
0
        ERR_raise(ERR_LIB_SM2, SM2_R_ID_TOO_LARGE);
108
0
        goto done;
109
0
    }
110
111
0
    entl = (uint16_t)(8 * id_len);
112
113
0
    e_byte = entl >> 8;
114
0
    if (!EVP_DigestUpdate(hash, &e_byte, 1)) {
115
0
        ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
116
0
        goto done;
117
0
    }
118
0
    e_byte = entl & 0xFF;
119
0
    if (!EVP_DigestUpdate(hash, &e_byte, 1)) {
120
0
        ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
121
0
        goto done;
122
0
    }
123
124
0
    if (id_len > 0 && !EVP_DigestUpdate(hash, id, id_len)) {
125
0
        ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
126
0
        goto done;
127
0
    }
128
129
0
    if (!EC_GROUP_get_curve(group, p, a, b, ctx)) {
130
0
        ERR_raise(ERR_LIB_SM2, ERR_R_EC_LIB);
131
0
        goto done;
132
0
    }
133
134
0
    p_bytes = BN_num_bytes(p);
135
0
    buf = OPENSSL_zalloc(p_bytes);
136
0
    if (buf == NULL)
137
0
        goto done;
138
139
0
    if (BN_bn2binpad(a, buf, p_bytes) < 0
140
0
        || !EVP_DigestUpdate(hash, buf, p_bytes)
141
0
        || BN_bn2binpad(b, buf, p_bytes) < 0
142
0
        || !EVP_DigestUpdate(hash, buf, p_bytes)
143
0
        || !EC_POINT_get_affine_coordinates(group,
144
0
            EC_GROUP_get0_generator(group),
145
0
            xG, yG, ctx)
146
0
        || BN_bn2binpad(xG, buf, p_bytes) < 0
147
0
        || !EVP_DigestUpdate(hash, buf, p_bytes)
148
0
        || BN_bn2binpad(yG, buf, p_bytes) < 0
149
0
        || !EVP_DigestUpdate(hash, buf, p_bytes)
150
0
        || !EC_POINT_get_affine_coordinates(group,
151
0
            pubkey,
152
0
            xA, yA, ctx)
153
0
        || BN_bn2binpad(xA, buf, p_bytes) < 0
154
0
        || !EVP_DigestUpdate(hash, buf, p_bytes)
155
0
        || BN_bn2binpad(yA, buf, p_bytes) < 0
156
0
        || !EVP_DigestUpdate(hash, buf, p_bytes)
157
0
        || !EVP_DigestFinal(hash, out, NULL)) {
158
0
        ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
159
0
        goto done;
160
0
    }
161
162
0
    rc = 1;
163
164
0
done:
165
0
    OPENSSL_free(buf);
166
0
    BN_CTX_end(ctx);
167
0
    BN_CTX_free(ctx);
168
0
    EVP_MD_CTX_free(hash);
169
0
    return rc;
170
0
}
171
172
static BIGNUM *sm2_compute_msg_hash(const EVP_MD *digest,
173
    const EC_KEY *key,
174
    const uint8_t *id,
175
    const size_t id_len,
176
    const uint8_t *msg, size_t msg_len)
177
0
{
178
0
    EVP_MD_CTX *hash = EVP_MD_CTX_new();
179
0
    const int md_size = EVP_MD_get_size(digest);
180
0
    uint8_t *z = NULL;
181
0
    BIGNUM *e = NULL;
182
0
    EVP_MD *fetched_digest = NULL;
183
0
    OSSL_LIB_CTX *libctx = ossl_ec_key_get_libctx(key);
184
0
    const char *propq = ossl_ec_key_get0_propq(key);
185
186
0
    if (md_size <= 0) {
187
0
        ERR_raise(ERR_LIB_SM2, SM2_R_INVALID_DIGEST);
188
0
        goto done;
189
0
    }
190
0
    if (hash == NULL) {
191
0
        ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
192
0
        goto done;
193
0
    }
194
195
0
    z = OPENSSL_zalloc(md_size);
196
0
    if (z == NULL)
197
0
        goto done;
198
199
0
    fetched_digest = EVP_MD_fetch(libctx, EVP_MD_get0_name(digest), propq);
200
0
    if (fetched_digest == NULL) {
201
0
        ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
202
0
        goto done;
203
0
    }
204
205
0
    if (!ossl_sm2_compute_z_digest(z, fetched_digest, id, id_len, key)) {
206
        /* SM2err already called */
207
0
        goto done;
208
0
    }
209
210
0
    if (!EVP_DigestInit(hash, fetched_digest)
211
0
        || !EVP_DigestUpdate(hash, z, md_size)
212
0
        || !EVP_DigestUpdate(hash, msg, msg_len)
213
        /* reuse z buffer to hold H(Z || M) */
214
0
        || !EVP_DigestFinal(hash, z, NULL)) {
215
0
        ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
216
0
        goto done;
217
0
    }
218
219
0
    e = BN_bin2bn(z, md_size, NULL);
220
0
    if (e == NULL)
221
0
        ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
222
223
0
done:
224
0
    EVP_MD_free(fetched_digest);
225
0
    OPENSSL_free(z);
226
0
    EVP_MD_CTX_free(hash);
227
0
    return e;
228
0
}
229
230
static ECDSA_SIG *sm2_sig_gen(const EC_KEY *key, const BIGNUM *e)
231
0
{
232
0
    const BIGNUM *dA = EC_KEY_get0_private_key(key);
233
0
    const EC_GROUP *group = EC_KEY_get0_group(key);
234
0
    const BIGNUM *order = EC_GROUP_get0_order(group);
235
0
    ECDSA_SIG *sig = NULL;
236
0
    EC_POINT *kG = NULL;
237
0
    BN_CTX *ctx = NULL;
238
0
    BIGNUM *k = NULL;
239
0
    BIGNUM *r = NULL;
240
0
    BIGNUM *s = NULL;
241
0
    BIGNUM *x1 = NULL;
242
0
    BIGNUM *tmp = NULL;
243
0
    BN_MONT_CTX *mont = EC_GROUP_get_mont_data(group);
244
0
    OSSL_LIB_CTX *libctx = ossl_ec_key_get_libctx(key);
245
246
0
    if (dA == NULL) {
247
0
        ERR_raise(ERR_LIB_SM2, SM2_R_INVALID_PRIVATE_KEY);
248
0
        goto done;
249
0
    }
250
251
0
    if (mont == NULL) {
252
0
        ERR_raise(ERR_LIB_SM2, ERR_R_EC_LIB);
253
0
        goto done;
254
0
    }
255
0
    kG = EC_POINT_new(group);
256
0
    if (kG == NULL) {
257
0
        ERR_raise(ERR_LIB_SM2, ERR_R_EC_LIB);
258
0
        goto done;
259
0
    }
260
0
    ctx = BN_CTX_new_ex(libctx);
261
0
    if (ctx == NULL) {
262
0
        ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
263
0
        goto done;
264
0
    }
265
266
0
    BN_CTX_start(ctx);
267
0
    k = BN_CTX_get(ctx);
268
0
    x1 = BN_CTX_get(ctx);
269
0
    tmp = BN_CTX_get(ctx);
270
0
    if (tmp == NULL) {
271
0
        ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
272
0
        goto done;
273
0
    }
274
275
    /*
276
     * These values are returned and so should not be allocated out of the
277
     * context
278
     */
279
0
    r = BN_new();
280
0
    s = BN_new();
281
282
0
    if (r == NULL || s == NULL) {
283
0
        ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
284
0
        goto done;
285
0
    }
286
287
    /*
288
     * A3: Generate a random number k in [1,n-1] using random number generators;
289
     * A4: Compute (x1,y1)=[k]G, and convert the type of data x1 to be integer
290
     *     as specified in clause 4.2.8 of GM/T 0003.1-2012;
291
     * A5: Compute r=(e+x1) mod n. If r=0 or r+k=n, then go to A3;
292
     * A6: Compute s=(1/(1+dA)*(k-r*dA)) mod n. If s=0, then go to A3;
293
     * A7: Convert the type of data (r,s) to be bit strings according to the details
294
     *     in clause 4.2.2 of GM/T 0003.1-2012. Then the signature of message M is (r,s).
295
     */
296
0
    for (;;) {
297
0
        if (!BN_priv_rand_range_ex(k, order, 0, ctx)) {
298
0
            ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
299
0
            goto done;
300
0
        }
301
        /*
302
         * Pin the nonce to a fixed, value-independent width and flag it
303
         * BN_FLG_CONSTTIME, so its magnitude does not leak through operand
304
         * lengths in the scalar copy inside the ladder, or in the arithmetic
305
         * below.  BN_priv_rand_range_ex() is kept so the nonce value itself
306
         * is unchanged; only its representation is pinned.
307
         */
308
0
        BN_set_flags(k, BN_FLG_CONSTTIME);
309
0
        if (!bn_set_top_fixed(k, bn_get_top(order))) {
310
0
            ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
311
0
            goto done;
312
0
        }
313
314
0
        if (!EC_POINT_mul(group, kG, k, NULL, NULL, ctx)
315
0
            || !EC_POINT_get_affine_coordinates(group, kG, x1, NULL,
316
0
                ctx)
317
0
            || !BN_mod_add(r, e, x1, order, ctx)) {
318
0
            ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
319
0
            goto done;
320
0
        }
321
322
        /* try again if r == 0 or r + k == n */
323
0
        if (BN_is_zero(r))
324
0
            continue;
325
326
        /*
327
         * Since 0 < r < n and 0 < k < n, r + k == n is the same as
328
         * k == n - r.  Both operands of the subtraction are public, so
329
         * compute it in the open and then compare against the nonce with a
330
         * fixed-width constant-time comparison.  A BN_cmp() on r + k would
331
         * branch on whether the sum carried into an extra word, which
332
         * depends on the value of k.
333
         */
334
0
        if (!BN_sub(tmp, order, r)
335
0
            || !bn_set_top_fixed(tmp, bn_get_top(order))) {
336
0
            ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
337
0
            goto done;
338
0
        }
339
340
0
        if (CRYPTO_memcmp(bn_get_words(k), bn_get_words(tmp),
341
0
                bn_get_top(order) * sizeof(BN_ULONG))
342
0
            == 0)
343
0
            continue;
344
345
        /*
346
         * s = ((1 + dA)^-1 * (k - r * dA)) mod order
347
         *
348
         * Computed with fixed-top / Montgomery constant-time primitives, so
349
         * that the running time does not depend on the secret k or dA (the
350
         * generic BN_mod_mul()/BN_sub() used previously reduce via BN_div(),
351
         * whose timing is value dependent).  This mirrors the ECDSA path.
352
         *
353
         * s holds (1 + dA)^-1 throughout; the (k - r * dA) term is built in
354
         * tmp.  bn_mul_mont_fixed_top() with one operand in the Montgomery
355
         * domain yields the plain product, and the final
356
         * BN_mod_mul_montgomery() returns the user-visible, normalised value.
357
         */
358
0
        if (!BN_add(s, dA, BN_value_one())
359
0
            || !ossl_ec_group_do_inverse_ord(group, s, s, ctx)
360
0
            || !bn_to_mont_fixed_top(tmp, r, mont, ctx)
361
0
            || !bn_mul_mont_fixed_top(tmp, tmp, dA, mont, ctx)
362
0
            || !bn_mod_sub_fixed_top(tmp, k, tmp, order)
363
0
            || !bn_to_mont_fixed_top(tmp, tmp, mont, ctx)
364
0
            || !BN_mod_mul_montgomery(s, tmp, s, mont, ctx)) {
365
0
            ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
366
0
            goto done;
367
0
        }
368
369
        /* try again if s == 0 */
370
0
        if (BN_is_zero(s))
371
0
            continue;
372
373
0
        sig = ECDSA_SIG_new();
374
0
        if (sig == NULL) {
375
0
            ERR_raise(ERR_LIB_SM2, ERR_R_ECDSA_LIB);
376
0
            goto done;
377
0
        }
378
379
        /* takes ownership of r and s */
380
0
        ECDSA_SIG_set0(sig, r, s);
381
0
        break;
382
0
    }
383
384
0
done:
385
0
    if (sig == NULL) {
386
0
        BN_free(r);
387
0
        BN_free(s);
388
0
    }
389
390
0
    BN_CTX_end(ctx);
391
0
    BN_CTX_free(ctx);
392
0
    EC_POINT_free(kG);
393
0
    return sig;
394
0
}
395
396
static int sm2_sig_verify(const EC_KEY *key, const ECDSA_SIG *sig,
397
    const BIGNUM *e)
398
0
{
399
0
    int ret = 0;
400
0
    const EC_GROUP *group = EC_KEY_get0_group(key);
401
0
    const BIGNUM *order = EC_GROUP_get0_order(group);
402
0
    BN_CTX *ctx = NULL;
403
0
    EC_POINT *pt = NULL;
404
0
    BIGNUM *t = NULL;
405
0
    BIGNUM *x1 = NULL;
406
0
    const BIGNUM *r = NULL;
407
0
    const BIGNUM *s = NULL;
408
0
    OSSL_LIB_CTX *libctx = ossl_ec_key_get_libctx(key);
409
410
0
    ctx = BN_CTX_new_ex(libctx);
411
0
    if (ctx == NULL) {
412
0
        ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
413
0
        goto done;
414
0
    }
415
0
    BN_CTX_start(ctx);
416
0
    t = BN_CTX_get(ctx);
417
0
    x1 = BN_CTX_get(ctx);
418
0
    if (x1 == NULL) {
419
0
        ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
420
0
        goto done;
421
0
    }
422
423
0
    pt = EC_POINT_new(group);
424
0
    if (pt == NULL) {
425
0
        ERR_raise(ERR_LIB_SM2, ERR_R_EC_LIB);
426
0
        goto done;
427
0
    }
428
429
    /*
430
     * B1: verify whether r' in [1,n-1], verification failed if not
431
     * B2: verify whether s' in [1,n-1], verification failed if not
432
     * B3: set M'~=ZA || M'
433
     * B4: calculate e'=Hv(M'~)
434
     * B5: calculate t = (r' + s') modn, verification failed if t=0
435
     * B6: calculate the point (x1', y1')=[s']G + [t]PA
436
     * B7: calculate R=(e'+x1') modn, verification pass if yes, otherwise failed
437
     */
438
439
0
    ECDSA_SIG_get0(sig, &r, &s);
440
441
0
    if (BN_cmp(r, BN_value_one()) < 0
442
0
        || BN_cmp(s, BN_value_one()) < 0
443
0
        || BN_cmp(order, r) <= 0
444
0
        || BN_cmp(order, s) <= 0) {
445
0
        ERR_raise(ERR_LIB_SM2, SM2_R_BAD_SIGNATURE);
446
0
        goto done;
447
0
    }
448
449
0
    if (!BN_mod_add(t, r, s, order, ctx)) {
450
0
        ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
451
0
        goto done;
452
0
    }
453
454
0
    if (BN_is_zero(t)) {
455
0
        ERR_raise(ERR_LIB_SM2, SM2_R_BAD_SIGNATURE);
456
0
        goto done;
457
0
    }
458
459
0
    if (!EC_POINT_mul(group, pt, s, EC_KEY_get0_public_key(key), t, ctx)
460
0
        || !EC_POINT_get_affine_coordinates(group, pt, x1, NULL, ctx)) {
461
0
        ERR_raise(ERR_LIB_SM2, ERR_R_EC_LIB);
462
0
        goto done;
463
0
    }
464
465
0
    if (!BN_mod_add(t, e, x1, order, ctx)) {
466
0
        ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
467
0
        goto done;
468
0
    }
469
470
0
    if (BN_cmp(r, t) == 0)
471
0
        ret = 1;
472
473
0
done:
474
0
    EC_POINT_free(pt);
475
0
    BN_CTX_end(ctx);
476
0
    BN_CTX_free(ctx);
477
0
    return ret;
478
0
}
479
480
ECDSA_SIG *ossl_sm2_do_sign(const EC_KEY *key,
481
    const EVP_MD *digest,
482
    const uint8_t *id,
483
    const size_t id_len,
484
    const uint8_t *msg, size_t msg_len)
485
0
{
486
0
    BIGNUM *e = NULL;
487
0
    ECDSA_SIG *sig = NULL;
488
489
0
    e = sm2_compute_msg_hash(digest, key, id, id_len, msg, msg_len);
490
0
    if (e == NULL) {
491
        /* SM2err already called */
492
0
        goto done;
493
0
    }
494
495
0
    sig = sm2_sig_gen(key, e);
496
497
0
done:
498
0
    BN_free(e);
499
0
    return sig;
500
0
}
501
502
int ossl_sm2_do_verify(const EC_KEY *key,
503
    const EVP_MD *digest,
504
    const ECDSA_SIG *sig,
505
    const uint8_t *id,
506
    const size_t id_len,
507
    const uint8_t *msg, size_t msg_len)
508
0
{
509
0
    BIGNUM *e = NULL;
510
0
    int ret = 0;
511
512
0
    e = sm2_compute_msg_hash(digest, key, id, id_len, msg, msg_len);
513
0
    if (e == NULL) {
514
        /* SM2err already called */
515
0
        goto done;
516
0
    }
517
518
0
    ret = sm2_sig_verify(key, sig, e);
519
520
0
done:
521
0
    BN_free(e);
522
0
    return ret;
523
0
}
524
525
int ossl_sm2_internal_sign(const unsigned char *dgst, int dgstlen,
526
    unsigned char *sig, unsigned int *siglen,
527
    EC_KEY *eckey)
528
0
{
529
0
    BIGNUM *e = NULL;
530
0
    ECDSA_SIG *s = NULL;
531
0
    int sigleni;
532
0
    int ret = -1;
533
534
0
    if (sig == NULL) {
535
0
        ERR_raise(ERR_LIB_SM2, ERR_R_PASSED_NULL_PARAMETER);
536
0
        goto done;
537
0
    }
538
539
0
    e = BN_bin2bn(dgst, dgstlen, NULL);
540
0
    if (e == NULL) {
541
0
        ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
542
0
        goto done;
543
0
    }
544
545
0
    s = sm2_sig_gen(eckey, e);
546
0
    if (s == NULL) {
547
0
        ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
548
0
        goto done;
549
0
    }
550
551
0
    sigleni = i2d_ECDSA_SIG(s, &sig);
552
0
    if (sigleni < 0) {
553
0
        ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
554
0
        goto done;
555
0
    }
556
0
    *siglen = (unsigned int)sigleni;
557
558
0
    ret = 1;
559
560
0
done:
561
0
    ECDSA_SIG_free(s);
562
0
    BN_free(e);
563
0
    return ret;
564
0
}
565
566
int ossl_sm2_internal_verify(const unsigned char *dgst, int dgstlen,
567
    const unsigned char *sig, int sig_len,
568
    EC_KEY *eckey)
569
0
{
570
0
    ECDSA_SIG *s = NULL;
571
0
    BIGNUM *e = NULL;
572
0
    const unsigned char *p = sig;
573
0
    unsigned char *der = NULL;
574
0
    int derlen = -1;
575
0
    int ret = -1;
576
577
0
    s = ECDSA_SIG_new();
578
0
    if (s == NULL) {
579
0
        ERR_raise(ERR_LIB_SM2, ERR_R_ECDSA_LIB);
580
0
        goto done;
581
0
    }
582
0
    if (d2i_ECDSA_SIG(&s, &p, sig_len) == NULL) {
583
0
        ERR_raise(ERR_LIB_SM2, SM2_R_INVALID_ENCODING);
584
0
        goto done;
585
0
    }
586
    /* Ensure signature uses DER and doesn't have trailing garbage */
587
0
    derlen = i2d_ECDSA_SIG(s, &der);
588
0
    if (derlen != sig_len || memcmp(sig, der, derlen) != 0) {
589
0
        ERR_raise(ERR_LIB_SM2, SM2_R_INVALID_ENCODING);
590
0
        goto done;
591
0
    }
592
593
0
    e = BN_bin2bn(dgst, dgstlen, NULL);
594
0
    if (e == NULL) {
595
0
        ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
596
0
        goto done;
597
0
    }
598
599
0
    ret = sm2_sig_verify(eckey, s, e);
600
601
0
done:
602
0
    OPENSSL_free(der);
603
0
    BN_free(e);
604
0
    ECDSA_SIG_free(s);
605
0
    return ret;
606
0
}