ISO9797Alg3Mac.cs 7.1 KB

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