CtsBlockCipher.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  7. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Modes
  8. {
  9. /**
  10. * A Cipher Text Stealing (CTS) mode cipher. CTS allows block ciphers to
  11. * be used to produce cipher text which is the same outLength as the plain text.
  12. */
  13. public class CtsBlockCipher
  14. : BufferedBlockCipher
  15. {
  16. private readonly int blockSize;
  17. /**
  18. * Create a buffered block cipher that uses Cipher Text Stealing
  19. *
  20. * @param cipher the underlying block cipher this buffering object wraps.
  21. */
  22. public CtsBlockCipher(
  23. IBlockCipher cipher)
  24. {
  25. // TODO Should this test for acceptable ones instead?
  26. if (cipher is OfbBlockCipher || cipher is CfbBlockCipher)
  27. throw new ArgumentException("CtsBlockCipher can only accept ECB, or CBC ciphers");
  28. this.cipher = cipher;
  29. blockSize = cipher.GetBlockSize();
  30. buf = new byte[blockSize * 2];
  31. bufOff = 0;
  32. }
  33. /**
  34. * return the size of the output buffer required for an update of 'length' bytes.
  35. *
  36. * @param length the outLength of the input.
  37. * @return the space required to accommodate a call to update
  38. * with length bytes of input.
  39. */
  40. public override int GetUpdateOutputSize(
  41. int length)
  42. {
  43. int total = length + bufOff;
  44. int leftOver = total % buf.Length;
  45. if (leftOver == 0)
  46. {
  47. return total - buf.Length;
  48. }
  49. return total - leftOver;
  50. }
  51. /**
  52. * return the size of the output buffer required for an update plus a
  53. * doFinal with an input of length bytes.
  54. *
  55. * @param length the outLength of the input.
  56. * @return the space required to accommodate a call to update and doFinal
  57. * with length bytes of input.
  58. */
  59. public override int GetOutputSize(
  60. int length)
  61. {
  62. return length + bufOff;
  63. }
  64. /**
  65. * process a single byte, producing an output block if necessary.
  66. *
  67. * @param in the input byte.
  68. * @param out the space for any output that might be produced.
  69. * @param outOff the offset from which the output will be copied.
  70. * @return the number of output bytes copied to out.
  71. * @exception DataLengthException if there isn't enough space in out.
  72. * @exception InvalidOperationException if the cipher isn't initialised.
  73. */
  74. public override int ProcessByte(
  75. byte input,
  76. byte[] output,
  77. int outOff)
  78. {
  79. int resultLen = 0;
  80. if (bufOff == buf.Length)
  81. {
  82. resultLen = cipher.ProcessBlock(buf, 0, output, outOff);
  83. Debug.Assert(resultLen == blockSize);
  84. Array.Copy(buf, blockSize, buf, 0, blockSize);
  85. bufOff = blockSize;
  86. }
  87. buf[bufOff++] = input;
  88. return resultLen;
  89. }
  90. /**
  91. * process an array of bytes, producing output if necessary.
  92. *
  93. * @param in the input byte array.
  94. * @param inOff the offset at which the input data starts.
  95. * @param length the number of bytes to be copied out of the input array.
  96. * @param out the space for any output that might be produced.
  97. * @param outOff the offset from which the output will be copied.
  98. * @return the number of output bytes copied to out.
  99. * @exception DataLengthException if there isn't enough space in out.
  100. * @exception InvalidOperationException if the cipher isn't initialised.
  101. */
  102. public override int ProcessBytes(
  103. byte[] input,
  104. int inOff,
  105. int length,
  106. byte[] output,
  107. int outOff)
  108. {
  109. if (length < 0)
  110. {
  111. throw new ArgumentException("Can't have a negative input outLength!");
  112. }
  113. int blockSize = GetBlockSize();
  114. int outLength = GetUpdateOutputSize(length);
  115. if (outLength > 0)
  116. {
  117. if ((outOff + outLength) > output.Length)
  118. {
  119. throw new DataLengthException("output buffer too short");
  120. }
  121. }
  122. int resultLen = 0;
  123. int gapLen = buf.Length - bufOff;
  124. if (length > gapLen)
  125. {
  126. Array.Copy(input, inOff, buf, bufOff, gapLen);
  127. resultLen += cipher.ProcessBlock(buf, 0, output, outOff);
  128. Array.Copy(buf, blockSize, buf, 0, blockSize);
  129. bufOff = blockSize;
  130. length -= gapLen;
  131. inOff += gapLen;
  132. while (length > blockSize)
  133. {
  134. Array.Copy(input, inOff, buf, bufOff, blockSize);
  135. resultLen += cipher.ProcessBlock(buf, 0, output, outOff + resultLen);
  136. Array.Copy(buf, blockSize, buf, 0, blockSize);
  137. length -= blockSize;
  138. inOff += blockSize;
  139. }
  140. }
  141. Array.Copy(input, inOff, buf, bufOff, length);
  142. bufOff += length;
  143. return resultLen;
  144. }
  145. /**
  146. * Process the last block in the buffer.
  147. *
  148. * @param out the array the block currently being held is copied into.
  149. * @param outOff the offset at which the copying starts.
  150. * @return the number of output bytes copied to out.
  151. * @exception DataLengthException if there is insufficient space in out for
  152. * the output.
  153. * @exception InvalidOperationException if the underlying cipher is not
  154. * initialised.
  155. * @exception InvalidCipherTextException if cipher text decrypts wrongly (in
  156. * case the exception will never Get thrown).
  157. */
  158. public override int DoFinal(
  159. byte[] output,
  160. int outOff)
  161. {
  162. if (bufOff + outOff > output.Length)
  163. {
  164. throw new DataLengthException("output buffer too small in doFinal");
  165. }
  166. int blockSize = cipher.GetBlockSize();
  167. int length = bufOff - blockSize;
  168. byte[] block = new byte[blockSize];
  169. if (forEncryption)
  170. {
  171. cipher.ProcessBlock(buf, 0, block, 0);
  172. if (bufOff < blockSize)
  173. {
  174. throw new DataLengthException("need at least one block of input for CTS");
  175. }
  176. for (int i = bufOff; i != buf.Length; i++)
  177. {
  178. buf[i] = block[i - blockSize];
  179. }
  180. for (int i = blockSize; i != bufOff; i++)
  181. {
  182. buf[i] ^= block[i - blockSize];
  183. }
  184. IBlockCipher c = (cipher is CbcBlockCipher)
  185. ? ((CbcBlockCipher)cipher).GetUnderlyingCipher()
  186. : cipher;
  187. c.ProcessBlock(buf, blockSize, output, outOff);
  188. Array.Copy(block, 0, output, outOff + blockSize, length);
  189. }
  190. else
  191. {
  192. byte[] lastBlock = new byte[blockSize];
  193. IBlockCipher c = (cipher is CbcBlockCipher)
  194. ? ((CbcBlockCipher)cipher).GetUnderlyingCipher()
  195. : cipher;
  196. c.ProcessBlock(buf, 0, block, 0);
  197. for (int i = blockSize; i != bufOff; i++)
  198. {
  199. lastBlock[i - blockSize] = (byte)(block[i - blockSize] ^ buf[i]);
  200. }
  201. Array.Copy(buf, blockSize, block, 0, length);
  202. cipher.ProcessBlock(block, 0, output, outOff);
  203. Array.Copy(lastBlock, 0, output, outOff + blockSize, length);
  204. }
  205. int offset = bufOff;
  206. Reset();
  207. return offset;
  208. }
  209. }
  210. }
  211. #pragma warning restore
  212. #endif