ANSSINamedCurves.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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.Anssi
  13. {
  14. public class AnssiNamedCurves
  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. /*
  31. * FRP256v1
  32. */
  33. internal class Frp256v1Holder
  34. : X9ECParametersHolder
  35. {
  36. private Frp256v1Holder() {}
  37. internal static readonly X9ECParametersHolder Instance = new Frp256v1Holder();
  38. protected override X9ECParameters CreateParameters()
  39. {
  40. BigInteger p = FromHex("F1FD178C0B3AD58F10126DE8CE42435B3961ADBCABC8CA6DE8FCF353D86E9C03");
  41. BigInteger a = FromHex("F1FD178C0B3AD58F10126DE8CE42435B3961ADBCABC8CA6DE8FCF353D86E9C00");
  42. BigInteger b = FromHex("EE353FCA5428A9300D4ABA754A44C00FDFEC0C9AE4B1A1803075ED967B7BB73F");
  43. byte[] S = null;
  44. BigInteger n = FromHex("F1FD178C0B3AD58F10126DE8CE42435B53DC67E140D2BF941FFDD459C6D655E1");
  45. BigInteger h = BigInteger.One;
  46. ECCurve curve = ConfigureCurve(new FpCurve(p, a, b, n, h));
  47. X9ECPoint G = ConfigureBasepoint(curve,
  48. "04B6B3D4C356C139EB31183D4749D423958C27D2DCAF98B70164C97A2DD98F5CFF6142E0F7C8B204911F9271F0F3ECEF8C2701C307E8E4C9E183115A1554062CFB");
  49. return new X9ECParameters(curve, G, n, h, S);
  50. }
  51. };
  52. private static readonly IDictionary objIds = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  53. private static readonly IDictionary curves = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  54. private static readonly IDictionary names = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  55. private static void DefineCurve(
  56. string name,
  57. DerObjectIdentifier oid,
  58. X9ECParametersHolder holder)
  59. {
  60. objIds.Add(BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.ToUpperInvariant(name), oid);
  61. names.Add(oid, name);
  62. curves.Add(oid, holder);
  63. }
  64. static AnssiNamedCurves()
  65. {
  66. DefineCurve("FRP256v1", AnssiObjectIdentifiers.FRP256v1, Frp256v1Holder.Instance);
  67. }
  68. public static X9ECParameters GetByName(
  69. string name)
  70. {
  71. DerObjectIdentifier oid = GetOid(name);
  72. return oid == null ? null : GetByOid(oid);
  73. }
  74. /**
  75. * return the X9ECParameters object for the named curve represented by
  76. * the passed in object identifier. Null if the curve isn't present.
  77. *
  78. * @param oid an object identifier representing a named curve, if present.
  79. */
  80. public static X9ECParameters GetByOid(
  81. DerObjectIdentifier oid)
  82. {
  83. X9ECParametersHolder holder = (X9ECParametersHolder)curves[oid];
  84. return holder == null ? null : holder.Parameters;
  85. }
  86. /**
  87. * return the object identifier signified by the passed in name. Null
  88. * if there is no object identifier associated with name.
  89. *
  90. * @return the object identifier associated with name, if present.
  91. */
  92. public static DerObjectIdentifier GetOid(
  93. string name)
  94. {
  95. return (DerObjectIdentifier)objIds[BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.ToUpperInvariant(name)];
  96. }
  97. /**
  98. * return the named curve name represented by the given object identifier.
  99. */
  100. public static string GetName(
  101. DerObjectIdentifier oid)
  102. {
  103. return (string)names[oid];
  104. }
  105. /**
  106. * returns an enumeration containing the name strings for curves
  107. * contained in this structure.
  108. */
  109. public static IEnumerable Names
  110. {
  111. get { return new EnumerableProxy(names.Values); }
  112. }
  113. }
  114. }
  115. #pragma warning restore
  116. #endif