sm2test.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5. #include <openssl/bn.h>
  6. #include <openssl/ec.h>
  7. #include <openssl/rand.h>
  8. #include <openssl/err.h>
  9. #include <openssl/ecdsa.h>
  10. #include <openssl/ecdh.h>
  11. #include "sm2.h"
  12. #pragma comment(lib,"libeay32.lib")
  13. #define ABORT do { \
  14. fflush(stdout); \
  15. fprintf(stderr, "%s:%d: ABORT\n", __FILE__, __LINE__); \
  16. ERR_print_errors_fp(stderr); \
  17. exit(1); \
  18. } while (0)
  19. static const char rnd_seed[] = "string to make the random number generator think it has entropy";
  20. void BNPrintf(BIGNUM* bn)
  21. {
  22. char *p=NULL;
  23. p=BN_bn2hex(bn);
  24. printf("%s",p);
  25. OPENSSL_free(p);
  26. }
  27. int SM2_Test_Vecotor()
  28. {
  29. BN_CTX *ctx = NULL;
  30. BIGNUM *p, *a, *b;
  31. EC_GROUP *group;
  32. EC_POINT *P, *Q, *R;
  33. BIGNUM *x, *y, *z;
  34. EC_KEY *eckey = NULL;
  35. unsigned char digest[20];
  36. unsigned char *signature = NULL;
  37. int sig_len;
  38. CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
  39. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
  40. ERR_load_crypto_strings();
  41. RAND_seed(rnd_seed, sizeof rnd_seed); /* or BN_generate_prime may fail */
  42. ctx = BN_CTX_new();
  43. if (!ctx) ABORT;
  44. /* Curve SM2 (Chinese National Algorithm) */
  45. //http://www.oscca.gov.cn/News/201012/News_1197.htm
  46. p = BN_new();
  47. a = BN_new();
  48. b = BN_new();
  49. if (!p || !a || !b) ABORT;
  50. group = EC_GROUP_new(EC_GFp_mont_method()); /* applications should use EC_GROUP_new_curve_GFp
  51. * so that the library gets to choose the EC_METHOD */
  52. if (!group) ABORT;
  53. if (!BN_hex2bn(&p, "8542D69E4C044F18E8B92435BF6FF7DE457283915C45517D722EDB8B08F1DFC3")) ABORT;
  54. if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) ABORT;
  55. if (!BN_hex2bn(&a, "787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498")) ABORT;
  56. if (!BN_hex2bn(&b, "63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A")) ABORT;
  57. if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
  58. P = EC_POINT_new(group);
  59. Q = EC_POINT_new(group);
  60. R = EC_POINT_new(group);
  61. if (!P || !Q || !R) ABORT;
  62. x = BN_new();
  63. y = BN_new();
  64. z = BN_new();
  65. if (!x || !y || !z) ABORT;
  66. // sm2 testing P256 Vetor
  67. // p£º8542D69E4C044F18E8B92435BF6FF7DE457283915C45517D722EDB8B08F1DFC3
  68. // a£º787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498
  69. // b£º63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A
  70. // xG 421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D
  71. // yG 0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2
  72. // n: 8542D69E4C044F18E8B92435BF6FF7DD297720630485628D5AE74EE7C32E79B7
  73. if (!BN_hex2bn(&x, "421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D")) ABORT;
  74. if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 0, ctx)) ABORT;
  75. if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
  76. if (!BN_hex2bn(&z, "8542D69E4C044F18E8B92435BF6FF7DD297720630485628D5AE74EE7C32E79B7")) ABORT;
  77. if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
  78. if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
  79. fprintf(stdout, "\nChinese sm2 algorithm test -- Generator:\n x = 0x");
  80. BNPrintf(x);
  81. fprintf(stdout, "\n y = 0x");
  82. BNPrintf( y);
  83. fprintf(stdout, "\n");
  84. /* G_y value taken from the standard: */
  85. if (!BN_hex2bn(&z, "0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2")) ABORT;
  86. if (0 != BN_cmp(y, z)) ABORT;
  87. fprintf(stdout, "verify degree ...");
  88. if (EC_GROUP_get_degree(group) != 256) ABORT;
  89. fprintf(stdout, " ok\n");
  90. fprintf(stdout, "verify group order ...");
  91. fflush(stdout);
  92. if (!EC_GROUP_get_order(group, z, ctx)) ABORT;
  93. if (!EC_GROUP_precompute_mult(group, ctx)) ABORT;
  94. if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
  95. if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
  96. fflush(stdout);
  97. fprintf(stdout, " ok\n");
  98. //testing ECDSA for SM2
  99. /* create new ecdsa key */
  100. if ((eckey = EC_KEY_new()) == NULL)
  101. goto builtin_err;
  102. if (EC_KEY_set_group(eckey, group) == 0)
  103. {
  104. fprintf(stdout," failed\n");
  105. goto builtin_err;
  106. }
  107. /* create key */
  108. if (!EC_KEY_generate_key(eckey))
  109. {
  110. fprintf(stdout," failed\n");
  111. goto builtin_err;
  112. }
  113. /* check key */
  114. if (!EC_KEY_check_key(eckey))
  115. {
  116. fprintf(stdout," failed\n");
  117. goto builtin_err;
  118. }
  119. /* create signature */
  120. sig_len = ECDSA_size(eckey);
  121. fprintf(stdout,"Siglength is: %d \n",sig_len);
  122. if (!RAND_pseudo_bytes(digest, 20))
  123. {
  124. fprintf(stdout," failed\n");
  125. goto builtin_err;
  126. }
  127. if ((signature = OPENSSL_malloc(sig_len)) == NULL)
  128. goto builtin_err;
  129. if (!SM2_sign(0, digest, 20, signature, &sig_len, eckey))
  130. {
  131. fprintf(stdout, " failed\n");
  132. goto builtin_err;
  133. }
  134. fprintf(stdout, "ECSign OK\n");
  135. /* verify signature */
  136. if (SM2_verify(0, digest, 20, signature, sig_len, eckey) != 1)
  137. {
  138. fprintf(stdout, " failed\n");
  139. goto builtin_err;
  140. }
  141. fprintf(stdout, "ECVerify OK\n");
  142. /* cleanup */
  143. OPENSSL_free(signature);
  144. signature = NULL;
  145. EC_KEY_free(eckey);
  146. eckey = NULL;
  147. builtin_err:
  148. EC_POINT_free(P);
  149. EC_POINT_free(Q);
  150. EC_POINT_free(R);
  151. EC_GROUP_free(group);
  152. BN_CTX_free(ctx);
  153. return 0;
  154. }
  155. int SM2_Test_Vecotor2()
  156. {
  157. BN_CTX *ctx = NULL;
  158. BIGNUM *p, *a, *b;
  159. EC_GROUP *group;
  160. EC_POINT *P, *Q, *R;
  161. BIGNUM *x, *y, *z;
  162. EC_KEY *eckey = NULL;
  163. unsigned char *signature;
  164. unsigned char digest[32] = "\xB5\x24\xF5\x52\xCD\x82\xB8\xB0\x28\x47\x6E\x00\x5C\x37\x7F\xB1\x9A\x87\xE6\xFC\x68\x2D\x48\xBB\x5D\x42\xE3\xD9\xB9\xEF\xFE\x76";
  165. int sig_len;
  166. BIGNUM *kinv, *rp,*order;
  167. ECDSA_SIG *ecsig = ECDSA_SIG_new();
  168. EC_POINT * DHPoint = NULL;
  169. // unsigned char *in="123456";
  170. // size_t inlen = 6;
  171. size_t outlen = 256;
  172. unsigned char outkey[256];
  173. size_t keylen = 256;
  174. size_t i;
  175. CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
  176. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
  177. ERR_load_crypto_strings();
  178. RAND_seed(rnd_seed, sizeof rnd_seed); /* or BN_generate_prime may fail */
  179. ctx = BN_CTX_new();
  180. if (!ctx) ABORT;
  181. /* Curve SM2 (Chinese National Algorithm) */
  182. //http://www.oscca.gov.cn/News/201012/News_1197.htm
  183. p = BN_new();
  184. a = BN_new();
  185. b = BN_new();
  186. if (!p || !a || !b) ABORT;
  187. group = EC_GROUP_new(EC_GFp_mont_method()); /* applications should use EC_GROUP_new_curve_GFp
  188. * so that the library gets to choose the EC_METHOD */
  189. if (!group) ABORT;
  190. if (!BN_hex2bn(&p, "8542D69E4C044F18E8B92435BF6FF7DE457283915C45517D722EDB8B08F1DFC3")) ABORT;
  191. if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) ABORT;
  192. if (!BN_hex2bn(&a, "787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498")) ABORT;
  193. if (!BN_hex2bn(&b, "63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A")) ABORT;
  194. if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
  195. P = EC_POINT_new(group);
  196. Q = EC_POINT_new(group);
  197. R = EC_POINT_new(group);
  198. if (!P || !Q || !R) ABORT;
  199. x = BN_new();
  200. y = BN_new();
  201. z = BN_new();
  202. if (!x || !y || !z) ABORT;
  203. // sm2 testing P256 Vetor
  204. // p£º8542D69E4C044F18E8B92435BF6FF7DE457283915C45517D722EDB8B08F1DFC3
  205. // a£º787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498
  206. // b£º63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A
  207. // xG 421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D
  208. // yG 0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2
  209. // n: 8542D69E4C044F18E8B92435BF6FF7DD297720630485628D5AE74EE7C32E79B7
  210. if (!BN_hex2bn(&x, "421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D")) ABORT;
  211. if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 0, ctx)) ABORT;
  212. if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
  213. if (!BN_hex2bn(&z, "8542D69E4C044F18E8B92435BF6FF7DD297720630485628D5AE74EE7C32E79B7")) ABORT;
  214. if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
  215. if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
  216. fprintf(stdout, "\nChinese sm2 algorithm test -- Generator:\n x = 0x");
  217. BNPrintf(x);
  218. fprintf(stdout, "\n y = 0x");
  219. BNPrintf( y);
  220. fprintf(stdout, "\n");
  221. /* G_y value taken from the standard: */
  222. if (!BN_hex2bn(&z, "0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2")) ABORT;
  223. if (0 != BN_cmp(y, z)) ABORT;
  224. fprintf(stdout, "verify degree ...");
  225. if (EC_GROUP_get_degree(group) != 256) ABORT;
  226. fprintf(stdout, " ok\n");
  227. fprintf(stdout, "verify group order ...");
  228. fflush(stdout);
  229. if (!EC_GROUP_get_order(group, z, ctx)) ABORT;
  230. if (!EC_GROUP_precompute_mult(group, ctx)) ABORT;
  231. if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
  232. if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
  233. fflush(stdout);
  234. fprintf(stdout, " ok\n");
  235. //testing ECDSA for SM2
  236. /* create new ecdsa key */
  237. if ((eckey = EC_KEY_new()) == NULL)
  238. goto builtin_err;
  239. if (EC_KEY_set_group(eckey, group) == 0)
  240. {
  241. fprintf(stdout," failed\n");
  242. goto builtin_err;
  243. }
  244. /* create key */
  245. if (!BN_hex2bn(&z, "128B2FA8BD433C6C068C8D803DFF79792A519A55171B1B650C23661D15897263")) ABORT;
  246. if (!EC_POINT_mul(group,P, z, NULL, NULL, ctx)) ABORT;
  247. if (!EC_POINT_get_affine_coordinates_GFp(group,P, x, y, ctx)) ABORT;
  248. fprintf(stdout, "\nTesting ECKey Point\n x = 0x");
  249. BNPrintf(x);
  250. fprintf(stdout, "\n y = 0x");
  251. BNPrintf( y);
  252. fprintf(stdout, "\n");
  253. EC_KEY_set_private_key(eckey,z);
  254. EC_KEY_set_public_key(eckey, P);
  255. /* check key */
  256. if (!EC_KEY_check_key(eckey))
  257. {
  258. fprintf(stdout," failed\n");
  259. goto builtin_err;
  260. }
  261. /* create signature */
  262. sig_len = ECDSA_size(eckey);
  263. //fprintf(stdout,"Siglength is: %d \n",sig_len);
  264. if ((signature = OPENSSL_malloc(sig_len)) == NULL)
  265. goto builtin_err;
  266. rp = BN_new();
  267. kinv = BN_new();
  268. order = BN_new();
  269. if (!BN_hex2bn(&z, "6CB28D99385C175C94F94E934817663FC176D925DD72B727260DBAAE1FB2F96F")) ABORT;
  270. if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx))
  271. {
  272. fprintf(stdout, " failed\n");
  273. goto builtin_err;
  274. }
  275. if (!EC_POINT_get_affine_coordinates_GFp(group,Q, x, y, ctx))
  276. {
  277. fprintf(stdout, " failed\n");
  278. goto builtin_err;
  279. }
  280. fprintf(stdout, "\nTesting K Point\n x = 0x");
  281. BNPrintf(x);
  282. fprintf(stdout, "\n y = 0x");
  283. BNPrintf( y);
  284. fprintf(stdout, "\n");
  285. EC_GROUP_get_order(group, order, ctx);
  286. if (!BN_nnmod(rp, x, order, ctx))
  287. {
  288. fprintf(stdout, " failed\n");
  289. goto builtin_err;
  290. }
  291. if (!BN_copy(kinv, z ))
  292. {
  293. fprintf(stdout, " failed\n");
  294. goto builtin_err;
  295. }
  296. // for(i=0;i<32;i++)
  297. // printf("%02X",digest[i]);
  298. // printf("\n");
  299. if (!SM2_sign_ex(1, digest, 32, signature, &sig_len, kinv, rp, eckey))
  300. {
  301. fprintf(stdout, " failed\n");
  302. goto builtin_err;
  303. }
  304. fprintf(stdout, "ECSign OK\n");
  305. /* verify signature */
  306. if (SM2_verify(1, digest, 32, signature, sig_len, eckey) != 1)
  307. {
  308. fprintf(stdout, " failed\n");
  309. goto builtin_err;
  310. }
  311. fprintf(stdout, "ECVerify OK\n r = 0x");
  312. d2i_ECDSA_SIG(&ecsig, &signature, sig_len);
  313. BNPrintf(ecsig->r);
  314. fprintf(stdout,"\n s = 0x");
  315. BNPrintf(ecsig->s);
  316. fprintf(stdout,"\n");
  317. //testing SM2DH vector
  318. /* create key */
  319. if (!BN_hex2bn(&z, "6FCBA2EF9AE0AB902BC3BDE3FF915D44BA4CC78F88E2F8E7F8996D3B8CCEEDEE")) ABORT;
  320. if (!EC_POINT_mul(group,P, z, NULL, NULL, ctx)) ABORT;
  321. if (!EC_POINT_get_affine_coordinates_GFp(group,P, x, y, ctx)) ABORT;
  322. fprintf(stdout, "\nTesting A Key Point\n x = 0x");
  323. BNPrintf(x);
  324. fprintf(stdout, "\n y = 0x");
  325. BNPrintf( y);
  326. fprintf(stdout, "\n");
  327. EC_KEY_set_private_key(eckey,z);
  328. EC_KEY_set_public_key(eckey, P);
  329. if (!BN_hex2bn(&z, "5E35D7D3F3C54DBAC72E61819E730B019A84208CA3A35E4C2E353DFCCB2A3B53")) ABORT;
  330. if (!EC_POINT_mul(group,Q, z, NULL, NULL, ctx)) ABORT;
  331. if (!EC_POINT_get_affine_coordinates_GFp(group,Q, x, y, ctx)) ABORT;
  332. fprintf(stdout, "\nTesting B Key Point\n x = 0x");
  333. BNPrintf(x);
  334. fprintf(stdout, "\n y = 0x");
  335. BNPrintf( y);
  336. fprintf(stdout, "\n");
  337. //EC_KEY_set_private_key(eckey,z);
  338. //EC_KEY_set_public_key(eckey, P);
  339. if (!BN_hex2bn(&z, "33FE21940342161C55619C4A0C060293D543C80AF19748CE176D83477DE71C80")) ABORT;
  340. if (!EC_POINT_mul(group,P, z, NULL, NULL, ctx)) ABORT;
  341. if (!EC_POINT_get_affine_coordinates_GFp(group,P, x, y, ctx)) ABORT;
  342. fprintf(stdout, "\nTesting Rb Key Point\n x = 0x");
  343. BNPrintf(x);
  344. fprintf(stdout, "\n y = 0x");
  345. BNPrintf( y);
  346. fprintf(stdout, "\n");
  347. if (!BN_hex2bn(&z, "83A2C9C8B96E5AF70BD480B472409A9A327257F1EBB73F5B073354B248668563")) ABORT;
  348. if (!EC_POINT_mul(group,R, z, NULL, NULL, ctx)) ABORT;
  349. if (!EC_POINT_get_affine_coordinates_GFp(group,R, x, y, ctx)) ABORT;
  350. fprintf(stdout, "\nTesting Ra Key Point\n x = 0x");
  351. BNPrintf(x);
  352. fprintf(stdout, "\n y = 0x");
  353. BNPrintf( y);
  354. fprintf(stdout, "\n");
  355. SM2_DH_key(group,P, Q, z,eckey,outkey,keylen);
  356. fprintf(stdout,"\nExchange key --KDF(Xv||Yv)-- :");
  357. for(i=0; i<outlen; i++)
  358. printf("%02X",outkey[i]);
  359. printf("\n");
  360. builtin_err:
  361. OPENSSL_free(signature);
  362. signature = NULL;
  363. EC_POINT_free(P);
  364. EC_POINT_free(Q);
  365. EC_POINT_free(R);
  366. EC_POINT_free(DHPoint);
  367. EC_KEY_free(eckey);
  368. eckey = NULL;
  369. EC_GROUP_free(group);
  370. BN_CTX_free(ctx);
  371. return 0;
  372. }
  373. int main()
  374. {
  375. CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
  376. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
  377. ERR_load_crypto_strings();
  378. RAND_seed(rnd_seed, sizeof rnd_seed);
  379. SM2_Test_Vecotor2();
  380. CRYPTO_cleanup_all_ex_data();
  381. ERR_free_strings();
  382. ERR_remove_state(0);
  383. CRYPTO_mem_leaks_fp(stderr);
  384. return 0;
  385. }