ShakeDigest.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 GetDigestSize()
  45. {
  46. return fixedOutputLength >> 2;
  47. }
  48. public override int DoFinal(byte[] output, int outOff)
  49. {
  50. return DoFinal(output, outOff, GetDigestSize());
  51. }
  52. public virtual int DoFinal(byte[] output, int outOff, int outLen)
  53. {
  54. int length = DoOutput(output, outOff, outLen);
  55. Reset();
  56. return length;
  57. }
  58. public virtual int DoOutput(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. /*
  68. * TODO Possible API change to support partial-byte suffixes.
  69. */
  70. protected override int DoFinal(byte[] output, int outOff, byte partialByte, int partialBits)
  71. {
  72. return DoFinal(output, outOff, GetDigestSize(), partialByte, partialBits);
  73. }
  74. /*
  75. * TODO Possible API change to support partial-byte suffixes.
  76. */
  77. protected virtual int DoFinal(byte[] output, int outOff, int outLen, byte partialByte, int partialBits)
  78. {
  79. if (partialBits < 0 || partialBits > 7)
  80. throw new ArgumentException("must be in the range [0,7]", "partialBits");
  81. int finalInput = (partialByte & ((1 << partialBits) - 1)) | (0x0F << partialBits);
  82. Debug.Assert(finalInput >= 0);
  83. int finalBits = partialBits + 4;
  84. if (finalBits >= 8)
  85. {
  86. Absorb((byte)finalInput);
  87. finalBits -= 8;
  88. finalInput >>= 8;
  89. }
  90. if (finalBits > 0)
  91. {
  92. AbsorbBits(finalInput, finalBits);
  93. }
  94. Squeeze(output, outOff, (long)outLen << 3);
  95. Reset();
  96. return outLen;
  97. }
  98. public override IMemoable Copy()
  99. {
  100. return new ShakeDigest(this);
  101. }
  102. }
  103. }
  104. #pragma warning restore
  105. #endif