BufferedBlockCipher.cs 8.8 KB

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