sm2.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. // \file:sm2.c
  2. //SM2 Algorithm
  3. //2011-11-10
  4. //author:goldboar
  5. //email:[email protected]
  6. //depending:opnessl library
  7. //SM2 Standards: http://www.oscca.gov.cn/News/201012/News_1197.htm
  8. #include <limits.h>
  9. #include <openssl/ec.h>
  10. #include <openssl/bn.h>
  11. #include <openssl/rand.h>
  12. #include <openssl/err.h>
  13. #include <openssl/ecdsa.h>
  14. #include <openssl/ecdh.h>
  15. #include "kdf.h"
  16. #define NID_X9_62_prime_field 406
  17. static void BNPrintf(BIGNUM* bn)
  18. {
  19. char *p=NULL;
  20. p=BN_bn2hex(bn);
  21. printf("%s",p);
  22. OPENSSL_free(p);
  23. }
  24. static int sm2_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kp, BIGNUM **rp)
  25. {
  26. BN_CTX *ctx = NULL;
  27. BIGNUM *k = NULL, *r = NULL, *order = NULL, *X = NULL;
  28. EC_POINT *tmp_point=NULL;
  29. const EC_GROUP *group;
  30. int ret = 0;
  31. if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL)
  32. {
  33. ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_PASSED_NULL_PARAMETER);
  34. return 0;
  35. }
  36. if (ctx_in == NULL)
  37. {
  38. if ((ctx = BN_CTX_new()) == NULL)
  39. {
  40. ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP,ERR_R_MALLOC_FAILURE);
  41. return 0;
  42. }
  43. }
  44. else
  45. ctx = ctx_in;
  46. k = BN_new(); /* this value is later returned in *kp */
  47. r = BN_new(); /* this value is later returned in *rp */
  48. order = BN_new();
  49. X = BN_new();
  50. if (!k || !r || !order || !X)
  51. {
  52. ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);
  53. goto err;
  54. }
  55. if ((tmp_point = EC_POINT_new(group)) == NULL)
  56. {
  57. ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
  58. goto err;
  59. }
  60. if (!EC_GROUP_get_order(group, order, ctx))
  61. {
  62. ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
  63. goto err;
  64. }
  65. do
  66. {
  67. /* get random k */
  68. do
  69. if (!BN_rand_range(k, order))
  70. {
  71. ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED);
  72. goto err;
  73. }
  74. while (BN_is_zero(k));
  75. /* compute r the x-coordinate of generator * k */
  76. if (!EC_POINT_mul(group, tmp_point, k, NULL, NULL, ctx))
  77. {
  78. ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
  79. goto err;
  80. }
  81. if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field)
  82. {
  83. if (!EC_POINT_get_affine_coordinates_GFp(group,
  84. tmp_point, X, NULL, ctx))
  85. {
  86. ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP,ERR_R_EC_LIB);
  87. goto err;
  88. }
  89. }
  90. else /* NID_X9_62_characteristic_two_field */
  91. {
  92. if (!EC_POINT_get_affine_coordinates_GF2m(group,
  93. tmp_point, X, NULL, ctx))
  94. {
  95. ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP,ERR_R_EC_LIB);
  96. goto err;
  97. }
  98. }
  99. if (!BN_nnmod(r, X, order, ctx))
  100. {
  101. ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
  102. goto err;
  103. }
  104. }
  105. while (BN_is_zero(r));
  106. /* compute the inverse of k */
  107. // if (!BN_mod_inverse(k, k, order, ctx))
  108. // {
  109. // ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
  110. // goto err;
  111. // }
  112. /* clear old values if necessary */
  113. if (*rp != NULL)
  114. BN_clear_free(*rp);
  115. if (*kp != NULL)
  116. BN_clear_free(*kp);
  117. /* save the pre-computed values */
  118. *rp = r;
  119. *kp = k;
  120. ret = 1;
  121. err:
  122. if (!ret)
  123. {
  124. if (k != NULL) BN_clear_free(k);
  125. if (r != NULL) BN_clear_free(r);
  126. }
  127. if (ctx_in == NULL)
  128. BN_CTX_free(ctx);
  129. if (order != NULL)
  130. BN_free(order);
  131. if (tmp_point != NULL)
  132. EC_POINT_free(tmp_point);
  133. if (X)
  134. BN_clear_free(X);
  135. return(ret);
  136. }
  137. static ECDSA_SIG *sm2_do_sign(const unsigned char *dgst, int dgst_len, const BIGNUM *in_k, const BIGNUM *in_r, EC_KEY *eckey)
  138. {
  139. int ok = 0, i;
  140. BIGNUM *k=NULL, *s, *m=NULL,*tmp=NULL,*order=NULL;
  141. const BIGNUM *ck;
  142. BN_CTX *ctx = NULL;
  143. const EC_GROUP *group;
  144. ECDSA_SIG *ret;
  145. //ECDSA_DATA *ecdsa;
  146. const BIGNUM *priv_key;
  147. BIGNUM *r,*x=NULL,*a=NULL; //new added
  148. //ecdsa = ecdsa_check(eckey);
  149. group = EC_KEY_get0_group(eckey);
  150. priv_key = EC_KEY_get0_private_key(eckey);
  151. if (group == NULL || priv_key == NULL /*|| ecdsa == NULL*/)
  152. {
  153. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_PASSED_NULL_PARAMETER);
  154. return NULL;
  155. }
  156. ret = ECDSA_SIG_new();
  157. if (!ret)
  158. {
  159. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_MALLOC_FAILURE);
  160. return NULL;
  161. }
  162. s = ret->s;
  163. r = ret->r;
  164. if ((ctx = BN_CTX_new()) == NULL || (order = BN_new()) == NULL ||
  165. (tmp = BN_new()) == NULL || (m = BN_new()) == NULL ||
  166. (x = BN_new()) == NULL || (a = BN_new()) == NULL)
  167. {
  168. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_MALLOC_FAILURE);
  169. goto err;
  170. }
  171. if (!EC_GROUP_get_order(group, order, ctx))
  172. {
  173. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_EC_LIB);
  174. goto err;
  175. }
  176. // for(i=0;i<dgst_len;i++)
  177. // printf("%02X",dgst[i]);
  178. // printf("\n");
  179. i = BN_num_bits(order);
  180. /* Need to truncate digest if it is too long: first truncate whole
  181. * bytes.
  182. */
  183. if (8 * dgst_len > i)
  184. dgst_len = (i + 7)/8;
  185. if (!BN_bin2bn(dgst, dgst_len, m))
  186. {
  187. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
  188. goto err;
  189. }
  190. /* If still too long truncate remaining bits with a shift */
  191. if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7)))
  192. {
  193. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
  194. goto err;
  195. }
  196. // fprintf(stdout,"m: ");
  197. // BNPrintf(m);
  198. // fprintf(stdout,"\n");
  199. do
  200. {
  201. if (in_k == NULL || in_r == NULL)
  202. {
  203. if (!sm2_sign_setup(eckey, ctx, &k, &x))
  204. {
  205. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN,ERR_R_ECDSA_LIB);
  206. goto err;
  207. }
  208. ck = k;
  209. }
  210. else
  211. {
  212. ck = in_k;
  213. if (BN_copy(x, in_r) == NULL)
  214. {
  215. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_MALLOC_FAILURE);
  216. goto err;
  217. }
  218. }
  219. //r=(e+x1) mod n
  220. if (!BN_mod_add_quick(r, m, x, order))
  221. {
  222. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
  223. goto err;
  224. }
  225. // BNPrintf(r);
  226. // fprintf(stdout,"\n");
  227. if(BN_is_zero(r) )
  228. continue;
  229. BN_add(tmp,r,ck);
  230. if(BN_ucmp(tmp,order) == 0)
  231. continue;
  232. if (!BN_mod_mul(tmp, priv_key, r, order, ctx))
  233. {
  234. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
  235. goto err;
  236. }
  237. if (!BN_mod_sub_quick(s, ck, tmp, order))
  238. {
  239. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
  240. goto err;
  241. }
  242. BN_one(a);
  243. //BN_set_word((a),1);
  244. if (!BN_mod_add_quick(tmp, priv_key, a, order))
  245. {
  246. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
  247. goto err;
  248. }
  249. /* compute the inverse of 1+dA */
  250. if (!BN_mod_inverse(tmp, tmp, order, ctx))
  251. {
  252. ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
  253. goto err;
  254. }
  255. // BNPrintf(tmp);
  256. // fprintf(stdout,"\n");
  257. if (!BN_mod_mul(s, s, tmp, order, ctx))
  258. {
  259. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
  260. goto err;
  261. }
  262. if (BN_is_zero(s))
  263. {
  264. /* if k and r have been supplied by the caller
  265. * don't to generate new k and r values */
  266. if (in_k != NULL && in_r != NULL)
  267. {
  268. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ECDSA_R_NEED_NEW_SETUP_VALUES);
  269. goto err;
  270. }
  271. }
  272. else
  273. /* s != 0 => we have a valid signature */
  274. break;
  275. }
  276. while (1);
  277. ok = 1;
  278. err:
  279. if (!ok)
  280. {
  281. ECDSA_SIG_free(ret);
  282. ret = NULL;
  283. }
  284. if (ctx)
  285. BN_CTX_free(ctx);
  286. if (m)
  287. BN_clear_free(m);
  288. if (tmp)
  289. BN_clear_free(tmp);
  290. if (order)
  291. BN_free(order);
  292. if (k)
  293. BN_clear_free(k);
  294. if (x)
  295. BN_clear_free(x);
  296. if (a)
  297. BN_clear_free(a);
  298. return ret;
  299. }
  300. static int sm2_do_verify(const unsigned char *dgst, int dgst_len,
  301. const ECDSA_SIG *sig, EC_KEY *eckey)
  302. {
  303. int ret = -1, i;
  304. BN_CTX *ctx;
  305. BIGNUM *order, *R, *m, *X,*t;
  306. EC_POINT *point = NULL;
  307. const EC_GROUP *group;
  308. const EC_POINT *pub_key;
  309. /* check input values */
  310. if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL ||
  311. (pub_key = EC_KEY_get0_public_key(eckey)) == NULL || sig == NULL)
  312. {
  313. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ECDSA_R_MISSING_PARAMETERS);
  314. return -1;
  315. }
  316. ctx = BN_CTX_new();
  317. if (!ctx)
  318. {
  319. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_MALLOC_FAILURE);
  320. return -1;
  321. }
  322. BN_CTX_start(ctx);
  323. order = BN_CTX_get(ctx);
  324. R = BN_CTX_get(ctx);
  325. t = BN_CTX_get(ctx);
  326. m = BN_CTX_get(ctx);
  327. X = BN_CTX_get(ctx);
  328. if (!X)
  329. {
  330. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
  331. goto err;
  332. }
  333. if (!EC_GROUP_get_order(group, order, ctx))
  334. {
  335. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
  336. goto err;
  337. }
  338. if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||
  339. BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s) ||
  340. BN_is_negative(sig->s) || BN_ucmp(sig->s, order) >= 0)
  341. {
  342. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ECDSA_R_BAD_SIGNATURE);
  343. ret = 0; /* signature is invalid */
  344. goto err;
  345. }
  346. //t =(r+s) mod n
  347. if (!BN_mod_add_quick(t, sig->s, sig->r,order))
  348. {
  349. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
  350. goto err;
  351. }
  352. if (BN_is_zero(t))
  353. {
  354. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ECDSA_R_BAD_SIGNATURE);
  355. ret = 0; /* signature is invalid */
  356. goto err;
  357. }
  358. //point = s*G+t*PA
  359. if ((point = EC_POINT_new(group)) == NULL)
  360. {
  361. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_MALLOC_FAILURE);
  362. goto err;
  363. }
  364. if (!EC_POINT_mul(group, point, sig->s, pub_key, t, ctx))
  365. {
  366. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
  367. goto err;
  368. }
  369. if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field)
  370. {
  371. if (!EC_POINT_get_affine_coordinates_GFp(group,
  372. point, X, NULL, ctx))
  373. {
  374. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
  375. goto err;
  376. }
  377. }
  378. else /* NID_X9_62_characteristic_two_field */
  379. {
  380. if (!EC_POINT_get_affine_coordinates_GF2m(group,
  381. point, X, NULL, ctx))
  382. {
  383. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
  384. goto err;
  385. }
  386. }
  387. i = BN_num_bits(order);
  388. /* Need to truncate digest if it is too long: first truncate whole
  389. * bytes.
  390. */
  391. if (8 * dgst_len > i)
  392. dgst_len = (i + 7)/8;
  393. if (!BN_bin2bn(dgst, dgst_len, m))
  394. {
  395. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
  396. goto err;
  397. }
  398. /* If still too long truncate remaining bits with a shift */
  399. if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7)))
  400. {
  401. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
  402. goto err;
  403. }
  404. /* R = m + X mod order */
  405. if (!BN_mod_add_quick(R, m, X, order))
  406. {
  407. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
  408. goto err;
  409. }
  410. /* if the signature is correct R is equal to sig->r */
  411. ret = (BN_ucmp(R, sig->r) == 0);
  412. err:
  413. BN_CTX_end(ctx);
  414. BN_CTX_free(ctx);
  415. if (point)
  416. EC_POINT_free(point);
  417. return ret;
  418. }
  419. EC_POINT *sm2_compute_key(const EC_POINT *b_pub_key_r, const EC_POINT *b_pub_key, const BIGNUM *a_r,EC_KEY *a_eckey)
  420. {
  421. BN_CTX *ctx;
  422. EC_POINT *tmp=NULL;
  423. BIGNUM *x=NULL, *y=NULL, *order=NULL,*z=NULL;
  424. const BIGNUM *priv_key;
  425. const EC_GROUP* group;
  426. EC_POINT *ret= NULL;
  427. /* size_t buflen, len;*/
  428. unsigned char *buf=NULL;
  429. int i, j;
  430. //char *p=NULL;
  431. BIGNUM *x1,*x2,*t,*h;
  432. if ((ctx = BN_CTX_new()) == NULL) goto err;
  433. BN_CTX_start(ctx);
  434. x = BN_CTX_get(ctx);
  435. y = BN_CTX_get(ctx);
  436. order = BN_CTX_get(ctx);
  437. z = BN_CTX_get(ctx);
  438. x1 = BN_CTX_get(ctx);
  439. x2 = BN_CTX_get(ctx);
  440. t = BN_CTX_get(ctx);
  441. h = BN_CTX_get(ctx);
  442. priv_key = EC_KEY_get0_private_key(a_eckey);
  443. if (priv_key == NULL)
  444. {
  445. ECDHerr(ECDH_F_ECDH_COMPUTE_KEY,ECDH_R_NO_PRIVATE_VALUE);
  446. goto err;
  447. }
  448. group = EC_KEY_get0_group(a_eckey);
  449. if ((tmp=EC_POINT_new(group)) == NULL)
  450. {
  451. ECDHerr(ECDH_F_ECDH_COMPUTE_KEY,ERR_R_MALLOC_FAILURE);
  452. goto err;
  453. }
  454. if (!EC_POINT_mul(group, tmp, a_r, NULL, NULL, ctx))
  455. {
  456. ECDHerr(ECDH_F_ECDH_COMPUTE_KEY,ECDH_R_POINT_ARITHMETIC_FAILURE);
  457. goto err;
  458. }
  459. if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field)
  460. {
  461. if (!EC_POINT_get_affine_coordinates_GFp(group, tmp, x, NULL, ctx))
  462. {
  463. ECDHerr(ECDH_F_ECDH_COMPUTE_KEY,ECDH_R_POINT_ARITHMETIC_FAILURE);
  464. goto err;
  465. }
  466. }
  467. else
  468. {
  469. if (!EC_POINT_get_affine_coordinates_GF2m(group, tmp, x, NULL, ctx))
  470. {
  471. ECDHerr(ECDH_F_ECDH_COMPUTE_KEY,ECDH_R_POINT_ARITHMETIC_FAILURE);
  472. goto err;
  473. }
  474. }
  475. if (!EC_GROUP_get_order(group, order, ctx))
  476. {
  477. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_EC_LIB);
  478. goto err;
  479. }
  480. i = BN_num_bits(order);
  481. j = i/2 -1;
  482. BN_mask_bits(x,j);
  483. BN_set_word(y,2);
  484. BN_set_word(z,j);
  485. BN_exp(y,y,z,ctx);
  486. BN_add(x1,x,y);
  487. // fprintf(stdout,"X1=: ");
  488. // BNPrintf(x1);
  489. // fprintf(stdout,"\n");
  490. BN_mod_mul(t,x1,a_r,order,ctx);
  491. BN_mod_add_quick(t,t,priv_key,order);
  492. //
  493. // fprintf(stdout,"ta=: ");
  494. // BNPrintf(t);
  495. // fprintf(stdout,"\n");
  496. if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field)
  497. {
  498. if (!EC_POINT_get_affine_coordinates_GFp(group, b_pub_key_r, x, NULL, ctx))
  499. {
  500. ECDHerr(ECDH_F_ECDH_COMPUTE_KEY,ECDH_R_POINT_ARITHMETIC_FAILURE);
  501. goto err;
  502. }
  503. }
  504. else
  505. {
  506. if (!EC_POINT_get_affine_coordinates_GF2m(group, b_pub_key_r, x, NULL, ctx))
  507. {
  508. ECDHerr(ECDH_F_ECDH_COMPUTE_KEY,ECDH_R_POINT_ARITHMETIC_FAILURE);
  509. goto err;
  510. }
  511. }
  512. i = BN_num_bits(order);
  513. j = i/2 -1;
  514. BN_mask_bits(x,j);
  515. BN_set_word(y,2);
  516. BN_set_word(z,j);
  517. BN_exp(y,y,z,ctx);
  518. BN_add(x2,x,y);
  519. // fprintf(stdout,"X2=: ");
  520. // BNPrintf(x2);
  521. // fprintf(stdout,"\n");
  522. //x2*Rb+Pb;
  523. if (!EC_POINT_mul(group, tmp, NULL,b_pub_key_r,x2,ctx) )
  524. {
  525. ECDHerr(ECDH_F_ECDH_COMPUTE_KEY,ECDH_R_POINT_ARITHMETIC_FAILURE);
  526. goto err;
  527. }
  528. if ((ret=EC_POINT_new(group)) == NULL)
  529. {
  530. ECDHerr(ECDH_F_ECDH_COMPUTE_KEY,ERR_R_MALLOC_FAILURE);
  531. goto err;
  532. }
  533. if (!EC_POINT_add(group, ret, b_pub_key, tmp, ctx))
  534. {
  535. ECDHerr(ECDH_F_ECDH_COMPUTE_KEY,ECDH_R_POINT_ARITHMETIC_FAILURE);
  536. goto err;
  537. }
  538. if (!EC_POINT_get_affine_coordinates_GFp(group,ret, x, y, ctx))
  539. {
  540. goto err;
  541. }
  542. // fprintf(stdout, "\nTesting x2*Rb+Pb Key Point\n x = 0x");
  543. // BNPrintf(x);
  544. // fprintf(stdout, "\n y = 0x");
  545. // BNPrintf( y);
  546. // fprintf(stdout, "\n");
  547. //
  548. if(!EC_GROUP_get_cofactor(group, h, ctx))
  549. {
  550. goto err;
  551. }
  552. BN_mul(t,t,h,ctx);
  553. //h*t*(x2*Rb+Pb)
  554. if (!EC_POINT_mul(group, ret, NULL,ret,t,ctx) )
  555. {
  556. goto err;
  557. }
  558. if (!EC_POINT_get_affine_coordinates_GFp(group,ret, x, y, ctx))
  559. {
  560. goto err;
  561. }
  562. // fprintf(stdout, "\nTesting ret Key Point\n x = 0x");
  563. // BNPrintf(x);
  564. // fprintf(stdout, "\n y = 0x");
  565. // BNPrintf( y);
  566. // fprintf(stdout, "\n");
  567. err:
  568. if (tmp) EC_POINT_free(tmp);
  569. if (ctx) BN_CTX_end(ctx);
  570. if (ctx) BN_CTX_free(ctx);
  571. if (buf) OPENSSL_free(buf);
  572. return(ret);
  573. }
  574. /** SM2_sign_setup
  575. * precompute parts of the signing operation.
  576. * \param eckey pointer to the EC_KEY object containing a private EC key
  577. * \param ctx pointer to a BN_CTX object (may be NULL)
  578. * \param k pointer to a BIGNUM pointer for the inverse of k
  579. * \param rp pointer to a BIGNUM pointer for x coordinate of k * generator
  580. * \return 1 on success and 0 otherwise
  581. */
  582. int SM2_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp)
  583. {
  584. // ECDSA_DATA *ecdsa = ecdsa_check(eckey);
  585. // if (ecdsa == NULL)
  586. // return 0;
  587. return SM2_sign_setup(eckey, ctx_in, kinvp, rp);
  588. }
  589. /** SM2_sign_ex
  590. * computes ECDSA signature of a given hash value using the supplied
  591. * private key (note: sig must point to ECDSA_size(eckey) bytes of memory).
  592. * \param type this parameter is ignored
  593. * \param dgst pointer to the hash value to sign
  594. * \param dgstlen length of the hash value
  595. * \param sig buffer to hold the DER encoded signature
  596. * \param siglen pointer to the length of the returned signature
  597. * \param k optional pointer to a pre-computed inverse k
  598. * \param rp optional pointer to the pre-computed rp value (see
  599. * ECDSA_sign_setup
  600. * \param eckey pointer to the EC_KEY object containing a private EC key
  601. * \return 1 on success and 0 otherwise
  602. */
  603. int SM2_sign_ex(int type, const unsigned char *dgst, int dlen, unsigned char
  604. *sig, unsigned int *siglen, const BIGNUM *kinv, const BIGNUM *r,
  605. EC_KEY *eckey)
  606. {
  607. ECDSA_SIG *s;
  608. RAND_seed(dgst, dlen);
  609. s = sm2_do_sign(dgst, dlen, kinv, r, eckey);
  610. if (s == NULL)
  611. {
  612. *siglen=0;
  613. return 0;
  614. }
  615. *siglen = i2d_ECDSA_SIG(s, &sig);
  616. ECDSA_SIG_free(s);
  617. return 1;
  618. }
  619. /** SM2_sign
  620. * computes ECDSA signature of a given hash value using the supplied
  621. * private key (note: sig must point to ECDSA_size(eckey) bytes of memory).
  622. * \param type this parameter is ignored
  623. * \param dgst pointer to the hash value to sign
  624. * \param dgstlen length of the hash value
  625. * \param sig buffer to hold the DER encoded signature
  626. * \param siglen pointer to the length of the returned signature
  627. * \param eckey pointer to the EC_KEY object containing a private EC key
  628. * \return 1 on success and 0 otherwise
  629. */
  630. int SM2_sign(int type, const unsigned char *dgst, int dlen, unsigned char
  631. *sig, unsigned int *siglen, EC_KEY *eckey)
  632. {
  633. return SM2_sign_ex(type, dgst, dlen, sig, siglen, NULL, NULL, eckey);
  634. }
  635. /** SM2_verify
  636. * verifies that the given signature is valid ECDSA signature
  637. * of the supplied hash value using the specified public key.
  638. * \param type this parameter is ignored
  639. * \param dgst pointer to the hash value
  640. * \param dgstlen length of the hash value
  641. * \param sig pointer to the DER encoded signature
  642. * \param siglen length of the DER encoded signature
  643. * \param eckey pointer to the EC_KEY object containing a public EC key
  644. * \return 1 if the signature is valid, 0 if the signature is invalid and -1 on error
  645. */
  646. int SM2_verify(int type, const unsigned char *dgst, int dgst_len,
  647. const unsigned char *sigbuf, int sig_len, EC_KEY *eckey)
  648. {
  649. ECDSA_SIG *s;
  650. int ret=-1;
  651. s = ECDSA_SIG_new();
  652. if (s == NULL) return(ret);
  653. if (d2i_ECDSA_SIG(&s, &sigbuf, sig_len) == NULL) goto err;
  654. ret=sm2_do_verify(dgst, dgst_len, s, eckey);
  655. err:
  656. ECDSA_SIG_free(s);
  657. return(ret);
  658. }
  659. int SM2_DH_key(const EC_GROUP * group, const EC_POINT *b_pub_key_r, const EC_POINT *b_pub_key, const BIGNUM *a_r,EC_KEY *a_eckey,
  660. unsigned char *outkey,size_t keylen)
  661. {
  662. EC_POINT *dhpoint = NULL;
  663. BN_CTX * ctx;
  664. EC_POINT *P;
  665. BIGNUM *x, *y;
  666. int ret = 0;
  667. unsigned char in[128];
  668. int inlen;
  669. int len;
  670. P = EC_POINT_new(group);
  671. if (!P ) goto err;
  672. ctx = BN_CTX_new();
  673. x = BN_new();
  674. y = BN_new();
  675. if (!x || !y ) goto err;
  676. dhpoint = sm2_compute_key(b_pub_key_r,b_pub_key,a_r,a_eckey);
  677. if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field)
  678. {
  679. if (!EC_POINT_get_affine_coordinates_GFp(group,dhpoint, x, y, ctx))
  680. {
  681. fprintf(stdout, " failed\n");
  682. goto err;
  683. }
  684. }
  685. else
  686. {
  687. if (!EC_POINT_get_affine_coordinates_GF2m(group,dhpoint, x, y, ctx))
  688. {
  689. ECDHerr(ECDH_F_ECDH_COMPUTE_KEY,ECDH_R_POINT_ARITHMETIC_FAILURE);
  690. goto err;
  691. }
  692. }
  693. // if (!EC_POINT_get_affine_coordinates_GFp(group,dhpoint, x, y, ctx))
  694. // {
  695. // fprintf(stdout, " failed\n");
  696. // goto err;
  697. // }
  698. fprintf(stdout, "\nTesting DH Point\n Xv = 0x");
  699. BNPrintf(x);
  700. fprintf(stdout, "\n Yv = 0x");
  701. BNPrintf( y);
  702. fprintf(stdout, "\n");
  703. len = BN_bn2bin(x,in);
  704. inlen =BN_bn2bin(y,in+len);
  705. inlen = inlen + len;
  706. ret = x9_63_kdf(EVP_sha256(),in,inlen,keylen,outkey);
  707. //ret = 1;
  708. err:
  709. EC_POINT_free(P);
  710. EC_POINT_free(dhpoint);
  711. BN_CTX_free(ctx);
  712. return ret;
  713. }