ISO9797Alg3Mac.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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.Engines;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Modes;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Paddings;
  7. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  8. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Macs
  9. {
  10. /**
  11. * DES based CBC Block Cipher MAC according to ISO9797, algorithm 3 (ANSI X9.19 Retail MAC)
  12. *
  13. * This could as well be derived from CBCBlockCipherMac, but then the property mac in the base
  14. * class must be changed to protected
  15. */
  16. public class ISO9797Alg3Mac
  17. : IMac
  18. {
  19. private byte[] mac;
  20. private byte[] buf;
  21. private int bufOff;
  22. private IBlockCipher cipher;
  23. private IBlockCipherPadding padding;
  24. private int macSize;
  25. private KeyParameter lastKey2;
  26. private KeyParameter lastKey3;
  27. /**
  28. * create a Retail-MAC based on a CBC block cipher. This will produce an
  29. * authentication code of the length of the block size of the cipher.
  30. *
  31. * @param cipher the cipher to be used as the basis of the MAC generation. This must
  32. * be DESEngine.
  33. */
  34. public ISO9797Alg3Mac(
  35. IBlockCipher cipher)
  36. : this(cipher, cipher.GetBlockSize() * 8, null)
  37. {
  38. }
  39. /**
  40. * create a Retail-MAC based on a CBC block cipher. This will produce an
  41. * authentication code of the length of the block size of the cipher.
  42. *
  43. * @param cipher the cipher to be used as the basis of the MAC generation.
  44. * @param padding the padding to be used to complete the last block.
  45. */
  46. public ISO9797Alg3Mac(
  47. IBlockCipher cipher,
  48. IBlockCipherPadding padding)
  49. : this(cipher, cipher.GetBlockSize() * 8, padding)
  50. {
  51. }
  52. /**
  53. * create a Retail-MAC based on a block cipher with the size of the
  54. * MAC been given in bits. This class uses single DES CBC mode as the basis for the
  55. * MAC generation.
  56. * <p>
  57. * Note: the size of the MAC must be at least 24 bits (FIPS Publication 81),
  58. * or 16 bits if being used as a data authenticator (FIPS Publication 113),
  59. * and in general should be less than the size of the block cipher as it reduces
  60. * the chance of an exhaustive attack (see Handbook of Applied Cryptography).
  61. * </p>
  62. * @param cipher the cipher to be used as the basis of the MAC generation.
  63. * @param macSizeInBits the size of the MAC in bits, must be a multiple of 8.
  64. */
  65. public ISO9797Alg3Mac(
  66. IBlockCipher cipher,
  67. int macSizeInBits)
  68. : this(cipher, macSizeInBits, null)
  69. {
  70. }
  71. /**
  72. * create a standard MAC based on a block cipher with the size of the
  73. * MAC been given in bits. This class uses single DES CBC mode as the basis for the
  74. * MAC generation. The final block is decrypted and then encrypted using the
  75. * middle and right part of the key.
  76. * <p>
  77. * Note: the size of the MAC must be at least 24 bits (FIPS Publication 81),
  78. * or 16 bits if being used as a data authenticator (FIPS Publication 113),
  79. * and in general should be less than the size of the block cipher as it reduces
  80. * the chance of an exhaustive attack (see Handbook of Applied Cryptography).
  81. * </p>
  82. * @param cipher the cipher to be used as the basis of the MAC generation.
  83. * @param macSizeInBits the size of the MAC in bits, must be a multiple of 8.
  84. * @param padding the padding to be used to complete the last block.
  85. */
  86. public ISO9797Alg3Mac(
  87. IBlockCipher cipher,
  88. int macSizeInBits,
  89. IBlockCipherPadding padding)
  90. {
  91. if ((macSizeInBits % 8) != 0)
  92. throw new ArgumentException("MAC size must be multiple of 8");
  93. if (!(cipher is DesEngine))
  94. throw new ArgumentException("cipher must be instance of DesEngine");
  95. this.cipher = new CbcBlockCipher(cipher);
  96. this.padding = padding;
  97. this.macSize = macSizeInBits / 8;
  98. mac = new byte[cipher.GetBlockSize()];
  99. buf = new byte[cipher.GetBlockSize()];
  100. bufOff = 0;
  101. }
  102. public string AlgorithmName
  103. {
  104. get { return "ISO9797Alg3"; }
  105. }
  106. public void Init(
  107. ICipherParameters parameters)
  108. {
  109. Reset();
  110. if (!(parameters is KeyParameter || parameters is ParametersWithIV))
  111. throw new ArgumentException("parameters must be an instance of KeyParameter or ParametersWithIV");
  112. // KeyParameter must contain a double or triple length DES key,
  113. // however the underlying cipher is a single DES. The middle and
  114. // right key are used only in the final step.
  115. KeyParameter kp;
  116. if (parameters is KeyParameter)
  117. {
  118. kp = (KeyParameter)parameters;
  119. }
  120. else
  121. {
  122. kp = (KeyParameter)((ParametersWithIV)parameters).Parameters;
  123. }
  124. KeyParameter key1;
  125. byte[] keyvalue = kp.GetKey();
  126. if (keyvalue.Length == 16)
  127. { // Double length DES key
  128. key1 = new KeyParameter(keyvalue, 0, 8);
  129. this.lastKey2 = new KeyParameter(keyvalue, 8, 8);
  130. this.lastKey3 = key1;
  131. }
  132. else if (keyvalue.Length == 24)
  133. { // Triple length DES key
  134. key1 = new KeyParameter(keyvalue, 0, 8);
  135. this.lastKey2 = new KeyParameter(keyvalue, 8, 8);
  136. this.lastKey3 = new KeyParameter(keyvalue, 16, 8);
  137. }
  138. else
  139. {
  140. throw new ArgumentException("Key must be either 112 or 168 bit long");
  141. }
  142. if (parameters is ParametersWithIV)
  143. {
  144. cipher.Init(true, new ParametersWithIV(key1, ((ParametersWithIV)parameters).GetIV()));
  145. }
  146. else
  147. {
  148. cipher.Init(true, key1);
  149. }
  150. }
  151. public int GetMacSize()
  152. {
  153. return macSize;
  154. }
  155. public void Update(
  156. byte input)
  157. {
  158. if (bufOff == buf.Length)
  159. {
  160. cipher.ProcessBlock(buf, 0, mac, 0);
  161. bufOff = 0;
  162. }
  163. buf[bufOff++] = input;
  164. }
  165. public void BlockUpdate(byte[] input, int inOff, int len)
  166. {
  167. if (len < 0)
  168. throw new ArgumentException("Can't have a negative input length!");
  169. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  170. BlockUpdate(input.AsSpan(inOff, len));
  171. #else
  172. int blockSize = cipher.GetBlockSize();
  173. int resultLen = 0;
  174. int gapLen = blockSize - bufOff;
  175. if (len > gapLen)
  176. {
  177. Array.Copy(input, inOff, buf, bufOff, gapLen);
  178. resultLen += cipher.ProcessBlock(buf, 0, mac, 0);
  179. bufOff = 0;
  180. len -= gapLen;
  181. inOff += gapLen;
  182. while (len > blockSize)
  183. {
  184. resultLen += cipher.ProcessBlock(input, inOff, mac, 0);
  185. len -= blockSize;
  186. inOff += blockSize;
  187. }
  188. }
  189. Array.Copy(input, inOff, buf, bufOff, len);
  190. bufOff += len;
  191. #endif
  192. }
  193. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  194. public void BlockUpdate(ReadOnlySpan<byte> input)
  195. {
  196. int blockSize = cipher.GetBlockSize();
  197. int resultLen = 0;
  198. int gapLen = blockSize - bufOff;
  199. if (input.Length > gapLen)
  200. {
  201. input[..gapLen].CopyTo(buf.AsSpan(bufOff));
  202. resultLen += cipher.ProcessBlock(buf, mac);
  203. bufOff = 0;
  204. input = input[gapLen..];
  205. while (input.Length > blockSize)
  206. {
  207. resultLen += cipher.ProcessBlock(input, mac);
  208. input = input[blockSize..];
  209. }
  210. }
  211. input.CopyTo(buf.AsSpan(bufOff));
  212. bufOff += input.Length;
  213. }
  214. #endif
  215. public int DoFinal(byte[] output, int outOff)
  216. {
  217. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  218. return DoFinal(output.AsSpan(outOff));
  219. #else
  220. int blockSize = cipher.GetBlockSize();
  221. if (padding == null)
  222. {
  223. // pad with zeroes
  224. while (bufOff < blockSize)
  225. {
  226. buf[bufOff++] = 0;
  227. }
  228. }
  229. else
  230. {
  231. if (bufOff == blockSize)
  232. {
  233. cipher.ProcessBlock(buf, 0, mac, 0);
  234. bufOff = 0;
  235. }
  236. padding.AddPadding(buf, bufOff);
  237. }
  238. cipher.ProcessBlock(buf, 0, mac, 0);
  239. // Added to code from base class
  240. DesEngine deseng = new DesEngine();
  241. deseng.Init(false, this.lastKey2);
  242. deseng.ProcessBlock(mac, 0, mac, 0);
  243. deseng.Init(true, this.lastKey3);
  244. deseng.ProcessBlock(mac, 0, mac, 0);
  245. // ****
  246. Array.Copy(mac, 0, output, outOff, macSize);
  247. Reset();
  248. return macSize;
  249. #endif
  250. }
  251. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  252. public int DoFinal(Span<byte> output)
  253. {
  254. int blockSize = cipher.GetBlockSize();
  255. if (padding == null)
  256. {
  257. // pad with zeroes
  258. while (bufOff < blockSize)
  259. {
  260. buf[bufOff++] = 0;
  261. }
  262. }
  263. else
  264. {
  265. if (bufOff == blockSize)
  266. {
  267. cipher.ProcessBlock(buf, mac);
  268. bufOff = 0;
  269. }
  270. padding.AddPadding(buf, bufOff);
  271. }
  272. cipher.ProcessBlock(buf, mac);
  273. // Added to code from base class
  274. DesEngine deseng = new DesEngine();
  275. deseng.Init(false, this.lastKey2);
  276. deseng.ProcessBlock(mac, mac);
  277. deseng.Init(true, this.lastKey3);
  278. deseng.ProcessBlock(mac, mac);
  279. // ****
  280. mac.AsSpan(0, macSize).CopyTo(output);
  281. Reset();
  282. return macSize;
  283. }
  284. #endif
  285. /**
  286. * Reset the mac generator.
  287. */
  288. public void Reset()
  289. {
  290. Array.Clear(buf, 0, buf.Length);
  291. bufOff = 0;
  292. }
  293. }
  294. }
  295. #pragma warning restore
  296. #endif