ECC384.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  1. // This is EASY-ECC by Kenneth MacKay with some very minor modifications for ZeroTier
  2. // https://github.com/esxgx/easy-ecc
  3. // This code is under the BSD 2-clause license, not ZeroTier's license
  4. #include "Constants.hpp"
  5. #include "ECC384.hpp"
  6. #include "Utils.hpp"
  7. namespace ZeroTier {
  8. namespace {
  9. #define uint unsigned int
  10. #define secp384r1 48
  11. #define ECC_CURVE secp384r1
  12. #define ECC_BYTES ECC_CURVE
  13. #define NUM_ECC_DIGITS (ECC_BYTES/8)
  14. #define MAX_TRIES 1024
  15. #ifdef ZT_HAVE_UINT128
  16. #define SUPPORTS_INT128 1
  17. #else
  18. #define SUPPORTS_INT128 0
  19. typedef struct
  20. {
  21. uint64_t m_low;
  22. uint64_t m_high;
  23. } uint128_t;
  24. #endif
  25. typedef struct EccPoint
  26. {
  27. uint64_t x[NUM_ECC_DIGITS];
  28. uint64_t y[NUM_ECC_DIGITS];
  29. } EccPoint;
  30. #define CONCAT1(a, b) a##b
  31. #define CONCAT(a, b) CONCAT1(a, b)
  32. #define Curve_P_48 {0x00000000FFFFFFFF, 0xFFFFFFFF00000000, 0xFFFFFFFFFFFFFFFE, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF}
  33. #define Curve_B_48 {0x2A85C8EDD3EC2AEF, 0xC656398D8A2ED19D, 0x0314088F5013875A, 0x181D9C6EFE814112, 0x988E056BE3F82D19, 0xB3312FA7E23EE7E4}
  34. #define Curve_G_48 {{0x3A545E3872760AB7, 0x5502F25DBF55296C, 0x59F741E082542A38, 0x6E1D3B628BA79B98, 0x8EB1C71EF320AD74, 0xAA87CA22BE8B0537}, {0x7A431D7C90EA0E5F, 0x0A60B1CE1D7E819D, 0xE9DA3113B5F0B8C0, 0xF8F41DBD289A147C, 0x5D9E98BF9292DC29, 0x3617DE4A96262C6F}}
  35. #define Curve_N_48 {0xECEC196ACCC52973, 0x581A0DB248B0A77A, 0xC7634D81F4372DDF, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF}
  36. const uint64_t curve_p[NUM_ECC_DIGITS] = CONCAT(Curve_P_, ECC_CURVE);
  37. const uint64_t curve_b[NUM_ECC_DIGITS] = CONCAT(Curve_B_, ECC_CURVE);
  38. const EccPoint curve_G = CONCAT(Curve_G_, ECC_CURVE);
  39. const uint64_t curve_n[NUM_ECC_DIGITS] = CONCAT(Curve_N_, ECC_CURVE);
  40. // Use ZeroTier's secure PRNG
  41. ZT_INLINE int getRandomNumber(uint64_t *p_vli)
  42. {
  43. Utils::getSecureRandom(p_vli,ECC_BYTES);
  44. return 1;
  45. }
  46. ZT_INLINE void vli_clear(uint64_t *p_vli)
  47. {
  48. uint i;
  49. for(i=0; i<NUM_ECC_DIGITS; ++i)
  50. {
  51. p_vli[i] = 0;
  52. }
  53. }
  54. /* Returns 1 if p_vli == 0, 0 otherwise. */
  55. ZT_INLINE int vli_isZero(const uint64_t *p_vli)
  56. {
  57. uint i;
  58. for(i = 0; i < NUM_ECC_DIGITS; ++i)
  59. {
  60. if(p_vli[i])
  61. {
  62. return 0;
  63. }
  64. }
  65. return 1;
  66. }
  67. /* Returns nonzero if bit p_bit of p_vli is set. */
  68. ZT_INLINE uint64_t vli_testBit(const uint64_t *p_vli,uint p_bit)
  69. {
  70. return (p_vli[p_bit/64] & ((uint64_t)1 << (p_bit % 64)));
  71. }
  72. /* Counts the number of 64-bit "digits" in p_vli. */
  73. ZT_INLINE uint vli_numDigits(const uint64_t *p_vli)
  74. {
  75. int i;
  76. /* Search from the end until we find a non-zero digit.
  77. We do it in reverse because we expect that most digits will be nonzero. */
  78. for(i = NUM_ECC_DIGITS - 1; i >= 0 && p_vli[i] == 0; --i)
  79. {
  80. }
  81. return (i + 1);
  82. }
  83. /* Counts the number of bits required for p_vli. */
  84. ZT_INLINE uint vli_numBits(const uint64_t *p_vli)
  85. {
  86. uint i;
  87. uint64_t l_digit;
  88. uint l_numDigits = vli_numDigits(p_vli);
  89. if(l_numDigits == 0)
  90. {
  91. return 0;
  92. }
  93. l_digit = p_vli[l_numDigits - 1];
  94. for(i=0; l_digit; ++i)
  95. {
  96. l_digit >>= 1;
  97. }
  98. return ((l_numDigits - 1) * 64 + i);
  99. }
  100. /* Sets p_dest = p_src. */
  101. ZT_INLINE void vli_set(uint64_t *p_dest,const uint64_t *p_src)
  102. {
  103. uint i;
  104. for(i=0; i<NUM_ECC_DIGITS; ++i)
  105. {
  106. p_dest[i] = p_src[i];
  107. }
  108. }
  109. /* Returns sign of p_left - p_right. */
  110. ZT_INLINE int vli_cmp(const uint64_t *p_left,const uint64_t *p_right)
  111. {
  112. int i;
  113. for(i = NUM_ECC_DIGITS-1; i >= 0; --i)
  114. {
  115. if(p_left[i] > p_right[i])
  116. {
  117. return 1;
  118. }
  119. else if(p_left[i] < p_right[i])
  120. {
  121. return -1;
  122. }
  123. }
  124. return 0;
  125. }
  126. /* Computes p_result = p_in << c, returning carry. Can modify in place (if p_result == p_in). 0 < p_shift < 64. */
  127. ZT_INLINE uint64_t vli_lshift(uint64_t *p_result,const uint64_t *p_in,uint p_shift)
  128. {
  129. uint64_t l_carry = 0;
  130. uint i;
  131. for(i = 0; i < NUM_ECC_DIGITS; ++i)
  132. {
  133. uint64_t l_temp = p_in[i];
  134. p_result[i] = (l_temp << p_shift) | l_carry;
  135. l_carry = l_temp >> (64 - p_shift);
  136. }
  137. return l_carry;
  138. }
  139. /* Computes p_vli = p_vli >> 1. */
  140. ZT_INLINE void vli_rshift1(uint64_t *p_vli)
  141. {
  142. uint64_t *l_end = p_vli;
  143. uint64_t l_carry = 0;
  144. p_vli += NUM_ECC_DIGITS;
  145. while(p_vli-- > l_end)
  146. {
  147. uint64_t l_temp = *p_vli;
  148. *p_vli = (l_temp >> 1) | l_carry;
  149. l_carry = l_temp << 63;
  150. }
  151. }
  152. /* Computes p_result = p_left + p_right, returning carry. Can modify in place. */
  153. ZT_INLINE uint64_t vli_add(uint64_t *p_result,const uint64_t *p_left,const uint64_t *p_right)
  154. {
  155. uint64_t l_carry = 0;
  156. uint i;
  157. for(i=0; i<NUM_ECC_DIGITS; ++i)
  158. {
  159. uint64_t l_sum = p_left[i] + p_right[i] + l_carry;
  160. if(l_sum != p_left[i])
  161. {
  162. l_carry = (l_sum < p_left[i]);
  163. }
  164. p_result[i] = l_sum;
  165. }
  166. return l_carry;
  167. }
  168. /* Computes p_result = p_left - p_right, returning borrow. Can modify in place. */
  169. ZT_INLINE uint64_t vli_sub(uint64_t *p_result,const uint64_t *p_left,const uint64_t *p_right)
  170. {
  171. uint64_t l_borrow = 0;
  172. uint i;
  173. for(i=0; i<NUM_ECC_DIGITS; ++i)
  174. {
  175. uint64_t l_diff = p_left[i] - p_right[i] - l_borrow;
  176. if(l_diff != p_left[i])
  177. {
  178. l_borrow = (l_diff > p_left[i]);
  179. }
  180. p_result[i] = l_diff;
  181. }
  182. return l_borrow;
  183. }
  184. #if SUPPORTS_INT128 == 1
  185. /* Computes p_result = p_left * p_right. */
  186. void vli_mult(uint64_t *p_result, const uint64_t *p_left, const uint64_t *p_right)
  187. {
  188. uint128_t r01 = 0;
  189. uint64_t r2 = 0;
  190. uint i, k;
  191. /* Compute each digit of p_result in sequence, maintaining the carries. */
  192. for(k=0; k < NUM_ECC_DIGITS*2 - 1; ++k)
  193. {
  194. uint l_min = (k < NUM_ECC_DIGITS ? 0 : (k + 1) - NUM_ECC_DIGITS);
  195. for(i=l_min; i<=k && i<NUM_ECC_DIGITS; ++i)
  196. {
  197. uint128_t l_product = (uint128_t)p_left[i] * p_right[k-i];
  198. r01 += l_product;
  199. r2 += (r01 < l_product);
  200. }
  201. p_result[k] = (uint64_t)r01;
  202. r01 = (r01 >> 64U) | (((uint128_t)r2) << 64U);
  203. r2 = 0;
  204. }
  205. p_result[NUM_ECC_DIGITS*2 - 1] = (uint64_t)r01;
  206. }
  207. /* Computes p_result = p_left^2. */
  208. void vli_square(uint64_t *p_result, const uint64_t *p_left)
  209. {
  210. uint128_t r01 = 0;
  211. uint64_t r2 = 0;
  212. uint i, k;
  213. for(k=0; k < NUM_ECC_DIGITS*2 - 1; ++k)
  214. {
  215. uint l_min = (k < NUM_ECC_DIGITS ? 0 : (k + 1) - NUM_ECC_DIGITS);
  216. for(i=l_min; i<=k && i<=k-i; ++i)
  217. {
  218. uint128_t l_product = (uint128_t)p_left[i] * p_left[k-i];
  219. if(i < k-i)
  220. {
  221. r2 += l_product >> 127;
  222. l_product *= 2;
  223. }
  224. r01 += l_product;
  225. r2 += (r01 < l_product);
  226. }
  227. p_result[k] = (uint64_t)r01;
  228. r01 = (r01 >> 64) | (((uint128_t)r2) << 64);
  229. r2 = 0;
  230. }
  231. p_result[NUM_ECC_DIGITS*2 - 1] = (uint64_t)r01;
  232. }
  233. #else /* #if SUPPORTS_INT128 */
  234. uint128_t mul_64_64(uint64_t p_left, uint64_t p_right)
  235. {
  236. uint128_t l_result;
  237. uint64_t a0 = p_left & 0xffffffffull;
  238. uint64_t a1 = p_left >> 32;
  239. uint64_t b0 = p_right & 0xffffffffull;
  240. uint64_t b1 = p_right >> 32;
  241. uint64_t m0 = a0 * b0;
  242. uint64_t m1 = a0 * b1;
  243. uint64_t m2 = a1 * b0;
  244. uint64_t m3 = a1 * b1;
  245. m2 += (m0 >> 32);
  246. m2 += m1;
  247. if(m2 < m1)
  248. { // overflow
  249. m3 += 0x100000000ull;
  250. }
  251. l_result.m_low = (m0 & 0xffffffffull) | (m2 << 32);
  252. l_result.m_high = m3 + (m2 >> 32);
  253. return l_result;
  254. }
  255. ZT_INLINE uint128_t add_128_128(uint128_t a, uint128_t b)
  256. {
  257. uint128_t l_result;
  258. l_result.m_low = a.m_low + b.m_low;
  259. l_result.m_high = a.m_high + b.m_high + (l_result.m_low < a.m_low);
  260. return l_result;
  261. }
  262. void vli_mult(uint64_t *p_result, uint64_t *p_left, const uint64_t *p_right)
  263. {
  264. uint128_t r01 = {0, 0};
  265. uint64_t r2 = 0;
  266. uint i, k;
  267. /* Compute each digit of p_result in sequence, maintaining the carries. */
  268. for(k=0; k < NUM_ECC_DIGITS*2 - 1; ++k)
  269. {
  270. uint l_min = (k < NUM_ECC_DIGITS ? 0 : (k + 1) - NUM_ECC_DIGITS);
  271. for(i=l_min; i<=k && i<NUM_ECC_DIGITS; ++i)
  272. {
  273. uint128_t l_product = mul_64_64(p_left[i], p_right[k-i]);
  274. r01 = add_128_128(r01, l_product);
  275. r2 += (r01.m_high < l_product.m_high);
  276. }
  277. p_result[k] = r01.m_low;
  278. r01.m_low = r01.m_high;
  279. r01.m_high = r2;
  280. r2 = 0;
  281. }
  282. p_result[NUM_ECC_DIGITS*2 - 1] = r01.m_low;
  283. }
  284. void vli_square(uint64_t *p_result, uint64_t *p_left)
  285. {
  286. uint128_t r01 = {0, 0};
  287. uint64_t r2 = 0;
  288. uint i, k;
  289. for(k=0; k < NUM_ECC_DIGITS*2 - 1; ++k)
  290. {
  291. uint l_min = (k < NUM_ECC_DIGITS ? 0 : (k + 1) - NUM_ECC_DIGITS);
  292. for(i=l_min; i<=k && i<=k-i; ++i)
  293. {
  294. uint128_t l_product = mul_64_64(p_left[i], p_left[k-i]);
  295. if(i < k-i)
  296. {
  297. r2 += l_product.m_high >> 63;
  298. l_product.m_high = (l_product.m_high << 1) | (l_product.m_low >> 63);
  299. l_product.m_low <<= 1;
  300. }
  301. r01 = add_128_128(r01, l_product);
  302. r2 += (r01.m_high < l_product.m_high);
  303. }
  304. p_result[k] = r01.m_low;
  305. r01.m_low = r01.m_high;
  306. r01.m_high = r2;
  307. r2 = 0;
  308. }
  309. p_result[NUM_ECC_DIGITS*2 - 1] = r01.m_low;
  310. }
  311. #endif /* SUPPORTS_INT128 */
  312. /* Computes p_result = (p_left + p_right) % p_mod.
  313. Assumes that p_left < p_mod and p_right < p_mod, p_result != p_mod. */
  314. void vli_modAdd(uint64_t *p_result, uint64_t *p_left, const uint64_t *p_right, const uint64_t *p_mod)
  315. {
  316. uint64_t l_carry = vli_add(p_result, p_left, p_right);
  317. if(l_carry || vli_cmp(p_result, p_mod) >= 0)
  318. { /* p_result > p_mod (p_result = p_mod + remainder), so subtract p_mod to get remainder. */
  319. vli_sub(p_result, p_result, p_mod);
  320. }
  321. }
  322. /* Computes p_result = (p_left - p_right) % p_mod.
  323. Assumes that p_left < p_mod and p_right < p_mod, p_result != p_mod. */
  324. void vli_modSub(uint64_t *p_result, uint64_t *p_left, const uint64_t *p_right, const uint64_t *p_mod)
  325. {
  326. uint64_t l_borrow = vli_sub(p_result, p_left, p_right);
  327. if(l_borrow)
  328. { /* In this case, p_result == -diff == (max int) - diff.
  329. Since -x % d == d - x, we can get the correct result from p_result + p_mod (with overflow). */
  330. vli_add(p_result, p_result, p_mod);
  331. }
  332. }
  333. void omega_mult(uint64_t *p_result, uint64_t *p_right)
  334. {
  335. uint64_t l_tmp[NUM_ECC_DIGITS];
  336. uint64_t l_carry, l_diff;
  337. /* Multiply by (2^128 + 2^96 - 2^32 + 1). */
  338. vli_set(p_result, p_right); /* 1 */
  339. l_carry = vli_lshift(l_tmp, p_right, 32);
  340. p_result[1 + NUM_ECC_DIGITS] = l_carry + vli_add(p_result + 1, p_result + 1, l_tmp); /* 2^96 + 1 */
  341. p_result[2 + NUM_ECC_DIGITS] = vli_add(p_result + 2, p_result + 2, p_right); /* 2^128 + 2^96 + 1 */
  342. l_carry += vli_sub(p_result, p_result, l_tmp); /* 2^128 + 2^96 - 2^32 + 1 */
  343. l_diff = p_result[NUM_ECC_DIGITS] - l_carry;
  344. if(l_diff > p_result[NUM_ECC_DIGITS])
  345. { /* Propagate borrow if necessary. */
  346. uint i;
  347. for(i = 1 + NUM_ECC_DIGITS; ; ++i)
  348. {
  349. --p_result[i];
  350. if(p_result[i] != (uint64_t)-1)
  351. {
  352. break;
  353. }
  354. }
  355. }
  356. p_result[NUM_ECC_DIGITS] = l_diff;
  357. }
  358. /* Computes p_result = p_product % curve_p
  359. see PDF "Comparing Elliptic Curve Cryptography and RSA on 8-bit CPUs"
  360. section "Curve-Specific Optimizations" */
  361. void vli_mmod_fast(uint64_t *p_result, uint64_t *p_product)
  362. {
  363. uint64_t l_tmp[2*NUM_ECC_DIGITS];
  364. while(!vli_isZero(p_product + NUM_ECC_DIGITS)) /* While c1 != 0 */
  365. {
  366. uint64_t l_carry = 0;
  367. uint i;
  368. vli_clear(l_tmp);
  369. vli_clear(l_tmp + NUM_ECC_DIGITS);
  370. omega_mult(l_tmp, p_product + NUM_ECC_DIGITS); /* tmp = w * c1 */
  371. vli_clear(p_product + NUM_ECC_DIGITS); /* p = c0 */
  372. /* (c1, c0) = c0 + w * c1 */
  373. for(i=0; i<NUM_ECC_DIGITS+3; ++i)
  374. {
  375. uint64_t l_sum = p_product[i] + l_tmp[i] + l_carry;
  376. if(l_sum != p_product[i])
  377. {
  378. l_carry = (l_sum < p_product[i]);
  379. }
  380. p_product[i] = l_sum;
  381. }
  382. }
  383. while(vli_cmp(p_product, curve_p) > 0)
  384. {
  385. vli_sub(p_product, p_product, curve_p);
  386. }
  387. vli_set(p_result, p_product);
  388. }
  389. /* Computes p_result = (p_left * p_right) % curve_p. */
  390. ZT_INLINE void vli_modMult_fast(uint64_t *p_result,uint64_t *p_left,const uint64_t *p_right)
  391. {
  392. uint64_t l_product[2 * NUM_ECC_DIGITS];
  393. vli_mult(l_product, p_left, p_right);
  394. vli_mmod_fast(p_result, l_product);
  395. }
  396. /* Computes p_result = p_left^2 % curve_p. */
  397. ZT_INLINE void vli_modSquare_fast(uint64_t *p_result,uint64_t *p_left)
  398. {
  399. uint64_t l_product[2 * NUM_ECC_DIGITS];
  400. vli_square(l_product, p_left);
  401. vli_mmod_fast(p_result, l_product);
  402. }
  403. #define EVEN(vli) (!(vli[0] & 1))
  404. /* Computes p_result = (1 / p_input) % p_mod. All VLIs are the same size.
  405. See "From Euclid's GCD to Montgomery Multiplication to the Great Divide"
  406. https://labs.oracle.com/techrep/2001/smli_tr-2001-95.pdf */
  407. void vli_modInv(uint64_t *p_result, uint64_t *p_input, const uint64_t *p_mod)
  408. {
  409. uint64_t a[NUM_ECC_DIGITS], b[NUM_ECC_DIGITS], u[NUM_ECC_DIGITS], v[NUM_ECC_DIGITS];
  410. uint64_t l_carry;
  411. int l_cmpResult;
  412. if(vli_isZero(p_input))
  413. {
  414. vli_clear(p_result);
  415. return;
  416. }
  417. vli_set(a, p_input);
  418. vli_set(b, p_mod);
  419. vli_clear(u);
  420. u[0] = 1;
  421. vli_clear(v);
  422. while((l_cmpResult = vli_cmp(a, b)) != 0)
  423. {
  424. l_carry = 0;
  425. if(EVEN(a))
  426. {
  427. vli_rshift1(a);
  428. if(!EVEN(u))
  429. {
  430. l_carry = vli_add(u, u, p_mod);
  431. }
  432. vli_rshift1(u);
  433. if(l_carry)
  434. {
  435. u[NUM_ECC_DIGITS-1] |= 0x8000000000000000ull;
  436. }
  437. }
  438. else if(EVEN(b))
  439. {
  440. vli_rshift1(b);
  441. if(!EVEN(v))
  442. {
  443. l_carry = vli_add(v, v, p_mod);
  444. }
  445. vli_rshift1(v);
  446. if(l_carry)
  447. {
  448. v[NUM_ECC_DIGITS-1] |= 0x8000000000000000ull;
  449. }
  450. }
  451. else if(l_cmpResult > 0)
  452. {
  453. vli_sub(a, a, b);
  454. vli_rshift1(a);
  455. if(vli_cmp(u, v) < 0)
  456. {
  457. vli_add(u, u, p_mod);
  458. }
  459. vli_sub(u, u, v);
  460. if(!EVEN(u))
  461. {
  462. l_carry = vli_add(u, u, p_mod);
  463. }
  464. vli_rshift1(u);
  465. if(l_carry)
  466. {
  467. u[NUM_ECC_DIGITS-1] |= 0x8000000000000000ull;
  468. }
  469. }
  470. else
  471. {
  472. vli_sub(b, b, a);
  473. vli_rshift1(b);
  474. if(vli_cmp(v, u) < 0)
  475. {
  476. vli_add(v, v, p_mod);
  477. }
  478. vli_sub(v, v, u);
  479. if(!EVEN(v))
  480. {
  481. l_carry = vli_add(v, v, p_mod);
  482. }
  483. vli_rshift1(v);
  484. if(l_carry)
  485. {
  486. v[NUM_ECC_DIGITS-1] |= 0x8000000000000000ull;
  487. }
  488. }
  489. }
  490. vli_set(p_result, u);
  491. }
  492. /* ------ Point operations ------ */
  493. /* Returns 1 if p_point is the point at infinity, 0 otherwise. */
  494. ZT_INLINE int EccPoint_isZero(EccPoint *p_point)
  495. {
  496. return (vli_isZero(p_point->x) && vli_isZero(p_point->y));
  497. }
  498. /* Point multiplication algorithm using Montgomery's ladder with co-Z coordinates.
  499. From http://eprint.iacr.org/2011/338.pdf
  500. */
  501. /* Double in place */
  502. void EccPoint_double_jacobian(uint64_t *X1, uint64_t *Y1, uint64_t *Z1)
  503. {
  504. /* t1 = X, t2 = Y, t3 = Z */
  505. uint64_t t4[NUM_ECC_DIGITS];
  506. uint64_t t5[NUM_ECC_DIGITS];
  507. if(vli_isZero(Z1))
  508. {
  509. return;
  510. }
  511. vli_modSquare_fast(t4, Y1); /* t4 = y1^2 */
  512. vli_modMult_fast(t5, X1, t4); /* t5 = x1*y1^2 = A */
  513. vli_modSquare_fast(t4, t4); /* t4 = y1^4 */
  514. vli_modMult_fast(Y1, Y1, Z1); /* t2 = y1*z1 = z3 */
  515. vli_modSquare_fast(Z1, Z1); /* t3 = z1^2 */
  516. vli_modAdd(X1, X1, Z1, curve_p); /* t1 = x1 + z1^2 */
  517. vli_modAdd(Z1, Z1, Z1, curve_p); /* t3 = 2*z1^2 */
  518. vli_modSub(Z1, X1, Z1, curve_p); /* t3 = x1 - z1^2 */
  519. vli_modMult_fast(X1, X1, Z1); /* t1 = x1^2 - z1^4 */
  520. vli_modAdd(Z1, X1, X1, curve_p); /* t3 = 2*(x1^2 - z1^4) */
  521. vli_modAdd(X1, X1, Z1, curve_p); /* t1 = 3*(x1^2 - z1^4) */
  522. if(vli_testBit(X1, 0))
  523. {
  524. uint64_t l_carry = vli_add(X1, X1, curve_p);
  525. vli_rshift1(X1);
  526. X1[NUM_ECC_DIGITS-1] |= l_carry << 63U;
  527. }
  528. else
  529. {
  530. vli_rshift1(X1);
  531. }
  532. /* t1 = 3/2*(x1^2 - z1^4) = B */
  533. vli_modSquare_fast(Z1, X1); /* t3 = B^2 */
  534. vli_modSub(Z1, Z1, t5, curve_p); /* t3 = B^2 - A */
  535. vli_modSub(Z1, Z1, t5, curve_p); /* t3 = B^2 - 2A = x3 */
  536. vli_modSub(t5, t5, Z1, curve_p); /* t5 = A - x3 */
  537. vli_modMult_fast(X1, X1, t5); /* t1 = B * (A - x3) */
  538. vli_modSub(t4, X1, t4, curve_p); /* t4 = B * (A - x3) - y1^4 = y3 */
  539. vli_set(X1, Z1);
  540. vli_set(Z1, Y1);
  541. vli_set(Y1, t4);
  542. }
  543. /* Modify (x1, y1) => (x1 * z^2, y1 * z^3) */
  544. void apply_z(uint64_t *X1, uint64_t *Y1, uint64_t *Z)
  545. {
  546. uint64_t t1[NUM_ECC_DIGITS];
  547. vli_modSquare_fast(t1, Z); /* z^2 */
  548. vli_modMult_fast(X1, X1, t1); /* x1 * z^2 */
  549. vli_modMult_fast(t1, t1, Z); /* z^3 */
  550. vli_modMult_fast(Y1, Y1, t1); /* y1 * z^3 */
  551. }
  552. /* P = (x1, y1) => 2P, (x2, y2) => P' */
  553. void XYcZ_initial_double(uint64_t *X1, uint64_t *Y1, uint64_t *X2, uint64_t *Y2, uint64_t *p_initialZ)
  554. {
  555. uint64_t z[NUM_ECC_DIGITS];
  556. vli_set(X2, X1);
  557. vli_set(Y2, Y1);
  558. vli_clear(z);
  559. z[0] = 1;
  560. if(p_initialZ)
  561. {
  562. vli_set(z, p_initialZ);
  563. }
  564. apply_z(X1, Y1, z);
  565. EccPoint_double_jacobian(X1, Y1, z);
  566. apply_z(X2, Y2, z);
  567. }
  568. /* Input P = (x1, y1, Z), Q = (x2, y2, Z)
  569. Output P' = (x1', y1', Z3), P + Q = (x3, y3, Z3)
  570. or P => P', Q => P + Q
  571. */
  572. void XYcZ_add(uint64_t *X1, uint64_t *Y1, uint64_t *X2, uint64_t *Y2)
  573. {
  574. /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
  575. uint64_t t5[NUM_ECC_DIGITS];
  576. vli_modSub(t5, X2, X1, curve_p); /* t5 = x2 - x1 */
  577. vli_modSquare_fast(t5, t5); /* t5 = (x2 - x1)^2 = A */
  578. vli_modMult_fast(X1, X1, t5); /* t1 = x1*A = B */
  579. vli_modMult_fast(X2, X2, t5); /* t3 = x2*A = C */
  580. vli_modSub(Y2, Y2, Y1, curve_p); /* t4 = y2 - y1 */
  581. vli_modSquare_fast(t5, Y2); /* t5 = (y2 - y1)^2 = D */
  582. vli_modSub(t5, t5, X1, curve_p); /* t5 = D - B */
  583. vli_modSub(t5, t5, X2, curve_p); /* t5 = D - B - C = x3 */
  584. vli_modSub(X2, X2, X1, curve_p); /* t3 = C - B */
  585. vli_modMult_fast(Y1, Y1, X2); /* t2 = y1*(C - B) */
  586. vli_modSub(X2, X1, t5, curve_p); /* t3 = B - x3 */
  587. vli_modMult_fast(Y2, Y2, X2); /* t4 = (y2 - y1)*(B - x3) */
  588. vli_modSub(Y2, Y2, Y1, curve_p); /* t4 = y3 */
  589. vli_set(X2, t5);
  590. }
  591. /* Input P = (x1, y1, Z), Q = (x2, y2, Z)
  592. Output P + Q = (x3, y3, Z3), P - Q = (x3', y3', Z3)
  593. or P => P - Q, Q => P + Q
  594. */
  595. void XYcZ_addC(uint64_t *X1, uint64_t *Y1, uint64_t *X2, uint64_t *Y2)
  596. {
  597. /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
  598. uint64_t t5[NUM_ECC_DIGITS];
  599. uint64_t t6[NUM_ECC_DIGITS];
  600. uint64_t t7[NUM_ECC_DIGITS];
  601. vli_modSub(t5, X2, X1, curve_p); /* t5 = x2 - x1 */
  602. vli_modSquare_fast(t5, t5); /* t5 = (x2 - x1)^2 = A */
  603. vli_modMult_fast(X1, X1, t5); /* t1 = x1*A = B */
  604. vli_modMult_fast(X2, X2, t5); /* t3 = x2*A = C */
  605. vli_modAdd(t5, Y2, Y1, curve_p); /* t4 = y2 + y1 */
  606. vli_modSub(Y2, Y2, Y1, curve_p); /* t4 = y2 - y1 */
  607. vli_modSub(t6, X2, X1, curve_p); /* t6 = C - B */
  608. vli_modMult_fast(Y1, Y1, t6); /* t2 = y1 * (C - B) */
  609. vli_modAdd(t6, X1, X2, curve_p); /* t6 = B + C */
  610. vli_modSquare_fast(X2, Y2); /* t3 = (y2 - y1)^2 */
  611. vli_modSub(X2, X2, t6, curve_p); /* t3 = x3 */
  612. vli_modSub(t7, X1, X2, curve_p); /* t7 = B - x3 */
  613. vli_modMult_fast(Y2, Y2, t7); /* t4 = (y2 - y1)*(B - x3) */
  614. vli_modSub(Y2, Y2, Y1, curve_p); /* t4 = y3 */
  615. vli_modSquare_fast(t7, t5); /* t7 = (y2 + y1)^2 = F */
  616. vli_modSub(t7, t7, t6, curve_p); /* t7 = x3' */
  617. vli_modSub(t6, t7, X1, curve_p); /* t6 = x3' - B */
  618. vli_modMult_fast(t6, t6, t5); /* t6 = (y2 + y1)*(x3' - B) */
  619. vli_modSub(Y1, t6, Y1, curve_p); /* t2 = y3' */
  620. vli_set(X1, t7);
  621. }
  622. void EccPoint_mult(EccPoint *p_result, const EccPoint *p_point, uint64_t *p_scalar, uint64_t *p_initialZ)
  623. {
  624. /* R0 and R1 */
  625. uint64_t Rx[2][NUM_ECC_DIGITS];
  626. uint64_t Ry[2][NUM_ECC_DIGITS];
  627. uint64_t z[NUM_ECC_DIGITS];
  628. int i, nb;
  629. vli_set(Rx[1], p_point->x);
  630. vli_set(Ry[1], p_point->y);
  631. XYcZ_initial_double(Rx[1], Ry[1], Rx[0], Ry[0], p_initialZ);
  632. for(i = (int)vli_numBits(p_scalar) - 2; i > 0; --i)
  633. {
  634. nb = !vli_testBit(p_scalar, i);
  635. XYcZ_addC(Rx[1-nb], Ry[1-nb], Rx[nb], Ry[nb]);
  636. XYcZ_add(Rx[nb], Ry[nb], Rx[1-nb], Ry[1-nb]);
  637. }
  638. nb = !vli_testBit(p_scalar, 0);
  639. XYcZ_addC(Rx[1-nb], Ry[1-nb], Rx[nb], Ry[nb]);
  640. /* Find final 1/Z value. */
  641. vli_modSub(z, Rx[1], Rx[0], curve_p); /* X1 - X0 */
  642. vli_modMult_fast(z, z, Ry[1-nb]); /* Yb * (X1 - X0) */
  643. vli_modMult_fast(z, z, p_point->x); /* xP * Yb * (X1 - X0) */
  644. vli_modInv(z, z, curve_p); /* 1 / (xP * Yb * (X1 - X0)) */
  645. vli_modMult_fast(z, z, p_point->y); /* yP / (xP * Yb * (X1 - X0)) */
  646. vli_modMult_fast(z, z, Rx[1-nb]); /* Xb * yP / (xP * Yb * (X1 - X0)) */
  647. /* End 1/Z calculation */
  648. XYcZ_add(Rx[nb], Ry[nb], Rx[1-nb], Ry[1-nb]);
  649. apply_z(Rx[0], Ry[0], z);
  650. vli_set(p_result->x, Rx[0]);
  651. vli_set(p_result->y, Ry[0]);
  652. }
  653. ZT_INLINE void ecc_bytes2native(uint64_t p_native[NUM_ECC_DIGITS],const uint8_t p_bytes[ECC_BYTES])
  654. {
  655. unsigned i;
  656. for(i=0; i<NUM_ECC_DIGITS; ++i)
  657. {
  658. const uint8_t *p_digit = p_bytes + 8 * (NUM_ECC_DIGITS - 1 - i);
  659. p_native[i] = ((uint64_t)p_digit[0] << 56) | ((uint64_t)p_digit[1] << 48) | ((uint64_t)p_digit[2] << 40) | ((uint64_t)p_digit[3] << 32) |
  660. ((uint64_t)p_digit[4] << 24) | ((uint64_t)p_digit[5] << 16) | ((uint64_t)p_digit[6] << 8) | (uint64_t)p_digit[7];
  661. }
  662. }
  663. ZT_INLINE void ecc_native2bytes(uint8_t p_bytes[ECC_BYTES],const uint64_t p_native[NUM_ECC_DIGITS])
  664. {
  665. unsigned i;
  666. for(i=0; i<NUM_ECC_DIGITS; ++i)
  667. {
  668. uint8_t *p_digit = p_bytes + 8 * (NUM_ECC_DIGITS - 1 - i);
  669. p_digit[0] = p_native[i] >> 56;
  670. p_digit[1] = p_native[i] >> 48;
  671. p_digit[2] = p_native[i] >> 40;
  672. p_digit[3] = p_native[i] >> 32;
  673. p_digit[4] = p_native[i] >> 24;
  674. p_digit[5] = p_native[i] >> 16;
  675. p_digit[6] = p_native[i] >> 8;
  676. p_digit[7] = p_native[i];
  677. }
  678. }
  679. /* Compute a = sqrt(a) (mod curve_p). */
  680. void mod_sqrt(uint64_t a[NUM_ECC_DIGITS])
  681. {
  682. unsigned i;
  683. uint64_t p1[NUM_ECC_DIGITS] = {1};
  684. uint64_t l_result[NUM_ECC_DIGITS] = {1};
  685. /* Since curve_p == 3 (mod 4) for all supported curves, we can
  686. compute sqrt(a) = a^((curve_p + 1) / 4) (mod curve_p). */
  687. vli_add(p1, curve_p, p1); /* p1 = curve_p + 1 */
  688. for(i = vli_numBits(p1) - 1; i > 1; --i)
  689. {
  690. vli_modSquare_fast(l_result, l_result);
  691. if(vli_testBit(p1, i))
  692. {
  693. vli_modMult_fast(l_result, l_result, a);
  694. }
  695. }
  696. vli_set(a, l_result);
  697. }
  698. void ecc_point_decompress(EccPoint *p_point, const uint8_t p_compressed[ECC_BYTES+1])
  699. {
  700. uint64_t _3[NUM_ECC_DIGITS] = {3}; /* -a = 3 */
  701. ecc_bytes2native(p_point->x, p_compressed+1);
  702. vli_modSquare_fast(p_point->y, p_point->x); /* y = x^2 */
  703. vli_modSub(p_point->y, p_point->y, _3, curve_p); /* y = x^2 - 3 */
  704. vli_modMult_fast(p_point->y, p_point->y, p_point->x); /* y = x^3 - 3x */
  705. vli_modAdd(p_point->y, p_point->y, curve_b, curve_p); /* y = x^3 - 3x + b */
  706. mod_sqrt(p_point->y);
  707. if((p_point->y[0] & 0x01) != (p_compressed[0] & 0x01))
  708. {
  709. vli_sub(p_point->y, curve_p, p_point->y);
  710. }
  711. }
  712. ZT_INLINE int ecc_make_key(uint8_t p_publicKey[ECC_BYTES + 1],uint8_t p_privateKey[ECC_BYTES])
  713. {
  714. uint64_t l_private[NUM_ECC_DIGITS];
  715. EccPoint l_public;
  716. unsigned l_tries = 0;
  717. do
  718. {
  719. if(!getRandomNumber(l_private) || (l_tries++ >= MAX_TRIES))
  720. {
  721. return 0;
  722. }
  723. if(vli_isZero(l_private))
  724. {
  725. continue;
  726. }
  727. /* Make sure the private key is in the range [1, n-1].
  728. For the supported curves, n is always large enough that we only need to subtract once at most. */
  729. if(vli_cmp(curve_n, l_private) != 1)
  730. {
  731. vli_sub(l_private, l_private, curve_n);
  732. }
  733. EccPoint_mult(&l_public, &curve_G, l_private, NULL);
  734. } while(EccPoint_isZero(&l_public));
  735. ecc_native2bytes(p_privateKey, l_private);
  736. ecc_native2bytes(p_publicKey + 1, l_public.x);
  737. p_publicKey[0] = 2 + (l_public.y[0] & 0x01);
  738. return 1;
  739. }
  740. ZT_INLINE int ecdh_shared_secret(const uint8_t p_publicKey[ECC_BYTES + 1],const uint8_t p_privateKey[ECC_BYTES],uint8_t p_secret[ECC_BYTES])
  741. {
  742. EccPoint l_public;
  743. uint64_t l_private[NUM_ECC_DIGITS];
  744. uint64_t l_random[NUM_ECC_DIGITS];
  745. if(!getRandomNumber(l_random))
  746. {
  747. return 0;
  748. }
  749. ecc_point_decompress(&l_public, p_publicKey);
  750. ecc_bytes2native(l_private, p_privateKey);
  751. EccPoint l_product;
  752. EccPoint_mult(&l_product, &l_public, l_private, l_random);
  753. ecc_native2bytes(p_secret, l_product.x);
  754. return !EccPoint_isZero(&l_product);
  755. }
  756. /* -------- ECDSA code -------- */
  757. /* Computes p_result = (p_left * p_right) % p_mod. */
  758. void vli_modMult(uint64_t *p_result, uint64_t *p_left, uint64_t *p_right, const uint64_t *p_mod)
  759. {
  760. uint64_t l_product[2 * NUM_ECC_DIGITS];
  761. uint64_t l_modMultiple[2 * NUM_ECC_DIGITS];
  762. uint l_digitShift, l_bitShift;
  763. uint l_productBits;
  764. uint l_modBits = vli_numBits(p_mod);
  765. vli_mult(l_product, p_left, p_right);
  766. l_productBits = vli_numBits(l_product + NUM_ECC_DIGITS);
  767. if(l_productBits)
  768. {
  769. l_productBits += NUM_ECC_DIGITS * 64;
  770. }
  771. else
  772. {
  773. l_productBits = vli_numBits(l_product);
  774. }
  775. if(l_productBits < l_modBits)
  776. { /* l_product < p_mod. */
  777. vli_set(p_result, l_product);
  778. return;
  779. }
  780. /* Shift p_mod by (l_leftBits - l_modBits). This multiplies p_mod by the largest
  781. power of two possible while still resulting in a number less than p_left. */
  782. vli_clear(l_modMultiple);
  783. vli_clear(l_modMultiple + NUM_ECC_DIGITS);
  784. l_digitShift = (l_productBits - l_modBits) / 64;
  785. l_bitShift = (l_productBits - l_modBits) % 64;
  786. if(l_bitShift)
  787. {
  788. l_modMultiple[l_digitShift + NUM_ECC_DIGITS] = vli_lshift(l_modMultiple + l_digitShift, p_mod, l_bitShift);
  789. }
  790. else
  791. {
  792. vli_set(l_modMultiple + l_digitShift, p_mod);
  793. }
  794. /* Subtract all multiples of p_mod to get the remainder. */
  795. vli_clear(p_result);
  796. p_result[0] = 1; /* Use p_result as a temp var to store 1 (for subtraction) */
  797. while(l_productBits > NUM_ECC_DIGITS * 64 || vli_cmp(l_modMultiple, p_mod) >= 0)
  798. {
  799. int l_cmp = vli_cmp(l_modMultiple + NUM_ECC_DIGITS, l_product + NUM_ECC_DIGITS);
  800. if(l_cmp < 0 || (l_cmp == 0 && vli_cmp(l_modMultiple, l_product) <= 0))
  801. {
  802. if(vli_sub(l_product, l_product, l_modMultiple))
  803. { /* borrow */
  804. vli_sub(l_product + NUM_ECC_DIGITS, l_product + NUM_ECC_DIGITS, p_result);
  805. }
  806. vli_sub(l_product + NUM_ECC_DIGITS, l_product + NUM_ECC_DIGITS, l_modMultiple + NUM_ECC_DIGITS);
  807. }
  808. uint64_t l_carry = (l_modMultiple[NUM_ECC_DIGITS] & 0x01) << 63;
  809. vli_rshift1(l_modMultiple + NUM_ECC_DIGITS);
  810. vli_rshift1(l_modMultiple);
  811. l_modMultiple[NUM_ECC_DIGITS-1] |= l_carry;
  812. --l_productBits;
  813. }
  814. vli_set(p_result, l_product);
  815. }
  816. ZT_INLINE uint umax(uint a,uint b)
  817. {
  818. return (a > b ? a : b);
  819. }
  820. ZT_INLINE int ecdsa_sign(const uint8_t p_privateKey[ECC_BYTES],const uint8_t p_hash[ECC_BYTES],uint8_t p_signature[ECC_BYTES * 2])
  821. {
  822. uint64_t k[NUM_ECC_DIGITS];
  823. uint64_t l_tmp[NUM_ECC_DIGITS];
  824. uint64_t l_s[NUM_ECC_DIGITS];
  825. EccPoint p;
  826. unsigned l_tries = 0;
  827. do
  828. {
  829. if(!getRandomNumber(k) || (l_tries++ >= MAX_TRIES))
  830. {
  831. return 0;
  832. }
  833. if(vli_isZero(k))
  834. {
  835. continue;
  836. }
  837. if(vli_cmp(curve_n, k) != 1)
  838. {
  839. vli_sub(k, k, curve_n);
  840. }
  841. /* tmp = k * G */
  842. EccPoint_mult(&p, &curve_G, k, NULL);
  843. /* r = x1 (mod n) */
  844. if(vli_cmp(curve_n, p.x) != 1)
  845. {
  846. vli_sub(p.x, p.x, curve_n);
  847. }
  848. } while(vli_isZero(p.x));
  849. ecc_native2bytes(p_signature, p.x);
  850. ecc_bytes2native(l_tmp, p_privateKey);
  851. vli_modMult(l_s, p.x, l_tmp, curve_n); /* s = r*d */
  852. ecc_bytes2native(l_tmp, p_hash);
  853. vli_modAdd(l_s, l_tmp, l_s, curve_n); /* s = e + r*d */
  854. vli_modInv(k, k, curve_n); /* k = 1 / k */
  855. vli_modMult(l_s, l_s, k, curve_n); /* s = (e + r*d) / k */
  856. ecc_native2bytes(p_signature + ECC_BYTES, l_s);
  857. return 1;
  858. }
  859. ZT_INLINE int ecdsa_verify(const uint8_t p_publicKey[ECC_BYTES + 1],const uint8_t p_hash[ECC_BYTES],const uint8_t p_signature[ECC_BYTES * 2])
  860. {
  861. uint64_t u1[NUM_ECC_DIGITS], u2[NUM_ECC_DIGITS];
  862. uint64_t z[NUM_ECC_DIGITS];
  863. EccPoint l_public, l_sum;
  864. uint64_t rx[NUM_ECC_DIGITS];
  865. uint64_t ry[NUM_ECC_DIGITS];
  866. uint64_t tx[NUM_ECC_DIGITS];
  867. uint64_t ty[NUM_ECC_DIGITS];
  868. uint64_t tz[NUM_ECC_DIGITS];
  869. uint64_t l_r[NUM_ECC_DIGITS], l_s[NUM_ECC_DIGITS];
  870. ecc_point_decompress(&l_public, p_publicKey);
  871. ecc_bytes2native(l_r, p_signature);
  872. ecc_bytes2native(l_s, p_signature + ECC_BYTES);
  873. if(vli_isZero(l_r) || vli_isZero(l_s))
  874. { /* r, s must not be 0. */
  875. return 0;
  876. }
  877. if(vli_cmp(curve_n, l_r) != 1 || vli_cmp(curve_n, l_s) != 1)
  878. { /* r, s must be < n. */
  879. return 0;
  880. }
  881. /* Calculate u1 and u2. */
  882. vli_modInv(z, l_s, curve_n); /* Z = s^-1 */
  883. ecc_bytes2native(u1, p_hash);
  884. vli_modMult(u1, u1, z, curve_n); /* u1 = e/s */
  885. vli_modMult(u2, l_r, z, curve_n); /* u2 = r/s */
  886. /* Calculate l_sum = G + Q. */
  887. vli_set(l_sum.x, l_public.x);
  888. vli_set(l_sum.y, l_public.y);
  889. vli_set(tx, curve_G.x);
  890. vli_set(ty, curve_G.y);
  891. vli_modSub(z, l_sum.x, tx, curve_p); /* Z = x2 - x1 */
  892. XYcZ_add(tx, ty, l_sum.x, l_sum.y);
  893. vli_modInv(z, z, curve_p); /* Z = 1/Z */
  894. apply_z(l_sum.x, l_sum.y, z);
  895. /* Use Shamir's trick to calculate u1*G + u2*Q */
  896. const EccPoint *l_points[4] = {NULL, &curve_G, &l_public, &l_sum};
  897. uint l_numBits = umax(vli_numBits(u1), vli_numBits(u2));
  898. const EccPoint *l_point = l_points[(!!vli_testBit(u1, l_numBits-1)) | ((!!vli_testBit(u2, l_numBits-1)) << 1)];
  899. vli_set(rx, l_point->x);
  900. vli_set(ry, l_point->y);
  901. vli_clear(z);
  902. z[0] = 1;
  903. int i;
  904. for(i = l_numBits - 2; i >= 0; --i)
  905. {
  906. EccPoint_double_jacobian(rx, ry, z);
  907. int l_index = (!!vli_testBit(u1, i)) | ((!!vli_testBit(u2, i)) << 1);
  908. const EccPoint *l_point = l_points[l_index];
  909. if(l_point)
  910. {
  911. vli_set(tx, l_point->x);
  912. vli_set(ty, l_point->y);
  913. apply_z(tx, ty, z);
  914. vli_modSub(tz, rx, tx, curve_p); /* Z = x2 - x1 */
  915. XYcZ_add(tx, ty, rx, ry);
  916. vli_modMult_fast(z, z, tz);
  917. }
  918. }
  919. vli_modInv(z, z, curve_p); /* Z = 1/Z */
  920. apply_z(rx, ry, z);
  921. /* v = x1 (mod n) */
  922. if(vli_cmp(curve_n, rx) != 1)
  923. {
  924. vli_sub(rx, rx, curve_n);
  925. }
  926. /* Accept only if v == r. */
  927. return (vli_cmp(rx, l_r) == 0);
  928. }
  929. } // anonymous namespace
  930. void ECC384GenerateKey(uint8_t pub[ZT_ECC384_PUBLIC_KEY_SIZE],uint8_t priv[ZT_ECC384_PRIVATE_KEY_SIZE])
  931. {
  932. if (!ecc_make_key(pub,priv)) {
  933. fprintf(stderr,"FATAL: ecdsa_make_key() failed!" ZT_EOL_S);
  934. abort();
  935. }
  936. }
  937. void ECC384ECDSASign(const uint8_t priv[ZT_ECC384_PRIVATE_KEY_SIZE],const uint8_t hash[ZT_ECC384_SIGNATURE_HASH_SIZE],uint8_t sig[ZT_ECC384_SIGNATURE_SIZE])
  938. {
  939. if (!ecdsa_sign(priv,hash,sig)) {
  940. fprintf(stderr,"FATAL: ecdsa_sign() failed!" ZT_EOL_S);
  941. abort();
  942. }
  943. }
  944. bool ECC384ECDSAVerify(const uint8_t pub[ZT_ECC384_PUBLIC_KEY_SIZE],const uint8_t hash[ZT_ECC384_SIGNATURE_HASH_SIZE],const uint8_t sig[ZT_ECC384_SIGNATURE_SIZE])
  945. {
  946. return (ecdsa_verify(pub,hash,sig) != 0);
  947. }
  948. bool ECC384ECDH(const uint8_t theirPub[ZT_ECC384_PUBLIC_KEY_SIZE],const uint8_t ourPriv[ZT_ECC384_PRIVATE_KEY_SIZE],uint8_t secret[ZT_ECC384_SHARED_SECRET_SIZE])
  949. {
  950. return (ecdh_shared_secret(theirPub,ourPriv,secret) != 0);
  951. }
  952. } // namespace ZeroTier