Ed25519.cs 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Diagnostics;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Digests;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Rfc7748;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
  9. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
  10. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  11. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Rfc8032
  12. {
  13. public abstract class Ed25519
  14. {
  15. public enum Algorithm
  16. {
  17. Ed25519 = 0,
  18. Ed25519ctx = 1,
  19. Ed25519ph = 2,
  20. }
  21. private const long M28L = 0x0FFFFFFFL;
  22. private const long M32L = 0xFFFFFFFFL;
  23. private const int PointBytes = 32;
  24. private const int ScalarUints = 8;
  25. private const int ScalarBytes = ScalarUints * 4;
  26. public static readonly int PrehashSize = 64;
  27. public static readonly int PublicKeySize = PointBytes;
  28. public static readonly int SecretKeySize = 32;
  29. public static readonly int SignatureSize = PointBytes + ScalarBytes;
  30. private static readonly byte[] Dom2Prefix = Strings.ToByteArray("SigEd25519 no Ed25519 collisions");
  31. private static readonly uint[] P = { 0xFFFFFFEDU, 0xFFFFFFFFU, 0xFFFFFFFFU, 0xFFFFFFFFU, 0xFFFFFFFFU, 0xFFFFFFFFU, 0xFFFFFFFFU, 0x7FFFFFFFU };
  32. private static readonly uint[] L = { 0x5CF5D3EDU, 0x5812631AU, 0xA2F79CD6U, 0x14DEF9DEU, 0x00000000U, 0x00000000U, 0x00000000U, 0x10000000U };
  33. private const int L0 = unchecked((int)0xFCF5D3ED); // L0:26/--
  34. private const int L1 = 0x012631A6; // L1:24/22
  35. private const int L2 = 0x079CD658; // L2:27/--
  36. private const int L3 = unchecked((int)0xFF9DEA2F); // L3:23/--
  37. private const int L4 = 0x000014DF; // L4:12/11
  38. private static readonly int[] B_x = { 0x0325D51A, 0x018B5823, 0x007B2C95, 0x0304A92D, 0x00D2598E, 0x01D6DC5C,
  39. 0x01388C7F, 0x013FEC0A, 0x029E6B72, 0x0042D26D };
  40. private static readonly int[] B_y = { 0x02666658, 0x01999999, 0x00666666, 0x03333333, 0x00CCCCCC, 0x02666666,
  41. 0x01999999, 0x00666666, 0x03333333, 0x00CCCCCC, };
  42. private static readonly int[] C_d = { 0x035978A3, 0x02D37284, 0x018AB75E, 0x026A0A0E, 0x0000E014, 0x0379E898,
  43. 0x01D01E5D, 0x01E738CC, 0x03715B7F, 0x00A406D9 };
  44. private static readonly int[] C_d2 = { 0x02B2F159, 0x01A6E509, 0x01156EBD, 0x00D4141D, 0x0001C029, 0x02F3D130,
  45. 0x03A03CBB, 0x01CE7198, 0x02E2B6FF, 0x00480DB3 };
  46. private static readonly int[] C_d4 = { 0x0165E2B2, 0x034DCA13, 0x002ADD7A, 0x01A8283B, 0x00038052, 0x01E7A260,
  47. 0x03407977, 0x019CE331, 0x01C56DFF, 0x00901B67 };
  48. private const int WnafWidthBase = 7;
  49. private const int PrecompBlocks = 8;
  50. private const int PrecompTeeth = 4;
  51. private const int PrecompSpacing = 8;
  52. private const int PrecompPoints = 1 << (PrecompTeeth - 1);
  53. private const int PrecompMask = PrecompPoints - 1;
  54. private static readonly object precompLock = new object();
  55. // TODO[ed25519] Convert to PointPrecomp
  56. private static PointExt[] precompBaseTable = null;
  57. private static int[] precompBase = null;
  58. private class PointAccum
  59. {
  60. internal int[] x = X25519Field.Create();
  61. internal int[] y = X25519Field.Create();
  62. internal int[] z = X25519Field.Create();
  63. internal int[] u = X25519Field.Create();
  64. internal int[] v = X25519Field.Create();
  65. }
  66. private class PointExt
  67. {
  68. internal int[] x = X25519Field.Create();
  69. internal int[] y = X25519Field.Create();
  70. internal int[] z = X25519Field.Create();
  71. internal int[] t = X25519Field.Create();
  72. }
  73. private class PointPrecomp
  74. {
  75. internal int[] ypx_h = X25519Field.Create();
  76. internal int[] ymx_h = X25519Field.Create();
  77. internal int[] xyd = X25519Field.Create();
  78. }
  79. private static byte[] CalculateS(byte[] r, byte[] k, byte[] s)
  80. {
  81. uint[] t = new uint[ScalarUints * 2]; DecodeScalar(r, 0, t);
  82. uint[] u = new uint[ScalarUints]; DecodeScalar(k, 0, u);
  83. uint[] v = new uint[ScalarUints]; DecodeScalar(s, 0, v);
  84. Nat256.MulAddTo(u, v, t);
  85. byte[] result = new byte[ScalarBytes * 2];
  86. for (int i = 0; i < t.Length; ++i)
  87. {
  88. Encode32(t[i], result, i * 4);
  89. }
  90. return ReduceScalar(result);
  91. }
  92. private static bool CheckContextVar(byte[] ctx, byte phflag)
  93. {
  94. return ctx == null && phflag == 0x00
  95. || ctx != null && ctx.Length < 256;
  96. }
  97. private static bool CheckPointVar(byte[] p)
  98. {
  99. uint[] t = new uint[8];
  100. Decode32(p, 0, t, 0, 8);
  101. t[7] &= 0x7FFFFFFFU;
  102. return !Nat256.Gte(t, P);
  103. }
  104. private static bool CheckScalarVar(byte[] s)
  105. {
  106. uint[] n = new uint[ScalarUints];
  107. DecodeScalar(s, 0, n);
  108. return !Nat256.Gte(n, L);
  109. }
  110. private static IDigest CreateDigest()
  111. {
  112. return new Sha512Digest();
  113. }
  114. public static IDigest CreatePrehash()
  115. {
  116. return CreateDigest();
  117. }
  118. private static uint Decode24(byte[] bs, int off)
  119. {
  120. uint n = bs[off];
  121. n |= (uint)bs[++off] << 8;
  122. n |= (uint)bs[++off] << 16;
  123. return n;
  124. }
  125. private static uint Decode32(byte[] bs, int off)
  126. {
  127. uint n = bs[off];
  128. n |= (uint)bs[++off] << 8;
  129. n |= (uint)bs[++off] << 16;
  130. n |= (uint)bs[++off] << 24;
  131. return n;
  132. }
  133. private static void Decode32(byte[] bs, int bsOff, uint[] n, int nOff, int nLen)
  134. {
  135. for (int i = 0; i < nLen; ++i)
  136. {
  137. n[nOff + i] = Decode32(bs, bsOff + i * 4);
  138. }
  139. }
  140. private static bool DecodePointVar(byte[] p, int pOff, bool negate, PointExt r)
  141. {
  142. byte[] py = Arrays.CopyOfRange(p, pOff, pOff + PointBytes);
  143. if (!CheckPointVar(py))
  144. return false;
  145. int x_0 = (py[PointBytes - 1] & 0x80) >> 7;
  146. py[PointBytes - 1] &= 0x7F;
  147. X25519Field.Decode(py, 0, r.y);
  148. int[] u = X25519Field.Create();
  149. int[] v = X25519Field.Create();
  150. X25519Field.Sqr(r.y, u);
  151. X25519Field.Mul(C_d, u, v);
  152. X25519Field.SubOne(u);
  153. X25519Field.AddOne(v);
  154. if (!X25519Field.SqrtRatioVar(u, v, r.x))
  155. return false;
  156. X25519Field.Normalize(r.x);
  157. if (x_0 == 1 && X25519Field.IsZeroVar(r.x))
  158. return false;
  159. if (negate ^ (x_0 != (r.x[0] & 1)))
  160. {
  161. X25519Field.Negate(r.x, r.x);
  162. }
  163. PointExtendXY(r);
  164. return true;
  165. }
  166. private static void DecodeScalar(byte[] k, int kOff, uint[] n)
  167. {
  168. Decode32(k, kOff, n, 0, ScalarUints);
  169. }
  170. private static void Dom2(IDigest d, byte phflag, byte[] ctx)
  171. {
  172. if (ctx != null)
  173. {
  174. d.BlockUpdate(Dom2Prefix, 0, Dom2Prefix.Length);
  175. d.Update(phflag);
  176. d.Update((byte)ctx.Length);
  177. d.BlockUpdate(ctx, 0, ctx.Length);
  178. }
  179. }
  180. private static void Encode24(uint n, byte[] bs, int off)
  181. {
  182. bs[off] = (byte)(n);
  183. bs[++off] = (byte)(n >> 8);
  184. bs[++off] = (byte)(n >> 16);
  185. }
  186. private static void Encode32(uint n, byte[] bs, int off)
  187. {
  188. bs[off] = (byte)(n);
  189. bs[++off] = (byte)(n >> 8);
  190. bs[++off] = (byte)(n >> 16);
  191. bs[++off] = (byte)(n >> 24);
  192. }
  193. private static void Encode56(ulong n, byte[] bs, int off)
  194. {
  195. Encode32((uint)n, bs, off);
  196. Encode24((uint)(n >> 32), bs, off + 4);
  197. }
  198. private static void EncodePoint(PointAccum p, byte[] r, int rOff)
  199. {
  200. int[] x = X25519Field.Create();
  201. int[] y = X25519Field.Create();
  202. X25519Field.Inv(p.z, y);
  203. X25519Field.Mul(p.x, y, x);
  204. X25519Field.Mul(p.y, y, y);
  205. X25519Field.Normalize(x);
  206. X25519Field.Normalize(y);
  207. X25519Field.Encode(y, r, rOff);
  208. r[rOff + PointBytes - 1] |= (byte)((x[0] & 1) << 7);
  209. }
  210. public static void GeneratePrivateKey(SecureRandom random, byte[] k)
  211. {
  212. random.NextBytes(k);
  213. }
  214. public static void GeneratePublicKey(byte[] sk, int skOff, byte[] pk, int pkOff)
  215. {
  216. IDigest d = CreateDigest();
  217. byte[] h = new byte[d.GetDigestSize()];
  218. d.BlockUpdate(sk, skOff, SecretKeySize);
  219. d.DoFinal(h, 0);
  220. byte[] s = new byte[ScalarBytes];
  221. PruneScalar(h, 0, s);
  222. ScalarMultBaseEncoded(s, pk, pkOff);
  223. }
  224. private static sbyte[] GetWnaf(uint[] n, int width)
  225. {
  226. Debug.Assert(n[ScalarUints - 1] >> 31 == 0);
  227. uint[] t = new uint[ScalarUints * 2];
  228. {
  229. uint c = 0;
  230. int tPos = t.Length, i = ScalarUints;
  231. while (--i >= 0)
  232. {
  233. uint next = n[i];
  234. t[--tPos] = (next >> 16) | (c << 16);
  235. t[--tPos] = c = next;
  236. }
  237. }
  238. sbyte[] ws = new sbyte[256];
  239. uint pow2 = 1U << width;
  240. uint mask = pow2 - 1U;
  241. uint sign = pow2 >> 1;
  242. uint carry = 0U;
  243. int j = 0;
  244. for (int i = 0; i < t.Length; ++i, j -= 16)
  245. {
  246. uint word = t[i];
  247. while (j < 16)
  248. {
  249. uint word16 = word >> j;
  250. uint bit = word16 & 1U;
  251. if (bit == carry)
  252. {
  253. ++j;
  254. continue;
  255. }
  256. uint digit = (word16 & mask) + carry;
  257. carry = digit & sign;
  258. digit -= (carry << 1);
  259. carry >>= (width - 1);
  260. ws[(i << 4) + j] = (sbyte)digit;
  261. j += width;
  262. }
  263. }
  264. Debug.Assert(carry == 0);
  265. return ws;
  266. }
  267. private static void ImplSign(IDigest d, byte[] h, byte[] s, byte[] pk, int pkOff, byte[] ctx, byte phflag,
  268. byte[] m, int mOff, int mLen, byte[] sig, int sigOff)
  269. {
  270. Dom2(d, phflag, ctx);
  271. d.BlockUpdate(h, ScalarBytes, ScalarBytes);
  272. d.BlockUpdate(m, mOff, mLen);
  273. d.DoFinal(h, 0);
  274. byte[] r = ReduceScalar(h);
  275. byte[] R = new byte[PointBytes];
  276. ScalarMultBaseEncoded(r, R, 0);
  277. Dom2(d, phflag, ctx);
  278. d.BlockUpdate(R, 0, PointBytes);
  279. d.BlockUpdate(pk, pkOff, PointBytes);
  280. d.BlockUpdate(m, mOff, mLen);
  281. d.DoFinal(h, 0);
  282. byte[] k = ReduceScalar(h);
  283. byte[] S = CalculateS(r, k, s);
  284. Array.Copy(R, 0, sig, sigOff, PointBytes);
  285. Array.Copy(S, 0, sig, sigOff + PointBytes, ScalarBytes);
  286. }
  287. private static void ImplSign(byte[] sk, int skOff, byte[] ctx, byte phflag, byte[] m, int mOff, int mLen,
  288. byte[] sig, int sigOff)
  289. {
  290. if (!CheckContextVar(ctx, phflag))
  291. throw new ArgumentException("ctx");
  292. IDigest d = CreateDigest();
  293. byte[] h = new byte[d.GetDigestSize()];
  294. d.BlockUpdate(sk, skOff, SecretKeySize);
  295. d.DoFinal(h, 0);
  296. byte[] s = new byte[ScalarBytes];
  297. PruneScalar(h, 0, s);
  298. byte[] pk = new byte[PointBytes];
  299. ScalarMultBaseEncoded(s, pk, 0);
  300. ImplSign(d, h, s, pk, 0, ctx, phflag, m, mOff, mLen, sig, sigOff);
  301. }
  302. private static void ImplSign(byte[] sk, int skOff, byte[] pk, int pkOff, byte[] ctx, byte phflag,
  303. byte[] m, int mOff, int mLen, byte[] sig, int sigOff)
  304. {
  305. if (!CheckContextVar(ctx, phflag))
  306. throw new ArgumentException("ctx");
  307. IDigest d = CreateDigest();
  308. byte[] h = new byte[d.GetDigestSize()];
  309. d.BlockUpdate(sk, skOff, SecretKeySize);
  310. d.DoFinal(h, 0);
  311. byte[] s = new byte[ScalarBytes];
  312. PruneScalar(h, 0, s);
  313. ImplSign(d, h, s, pk, pkOff, ctx, phflag, m, mOff, mLen, sig, sigOff);
  314. }
  315. private static bool ImplVerify(byte[] sig, int sigOff, byte[] pk, int pkOff, byte[] ctx, byte phflag,
  316. byte[] m, int mOff, int mLen)
  317. {
  318. if (!CheckContextVar(ctx, phflag))
  319. throw new ArgumentException("ctx");
  320. byte[] R = Arrays.CopyOfRange(sig, sigOff, sigOff + PointBytes);
  321. byte[] S = Arrays.CopyOfRange(sig, sigOff + PointBytes, sigOff + SignatureSize);
  322. if (!CheckPointVar(R))
  323. return false;
  324. if (!CheckScalarVar(S))
  325. return false;
  326. PointExt pA = new PointExt();
  327. if (!DecodePointVar(pk, pkOff, true, pA))
  328. return false;
  329. IDigest d = CreateDigest();
  330. byte[] h = new byte[d.GetDigestSize()];
  331. Dom2(d, phflag, ctx);
  332. d.BlockUpdate(R, 0, PointBytes);
  333. d.BlockUpdate(pk, pkOff, PointBytes);
  334. d.BlockUpdate(m, mOff, mLen);
  335. d.DoFinal(h, 0);
  336. byte[] k = ReduceScalar(h);
  337. uint[] nS = new uint[ScalarUints];
  338. DecodeScalar(S, 0, nS);
  339. uint[] nA = new uint[ScalarUints];
  340. DecodeScalar(k, 0, nA);
  341. PointAccum pR = new PointAccum();
  342. ScalarMultStraussVar(nS, nA, pA, pR);
  343. byte[] check = new byte[PointBytes];
  344. EncodePoint(pR, check, 0);
  345. return Arrays.AreEqual(check, R);
  346. }
  347. private static void PointAddVar(bool negate, PointExt p, PointAccum r)
  348. {
  349. int[] A = X25519Field.Create();
  350. int[] B = X25519Field.Create();
  351. int[] C = X25519Field.Create();
  352. int[] D = X25519Field.Create();
  353. int[] E = r.u;
  354. int[] F = X25519Field.Create();
  355. int[] G = X25519Field.Create();
  356. int[] H = r.v;
  357. int[] c, d, f, g;
  358. if (negate)
  359. {
  360. c = D; d = C; f = G; g = F;
  361. }
  362. else
  363. {
  364. c = C; d = D; f = F; g = G;
  365. }
  366. X25519Field.Apm(r.y, r.x, B, A);
  367. X25519Field.Apm(p.y, p.x, d, c);
  368. X25519Field.Mul(A, C, A);
  369. X25519Field.Mul(B, D, B);
  370. X25519Field.Mul(r.u, r.v, C);
  371. X25519Field.Mul(C, p.t, C);
  372. X25519Field.Mul(C, C_d2, C);
  373. X25519Field.Mul(r.z, p.z, D);
  374. X25519Field.Add(D, D, D);
  375. X25519Field.Apm(B, A, H, E);
  376. X25519Field.Apm(D, C, g, f);
  377. X25519Field.Carry(g);
  378. X25519Field.Mul(E, F, r.x);
  379. X25519Field.Mul(G, H, r.y);
  380. X25519Field.Mul(F, G, r.z);
  381. }
  382. private static void PointAddVar(bool negate, PointExt p, PointExt q, PointExt r)
  383. {
  384. int[] A = X25519Field.Create();
  385. int[] B = X25519Field.Create();
  386. int[] C = X25519Field.Create();
  387. int[] D = X25519Field.Create();
  388. int[] E = X25519Field.Create();
  389. int[] F = X25519Field.Create();
  390. int[] G = X25519Field.Create();
  391. int[] H = X25519Field.Create();
  392. int[] c, d, f, g;
  393. if (negate)
  394. {
  395. c = D; d = C; f = G; g = F;
  396. }
  397. else
  398. {
  399. c = C; d = D; f = F; g = G;
  400. }
  401. X25519Field.Apm(p.y, p.x, B, A);
  402. X25519Field.Apm(q.y, q.x, d, c);
  403. X25519Field.Mul(A, C, A);
  404. X25519Field.Mul(B, D, B);
  405. X25519Field.Mul(p.t, q.t, C);
  406. X25519Field.Mul(C, C_d2, C);
  407. X25519Field.Mul(p.z, q.z, D);
  408. X25519Field.Add(D, D, D);
  409. X25519Field.Apm(B, A, H, E);
  410. X25519Field.Apm(D, C, g, f);
  411. X25519Field.Carry(g);
  412. X25519Field.Mul(E, F, r.x);
  413. X25519Field.Mul(G, H, r.y);
  414. X25519Field.Mul(F, G, r.z);
  415. X25519Field.Mul(E, H, r.t);
  416. }
  417. private static void PointAddPrecomp(PointPrecomp p, PointAccum r)
  418. {
  419. int[] A = X25519Field.Create();
  420. int[] B = X25519Field.Create();
  421. int[] C = X25519Field.Create();
  422. int[] E = r.u;
  423. int[] F = X25519Field.Create();
  424. int[] G = X25519Field.Create();
  425. int[] H = r.v;
  426. X25519Field.Apm(r.y, r.x, B, A);
  427. X25519Field.Mul(A, p.ymx_h, A);
  428. X25519Field.Mul(B, p.ypx_h, B);
  429. X25519Field.Mul(r.u, r.v, C);
  430. X25519Field.Mul(C, p.xyd, C);
  431. X25519Field.Apm(B, A, H, E);
  432. X25519Field.Apm(r.z, C, G, F);
  433. X25519Field.Carry(G);
  434. X25519Field.Mul(E, F, r.x);
  435. X25519Field.Mul(G, H, r.y);
  436. X25519Field.Mul(F, G, r.z);
  437. }
  438. private static PointExt PointCopy(PointAccum p)
  439. {
  440. PointExt r = new PointExt();
  441. X25519Field.Copy(p.x, 0, r.x, 0);
  442. X25519Field.Copy(p.y, 0, r.y, 0);
  443. X25519Field.Copy(p.z, 0, r.z, 0);
  444. X25519Field.Mul(p.u, p.v, r.t);
  445. return r;
  446. }
  447. private static PointExt PointCopy(PointExt p)
  448. {
  449. PointExt r = new PointExt();
  450. X25519Field.Copy(p.x, 0, r.x, 0);
  451. X25519Field.Copy(p.y, 0, r.y, 0);
  452. X25519Field.Copy(p.z, 0, r.z, 0);
  453. X25519Field.Copy(p.t, 0, r.t, 0);
  454. return r;
  455. }
  456. private static void PointDouble(PointAccum r)
  457. {
  458. int[] A = X25519Field.Create();
  459. int[] B = X25519Field.Create();
  460. int[] C = X25519Field.Create();
  461. int[] E = r.u;
  462. int[] F = X25519Field.Create();
  463. int[] G = X25519Field.Create();
  464. int[] H = r.v;
  465. X25519Field.Sqr(r.x, A);
  466. X25519Field.Sqr(r.y, B);
  467. X25519Field.Sqr(r.z, C);
  468. X25519Field.Add(C, C, C);
  469. X25519Field.Apm(A, B, H, G);
  470. X25519Field.Add(r.x, r.y, E);
  471. X25519Field.Sqr(E, E);
  472. X25519Field.Sub(H, E, E);
  473. X25519Field.Add(C, G, F);
  474. X25519Field.Carry(F);
  475. X25519Field.Mul(E, F, r.x);
  476. X25519Field.Mul(G, H, r.y);
  477. X25519Field.Mul(F, G, r.z);
  478. }
  479. private static void PointExtendXY(PointAccum p)
  480. {
  481. X25519Field.One(p.z);
  482. X25519Field.Copy(p.x, 0, p.u, 0);
  483. X25519Field.Copy(p.y, 0, p.v, 0);
  484. }
  485. private static void PointExtendXY(PointExt p)
  486. {
  487. X25519Field.One(p.z);
  488. X25519Field.Mul(p.x, p.y, p.t);
  489. }
  490. private static void PointLookup(int block, int index, PointPrecomp p)
  491. {
  492. Debug.Assert(0 <= block && block < PrecompBlocks);
  493. Debug.Assert(0 <= index && index < PrecompPoints);
  494. int off = block * PrecompPoints * 3 * X25519Field.Size;
  495. for (int i = 0; i < PrecompPoints; ++i)
  496. {
  497. int mask = ((i ^ index) - 1) >> 31;
  498. Nat.CMov(X25519Field.Size, mask, precompBase, off, p.ypx_h, 0); off += X25519Field.Size;
  499. Nat.CMov(X25519Field.Size, mask, precompBase, off, p.ymx_h, 0); off += X25519Field.Size;
  500. Nat.CMov(X25519Field.Size, mask, precompBase, off, p.xyd, 0); off += X25519Field.Size;
  501. }
  502. }
  503. private static PointExt[] PointPrecompVar(PointExt p, int count)
  504. {
  505. Debug.Assert(count > 0);
  506. PointExt d = new PointExt();
  507. PointAddVar(false, p, p, d);
  508. PointExt[] table = new PointExt[count];
  509. table[0] = PointCopy(p);
  510. for (int i = 1; i < count; ++i)
  511. {
  512. PointAddVar(false, table[i - 1], d, table[i] = new PointExt());
  513. }
  514. return table;
  515. }
  516. private static void PointSetNeutral(PointAccum p)
  517. {
  518. X25519Field.Zero(p.x);
  519. X25519Field.One(p.y);
  520. X25519Field.One(p.z);
  521. X25519Field.Zero(p.u);
  522. X25519Field.One(p.v);
  523. }
  524. private static void PointSetNeutral(PointExt p)
  525. {
  526. X25519Field.Zero(p.x);
  527. X25519Field.One(p.y);
  528. X25519Field.One(p.z);
  529. X25519Field.Zero(p.t);
  530. }
  531. public static void Precompute()
  532. {
  533. lock (precompLock)
  534. {
  535. if (precompBase != null)
  536. return;
  537. // Precomputed table for the base point in verification ladder
  538. {
  539. PointExt b = new PointExt();
  540. X25519Field.Copy(B_x, 0, b.x, 0);
  541. X25519Field.Copy(B_y, 0, b.y, 0);
  542. PointExtendXY(b);
  543. precompBaseTable = PointPrecompVar(b, 1 << (WnafWidthBase - 2));
  544. }
  545. PointAccum p = new PointAccum();
  546. X25519Field.Copy(B_x, 0, p.x, 0);
  547. X25519Field.Copy(B_y, 0, p.y, 0);
  548. PointExtendXY(p);
  549. precompBase = new int[PrecompBlocks * PrecompPoints * 3 * X25519Field.Size];
  550. int off = 0;
  551. for (int b = 0; b < PrecompBlocks; ++b)
  552. {
  553. PointExt[] ds = new PointExt[PrecompTeeth];
  554. PointExt sum = new PointExt();
  555. PointSetNeutral(sum);
  556. for (int t = 0; t < PrecompTeeth; ++t)
  557. {
  558. PointExt q = PointCopy(p);
  559. PointAddVar(true, sum, q, sum);
  560. PointDouble(p);
  561. ds[t] = PointCopy(p);
  562. if (b + t != PrecompBlocks + PrecompTeeth - 2)
  563. {
  564. for (int s = 1; s < PrecompSpacing; ++s)
  565. {
  566. PointDouble(p);
  567. }
  568. }
  569. }
  570. PointExt[] points = new PointExt[PrecompPoints];
  571. int k = 0;
  572. points[k++] = sum;
  573. for (int t = 0; t < (PrecompTeeth - 1); ++t)
  574. {
  575. int size = 1 << t;
  576. for (int j = 0; j < size; ++j, ++k)
  577. {
  578. PointAddVar(false, points[k - size], ds[t], points[k] = new PointExt());
  579. }
  580. }
  581. Debug.Assert(k == PrecompPoints);
  582. for (int i = 0; i < PrecompPoints; ++i)
  583. {
  584. PointExt q = points[i];
  585. int[] x = X25519Field.Create();
  586. int[] y = X25519Field.Create();
  587. X25519Field.Add(q.z, q.z, x);
  588. // TODO[ed25519] Batch inversion
  589. X25519Field.Inv(x, y);
  590. X25519Field.Mul(q.x, y, x);
  591. X25519Field.Mul(q.y, y, y);
  592. PointPrecomp r = new PointPrecomp();
  593. X25519Field.Apm(y, x, r.ypx_h, r.ymx_h);
  594. X25519Field.Mul(x, y, r.xyd);
  595. X25519Field.Mul(r.xyd, C_d4, r.xyd);
  596. X25519Field.Normalize(r.ypx_h);
  597. X25519Field.Normalize(r.ymx_h);
  598. //X25519Field.Normalize(r.xyd);
  599. X25519Field.Copy(r.ypx_h, 0, precompBase, off); off += X25519Field.Size;
  600. X25519Field.Copy(r.ymx_h, 0, precompBase, off); off += X25519Field.Size;
  601. X25519Field.Copy(r.xyd, 0, precompBase, off); off += X25519Field.Size;
  602. }
  603. }
  604. Debug.Assert(off == precompBase.Length);
  605. }
  606. }
  607. private static void PruneScalar(byte[] n, int nOff, byte[] r)
  608. {
  609. Array.Copy(n, nOff, r, 0, ScalarBytes);
  610. r[0] &= 0xF8;
  611. r[ScalarBytes - 1] &= 0x7F;
  612. r[ScalarBytes - 1] |= 0x40;
  613. }
  614. private static byte[] ReduceScalar(byte[] n)
  615. {
  616. long x00 = Decode32(n, 0) & M32L; // x00:32/--
  617. long x01 = (Decode24(n, 4) << 4) & M32L; // x01:28/--
  618. long x02 = Decode32(n, 7) & M32L; // x02:32/--
  619. long x03 = (Decode24(n, 11) << 4) & M32L; // x03:28/--
  620. long x04 = Decode32(n, 14) & M32L; // x04:32/--
  621. long x05 = (Decode24(n, 18) << 4) & M32L; // x05:28/--
  622. long x06 = Decode32(n, 21) & M32L; // x06:32/--
  623. long x07 = (Decode24(n, 25) << 4) & M32L; // x07:28/--
  624. long x08 = Decode32(n, 28) & M32L; // x08:32/--
  625. long x09 = (Decode24(n, 32) << 4) & M32L; // x09:28/--
  626. long x10 = Decode32(n, 35) & M32L; // x10:32/--
  627. long x11 = (Decode24(n, 39) << 4) & M32L; // x11:28/--
  628. long x12 = Decode32(n, 42) & M32L; // x12:32/--
  629. long x13 = (Decode24(n, 46) << 4) & M32L; // x13:28/--
  630. long x14 = Decode32(n, 49) & M32L; // x14:32/--
  631. long x15 = (Decode24(n, 53) << 4) & M32L; // x15:28/--
  632. long x16 = Decode32(n, 56) & M32L; // x16:32/--
  633. long x17 = (Decode24(n, 60) << 4) & M32L; // x17:28/--
  634. long x18 = n[63] & 0xFFL; // x18:08/--
  635. long t;
  636. //x18 += (x17 >> 28); x17 &= M28L;
  637. x09 -= x18 * L0; // x09:34/28
  638. x10 -= x18 * L1; // x10:33/30
  639. x11 -= x18 * L2; // x11:35/28
  640. x12 -= x18 * L3; // x12:32/31
  641. x13 -= x18 * L4; // x13:28/21
  642. x17 += (x16 >> 28); x16 &= M28L; // x17:28/--, x16:28/--
  643. x08 -= x17 * L0; // x08:54/32
  644. x09 -= x17 * L1; // x09:52/51
  645. x10 -= x17 * L2; // x10:55/34
  646. x11 -= x17 * L3; // x11:51/36
  647. x12 -= x17 * L4; // x12:41/--
  648. //x16 += (x15 >> 28); x15 &= M28L;
  649. x07 -= x16 * L0; // x07:54/28
  650. x08 -= x16 * L1; // x08:54/53
  651. x09 -= x16 * L2; // x09:55/53
  652. x10 -= x16 * L3; // x10:55/52
  653. x11 -= x16 * L4; // x11:51/41
  654. x15 += (x14 >> 28); x14 &= M28L; // x15:28/--, x14:28/--
  655. x06 -= x15 * L0; // x06:54/32
  656. x07 -= x15 * L1; // x07:54/53
  657. x08 -= x15 * L2; // x08:56/--
  658. x09 -= x15 * L3; // x09:55/54
  659. x10 -= x15 * L4; // x10:55/53
  660. //x14 += (x13 >> 28); x13 &= M28L;
  661. x05 -= x14 * L0; // x05:54/28
  662. x06 -= x14 * L1; // x06:54/53
  663. x07 -= x14 * L2; // x07:56/--
  664. x08 -= x14 * L3; // x08:56/51
  665. x09 -= x14 * L4; // x09:56/--
  666. x13 += (x12 >> 28); x12 &= M28L; // x13:28/22, x12:28/--
  667. x04 -= x13 * L0; // x04:54/49
  668. x05 -= x13 * L1; // x05:54/53
  669. x06 -= x13 * L2; // x06:56/--
  670. x07 -= x13 * L3; // x07:56/52
  671. x08 -= x13 * L4; // x08:56/52
  672. x12 += (x11 >> 28); x11 &= M28L; // x12:28/24, x11:28/--
  673. x03 -= x12 * L0; // x03:54/49
  674. x04 -= x12 * L1; // x04:54/51
  675. x05 -= x12 * L2; // x05:56/--
  676. x06 -= x12 * L3; // x06:56/52
  677. x07 -= x12 * L4; // x07:56/53
  678. x11 += (x10 >> 28); x10 &= M28L; // x11:29/--, x10:28/--
  679. x02 -= x11 * L0; // x02:55/32
  680. x03 -= x11 * L1; // x03:55/--
  681. x04 -= x11 * L2; // x04:56/55
  682. x05 -= x11 * L3; // x05:56/52
  683. x06 -= x11 * L4; // x06:56/53
  684. x10 += (x09 >> 28); x09 &= M28L; // x10:29/--, x09:28/--
  685. x01 -= x10 * L0; // x01:55/28
  686. x02 -= x10 * L1; // x02:55/54
  687. x03 -= x10 * L2; // x03:56/55
  688. x04 -= x10 * L3; // x04:57/--
  689. x05 -= x10 * L4; // x05:56/53
  690. x08 += (x07 >> 28); x07 &= M28L; // x08:56/53, x07:28/--
  691. x09 += (x08 >> 28); x08 &= M28L; // x09:29/25, x08:28/--
  692. t = (x08 >> 27) & 1L;
  693. x09 += t; // x09:29/26
  694. x00 -= x09 * L0; // x00:55/53
  695. x01 -= x09 * L1; // x01:55/54
  696. x02 -= x09 * L2; // x02:57/--
  697. x03 -= x09 * L3; // x03:57/--
  698. x04 -= x09 * L4; // x04:57/42
  699. x01 += (x00 >> 28); x00 &= M28L;
  700. x02 += (x01 >> 28); x01 &= M28L;
  701. x03 += (x02 >> 28); x02 &= M28L;
  702. x04 += (x03 >> 28); x03 &= M28L;
  703. x05 += (x04 >> 28); x04 &= M28L;
  704. x06 += (x05 >> 28); x05 &= M28L;
  705. x07 += (x06 >> 28); x06 &= M28L;
  706. x08 += (x07 >> 28); x07 &= M28L;
  707. x09 = (x08 >> 28); x08 &= M28L;
  708. x09 -= t;
  709. Debug.Assert(x09 == 0L || x09 == -1L);
  710. x00 += x09 & L0;
  711. x01 += x09 & L1;
  712. x02 += x09 & L2;
  713. x03 += x09 & L3;
  714. x04 += x09 & L4;
  715. x01 += (x00 >> 28); x00 &= M28L;
  716. x02 += (x01 >> 28); x01 &= M28L;
  717. x03 += (x02 >> 28); x02 &= M28L;
  718. x04 += (x03 >> 28); x03 &= M28L;
  719. x05 += (x04 >> 28); x04 &= M28L;
  720. x06 += (x05 >> 28); x05 &= M28L;
  721. x07 += (x06 >> 28); x06 &= M28L;
  722. x08 += (x07 >> 28); x07 &= M28L;
  723. byte[] r = new byte[ScalarBytes];
  724. Encode56((ulong)(x00 | (x01 << 28)), r, 0);
  725. Encode56((ulong)(x02 | (x03 << 28)), r, 7);
  726. Encode56((ulong)(x04 | (x05 << 28)), r, 14);
  727. Encode56((ulong)(x06 | (x07 << 28)), r, 21);
  728. Encode32((uint)x08, r, 28);
  729. return r;
  730. }
  731. private static void ScalarMultBase(byte[] k, PointAccum r)
  732. {
  733. Precompute();
  734. PointSetNeutral(r);
  735. uint[] n = new uint[ScalarUints];
  736. DecodeScalar(k, 0, n);
  737. // Recode the scalar into signed-digit form, then group comb bits in each block
  738. {
  739. uint c1 = Nat.CAdd(ScalarUints, ~(int)n[0] & 1, n, L, n); Debug.Assert(c1 == 0);
  740. uint c2 = Nat.ShiftDownBit(ScalarUints, n, 1U); Debug.Assert(c2 == (1U << 31));
  741. for (int i = 0; i < ScalarUints; ++i)
  742. {
  743. n[i] = Interleave.Shuffle2(n[i]);
  744. }
  745. }
  746. PointPrecomp p = new PointPrecomp();
  747. int cOff = (PrecompSpacing - 1) * PrecompTeeth;
  748. for (; ; )
  749. {
  750. for (int b = 0; b < PrecompBlocks; ++b)
  751. {
  752. uint w = n[b] >> cOff;
  753. int sign = (int)(w >> (PrecompTeeth - 1)) & 1;
  754. int abs = ((int)w ^ -sign) & PrecompMask;
  755. Debug.Assert(sign == 0 || sign == 1);
  756. Debug.Assert(0 <= abs && abs < PrecompPoints);
  757. PointLookup(b, abs, p);
  758. X25519Field.CSwap(sign, p.ypx_h, p.ymx_h);
  759. X25519Field.CNegate(sign, p.xyd);
  760. PointAddPrecomp(p, r);
  761. }
  762. if ((cOff -= PrecompTeeth) < 0)
  763. break;
  764. PointDouble(r);
  765. }
  766. }
  767. private static void ScalarMultBaseEncoded(byte[] k, byte[] r, int rOff)
  768. {
  769. PointAccum p = new PointAccum();
  770. ScalarMultBase(k, p);
  771. EncodePoint(p, r, rOff);
  772. }
  773. internal static void ScalarMultBaseYZ(byte[] k, int kOff, int[] y, int[] z)
  774. {
  775. byte[] n = new byte[ScalarBytes];
  776. PruneScalar(k, kOff, n);
  777. PointAccum p = new PointAccum();
  778. ScalarMultBase(n, p);
  779. X25519Field.Copy(p.y, 0, y, 0);
  780. X25519Field.Copy(p.z, 0, z, 0);
  781. }
  782. private static void ScalarMultStraussVar(uint[] nb, uint[] np, PointExt p, PointAccum r)
  783. {
  784. Precompute();
  785. int width = 5;
  786. sbyte[] ws_b = GetWnaf(nb, WnafWidthBase);
  787. sbyte[] ws_p = GetWnaf(np, width);
  788. PointExt[] tp = PointPrecompVar(p, 1 << (width - 2));
  789. PointSetNeutral(r);
  790. int bit = 255;
  791. while (bit > 0 && (ws_b[bit] | ws_p[bit]) == 0)
  792. {
  793. --bit;
  794. }
  795. for (; ; )
  796. {
  797. int wb = ws_b[bit];
  798. if (wb != 0)
  799. {
  800. int sign = wb >> 31;
  801. int index = (wb ^ sign) >> 1;
  802. PointAddVar((sign != 0), precompBaseTable[index], r);
  803. }
  804. int wp = ws_p[bit];
  805. if (wp != 0)
  806. {
  807. int sign = wp >> 31;
  808. int index = (wp ^ sign) >> 1;
  809. PointAddVar((sign != 0), tp[index], r);
  810. }
  811. if (--bit < 0)
  812. break;
  813. PointDouble(r);
  814. }
  815. }
  816. public static void Sign(byte[] sk, int skOff, byte[] m, int mOff, int mLen, byte[] sig, int sigOff)
  817. {
  818. byte[] ctx = null;
  819. byte phflag = 0x00;
  820. ImplSign(sk, skOff, ctx, phflag, m, mOff, mLen, sig, sigOff);
  821. }
  822. public static void Sign(byte[] sk, int skOff, byte[] pk, int pkOff, byte[] m, int mOff, int mLen, byte[] sig, int sigOff)
  823. {
  824. byte[] ctx = null;
  825. byte phflag = 0x00;
  826. ImplSign(sk, skOff, pk, pkOff, ctx, phflag, m, mOff, mLen, sig, sigOff);
  827. }
  828. public static void Sign(byte[] sk, int skOff, byte[] ctx, byte[] m, int mOff, int mLen, byte[] sig, int sigOff)
  829. {
  830. byte phflag = 0x00;
  831. ImplSign(sk, skOff, ctx, phflag, m, mOff, mLen, sig, sigOff);
  832. }
  833. public static void Sign(byte[] sk, int skOff, byte[] pk, int pkOff, byte[] ctx, byte[] m, int mOff, int mLen, byte[] sig, int sigOff)
  834. {
  835. byte phflag = 0x00;
  836. ImplSign(sk, skOff, pk, pkOff, ctx, phflag, m, mOff, mLen, sig, sigOff);
  837. }
  838. public static void SignPrehash(byte[] sk, int skOff, byte[] ctx, byte[] ph, int phOff, byte[] sig, int sigOff)
  839. {
  840. byte phflag = 0x01;
  841. ImplSign(sk, skOff, ctx, phflag, ph, phOff, PrehashSize, sig, sigOff);
  842. }
  843. public static void SignPrehash(byte[] sk, int skOff, byte[] pk, int pkOff, byte[] ctx, byte[] ph, int phOff, byte[] sig, int sigOff)
  844. {
  845. byte phflag = 0x01;
  846. ImplSign(sk, skOff, pk, pkOff, ctx, phflag, ph, phOff, PrehashSize, sig, sigOff);
  847. }
  848. public static void SignPrehash(byte[] sk, int skOff, byte[] ctx, IDigest ph, byte[] sig, int sigOff)
  849. {
  850. byte[] m = new byte[PrehashSize];
  851. if (PrehashSize != ph.DoFinal(m, 0))
  852. throw new ArgumentException("ph");
  853. byte phflag = 0x01;
  854. ImplSign(sk, skOff, ctx, phflag, m, 0, m.Length, sig, sigOff);
  855. }
  856. public static void SignPrehash(byte[] sk, int skOff, byte[] pk, int pkOff, byte[] ctx, IDigest ph, byte[] sig, int sigOff)
  857. {
  858. byte[] m = new byte[PrehashSize];
  859. if (PrehashSize != ph.DoFinal(m, 0))
  860. throw new ArgumentException("ph");
  861. byte phflag = 0x01;
  862. ImplSign(sk, skOff, pk, pkOff, ctx, phflag, m, 0, m.Length, sig, sigOff);
  863. }
  864. public static bool Verify(byte[] sig, int sigOff, byte[] pk, int pkOff, byte[] m, int mOff, int mLen)
  865. {
  866. byte[] ctx = null;
  867. byte phflag = 0x00;
  868. return ImplVerify(sig, sigOff, pk, pkOff, ctx, phflag, m, mOff, mLen);
  869. }
  870. public static bool Verify(byte[] sig, int sigOff, byte[] pk, int pkOff, byte[] ctx, byte[] m, int mOff, int mLen)
  871. {
  872. byte phflag = 0x00;
  873. return ImplVerify(sig, sigOff, pk, pkOff, ctx, phflag, m, mOff, mLen);
  874. }
  875. public static bool VerifyPrehash(byte[] sig, int sigOff, byte[] pk, int pkOff, byte[] ctx, byte[] ph, int phOff)
  876. {
  877. byte phflag = 0x01;
  878. return ImplVerify(sig, sigOff, pk, pkOff, ctx, phflag, ph, phOff, PrehashSize);
  879. }
  880. public static bool VerifyPrehash(byte[] sig, int sigOff, byte[] pk, int pkOff, byte[] ctx, IDigest ph)
  881. {
  882. byte[] m = new byte[PrehashSize];
  883. if (PrehashSize != ph.DoFinal(m, 0))
  884. throw new ArgumentException("ph");
  885. byte phflag = 0x01;
  886. return ImplVerify(sig, sigOff, pk, pkOff, ctx, phflag, m, 0, m.Length);
  887. }
  888. }
  889. }
  890. #pragma warning restore
  891. #endif