CfbBlockCipher.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  5. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Modes
  6. {
  7. /**
  8. * implements a Cipher-FeedBack (CFB) mode on top of a simple cipher.
  9. */
  10. public class CfbBlockCipher
  11. : IBlockCipher
  12. {
  13. private byte[] IV;
  14. private byte[] cfbV;
  15. private byte[] cfbOutV;
  16. private bool encrypting;
  17. private readonly int blockSize;
  18. private readonly IBlockCipher cipher;
  19. /**
  20. * Basic constructor.
  21. *
  22. * @param cipher the block cipher to be used as the basis of the
  23. * feedback mode.
  24. * @param blockSize the block size in bits (note: a multiple of 8)
  25. */
  26. public CfbBlockCipher(
  27. IBlockCipher cipher,
  28. int bitBlockSize)
  29. {
  30. this.cipher = cipher;
  31. this.blockSize = bitBlockSize / 8;
  32. this.IV = new byte[cipher.GetBlockSize()];
  33. this.cfbV = new byte[cipher.GetBlockSize()];
  34. this.cfbOutV = new byte[cipher.GetBlockSize()];
  35. }
  36. /**
  37. * return the underlying block cipher that we are wrapping.
  38. *
  39. * @return the underlying block cipher that we are wrapping.
  40. */
  41. public IBlockCipher GetUnderlyingCipher()
  42. {
  43. return cipher;
  44. }
  45. /**
  46. * Initialise the cipher and, possibly, the initialisation vector (IV).
  47. * If an IV isn't passed as part of the parameter, the IV will be all zeros.
  48. * An IV which is too short is handled in FIPS compliant fashion.
  49. *
  50. * @param forEncryption if true the cipher is initialised for
  51. * encryption, if false for decryption.
  52. * @param param the key and other data required by the cipher.
  53. * @exception ArgumentException if the parameters argument is
  54. * inappropriate.
  55. */
  56. public void Init(
  57. bool forEncryption,
  58. ICipherParameters parameters)
  59. {
  60. this.encrypting = forEncryption;
  61. if (parameters is ParametersWithIV)
  62. {
  63. ParametersWithIV ivParam = (ParametersWithIV) parameters;
  64. byte[] iv = ivParam.GetIV();
  65. int diff = IV.Length - iv.Length;
  66. Array.Copy(iv, 0, IV, diff, iv.Length);
  67. Array.Clear(IV, 0, diff);
  68. parameters = ivParam.Parameters;
  69. }
  70. Reset();
  71. // if it's null, key is to be reused.
  72. if (parameters != null)
  73. {
  74. cipher.Init(true, parameters);
  75. }
  76. }
  77. /**
  78. * return the algorithm name and mode.
  79. *
  80. * @return the name of the underlying algorithm followed by "/CFB"
  81. * and the block size in bits.
  82. */
  83. public string AlgorithmName
  84. {
  85. get { return cipher.AlgorithmName + "/CFB" + (blockSize * 8); }
  86. }
  87. public bool IsPartialBlockOkay
  88. {
  89. get { return true; }
  90. }
  91. /**
  92. * return the block size we are operating at.
  93. *
  94. * @return the block size we are operating at (in bytes).
  95. */
  96. public int GetBlockSize()
  97. {
  98. return blockSize;
  99. }
  100. /**
  101. * Process one block of input from the array in and write it to
  102. * the out array.
  103. *
  104. * @param in the array containing the input data.
  105. * @param inOff offset into the in array the data starts at.
  106. * @param out the array the output data will be copied into.
  107. * @param outOff the offset into the out array the output will start at.
  108. * @exception DataLengthException if there isn't enough data in in, or
  109. * space in out.
  110. * @exception InvalidOperationException if the cipher isn't initialised.
  111. * @return the number of bytes processed and produced.
  112. */
  113. public int ProcessBlock(
  114. byte[] input,
  115. int inOff,
  116. byte[] output,
  117. int outOff)
  118. {
  119. return (encrypting)
  120. ? EncryptBlock(input, inOff, output, outOff)
  121. : DecryptBlock(input, inOff, output, outOff);
  122. }
  123. /**
  124. * Do the appropriate processing for CFB mode encryption.
  125. *
  126. * @param in the array containing the data to be encrypted.
  127. * @param inOff offset into the in array the data starts at.
  128. * @param out the array the encrypted data will be copied into.
  129. * @param outOff the offset into the out array the output will start at.
  130. * @exception DataLengthException if there isn't enough data in in, or
  131. * space in out.
  132. * @exception InvalidOperationException if the cipher isn't initialised.
  133. * @return the number of bytes processed and produced.
  134. */
  135. public int EncryptBlock(
  136. byte[] input,
  137. int inOff,
  138. byte[] outBytes,
  139. int outOff)
  140. {
  141. if ((inOff + blockSize) > input.Length)
  142. {
  143. throw new DataLengthException("input buffer too short");
  144. }
  145. if ((outOff + blockSize) > outBytes.Length)
  146. {
  147. throw new DataLengthException("output buffer too short");
  148. }
  149. cipher.ProcessBlock(cfbV, 0, cfbOutV, 0);
  150. //
  151. // XOR the cfbV with the plaintext producing the ciphertext
  152. //
  153. for (int i = 0; i < blockSize; i++)
  154. {
  155. outBytes[outOff + i] = (byte)(cfbOutV[i] ^ input[inOff + i]);
  156. }
  157. //
  158. // change over the input block.
  159. //
  160. Array.Copy(cfbV, blockSize, cfbV, 0, cfbV.Length - blockSize);
  161. Array.Copy(outBytes, outOff, cfbV, cfbV.Length - blockSize, blockSize);
  162. return blockSize;
  163. }
  164. /**
  165. * Do the appropriate processing for CFB mode decryption.
  166. *
  167. * @param in the array containing the data to be decrypted.
  168. * @param inOff offset into the in array the data starts at.
  169. * @param out the array the encrypted data will be copied into.
  170. * @param outOff the offset into the out array the output will start at.
  171. * @exception DataLengthException if there isn't enough data in in, or
  172. * space in out.
  173. * @exception InvalidOperationException if the cipher isn't initialised.
  174. * @return the number of bytes processed and produced.
  175. */
  176. public int DecryptBlock(
  177. byte[] input,
  178. int inOff,
  179. byte[] outBytes,
  180. int outOff)
  181. {
  182. if ((inOff + blockSize) > input.Length)
  183. {
  184. throw new DataLengthException("input buffer too short");
  185. }
  186. if ((outOff + blockSize) > outBytes.Length)
  187. {
  188. throw new DataLengthException("output buffer too short");
  189. }
  190. cipher.ProcessBlock(cfbV, 0, cfbOutV, 0);
  191. //
  192. // change over the input block.
  193. //
  194. Array.Copy(cfbV, blockSize, cfbV, 0, cfbV.Length - blockSize);
  195. Array.Copy(input, inOff, cfbV, cfbV.Length - blockSize, blockSize);
  196. //
  197. // XOR the cfbV with the ciphertext producing the plaintext
  198. //
  199. for (int i = 0; i < blockSize; i++)
  200. {
  201. outBytes[outOff + i] = (byte)(cfbOutV[i] ^ input[inOff + i]);
  202. }
  203. return blockSize;
  204. }
  205. /**
  206. * reset the chaining vector back to the IV and reset the underlying
  207. * cipher.
  208. */
  209. public void Reset()
  210. {
  211. Array.Copy(IV, 0, cfbV, 0, IV.Length);
  212. cipher.Reset();
  213. }
  214. }
  215. }
  216. #pragma warning restore
  217. #endif