KMac.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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.Digests;
  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. public class KMac
  10. : IMac, IXof
  11. {
  12. private static readonly byte[] padding = new byte[100];
  13. private readonly CShakeDigest cshake;
  14. private readonly int bitLength;
  15. private readonly int outputLength;
  16. private byte[] key;
  17. private bool initialised;
  18. private bool firstOutput;
  19. public KMac(int bitLength, byte[] S)
  20. {
  21. this.cshake = new CShakeDigest(bitLength, Strings.ToAsciiByteArray("KMAC"), S);
  22. this.bitLength = bitLength;
  23. this.outputLength = bitLength * 2 / 8;
  24. }
  25. public string AlgorithmName
  26. {
  27. get { return "KMAC" + cshake.AlgorithmName.Substring(6); }
  28. }
  29. public void BlockUpdate(byte[] input, int inOff, int len)
  30. {
  31. if (!initialised)
  32. throw new InvalidOperationException("KMAC not initialized");
  33. cshake.BlockUpdate(input, inOff, len);
  34. }
  35. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  36. public void BlockUpdate(ReadOnlySpan<byte> input)
  37. {
  38. if (!initialised)
  39. throw new InvalidOperationException("KMAC not initialized");
  40. cshake.BlockUpdate(input);
  41. }
  42. #endif
  43. public int DoFinal(byte[] output, int outOff)
  44. {
  45. if (firstOutput)
  46. {
  47. if (!initialised)
  48. throw new InvalidOperationException("KMAC not initialized");
  49. byte[] encOut = XofUtilities.RightEncode(GetMacSize() * 8);
  50. cshake.BlockUpdate(encOut, 0, encOut.Length);
  51. }
  52. int rv = cshake.OutputFinal(output, outOff, GetMacSize());
  53. Reset();
  54. return rv;
  55. }
  56. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  57. public int DoFinal(Span<byte> output)
  58. {
  59. if (firstOutput)
  60. {
  61. if (!initialised)
  62. throw new InvalidOperationException("KMAC not initialized");
  63. Span<byte> lengthEncoding = stackalloc byte[9];
  64. int count = XofUtilities.RightEncode(GetMacSize() * 8, lengthEncoding);
  65. cshake.BlockUpdate(lengthEncoding[..count]);
  66. }
  67. int rv = cshake.OutputFinal(output[..GetMacSize()]);
  68. Reset();
  69. return rv;
  70. }
  71. #endif
  72. public int OutputFinal(byte[] output, int outOff, int outLen)
  73. {
  74. if (firstOutput)
  75. {
  76. if (!initialised)
  77. throw new InvalidOperationException("KMAC not initialized");
  78. byte[] encOut = XofUtilities.RightEncode(outLen * 8);
  79. cshake.BlockUpdate(encOut, 0, encOut.Length);
  80. }
  81. int rv = cshake.OutputFinal(output, outOff, outLen);
  82. Reset();
  83. return rv;
  84. }
  85. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  86. public int OutputFinal(Span<byte> output)
  87. {
  88. if (firstOutput)
  89. {
  90. if (!initialised)
  91. throw new InvalidOperationException("KMAC not initialized");
  92. Span<byte> lengthEncoding = stackalloc byte[9];
  93. int count = XofUtilities.RightEncode(output.Length * 8, lengthEncoding);
  94. cshake.BlockUpdate(lengthEncoding[..count]);
  95. }
  96. int rv = cshake.OutputFinal(output);
  97. Reset();
  98. return rv;
  99. }
  100. #endif
  101. public int Output(byte[] output, int outOff, int outLen)
  102. {
  103. if (firstOutput)
  104. {
  105. if (!initialised)
  106. throw new InvalidOperationException("KMAC not initialized");
  107. byte[] encOut = XofUtilities.RightEncode(0);
  108. cshake.BlockUpdate(encOut, 0, encOut.Length);
  109. firstOutput = false;
  110. }
  111. return cshake.Output(output, outOff, outLen);
  112. }
  113. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  114. public int Output(Span<byte> output)
  115. {
  116. if (firstOutput)
  117. {
  118. if (!initialised)
  119. throw new InvalidOperationException("KMAC not initialized");
  120. Span<byte> lengthEncoding = stackalloc byte[9];
  121. int count = XofUtilities.RightEncode(0, lengthEncoding);
  122. cshake.BlockUpdate(lengthEncoding[..count]);
  123. firstOutput = false;
  124. }
  125. return cshake.Output(output);
  126. }
  127. #endif
  128. public int GetByteLength()
  129. {
  130. return cshake.GetByteLength();
  131. }
  132. public int GetDigestSize()
  133. {
  134. return outputLength;
  135. }
  136. public int GetMacSize()
  137. {
  138. return outputLength;
  139. }
  140. public void Init(ICipherParameters parameters)
  141. {
  142. KeyParameter kParam = (KeyParameter)parameters;
  143. this.key = Arrays.Clone(kParam.GetKey());
  144. this.initialised = true;
  145. Reset();
  146. }
  147. public void Reset()
  148. {
  149. cshake.Reset();
  150. if (key != null)
  151. {
  152. if (bitLength == 128)
  153. {
  154. bytePad(key, 168);
  155. }
  156. else
  157. {
  158. bytePad(key, 136);
  159. }
  160. }
  161. firstOutput = true;
  162. }
  163. private void bytePad(byte[] X, int w)
  164. {
  165. byte[] bytes = XofUtilities.LeftEncode(w);
  166. BlockUpdate(bytes, 0, bytes.Length);
  167. byte[] encX = encode(X);
  168. BlockUpdate(encX, 0, encX.Length);
  169. int required = w - ((bytes.Length + encX.Length) % w);
  170. if (required > 0 && required != w)
  171. {
  172. while (required > padding.Length)
  173. {
  174. BlockUpdate(padding, 0, padding.Length);
  175. required -= padding.Length;
  176. }
  177. BlockUpdate(padding, 0, required);
  178. }
  179. }
  180. private static byte[] encode(byte[] X)
  181. {
  182. return Arrays.Concatenate(XofUtilities.LeftEncode(X.Length * 8), X);
  183. }
  184. public void Update(byte input)
  185. {
  186. if (!initialised)
  187. throw new InvalidOperationException("KMAC not initialized");
  188. cshake.Update(input);
  189. }
  190. }
  191. }
  192. #pragma warning restore
  193. #endif