ed25519-password-auth.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  1. 'use strict';
  2. const PluginAuth = require('./plugin-auth');
  3. const Crypto = require('crypto');
  4. /**
  5. * Standard authentication plugin
  6. */
  7. class Ed25519PasswordAuth extends PluginAuth {
  8. constructor(packSeq, compressPackSeq, pluginData, resolve, reject, multiAuthResolver) {
  9. super(resolve, reject, multiAuthResolver);
  10. this.pluginData = pluginData;
  11. this.sequenceNo = packSeq;
  12. }
  13. start(out, opts, info) {
  14. //seed is ended with a null byte value.
  15. const data = this.pluginData;
  16. const sign = Ed25519PasswordAuth.encryptPassword(opts.password, data);
  17. out.startPacket(this);
  18. out.writeBuffer(sign, 0, sign.length);
  19. out.flushBuffer(true);
  20. this.emit('send_end');
  21. this.onPacketReceive = this.successSend;
  22. }
  23. static encryptPassword(password, seed) {
  24. if (!password) return Buffer.alloc(0);
  25. let i, j;
  26. let p = [gf(), gf(), gf(), gf()];
  27. const signedMsg = Buffer.alloc(96);
  28. const bytePwd = Buffer.from(password);
  29. let hash = Crypto.createHash('sha512');
  30. const d = hash.update(bytePwd).digest();
  31. d[0] &= 248;
  32. d[31] &= 127;
  33. d[31] |= 64;
  34. for (i = 0; i < 32; i++) signedMsg[64 + i] = seed[i];
  35. for (i = 0; i < 32; i++) signedMsg[32 + i] = d[32 + i];
  36. hash = Crypto.createHash('sha512');
  37. const r = hash.update(signedMsg.slice(32, 96)).digest();
  38. reduce(r);
  39. scalarbase(p, r);
  40. pack(signedMsg, p);
  41. p = [gf(), gf(), gf(), gf()];
  42. scalarbase(p, d);
  43. const tt = Buffer.alloc(32);
  44. pack(tt, p);
  45. for (i = 32; i < 64; i++) signedMsg[i] = tt[i - 32];
  46. hash = Crypto.createHash('sha512');
  47. const h = hash.update(signedMsg).digest();
  48. reduce(h);
  49. const x = new Float64Array(64);
  50. for (i = 0; i < 64; i++) x[i] = 0;
  51. for (i = 0; i < 32; i++) x[i] = r[i];
  52. for (i = 0; i < 32; i++) {
  53. for (j = 0; j < 32; j++) {
  54. x[i + j] += h[i] * d[j];
  55. }
  56. }
  57. modL(signedMsg.subarray(32), x);
  58. return signedMsg.slice(0, 64);
  59. }
  60. }
  61. /*******************************************************
  62. *
  63. * This plugin uses the following public domain tweetnacl-js code by Dmitry Chestnykh (from https://github.com/dchest/tweetnacl-js/blob/master/nacl-fast.js).
  64. * tweetnacl cannot be used directly (secret key mandatory size is 32 in nacl + implementation differ : second scalarbase use hash of secret key, not secret key).
  65. *
  66. *******************************************************/
  67. const gf = function (init) {
  68. const r = new Float64Array(16);
  69. if (init) for (let i = 0; i < init.length; i++) r[i] = init[i];
  70. return r;
  71. };
  72. const gf0 = gf(),
  73. gf1 = gf([1]),
  74. D2 = gf([
  75. 0xf159,
  76. 0x26b2,
  77. 0x9b94,
  78. 0xebd6,
  79. 0xb156,
  80. 0x8283,
  81. 0x149a,
  82. 0x00e0,
  83. 0xd130,
  84. 0xeef3,
  85. 0x80f2,
  86. 0x198e,
  87. 0xfce7,
  88. 0x56df,
  89. 0xd9dc,
  90. 0x2406
  91. ]),
  92. X = gf([
  93. 0xd51a,
  94. 0x8f25,
  95. 0x2d60,
  96. 0xc956,
  97. 0xa7b2,
  98. 0x9525,
  99. 0xc760,
  100. 0x692c,
  101. 0xdc5c,
  102. 0xfdd6,
  103. 0xe231,
  104. 0xc0a4,
  105. 0x53fe,
  106. 0xcd6e,
  107. 0x36d3,
  108. 0x2169
  109. ]),
  110. Y = gf([
  111. 0x6658,
  112. 0x6666,
  113. 0x6666,
  114. 0x6666,
  115. 0x6666,
  116. 0x6666,
  117. 0x6666,
  118. 0x6666,
  119. 0x6666,
  120. 0x6666,
  121. 0x6666,
  122. 0x6666,
  123. 0x6666,
  124. 0x6666,
  125. 0x6666,
  126. 0x6666
  127. ]);
  128. const L = new Float64Array([
  129. 0xed,
  130. 0xd3,
  131. 0xf5,
  132. 0x5c,
  133. 0x1a,
  134. 0x63,
  135. 0x12,
  136. 0x58,
  137. 0xd6,
  138. 0x9c,
  139. 0xf7,
  140. 0xa2,
  141. 0xde,
  142. 0xf9,
  143. 0xde,
  144. 0x14,
  145. 0,
  146. 0,
  147. 0,
  148. 0,
  149. 0,
  150. 0,
  151. 0,
  152. 0,
  153. 0,
  154. 0,
  155. 0,
  156. 0,
  157. 0,
  158. 0,
  159. 0,
  160. 0x10
  161. ]);
  162. function reduce(r) {
  163. const x = new Float64Array(64);
  164. let i;
  165. for (i = 0; i < 64; i++) x[i] = r[i];
  166. for (i = 0; i < 64; i++) r[i] = 0;
  167. modL(r, x);
  168. }
  169. function modL(r, x) {
  170. let carry, i, j, k;
  171. for (i = 63; i >= 32; --i) {
  172. carry = 0;
  173. for (j = i - 32, k = i - 12; j < k; ++j) {
  174. x[j] += carry - 16 * x[i] * L[j - (i - 32)];
  175. carry = (x[j] + 128) >> 8;
  176. x[j] -= carry * 256;
  177. }
  178. x[j] += carry;
  179. x[i] = 0;
  180. }
  181. carry = 0;
  182. for (j = 0; j < 32; j++) {
  183. x[j] += carry - (x[31] >> 4) * L[j];
  184. carry = x[j] >> 8;
  185. x[j] &= 255;
  186. }
  187. for (j = 0; j < 32; j++) x[j] -= carry * L[j];
  188. for (i = 0; i < 32; i++) {
  189. x[i + 1] += x[i] >> 8;
  190. r[i] = x[i] & 255;
  191. }
  192. }
  193. function scalarbase(p, s) {
  194. const q = [gf(), gf(), gf(), gf()];
  195. set25519(q[0], X);
  196. set25519(q[1], Y);
  197. set25519(q[2], gf1);
  198. M(q[3], X, Y);
  199. scalarmult(p, q, s);
  200. }
  201. function set25519(r, a) {
  202. for (let i = 0; i < 16; i++) r[i] = a[i] | 0;
  203. }
  204. function M(o, a, b) {
  205. let v,
  206. c,
  207. t0 = 0,
  208. t1 = 0,
  209. t2 = 0,
  210. t3 = 0,
  211. t4 = 0,
  212. t5 = 0,
  213. t6 = 0,
  214. t7 = 0,
  215. t8 = 0,
  216. t9 = 0,
  217. t10 = 0,
  218. t11 = 0,
  219. t12 = 0,
  220. t13 = 0,
  221. t14 = 0,
  222. t15 = 0,
  223. t16 = 0,
  224. t17 = 0,
  225. t18 = 0,
  226. t19 = 0,
  227. t20 = 0,
  228. t21 = 0,
  229. t22 = 0,
  230. t23 = 0,
  231. t24 = 0,
  232. t25 = 0,
  233. t26 = 0,
  234. t27 = 0,
  235. t28 = 0,
  236. t29 = 0,
  237. t30 = 0;
  238. const b0 = b[0],
  239. b1 = b[1],
  240. b2 = b[2],
  241. b3 = b[3],
  242. b4 = b[4],
  243. b5 = b[5],
  244. b6 = b[6],
  245. b7 = b[7],
  246. b8 = b[8],
  247. b9 = b[9],
  248. b10 = b[10],
  249. b11 = b[11],
  250. b12 = b[12],
  251. b13 = b[13],
  252. b14 = b[14],
  253. b15 = b[15];
  254. v = a[0];
  255. t0 += v * b0;
  256. t1 += v * b1;
  257. t2 += v * b2;
  258. t3 += v * b3;
  259. t4 += v * b4;
  260. t5 += v * b5;
  261. t6 += v * b6;
  262. t7 += v * b7;
  263. t8 += v * b8;
  264. t9 += v * b9;
  265. t10 += v * b10;
  266. t11 += v * b11;
  267. t12 += v * b12;
  268. t13 += v * b13;
  269. t14 += v * b14;
  270. t15 += v * b15;
  271. v = a[1];
  272. t1 += v * b0;
  273. t2 += v * b1;
  274. t3 += v * b2;
  275. t4 += v * b3;
  276. t5 += v * b4;
  277. t6 += v * b5;
  278. t7 += v * b6;
  279. t8 += v * b7;
  280. t9 += v * b8;
  281. t10 += v * b9;
  282. t11 += v * b10;
  283. t12 += v * b11;
  284. t13 += v * b12;
  285. t14 += v * b13;
  286. t15 += v * b14;
  287. t16 += v * b15;
  288. v = a[2];
  289. t2 += v * b0;
  290. t3 += v * b1;
  291. t4 += v * b2;
  292. t5 += v * b3;
  293. t6 += v * b4;
  294. t7 += v * b5;
  295. t8 += v * b6;
  296. t9 += v * b7;
  297. t10 += v * b8;
  298. t11 += v * b9;
  299. t12 += v * b10;
  300. t13 += v * b11;
  301. t14 += v * b12;
  302. t15 += v * b13;
  303. t16 += v * b14;
  304. t17 += v * b15;
  305. v = a[3];
  306. t3 += v * b0;
  307. t4 += v * b1;
  308. t5 += v * b2;
  309. t6 += v * b3;
  310. t7 += v * b4;
  311. t8 += v * b5;
  312. t9 += v * b6;
  313. t10 += v * b7;
  314. t11 += v * b8;
  315. t12 += v * b9;
  316. t13 += v * b10;
  317. t14 += v * b11;
  318. t15 += v * b12;
  319. t16 += v * b13;
  320. t17 += v * b14;
  321. t18 += v * b15;
  322. v = a[4];
  323. t4 += v * b0;
  324. t5 += v * b1;
  325. t6 += v * b2;
  326. t7 += v * b3;
  327. t8 += v * b4;
  328. t9 += v * b5;
  329. t10 += v * b6;
  330. t11 += v * b7;
  331. t12 += v * b8;
  332. t13 += v * b9;
  333. t14 += v * b10;
  334. t15 += v * b11;
  335. t16 += v * b12;
  336. t17 += v * b13;
  337. t18 += v * b14;
  338. t19 += v * b15;
  339. v = a[5];
  340. t5 += v * b0;
  341. t6 += v * b1;
  342. t7 += v * b2;
  343. t8 += v * b3;
  344. t9 += v * b4;
  345. t10 += v * b5;
  346. t11 += v * b6;
  347. t12 += v * b7;
  348. t13 += v * b8;
  349. t14 += v * b9;
  350. t15 += v * b10;
  351. t16 += v * b11;
  352. t17 += v * b12;
  353. t18 += v * b13;
  354. t19 += v * b14;
  355. t20 += v * b15;
  356. v = a[6];
  357. t6 += v * b0;
  358. t7 += v * b1;
  359. t8 += v * b2;
  360. t9 += v * b3;
  361. t10 += v * b4;
  362. t11 += v * b5;
  363. t12 += v * b6;
  364. t13 += v * b7;
  365. t14 += v * b8;
  366. t15 += v * b9;
  367. t16 += v * b10;
  368. t17 += v * b11;
  369. t18 += v * b12;
  370. t19 += v * b13;
  371. t20 += v * b14;
  372. t21 += v * b15;
  373. v = a[7];
  374. t7 += v * b0;
  375. t8 += v * b1;
  376. t9 += v * b2;
  377. t10 += v * b3;
  378. t11 += v * b4;
  379. t12 += v * b5;
  380. t13 += v * b6;
  381. t14 += v * b7;
  382. t15 += v * b8;
  383. t16 += v * b9;
  384. t17 += v * b10;
  385. t18 += v * b11;
  386. t19 += v * b12;
  387. t20 += v * b13;
  388. t21 += v * b14;
  389. t22 += v * b15;
  390. v = a[8];
  391. t8 += v * b0;
  392. t9 += v * b1;
  393. t10 += v * b2;
  394. t11 += v * b3;
  395. t12 += v * b4;
  396. t13 += v * b5;
  397. t14 += v * b6;
  398. t15 += v * b7;
  399. t16 += v * b8;
  400. t17 += v * b9;
  401. t18 += v * b10;
  402. t19 += v * b11;
  403. t20 += v * b12;
  404. t21 += v * b13;
  405. t22 += v * b14;
  406. t23 += v * b15;
  407. v = a[9];
  408. t9 += v * b0;
  409. t10 += v * b1;
  410. t11 += v * b2;
  411. t12 += v * b3;
  412. t13 += v * b4;
  413. t14 += v * b5;
  414. t15 += v * b6;
  415. t16 += v * b7;
  416. t17 += v * b8;
  417. t18 += v * b9;
  418. t19 += v * b10;
  419. t20 += v * b11;
  420. t21 += v * b12;
  421. t22 += v * b13;
  422. t23 += v * b14;
  423. t24 += v * b15;
  424. v = a[10];
  425. t10 += v * b0;
  426. t11 += v * b1;
  427. t12 += v * b2;
  428. t13 += v * b3;
  429. t14 += v * b4;
  430. t15 += v * b5;
  431. t16 += v * b6;
  432. t17 += v * b7;
  433. t18 += v * b8;
  434. t19 += v * b9;
  435. t20 += v * b10;
  436. t21 += v * b11;
  437. t22 += v * b12;
  438. t23 += v * b13;
  439. t24 += v * b14;
  440. t25 += v * b15;
  441. v = a[11];
  442. t11 += v * b0;
  443. t12 += v * b1;
  444. t13 += v * b2;
  445. t14 += v * b3;
  446. t15 += v * b4;
  447. t16 += v * b5;
  448. t17 += v * b6;
  449. t18 += v * b7;
  450. t19 += v * b8;
  451. t20 += v * b9;
  452. t21 += v * b10;
  453. t22 += v * b11;
  454. t23 += v * b12;
  455. t24 += v * b13;
  456. t25 += v * b14;
  457. t26 += v * b15;
  458. v = a[12];
  459. t12 += v * b0;
  460. t13 += v * b1;
  461. t14 += v * b2;
  462. t15 += v * b3;
  463. t16 += v * b4;
  464. t17 += v * b5;
  465. t18 += v * b6;
  466. t19 += v * b7;
  467. t20 += v * b8;
  468. t21 += v * b9;
  469. t22 += v * b10;
  470. t23 += v * b11;
  471. t24 += v * b12;
  472. t25 += v * b13;
  473. t26 += v * b14;
  474. t27 += v * b15;
  475. v = a[13];
  476. t13 += v * b0;
  477. t14 += v * b1;
  478. t15 += v * b2;
  479. t16 += v * b3;
  480. t17 += v * b4;
  481. t18 += v * b5;
  482. t19 += v * b6;
  483. t20 += v * b7;
  484. t21 += v * b8;
  485. t22 += v * b9;
  486. t23 += v * b10;
  487. t24 += v * b11;
  488. t25 += v * b12;
  489. t26 += v * b13;
  490. t27 += v * b14;
  491. t28 += v * b15;
  492. v = a[14];
  493. t14 += v * b0;
  494. t15 += v * b1;
  495. t16 += v * b2;
  496. t17 += v * b3;
  497. t18 += v * b4;
  498. t19 += v * b5;
  499. t20 += v * b6;
  500. t21 += v * b7;
  501. t22 += v * b8;
  502. t23 += v * b9;
  503. t24 += v * b10;
  504. t25 += v * b11;
  505. t26 += v * b12;
  506. t27 += v * b13;
  507. t28 += v * b14;
  508. t29 += v * b15;
  509. v = a[15];
  510. t15 += v * b0;
  511. t16 += v * b1;
  512. t17 += v * b2;
  513. t18 += v * b3;
  514. t19 += v * b4;
  515. t20 += v * b5;
  516. t21 += v * b6;
  517. t22 += v * b7;
  518. t23 += v * b8;
  519. t24 += v * b9;
  520. t25 += v * b10;
  521. t26 += v * b11;
  522. t27 += v * b12;
  523. t28 += v * b13;
  524. t29 += v * b14;
  525. t30 += v * b15;
  526. t0 += 38 * t16;
  527. t1 += 38 * t17;
  528. t2 += 38 * t18;
  529. t3 += 38 * t19;
  530. t4 += 38 * t20;
  531. t5 += 38 * t21;
  532. t6 += 38 * t22;
  533. t7 += 38 * t23;
  534. t8 += 38 * t24;
  535. t9 += 38 * t25;
  536. t10 += 38 * t26;
  537. t11 += 38 * t27;
  538. t12 += 38 * t28;
  539. t13 += 38 * t29;
  540. t14 += 38 * t30;
  541. // t15 left as is
  542. // first car
  543. c = 1;
  544. v = t0 + c + 65535;
  545. c = Math.floor(v / 65536);
  546. t0 = v - c * 65536;
  547. v = t1 + c + 65535;
  548. c = Math.floor(v / 65536);
  549. t1 = v - c * 65536;
  550. v = t2 + c + 65535;
  551. c = Math.floor(v / 65536);
  552. t2 = v - c * 65536;
  553. v = t3 + c + 65535;
  554. c = Math.floor(v / 65536);
  555. t3 = v - c * 65536;
  556. v = t4 + c + 65535;
  557. c = Math.floor(v / 65536);
  558. t4 = v - c * 65536;
  559. v = t5 + c + 65535;
  560. c = Math.floor(v / 65536);
  561. t5 = v - c * 65536;
  562. v = t6 + c + 65535;
  563. c = Math.floor(v / 65536);
  564. t6 = v - c * 65536;
  565. v = t7 + c + 65535;
  566. c = Math.floor(v / 65536);
  567. t7 = v - c * 65536;
  568. v = t8 + c + 65535;
  569. c = Math.floor(v / 65536);
  570. t8 = v - c * 65536;
  571. v = t9 + c + 65535;
  572. c = Math.floor(v / 65536);
  573. t9 = v - c * 65536;
  574. v = t10 + c + 65535;
  575. c = Math.floor(v / 65536);
  576. t10 = v - c * 65536;
  577. v = t11 + c + 65535;
  578. c = Math.floor(v / 65536);
  579. t11 = v - c * 65536;
  580. v = t12 + c + 65535;
  581. c = Math.floor(v / 65536);
  582. t12 = v - c * 65536;
  583. v = t13 + c + 65535;
  584. c = Math.floor(v / 65536);
  585. t13 = v - c * 65536;
  586. v = t14 + c + 65535;
  587. c = Math.floor(v / 65536);
  588. t14 = v - c * 65536;
  589. v = t15 + c + 65535;
  590. c = Math.floor(v / 65536);
  591. t15 = v - c * 65536;
  592. t0 += c - 1 + 37 * (c - 1);
  593. // second car
  594. c = 1;
  595. v = t0 + c + 65535;
  596. c = Math.floor(v / 65536);
  597. t0 = v - c * 65536;
  598. v = t1 + c + 65535;
  599. c = Math.floor(v / 65536);
  600. t1 = v - c * 65536;
  601. v = t2 + c + 65535;
  602. c = Math.floor(v / 65536);
  603. t2 = v - c * 65536;
  604. v = t3 + c + 65535;
  605. c = Math.floor(v / 65536);
  606. t3 = v - c * 65536;
  607. v = t4 + c + 65535;
  608. c = Math.floor(v / 65536);
  609. t4 = v - c * 65536;
  610. v = t5 + c + 65535;
  611. c = Math.floor(v / 65536);
  612. t5 = v - c * 65536;
  613. v = t6 + c + 65535;
  614. c = Math.floor(v / 65536);
  615. t6 = v - c * 65536;
  616. v = t7 + c + 65535;
  617. c = Math.floor(v / 65536);
  618. t7 = v - c * 65536;
  619. v = t8 + c + 65535;
  620. c = Math.floor(v / 65536);
  621. t8 = v - c * 65536;
  622. v = t9 + c + 65535;
  623. c = Math.floor(v / 65536);
  624. t9 = v - c * 65536;
  625. v = t10 + c + 65535;
  626. c = Math.floor(v / 65536);
  627. t10 = v - c * 65536;
  628. v = t11 + c + 65535;
  629. c = Math.floor(v / 65536);
  630. t11 = v - c * 65536;
  631. v = t12 + c + 65535;
  632. c = Math.floor(v / 65536);
  633. t12 = v - c * 65536;
  634. v = t13 + c + 65535;
  635. c = Math.floor(v / 65536);
  636. t13 = v - c * 65536;
  637. v = t14 + c + 65535;
  638. c = Math.floor(v / 65536);
  639. t14 = v - c * 65536;
  640. v = t15 + c + 65535;
  641. c = Math.floor(v / 65536);
  642. t15 = v - c * 65536;
  643. t0 += c - 1 + 37 * (c - 1);
  644. o[0] = t0;
  645. o[1] = t1;
  646. o[2] = t2;
  647. o[3] = t3;
  648. o[4] = t4;
  649. o[5] = t5;
  650. o[6] = t6;
  651. o[7] = t7;
  652. o[8] = t8;
  653. o[9] = t9;
  654. o[10] = t10;
  655. o[11] = t11;
  656. o[12] = t12;
  657. o[13] = t13;
  658. o[14] = t14;
  659. o[15] = t15;
  660. }
  661. function scalarmult(p, q, s) {
  662. let b, i;
  663. set25519(p[0], gf0);
  664. set25519(p[1], gf1);
  665. set25519(p[2], gf1);
  666. set25519(p[3], gf0);
  667. for (i = 255; i >= 0; --i) {
  668. b = (s[(i / 8) | 0] >> (i & 7)) & 1;
  669. cswap(p, q, b);
  670. add(q, p);
  671. add(p, p);
  672. cswap(p, q, b);
  673. }
  674. }
  675. function pack(r, p) {
  676. const tx = gf(),
  677. ty = gf(),
  678. zi = gf();
  679. inv25519(zi, p[2]);
  680. M(tx, p[0], zi);
  681. M(ty, p[1], zi);
  682. pack25519(r, ty);
  683. r[31] ^= par25519(tx) << 7;
  684. }
  685. function inv25519(o, i) {
  686. const c = gf();
  687. let a;
  688. for (a = 0; a < 16; a++) c[a] = i[a];
  689. for (a = 253; a >= 0; a--) {
  690. S(c, c);
  691. if (a !== 2 && a !== 4) M(c, c, i);
  692. }
  693. for (a = 0; a < 16; a++) o[a] = c[a];
  694. }
  695. function S(o, a) {
  696. M(o, a, a);
  697. }
  698. function par25519(a) {
  699. const d = new Uint8Array(32);
  700. pack25519(d, a);
  701. return d[0] & 1;
  702. }
  703. function car25519(o) {
  704. let i,
  705. v,
  706. c = 1;
  707. for (i = 0; i < 16; i++) {
  708. v = o[i] + c + 65535;
  709. c = Math.floor(v / 65536);
  710. o[i] = v - c * 65536;
  711. }
  712. o[0] += c - 1 + 37 * (c - 1);
  713. }
  714. function pack25519(o, n) {
  715. let i, j, b;
  716. const m = gf(),
  717. t = gf();
  718. for (i = 0; i < 16; i++) t[i] = n[i];
  719. car25519(t);
  720. car25519(t);
  721. car25519(t);
  722. for (j = 0; j < 2; j++) {
  723. m[0] = t[0] - 0xffed;
  724. for (i = 1; i < 15; i++) {
  725. m[i] = t[i] - 0xffff - ((m[i - 1] >> 16) & 1);
  726. m[i - 1] &= 0xffff;
  727. }
  728. m[15] = t[15] - 0x7fff - ((m[14] >> 16) & 1);
  729. b = (m[15] >> 16) & 1;
  730. m[14] &= 0xffff;
  731. sel25519(t, m, 1 - b);
  732. }
  733. for (i = 0; i < 16; i++) {
  734. o[2 * i] = t[i] & 0xff;
  735. o[2 * i + 1] = t[i] >> 8;
  736. }
  737. }
  738. function cswap(p, q, b) {
  739. for (let i = 0; i < 4; i++) {
  740. sel25519(p[i], q[i], b);
  741. }
  742. }
  743. function A(o, a, b) {
  744. for (let i = 0; i < 16; i++) o[i] = a[i] + b[i];
  745. }
  746. function Z(o, a, b) {
  747. for (let i = 0; i < 16; i++) o[i] = a[i] - b[i];
  748. }
  749. function add(p, q) {
  750. const a = gf(),
  751. b = gf(),
  752. c = gf(),
  753. d = gf(),
  754. e = gf(),
  755. f = gf(),
  756. g = gf(),
  757. h = gf(),
  758. t = gf();
  759. Z(a, p[1], p[0]);
  760. Z(t, q[1], q[0]);
  761. M(a, a, t);
  762. A(b, p[0], p[1]);
  763. A(t, q[0], q[1]);
  764. M(b, b, t);
  765. M(c, p[3], q[3]);
  766. M(c, c, D2);
  767. M(d, p[2], q[2]);
  768. A(d, d, d);
  769. Z(e, b, a);
  770. Z(f, d, c);
  771. A(g, d, c);
  772. A(h, b, a);
  773. M(p[0], e, f);
  774. M(p[1], h, g);
  775. M(p[2], g, f);
  776. M(p[3], e, h);
  777. }
  778. function sel25519(p, q, b) {
  779. const c = ~(b - 1);
  780. let t;
  781. for (let i = 0; i < 16; i++) {
  782. t = c & (p[i] ^ q[i]);
  783. p[i] ^= t;
  784. q[i] ^= t;
  785. }
  786. }
  787. module.exports = Ed25519PasswordAuth;