DSTU7624Mac.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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.Parameters;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  7. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Macs
  8. {
  9. /**
  10. * implementation of DSTU 7624 MAC
  11. */
  12. public class Dstu7624Mac : IMac
  13. {
  14. private int macSize;
  15. private Dstu7624Engine engine;
  16. private int blockSize;
  17. private byte[] c, cTemp, kDelta;
  18. private byte[] buf;
  19. private int bufOff;
  20. public Dstu7624Mac(int blockSizeBits, int q)
  21. {
  22. engine = new Dstu7624Engine(blockSizeBits);
  23. blockSize = blockSizeBits / 8;
  24. macSize = q / 8;
  25. c = new byte[blockSize];
  26. cTemp = new byte[blockSize];
  27. kDelta = new byte[blockSize];
  28. buf = new byte[blockSize];
  29. }
  30. public void Init(ICipherParameters parameters)
  31. {
  32. if (parameters is KeyParameter)
  33. {
  34. engine.Init(true, (KeyParameter)parameters);
  35. engine.ProcessBlock(kDelta, 0, kDelta, 0);
  36. }
  37. else
  38. {
  39. throw new ArgumentException("invalid parameter passed to Dstu7624Mac init - "
  40. + Org.BouncyCastle.Utilities.Platform.GetTypeName(parameters));
  41. }
  42. }
  43. public string AlgorithmName
  44. {
  45. get { return "Dstu7624Mac"; }
  46. }
  47. public int GetMacSize()
  48. {
  49. return macSize;
  50. }
  51. public void Update(byte input)
  52. {
  53. if (bufOff == buf.Length)
  54. {
  55. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  56. ProcessBlock(buf);
  57. #else
  58. ProcessBlock(buf, 0);
  59. #endif
  60. bufOff = 0;
  61. }
  62. buf[bufOff++] = input;
  63. }
  64. public void BlockUpdate(byte[] input, int inOff, int len)
  65. {
  66. if (len < 0)
  67. throw new ArgumentException("Can't have a negative input length!");
  68. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  69. BlockUpdate(input.AsSpan(inOff, len));
  70. #else
  71. int blockSize = engine.GetBlockSize();
  72. int gapLen = blockSize - bufOff;
  73. if (len > gapLen)
  74. {
  75. Array.Copy(input, inOff, buf, bufOff, gapLen);
  76. ProcessBlock(buf, 0);
  77. bufOff = 0;
  78. len -= gapLen;
  79. inOff += gapLen;
  80. while (len > blockSize)
  81. {
  82. ProcessBlock(input, inOff);
  83. len -= blockSize;
  84. inOff += blockSize;
  85. }
  86. }
  87. Array.Copy(input, inOff, buf, bufOff, len);
  88. bufOff += len;
  89. #endif
  90. }
  91. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  92. public void BlockUpdate(ReadOnlySpan<byte> input)
  93. {
  94. int blockSize = engine.GetBlockSize();
  95. int gapLen = blockSize - bufOff;
  96. if (input.Length > gapLen)
  97. {
  98. input[..gapLen].CopyTo(buf.AsSpan(bufOff));
  99. ProcessBlock(buf);
  100. bufOff = 0;
  101. input = input[gapLen..];
  102. while (input.Length > blockSize)
  103. {
  104. ProcessBlock(input);
  105. input = input[blockSize..];
  106. }
  107. }
  108. input.CopyTo(buf.AsSpan(bufOff));
  109. bufOff += input.Length;
  110. }
  111. #endif
  112. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  113. private void ProcessBlock(ReadOnlySpan<byte> input)
  114. {
  115. Xor(c, input, cTemp);
  116. engine.ProcessBlock(cTemp, c);
  117. }
  118. private void Xor(ReadOnlySpan<byte> c, ReadOnlySpan<byte> input, Span<byte> xorResult)
  119. {
  120. for (int byteIndex = 0; byteIndex < blockSize; byteIndex++)
  121. {
  122. xorResult[byteIndex] = (byte)(c[byteIndex] ^ input[byteIndex]);
  123. }
  124. }
  125. #else
  126. private void ProcessBlock(byte[] input, int inOff)
  127. {
  128. Xor(c, 0, input, inOff, cTemp);
  129. engine.ProcessBlock(cTemp, 0, c, 0);
  130. }
  131. #endif
  132. private void Xor(byte[] c, int cOff, byte[] input, int inOff, byte[] xorResult)
  133. {
  134. for (int byteIndex = 0; byteIndex < blockSize; byteIndex++)
  135. {
  136. xorResult[byteIndex] = (byte)(c[byteIndex + cOff] ^ input[byteIndex + inOff]);
  137. }
  138. }
  139. public int DoFinal(byte[] output, int outOff)
  140. {
  141. if (bufOff % buf.Length != 0)
  142. throw new DataLengthException("Input must be a multiple of blocksize");
  143. Check.OutputLength(output, outOff, macSize, "output buffer too short");
  144. //Last block
  145. Xor(c, 0, buf, 0, cTemp);
  146. Xor(cTemp, 0, kDelta, 0, c);
  147. engine.ProcessBlock(c, 0, c, 0);
  148. Array.Copy(c, 0, output, outOff, macSize);
  149. return macSize;
  150. }
  151. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  152. public int DoFinal(Span<byte> output)
  153. {
  154. if (bufOff % buf.Length != 0)
  155. throw new DataLengthException("Input must be a multiple of blocksize");
  156. Check.OutputLength(output, macSize, "output buffer too short");
  157. //Last block
  158. Xor(c, 0, buf, 0, cTemp);
  159. Xor(cTemp, 0, kDelta, 0, c);
  160. engine.ProcessBlock(c, c);
  161. c.AsSpan(0, macSize).CopyTo(output);
  162. return macSize;
  163. }
  164. #endif
  165. public void Reset()
  166. {
  167. Arrays.Fill(c, (byte)0x00);
  168. Arrays.Fill(cTemp, (byte)0x00);
  169. Arrays.Fill(kDelta, (byte)0x00);
  170. Arrays.Fill(buf, (byte)0x00);
  171. engine.ProcessBlock(kDelta, 0, kDelta, 0);
  172. bufOff = 0;
  173. }
  174. }
  175. }
  176. #pragma warning restore
  177. #endif