BcTlsECDH.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Tls.Crypto.Impl.BC
  7. {
  8. /// <summary>Support class for ephemeral Elliptic Curve Diffie-Hellman using the BC light-weight library.</summary>
  9. public class BcTlsECDH
  10. : TlsAgreement
  11. {
  12. protected readonly BcTlsECDomain m_domain;
  13. protected AsymmetricCipherKeyPair m_localKeyPair;
  14. protected ECPublicKeyParameters m_peerPublicKey;
  15. public BcTlsECDH(BcTlsECDomain domain)
  16. {
  17. this.m_domain = domain;
  18. }
  19. public virtual byte[] GenerateEphemeral()
  20. {
  21. this.m_localKeyPair = m_domain.GenerateKeyPair();
  22. return m_domain.EncodePublicKey((ECPublicKeyParameters)m_localKeyPair.Public);
  23. }
  24. public virtual void ReceivePeerValue(byte[] peerValue)
  25. {
  26. this.m_peerPublicKey = m_domain.DecodePublicKey(peerValue);
  27. }
  28. public virtual TlsSecret CalculateSecret()
  29. {
  30. return m_domain.CalculateECDHAgreement((ECPrivateKeyParameters)m_localKeyPair.Private, m_peerPublicKey);
  31. }
  32. }
  33. }
  34. #pragma warning restore
  35. #endif