SecP384R1Curve.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 SecP384R1Curve
  10. : AbstractFpCurve
  11. {
  12. public static readonly BigInteger q = SecP384R1FieldElement.Q;
  13. private const int SECP384R1_DEFAULT_COORDS = COORD_JACOBIAN;
  14. private const int SECP384R1_FE_INTS = 12;
  15. private static readonly ECFieldElement[] SECP384R1_AFFINE_ZS = new ECFieldElement[] { new SecP384R1FieldElement(BigInteger.One) };
  16. protected readonly SecP384R1Point m_infinity;
  17. public SecP384R1Curve()
  18. : base(q)
  19. {
  20. this.m_infinity = new SecP384R1Point(this, null, null);
  21. this.m_a = FromBigInteger(new BigInteger(1,
  22. Hex.DecodeStrict("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFC")));
  23. this.m_b = FromBigInteger(new BigInteger(1,
  24. Hex.DecodeStrict("B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE8141120314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF")));
  25. this.m_order = new BigInteger(1, Hex.DecodeStrict("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973"));
  26. this.m_cofactor = BigInteger.One;
  27. this.m_coord = SECP384R1_DEFAULT_COORDS;
  28. }
  29. protected override ECCurve CloneCurve()
  30. {
  31. return new SecP384R1Curve();
  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 SecP384R1FieldElement(x);
  58. }
  59. protected internal override ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y, bool withCompression)
  60. {
  61. return new SecP384R1Point(this, x, y, withCompression);
  62. }
  63. protected internal override ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y, ECFieldElement[] zs, bool withCompression)
  64. {
  65. return new SecP384R1Point(this, x, y, zs, withCompression);
  66. }
  67. public override ECLookupTable CreateCacheSafeLookupTable(ECPoint[] points, int off, int len)
  68. {
  69. uint[] table = new uint[len * SECP384R1_FE_INTS * 2];
  70. {
  71. int pos = 0;
  72. for (int i = 0; i < len; ++i)
  73. {
  74. ECPoint p = points[off + i];
  75. Nat.Copy(SECP384R1_FE_INTS, ((SecP384R1FieldElement)p.RawXCoord).x, 0, table, pos); pos += SECP384R1_FE_INTS;
  76. Nat.Copy(SECP384R1_FE_INTS, ((SecP384R1FieldElement)p.RawYCoord).x, 0, table, pos); pos += SECP384R1_FE_INTS;
  77. }
  78. }
  79. return new SecP384R1LookupTable(this, table, len);
  80. }
  81. public override ECFieldElement RandomFieldElement(SecureRandom r)
  82. {
  83. uint[] x = Nat.Create(12);
  84. SecP384R1Field.Random(r, x);
  85. return new SecP384R1FieldElement(x);
  86. }
  87. public override ECFieldElement RandomFieldElementMult(SecureRandom r)
  88. {
  89. uint[] x = Nat.Create(12);
  90. SecP384R1Field.RandomMult(r, x);
  91. return new SecP384R1FieldElement(x);
  92. }
  93. private class SecP384R1LookupTable
  94. : AbstractECLookupTable
  95. {
  96. private readonly SecP384R1Curve m_outer;
  97. private readonly uint[] m_table;
  98. private readonly int m_size;
  99. internal SecP384R1LookupTable(SecP384R1Curve 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 = Nat.Create(SECP384R1_FE_INTS), y = Nat.Create(SECP384R1_FE_INTS);
  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 < SECP384R1_FE_INTS; ++j)
  117. {
  118. x[j] ^= m_table[pos + j] & MASK;
  119. y[j] ^= m_table[pos + SECP384R1_FE_INTS + j] & MASK;
  120. }
  121. pos += (SECP384R1_FE_INTS * 2);
  122. }
  123. return CreatePoint(x, y);
  124. }
  125. public override ECPoint LookupVar(int index)
  126. {
  127. uint[] x = Nat.Create(SECP384R1_FE_INTS), y = Nat.Create(SECP384R1_FE_INTS);
  128. int pos = index * SECP384R1_FE_INTS * 2;
  129. for (int j = 0; j < SECP384R1_FE_INTS; ++j)
  130. {
  131. x[j] = m_table[pos + j];
  132. y[j] = m_table[pos + SECP384R1_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 SecP384R1FieldElement(x), new SecP384R1FieldElement(y), SECP384R1_AFFINE_ZS, false);
  139. }
  140. }
  141. }
  142. }
  143. #pragma warning restore
  144. #endif