CfbBlockCipher.cs 7.6 KB

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