CbcBlockCipher.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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.Parameters;
  5. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Modes
  6. {
  7. /**
  8. * implements Cipher-Block-Chaining (CBC) mode on top of a simple cipher.
  9. */
  10. public sealed class CbcBlockCipher
  11. : IBlockCipherMode
  12. {
  13. private byte[] IV, cbcV, cbcNextV;
  14. private int blockSize;
  15. private IBlockCipher cipher;
  16. private bool encrypting;
  17. /**
  18. * Basic constructor.
  19. *
  20. * @param cipher the block cipher to be used as the basis of chaining.
  21. */
  22. public CbcBlockCipher(
  23. IBlockCipher cipher)
  24. {
  25. this.cipher = cipher;
  26. this.blockSize = cipher.GetBlockSize();
  27. this.IV = new byte[blockSize];
  28. this.cbcV = new byte[blockSize];
  29. this.cbcNextV = new byte[blockSize];
  30. }
  31. /**
  32. * return the underlying block cipher that we are wrapping.
  33. *
  34. * @return the underlying block cipher that we are wrapping.
  35. */
  36. public IBlockCipher UnderlyingCipher => cipher;
  37. /**
  38. * Initialise the cipher and, possibly, the initialisation vector (IV).
  39. * If an IV isn't passed as part of the parameter, the IV will be all zeros.
  40. *
  41. * @param forEncryption if true the cipher is initialised for
  42. * encryption, if false for decryption.
  43. * @param param the key and other data required by the cipher.
  44. * @exception ArgumentException if the parameters argument is
  45. * inappropriate.
  46. */
  47. public void Init(bool forEncryption, ICipherParameters parameters)
  48. {
  49. bool oldEncrypting = this.encrypting;
  50. this.encrypting = forEncryption;
  51. if (parameters is ParametersWithIV ivParam)
  52. {
  53. byte[] iv = ivParam.GetIV();
  54. if (iv.Length != blockSize)
  55. throw new ArgumentException("initialisation vector must be the same length as block size");
  56. Array.Copy(iv, 0, IV, 0, iv.Length);
  57. parameters = ivParam.Parameters;
  58. }
  59. Reset();
  60. // if null it's an IV changed only.
  61. if (parameters != null)
  62. {
  63. cipher.Init(encrypting, parameters);
  64. }
  65. else if (oldEncrypting != encrypting)
  66. {
  67. throw new ArgumentException("cannot change encrypting state without providing key.");
  68. }
  69. }
  70. /**
  71. * return the algorithm name and mode.
  72. *
  73. * @return the name of the underlying algorithm followed by "/CBC".
  74. */
  75. public string AlgorithmName
  76. {
  77. get { return cipher.AlgorithmName + "/CBC"; }
  78. }
  79. public bool IsPartialBlockOkay
  80. {
  81. get { return false; }
  82. }
  83. /**
  84. * return the block size of the underlying cipher.
  85. *
  86. * @return the block size of the underlying cipher.
  87. */
  88. public int GetBlockSize()
  89. {
  90. return cipher.GetBlockSize();
  91. }
  92. public int ProcessBlock(byte[] input, int inOff, byte[] output, int outOff)
  93. {
  94. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  95. return encrypting
  96. ? EncryptBlock(input.AsSpan(inOff), output.AsSpan(outOff))
  97. : DecryptBlock(input.AsSpan(inOff), output.AsSpan(outOff));
  98. #else
  99. return encrypting
  100. ? EncryptBlock(input, inOff, output, outOff)
  101. : DecryptBlock(input, inOff, output, outOff);
  102. #endif
  103. }
  104. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  105. public int ProcessBlock(ReadOnlySpan<byte> input, Span<byte> output)
  106. {
  107. return encrypting
  108. ? EncryptBlock(input, output)
  109. : DecryptBlock(input, output);
  110. }
  111. #endif
  112. /**
  113. * reset the chaining vector back to the IV and reset the underlying
  114. * cipher.
  115. */
  116. public void Reset()
  117. {
  118. Array.Copy(IV, 0, cbcV, 0, IV.Length);
  119. Array.Clear(cbcNextV, 0, cbcNextV.Length);
  120. }
  121. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  122. private int EncryptBlock(ReadOnlySpan<byte> input, Span<byte> output)
  123. {
  124. Check.DataLength(input, blockSize, "input buffer too short");
  125. Check.OutputLength(output, blockSize, "output buffer too short");
  126. for (int i = 0; i < blockSize; i++)
  127. {
  128. cbcV[i] ^= input[i];
  129. }
  130. int length = cipher.ProcessBlock(cbcV, output);
  131. output[..blockSize].CopyTo(cbcV);
  132. return length;
  133. }
  134. private int DecryptBlock(ReadOnlySpan<byte> input, Span<byte> output)
  135. {
  136. Check.DataLength(input, blockSize, "input buffer too short");
  137. Check.OutputLength(output, blockSize, "output buffer too short");
  138. input[..blockSize].CopyTo(cbcNextV);
  139. int length = cipher.ProcessBlock(input, output);
  140. for (int i = 0; i < blockSize; i++)
  141. {
  142. output[i] ^= cbcV[i];
  143. }
  144. byte[] tmp = cbcV;
  145. cbcV = cbcNextV;
  146. cbcNextV = tmp;
  147. return length;
  148. }
  149. #else
  150. private int EncryptBlock(byte[] input, int inOff, byte[] outBytes, int outOff)
  151. {
  152. Check.DataLength(input, inOff, blockSize, "input buffer too short");
  153. Check.OutputLength(outBytes, outOff, blockSize, "output buffer too short");
  154. for (int i = 0; i < blockSize; i++)
  155. {
  156. cbcV[i] ^= input[inOff + i];
  157. }
  158. int length = cipher.ProcessBlock(cbcV, 0, outBytes, outOff);
  159. Array.Copy(outBytes, outOff, cbcV, 0, cbcV.Length);
  160. return length;
  161. }
  162. private int DecryptBlock(byte[] input, int inOff, byte[] outBytes, int outOff)
  163. {
  164. Check.DataLength(input, inOff, blockSize, "input buffer too short");
  165. Check.OutputLength(outBytes, outOff, blockSize, "output buffer too short");
  166. Array.Copy(input, inOff, cbcNextV, 0, blockSize);
  167. int length = cipher.ProcessBlock(input, inOff, outBytes, outOff);
  168. for (int i = 0; i < blockSize; i++)
  169. {
  170. outBytes[outOff + i] ^= cbcV[i];
  171. }
  172. byte[] tmp = cbcV;
  173. cbcV = cbcNextV;
  174. cbcNextV = tmp;
  175. return length;
  176. }
  177. #endif
  178. }
  179. }
  180. #pragma warning restore
  181. #endif