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