SecP256R1Curve.cs 4.5 KB

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