ECC384.cpp 30 KB

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