ANSSINamedCurves.cs 4.6 KB

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