ANSSINamedCurves.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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.Anssi
  12. {
  13. /// <summary>Elliptic curve registry for ANSSI.</summary>
  14. public static 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. internal class Frp256v1Holder
  31. : X9ECParametersHolder
  32. {
  33. private Frp256v1Holder() {}
  34. internal static readonly X9ECParametersHolder Instance = new Frp256v1Holder();
  35. protected override ECCurve CreateCurve()
  36. {
  37. BigInteger p = FromHex("F1FD178C0B3AD58F10126DE8CE42435B3961ADBCABC8CA6DE8FCF353D86E9C03");
  38. BigInteger a = FromHex("F1FD178C0B3AD58F10126DE8CE42435B3961ADBCABC8CA6DE8FCF353D86E9C00");
  39. BigInteger b = FromHex("EE353FCA5428A9300D4ABA754A44C00FDFEC0C9AE4B1A1803075ED967B7BB73F");
  40. BigInteger n = FromHex("F1FD178C0B3AD58F10126DE8CE42435B53DC67E140D2BF941FFDD459C6D655E1");
  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. "04B6B3D4C356C139EB31183D4749D423958C27D2DCAF98B70164C97A2DD98F5CFF6142E0F7C8B204911F9271F0F3ECEF8C2701C307E8E4C9E183115A1554062CFB");
  50. return new X9ECParameters(curve, G, curve.Order, curve.Cofactor, S);
  51. }
  52. }
  53. private static readonly Dictionary<string, DerObjectIdentifier> objIds =
  54. new Dictionary<string, DerObjectIdentifier>(StringComparer.OrdinalIgnoreCase);
  55. private static readonly Dictionary<DerObjectIdentifier, X9ECParametersHolder> curves =
  56. new Dictionary<DerObjectIdentifier, X9ECParametersHolder>();
  57. private static readonly Dictionary<DerObjectIdentifier, string> names =
  58. new Dictionary<DerObjectIdentifier, string>();
  59. private static void DefineCurve(string name, DerObjectIdentifier oid, X9ECParametersHolder holder)
  60. {
  61. objIds.Add(name, oid);
  62. names.Add(oid, name);
  63. curves.Add(oid, holder);
  64. }
  65. static AnssiNamedCurves()
  66. {
  67. DefineCurve("FRP256v1", AnssiObjectIdentifiers.FRP256v1, Frp256v1Holder.Instance);
  68. }
  69. /// <summary>Look up the <see cref="X9ECParameters"/> for the curve with the given name.</summary>
  70. /// <param name="name">The name of the curve.</param>
  71. public static X9ECParameters GetByName(string name)
  72. {
  73. DerObjectIdentifier oid = GetOid(name);
  74. return oid == null ? null : GetByOid(oid);
  75. }
  76. /// <summary>Look up an <see cref="X9ECParametersHolder"/> for the curve with the given name.</summary>
  77. /// <remarks>
  78. /// Allows accessing the <see cref="ECCurve">curve</see> without necessarily triggering the creation of the
  79. /// full <see cref="X9ECParameters"/>.
  80. /// </remarks>
  81. /// <param name="name">The name of the curve.</param>
  82. public static X9ECParametersHolder GetByNameLazy(string name)
  83. {
  84. DerObjectIdentifier oid = GetOid(name);
  85. return oid == null ? null : GetByOidLazy(oid);
  86. }
  87. /// <summary>Look up the <see cref="X9ECParameters"/> for the curve with the given
  88. /// <see cref="DerObjectIdentifier">OID</see>.</summary>
  89. /// <param name="oid">The <see cref="DerObjectIdentifier">OID</see> for the curve.</param>
  90. public static X9ECParameters GetByOid(DerObjectIdentifier oid)
  91. {
  92. return GetByOidLazy(oid)?.Parameters;
  93. }
  94. /// <summary>Look up an <see cref="X9ECParametersHolder"/> for the curve with the given
  95. /// <see cref="DerObjectIdentifier">OID</see>.</summary>
  96. /// <remarks>
  97. /// Allows accessing the <see cref="ECCurve">curve</see> without necessarily triggering the creation of the
  98. /// full <see cref="X9ECParameters"/>.
  99. /// </remarks>
  100. /// <param name="oid">The <see cref="DerObjectIdentifier">OID</see> for the curve.</param>
  101. public static X9ECParametersHolder GetByOidLazy(DerObjectIdentifier oid)
  102. {
  103. return CollectionUtilities.GetValueOrNull(curves, oid);
  104. }
  105. /// <summary>Look up the name of the curve with the given <see cref="DerObjectIdentifier">OID</see>.</summary>
  106. /// <param name="oid">The <see cref="DerObjectIdentifier">OID</see> for the curve.</param>
  107. public static string GetName(DerObjectIdentifier oid)
  108. {
  109. return CollectionUtilities.GetValueOrNull(names, oid);
  110. }
  111. /// <summary>Look up the <see cref="DerObjectIdentifier">OID</see> of the curve with the given name.</summary>
  112. /// <param name="name">The name of the curve.</param>
  113. public static DerObjectIdentifier GetOid(string name)
  114. {
  115. return CollectionUtilities.GetValueOrNull(objIds, name);
  116. }
  117. /// <summary>Enumerate the available curve names in this registry.</summary>
  118. public static IEnumerable<string> Names
  119. {
  120. get { return CollectionUtilities.Proxy(objIds.Keys); }
  121. }
  122. }
  123. }
  124. #pragma warning restore
  125. #endif