PaddedBufferedBlockCipher.cs 7.1 KB

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