BcTlsAeadCipherImpl.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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.Modes;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  7. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Tls.Crypto.Impl.BC
  8. {
  9. internal sealed class BcTlsAeadCipherImpl
  10. : TlsAeadCipherImpl
  11. {
  12. private readonly bool m_isEncrypting;
  13. private readonly IAeadBlockCipher m_cipher;
  14. private KeyParameter key;
  15. internal BcTlsAeadCipherImpl(IAeadBlockCipher cipher, bool isEncrypting)
  16. {
  17. this.m_cipher = cipher;
  18. this.m_isEncrypting = isEncrypting;
  19. }
  20. public void SetKey(byte[] key, int keyOff, int keyLen)
  21. {
  22. this.key = new KeyParameter(key, keyOff, keyLen);
  23. }
  24. public void Init(byte[] nonce, int macSize, byte[] additionalData)
  25. {
  26. m_cipher.Init(m_isEncrypting, new AeadParameters(key, macSize * 8, nonce, additionalData));
  27. }
  28. public int GetOutputSize(int inputLength)
  29. {
  30. return m_cipher.GetOutputSize(inputLength);
  31. }
  32. public int DoFinal(byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset)
  33. {
  34. int len = m_cipher.ProcessBytes(input, inputOffset, inputLength, output, outputOffset);
  35. try
  36. {
  37. len += m_cipher.DoFinal(output, outputOffset + len);
  38. }
  39. catch (InvalidCipherTextException e)
  40. {
  41. throw new TlsFatalAlert(AlertDescription.bad_record_mac, e);
  42. }
  43. return len;
  44. }
  45. }
  46. }
  47. #pragma warning restore
  48. #endif