PageRenderTime 56ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/ctaocrypt/src/ecc.c

https://github.com/andersmalm/cyassl
C | 1537 lines | 1043 code | 228 blank | 266 comment | 646 complexity | fe067ba0b0cb666b4dd33f69234d94f7 MD5 | raw file
Possible License(s): GPL-2.0
  1. /* ecc.c
  2. *
  3. * Copyright (C) 2006-2012 Sawtooth Consulting Ltd.
  4. *
  5. * This file is part of CyaSSL.
  6. *
  7. * CyaSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * CyaSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. /* in case user set HAVE_ECC there */
  25. #include <cyassl/ctaocrypt/settings.h>
  26. #ifdef HAVE_ECC
  27. #include <cyassl/ctaocrypt/ecc.h>
  28. #include <cyassl/ctaocrypt/asn.h>
  29. #include <cyassl/ctaocrypt/error.h>
  30. /* map
  31. ptmul -> mulmod
  32. */
  33. #define ECC112
  34. #define ECC128
  35. #define ECC160
  36. #define ECC192
  37. #define ECC224
  38. #define ECC256
  39. #define ECC384
  40. #define ECC521
  41. /* This holds the key settings. ***MUST*** be organized by size from
  42. smallest to largest. */
  43. const ecc_set_type ecc_sets[] = {
  44. #ifdef ECC112
  45. {
  46. 14,
  47. "SECP112R1",
  48. "DB7C2ABF62E35E668076BEAD208B",
  49. "659EF8BA043916EEDE8911702B22",
  50. "DB7C2ABF62E35E7628DFAC6561C5",
  51. "09487239995A5EE76B55F9C2F098",
  52. "A89CE5AF8724C0A23E0E0FF77500"
  53. },
  54. #endif
  55. #ifdef ECC128
  56. {
  57. 16,
  58. "SECP128R1",
  59. "FFFFFFFDFFFFFFFFFFFFFFFFFFFFFFFF",
  60. "E87579C11079F43DD824993C2CEE5ED3",
  61. "FFFFFFFE0000000075A30D1B9038A115",
  62. "161FF7528B899B2D0C28607CA52C5B86",
  63. "CF5AC8395BAFEB13C02DA292DDED7A83",
  64. },
  65. #endif
  66. #ifdef ECC160
  67. {
  68. 20,
  69. "SECP160R1",
  70. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFF",
  71. "1C97BEFC54BD7A8B65ACF89F81D4D4ADC565FA45",
  72. "0100000000000000000001F4C8F927AED3CA752257",
  73. "4A96B5688EF573284664698968C38BB913CBFC82",
  74. "23A628553168947D59DCC912042351377AC5FB32",
  75. },
  76. #endif
  77. #ifdef ECC192
  78. {
  79. 24,
  80. "ECC-192",
  81. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF",
  82. "64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1",
  83. "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831",
  84. "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012",
  85. "7192B95FFC8DA78631011ED6B24CDD573F977A11E794811",
  86. },
  87. #endif
  88. #ifdef ECC224
  89. {
  90. 28,
  91. "ECC-224",
  92. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001",
  93. "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4",
  94. "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D",
  95. "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21",
  96. "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34",
  97. },
  98. #endif
  99. #ifdef ECC256
  100. {
  101. 32,
  102. "ECC-256",
  103. "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF",
  104. "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B",
  105. "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551",
  106. "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296",
  107. "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5",
  108. },
  109. #endif
  110. #ifdef ECC384
  111. {
  112. 48,
  113. "ECC-384",
  114. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF",
  115. "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE8141120314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF",
  116. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973",
  117. "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B9859F741E082542A385502F25DBF55296C3A545E3872760AB7",
  118. "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A147CE9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F",
  119. },
  120. #endif
  121. #ifdef ECC521
  122. {
  123. 66,
  124. "ECC-521",
  125. "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  126. "51953EB9618E1C9A1F929A21A0B68540EEA2DA725B99B315F3B8B489918EF109E156193951EC7E937B1652C0BD3BB1BF073573DF883D2C34F1EF451FD46B503F00",
  127. "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148F709A5D03BB5C9B8899C47AEBB6FB71E91386409",
  128. "C6858E06B70404E9CD9E3ECB662395B4429C648139053FB521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127A2FFA8DE3348B3C1856A429BF97E7E31C2E5BD66",
  129. "11839296A789A3BC0045C8A5FB42C7D1BD998F54449579B446817AFBD17273E662C97EE72995EF42640C550B9013FAD0761353C7086A272C24088BE94769FD16650",
  130. },
  131. #endif
  132. {
  133. 0,
  134. NULL, NULL, NULL, NULL, NULL, NULL
  135. }
  136. };
  137. ecc_point* ecc_new_point(void);
  138. void ecc_del_point(ecc_point* p);
  139. int ecc_map(ecc_point*, mp_int*, mp_digit*);
  140. int ecc_projective_add_point(ecc_point* P, ecc_point* Q, ecc_point* R,
  141. mp_int* modulus, mp_digit* mp);
  142. int ecc_projective_dbl_point(ecc_point* P, ecc_point* R, mp_int* modulus,
  143. mp_digit* mp);
  144. /* helper for either lib */
  145. static int get_digit_count(mp_int* a)
  146. {
  147. if (a == NULL)
  148. return 0;
  149. return a->used;
  150. }
  151. /* helper for either lib */
  152. static unsigned long get_digit(mp_int* a, int n)
  153. {
  154. if (a == NULL)
  155. return 0;
  156. return (n >= a->used || n < 0) ? 0 : a->dp[n];
  157. }
  158. /**
  159. Add two ECC points
  160. P The point to add
  161. Q The point to add
  162. R [out] The destination of the double
  163. modulus The modulus of the field the ECC curve is in
  164. mp The "b" value from montgomery_setup()
  165. return MP_OKAY on success
  166. */
  167. int ecc_projective_add_point(ecc_point* P, ecc_point* Q, ecc_point* R,
  168. mp_int* modulus, mp_digit* mp)
  169. {
  170. mp_int t1;
  171. mp_int t2;
  172. mp_int x;
  173. mp_int y;
  174. mp_int z;
  175. int err;
  176. if (P == NULL || Q == NULL || R == NULL || modulus == NULL || mp == NULL)
  177. return ECC_BAD_ARG_E;
  178. if ((err = mp_init_multi(&t1, &t2, &x, &y, &z, NULL)) != MP_OKAY) {
  179. return err;
  180. }
  181. /* should we dbl instead? */
  182. err = mp_sub(modulus, &Q->y, &t1);
  183. if (err == MP_OKAY) {
  184. if ( (mp_cmp(&P->x, &Q->x) == MP_EQ) &&
  185. (get_digit_count(&Q->z) && mp_cmp(&P->z, &Q->z) == MP_EQ) &&
  186. (mp_cmp(&P->y, &Q->y) == MP_EQ || mp_cmp(&P->y, &t1) == MP_EQ)) {
  187. mp_clear(&t1);
  188. mp_clear(&t2);
  189. mp_clear(&x);
  190. mp_clear(&y);
  191. mp_clear(&z);
  192. return ecc_projective_dbl_point(P, R, modulus, mp);
  193. }
  194. }
  195. if (err == MP_OKAY)
  196. err = mp_copy(&P->x, &x);
  197. if (err == MP_OKAY)
  198. err = mp_copy(&P->y, &y);
  199. if (err == MP_OKAY)
  200. err = mp_copy(&P->z, &z);
  201. /* if Z is one then these are no-operations */
  202. if (err == MP_OKAY) {
  203. if (get_digit_count(&Q->z)) {
  204. /* T1 = Z' * Z' */
  205. err = mp_sqr(&Q->z, &t1);
  206. if (err == MP_OKAY)
  207. err = mp_montgomery_reduce(&t1, modulus, *mp);
  208. /* X = X * T1 */
  209. if (err == MP_OKAY)
  210. err = mp_mul(&t1, &x, &x);
  211. if (err == MP_OKAY)
  212. err = mp_montgomery_reduce(&x, modulus, *mp);
  213. /* T1 = Z' * T1 */
  214. if (err == MP_OKAY)
  215. err = mp_mul(&Q->z, &t1, &t1);
  216. if (err == MP_OKAY)
  217. err = mp_montgomery_reduce(&t1, modulus, *mp);
  218. /* Y = Y * T1 */
  219. if (err == MP_OKAY)
  220. err = mp_mul(&t1, &y, &y);
  221. if (err == MP_OKAY)
  222. err = mp_montgomery_reduce(&y, modulus, *mp);
  223. }
  224. }
  225. /* T1 = Z*Z */
  226. if (err == MP_OKAY)
  227. err = mp_sqr(&z, &t1);
  228. if (err == MP_OKAY)
  229. err = mp_montgomery_reduce(&t1, modulus, *mp);
  230. /* T2 = X' * T1 */
  231. if (err == MP_OKAY)
  232. err = mp_mul(&Q->x, &t1, &t2);
  233. if (err == MP_OKAY)
  234. err = mp_montgomery_reduce(&t2, modulus, *mp);
  235. /* T1 = Z * T1 */
  236. if (err == MP_OKAY)
  237. err = mp_mul(&z, &t1, &t1);
  238. if (err == MP_OKAY)
  239. err = mp_montgomery_reduce(&t1, modulus, *mp);
  240. /* T1 = Y' * T1 */
  241. if (err == MP_OKAY)
  242. err = mp_mul(&Q->y, &t1, &t1);
  243. if (err == MP_OKAY)
  244. err = mp_montgomery_reduce(&t1, modulus, *mp);
  245. /* Y = Y - T1 */
  246. if (err == MP_OKAY)
  247. err = mp_sub(&y, &t1, &y);
  248. if (err == MP_OKAY) {
  249. if (mp_cmp_d(&y, 0) == MP_LT)
  250. err = mp_add(&y, modulus, &y);
  251. }
  252. /* T1 = 2T1 */
  253. if (err == MP_OKAY)
  254. err = mp_add(&t1, &t1, &t1);
  255. if (err == MP_OKAY) {
  256. if (mp_cmp(&t1, modulus) != MP_LT)
  257. err = mp_sub(&t1, modulus, &t1);
  258. }
  259. /* T1 = Y + T1 */
  260. if (err == MP_OKAY)
  261. err = mp_add(&t1, &y, &t1);
  262. if (err == MP_OKAY) {
  263. if (mp_cmp(&t1, modulus) != MP_LT)
  264. err = mp_sub(&t1, modulus, &t1);
  265. }
  266. /* X = X - T2 */
  267. if (err == MP_OKAY)
  268. err = mp_sub(&x, &t2, &x);
  269. if (err == MP_OKAY) {
  270. if (mp_cmp_d(&x, 0) == MP_LT)
  271. err = mp_add(&x, modulus, &x);
  272. }
  273. /* T2 = 2T2 */
  274. if (err == MP_OKAY)
  275. err = mp_add(&t2, &t2, &t2);
  276. if (err == MP_OKAY) {
  277. if (mp_cmp(&t2, modulus) != MP_LT)
  278. err = mp_sub(&t2, modulus, &t2);
  279. }
  280. /* T2 = X + T2 */
  281. if (err == MP_OKAY)
  282. err = mp_add(&t2, &x, &t2);
  283. if (err == MP_OKAY) {
  284. if (mp_cmp(&t2, modulus) != MP_LT)
  285. err = mp_sub(&t2, modulus, &t2);
  286. }
  287. if (err == MP_OKAY) {
  288. if (get_digit_count(&Q->z)) {
  289. /* Z = Z * Z' */
  290. err = mp_mul(&z, &Q->z, &z);
  291. if (err == MP_OKAY)
  292. err = mp_montgomery_reduce(&z, modulus, *mp);
  293. }
  294. }
  295. /* Z = Z * X */
  296. if (err == MP_OKAY)
  297. err = mp_mul(&z, &x, &z);
  298. if (err == MP_OKAY)
  299. err = mp_montgomery_reduce(&z, modulus, *mp);
  300. /* T1 = T1 * X */
  301. if (err == MP_OKAY)
  302. err = mp_mul(&t1, &x, &t1);
  303. if (err == MP_OKAY)
  304. err = mp_montgomery_reduce(&t1, modulus, *mp);
  305. /* X = X * X */
  306. if (err == MP_OKAY)
  307. err = mp_sqr(&x, &x);
  308. if (err == MP_OKAY)
  309. err = mp_montgomery_reduce(&x, modulus, *mp);
  310. /* T2 = T2 * x */
  311. if (err == MP_OKAY)
  312. err = mp_mul(&t2, &x, &t2);
  313. if (err == MP_OKAY)
  314. err = mp_montgomery_reduce(&t2, modulus, *mp);
  315. /* T1 = T1 * X */
  316. if (err == MP_OKAY)
  317. err = mp_mul(&t1, &x, &t1);
  318. if (err == MP_OKAY)
  319. err = mp_montgomery_reduce(&t1, modulus, *mp);
  320. /* X = Y*Y */
  321. if (err == MP_OKAY)
  322. err = mp_sqr(&y, &x);
  323. if (err == MP_OKAY)
  324. err = mp_montgomery_reduce(&x, modulus, *mp);
  325. /* X = X - T2 */
  326. if (err == MP_OKAY)
  327. err = mp_sub(&x, &t2, &x);
  328. if (err == MP_OKAY) {
  329. if (mp_cmp_d(&x, 0) == MP_LT)
  330. err = mp_add(&x, modulus, &x);
  331. }
  332. /* T2 = T2 - X */
  333. if (err == MP_OKAY)
  334. err = mp_sub(&t2, &x, &t2);
  335. if (err == MP_OKAY) {
  336. if (mp_cmp_d(&t2, 0) == MP_LT)
  337. err = mp_add(&t2, modulus, &t2);
  338. }
  339. /* T2 = T2 - X */
  340. if (err == MP_OKAY)
  341. err = mp_sub(&t2, &x, &t2);
  342. if (err == MP_OKAY) {
  343. if (mp_cmp_d(&t2, 0) == MP_LT)
  344. err = mp_add(&t2, modulus, &t2);
  345. }
  346. /* T2 = T2 * Y */
  347. if (err == MP_OKAY)
  348. err = mp_mul(&t2, &y, &t2);
  349. if (err == MP_OKAY)
  350. err = mp_montgomery_reduce(&t2, modulus, *mp);
  351. /* Y = T2 - T1 */
  352. if (err == MP_OKAY)
  353. err = mp_sub(&t2, &t1, &y);
  354. if (err == MP_OKAY) {
  355. if (mp_cmp_d(&y, 0) == MP_LT)
  356. err = mp_add(&y, modulus, &y);
  357. }
  358. /* Y = Y/2 */
  359. if (err == MP_OKAY) {
  360. if (mp_isodd(&y))
  361. err = mp_add(&y, modulus, &y);
  362. }
  363. if (err == MP_OKAY)
  364. err = mp_div_2(&y, &y);
  365. if (err == MP_OKAY)
  366. err = mp_copy(&x, &R->x);
  367. if (err == MP_OKAY)
  368. err = mp_copy(&y, &R->y);
  369. if (err == MP_OKAY)
  370. err = mp_copy(&z, &R->z);
  371. /* clean up */
  372. mp_clear(&t1);
  373. mp_clear(&t2);
  374. mp_clear(&x);
  375. mp_clear(&y);
  376. mp_clear(&z);
  377. return err;
  378. }
  379. /**
  380. Double an ECC point
  381. P The point to double
  382. R [out] The destination of the double
  383. modulus The modulus of the field the ECC curve is in
  384. mp The "b" value from montgomery_setup()
  385. return MP_OKAY on success
  386. */
  387. int ecc_projective_dbl_point(ecc_point *P, ecc_point *R, mp_int* modulus,
  388. mp_digit* mp)
  389. {
  390. mp_int t1;
  391. mp_int t2;
  392. int err;
  393. if (P == NULL || R == NULL || modulus == NULL || mp == NULL)
  394. return ECC_BAD_ARG_E;
  395. if ((err = mp_init_multi(&t1, &t2, NULL, NULL, NULL, NULL)) != MP_OKAY) {
  396. return err;
  397. }
  398. if (P != R) {
  399. err = mp_copy(&P->x, &R->x);
  400. if (err == MP_OKAY)
  401. err = mp_copy(&P->y, &R->y);
  402. if (err == MP_OKAY)
  403. err = mp_copy(&P->z, &R->z);
  404. }
  405. /* t1 = Z * Z */
  406. if (err == MP_OKAY)
  407. err = mp_sqr(&R->z, &t1);
  408. if (err == MP_OKAY)
  409. err = mp_montgomery_reduce(&t1, modulus, *mp);
  410. /* Z = Y * Z */
  411. if (err == MP_OKAY)
  412. err = mp_mul(&R->z, &R->y, &R->z);
  413. if (err == MP_OKAY)
  414. err = mp_montgomery_reduce(&R->z, modulus, *mp);
  415. /* Z = 2Z */
  416. if (err == MP_OKAY)
  417. err = mp_add(&R->z, &R->z, &R->z);
  418. if (err == MP_OKAY) {
  419. if (mp_cmp(&R->z, modulus) != MP_LT)
  420. err = mp_sub(&R->z, modulus, &R->z);
  421. }
  422. /* T2 = X - T1 */
  423. if (err == MP_OKAY)
  424. err = mp_sub(&R->x, &t1, &t2);
  425. if (err == MP_OKAY) {
  426. if (mp_cmp_d(&t2, 0) == MP_LT)
  427. err = mp_add(&t2, modulus, &t2);
  428. }
  429. /* T1 = X + T1 */
  430. if (err == MP_OKAY)
  431. err = mp_add(&t1, &R->x, &t1);
  432. if (err == MP_OKAY) {
  433. if (mp_cmp(&t1, modulus) != MP_LT)
  434. err = mp_sub(&t1, modulus, &t1);
  435. }
  436. /* T2 = T1 * T2 */
  437. if (err == MP_OKAY)
  438. err = mp_mul(&t1, &t2, &t2);
  439. if (err == MP_OKAY)
  440. err = mp_montgomery_reduce(&t2, modulus, *mp);
  441. /* T1 = 2T2 */
  442. if (err == MP_OKAY)
  443. err = mp_add(&t2, &t2, &t1);
  444. if (err == MP_OKAY) {
  445. if (mp_cmp(&t1, modulus) != MP_LT)
  446. err = mp_sub(&t1, modulus, &t1);
  447. }
  448. /* T1 = T1 + T2 */
  449. if (err == MP_OKAY)
  450. err = mp_add(&t1, &t2, &t1);
  451. if (err == MP_OKAY) {
  452. if (mp_cmp(&t1, modulus) != MP_LT)
  453. err = mp_sub(&t1, modulus, &t1);
  454. }
  455. /* Y = 2Y */
  456. if (err == MP_OKAY)
  457. err = mp_add(&R->y, &R->y, &R->y);
  458. if (err == MP_OKAY) {
  459. if (mp_cmp(&R->y, modulus) != MP_LT)
  460. err = mp_sub(&R->y, modulus, &R->y);
  461. }
  462. /* Y = Y * Y */
  463. if (err == MP_OKAY)
  464. err = mp_sqr(&R->y, &R->y);
  465. if (err == MP_OKAY)
  466. err = mp_montgomery_reduce(&R->y, modulus, *mp);
  467. /* T2 = Y * Y */
  468. if (err == MP_OKAY)
  469. err = mp_sqr(&R->y, &t2);
  470. if (err == MP_OKAY)
  471. err = mp_montgomery_reduce(&t2, modulus, *mp);
  472. /* T2 = T2/2 */
  473. if (err == MP_OKAY) {
  474. if (mp_isodd(&t2))
  475. err = mp_add(&t2, modulus, &t2);
  476. }
  477. if (err == MP_OKAY)
  478. err = mp_div_2(&t2, &t2);
  479. /* Y = Y * X */
  480. if (err == MP_OKAY)
  481. err = mp_mul(&R->y, &R->x, &R->y);
  482. if (err == MP_OKAY)
  483. err = mp_montgomery_reduce(&R->y, modulus, *mp);
  484. /* X = T1 * T1 */
  485. if (err == MP_OKAY)
  486. err = mp_sqr(&t1, &R->x);
  487. if (err == MP_OKAY)
  488. err = mp_montgomery_reduce(&R->x, modulus, *mp);
  489. /* X = X - Y */
  490. if (err == MP_OKAY)
  491. err = mp_sub(&R->x, &R->y, &R->x);
  492. if (err == MP_OKAY) {
  493. if (mp_cmp_d(&R->x, 0) == MP_LT)
  494. err = mp_add(&R->x, modulus, &R->x);
  495. }
  496. /* X = X - Y */
  497. if (err == MP_OKAY)
  498. err = mp_sub(&R->x, &R->y, &R->x);
  499. if (err == MP_OKAY) {
  500. if (mp_cmp_d(&R->x, 0) == MP_LT)
  501. err = mp_add(&R->x, modulus, &R->x);
  502. }
  503. /* Y = Y - X */
  504. if (err == MP_OKAY)
  505. err = mp_sub(&R->y, &R->x, &R->y);
  506. if (err == MP_OKAY) {
  507. if (mp_cmp_d(&R->y, 0) == MP_LT)
  508. err = mp_add(&R->y, modulus, &R->y);
  509. }
  510. /* Y = Y * T1 */
  511. if (err == MP_OKAY)
  512. err = mp_mul(&R->y, &t1, &R->y);
  513. if (err == MP_OKAY)
  514. err = mp_montgomery_reduce(&R->y, modulus, *mp);
  515. /* Y = Y - T2 */
  516. if (err == MP_OKAY)
  517. err = mp_sub(&R->y, &t2, &R->y);
  518. if (err == MP_OKAY) {
  519. if (mp_cmp_d(&R->y, 0) == MP_LT)
  520. err = mp_add(&R->y, modulus, &R->y);
  521. }
  522. /* clean up */
  523. mp_clear(&t1);
  524. mp_clear(&t2);
  525. return err;
  526. }
  527. /**
  528. Map a projective jacbobian point back to affine space
  529. P [in/out] The point to map
  530. modulus The modulus of the field the ECC curve is in
  531. mp The "b" value from montgomery_setup()
  532. return MP_OKAY on success
  533. */
  534. int ecc_map(ecc_point* P, mp_int* modulus, mp_digit* mp)
  535. {
  536. mp_int t1;
  537. mp_int t2;
  538. int err;
  539. if (P == NULL || mp == NULL || modulus == NULL)
  540. return ECC_BAD_ARG_E;
  541. if ((err = mp_init_multi(&t1, &t2, NULL, NULL, NULL, NULL)) != MP_OKAY) {
  542. return MEMORY_E;
  543. }
  544. /* first map z back to normal */
  545. err = mp_montgomery_reduce(&P->z, modulus, *mp);
  546. /* get 1/z */
  547. if (err == MP_OKAY)
  548. err = mp_invmod(&P->z, modulus, &t1);
  549. /* get 1/z^2 and 1/z^3 */
  550. if (err == MP_OKAY)
  551. err = mp_sqr(&t1, &t2);
  552. if (err == MP_OKAY)
  553. err = mp_mod(&t2, modulus, &t2);
  554. if (err == MP_OKAY)
  555. err = mp_mul(&t1, &t2, &t1);
  556. if (err == MP_OKAY)
  557. err = mp_mod(&t1, modulus, &t1);
  558. /* multiply against x/y */
  559. if (err == MP_OKAY)
  560. err = mp_mul(&P->x, &t2, &P->x);
  561. if (err == MP_OKAY)
  562. err = mp_montgomery_reduce(&P->x, modulus, *mp);
  563. if (err == MP_OKAY)
  564. err = mp_mul(&P->y, &t1, &P->y);
  565. if (err == MP_OKAY)
  566. err = mp_montgomery_reduce(&P->y, modulus, *mp);
  567. if (err == MP_OKAY)
  568. mp_set(&P->z, 1);
  569. /* clean up */
  570. mp_clear(&t1);
  571. mp_clear(&t2);
  572. return err;
  573. }
  574. #ifndef ECC_TIMING_RESISTANT
  575. /* size of sliding window, don't change this! */
  576. #define WINSIZE 4
  577. /**
  578. Perform a point multiplication
  579. k The scalar to multiply by
  580. G The base point
  581. R [out] Destination for kG
  582. modulus The modulus of the field the ECC curve is in
  583. map Boolean whether to map back to affine or not
  584. (1==map, 0 == leave in projective)
  585. return MP_OKAY on success
  586. */
  587. static int ecc_mulmod(mp_int* k, ecc_point *G, ecc_point *R, mp_int* modulus,
  588. int map)
  589. {
  590. ecc_point *tG, *M[8];
  591. int i, j, err;
  592. mp_int mu;
  593. mp_digit mp;
  594. unsigned long buf;
  595. int first, bitbuf, bitcpy, bitcnt, mode, digidx;
  596. if (k == NULL || G == NULL || R == NULL || modulus == NULL)
  597. return ECC_BAD_ARG_E;
  598. /* init montgomery reduction */
  599. if ((err = mp_montgomery_setup(modulus, &mp)) != MP_OKAY) {
  600. return err;
  601. }
  602. if ((err = mp_init(&mu)) != MP_OKAY) {
  603. return err;
  604. }
  605. if ((err = mp_montgomery_calc_normalization(&mu, modulus)) != MP_OKAY) {
  606. mp_clear(&mu);
  607. return err;
  608. }
  609. /* alloc ram for window temps */
  610. for (i = 0; i < 8; i++) {
  611. M[i] = ecc_new_point();
  612. if (M[i] == NULL) {
  613. for (j = 0; j < i; j++) {
  614. ecc_del_point(M[j]);
  615. }
  616. mp_clear(&mu);
  617. return MEMORY_E;
  618. }
  619. }
  620. /* make a copy of G incase R==G */
  621. tG = ecc_new_point();
  622. if (tG == NULL)
  623. err = MEMORY_E;
  624. /* tG = G and convert to montgomery */
  625. if (err == MP_OKAY) {
  626. if (mp_cmp_d(&mu, 1) == MP_EQ) {
  627. err = mp_copy(&G->x, &tG->x);
  628. if (err == MP_OKAY)
  629. err = mp_copy(&G->y, &tG->y);
  630. if (err == MP_OKAY)
  631. err = mp_copy(&G->z, &tG->z);
  632. } else {
  633. err = mp_mulmod(&G->x, &mu, modulus, &tG->x);
  634. if (err == MP_OKAY)
  635. err = mp_mulmod(&G->y, &mu, modulus, &tG->y);
  636. if (err == MP_OKAY)
  637. err = mp_mulmod(&G->z, &mu, modulus, &tG->z);
  638. }
  639. }
  640. mp_clear(&mu);
  641. /* calc the M tab, which holds kG for k==8..15 */
  642. /* M[0] == 8G */
  643. if (err == MP_OKAY)
  644. err = ecc_projective_dbl_point(tG, M[0], modulus, &mp);
  645. if (err == MP_OKAY)
  646. err = ecc_projective_dbl_point(M[0], M[0], modulus, &mp);
  647. if (err == MP_OKAY)
  648. err = ecc_projective_dbl_point(M[0], M[0], modulus, &mp);
  649. /* now find (8+k)G for k=1..7 */
  650. if (err == MP_OKAY)
  651. for (j = 9; j < 16; j++) {
  652. err = ecc_projective_add_point(M[j-9], tG, M[j-8], modulus, &mp);
  653. if (err != MP_OKAY) break;
  654. }
  655. /* setup sliding window */
  656. if (err == MP_OKAY) {
  657. mode = 0;
  658. bitcnt = 1;
  659. buf = 0;
  660. digidx = get_digit_count(k) - 1;
  661. bitcpy = bitbuf = 0;
  662. first = 1;
  663. /* perform ops */
  664. for (;;) {
  665. /* grab next digit as required */
  666. if (--bitcnt == 0) {
  667. if (digidx == -1) {
  668. break;
  669. }
  670. buf = get_digit(k, digidx);
  671. bitcnt = (int) DIGIT_BIT;
  672. --digidx;
  673. }
  674. /* grab the next msb from the ltiplicand */
  675. i = (int)(buf >> (DIGIT_BIT - 1)) & 1;
  676. buf <<= 1;
  677. /* skip leading zero bits */
  678. if (mode == 0 && i == 0)
  679. continue;
  680. /* if the bit is zero and mode == 1 then we double */
  681. if (mode == 1 && i == 0) {
  682. err = ecc_projective_dbl_point(R, R, modulus, &mp);
  683. if (err != MP_OKAY) break;
  684. continue;
  685. }
  686. /* else we add it to the window */
  687. bitbuf |= (i << (WINSIZE - ++bitcpy));
  688. mode = 2;
  689. if (bitcpy == WINSIZE) {
  690. /* if this is the first window we do a simple copy */
  691. if (first == 1) {
  692. /* R = kG [k = first window] */
  693. err = mp_copy(&M[bitbuf-8]->x, &R->x);
  694. if (err != MP_OKAY) break;
  695. err = mp_copy(&M[bitbuf-8]->y, &R->y);
  696. if (err != MP_OKAY) break;
  697. err = mp_copy(&M[bitbuf-8]->z, &R->z);
  698. first = 0;
  699. } else {
  700. /* normal window */
  701. /* ok window is filled so double as required and add */
  702. /* double first */
  703. for (j = 0; j < WINSIZE; j++) {
  704. err = ecc_projective_dbl_point(R, R, modulus, &mp);
  705. if (err != MP_OKAY) break;
  706. }
  707. if (err != MP_OKAY) break; /* out of first for(;;) */
  708. /* then add, bitbuf will be 8..15 [8..2^WINSIZE] guaranted */
  709. err = ecc_projective_add_point(R,M[bitbuf-8],R,modulus,&mp);
  710. }
  711. if (err != MP_OKAY) break;
  712. /* empty window and reset */
  713. bitcpy = bitbuf = 0;
  714. mode = 1;
  715. }
  716. }
  717. }
  718. /* if bits remain then double/add */
  719. if (err == MP_OKAY) {
  720. if (mode == 2 && bitcpy > 0) {
  721. /* double then add */
  722. for (j = 0; j < bitcpy; j++) {
  723. /* only double if we have had at least one add first */
  724. if (first == 0) {
  725. err = ecc_projective_dbl_point(R, R, modulus, &mp);
  726. if (err != MP_OKAY) break;
  727. }
  728. bitbuf <<= 1;
  729. if ((bitbuf & (1 << WINSIZE)) != 0) {
  730. if (first == 1) {
  731. /* first add, so copy */
  732. err = mp_copy(&tG->x, &R->x);
  733. if (err != MP_OKAY) break;
  734. err = mp_copy(&tG->y, &R->y);
  735. if (err != MP_OKAY) break;
  736. err = mp_copy(&tG->z, &R->z);
  737. if (err != MP_OKAY) break;
  738. first = 0;
  739. } else {
  740. /* then add */
  741. err = ecc_projective_add_point(R, tG, R, modulus, &mp);
  742. if (err != MP_OKAY) break;
  743. }
  744. }
  745. }
  746. }
  747. }
  748. /* map R back from projective space */
  749. if (err == MP_OKAY && map)
  750. err = ecc_map(R, modulus, &mp);
  751. mp_clear(&mu);
  752. ecc_del_point(tG);
  753. for (i = 0; i < 8; i++) {
  754. ecc_del_point(M[i]);
  755. }
  756. return err;
  757. }
  758. #undef WINSIZE
  759. #endif /* ECC_TIMING_RESISTANT */
  760. /**
  761. Allocate a new ECC point
  762. return A newly allocated point or NULL on error
  763. */
  764. ecc_point* ecc_new_point(void)
  765. {
  766. ecc_point* p;
  767. p = (ecc_point*)XMALLOC(sizeof(ecc_point), 0, DYNAMIC_TYPE_BIGINT);
  768. if (p == NULL) {
  769. return NULL;
  770. }
  771. XMEMSET(p, 0, sizeof(ecc_point));
  772. if (mp_init_multi(&p->x, &p->y, &p->z, NULL, NULL, NULL) != MP_OKAY) {
  773. XFREE(p, 0, DYNAMIC_TYPE_BIGINT);
  774. return NULL;
  775. }
  776. return p;
  777. }
  778. /** Free an ECC point from memory
  779. p The point to free
  780. */
  781. void ecc_del_point(ecc_point* p)
  782. {
  783. /* prevents free'ing null arguments */
  784. if (p != NULL) {
  785. mp_clear(&p->x);
  786. mp_clear(&p->y);
  787. mp_clear(&p->z);
  788. XFREE(p, 0, DYNAMIC_TYPE_BIGINT);
  789. }
  790. }
  791. /** Returns whether an ECC idx is valid or not
  792. n The idx number to check
  793. return 1 if valid, 0 if not
  794. */
  795. static int ecc_is_valid_idx(int n)
  796. {
  797. int x;
  798. for (x = 0; ecc_sets[x].size != 0; x++)
  799. ;
  800. /* -1 is a valid index --- indicating that the domain params
  801. were supplied by the user */
  802. if ((n >= -1) && (n < x)) {
  803. return 1;
  804. }
  805. return 0;
  806. }
  807. /**
  808. Create an ECC shared secret between two keys
  809. private_key The private ECC key
  810. public_key The public key
  811. out [out] Destination of the shared secret
  812. Conforms to EC-DH from ANSI X9.63
  813. outlen [in/out] The max size and resulting size of the shared secret
  814. return MP_OKAY if successful
  815. */
  816. int ecc_shared_secret(ecc_key* private_key, ecc_key* public_key, byte* out,
  817. word32* outlen)
  818. {
  819. word32 x = 0;
  820. ecc_point* result;
  821. mp_int prime;
  822. int err;
  823. if (private_key == NULL || public_key == NULL || out == NULL ||
  824. outlen == NULL)
  825. return BAD_FUNC_ARG;
  826. /* type valid? */
  827. if (private_key->type != ECC_PRIVATEKEY) {
  828. return ECC_BAD_ARG_E;
  829. }
  830. if (ecc_is_valid_idx(private_key->idx) == 0 ||
  831. ecc_is_valid_idx(public_key->idx) == 0)
  832. return ECC_BAD_ARG_E;
  833. if (XSTRNCMP(private_key->dp->name, public_key->dp->name, ECC_MAXNAME) != 0)
  834. return ECC_BAD_ARG_E;
  835. /* make new point */
  836. result = ecc_new_point();
  837. if (result == NULL) {
  838. return MEMORY_E;
  839. }
  840. if ((err = mp_init(&prime)) != MP_OKAY) {
  841. ecc_del_point(result);
  842. return err;
  843. }
  844. err = mp_read_radix(&prime, (char *)private_key->dp->prime, 16);
  845. if (err == MP_OKAY)
  846. err = ecc_mulmod(&private_key->k, &public_key->pubkey, result, &prime,1);
  847. if (err == MP_OKAY) {
  848. x = mp_unsigned_bin_size(&prime);
  849. if (*outlen < x)
  850. err = BUFFER_E;
  851. }
  852. if (err == MP_OKAY) {
  853. XMEMSET(out, 0, x);
  854. err = mp_to_unsigned_bin(&result->x,out + (x -
  855. mp_unsigned_bin_size(&result->x)));
  856. *outlen = x;
  857. }
  858. mp_clear(&prime);
  859. ecc_del_point(result);
  860. return err;
  861. }
  862. int ecc_make_key_ex(RNG* rng, ecc_key* key, const ecc_set_type* dp);
  863. /**
  864. Make a new ECC key
  865. rng An active RNG state
  866. keysize The keysize for the new key (in octets from 20 to 65 bytes)
  867. key [out] Destination of the newly created key
  868. return MP_OKAY if successful,
  869. upon error all allocated memory will be freed
  870. */
  871. int ecc_make_key(RNG* rng, int keysize, ecc_key* key)
  872. {
  873. int x, err;
  874. /* find key size */
  875. for (x = 0; (keysize > ecc_sets[x].size) && (ecc_sets[x].size != 0); x++)
  876. ;
  877. keysize = ecc_sets[x].size;
  878. if (keysize > ECC_MAXSIZE || ecc_sets[x].size == 0) {
  879. return BAD_FUNC_ARG;
  880. }
  881. err = ecc_make_key_ex(rng, key, &ecc_sets[x]);
  882. key->idx = x;
  883. return err;
  884. }
  885. int ecc_make_key_ex(RNG* rng, ecc_key* key, const ecc_set_type* dp)
  886. {
  887. int err;
  888. ecc_point* base;
  889. mp_int prime;
  890. mp_int order;
  891. byte buf[ECC_MAXSIZE];
  892. int keysize;
  893. if (key == NULL || rng == NULL || dp == NULL)
  894. return ECC_BAD_ARG_E;
  895. key->idx = -1;
  896. key->dp = dp;
  897. keysize = dp->size;
  898. /* allocate ram */
  899. base = NULL;
  900. /* make up random string */
  901. RNG_GenerateBlock(rng, buf, keysize);
  902. buf[0] |= 0x0c;
  903. /* setup the key variables */
  904. if ((err = mp_init_multi(&key->pubkey.x, &key->pubkey.y, &key->pubkey.z,
  905. &key->k, &prime, &order)) != MP_OKAY)
  906. return MEMORY_E;
  907. base = ecc_new_point();
  908. if (base == NULL)
  909. err = MEMORY_E;
  910. /* read in the specs for this key */
  911. if (err == MP_OKAY)
  912. err = mp_read_radix(&prime, (char *)key->dp->prime, 16);
  913. if (err == MP_OKAY)
  914. err = mp_read_radix(&order, (char *)key->dp->order, 16);
  915. if (err == MP_OKAY)
  916. err = mp_read_radix(&base->x, (char *)key->dp->Gx, 16);
  917. if (err == MP_OKAY)
  918. err = mp_read_radix(&base->y, (char *)key->dp->Gy, 16);
  919. if (err == MP_OKAY)
  920. mp_set(&base->z, 1);
  921. if (err == MP_OKAY)
  922. err = mp_read_unsigned_bin(&key->k, (byte*)buf, keysize);
  923. /* the key should be smaller than the order of base point */
  924. if (err == MP_OKAY) {
  925. if (mp_cmp(&key->k, &order) != MP_LT)
  926. err = mp_mod(&key->k, &order, &key->k);
  927. }
  928. /* make the public key */
  929. if (err == MP_OKAY)
  930. err = ecc_mulmod(&key->k, base, &key->pubkey, &prime, 1);
  931. if (err == MP_OKAY)
  932. key->type = ECC_PRIVATEKEY;
  933. if (err != MP_OKAY) {
  934. /* clean up */
  935. mp_clear(&key->pubkey.x);
  936. mp_clear(&key->pubkey.y);
  937. mp_clear(&key->pubkey.z);
  938. mp_clear(&key->k);
  939. }
  940. ecc_del_point(base);
  941. mp_clear(&prime);
  942. mp_clear(&order);
  943. #ifdef ECC_CLEAN_STACK
  944. XMEMSET(buff, 0, ECC_MAXSIZE);
  945. #endif
  946. return err;
  947. }
  948. /* Setup dynamic pointers is using normal math for proper freeing */
  949. void ecc_init(ecc_key* key)
  950. {
  951. (void)key;
  952. #ifndef USE_FAST_MATH
  953. key->pubkey.x.dp = NULL;
  954. key->pubkey.y.dp = NULL;
  955. key->pubkey.z.dp = NULL;
  956. key->k.dp = NULL;
  957. #endif
  958. }
  959. /**
  960. Sign a message digest
  961. in The message digest to sign
  962. inlen The length of the digest
  963. out [out] The destination for the signature
  964. outlen [in/out] The max size and resulting size of the signature
  965. key A private ECC key
  966. return MP_OKAY if successful
  967. */
  968. int ecc_sign_hash(const byte* in, word32 inlen, byte* out, word32 *outlen,
  969. RNG* rng, ecc_key* key)
  970. {
  971. mp_int r;
  972. mp_int s;
  973. mp_int e;
  974. mp_int p;
  975. int err;
  976. if (in == NULL || out == NULL || outlen == NULL || key == NULL || rng ==NULL)
  977. return ECC_BAD_ARG_E;
  978. /* is this a private key? */
  979. if (key->type != ECC_PRIVATEKEY) {
  980. return ECC_BAD_ARG_E;
  981. }
  982. /* is the IDX valid ? */
  983. if (ecc_is_valid_idx(key->idx) != 1) {
  984. return ECC_BAD_ARG_E;
  985. }
  986. /* get the hash and load it as a bignum into 'e' */
  987. /* init the bignums */
  988. if ((err = mp_init_multi(&r, &s, &p, &e, NULL, NULL)) != MP_OKAY) {
  989. return err;
  990. }
  991. err = mp_read_radix(&p, (char *)key->dp->order, 16);
  992. if (err == MP_OKAY) {
  993. int truncLen = (int)inlen;
  994. if (truncLen > ecc_size(key))
  995. truncLen = ecc_size(key);
  996. err = mp_read_unsigned_bin(&e, (byte*)in, truncLen);
  997. }
  998. /* make up a key and export the public copy */
  999. if (err == MP_OKAY) {
  1000. ecc_key pubkey;
  1001. ecc_init(&pubkey);
  1002. for (;;) {
  1003. err = ecc_make_key_ex(rng, &pubkey, key->dp);
  1004. if (err != MP_OKAY) break;
  1005. /* find r = x1 mod n */
  1006. err = mp_mod(&pubkey.pubkey.x, &p, &r);
  1007. if (err != MP_OKAY) break;
  1008. if (mp_iszero(&r) == MP_YES)
  1009. ecc_free(&pubkey);
  1010. else {
  1011. /* find s = (e + xr)/k */
  1012. err = mp_invmod(&pubkey.k, &p, &pubkey.k);
  1013. if (err != MP_OKAY) break;
  1014. err = mp_mulmod(&key->k, &r, &p, &s); /* s = xr */
  1015. if (err != MP_OKAY) break;
  1016. err = mp_add(&e, &s, &s); /* s = e + xr */
  1017. if (err != MP_OKAY) break;
  1018. err = mp_mod(&s, &p, &s); /* s = e + xr */
  1019. if (err != MP_OKAY) break;
  1020. err = mp_mulmod(&s, &pubkey.k, &p, &s); /* s = (e + xr)/k */
  1021. if (err != MP_OKAY) break;
  1022. ecc_free(&pubkey);
  1023. if (mp_iszero(&s) == MP_NO)
  1024. break;
  1025. }
  1026. }
  1027. ecc_free(&pubkey);
  1028. }
  1029. /* store as SEQUENCE { r, s -- integer } */
  1030. if (err == MP_OKAY)
  1031. err = StoreECC_DSA_Sig(out, outlen, &r, &s);
  1032. mp_clear(&r);
  1033. mp_clear(&s);
  1034. mp_clear(&p);
  1035. mp_clear(&e);
  1036. return err;
  1037. }
  1038. /**
  1039. Free an ECC key from memory
  1040. key The key you wish to free
  1041. */
  1042. void ecc_free(ecc_key* key)
  1043. {
  1044. if (key == NULL)
  1045. return;
  1046. mp_clear(&key->pubkey.x);
  1047. mp_clear(&key->pubkey.y);
  1048. mp_clear(&key->pubkey.z);
  1049. mp_clear(&key->k);
  1050. }
  1051. /* verify
  1052. *
  1053. * w = s^-1 mod n
  1054. * u1 = xw
  1055. * u2 = rw
  1056. * X = u1*G + u2*Q
  1057. * v = X_x1 mod n
  1058. * accept if v == r
  1059. */
  1060. /**
  1061. Verify an ECC signature
  1062. sig The signature to verify
  1063. siglen The length of the signature (octets)
  1064. hash The hash (message digest) that was signed
  1065. hashlen The length of the hash (octets)
  1066. stat Result of signature, 1==valid, 0==invalid
  1067. key The corresponding public ECC key
  1068. return MP_OKAY if successful (even if the signature is not valid)
  1069. */
  1070. int ecc_verify_hash(const byte* sig, word32 siglen, byte* hash, word32 hashlen,
  1071. int* stat, ecc_key* key)
  1072. {
  1073. ecc_point *mG, *mQ;
  1074. mp_int r;
  1075. mp_int s;
  1076. mp_int v;
  1077. mp_int w;
  1078. mp_int u1;
  1079. mp_int u2;
  1080. mp_int e;
  1081. mp_int p;
  1082. mp_int m;
  1083. mp_digit mp;
  1084. int err;
  1085. if (sig == NULL || hash == NULL || stat == NULL || key == NULL)
  1086. return ECC_BAD_ARG_E;
  1087. /* default to invalid signature */
  1088. *stat = 0;
  1089. /* is the IDX valid ? */
  1090. if (ecc_is_valid_idx(key->idx) != 1) {
  1091. return ECC_BAD_ARG_E;
  1092. }
  1093. /* allocate ints */
  1094. if ((err = mp_init_multi(&v, &w, &u1, &u2, &p, &e)) != MP_OKAY) {
  1095. return MEMORY_E;
  1096. }
  1097. if ((err = mp_init(&m)) != MP_OKAY) {
  1098. mp_clear(&v);
  1099. mp_clear(&w);
  1100. mp_clear(&u1);
  1101. mp_clear(&u2);
  1102. mp_clear(&p);
  1103. mp_clear(&e);
  1104. return MEMORY_E;
  1105. }
  1106. /* allocate points */
  1107. mG = ecc_new_point();
  1108. mQ = ecc_new_point();
  1109. if (mQ == NULL || mG == NULL)
  1110. err = MEMORY_E;
  1111. /* Note, DecodeECC_DSA_Sig() calls mp_init() on r and s.
  1112. * If either of those don't allocate correctly, none of
  1113. * the rest of this function will execute, and everything
  1114. * gets cleaned up at the end. */
  1115. XMEMSET(&r, 0, sizeof(r));
  1116. XMEMSET(&s, 0, sizeof(s));
  1117. if (err == MP_OKAY)
  1118. err = DecodeECC_DSA_Sig(sig, siglen, &r, &s);
  1119. /* get the order */
  1120. if (err == MP_OKAY)
  1121. err = mp_read_radix(&p, (char *)key->dp->order, 16);
  1122. /* get the modulus */
  1123. if (err == MP_OKAY)
  1124. err = mp_read_radix(&m, (char *)key->dp->prime, 16);
  1125. /* check for zero */
  1126. if (err == MP_OKAY) {
  1127. if (mp_iszero(&r) || mp_iszero(&s) || mp_cmp(&r, &p) != MP_LT ||
  1128. mp_cmp(&s, &p) != MP_LT)
  1129. err = MP_ZERO_E;
  1130. }
  1131. /* read hash */
  1132. if (err == MP_OKAY) {
  1133. int truncLen = (int)hashlen;
  1134. if (truncLen > ecc_size(key))
  1135. truncLen = ecc_size(key);
  1136. err = mp_read_unsigned_bin(&e, (byte*)hash, truncLen);
  1137. }
  1138. /* w = s^-1 mod n */
  1139. if (err == MP_OKAY)
  1140. err = mp_invmod(&s, &p, &w);
  1141. /* u1 = ew */
  1142. if (err == MP_OKAY)
  1143. err = mp_mulmod(&e, &w, &p, &u1);
  1144. /* u2 = rw */
  1145. if (err == MP_OKAY)
  1146. err = mp_mulmod(&r, &w, &p, &u2);
  1147. /* find mG and mQ */
  1148. if (err == MP_OKAY)
  1149. err = mp_read_radix(&mG->x, (char *)key->dp->Gx, 16);
  1150. if (err == MP_OKAY)
  1151. err = mp_read_radix(&mG->y, (char *)key->dp->Gy, 16);
  1152. if (err == MP_OKAY)
  1153. mp_set(&mG->z, 1);
  1154. if (err == MP_OKAY)
  1155. err = mp_copy(&key->pubkey.x, &mQ->x);
  1156. if (err == MP_OKAY)
  1157. err = mp_copy(&key->pubkey.y, &mQ->y);
  1158. if (err == MP_OKAY)
  1159. err = mp_copy(&key->pubkey.z, &mQ->z);
  1160. #ifndef ECC_SHAMIR
  1161. /* compute u1*mG + u2*mQ = mG */
  1162. if (err == MP_OKAY)
  1163. err = ecc_mulmod(&u1, mG, mG, &m, 0);
  1164. if (err == MP_OKAY)
  1165. err = ecc_mulmod(&u2, mQ, mQ, &m, 0);
  1166. /* find the montgomery mp */
  1167. if (err == MP_OKAY)
  1168. err = mp_montgomery_setup(&m, &mp);
  1169. /* add them */
  1170. if (err == MP_OKAY)
  1171. err = ecc_projective_add_point(mQ, mG, mG, &m, &mp);
  1172. /* reduce */
  1173. if (err == MP_OKAY)
  1174. err = ecc_map(mG, &m, &mp);
  1175. #else
  1176. /* use Shamir's trick to compute u1*mG + u2*mQ using half the doubles */
  1177. if (err == MP_OKAY)
  1178. err = ecc_mul2add(mG, &u1, mQ, &u2, mG, &m);
  1179. #endif /* ECC_SHAMIR */
  1180. /* v = X_x1 mod n */
  1181. if (err == MP_OKAY)
  1182. err = mp_mod(&mG->x, &p, &v);
  1183. /* does v == r */
  1184. if (err == MP_OKAY) {
  1185. if (mp_cmp(&v, &r) == MP_EQ)
  1186. *stat = 1;
  1187. }
  1188. ecc_del_point(mG);
  1189. ecc_del_point(mQ);
  1190. mp_clear(&r);
  1191. mp_clear(&s);
  1192. mp_clear(&v);
  1193. mp_clear(&w);
  1194. mp_clear(&u1);
  1195. mp_clear(&u2);
  1196. mp_clear(&p);
  1197. mp_clear(&e);
  1198. mp_clear(&m);
  1199. return err;
  1200. }
  1201. /* export public ECC key in ANSI X9.63 format */
  1202. int ecc_export_x963(ecc_key* key, byte* out, word32* outLen)
  1203. {
  1204. byte buf[ECC_BUFSIZE];
  1205. word32 numlen;
  1206. if (key == NULL || out == NULL || outLen == NULL)
  1207. return ECC_BAD_ARG_E;
  1208. if (ecc_is_valid_idx(key->idx) == 0) {
  1209. return ECC_BAD_ARG_E;
  1210. }
  1211. numlen = key->dp->size;
  1212. if (*outLen < (1 + 2*numlen)) {
  1213. *outLen = 1 + 2*numlen;
  1214. return BUFFER_E;
  1215. }
  1216. /* store byte 0x04 */
  1217. out[0] = 0x04;
  1218. /* pad and store x */
  1219. XMEMSET(buf, 0, sizeof(buf));
  1220. mp_to_unsigned_bin(&key->pubkey.x,
  1221. buf + (numlen - mp_unsigned_bin_size(&key->pubkey.x)));
  1222. XMEMCPY(out+1, buf, numlen);
  1223. /* pad and store y */
  1224. XMEMSET(buf, 0, sizeof(buf));
  1225. mp_to_unsigned_bin(&key->pubkey.y,
  1226. buf + (numlen - mp_unsigned_bin_size(&key->pubkey.y)));
  1227. XMEMCPY(out+1+numlen, buf, numlen);
  1228. *outLen = 1 + 2*numlen;
  1229. return 0;
  1230. }
  1231. /* import public ECC key in ANSI X9.63 format */
  1232. int ecc_import_x963(const byte* in, word32 inLen, ecc_key* key)
  1233. {
  1234. int x, err;
  1235. if (in == NULL || key == NULL)
  1236. return ECC_BAD_ARG_E;
  1237. /* must be odd */
  1238. if ((inLen & 1) == 0) {
  1239. return ECC_BAD_ARG_E;
  1240. }
  1241. /* init key */
  1242. if (mp_init_multi(&key->pubkey.x, &key->pubkey.y, &key->pubkey.z, &key->k,
  1243. NULL, NULL) != MP_OKAY) {
  1244. return MEMORY_E;
  1245. }
  1246. err = MP_OKAY;
  1247. /* check for 4, 6 or 7 */
  1248. if (in[0] != 4 && in[0] != 6 && in[0] != 7) {
  1249. err = ASN_PARSE_E;
  1250. }
  1251. /* read data */
  1252. if (err == MP_OKAY)
  1253. err = mp_read_unsigned_bin(&key->pubkey.x, (byte*)in+1, (inLen-1)>>1);
  1254. if (err == MP_OKAY)
  1255. err = mp_read_unsigned_bin(&key->pubkey.y, (byte*)in+1+((inLen-1)>>1),
  1256. (inLen-1)>>1);
  1257. if (err == MP_OKAY)
  1258. mp_set(&key->pubkey.z, 1);
  1259. if (err == MP_OKAY) {
  1260. /* determine the idx */
  1261. for (x = 0; ecc_sets[x].size != 0; x++) {
  1262. if ((unsigned)ecc_sets[x].size >= ((inLen-1)>>1)) {
  1263. break;
  1264. }
  1265. }
  1266. if (ecc_sets[x].size == 0) {
  1267. err = ASN_PARSE_E;
  1268. } else {
  1269. /* set the idx */
  1270. key->idx = x;
  1271. key->dp = &ecc_sets[x];
  1272. key->type = ECC_PUBLICKEY;
  1273. }
  1274. }
  1275. if (err != MP_OKAY) {
  1276. mp_clear(&key->pubkey.x);
  1277. mp_clear(&key->pubkey.y);
  1278. mp_clear(&key->pubkey.z);
  1279. mp_clear(&key->k);
  1280. }
  1281. return err;
  1282. }
  1283. /* ecc private key import, public key in ANSI X9.63 format, private raw */
  1284. int ecc_import_private_key(const byte* priv, word32 privSz, const byte* pub,
  1285. word32 pubSz, ecc_key* key)
  1286. {
  1287. int ret = ecc_import_x963(pub, pubSz, key);
  1288. if (ret != 0)
  1289. return ret;
  1290. key->type = ECC_PRIVATEKEY;
  1291. return mp_read_unsigned_bin(&key->k, priv, privSz);
  1292. }
  1293. /* key size in octets */
  1294. int ecc_size(ecc_key* key)
  1295. {
  1296. if (key == NULL) return 0;
  1297. return key->dp->size;
  1298. }
  1299. /* signature size in octets */
  1300. int ecc_sig_size(ecc_key* key)
  1301. {
  1302. int sz = ecc_size(key);
  1303. if (sz < 0)
  1304. return sz;
  1305. return sz * 2 + SIG_HEADER_SZ;
  1306. }
  1307. #endif /* HAVE_ECC */