NISTNamedCurves.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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.Sec;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.X9;
  7. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.Collections;
  8. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Nist
  9. {
  10. /// <summary>Elliptic curve registry for NIST curves.</summary>
  11. public static class NistNamedCurves
  12. {
  13. private static readonly Dictionary<string, DerObjectIdentifier> objIds =
  14. new Dictionary<string, DerObjectIdentifier>(StringComparer.OrdinalIgnoreCase);
  15. private static readonly Dictionary<DerObjectIdentifier, string> names =
  16. new Dictionary<DerObjectIdentifier, string>();
  17. private static void DefineCurveAlias(string name, DerObjectIdentifier oid)
  18. {
  19. if (SecNamedCurves.GetByOidLazy(oid) == null)
  20. throw new InvalidOperationException();
  21. objIds.Add(name, oid);
  22. names.Add(oid, name);
  23. }
  24. static NistNamedCurves()
  25. {
  26. DefineCurveAlias("B-163", SecObjectIdentifiers.SecT163r2);
  27. DefineCurveAlias("B-233", SecObjectIdentifiers.SecT233r1);
  28. DefineCurveAlias("B-283", SecObjectIdentifiers.SecT283r1);
  29. DefineCurveAlias("B-409", SecObjectIdentifiers.SecT409r1);
  30. DefineCurveAlias("B-571", SecObjectIdentifiers.SecT571r1);
  31. DefineCurveAlias("K-163", SecObjectIdentifiers.SecT163k1);
  32. DefineCurveAlias("K-233", SecObjectIdentifiers.SecT233k1);
  33. DefineCurveAlias("K-283", SecObjectIdentifiers.SecT283k1);
  34. DefineCurveAlias("K-409", SecObjectIdentifiers.SecT409k1);
  35. DefineCurveAlias("K-571", SecObjectIdentifiers.SecT571k1);
  36. DefineCurveAlias("P-192", SecObjectIdentifiers.SecP192r1);
  37. DefineCurveAlias("P-224", SecObjectIdentifiers.SecP224r1);
  38. DefineCurveAlias("P-256", SecObjectIdentifiers.SecP256r1);
  39. DefineCurveAlias("P-384", SecObjectIdentifiers.SecP384r1);
  40. DefineCurveAlias("P-521", SecObjectIdentifiers.SecP521r1);
  41. }
  42. /// <summary>Look up the <see cref="X9ECParameters"/> for the curve with the given name.</summary>
  43. /// <param name="name">The name of the curve.</param>
  44. public static X9ECParameters GetByName(string name)
  45. {
  46. DerObjectIdentifier oid = GetOid(name);
  47. return oid == null ? null : GetByOid(oid);
  48. }
  49. /// <summary>Look up an <see cref="X9ECParametersHolder"/> for the curve with the given name.</summary>
  50. /// <remarks>
  51. /// Allows accessing the <see cref="Math.EC.ECCurve">curve</see> without necessarily triggering the creation of
  52. /// the full <see cref="X9ECParameters"/>.
  53. /// </remarks>
  54. /// <param name="name">The name of the curve.</param>
  55. public static X9ECParametersHolder GetByNameLazy(string name)
  56. {
  57. DerObjectIdentifier oid = GetOid(name);
  58. return oid == null ? null : GetByOidLazy(oid);
  59. }
  60. /// <summary>Look up the <see cref="X9ECParameters"/> for the curve with the given
  61. /// <see cref="DerObjectIdentifier">OID</see>.</summary>
  62. /// <param name="oid">The <see cref="DerObjectIdentifier">OID</see> for the curve.</param>
  63. public static X9ECParameters GetByOid(DerObjectIdentifier oid)
  64. {
  65. return GetByOidLazy(oid)?.Parameters;
  66. }
  67. /// <summary>Look up an <see cref="X9ECParametersHolder"/> for the curve with the given
  68. /// <see cref="DerObjectIdentifier">OID</see>.</summary>
  69. /// <remarks>
  70. /// Allows accessing the <see cref="Math.EC.ECCurve">curve</see> without necessarily triggering the creation of
  71. /// the full <see cref="X9ECParameters"/>.
  72. /// </remarks>
  73. /// <param name="oid">The <see cref="DerObjectIdentifier">OID</see> for the curve.</param>
  74. public static X9ECParametersHolder GetByOidLazy(DerObjectIdentifier oid)
  75. {
  76. return names.ContainsKey(oid) ? SecNamedCurves.GetByOidLazy(oid) : null;
  77. }
  78. /// <summary>Look up the name of the curve with the given <see cref="DerObjectIdentifier">OID</see>.</summary>
  79. /// <param name="oid">The <see cref="DerObjectIdentifier">OID</see> for the curve.</param>
  80. public static string GetName(DerObjectIdentifier oid)
  81. {
  82. return CollectionUtilities.GetValueOrNull(names, oid);
  83. }
  84. /// <summary>Look up the <see cref="DerObjectIdentifier">OID</see> of the curve with the given name.</summary>
  85. /// <param name="name">The name of the curve.</param>
  86. public static DerObjectIdentifier GetOid(string name)
  87. {
  88. return CollectionUtilities.GetValueOrNull(objIds, name);
  89. }
  90. /// <summary>Enumerate the available curve names in this registry.</summary>
  91. public static IEnumerable<string> Names
  92. {
  93. get { return CollectionUtilities.Proxy(objIds.Keys); }
  94. }
  95. }
  96. }
  97. #pragma warning restore
  98. #endif