CfbBlockCipherMac.cs 11 KB

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