BcTlsBlockCipherImpl.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. internal sealed class BcTlsBlockCipherImpl
  9. : TlsBlockCipherImpl
  10. {
  11. private readonly bool m_isEncrypting;
  12. private readonly IBlockCipher m_cipher;
  13. private KeyParameter key;
  14. internal BcTlsBlockCipherImpl(IBlockCipher cipher, bool isEncrypting)
  15. {
  16. this.m_cipher = cipher;
  17. this.m_isEncrypting = isEncrypting;
  18. }
  19. public void SetKey(byte[] key, int keyOff, int keyLen)
  20. {
  21. this.key = new KeyParameter(key, keyOff, keyLen);
  22. }
  23. public void Init(byte[] iv, int ivOff, int ivLen)
  24. {
  25. m_cipher.Init(m_isEncrypting, new ParametersWithIV(key, iv, ivOff, ivLen));
  26. }
  27. public int DoFinal(byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset)
  28. {
  29. int blockSize = m_cipher.GetBlockSize();
  30. for (int i = 0; i < inputLength; i += blockSize)
  31. {
  32. m_cipher.ProcessBlock(input, inputOffset + i, output, outputOffset + i);
  33. }
  34. return inputLength;
  35. }
  36. public int GetBlockSize()
  37. {
  38. return m_cipher.GetBlockSize();
  39. }
  40. }
  41. }
  42. #pragma warning restore
  43. #endif