Curve25519.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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.Utilities.Encoders;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Djb
  7. {
  8. internal class Curve25519
  9. : AbstractFpCurve
  10. {
  11. public static readonly BigInteger q = Nat256.ToBigInteger(Curve25519Field.P);
  12. private const int Curve25519_DEFAULT_COORDS = COORD_JACOBIAN_MODIFIED;
  13. private const int CURVE25519_FE_INTS = 8;
  14. protected readonly Curve25519Point m_infinity;
  15. public Curve25519()
  16. : base(q)
  17. {
  18. this.m_infinity = new Curve25519Point(this, null, null);
  19. this.m_a = FromBigInteger(new BigInteger(1,
  20. Hex.Decode("2AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA984914A144")));
  21. this.m_b = FromBigInteger(new BigInteger(1,
  22. Hex.Decode("7B425ED097B425ED097B425ED097B425ED097B425ED097B4260B5E9C7710C864")));
  23. this.m_order = new BigInteger(1, Hex.Decode("1000000000000000000000000000000014DEF9DEA2F79CD65812631A5CF5D3ED"));
  24. this.m_cofactor = BigInteger.ValueOf(8);
  25. this.m_coord = Curve25519_DEFAULT_COORDS;
  26. }
  27. protected override ECCurve CloneCurve()
  28. {
  29. return new Curve25519();
  30. }
  31. public override bool SupportsCoordinateSystem(int coord)
  32. {
  33. switch (coord)
  34. {
  35. case COORD_JACOBIAN_MODIFIED:
  36. return true;
  37. default:
  38. return false;
  39. }
  40. }
  41. public virtual BigInteger Q
  42. {
  43. get { return q; }
  44. }
  45. public override ECPoint Infinity
  46. {
  47. get { return m_infinity; }
  48. }
  49. public override int FieldSize
  50. {
  51. get { return q.BitLength; }
  52. }
  53. public override ECFieldElement FromBigInteger(BigInteger x)
  54. {
  55. return new Curve25519FieldElement(x);
  56. }
  57. protected internal override ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y, bool withCompression)
  58. {
  59. return new Curve25519Point(this, x, y, withCompression);
  60. }
  61. protected internal override ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y, ECFieldElement[] zs, bool withCompression)
  62. {
  63. return new Curve25519Point(this, x, y, zs, withCompression);
  64. }
  65. public override ECLookupTable CreateCacheSafeLookupTable(ECPoint[] points, int off, int len)
  66. {
  67. uint[] table = new uint[len * CURVE25519_FE_INTS * 2];
  68. {
  69. int pos = 0;
  70. for (int i = 0; i < len; ++i)
  71. {
  72. ECPoint p = points[off + i];
  73. Nat256.Copy(((Curve25519FieldElement)p.RawXCoord).x, 0, table, pos); pos += CURVE25519_FE_INTS;
  74. Nat256.Copy(((Curve25519FieldElement)p.RawYCoord).x, 0, table, pos); pos += CURVE25519_FE_INTS;
  75. }
  76. }
  77. return new Curve25519LookupTable(this, table, len);
  78. }
  79. private class Curve25519LookupTable
  80. : ECLookupTable
  81. {
  82. private readonly Curve25519 m_outer;
  83. private readonly uint[] m_table;
  84. private readonly int m_size;
  85. internal Curve25519LookupTable(Curve25519 outer, uint[] table, int size)
  86. {
  87. this.m_outer = outer;
  88. this.m_table = table;
  89. this.m_size = size;
  90. }
  91. public virtual int Size
  92. {
  93. get { return m_size; }
  94. }
  95. public virtual ECPoint Lookup(int index)
  96. {
  97. uint[] x = Nat256.Create(), y = Nat256.Create();
  98. int pos = 0;
  99. for (int i = 0; i < m_size; ++i)
  100. {
  101. uint MASK = (uint)(((i ^ index) - 1) >> 31);
  102. for (int j = 0; j < CURVE25519_FE_INTS; ++j)
  103. {
  104. x[j] ^= m_table[pos + j] & MASK;
  105. y[j] ^= m_table[pos + CURVE25519_FE_INTS + j] & MASK;
  106. }
  107. pos += (CURVE25519_FE_INTS * 2);
  108. }
  109. return m_outer.CreateRawPoint(new Curve25519FieldElement(x), new Curve25519FieldElement(y), false);
  110. }
  111. }
  112. }
  113. }
  114. #pragma warning restore
  115. #endif