BcTlsBlockCipherImpl.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  6. namespace Best.HTTP.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. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  24. public void SetKey(ReadOnlySpan<byte> key)
  25. {
  26. this.key = new KeyParameter(key);
  27. }
  28. #endif
  29. public void Init(byte[] iv, int ivOff, int ivLen)
  30. {
  31. m_cipher.Init(m_isEncrypting, new ParametersWithIV(key, iv, ivOff, ivLen));
  32. }
  33. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  34. public void Init(ReadOnlySpan<byte> iv)
  35. {
  36. m_cipher.Init(m_isEncrypting, new ParametersWithIV(key, iv));
  37. }
  38. #endif
  39. public int DoFinal(byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset)
  40. {
  41. int blockSize = m_cipher.GetBlockSize();
  42. for (int i = 0; i < inputLength; i += blockSize)
  43. {
  44. m_cipher.ProcessBlock(input, inputOffset + i, output, outputOffset + i);
  45. }
  46. return inputLength;
  47. }
  48. public int GetBlockSize()
  49. {
  50. return m_cipher.GetBlockSize();
  51. }
  52. }
  53. }
  54. #pragma warning restore
  55. #endif