| 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 |
#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 |
{ |
| 42 |
gpg_err_code_t rc; |
| 43 |
gcry_md_hd_t hd; |
| 44 |
int mdlen; |
| 45 |
u32 counter = 1; |
| 46 |
u32 counter_be; |
| 47 |
unsigned char *dgst; |
| 48 |
unsigned char *pout = out; |
| 49 |
size_t rlen = outlen; |
| 50 |
size_t len; |
| 51 |
|
| 52 |
rc = _gcry_md_open (&hd, algo, 0); |
| 53 |
if (rc) |
| 54 |
return rc; |
| 55 |
|
| 56 |
mdlen = _gcry_md_get_algo_dlen (algo); |
| 57 |
|
| 58 |
while (rlen > 0) |
| 59 |
{ |
| 60 |
counter_be = be_bswap32 (counter); /* cpu_to_be32 */ |
| 61 |
counter++; |
| 62 |
|
| 63 |
_gcry_md_write (hd, in, inlen); |
| 64 |
_gcry_md_write (hd, &counter_be, sizeof(counter_be)); |
| 65 |
|
| 66 |
dgst = _gcry_md_read (hd, algo); |
| 67 |
if (dgst == NULL) |
| 68 |
{ |
| 69 |
rc = GPG_ERR_DIGEST_ALGO; |
| 70 |
break; |
| 71 |
} |
| 72 |
|
| 73 |
len = mdlen < rlen ? mdlen : rlen; /* min(mdlen, rlen) */ |
| 74 |
memcpy (pout, dgst, len); |
| 75 |
rlen -= len; |
| 76 |
pout += len; |
| 77 |
|
| 78 |
_gcry_md_reset (hd); |
| 79 |
} |
| 80 |
|
| 81 |
_gcry_md_close (hd); |
| 82 |
return rc; |
| 83 |
} |
| 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 |
{ |
| 100 |
gpg_err_code_t rc; |
| 101 |
const int algo = GCRY_MD_SM3; |
| 102 |
gcry_md_hd_t md = NULL; |
| 103 |
int mdlen; |
| 104 |
unsigned char *dgst; |
| 105 |
gcry_mpi_t k = NULL; |
| 106 |
mpi_point_struct kG, kP; |
| 107 |
gcry_mpi_t x1, y1; |
| 108 |
gcry_mpi_t x2, y2; |
| 109 |
gcry_mpi_t x2y2 = NULL; |
| 110 |
unsigned char *in = NULL; |
| 111 |
unsigned int inlen; |
| 112 |
unsigned char *raw; |
| 113 |
unsigned int rawlen; |
| 114 |
unsigned char *cipher = NULL; |
| 115 |
int i; |
| 116 |
|
| 117 |
point_init (&kG, ec->nbits); |
| 118 |
point_init (&kP, ec->nbits); |
| 119 |
x1 = mpi_new (ec->nbits); |
| 120 |
y1 = mpi_new (ec->nbits); |
| 121 |
x2 = mpi_new (ec->nbits); |
| 122 |
y2 = mpi_new (ec->nbits); |
| 123 |
|
| 124 |
in = _gcry_mpi_get_buffer (input, 0, &inlen, NULL); |
| 125 |
if (!in) |
| 126 |
{ |
| 127 |
rc = gpg_err_code_from_syserror (); |
| 128 |
goto leave; |
| 129 |
} |
| 130 |
|
| 131 |
cipher = xtrymalloc (inlen); |
| 132 |
if (!cipher) |
| 133 |
{ |
| 134 |
rc = gpg_err_code_from_syserror (); |
| 135 |
goto leave; |
| 136 |
} |
| 137 |
|
| 138 |
/* rand k in [1, n-1] */ |
| 139 |
k = _gcry_dsa_gen_k (ec->n, GCRY_VERY_STRONG_RANDOM); |
| 140 |
|
| 141 |
/* [k]G = (x1, y1) */ |
| 142 |
_gcry_mpi_ec_mul_point (&kG, k, ec->G, ec); |
| 143 |
if (_gcry_mpi_ec_get_affine (x1, y1, &kG, ec)) |
| 144 |
{ |
| 145 |
if (DBG_CIPHER) |
| 146 |
log_debug ("Bad check: kG can not be a Point at Infinity!\n"); |
| 147 |
rc = GPG_ERR_INV_DATA; |
| 148 |
goto leave; |
| 149 |
} |
| 150 |
|
| 151 |
/* [k]P = (x2, y2) */ |
| 152 |
_gcry_mpi_ec_mul_point (&kP, k, ec->Q, ec); |
| 153 |
if (_gcry_mpi_ec_get_affine (x2, y2, &kP, ec)) |
| 154 |
{ |
| 155 |
rc = GPG_ERR_INV_DATA; |
| 156 |
goto leave; |
| 157 |
} |
| 158 |
|
| 159 |
/* t = KDF(x2 || y2, klen) */ |
| 160 |
x2y2 = _gcry_mpi_ec_ec2os (&kP, ec); |
| 161 |
raw = mpi_get_opaque (x2y2, &rawlen); |
| 162 |
rawlen = (rawlen + 7) / 8; |
| 163 |
|
| 164 |
/* skip the prefix '0x04' */ |
| 165 |
raw += 1; |
| 166 |
rawlen -= 1; |
| 167 |
rc = kdf_x9_63 (algo, raw, rawlen, cipher, inlen); |
| 168 |
if (rc) |
| 169 |
goto leave; |
| 170 |
|
| 171 |
/* cipher = t xor in */ |
| 172 |
for (i = 0; i < inlen; i++) |
| 173 |
cipher[i] ^= in[i]; |
| 174 |
|
| 175 |
/* hash(x2 || IN || y2) */ |
| 176 |
mdlen = _gcry_md_get_algo_dlen (algo); |
| 177 |
rc = _gcry_md_open (&md, algo, 0); |
| 178 |
if (rc) |
| 179 |
goto leave; |
| 180 |
_gcry_md_write (md, raw, MPI_NBYTES(x2)); |
| 181 |
_gcry_md_write (md, in, inlen); |
| 182 |
_gcry_md_write (md, raw + MPI_NBYTES(x2), MPI_NBYTES(y2)); |
| 183 |
dgst = _gcry_md_read (md, algo); |
| 184 |
if (dgst == NULL) |
| 185 |
{ |
| 186 |
rc = GPG_ERR_DIGEST_ALGO; |
| 187 |
goto leave; |
| 188 |
} |
| 189 |
|
| 190 |
if (!rc) |
| 191 |
{ |
| 192 |
gcry_mpi_t c1; |
| 193 |
gcry_mpi_t c3; |
| 194 |
gcry_mpi_t c2; |
| 195 |
|
| 196 |
c3 = mpi_new (0); |
| 197 |
c2 = mpi_new (0); |
| 198 |
|
| 199 |
c1 = _gcry_ecc_ec2os (x1, y1, ec->p); |
| 200 |
_gcry_mpi_set_opaque_copy (c3, dgst, mdlen * 8); |
| 201 |
_gcry_mpi_set_opaque_copy (c2, cipher, inlen * 8); |
| 202 |
|
| 203 |
rc = sexp_build (r_ciph, NULL, |
| 204 |
"(enc-val(flags sm2)(sm2(a%M)(b%M)(c%M)))", |
| 205 |
c1, c3, c2); |
| 206 |
|
| 207 |
mpi_free (c1); |
| 208 |
mpi_free (c3); |
| 209 |
mpi_free (c2); |
| 210 |
} |
| 211 |
|
| 212 |
leave: |
| 213 |
_gcry_md_close (md); |
| 214 |
mpi_free (x2y2); |
| 215 |
mpi_free (k); |
| 216 |
|
| 217 |
point_free (&kG); |
| 218 |
point_free (&kP); |
| 219 |
mpi_free (x1); |
| 220 |
mpi_free (y1); |
| 221 |
mpi_free (x2); |
| 222 |
mpi_free (y2); |
| 223 |
|
| 224 |
xfree (cipher); |
| 225 |
xfree (in); |
| 226 |
|
| 227 |
return rc; |
| 228 |
} |
| 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 |
{ |
| 234 |
gpg_err_code_t rc; |
| 235 |
gcry_mpi_t data_c1 = NULL; |
| 236 |
gcry_mpi_t data_c3 = NULL; |
| 237 |
gcry_mpi_t data_c2 = NULL; |
| 238 |
|
| 239 |
/* |
| 240 |
* Extract the data. |
| 241 |
*/ |
| 242 |
rc = sexp_extract_param (data_list, NULL, "/a/b/c", |
| 243 |
&data_c1, &data_c3, &data_c2, NULL); |
| 244 |
if (rc) |
| 245 |
goto leave; |
| 246 |
if (DBG_CIPHER) |
| 247 |
{ |
| 248 |
log_printmpi ("ecc_decrypt d_c1", data_c1); |
| 249 |
log_printmpi ("ecc_decrypt d_c3", data_c3); |
| 250 |
log_printmpi ("ecc_decrypt d_c2", data_c2); |
| 251 |
} |
| 252 |
|
| 253 |
{ |
| 254 |
const int algo = GCRY_MD_SM3; |
| 255 |
gcry_md_hd_t md = NULL; |
| 256 |
int mdlen; |
| 257 |
unsigned char *dgst; |
| 258 |
mpi_point_struct c1; |
| 259 |
mpi_point_struct kP; |
| 260 |
gcry_mpi_t x2, y2; |
| 261 |
gcry_mpi_t x2y2 = NULL; |
| 262 |
unsigned char *in = NULL; |
| 263 |
unsigned int inlen; |
| 264 |
unsigned char *plain = NULL; |
| 265 |
unsigned char *raw; |
| 266 |
unsigned int rawlen; |
| 267 |
unsigned char *c3 = NULL; |
| 268 |
unsigned int c3_len; |
| 269 |
int i; |
| 270 |
|
| 271 |
point_init (&c1, ec->nbits); |
| 272 |
point_init (&kP, ec->nbits); |
| 273 |
x2 = mpi_new (ec->nbits); |
| 274 |
y2 = mpi_new (ec->nbits); |
| 275 |
|
| 276 |
in = mpi_get_opaque (data_c2, &inlen); |
| 277 |
inlen = (inlen + 7) / 8; |
| 278 |
plain = xtrymalloc (inlen); |
| 279 |
if (!plain) |
| 280 |
{ |
| 281 |
rc = gpg_err_code_from_syserror (); |
| 282 |
goto leave_main; |
| 283 |
} |
| 284 |
|
| 285 |
rc = _gcry_ecc_sec_decodepoint (data_c1, ec, &c1); |
| 286 |
if (rc) |
| 287 |
goto leave_main; |
| 288 |
|
| 289 |
if (!_gcry_mpi_ec_curve_point (&c1, ec)) |
| 290 |
{ |
| 291 |
rc = GPG_ERR_INV_DATA; |
| 292 |
goto leave_main; |
| 293 |
} |
| 294 |
|
| 295 |
/* [d]C1 = (x2, y2), C1 = [k]G */ |
| 296 |
_gcry_mpi_ec_mul_point (&kP, ec->d, &c1, ec); |
| 297 |
if (_gcry_mpi_ec_get_affine (x2, y2, &kP, ec)) |
| 298 |
{ |
| 299 |
rc = GPG_ERR_INV_DATA; |
| 300 |
goto leave_main; |
| 301 |
} |
| 302 |
|
| 303 |
/* t = KDF(x2 || y2, inlen) */ |
| 304 |
x2y2 = _gcry_mpi_ec_ec2os (&kP, ec); |
| 305 |
raw = mpi_get_opaque (x2y2, &rawlen); |
| 306 |
rawlen = (rawlen + 7) / 8; |
| 307 |
/* skip the prefix '0x04' */ |
| 308 |
raw += 1; |
| 309 |
rawlen -= 1; |
| 310 |
rc = kdf_x9_63 (algo, raw, rawlen, plain, inlen); |
| 311 |
if (rc) |
| 312 |
goto leave_main; |
| 313 |
|
| 314 |
/* plain = C2 xor t */ |
| 315 |
for (i = 0; i < inlen; i++) |
| 316 |
plain[i] ^= in[i]; |
| 317 |
|
| 318 |
/* Hash(x2 || IN || y2) == C3 */ |
| 319 |
mdlen = _gcry_md_get_algo_dlen (algo); |
| 320 |
rc = _gcry_md_open (&md, algo, 0); |
| 321 |
if (rc) |
| 322 |
goto leave_main; |
| 323 |
_gcry_md_write (md, raw, MPI_NBYTES(x2)); |
| 324 |
_gcry_md_write (md, plain, inlen); |
| 325 |
_gcry_md_write (md, raw + MPI_NBYTES(x2), MPI_NBYTES(y2)); |
| 326 |
dgst = _gcry_md_read (md, algo); |
| 327 |
if (dgst == NULL) |
| 328 |
{ |
| 329 |
memset (plain, 0, inlen); |
| 330 |
rc = GPG_ERR_DIGEST_ALGO; |
| 331 |
goto leave_main; |
| 332 |
} |
| 333 |
c3 = mpi_get_opaque (data_c3, &c3_len); |
| 334 |
c3_len = (c3_len + 7) / 8; |
| 335 |
if (c3_len != mdlen || memcmp (dgst, c3, c3_len) != 0) |
| 336 |
{ |
| 337 |
memset (plain, 0, inlen); |
| 338 |
rc = GPG_ERR_INV_DATA; |
| 339 |
goto leave_main; |
| 340 |
} |
| 341 |
|
| 342 |
if (!rc) |
| 343 |
{ |
| 344 |
gcry_mpi_t r; |
| 345 |
|
| 346 |
r = mpi_new (inlen * 8); |
| 347 |
_gcry_mpi_set_buffer (r, plain, inlen, 0); |
| 348 |
|
| 349 |
rc = sexp_build (r_plain, NULL, "(value %m)", r); |
| 350 |
|
| 351 |
mpi_free (r); |
| 352 |
} |
| 353 |
|
| 354 |
leave_main: |
| 355 |
_gcry_md_close (md); |
| 356 |
mpi_free (x2y2); |
| 357 |
xfree (plain); |
| 358 |
|
| 359 |
point_free (&c1); |
| 360 |
point_free (&kP); |
| 361 |
mpi_free (x2); |
| 362 |
mpi_free (y2); |
| 363 |
} |
| 364 |
|
| 365 |
leave: |
| 366 |
_gcry_mpi_release (data_c1); |
| 367 |
_gcry_mpi_release (data_c3); |
| 368 |
_gcry_mpi_release (data_c2); |
| 369 |
|
| 370 |
return rc; |
| 371 |
} |
| 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 |
{ |
| 383 |
gpg_err_code_t rc = 0; |
| 384 |
int extraloops = 0; |
| 385 |
gcry_mpi_t hash; |
| 386 |
const void *abuf; |
| 387 |
unsigned int abits, qbits; |
| 388 |
gcry_mpi_t tmp = NULL; |
| 389 |
gcry_mpi_t k = NULL; |
| 390 |
gcry_mpi_t rk = NULL; |
| 391 |
mpi_point_struct kG; |
| 392 |
gcry_mpi_t x1; |
| 393 |
|
| 394 |
if (DBG_CIPHER) |
| 395 |
log_mpidump ("sm2 sign hash ", input); |
| 396 |
|
| 397 |
qbits = mpi_get_nbits (ec->n); |
| 398 |
|
| 399 |
/* Convert the INPUT into an MPI if needed. */ |
| 400 |
rc = _gcry_dsa_normalize_hash (input, &hash, qbits); |
| 401 |
if (rc) |
| 402 |
return rc; |
| 403 |
|
| 404 |
point_init (&kG, ec->nbits); |
| 405 |
x1 = mpi_new (ec->nbits); |
| 406 |
rk = mpi_new (ec->nbits); |
| 407 |
tmp = mpi_new (ec->nbits); |
| 408 |
|
| 409 |
for (;;) |
| 410 |
{ |
| 411 |
/* rand k in [1, n-1] */ |
| 412 |
if ((flags & PUBKEY_FLAG_RFC6979) && hashalgo) |
| 413 |
{ |
| 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 |
if (!mpi_is_opaque (input)) |
| 419 |
{ |
| 420 |
rc = GPG_ERR_CONFLICT; |
| 421 |
goto leave; |
| 422 |
} |
| 423 |
|
| 424 |
abuf = mpi_get_opaque (input, &abits); |
| 425 |
rc = _gcry_dsa_gen_rfc6979_k (&k, ec->n, ec->d, |
| 426 |
abuf, (abits+7)/8, |
| 427 |
hashalgo, extraloops); |
| 428 |
if (rc) |
| 429 |
goto leave; |
| 430 |
extraloops++; |
| 431 |
} |
| 432 |
else |
| 433 |
k = _gcry_dsa_gen_k (ec->n, GCRY_VERY_STRONG_RANDOM); |
| 434 |
|
| 435 |
_gcry_dsa_modify_k (k, ec->n, qbits); |
| 436 |
|
| 437 |
/* [k]G = (x1, y1) */ |
| 438 |
_gcry_mpi_ec_mul_point (&kG, k, ec->G, ec); |
| 439 |
if (_gcry_mpi_ec_get_affine (x1, NULL, &kG, ec)) |
| 440 |
{ |
| 441 |
rc = GPG_ERR_INV_DATA; |
| 442 |
goto leave; |
| 443 |
} |
| 444 |
|
| 445 |
/* r = (e + x1) % n */ |
| 446 |
mpi_addm (r, hash, x1, ec->n); |
| 447 |
|
| 448 |
/* r != 0 && r + k != n */ |
| 449 |
if (mpi_cmp_ui (r, 0) == 0) |
| 450 |
continue; |
| 451 |
mpi_add (rk, r, k); |
| 452 |
if (mpi_cmp (rk, ec->n) == 0) |
| 453 |
continue; |
| 454 |
|
| 455 |
/* s = ((d + 1)^-1 * (k - rd)) % n */ |
| 456 |
mpi_addm (s, ec->d, GCRYMPI_CONST_ONE, ec->n); |
| 457 |
mpi_invm (s, s, ec->n); |
| 458 |
mpi_mulm (tmp, r, ec->d, ec->n); |
| 459 |
mpi_subm (tmp, k, tmp, ec->n); |
| 460 |
mpi_mulm (s, s, tmp, ec->n); |
| 461 |
|
| 462 |
/* s != 0 */ |
| 463 |
if (mpi_cmp_ui (s, 0) == 0) |
| 464 |
continue; |
| 465 |
|
| 466 |
break; /* Okay */ |
| 467 |
} |
| 468 |
|
| 469 |
if (DBG_CIPHER) |
| 470 |
{ |
| 471 |
log_mpidump ("sm2 sign result r ", r); |
| 472 |
log_mpidump ("sm2 sign result s ", s); |
| 473 |
} |
| 474 |
|
| 475 |
leave: |
| 476 |
point_free (&kG); |
| 477 |
mpi_free (k); |
| 478 |
mpi_free (x1); |
| 479 |
mpi_free (rk); |
| 480 |
mpi_free (tmp); |
| 481 |
|
| 482 |
if (hash != input) |
| 483 |
mpi_free (hash); |
| 484 |
|
| 485 |
return rc; |
| 486 |
} |
| 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 |
{ |
| 496 |
gpg_err_code_t err = 0; |
| 497 |
gcry_mpi_t hash = NULL; |
| 498 |
gcry_mpi_t t = NULL; |
| 499 |
mpi_point_struct sG, tP; |
| 500 |
gcry_mpi_t x1, y1; |
| 501 |
unsigned int nbits; |
| 502 |
|
| 503 |
if (!_gcry_mpi_ec_curve_point (ec->Q, ec)) |
| 504 |
return GPG_ERR_BROKEN_PUBKEY; |
| 505 |
|
| 506 |
/* r, s within [1, n-1] */ |
| 507 |
if (mpi_cmp_ui (r, 1) < 0 || mpi_cmp (r, ec->n) > 0) |
| 508 |
return GPG_ERR_BAD_SIGNATURE; |
| 509 |
if (mpi_cmp_ui (s, 1) < 0 || mpi_cmp (s, ec->n) > 0) |
| 510 |
return GPG_ERR_BAD_SIGNATURE; |
| 511 |
|
| 512 |
nbits = mpi_get_nbits (ec->n); |
| 513 |
err = _gcry_dsa_normalize_hash (input, &hash, nbits); |
| 514 |
if (err) |
| 515 |
return err; |
| 516 |
|
| 517 |
point_init (&sG, ec->nbits); |
| 518 |
point_init (&tP, ec->nbits); |
| 519 |
x1 = mpi_new (ec->nbits); |
| 520 |
y1 = mpi_new (ec->nbits); |
| 521 |
t = mpi_new (ec->nbits); |
| 522 |
|
| 523 |
/* t = (r + s) % n, t != 0 */ |
| 524 |
mpi_addm (t, r, s, ec->n); |
| 525 |
if (mpi_cmp_ui (t, 0) == 0) |
| 526 |
{ |
| 527 |
err = GPG_ERR_BAD_SIGNATURE; |
| 528 |
goto leave; |
| 529 |
} |
| 530 |
|
| 531 |
/* sG + tP = (x1, y1) */ |
| 532 |
_gcry_mpi_ec_mul_point (&sG, s, ec->G, ec); |
| 533 |
_gcry_mpi_ec_mul_point (&tP, t, ec->Q, ec); |
| 534 |
_gcry_mpi_ec_add_points (&sG, &sG, &tP, ec); |
| 535 |
if (_gcry_mpi_ec_get_affine (x1, y1, &sG, ec)) |
| 536 |
{ |
| 537 |
err = GPG_ERR_INV_DATA; |
| 538 |
goto leave; |
| 539 |
} |
| 540 |
|
| 541 |
/* R = (e + x1) % n */ |
| 542 |
mpi_addm (t, hash, x1, ec->n); |
| 543 |
|
| 544 |
/* check R == r */ |
| 545 |
if (mpi_cmp (t, r)) |
| 546 |
{ |
| 547 |
if (DBG_CIPHER) |
| 548 |
{ |
| 549 |
log_mpidump (" R", t); |
| 550 |
log_mpidump (" r", r); |
| 551 |
log_mpidump (" s", s); |
| 552 |
} |
| 553 |
err = GPG_ERR_BAD_SIGNATURE; |
| 554 |
goto leave; |
| 555 |
} |
| 556 |
if (DBG_CIPHER) |
| 557 |
log_debug ("sm2 verify: Accepted\n"); |
| 558 |
|
| 559 |
leave: |
| 560 |
point_free (&sG); |
| 561 |
point_free (&tP); |
| 562 |
mpi_free (x1); |
| 563 |
mpi_free (y1); |
| 564 |
mpi_free (t); |
| 565 |
if (hash != input) |
| 566 |
mpi_free (hash); |
| 567 |
|
| 568 |
return err; |
| 569 |
} |