sm2_lib.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  1. /*
  2. * Copyright 2014-2023 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 <stdlib.h>
  12. #include <gmssl/mem.h>
  13. #include <gmssl/sm2.h>
  14. #include <gmssl/sm3.h>
  15. #include <gmssl/asn1.h>
  16. #include <gmssl/error.h>
  17. #include <gmssl/endian.h>
  18. extern const SM2_BN SM2_N;
  19. extern const SM2_BN SM2_ONE;
  20. int sm2_do_sign(const SM2_KEY *key, const uint8_t dgst[32], SM2_SIGNATURE *sig)
  21. {
  22. SM2_JACOBIAN_POINT _P, *P = &_P;
  23. SM2_BN d;
  24. SM2_BN d_inv;
  25. SM2_BN e;
  26. SM2_BN k;
  27. SM2_BN x;
  28. SM2_BN t;
  29. SM2_BN r;
  30. SM2_BN s;
  31. //fprintf(stderr, "sm2_do_sign\n");
  32. sm2_bn_from_bytes(d, key->private_key);
  33. // compute (d + 1)^-1 (mod n)
  34. sm2_fn_add(d_inv, d, SM2_ONE); //sm2_bn_print(stderr, 0, 4, "(1+d)", d_inv);
  35. if (sm2_bn_is_zero(d_inv)) {
  36. error_print();
  37. return -1;
  38. }
  39. sm2_fn_inv(d_inv, d_inv); //sm2_bn_print(stderr, 0, 4, "(1+d)^-1", d_inv);
  40. // e = H(M)
  41. sm2_bn_from_bytes(e, dgst); //sm2_bn_print(stderr, 0, 4, "e", e);
  42. retry:
  43. // rand k in [1, n - 1]
  44. do {
  45. if (sm2_fn_rand(k) != 1) {
  46. error_print();
  47. return -1;
  48. }
  49. } while (sm2_bn_is_zero(k)); //sm2_bn_print(stderr, 0, 4, "k", k);
  50. // (x, y) = kG
  51. sm2_jacobian_point_mul_generator(P, k);
  52. sm2_jacobian_point_get_xy(P, x, NULL);
  53. //sm2_bn_print(stderr, 0, 4, "x", x);
  54. // r = e + x (mod n)
  55. if (sm2_bn_cmp(e, SM2_N) >= 0) {
  56. sm2_bn_sub(e, e, SM2_N);
  57. }
  58. if (sm2_bn_cmp(x, SM2_N) >= 0) {
  59. sm2_bn_sub(x, x, SM2_N);
  60. }
  61. sm2_fn_add(r, e, x); //sm2_bn_print(stderr, 0, 4, "r = e + x (mod n)", r);
  62. // if r == 0 or r + k == n re-generate k
  63. sm2_bn_add(t, r, k);
  64. if (sm2_bn_is_zero(r) || sm2_bn_cmp(t, SM2_N) == 0) {
  65. //sm2_bn_print(stderr, 0, 4, "r + k", t);
  66. goto retry;
  67. }
  68. // s = ((1 + d)^-1 * (k - r * d)) mod n
  69. sm2_fn_mul(t, r, d); //sm2_bn_print(stderr, 0, 4, "r*d", t);
  70. sm2_fn_sub(k, k, t); //sm2_bn_print(stderr, 0, 4, "k-r*d", k);
  71. sm2_fn_mul(s, d_inv, k); //sm2_bn_print(stderr, 0, 4, "s = ((1 + d)^-1 * (k - r * d)) mod n", s);
  72. // check s != 0
  73. if (sm2_bn_is_zero(s)) {
  74. goto retry;
  75. }
  76. sm2_bn_to_bytes(r, sig->r); //sm2_bn_print_bn(stderr, 0, 4, "r", r);
  77. sm2_bn_to_bytes(s, sig->s); //sm2_bn_print_bn(stderr, 0, 4, "s", s);
  78. gmssl_secure_clear(d, sizeof(d));
  79. gmssl_secure_clear(d_inv, sizeof(d_inv ));
  80. gmssl_secure_clear(k, sizeof(k));
  81. gmssl_secure_clear(t, sizeof(t));
  82. return 1;
  83. }
  84. int sm2_do_verify(const SM2_KEY *key, const uint8_t dgst[32], const SM2_SIGNATURE *sig)
  85. {
  86. SM2_JACOBIAN_POINT _P, *P = &_P;
  87. SM2_JACOBIAN_POINT _R, *R = &_R;
  88. SM2_BN r;
  89. SM2_BN s;
  90. SM2_BN e;
  91. SM2_BN x;
  92. SM2_BN t;
  93. // parse public key
  94. sm2_jacobian_point_from_bytes(P, (const uint8_t *)&key->public_key);
  95. //sm2_jacobian_point_print(stderr, 0, 4, "P", P);
  96. // parse signature values
  97. sm2_bn_from_bytes(r, sig->r); //sm2_bn_print(stderr, 0, 4, "r", r);
  98. sm2_bn_from_bytes(s, sig->s); //sm2_bn_print(stderr, 0, 4, "s", s);
  99. // check r, s in [1, n-1]
  100. if (sm2_bn_is_zero(r) == 1
  101. || sm2_bn_cmp(r, SM2_N) >= 0
  102. || sm2_bn_is_zero(s) == 1
  103. || sm2_bn_cmp(s, SM2_N) >= 0) {
  104. error_print();
  105. return -1;
  106. }
  107. // e = H(M)
  108. sm2_bn_from_bytes(e, dgst); //sm2_bn_print(stderr, 0, 4, "e = H(M)", e);
  109. // t = r + s (mod n), check t != 0
  110. sm2_fn_add(t, r, s); //sm2_bn_print(stderr, 0, 4, "t = r + s (mod n)", t);
  111. if (sm2_bn_is_zero(t)) {
  112. error_print();
  113. return -1;
  114. }
  115. // Q = s * G + t * P
  116. sm2_jacobian_point_mul_sum(R, t, P, s);
  117. sm2_jacobian_point_get_xy(R, x, NULL);
  118. //sm2_bn_print(stderr, 0, 4, "x", x);
  119. // r' = e + x (mod n)
  120. if (sm2_bn_cmp(e, SM2_N) >= 0) {
  121. sm2_bn_sub(e, e, SM2_N);
  122. }
  123. if (sm2_bn_cmp(x, SM2_N) >= 0) {
  124. sm2_bn_sub(x, x, SM2_N);
  125. }
  126. sm2_fn_add(e, e, x); //sm2_bn_print(stderr, 0, 4, "e + x (mod n)", e);
  127. // check if r == r'
  128. if (sm2_bn_cmp(e, r) != 0) {
  129. error_print();
  130. return -1;
  131. }
  132. return 1;
  133. }
  134. int sm2_signature_to_der(const SM2_SIGNATURE *sig, uint8_t **out, size_t *outlen)
  135. {
  136. size_t len = 0;
  137. if (!sig) {
  138. return 0;
  139. }
  140. if (asn1_integer_to_der(sig->r, 32, NULL, &len) != 1
  141. || asn1_integer_to_der(sig->s, 32, NULL, &len) != 1
  142. || asn1_sequence_header_to_der(len, out, outlen) != 1
  143. || asn1_integer_to_der(sig->r, 32, out, outlen) != 1
  144. || asn1_integer_to_der(sig->s, 32, out, outlen) != 1) {
  145. error_print();
  146. return -1;
  147. }
  148. return 1;
  149. }
  150. int sm2_signature_from_der(SM2_SIGNATURE *sig, const uint8_t **in, size_t *inlen)
  151. {
  152. int ret;
  153. const uint8_t *d;
  154. size_t dlen;
  155. const uint8_t *r;
  156. size_t rlen;
  157. const uint8_t *s;
  158. size_t slen;
  159. if ((ret = asn1_sequence_from_der(&d, &dlen, in, inlen)) != 1) {
  160. if (ret < 0) error_print();
  161. return ret;
  162. }
  163. if (asn1_integer_from_der(&r, &rlen, &d, &dlen) != 1
  164. || asn1_integer_from_der(&s, &slen, &d, &dlen) != 1
  165. || asn1_length_le(rlen, 32) != 1
  166. || asn1_length_le(slen, 32) != 1
  167. || asn1_length_is_zero(dlen) != 1) {
  168. error_print();
  169. return -1;
  170. }
  171. memset(sig, 0, sizeof(*sig));
  172. memcpy(sig->r + 32 - rlen, r, rlen);
  173. memcpy(sig->s + 32 - slen, s, slen);
  174. return 1;
  175. }
  176. int sm2_signature_print(FILE *fp, int fmt, int ind, const char *label, const uint8_t *a, size_t alen)
  177. {
  178. SM2_SIGNATURE sig;
  179. format_print(fp, fmt, ind, "%s\n", label);
  180. ind += 4;
  181. if (sm2_signature_from_der(&sig, &a, &alen) != 1
  182. || asn1_length_is_zero(alen) != 1) {
  183. error_print();
  184. return -1;
  185. }
  186. format_bytes(fp, fmt, ind, "r", sig.r, 32);
  187. format_bytes(fp, fmt, ind, "s", sig.s, 32);
  188. return 1;
  189. }
  190. int sm2_sign(const SM2_KEY *key, const uint8_t dgst[32], uint8_t *sigbuf, size_t *siglen)
  191. {
  192. SM2_SIGNATURE sig;
  193. if (!key || !dgst || !sigbuf || !siglen) {
  194. error_print();
  195. return -1;
  196. }
  197. if (sm2_do_sign(key, dgst, &sig) != 1) {
  198. error_print();
  199. return -1;
  200. }
  201. *siglen = 0;
  202. if (sm2_signature_to_der(&sig, &sigbuf, siglen) != 1) {
  203. error_print();
  204. return -1;
  205. }
  206. return 1;
  207. }
  208. int sm2_sign_fixlen(const SM2_KEY *key, const uint8_t dgst[32], size_t siglen, uint8_t *sig)
  209. {
  210. unsigned int trys = 200; // 200 trys is engouh
  211. uint8_t buf[SM2_MAX_SIGNATURE_SIZE];
  212. size_t len;
  213. switch (siglen) {
  214. case SM2_signature_compact_size:
  215. case SM2_signature_typical_size:
  216. case SM2_signature_max_size:
  217. break;
  218. default:
  219. error_print();
  220. return -1;
  221. }
  222. while (trys--) {
  223. if (sm2_sign(key, dgst, buf, &len) != 1) {
  224. error_print();
  225. return -1;
  226. }
  227. if (len == siglen) {
  228. memcpy(sig, buf, len);
  229. return 1;
  230. }
  231. }
  232. // might caused by bad randomness
  233. error_print();
  234. return -1;
  235. }
  236. int sm2_verify(const SM2_KEY *key, const uint8_t dgst[32], const uint8_t *sigbuf, size_t siglen)
  237. {
  238. SM2_SIGNATURE sig;
  239. if (!key || !dgst || !sigbuf || !siglen) {
  240. error_print();
  241. return -1;
  242. }
  243. if (sm2_signature_from_der(&sig, &sigbuf, &siglen) != 1
  244. || asn1_length_is_zero(siglen) != 1) {
  245. error_print();
  246. return -1;
  247. }
  248. if (sm2_do_verify(key, dgst, &sig) != 1) {
  249. error_print();
  250. return -1;
  251. }
  252. return 1;
  253. }
  254. int sm2_compute_z(uint8_t z[32], const SM2_POINT *pub, const char *id, size_t idlen)
  255. {
  256. SM3_CTX ctx;
  257. uint8_t zin[18 + 32 * 6] = {
  258. 0x00, 0x80,
  259. 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,
  260. 0xFF,0xFF,0xFF,0xFE,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  261. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFC,
  262. 0x28,0xE9,0xFA,0x9E,0x9D,0x9F,0x5E,0x34,0x4D,0x5A,0x9E,0x4B,0xCF,0x65,0x09,0xA7,
  263. 0xF3,0x97,0x89,0xF5,0x15,0xAB,0x8F,0x92,0xDD,0xBC,0xBD,0x41,0x4D,0x94,0x0E,0x93,
  264. 0x32,0xC4,0xAE,0x2C,0x1F,0x19,0x81,0x19,0x5F,0x99,0x04,0x46,0x6A,0x39,0xC9,0x94,
  265. 0x8F,0xE3,0x0B,0xBF,0xF2,0x66,0x0B,0xE1,0x71,0x5A,0x45,0x89,0x33,0x4C,0x74,0xC7,
  266. 0xBC,0x37,0x36,0xA2,0xF4,0xF6,0x77,0x9C,0x59,0xBD,0xCE,0xE3,0x6B,0x69,0x21,0x53,
  267. 0xD0,0xA9,0x87,0x7C,0xC6,0x2A,0x47,0x40,0x02,0xDF,0x32,0xE5,0x21,0x39,0xF0,0xA0,
  268. };
  269. if (!z || !pub || !id) {
  270. error_print();
  271. return -1;
  272. }
  273. memcpy(&zin[18 + 32 * 4], pub->x, 32);
  274. memcpy(&zin[18 + 32 * 5], pub->y, 32);
  275. sm3_init(&ctx);
  276. if (strcmp(id, SM2_DEFAULT_ID) == 0) {
  277. sm3_update(&ctx, zin, sizeof(zin));
  278. } else {
  279. uint8_t idbits[2];
  280. idbits[0] = (uint8_t)(idlen >> 5);
  281. idbits[1] = (uint8_t)(idlen << 3);
  282. sm3_update(&ctx, idbits, 2);
  283. sm3_update(&ctx, (uint8_t *)id, idlen);
  284. sm3_update(&ctx, zin + 18, 32 * 6);
  285. }
  286. sm3_finish(&ctx, z);
  287. return 1;
  288. }
  289. int sm2_sign_init(SM2_SIGN_CTX *ctx, const SM2_KEY *key, const char *id, size_t idlen)
  290. {
  291. if (!ctx || !key) {
  292. error_print();
  293. return -1;
  294. }
  295. ctx->key = *key;
  296. sm3_init(&ctx->sm3_ctx);
  297. if (id) {
  298. uint8_t z[SM3_DIGEST_SIZE];
  299. if (idlen <= 0 || idlen > SM2_MAX_ID_LENGTH) {
  300. error_print();
  301. return -1;
  302. }
  303. sm2_compute_z(z, &key->public_key, id, idlen);
  304. sm3_update(&ctx->sm3_ctx, z, sizeof(z));
  305. }
  306. return 1;
  307. }
  308. int sm2_sign_update(SM2_SIGN_CTX *ctx, const uint8_t *data, size_t datalen)
  309. {
  310. if (!ctx) {
  311. error_print();
  312. return -1;
  313. }
  314. if (data && datalen > 0) {
  315. sm3_update(&ctx->sm3_ctx, data, datalen);
  316. }
  317. return 1;
  318. }
  319. int sm2_sign_finish(SM2_SIGN_CTX *ctx, uint8_t *sig, size_t *siglen)
  320. {
  321. uint8_t dgst[SM3_DIGEST_SIZE];
  322. if (!ctx || !sig || !siglen) {
  323. error_print();
  324. return -1;
  325. }
  326. sm3_finish(&ctx->sm3_ctx, dgst);
  327. if (sm2_sign(&ctx->key, dgst, sig, siglen) != 1) {
  328. error_print();
  329. return -1;
  330. }
  331. return 1;
  332. }
  333. int sm2_sign_finish_fixlen(SM2_SIGN_CTX *ctx, size_t siglen, uint8_t *sig)
  334. {
  335. uint8_t dgst[SM3_DIGEST_SIZE];
  336. if (!ctx || !sig || !siglen) {
  337. error_print();
  338. return -1;
  339. }
  340. sm3_finish(&ctx->sm3_ctx, dgst);
  341. if (sm2_sign_fixlen(&ctx->key, dgst, siglen, sig) != 1) {
  342. error_print();
  343. return -1;
  344. }
  345. return 1;
  346. }
  347. int sm2_verify_init(SM2_SIGN_CTX *ctx, const SM2_KEY *key, const char *id, size_t idlen)
  348. {
  349. if (!ctx || !key) {
  350. error_print();
  351. return -1;
  352. }
  353. memset(ctx, 0, sizeof(*ctx));
  354. ctx->key.public_key = key->public_key;
  355. sm3_init(&ctx->sm3_ctx);
  356. if (id) {
  357. uint8_t z[SM3_DIGEST_SIZE];
  358. if (idlen <= 0 || idlen > SM2_MAX_ID_LENGTH) {
  359. error_print();
  360. return -1;
  361. }
  362. sm2_compute_z(z, &key->public_key, id, idlen);
  363. sm3_update(&ctx->sm3_ctx, z, sizeof(z));
  364. }
  365. return 1;
  366. }
  367. int sm2_verify_update(SM2_SIGN_CTX *ctx, const uint8_t *data, size_t datalen)
  368. {
  369. if (!ctx) {
  370. error_print();
  371. return -1;
  372. }
  373. if (data && datalen > 0) {
  374. sm3_update(&ctx->sm3_ctx, data, datalen);
  375. }
  376. return 1;
  377. }
  378. int sm2_verify_finish(SM2_SIGN_CTX *ctx, const uint8_t *sig, size_t siglen)
  379. {
  380. uint8_t dgst[SM3_DIGEST_SIZE];
  381. if (!ctx || !sig) {
  382. error_print();
  383. return -1;
  384. }
  385. sm3_finish(&ctx->sm3_ctx, dgst);
  386. if (sm2_verify(&ctx->key, dgst, sig, siglen) != 1) {
  387. error_print();
  388. return -1;
  389. }
  390. return 1;
  391. }
  392. int sm2_kdf(const uint8_t *in, size_t inlen, size_t outlen, uint8_t *out)
  393. {
  394. SM3_CTX ctx;
  395. uint8_t counter_be[4];
  396. uint8_t dgst[SM3_DIGEST_SIZE];
  397. uint32_t counter = 1;
  398. size_t len;
  399. while (outlen) {
  400. PUTU32(counter_be, counter);
  401. counter++;
  402. sm3_init(&ctx);
  403. sm3_update(&ctx, in, inlen);
  404. sm3_update(&ctx, counter_be, sizeof(counter_be));
  405. sm3_finish(&ctx, dgst);
  406. len = outlen < SM3_DIGEST_SIZE ? outlen : SM3_DIGEST_SIZE;
  407. memcpy(out, dgst, len);
  408. out += len;
  409. outlen -= len;
  410. }
  411. memset(&ctx, 0, sizeof(SM3_CTX));
  412. memset(dgst, 0, sizeof(dgst));
  413. return 1;
  414. }
  415. static int all_zero(const uint8_t *buf, size_t len)
  416. {
  417. size_t i;
  418. for (i = 0; i < len; i++) {
  419. if (buf[i]) {
  420. return 0;
  421. }
  422. }
  423. return 1;
  424. }
  425. int sm2_do_encrypt(const SM2_KEY *key, const uint8_t *in, size_t inlen, SM2_CIPHERTEXT *out)
  426. {
  427. SM2_BN k;
  428. SM2_JACOBIAN_POINT _P, *P = &_P;
  429. SM2_JACOBIAN_POINT _C1, *C1 = &_C1;
  430. SM2_JACOBIAN_POINT _kP, *kP = &_kP;
  431. uint8_t x2y2[64];
  432. SM3_CTX sm3_ctx;
  433. if (!(SM2_MIN_PLAINTEXT_SIZE <= inlen && inlen <= SM2_MAX_PLAINTEXT_SIZE)) {
  434. error_print();
  435. return -1;
  436. }
  437. sm2_jacobian_point_from_bytes(P, (uint8_t *)&key->public_key);
  438. // S = h * P, check S != O
  439. // for sm2 curve, h == 1 and S == P
  440. // SM2_POINT can not present point at infinity, do do nothing here
  441. retry:
  442. // rand k in [1, n - 1]
  443. do {
  444. if (sm2_fn_rand(k) != 1) {
  445. error_print();
  446. return -1;
  447. }
  448. } while (sm2_bn_is_zero(k)); //sm2_bn_print(stderr, 0, 4, "k", k);
  449. // output C1 = k * G = (x1, y1)
  450. sm2_jacobian_point_mul_generator(C1, k);
  451. sm2_jacobian_point_to_bytes(C1, (uint8_t *)&out->point);
  452. // k * P = (x2, y2)
  453. sm2_jacobian_point_mul(kP, k, P);
  454. sm2_jacobian_point_to_bytes(kP, x2y2);
  455. // t = KDF(x2 || y2, inlen)
  456. sm2_kdf(x2y2, 64, inlen, out->ciphertext);
  457. // if t is all zero, retry
  458. if (all_zero(out->ciphertext, inlen)) {
  459. goto retry;
  460. }
  461. // output C2 = M xor t
  462. gmssl_memxor(out->ciphertext, out->ciphertext, in, inlen);
  463. out->ciphertext_size = (uint32_t)inlen;
  464. // output C3 = Hash(x2 || m || y2)
  465. sm3_init(&sm3_ctx);
  466. sm3_update(&sm3_ctx, x2y2, 32);
  467. sm3_update(&sm3_ctx, in, inlen);
  468. sm3_update(&sm3_ctx, x2y2 + 32, 32);
  469. sm3_finish(&sm3_ctx, out->hash);
  470. gmssl_secure_clear(k, sizeof(k));
  471. gmssl_secure_clear(kP, sizeof(SM2_JACOBIAN_POINT));
  472. gmssl_secure_clear(x2y2, sizeof(x2y2));
  473. return 1;
  474. }
  475. int sm2_do_encrypt_fixlen(const SM2_KEY *key, const uint8_t *in, size_t inlen, int point_size, SM2_CIPHERTEXT *out)
  476. {
  477. unsigned int trys = 200;
  478. SM2_BN k;
  479. SM2_JACOBIAN_POINT _P, *P = &_P;
  480. SM2_JACOBIAN_POINT _C1, *C1 = &_C1;
  481. SM2_JACOBIAN_POINT _kP, *kP = &_kP;
  482. uint8_t x2y2[64];
  483. SM3_CTX sm3_ctx;
  484. if (!(SM2_MIN_PLAINTEXT_SIZE <= inlen && inlen <= SM2_MAX_PLAINTEXT_SIZE)) {
  485. error_print();
  486. return -1;
  487. }
  488. switch (point_size) {
  489. case SM2_ciphertext_compact_point_size:
  490. case SM2_ciphertext_typical_point_size:
  491. case SM2_ciphertext_max_point_size:
  492. break;
  493. default:
  494. error_print();
  495. return -1;
  496. }
  497. sm2_jacobian_point_from_bytes(P, (uint8_t *)&key->public_key);
  498. // S = h * P, check S != O
  499. // for sm2 curve, h == 1 and S == P
  500. // SM2_POINT can not present point at infinity, do do nothing here
  501. retry:
  502. // rand k in [1, n - 1]
  503. do {
  504. if (sm2_fn_rand(k) != 1) {
  505. error_print();
  506. return -1;
  507. }
  508. } while (sm2_bn_is_zero(k)); //sm2_bn_print(stderr, 0, 4, "k", k);
  509. // output C1 = k * G = (x1, y1)
  510. sm2_jacobian_point_mul_generator(C1, k);
  511. sm2_jacobian_point_to_bytes(C1, (uint8_t *)&out->point);
  512. // check fixlen
  513. if (trys) {
  514. size_t len = 0;
  515. asn1_integer_to_der(out->point.x, 32, NULL, &len);
  516. asn1_integer_to_der(out->point.y, 32, NULL, &len);
  517. if (len != point_size) {
  518. trys--;
  519. goto retry;
  520. }
  521. } else {
  522. gmssl_secure_clear(k, sizeof(k));
  523. error_print();
  524. return -1;
  525. }
  526. // k * P = (x2, y2)
  527. sm2_jacobian_point_mul(kP, k, P);
  528. sm2_jacobian_point_to_bytes(kP, x2y2);
  529. // t = KDF(x2 || y2, inlen)
  530. sm2_kdf(x2y2, 64, inlen, out->ciphertext);
  531. // if t is all zero, retry
  532. if (all_zero(out->ciphertext, inlen)) {
  533. goto retry;
  534. }
  535. // output C2 = M xor t
  536. gmssl_memxor(out->ciphertext, out->ciphertext, in, inlen);
  537. out->ciphertext_size = (uint32_t)inlen;
  538. // output C3 = Hash(x2 || m || y2)
  539. sm3_init(&sm3_ctx);
  540. sm3_update(&sm3_ctx, x2y2, 32);
  541. sm3_update(&sm3_ctx, in, inlen);
  542. sm3_update(&sm3_ctx, x2y2 + 32, 32);
  543. sm3_finish(&sm3_ctx, out->hash);
  544. gmssl_secure_clear(k, sizeof(k));
  545. gmssl_secure_clear(kP, sizeof(SM2_JACOBIAN_POINT));
  546. gmssl_secure_clear(x2y2, sizeof(x2y2));
  547. return 1;
  548. }
  549. int sm2_do_decrypt(const SM2_KEY *key, const SM2_CIPHERTEXT *in, uint8_t *out, size_t *outlen)
  550. {
  551. int ret = -1;
  552. SM2_BN d;
  553. SM2_JACOBIAN_POINT _C1, *C1 = &_C1;
  554. uint8_t x2y2[64];
  555. SM3_CTX sm3_ctx;
  556. uint8_t hash[32];
  557. // check C1 is on sm2 curve
  558. sm2_jacobian_point_from_bytes(C1, (uint8_t *)&in->point);
  559. if (!sm2_jacobian_point_is_on_curve(C1)) {
  560. error_print();
  561. return -1;
  562. }
  563. // check if S = h * C1 is point at infinity
  564. // this will not happen, as SM2_POINT can not present point at infinity
  565. // d * C1 = (x2, y2)
  566. sm2_bn_from_bytes(d, key->private_key);
  567. sm2_jacobian_point_mul(C1, d, C1);
  568. // t = KDF(x2 || y2, klen) and check t is not all zeros
  569. sm2_jacobian_point_to_bytes(C1, x2y2);
  570. sm2_kdf(x2y2, 64, in->ciphertext_size, out);
  571. if (all_zero(out, in->ciphertext_size)) {
  572. error_print();
  573. goto end;
  574. }
  575. // M = C2 xor t
  576. gmssl_memxor(out, out, in->ciphertext, in->ciphertext_size);
  577. *outlen = in->ciphertext_size;
  578. // u = Hash(x2 || M || y2)
  579. sm3_init(&sm3_ctx);
  580. sm3_update(&sm3_ctx, x2y2, 32);
  581. sm3_update(&sm3_ctx, out, in->ciphertext_size);
  582. sm3_update(&sm3_ctx, x2y2 + 32, 32);
  583. sm3_finish(&sm3_ctx, hash);
  584. // check if u == C3
  585. if (memcmp(in->hash, hash, sizeof(hash)) != 0) {
  586. error_print();
  587. goto end;
  588. }
  589. ret = 1;
  590. end:
  591. gmssl_secure_clear(d, sizeof(d));
  592. gmssl_secure_clear(C1, sizeof(SM2_JACOBIAN_POINT));
  593. gmssl_secure_clear(x2y2, sizeof(x2y2));
  594. return ret;
  595. }
  596. int sm2_ciphertext_to_der(const SM2_CIPHERTEXT *C, uint8_t **out, size_t *outlen)
  597. {
  598. size_t len = 0;
  599. if (!C) {
  600. return 0;
  601. }
  602. if (asn1_integer_to_der(C->point.x, 32, NULL, &len) != 1
  603. || asn1_integer_to_der(C->point.y, 32, NULL, &len) != 1
  604. || asn1_octet_string_to_der(C->hash, 32, NULL, &len) != 1
  605. || asn1_octet_string_to_der(C->ciphertext, C->ciphertext_size, NULL, &len) != 1
  606. || asn1_sequence_header_to_der(len, out, outlen) != 1
  607. || asn1_integer_to_der(C->point.x, 32, out, outlen) != 1
  608. || asn1_integer_to_der(C->point.y, 32, out, outlen) != 1
  609. || asn1_octet_string_to_der(C->hash, 32, out, outlen) != 1
  610. || asn1_octet_string_to_der(C->ciphertext, C->ciphertext_size, out, outlen) != 1) {
  611. error_print();
  612. return -1;
  613. }
  614. return 1;
  615. }
  616. int sm2_ciphertext_from_der(SM2_CIPHERTEXT *C, const uint8_t **in, size_t *inlen)
  617. {
  618. int ret;
  619. const uint8_t *d;
  620. size_t dlen;
  621. const uint8_t *x;
  622. const uint8_t *y;
  623. const uint8_t *hash;
  624. const uint8_t *c;
  625. size_t xlen, ylen, hashlen, clen;
  626. if ((ret = asn1_sequence_from_der(&d, &dlen, in, inlen)) != 1) {
  627. if (ret < 0) error_print();
  628. return ret;
  629. }
  630. if (asn1_integer_from_der(&x, &xlen, &d, &dlen) != 1
  631. || asn1_length_le(xlen, 32) != 1) {
  632. error_print();
  633. return -1;
  634. }
  635. if (asn1_integer_from_der(&y, &ylen, &d, &dlen) != 1
  636. || asn1_length_le(ylen, 32) != 1) {
  637. error_print();
  638. return -1;
  639. }
  640. if (asn1_octet_string_from_der(&hash, &hashlen, &d, &dlen) != 1
  641. || asn1_check(hashlen == 32) != 1) {
  642. error_print();
  643. return -1;
  644. }
  645. if (asn1_octet_string_from_der(&c, &clen, &d, &dlen) != 1
  646. // || asn1_length_is_zero(clen) == 1
  647. || asn1_length_le(clen, SM2_MAX_PLAINTEXT_SIZE) != 1) {
  648. error_print();
  649. return -1;
  650. }
  651. if (asn1_length_is_zero(dlen) != 1) {
  652. error_print();
  653. return -1;
  654. }
  655. memset(C, 0, sizeof(SM2_CIPHERTEXT));
  656. memcpy(C->point.x + 32 - xlen, x, xlen);
  657. memcpy(C->point.y + 32 - ylen, y, ylen);
  658. if (sm2_point_is_on_curve(&C->point) != 1) {
  659. error_print();
  660. return -1;
  661. }
  662. memcpy(C->hash, hash, hashlen);
  663. memcpy(C->ciphertext, c, clen);
  664. C->ciphertext_size = (uint8_t)clen;
  665. return 1;
  666. }
  667. int sm2_ciphertext_print(FILE *fp, int fmt, int ind, const char *label, const uint8_t *a, size_t alen)
  668. {
  669. uint8_t buf[512] = {0};
  670. SM2_CIPHERTEXT *c = (SM2_CIPHERTEXT *)buf;
  671. if (sm2_ciphertext_from_der(c, &a, &alen) != 1
  672. || asn1_length_is_zero(alen) != 1) {
  673. error_print();
  674. return -1;
  675. }
  676. format_print(fp, fmt, ind, "%s\n", label);
  677. ind += 4;
  678. format_bytes(fp, fmt, ind, "XCoordinate", c->point.x, 32);
  679. format_bytes(fp, fmt, ind, "YCoordinate", c->point.y, 32);
  680. format_bytes(fp, fmt, ind, "HASH", c->hash, 32);
  681. format_bytes(fp, fmt, ind, "CipherText", c->ciphertext, c->ciphertext_size);
  682. return 1;
  683. }
  684. int sm2_encrypt(const SM2_KEY *key, const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen)
  685. {
  686. SM2_CIPHERTEXT C;
  687. if (!key || !in || !out || !outlen) {
  688. error_print();
  689. return -1;
  690. }
  691. if (!inlen) {
  692. error_print();
  693. return -1;
  694. }
  695. if (sm2_do_encrypt(key, in, inlen, &C) != 1) {
  696. error_print();
  697. return -1;
  698. }
  699. *outlen = 0;
  700. if (sm2_ciphertext_to_der(&C, &out, outlen) != 1) {
  701. error_print();
  702. return -1;
  703. }
  704. return 1;
  705. }
  706. int sm2_encrypt_fixlen(const SM2_KEY *key, const uint8_t *in, size_t inlen, int point_size, uint8_t *out, size_t *outlen)
  707. {
  708. SM2_CIPHERTEXT C;
  709. if (!key || !in || !out || !outlen) {
  710. error_print();
  711. return -1;
  712. }
  713. if (!inlen) {
  714. error_print();
  715. return -1;
  716. }
  717. if (sm2_do_encrypt_fixlen(key, in, inlen, point_size, &C) != 1) {
  718. error_print();
  719. return -1;
  720. }
  721. *outlen = 0;
  722. if (sm2_ciphertext_to_der(&C, &out, outlen) != 1) {
  723. error_print();
  724. return -1;
  725. }
  726. return 1;
  727. }
  728. int sm2_decrypt(const SM2_KEY *key, const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen)
  729. {
  730. SM2_CIPHERTEXT C;
  731. if (!key || !in || !out || !outlen) {
  732. error_print();
  733. return -1;
  734. }
  735. if (sm2_ciphertext_from_der(&C, &in, &inlen) != 1
  736. || asn1_length_is_zero(inlen) != 1) {
  737. error_print();
  738. return -1;
  739. }
  740. if (sm2_do_decrypt(key, &C, out, outlen) != 1) {
  741. error_print();
  742. return -1;
  743. }
  744. return 1;
  745. }
  746. int sm2_do_ecdh(const SM2_KEY *key, const SM2_POINT *peer_public, SM2_POINT *out)
  747. {
  748. /*
  749. if (sm2_point_is_on_curve(peer_public) != 1) {
  750. error_print();
  751. return -1;
  752. }
  753. */
  754. if (sm2_point_mul(out, key->private_key, peer_public) != 1) {
  755. error_print();
  756. return -1;
  757. }
  758. return 1;
  759. }
  760. int sm2_ecdh(const SM2_KEY *key, const uint8_t *peer_public, size_t peer_public_len, SM2_POINT *out)
  761. {
  762. SM2_POINT point;
  763. if (!key || !peer_public || !peer_public_len || !out) {
  764. error_print();
  765. return -1;
  766. }
  767. if (sm2_point_from_octets(&point, peer_public, peer_public_len) != 1) {
  768. error_print();
  769. return -1;
  770. }
  771. if (sm2_do_ecdh(key, &point, out) != 1) {
  772. error_print();
  773. return -1;
  774. }
  775. return 1;
  776. }
  777. // (x1, y1) = k * G
  778. // r = e + x1
  779. // s = (k - r * d)/(1 + d) = (k +r - r * d - r)/(1 + d) = (k + r - r(1 +d))/(1 + d) = (k + r)/(1 + d) - r
  780. // = -r + (k + r)*(1 + d)^-1
  781. // = -r + (k + r) * d'
  782. int sm2_do_sign_fast(const SM2_Fn d, const uint8_t dgst[32], SM2_SIGNATURE *sig)
  783. {
  784. SM2_JACOBIAN_POINT R;
  785. SM2_BN e;
  786. SM2_BN k;
  787. SM2_BN x1;
  788. SM2_BN r;
  789. SM2_BN s;
  790. // e = H(M)
  791. sm2_bn_from_bytes(e, dgst);
  792. if (sm2_bn_cmp(e, SM2_N) >= 0) {
  793. sm2_bn_sub(e, e, SM2_N);
  794. }
  795. // rand k in [1, n - 1]
  796. do {
  797. if (sm2_fn_rand(k) != 1) {
  798. error_print();
  799. return -1;
  800. }
  801. } while (sm2_bn_is_zero(k));
  802. // (x1, y1) = kG
  803. sm2_jacobian_point_mul_generator(&R, k);
  804. sm2_jacobian_point_get_xy(&R, x1, NULL);
  805. // r = e + x1 (mod n)
  806. sm2_fn_add(r, e, x1);
  807. // s = (k + r) * d' - r
  808. sm2_bn_add(s, k, r);
  809. sm2_fn_mul(s, s, d);
  810. sm2_fn_sub(s, s, r);
  811. sm2_bn_to_bytes(r, sig->r);
  812. sm2_bn_to_bytes(s, sig->s);
  813. return 1;
  814. }