GMNamedCurves.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections.Generic;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.X9;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math;
  7. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC;
  8. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Multiplier;
  9. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.Collections;
  10. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders;
  11. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.GM
  12. {
  13. /// <summary>Elliptic curve registry for GM.</summary>
  14. public static class GMNamedCurves
  15. {
  16. private static X9ECPoint ConfigureBasepoint(ECCurve curve, string encoding)
  17. {
  18. X9ECPoint G = new X9ECPoint(curve, Hex.DecodeStrict(encoding));
  19. WNafUtilities.ConfigureBasepoint(G.Point);
  20. return G;
  21. }
  22. private static ECCurve ConfigureCurve(ECCurve curve)
  23. {
  24. return curve;
  25. }
  26. private static BigInteger FromHex(string hex)
  27. {
  28. return new BigInteger(1, Hex.DecodeStrict(hex));
  29. }
  30. internal class SM2P256V1Holder
  31. : X9ECParametersHolder
  32. {
  33. private SM2P256V1Holder() {}
  34. internal static readonly X9ECParametersHolder Instance = new SM2P256V1Holder();
  35. protected override ECCurve CreateCurve()
  36. {
  37. BigInteger p = FromHex("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF");
  38. BigInteger a = FromHex("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFC");
  39. BigInteger b = FromHex("28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93");
  40. BigInteger n = FromHex("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123");
  41. BigInteger h = BigInteger.One;
  42. return ConfigureCurve(new FpCurve(p, a, b, n, h, true));
  43. }
  44. protected override X9ECParameters CreateParameters()
  45. {
  46. byte[] S = null;
  47. ECCurve curve = Curve;
  48. X9ECPoint G = ConfigureBasepoint(curve,
  49. "0432C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0");
  50. return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S);
  51. }
  52. }
  53. internal class WapiP192V1Holder
  54. : X9ECParametersHolder
  55. {
  56. private WapiP192V1Holder() { }
  57. internal static readonly X9ECParametersHolder Instance = new WapiP192V1Holder();
  58. protected override ECCurve CreateCurve()
  59. {
  60. BigInteger p = FromHex("BDB6F4FE3E8B1D9E0DA8C0D46F4C318CEFE4AFE3B6B8551F");
  61. BigInteger a = FromHex("BB8E5E8FBC115E139FE6A814FE48AAA6F0ADA1AA5DF91985");
  62. BigInteger b = FromHex("1854BEBDC31B21B7AEFC80AB0ECD10D5B1B3308E6DBF11C1");
  63. BigInteger n = FromHex("BDB6F4FE3E8B1D9E0DA8C0D40FC962195DFAE76F56564677");
  64. BigInteger h = BigInteger.One;
  65. return ConfigureCurve(new FpCurve(p, a, b, n, h, true));
  66. }
  67. protected override X9ECParameters CreateParameters()
  68. {
  69. byte[] S = null;
  70. ECCurve curve = Curve;
  71. X9ECPoint G = ConfigureBasepoint(curve,
  72. "044AD5F7048DE709AD51236DE65E4D4B482C836DC6E410664002BB3A02D4AAADACAE24817A4CA3A1B014B5270432DB27D2");
  73. return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S);
  74. }
  75. }
  76. private static readonly Dictionary<string, DerObjectIdentifier> objIds =
  77. new Dictionary<string, DerObjectIdentifier>(StringComparer.OrdinalIgnoreCase);
  78. private static readonly Dictionary<DerObjectIdentifier, X9ECParametersHolder> curves =
  79. new Dictionary<DerObjectIdentifier, X9ECParametersHolder>();
  80. private static readonly Dictionary<DerObjectIdentifier, string> names =
  81. new Dictionary<DerObjectIdentifier, string>();
  82. private static void DefineCurve(string name, DerObjectIdentifier oid, X9ECParametersHolder holder)
  83. {
  84. objIds.Add(name, oid);
  85. names.Add(oid, name);
  86. curves.Add(oid, holder);
  87. }
  88. static GMNamedCurves()
  89. {
  90. DefineCurve("wapip192v1", GMObjectIdentifiers.wapip192v1, WapiP192V1Holder.Instance);
  91. DefineCurve("sm2p256v1", GMObjectIdentifiers.sm2p256v1, SM2P256V1Holder.Instance);
  92. }
  93. /// <summary>Look up the <see cref="X9ECParameters"/> for the curve with the given name.</summary>
  94. /// <param name="name">The name of the curve.</param>
  95. public static X9ECParameters GetByName(string name)
  96. {
  97. DerObjectIdentifier oid = GetOid(name);
  98. return oid == null ? null : GetByOid(oid);
  99. }
  100. /// <summary>Look up an <see cref="X9ECParametersHolder"/> for the curve with the given name.</summary>
  101. /// <remarks>
  102. /// Allows accessing the <see cref="ECCurve">curve</see> without necessarily triggering the creation of the
  103. /// full <see cref="X9ECParameters"/>.
  104. /// </remarks>
  105. /// <param name="name">The name of the curve.</param>
  106. public static X9ECParametersHolder GetByNameLazy(string name)
  107. {
  108. DerObjectIdentifier oid = GetOid(name);
  109. return oid == null ? null : GetByOidLazy(oid);
  110. }
  111. /// <summary>Look up the <see cref="X9ECParameters"/> for the curve with the given
  112. /// <see cref="DerObjectIdentifier">OID</see>.</summary>
  113. /// <param name="oid">The <see cref="DerObjectIdentifier">OID</see> for the curve.</param>
  114. public static X9ECParameters GetByOid(DerObjectIdentifier oid)
  115. {
  116. return GetByOidLazy(oid)?.Parameters;
  117. }
  118. /// <summary>Look up an <see cref="X9ECParametersHolder"/> for the curve with the given
  119. /// <see cref="DerObjectIdentifier">OID</see>.</summary>
  120. /// <remarks>
  121. /// Allows accessing the <see cref="ECCurve">curve</see> without necessarily triggering the creation of the
  122. /// full <see cref="X9ECParameters"/>.
  123. /// </remarks>
  124. /// <param name="oid">The <see cref="DerObjectIdentifier">OID</see> for the curve.</param>
  125. public static X9ECParametersHolder GetByOidLazy(DerObjectIdentifier oid)
  126. {
  127. return CollectionUtilities.GetValueOrNull(curves, oid);
  128. }
  129. /// <summary>Look up the name of the curve with the given <see cref="DerObjectIdentifier">OID</see>.</summary>
  130. /// <param name="oid">The <see cref="DerObjectIdentifier">OID</see> for the curve.</param>
  131. public static string GetName(DerObjectIdentifier oid)
  132. {
  133. return CollectionUtilities.GetValueOrNull(names, oid);
  134. }
  135. /// <summary>Look up the <see cref="DerObjectIdentifier">OID</see> of the curve with the given name.</summary>
  136. /// <param name="name">The name of the curve.</param>
  137. public static DerObjectIdentifier GetOid(string name)
  138. {
  139. return CollectionUtilities.GetValueOrNull(objIds, name);
  140. }
  141. /// <summary>Enumerate the available curve names in this registry.</summary>
  142. public static IEnumerable<string> Names
  143. {
  144. get { return CollectionUtilities.Proxy(objIds.Keys); }
  145. }
  146. }
  147. }
  148. #pragma warning restore
  149. #endif