SM2KeyExchangePrivateParameters.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math.EC;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Multiplier;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters
  7. {
  8. /// <summary>Private parameters for an SM2 key exchange.</summary>
  9. /// <remarks>The ephemeralPrivateKey is used to calculate the random point used in the algorithm.</remarks>
  10. public class SM2KeyExchangePrivateParameters
  11. : ICipherParameters
  12. {
  13. private readonly bool mInitiator;
  14. private readonly ECPrivateKeyParameters mStaticPrivateKey;
  15. private readonly ECPoint mStaticPublicPoint;
  16. private readonly ECPrivateKeyParameters mEphemeralPrivateKey;
  17. private readonly ECPoint mEphemeralPublicPoint;
  18. public SM2KeyExchangePrivateParameters(
  19. bool initiator,
  20. ECPrivateKeyParameters staticPrivateKey,
  21. ECPrivateKeyParameters ephemeralPrivateKey)
  22. {
  23. if (staticPrivateKey == null)
  24. throw new ArgumentNullException("staticPrivateKey");
  25. if (ephemeralPrivateKey == null)
  26. throw new ArgumentNullException("ephemeralPrivateKey");
  27. ECDomainParameters parameters = staticPrivateKey.Parameters;
  28. if (!parameters.Equals(ephemeralPrivateKey.Parameters))
  29. throw new ArgumentException("Static and ephemeral private keys have different domain parameters");
  30. ECMultiplier m = new FixedPointCombMultiplier();
  31. this.mInitiator = initiator;
  32. this.mStaticPrivateKey = staticPrivateKey;
  33. this.mStaticPublicPoint = m.Multiply(parameters.G, staticPrivateKey.D).Normalize();
  34. this.mEphemeralPrivateKey = ephemeralPrivateKey;
  35. this.mEphemeralPublicPoint = m.Multiply(parameters.G, ephemeralPrivateKey.D).Normalize();
  36. }
  37. public virtual bool IsInitiator
  38. {
  39. get { return mInitiator; }
  40. }
  41. public virtual ECPrivateKeyParameters StaticPrivateKey
  42. {
  43. get { return mStaticPrivateKey; }
  44. }
  45. public virtual ECPoint StaticPublicPoint
  46. {
  47. get { return mStaticPublicPoint; }
  48. }
  49. public virtual ECPrivateKeyParameters EphemeralPrivateKey
  50. {
  51. get { return mEphemeralPrivateKey; }
  52. }
  53. public virtual ECPoint EphemeralPublicPoint
  54. {
  55. get { return mEphemeralPublicPoint; }
  56. }
  57. }
  58. }
  59. #pragma warning restore
  60. #endif