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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 readonly byte[] IV;
  14. private readonly byte[] cfbV;
  15. private readonly 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 ivParam)
  62. {
  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 => cipher.AlgorithmName + "/CFB" + (blockSize * 8);
  83. public bool IsPartialBlockOkay => true;
  84. /**
  85. * return the block size we are operating at.
  86. *
  87. * @return the block size we are operating at (in bytes).
  88. */
  89. public int GetBlockSize()
  90. {
  91. return blockSize;
  92. }
  93. /**
  94. * Process one block of input from the array in and write it to
  95. * the out array.
  96. *
  97. * @param in the array containing the input data.
  98. * @param inOff offset into the in array the data starts at.
  99. * @param out the array the output data will be copied into.
  100. * @param outOff the offset into the out array the output will start at.
  101. * @param updateContext update internal state after process.
  102. * @exception DataLengthException if there isn't enough data in in, or
  103. * space in out.
  104. * @exception InvalidOperationException if the cipher isn't initialised.
  105. * @return the number of bytes processed and produced.
  106. */
  107. public int ProcessBlock(
  108. byte[] input,
  109. int inOff,
  110. byte[] output,
  111. int outOff,
  112. bool updateContext)
  113. {
  114. return (encrypting)
  115. ? EncryptBlock(input, inOff, output, outOff, updateContext)
  116. : DecryptBlock(input, inOff, output, outOff, updateContext);
  117. }
  118. /**
  119. * Do the appropriate processing for CFB mode encryption.
  120. *
  121. * @param in the array containing the data to be encrypted.
  122. * @param inOff offset into the in array the data starts at.
  123. * @param out the array the encrypted data will be copied into.
  124. * @param outOff the offset into the out array the output will start at.
  125. * @exception DataLengthException if there isn't enough data in in, or
  126. * space in out.
  127. * @exception InvalidOperationException if the cipher isn't initialised.
  128. * @return the number of bytes processed and produced.
  129. */
  130. public int EncryptBlock(
  131. byte[] input,
  132. int inOff,
  133. byte[] outBytes,
  134. int outOff,
  135. bool updateContext = true)
  136. {
  137. if ((inOff + blockSize) > input.Length)
  138. {
  139. throw new DataLengthException("input buffer too short");
  140. }
  141. if ((outOff + blockSize) > outBytes.Length)
  142. {
  143. throw new DataLengthException("output buffer too short");
  144. }
  145. cipher.ProcessBlock(cfbV, 0, cfbOutV, 0);
  146. //
  147. // XOR the cfbV with the plaintext producing the ciphertext
  148. //
  149. for (int i = 0; i < blockSize; i++)
  150. {
  151. outBytes[outOff + i] = (byte)(cfbOutV[i] ^ input[inOff + i]);
  152. }
  153. if (updateContext)
  154. {
  155. //
  156. // change over the input block.
  157. //
  158. Array.Copy(cfbV, blockSize, cfbV, 0, cfbV.Length - blockSize);
  159. Array.Copy(outBytes, outOff, cfbV, cfbV.Length - blockSize, blockSize);
  160. }
  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. bool updateContext = true)
  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. if (updateContext)
  192. {
  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. //
  200. // XOR the cfbV with the ciphertext producing the plaintext
  201. //
  202. for (int i = 0; i < blockSize; i++)
  203. {
  204. outBytes[outOff + i] = (byte)(cfbOutV[i] ^ input[inOff + i]);
  205. }
  206. return blockSize;
  207. }
  208. /**
  209. * reset the chaining vector back to the IV and reset the underlying
  210. * cipher.
  211. */
  212. public void Reset()
  213. {
  214. Array.Copy(IV, 0, cfbV, 0, IV.Length);
  215. cipher.Reset();
  216. }
  217. public int ProcessBlock(byte[] inBuf, int inOff, byte[] outBuf, int outOff)
  218. {
  219. return ProcessBlock(inBuf, inOff, outBuf, outOff, true);
  220. }
  221. }
  222. }