BufferedBlockCipher.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Diagnostics;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto
  7. {
  8. /**
  9. * A wrapper class that allows block ciphers to be used to process data in
  10. * a piecemeal fashion. The BufferedBlockCipher outputs a block only when the
  11. * buffer is full and more data is being added, or on a doFinal.
  12. * <p>
  13. * Note: in the case where the underlying cipher is either a CFB cipher or an
  14. * OFB one the last block may not be a multiple of the block size.
  15. * </p>
  16. */
  17. public class BufferedBlockCipher
  18. : BufferedCipherBase
  19. {
  20. internal byte[] buf;
  21. internal int bufOff;
  22. internal bool forEncryption;
  23. internal IBlockCipher cipher;
  24. /**
  25. * constructor for subclasses
  26. */
  27. protected BufferedBlockCipher()
  28. {
  29. }
  30. /**
  31. * Create a buffered block cipher without padding.
  32. *
  33. * @param cipher the underlying block cipher this buffering object wraps.
  34. * false otherwise.
  35. */
  36. public BufferedBlockCipher(
  37. IBlockCipher cipher)
  38. {
  39. if (cipher == null)
  40. throw new ArgumentNullException("cipher");
  41. this.cipher = cipher;
  42. buf = new byte[cipher.GetBlockSize()];
  43. bufOff = 0;
  44. }
  45. public override string AlgorithmName
  46. {
  47. get { return cipher.AlgorithmName; }
  48. }
  49. /**
  50. * initialise the cipher.
  51. *
  52. * @param forEncryption if true the cipher is initialised for
  53. * encryption, if false for decryption.
  54. * @param param the key and other data required by the cipher.
  55. * @exception ArgumentException if the parameters argument is
  56. * inappropriate.
  57. */
  58. // Note: This doubles as the Init in the event that this cipher is being used as an IWrapper
  59. public override void Init(
  60. bool forEncryption,
  61. ICipherParameters parameters)
  62. {
  63. this.forEncryption = forEncryption;
  64. ParametersWithRandom pwr = parameters as ParametersWithRandom;
  65. if (pwr != null)
  66. parameters = pwr.Parameters;
  67. Reset();
  68. cipher.Init(forEncryption, parameters);
  69. }
  70. /**
  71. * return the blocksize for the underlying cipher.
  72. *
  73. * @return the blocksize for the underlying cipher.
  74. */
  75. public override int GetBlockSize()
  76. {
  77. return cipher.GetBlockSize();
  78. }
  79. /**
  80. * return the size of the output buffer required for an update
  81. * an input of len bytes.
  82. *
  83. * @param len the length of the input.
  84. * @return the space required to accommodate a call to update
  85. * with len bytes of input.
  86. */
  87. public override int GetUpdateOutputSize(
  88. int length)
  89. {
  90. int total = length + bufOff;
  91. int leftOver = total % buf.Length;
  92. return total - leftOver;
  93. }
  94. /**
  95. * return the size of the output buffer required for an update plus a
  96. * doFinal with an input of len bytes.
  97. *
  98. * @param len the length of the input.
  99. * @return the space required to accommodate a call to update and doFinal
  100. * with len bytes of input.
  101. */
  102. public override int GetOutputSize(
  103. int length)
  104. {
  105. // Note: Can assume IsPartialBlockOkay is true for purposes of this calculation
  106. return length + bufOff;
  107. }
  108. /**
  109. * process a single byte, producing an output block if necessary.
  110. *
  111. * @param in the input byte.
  112. * @param out the space for any output that might be produced.
  113. * @param outOff the offset from which the output will be copied.
  114. * @return the number of output bytes copied to out.
  115. * @exception DataLengthException if there isn't enough space in out.
  116. * @exception InvalidOperationException if the cipher isn't initialised.
  117. */
  118. public override int ProcessByte(
  119. byte input,
  120. byte[] output,
  121. int outOff)
  122. {
  123. buf[bufOff++] = input;
  124. if (bufOff == buf.Length)
  125. {
  126. if ((outOff + buf.Length) > output.Length)
  127. throw new DataLengthException("output buffer too short");
  128. bufOff = 0;
  129. return cipher.ProcessBlock(buf, 0, output, outOff);
  130. }
  131. return 0;
  132. }
  133. public override byte[] ProcessByte(
  134. byte input)
  135. {
  136. int outLength = GetUpdateOutputSize(1);
  137. byte[] outBytes = outLength > 0 ? new byte[outLength] : null;
  138. int pos = ProcessByte(input, outBytes, 0);
  139. if (outLength > 0 && pos < outLength)
  140. {
  141. byte[] tmp = new byte[pos];
  142. Array.Copy(outBytes, 0, tmp, 0, pos);
  143. outBytes = tmp;
  144. }
  145. return outBytes;
  146. }
  147. public override byte[] ProcessBytes(
  148. byte[] input,
  149. int inOff,
  150. int length)
  151. {
  152. if (input == null)
  153. throw new ArgumentNullException("input");
  154. if (length < 1)
  155. return null;
  156. int outLength = GetUpdateOutputSize(length);
  157. byte[] outBytes = outLength > 0 ? new byte[outLength] : null;
  158. int pos = ProcessBytes(input, inOff, length, outBytes, 0);
  159. if (outLength > 0 && pos < outLength)
  160. {
  161. byte[] tmp = new byte[pos];
  162. Array.Copy(outBytes, 0, tmp, 0, pos);
  163. outBytes = tmp;
  164. }
  165. return outBytes;
  166. }
  167. /**
  168. * process an array of bytes, producing output if necessary.
  169. *
  170. * @param in the input byte array.
  171. * @param inOff the offset at which the input data starts.
  172. * @param len the number of bytes to be copied out of the input array.
  173. * @param out the space for any output that might be produced.
  174. * @param outOff the offset from which the output will be copied.
  175. * @return the number of output bytes copied to out.
  176. * @exception DataLengthException if there isn't enough space in out.
  177. * @exception InvalidOperationException if the cipher isn't initialised.
  178. */
  179. public override int ProcessBytes(
  180. byte[] input,
  181. int inOff,
  182. int length,
  183. byte[] output,
  184. int outOff)
  185. {
  186. if (length < 1)
  187. {
  188. if (length < 0)
  189. throw new ArgumentException("Can't have a negative input length!");
  190. return 0;
  191. }
  192. int blockSize = GetBlockSize();
  193. int outLength = GetUpdateOutputSize(length);
  194. if (outLength > 0)
  195. {
  196. Check.OutputLength(output, outOff, outLength, "output buffer too short");
  197. }
  198. int resultLen = 0;
  199. int gapLen = buf.Length - bufOff;
  200. if (length > gapLen)
  201. {
  202. Array.Copy(input, inOff, buf, bufOff, gapLen);
  203. resultLen += cipher.ProcessBlock(buf, 0, output, outOff);
  204. bufOff = 0;
  205. length -= gapLen;
  206. inOff += gapLen;
  207. while (length > buf.Length)
  208. {
  209. resultLen += cipher.ProcessBlock(input, inOff, output, outOff + resultLen);
  210. length -= blockSize;
  211. inOff += blockSize;
  212. }
  213. }
  214. Array.Copy(input, inOff, buf, bufOff, length);
  215. bufOff += length;
  216. if (bufOff == buf.Length)
  217. {
  218. resultLen += cipher.ProcessBlock(buf, 0, output, outOff + resultLen);
  219. bufOff = 0;
  220. }
  221. return resultLen;
  222. }
  223. public override byte[] DoFinal()
  224. {
  225. byte[] outBytes = EmptyBuffer;
  226. int length = GetOutputSize(0);
  227. if (length > 0)
  228. {
  229. outBytes = new byte[length];
  230. int pos = DoFinal(outBytes, 0);
  231. if (pos < outBytes.Length)
  232. {
  233. byte[] tmp = new byte[pos];
  234. Array.Copy(outBytes, 0, tmp, 0, pos);
  235. outBytes = tmp;
  236. }
  237. }
  238. else
  239. {
  240. Reset();
  241. }
  242. return outBytes;
  243. }
  244. public override byte[] DoFinal(
  245. byte[] input,
  246. int inOff,
  247. int inLen)
  248. {
  249. if (input == null)
  250. throw new ArgumentNullException("input");
  251. int length = GetOutputSize(inLen);
  252. byte[] outBytes = EmptyBuffer;
  253. if (length > 0)
  254. {
  255. outBytes = new byte[length];
  256. int pos = (inLen > 0)
  257. ? ProcessBytes(input, inOff, inLen, outBytes, 0)
  258. : 0;
  259. pos += DoFinal(outBytes, pos);
  260. if (pos < outBytes.Length)
  261. {
  262. byte[] tmp = new byte[pos];
  263. Array.Copy(outBytes, 0, tmp, 0, pos);
  264. outBytes = tmp;
  265. }
  266. }
  267. else
  268. {
  269. Reset();
  270. }
  271. return outBytes;
  272. }
  273. /**
  274. * Process the last block in the buffer.
  275. *
  276. * @param out the array the block currently being held is copied into.
  277. * @param outOff the offset at which the copying starts.
  278. * @return the number of output bytes copied to out.
  279. * @exception DataLengthException if there is insufficient space in out for
  280. * the output, or the input is not block size aligned and should be.
  281. * @exception InvalidOperationException if the underlying cipher is not
  282. * initialised.
  283. * @exception InvalidCipherTextException if padding is expected and not found.
  284. * @exception DataLengthException if the input is not block size
  285. * aligned.
  286. */
  287. public override int DoFinal(
  288. byte[] output,
  289. int outOff)
  290. {
  291. try
  292. {
  293. if (bufOff != 0)
  294. {
  295. Check.DataLength(!cipher.IsPartialBlockOkay, "data not block size aligned");
  296. Check.OutputLength(output, outOff, bufOff, "output buffer too short for DoFinal()");
  297. // NB: Can't copy directly, or we may write too much output
  298. cipher.ProcessBlock(buf, 0, buf, 0);
  299. Array.Copy(buf, 0, output, outOff, bufOff);
  300. }
  301. return bufOff;
  302. }
  303. finally
  304. {
  305. Reset();
  306. }
  307. }
  308. /**
  309. * Reset the buffer and cipher. After resetting the object is in the same
  310. * state as it was after the last init (if there was one).
  311. */
  312. public override void Reset()
  313. {
  314. Array.Clear(buf, 0, buf.Length);
  315. bufOff = 0;
  316. cipher.Reset();
  317. }
  318. }
  319. }
  320. #pragma warning restore
  321. #endif