sm2test.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  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 <stdlib.h>
  12. #include <gmssl/asn1.h>
  13. #include <gmssl/error.h>
  14. #include <gmssl/sm2.h>
  15. #include <gmssl/pkcs8.h>
  16. #define sm2_print_bn(label,a) sm2_bn_print(stderr,0,0,label,a) // 这个不应该放在这里,应该放在测试文件中
  17. #define hex_fp_add_x_y "eefbe4cf140ff8b5b956d329d5a2eae8608c933cb89053217439786e54866567"
  18. #define hex_fp_sub_x_y "768d77882a23097d05db3562fed0a840bf3984422c3bc4a26e7b12a412128426"
  19. #define hex_fp_sub_y_x "89728876d5dcf682fa24ca9d012f57bf40c67bbcd3c43b5e9184ed5beded7bd9"
  20. #define hex_fp_neg_x "cd3b51d2e0e67ee6a066fbb995c6366b701cf43f0d99f41f8ea5ba76ccb38b38"
  21. #define hex_fp_mul_x_y "edd7e745bdc4630ccfa1da1057033a525346dbf202f082f3c431349991ace76a"
  22. #define hex_fp_squ_x "f4e2cca0bcfd67fba8531eebff519e4cb3d47f9fe8c5eff5151f4c497ec99fbf"
  23. #define hex_fp_exp_x_y "8cafd11b1a0d2072b82911ba87e0d376103a1be5986fce91d8d297b758f68146"
  24. #define hex_fp_inv_x "053b878fb82e213c17e554b9a574b7bd31775222704b7fd9c7d6f8441026cd80"
  25. #define hex_fn_add_x_y "eefbe4cf140ff8b5b956d329d5a2eae8608c933cb89053217439786e54866567"
  26. #define hex_fn_sub_x_y "768d77882a23097d05db3562fed0a840313d63ae4e01c9ccc23706ad4be7c54a"
  27. #define hex_fn_sub_y_x "89728876d5dcf682fa24ca9d012f57bf40c67bbcd3c43b5e9184ed5beded7bd9"
  28. #define hex_fn_neg_x "cd3b51d2e0e67ee6a066fbb995c6366ae220d3ab2f5ff949e261ae800688cc5c"
  29. #define hex_fn_mul_x_y "cf7296d5cbf0b64bb5e9a11b294962e9c779b41c038e9c8d815234a0df9d6623"
  30. #define hex_fn_sqr_x "82d3d1b296d3a3803888b7ffc78f23eca824e7ec8d7ddaf231ffb0d256a19da2"
  31. #define hex_fn_exp_x_y "0cf4df7e76d7d49ff23b94853a98aba1e36e9ca0358acbf23a3bbda406f46df3"
  32. #define hex_fn_inv_x "96340ec8b80f44e9b345a706bdb5c9e3ab8a6474a5cb4e0d4645dbaecf1cf03d"
  33. #define hex_v "d3da0ef661be97360e1b32f834e6ca5673b1984b22bb420133da05e56ccd59fb"
  34. #define hex_fn_mul_x_v "0375c61e1ed13e460f4b5d462dc5b2c846f36c7b481cd4bed8f7dd55908a6afd"
  35. #define hex_t "2fbadf57b52dc19e8470bf201cb182e0a4f7fa5e28d356b15da173132b94b325"
  36. int test_sm2_bn(void)
  37. {
  38. const SM2_JACOBIAN_POINT _G = {
  39. {
  40. 0x334c74c7, 0x715a4589, 0xf2660be1, 0x8fe30bbf,
  41. 0x6a39c994, 0x5f990446, 0x1f198119, 0x32c4ae2c,
  42. },
  43. {
  44. 0x2139f0a0, 0x02df32e5, 0xc62a4740, 0xd0a9877c,
  45. 0x6b692153, 0x59bdcee3, 0xf4f6779c, 0xbc3736a2,
  46. },
  47. {
  48. 1, 0, 0, 0, 0, 0, 0, 0,
  49. },
  50. };
  51. const SM2_JACOBIAN_POINT *G = &_G;
  52. SM2_BN r;
  53. SM2_BN x;
  54. SM2_BN y;
  55. int ok, i = 1;
  56. char hex[65];
  57. SM2_BN v = {
  58. 0x6ccd59fb, 0x33da05e5, 0x22bb4201, 0x73b1984b,
  59. 0x34e6ca56, 0x0e1b32f8, 0x61be9736, 0xd3da0ef6,
  60. };
  61. SM2_BN t;
  62. sm2_bn_copy(x, G->X);
  63. sm2_bn_copy(y, G->Y);
  64. sm2_bn_from_hex(r, hex_v);
  65. ok = (sm2_bn_cmp(r, v) == 0);
  66. printf("sm2 bn test %d %s\n", i++, ok ? "ok" : "failed");
  67. if (!ok) return -1;
  68. // fp tests
  69. sm2_fp_add(r, x, y);
  70. ok = sm2_bn_equ_hex(r, hex_fp_add_x_y);
  71. printf("sm2 bn test %d %s\n", i++, ok ? "ok" : "failed");
  72. if (!ok) return -1;
  73. sm2_fp_sub(r, x, y);
  74. ok = sm2_bn_equ_hex(r, hex_fp_sub_x_y);
  75. printf("sm2 bn test %d %s\n", i++, ok ? "ok" : "failed");
  76. if (!ok) return -1;
  77. sm2_fp_mul(r, x, y);
  78. ok = sm2_bn_equ_hex(r, hex_fp_mul_x_y);
  79. printf("sm2 bn test %d %s\n", i++, ok ? "ok" : "failed");
  80. if (!ok) return -1;
  81. sm2_fp_exp(r, x, y);
  82. ok = sm2_bn_equ_hex(r, hex_fp_exp_x_y);
  83. printf("sm2 bn test %d %s\n", i++, ok ? "ok" : "failed");
  84. if (!ok) return -1;
  85. sm2_fp_inv(r, x);
  86. ok = sm2_bn_equ_hex(r, hex_fp_inv_x);
  87. printf("sm2 bn test %d %s\n", i++, ok ? "ok" : "failed");
  88. if (!ok) return -1;
  89. sm2_fp_neg(r, x);
  90. ok = sm2_bn_equ_hex(r, hex_fp_neg_x);
  91. printf("sm2 bn test %d %s\n", i++, ok ? "ok" : "failed");
  92. if (!ok) return -1;
  93. // fn tests
  94. sm2_fn_add(r, x, y);
  95. ok = sm2_bn_equ_hex(r, hex_fn_add_x_y);
  96. printf("sm2 bn test %d %s\n", i++, ok ? "ok" : "failed");
  97. if (!ok) return -1;
  98. sm2_fn_sub(r, x, y);
  99. ok = sm2_bn_equ_hex(r, hex_fn_sub_x_y);
  100. printf("sm2 bn test %d %s\n", i++, ok ? "ok" : "failed");
  101. if (!ok) return -1;
  102. sm2_fn_sub(r, y, x);
  103. ok = sm2_bn_equ_hex(r, hex_fn_sub_y_x);
  104. printf("sm2 bn test %d %s\n", i++, ok ? "ok" : "failed");
  105. if (!ok) return -1;
  106. sm2_fn_neg(r, x);
  107. ok = sm2_bn_equ_hex(r, hex_fn_neg_x);
  108. printf("sm2 bn test %d %s\n", i++, ok ? "ok" : "failed");
  109. if (!ok) return -1;
  110. sm2_fn_mul(r, x, y);
  111. ok = sm2_bn_equ_hex(r, hex_fn_mul_x_y);
  112. printf("sm2 bn test %d %s\n", i++, ok ? "ok" : "failed");
  113. if (!ok) return -1;
  114. sm2_fn_mul(r, x, v);
  115. ok = sm2_bn_equ_hex(r, hex_fn_mul_x_v);
  116. printf("sm2 bn test %d %s\n", i++, ok ? "ok" : "failed");
  117. if (!ok) return -1;
  118. sm2_fn_sqr(r, x);
  119. ok = sm2_bn_equ_hex(r, hex_fn_sqr_x);
  120. printf("sm2 bn test %d %s\n", i++, ok ? "ok" : "failed");
  121. if (!ok) return -1;
  122. sm2_fn_exp(r, x, y);
  123. ok = sm2_bn_equ_hex(r, hex_fn_exp_x_y);
  124. printf("sm2 bn test %d %s\n", i++, ok ? "ok" : "failed");
  125. if (!ok) return -1;
  126. sm2_fn_inv(r, x);
  127. ok = sm2_bn_equ_hex(r, hex_fn_inv_x);
  128. printf("sm2 bn test %d %s\n", i++, ok ? "ok" : "failed");
  129. if (!ok) return -1;
  130. SM2_BN tv = {
  131. 0x2b94b325, 0x5da17313, 0x28d356b1, 0xa4f7fa5e,
  132. 0x1cb182e0, 0x8470bf20, 0xb52dc19e, 0x2fbadf57,
  133. };
  134. sm2_bn_from_hex(t, hex_t);
  135. ok = (sm2_bn_cmp(t, tv) == 0);
  136. if (!ok) return -1;
  137. sm2_bn_to_hex(t, hex);
  138. return 1;
  139. }
  140. #define hex_G \
  141. "32c4ae2c1f1981195f9904466a39c9948fe30bbff2660be1715a4589334c74c7" \
  142. "bc3736a2f4f6779c59bdcee36b692153d0a9877cc62a474002df32e52139f0a0"
  143. #define hex_2G \
  144. "56cefd60d7c87c000d58ef57fa73ba4d9c0dfa08c08a7331495c2e1da3f2bd52" \
  145. "31b7e7e6cc8189f668535ce0f8eaf1bd6de84c182f6c8e716f780d3a970a23c3"
  146. #define hex_3G \
  147. "a97f7cd4b3c993b4be2daa8cdb41e24ca13f6bd945302244e26918f1d0509ebf" \
  148. "530b5dd88c688ef5ccc5cec08a72150f7c400ee5cd045292aaacdd037458f6e6"
  149. #define hex_negG \
  150. "32c4ae2c1f1981195f9904466a39c9948fe30bbff2660be1715a4589334c74c7" \
  151. "43c8c95c0b098863a642311c9496deac2f56788239d5b8c0fd20cd1adec60f5f"
  152. #define hex_10G \
  153. "d3f94862519621c121666061f65c3e32b2d0d065cd219e3284a04814db522756" \
  154. "4b9030cf676f6a742ebd57d146dca428f6b743f64d1482d147d46fb2bab82a14"
  155. #define hex_bG \
  156. "528470bc74a6ebc663c06fc4cfa1b630d1e9d4a80c0a127b47f73c324c46c0ba" \
  157. "832cf9c5a15b997e60962b4cf6e2c9cee488faaec98d20599d323d4cabfc1bf4"
  158. #define hex_P \
  159. "504cfe2fae749d645e99fbb5b25995cc6fed70196007b039bdc44706bdabc0d9" \
  160. "b80a8018eda5f55ddc4b870d7784b7b84e53af02f575ab53ed8a99a3bbe2abc2"
  161. #define hex_2P \
  162. "a53d20e89312b5243f66aec12ef6471f5911941d86302d5d8337cb70937d65ae" \
  163. "96953c46815e4259363256ddd6c77fcc33787aeafc6a57beec5833f476dd69e0"
  164. #define hex_tP \
  165. "02deff2c5b3656ca3f7c7ca9d710ca1d69860c75a9c7ec284b96b8adc50b2936" \
  166. "b74bcba937e9267fce4ccc069a6681f5b04dcedd9e2794c6a25ddc7856df7145"
  167. int test_sm2_jacobian_point(void)
  168. {
  169. const SM2_JACOBIAN_POINT _G = {
  170. {
  171. 0x334c74c7, 0x715a4589, 0xf2660be1, 0x8fe30bbf,
  172. 0x6a39c994, 0x5f990446, 0x1f198119, 0x32c4ae2c,
  173. },
  174. {
  175. 0x2139f0a0, 0x02df32e5, 0xc62a4740, 0xd0a9877c,
  176. 0x6b692153, 0x59bdcee3, 0xf4f6779c, 0xbc3736a2,
  177. },
  178. {
  179. 1, 0, 0, 0, 0, 0, 0, 0,
  180. },
  181. };
  182. const SM2_BN _B = {
  183. 0x4d940e93, 0xddbcbd41, 0x15ab8f92, 0xf39789f5,
  184. 0xcf6509a7, 0x4d5a9e4b, 0x9d9f5e34, 0x28e9fa9e,
  185. };
  186. const SM2_JACOBIAN_POINT *G = &_G;
  187. SM2_JACOBIAN_POINT _P, *P = &_P;
  188. SM2_BN k;
  189. int i = 1, ok;
  190. uint8_t buf[64];
  191. printf("sm2_jacobian_point_test\n");
  192. ok = sm2_jacobian_point_equ_hex(G, hex_G);
  193. printf("sm2 point test %d %s\n", i++, ok ? "ok" : "failed");
  194. if (!ok) return -1;
  195. ok = sm2_jacobian_point_is_on_curve(G);
  196. printf("sm2 point test %d %s\n", i++, ok ? "ok" : "failed");
  197. if (!ok) return -1;
  198. sm2_jacobian_point_dbl(P, G);
  199. ok = sm2_jacobian_point_equ_hex(P, hex_2G);
  200. printf("sm2 point test %d %s\n", i++, ok ? "ok" : "failed");
  201. if (!ok) return -1;
  202. sm2_jacobian_point_add(P, P, G);
  203. ok = sm2_jacobian_point_equ_hex(P, hex_3G);
  204. printf("sm2 point test %d %s\n", i++, ok ? "ok" : "failed");
  205. if (!ok) return -1;
  206. sm2_jacobian_point_sub(P, P, G);
  207. ok = sm2_jacobian_point_equ_hex(P, hex_2G);
  208. printf("sm2 point test %d %s\n", i++, ok ? "ok" : "failed");
  209. if (!ok) return -1;
  210. sm2_jacobian_point_neg(P, G);
  211. ok = sm2_jacobian_point_equ_hex(P, hex_negG);
  212. printf("sm2 point test %d %s\n", i++, ok ? "ok" : "failed");
  213. if (!ok) return -1;
  214. sm2_bn_set_word(k, 10);
  215. sm2_jacobian_point_mul(P, k, G);
  216. ok = sm2_jacobian_point_equ_hex(P, hex_10G);
  217. printf("sm2 point test %d %s\n", i++, ok ? "ok" : "failed");
  218. if (!ok) return -1;
  219. sm2_jacobian_point_mul_generator(P, _B);
  220. ok = sm2_jacobian_point_equ_hex(P, hex_bG);
  221. printf("sm2 point test %d %s\n", i++, ok ? "ok" : "failed");
  222. if (!ok) return -1;
  223. sm2_jacobian_point_to_bytes(P, buf);
  224. sm2_jacobian_point_from_hex(P, hex_P);
  225. return 1;
  226. }
  227. #define hex_d "5aebdfd947543b713bc0df2c65baaecc5dadd2cab39c6971402daf92c263fad2"
  228. #define hex_e "c0881c19beec741b9af27cc26493dcc33b05d481bfeab2f3ce9cc056e6ff8400"
  229. #define hex_k "981325ee1ab171e9d2cffb317181a02957b18a34bca610a6d2f8afcdeb53f6b8"
  230. #define hex_x1 "17d2dfe83f23cce8499bca983950d59f0fd56c4c671dd63c04b27e4e94cfd767"
  231. #define hex_r "d85afc01fe104103e48e475a9de4b2624adb40ce2708892fd34f3ea57bcf5b67"
  232. #define hex_rd "a70ba64f9c30e05095f39fe26675114e3f157b2c35191bf6ff06246452f82eb3"
  233. #define hex_di "3ecfdb51c24b0eecb2d4238d1da8c013b8b575cef14ef43e2ddb7bce740ce9cf"
  234. #define hex_krd "f1077f9d7e8091993cdc5b4f0b0c8eda8a9fee73a952f9db27ae7f72d2310928"
  235. #define hex_s "006bac5b8057ca829534dfde72a0d7883444a3b9bfe9bcdfb383fb90ed7d9486"
  236. static int test_sm2_point(void)
  237. {
  238. SM2_POINT P, Q;
  239. uint8_t k[32] = {0};
  240. uint8_t buf[65] = {0};
  241. int i;
  242. for (i = 1; i < 8; i++) {
  243. k[31] = (uint8_t)i;
  244. if (sm2_point_mul_generator(&P, k) != 1
  245. || sm2_point_is_on_curve(&P) != 1) {
  246. error_print();
  247. return -1;
  248. }
  249. format_print(stderr, 0, 0, "k = %d, ", i);
  250. sm2_point_print(stderr, 0, 0, "k * G", &P);
  251. memset(buf, 0, sizeof(buf));
  252. sm2_point_to_compressed_octets(&P, buf);
  253. format_bytes(stderr, 0, 4, "compressedPoint", buf, 33);
  254. memset(&Q, 0, sizeof(Q));
  255. if (sm2_point_from_x(&Q, buf + 1, buf[0]) != 1
  256. || memcmp(&P, &Q, sizeof(SM2_POINT)) != 0) {
  257. sm2_point_print(stderr, 0, 4, "P", &P);
  258. sm2_point_print(stderr, 0, 4, "Q", &Q);
  259. error_print();
  260. return -1;
  261. }
  262. memset(buf, 0, sizeof(buf));
  263. sm2_point_to_uncompressed_octets(&P, buf);
  264. format_bytes(stderr, 0, 4, "compressedPoint", buf, 65);
  265. memset(&Q, 0, sizeof(Q));
  266. if (sm2_point_from_octets(&Q, buf, 65) != 1
  267. || memcmp(&P, &Q, sizeof(SM2_POINT)) != 0) {
  268. error_print();
  269. return -1;
  270. }
  271. }
  272. printf("%s() ok\n", __FUNCTION__);
  273. return 1;
  274. }
  275. static int test_sm2_point_der(void)
  276. {
  277. SM2_POINT P, Q;
  278. uint8_t k[32] = {0};
  279. uint8_t buf[512];
  280. int i;
  281. for (i = 1; i < 8; i++) {
  282. uint8_t *p = buf;
  283. const uint8_t *cp = buf;
  284. size_t len = 0;
  285. k[31] = i;
  286. memset(&P, 0, sizeof(P));
  287. memset(&Q, 0, sizeof(Q));
  288. if (sm2_point_mul_generator(&P, k) != 1
  289. || sm2_point_to_der(&P, &p, &len) != 1
  290. || format_bytes(stderr, 0, 4, "ECPoint", buf, len) != 1
  291. || sm2_point_from_der(&Q, &cp, &len) != 1
  292. || asn1_length_is_zero(len) != 1) {
  293. error_print();
  294. return -1;
  295. }
  296. if (memcmp(&P, &Q, sizeof(SM2_POINT)) != 0) {
  297. error_print();
  298. sm2_point_print(stderr, 0, 4, "P", &P);
  299. sm2_point_print(stderr, 0, 4, "Q", &Q);
  300. return -1;
  301. }
  302. }
  303. printf("%s() ok\n", __FUNCTION__);
  304. return 1;
  305. }
  306. static int test_sm2_point_octets(void)
  307. {
  308. SM2_POINT P, Q;
  309. uint8_t k[32] = {0};
  310. uint8_t buf[33];
  311. int i;
  312. for (i = 1; i < 8; i++) {
  313. uint8_t *p = buf;
  314. const uint8_t *cp = buf;
  315. size_t len = 0;
  316. k[31] = i;
  317. memset(&P, 0, sizeof(P));
  318. memset(&Q, 0, sizeof(Q));
  319. if (sm2_point_mul_generator(&P, k) != 1) {
  320. error_print();
  321. return -1;
  322. }
  323. sm2_point_to_compressed_octets(&P, buf);
  324. format_bytes(stderr, 0, 4, "compressedPoint", buf, sizeof(buf));
  325. if (sm2_point_from_octets(&Q, buf, sizeof(buf)) != 1) {
  326. error_print();
  327. return -1;
  328. }
  329. if (memcmp(&P, &Q, sizeof(SM2_POINT)) != 0) {
  330. error_print();
  331. sm2_point_print(stderr, 0, 4, "P", &P);
  332. sm2_point_print(stderr, 0, 4, "Q", &Q);
  333. return -1;
  334. }
  335. }
  336. printf("%s() ok\n", __FUNCTION__);
  337. return 1;
  338. }
  339. static int test_sm2_point_from_x(void)
  340. {
  341. SM2_POINT P, Q;
  342. uint8_t k[32] = {0};
  343. uint8_t buf[33];
  344. int i;
  345. for (i = 1; i < 8; i++) {
  346. uint8_t *p = buf;
  347. const uint8_t *cp = buf;
  348. size_t len = 0;
  349. k[31] = i;
  350. memset(&P, 0, sizeof(P));
  351. memset(&Q, 0, sizeof(Q));
  352. if (sm2_point_mul_generator(&P, k) != 1) {
  353. error_print();
  354. return -1;
  355. }
  356. sm2_point_to_compressed_octets(&P, buf);
  357. if (sm2_point_from_x(&Q, buf + 1, buf[0]) != 1) {
  358. error_print();
  359. return -1;
  360. }
  361. if (memcmp(&P, &Q, sizeof(SM2_POINT)) != 0) {
  362. error_print();
  363. sm2_point_print(stderr, 0, 4, "P", &P);
  364. sm2_point_print(stderr, 0, 4, "Q", &Q);
  365. return -1;
  366. }
  367. }
  368. printf("%s() ok\n", __FUNCTION__);
  369. return 1;
  370. }
  371. static int test_sm2_signature(void)
  372. {
  373. SM2_SIGNATURE sig;
  374. uint8_t buf[512];
  375. uint8_t *p = buf;
  376. const uint8_t *cp = buf;
  377. size_t len = 0;
  378. // MinLen
  379. memset(&sig, 0x00, sizeof(sig));
  380. cp = p = buf; len = 0;
  381. if (sm2_signature_to_der(&sig, &p, &len) != 1) {
  382. error_print();
  383. return -1;
  384. }
  385. format_print(stderr, 0, 4, "SM2_MIN_SIGNATURE_SIZE: %zu\n", len);
  386. format_bytes(stderr, 0, 4, "", buf, len);
  387. sm2_signature_print(stderr, 0, 4, "signature", buf, len);
  388. if (len != SM2_MIN_SIGNATURE_SIZE) {
  389. error_print();
  390. return -1;
  391. }
  392. if (sm2_signature_from_der(&sig, &cp, &len) != 1
  393. || asn1_length_is_zero(len) != 1) {
  394. error_print();
  395. return -1;
  396. }
  397. // MaxLen
  398. memset(&sig, 0x80, sizeof(sig));
  399. cp = p = buf; len = 0;
  400. if (sm2_signature_to_der(&sig, &p, &len) != 1) {
  401. error_print();
  402. return -1;
  403. }
  404. format_print(stderr, 0, 4, "SM2_MAX_SIGNATURE_SIZE: %zu\n", len);
  405. format_bytes(stderr, 0, 4, "", buf, len);
  406. sm2_signature_print(stderr, 0, 4, "signature", buf, len);
  407. if (len != SM2_MAX_SIGNATURE_SIZE) {
  408. error_print();
  409. return -1;
  410. }
  411. if (sm2_signature_from_der(&sig, &cp, &len) != 1
  412. || asn1_length_is_zero(len) != 1) {
  413. error_print();
  414. return -1;
  415. }
  416. printf("%s() ok\n", __FUNCTION__);
  417. return 1;
  418. }
  419. static int test_sm2_sign(void)
  420. {
  421. int ret;
  422. SM2_KEY sm2_key;
  423. SM2_SIGN_CTX sign_ctx;
  424. uint8_t msg[] = "Hello World!";
  425. uint8_t sig[SM2_MAX_SIGNATURE_SIZE] = {0};
  426. size_t siglen;
  427. if (sm2_key_generate(&sm2_key) != 1) {
  428. error_print();
  429. return -1;
  430. }
  431. sm2_key_print(stderr, 0, 4, "SM2_KEY", &sm2_key);
  432. if (sm2_sign_init(&sign_ctx, &sm2_key, SM2_DEFAULT_ID, SM2_DEFAULT_ID_LENGTH) != 1
  433. || sm2_sign_update(&sign_ctx, msg, sizeof(msg)) != 1
  434. || sm2_sign_finish(&sign_ctx, sig, &siglen) != 1) {
  435. error_print();
  436. return -1;
  437. }
  438. format_bytes(stderr, 0, 4, "signature", sig, siglen);
  439. sm2_signature_print(stderr, 0, 4, "signature", sig, siglen);
  440. if (sm2_verify_init(&sign_ctx, &sm2_key, SM2_DEFAULT_ID, SM2_DEFAULT_ID_LENGTH) != 1
  441. || sm2_verify_update(&sign_ctx, msg, sizeof(msg)) != 1
  442. || (ret = sm2_verify_finish(&sign_ctx, sig, siglen)) != 1) {
  443. error_print();
  444. return -1;
  445. }
  446. format_print(stderr, 0, 4, "verification: %s\n", ret ? "success" : "failed");
  447. // FIXME: 还应该增加验证不通过的测试
  448. // 还应该增加底层的参数
  449. printf("%s() ok\n", __FUNCTION__);
  450. return 1;
  451. }
  452. // 由于当前Ciphertext中椭圆曲线点数据不正确,因此无法通过测试
  453. static int test_sm2_ciphertext(void)
  454. {
  455. SM2_CIPHERTEXT C;
  456. uint8_t buf[1024];
  457. uint8_t *p = buf;
  458. const uint8_t *cp = buf;
  459. size_t len = 0;
  460. memset(&C, 0, sizeof(SM2_CIPHERTEXT));
  461. cp = p = buf; len = 0;
  462. if (sm2_ciphertext_to_der(&C, &p, &len) != 1) {
  463. error_print();
  464. return -1;
  465. }
  466. format_print(stderr, 0, 4, "SM2_NULL_CIPHERTEXT_SIZE: %zu\n", len);
  467. format_bytes(stderr, 0, 4, "", buf, len);
  468. if (sm2_ciphertext_from_der(&C, &cp, &len) != 1
  469. || asn1_length_is_zero(len) != 1) {
  470. error_print();
  471. return -1;
  472. }
  473. // {0, 0, Hash, MinLen}
  474. C.ciphertext_size = SM2_MIN_PLAINTEXT_SIZE;
  475. cp = p = buf; len = 0;
  476. if (sm2_ciphertext_to_der(&C, &p, &len) != 1) {
  477. error_print();
  478. return -1;
  479. }
  480. format_print(stderr, 0, 4, "SM2_MIN_PLAINTEXT_SIZE: %zu\n", SM2_MIN_PLAINTEXT_SIZE);
  481. format_print(stderr, 0, 4, "SM2_MIN_CIPHERTEXT_SIZE: %zu\n", len);
  482. format_bytes(stderr, 0, 4, "", buf, len);
  483. if (len != SM2_MIN_CIPHERTEXT_SIZE) {
  484. error_print();
  485. return -1;
  486. }
  487. if (sm2_ciphertext_from_der(&C, &cp, &len) != 1
  488. || asn1_length_is_zero(len) != 1) {
  489. error_print();
  490. return -1;
  491. }
  492. // { 33, 33, Hash, NULL }
  493. memset(&C, 0x80, sizeof(SM2_POINT));
  494. cp = p = buf; len = 0;
  495. if (sm2_ciphertext_to_der(&C, &p, &len) != 1) {
  496. error_print();
  497. return -1;
  498. }
  499. format_print(stderr, 0, 4, "ciphertext len: %zu\n", len);
  500. format_bytes(stderr, 0, 4, "", buf, len);
  501. if (sm2_ciphertext_from_der(&C, &cp, &len) != 1
  502. || asn1_length_is_zero(len) != 1) {
  503. error_print();
  504. return -1;
  505. }
  506. // { 33, 33, Hash, MaxLen }
  507. C.ciphertext_size = SM2_MAX_PLAINTEXT_SIZE;//SM2_MAX_PLAINTEXT_SIZE;
  508. cp = p = buf; len = 0;
  509. if (sm2_ciphertext_to_der(&C, &p, &len) != 1) {
  510. error_print();
  511. return -1;
  512. }
  513. format_print(stderr, 0, 4, "SM2_MAX_PLAINTEXT_SIZE: %zu\n", SM2_MAX_PLAINTEXT_SIZE);
  514. format_print(stderr, 0, 4, "SM2_MAX_CIPHERTEXT_SIZE: %zu\n", len);
  515. format_bytes(stderr, 0, 4, "", buf, len);
  516. if (len != SM2_MAX_CIPHERTEXT_SIZE) {
  517. error_print();
  518. return -1;
  519. }
  520. if (sm2_ciphertext_from_der(&C, &cp, &len) != 1
  521. || asn1_length_is_zero(len) != 1) {
  522. error_print();
  523. return -1;
  524. }
  525. printf("%s() ok\n", __FUNCTION__);
  526. return 1;
  527. }
  528. static int test_sm2_do_encrypt(void)
  529. {
  530. SM2_KEY sm2_key;
  531. uint8_t plaintext[] = "Hello World!";
  532. SM2_CIPHERTEXT ciphertext;
  533. uint8_t plainbuf[SM2_MAX_PLAINTEXT_SIZE] = {0};
  534. size_t plainlen = 0;
  535. int r = 0;
  536. if (sm2_key_generate(&sm2_key) != 1) {
  537. error_print();
  538. return -1;
  539. }
  540. if (sm2_do_encrypt(&sm2_key, plaintext, sizeof(plaintext), &ciphertext) != 1
  541. || sm2_do_decrypt(&sm2_key, &ciphertext, plainbuf, &plainlen) != 1) {
  542. error_print();
  543. return -1;
  544. }
  545. if (plainlen != sizeof(plaintext)
  546. || memcmp(plainbuf, plaintext, sizeof(plaintext)) != 0) {
  547. error_print();
  548. return -1;
  549. }
  550. printf("%s() ok\n", __FUNCTION__);
  551. return 1;
  552. }
  553. static int test_sm2_encrypt(void)
  554. {
  555. SM2_KEY sm2_key;
  556. uint8_t msg[SM2_MAX_PLAINTEXT_SIZE];
  557. uint8_t cbuf[SM2_MAX_CIPHERTEXT_SIZE+100];
  558. uint8_t mbuf[SM2_MAX_CIPHERTEXT_SIZE];
  559. size_t lens[] = {
  560. // 0,
  561. 1,
  562. 16,
  563. SM2_MAX_PLAINTEXT_SIZE,
  564. };
  565. size_t clen, mlen;
  566. int i;
  567. if (sm2_key_generate(&sm2_key) != 1) {
  568. error_print();
  569. return -1;
  570. }
  571. for (i = 0; i < sizeof(msg); i++) {
  572. msg[i] = (uint8_t)i;
  573. }
  574. for (i = 0; i < sizeof(lens)/sizeof(lens[0]); i++) {
  575. format_print(stderr, 0, 0, "test %d\n", i + 1);
  576. format_bytes(stderr, 0, 4, "plaintext", msg, lens[i]);
  577. if (sm2_encrypt(&sm2_key, msg, lens[i], cbuf, &clen) != 1) {
  578. error_print();
  579. return -1;
  580. }
  581. format_bytes(stderr, 0, 4, "ciphertext", cbuf, clen);
  582. sm2_ciphertext_print(stderr, 0, 4, "Ciphertext", cbuf, clen);
  583. format_print(stderr, 0, 0, "\n");
  584. if (sm2_decrypt(&sm2_key, cbuf, clen, mbuf, &mlen) != 1) {
  585. error_print();
  586. return -1;
  587. }
  588. if (mlen != lens[i]
  589. || memcmp(mbuf, msg, lens[i]) != 0) {
  590. error_print();
  591. return -1;
  592. }
  593. }
  594. printf("%s() ok\n", __FUNCTION__);
  595. return 1;
  596. }
  597. static int test_sm2_private_key(void)
  598. {
  599. SM2_KEY sm2_key;
  600. SM2_KEY tmp_key;
  601. uint8_t buf[SM2_PRIVATE_KEY_BUF_SIZE];
  602. uint8_t *p = buf;
  603. const uint8_t *cp = buf;
  604. size_t len = 0;
  605. const uint8_t *d;
  606. size_t dlen;
  607. if (sm2_key_generate(&sm2_key) != 1) {
  608. error_print();
  609. return -1;
  610. }
  611. sm2_key_print(stderr, 0, 4, "SM2_KEY", &sm2_key);
  612. if (sm2_private_key_to_der(&sm2_key, &p, &len) != 1) {
  613. error_print();
  614. return -1;
  615. }
  616. format_bytes(stderr, 0, 4, "ECPrivateKey", buf, len);
  617. format_print(stderr, 0, 4, "#define SM2_PRIVATE_KEY_DEFAULT_SIZE %zu\n", len);
  618. if (sm2_private_key_from_der(&tmp_key, &cp, &len) != 1
  619. || asn1_length_is_zero(len) != 1) {
  620. error_print();
  621. return -1;
  622. }
  623. if (memcmp(&tmp_key, &sm2_key, sizeof(SM2_KEY)) != 0) {
  624. sm2_key_print(stderr, 0, 0, "sm2_key", &sm2_key);
  625. sm2_key_print(stderr, 0, 0, "tmp_key", &tmp_key);
  626. error_print();
  627. return -1;
  628. }
  629. cp = p = buf; len = 0;
  630. memset(&tmp_key, 0, sizeof(tmp_key));
  631. if (sm2_private_key_to_der(&sm2_key, &p, &len) != 1
  632. || asn1_sequence_from_der(&d, &dlen, &cp, &len) != 1
  633. || asn1_length_is_zero(len) != 1) {
  634. error_print();
  635. return -1;
  636. }
  637. sm2_private_key_print(stderr, 0, 4, "ECPrivateKey", d, dlen);
  638. printf("%s() ok\n", __FUNCTION__);
  639. return 1;
  640. }
  641. static int test_sm2_private_key_info(void)
  642. {
  643. uint8_t buf[512];
  644. uint8_t *p = buf;
  645. const uint8_t *cp = buf;
  646. size_t len = 0;
  647. const uint8_t *d;
  648. size_t dlen;
  649. SM2_KEY sm2_key;
  650. SM2_KEY tmp_key;
  651. const uint8_t *attrs;
  652. size_t attrs_len;
  653. if (sm2_key_generate(&sm2_key) != 1) {
  654. error_print();
  655. return -1;
  656. }
  657. sm2_key_print(stderr, 0, 4, "SM2_KEY", &sm2_key);
  658. if (sm2_private_key_info_to_der(&sm2_key, &p, &len) != 1) {
  659. error_print();
  660. return -1;
  661. }
  662. format_bytes(stderr, 0, 4, "PrivateKeyInfo", buf, len);
  663. format_print(stderr, 0, 4, "sizeof(PrivateKeyInfo): %zu\n", len);
  664. if (asn1_sequence_from_der(&d, &dlen, &cp, &len) != 1
  665. || asn1_length_is_zero(len) != 1) {
  666. error_print();
  667. return -1;
  668. }
  669. sm2_private_key_info_print(stderr, 0, 4, "PrivateKeyInfo", d, dlen);
  670. cp = p = buf; len = 0;
  671. if (sm2_private_key_info_to_der(&sm2_key, &p, &len) != 1) {
  672. error_print();
  673. return -1;
  674. }
  675. if (sm2_private_key_info_from_der(&tmp_key, &attrs, &attrs_len, &cp, &len) != 1
  676. || asn1_length_is_zero(len) != 1
  677. || memcmp(&tmp_key, &sm2_key, sizeof(SM2_KEY)) != 0) {
  678. error_print();
  679. return -1;
  680. }
  681. printf("%s() ok\n", __FUNCTION__);
  682. return 1;
  683. }
  684. static int test_sm2_enced_private_key_info(void)
  685. {
  686. uint8_t buf[512];
  687. uint8_t *p = buf;
  688. const uint8_t *cp = buf;
  689. size_t len = 0;
  690. const uint8_t *d;
  691. size_t dlen;
  692. SM2_KEY sm2_key;
  693. SM2_KEY tmp_key;
  694. const uint8_t *attrs;
  695. size_t attrs_len;
  696. const char *pass = "Password";
  697. if (sm2_key_generate(&sm2_key) != 1) {
  698. error_print();
  699. return -1;
  700. }
  701. sm2_key_print(stderr, 0, 4, "SM2_KEY", &sm2_key);
  702. if (sm2_private_key_info_encrypt_to_der(&sm2_key, pass, &p, &len) != 1) {
  703. error_print();
  704. return -1;
  705. }
  706. format_bytes(stderr, 0, 4, "EncryptedPrivateKeyInfo", buf, len);
  707. format_print(stderr, 0, 4, "sizeof(EncryptedPrivateKeyInfo): %zu\n", len);
  708. if (asn1_sequence_from_der(&d, &dlen, &cp, &len) != 1
  709. || asn1_length_is_zero(len) != 1) {
  710. error_print();
  711. return -1;
  712. }
  713. pkcs8_enced_private_key_info_print(stderr, 0, 4, "EncryptedPrivateKeyInfo", d, dlen);
  714. cp = p = buf; len = 0;
  715. if (sm2_private_key_info_encrypt_to_der(&sm2_key, pass, &p, &len) != 1) {
  716. error_print();
  717. return -1;
  718. }
  719. if (sm2_private_key_info_decrypt_from_der(&tmp_key, &attrs, &attrs_len, pass, &cp, &len) != 1
  720. || asn1_length_is_zero(len) != 1
  721. || memcmp(&tmp_key, &sm2_key, sizeof(SM2_KEY)) != 0) {
  722. error_print();
  723. return -1;
  724. }
  725. printf("%s() ok\n", __FUNCTION__);
  726. return 1;
  727. }
  728. int main(void)
  729. {
  730. if (test_sm2_bn() != 1) goto err;
  731. if (test_sm2_jacobian_point() != 1) goto err;
  732. if (test_sm2_point() != 1) goto err;
  733. if (test_sm2_point_octets() != 1) goto err;
  734. if (test_sm2_point_from_x() != 1) goto err;
  735. if (test_sm2_point_der() != 1) goto err;
  736. if (test_sm2_private_key() != 1) goto err;
  737. if (test_sm2_private_key_info() != 1) goto err;
  738. if (test_sm2_enced_private_key_info() != 1) goto err;
  739. if (test_sm2_signature() != 1) goto err;
  740. if (test_sm2_sign() != 1) goto err;
  741. //if (test_sm2_ciphertext() != 1) goto err; // 需要正确的Ciphertext数据
  742. if (test_sm2_do_encrypt() != 1) goto err;
  743. if (test_sm2_encrypt() != 1) goto err;
  744. printf("%s all tests passed\n", __FILE__);
  745. return 0;
  746. err:
  747. error_print();
  748. return -1;
  749. }