ECKeyParameters.cs 4.2 KB

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