CfbBlockCipherMac.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Modes;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Paddings;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  7. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Macs
  8. {
  9. /**
  10. * implements a Cipher-FeedBack (CFB) mode on top of a simple cipher.
  11. */
  12. internal class MacCfbBlockCipher
  13. : IBlockCipherMode
  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(bool forEncryption, ICipherParameters parameters)
  47. {
  48. if (parameters is ParametersWithIV ivParam)
  49. {
  50. byte[] iv = ivParam.GetIV();
  51. if (iv.Length < IV.Length)
  52. {
  53. Array.Copy(iv, 0, IV, IV.Length - iv.Length, iv.Length);
  54. }
  55. else
  56. {
  57. Array.Copy(iv, 0, IV, 0, IV.Length);
  58. }
  59. parameters = ivParam.Parameters;
  60. }
  61. Reset();
  62. cipher.Init(true, parameters);
  63. }
  64. /**
  65. * return the algorithm name and mode.
  66. *
  67. * @return the name of the underlying algorithm followed by "/CFB"
  68. * and the block size in bits.
  69. */
  70. public string AlgorithmName
  71. {
  72. get { return cipher.AlgorithmName + "/CFB" + (blockSize * 8); }
  73. }
  74. public IBlockCipher UnderlyingCipher => cipher;
  75. public bool IsPartialBlockOkay
  76. {
  77. get { return true; }
  78. }
  79. /**
  80. * return the block size we are operating at.
  81. *
  82. * @return the block size we are operating at (in bytes).
  83. */
  84. public int GetBlockSize()
  85. {
  86. return blockSize;
  87. }
  88. public int ProcessBlock(byte[] input, int inOff, byte[] outBytes, int outOff)
  89. {
  90. Check.DataLength(input, inOff, blockSize, "input buffer too short");
  91. Check.OutputLength(outBytes, outOff, blockSize, "output buffer too short");
  92. cipher.ProcessBlock(cfbV, 0, cfbOutV, 0);
  93. //
  94. // XOR the cfbV with the plaintext producing the cipher text
  95. //
  96. for (int i = 0; i < blockSize; i++)
  97. {
  98. outBytes[outOff + i] = (byte)(cfbOutV[i] ^ input[inOff + i]);
  99. }
  100. //
  101. // change over the input block.
  102. //
  103. Array.Copy(cfbV, blockSize, cfbV, 0, cfbV.Length - blockSize);
  104. Array.Copy(outBytes, outOff, cfbV, cfbV.Length - blockSize, blockSize);
  105. return blockSize;
  106. }
  107. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  108. public int ProcessBlock(ReadOnlySpan<byte> input, Span<byte> output)
  109. {
  110. Check.DataLength(input, blockSize, "input buffer too short");
  111. Check.OutputLength(output, blockSize, "output buffer too short");
  112. cipher.ProcessBlock(cfbV, cfbOutV);
  113. //
  114. // XOR the cfbV with the plaintext producing the cipher text
  115. //
  116. for (int i = 0; i < blockSize; i++)
  117. {
  118. output[i] = (byte)(cfbOutV[i] ^ input[i]);
  119. }
  120. //
  121. // change over the input block.
  122. //
  123. Array.Copy(cfbV, blockSize, cfbV, 0, cfbV.Length - blockSize);
  124. output[..blockSize].CopyTo(cfbV.AsSpan(cfbV.Length - blockSize));
  125. return blockSize;
  126. }
  127. #endif
  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. }
  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(ICipherParameters parameters)
  233. {
  234. Reset();
  235. cipher.Init(true, parameters);
  236. }
  237. public int GetMacSize()
  238. {
  239. return macSize;
  240. }
  241. public void Update(byte input)
  242. {
  243. if (bufOff == Buffer.Length)
  244. {
  245. cipher.ProcessBlock(Buffer, 0, mac, 0);
  246. bufOff = 0;
  247. }
  248. Buffer[bufOff++] = input;
  249. }
  250. public void BlockUpdate(byte[] input, int inOff, int len)
  251. {
  252. if (len < 0)
  253. throw new ArgumentException("Can't have a negative input length!");
  254. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  255. BlockUpdate(input.AsSpan(inOff, len));
  256. #else
  257. int blockSize = cipher.GetBlockSize();
  258. int resultLen = 0;
  259. int gapLen = blockSize - bufOff;
  260. if (len > gapLen)
  261. {
  262. Array.Copy(input, inOff, Buffer, bufOff, gapLen);
  263. resultLen += cipher.ProcessBlock(Buffer, 0, mac, 0);
  264. bufOff = 0;
  265. len -= gapLen;
  266. inOff += gapLen;
  267. while (len > blockSize)
  268. {
  269. resultLen += cipher.ProcessBlock(input, inOff, mac, 0);
  270. len -= blockSize;
  271. inOff += blockSize;
  272. }
  273. }
  274. Array.Copy(input, inOff, Buffer, bufOff, len);
  275. bufOff += len;
  276. #endif
  277. }
  278. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  279. public void BlockUpdate(ReadOnlySpan<byte> input)
  280. {
  281. int blockSize = cipher.GetBlockSize();
  282. int resultLen = 0;
  283. int gapLen = blockSize - bufOff;
  284. if (input.Length > gapLen)
  285. {
  286. input[..gapLen].CopyTo(Buffer.AsSpan(bufOff));
  287. resultLen += cipher.ProcessBlock(Buffer, mac);
  288. bufOff = 0;
  289. input = input[gapLen..];
  290. while (input.Length > blockSize)
  291. {
  292. resultLen += cipher.ProcessBlock(input, mac);
  293. input = input[blockSize..];
  294. }
  295. }
  296. input.CopyTo(Buffer.AsSpan(bufOff));
  297. bufOff += input.Length;
  298. }
  299. #endif
  300. public int DoFinal(byte[] output, int outOff)
  301. {
  302. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  303. return DoFinal(output.AsSpan(outOff));
  304. #else
  305. int blockSize = cipher.GetBlockSize();
  306. // pad with zeroes
  307. if (this.padding == null)
  308. {
  309. while (bufOff < blockSize)
  310. {
  311. Buffer[bufOff++] = 0;
  312. }
  313. }
  314. else
  315. {
  316. padding.AddPadding(Buffer, bufOff);
  317. }
  318. cipher.ProcessBlock(Buffer, 0, mac, 0);
  319. cipher.GetMacBlock(mac);
  320. Array.Copy(mac, 0, output, outOff, macSize);
  321. Reset();
  322. return macSize;
  323. #endif
  324. }
  325. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  326. public int DoFinal(Span<byte> output)
  327. {
  328. int blockSize = cipher.GetBlockSize();
  329. // pad with zeroes
  330. if (this.padding == null)
  331. {
  332. while (bufOff < blockSize)
  333. {
  334. Buffer[bufOff++] = 0;
  335. }
  336. }
  337. else
  338. {
  339. padding.AddPadding(Buffer, bufOff);
  340. }
  341. cipher.ProcessBlock(Buffer, 0, mac, 0);
  342. cipher.GetMacBlock(mac);
  343. mac.AsSpan(0, macSize).CopyTo(output);
  344. Reset();
  345. return macSize;
  346. }
  347. #endif
  348. /**
  349. * Reset the mac generator.
  350. */
  351. public void Reset()
  352. {
  353. // Clear the buffer.
  354. Array.Clear(Buffer, 0, Buffer.Length);
  355. bufOff = 0;
  356. // Reset the underlying cipher.
  357. cipher.Reset();
  358. }
  359. }
  360. }
  361. #pragma warning restore
  362. #endif