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