PaddedBufferedBlockCipher.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using Org.BouncyCastle.Crypto;
  4. using Org.BouncyCastle.Crypto.Parameters;
  5. using Org.BouncyCastle.Security;
  6. namespace Org.BouncyCastle.Crypto.Paddings
  7. {
  8. /**
  9. * A wrapper class that allows block ciphers to be used to process data in
  10. * a piecemeal fashion with padding. The PaddedBufferedBlockCipher
  11. * outputs a block only when the buffer is full and more data is being added,
  12. * or on a doFinal (unless the current block in the buffer is a pad block).
  13. * The default padding mechanism used is the one outlined in Pkcs5/Pkcs7.
  14. */
  15. public class PaddedBufferedBlockCipher
  16. : BufferedBlockCipher
  17. {
  18. private readonly IBlockCipherPadding padding;
  19. /**
  20. * Create a buffered block cipher with the desired padding.
  21. *
  22. * @param cipher the underlying block cipher this buffering object wraps.
  23. * @param padding the padding type.
  24. */
  25. public PaddedBufferedBlockCipher(
  26. IBlockCipher cipher,
  27. IBlockCipherPadding padding)
  28. {
  29. this.cipher = cipher;
  30. this.padding = padding;
  31. buf = new byte[cipher.GetBlockSize()];
  32. bufOff = 0;
  33. }
  34. /**
  35. * Create a buffered block cipher Pkcs7 padding
  36. *
  37. * @param cipher the underlying block cipher this buffering object wraps.
  38. */
  39. public PaddedBufferedBlockCipher(
  40. IBlockCipher cipher)
  41. : this(cipher, new Pkcs7Padding()) { }
  42. /**
  43. * initialise the cipher.
  44. *
  45. * @param forEncryption if true the cipher is initialised for
  46. * encryption, if false for decryption.
  47. * @param param the key and other data required by the cipher.
  48. * @exception ArgumentException if the parameters argument is
  49. * inappropriate.
  50. */
  51. public override void Init(
  52. bool forEncryption,
  53. ICipherParameters parameters)
  54. {
  55. this.forEncryption = forEncryption;
  56. SecureRandom initRandom = null;
  57. if (parameters is ParametersWithRandom)
  58. {
  59. ParametersWithRandom p = (ParametersWithRandom)parameters;
  60. initRandom = p.Random;
  61. parameters = p.Parameters;
  62. }
  63. Reset();
  64. padding.Init(initRandom);
  65. cipher.Init(forEncryption, parameters);
  66. }
  67. /**
  68. * return the minimum size of the output buffer required for an update
  69. * plus a doFinal with an input of len bytes.
  70. *
  71. * @param len the length of the input.
  72. * @return the space required to accommodate a call to update and doFinal
  73. * with len bytes of input.
  74. */
  75. public override int GetOutputSize(
  76. int length)
  77. {
  78. int total = length + bufOff;
  79. int leftOver = total % buf.Length;
  80. if (leftOver == 0)
  81. {
  82. if (forEncryption)
  83. {
  84. return total + buf.Length;
  85. }
  86. return total;
  87. }
  88. return total - leftOver + buf.Length;
  89. }
  90. /**
  91. * return the size of the output buffer required for an update
  92. * an input of len bytes.
  93. *
  94. * @param len the length of the input.
  95. * @return the space required to accommodate a call to update
  96. * with len bytes of input.
  97. */
  98. public override int GetUpdateOutputSize(
  99. int length)
  100. {
  101. int total = length + bufOff;
  102. int leftOver = total % buf.Length;
  103. if (leftOver == 0)
  104. {
  105. return total - buf.Length;
  106. }
  107. return total - leftOver;
  108. }
  109. /**
  110. * process a single byte, producing an output block if necessary.
  111. *
  112. * @param in the input byte.
  113. * @param out the space for any output that might be produced.
  114. * @param outOff the offset from which the output will be copied.
  115. * @return the number of output bytes copied to out.
  116. * @exception DataLengthException if there isn't enough space in out.
  117. * @exception InvalidOperationException if the cipher isn't initialised.
  118. */
  119. public override int ProcessByte(
  120. byte input,
  121. byte[] output,
  122. int outOff)
  123. {
  124. int resultLen = 0;
  125. if (bufOff == buf.Length)
  126. {
  127. resultLen = cipher.ProcessBlock(buf, 0, output, outOff);
  128. bufOff = 0;
  129. }
  130. buf[bufOff++] = input;
  131. return resultLen;
  132. }
  133. /**
  134. * process an array of bytes, producing output if necessary.
  135. *
  136. * @param in the input byte array.
  137. * @param inOff the offset at which the input data starts.
  138. * @param len the number of bytes to be copied out of the input array.
  139. * @param out the space for any output that might be produced.
  140. * @param outOff the offset from which the output will be copied.
  141. * @return the number of output bytes copied to out.
  142. * @exception DataLengthException if there isn't enough space in out.
  143. * @exception InvalidOperationException if the cipher isn't initialised.
  144. */
  145. public override int ProcessBytes(
  146. byte[] input,
  147. int inOff,
  148. int length,
  149. byte[] output,
  150. int outOff)
  151. {
  152. if (length < 0)
  153. {
  154. throw new ArgumentException("Can't have a negative input length!");
  155. }
  156. int blockSize = GetBlockSize();
  157. int outLength = GetUpdateOutputSize(length);
  158. if (outLength > 0)
  159. {
  160. Check.OutputLength(output, outOff, outLength, "output buffer too short");
  161. }
  162. int resultLen = 0;
  163. int gapLen = buf.Length - bufOff;
  164. if (length > gapLen)
  165. {
  166. Array.Copy(input, inOff, buf, bufOff, gapLen);
  167. resultLen += cipher.ProcessBlock(buf, 0, output, outOff);
  168. bufOff = 0;
  169. length -= gapLen;
  170. inOff += gapLen;
  171. while (length > buf.Length)
  172. {
  173. resultLen += cipher.ProcessBlock(input, inOff, output, outOff + resultLen);
  174. length -= blockSize;
  175. inOff += blockSize;
  176. }
  177. }
  178. Array.Copy(input, inOff, buf, bufOff, length);
  179. bufOff += length;
  180. return resultLen;
  181. }
  182. /**
  183. * Process the last block in the buffer. If the buffer is currently
  184. * full and padding needs to be added a call to doFinal will produce
  185. * 2 * GetBlockSize() bytes.
  186. *
  187. * @param out the array the block currently being held is copied into.
  188. * @param outOff the offset at which the copying starts.
  189. * @return the number of output bytes copied to out.
  190. * @exception DataLengthException if there is insufficient space in out for
  191. * the output or we are decrypting and the input is not block size aligned.
  192. * @exception InvalidOperationException if the underlying cipher is not
  193. * initialised.
  194. * @exception InvalidCipherTextException if padding is expected and not found.
  195. */
  196. public override int DoFinal(
  197. byte[] output,
  198. int outOff)
  199. {
  200. int blockSize = cipher.GetBlockSize();
  201. int resultLen = 0;
  202. if (forEncryption)
  203. {
  204. if (bufOff == blockSize)
  205. {
  206. if ((outOff + 2 * blockSize) > output.Length)
  207. {
  208. Reset();
  209. throw new OutputLengthException("output buffer too short");
  210. }
  211. resultLen = cipher.ProcessBlock(buf, 0, output, outOff);
  212. bufOff = 0;
  213. }
  214. padding.AddPadding(buf, bufOff);
  215. resultLen += cipher.ProcessBlock(buf, 0, output, outOff + resultLen);
  216. Reset();
  217. }
  218. else
  219. {
  220. if (bufOff == blockSize)
  221. {
  222. resultLen = cipher.ProcessBlock(buf, 0, buf, 0);
  223. bufOff = 0;
  224. }
  225. else
  226. {
  227. Reset();
  228. throw new DataLengthException("last block incomplete in decryption");
  229. }
  230. try
  231. {
  232. resultLen -= padding.PadCount(buf);
  233. Array.Copy(buf, 0, output, outOff, resultLen);
  234. }
  235. finally
  236. {
  237. Reset();
  238. }
  239. }
  240. return resultLen;
  241. }
  242. }
  243. }
  244. #endif