You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ExtendedCfbBlockCipher.cs 8.4 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // original: https://github.com/bcgit/bc-csharp/blob/master/crypto/src/crypto/modes/CfbBlockCipher.cs
  2. // changes: 5th parameter for ProcessBlock, to process without change internal state
  3. using Org.BouncyCastle.Crypto.Parameters;
  4. using System;
  5. namespace Org.BouncyCastle.Crypto.Modes
  6. {
  7. /**
  8. * implements a Cipher-FeedBack (CFB) mode on top of a simple cipher.
  9. */
  10. public class ExtendedCfbBlockCipher
  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 ExtendedCfbBlockCipher(
  27. IBlockCipher cipher,
  28. int bitBlockSize)
  29. {
  30. this.cipher = cipher;
  31. blockSize = bitBlockSize / 8;
  32. IV = new byte[cipher.GetBlockSize()];
  33. cfbV = new byte[cipher.GetBlockSize()];
  34. 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. 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 => cipher.AlgorithmName + "/CFB" + (blockSize * 8);
  84. public bool IsPartialBlockOkay => true;
  85. /**
  86. * return the block size we are operating at.
  87. *
  88. * @return the block size we are operating at (in bytes).
  89. */
  90. public int GetBlockSize()
  91. {
  92. return blockSize;
  93. }
  94. /**
  95. * Process one block of input from the array in and write it to
  96. * the out array.
  97. *
  98. * @param in the array containing the input data.
  99. * @param inOff offset into the in array the data starts at.
  100. * @param out the array the output data will be copied into.
  101. * @param outOff the offset into the out array the output will start at.
  102. * @param updateContext update internal state after process.
  103. * @exception DataLengthException if there isn't enough data in in, or
  104. * space in out.
  105. * @exception InvalidOperationException if the cipher isn't initialised.
  106. * @return the number of bytes processed and produced.
  107. */
  108. public int ProcessBlock(
  109. byte[] input,
  110. int inOff,
  111. byte[] output,
  112. int outOff,
  113. bool updateContext)
  114. {
  115. return (encrypting)
  116. ? EncryptBlock(input, inOff, output, outOff, updateContext)
  117. : DecryptBlock(input, inOff, output, outOff, updateContext);
  118. }
  119. /**
  120. * Do the appropriate processing for CFB mode encryption.
  121. *
  122. * @param in the array containing the data to be encrypted.
  123. * @param inOff offset into the in array the data starts at.
  124. * @param out the array the encrypted data will be copied into.
  125. * @param outOff the offset into the out array the output will start at.
  126. * @exception DataLengthException if there isn't enough data in in, or
  127. * space in out.
  128. * @exception InvalidOperationException if the cipher isn't initialised.
  129. * @return the number of bytes processed and produced.
  130. */
  131. public int EncryptBlock(
  132. byte[] input,
  133. int inOff,
  134. byte[] outBytes,
  135. int outOff,
  136. bool updateContext = true)
  137. {
  138. if ((inOff + blockSize) > input.Length)
  139. {
  140. throw new DataLengthException("input buffer too short");
  141. }
  142. if ((outOff + blockSize) > outBytes.Length)
  143. {
  144. throw new DataLengthException("output buffer too short");
  145. }
  146. cipher.ProcessBlock(cfbV, 0, cfbOutV, 0);
  147. //
  148. // XOR the cfbV with the plaintext producing the ciphertext
  149. //
  150. for (int i = 0; i < blockSize; i++)
  151. {
  152. outBytes[outOff + i] = (byte)(cfbOutV[i] ^ input[inOff + i]);
  153. }
  154. if (updateContext)
  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. }
  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. bool updateContext = true)
  182. {
  183. if ((inOff + blockSize) > input.Length)
  184. {
  185. throw new DataLengthException("input buffer too short");
  186. }
  187. if ((outOff + blockSize) > outBytes.Length)
  188. {
  189. throw new DataLengthException("output buffer too short");
  190. }
  191. cipher.ProcessBlock(cfbV, 0, cfbOutV, 0);
  192. if (updateContext)
  193. {
  194. //
  195. // change over the input block.
  196. //
  197. Array.Copy(cfbV, blockSize, cfbV, 0, cfbV.Length - blockSize);
  198. Array.Copy(input, inOff, cfbV, cfbV.Length - blockSize, blockSize);
  199. }
  200. //
  201. // XOR the cfbV with the ciphertext producing the plaintext
  202. //
  203. for (int i = 0; i < blockSize; i++)
  204. {
  205. outBytes[outOff + i] = (byte)(cfbOutV[i] ^ input[inOff + i]);
  206. }
  207. return blockSize;
  208. }
  209. /**
  210. * reset the chaining vector back to the IV and reset the underlying
  211. * cipher.
  212. */
  213. public void Reset()
  214. {
  215. Array.Copy(IV, 0, cfbV, 0, IV.Length);
  216. cipher.Reset();
  217. }
  218. public int ProcessBlock(byte[] inBuf, int inOff, byte[] outBuf, int outOff)
  219. {
  220. return ProcessBlock(inBuf, inOff, outBuf, outOff, true);
  221. }
  222. }
  223. }