ShakeDigest.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Diagnostics;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace BestHTTP.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 DoFinal(byte[] output, int outOff)
  45. {
  46. return DoFinal(output, outOff, GetDigestSize());
  47. }
  48. public virtual int DoFinal(byte[] output, int outOff, int outLen)
  49. {
  50. DoOutput(output, outOff, outLen);
  51. Reset();
  52. return outLen;
  53. }
  54. public virtual int DoOutput(byte[] output, int outOff, int outLen)
  55. {
  56. if (!squeezing)
  57. {
  58. AbsorbBits(0x0F, 4);
  59. }
  60. Squeeze(output, outOff, (long)outLen << 3);
  61. return outLen;
  62. }
  63. /*
  64. * TODO Possible API change to support partial-byte suffixes.
  65. */
  66. protected override int DoFinal(byte[] output, int outOff, byte partialByte, int partialBits)
  67. {
  68. return DoFinal(output, outOff, GetDigestSize(), partialByte, partialBits);
  69. }
  70. /*
  71. * TODO Possible API change to support partial-byte suffixes.
  72. */
  73. protected virtual int DoFinal(byte[] output, int outOff, int outLen, byte partialByte, int partialBits)
  74. {
  75. if (partialBits < 0 || partialBits > 7)
  76. throw new ArgumentException("must be in the range [0,7]", "partialBits");
  77. int finalInput = (partialByte & ((1 << partialBits) - 1)) | (0x0F << partialBits);
  78. Debug.Assert(finalInput >= 0);
  79. int finalBits = partialBits + 4;
  80. if (finalBits >= 8)
  81. {
  82. Absorb(new byte[]{ (byte)finalInput }, 0, 1);
  83. finalBits -= 8;
  84. finalInput >>= 8;
  85. }
  86. if (finalBits > 0)
  87. {
  88. AbsorbBits(finalInput, finalBits);
  89. }
  90. Squeeze(output, outOff, (long)outLen << 3);
  91. Reset();
  92. return outLen;
  93. }
  94. public override IMemoable Copy()
  95. {
  96. return new ShakeDigest(this);
  97. }
  98. }
  99. }
  100. #pragma warning restore
  101. #endif