ECC384.cpp 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2019 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <stdint.h>
  29. #include <string.h>
  30. #include "Constants.hpp"
  31. #include "ECC384.hpp"
  32. #include "Utils.hpp"
  33. namespace ZeroTier {
  34. namespace {
  35. //////////////////////////////////////////////////////////////////////////////
  36. // This is EASY-ECC by Kenneth MacKay
  37. // https://github.com/esxgx/easy-ecc
  38. // This code is under the BSD 2-clause license, not ZeroTier's license
  39. //////////////////////////////////////////////////////////////////////////////
  40. //////////////////////////////////////////////////////////////////////////////
  41. // ecc.h from easy-ecc
  42. //////////////////////////////////////////////////////////////////////////////
  43. #define secp128r1 16
  44. #define secp192r1 24
  45. #define secp256r1 32
  46. #define secp384r1 48
  47. //#ifndef ECC_CURVE
  48. // #define ECC_CURVE secp256r1
  49. //#endif
  50. #define ECC_CURVE secp384r1
  51. //#if (ECC_CURVE != secp128r1 && ECC_CURVE != secp192r1 && ECC_CURVE != secp256r1 && ECC_CURVE != secp384r1)
  52. // #error "Must define ECC_CURVE to one of the available curves"
  53. //#endif
  54. #define ECC_BYTES ECC_CURVE
  55. //////////////////////////////////////////////////////////////////////////////
  56. // ecc.c from easy-ecc
  57. //////////////////////////////////////////////////////////////////////////////
  58. //#include "ecc.h"
  59. //#include <string.h>
  60. #define NUM_ECC_DIGITS (ECC_BYTES/8)
  61. #define MAX_TRIES 16
  62. typedef unsigned int uint;
  63. #if defined(__SIZEOF_INT128__) || ((__clang_major__ * 100 + __clang_minor__) >= 302)
  64. #define SUPPORTS_INT128 1
  65. #else
  66. #define SUPPORTS_INT128 0
  67. #endif
  68. #if SUPPORTS_INT128
  69. typedef unsigned __int128 uint128_t;
  70. #else
  71. typedef struct
  72. {
  73. uint64_t m_low;
  74. uint64_t m_high;
  75. } uint128_t;
  76. #endif
  77. typedef struct EccPoint
  78. {
  79. uint64_t x[NUM_ECC_DIGITS];
  80. uint64_t y[NUM_ECC_DIGITS];
  81. } EccPoint;
  82. #define CONCAT1(a, b) a##b
  83. #define CONCAT(a, b) CONCAT1(a, b)
  84. #define Curve_P_16 {0xFFFFFFFFFFFFFFFF, 0xFFFFFFFDFFFFFFFF}
  85. #define Curve_P_24 {0xFFFFFFFFFFFFFFFFull, 0xFFFFFFFFFFFFFFFEull, 0xFFFFFFFFFFFFFFFFull}
  86. #define Curve_P_32 {0xFFFFFFFFFFFFFFFFull, 0x00000000FFFFFFFFull, 0x0000000000000000ull, 0xFFFFFFFF00000001ull}
  87. #define Curve_P_48 {0x00000000FFFFFFFF, 0xFFFFFFFF00000000, 0xFFFFFFFFFFFFFFFE, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF}
  88. #define Curve_B_16 {0xD824993C2CEE5ED3, 0xE87579C11079F43D}
  89. #define Curve_B_24 {0xFEB8DEECC146B9B1ull, 0x0FA7E9AB72243049ull, 0x64210519E59C80E7ull}
  90. #define Curve_B_32 {0x3BCE3C3E27D2604Bull, 0x651D06B0CC53B0F6ull, 0xB3EBBD55769886BCull, 0x5AC635D8AA3A93E7ull}
  91. #define Curve_B_48 {0x2A85C8EDD3EC2AEF, 0xC656398D8A2ED19D, 0x0314088F5013875A, 0x181D9C6EFE814112, 0x988E056BE3F82D19, 0xB3312FA7E23EE7E4}
  92. #define Curve_G_16 { \
  93. {0x0C28607CA52C5B86, 0x161FF7528B899B2D}, \
  94. {0xC02DA292DDED7A83, 0xCF5AC8395BAFEB13}}
  95. #define Curve_G_24 { \
  96. {0xF4FF0AFD82FF1012ull, 0x7CBF20EB43A18800ull, 0x188DA80EB03090F6ull}, \
  97. {0x73F977A11E794811ull, 0x631011ED6B24CDD5ull, 0x07192B95FFC8DA78ull}}
  98. #define Curve_G_32 { \
  99. {0xF4A13945D898C296ull, 0x77037D812DEB33A0ull, 0xF8BCE6E563A440F2ull, 0x6B17D1F2E12C4247ull}, \
  100. {0xCBB6406837BF51F5ull, 0x2BCE33576B315ECEull, 0x8EE7EB4A7C0F9E16ull, 0x4FE342E2FE1A7F9Bull}}
  101. #define Curve_G_48 { \
  102. {0x3A545E3872760AB7, 0x5502F25DBF55296C, 0x59F741E082542A38, 0x6E1D3B628BA79B98, 0x8EB1C71EF320AD74, 0xAA87CA22BE8B0537}, \
  103. {0x7A431D7C90EA0E5F, 0x0A60B1CE1D7E819D, 0xE9DA3113B5F0B8C0, 0xF8F41DBD289A147C, 0x5D9E98BF9292DC29, 0x3617DE4A96262C6F}}
  104. #define Curve_N_16 {0x75A30D1B9038A115, 0xFFFFFFFE00000000}
  105. #define Curve_N_24 {0x146BC9B1B4D22831ull, 0xFFFFFFFF99DEF836ull, 0xFFFFFFFFFFFFFFFFull}
  106. #define Curve_N_32 {0xF3B9CAC2FC632551ull, 0xBCE6FAADA7179E84ull, 0xFFFFFFFFFFFFFFFFull, 0xFFFFFFFF00000000ull}
  107. #define Curve_N_48 {0xECEC196ACCC52973, 0x581A0DB248B0A77A, 0xC7634D81F4372DDF, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF}
  108. static uint64_t curve_p[NUM_ECC_DIGITS] = CONCAT(Curve_P_, ECC_CURVE);
  109. static uint64_t curve_b[NUM_ECC_DIGITS] = CONCAT(Curve_B_, ECC_CURVE);
  110. static EccPoint curve_G = CONCAT(Curve_G_, ECC_CURVE);
  111. static uint64_t curve_n[NUM_ECC_DIGITS] = CONCAT(Curve_N_, ECC_CURVE);
  112. #if 0
  113. #if (defined(_WIN32) || defined(_WIN64))
  114. /* Windows */
  115. #define WIN32_LEAN_AND_MEAN
  116. #include <windows.h>
  117. #include <wincrypt.h>
  118. static int getRandomNumber(uint64_t *p_vli)
  119. {
  120. HCRYPTPROV l_prov;
  121. if(!CryptAcquireContext(&l_prov, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
  122. {
  123. return 0;
  124. }
  125. CryptGenRandom(l_prov, ECC_BYTES, (BYTE *)p_vli);
  126. CryptReleaseContext(l_prov, 0);
  127. return 1;
  128. }
  129. #else /* _WIN32 */
  130. /* Assume that we are using a POSIX-like system with /dev/urandom or /dev/random. */
  131. #include <sys/types.h>
  132. #include <fcntl.h>
  133. #include <unistd.h>
  134. #ifndef O_CLOEXEC
  135. #define O_CLOEXEC 0
  136. #endif
  137. static int getRandomNumber(uint64_t *p_vli)
  138. {
  139. int l_fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC);
  140. if(l_fd == -1)
  141. {
  142. l_fd = open("/dev/random", O_RDONLY | O_CLOEXEC);
  143. if(l_fd == -1)
  144. {
  145. return 0;
  146. }
  147. }
  148. char *l_ptr = (char *)p_vli;
  149. size_t l_left = ECC_BYTES;
  150. while(l_left > 0)
  151. {
  152. int l_read = read(l_fd, l_ptr, l_left);
  153. if(l_read <= 0)
  154. { // read failed
  155. close(l_fd);
  156. return 0;
  157. }
  158. l_left -= l_read;
  159. l_ptr += l_read;
  160. }
  161. close(l_fd);
  162. return 1;
  163. }
  164. #endif /* _WIN32 */
  165. #endif
  166. // Use ZeroTier's secure PRNG
  167. static inline int getRandomNumber(uint64_t *p_vli)
  168. {
  169. Utils::getSecureRandom(p_vli,ECC_BYTES);
  170. return 1;
  171. }
  172. static inline void vli_clear(uint64_t *p_vli)
  173. {
  174. uint i;
  175. for(i=0; i<NUM_ECC_DIGITS; ++i)
  176. {
  177. p_vli[i] = 0;
  178. }
  179. }
  180. /* Returns 1 if p_vli == 0, 0 otherwise. */
  181. static inline int vli_isZero(uint64_t *p_vli)
  182. {
  183. uint i;
  184. for(i = 0; i < NUM_ECC_DIGITS; ++i)
  185. {
  186. if(p_vli[i])
  187. {
  188. return 0;
  189. }
  190. }
  191. return 1;
  192. }
  193. /* Returns nonzero if bit p_bit of p_vli is set. */
  194. static inline uint64_t vli_testBit(uint64_t *p_vli, uint p_bit)
  195. {
  196. return (p_vli[p_bit/64] & ((uint64_t)1 << (p_bit % 64)));
  197. }
  198. /* Counts the number of 64-bit "digits" in p_vli. */
  199. static inline uint vli_numDigits(uint64_t *p_vli)
  200. {
  201. int i;
  202. /* Search from the end until we find a non-zero digit.
  203. We do it in reverse because we expect that most digits will be nonzero. */
  204. for(i = NUM_ECC_DIGITS - 1; i >= 0 && p_vli[i] == 0; --i)
  205. {
  206. }
  207. return (i + 1);
  208. }
  209. /* Counts the number of bits required for p_vli. */
  210. static inline uint vli_numBits(uint64_t *p_vli)
  211. {
  212. uint i;
  213. uint64_t l_digit;
  214. uint l_numDigits = vli_numDigits(p_vli);
  215. if(l_numDigits == 0)
  216. {
  217. return 0;
  218. }
  219. l_digit = p_vli[l_numDigits - 1];
  220. for(i=0; l_digit; ++i)
  221. {
  222. l_digit >>= 1;
  223. }
  224. return ((l_numDigits - 1) * 64 + i);
  225. }
  226. /* Sets p_dest = p_src. */
  227. static inline void vli_set(uint64_t *p_dest, uint64_t *p_src)
  228. {
  229. uint i;
  230. for(i=0; i<NUM_ECC_DIGITS; ++i)
  231. {
  232. p_dest[i] = p_src[i];
  233. }
  234. }
  235. /* Returns sign of p_left - p_right. */
  236. static inline int vli_cmp(uint64_t *p_left, uint64_t *p_right)
  237. {
  238. int i;
  239. for(i = NUM_ECC_DIGITS-1; i >= 0; --i)
  240. {
  241. if(p_left[i] > p_right[i])
  242. {
  243. return 1;
  244. }
  245. else if(p_left[i] < p_right[i])
  246. {
  247. return -1;
  248. }
  249. }
  250. return 0;
  251. }
  252. /* Computes p_result = p_in << c, returning carry. Can modify in place (if p_result == p_in). 0 < p_shift < 64. */
  253. static inline uint64_t vli_lshift(uint64_t *p_result, uint64_t *p_in, uint p_shift)
  254. {
  255. uint64_t l_carry = 0;
  256. uint i;
  257. for(i = 0; i < NUM_ECC_DIGITS; ++i)
  258. {
  259. uint64_t l_temp = p_in[i];
  260. p_result[i] = (l_temp << p_shift) | l_carry;
  261. l_carry = l_temp >> (64 - p_shift);
  262. }
  263. return l_carry;
  264. }
  265. /* Computes p_vli = p_vli >> 1. */
  266. static inline void vli_rshift1(uint64_t *p_vli)
  267. {
  268. uint64_t *l_end = p_vli;
  269. uint64_t l_carry = 0;
  270. p_vli += NUM_ECC_DIGITS;
  271. while(p_vli-- > l_end)
  272. {
  273. uint64_t l_temp = *p_vli;
  274. *p_vli = (l_temp >> 1) | l_carry;
  275. l_carry = l_temp << 63;
  276. }
  277. }
  278. /* Computes p_result = p_left + p_right, returning carry. Can modify in place. */
  279. static inline uint64_t vli_add(uint64_t *p_result, uint64_t *p_left, uint64_t *p_right)
  280. {
  281. uint64_t l_carry = 0;
  282. uint i;
  283. for(i=0; i<NUM_ECC_DIGITS; ++i)
  284. {
  285. uint64_t l_sum = p_left[i] + p_right[i] + l_carry;
  286. if(l_sum != p_left[i])
  287. {
  288. l_carry = (l_sum < p_left[i]);
  289. }
  290. p_result[i] = l_sum;
  291. }
  292. return l_carry;
  293. }
  294. /* Computes p_result = p_left - p_right, returning borrow. Can modify in place. */
  295. static inline uint64_t vli_sub(uint64_t *p_result, uint64_t *p_left, uint64_t *p_right)
  296. {
  297. uint64_t l_borrow = 0;
  298. uint i;
  299. for(i=0; i<NUM_ECC_DIGITS; ++i)
  300. {
  301. uint64_t l_diff = p_left[i] - p_right[i] - l_borrow;
  302. if(l_diff != p_left[i])
  303. {
  304. l_borrow = (l_diff > p_left[i]);
  305. }
  306. p_result[i] = l_diff;
  307. }
  308. return l_borrow;
  309. }
  310. #if SUPPORTS_INT128
  311. /* Computes p_result = p_left * p_right. */
  312. static inline void vli_mult(uint64_t *p_result, uint64_t *p_left, uint64_t *p_right)
  313. {
  314. uint128_t r01 = 0;
  315. uint64_t r2 = 0;
  316. uint i, k;
  317. /* Compute each digit of p_result in sequence, maintaining the carries. */
  318. for(k=0; k < NUM_ECC_DIGITS*2 - 1; ++k)
  319. {
  320. uint l_min = (k < NUM_ECC_DIGITS ? 0 : (k + 1) - NUM_ECC_DIGITS);
  321. for(i=l_min; i<=k && i<NUM_ECC_DIGITS; ++i)
  322. {
  323. uint128_t l_product = (uint128_t)p_left[i] * p_right[k-i];
  324. r01 += l_product;
  325. r2 += (r01 < l_product);
  326. }
  327. p_result[k] = (uint64_t)r01;
  328. r01 = (r01 >> 64) | (((uint128_t)r2) << 64);
  329. r2 = 0;
  330. }
  331. p_result[NUM_ECC_DIGITS*2 - 1] = (uint64_t)r01;
  332. }
  333. /* Computes p_result = p_left^2. */
  334. static inline void vli_square(uint64_t *p_result, uint64_t *p_left)
  335. {
  336. uint128_t r01 = 0;
  337. uint64_t r2 = 0;
  338. uint i, k;
  339. for(k=0; k < NUM_ECC_DIGITS*2 - 1; ++k)
  340. {
  341. uint l_min = (k < NUM_ECC_DIGITS ? 0 : (k + 1) - NUM_ECC_DIGITS);
  342. for(i=l_min; i<=k && i<=k-i; ++i)
  343. {
  344. uint128_t l_product = (uint128_t)p_left[i] * p_left[k-i];
  345. if(i < k-i)
  346. {
  347. r2 += l_product >> 127;
  348. l_product *= 2;
  349. }
  350. r01 += l_product;
  351. r2 += (r01 < l_product);
  352. }
  353. p_result[k] = (uint64_t)r01;
  354. r01 = (r01 >> 64) | (((uint128_t)r2) << 64);
  355. r2 = 0;
  356. }
  357. p_result[NUM_ECC_DIGITS*2 - 1] = (uint64_t)r01;
  358. }
  359. #else /* #if SUPPORTS_INT128 */
  360. static inline uint128_t mul_64_64(uint64_t p_left, uint64_t p_right)
  361. {
  362. uint128_t l_result;
  363. uint64_t a0 = p_left & 0xffffffffull;
  364. uint64_t a1 = p_left >> 32;
  365. uint64_t b0 = p_right & 0xffffffffull;
  366. uint64_t b1 = p_right >> 32;
  367. uint64_t m0 = a0 * b0;
  368. uint64_t m1 = a0 * b1;
  369. uint64_t m2 = a1 * b0;
  370. uint64_t m3 = a1 * b1;
  371. m2 += (m0 >> 32);
  372. m2 += m1;
  373. if(m2 < m1)
  374. { // overflow
  375. m3 += 0x100000000ull;
  376. }
  377. l_result.m_low = (m0 & 0xffffffffull) | (m2 << 32);
  378. l_result.m_high = m3 + (m2 >> 32);
  379. return l_result;
  380. }
  381. static inline uint128_t add_128_128(uint128_t a, uint128_t b)
  382. {
  383. uint128_t l_result;
  384. l_result.m_low = a.m_low + b.m_low;
  385. l_result.m_high = a.m_high + b.m_high + (l_result.m_low < a.m_low);
  386. return l_result;
  387. }
  388. static inline void vli_mult(uint64_t *p_result, uint64_t *p_left, uint64_t *p_right)
  389. {
  390. uint128_t r01 = {0, 0};
  391. uint64_t r2 = 0;
  392. uint i, k;
  393. /* Compute each digit of p_result in sequence, maintaining the carries. */
  394. for(k=0; k < NUM_ECC_DIGITS*2 - 1; ++k)
  395. {
  396. uint l_min = (k < NUM_ECC_DIGITS ? 0 : (k + 1) - NUM_ECC_DIGITS);
  397. for(i=l_min; i<=k && i<NUM_ECC_DIGITS; ++i)
  398. {
  399. uint128_t l_product = mul_64_64(p_left[i], p_right[k-i]);
  400. r01 = add_128_128(r01, l_product);
  401. r2 += (r01.m_high < l_product.m_high);
  402. }
  403. p_result[k] = r01.m_low;
  404. r01.m_low = r01.m_high;
  405. r01.m_high = r2;
  406. r2 = 0;
  407. }
  408. p_result[NUM_ECC_DIGITS*2 - 1] = r01.m_low;
  409. }
  410. static inline void vli_square(uint64_t *p_result, uint64_t *p_left)
  411. {
  412. uint128_t r01 = {0, 0};
  413. uint64_t r2 = 0;
  414. uint i, k;
  415. for(k=0; k < NUM_ECC_DIGITS*2 - 1; ++k)
  416. {
  417. uint l_min = (k < NUM_ECC_DIGITS ? 0 : (k + 1) - NUM_ECC_DIGITS);
  418. for(i=l_min; i<=k && i<=k-i; ++i)
  419. {
  420. uint128_t l_product = mul_64_64(p_left[i], p_left[k-i]);
  421. if(i < k-i)
  422. {
  423. r2 += l_product.m_high >> 63;
  424. l_product.m_high = (l_product.m_high << 1) | (l_product.m_low >> 63);
  425. l_product.m_low <<= 1;
  426. }
  427. r01 = add_128_128(r01, l_product);
  428. r2 += (r01.m_high < l_product.m_high);
  429. }
  430. p_result[k] = r01.m_low;
  431. r01.m_low = r01.m_high;
  432. r01.m_high = r2;
  433. r2 = 0;
  434. }
  435. p_result[NUM_ECC_DIGITS*2 - 1] = r01.m_low;
  436. }
  437. #endif /* SUPPORTS_INT128 */
  438. /* Computes p_result = (p_left + p_right) % p_mod.
  439. Assumes that p_left < p_mod and p_right < p_mod, p_result != p_mod. */
  440. static inline void vli_modAdd(uint64_t *p_result, uint64_t *p_left, uint64_t *p_right, uint64_t *p_mod)
  441. {
  442. uint64_t l_carry = vli_add(p_result, p_left, p_right);
  443. if(l_carry || vli_cmp(p_result, p_mod) >= 0)
  444. { /* p_result > p_mod (p_result = p_mod + remainder), so subtract p_mod to get remainder. */
  445. vli_sub(p_result, p_result, p_mod);
  446. }
  447. }
  448. /* Computes p_result = (p_left - p_right) % p_mod.
  449. Assumes that p_left < p_mod and p_right < p_mod, p_result != p_mod. */
  450. static inline void vli_modSub(uint64_t *p_result, uint64_t *p_left, uint64_t *p_right, uint64_t *p_mod)
  451. {
  452. uint64_t l_borrow = vli_sub(p_result, p_left, p_right);
  453. if(l_borrow)
  454. { /* In this case, p_result == -diff == (max int) - diff.
  455. Since -x % d == d - x, we can get the correct result from p_result + p_mod (with overflow). */
  456. vli_add(p_result, p_result, p_mod);
  457. }
  458. }
  459. #if ECC_CURVE == secp128r1
  460. /* Computes p_result = p_product % curve_p.
  461. See algorithm 5 and 6 from http://www.isys.uni-klu.ac.at/PDF/2001-0126-MT.pdf */
  462. static void vli_mmod_fast(uint64_t *p_result, uint64_t *p_product)
  463. {
  464. uint64_t l_tmp[NUM_ECC_DIGITS];
  465. int l_carry;
  466. vli_set(p_result, p_product);
  467. l_tmp[0] = p_product[2];
  468. l_tmp[1] = (p_product[3] & 0x1FFFFFFFFull) | (p_product[2] << 33);
  469. l_carry = vli_add(p_result, p_result, l_tmp);
  470. l_tmp[0] = (p_product[2] >> 31) | (p_product[3] << 33);
  471. l_tmp[1] = (p_product[3] >> 31) | ((p_product[2] & 0xFFFFFFFF80000000ull) << 2);
  472. l_carry += vli_add(p_result, p_result, l_tmp);
  473. l_tmp[0] = (p_product[2] >> 62) | (p_product[3] << 2);
  474. l_tmp[1] = (p_product[3] >> 62) | ((p_product[2] & 0xC000000000000000ull) >> 29) | (p_product[3] << 35);
  475. l_carry += vli_add(p_result, p_result, l_tmp);
  476. l_tmp[0] = (p_product[3] >> 29);
  477. l_tmp[1] = ((p_product[3] & 0xFFFFFFFFE0000000ull) << 4);
  478. l_carry += vli_add(p_result, p_result, l_tmp);
  479. l_tmp[0] = (p_product[3] >> 60);
  480. l_tmp[1] = (p_product[3] & 0xFFFFFFFE00000000ull);
  481. l_carry += vli_add(p_result, p_result, l_tmp);
  482. l_tmp[0] = 0;
  483. l_tmp[1] = ((p_product[3] & 0xF000000000000000ull) >> 27);
  484. l_carry += vli_add(p_result, p_result, l_tmp);
  485. while(l_carry || vli_cmp(curve_p, p_result) != 1)
  486. {
  487. l_carry -= vli_sub(p_result, p_result, curve_p);
  488. }
  489. }
  490. #elif ECC_CURVE == secp192r1
  491. /* Computes p_result = p_product % curve_p.
  492. See algorithm 5 and 6 from http://www.isys.uni-klu.ac.at/PDF/2001-0126-MT.pdf */
  493. static void vli_mmod_fast(uint64_t *p_result, uint64_t *p_product)
  494. {
  495. uint64_t l_tmp[NUM_ECC_DIGITS];
  496. int l_carry;
  497. vli_set(p_result, p_product);
  498. vli_set(l_tmp, &p_product[3]);
  499. l_carry = vli_add(p_result, p_result, l_tmp);
  500. l_tmp[0] = 0;
  501. l_tmp[1] = p_product[3];
  502. l_tmp[2] = p_product[4];
  503. l_carry += vli_add(p_result, p_result, l_tmp);
  504. l_tmp[0] = l_tmp[1] = p_product[5];
  505. l_tmp[2] = 0;
  506. l_carry += vli_add(p_result, p_result, l_tmp);
  507. while(l_carry || vli_cmp(curve_p, p_result) != 1)
  508. {
  509. l_carry -= vli_sub(p_result, p_result, curve_p);
  510. }
  511. }
  512. #elif ECC_CURVE == secp256r1
  513. /* Computes p_result = p_product % curve_p
  514. from http://www.nsa.gov/ia/_files/nist-routines.pdf */
  515. static void vli_mmod_fast(uint64_t *p_result, uint64_t *p_product)
  516. {
  517. uint64_t l_tmp[NUM_ECC_DIGITS];
  518. int l_carry;
  519. /* t */
  520. vli_set(p_result, p_product);
  521. /* s1 */
  522. l_tmp[0] = 0;
  523. l_tmp[1] = p_product[5] & 0xffffffff00000000ull;
  524. l_tmp[2] = p_product[6];
  525. l_tmp[3] = p_product[7];
  526. l_carry = vli_lshift(l_tmp, l_tmp, 1);
  527. l_carry += vli_add(p_result, p_result, l_tmp);
  528. /* s2 */
  529. l_tmp[1] = p_product[6] << 32;
  530. l_tmp[2] = (p_product[6] >> 32) | (p_product[7] << 32);
  531. l_tmp[3] = p_product[7] >> 32;
  532. l_carry += vli_lshift(l_tmp, l_tmp, 1);
  533. l_carry += vli_add(p_result, p_result, l_tmp);
  534. /* s3 */
  535. l_tmp[0] = p_product[4];
  536. l_tmp[1] = p_product[5] & 0xffffffff;
  537. l_tmp[2] = 0;
  538. l_tmp[3] = p_product[7];
  539. l_carry += vli_add(p_result, p_result, l_tmp);
  540. /* s4 */
  541. l_tmp[0] = (p_product[4] >> 32) | (p_product[5] << 32);
  542. l_tmp[1] = (p_product[5] >> 32) | (p_product[6] & 0xffffffff00000000ull);
  543. l_tmp[2] = p_product[7];
  544. l_tmp[3] = (p_product[6] >> 32) | (p_product[4] << 32);
  545. l_carry += vli_add(p_result, p_result, l_tmp);
  546. /* d1 */
  547. l_tmp[0] = (p_product[5] >> 32) | (p_product[6] << 32);
  548. l_tmp[1] = (p_product[6] >> 32);
  549. l_tmp[2] = 0;
  550. l_tmp[3] = (p_product[4] & 0xffffffff) | (p_product[5] << 32);
  551. l_carry -= vli_sub(p_result, p_result, l_tmp);
  552. /* d2 */
  553. l_tmp[0] = p_product[6];
  554. l_tmp[1] = p_product[7];
  555. l_tmp[2] = 0;
  556. l_tmp[3] = (p_product[4] >> 32) | (p_product[5] & 0xffffffff00000000ull);
  557. l_carry -= vli_sub(p_result, p_result, l_tmp);
  558. /* d3 */
  559. l_tmp[0] = (p_product[6] >> 32) | (p_product[7] << 32);
  560. l_tmp[1] = (p_product[7] >> 32) | (p_product[4] << 32);
  561. l_tmp[2] = (p_product[4] >> 32) | (p_product[5] << 32);
  562. l_tmp[3] = (p_product[6] << 32);
  563. l_carry -= vli_sub(p_result, p_result, l_tmp);
  564. /* d4 */
  565. l_tmp[0] = p_product[7];
  566. l_tmp[1] = p_product[4] & 0xffffffff00000000ull;
  567. l_tmp[2] = p_product[5];
  568. l_tmp[3] = p_product[6] & 0xffffffff00000000ull;
  569. l_carry -= vli_sub(p_result, p_result, l_tmp);
  570. if(l_carry < 0)
  571. {
  572. do
  573. {
  574. l_carry += vli_add(p_result, p_result, curve_p);
  575. } while(l_carry < 0);
  576. }
  577. else
  578. {
  579. while(l_carry || vli_cmp(curve_p, p_result) != 1)
  580. {
  581. l_carry -= vli_sub(p_result, p_result, curve_p);
  582. }
  583. }
  584. }
  585. #elif ECC_CURVE == secp384r1
  586. static inline void omega_mult(uint64_t *p_result, uint64_t *p_right)
  587. {
  588. uint64_t l_tmp[NUM_ECC_DIGITS];
  589. uint64_t l_carry, l_diff;
  590. /* Multiply by (2^128 + 2^96 - 2^32 + 1). */
  591. vli_set(p_result, p_right); /* 1 */
  592. l_carry = vli_lshift(l_tmp, p_right, 32);
  593. p_result[1 + NUM_ECC_DIGITS] = l_carry + vli_add(p_result + 1, p_result + 1, l_tmp); /* 2^96 + 1 */
  594. p_result[2 + NUM_ECC_DIGITS] = vli_add(p_result + 2, p_result + 2, p_right); /* 2^128 + 2^96 + 1 */
  595. l_carry += vli_sub(p_result, p_result, l_tmp); /* 2^128 + 2^96 - 2^32 + 1 */
  596. l_diff = p_result[NUM_ECC_DIGITS] - l_carry;
  597. if(l_diff > p_result[NUM_ECC_DIGITS])
  598. { /* Propagate borrow if necessary. */
  599. uint i;
  600. for(i = 1 + NUM_ECC_DIGITS; ; ++i)
  601. {
  602. --p_result[i];
  603. if(p_result[i] != (uint64_t)-1)
  604. {
  605. break;
  606. }
  607. }
  608. }
  609. p_result[NUM_ECC_DIGITS] = l_diff;
  610. }
  611. /* Computes p_result = p_product % curve_p
  612. see PDF "Comparing Elliptic Curve Cryptography and RSA on 8-bit CPUs"
  613. section "Curve-Specific Optimizations" */
  614. static inline void vli_mmod_fast(uint64_t *p_result, uint64_t *p_product)
  615. {
  616. uint64_t l_tmp[2*NUM_ECC_DIGITS];
  617. while(!vli_isZero(p_product + NUM_ECC_DIGITS)) /* While c1 != 0 */
  618. {
  619. uint64_t l_carry = 0;
  620. uint i;
  621. vli_clear(l_tmp);
  622. vli_clear(l_tmp + NUM_ECC_DIGITS);
  623. omega_mult(l_tmp, p_product + NUM_ECC_DIGITS); /* tmp = w * c1 */
  624. vli_clear(p_product + NUM_ECC_DIGITS); /* p = c0 */
  625. /* (c1, c0) = c0 + w * c1 */
  626. for(i=0; i<NUM_ECC_DIGITS+3; ++i)
  627. {
  628. uint64_t l_sum = p_product[i] + l_tmp[i] + l_carry;
  629. if(l_sum != p_product[i])
  630. {
  631. l_carry = (l_sum < p_product[i]);
  632. }
  633. p_product[i] = l_sum;
  634. }
  635. }
  636. while(vli_cmp(p_product, curve_p) > 0)
  637. {
  638. vli_sub(p_product, p_product, curve_p);
  639. }
  640. vli_set(p_result, p_product);
  641. }
  642. #endif
  643. /* Computes p_result = (p_left * p_right) % curve_p. */
  644. static inline void vli_modMult_fast(uint64_t *p_result, uint64_t *p_left, uint64_t *p_right)
  645. {
  646. uint64_t l_product[2 * NUM_ECC_DIGITS];
  647. vli_mult(l_product, p_left, p_right);
  648. vli_mmod_fast(p_result, l_product);
  649. }
  650. /* Computes p_result = p_left^2 % curve_p. */
  651. static inline void vli_modSquare_fast(uint64_t *p_result, uint64_t *p_left)
  652. {
  653. uint64_t l_product[2 * NUM_ECC_DIGITS];
  654. vli_square(l_product, p_left);
  655. vli_mmod_fast(p_result, l_product);
  656. }
  657. #define EVEN(vli) (!(vli[0] & 1))
  658. /* Computes p_result = (1 / p_input) % p_mod. All VLIs are the same size.
  659. See "From Euclid's GCD to Montgomery Multiplication to the Great Divide"
  660. https://labs.oracle.com/techrep/2001/smli_tr-2001-95.pdf */
  661. static inline void vli_modInv(uint64_t *p_result, uint64_t *p_input, uint64_t *p_mod)
  662. {
  663. uint64_t a[NUM_ECC_DIGITS], b[NUM_ECC_DIGITS], u[NUM_ECC_DIGITS], v[NUM_ECC_DIGITS];
  664. uint64_t l_carry;
  665. int l_cmpResult;
  666. if(vli_isZero(p_input))
  667. {
  668. vli_clear(p_result);
  669. return;
  670. }
  671. vli_set(a, p_input);
  672. vli_set(b, p_mod);
  673. vli_clear(u);
  674. u[0] = 1;
  675. vli_clear(v);
  676. while((l_cmpResult = vli_cmp(a, b)) != 0)
  677. {
  678. l_carry = 0;
  679. if(EVEN(a))
  680. {
  681. vli_rshift1(a);
  682. if(!EVEN(u))
  683. {
  684. l_carry = vli_add(u, u, p_mod);
  685. }
  686. vli_rshift1(u);
  687. if(l_carry)
  688. {
  689. u[NUM_ECC_DIGITS-1] |= 0x8000000000000000ull;
  690. }
  691. }
  692. else if(EVEN(b))
  693. {
  694. vli_rshift1(b);
  695. if(!EVEN(v))
  696. {
  697. l_carry = vli_add(v, v, p_mod);
  698. }
  699. vli_rshift1(v);
  700. if(l_carry)
  701. {
  702. v[NUM_ECC_DIGITS-1] |= 0x8000000000000000ull;
  703. }
  704. }
  705. else if(l_cmpResult > 0)
  706. {
  707. vli_sub(a, a, b);
  708. vli_rshift1(a);
  709. if(vli_cmp(u, v) < 0)
  710. {
  711. vli_add(u, u, p_mod);
  712. }
  713. vli_sub(u, u, v);
  714. if(!EVEN(u))
  715. {
  716. l_carry = vli_add(u, u, p_mod);
  717. }
  718. vli_rshift1(u);
  719. if(l_carry)
  720. {
  721. u[NUM_ECC_DIGITS-1] |= 0x8000000000000000ull;
  722. }
  723. }
  724. else
  725. {
  726. vli_sub(b, b, a);
  727. vli_rshift1(b);
  728. if(vli_cmp(v, u) < 0)
  729. {
  730. vli_add(v, v, p_mod);
  731. }
  732. vli_sub(v, v, u);
  733. if(!EVEN(v))
  734. {
  735. l_carry = vli_add(v, v, p_mod);
  736. }
  737. vli_rshift1(v);
  738. if(l_carry)
  739. {
  740. v[NUM_ECC_DIGITS-1] |= 0x8000000000000000ull;
  741. }
  742. }
  743. }
  744. vli_set(p_result, u);
  745. }
  746. /* ------ Point operations ------ */
  747. /* Returns 1 if p_point is the point at infinity, 0 otherwise. */
  748. static inline int EccPoint_isZero(EccPoint *p_point)
  749. {
  750. return (vli_isZero(p_point->x) && vli_isZero(p_point->y));
  751. }
  752. /* Point multiplication algorithm using Montgomery's ladder with co-Z coordinates.
  753. From http://eprint.iacr.org/2011/338.pdf
  754. */
  755. /* Double in place */
  756. static inline void EccPoint_double_jacobian(uint64_t *X1, uint64_t *Y1, uint64_t *Z1)
  757. {
  758. /* t1 = X, t2 = Y, t3 = Z */
  759. uint64_t t4[NUM_ECC_DIGITS];
  760. uint64_t t5[NUM_ECC_DIGITS];
  761. if(vli_isZero(Z1))
  762. {
  763. return;
  764. }
  765. vli_modSquare_fast(t4, Y1); /* t4 = y1^2 */
  766. vli_modMult_fast(t5, X1, t4); /* t5 = x1*y1^2 = A */
  767. vli_modSquare_fast(t4, t4); /* t4 = y1^4 */
  768. vli_modMult_fast(Y1, Y1, Z1); /* t2 = y1*z1 = z3 */
  769. vli_modSquare_fast(Z1, Z1); /* t3 = z1^2 */
  770. vli_modAdd(X1, X1, Z1, curve_p); /* t1 = x1 + z1^2 */
  771. vli_modAdd(Z1, Z1, Z1, curve_p); /* t3 = 2*z1^2 */
  772. vli_modSub(Z1, X1, Z1, curve_p); /* t3 = x1 - z1^2 */
  773. vli_modMult_fast(X1, X1, Z1); /* t1 = x1^2 - z1^4 */
  774. vli_modAdd(Z1, X1, X1, curve_p); /* t3 = 2*(x1^2 - z1^4) */
  775. vli_modAdd(X1, X1, Z1, curve_p); /* t1 = 3*(x1^2 - z1^4) */
  776. if(vli_testBit(X1, 0))
  777. {
  778. uint64_t l_carry = vli_add(X1, X1, curve_p);
  779. vli_rshift1(X1);
  780. X1[NUM_ECC_DIGITS-1] |= l_carry << 63;
  781. }
  782. else
  783. {
  784. vli_rshift1(X1);
  785. }
  786. /* t1 = 3/2*(x1^2 - z1^4) = B */
  787. vli_modSquare_fast(Z1, X1); /* t3 = B^2 */
  788. vli_modSub(Z1, Z1, t5, curve_p); /* t3 = B^2 - A */
  789. vli_modSub(Z1, Z1, t5, curve_p); /* t3 = B^2 - 2A = x3 */
  790. vli_modSub(t5, t5, Z1, curve_p); /* t5 = A - x3 */
  791. vli_modMult_fast(X1, X1, t5); /* t1 = B * (A - x3) */
  792. vli_modSub(t4, X1, t4, curve_p); /* t4 = B * (A - x3) - y1^4 = y3 */
  793. vli_set(X1, Z1);
  794. vli_set(Z1, Y1);
  795. vli_set(Y1, t4);
  796. }
  797. /* Modify (x1, y1) => (x1 * z^2, y1 * z^3) */
  798. static inline void apply_z(uint64_t *X1, uint64_t *Y1, uint64_t *Z)
  799. {
  800. uint64_t t1[NUM_ECC_DIGITS];
  801. vli_modSquare_fast(t1, Z); /* z^2 */
  802. vli_modMult_fast(X1, X1, t1); /* x1 * z^2 */
  803. vli_modMult_fast(t1, t1, Z); /* z^3 */
  804. vli_modMult_fast(Y1, Y1, t1); /* y1 * z^3 */
  805. }
  806. /* P = (x1, y1) => 2P, (x2, y2) => P' */
  807. static inline void XYcZ_initial_double(uint64_t *X1, uint64_t *Y1, uint64_t *X2, uint64_t *Y2, uint64_t *p_initialZ)
  808. {
  809. uint64_t z[NUM_ECC_DIGITS];
  810. vli_set(X2, X1);
  811. vli_set(Y2, Y1);
  812. vli_clear(z);
  813. z[0] = 1;
  814. if(p_initialZ)
  815. {
  816. vli_set(z, p_initialZ);
  817. }
  818. apply_z(X1, Y1, z);
  819. EccPoint_double_jacobian(X1, Y1, z);
  820. apply_z(X2, Y2, z);
  821. }
  822. /* Input P = (x1, y1, Z), Q = (x2, y2, Z)
  823. Output P' = (x1', y1', Z3), P + Q = (x3, y3, Z3)
  824. or P => P', Q => P + Q
  825. */
  826. static inline void XYcZ_add(uint64_t *X1, uint64_t *Y1, uint64_t *X2, uint64_t *Y2)
  827. {
  828. /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
  829. uint64_t t5[NUM_ECC_DIGITS];
  830. vli_modSub(t5, X2, X1, curve_p); /* t5 = x2 - x1 */
  831. vli_modSquare_fast(t5, t5); /* t5 = (x2 - x1)^2 = A */
  832. vli_modMult_fast(X1, X1, t5); /* t1 = x1*A = B */
  833. vli_modMult_fast(X2, X2, t5); /* t3 = x2*A = C */
  834. vli_modSub(Y2, Y2, Y1, curve_p); /* t4 = y2 - y1 */
  835. vli_modSquare_fast(t5, Y2); /* t5 = (y2 - y1)^2 = D */
  836. vli_modSub(t5, t5, X1, curve_p); /* t5 = D - B */
  837. vli_modSub(t5, t5, X2, curve_p); /* t5 = D - B - C = x3 */
  838. vli_modSub(X2, X2, X1, curve_p); /* t3 = C - B */
  839. vli_modMult_fast(Y1, Y1, X2); /* t2 = y1*(C - B) */
  840. vli_modSub(X2, X1, t5, curve_p); /* t3 = B - x3 */
  841. vli_modMult_fast(Y2, Y2, X2); /* t4 = (y2 - y1)*(B - x3) */
  842. vli_modSub(Y2, Y2, Y1, curve_p); /* t4 = y3 */
  843. vli_set(X2, t5);
  844. }
  845. /* Input P = (x1, y1, Z), Q = (x2, y2, Z)
  846. Output P + Q = (x3, y3, Z3), P - Q = (x3', y3', Z3)
  847. or P => P - Q, Q => P + Q
  848. */
  849. static inline void XYcZ_addC(uint64_t *X1, uint64_t *Y1, uint64_t *X2, uint64_t *Y2)
  850. {
  851. /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
  852. uint64_t t5[NUM_ECC_DIGITS];
  853. uint64_t t6[NUM_ECC_DIGITS];
  854. uint64_t t7[NUM_ECC_DIGITS];
  855. vli_modSub(t5, X2, X1, curve_p); /* t5 = x2 - x1 */
  856. vli_modSquare_fast(t5, t5); /* t5 = (x2 - x1)^2 = A */
  857. vli_modMult_fast(X1, X1, t5); /* t1 = x1*A = B */
  858. vli_modMult_fast(X2, X2, t5); /* t3 = x2*A = C */
  859. vli_modAdd(t5, Y2, Y1, curve_p); /* t4 = y2 + y1 */
  860. vli_modSub(Y2, Y2, Y1, curve_p); /* t4 = y2 - y1 */
  861. vli_modSub(t6, X2, X1, curve_p); /* t6 = C - B */
  862. vli_modMult_fast(Y1, Y1, t6); /* t2 = y1 * (C - B) */
  863. vli_modAdd(t6, X1, X2, curve_p); /* t6 = B + C */
  864. vli_modSquare_fast(X2, Y2); /* t3 = (y2 - y1)^2 */
  865. vli_modSub(X2, X2, t6, curve_p); /* t3 = x3 */
  866. vli_modSub(t7, X1, X2, curve_p); /* t7 = B - x3 */
  867. vli_modMult_fast(Y2, Y2, t7); /* t4 = (y2 - y1)*(B - x3) */
  868. vli_modSub(Y2, Y2, Y1, curve_p); /* t4 = y3 */
  869. vli_modSquare_fast(t7, t5); /* t7 = (y2 + y1)^2 = F */
  870. vli_modSub(t7, t7, t6, curve_p); /* t7 = x3' */
  871. vli_modSub(t6, t7, X1, curve_p); /* t6 = x3' - B */
  872. vli_modMult_fast(t6, t6, t5); /* t6 = (y2 + y1)*(x3' - B) */
  873. vli_modSub(Y1, t6, Y1, curve_p); /* t2 = y3' */
  874. vli_set(X1, t7);
  875. }
  876. static inline void EccPoint_mult(EccPoint *p_result, EccPoint *p_point, uint64_t *p_scalar, uint64_t *p_initialZ)
  877. {
  878. /* R0 and R1 */
  879. uint64_t Rx[2][NUM_ECC_DIGITS];
  880. uint64_t Ry[2][NUM_ECC_DIGITS];
  881. uint64_t z[NUM_ECC_DIGITS];
  882. int i, nb;
  883. vli_set(Rx[1], p_point->x);
  884. vli_set(Ry[1], p_point->y);
  885. XYcZ_initial_double(Rx[1], Ry[1], Rx[0], Ry[0], p_initialZ);
  886. for(i = vli_numBits(p_scalar) - 2; i > 0; --i)
  887. {
  888. nb = !vli_testBit(p_scalar, i);
  889. XYcZ_addC(Rx[1-nb], Ry[1-nb], Rx[nb], Ry[nb]);
  890. XYcZ_add(Rx[nb], Ry[nb], Rx[1-nb], Ry[1-nb]);
  891. }
  892. nb = !vli_testBit(p_scalar, 0);
  893. XYcZ_addC(Rx[1-nb], Ry[1-nb], Rx[nb], Ry[nb]);
  894. /* Find final 1/Z value. */
  895. vli_modSub(z, Rx[1], Rx[0], curve_p); /* X1 - X0 */
  896. vli_modMult_fast(z, z, Ry[1-nb]); /* Yb * (X1 - X0) */
  897. vli_modMult_fast(z, z, p_point->x); /* xP * Yb * (X1 - X0) */
  898. vli_modInv(z, z, curve_p); /* 1 / (xP * Yb * (X1 - X0)) */
  899. vli_modMult_fast(z, z, p_point->y); /* yP / (xP * Yb * (X1 - X0)) */
  900. vli_modMult_fast(z, z, Rx[1-nb]); /* Xb * yP / (xP * Yb * (X1 - X0)) */
  901. /* End 1/Z calculation */
  902. XYcZ_add(Rx[nb], Ry[nb], Rx[1-nb], Ry[1-nb]);
  903. apply_z(Rx[0], Ry[0], z);
  904. vli_set(p_result->x, Rx[0]);
  905. vli_set(p_result->y, Ry[0]);
  906. }
  907. static inline void ecc_bytes2native(uint64_t p_native[NUM_ECC_DIGITS], const uint8_t p_bytes[ECC_BYTES])
  908. {
  909. unsigned i;
  910. for(i=0; i<NUM_ECC_DIGITS; ++i)
  911. {
  912. const uint8_t *p_digit = p_bytes + 8 * (NUM_ECC_DIGITS - 1 - i);
  913. 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) |
  914. ((uint64_t)p_digit[4] << 24) | ((uint64_t)p_digit[5] << 16) | ((uint64_t)p_digit[6] << 8) | (uint64_t)p_digit[7];
  915. }
  916. }
  917. static inline void ecc_native2bytes(uint8_t p_bytes[ECC_BYTES], const uint64_t p_native[NUM_ECC_DIGITS])
  918. {
  919. unsigned i;
  920. for(i=0; i<NUM_ECC_DIGITS; ++i)
  921. {
  922. uint8_t *p_digit = p_bytes + 8 * (NUM_ECC_DIGITS - 1 - i);
  923. p_digit[0] = p_native[i] >> 56;
  924. p_digit[1] = p_native[i] >> 48;
  925. p_digit[2] = p_native[i] >> 40;
  926. p_digit[3] = p_native[i] >> 32;
  927. p_digit[4] = p_native[i] >> 24;
  928. p_digit[5] = p_native[i] >> 16;
  929. p_digit[6] = p_native[i] >> 8;
  930. p_digit[7] = p_native[i];
  931. }
  932. }
  933. /* Compute a = sqrt(a) (mod curve_p). */
  934. static inline void mod_sqrt(uint64_t a[NUM_ECC_DIGITS])
  935. {
  936. unsigned i;
  937. uint64_t p1[NUM_ECC_DIGITS] = {1};
  938. uint64_t l_result[NUM_ECC_DIGITS] = {1};
  939. /* Since curve_p == 3 (mod 4) for all supported curves, we can
  940. compute sqrt(a) = a^((curve_p + 1) / 4) (mod curve_p). */
  941. vli_add(p1, curve_p, p1); /* p1 = curve_p + 1 */
  942. for(i = vli_numBits(p1) - 1; i > 1; --i)
  943. {
  944. vli_modSquare_fast(l_result, l_result);
  945. if(vli_testBit(p1, i))
  946. {
  947. vli_modMult_fast(l_result, l_result, a);
  948. }
  949. }
  950. vli_set(a, l_result);
  951. }
  952. static inline void ecc_point_decompress(EccPoint *p_point, const uint8_t p_compressed[ECC_BYTES+1])
  953. {
  954. uint64_t _3[NUM_ECC_DIGITS] = {3}; /* -a = 3 */
  955. ecc_bytes2native(p_point->x, p_compressed+1);
  956. vli_modSquare_fast(p_point->y, p_point->x); /* y = x^2 */
  957. vli_modSub(p_point->y, p_point->y, _3, curve_p); /* y = x^2 - 3 */
  958. vli_modMult_fast(p_point->y, p_point->y, p_point->x); /* y = x^3 - 3x */
  959. vli_modAdd(p_point->y, p_point->y, curve_b, curve_p); /* y = x^3 - 3x + b */
  960. mod_sqrt(p_point->y);
  961. if((p_point->y[0] & 0x01) != (p_compressed[0] & 0x01))
  962. {
  963. vli_sub(p_point->y, curve_p, p_point->y);
  964. }
  965. }
  966. static inline int ecc_make_key(uint8_t p_publicKey[ECC_BYTES+1], uint8_t p_privateKey[ECC_BYTES])
  967. {
  968. uint64_t l_private[NUM_ECC_DIGITS];
  969. EccPoint l_public;
  970. unsigned l_tries = 0;
  971. do
  972. {
  973. if(!getRandomNumber(l_private) || (l_tries++ >= MAX_TRIES))
  974. {
  975. return 0;
  976. }
  977. if(vli_isZero(l_private))
  978. {
  979. continue;
  980. }
  981. /* Make sure the private key is in the range [1, n-1].
  982. For the supported curves, n is always large enough that we only need to subtract once at most. */
  983. if(vli_cmp(curve_n, l_private) != 1)
  984. {
  985. vli_sub(l_private, l_private, curve_n);
  986. }
  987. EccPoint_mult(&l_public, &curve_G, l_private, NULL);
  988. } while(EccPoint_isZero(&l_public));
  989. ecc_native2bytes(p_privateKey, l_private);
  990. ecc_native2bytes(p_publicKey + 1, l_public.x);
  991. p_publicKey[0] = 2 + (l_public.y[0] & 0x01);
  992. return 1;
  993. }
  994. 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])
  995. {
  996. EccPoint l_public;
  997. uint64_t l_private[NUM_ECC_DIGITS];
  998. uint64_t l_random[NUM_ECC_DIGITS];
  999. if(!getRandomNumber(l_random))
  1000. {
  1001. return 0;
  1002. }
  1003. ecc_point_decompress(&l_public, p_publicKey);
  1004. ecc_bytes2native(l_private, p_privateKey);
  1005. EccPoint l_product;
  1006. EccPoint_mult(&l_product, &l_public, l_private, l_random);
  1007. ecc_native2bytes(p_secret, l_product.x);
  1008. return !EccPoint_isZero(&l_product);
  1009. }
  1010. /* -------- ECDSA code -------- */
  1011. /* Computes p_result = (p_left * p_right) % p_mod. */
  1012. static inline void vli_modMult(uint64_t *p_result, uint64_t *p_left, uint64_t *p_right, uint64_t *p_mod)
  1013. {
  1014. uint64_t l_product[2 * NUM_ECC_DIGITS];
  1015. uint64_t l_modMultiple[2 * NUM_ECC_DIGITS];
  1016. uint l_digitShift, l_bitShift;
  1017. uint l_productBits;
  1018. uint l_modBits = vli_numBits(p_mod);
  1019. vli_mult(l_product, p_left, p_right);
  1020. l_productBits = vli_numBits(l_product + NUM_ECC_DIGITS);
  1021. if(l_productBits)
  1022. {
  1023. l_productBits += NUM_ECC_DIGITS * 64;
  1024. }
  1025. else
  1026. {
  1027. l_productBits = vli_numBits(l_product);
  1028. }
  1029. if(l_productBits < l_modBits)
  1030. { /* l_product < p_mod. */
  1031. vli_set(p_result, l_product);
  1032. return;
  1033. }
  1034. /* Shift p_mod by (l_leftBits - l_modBits). This multiplies p_mod by the largest
  1035. power of two possible while still resulting in a number less than p_left. */
  1036. vli_clear(l_modMultiple);
  1037. vli_clear(l_modMultiple + NUM_ECC_DIGITS);
  1038. l_digitShift = (l_productBits - l_modBits) / 64;
  1039. l_bitShift = (l_productBits - l_modBits) % 64;
  1040. if(l_bitShift)
  1041. {
  1042. l_modMultiple[l_digitShift + NUM_ECC_DIGITS] = vli_lshift(l_modMultiple + l_digitShift, p_mod, l_bitShift);
  1043. }
  1044. else
  1045. {
  1046. vli_set(l_modMultiple + l_digitShift, p_mod);
  1047. }
  1048. /* Subtract all multiples of p_mod to get the remainder. */
  1049. vli_clear(p_result);
  1050. p_result[0] = 1; /* Use p_result as a temp var to store 1 (for subtraction) */
  1051. while(l_productBits > NUM_ECC_DIGITS * 64 || vli_cmp(l_modMultiple, p_mod) >= 0)
  1052. {
  1053. int l_cmp = vli_cmp(l_modMultiple + NUM_ECC_DIGITS, l_product + NUM_ECC_DIGITS);
  1054. if(l_cmp < 0 || (l_cmp == 0 && vli_cmp(l_modMultiple, l_product) <= 0))
  1055. {
  1056. if(vli_sub(l_product, l_product, l_modMultiple))
  1057. { /* borrow */
  1058. vli_sub(l_product + NUM_ECC_DIGITS, l_product + NUM_ECC_DIGITS, p_result);
  1059. }
  1060. vli_sub(l_product + NUM_ECC_DIGITS, l_product + NUM_ECC_DIGITS, l_modMultiple + NUM_ECC_DIGITS);
  1061. }
  1062. uint64_t l_carry = (l_modMultiple[NUM_ECC_DIGITS] & 0x01) << 63;
  1063. vli_rshift1(l_modMultiple + NUM_ECC_DIGITS);
  1064. vli_rshift1(l_modMultiple);
  1065. l_modMultiple[NUM_ECC_DIGITS-1] |= l_carry;
  1066. --l_productBits;
  1067. }
  1068. vli_set(p_result, l_product);
  1069. }
  1070. static inline uint umax(uint a, uint b)
  1071. {
  1072. return (a > b ? a : b);
  1073. }
  1074. 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])
  1075. {
  1076. uint64_t k[NUM_ECC_DIGITS];
  1077. uint64_t l_tmp[NUM_ECC_DIGITS];
  1078. uint64_t l_s[NUM_ECC_DIGITS];
  1079. EccPoint p;
  1080. unsigned l_tries = 0;
  1081. do
  1082. {
  1083. if(!getRandomNumber(k) || (l_tries++ >= MAX_TRIES))
  1084. {
  1085. return 0;
  1086. }
  1087. if(vli_isZero(k))
  1088. {
  1089. continue;
  1090. }
  1091. if(vli_cmp(curve_n, k) != 1)
  1092. {
  1093. vli_sub(k, k, curve_n);
  1094. }
  1095. /* tmp = k * G */
  1096. EccPoint_mult(&p, &curve_G, k, NULL);
  1097. /* r = x1 (mod n) */
  1098. if(vli_cmp(curve_n, p.x) != 1)
  1099. {
  1100. vli_sub(p.x, p.x, curve_n);
  1101. }
  1102. } while(vli_isZero(p.x));
  1103. ecc_native2bytes(p_signature, p.x);
  1104. ecc_bytes2native(l_tmp, p_privateKey);
  1105. vli_modMult(l_s, p.x, l_tmp, curve_n); /* s = r*d */
  1106. ecc_bytes2native(l_tmp, p_hash);
  1107. vli_modAdd(l_s, l_tmp, l_s, curve_n); /* s = e + r*d */
  1108. vli_modInv(k, k, curve_n); /* k = 1 / k */
  1109. vli_modMult(l_s, l_s, k, curve_n); /* s = (e + r*d) / k */
  1110. ecc_native2bytes(p_signature + ECC_BYTES, l_s);
  1111. return 1;
  1112. }
  1113. 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])
  1114. {
  1115. uint64_t u1[NUM_ECC_DIGITS], u2[NUM_ECC_DIGITS];
  1116. uint64_t z[NUM_ECC_DIGITS];
  1117. EccPoint l_public, l_sum;
  1118. uint64_t rx[NUM_ECC_DIGITS];
  1119. uint64_t ry[NUM_ECC_DIGITS];
  1120. uint64_t tx[NUM_ECC_DIGITS];
  1121. uint64_t ty[NUM_ECC_DIGITS];
  1122. uint64_t tz[NUM_ECC_DIGITS];
  1123. uint64_t l_r[NUM_ECC_DIGITS], l_s[NUM_ECC_DIGITS];
  1124. ecc_point_decompress(&l_public, p_publicKey);
  1125. ecc_bytes2native(l_r, p_signature);
  1126. ecc_bytes2native(l_s, p_signature + ECC_BYTES);
  1127. if(vli_isZero(l_r) || vli_isZero(l_s))
  1128. { /* r, s must not be 0. */
  1129. return 0;
  1130. }
  1131. if(vli_cmp(curve_n, l_r) != 1 || vli_cmp(curve_n, l_s) != 1)
  1132. { /* r, s must be < n. */
  1133. return 0;
  1134. }
  1135. /* Calculate u1 and u2. */
  1136. vli_modInv(z, l_s, curve_n); /* Z = s^-1 */
  1137. ecc_bytes2native(u1, p_hash);
  1138. vli_modMult(u1, u1, z, curve_n); /* u1 = e/s */
  1139. vli_modMult(u2, l_r, z, curve_n); /* u2 = r/s */
  1140. /* Calculate l_sum = G + Q. */
  1141. vli_set(l_sum.x, l_public.x);
  1142. vli_set(l_sum.y, l_public.y);
  1143. vli_set(tx, curve_G.x);
  1144. vli_set(ty, curve_G.y);
  1145. vli_modSub(z, l_sum.x, tx, curve_p); /* Z = x2 - x1 */
  1146. XYcZ_add(tx, ty, l_sum.x, l_sum.y);
  1147. vli_modInv(z, z, curve_p); /* Z = 1/Z */
  1148. apply_z(l_sum.x, l_sum.y, z);
  1149. /* Use Shamir's trick to calculate u1*G + u2*Q */
  1150. EccPoint *l_points[4] = {NULL, &curve_G, &l_public, &l_sum};
  1151. uint l_numBits = umax(vli_numBits(u1), vli_numBits(u2));
  1152. EccPoint *l_point = l_points[(!!vli_testBit(u1, l_numBits-1)) | ((!!vli_testBit(u2, l_numBits-1)) << 1)];
  1153. vli_set(rx, l_point->x);
  1154. vli_set(ry, l_point->y);
  1155. vli_clear(z);
  1156. z[0] = 1;
  1157. int i;
  1158. for(i = l_numBits - 2; i >= 0; --i)
  1159. {
  1160. EccPoint_double_jacobian(rx, ry, z);
  1161. int l_index = (!!vli_testBit(u1, i)) | ((!!vli_testBit(u2, i)) << 1);
  1162. EccPoint *l_point = l_points[l_index];
  1163. if(l_point)
  1164. {
  1165. vli_set(tx, l_point->x);
  1166. vli_set(ty, l_point->y);
  1167. apply_z(tx, ty, z);
  1168. vli_modSub(tz, rx, tx, curve_p); /* Z = x2 - x1 */
  1169. XYcZ_add(tx, ty, rx, ry);
  1170. vli_modMult_fast(z, z, tz);
  1171. }
  1172. }
  1173. vli_modInv(z, z, curve_p); /* Z = 1/Z */
  1174. apply_z(rx, ry, z);
  1175. /* v = x1 (mod n) */
  1176. if(vli_cmp(curve_n, rx) != 1)
  1177. {
  1178. vli_sub(rx, rx, curve_n);
  1179. }
  1180. /* Accept only if v == r. */
  1181. return (vli_cmp(rx, l_r) == 0);
  1182. }
  1183. //////////////////////////////////////////////////////////////////////////////
  1184. //////////////////////////////////////////////////////////////////////////////
  1185. //////////////////////////////////////////////////////////////////////////////
  1186. } // anonymous namespace
  1187. void ECC384GenerateKey(uint8_t pub[ZT_ECC384_PUBLIC_KEY_SIZE],uint8_t priv[ZT_ECC384_PRIVATE_KEY_SIZE])
  1188. {
  1189. if (!ecc_make_key(pub,priv)) {
  1190. fprintf(stderr,"FATAL: ecdsa_make_key() failed!" ZT_EOL_S);
  1191. abort();
  1192. }
  1193. }
  1194. 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])
  1195. {
  1196. if (!ecdsa_sign(priv,hash,sig)) {
  1197. fprintf(stderr,"FATAL: ecdsa_sign() failed!" ZT_EOL_S);
  1198. abort();
  1199. }
  1200. }
  1201. 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])
  1202. {
  1203. return (ecdsa_verify(pub,hash,sig) != 0);
  1204. }
  1205. 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])
  1206. {
  1207. return (ecdh_shared_secret(theirPub,ourPriv,secret) != 0);
  1208. }
  1209. } // namespace ZeroTier