sm2_alg.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351
  1. /*
  2. * Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the License); you may
  5. * not use this file except in compliance with the License.
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <assert.h>
  12. #include <gmssl/sm2.h>
  13. #include <gmssl/mem.h>
  14. #include <gmssl/asn1.h>
  15. #include <gmssl/rand.h>
  16. #include <gmssl/error.h>
  17. #include <gmssl/endian.h>
  18. #define sm2_print_bn(label,a) sm2_bn_print(stderr,0,0,label,a) // 这个不应该放在这里,应该放在测试文件中
  19. const SM2_BN SM2_P = {
  20. 0xffffffff, 0xffffffff, 0x00000000, 0xffffffff,
  21. 0xffffffff, 0xffffffff, 0xffffffff, 0xfffffffe,
  22. };
  23. const SM2_BN SM2_B = {
  24. 0x4d940e93, 0xddbcbd41, 0x15ab8f92, 0xf39789f5,
  25. 0xcf6509a7, 0x4d5a9e4b, 0x9d9f5e34, 0x28e9fa9e,
  26. };
  27. const SM2_JACOBIAN_POINT _SM2_G = {
  28. {
  29. 0x334c74c7, 0x715a4589, 0xf2660be1, 0x8fe30bbf,
  30. 0x6a39c994, 0x5f990446, 0x1f198119, 0x32c4ae2c,
  31. },
  32. {
  33. 0x2139f0a0, 0x02df32e5, 0xc62a4740, 0xd0a9877c,
  34. 0x6b692153, 0x59bdcee3, 0xf4f6779c, 0xbc3736a2,
  35. },
  36. {
  37. 1, 0, 0, 0, 0, 0, 0, 0,
  38. },
  39. };
  40. const SM2_JACOBIAN_POINT *SM2_G = &_SM2_G;
  41. const SM2_BN SM2_N = {
  42. 0x39d54123, 0x53bbf409, 0x21c6052b, 0x7203df6b,
  43. 0xffffffff, 0xffffffff, 0xffffffff, 0xfffffffe,
  44. };
  45. // u = (p - 1)/4, u + 1 = (p + 1)/4
  46. const SM2_BN SM2_U_PLUS_ONE = {
  47. 0x00000000, 0x40000000, 0xc0000000, 0xffffffff,
  48. 0xffffffff, 0xffffffff, 0xbfffffff, 0x3fffffff,
  49. };
  50. const SM2_BN SM2_ONE = {1,0,0,0,0,0,0,0};
  51. const SM2_BN SM2_TWO = {2,0,0,0,0,0,0,0};
  52. const SM2_BN SM2_THREE = {3,0,0,0,0,0,0,0};
  53. int sm2_bn_check(const SM2_BN a)
  54. {
  55. int err = 0;
  56. int i;
  57. for (i = 0; i < 8; i++) {
  58. if (a[i] > 0xffffffff) {
  59. fprintf(stderr, "%s %d: error\n", __FILE__, __LINE__);
  60. err++;
  61. }
  62. }
  63. if (err)
  64. return -1;
  65. else return 1;
  66. }
  67. int sm2_bn_is_zero(const SM2_BN a)
  68. {
  69. int i;
  70. for (i = 0; i < 8; i++) {
  71. if (a[i] != 0)
  72. return 0;
  73. }
  74. return 1;
  75. }
  76. int sm2_bn_is_one(const SM2_BN a)
  77. {
  78. int i;
  79. if (a[0] != 1)
  80. return 0;
  81. for (i = 1; i < 8; i++) {
  82. if (a[i] != 0)
  83. return 0;
  84. }
  85. return 1;
  86. }
  87. void sm2_bn_to_bytes(const SM2_BN a, uint8_t out[32])
  88. {
  89. int i;
  90. uint8_t *p = out;
  91. /*
  92. fprintf(stderr, "sm2_bn_to_bytes:\n");
  93. for (i = 0; i < 8; i++) {
  94. fprintf(stderr, "%016lx ", a[i]);
  95. }
  96. fprintf(stderr, "\n");
  97. */
  98. for (i = 7; i >= 0; i--) {
  99. uint32_t ai = (uint32_t)a[i];
  100. PUTU32(out, ai);
  101. out += sizeof(uint32_t);
  102. }
  103. /*
  104. for (i = 0; i < 32; i++) {
  105. fprintf(stderr, "%02X ", p[i]);
  106. }
  107. */
  108. }
  109. void sm2_bn_from_bytes(SM2_BN r, const uint8_t in[32])
  110. {
  111. int i;
  112. for (i = 7; i >= 0; i--) {
  113. r[i] = GETU32(in);
  114. in += sizeof(uint32_t);
  115. }
  116. }
  117. static int hexchar2int(char c)
  118. {
  119. if ('0' <= c && c <= '9') return c - '0';
  120. else if ('a' <= c && c <= 'f') return c - 'a' + 10;
  121. else if ('A' <= c && c <= 'F') return c - 'A' + 10;
  122. else return -1;
  123. }
  124. static int hex2bin(const char *in, size_t inlen, uint8_t *out)
  125. {
  126. int c;
  127. if (inlen % 2)
  128. return -1;
  129. while (inlen) {
  130. if ((c = hexchar2int(*in++)) < 0)
  131. return -1;
  132. *out = (uint8_t)c << 4;
  133. if ((c = hexchar2int(*in++)) < 0)
  134. return -1;
  135. *out |= (uint8_t)c;
  136. inlen -= 2;
  137. out++;
  138. }
  139. return 1;
  140. }
  141. void sm2_bn_to_hex(const SM2_BN a, char hex[64])
  142. {
  143. int i;
  144. for (i = 7; i >= 0; i--) {
  145. int len;
  146. len = sprintf(hex, "%08x", (uint32_t)a[i]);
  147. assert(len == 8);
  148. hex += 8;
  149. }
  150. }
  151. int sm2_bn_from_hex(SM2_BN r, const char hex[64])
  152. {
  153. uint8_t buf[32];
  154. if (hex2bin(hex, 64, buf) < 0)
  155. return -1;
  156. sm2_bn_from_bytes(r, buf);
  157. return 1;
  158. }
  159. int sm2_bn_from_asn1_integer(SM2_BN r, const uint8_t *d, size_t dlen)
  160. {
  161. uint8_t buf[32] = {0};
  162. if (!d || dlen == 0) {
  163. error_print();
  164. return -1;
  165. }
  166. if (dlen > sizeof(buf)) {
  167. error_print();
  168. return -1;
  169. }
  170. memcpy(buf + sizeof(buf) - dlen, d, dlen);
  171. sm2_bn_from_bytes(r, buf);
  172. return 1;
  173. }
  174. int sm2_bn_print(FILE *fp, int fmt, int ind, const char *label, const SM2_BN a)
  175. {
  176. int ret = 0, i;
  177. format_print(fp, fmt, ind, "%s: ", label);
  178. for (i = 7; i >= 0; i--) {
  179. if (a[i] >= ((uint64_t)1 << 32)) {
  180. printf("bn_print check failed\n");
  181. }
  182. ret += fprintf(fp, "%08x", (uint32_t)a[i]);
  183. }
  184. ret += fprintf(fp, "\n");
  185. return ret;
  186. }
  187. void sm2_bn_to_bits(const SM2_BN a, char bits[256])
  188. {
  189. int i, j;
  190. uint64_t w;
  191. for (i = 7; i >= 0; i--) {
  192. w = a[i];
  193. for (j = 0; j < 32; j++) {
  194. *bits++ = (w & 0x80000000) ? '1' : '0';
  195. w <<= 1;
  196. }
  197. }
  198. }
  199. int sm2_bn_cmp(const SM2_BN a, const SM2_BN b)
  200. {
  201. int i;
  202. for (i = 7; i >= 0; i--) {
  203. if (a[i] > b[i])
  204. return 1;
  205. if (a[i] < b[i])
  206. return -1;
  207. }
  208. return 0;
  209. }
  210. int sm2_bn_equ_hex(const SM2_BN a, const char *hex)
  211. {
  212. char buf[65] = {0};
  213. char *p = buf;
  214. int i;
  215. for (i = 7; i >= 0; i--) {
  216. sprintf(p, "%08x", (uint32_t)a[i]);
  217. p += 8;
  218. }
  219. return (strcmp(buf, hex) == 0);
  220. }
  221. int sm2_bn_is_odd(const SM2_BN a)
  222. {
  223. return a[0] & 0x01;
  224. }
  225. void sm2_bn_set_word(SM2_BN r, uint32_t a)
  226. {
  227. int i;
  228. r[0] = a;
  229. for (i = 1; i < 8; i++) {
  230. r[i] = 0;
  231. }
  232. }
  233. int sm2_bn_rshift(SM2_BN ret, const SM2_BN a, unsigned int nbits)
  234. {
  235. SM2_BN r;
  236. int i;
  237. if (nbits > 31) {
  238. error_print();
  239. return -1;
  240. }
  241. if (nbits == 0) {
  242. sm2_bn_copy(ret, a);
  243. }
  244. for (i = 0; i < 7; i++) {
  245. r[i] = a[i] >> nbits;
  246. r[i] |= (a[i+1] << (32 - nbits)) & 0xffffffff;
  247. }
  248. r[i] = a[i] >> nbits;
  249. sm2_bn_copy(ret, r);
  250. return 1;
  251. }
  252. void sm2_bn_add(SM2_BN r, const SM2_BN a, const SM2_BN b)
  253. {
  254. int i;
  255. r[0] = a[0] + b[0];
  256. for (i = 1; i < 8; i++) {
  257. r[i] = a[i] + b[i] + (r[i-1] >> 32);
  258. }
  259. for (i = 0; i < 7; i++) {
  260. r[i] &= 0xffffffff;
  261. }
  262. }
  263. void sm2_bn_sub(SM2_BN ret, const SM2_BN a, const SM2_BN b)
  264. {
  265. int i;
  266. SM2_BN r;
  267. r[0] = ((uint64_t)1 << 32) + a[0] - b[0];
  268. for (i = 1; i < 7; i++) {
  269. r[i] = 0xffffffff + a[i] - b[i] + (r[i - 1] >> 32);
  270. r[i - 1] &= 0xffffffff;
  271. }
  272. r[i] = a[i] - b[i] + (r[i - 1] >> 32) - 1;
  273. r[i - 1] &= 0xffffffff;
  274. sm2_bn_copy(ret, r);
  275. }
  276. int sm2_bn_rand_range(SM2_BN r, const SM2_BN range)
  277. {
  278. uint8_t buf[32];
  279. do {
  280. if (rand_bytes(buf, sizeof(buf)) != 1) {
  281. error_print();
  282. return -1;
  283. }
  284. sm2_bn_from_bytes(r, buf);
  285. } while (sm2_bn_cmp(r, range) >= 0);
  286. return 1;
  287. }
  288. int sm2_fp_rand(SM2_Fp r)
  289. {
  290. if (sm2_bn_rand_range(r, SM2_P) != 1) {
  291. error_print();
  292. return -1;
  293. }
  294. return 1;
  295. }
  296. void sm2_fp_add(SM2_Fp r, const SM2_Fp a, const SM2_Fp b)
  297. {
  298. sm2_bn_add(r, a, b);
  299. if (sm2_bn_cmp(r, SM2_P) >= 0) {
  300. sm2_bn_sub(r, r, SM2_P);
  301. }
  302. }
  303. void sm2_fp_sub(SM2_Fp r, const SM2_Fp a, const SM2_Fp b)
  304. {
  305. if (sm2_bn_cmp(a, b) >= 0) {
  306. sm2_bn_sub(r, a, b);
  307. } else {
  308. SM2_BN t;
  309. sm2_bn_sub(t, SM2_P, b);
  310. sm2_bn_add(r, t, a);
  311. }
  312. }
  313. void sm2_fp_dbl(SM2_Fp r, const SM2_Fp a)
  314. {
  315. sm2_fp_add(r, a, a);
  316. }
  317. void sm2_fp_tri(SM2_Fp r, const SM2_Fp a)
  318. {
  319. SM2_BN t;
  320. sm2_fp_dbl(t, a);
  321. sm2_fp_add(r, t, a);
  322. }
  323. void sm2_fp_div2(SM2_Fp r, const SM2_Fp a)
  324. {
  325. int i;
  326. sm2_bn_copy(r, a);
  327. if (r[0] & 0x01) {
  328. sm2_bn_add(r, r, SM2_P);
  329. }
  330. for (i = 0; i < 7; i++) {
  331. r[i] = (r[i] >> 1) | ((r[i + 1] & 0x01) << 31);
  332. }
  333. r[i] >>= 1;
  334. }
  335. void sm2_fp_neg(SM2_Fp r, const SM2_Fp a)
  336. {
  337. if (sm2_bn_is_zero(a)) {
  338. sm2_bn_copy(r, a);
  339. } else {
  340. sm2_bn_sub(r, SM2_P, a);
  341. }
  342. }
  343. void sm2_fp_mul(SM2_Fp r, const SM2_Fp a, const SM2_Fp b)
  344. {
  345. int i, j;
  346. uint64_t s[16] = {0};
  347. SM2_BN d = {0};
  348. uint64_t u;
  349. // s = a * b
  350. for (i = 0; i < 8; i++) {
  351. u = 0;
  352. for (j = 0; j < 8; j++) {
  353. u = s[i + j] + a[i] * b[j] + u;
  354. s[i + j] = u & 0xffffffff;
  355. u >>= 32;
  356. }
  357. s[i + 8] = u;
  358. }
  359. r[0] = s[0] + s[ 8] + s[ 9] + s[10] + s[11] + s[12] + ((s[13] + s[14] + s[15]) << 1);
  360. r[1] = s[1] + s[ 9] + s[10] + s[11] + s[12] + s[13] + ((s[14] + s[15]) << 1);
  361. r[2] = s[2];
  362. r[3] = s[3] + s[ 8] + s[11] + s[12] + s[14] + s[15] + (s[13] << 1);
  363. r[4] = s[4] + s[ 9] + s[12] + s[13] + s[15] + (s[14] << 1);
  364. r[5] = s[5] + s[10] + s[13] + s[14] + (s[15] << 1);
  365. r[6] = s[6] + s[11] + s[14] + s[15];
  366. r[7] = s[7] + s[ 8] + s[ 9] + s[10] + s[11] + s[15] + ((s[12] + s[13] + s[14] + s[15]) << 1);
  367. for (i = 1; i < 8; i++) {
  368. r[i] += r[i - 1] >> 32;
  369. r[i - 1] &= 0xffffffff;
  370. }
  371. d[2] = s[8] + s[9] + s[13] + s[14];
  372. d[3] = d[2] >> 32;
  373. d[2] &= 0xffffffff;
  374. sm2_bn_sub(r, r, d);
  375. // max times ?
  376. while (sm2_bn_cmp(r, SM2_P) >= 0) {
  377. sm2_bn_sub(r, r, SM2_P);
  378. }
  379. }
  380. void sm2_fp_sqr(SM2_Fp r, const SM2_Fp a)
  381. {
  382. sm2_fp_mul(r, a, a);
  383. }
  384. void sm2_fp_exp(SM2_Fp r, const SM2_Fp a, const SM2_Fp e)
  385. {
  386. SM2_BN t;
  387. uint32_t w;
  388. int i, j;
  389. sm2_bn_set_one(t);
  390. for (i = 7; i >= 0; i--) {
  391. w = (uint32_t)e[i];
  392. for (j = 0; j < 32; j++) {
  393. sm2_fp_sqr(t, t);
  394. if (w & 0x80000000)
  395. sm2_fp_mul(t, t, a);
  396. w <<= 1;
  397. }
  398. }
  399. sm2_bn_copy(r, t);
  400. }
  401. void sm2_fp_inv(SM2_Fp r, const SM2_Fp a)
  402. {
  403. SM2_BN a1;
  404. SM2_BN a2;
  405. SM2_BN a3;
  406. SM2_BN a4;
  407. SM2_BN a5;
  408. int i;
  409. sm2_fp_sqr(a1, a);
  410. sm2_fp_mul(a2, a1, a);
  411. sm2_fp_sqr(a3, a2);
  412. sm2_fp_sqr(a3, a3);
  413. sm2_fp_mul(a3, a3, a2);
  414. sm2_fp_sqr(a4, a3);
  415. sm2_fp_sqr(a4, a4);
  416. sm2_fp_sqr(a4, a4);
  417. sm2_fp_sqr(a4, a4);
  418. sm2_fp_mul(a4, a4, a3);
  419. sm2_fp_sqr(a5, a4);
  420. for (i = 1; i < 8; i++)
  421. sm2_fp_sqr(a5, a5);
  422. sm2_fp_mul(a5, a5, a4);
  423. for (i = 0; i < 8; i++)
  424. sm2_fp_sqr(a5, a5);
  425. sm2_fp_mul(a5, a5, a4);
  426. for (i = 0; i < 4; i++)
  427. sm2_fp_sqr(a5, a5);
  428. sm2_fp_mul(a5, a5, a3);
  429. sm2_fp_sqr(a5, a5);
  430. sm2_fp_sqr(a5, a5);
  431. sm2_fp_mul(a5, a5, a2);
  432. sm2_fp_sqr(a5, a5);
  433. sm2_fp_mul(a5, a5, a);
  434. sm2_fp_sqr(a4, a5);
  435. sm2_fp_mul(a3, a4, a1);
  436. sm2_fp_sqr(a5, a4);
  437. for (i = 1; i< 31; i++)
  438. sm2_fp_sqr(a5, a5);
  439. sm2_fp_mul(a4, a5, a4);
  440. sm2_fp_sqr(a4, a4);
  441. sm2_fp_mul(a4, a4, a);
  442. sm2_fp_mul(a3, a4, a2);
  443. for (i = 0; i < 33; i++)
  444. sm2_fp_sqr(a5, a5);
  445. sm2_fp_mul(a2, a5, a3);
  446. sm2_fp_mul(a3, a2, a3);
  447. for (i = 0; i < 32; i++)
  448. sm2_fp_sqr(a5, a5);
  449. sm2_fp_mul(a2, a5, a3);
  450. sm2_fp_mul(a3, a2, a3);
  451. sm2_fp_mul(a4, a2, a4);
  452. for (i = 0; i < 32; i++)
  453. sm2_fp_sqr(a5, a5);
  454. sm2_fp_mul(a2, a5, a3);
  455. sm2_fp_mul(a3, a2, a3);
  456. sm2_fp_mul(a4, a2, a4);
  457. for (i = 0; i < 32; i++)
  458. sm2_fp_sqr(a5, a5);
  459. sm2_fp_mul(a2, a5, a3);
  460. sm2_fp_mul(a3, a2, a3);
  461. sm2_fp_mul(a4, a2, a4);
  462. for (i = 0; i < 32; i++)
  463. sm2_fp_sqr(a5, a5);
  464. sm2_fp_mul(a2, a5, a3);
  465. sm2_fp_mul(a3, a2, a3);
  466. sm2_fp_mul(a4, a2, a4);
  467. for (i = 0; i < 32; i++)
  468. sm2_fp_sqr(a5, a5);
  469. sm2_fp_mul(r, a4, a5);
  470. sm2_bn_clean(a1);
  471. sm2_bn_clean(a2);
  472. sm2_bn_clean(a3);
  473. sm2_bn_clean(a4);
  474. sm2_bn_clean(a5);
  475. }
  476. int sm2_fp_sqrt(SM2_Fp r, const SM2_Fp a)
  477. {
  478. SM2_BN u;
  479. SM2_BN y; // temp result, prevent call sm2_fp_sqrt(a, a)
  480. // r = a^((p + 1)/4) when p = 3 (mod 4)
  481. sm2_bn_add(u, SM2_P, SM2_ONE);
  482. sm2_bn_rshift(u, u, 2);
  483. sm2_fp_exp(y, a, u);
  484. // check r^2 == a
  485. sm2_fp_sqr(u, y);
  486. if (sm2_bn_cmp(u, a) != 0) {
  487. error_print();
  488. return -1;
  489. }
  490. sm2_bn_copy(r, y);
  491. return 1;
  492. }
  493. void sm2_fn_add(SM2_Fn r, const SM2_Fn a, const SM2_Fn b)
  494. {
  495. sm2_bn_add(r, a, b);
  496. if (sm2_bn_cmp(r, SM2_N) >= 0) {
  497. sm2_bn_sub(r, r, SM2_N);
  498. }
  499. }
  500. void sm2_fn_sub(SM2_Fn r, const SM2_Fn a, const SM2_Fn b)
  501. {
  502. if (sm2_bn_cmp(a, b) >= 0) {
  503. sm2_bn_sub(r, a, b);
  504. } else {
  505. SM2_BN t;
  506. sm2_bn_add(t, a, SM2_N);
  507. sm2_bn_sub(r, t, b);
  508. }
  509. }
  510. void sm2_fn_neg(SM2_Fn r, const SM2_Fn a)
  511. {
  512. if (sm2_bn_is_zero(a)) {
  513. sm2_bn_copy(r, a);
  514. } else {
  515. sm2_bn_sub(r, SM2_N, a);
  516. }
  517. }
  518. /* bn288 only used in barrett reduction */
  519. static int sm2_bn288_cmp(const uint64_t a[9], const uint64_t b[9])
  520. {
  521. int i;
  522. for (i = 8; i >= 0; i--) {
  523. if (a[i] > b[i])
  524. return 1;
  525. if (a[i] < b[i])
  526. return -1;
  527. }
  528. return 0;
  529. }
  530. static void sm2_bn288_add(uint64_t r[9], const uint64_t a[9], const uint64_t b[9])
  531. {
  532. int i;
  533. r[0] = a[0] + b[0];
  534. for (i = 1; i < 9; i++) {
  535. r[i] = a[i] + b[i] + (r[i-1] >> 32);
  536. }
  537. for (i = 0; i < 8; i++) {
  538. r[i] &= 0xffffffff;
  539. }
  540. }
  541. static void sm2_bn288_sub(uint64_t ret[9], const uint64_t a[9], const uint64_t b[9])
  542. {
  543. int i;
  544. uint64_t r[9];
  545. r[0] = ((uint64_t)1 << 32) + a[0] - b[0];
  546. for (i = 1; i < 8; i++) {
  547. r[i] = 0xffffffff + a[i] - b[i] + (r[i - 1] >> 32);
  548. r[i - 1] &= 0xffffffff;
  549. }
  550. r[i] = a[i] - b[i] + (r[i - 1] >> 32) - 1;
  551. r[i - 1] &= 0xffffffff;
  552. for (i = 0; i < 9; i++) {
  553. ret[i] = r[i];
  554. }
  555. }
  556. void sm2_fn_mul(SM2_BN ret, const SM2_BN a, const SM2_BN b)
  557. {
  558. SM2_BN r;
  559. static const uint64_t mu[9] = {
  560. 0xf15149a0, 0x12ac6361, 0xfa323c01, 0x8dfc2096, 1, 1, 1, 1, 1,
  561. };
  562. uint64_t s[18];
  563. uint64_t zh[9];
  564. uint64_t zl[9];
  565. uint64_t q[9];
  566. uint64_t w;
  567. int i, j;
  568. /* z = a * b */
  569. for (i = 0; i < 8; i++) {
  570. s[i] = 0;
  571. }
  572. for (i = 0; i < 8; i++) {
  573. w = 0;
  574. for (j = 0; j < 8; j++) {
  575. w += s[i + j] + a[i] * b[j];
  576. s[i + j] = w & 0xffffffff;
  577. w >>= 32;
  578. }
  579. s[i + 8] = w;
  580. }
  581. /* zl = z mod (2^32)^9 = z[0..8]
  582. * zh = z // (2^32)^7 = z[7..15] */
  583. for (i = 0; i < 9; i++) {
  584. zl[i] = s[i];
  585. zh[i] = s[7 + i];
  586. }
  587. //printf("zl = "); for (i = 8; i >= 0; i--) printf("%08x", (uint32_t)zl[i]); printf("\n");
  588. //printf("zh = "); for (i = 8; i >= 0; i--) printf("%08x", (uint32_t)zh[i]); printf("\n");
  589. /* q = zh * mu // (2^32)^9 */
  590. for (i = 0; i < 9; i++) {
  591. s[i] = 0;
  592. }
  593. for (i = 0; i < 9; i++) {
  594. w = 0;
  595. for (j = 0; j < 9; j++) {
  596. w += s[i + j] + zh[i] * mu[j];
  597. s[i + j] = w & 0xffffffff;
  598. w >>= 32;
  599. }
  600. s[i + 9] = w;
  601. }
  602. for (i = 0; i < 8; i++) {
  603. q[i] = s[9 + i];
  604. }
  605. //printf("q = "); for (i = 7; i >= 0; i--) printf("%08x", (uint32_t)q[i]); printf("\n");
  606. /* q = q * n mod (2^32)^9 */
  607. for (i = 0; i < 17; i++) {
  608. s[i] = 0;
  609. }
  610. for (i = 0; i < 8; i++) {
  611. w = 0;
  612. for (j = 0; j < 8; j++) {
  613. w += s[i + j] + q[i] * SM2_N[j];
  614. s[i + j] = w & 0xffffffff;
  615. w >>= 32;
  616. }
  617. s[i + 8] = w;
  618. }
  619. for (i = 0; i < 9; i++) {
  620. q[i] = s[i];
  621. }
  622. //printf("qn = "); for (i = 8; i >= 0; i--) printf("%08x ", (uint32_t)q[i]); printf("\n");
  623. /* r = zl - q (mod (2^32)^9) */
  624. if (sm2_bn288_cmp(zl, q)) {
  625. sm2_bn288_sub(zl, zl, q);
  626. } else {
  627. uint64_t c[9] = {0,0,0,0,0,0,0,0,0x100000000};
  628. sm2_bn288_sub(q, c, q);
  629. sm2_bn288_add(zl, q, zl);
  630. }
  631. //printf("zl = "); for (i = 8; i >= 0; i--) printf("%08x ", (uint32_t)zl[i]); printf("\n");
  632. for (i = 0; i < 8; i++) {
  633. r[i] = zl[i];
  634. }
  635. r[7] += zl[8] << 32;
  636. /* while r >= p do: r = r - n */
  637. while (sm2_bn_cmp(r, SM2_N) >= 0) {
  638. sm2_bn_sub(r, r, SM2_N);
  639. //printf("r-n = "); for (i = 7; i >= 0; i--) printf("%16llx ", r[i]); printf("\n");
  640. }
  641. sm2_bn_copy(ret, r);
  642. }
  643. void sm2_fn_mul_word(SM2_Fn r, const SM2_Fn a, uint32_t b)
  644. {
  645. SM2_Fn t;
  646. sm2_bn_set_word(t, b);
  647. sm2_fn_mul(r, a, t);
  648. }
  649. void sm2_fn_sqr(SM2_BN r, const SM2_BN a)
  650. {
  651. sm2_fn_mul(r, a, a);
  652. }
  653. void sm2_fn_exp(SM2_BN r, const SM2_BN a, const SM2_BN e)
  654. {
  655. SM2_BN t;
  656. uint32_t w;
  657. int i, j;
  658. sm2_bn_set_one(t);
  659. for (i = 7; i >= 0; i--) {
  660. w = (uint32_t)e[i];
  661. for (j = 0; j < 32; j++) {
  662. sm2_fn_sqr(t, t);
  663. if (w & 0x80000000) {
  664. sm2_fn_mul(t, t, a);
  665. }
  666. w <<= 1;
  667. }
  668. }
  669. sm2_bn_copy(r, t);
  670. }
  671. void sm2_fn_inv(SM2_BN r, const SM2_BN a)
  672. {
  673. SM2_BN e;
  674. sm2_bn_sub(e, SM2_N, SM2_TWO);
  675. sm2_fn_exp(r, a, e);
  676. }
  677. int sm2_fn_rand(SM2_BN r)
  678. {
  679. if (sm2_bn_rand_range(r, SM2_N) != 1) {
  680. error_print();
  681. return -1;
  682. }
  683. return 1;
  684. }
  685. void sm2_jacobian_point_init(SM2_JACOBIAN_POINT *R)
  686. {
  687. memset(R, 0, sizeof(SM2_JACOBIAN_POINT));
  688. R->X[0] = 1;
  689. R->Y[0] = 1;
  690. }
  691. int sm2_jacobian_point_is_at_infinity(const SM2_JACOBIAN_POINT *P)
  692. {
  693. return sm2_bn_is_zero(P->Z);
  694. }
  695. void sm2_jacobian_point_set_xy(SM2_JACOBIAN_POINT *R, const SM2_BN x, const SM2_BN y)
  696. {
  697. sm2_bn_copy(R->X, x);
  698. sm2_bn_copy(R->Y, y);
  699. sm2_bn_set_one(R->Z);
  700. }
  701. void sm2_jacobian_point_get_xy(const SM2_JACOBIAN_POINT *P, SM2_BN x, SM2_BN y)
  702. {
  703. if (sm2_bn_is_one(P->Z)) {
  704. sm2_bn_copy(x, P->X);
  705. if (y) {
  706. sm2_bn_copy(y, P->Y);
  707. }
  708. } else {
  709. SM2_BN z_inv;
  710. sm2_fp_inv(z_inv, P->Z);
  711. if (y) {
  712. sm2_fp_mul(y, P->Y, z_inv);
  713. }
  714. sm2_fp_sqr(z_inv, z_inv);
  715. sm2_fp_mul(x, P->X, z_inv);
  716. if (y) {
  717. sm2_fp_mul(y, y, z_inv);
  718. }
  719. }
  720. }
  721. int sm2_jacobian_pointpoint_print(FILE *fp, int fmt, int ind, const char *label, const SM2_JACOBIAN_POINT *P)
  722. {
  723. int len = 0;
  724. SM2_BN x;
  725. SM2_BN y;
  726. format_print(fp, fmt, ind, "%s\n", label);
  727. ind += 4;
  728. sm2_jacobian_point_get_xy(P, x, y);
  729. sm2_bn_print(fp, fmt, ind, "x", x);
  730. sm2_bn_print(fp, fmt, ind, "y", y);
  731. return 1;
  732. }
  733. int sm2_jacobian_point_is_on_curve(const SM2_JACOBIAN_POINT *P)
  734. {
  735. SM2_BN t0;
  736. SM2_BN t1;
  737. SM2_BN t2;
  738. if (sm2_bn_is_one(P->Z)) {
  739. sm2_fp_sqr(t0, P->Y);
  740. sm2_fp_add(t0, t0, P->X);
  741. sm2_fp_add(t0, t0, P->X);
  742. sm2_fp_add(t0, t0, P->X);
  743. sm2_fp_sqr(t1, P->X);
  744. sm2_fp_mul(t1, t1, P->X);
  745. sm2_fp_add(t1, t1, SM2_B);
  746. } else {
  747. sm2_fp_sqr(t0, P->Y);
  748. sm2_fp_sqr(t1, P->Z);
  749. sm2_fp_sqr(t2, t1);
  750. sm2_fp_mul(t1, t1, t2);
  751. sm2_fp_mul(t1, t1, SM2_B);
  752. sm2_fp_mul(t2, t2, P->X);
  753. sm2_fp_add(t0, t0, t2);
  754. sm2_fp_add(t0, t0, t2);
  755. sm2_fp_add(t0, t0, t2);
  756. sm2_fp_sqr(t2, P->X);
  757. sm2_fp_mul(t2, t2, P->X);
  758. sm2_fp_add(t1, t1, t2);
  759. }
  760. if (sm2_bn_cmp(t0, t1) != 0) {
  761. error_print();
  762. return -1;
  763. }
  764. return 1;
  765. }
  766. void sm2_jacobian_point_neg(SM2_JACOBIAN_POINT *R, const SM2_JACOBIAN_POINT *P)
  767. {
  768. sm2_bn_copy(R->X, P->X);
  769. sm2_fp_neg(R->Y, P->Y);
  770. sm2_bn_copy(R->Z, P->Z);
  771. }
  772. void sm2_jacobian_point_dbl(SM2_JACOBIAN_POINT *R, const SM2_JACOBIAN_POINT *P)
  773. {
  774. const uint64_t *X1 = P->X;
  775. const uint64_t *Y1 = P->Y;
  776. const uint64_t *Z1 = P->Z;
  777. SM2_BN T1;
  778. SM2_BN T2;
  779. SM2_BN T3;
  780. SM2_BN X3;
  781. SM2_BN Y3;
  782. SM2_BN Z3;
  783. //printf("X1 = "); print_bn(X1);
  784. //printf("Y1 = "); print_bn(Y1);
  785. //printf("Z1 = "); print_bn(Z1);
  786. if (sm2_jacobian_point_is_at_infinity(P)) {
  787. sm2_jacobian_point_copy(R, P);
  788. return;
  789. }
  790. sm2_fp_sqr(T1, Z1); //printf("T1 = Z1^2 = "); print_bn(T1);
  791. sm2_fp_sub(T2, X1, T1); //printf("T2 = X1 - T1 = "); print_bn(T2);
  792. sm2_fp_add(T1, X1, T1); //printf("T1 = X1 + T1 = "); print_bn(T1);
  793. sm2_fp_mul(T2, T2, T1); //printf("T2 = T2 * T1 = "); print_bn(T2);
  794. sm2_fp_tri(T2, T2); //printf("T2 = 3 * T2 = "); print_bn(T2);
  795. sm2_fp_dbl(Y3, Y1); //printf("Y3 = 2 * Y1 = "); print_bn(Y3);
  796. sm2_fp_mul(Z3, Y3, Z1); //printf("Z3 = Y3 * Z1 = "); print_bn(Z3);
  797. sm2_fp_sqr(Y3, Y3); //printf("Y3 = Y3^2 = "); print_bn(Y3);
  798. sm2_fp_mul(T3, Y3, X1); //printf("T3 = Y3 * X1 = "); print_bn(T3);
  799. sm2_fp_sqr(Y3, Y3); //printf("Y3 = Y3^2 = "); print_bn(Y3);
  800. sm2_fp_div2(Y3, Y3); //printf("Y3 = Y3/2 = "); print_bn(Y3);
  801. sm2_fp_sqr(X3, T2); //printf("X3 = T2^2 = "); print_bn(X3);
  802. sm2_fp_dbl(T1, T3); //printf("T1 = 2 * T1 = "); print_bn(T1);
  803. sm2_fp_sub(X3, X3, T1); //printf("X3 = X3 - T1 = "); print_bn(X3);
  804. sm2_fp_sub(T1, T3, X3); //printf("T1 = T3 - X3 = "); print_bn(T1);
  805. sm2_fp_mul(T1, T1, T2); //printf("T1 = T1 * T2 = "); print_bn(T1);
  806. sm2_fp_sub(Y3, T1, Y3); //printf("Y3 = T1 - Y3 = "); print_bn(Y3);
  807. sm2_bn_copy(R->X, X3);
  808. sm2_bn_copy(R->Y, Y3);
  809. sm2_bn_copy(R->Z, Z3);
  810. //printf("X3 = "); print_bn(R->X);
  811. //printf("Y3 = "); print_bn(R->Y);
  812. //printf("Z3 = "); print_bn(R->Z);
  813. }
  814. void sm2_jacobian_point_add(SM2_JACOBIAN_POINT *R, const SM2_JACOBIAN_POINT *P, const SM2_JACOBIAN_POINT *Q)
  815. {
  816. const uint64_t *X1 = P->X;
  817. const uint64_t *Y1 = P->Y;
  818. const uint64_t *Z1 = P->Z;
  819. const uint64_t *x2 = Q->X;
  820. const uint64_t *y2 = Q->Y;
  821. SM2_BN T1;
  822. SM2_BN T2;
  823. SM2_BN T3;
  824. SM2_BN T4;
  825. SM2_BN X3;
  826. SM2_BN Y3;
  827. SM2_BN Z3;
  828. if (sm2_jacobian_point_is_at_infinity(Q)) {
  829. sm2_jacobian_point_copy(R, P);
  830. return;
  831. }
  832. if (sm2_jacobian_point_is_at_infinity(P)) {
  833. sm2_jacobian_point_copy(R, Q);
  834. return;
  835. }
  836. assert(sm2_bn_is_one(Q->Z));
  837. sm2_fp_sqr(T1, Z1);
  838. sm2_fp_mul(T2, T1, Z1);
  839. sm2_fp_mul(T1, T1, x2);
  840. sm2_fp_mul(T2, T2, y2);
  841. sm2_fp_sub(T1, T1, X1);
  842. sm2_fp_sub(T2, T2, Y1);
  843. if (sm2_bn_is_zero(T1)) {
  844. if (sm2_bn_is_zero(T2)) {
  845. SM2_JACOBIAN_POINT _Q, *Q = &_Q;
  846. sm2_jacobian_point_set_xy(Q, x2, y2);
  847. sm2_jacobian_point_dbl(R, Q);
  848. return;
  849. } else {
  850. sm2_jacobian_point_set_infinity(R);
  851. return;
  852. }
  853. }
  854. sm2_fp_mul(Z3, Z1, T1);
  855. sm2_fp_sqr(T3, T1);
  856. sm2_fp_mul(T4, T3, T1);
  857. sm2_fp_mul(T3, T3, X1);
  858. sm2_fp_dbl(T1, T3);
  859. sm2_fp_sqr(X3, T2);
  860. sm2_fp_sub(X3, X3, T1);
  861. sm2_fp_sub(X3, X3, T4);
  862. sm2_fp_sub(T3, T3, X3);
  863. sm2_fp_mul(T3, T3, T2);
  864. sm2_fp_mul(T4, T4, Y1);
  865. sm2_fp_sub(Y3, T3, T4);
  866. sm2_bn_copy(R->X, X3);
  867. sm2_bn_copy(R->Y, Y3);
  868. sm2_bn_copy(R->Z, Z3);
  869. }
  870. void sm2_jacobian_point_sub(SM2_JACOBIAN_POINT *R, const SM2_JACOBIAN_POINT *P, const SM2_JACOBIAN_POINT *Q)
  871. {
  872. SM2_JACOBIAN_POINT _T, *T = &_T;
  873. sm2_jacobian_point_neg(T, Q);
  874. sm2_jacobian_point_add(R, P, T);
  875. }
  876. void sm2_jacobian_point_mul(SM2_JACOBIAN_POINT *R, const SM2_BN k, const SM2_JACOBIAN_POINT *P)
  877. {
  878. char bits[257] = {0};
  879. SM2_JACOBIAN_POINT _Q, *Q = &_Q;
  880. SM2_JACOBIAN_POINT _T, *T = &_T;
  881. int i;
  882. // FIXME: point_add need affine, so we can not use point_add
  883. if (!sm2_bn_is_one(P->Z)) {
  884. SM2_BN x;
  885. SM2_BN y;
  886. sm2_jacobian_point_get_xy(P, x, y);
  887. sm2_jacobian_point_set_xy(T, x, y);
  888. P = T;
  889. }
  890. sm2_jacobian_point_set_infinity(Q);
  891. sm2_bn_to_bits(k, bits);
  892. for (i = 0; i < 256; i++) {
  893. sm2_jacobian_point_dbl(Q, Q);
  894. if (bits[i] == '1') {
  895. sm2_jacobian_point_add(Q, Q, P);
  896. }
  897. }
  898. sm2_jacobian_point_copy(R, Q);
  899. }
  900. void sm2_jacobian_point_to_bytes(const SM2_JACOBIAN_POINT *P, uint8_t out[64])
  901. {
  902. SM2_BN x;
  903. SM2_BN y;
  904. sm2_jacobian_point_get_xy(P, x, y);
  905. sm2_bn_to_bytes(x, out);
  906. sm2_bn_to_bytes(y, out + 32);
  907. }
  908. void sm2_jacobian_point_from_bytes(SM2_JACOBIAN_POINT *P, const uint8_t in[64])
  909. {
  910. sm2_bn_from_bytes(P->X, in);
  911. sm2_bn_from_bytes(P->Y, in + 32);
  912. sm2_bn_set_word(P->Z, 1);
  913. /* should we check if sm2_jacobian_point_is_on_curve */
  914. }
  915. void sm2_jacobian_point_mul_generator(SM2_JACOBIAN_POINT *R, const SM2_BN k)
  916. {
  917. sm2_jacobian_point_mul(R, k, SM2_G);
  918. }
  919. /* R = t * P + s * G */
  920. void sm2_jacobian_point_mul_sum(SM2_JACOBIAN_POINT *R, const SM2_BN t, const SM2_JACOBIAN_POINT *P, const SM2_BN s)
  921. {
  922. SM2_JACOBIAN_POINT _sG, *sG = &_sG;
  923. SM2_BN x;
  924. SM2_BN y;
  925. /* T = s * G */
  926. sm2_jacobian_point_mul_generator(sG, s);
  927. // R = t * P
  928. sm2_jacobian_point_mul(R, t, P);
  929. sm2_jacobian_point_get_xy(R, x, y);
  930. sm2_jacobian_point_set_xy(R, x, y);
  931. // R = R + T
  932. sm2_jacobian_point_add(R, sG, R);
  933. }
  934. void sm2_jacobian_point_from_hex(SM2_JACOBIAN_POINT *P, const char hex[64 * 2])
  935. {
  936. sm2_bn_from_hex(P->X, hex);
  937. sm2_bn_from_hex(P->Y, hex + 64);
  938. sm2_bn_set_one(P->Z);
  939. }
  940. int sm2_jacobian_point_equ_hex(const SM2_JACOBIAN_POINT *P, const char hex[128])
  941. {
  942. SM2_BN x;
  943. SM2_BN y;
  944. SM2_JACOBIAN_POINT _T, *T = &_T;
  945. sm2_jacobian_point_get_xy(P, x, y);
  946. sm2_jacobian_point_from_hex(T, hex);
  947. return (sm2_bn_cmp(x, T->X) == 0) && (sm2_bn_cmp(y, T->Y) == 0);
  948. }
  949. int sm2_point_is_on_curve(const SM2_POINT *P)
  950. {
  951. SM2_JACOBIAN_POINT T;
  952. sm2_jacobian_point_from_bytes(&T, (const uint8_t *)P);
  953. return sm2_jacobian_point_is_on_curve(&T);
  954. }
  955. int sm2_point_is_at_infinity(const SM2_POINT *P)
  956. {
  957. return mem_is_zero((uint8_t *)P, sizeof(SM2_POINT));
  958. }
  959. int sm2_point_from_x(SM2_POINT *P, const uint8_t x[32], int y)
  960. {
  961. SM2_BN _x, _y, _g, _z;
  962. sm2_bn_from_bytes(_x, x);
  963. // g = x^3 - 3x + b = (x^2 - 3)*x + b
  964. sm2_fp_sqr(_g, _x);
  965. sm2_fp_sub(_g, _g, SM2_THREE);
  966. sm2_fp_mul(_g, _g, _x);
  967. sm2_fp_add(_g, _g, SM2_B);
  968. // y = g^(u + 1) mod p, u = (p - 3)/4
  969. sm2_fp_exp(_y, _g, SM2_U_PLUS_ONE);
  970. // z = y^2 mod p
  971. sm2_fp_sqr(_z, _y);
  972. if (sm2_bn_cmp(_z, _g)) {
  973. error_print();
  974. return -1;
  975. }
  976. if ((y == 0x02 && sm2_bn_is_odd(_y)) || (y == 0x03) && !sm2_bn_is_odd(_y)) {
  977. sm2_fp_neg(_y, _y);
  978. }
  979. sm2_bn_to_bytes(_x, P->x);
  980. sm2_bn_to_bytes(_y, P->y);
  981. sm2_bn_clean(_x);
  982. sm2_bn_clean(_y);
  983. sm2_bn_clean(_g);
  984. sm2_bn_clean(_z);
  985. if (!sm2_point_is_on_curve(P)) {
  986. error_print();
  987. return -1;
  988. }
  989. return 1;
  990. }
  991. int sm2_point_from_xy(SM2_POINT *P, const uint8_t x[32], const uint8_t y[32])
  992. {
  993. memcpy(P->x, x, 32);
  994. memcpy(P->y, y, 32);
  995. return sm2_point_is_on_curve(P);
  996. }
  997. int sm2_point_add(SM2_POINT *R, const SM2_POINT *P, const SM2_POINT *Q)
  998. {
  999. SM2_JACOBIAN_POINT P_;
  1000. SM2_JACOBIAN_POINT Q_;
  1001. sm2_jacobian_point_from_bytes(&P_, (uint8_t *)P);
  1002. sm2_jacobian_point_from_bytes(&Q_, (uint8_t *)Q);
  1003. sm2_jacobian_point_add(&P_, &P_, &Q_);
  1004. sm2_jacobian_point_to_bytes(&P_, (uint8_t *)R);
  1005. return 1;
  1006. }
  1007. int sm2_point_sub(SM2_POINT *R, const SM2_POINT *P, const SM2_POINT *Q)
  1008. {
  1009. SM2_JACOBIAN_POINT P_;
  1010. SM2_JACOBIAN_POINT Q_;
  1011. sm2_jacobian_point_from_bytes(&P_, (uint8_t *)P);
  1012. sm2_jacobian_point_from_bytes(&Q_, (uint8_t *)Q);
  1013. sm2_jacobian_point_sub(&P_, &P_, &Q_);
  1014. sm2_jacobian_point_to_bytes(&P_, (uint8_t *)R);
  1015. return 1;
  1016. }
  1017. int sm2_point_neg(SM2_POINT *R, const SM2_POINT *P)
  1018. {
  1019. SM2_JACOBIAN_POINT P_;
  1020. sm2_jacobian_point_from_bytes(&P_, (uint8_t *)P);
  1021. sm2_jacobian_point_neg(&P_, &P_);
  1022. sm2_jacobian_point_to_bytes(&P_, (uint8_t *)R);
  1023. return 1;
  1024. }
  1025. int sm2_point_dbl(SM2_POINT *R, const SM2_POINT *P)
  1026. {
  1027. SM2_JACOBIAN_POINT P_;
  1028. sm2_jacobian_point_from_bytes(&P_, (uint8_t *)P);
  1029. sm2_jacobian_point_dbl(&P_, &P_);
  1030. sm2_jacobian_point_to_bytes(&P_, (uint8_t *)R);
  1031. return 1;
  1032. }
  1033. int sm2_point_mul(SM2_POINT *R, const uint8_t k[32], const SM2_POINT *P)
  1034. {
  1035. SM2_BN _k;
  1036. SM2_JACOBIAN_POINT _P;
  1037. sm2_bn_from_bytes(_k, k);
  1038. sm2_jacobian_point_from_bytes(&_P, (uint8_t *)P);
  1039. sm2_jacobian_point_mul(&_P, _k, &_P);
  1040. sm2_jacobian_point_to_bytes(&_P, (uint8_t *)R);
  1041. sm2_bn_clean(_k);
  1042. return 1;
  1043. }
  1044. int sm2_point_mul_generator(SM2_POINT *R, const uint8_t k[32])
  1045. {
  1046. SM2_BN _k;
  1047. SM2_JACOBIAN_POINT _R;
  1048. sm2_bn_from_bytes(_k, k);
  1049. sm2_jacobian_point_mul_generator(&_R, _k);
  1050. sm2_jacobian_point_to_bytes(&_R, (uint8_t *)R);
  1051. sm2_bn_clean(_k);
  1052. return 1;
  1053. }
  1054. int sm2_point_mul_sum(SM2_POINT *R, const uint8_t k[32], const SM2_POINT *P, const uint8_t s[32])
  1055. {
  1056. SM2_BN _k;
  1057. SM2_JACOBIAN_POINT _P;
  1058. SM2_BN _s;
  1059. sm2_bn_from_bytes(_k, k);
  1060. sm2_jacobian_point_from_bytes(&_P, (uint8_t *)P);
  1061. sm2_bn_from_bytes(_s, s);
  1062. sm2_jacobian_point_mul_sum(&_P, _k, &_P, _s);
  1063. sm2_jacobian_point_to_bytes(&_P, (uint8_t *)R);
  1064. sm2_bn_clean(_k);
  1065. sm2_bn_clean(_s);
  1066. return 1;
  1067. }
  1068. int sm2_point_print(FILE *fp, int fmt, int ind, const char *label, const SM2_POINT *P)
  1069. {
  1070. format_print(fp, fmt, ind, "%s\n", label);
  1071. ind += 4;
  1072. format_bytes(fp, fmt, ind, "x", P->x, 32);
  1073. format_bytes(fp, fmt, ind, "y", P->y, 32);
  1074. return 1;
  1075. }
  1076. void sm2_point_to_compressed_octets(const SM2_POINT *P, uint8_t out[33])
  1077. {
  1078. *out++ = (P->y[31] & 0x01) ? 0x03 : 0x02;
  1079. memcpy(out, P->x, 32);
  1080. }
  1081. void sm2_point_to_uncompressed_octets(const SM2_POINT *P, uint8_t out[65])
  1082. {
  1083. *out++ = 0x04;
  1084. memcpy(out, P, 64);
  1085. }
  1086. int sm2_point_from_octets(SM2_POINT *P, const uint8_t *in, size_t inlen)
  1087. {
  1088. if ((*in == 0x02 || *in == 0x03) && inlen == 33) {
  1089. if (sm2_point_from_x(P, in + 1, *in) != 1) {
  1090. error_print();
  1091. return -1;
  1092. }
  1093. } else if (*in == 0x04 && inlen == 65) {
  1094. if (sm2_point_from_xy(P, in + 1, in + 33) != 1) {
  1095. error_print();
  1096. return -1;
  1097. }
  1098. } else {
  1099. error_print();
  1100. return -1;
  1101. }
  1102. return 1;
  1103. }
  1104. int sm2_point_to_der(const SM2_POINT *P, uint8_t **out, size_t *outlen)
  1105. {
  1106. uint8_t octets[65];
  1107. if (!P) {
  1108. return 0;
  1109. }
  1110. sm2_point_to_uncompressed_octets(P, octets);
  1111. if (asn1_octet_string_to_der(octets, sizeof(octets), out, outlen) != 1) {
  1112. error_print();
  1113. return -1;
  1114. }
  1115. return 1;
  1116. }
  1117. int sm2_point_from_der(SM2_POINT *P, const uint8_t **in, size_t *inlen)
  1118. {
  1119. int ret;
  1120. const uint8_t *d;
  1121. size_t dlen;
  1122. if ((ret = asn1_octet_string_from_der(&d, &dlen, in, inlen)) != 1) {
  1123. if (ret < 0) error_print();
  1124. return ret;
  1125. }
  1126. if (dlen != 65) {
  1127. error_print();
  1128. return -1;
  1129. }
  1130. if (sm2_point_from_octets(P, d, dlen) != 1) {
  1131. error_print();
  1132. return -1;
  1133. }
  1134. return 1;
  1135. }
  1136. int sm2_point_from_hash(SM2_POINT *R, const uint8_t *data, size_t datalen)
  1137. {
  1138. SM2_BN u;
  1139. SM2_Fp x;
  1140. SM2_Fp y;
  1141. SM2_Fp s;
  1142. SM2_Fp s_;
  1143. uint8_t dgst[32];
  1144. // u = (p + 1)/4
  1145. sm2_bn_add(u, SM2_P, SM2_ONE);
  1146. sm2_bn_rshift(u, u, 2);
  1147. do {
  1148. sm3_digest(data, datalen, dgst);
  1149. sm2_bn_from_bytes(x, dgst);
  1150. if (sm2_bn_cmp(x, SM2_P) >= 0) {
  1151. sm2_bn_sub(x, x, SM2_P);
  1152. }
  1153. // s = y^2 = x^3 + a*x + b
  1154. sm2_fp_sqr(s, x);
  1155. sm2_fp_sub(s, s, SM2_THREE);
  1156. sm2_fp_mul(s, s, x);
  1157. sm2_fp_add(s, s, SM2_B);
  1158. // y = s^((p+1)/4) = (sqrt(s) (mod p))
  1159. sm2_fp_exp(y, s, u);
  1160. sm2_fp_sqr(s_, y);
  1161. data = dgst;
  1162. datalen = sizeof(dgst);
  1163. } while (sm2_bn_cmp(s, s_) != 0);
  1164. sm2_bn_to_bytes(x, R->x);
  1165. sm2_bn_to_bytes(y, R->y);
  1166. return 1;
  1167. }