ecc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. /*
  2. * Copyright (c) 2013, Kenneth MacKay
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are
  7. * met:
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  18. * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  20. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <linux/random.h>
  27. #include "ecc.h"
  28. /* 256-bit curve */
  29. #define ECC_BYTES 32
  30. #define MAX_TRIES 16
  31. /* Number of u64's needed */
  32. #define NUM_ECC_DIGITS (ECC_BYTES / 8)
  33. struct ecc_point {
  34. u64 x[NUM_ECC_DIGITS];
  35. u64 y[NUM_ECC_DIGITS];
  36. };
  37. typedef struct {
  38. u64 m_low;
  39. u64 m_high;
  40. } uint128_t;
  41. #define CURVE_P_32 { 0xFFFFFFFFFFFFFFFFull, 0x00000000FFFFFFFFull, \
  42. 0x0000000000000000ull, 0xFFFFFFFF00000001ull }
  43. #define CURVE_G_32 { \
  44. { 0xF4A13945D898C296ull, 0x77037D812DEB33A0ull, \
  45. 0xF8BCE6E563A440F2ull, 0x6B17D1F2E12C4247ull }, \
  46. { 0xCBB6406837BF51F5ull, 0x2BCE33576B315ECEull, \
  47. 0x8EE7EB4A7C0F9E16ull, 0x4FE342E2FE1A7F9Bull } \
  48. }
  49. #define CURVE_N_32 { 0xF3B9CAC2FC632551ull, 0xBCE6FAADA7179E84ull, \
  50. 0xFFFFFFFFFFFFFFFFull, 0xFFFFFFFF00000000ull }
  51. static u64 curve_p[NUM_ECC_DIGITS] = CURVE_P_32;
  52. static struct ecc_point curve_g = CURVE_G_32;
  53. static u64 curve_n[NUM_ECC_DIGITS] = CURVE_N_32;
  54. static void vli_clear(u64 *vli)
  55. {
  56. int i;
  57. for (i = 0; i < NUM_ECC_DIGITS; i++)
  58. vli[i] = 0;
  59. }
  60. /* Returns true if vli == 0, false otherwise. */
  61. static bool vli_is_zero(const u64 *vli)
  62. {
  63. int i;
  64. for (i = 0; i < NUM_ECC_DIGITS; i++) {
  65. if (vli[i])
  66. return false;
  67. }
  68. return true;
  69. }
  70. /* Returns nonzero if bit bit of vli is set. */
  71. static u64 vli_test_bit(const u64 *vli, unsigned int bit)
  72. {
  73. return (vli[bit / 64] & ((u64) 1 << (bit % 64)));
  74. }
  75. /* Counts the number of 64-bit "digits" in vli. */
  76. static unsigned int vli_num_digits(const u64 *vli)
  77. {
  78. int i;
  79. /* Search from the end until we find a non-zero digit.
  80. * We do it in reverse because we expect that most digits will
  81. * be nonzero.
  82. */
  83. for (i = NUM_ECC_DIGITS - 1; i >= 0 && vli[i] == 0; i--);
  84. return (i + 1);
  85. }
  86. /* Counts the number of bits required for vli. */
  87. static unsigned int vli_num_bits(const u64 *vli)
  88. {
  89. unsigned int i, num_digits;
  90. u64 digit;
  91. num_digits = vli_num_digits(vli);
  92. if (num_digits == 0)
  93. return 0;
  94. digit = vli[num_digits - 1];
  95. for (i = 0; digit; i++)
  96. digit >>= 1;
  97. return ((num_digits - 1) * 64 + i);
  98. }
  99. /* Sets dest = src. */
  100. static void vli_set(u64 *dest, const u64 *src)
  101. {
  102. int i;
  103. for (i = 0; i < NUM_ECC_DIGITS; i++)
  104. dest[i] = src[i];
  105. }
  106. /* Returns sign of left - right. */
  107. static int vli_cmp(const u64 *left, const u64 *right)
  108. {
  109. int i;
  110. for (i = NUM_ECC_DIGITS - 1; i >= 0; i--) {
  111. if (left[i] > right[i])
  112. return 1;
  113. else if (left[i] < right[i])
  114. return -1;
  115. }
  116. return 0;
  117. }
  118. /* Computes result = in << c, returning carry. Can modify in place
  119. * (if result == in). 0 < shift < 64.
  120. */
  121. static u64 vli_lshift(u64 *result, const u64 *in,
  122. unsigned int shift)
  123. {
  124. u64 carry = 0;
  125. int i;
  126. for (i = 0; i < NUM_ECC_DIGITS; i++) {
  127. u64 temp = in[i];
  128. result[i] = (temp << shift) | carry;
  129. carry = temp >> (64 - shift);
  130. }
  131. return carry;
  132. }
  133. /* Computes vli = vli >> 1. */
  134. static void vli_rshift1(u64 *vli)
  135. {
  136. u64 *end = vli;
  137. u64 carry = 0;
  138. vli += NUM_ECC_DIGITS;
  139. while (vli-- > end) {
  140. u64 temp = *vli;
  141. *vli = (temp >> 1) | carry;
  142. carry = temp << 63;
  143. }
  144. }
  145. /* Computes result = left + right, returning carry. Can modify in place. */
  146. static u64 vli_add(u64 *result, const u64 *left,
  147. const u64 *right)
  148. {
  149. u64 carry = 0;
  150. int i;
  151. for (i = 0; i < NUM_ECC_DIGITS; i++) {
  152. u64 sum;
  153. sum = left[i] + right[i] + carry;
  154. if (sum != left[i])
  155. carry = (sum < left[i]);
  156. result[i] = sum;
  157. }
  158. return carry;
  159. }
  160. /* Computes result = left - right, returning borrow. Can modify in place. */
  161. static u64 vli_sub(u64 *result, const u64 *left, const u64 *right)
  162. {
  163. u64 borrow = 0;
  164. int i;
  165. for (i = 0; i < NUM_ECC_DIGITS; i++) {
  166. u64 diff;
  167. diff = left[i] - right[i] - borrow;
  168. if (diff != left[i])
  169. borrow = (diff > left[i]);
  170. result[i] = diff;
  171. }
  172. return borrow;
  173. }
  174. static uint128_t mul_64_64(u64 left, u64 right)
  175. {
  176. u64 a0 = left & 0xffffffffull;
  177. u64 a1 = left >> 32;
  178. u64 b0 = right & 0xffffffffull;
  179. u64 b1 = right >> 32;
  180. u64 m0 = a0 * b0;
  181. u64 m1 = a0 * b1;
  182. u64 m2 = a1 * b0;
  183. u64 m3 = a1 * b1;
  184. uint128_t result;
  185. m2 += (m0 >> 32);
  186. m2 += m1;
  187. /* Overflow */
  188. if (m2 < m1)
  189. m3 += 0x100000000ull;
  190. result.m_low = (m0 & 0xffffffffull) | (m2 << 32);
  191. result.m_high = m3 + (m2 >> 32);
  192. return result;
  193. }
  194. static uint128_t add_128_128(uint128_t a, uint128_t b)
  195. {
  196. uint128_t result;
  197. result.m_low = a.m_low + b.m_low;
  198. result.m_high = a.m_high + b.m_high + (result.m_low < a.m_low);
  199. return result;
  200. }
  201. static void vli_mult(u64 *result, const u64 *left, const u64 *right)
  202. {
  203. uint128_t r01 = { 0, 0 };
  204. u64 r2 = 0;
  205. unsigned int i, k;
  206. /* Compute each digit of result in sequence, maintaining the
  207. * carries.
  208. */
  209. for (k = 0; k < NUM_ECC_DIGITS * 2 - 1; k++) {
  210. unsigned int min;
  211. if (k < NUM_ECC_DIGITS)
  212. min = 0;
  213. else
  214. min = (k + 1) - NUM_ECC_DIGITS;
  215. for (i = min; i <= k && i < NUM_ECC_DIGITS; i++) {
  216. uint128_t product;
  217. product = mul_64_64(left[i], right[k - i]);
  218. r01 = add_128_128(r01, product);
  219. r2 += (r01.m_high < product.m_high);
  220. }
  221. result[k] = r01.m_low;
  222. r01.m_low = r01.m_high;
  223. r01.m_high = r2;
  224. r2 = 0;
  225. }
  226. result[NUM_ECC_DIGITS * 2 - 1] = r01.m_low;
  227. }
  228. static void vli_square(u64 *result, const u64 *left)
  229. {
  230. uint128_t r01 = { 0, 0 };
  231. u64 r2 = 0;
  232. int i, k;
  233. for (k = 0; k < NUM_ECC_DIGITS * 2 - 1; k++) {
  234. unsigned int min;
  235. if (k < NUM_ECC_DIGITS)
  236. min = 0;
  237. else
  238. min = (k + 1) - NUM_ECC_DIGITS;
  239. for (i = min; i <= k && i <= k - i; i++) {
  240. uint128_t product;
  241. product = mul_64_64(left[i], left[k - i]);
  242. if (i < k - i) {
  243. r2 += product.m_high >> 63;
  244. product.m_high = (product.m_high << 1) |
  245. (product.m_low >> 63);
  246. product.m_low <<= 1;
  247. }
  248. r01 = add_128_128(r01, product);
  249. r2 += (r01.m_high < product.m_high);
  250. }
  251. result[k] = r01.m_low;
  252. r01.m_low = r01.m_high;
  253. r01.m_high = r2;
  254. r2 = 0;
  255. }
  256. result[NUM_ECC_DIGITS * 2 - 1] = r01.m_low;
  257. }
  258. /* Computes result = (left + right) % mod.
  259. * Assumes that left < mod and right < mod, result != mod.
  260. */
  261. static void vli_mod_add(u64 *result, const u64 *left, const u64 *right,
  262. const u64 *mod)
  263. {
  264. u64 carry;
  265. carry = vli_add(result, left, right);
  266. /* result > mod (result = mod + remainder), so subtract mod to
  267. * get remainder.
  268. */
  269. if (carry || vli_cmp(result, mod) >= 0)
  270. vli_sub(result, result, mod);
  271. }
  272. /* Computes result = (left - right) % mod.
  273. * Assumes that left < mod and right < mod, result != mod.
  274. */
  275. static void vli_mod_sub(u64 *result, const u64 *left, const u64 *right,
  276. const u64 *mod)
  277. {
  278. u64 borrow = vli_sub(result, left, right);
  279. /* In this case, p_result == -diff == (max int) - diff.
  280. * Since -x % d == d - x, we can get the correct result from
  281. * result + mod (with overflow).
  282. */
  283. if (borrow)
  284. vli_add(result, result, mod);
  285. }
  286. /* Computes result = product % curve_p
  287. from http://www.nsa.gov/ia/_files/nist-routines.pdf */
  288. static void vli_mmod_fast(u64 *result, const u64 *product)
  289. {
  290. u64 tmp[NUM_ECC_DIGITS];
  291. int carry;
  292. /* t */
  293. vli_set(result, product);
  294. /* s1 */
  295. tmp[0] = 0;
  296. tmp[1] = product[5] & 0xffffffff00000000ull;
  297. tmp[2] = product[6];
  298. tmp[3] = product[7];
  299. carry = vli_lshift(tmp, tmp, 1);
  300. carry += vli_add(result, result, tmp);
  301. /* s2 */
  302. tmp[1] = product[6] << 32;
  303. tmp[2] = (product[6] >> 32) | (product[7] << 32);
  304. tmp[3] = product[7] >> 32;
  305. carry += vli_lshift(tmp, tmp, 1);
  306. carry += vli_add(result, result, tmp);
  307. /* s3 */
  308. tmp[0] = product[4];
  309. tmp[1] = product[5] & 0xffffffff;
  310. tmp[2] = 0;
  311. tmp[3] = product[7];
  312. carry += vli_add(result, result, tmp);
  313. /* s4 */
  314. tmp[0] = (product[4] >> 32) | (product[5] << 32);
  315. tmp[1] = (product[5] >> 32) | (product[6] & 0xffffffff00000000ull);
  316. tmp[2] = product[7];
  317. tmp[3] = (product[6] >> 32) | (product[4] << 32);
  318. carry += vli_add(result, result, tmp);
  319. /* d1 */
  320. tmp[0] = (product[5] >> 32) | (product[6] << 32);
  321. tmp[1] = (product[6] >> 32);
  322. tmp[2] = 0;
  323. tmp[3] = (product[4] & 0xffffffff) | (product[5] << 32);
  324. carry -= vli_sub(result, result, tmp);
  325. /* d2 */
  326. tmp[0] = product[6];
  327. tmp[1] = product[7];
  328. tmp[2] = 0;
  329. tmp[3] = (product[4] >> 32) | (product[5] & 0xffffffff00000000ull);
  330. carry -= vli_sub(result, result, tmp);
  331. /* d3 */
  332. tmp[0] = (product[6] >> 32) | (product[7] << 32);
  333. tmp[1] = (product[7] >> 32) | (product[4] << 32);
  334. tmp[2] = (product[4] >> 32) | (product[5] << 32);
  335. tmp[3] = (product[6] << 32);
  336. carry -= vli_sub(result, result, tmp);
  337. /* d4 */
  338. tmp[0] = product[7];
  339. tmp[1] = product[4] & 0xffffffff00000000ull;
  340. tmp[2] = product[5];
  341. tmp[3] = product[6] & 0xffffffff00000000ull;
  342. carry -= vli_sub(result, result, tmp);
  343. if (carry < 0) {
  344. do {
  345. carry += vli_add(result, result, curve_p);
  346. } while (carry < 0);
  347. } else {
  348. while (carry || vli_cmp(curve_p, result) != 1)
  349. carry -= vli_sub(result, result, curve_p);
  350. }
  351. }
  352. /* Computes result = (left * right) % curve_p. */
  353. static void vli_mod_mult_fast(u64 *result, const u64 *left, const u64 *right)
  354. {
  355. u64 product[2 * NUM_ECC_DIGITS];
  356. vli_mult(product, left, right);
  357. vli_mmod_fast(result, product);
  358. }
  359. /* Computes result = left^2 % curve_p. */
  360. static void vli_mod_square_fast(u64 *result, const u64 *left)
  361. {
  362. u64 product[2 * NUM_ECC_DIGITS];
  363. vli_square(product, left);
  364. vli_mmod_fast(result, product);
  365. }
  366. #define EVEN(vli) (!(vli[0] & 1))
  367. /* Computes result = (1 / p_input) % mod. All VLIs are the same size.
  368. * See "From Euclid's GCD to Montgomery Multiplication to the Great Divide"
  369. * https://labs.oracle.com/techrep/2001/smli_tr-2001-95.pdf
  370. */
  371. static void vli_mod_inv(u64 *result, const u64 *input, const u64 *mod)
  372. {
  373. u64 a[NUM_ECC_DIGITS], b[NUM_ECC_DIGITS];
  374. u64 u[NUM_ECC_DIGITS], v[NUM_ECC_DIGITS];
  375. u64 carry;
  376. int cmp_result;
  377. if (vli_is_zero(input)) {
  378. vli_clear(result);
  379. return;
  380. }
  381. vli_set(a, input);
  382. vli_set(b, mod);
  383. vli_clear(u);
  384. u[0] = 1;
  385. vli_clear(v);
  386. while ((cmp_result = vli_cmp(a, b)) != 0) {
  387. carry = 0;
  388. if (EVEN(a)) {
  389. vli_rshift1(a);
  390. if (!EVEN(u))
  391. carry = vli_add(u, u, mod);
  392. vli_rshift1(u);
  393. if (carry)
  394. u[NUM_ECC_DIGITS - 1] |= 0x8000000000000000ull;
  395. } else if (EVEN(b)) {
  396. vli_rshift1(b);
  397. if (!EVEN(v))
  398. carry = vli_add(v, v, mod);
  399. vli_rshift1(v);
  400. if (carry)
  401. v[NUM_ECC_DIGITS - 1] |= 0x8000000000000000ull;
  402. } else if (cmp_result > 0) {
  403. vli_sub(a, a, b);
  404. vli_rshift1(a);
  405. if (vli_cmp(u, v) < 0)
  406. vli_add(u, u, mod);
  407. vli_sub(u, u, v);
  408. if (!EVEN(u))
  409. carry = vli_add(u, u, mod);
  410. vli_rshift1(u);
  411. if (carry)
  412. u[NUM_ECC_DIGITS - 1] |= 0x8000000000000000ull;
  413. } else {
  414. vli_sub(b, b, a);
  415. vli_rshift1(b);
  416. if (vli_cmp(v, u) < 0)
  417. vli_add(v, v, mod);
  418. vli_sub(v, v, u);
  419. if (!EVEN(v))
  420. carry = vli_add(v, v, mod);
  421. vli_rshift1(v);
  422. if (carry)
  423. v[NUM_ECC_DIGITS - 1] |= 0x8000000000000000ull;
  424. }
  425. }
  426. vli_set(result, u);
  427. }
  428. /* ------ Point operations ------ */
  429. /* Returns true if p_point is the point at infinity, false otherwise. */
  430. static bool ecc_point_is_zero(const struct ecc_point *point)
  431. {
  432. return (vli_is_zero(point->x) && vli_is_zero(point->y));
  433. }
  434. /* Point multiplication algorithm using Montgomery's ladder with co-Z
  435. * coordinates. From http://eprint.iacr.org/2011/338.pdf
  436. */
  437. /* Double in place */
  438. static void ecc_point_double_jacobian(u64 *x1, u64 *y1, u64 *z1)
  439. {
  440. /* t1 = x, t2 = y, t3 = z */
  441. u64 t4[NUM_ECC_DIGITS];
  442. u64 t5[NUM_ECC_DIGITS];
  443. if (vli_is_zero(z1))
  444. return;
  445. vli_mod_square_fast(t4, y1); /* t4 = y1^2 */
  446. vli_mod_mult_fast(t5, x1, t4); /* t5 = x1*y1^2 = A */
  447. vli_mod_square_fast(t4, t4); /* t4 = y1^4 */
  448. vli_mod_mult_fast(y1, y1, z1); /* t2 = y1*z1 = z3 */
  449. vli_mod_square_fast(z1, z1); /* t3 = z1^2 */
  450. vli_mod_add(x1, x1, z1, curve_p); /* t1 = x1 + z1^2 */
  451. vli_mod_add(z1, z1, z1, curve_p); /* t3 = 2*z1^2 */
  452. vli_mod_sub(z1, x1, z1, curve_p); /* t3 = x1 - z1^2 */
  453. vli_mod_mult_fast(x1, x1, z1); /* t1 = x1^2 - z1^4 */
  454. vli_mod_add(z1, x1, x1, curve_p); /* t3 = 2*(x1^2 - z1^4) */
  455. vli_mod_add(x1, x1, z1, curve_p); /* t1 = 3*(x1^2 - z1^4) */
  456. if (vli_test_bit(x1, 0)) {
  457. u64 carry = vli_add(x1, x1, curve_p);
  458. vli_rshift1(x1);
  459. x1[NUM_ECC_DIGITS - 1] |= carry << 63;
  460. } else {
  461. vli_rshift1(x1);
  462. }
  463. /* t1 = 3/2*(x1^2 - z1^4) = B */
  464. vli_mod_square_fast(z1, x1); /* t3 = B^2 */
  465. vli_mod_sub(z1, z1, t5, curve_p); /* t3 = B^2 - A */
  466. vli_mod_sub(z1, z1, t5, curve_p); /* t3 = B^2 - 2A = x3 */
  467. vli_mod_sub(t5, t5, z1, curve_p); /* t5 = A - x3 */
  468. vli_mod_mult_fast(x1, x1, t5); /* t1 = B * (A - x3) */
  469. vli_mod_sub(t4, x1, t4, curve_p); /* t4 = B * (A - x3) - y1^4 = y3 */
  470. vli_set(x1, z1);
  471. vli_set(z1, y1);
  472. vli_set(y1, t4);
  473. }
  474. /* Modify (x1, y1) => (x1 * z^2, y1 * z^3) */
  475. static void apply_z(u64 *x1, u64 *y1, u64 *z)
  476. {
  477. u64 t1[NUM_ECC_DIGITS];
  478. vli_mod_square_fast(t1, z); /* z^2 */
  479. vli_mod_mult_fast(x1, x1, t1); /* x1 * z^2 */
  480. vli_mod_mult_fast(t1, t1, z); /* z^3 */
  481. vli_mod_mult_fast(y1, y1, t1); /* y1 * z^3 */
  482. }
  483. /* P = (x1, y1) => 2P, (x2, y2) => P' */
  484. static void xycz_initial_double(u64 *x1, u64 *y1, u64 *x2, u64 *y2,
  485. u64 *p_initial_z)
  486. {
  487. u64 z[NUM_ECC_DIGITS];
  488. vli_set(x2, x1);
  489. vli_set(y2, y1);
  490. vli_clear(z);
  491. z[0] = 1;
  492. if (p_initial_z)
  493. vli_set(z, p_initial_z);
  494. apply_z(x1, y1, z);
  495. ecc_point_double_jacobian(x1, y1, z);
  496. apply_z(x2, y2, z);
  497. }
  498. /* Input P = (x1, y1, Z), Q = (x2, y2, Z)
  499. * Output P' = (x1', y1', Z3), P + Q = (x3, y3, Z3)
  500. * or P => P', Q => P + Q
  501. */
  502. static void xycz_add(u64 *x1, u64 *y1, u64 *x2, u64 *y2)
  503. {
  504. /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
  505. u64 t5[NUM_ECC_DIGITS];
  506. vli_mod_sub(t5, x2, x1, curve_p); /* t5 = x2 - x1 */
  507. vli_mod_square_fast(t5, t5); /* t5 = (x2 - x1)^2 = A */
  508. vli_mod_mult_fast(x1, x1, t5); /* t1 = x1*A = B */
  509. vli_mod_mult_fast(x2, x2, t5); /* t3 = x2*A = C */
  510. vli_mod_sub(y2, y2, y1, curve_p); /* t4 = y2 - y1 */
  511. vli_mod_square_fast(t5, y2); /* t5 = (y2 - y1)^2 = D */
  512. vli_mod_sub(t5, t5, x1, curve_p); /* t5 = D - B */
  513. vli_mod_sub(t5, t5, x2, curve_p); /* t5 = D - B - C = x3 */
  514. vli_mod_sub(x2, x2, x1, curve_p); /* t3 = C - B */
  515. vli_mod_mult_fast(y1, y1, x2); /* t2 = y1*(C - B) */
  516. vli_mod_sub(x2, x1, t5, curve_p); /* t3 = B - x3 */
  517. vli_mod_mult_fast(y2, y2, x2); /* t4 = (y2 - y1)*(B - x3) */
  518. vli_mod_sub(y2, y2, y1, curve_p); /* t4 = y3 */
  519. vli_set(x2, t5);
  520. }
  521. /* Input P = (x1, y1, Z), Q = (x2, y2, Z)
  522. * Output P + Q = (x3, y3, Z3), P - Q = (x3', y3', Z3)
  523. * or P => P - Q, Q => P + Q
  524. */
  525. static void xycz_add_c(u64 *x1, u64 *y1, u64 *x2, u64 *y2)
  526. {
  527. /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
  528. u64 t5[NUM_ECC_DIGITS];
  529. u64 t6[NUM_ECC_DIGITS];
  530. u64 t7[NUM_ECC_DIGITS];
  531. vli_mod_sub(t5, x2, x1, curve_p); /* t5 = x2 - x1 */
  532. vli_mod_square_fast(t5, t5); /* t5 = (x2 - x1)^2 = A */
  533. vli_mod_mult_fast(x1, x1, t5); /* t1 = x1*A = B */
  534. vli_mod_mult_fast(x2, x2, t5); /* t3 = x2*A = C */
  535. vli_mod_add(t5, y2, y1, curve_p); /* t4 = y2 + y1 */
  536. vli_mod_sub(y2, y2, y1, curve_p); /* t4 = y2 - y1 */
  537. vli_mod_sub(t6, x2, x1, curve_p); /* t6 = C - B */
  538. vli_mod_mult_fast(y1, y1, t6); /* t2 = y1 * (C - B) */
  539. vli_mod_add(t6, x1, x2, curve_p); /* t6 = B + C */
  540. vli_mod_square_fast(x2, y2); /* t3 = (y2 - y1)^2 */
  541. vli_mod_sub(x2, x2, t6, curve_p); /* t3 = x3 */
  542. vli_mod_sub(t7, x1, x2, curve_p); /* t7 = B - x3 */
  543. vli_mod_mult_fast(y2, y2, t7); /* t4 = (y2 - y1)*(B - x3) */
  544. vli_mod_sub(y2, y2, y1, curve_p); /* t4 = y3 */
  545. vli_mod_square_fast(t7, t5); /* t7 = (y2 + y1)^2 = F */
  546. vli_mod_sub(t7, t7, t6, curve_p); /* t7 = x3' */
  547. vli_mod_sub(t6, t7, x1, curve_p); /* t6 = x3' - B */
  548. vli_mod_mult_fast(t6, t6, t5); /* t6 = (y2 + y1)*(x3' - B) */
  549. vli_mod_sub(y1, t6, y1, curve_p); /* t2 = y3' */
  550. vli_set(x1, t7);
  551. }
  552. static void ecc_point_mult(struct ecc_point *result,
  553. const struct ecc_point *point, u64 *scalar,
  554. u64 *initial_z, int num_bits)
  555. {
  556. /* R0 and R1 */
  557. u64 rx[2][NUM_ECC_DIGITS];
  558. u64 ry[2][NUM_ECC_DIGITS];
  559. u64 z[NUM_ECC_DIGITS];
  560. int i, nb;
  561. vli_set(rx[1], point->x);
  562. vli_set(ry[1], point->y);
  563. xycz_initial_double(rx[1], ry[1], rx[0], ry[0], initial_z);
  564. for (i = num_bits - 2; i > 0; i--) {
  565. nb = !vli_test_bit(scalar, i);
  566. xycz_add_c(rx[1 - nb], ry[1 - nb], rx[nb], ry[nb]);
  567. xycz_add(rx[nb], ry[nb], rx[1 - nb], ry[1 - nb]);
  568. }
  569. nb = !vli_test_bit(scalar, 0);
  570. xycz_add_c(rx[1 - nb], ry[1 - nb], rx[nb], ry[nb]);
  571. /* Find final 1/Z value. */
  572. vli_mod_sub(z, rx[1], rx[0], curve_p); /* X1 - X0 */
  573. vli_mod_mult_fast(z, z, ry[1 - nb]); /* Yb * (X1 - X0) */
  574. vli_mod_mult_fast(z, z, point->x); /* xP * Yb * (X1 - X0) */
  575. vli_mod_inv(z, z, curve_p); /* 1 / (xP * Yb * (X1 - X0)) */
  576. vli_mod_mult_fast(z, z, point->y); /* yP / (xP * Yb * (X1 - X0)) */
  577. vli_mod_mult_fast(z, z, rx[1 - nb]); /* Xb * yP / (xP * Yb * (X1 - X0)) */
  578. /* End 1/Z calculation */
  579. xycz_add(rx[nb], ry[nb], rx[1 - nb], ry[1 - nb]);
  580. apply_z(rx[0], ry[0], z);
  581. vli_set(result->x, rx[0]);
  582. vli_set(result->y, ry[0]);
  583. }
  584. static void ecc_bytes2native(const u8 bytes[ECC_BYTES],
  585. u64 native[NUM_ECC_DIGITS])
  586. {
  587. int i;
  588. for (i = 0; i < NUM_ECC_DIGITS; i++) {
  589. const u8 *digit = bytes + 8 * (NUM_ECC_DIGITS - 1 - i);
  590. native[NUM_ECC_DIGITS - 1 - i] =
  591. ((u64) digit[0] << 0) |
  592. ((u64) digit[1] << 8) |
  593. ((u64) digit[2] << 16) |
  594. ((u64) digit[3] << 24) |
  595. ((u64) digit[4] << 32) |
  596. ((u64) digit[5] << 40) |
  597. ((u64) digit[6] << 48) |
  598. ((u64) digit[7] << 56);
  599. }
  600. }
  601. static void ecc_native2bytes(const u64 native[NUM_ECC_DIGITS],
  602. u8 bytes[ECC_BYTES])
  603. {
  604. int i;
  605. for (i = 0; i < NUM_ECC_DIGITS; i++) {
  606. u8 *digit = bytes + 8 * (NUM_ECC_DIGITS - 1 - i);
  607. digit[0] = native[NUM_ECC_DIGITS - 1 - i] >> 0;
  608. digit[1] = native[NUM_ECC_DIGITS - 1 - i] >> 8;
  609. digit[2] = native[NUM_ECC_DIGITS - 1 - i] >> 16;
  610. digit[3] = native[NUM_ECC_DIGITS - 1 - i] >> 24;
  611. digit[4] = native[NUM_ECC_DIGITS - 1 - i] >> 32;
  612. digit[5] = native[NUM_ECC_DIGITS - 1 - i] >> 40;
  613. digit[6] = native[NUM_ECC_DIGITS - 1 - i] >> 48;
  614. digit[7] = native[NUM_ECC_DIGITS - 1 - i] >> 56;
  615. }
  616. }
  617. bool ecc_make_key(u8 public_key[64], u8 private_key[32])
  618. {
  619. struct ecc_point pk;
  620. u64 priv[NUM_ECC_DIGITS];
  621. unsigned int tries = 0;
  622. do {
  623. if (tries++ >= MAX_TRIES)
  624. return false;
  625. get_random_bytes(priv, ECC_BYTES);
  626. if (vli_is_zero(priv))
  627. continue;
  628. /* Make sure the private key is in the range [1, n-1]. */
  629. if (vli_cmp(curve_n, priv) != 1)
  630. continue;
  631. ecc_point_mult(&pk, &curve_g, priv, NULL, vli_num_bits(priv));
  632. } while (ecc_point_is_zero(&pk));
  633. ecc_native2bytes(priv, private_key);
  634. ecc_native2bytes(pk.x, public_key);
  635. ecc_native2bytes(pk.y, &public_key[32]);
  636. return true;
  637. }
  638. bool ecdh_shared_secret(const u8 public_key[64], const u8 private_key[32],
  639. u8 secret[32])
  640. {
  641. u64 priv[NUM_ECC_DIGITS];
  642. u64 rand[NUM_ECC_DIGITS];
  643. struct ecc_point product, pk;
  644. get_random_bytes(rand, ECC_BYTES);
  645. ecc_bytes2native(public_key, pk.x);
  646. ecc_bytes2native(&public_key[32], pk.y);
  647. ecc_bytes2native(private_key, priv);
  648. ecc_point_mult(&product, &pk, priv, rand, vli_num_bits(priv));
  649. ecc_native2bytes(product.x, secret);
  650. return !ecc_point_is_zero(&product);
  651. }