ParallelHash.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  5. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Digests
  6. {
  7. /// <summary>
  8. /// ParallelHash - a hash designed to support the efficient hashing of very long strings, by taking advantage,
  9. /// of the parallelism available in modern processors with an optional XOF mode.
  10. /// <para>
  11. /// From NIST Special Publication 800-185 - SHA-3 Derived Functions:cSHAKE, KMAC, TupleHash and ParallelHash
  12. /// </para>
  13. /// </summary>
  14. public class ParallelHash
  15. : IXof, IDigest
  16. {
  17. private static readonly byte[] N_PARALLEL_HASH = Strings.ToByteArray("ParallelHash");
  18. private readonly CShakeDigest cshake;
  19. private readonly CShakeDigest compressor;
  20. private readonly int bitLength;
  21. private readonly int outputLength;
  22. private readonly int B;
  23. private readonly byte[] buffer;
  24. private readonly byte[] compressorBuffer;
  25. private bool firstOutput;
  26. private int nCount;
  27. private int bufOff;
  28. /**
  29. * Base constructor.
  30. *
  31. * @param bitLength bit length of the underlying SHAKE function, 128 or 256.
  32. * @param S the customization string - available for local use.
  33. * @param B the blocksize (in bytes) for hashing.
  34. */
  35. public ParallelHash(int bitLength, byte[] S, int B)
  36. : this(bitLength, S, B, bitLength * 2)
  37. {
  38. }
  39. public ParallelHash(int bitLength, byte[] S, int B, int outputSize)
  40. {
  41. this.cshake = new CShakeDigest(bitLength, N_PARALLEL_HASH, S);
  42. this.compressor = new CShakeDigest(bitLength, new byte[0], new byte[0]);
  43. this.bitLength = bitLength;
  44. this.B = B;
  45. this.outputLength = (outputSize + 7) / 8;
  46. this.buffer = new byte[B];
  47. this.compressorBuffer = new byte[bitLength * 2 / 8];
  48. Reset();
  49. }
  50. public ParallelHash(ParallelHash source)
  51. {
  52. this.cshake = new CShakeDigest(source.cshake);
  53. this.compressor = new CShakeDigest(source.compressor);
  54. this.bitLength = source.bitLength;
  55. this.B = source.B;
  56. this.outputLength = source.outputLength;
  57. this.buffer = Arrays.Clone(source.buffer);
  58. this.compressorBuffer = Arrays.Clone(source.compressorBuffer);
  59. }
  60. public virtual string AlgorithmName
  61. {
  62. get { return "ParallelHash" + cshake.AlgorithmName.Substring(6); }
  63. }
  64. public virtual int GetByteLength()
  65. {
  66. return cshake.GetByteLength();
  67. }
  68. public virtual int GetDigestSize()
  69. {
  70. return outputLength;
  71. }
  72. public virtual void Update(byte b)
  73. {
  74. buffer[bufOff++] = b;
  75. if (bufOff == buffer.Length)
  76. {
  77. Compress();
  78. }
  79. }
  80. public virtual void BlockUpdate(byte[] inBuf, int inOff, int len)
  81. {
  82. len = System.Math.Max(0, len);
  83. //
  84. // fill the current word
  85. //
  86. int i = 0;
  87. if (bufOff != 0)
  88. {
  89. while (i < len && bufOff != buffer.Length)
  90. {
  91. buffer[bufOff++] = inBuf[inOff + i++];
  92. }
  93. if (bufOff == buffer.Length)
  94. {
  95. Compress();
  96. }
  97. }
  98. if (i < len)
  99. {
  100. while (len - i >= B)
  101. {
  102. Compress(inBuf, inOff + i, B);
  103. i += B;
  104. }
  105. }
  106. while (i < len)
  107. {
  108. Update(inBuf[inOff + i++]);
  109. }
  110. }
  111. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  112. public virtual void BlockUpdate(ReadOnlySpan<byte> input)
  113. {
  114. //
  115. // fill the current word
  116. //
  117. int i = 0;
  118. if (bufOff != 0)
  119. {
  120. while (i < input.Length && bufOff != buffer.Length)
  121. {
  122. buffer[bufOff++] = input[i++];
  123. }
  124. if (bufOff == buffer.Length)
  125. {
  126. Compress();
  127. }
  128. }
  129. if (i < input.Length)
  130. {
  131. while (input.Length - i >= B)
  132. {
  133. Compress(input, i, B);
  134. i += B;
  135. }
  136. }
  137. while (i < input.Length)
  138. {
  139. Update(input[i++]);
  140. }
  141. }
  142. #endif
  143. private void Compress()
  144. {
  145. Compress(buffer, 0, bufOff);
  146. bufOff = 0;
  147. }
  148. private void Compress(byte[] buf, int offSet, int len)
  149. {
  150. compressor.BlockUpdate(buf, offSet, len);
  151. compressor.OutputFinal(compressorBuffer, 0, compressorBuffer.Length);
  152. cshake.BlockUpdate(compressorBuffer, 0, compressorBuffer.Length);
  153. nCount++;
  154. }
  155. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  156. private void Compress(ReadOnlySpan<byte> input, int pos, int len)
  157. {
  158. compressor.BlockUpdate(input.Slice(pos, len));
  159. compressor.OutputFinal(compressorBuffer, 0, compressorBuffer.Length);
  160. cshake.BlockUpdate(compressorBuffer, 0, compressorBuffer.Length);
  161. nCount++;
  162. }
  163. #endif
  164. private void WrapUp(int outputSize)
  165. {
  166. if (bufOff != 0)
  167. {
  168. Compress();
  169. }
  170. byte[] nOut = XofUtilities.RightEncode(nCount);
  171. byte[] encOut = XofUtilities.RightEncode(outputSize * 8);
  172. cshake.BlockUpdate(nOut, 0, nOut.Length);
  173. cshake.BlockUpdate(encOut, 0, encOut.Length);
  174. firstOutput = false;
  175. }
  176. public virtual int DoFinal(byte[] outBuf, int outOff)
  177. {
  178. if (firstOutput)
  179. {
  180. WrapUp(outputLength);
  181. }
  182. int rv = cshake.DoFinal(outBuf, outOff);
  183. Reset();
  184. return rv;
  185. }
  186. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  187. public virtual int DoFinal(Span<byte> output)
  188. {
  189. if (firstOutput)
  190. {
  191. WrapUp(outputLength);
  192. }
  193. int rv = cshake.DoFinal(output);
  194. Reset();
  195. return rv;
  196. }
  197. #endif
  198. public virtual int OutputFinal(byte[] outBuf, int outOff, int outLen)
  199. {
  200. if (firstOutput)
  201. {
  202. WrapUp(outputLength);
  203. }
  204. int rv = cshake.OutputFinal(outBuf, outOff, outLen);
  205. Reset();
  206. return rv;
  207. }
  208. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  209. public virtual int OutputFinal(Span<byte> output)
  210. {
  211. if (firstOutput)
  212. {
  213. WrapUp(outputLength);
  214. }
  215. int rv = cshake.OutputFinal(output);
  216. Reset();
  217. return rv;
  218. }
  219. #endif
  220. public virtual int Output(byte[] outBuf, int outOff, int outLen)
  221. {
  222. if (firstOutput)
  223. {
  224. WrapUp(0);
  225. }
  226. return cshake.Output(outBuf, outOff, outLen);
  227. }
  228. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  229. public virtual int Output(Span<byte> output)
  230. {
  231. if (firstOutput)
  232. {
  233. WrapUp(0);
  234. }
  235. return cshake.Output(output);
  236. }
  237. #endif
  238. public virtual void Reset()
  239. {
  240. cshake.Reset();
  241. Arrays.Clear(buffer);
  242. byte[] hdr = XofUtilities.LeftEncode(B);
  243. cshake.BlockUpdate(hdr, 0, hdr.Length);
  244. nCount = 0;
  245. bufOff = 0;
  246. firstOutput = true;
  247. }
  248. }
  249. }
  250. #pragma warning restore
  251. #endif