GMNamedCurves.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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.Endo;
  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 ECCurve ConfigureCurve(ECCurve curve)
  20. {
  21. return curve;
  22. }
  23. private static BigInteger FromHex(string hex)
  24. {
  25. return new BigInteger(1, Hex.Decode(hex));
  26. }
  27. /*
  28. * sm2p256v1
  29. */
  30. internal class SM2P256V1Holder
  31. : X9ECParametersHolder
  32. {
  33. private SM2P256V1Holder() {}
  34. internal static readonly X9ECParametersHolder Instance = new SM2P256V1Holder();
  35. protected override X9ECParameters CreateParameters()
  36. {
  37. BigInteger p = FromHex("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF");
  38. BigInteger a = FromHex("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFC");
  39. BigInteger b = FromHex("28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93");
  40. byte[] S = null;
  41. BigInteger n = FromHex("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123");
  42. BigInteger h = BigInteger.One;
  43. ECCurve curve = ConfigureCurve(new FpCurve(p, a, b, n, h));
  44. X9ECPoint G = new X9ECPoint(curve, Hex.Decode("04"
  45. + "32C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7"
  46. + "BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0"));
  47. return new X9ECParameters(curve, G, n, h, S);
  48. }
  49. }
  50. /*
  51. * wapip192v1
  52. */
  53. internal class WapiP192V1Holder
  54. : X9ECParametersHolder
  55. {
  56. private WapiP192V1Holder() { }
  57. internal static readonly X9ECParametersHolder Instance = new WapiP192V1Holder();
  58. protected override X9ECParameters CreateParameters()
  59. {
  60. BigInteger p = FromHex("BDB6F4FE3E8B1D9E0DA8C0D46F4C318CEFE4AFE3B6B8551F");
  61. BigInteger a = FromHex("BB8E5E8FBC115E139FE6A814FE48AAA6F0ADA1AA5DF91985");
  62. BigInteger b = FromHex("1854BEBDC31B21B7AEFC80AB0ECD10D5B1B3308E6DBF11C1");
  63. byte[] S = null;
  64. BigInteger n = FromHex("BDB6F4FE3E8B1D9E0DA8C0D40FC962195DFAE76F56564677");
  65. BigInteger h = BigInteger.One;
  66. ECCurve curve = ConfigureCurve(new FpCurve(p, a, b, n, h));
  67. X9ECPoint G = new X9ECPoint(curve, Hex.Decode("04"
  68. + "4AD5F7048DE709AD51236DE6" + "5E4D4B482C836DC6E4106640"
  69. + "02BB3A02D4AAADACAE24817A" + "4CA3A1B014B5270432DB27D2"));
  70. return new X9ECParameters(curve, G, n, h, S);
  71. }
  72. }
  73. private static readonly IDictionary objIds = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  74. private static readonly IDictionary curves = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  75. private static readonly IDictionary names = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  76. private static void DefineCurve(
  77. string name,
  78. DerObjectIdentifier oid,
  79. X9ECParametersHolder holder)
  80. {
  81. objIds.Add(BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.ToUpperInvariant(name), oid);
  82. names.Add(oid, name);
  83. curves.Add(oid, holder);
  84. }
  85. static GMNamedCurves()
  86. {
  87. DefineCurve("wapip192v1", GMObjectIdentifiers.wapip192v1, WapiP192V1Holder.Instance);
  88. DefineCurve("sm2p256v1", GMObjectIdentifiers.sm2p256v1, SM2P256V1Holder.Instance);
  89. }
  90. public static X9ECParameters GetByName(
  91. string name)
  92. {
  93. DerObjectIdentifier oid = GetOid(name);
  94. return oid == null ? null : GetByOid(oid);
  95. }
  96. /**
  97. * return the X9ECParameters object for the named curve represented by
  98. * the passed in object identifier. Null if the curve isn't present.
  99. *
  100. * @param oid an object identifier representing a named curve, if present.
  101. */
  102. public static X9ECParameters GetByOid(
  103. DerObjectIdentifier oid)
  104. {
  105. X9ECParametersHolder holder = (X9ECParametersHolder)curves[oid];
  106. return holder == null ? null : holder.Parameters;
  107. }
  108. /**
  109. * return the object identifier signified by the passed in name. Null
  110. * if there is no object identifier associated with name.
  111. *
  112. * @return the object identifier associated with name, if present.
  113. */
  114. public static DerObjectIdentifier GetOid(
  115. string name)
  116. {
  117. return (DerObjectIdentifier)objIds[BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.ToUpperInvariant(name)];
  118. }
  119. /**
  120. * return the named curve name represented by the given object identifier.
  121. */
  122. public static string GetName(
  123. DerObjectIdentifier oid)
  124. {
  125. return (string)names[oid];
  126. }
  127. /**
  128. * returns an enumeration containing the name strings for curves
  129. * contained in this structure.
  130. */
  131. public static IEnumerable Names
  132. {
  133. get { return new EnumerableProxy(names.Values); }
  134. }
  135. }
  136. }
  137. #pragma warning restore
  138. #endif