CfbBlockCipherMac.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using Org.BouncyCastle.Crypto.Modes;
  4. using Org.BouncyCastle.Crypto.Paddings;
  5. using Org.BouncyCastle.Crypto.Parameters;
  6. namespace Org.BouncyCastle.Crypto.Macs
  7. {
  8. /**
  9. * implements a Cipher-FeedBack (CFB) mode on top of a simple cipher.
  10. */
  11. class MacCFBBlockCipher
  12. : IBlockCipher
  13. {
  14. private byte[] IV;
  15. private byte[] cfbV;
  16. private byte[] cfbOutV;
  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 MacCFBBlockCipher(
  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. * Initialise the cipher and, possibly, the initialisation vector (IV).
  38. * If an IV isn't passed as part of the parameter, the IV will be all zeros.
  39. * An IV which is too short is handled in FIPS compliant fashion.
  40. *
  41. * @param param the key and other data required by the cipher.
  42. * @exception ArgumentException if the parameters argument is
  43. * inappropriate.
  44. */
  45. public void Init(
  46. bool forEncryption,
  47. ICipherParameters parameters)
  48. {
  49. if (parameters is ParametersWithIV)
  50. {
  51. ParametersWithIV ivParam = (ParametersWithIV)parameters;
  52. byte[] iv = ivParam.GetIV();
  53. if (iv.Length < IV.Length)
  54. {
  55. Array.Copy(iv, 0, IV, IV.Length - iv.Length, iv.Length);
  56. }
  57. else
  58. {
  59. Array.Copy(iv, 0, IV, 0, IV.Length);
  60. }
  61. parameters = ivParam.Parameters;
  62. }
  63. Reset();
  64. cipher.Init(true, parameters);
  65. }
  66. /**
  67. * return the algorithm name and mode.
  68. *
  69. * @return the name of the underlying algorithm followed by "/CFB"
  70. * and the block size in bits.
  71. */
  72. public string AlgorithmName
  73. {
  74. get { return cipher.AlgorithmName + "/CFB" + (blockSize * 8); }
  75. }
  76. public bool IsPartialBlockOkay
  77. {
  78. get { return true; }
  79. }
  80. /**
  81. * return the block size we are operating at.
  82. *
  83. * @return the block size we are operating at (in bytes).
  84. */
  85. public int GetBlockSize()
  86. {
  87. return blockSize;
  88. }
  89. /**
  90. * Process one block of input from the array in and write it to
  91. * the out array.
  92. *
  93. * @param in the array containing the input data.
  94. * @param inOff offset into the in array the data starts at.
  95. * @param out the array the output data will be copied into.
  96. * @param outOff the offset into the out array the output will start at.
  97. * @exception DataLengthException if there isn't enough data in in, or
  98. * space in out.
  99. * @exception InvalidOperationException if the cipher isn't initialised.
  100. * @return the number of bytes processed and produced.
  101. */
  102. public int ProcessBlock(
  103. byte[] input,
  104. int inOff,
  105. byte[] outBytes,
  106. int outOff)
  107. {
  108. if ((inOff + blockSize) > input.Length)
  109. throw new DataLengthException("input buffer too short");
  110. if ((outOff + blockSize) > outBytes.Length)
  111. throw new DataLengthException("output buffer too short");
  112. cipher.ProcessBlock(cfbV, 0, cfbOutV, 0);
  113. //
  114. // XOR the cfbV with the plaintext producing the cipher text
  115. //
  116. for (int i = 0; i < blockSize; i++)
  117. {
  118. outBytes[outOff + i] = (byte)(cfbOutV[i] ^ input[inOff + i]);
  119. }
  120. //
  121. // change over the input block.
  122. //
  123. Array.Copy(cfbV, blockSize, cfbV, 0, cfbV.Length - blockSize);
  124. Array.Copy(outBytes, outOff, cfbV, cfbV.Length - blockSize, blockSize);
  125. return blockSize;
  126. }
  127. /**
  128. * reset the chaining vector back to the IV and reset the underlying
  129. * cipher.
  130. */
  131. public void Reset()
  132. {
  133. IV.CopyTo(cfbV, 0);
  134. cipher.Reset();
  135. }
  136. public void GetMacBlock(
  137. byte[] mac)
  138. {
  139. cipher.ProcessBlock(cfbV, 0, mac, 0);
  140. }
  141. }
  142. public class CfbBlockCipherMac
  143. : IMac
  144. {
  145. private byte[] mac;
  146. private byte[] Buffer;
  147. private int bufOff;
  148. private MacCFBBlockCipher cipher;
  149. private IBlockCipherPadding padding;
  150. private int macSize;
  151. /**
  152. * create a standard MAC based on a CFB block cipher. This will produce an
  153. * authentication code half the length of the block size of the cipher, with
  154. * the CFB mode set to 8 bits.
  155. *
  156. * @param cipher the cipher to be used as the basis of the MAC generation.
  157. */
  158. public CfbBlockCipherMac(
  159. IBlockCipher cipher)
  160. : this(cipher, 8, (cipher.GetBlockSize() * 8) / 2, null)
  161. {
  162. }
  163. /**
  164. * create a standard MAC based on a CFB block cipher. This will produce an
  165. * authentication code half the length of the block size of the cipher, with
  166. * the CFB mode set to 8 bits.
  167. *
  168. * @param cipher the cipher to be used as the basis of the MAC generation.
  169. * @param padding the padding to be used.
  170. */
  171. public CfbBlockCipherMac(
  172. IBlockCipher cipher,
  173. IBlockCipherPadding padding)
  174. : this(cipher, 8, (cipher.GetBlockSize() * 8) / 2, padding)
  175. {
  176. }
  177. /**
  178. * create a standard MAC based on a block cipher with the size of the
  179. * MAC been given in bits. This class uses CFB mode as the basis for the
  180. * MAC generation.
  181. * <p>
  182. * Note: the size of the MAC must be at least 24 bits (FIPS Publication 81),
  183. * or 16 bits if being used as a data authenticator (FIPS Publication 113),
  184. * and in general should be less than the size of the block cipher as it reduces
  185. * the chance of an exhaustive attack (see Handbook of Applied Cryptography).
  186. * </p>
  187. * @param cipher the cipher to be used as the basis of the MAC generation.
  188. * @param cfbBitSize the size of an output block produced by the CFB mode.
  189. * @param macSizeInBits the size of the MAC in bits, must be a multiple of 8.
  190. */
  191. public CfbBlockCipherMac(
  192. IBlockCipher cipher,
  193. int cfbBitSize,
  194. int macSizeInBits)
  195. : this(cipher, cfbBitSize, macSizeInBits, null)
  196. {
  197. }
  198. /**
  199. * create a standard MAC based on a block cipher with the size of the
  200. * MAC been given in bits. This class uses CFB mode as the basis for the
  201. * MAC generation.
  202. * <p>
  203. * Note: the size of the MAC must be at least 24 bits (FIPS Publication 81),
  204. * or 16 bits if being used as a data authenticator (FIPS Publication 113),
  205. * and in general should be less than the size of the block cipher as it reduces
  206. * the chance of an exhaustive attack (see Handbook of Applied Cryptography).
  207. * </p>
  208. * @param cipher the cipher to be used as the basis of the MAC generation.
  209. * @param cfbBitSize the size of an output block produced by the CFB mode.
  210. * @param macSizeInBits the size of the MAC in bits, must be a multiple of 8.
  211. * @param padding a padding to be used.
  212. */
  213. public CfbBlockCipherMac(
  214. IBlockCipher cipher,
  215. int cfbBitSize,
  216. int macSizeInBits,
  217. IBlockCipherPadding padding)
  218. {
  219. if ((macSizeInBits % 8) != 0)
  220. throw new ArgumentException("MAC size must be multiple of 8");
  221. mac = new byte[cipher.GetBlockSize()];
  222. this.cipher = new MacCFBBlockCipher(cipher, cfbBitSize);
  223. this.padding = padding;
  224. this.macSize = macSizeInBits / 8;
  225. Buffer = new byte[this.cipher.GetBlockSize()];
  226. bufOff = 0;
  227. }
  228. public string AlgorithmName
  229. {
  230. get { return cipher.AlgorithmName; }
  231. }
  232. public void Init(
  233. ICipherParameters parameters)
  234. {
  235. Reset();
  236. cipher.Init(true, parameters);
  237. }
  238. public int GetMacSize()
  239. {
  240. return macSize;
  241. }
  242. public void Update(
  243. byte input)
  244. {
  245. if (bufOff == Buffer.Length)
  246. {
  247. cipher.ProcessBlock(Buffer, 0, mac, 0);
  248. bufOff = 0;
  249. }
  250. Buffer[bufOff++] = input;
  251. }
  252. public void BlockUpdate(
  253. byte[] input,
  254. int inOff,
  255. int len)
  256. {
  257. if (len < 0)
  258. throw new ArgumentException("Can't have a negative input length!");
  259. int blockSize = cipher.GetBlockSize();
  260. int resultLen = 0;
  261. int gapLen = blockSize - bufOff;
  262. if (len > gapLen)
  263. {
  264. Array.Copy(input, inOff, Buffer, bufOff, gapLen);
  265. resultLen += cipher.ProcessBlock(Buffer, 0, mac, 0);
  266. bufOff = 0;
  267. len -= gapLen;
  268. inOff += gapLen;
  269. while (len > blockSize)
  270. {
  271. resultLen += cipher.ProcessBlock(input, inOff, mac, 0);
  272. len -= blockSize;
  273. inOff += blockSize;
  274. }
  275. }
  276. Array.Copy(input, inOff, Buffer, bufOff, len);
  277. bufOff += len;
  278. }
  279. public int DoFinal(
  280. byte[] output,
  281. int outOff)
  282. {
  283. int blockSize = cipher.GetBlockSize();
  284. // pad with zeroes
  285. if (this.padding == null)
  286. {
  287. while (bufOff < blockSize)
  288. {
  289. Buffer[bufOff++] = 0;
  290. }
  291. }
  292. else
  293. {
  294. padding.AddPadding(Buffer, bufOff);
  295. }
  296. cipher.ProcessBlock(Buffer, 0, mac, 0);
  297. cipher.GetMacBlock(mac);
  298. Array.Copy(mac, 0, output, outOff, macSize);
  299. Reset();
  300. return macSize;
  301. }
  302. /**
  303. * Reset the mac generator.
  304. */
  305. public void Reset()
  306. {
  307. // Clear the buffer.
  308. Array.Clear(Buffer, 0, Buffer.Length);
  309. bufOff = 0;
  310. // Reset the underlying cipher.
  311. cipher.Reset();
  312. }
  313. }
  314. }
  315. #endif