NISTNamedCurves.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using System.Collections;
  4. using Org.BouncyCastle.Asn1;
  5. using Org.BouncyCastle.Asn1.Sec;
  6. using Org.BouncyCastle.Asn1.X9;
  7. using Org.BouncyCastle.Utilities;
  8. using Org.BouncyCastle.Utilities.Collections;
  9. namespace Org.BouncyCastle.Asn1.Nist
  10. {
  11. /**
  12. * Utility class for fetching curves using their NIST names as published in FIPS-PUB 186-3
  13. */
  14. public sealed class NistNamedCurves
  15. {
  16. private NistNamedCurves()
  17. {
  18. }
  19. private static readonly IDictionary objIds = Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  20. private static readonly IDictionary names = Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  21. private static void DefineCurveAlias(
  22. string name,
  23. DerObjectIdentifier oid)
  24. {
  25. objIds.Add(Org.BouncyCastle.Utilities.Platform.ToUpperInvariant(name), oid);
  26. names.Add(oid, name);
  27. }
  28. static NistNamedCurves()
  29. {
  30. DefineCurveAlias("B-163", SecObjectIdentifiers.SecT163r2);
  31. DefineCurveAlias("B-233", SecObjectIdentifiers.SecT233r1);
  32. DefineCurveAlias("B-283", SecObjectIdentifiers.SecT283r1);
  33. DefineCurveAlias("B-409", SecObjectIdentifiers.SecT409r1);
  34. DefineCurveAlias("B-571", SecObjectIdentifiers.SecT571r1);
  35. DefineCurveAlias("K-163", SecObjectIdentifiers.SecT163k1);
  36. DefineCurveAlias("K-233", SecObjectIdentifiers.SecT233k1);
  37. DefineCurveAlias("K-283", SecObjectIdentifiers.SecT283k1);
  38. DefineCurveAlias("K-409", SecObjectIdentifiers.SecT409k1);
  39. DefineCurveAlias("K-571", SecObjectIdentifiers.SecT571k1);
  40. DefineCurveAlias("P-192", SecObjectIdentifiers.SecP192r1);
  41. DefineCurveAlias("P-224", SecObjectIdentifiers.SecP224r1);
  42. DefineCurveAlias("P-256", SecObjectIdentifiers.SecP256r1);
  43. DefineCurveAlias("P-384", SecObjectIdentifiers.SecP384r1);
  44. DefineCurveAlias("P-521", SecObjectIdentifiers.SecP521r1);
  45. }
  46. public static X9ECParameters GetByName(
  47. string name)
  48. {
  49. DerObjectIdentifier oid = GetOid(name);
  50. return oid == null ? null : GetByOid(oid);
  51. }
  52. /**
  53. * return the X9ECParameters object for the named curve represented by
  54. * the passed in object identifier. Null if the curve isn't present.
  55. *
  56. * @param oid an object identifier representing a named curve, if present.
  57. */
  58. public static X9ECParameters GetByOid(
  59. DerObjectIdentifier oid)
  60. {
  61. return SecNamedCurves.GetByOid(oid);
  62. }
  63. /**
  64. * return the object identifier signified by the passed in name. Null
  65. * if there is no object identifier associated with name.
  66. *
  67. * @return the object identifier associated with name, if present.
  68. */
  69. public static DerObjectIdentifier GetOid(
  70. string name)
  71. {
  72. return (DerObjectIdentifier) objIds[Org.BouncyCastle.Utilities.Platform.ToUpperInvariant(name)];
  73. }
  74. /**
  75. * return the named curve name represented by the given object identifier.
  76. */
  77. public static string GetName(
  78. DerObjectIdentifier oid)
  79. {
  80. return (string) names[oid];
  81. }
  82. /**
  83. * returns an enumeration containing the name strings for curves
  84. * contained in this structure.
  85. */
  86. public static IEnumerable Names
  87. {
  88. get { return new EnumerableProxy(names.Values); }
  89. }
  90. }
  91. }
  92. #endif