ShakeDigest.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Diagnostics;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Digests
  7. {
  8. /// <summary>
  9. /// Implementation of SHAKE based on following KeccakNISTInterface.c from http://keccak.noekeon.org/
  10. /// </summary>
  11. /// <remarks>
  12. /// Following the naming conventions used in the C source code to enable easy review of the implementation.
  13. /// </remarks>
  14. public class ShakeDigest
  15. : KeccakDigest, IXof
  16. {
  17. private static int CheckBitLength(int bitLength)
  18. {
  19. switch (bitLength)
  20. {
  21. case 128:
  22. case 256:
  23. return bitLength;
  24. default:
  25. throw new ArgumentException(bitLength + " not supported for SHAKE", "bitLength");
  26. }
  27. }
  28. public ShakeDigest()
  29. : this(128)
  30. {
  31. }
  32. public ShakeDigest(int bitLength)
  33. : base(CheckBitLength(bitLength))
  34. {
  35. }
  36. public ShakeDigest(ShakeDigest source)
  37. : base(source)
  38. {
  39. }
  40. public override string AlgorithmName
  41. {
  42. get { return "SHAKE" + fixedOutputLength; }
  43. }
  44. public override int GetDigestSize()
  45. {
  46. return fixedOutputLength >> 2;
  47. }
  48. public override int DoFinal(byte[] output, int outOff)
  49. {
  50. return OutputFinal(output, outOff, GetDigestSize());
  51. }
  52. public virtual int OutputFinal(byte[] output, int outOff, int outLen)
  53. {
  54. int length = Output(output, outOff, outLen);
  55. Reset();
  56. return length;
  57. }
  58. public virtual int Output(byte[] output, int outOff, int outLen)
  59. {
  60. if (!squeezing)
  61. {
  62. AbsorbBits(0x0F, 4);
  63. }
  64. Squeeze(output, outOff, (long)outLen << 3);
  65. return outLen;
  66. }
  67. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  68. public override int DoFinal(Span<byte> output)
  69. {
  70. return OutputFinal(output[..GetDigestSize()]);
  71. }
  72. public virtual int OutputFinal(Span<byte> output)
  73. {
  74. int length = Output(output);
  75. Reset();
  76. return length;
  77. }
  78. public virtual int Output(Span<byte> output)
  79. {
  80. if (!squeezing)
  81. {
  82. AbsorbBits(0x0F, 4);
  83. }
  84. Squeeze(output);
  85. return output.Length;
  86. }
  87. #endif
  88. /*
  89. * TODO Possible API change to support partial-byte suffixes.
  90. */
  91. protected override int DoFinal(byte[] output, int outOff, byte partialByte, int partialBits)
  92. {
  93. return OutputFinal(output, outOff, GetDigestSize(), partialByte, partialBits);
  94. }
  95. /*
  96. * TODO Possible API change to support partial-byte suffixes.
  97. */
  98. protected virtual int OutputFinal(byte[] output, int outOff, int outLen, byte partialByte, int partialBits)
  99. {
  100. if (partialBits < 0 || partialBits > 7)
  101. throw new ArgumentException("must be in the range [0,7]", "partialBits");
  102. int finalInput = (partialByte & ((1 << partialBits) - 1)) | (0x0F << partialBits);
  103. Debug.Assert(finalInput >= 0);
  104. int finalBits = partialBits + 4;
  105. if (finalBits >= 8)
  106. {
  107. Absorb((byte)finalInput);
  108. finalBits -= 8;
  109. finalInput >>= 8;
  110. }
  111. if (finalBits > 0)
  112. {
  113. AbsorbBits(finalInput, finalBits);
  114. }
  115. Squeeze(output, outOff, (long)outLen << 3);
  116. Reset();
  117. return outLen;
  118. }
  119. public override IMemoable Copy()
  120. {
  121. return new ShakeDigest(this);
  122. }
  123. }
  124. }
  125. #pragma warning restore
  126. #endif