DSTU7624Mac.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Engines;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  8. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Macs
  9. {
  10. /**
  11. * implementation of DSTU 7624 MAC
  12. */
  13. public class Dstu7624Mac : IMac
  14. {
  15. private int macSize;
  16. private Dstu7624Engine engine;
  17. private int blockSize;
  18. private byte[] c, cTemp, kDelta;
  19. private byte[] buf;
  20. private int bufOff;
  21. public Dstu7624Mac(int blockSizeBits, int q)
  22. {
  23. engine = new Dstu7624Engine(blockSizeBits);
  24. blockSize = blockSizeBits / 8;
  25. macSize = q / 8;
  26. c = new byte[blockSize];
  27. cTemp = new byte[blockSize];
  28. kDelta = new byte[blockSize];
  29. buf = new byte[blockSize];
  30. }
  31. public void Init(ICipherParameters parameters)
  32. {
  33. if (parameters is KeyParameter)
  34. {
  35. engine.Init(true, (KeyParameter)parameters);
  36. engine.ProcessBlock(kDelta, 0, kDelta, 0);
  37. }
  38. else
  39. {
  40. throw new ArgumentException("invalid parameter passed to Dstu7624Mac init - "
  41. + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(parameters));
  42. }
  43. }
  44. public string AlgorithmName
  45. {
  46. get { return "Dstu7624Mac"; }
  47. }
  48. public int GetMacSize()
  49. {
  50. return macSize;
  51. }
  52. public void Update(byte input)
  53. {
  54. if (bufOff == buf.Length)
  55. {
  56. processBlock(buf, 0);
  57. bufOff = 0;
  58. }
  59. buf[bufOff++] = input;
  60. }
  61. public void BlockUpdate(byte[] input, int inOff, int len)
  62. {
  63. if (len < 0)
  64. {
  65. throw new ArgumentException(
  66. "Can't have a negative input length!");
  67. }
  68. int blockSize = engine.GetBlockSize();
  69. int gapLen = blockSize - bufOff;
  70. if (len > gapLen)
  71. {
  72. Array.Copy(input, inOff, buf, bufOff, gapLen);
  73. processBlock(buf, 0);
  74. bufOff = 0;
  75. len -= gapLen;
  76. inOff += gapLen;
  77. while (len > blockSize)
  78. {
  79. processBlock(input, inOff);
  80. len -= blockSize;
  81. inOff += blockSize;
  82. }
  83. }
  84. Array.Copy(input, inOff, buf, bufOff, len);
  85. bufOff += len;
  86. }
  87. private void processBlock(byte[] input, int inOff)
  88. {
  89. Xor(c, 0, input, inOff, cTemp);
  90. engine.ProcessBlock(cTemp, 0, c, 0);
  91. }
  92. private void Xor(byte[] c, int cOff, byte[] input, int inOff, byte[] xorResult)
  93. {
  94. for (int byteIndex = 0; byteIndex < blockSize; byteIndex++)
  95. {
  96. xorResult[byteIndex] = (byte)(c[byteIndex + cOff] ^ input[byteIndex + inOff]);
  97. }
  98. }
  99. public int DoFinal(byte[] output, int outOff)
  100. {
  101. if (bufOff % buf.Length != 0)
  102. {
  103. throw new DataLengthException("Input must be a multiple of blocksize");
  104. }
  105. //Last block
  106. Xor(c, 0, buf, 0, cTemp);
  107. Xor(cTemp, 0, kDelta, 0, c);
  108. engine.ProcessBlock(c, 0, c, 0);
  109. if (macSize + outOff > output.Length)
  110. {
  111. throw new DataLengthException("Output buffer too short");
  112. }
  113. Array.Copy(c, 0, output, outOff, macSize);
  114. return macSize;
  115. }
  116. public void Reset()
  117. {
  118. Arrays.Fill(c, (byte)0x00);
  119. Arrays.Fill(cTemp, (byte)0x00);
  120. Arrays.Fill(kDelta, (byte)0x00);
  121. Arrays.Fill(buf, (byte)0x00);
  122. engine.Reset();
  123. engine.ProcessBlock(kDelta, 0, kDelta, 0);
  124. bufOff = 0;
  125. }
  126. }
  127. }
  128. #pragma warning restore
  129. #endif