You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

MyCfbBlockCipher.cs 8.8 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. using Org.BouncyCastle.Crypto;
  2. using Org.BouncyCastle.Crypto.Parameters;
  3. using System;
  4. using System.IO;
  5. namespace Shadowsocks.Net.Crypto.Extensions
  6. {
  7. /**
  8. * implements a Cipher-FeedBack (CFB) mode on top of a simple cipher.
  9. */
  10. public class MyCfbBlockCipher
  11. : IBlockCipher
  12. {
  13. private byte[] IV;
  14. private byte[] cfbV;
  15. private byte[] cfbOutV;
  16. private byte[] buff;
  17. private int _offset;
  18. private bool encrypting;
  19. private readonly int blockSize;
  20. private readonly IBlockCipher cipher;
  21. /**
  22. * Basic constructor.
  23. *
  24. * @param cipher the block cipher to be used as the basis of the
  25. * feedback mode.
  26. * @param blockSize the block size in bits (note: a multiple of 8)
  27. */
  28. public MyCfbBlockCipher(
  29. IBlockCipher cipher,
  30. int bitBlockSize)
  31. {
  32. this.cipher = cipher;
  33. blockSize = bitBlockSize / 8;
  34. IV = new byte[cipher.GetBlockSize()];
  35. cfbV = new byte[cipher.GetBlockSize()];
  36. cfbOutV = new byte[cipher.GetBlockSize()];
  37. buff = new byte[cipher.GetBlockSize()];
  38. _offset = 0;
  39. }
  40. /**
  41. * return the underlying block cipher that we are wrapping.
  42. *
  43. * @return the underlying block cipher that we are wrapping.
  44. */
  45. public IBlockCipher GetUnderlyingCipher()
  46. {
  47. return cipher;
  48. }
  49. /**
  50. * Initialise the cipher and, possibly, the initialisation vector (IV).
  51. * If an IV isn't passed as part of the parameter, the IV will be all zeros.
  52. * An IV which is too short is handled in FIPS compliant fashion.
  53. *
  54. * @param forEncryption if true the cipher is initialised for
  55. * encryption, if false for decryption.
  56. * @param param the key and other data required by the cipher.
  57. * @exception ArgumentException if the parameters argument is
  58. * inappropriate.
  59. */
  60. public void Init(
  61. bool forEncryption,
  62. ICipherParameters parameters)
  63. {
  64. encrypting = forEncryption;
  65. if (parameters is ParametersWithIV ivParam)
  66. {
  67. var iv = ivParam.GetIV();
  68. var diff = IV.Length - iv.Length;
  69. Array.Copy(iv, 0, IV, diff, iv.Length);
  70. Array.Clear(IV, 0, diff);
  71. parameters = ivParam.Parameters;
  72. }
  73. Reset();
  74. // if it's null, key is to be reused.
  75. if (parameters != null)
  76. {
  77. cipher.Init(true, parameters);
  78. }
  79. }
  80. /**
  81. * return the algorithm name and mode.
  82. *
  83. * @return the name of the underlying algorithm followed by "/CFB"
  84. * and the block size in bits.
  85. */
  86. public string AlgorithmName => $@"{cipher.AlgorithmName}/CFB{blockSize * 8}";
  87. public bool IsPartialBlockOkay => true;
  88. /**
  89. * return the block size we are operating at.
  90. *
  91. * @return the block size we are operating at (in bytes).
  92. */
  93. public int GetBlockSize()
  94. {
  95. return blockSize;
  96. }
  97. /**
  98. * Process one block of input from the array in and write it to
  99. * the out array.
  100. *
  101. * @param in the array containing the input data.
  102. * @param inOff offset into the in array the data starts at.
  103. * @param out the array the output data will be copied into.
  104. * @param outOff the offset into the out array the output will start at.
  105. * @exception DataLengthException if there isn't enough data in in, or
  106. * space in out.
  107. * @exception InvalidOperationException if the cipher isn't initialised.
  108. * @return the number of bytes processed and produced.
  109. */
  110. private int ProcessBlock(
  111. byte[] input,
  112. int inOff,
  113. byte[] output,
  114. int outOff,
  115. bool change)
  116. {
  117. return encrypting
  118. ? EncryptBlock(input, inOff, output, outOff, change)
  119. : DecryptBlock(input, inOff, output, outOff, change);
  120. }
  121. public int ProcessBlock(byte[] inBuf, int inOff, byte[] outBuf, int outOff)
  122. {
  123. using var m = new MemoryStream(inBuf, inOff, inBuf.Length);
  124. var tmp = new byte[blockSize];
  125. var o = new byte[outBuf.Length - outOff + blockSize * 8];
  126. using var outStream = new MemoryStream(o);
  127. var ptr = _offset;
  128. int read;
  129. while ((read = m.Read(buff, _offset, buff.Length - _offset)) > 0)
  130. {
  131. if (read + _offset < buff.Length)
  132. {
  133. var len = ProcessBlock(buff, 0, tmp, 0, false);
  134. outStream.Write(tmp, 0, len);
  135. _offset += read;
  136. break;
  137. }
  138. else
  139. {
  140. var len = ProcessBlock(buff, 0, tmp, 0, true);
  141. outStream.Write(tmp, 0, len);
  142. _offset = 0;
  143. }
  144. }
  145. outStream.Seek(ptr, SeekOrigin.Begin);
  146. var res = inBuf.Length;
  147. outStream.Read(outBuf, outOff, res);
  148. return res;
  149. }
  150. /**
  151. * Do the appropriate processing for CFB mode encryption.
  152. *
  153. * @param in the array containing the data to be encrypted.
  154. * @param inOff offset into the in array the data starts at.
  155. * @param out the array the encrypted data will be copied into.
  156. * @param outOff the offset into the out array the output will start at.
  157. * @exception DataLengthException if there isn't enough data in in, or
  158. * space in out.
  159. * @exception InvalidOperationException if the cipher isn't initialised.
  160. * @return the number of bytes processed and produced.
  161. */
  162. public int EncryptBlock(
  163. byte[] input,
  164. int inOff,
  165. byte[] outBytes,
  166. int outOff,
  167. bool change)
  168. {
  169. if (inOff + blockSize > input.Length)
  170. {
  171. throw new DataLengthException("input buffer too short");
  172. }
  173. if (outOff + blockSize > outBytes.Length)
  174. {
  175. throw new DataLengthException("output buffer too short");
  176. }
  177. cipher.ProcessBlock(cfbV, 0, cfbOutV, 0);
  178. //
  179. // XOR the cfbV with the plaintext producing the ciphertext
  180. //
  181. for (var i = 0; i < blockSize; i++)
  182. {
  183. outBytes[outOff + i] = (byte)(cfbOutV[i] ^ input[inOff + i]);
  184. }
  185. //
  186. // change over the input block.
  187. //
  188. if (change)
  189. {
  190. Array.Copy(cfbV, blockSize, cfbV, 0, cfbV.Length - blockSize);
  191. Array.Copy(outBytes, outOff, cfbV, cfbV.Length - blockSize, blockSize);
  192. }
  193. return blockSize;
  194. }
  195. /**
  196. * Do the appropriate processing for CFB mode decryption.
  197. *
  198. * @param in the array containing the data to be decrypted.
  199. * @param inOff offset into the in array the data starts at.
  200. * @param out the array the encrypted data will be copied into.
  201. * @param outOff the offset into the out array the output will start at.
  202. * @exception DataLengthException if there isn't enough data in in, or
  203. * space in out.
  204. * @exception InvalidOperationException if the cipher isn't initialised.
  205. * @return the number of bytes processed and produced.
  206. */
  207. public int DecryptBlock(
  208. byte[] input,
  209. int inOff,
  210. byte[] outBytes,
  211. int outOff,
  212. bool change)
  213. {
  214. if (inOff + blockSize > input.Length)
  215. {
  216. throw new DataLengthException("input buffer too short");
  217. }
  218. if (outOff + blockSize > outBytes.Length)
  219. {
  220. throw new DataLengthException("output buffer too short");
  221. }
  222. cipher.ProcessBlock(cfbV, 0, cfbOutV, 0);
  223. //
  224. // change over the input block.
  225. //
  226. if (change)
  227. {
  228. Array.Copy(cfbV, blockSize, cfbV, 0, cfbV.Length - blockSize);
  229. Array.Copy(input, inOff, cfbV, cfbV.Length - blockSize, blockSize);
  230. }
  231. //
  232. // XOR the cfbV with the ciphertext producing the plaintext
  233. //
  234. for (var i = 0; i < blockSize; i++)
  235. {
  236. outBytes[outOff + i] = (byte)(cfbOutV[i] ^ input[inOff + i]);
  237. }
  238. return blockSize;
  239. }
  240. /**
  241. * reset the chaining vector back to the IV and reset the underlying
  242. * cipher.
  243. */
  244. public void Reset()
  245. {
  246. Array.Copy(IV, 0, cfbV, 0, IV.Length);
  247. _offset = 0;
  248. cipher.Reset();
  249. }
  250. }
  251. }