GMNamedCurves.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X9;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math.EC;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Multiplier;
  9. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  10. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Collections;
  11. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders;
  12. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.GM
  13. {
  14. public sealed class GMNamedCurves
  15. {
  16. private GMNamedCurves()
  17. {
  18. }
  19. private static X9ECPoint ConfigureBasepoint(ECCurve curve, string encoding)
  20. {
  21. X9ECPoint G = new X9ECPoint(curve, Hex.DecodeStrict(encoding));
  22. WNafUtilities.ConfigureBasepoint(G.Point);
  23. return G;
  24. }
  25. private static ECCurve ConfigureCurve(ECCurve curve)
  26. {
  27. return curve;
  28. }
  29. private static BigInteger FromHex(string hex)
  30. {
  31. return new BigInteger(1, Hex.DecodeStrict(hex));
  32. }
  33. /*
  34. * sm2p256v1
  35. */
  36. internal class SM2P256V1Holder
  37. : X9ECParametersHolder
  38. {
  39. private SM2P256V1Holder() {}
  40. internal static readonly X9ECParametersHolder Instance = new SM2P256V1Holder();
  41. protected override X9ECParameters CreateParameters()
  42. {
  43. BigInteger p = FromHex("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF");
  44. BigInteger a = FromHex("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFC");
  45. BigInteger b = FromHex("28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93");
  46. byte[] S = null;
  47. BigInteger n = FromHex("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123");
  48. BigInteger h = BigInteger.One;
  49. ECCurve curve = ConfigureCurve(new FpCurve(p, a, b, n, h));
  50. X9ECPoint G = ConfigureBasepoint(curve,
  51. "0432C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0");
  52. return new X9ECParameters(curve, G, n, h, S);
  53. }
  54. }
  55. /*
  56. * wapip192v1
  57. */
  58. internal class WapiP192V1Holder
  59. : X9ECParametersHolder
  60. {
  61. private WapiP192V1Holder() { }
  62. internal static readonly X9ECParametersHolder Instance = new WapiP192V1Holder();
  63. protected override X9ECParameters CreateParameters()
  64. {
  65. BigInteger p = FromHex("BDB6F4FE3E8B1D9E0DA8C0D46F4C318CEFE4AFE3B6B8551F");
  66. BigInteger a = FromHex("BB8E5E8FBC115E139FE6A814FE48AAA6F0ADA1AA5DF91985");
  67. BigInteger b = FromHex("1854BEBDC31B21B7AEFC80AB0ECD10D5B1B3308E6DBF11C1");
  68. byte[] S = null;
  69. BigInteger n = FromHex("BDB6F4FE3E8B1D9E0DA8C0D40FC962195DFAE76F56564677");
  70. BigInteger h = BigInteger.One;
  71. ECCurve curve = ConfigureCurve(new FpCurve(p, a, b, n, h));
  72. X9ECPoint G = ConfigureBasepoint(curve,
  73. "044AD5F7048DE709AD51236DE65E4D4B482C836DC6E410664002BB3A02D4AAADACAE24817A4CA3A1B014B5270432DB27D2");
  74. return new X9ECParameters(curve, G, n, h, S);
  75. }
  76. }
  77. private static readonly IDictionary objIds = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  78. private static readonly IDictionary curves = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  79. private static readonly IDictionary names = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  80. private static void DefineCurve(
  81. string name,
  82. DerObjectIdentifier oid,
  83. X9ECParametersHolder holder)
  84. {
  85. objIds.Add(BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.ToUpperInvariant(name), oid);
  86. names.Add(oid, name);
  87. curves.Add(oid, holder);
  88. }
  89. static GMNamedCurves()
  90. {
  91. DefineCurve("wapip192v1", GMObjectIdentifiers.wapip192v1, WapiP192V1Holder.Instance);
  92. DefineCurve("sm2p256v1", GMObjectIdentifiers.sm2p256v1, SM2P256V1Holder.Instance);
  93. }
  94. public static X9ECParameters GetByName(
  95. string name)
  96. {
  97. DerObjectIdentifier oid = GetOid(name);
  98. return oid == null ? null : GetByOid(oid);
  99. }
  100. /**
  101. * return the X9ECParameters object for the named curve represented by
  102. * the passed in object identifier. Null if the curve isn't present.
  103. *
  104. * @param oid an object identifier representing a named curve, if present.
  105. */
  106. public static X9ECParameters GetByOid(
  107. DerObjectIdentifier oid)
  108. {
  109. X9ECParametersHolder holder = (X9ECParametersHolder)curves[oid];
  110. return holder == null ? null : holder.Parameters;
  111. }
  112. /**
  113. * return the object identifier signified by the passed in name. Null
  114. * if there is no object identifier associated with name.
  115. *
  116. * @return the object identifier associated with name, if present.
  117. */
  118. public static DerObjectIdentifier GetOid(
  119. string name)
  120. {
  121. return (DerObjectIdentifier)objIds[BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.ToUpperInvariant(name)];
  122. }
  123. /**
  124. * return the named curve name represented by the given object identifier.
  125. */
  126. public static string GetName(
  127. DerObjectIdentifier oid)
  128. {
  129. return (string)names[oid];
  130. }
  131. /**
  132. * returns an enumeration containing the name strings for curves
  133. * contained in this structure.
  134. */
  135. public static IEnumerable Names
  136. {
  137. get { return new EnumerableProxy(names.Values); }
  138. }
  139. }
  140. }
  141. #pragma warning restore
  142. #endif