ecc.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  1. /* LibTomCrypt, modular cryptographic library -- Tom St Denis
  2. *
  3. * LibTomCrypt is a library that provides various cryptographic
  4. * algorithms in a highly modular and flexible manner.
  5. *
  6. * The library is free for all purposes without any express
  7. * guarantee it works.
  8. *
  9. * Tom St Denis, [email protected], http://libtomcrypt.org
  10. */
  11. /* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b
  12. *
  13. * All curves taken from NIST recommendation paper of July 1999
  14. * Available at http://csrc.nist.gov/cryptval/dss.htm
  15. */
  16. #include "mycrypt.h"
  17. #ifdef MECC
  18. /* size of our temp buffers for exported keys */
  19. #define ECC_BUF_SIZE 160
  20. /* max private key size */
  21. #define ECC_MAXSIZE 66
  22. /* This holds the key settings. ***MUST*** be organized by size from smallest to largest. */
  23. static const struct {
  24. int size;
  25. char *name, *prime, *B, *order, *Gx, *Gy;
  26. } sets[] = {
  27. #ifdef ECC160
  28. {
  29. 20,
  30. "ECC-160",
  31. /* prime */
  32. "G00000000000000000000000007",
  33. /* B */
  34. "1oUV2vOaSlWbxr6",
  35. /* order */
  36. "G0000000000004sCQUtDxaqDUN5",
  37. /* Gx */
  38. "jpqOf1BHus6Yd/pyhyVpP",
  39. /* Gy */
  40. "D/wykuuIFfr+vPyx7kQEPu8MixO",
  41. },
  42. #endif
  43. #ifdef ECC192
  44. {
  45. 24,
  46. "ECC-192",
  47. /* prime */
  48. "/////////////////////l//////////",
  49. /* B */
  50. "P2456UMSWESFf+chSYGmIVwutkp1Hhcn",
  51. /* order */
  52. "////////////////cTxuDXHhoR6qqYWn",
  53. /* Gx */
  54. "68se3h0maFPylo3hGw680FJ/2ls2/n0I",
  55. /* Gy */
  56. "1nahbV/8sdXZ417jQoJDrNFvTw4UUKWH"
  57. },
  58. #endif
  59. #ifdef ECC224
  60. {
  61. 28,
  62. "ECC-224",
  63. /* prime */
  64. "400000000000000000000000000000000000BV",
  65. /* B */
  66. "21HkWGL2CxJIp",
  67. /* order */
  68. "4000000000000000000Kxnixk9t8MLzMiV264/",
  69. /* Gx */
  70. "jpqOf1BHus6Yd/pyhyVpP",
  71. /* Gy */
  72. "3FCtyo2yHA5SFjkCGbYxbOvNeChwS+j6wSIwck",
  73. },
  74. #endif
  75. #ifdef ECC256
  76. {
  77. 32,
  78. "ECC-256",
  79. /* Prime */
  80. "F////y000010000000000000000////////////////",
  81. /* B */
  82. "5h6DTYgEfFdi+kzLNQOXhnb7GQmp5EmzZlEF3udqc1B",
  83. /* Order */
  84. "F////y00000//////////+yvlgjfnUUXFEvoiByOoLH",
  85. /* Gx */
  86. "6iNqVBXB497+BpcvMEaGF9t0ts1BUipeFIXEKNOcCAM",
  87. /* Gy */
  88. "4/ZGkB+6d+RZkVhIdmFdXOhpZDNQp5UpiksG6Wtlr7r"
  89. },
  90. #endif
  91. #ifdef ECC384
  92. {
  93. 48,
  94. "ECC-384",
  95. /* prime */
  96. "//////////////////////////////////////////x/////00000000003/"
  97. "////",
  98. /* B */
  99. "ip4lf+8+v+IOZWLhu/Wj6HWTd6x+WK4I0nG8Zr0JXrh6LZcDYYxHdIg5oEtJ"
  100. "x2hl",
  101. /* Order */
  102. "////////////////////////////////nsDDWVGtBTzO6WsoIB2dUkpi6MhC"
  103. "nIbp",
  104. /* Gx and Gy */
  105. "geVA8hwB1JUEiSSUyo2jT6uTEsABfvkOMVT1u89KAZXL0l9TlrKfR3fKNZXo"
  106. "TWgt",
  107. "DXVUIfOcB6zTdfY/afBSAVZq7RqecXHywTen4xNmkC0AOB7E7Nw1dNf37NoG"
  108. "wWvV"
  109. },
  110. #endif
  111. #ifdef ECC521
  112. {
  113. 65,
  114. "ECC-521",
  115. /* prime */
  116. "V///////////////////////////////////////////////////////////"
  117. "///////////////////////////",
  118. /* B */
  119. "56LFhbXZXoQ7vAQ8Q2sXK3kejfoMvcp5VEuj8cHZl49uLOPEL7iVfDx5bB0l"
  120. "JknlmSrSz+8FImqyUz57zHhK3y0",
  121. /* Order */
  122. "V//////////////////////////////////////////+b66XuE/BvPhVym1I"
  123. "FS9fT0xjScuYPn7hhjljnwHE6G9",
  124. /* Gx and Gy */
  125. "CQ5ZWQt10JfpPu+osOZbRH2d6I1EGK/jI7uAAzWQqqzkg5BNdVlvrae/Xt19"
  126. "wB/gDupIBF1XMf2c/b+VZ72vRrc",
  127. "HWvAMfucZl015oANxGiVHlPcFL4ILURH6WNhxqN9pvcB9VkSfbUz2P0nL2v0"
  128. "J+j1s4rF726edB2G8Y+b7QVqMPG",
  129. },
  130. #endif
  131. {
  132. 0,
  133. NULL, NULL, NULL, NULL, NULL, NULL
  134. }
  135. };
  136. #if 0
  137. /* you plug in a prime and B value and it finds a pseudo-random base point */
  138. void ecc_find_base(void)
  139. {
  140. static char *prime = "26959946667150639794667015087019630673637144422540572481103610249951";
  141. static char *order = "26959946667150639794667015087019637467111563745054605861463538557247";
  142. static char *b = "9538957348957353489587";
  143. mp_int pp, p, r, B, tmp1, tmp2, tx, ty, x, y;
  144. char buf[4096];
  145. int i;
  146. mp_init_multi(&tx, &ty, &x, &y, &p, &pp, &r, &B, &tmp1, &tmp2, NULL);
  147. mp_read_radix(&p, prime, 10);
  148. mp_read_radix(&r, order, 10);
  149. mp_read_radix(&B, b, 10);
  150. /* get (p+1)/4 */
  151. mp_add_d(&p, 1, &pp);
  152. mp_div_2(&pp, &pp);
  153. mp_div_2(&pp, &pp);
  154. buf[0] = 0;
  155. do {
  156. printf("."); fflush(stdout);
  157. /* make a random value of x */
  158. for (i = 0; i < 16; i++) buf[i+1] = rand() & 255;
  159. mp_read_raw(&x, buf, 17);
  160. mp_copy(&x, &tx);
  161. /* now compute x^3 - 3x + b */
  162. mp_expt_d(&x, 3, &tmp1);
  163. mp_mul_d(&x, 3, &tmp2);
  164. mp_sub(&tmp1, &tmp2, &tmp1);
  165. mp_add(&tmp1, &B, &tmp1);
  166. mp_mod(&tmp1, &p, &tmp1);
  167. /* now compute sqrt via x^((p+1)/4) */
  168. mp_exptmod(&tmp1, &pp, &p, &tmp2);
  169. mp_copy(&tmp2, &ty);
  170. /* now square it */
  171. mp_sqrmod(&tmp2, &p, &tmp2);
  172. /* tmp2 should equal tmp1 */
  173. } while (mp_cmp(&tmp1, &tmp2));
  174. /* now output values in way that libtomcrypt wants */
  175. mp_todecimal(&p, buf);
  176. printf("\n\np==%s\n", buf);
  177. mp_tohex(&B, buf);
  178. printf("b==%s\n", buf);
  179. mp_todecimal(&r, buf);
  180. printf("r==%s\n", buf);
  181. mp_tohex(&tx, buf);
  182. printf("Gx==%s\n", buf);
  183. mp_tohex(&ty, buf);
  184. printf("Gy==%s\n", buf);
  185. mp_clear_multi(&tx, &ty, &x, &y, &p, &pp, &r, &B, &tmp1, &tmp2, NULL);
  186. }
  187. #endif
  188. static int is_valid_idx(int n)
  189. {
  190. int x;
  191. for (x = 0; sets[x].size != 0; x++);
  192. if ((n < 0) || (n >= x)) {
  193. return 0;
  194. }
  195. return 1;
  196. }
  197. static ecc_point *new_point(void)
  198. {
  199. ecc_point *p;
  200. p = XMALLOC(sizeof(ecc_point));
  201. if (p == NULL) {
  202. return NULL;
  203. }
  204. if (mp_init_multi(&p->x, &p->y, NULL) != MP_OKAY) {
  205. XFREE(p);
  206. return NULL;
  207. }
  208. return p;
  209. }
  210. static void del_point(ecc_point *p)
  211. {
  212. /* prevents free'ing null arguments */
  213. if (p != NULL) {
  214. mp_clear_multi(&p->x, &p->y, NULL);
  215. XFREE(p);
  216. }
  217. }
  218. /* double a point R = 2P, R can be P*/
  219. static int dbl_point(ecc_point *P, ecc_point *R, mp_int *modulus, mp_int *mu)
  220. {
  221. mp_int s, tmp, tmpx;
  222. int err;
  223. if ((err = mp_init_multi(&s, &tmp, &tmpx, NULL)) != MP_OKAY) {
  224. return mpi_to_ltc_error(err);
  225. }
  226. /* s = (3Xp^2 + a) / (2Yp) */
  227. if ((err = mp_mul_2(&P->y, &tmp)) != MP_OKAY) { goto error; } /* tmp = 2*y */
  228. if ((err = mp_invmod(&tmp, modulus, &tmp)) != MP_OKAY) { goto error; } /* tmp = 1/tmp mod modulus */
  229. if ((err = mp_sqr(&P->x, &s)) != MP_OKAY) { goto error; } /* s = x^2 */
  230. if ((err = mp_reduce(&s, modulus, mu)) != MP_OKAY) { goto error; }
  231. if ((err = mp_mul_d(&s,(mp_digit)3, &s)) != MP_OKAY) { goto error; } /* s = 3*(x^2) */
  232. if ((err = mp_sub_d(&s,(mp_digit)3, &s)) != MP_OKAY) { goto error; } /* s = 3*(x^2) - 3 */
  233. if (mp_cmp_d(&s, 0) == MP_LT) { /* if s < 0 add modulus */
  234. if ((err = mp_add(&s, modulus, &s)) != MP_OKAY) { goto error; }
  235. }
  236. if ((err = mp_mul(&s, &tmp, &s)) != MP_OKAY) { goto error; } /* s = tmp * s mod modulus */
  237. if ((err = mp_reduce(&s, modulus, mu)) != MP_OKAY) { goto error; }
  238. /* Xr = s^2 - 2Xp */
  239. if ((err = mp_sqr(&s, &tmpx)) != MP_OKAY) { goto error; } /* tmpx = s^2 */
  240. if ((err = mp_reduce(&tmpx, modulus, mu)) != MP_OKAY) { goto error; } /* tmpx = tmpx mod modulus */
  241. if ((err = mp_sub(&tmpx, &P->x, &tmpx)) != MP_OKAY) { goto error; } /* tmpx = tmpx - x */
  242. if ((err = mp_submod(&tmpx, &P->x, modulus, &tmpx)) != MP_OKAY) { goto error; } /* tmpx = tmpx - x mod modulus */
  243. /* Yr = -Yp + s(Xp - Xr) */
  244. if ((err = mp_sub(&P->x, &tmpx, &tmp)) != MP_OKAY) { goto error; } /* tmp = x - tmpx */
  245. if ((err = mp_mul(&tmp, &s, &tmp)) != MP_OKAY) { goto error; } /* tmp = tmp * s */
  246. if ((err = mp_submod(&tmp, &P->y, modulus, &R->y)) != MP_OKAY) { goto error; } /* y = tmp - y mod modulus */
  247. if ((err = mp_copy(&tmpx, &R->x)) != MP_OKAY) { goto error; } /* x = tmpx */
  248. err = CRYPT_OK;
  249. goto done;
  250. error:
  251. err = mpi_to_ltc_error(err);
  252. done:
  253. mp_clear_multi(&tmpx, &tmp, &s, NULL);
  254. return err;
  255. }
  256. /* add two different points over Z/pZ, R = P + Q, note R can equal either P or Q */
  257. static int add_point(ecc_point *P, ecc_point *Q, ecc_point *R, mp_int *modulus, mp_int *mu)
  258. {
  259. mp_int s, tmp, tmpx;
  260. int err;
  261. if ((err = mp_init(&tmp)) != MP_OKAY) {
  262. return mpi_to_ltc_error(err);
  263. }
  264. /* is P==Q or P==-Q? */
  265. if (((err = mp_neg(&Q->y, &tmp)) != MP_OKAY) || ((err = mp_mod(&tmp, modulus, &tmp)) != MP_OKAY)) {
  266. mp_clear(&tmp);
  267. return mpi_to_ltc_error(err);
  268. }
  269. if (mp_cmp(&P->x, &Q->x) == MP_EQ)
  270. if (mp_cmp(&P->y, &Q->y) == MP_EQ || mp_cmp(&P->y, &tmp) == MP_EQ) {
  271. mp_clear(&tmp);
  272. return dbl_point(P, R, modulus, mu);
  273. }
  274. if ((err = mp_init_multi(&tmpx, &s, NULL)) != MP_OKAY) {
  275. mp_clear(&tmp);
  276. return mpi_to_ltc_error(err);
  277. }
  278. /* get s = (Yp - Yq)/(Xp-Xq) mod p */
  279. if ((err = mp_sub(&P->x, &Q->x, &tmp)) != MP_OKAY) { goto error; } /* tmp = Px - Qx mod modulus */
  280. if (mp_cmp_d(&tmp, 0) == MP_LT) { /* if tmp<0 add modulus */
  281. if ((err = mp_add(&tmp, modulus, &tmp)) != MP_OKAY) { goto error; }
  282. }
  283. if ((err = mp_invmod(&tmp, modulus, &tmp)) != MP_OKAY) { goto error; } /* tmp = 1/tmp mod modulus */
  284. if ((err = mp_sub(&P->y, &Q->y, &s)) != MP_OKAY) { goto error; } /* s = Py - Qy mod modulus */
  285. if (mp_cmp_d(&s, 0) == MP_LT) { /* if s<0 add modulus */
  286. if ((err = mp_add(&s, modulus, &s)) != MP_OKAY) { goto error; }
  287. }
  288. if ((err = mp_mul(&s, &tmp, &s)) != MP_OKAY) { goto error; } /* s = s * tmp mod modulus */
  289. if ((err = mp_reduce(&s, modulus, mu)) != MP_OKAY) { goto error; }
  290. /* Xr = s^2 - Xp - Xq */
  291. if ((err = mp_sqr(&s, &tmp)) != MP_OKAY) { goto error; } /* tmp = s^2 mod modulus */
  292. if ((err = mp_reduce(&tmp, modulus, mu)) != MP_OKAY) { goto error; }
  293. if ((err = mp_sub(&tmp, &P->x, &tmp)) != MP_OKAY) { goto error; } /* tmp = tmp - Px */
  294. if ((err = mp_sub(&tmp, &Q->x, &tmpx)) != MP_OKAY) { goto error; } /* tmpx = tmp - Qx */
  295. /* Yr = -Yp + s(Xp - Xr) */
  296. if ((err = mp_sub(&P->x, &tmpx, &tmp)) != MP_OKAY) { goto error; } /* tmp = Px - tmpx */
  297. if ((err = mp_mul(&tmp, &s, &tmp)) != MP_OKAY) { goto error; } /* tmp = tmp * s */
  298. if ((err = mp_submod(&tmp, &P->y, modulus, &R->y)) != MP_OKAY) { goto error; } /* Ry = tmp - Py mod modulus */
  299. if ((err = mp_mod(&tmpx, modulus, &R->x)) != MP_OKAY) { goto error; } /* Rx = tmpx mod modulus */
  300. err = CRYPT_OK;
  301. goto done;
  302. error:
  303. err = mpi_to_ltc_error(err);
  304. done:
  305. mp_clear_multi(&s, &tmpx, &tmp, NULL);
  306. return err;
  307. }
  308. /* size of sliding window, don't change this! */
  309. #define WINSIZE 4
  310. /* perform R = kG where k == integer and G == ecc_point */
  311. static int ecc_mulmod(mp_int *k, ecc_point *G, ecc_point *R, mp_int *modulus)
  312. {
  313. ecc_point *tG, *M[8];
  314. int i, j, err;
  315. mp_int mu;
  316. mp_digit buf;
  317. int first, bitbuf, bitcpy, bitcnt, mode, digidx;
  318. /* init barrett reduction */
  319. if ((err = mp_init(&mu)) != MP_OKAY) {
  320. return mpi_to_ltc_error(err);
  321. }
  322. if ((err = mp_reduce_setup(&mu, modulus)) != MP_OKAY) {
  323. mp_clear(&mu);
  324. return mpi_to_ltc_error(err);
  325. }
  326. /* alloc ram for window temps */
  327. for (i = 0; i < 8; i++) {
  328. M[i] = new_point();
  329. if (M[i] == NULL) {
  330. for (j = 0; j < i; j++) {
  331. del_point(M[j]);
  332. }
  333. mp_clear(&mu);
  334. return CRYPT_MEM;
  335. }
  336. }
  337. /* make a copy of G incase R==G */
  338. tG = new_point();
  339. if (tG == NULL) { err = CRYPT_MEM; goto done; }
  340. /* tG = G */
  341. if ((err = mp_copy(&G->x, &tG->x)) != MP_OKAY) { goto error; }
  342. if ((err = mp_copy(&G->y, &tG->y)) != MP_OKAY) { goto error; }
  343. /* calc the M tab, which holds kG for k==8..15 */
  344. /* M[0] == 8G */
  345. if ((err = dbl_point(G, M[0], modulus, &mu)) != CRYPT_OK) { goto done; }
  346. if ((err = dbl_point(M[0], M[0], modulus, &mu)) != CRYPT_OK) { goto done; }
  347. if ((err = dbl_point(M[0], M[0], modulus, &mu)) != CRYPT_OK) { goto done; }
  348. /* now find (8+k)G for k=1..7 */
  349. for (j = 9; j < 16; j++) {
  350. if ((err = add_point(M[j-9], G, M[j-8], modulus, &mu)) != CRYPT_OK) { goto done; }
  351. }
  352. /* setup sliding window */
  353. mode = 0;
  354. bitcnt = 1;
  355. buf = 0;
  356. digidx = k->used - 1;
  357. bitcpy = bitbuf = 0;
  358. first = 1;
  359. /* perform ops */
  360. for (;;) {
  361. /* grab next digit as required */
  362. if (--bitcnt == 0) {
  363. if (digidx == -1) {
  364. break;
  365. }
  366. buf = k->dp[digidx--];
  367. bitcnt = (int) DIGIT_BIT;
  368. }
  369. /* grab the next msb from the multiplicand */
  370. i = (buf >> (DIGIT_BIT - 1)) & 1;
  371. buf <<= 1;
  372. /* skip leading zero bits */
  373. if (mode == 0 && i == 0) {
  374. continue;
  375. }
  376. /* if the bit is zero and mode == 1 then we double */
  377. if (mode == 1 && i == 0) {
  378. if ((err = dbl_point(R, R, modulus, &mu)) != CRYPT_OK) { goto done; }
  379. continue;
  380. }
  381. /* else we add it to the window */
  382. bitbuf |= (i << (WINSIZE - ++bitcpy));
  383. mode = 2;
  384. if (bitcpy == WINSIZE) {
  385. /* if this is the first window we do a simple copy */
  386. if (first == 1) {
  387. /* R = kG [k = first window] */
  388. if ((err = mp_copy(&M[bitbuf-8]->x, &R->x)) != MP_OKAY) { goto error; }
  389. if ((err = mp_copy(&M[bitbuf-8]->y, &R->y)) != MP_OKAY) { goto error; }
  390. first = 0;
  391. } else {
  392. /* normal window */
  393. /* ok window is filled so double as required and add */
  394. /* double first */
  395. for (j = 0; j < WINSIZE; j++) {
  396. if ((err = dbl_point(R, R, modulus, &mu)) != CRYPT_OK) { goto done; }
  397. }
  398. /* then add, bitbuf will be 8..15 [8..2^WINSIZE] guaranteed */
  399. if ((err = add_point(R, M[bitbuf-8], R, modulus, &mu)) != CRYPT_OK) { goto done; }
  400. }
  401. /* empty window and reset */
  402. bitcpy = bitbuf = 0;
  403. mode = 1;
  404. }
  405. }
  406. /* if bits remain then double/add */
  407. if (mode == 2 && bitcpy > 0) {
  408. /* double then add */
  409. for (j = 0; j < bitcpy; j++) {
  410. /* only double if we have had at least one add first */
  411. if (first == 0) {
  412. if ((err = dbl_point(R, R, modulus, &mu)) != CRYPT_OK) { goto done; }
  413. }
  414. bitbuf <<= 1;
  415. if ((bitbuf & (1 << WINSIZE)) != 0) {
  416. if (first == 1){
  417. /* first add, so copy */
  418. if ((err = mp_copy(&tG->x, &R->x)) != MP_OKAY) { goto error; }
  419. if ((err = mp_copy(&tG->y, &R->y)) != MP_OKAY) { goto error; }
  420. first = 0;
  421. } else {
  422. /* then add */
  423. if ((err = add_point(R, tG, R, modulus, &mu)) != CRYPT_OK) { goto done; }
  424. }
  425. }
  426. }
  427. }
  428. err = CRYPT_OK;
  429. goto done;
  430. error:
  431. err = mpi_to_ltc_error(err);
  432. done:
  433. del_point(tG);
  434. for (i = 0; i < 8; i++) {
  435. del_point(M[i]);
  436. }
  437. mp_clear(&mu);
  438. return err;
  439. }
  440. #undef WINSIZE
  441. int ecc_test(void)
  442. {
  443. mp_int modulus, order;
  444. ecc_point *G, *GG;
  445. int i, err, primality;
  446. if ((err = mp_init_multi(&modulus, &order, NULL)) != MP_OKAY) {
  447. return mpi_to_ltc_error(err);
  448. }
  449. G = new_point();
  450. GG = new_point();
  451. if (G == NULL || GG == NULL) {
  452. mp_clear_multi(&modulus, &order, NULL);
  453. del_point(G);
  454. del_point(GG);
  455. return CRYPT_MEM;
  456. }
  457. for (i = 0; sets[i].size; i++) {
  458. #if 0
  459. printf("Testing %d\n", sets[i].size);
  460. #endif
  461. if ((err = mp_read_radix(&modulus, (char *)sets[i].prime, 64)) != MP_OKAY) { goto error; }
  462. if ((err = mp_read_radix(&order, (char *)sets[i].order, 64)) != MP_OKAY) { goto error; }
  463. /* is prime actually prime? */
  464. if ((err = is_prime(&modulus, &primality)) != CRYPT_OK) { goto done; }
  465. if (primality == 0) {
  466. err = CRYPT_FAIL_TESTVECTOR;
  467. goto done;
  468. }
  469. /* is order prime ? */
  470. if ((err = is_prime(&order, &primality)) != CRYPT_OK) { goto done; }
  471. if (primality == 0) {
  472. err = CRYPT_FAIL_TESTVECTOR;
  473. goto done;
  474. }
  475. if ((err = mp_read_radix(&G->x, (char *)sets[i].Gx, 64)) != MP_OKAY) { goto error; }
  476. if ((err = mp_read_radix(&G->y, (char *)sets[i].Gy, 64)) != MP_OKAY) { goto error; }
  477. /* then we should have G == (order + 1)G */
  478. if ((err = mp_add_d(&order, 1, &order)) != MP_OKAY) { goto error; }
  479. if ((err = ecc_mulmod(&order, G, GG, &modulus)) != CRYPT_OK) { goto done; }
  480. if (mp_cmp(&G->x, &GG->x) != 0 || mp_cmp(&G->y, &GG->y) != 0) {
  481. err = CRYPT_FAIL_TESTVECTOR;
  482. goto done;
  483. }
  484. }
  485. err = CRYPT_OK;
  486. goto done;
  487. error:
  488. err = mpi_to_ltc_error(err);
  489. done:
  490. del_point(GG);
  491. del_point(G);
  492. mp_clear_multi(&order, &modulus, NULL);
  493. return err;
  494. }
  495. void ecc_sizes(int *low, int *high)
  496. {
  497. int i;
  498. _ARGCHK(low != NULL);
  499. _ARGCHK(high != NULL);
  500. *low = INT_MAX;
  501. *high = 0;
  502. for (i = 0; sets[i].size != 0; i++) {
  503. if (sets[i].size < *low) {
  504. *low = sets[i].size;
  505. }
  506. if (sets[i].size > *high) {
  507. *high = sets[i].size;
  508. }
  509. }
  510. }
  511. int ecc_make_key(prng_state *prng, int wprng, int keysize, ecc_key *key)
  512. {
  513. int x, err;
  514. ecc_point *base;
  515. mp_int prime;
  516. unsigned char *buf;
  517. _ARGCHK(key != NULL);
  518. /* good prng? */
  519. if ((err = prng_is_valid(wprng)) != CRYPT_OK) {
  520. return err;
  521. }
  522. /* find key size */
  523. for (x = 0; (keysize > sets[x].size) && (sets[x].size != 0); x++);
  524. keysize = sets[x].size;
  525. _ARGCHK(keysize <= ECC_MAXSIZE);
  526. if (sets[x].size == 0) {
  527. return CRYPT_INVALID_KEYSIZE;
  528. }
  529. key->idx = x;
  530. /* allocate ram */
  531. base = NULL;
  532. buf = XMALLOC(ECC_MAXSIZE);
  533. if (buf == NULL) {
  534. return CRYPT_MEM;
  535. }
  536. /* make up random string */
  537. if (prng_descriptor[wprng].read(buf, (unsigned long)keysize, prng) != (unsigned long)keysize) {
  538. err = CRYPT_ERROR_READPRNG;
  539. goto __ERR2;
  540. }
  541. /* setup the key variables */
  542. if ((err = mp_init_multi(&key->pubkey.x, &key->pubkey.y, &key->k, &prime, NULL)) != MP_OKAY) {
  543. err = mpi_to_ltc_error(err);
  544. goto __ERR;
  545. }
  546. base = new_point();
  547. if (base == NULL) {
  548. mp_clear_multi(&key->pubkey.x, &key->pubkey.y, &key->k, &prime, NULL);
  549. err = CRYPT_MEM;
  550. goto __ERR;
  551. }
  552. /* read in the specs for this key */
  553. if ((err = mp_read_radix(&prime, (char *)sets[key->idx].prime, 64)) != MP_OKAY) { goto error; }
  554. if ((err = mp_read_radix(&base->x, (char *)sets[key->idx].Gx, 64)) != MP_OKAY) { goto error; }
  555. if ((err = mp_read_radix(&base->y, (char *)sets[key->idx].Gy, 64)) != MP_OKAY) { goto error; }
  556. if ((err = mp_read_unsigned_bin(&key->k, (unsigned char *)buf, keysize)) != MP_OKAY) { goto error; }
  557. /* make the public key */
  558. if ((err = ecc_mulmod(&key->k, base, &key->pubkey, &prime)) != CRYPT_OK) { goto __ERR; }
  559. key->type = PK_PRIVATE;
  560. /* shrink key */
  561. if ((err = mp_shrink(&key->k)) != MP_OKAY) { goto error; }
  562. if ((err = mp_shrink(&key->pubkey.x)) != MP_OKAY) { goto error; }
  563. if ((err = mp_shrink(&key->pubkey.y)) != MP_OKAY) { goto error; }
  564. /* free up ram */
  565. err = CRYPT_OK;
  566. goto __ERR;
  567. error:
  568. err = mpi_to_ltc_error(err);
  569. __ERR:
  570. del_point(base);
  571. mp_clear(&prime);
  572. __ERR2:
  573. #ifdef CLEAN_STACK
  574. zeromem(buf, ECC_MAXSIZE);
  575. #endif
  576. XFREE(buf);
  577. return err;
  578. }
  579. void ecc_free(ecc_key *key)
  580. {
  581. _ARGCHK(key != NULL);
  582. mp_clear_multi(&key->pubkey.x, &key->pubkey.y, &key->k, NULL);
  583. }
  584. static int compress_y_point(ecc_point *pt, int idx, int *result)
  585. {
  586. mp_int tmp, tmp2, p;
  587. int err;
  588. _ARGCHK(pt != NULL);
  589. _ARGCHK(result != NULL);
  590. if ((err = mp_init_multi(&tmp, &tmp2, &p, NULL)) != MP_OKAY) {
  591. return mpi_to_ltc_error(err);
  592. }
  593. /* get x^3 - 3x + b */
  594. if ((err = mp_read_radix(&p, (char *)sets[idx].B, 64)) != MP_OKAY) { goto error; } /* p = B */
  595. if ((err = mp_expt_d(&pt->x, 3, &tmp)) != MP_OKAY) { goto error; } /* tmp = pX^3 */
  596. if ((err = mp_mul_d(&pt->x, 3, &tmp2)) != MP_OKAY) { goto error; } /* tmp2 = 3*pX^3 */
  597. if ((err = mp_sub(&tmp, &tmp2, &tmp)) != MP_OKAY) { goto error; } /* tmp = tmp - tmp2 */
  598. if ((err = mp_add(&tmp, &p, &tmp)) != MP_OKAY) { goto error; } /* tmp = tmp + p */
  599. if ((err = mp_read_radix(&p, (char *)sets[idx].prime, 64)) != MP_OKAY) { goto error; } /* p = prime */
  600. if ((err = mp_mod(&tmp, &p, &tmp)) != MP_OKAY) { goto error; } /* tmp = tmp mod p */
  601. /* now find square root */
  602. if ((err = mp_add_d(&p, 1, &tmp2)) != MP_OKAY) { goto error; } /* tmp2 = p + 1 */
  603. if ((err = mp_div_2d(&tmp2, 2, &tmp2, NULL)) != MP_OKAY) { goto error; } /* tmp2 = (p+1)/4 */
  604. if ((err = mp_exptmod(&tmp, &tmp2, &p, &tmp)) != MP_OKAY) { goto error; } /* tmp = (x^3 - 3x + b)^((p+1)/4) mod p */
  605. /* if tmp equals the y point give a 0, otherwise 1 */
  606. if (mp_cmp(&tmp, &pt->y) == 0) {
  607. *result = 0;
  608. } else {
  609. *result = 1;
  610. }
  611. err = CRYPT_OK;
  612. goto done;
  613. error:
  614. err = mpi_to_ltc_error(err);
  615. done:
  616. mp_clear_multi(&p, &tmp, &tmp2, NULL);
  617. return err;
  618. }
  619. static int expand_y_point(ecc_point *pt, int idx, int result)
  620. {
  621. mp_int tmp, tmp2, p;
  622. int err;
  623. _ARGCHK(pt != NULL);
  624. if ((err = mp_init_multi(&tmp, &tmp2, &p, NULL)) != MP_OKAY) {
  625. return CRYPT_MEM;
  626. }
  627. /* get x^3 - 3x + b */
  628. if ((err = mp_read_radix(&p, (char *)sets[idx].B, 64)) != MP_OKAY) { goto error; } /* p = B */
  629. if ((err = mp_expt_d(&pt->x, 3, &tmp)) != MP_OKAY) { goto error; } /* tmp = pX^3 */
  630. if ((err = mp_mul_d(&pt->x, 3, &tmp2)) != MP_OKAY) { goto error; } /* tmp2 = 3*pX^3 */
  631. if ((err = mp_sub(&tmp, &tmp2, &tmp)) != MP_OKAY) { goto error; } /* tmp = tmp - tmp2 */
  632. if ((err = mp_add(&tmp, &p, &tmp)) != MP_OKAY) { goto error; } /* tmp = tmp + p */
  633. if ((err = mp_read_radix(&p, (char *)sets[idx].prime, 64)) != MP_OKAY) { goto error; } /* p = prime */
  634. if ((err = mp_mod(&tmp, &p, &tmp)) != MP_OKAY) { goto error; } /* tmp = tmp mod p */
  635. /* now find square root */
  636. if ((err = mp_add_d(&p, 1, &tmp2)) != MP_OKAY) { goto error; } /* tmp2 = p + 1 */
  637. if ((err = mp_div_2d(&tmp2, 2, &tmp2, NULL)) != MP_OKAY) { goto error; } /* tmp2 = (p+1)/4 */
  638. if ((err = mp_exptmod(&tmp, &tmp2, &p, &tmp)) != MP_OKAY) { goto error; } /* tmp = (x^3 - 3x + b)^((p+1)/4) mod p */
  639. /* if result==0, then y==tmp, otherwise y==p-tmp */
  640. if (result == 0) {
  641. if ((err = mp_copy(&tmp, &pt->y) != MP_OKAY)) { goto error; }
  642. } else {
  643. if ((err = mp_sub(&p, &tmp, &pt->y) != MP_OKAY)) { goto error; }
  644. }
  645. err = CRYPT_OK;
  646. goto done;
  647. error:
  648. err = mpi_to_ltc_error(err);
  649. done:
  650. mp_clear_multi(&p, &tmp, &tmp2, NULL);
  651. return err;
  652. }
  653. int ecc_export(unsigned char *out, unsigned long *outlen, int type, ecc_key *key)
  654. {
  655. unsigned long y, z;
  656. int cp, err;
  657. _ARGCHK(out != NULL);
  658. _ARGCHK(outlen != NULL);
  659. _ARGCHK(key != NULL);
  660. /* can we store the static header? */
  661. if (*outlen < (PACKET_SIZE + 3)) {
  662. return CRYPT_BUFFER_OVERFLOW;
  663. }
  664. /* type valid? */
  665. if (key->type != PK_PRIVATE && type == PK_PRIVATE) {
  666. return CRYPT_PK_TYPE_MISMATCH;
  667. }
  668. /* output type and magic byte */
  669. y = PACKET_SIZE;
  670. out[y++] = (unsigned char)type;
  671. out[y++] = (unsigned char)sets[key->idx].size;
  672. /* output x coordinate */
  673. OUTPUT_BIGNUM(&(key->pubkey.x), out, y, z);
  674. /* compress y and output it */
  675. if ((err = compress_y_point(&key->pubkey, key->idx, &cp)) != CRYPT_OK) {
  676. return err;
  677. }
  678. out[y++] = (unsigned char)cp;
  679. if (type == PK_PRIVATE) {
  680. OUTPUT_BIGNUM(&key->k, out, y, z);
  681. }
  682. /* store header */
  683. packet_store_header(out, PACKET_SECT_ECC, PACKET_SUB_KEY);
  684. *outlen = y;
  685. return CRYPT_OK;
  686. }
  687. int ecc_import(const unsigned char *in, unsigned long inlen, ecc_key *key)
  688. {
  689. unsigned long x, y, s;
  690. int err;
  691. _ARGCHK(in != NULL);
  692. _ARGCHK(key != NULL);
  693. /* check length */
  694. if ((3+PACKET_SIZE) > inlen) {
  695. return CRYPT_INVALID_PACKET;
  696. }
  697. /* check type */
  698. if ((err = packet_valid_header((unsigned char *)in, PACKET_SECT_ECC, PACKET_SUB_KEY)) != CRYPT_OK) {
  699. return err;
  700. }
  701. /* init key */
  702. if (mp_init_multi(&key->pubkey.x, &key->pubkey.y, &key->k, NULL) != MP_OKAY) {
  703. return CRYPT_MEM;
  704. }
  705. y = PACKET_SIZE;
  706. key->type = (int)in[y++];
  707. s = (unsigned long)in[y++];
  708. for (x = 0; (s > (unsigned long)sets[x].size) && (sets[x].size != 0); x++);
  709. if (sets[x].size == 0) {
  710. err = CRYPT_INVALID_KEYSIZE;
  711. goto error;
  712. }
  713. key->idx = (int)x;
  714. /* type check both values */
  715. if ((key->type != PK_PUBLIC) && (key->type != PK_PRIVATE)) {
  716. err = CRYPT_INVALID_PACKET;
  717. goto error;
  718. }
  719. /* is the key idx valid? */
  720. if (is_valid_idx(key->idx) != 1) {
  721. err = CRYPT_INVALID_PACKET;
  722. goto error;
  723. }
  724. /* load x coordinate */
  725. INPUT_BIGNUM(&key->pubkey.x, in, x, y, inlen);
  726. /* load y */
  727. x = (unsigned long)in[y++];
  728. if ((err = expand_y_point(&key->pubkey, key->idx, (int)x)) != CRYPT_OK) {
  729. goto error;
  730. }
  731. if (key->type == PK_PRIVATE) {
  732. /* load private key */
  733. INPUT_BIGNUM(&key->k, in, x, y, inlen);
  734. }
  735. /* eliminate private key if public */
  736. if (key->type == PK_PUBLIC) {
  737. mp_clear(&key->k);
  738. }
  739. return CRYPT_OK;
  740. error:
  741. mp_clear_multi(&key->pubkey.x, &key->pubkey.y, &key->k, NULL);
  742. return err;
  743. }
  744. int ecc_shared_secret(ecc_key *private_key, ecc_key *public_key,
  745. unsigned char *out, unsigned long *outlen)
  746. {
  747. unsigned long x, y;
  748. ecc_point *result;
  749. mp_int prime;
  750. int err;
  751. _ARGCHK(private_key != NULL);
  752. _ARGCHK(public_key != NULL);
  753. _ARGCHK(out != NULL);
  754. _ARGCHK(outlen != NULL);
  755. /* type valid? */
  756. if (private_key->type != PK_PRIVATE) {
  757. return CRYPT_PK_NOT_PRIVATE;
  758. }
  759. if (private_key->idx != public_key->idx) {
  760. return CRYPT_PK_TYPE_MISMATCH;
  761. }
  762. /* make new point */
  763. result = new_point();
  764. if (result == NULL) {
  765. return CRYPT_MEM;
  766. }
  767. if ((err = mp_init(&prime)) != MP_OKAY) {
  768. del_point(result);
  769. return mpi_to_ltc_error(err);
  770. }
  771. if ((err = mp_read_radix(&prime, (char *)sets[private_key->idx].prime, 64)) != MP_OKAY) { goto error; }
  772. if ((err = ecc_mulmod(&private_key->k, &public_key->pubkey, result, &prime)) != CRYPT_OK) { goto done1; }
  773. x = (unsigned long)mp_unsigned_bin_size(&result->x);
  774. y = (unsigned long)mp_unsigned_bin_size(&result->y);
  775. if (*outlen < (x+y)) {
  776. err = CRYPT_BUFFER_OVERFLOW;
  777. goto done1;
  778. }
  779. *outlen = x+y;
  780. if ((err = mp_to_unsigned_bin(&result->x, out)) != MP_OKAY) { goto error; }
  781. if ((err = mp_to_unsigned_bin(&result->y, out+x)) != MP_OKAY) { goto error; }
  782. err = CRYPT_OK;
  783. goto done1;
  784. error:
  785. err = mpi_to_ltc_error(err);
  786. done1:
  787. mp_clear(&prime);
  788. del_point(result);
  789. return err;
  790. }
  791. int ecc_get_size(ecc_key *key)
  792. {
  793. _ARGCHK(key != NULL);
  794. if (is_valid_idx(key->idx))
  795. return sets[key->idx].size;
  796. else
  797. return INT_MAX; /* large value known to cause it to fail when passed to ecc_make_key() */
  798. }
  799. #include "ecc_sys.c"
  800. #endif