ANSSINamedCurves.cs 4.3 KB

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