FastTlsBlockCipherImpl.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. using BestHTTP.SecureProtocol.Org.BouncyCastle.Tls.Crypto.Impl;
  7. namespace BestHTTP.Connections.TLS.Crypto.Impl
  8. {
  9. internal sealed class FastTlsBlockCipherImpl
  10. : TlsBlockCipherImpl
  11. {
  12. private readonly bool m_isEncrypting;
  13. private readonly IBlockCipher m_cipher;
  14. private NoCopyKeyParameter key;
  15. internal FastTlsBlockCipherImpl(IBlockCipher 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 NoCopyKeyParameter(key, keyOff, keyLen);
  23. }
  24. public void Init(byte[] iv, int ivOff, int ivLen)
  25. {
  26. m_cipher.Init(m_isEncrypting, new FastParametersWithIV(key, iv, ivOff, ivLen));
  27. }
  28. public int DoFinal(byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset)
  29. {
  30. int blockSize = m_cipher.GetBlockSize();
  31. for (int i = 0; i < inputLength; i += blockSize)
  32. {
  33. m_cipher.ProcessBlock(input, inputOffset + i, output, outputOffset + i);
  34. }
  35. return inputLength;
  36. }
  37. public int GetBlockSize()
  38. {
  39. return m_cipher.GetBlockSize();
  40. }
  41. }
  42. }
  43. #pragma warning restore
  44. #endif