HMac.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  8. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Macs
  9. {
  10. /**
  11. * HMAC implementation based on RFC2104
  12. *
  13. * H(K XOR opad, H(K XOR ipad, text))
  14. */
  15. public class HMac
  16. : IMac
  17. {
  18. private const byte IPAD = (byte)0x36;
  19. private const byte OPAD = (byte)0x5C;
  20. private readonly IDigest digest;
  21. private readonly int digestSize;
  22. private readonly int blockLength;
  23. private IMemoable ipadState;
  24. private IMemoable opadState;
  25. private readonly byte[] inputPad;
  26. private readonly byte[] outputBuf;
  27. public HMac(IDigest digest)
  28. {
  29. this.digest = digest;
  30. this.digestSize = digest.GetDigestSize();
  31. this.blockLength = digest.GetByteLength();
  32. this.inputPad = new byte[blockLength];
  33. this.outputBuf = new byte[blockLength + digestSize];
  34. }
  35. public virtual string AlgorithmName
  36. {
  37. get { return digest.AlgorithmName + "/HMAC"; }
  38. }
  39. public virtual IDigest GetUnderlyingDigest()
  40. {
  41. return digest;
  42. }
  43. public virtual void Init(ICipherParameters parameters)
  44. {
  45. digest.Reset();
  46. byte[] key = ((KeyParameter)parameters).GetKey();
  47. int keyLength = key.Length;
  48. if (keyLength > blockLength)
  49. {
  50. digest.BlockUpdate(key, 0, keyLength);
  51. digest.DoFinal(inputPad, 0);
  52. keyLength = digestSize;
  53. }
  54. else
  55. {
  56. Array.Copy(key, 0, inputPad, 0, keyLength);
  57. }
  58. Array.Clear(inputPad, keyLength, blockLength - keyLength);
  59. Array.Copy(inputPad, 0, outputBuf, 0, blockLength);
  60. XorPad(inputPad, blockLength, IPAD);
  61. XorPad(outputBuf, blockLength, OPAD);
  62. if (digest is IMemoable)
  63. {
  64. opadState = ((IMemoable)digest).Copy();
  65. ((IDigest)opadState).BlockUpdate(outputBuf, 0, blockLength);
  66. }
  67. digest.BlockUpdate(inputPad, 0, inputPad.Length);
  68. if (digest is IMemoable)
  69. {
  70. ipadState = ((IMemoable)digest).Copy();
  71. }
  72. }
  73. public virtual int GetMacSize()
  74. {
  75. return digestSize;
  76. }
  77. public virtual void Update(byte input)
  78. {
  79. digest.Update(input);
  80. }
  81. public virtual void BlockUpdate(byte[] input, int inOff, int len)
  82. {
  83. digest.BlockUpdate(input, inOff, len);
  84. }
  85. public virtual int DoFinal(byte[] output, int outOff)
  86. {
  87. digest.DoFinal(outputBuf, blockLength);
  88. if (opadState != null)
  89. {
  90. ((IMemoable)digest).Reset(opadState);
  91. digest.BlockUpdate(outputBuf, blockLength, digest.GetDigestSize());
  92. }
  93. else
  94. {
  95. digest.BlockUpdate(outputBuf, 0, outputBuf.Length);
  96. }
  97. int len = digest.DoFinal(output, outOff);
  98. Array.Clear(outputBuf, blockLength, digestSize);
  99. if (ipadState != null)
  100. {
  101. ((IMemoable)digest).Reset(ipadState);
  102. }
  103. else
  104. {
  105. digest.BlockUpdate(inputPad, 0, inputPad.Length);
  106. }
  107. return len;
  108. }
  109. /**
  110. * Reset the mac generator.
  111. */
  112. public virtual void Reset()
  113. {
  114. // Reset underlying digest
  115. digest.Reset();
  116. // Initialise the digest
  117. digest.BlockUpdate(inputPad, 0, inputPad.Length);
  118. }
  119. private static void XorPad(byte[] pad, int len, byte n)
  120. {
  121. for (int i = 0; i < len; ++i)
  122. {
  123. pad[i] ^= n;
  124. }
  125. }
  126. }
  127. }
  128. #pragma warning restore
  129. #endif