DHPublicKeyParameters.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters
  7. {
  8. public class DHPublicKeyParameters
  9. : DHKeyParameters
  10. {
  11. private static BigInteger Validate(BigInteger y, DHParameters dhParams)
  12. {
  13. if (y == null)
  14. throw new ArgumentNullException("y");
  15. // TLS check
  16. if (y.CompareTo(BigInteger.Two) < 0 || y.CompareTo(dhParams.P.Subtract(BigInteger.Two)) > 0)
  17. throw new ArgumentException("invalid DH public key", "y");
  18. // we can't validate without Q.
  19. if (dhParams.Q != null
  20. && !y.ModPow(dhParams.Q, dhParams.P).Equals(BigInteger.One))
  21. {
  22. throw new ArgumentException("y value does not appear to be in correct group", "y");
  23. }
  24. return y;
  25. }
  26. private readonly BigInteger y;
  27. public DHPublicKeyParameters(
  28. BigInteger y,
  29. DHParameters parameters)
  30. : base(false, parameters)
  31. {
  32. this.y = Validate(y, parameters);
  33. }
  34. public DHPublicKeyParameters(
  35. BigInteger y,
  36. DHParameters parameters,
  37. DerObjectIdentifier algorithmOid)
  38. : base(false, parameters, algorithmOid)
  39. {
  40. this.y = Validate(y, parameters);
  41. }
  42. public virtual BigInteger Y
  43. {
  44. get { return y; }
  45. }
  46. public override bool Equals(
  47. object obj)
  48. {
  49. if (obj == this)
  50. return true;
  51. DHPublicKeyParameters other = obj as DHPublicKeyParameters;
  52. if (other == null)
  53. return false;
  54. return Equals(other);
  55. }
  56. protected bool Equals(
  57. DHPublicKeyParameters other)
  58. {
  59. return y.Equals(other.y) && base.Equals(other);
  60. }
  61. public override int GetHashCode()
  62. {
  63. return y.GetHashCode() ^ base.GetHashCode();
  64. }
  65. }
  66. }
  67. #pragma warning restore
  68. #endif