Curve25519.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders;
  7. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Djb
  8. {
  9. internal class Curve25519
  10. : AbstractFpCurve
  11. {
  12. public static readonly BigInteger q = Curve25519FieldElement.Q;
  13. private static readonly BigInteger C_a = new BigInteger(1, Hex.DecodeStrict("2AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA984914A144"));
  14. private static readonly BigInteger C_b = new BigInteger(1, Hex.DecodeStrict("7B425ED097B425ED097B425ED097B425ED097B425ED097B4260B5E9C7710C864"));
  15. private const int CURVE25519_DEFAULT_COORDS = COORD_JACOBIAN_MODIFIED;
  16. private const int CURVE25519_FE_INTS = 8;
  17. private static readonly ECFieldElement[] CURVE25519_AFFINE_ZS = new ECFieldElement[] {
  18. new Curve25519FieldElement(BigInteger.One), new Curve25519FieldElement(C_a) };
  19. protected readonly Curve25519Point m_infinity;
  20. public Curve25519()
  21. : base(q)
  22. {
  23. this.m_infinity = new Curve25519Point(this, null, null);
  24. this.m_a = FromBigInteger(C_a);
  25. this.m_b = FromBigInteger(C_b);
  26. this.m_order = new BigInteger(1, Hex.DecodeStrict("1000000000000000000000000000000014DEF9DEA2F79CD65812631A5CF5D3ED"));
  27. this.m_cofactor = BigInteger.ValueOf(8);
  28. this.m_coord = CURVE25519_DEFAULT_COORDS;
  29. }
  30. protected override ECCurve CloneCurve()
  31. {
  32. return new Curve25519();
  33. }
  34. public override bool SupportsCoordinateSystem(int coord)
  35. {
  36. switch (coord)
  37. {
  38. case COORD_JACOBIAN_MODIFIED:
  39. return true;
  40. default:
  41. return false;
  42. }
  43. }
  44. public virtual BigInteger Q
  45. {
  46. get { return q; }
  47. }
  48. public override ECPoint Infinity
  49. {
  50. get { return m_infinity; }
  51. }
  52. public override int FieldSize
  53. {
  54. get { return q.BitLength; }
  55. }
  56. public override ECFieldElement FromBigInteger(BigInteger x)
  57. {
  58. return new Curve25519FieldElement(x);
  59. }
  60. protected internal override ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y, bool withCompression)
  61. {
  62. return new Curve25519Point(this, x, y, withCompression);
  63. }
  64. protected internal override ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y, ECFieldElement[] zs, bool withCompression)
  65. {
  66. return new Curve25519Point(this, x, y, zs, withCompression);
  67. }
  68. public override ECLookupTable CreateCacheSafeLookupTable(ECPoint[] points, int off, int len)
  69. {
  70. uint[] table = new uint[len * CURVE25519_FE_INTS * 2];
  71. {
  72. int pos = 0;
  73. for (int i = 0; i < len; ++i)
  74. {
  75. ECPoint p = points[off + i];
  76. Nat256.Copy(((Curve25519FieldElement)p.RawXCoord).x, 0, table, pos); pos += CURVE25519_FE_INTS;
  77. Nat256.Copy(((Curve25519FieldElement)p.RawYCoord).x, 0, table, pos); pos += CURVE25519_FE_INTS;
  78. }
  79. }
  80. return new Curve25519LookupTable(this, table, len);
  81. }
  82. public override ECFieldElement RandomFieldElement(SecureRandom r)
  83. {
  84. uint[] x = Nat256.Create();
  85. Curve25519Field.Random(r, x);
  86. return new Curve25519FieldElement(x);
  87. }
  88. public override ECFieldElement RandomFieldElementMult(SecureRandom r)
  89. {
  90. uint[] x = Nat256.Create();
  91. Curve25519Field.RandomMult(r, x);
  92. return new Curve25519FieldElement(x);
  93. }
  94. private class Curve25519LookupTable
  95. : AbstractECLookupTable
  96. {
  97. private readonly Curve25519 m_outer;
  98. private readonly uint[] m_table;
  99. private readonly int m_size;
  100. internal Curve25519LookupTable(Curve25519 outer, uint[] table, int size)
  101. {
  102. this.m_outer = outer;
  103. this.m_table = table;
  104. this.m_size = size;
  105. }
  106. public override int Size
  107. {
  108. get { return m_size; }
  109. }
  110. public override ECPoint Lookup(int index)
  111. {
  112. uint[] x = Nat256.Create(), y = Nat256.Create();
  113. int pos = 0;
  114. for (int i = 0; i < m_size; ++i)
  115. {
  116. uint MASK = (uint)(((i ^ index) - 1) >> 31);
  117. for (int j = 0; j < CURVE25519_FE_INTS; ++j)
  118. {
  119. x[j] ^= m_table[pos + j] & MASK;
  120. y[j] ^= m_table[pos + CURVE25519_FE_INTS + j] & MASK;
  121. }
  122. pos += (CURVE25519_FE_INTS * 2);
  123. }
  124. return CreatePoint(x, y);
  125. }
  126. public override ECPoint LookupVar(int index)
  127. {
  128. uint[] x = Nat256.Create(), y = Nat256.Create();
  129. int pos = index * CURVE25519_FE_INTS * 2;
  130. for (int j = 0; j < CURVE25519_FE_INTS; ++j)
  131. {
  132. x[j] = m_table[pos + j];
  133. y[j] = m_table[pos + CURVE25519_FE_INTS + j];
  134. }
  135. return CreatePoint(x, y);
  136. }
  137. private ECPoint CreatePoint(uint[] x, uint[] y)
  138. {
  139. return m_outer.CreateRawPoint(new Curve25519FieldElement(x), new Curve25519FieldElement(y), CURVE25519_AFFINE_ZS, false);
  140. }
  141. }
  142. }
  143. }
  144. #pragma warning restore
  145. #endif