ParallelHash.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. namespace BestHTTP.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. private void compress()
  112. {
  113. compress(buffer, 0, bufOff);
  114. bufOff = 0;
  115. }
  116. private void compress(byte[] buf, int offSet, int len)
  117. {
  118. compressor.BlockUpdate(buf, offSet, len);
  119. compressor.DoFinal(compressorBuffer, 0, compressorBuffer.Length);
  120. cshake.BlockUpdate(compressorBuffer, 0, compressorBuffer.Length);
  121. nCount++;
  122. }
  123. private void wrapUp(int outputSize)
  124. {
  125. if (bufOff != 0)
  126. {
  127. compress();
  128. }
  129. byte[] nOut = XofUtilities.RightEncode(nCount);
  130. byte[] encOut = XofUtilities.RightEncode(outputSize * 8);
  131. cshake.BlockUpdate(nOut, 0, nOut.Length);
  132. cshake.BlockUpdate(encOut, 0, encOut.Length);
  133. firstOutput = false;
  134. }
  135. public virtual int DoFinal(byte[] outBuf, int outOff)
  136. {
  137. if (firstOutput)
  138. {
  139. wrapUp(outputLength);
  140. }
  141. int rv = cshake.DoFinal(outBuf, outOff, GetDigestSize());
  142. Reset();
  143. return rv;
  144. }
  145. public virtual int DoFinal(byte[] outBuf, int outOff, int outLen)
  146. {
  147. if (firstOutput)
  148. {
  149. wrapUp(outputLength);
  150. }
  151. int rv = cshake.DoFinal(outBuf, outOff, outLen);
  152. Reset();
  153. return rv;
  154. }
  155. public virtual int DoOutput(byte[] outBuf, int outOff, int outLen)
  156. {
  157. if (firstOutput)
  158. {
  159. wrapUp(0);
  160. }
  161. return cshake.DoOutput(outBuf, outOff, outLen);
  162. }
  163. public virtual void Reset()
  164. {
  165. cshake.Reset();
  166. Arrays.Clear(buffer);
  167. byte[] hdr = XofUtilities.LeftEncode(B);
  168. cshake.BlockUpdate(hdr, 0, hdr.Length);
  169. nCount = 0;
  170. bufOff = 0;
  171. firstOutput = true;
  172. }
  173. }
  174. }
  175. #pragma warning restore
  176. #endif