CbcBlockCipher.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  5. namespace BestHTTP.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. : IBlockCipher
  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 GetUnderlyingCipher()
  37. {
  38. return cipher;
  39. }
  40. /**
  41. * Initialise the cipher and, possibly, the initialisation vector (IV).
  42. * If an IV isn't passed as part of the parameter, the IV will be all zeros.
  43. *
  44. * @param forEncryption if true the cipher is initialised for
  45. * encryption, if false for decryption.
  46. * @param param the key and other data required by the cipher.
  47. * @exception ArgumentException if the parameters argument is
  48. * inappropriate.
  49. */
  50. public void Init(
  51. bool forEncryption,
  52. ICipherParameters parameters)
  53. {
  54. bool oldEncrypting = this.encrypting;
  55. this.encrypting = forEncryption;
  56. if (parameters is ParametersWithIV)
  57. {
  58. ParametersWithIV ivParam = (ParametersWithIV)parameters;
  59. byte[] iv = ivParam.GetIV();
  60. if (iv.Length != blockSize)
  61. {
  62. throw new ArgumentException("initialisation vector must be the same length as block size");
  63. }
  64. Array.Copy(iv, 0, IV, 0, iv.Length);
  65. parameters = ivParam.Parameters;
  66. }
  67. Reset();
  68. // if null it's an IV changed only.
  69. if (parameters != null)
  70. {
  71. cipher.Init(encrypting, parameters);
  72. }
  73. else if (oldEncrypting != encrypting)
  74. {
  75. throw new ArgumentException("cannot change encrypting state without providing key.");
  76. }
  77. }
  78. /**
  79. * return the algorithm name and mode.
  80. *
  81. * @return the name of the underlying algorithm followed by "/CBC".
  82. */
  83. public string AlgorithmName
  84. {
  85. get { return cipher.AlgorithmName + "/CBC"; }
  86. }
  87. public bool IsPartialBlockOkay
  88. {
  89. get { return false; }
  90. }
  91. /**
  92. * return the block size of the underlying cipher.
  93. *
  94. * @return the block size of the underlying cipher.
  95. */
  96. public int GetBlockSize()
  97. {
  98. return cipher.GetBlockSize();
  99. }
  100. /**
  101. * Process one block of input from the array in and write it to
  102. * the out array.
  103. *
  104. * @param in the array containing the input data.
  105. * @param inOff offset into the in array the data starts at.
  106. * @param out the array the output data will be copied into.
  107. * @param outOff the offset into the out array the output will start at.
  108. * @exception DataLengthException if there isn't enough data in in, or
  109. * space in out.
  110. * @exception InvalidOperationException if the cipher isn't initialised.
  111. * @return the number of bytes processed and produced.
  112. */
  113. public int ProcessBlock(
  114. byte[] input,
  115. int inOff,
  116. byte[] output,
  117. int outOff)
  118. {
  119. return (encrypting)
  120. ? EncryptBlock(input, inOff, output, outOff)
  121. : DecryptBlock(input, inOff, output, outOff);
  122. }
  123. /**
  124. * reset the chaining vector back to the IV and reset the underlying
  125. * cipher.
  126. */
  127. public void Reset()
  128. {
  129. Array.Copy(IV, 0, cbcV, 0, IV.Length);
  130. Array.Clear(cbcNextV, 0, cbcNextV.Length);
  131. cipher.Reset();
  132. }
  133. /**
  134. * Do the appropriate chaining step for CBC mode encryption.
  135. *
  136. * @param in the array containing the data to be encrypted.
  137. * @param inOff offset into the in array the data starts at.
  138. * @param out the array the encrypted data will be copied into.
  139. * @param outOff the offset into the out array the output will start at.
  140. * @exception DataLengthException if there isn't enough data in in, or
  141. * space in out.
  142. * @exception InvalidOperationException if the cipher isn't initialised.
  143. * @return the number of bytes processed and produced.
  144. */
  145. private int EncryptBlock(
  146. byte[] input,
  147. int inOff,
  148. byte[] outBytes,
  149. int outOff)
  150. {
  151. if ((inOff + blockSize) > input.Length)
  152. {
  153. throw new DataLengthException("input buffer too short");
  154. }
  155. /*
  156. * XOR the cbcV and the input,
  157. * then encrypt the cbcV
  158. */
  159. for (int i = 0; i < blockSize; i++)
  160. {
  161. cbcV[i] ^= input[inOff + i];
  162. }
  163. int length = cipher.ProcessBlock(cbcV, 0, outBytes, outOff);
  164. /*
  165. * copy ciphertext to cbcV
  166. */
  167. Array.Copy(outBytes, outOff, cbcV, 0, cbcV.Length);
  168. return length;
  169. }
  170. /**
  171. * Do the appropriate chaining step for CBC mode decryption.
  172. *
  173. * @param in the array containing the data to be decrypted.
  174. * @param inOff offset into the in array the data starts at.
  175. * @param out the array the decrypted data will be copied into.
  176. * @param outOff the offset into the out array the output will start at.
  177. * @exception DataLengthException if there isn't enough data in in, or
  178. * space in out.
  179. * @exception InvalidOperationException if the cipher isn't initialised.
  180. * @return the number of bytes processed and produced.
  181. */
  182. private int DecryptBlock(
  183. byte[] input,
  184. int inOff,
  185. byte[] outBytes,
  186. int outOff)
  187. {
  188. if ((inOff + blockSize) > input.Length)
  189. {
  190. throw new DataLengthException("input buffer too short");
  191. }
  192. Array.Copy(input, inOff, cbcNextV, 0, blockSize);
  193. int length = cipher.ProcessBlock(input, inOff, outBytes, outOff);
  194. /*
  195. * XOR the cbcV and the output
  196. */
  197. for (int i = 0; i < blockSize; i++)
  198. {
  199. outBytes[outOff + i] ^= cbcV[i];
  200. }
  201. /*
  202. * swap the back up buffer into next position
  203. */
  204. byte[] tmp;
  205. tmp = cbcV;
  206. cbcV = cbcNextV;
  207. cbcNextV = tmp;
  208. return length;
  209. }
  210. }
  211. }
  212. #pragma warning restore
  213. #endif