ECKeyParameters.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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.CryptoPro;
  6. using Org.BouncyCastle.Asn1.X9;
  7. using Org.BouncyCastle.Crypto.Generators;
  8. using Org.BouncyCastle.Security;
  9. using Org.BouncyCastle.Utilities;
  10. using Org.BouncyCastle.Utilities.Collections;
  11. namespace Org.BouncyCastle.Crypto.Parameters
  12. {
  13. public abstract class ECKeyParameters
  14. : AsymmetricKeyParameter
  15. {
  16. private static readonly string[] algorithms = { "EC", "ECDSA", "ECDH", "ECDHC", "ECGOST3410", "ECMQV" };
  17. private readonly string algorithm;
  18. private readonly ECDomainParameters parameters;
  19. private readonly DerObjectIdentifier publicKeyParamSet;
  20. protected ECKeyParameters(
  21. string algorithm,
  22. bool isPrivate,
  23. ECDomainParameters parameters)
  24. : base(isPrivate)
  25. {
  26. if (algorithm == null)
  27. throw new ArgumentNullException("algorithm");
  28. if (parameters == null)
  29. throw new ArgumentNullException("parameters");
  30. this.algorithm = VerifyAlgorithmName(algorithm);
  31. this.parameters = parameters;
  32. }
  33. protected ECKeyParameters(
  34. string algorithm,
  35. bool isPrivate,
  36. DerObjectIdentifier publicKeyParamSet)
  37. : base(isPrivate)
  38. {
  39. if (algorithm == null)
  40. throw new ArgumentNullException("algorithm");
  41. if (publicKeyParamSet == null)
  42. throw new ArgumentNullException("publicKeyParamSet");
  43. this.algorithm = VerifyAlgorithmName(algorithm);
  44. this.parameters = LookupParameters(publicKeyParamSet);
  45. this.publicKeyParamSet = publicKeyParamSet;
  46. }
  47. public string AlgorithmName
  48. {
  49. get { return algorithm; }
  50. }
  51. public ECDomainParameters Parameters
  52. {
  53. get { return parameters; }
  54. }
  55. public DerObjectIdentifier PublicKeyParamSet
  56. {
  57. get { return publicKeyParamSet; }
  58. }
  59. public override bool Equals(
  60. object obj)
  61. {
  62. if (obj == this)
  63. return true;
  64. ECDomainParameters other = obj as ECDomainParameters;
  65. if (other == null)
  66. return false;
  67. return Equals(other);
  68. }
  69. protected bool Equals(
  70. ECKeyParameters other)
  71. {
  72. return parameters.Equals(other.parameters) && base.Equals(other);
  73. }
  74. public override int GetHashCode()
  75. {
  76. return parameters.GetHashCode() ^ base.GetHashCode();
  77. }
  78. internal ECKeyGenerationParameters CreateKeyGenerationParameters(
  79. SecureRandom random)
  80. {
  81. if (publicKeyParamSet != null)
  82. {
  83. return new ECKeyGenerationParameters(publicKeyParamSet, random);
  84. }
  85. return new ECKeyGenerationParameters(parameters, random);
  86. }
  87. internal static string VerifyAlgorithmName(string algorithm)
  88. {
  89. string upper = Org.BouncyCastle.Utilities.Platform.ToUpperInvariant(algorithm);
  90. if (Array.IndexOf(algorithms, algorithm, 0, algorithms.Length) < 0)
  91. throw new ArgumentException("unrecognised algorithm: " + algorithm, "algorithm");
  92. return upper;
  93. }
  94. internal static ECDomainParameters LookupParameters(
  95. DerObjectIdentifier publicKeyParamSet)
  96. {
  97. if (publicKeyParamSet == null)
  98. throw new ArgumentNullException("publicKeyParamSet");
  99. ECDomainParameters p = ECGost3410NamedCurves.GetByOid(publicKeyParamSet);
  100. if (p == null)
  101. {
  102. X9ECParameters x9 = ECKeyPairGenerator.FindECCurveByOid(publicKeyParamSet);
  103. if (x9 == null)
  104. {
  105. throw new ArgumentException("OID is not a valid public key parameter set", "publicKeyParamSet");
  106. }
  107. p = new ECDomainParameters(x9.Curve, x9.G, x9.N, x9.H, x9.GetSeed());
  108. }
  109. return p;
  110. }
  111. }
  112. }
  113. #endif