CfbBlockCipher.cs 7.9 KB

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