/src/libgcrypt/cipher/ecc-sm2.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* ecc-sm2.c - Elliptic Curve SM2 implementation |
2 | | * Copyright (C) 2020 Tianjia Zhang |
3 | | * |
4 | | * This file is part of Libgcrypt. |
5 | | * |
6 | | * Libgcrypt is free software; you can redistribute it and/or modify |
7 | | * it under the terms of the GNU Lesser General Public License as |
8 | | * published by the Free Software Foundation; either version 2.1 of |
9 | | * the License, or (at your option) any later version. |
10 | | * |
11 | | * Libgcrypt is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | | * GNU Lesser General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU Lesser General Public |
17 | | * License along with this program; if not, see <http://www.gnu.org/licenses/>. |
18 | | */ |
19 | | |
20 | | #include <config.h> |
21 | | #include <stdio.h> |
22 | | #include <stdlib.h> |
23 | | #include <string.h> |
24 | | #include <errno.h> |
25 | | |
26 | | #include "g10lib.h" |
27 | | #include "bithelp.h" |
28 | | #include "mpi.h" |
29 | | #include "cipher.h" |
30 | | #include "context.h" |
31 | | #include "ec-context.h" |
32 | | #include "pubkey-internal.h" |
33 | | #include "ecc-common.h" |
34 | | |
35 | 0 | #define MPI_NBYTES(m) ((mpi_get_nbits(m) + 7) / 8) |
36 | | |
37 | | |
38 | | /* Key derivation function from X9.63/SECG */ |
39 | | static gpg_err_code_t |
40 | | kdf_x9_63 (int algo, const void *in, size_t inlen, void *out, size_t outlen) |
41 | 0 | { |
42 | 0 | gpg_err_code_t rc; |
43 | 0 | gcry_md_hd_t hd; |
44 | 0 | int mdlen; |
45 | 0 | u32 counter = 1; |
46 | 0 | u32 counter_be; |
47 | 0 | unsigned char *dgst; |
48 | 0 | unsigned char *pout = out; |
49 | 0 | size_t rlen = outlen; |
50 | 0 | size_t len; |
51 | |
|
52 | 0 | rc = _gcry_md_open (&hd, algo, 0); |
53 | 0 | if (rc) |
54 | 0 | return rc; |
55 | | |
56 | 0 | mdlen = _gcry_md_get_algo_dlen (algo); |
57 | |
|
58 | 0 | while (rlen > 0) |
59 | 0 | { |
60 | 0 | counter_be = be_bswap32 (counter); /* cpu_to_be32 */ |
61 | 0 | counter++; |
62 | |
|
63 | 0 | _gcry_md_write (hd, in, inlen); |
64 | 0 | _gcry_md_write (hd, &counter_be, sizeof(counter_be)); |
65 | |
|
66 | 0 | dgst = _gcry_md_read (hd, algo); |
67 | 0 | if (dgst == NULL) |
68 | 0 | { |
69 | 0 | rc = GPG_ERR_DIGEST_ALGO; |
70 | 0 | break; |
71 | 0 | } |
72 | | |
73 | 0 | len = mdlen < rlen ? mdlen : rlen; /* min(mdlen, rlen) */ |
74 | 0 | memcpy (pout, dgst, len); |
75 | 0 | rlen -= len; |
76 | 0 | pout += len; |
77 | |
|
78 | 0 | _gcry_md_reset (hd); |
79 | 0 | } |
80 | |
|
81 | 0 | _gcry_md_close (hd); |
82 | 0 | return rc; |
83 | 0 | } |
84 | | |
85 | | |
86 | | /* _gcry_ecc_sm2_encrypt description: |
87 | | * input: |
88 | | * data[0] : octet string |
89 | | * output: A new S-expression with the parameters: |
90 | | * a: c1 : generated ephemeral public key (kG) |
91 | | * b: c3 : Hash(x2 || IN || y2) |
92 | | * c: c2 : cipher |
93 | | * |
94 | | * sm2_decrypt description: |
95 | | * in contrast to encrypt |
96 | | */ |
97 | | gpg_err_code_t |
98 | | _gcry_ecc_sm2_encrypt (gcry_sexp_t *r_ciph, gcry_mpi_t input, mpi_ec_t ec) |
99 | 0 | { |
100 | 0 | gpg_err_code_t rc; |
101 | 0 | const int algo = GCRY_MD_SM3; |
102 | 0 | gcry_md_hd_t md = NULL; |
103 | 0 | int mdlen; |
104 | 0 | unsigned char *dgst; |
105 | 0 | gcry_mpi_t k = NULL; |
106 | 0 | mpi_point_struct kG, kP; |
107 | 0 | gcry_mpi_t x1, y1; |
108 | 0 | gcry_mpi_t x2, y2; |
109 | 0 | gcry_mpi_t x2y2 = NULL; |
110 | 0 | unsigned char *in = NULL; |
111 | 0 | unsigned int inlen; |
112 | 0 | unsigned char *raw; |
113 | 0 | unsigned int rawlen; |
114 | 0 | unsigned char *cipher = NULL; |
115 | 0 | int i; |
116 | |
|
117 | 0 | point_init (&kG); |
118 | 0 | point_init (&kP); |
119 | 0 | x1 = mpi_new (0); |
120 | 0 | y1 = mpi_new (0); |
121 | 0 | x2 = mpi_new (0); |
122 | 0 | y2 = mpi_new (0); |
123 | |
|
124 | 0 | in = _gcry_mpi_get_buffer (input, 0, &inlen, NULL); |
125 | 0 | if (!in) |
126 | 0 | { |
127 | 0 | rc = gpg_err_code_from_syserror (); |
128 | 0 | goto leave; |
129 | 0 | } |
130 | | |
131 | 0 | cipher = xtrymalloc (inlen); |
132 | 0 | if (!cipher) |
133 | 0 | { |
134 | 0 | rc = gpg_err_code_from_syserror (); |
135 | 0 | goto leave; |
136 | 0 | } |
137 | | |
138 | | /* rand k in [1, n-1] */ |
139 | 0 | k = _gcry_dsa_gen_k (ec->n, GCRY_VERY_STRONG_RANDOM); |
140 | | |
141 | | /* [k]G = (x1, y1) */ |
142 | 0 | _gcry_mpi_ec_mul_point (&kG, k, ec->G, ec); |
143 | 0 | if (_gcry_mpi_ec_get_affine (x1, y1, &kG, ec)) |
144 | 0 | { |
145 | 0 | if (DBG_CIPHER) |
146 | 0 | log_debug ("Bad check: kG can not be a Point at Infinity!\n"); |
147 | 0 | rc = GPG_ERR_INV_DATA; |
148 | 0 | goto leave; |
149 | 0 | } |
150 | | |
151 | | /* [k]P = (x2, y2) */ |
152 | 0 | _gcry_mpi_ec_mul_point (&kP, k, ec->Q, ec); |
153 | 0 | if (_gcry_mpi_ec_get_affine (x2, y2, &kP, ec)) |
154 | 0 | { |
155 | 0 | rc = GPG_ERR_INV_DATA; |
156 | 0 | goto leave; |
157 | 0 | } |
158 | | |
159 | | /* t = KDF(x2 || y2, klen) */ |
160 | 0 | x2y2 = _gcry_mpi_ec_ec2os (&kP, ec); |
161 | 0 | raw = mpi_get_opaque (x2y2, &rawlen); |
162 | 0 | rawlen = (rawlen + 7) / 8; |
163 | | |
164 | | /* skip the prefix '0x04' */ |
165 | 0 | raw += 1; |
166 | 0 | rawlen -= 1; |
167 | 0 | rc = kdf_x9_63 (algo, raw, rawlen, cipher, inlen); |
168 | 0 | if (rc) |
169 | 0 | goto leave; |
170 | | |
171 | | /* cipher = t xor in */ |
172 | 0 | for (i = 0; i < inlen; i++) |
173 | 0 | cipher[i] ^= in[i]; |
174 | | |
175 | | /* hash(x2 || IN || y2) */ |
176 | 0 | mdlen = _gcry_md_get_algo_dlen (algo); |
177 | 0 | rc = _gcry_md_open (&md, algo, 0); |
178 | 0 | if (rc) |
179 | 0 | goto leave; |
180 | 0 | _gcry_md_write (md, raw, MPI_NBYTES(x2)); |
181 | 0 | _gcry_md_write (md, in, inlen); |
182 | 0 | _gcry_md_write (md, raw + MPI_NBYTES(x2), MPI_NBYTES(y2)); |
183 | 0 | dgst = _gcry_md_read (md, algo); |
184 | 0 | if (dgst == NULL) |
185 | 0 | { |
186 | 0 | rc = GPG_ERR_DIGEST_ALGO; |
187 | 0 | goto leave; |
188 | 0 | } |
189 | | |
190 | 0 | if (!rc) |
191 | 0 | { |
192 | 0 | gcry_mpi_t c1; |
193 | 0 | gcry_mpi_t c3; |
194 | 0 | gcry_mpi_t c2; |
195 | |
|
196 | 0 | c3 = mpi_new (0); |
197 | 0 | c2 = mpi_new (0); |
198 | |
|
199 | 0 | c1 = _gcry_ecc_ec2os (x1, y1, ec->p); |
200 | 0 | _gcry_mpi_set_opaque_copy (c3, dgst, mdlen * 8); |
201 | 0 | _gcry_mpi_set_opaque_copy (c2, cipher, inlen * 8); |
202 | |
|
203 | 0 | rc = sexp_build (r_ciph, NULL, |
204 | 0 | "(enc-val(flags sm2)(sm2(a%M)(b%M)(c%M)))", |
205 | 0 | c1, c3, c2); |
206 | |
|
207 | 0 | mpi_free (c1); |
208 | 0 | mpi_free (c3); |
209 | 0 | mpi_free (c2); |
210 | 0 | } |
211 | |
|
212 | 0 | leave: |
213 | 0 | _gcry_md_close (md); |
214 | 0 | mpi_free (x2y2); |
215 | 0 | mpi_free (k); |
216 | |
|
217 | 0 | point_free (&kG); |
218 | 0 | point_free (&kP); |
219 | 0 | mpi_free (x1); |
220 | 0 | mpi_free (y1); |
221 | 0 | mpi_free (x2); |
222 | 0 | mpi_free (y2); |
223 | |
|
224 | 0 | xfree (cipher); |
225 | 0 | xfree (in); |
226 | |
|
227 | 0 | return rc; |
228 | 0 | } |
229 | | |
230 | | |
231 | | gpg_err_code_t |
232 | | _gcry_ecc_sm2_decrypt (gcry_sexp_t *r_plain, gcry_sexp_t data_list, mpi_ec_t ec) |
233 | 0 | { |
234 | 0 | gpg_err_code_t rc; |
235 | 0 | gcry_mpi_t data_c1 = NULL; |
236 | 0 | gcry_mpi_t data_c3 = NULL; |
237 | 0 | gcry_mpi_t data_c2 = NULL; |
238 | | |
239 | | /* |
240 | | * Extract the data. |
241 | | */ |
242 | 0 | rc = sexp_extract_param (data_list, NULL, "/a/b/c", |
243 | 0 | &data_c1, &data_c3, &data_c2, NULL); |
244 | 0 | if (rc) |
245 | 0 | goto leave; |
246 | 0 | if (DBG_CIPHER) |
247 | 0 | { |
248 | 0 | log_printmpi ("ecc_decrypt d_c1", data_c1); |
249 | 0 | log_printmpi ("ecc_decrypt d_c3", data_c3); |
250 | 0 | log_printmpi ("ecc_decrypt d_c2", data_c2); |
251 | 0 | } |
252 | |
|
253 | 0 | { |
254 | 0 | const int algo = GCRY_MD_SM3; |
255 | 0 | gcry_md_hd_t md = NULL; |
256 | 0 | int mdlen; |
257 | 0 | unsigned char *dgst; |
258 | 0 | mpi_point_struct c1; |
259 | 0 | mpi_point_struct kP; |
260 | 0 | gcry_mpi_t x2, y2; |
261 | 0 | gcry_mpi_t x2y2 = NULL; |
262 | 0 | unsigned char *in = NULL; |
263 | 0 | unsigned int inlen; |
264 | 0 | unsigned char *plain = NULL; |
265 | 0 | unsigned char *raw; |
266 | 0 | unsigned int rawlen; |
267 | 0 | unsigned char *c3 = NULL; |
268 | 0 | unsigned int c3_len; |
269 | 0 | int i; |
270 | |
|
271 | 0 | point_init (&c1); |
272 | 0 | point_init (&kP); |
273 | 0 | x2 = mpi_new (0); |
274 | 0 | y2 = mpi_new (0); |
275 | |
|
276 | 0 | in = mpi_get_opaque (data_c2, &inlen); |
277 | 0 | inlen = (inlen + 7) / 8; |
278 | 0 | plain = xtrymalloc (inlen); |
279 | 0 | if (!plain) |
280 | 0 | { |
281 | 0 | rc = gpg_err_code_from_syserror (); |
282 | 0 | goto leave_main; |
283 | 0 | } |
284 | | |
285 | 0 | rc = _gcry_ecc_sec_decodepoint (data_c1, ec, &c1); |
286 | 0 | if (rc) |
287 | 0 | goto leave_main; |
288 | | |
289 | 0 | if (!_gcry_mpi_ec_curve_point (&c1, ec)) |
290 | 0 | { |
291 | 0 | rc = GPG_ERR_INV_DATA; |
292 | 0 | goto leave_main; |
293 | 0 | } |
294 | | |
295 | | /* [d]C1 = (x2, y2), C1 = [k]G */ |
296 | 0 | _gcry_mpi_ec_mul_point (&kP, ec->d, &c1, ec); |
297 | 0 | if (_gcry_mpi_ec_get_affine (x2, y2, &kP, ec)) |
298 | 0 | { |
299 | 0 | rc = GPG_ERR_INV_DATA; |
300 | 0 | goto leave_main; |
301 | 0 | } |
302 | | |
303 | | /* t = KDF(x2 || y2, inlen) */ |
304 | 0 | x2y2 = _gcry_mpi_ec_ec2os (&kP, ec); |
305 | 0 | raw = mpi_get_opaque (x2y2, &rawlen); |
306 | 0 | rawlen = (rawlen + 7) / 8; |
307 | | /* skip the prefix '0x04' */ |
308 | 0 | raw += 1; |
309 | 0 | rawlen -= 1; |
310 | 0 | rc = kdf_x9_63 (algo, raw, rawlen, plain, inlen); |
311 | 0 | if (rc) |
312 | 0 | goto leave_main; |
313 | | |
314 | | /* plain = C2 xor t */ |
315 | 0 | for (i = 0; i < inlen; i++) |
316 | 0 | plain[i] ^= in[i]; |
317 | | |
318 | | /* Hash(x2 || IN || y2) == C3 */ |
319 | 0 | mdlen = _gcry_md_get_algo_dlen (algo); |
320 | 0 | rc = _gcry_md_open (&md, algo, 0); |
321 | 0 | if (rc) |
322 | 0 | goto leave_main; |
323 | 0 | _gcry_md_write (md, raw, MPI_NBYTES(x2)); |
324 | 0 | _gcry_md_write (md, plain, inlen); |
325 | 0 | _gcry_md_write (md, raw + MPI_NBYTES(x2), MPI_NBYTES(y2)); |
326 | 0 | dgst = _gcry_md_read (md, algo); |
327 | 0 | if (dgst == NULL) |
328 | 0 | { |
329 | 0 | memset (plain, 0, inlen); |
330 | 0 | rc = GPG_ERR_DIGEST_ALGO; |
331 | 0 | goto leave_main; |
332 | 0 | } |
333 | 0 | c3 = mpi_get_opaque (data_c3, &c3_len); |
334 | 0 | c3_len = (c3_len + 7) / 8; |
335 | 0 | if (c3_len != mdlen || memcmp (dgst, c3, c3_len) != 0) |
336 | 0 | { |
337 | 0 | memset (plain, 0, inlen); |
338 | 0 | rc = GPG_ERR_INV_DATA; |
339 | 0 | goto leave_main; |
340 | 0 | } |
341 | | |
342 | 0 | if (!rc) |
343 | 0 | { |
344 | 0 | gcry_mpi_t r; |
345 | |
|
346 | 0 | r = mpi_new (inlen * 8); |
347 | 0 | _gcry_mpi_set_buffer (r, plain, inlen, 0); |
348 | |
|
349 | 0 | rc = sexp_build (r_plain, NULL, "(value %m)", r); |
350 | |
|
351 | 0 | mpi_free (r); |
352 | 0 | } |
353 | |
|
354 | 0 | leave_main: |
355 | 0 | _gcry_md_close (md); |
356 | 0 | mpi_free (x2y2); |
357 | 0 | xfree (plain); |
358 | |
|
359 | 0 | point_free (&c1); |
360 | 0 | point_free (&kP); |
361 | 0 | mpi_free (x2); |
362 | 0 | mpi_free (y2); |
363 | 0 | } |
364 | | |
365 | 0 | leave: |
366 | 0 | _gcry_mpi_release (data_c1); |
367 | 0 | _gcry_mpi_release (data_c3); |
368 | 0 | _gcry_mpi_release (data_c2); |
369 | |
|
370 | 0 | return rc; |
371 | 0 | } |
372 | | |
373 | | |
374 | | /* Compute an SM2 signature. |
375 | | * Return the signature struct (r,s) from the message hash. The caller |
376 | | * must have allocated R and S. |
377 | | */ |
378 | | gpg_err_code_t |
379 | | _gcry_ecc_sm2_sign (gcry_mpi_t input, mpi_ec_t ec, |
380 | | gcry_mpi_t r, gcry_mpi_t s, |
381 | | int flags, int hashalgo) |
382 | 0 | { |
383 | 0 | gpg_err_code_t rc = 0; |
384 | 0 | int extraloops = 0; |
385 | 0 | gcry_mpi_t hash; |
386 | 0 | const void *abuf; |
387 | 0 | unsigned int abits, qbits; |
388 | 0 | gcry_mpi_t tmp = NULL; |
389 | 0 | gcry_mpi_t k = NULL; |
390 | 0 | gcry_mpi_t rk = NULL; |
391 | 0 | mpi_point_struct kG; |
392 | 0 | gcry_mpi_t x1; |
393 | |
|
394 | 0 | if (DBG_CIPHER) |
395 | 0 | log_mpidump ("sm2 sign hash ", input); |
396 | |
|
397 | 0 | qbits = mpi_get_nbits (ec->n); |
398 | | |
399 | | /* Convert the INPUT into an MPI if needed. */ |
400 | 0 | rc = _gcry_dsa_normalize_hash (input, &hash, qbits); |
401 | 0 | if (rc) |
402 | 0 | return rc; |
403 | | |
404 | 0 | point_init (&kG); |
405 | 0 | x1 = mpi_new (0); |
406 | 0 | rk = mpi_new (0); |
407 | 0 | tmp = mpi_new (0); |
408 | |
|
409 | 0 | for (;;) |
410 | 0 | { |
411 | | /* rand k in [1, n-1] */ |
412 | 0 | if ((flags & PUBKEY_FLAG_RFC6979) && hashalgo) |
413 | 0 | { |
414 | | /* Use Pornin's method for deterministic DSA. If this |
415 | | flag is set, it is expected that HASH is an opaque |
416 | | MPI with the to be signed hash. That hash is also |
417 | | used as h1 from 3.2.a. */ |
418 | 0 | if (!mpi_is_opaque (input)) |
419 | 0 | { |
420 | 0 | rc = GPG_ERR_CONFLICT; |
421 | 0 | goto leave; |
422 | 0 | } |
423 | | |
424 | 0 | abuf = mpi_get_opaque (input, &abits); |
425 | 0 | rc = _gcry_dsa_gen_rfc6979_k (&k, ec->n, ec->d, |
426 | 0 | abuf, (abits+7)/8, |
427 | 0 | hashalgo, extraloops); |
428 | 0 | if (rc) |
429 | 0 | goto leave; |
430 | 0 | extraloops++; |
431 | 0 | } |
432 | 0 | else |
433 | 0 | k = _gcry_dsa_gen_k (ec->n, GCRY_VERY_STRONG_RANDOM); |
434 | | |
435 | 0 | _gcry_dsa_modify_k (k, ec->n, qbits); |
436 | | |
437 | | /* [k]G = (x1, y1) */ |
438 | 0 | _gcry_mpi_ec_mul_point (&kG, k, ec->G, ec); |
439 | 0 | if (_gcry_mpi_ec_get_affine (x1, NULL, &kG, ec)) |
440 | 0 | { |
441 | 0 | rc = GPG_ERR_INV_DATA; |
442 | 0 | goto leave; |
443 | 0 | } |
444 | | |
445 | | /* r = (e + x1) % n */ |
446 | 0 | mpi_addm (r, hash, x1, ec->n); |
447 | | |
448 | | /* r != 0 && r + k != n */ |
449 | 0 | if (mpi_cmp_ui (r, 0) == 0) |
450 | 0 | continue; |
451 | 0 | mpi_add (rk, r, k); |
452 | 0 | if (mpi_cmp (rk, ec->n) == 0) |
453 | 0 | continue; |
454 | | |
455 | | /* s = ((d + 1)^-1 * (k - rd)) % n */ |
456 | 0 | mpi_addm (s, ec->d, GCRYMPI_CONST_ONE, ec->n); |
457 | 0 | mpi_invm (s, s, ec->n); |
458 | 0 | mpi_mulm (tmp, r, ec->d, ec->n); |
459 | 0 | mpi_subm (tmp, k, tmp, ec->n); |
460 | 0 | mpi_mulm (s, s, tmp, ec->n); |
461 | | |
462 | | /* s != 0 */ |
463 | 0 | if (mpi_cmp_ui (s, 0) == 0) |
464 | 0 | continue; |
465 | | |
466 | 0 | break; /* Okay */ |
467 | 0 | } |
468 | | |
469 | 0 | if (DBG_CIPHER) |
470 | 0 | { |
471 | 0 | log_mpidump ("sm2 sign result r ", r); |
472 | 0 | log_mpidump ("sm2 sign result s ", s); |
473 | 0 | } |
474 | |
|
475 | 0 | leave: |
476 | 0 | point_free (&kG); |
477 | 0 | mpi_free (k); |
478 | 0 | mpi_free (x1); |
479 | 0 | mpi_free (rk); |
480 | 0 | mpi_free (tmp); |
481 | |
|
482 | 0 | if (hash != input) |
483 | 0 | mpi_free (hash); |
484 | |
|
485 | 0 | return rc; |
486 | 0 | } |
487 | | |
488 | | |
489 | | /* Verify an SM2 signature. |
490 | | * Check if R and S verifies INPUT. |
491 | | */ |
492 | | gpg_err_code_t |
493 | | _gcry_ecc_sm2_verify (gcry_mpi_t input, mpi_ec_t ec, |
494 | | gcry_mpi_t r, gcry_mpi_t s) |
495 | 0 | { |
496 | 0 | gpg_err_code_t err = 0; |
497 | 0 | gcry_mpi_t hash = NULL; |
498 | 0 | gcry_mpi_t t = NULL; |
499 | 0 | mpi_point_struct sG, tP; |
500 | 0 | gcry_mpi_t x1, y1; |
501 | 0 | unsigned int nbits; |
502 | |
|
503 | 0 | if (!_gcry_mpi_ec_curve_point (ec->Q, ec)) |
504 | 0 | return GPG_ERR_BROKEN_PUBKEY; |
505 | | |
506 | | /* r, s within [1, n-1] */ |
507 | 0 | if (mpi_cmp_ui (r, 1) < 0 || mpi_cmp (r, ec->n) > 0) |
508 | 0 | return GPG_ERR_BAD_SIGNATURE; |
509 | 0 | if (mpi_cmp_ui (s, 1) < 0 || mpi_cmp (s, ec->n) > 0) |
510 | 0 | return GPG_ERR_BAD_SIGNATURE; |
511 | | |
512 | 0 | nbits = mpi_get_nbits (ec->n); |
513 | 0 | err = _gcry_dsa_normalize_hash (input, &hash, nbits); |
514 | 0 | if (err) |
515 | 0 | return err; |
516 | | |
517 | 0 | point_init (&sG); |
518 | 0 | point_init (&tP); |
519 | 0 | x1 = mpi_new (0); |
520 | 0 | y1 = mpi_new (0); |
521 | 0 | t = mpi_new (0); |
522 | | |
523 | | /* t = (r + s) % n, t != 0 */ |
524 | 0 | mpi_addm (t, r, s, ec->n); |
525 | 0 | if (mpi_cmp_ui (t, 0) == 0) |
526 | 0 | { |
527 | 0 | err = GPG_ERR_BAD_SIGNATURE; |
528 | 0 | goto leave; |
529 | 0 | } |
530 | | |
531 | | /* sG + tP = (x1, y1) */ |
532 | 0 | _gcry_mpi_ec_mul_point (&sG, s, ec->G, ec); |
533 | 0 | _gcry_mpi_ec_mul_point (&tP, t, ec->Q, ec); |
534 | 0 | _gcry_mpi_ec_add_points (&sG, &sG, &tP, ec); |
535 | 0 | if (_gcry_mpi_ec_get_affine (x1, y1, &sG, ec)) |
536 | 0 | { |
537 | 0 | err = GPG_ERR_INV_DATA; |
538 | 0 | goto leave; |
539 | 0 | } |
540 | | |
541 | | /* R = (e + x1) % n */ |
542 | 0 | mpi_addm (t, hash, x1, ec->n); |
543 | | |
544 | | /* check R == r */ |
545 | 0 | if (mpi_cmp (t, r)) |
546 | 0 | { |
547 | 0 | if (DBG_CIPHER) |
548 | 0 | { |
549 | 0 | log_mpidump (" R", t); |
550 | 0 | log_mpidump (" r", r); |
551 | 0 | log_mpidump (" s", s); |
552 | 0 | } |
553 | 0 | err = GPG_ERR_BAD_SIGNATURE; |
554 | 0 | goto leave; |
555 | 0 | } |
556 | 0 | if (DBG_CIPHER) |
557 | 0 | log_debug ("sm2 verify: Accepted\n"); |
558 | |
|
559 | 0 | leave: |
560 | 0 | point_free (&sG); |
561 | 0 | point_free (&tP); |
562 | 0 | mpi_free (x1); |
563 | 0 | mpi_free (y1); |
564 | 0 | mpi_free (t); |
565 | 0 | if (hash != input) |
566 | 0 | mpi_free (hash); |
567 | |
|
568 | 0 | return err; |
569 | 0 | } |