CbcBlockCipherMac.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. namespace Org.BouncyCastle.Crypto.Macs
  6. {
  7. /**
  8. * standard CBC Block Cipher MAC - if no padding is specified the default of
  9. * pad of zeroes is used.
  10. */
  11. public class CbcBlockCipherMac
  12. : IMac
  13. {
  14. private byte[] buf;
  15. private int bufOff;
  16. private IBlockCipher cipher;
  17. private IBlockCipherPadding padding;
  18. private int macSize;
  19. /**
  20. * create a standard MAC based on a CBC block cipher. This will produce an
  21. * authentication code half the length of the block size of the cipher.
  22. *
  23. * @param cipher the cipher to be used as the basis of the MAC generation.
  24. */
  25. public CbcBlockCipherMac(
  26. IBlockCipher cipher)
  27. : this(cipher, (cipher.GetBlockSize() * 8) / 2, null)
  28. {
  29. }
  30. /**
  31. * create a standard MAC based on a CBC block cipher. This will produce an
  32. * authentication code half the length of the block size of the cipher.
  33. *
  34. * @param cipher the cipher to be used as the basis of the MAC generation.
  35. * @param padding the padding to be used to complete the last block.
  36. */
  37. public CbcBlockCipherMac(
  38. IBlockCipher cipher,
  39. IBlockCipherPadding padding)
  40. : this(cipher, (cipher.GetBlockSize() * 8) / 2, padding)
  41. {
  42. }
  43. /**
  44. * create a standard MAC based on a block cipher with the size of the
  45. * MAC been given in bits. This class uses CBC mode as the basis for the
  46. * MAC generation.
  47. * <p>
  48. * Note: the size of the MAC must be at least 24 bits (FIPS Publication 81),
  49. * or 16 bits if being used as a data authenticator (FIPS Publication 113),
  50. * and in general should be less than the size of the block cipher as it reduces
  51. * the chance of an exhaustive attack (see Handbook of Applied Cryptography).
  52. * </p>
  53. * @param cipher the cipher to be used as the basis of the MAC generation.
  54. * @param macSizeInBits the size of the MAC in bits, must be a multiple of 8.
  55. */
  56. public CbcBlockCipherMac(
  57. IBlockCipher cipher,
  58. int macSizeInBits)
  59. : this(cipher, macSizeInBits, null)
  60. {
  61. }
  62. /**
  63. * create a standard MAC based on a block cipher with the size of the
  64. * MAC been given in bits. This class uses CBC mode as the basis for the
  65. * MAC generation.
  66. * <p>
  67. * Note: the size of the MAC must be at least 24 bits (FIPS Publication 81),
  68. * or 16 bits if being used as a data authenticator (FIPS Publication 113),
  69. * and in general should be less than the size of the block cipher as it reduces
  70. * the chance of an exhaustive attack (see Handbook of Applied Cryptography).
  71. * </p>
  72. * @param cipher the cipher to be used as the basis of the MAC generation.
  73. * @param macSizeInBits the size of the MAC in bits, must be a multiple of 8.
  74. * @param padding the padding to be used to complete the last block.
  75. */
  76. public CbcBlockCipherMac(
  77. IBlockCipher cipher,
  78. int macSizeInBits,
  79. IBlockCipherPadding padding)
  80. {
  81. if ((macSizeInBits % 8) != 0)
  82. throw new ArgumentException("MAC size must be multiple of 8");
  83. this.cipher = new CbcBlockCipher(cipher);
  84. this.padding = padding;
  85. this.macSize = macSizeInBits / 8;
  86. buf = new byte[cipher.GetBlockSize()];
  87. bufOff = 0;
  88. }
  89. public string AlgorithmName
  90. {
  91. get { return cipher.AlgorithmName; }
  92. }
  93. public void Init(
  94. ICipherParameters parameters)
  95. {
  96. Reset();
  97. cipher.Init(true, parameters);
  98. }
  99. public int GetMacSize()
  100. {
  101. return macSize;
  102. }
  103. public void Update(
  104. byte input)
  105. {
  106. if (bufOff == buf.Length)
  107. {
  108. cipher.ProcessBlock(buf, 0, buf, 0);
  109. bufOff = 0;
  110. }
  111. buf[bufOff++] = input;
  112. }
  113. public void BlockUpdate(
  114. byte[] input,
  115. int inOff,
  116. int len)
  117. {
  118. if (len < 0)
  119. throw new ArgumentException("Can't have a negative input length!");
  120. int blockSize = cipher.GetBlockSize();
  121. int gapLen = blockSize - bufOff;
  122. if (len > gapLen)
  123. {
  124. Array.Copy(input, inOff, buf, bufOff, gapLen);
  125. cipher.ProcessBlock(buf, 0, buf, 0);
  126. bufOff = 0;
  127. len -= gapLen;
  128. inOff += gapLen;
  129. while (len > blockSize)
  130. {
  131. cipher.ProcessBlock(input, inOff, buf, 0);
  132. len -= blockSize;
  133. inOff += blockSize;
  134. }
  135. }
  136. Array.Copy(input, inOff, buf, bufOff, len);
  137. bufOff += len;
  138. }
  139. public int DoFinal(
  140. byte[] output,
  141. int outOff)
  142. {
  143. int blockSize = cipher.GetBlockSize();
  144. if (padding == null)
  145. {
  146. // pad with zeroes
  147. while (bufOff < blockSize)
  148. {
  149. buf[bufOff++] = 0;
  150. }
  151. }
  152. else
  153. {
  154. if (bufOff == blockSize)
  155. {
  156. cipher.ProcessBlock(buf, 0, buf, 0);
  157. bufOff = 0;
  158. }
  159. padding.AddPadding(buf, bufOff);
  160. }
  161. cipher.ProcessBlock(buf, 0, buf, 0);
  162. Array.Copy(buf, 0, output, outOff, macSize);
  163. Reset();
  164. return macSize;
  165. }
  166. /**
  167. * Reset the mac generator.
  168. */
  169. public void Reset()
  170. {
  171. // Clear the buffer.
  172. Array.Clear(buf, 0, buf.Length);
  173. bufOff = 0;
  174. // Reset the underlying cipher.
  175. cipher.Reset();
  176. }
  177. }
  178. }
  179. #endif