BcSsl3Hmac.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  7. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Tls.Crypto.Impl.BC
  8. {
  9. /// <summary>HMAC implementation based on original internet draft for HMAC (RFC 2104).</summary>
  10. /// <remarks>
  11. /// The difference is that padding is concatenated versus XORed with the key, e.g:
  12. /// <code>H(K + opad, H(K + ipad, text))</code>
  13. /// </remarks>
  14. internal class BcSsl3Hmac
  15. : TlsHmac
  16. {
  17. private const byte IPAD_BYTE = (byte)0x36;
  18. private const byte OPAD_BYTE = (byte)0x5C;
  19. private static readonly byte[] IPAD = GenPad(IPAD_BYTE, 48);
  20. private static readonly byte[] OPAD = GenPad(OPAD_BYTE, 48);
  21. private readonly IDigest m_digest;
  22. private readonly int m_padLength;
  23. private byte[] m_secret;
  24. /// <summary>Base constructor for one of the standard digest algorithms for which the byteLength is known.
  25. /// </summary>
  26. /// <remarks>
  27. /// Behaviour is undefined for digests other than MD5 or SHA1.
  28. /// </remarks>
  29. /// <param name="digest">the digest.</param>
  30. internal BcSsl3Hmac(IDigest digest)
  31. {
  32. this.m_digest = digest;
  33. if (digest.GetDigestSize() == 20)
  34. {
  35. this.m_padLength = 40;
  36. }
  37. else
  38. {
  39. this.m_padLength = 48;
  40. }
  41. }
  42. public virtual void SetKey(byte[] key, int keyOff, int keyLen)
  43. {
  44. this.m_secret = TlsUtilities.CopyOfRangeExact(key, keyOff, keyOff + keyLen);
  45. Reset();
  46. }
  47. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  48. public void SetKey(ReadOnlySpan<byte> key)
  49. {
  50. this.m_secret = key.ToArray();
  51. Reset();
  52. }
  53. #endif
  54. public virtual void Update(byte[] input, int inOff, int len)
  55. {
  56. m_digest.BlockUpdate(input, inOff, len);
  57. }
  58. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  59. public void Update(ReadOnlySpan<byte> input)
  60. {
  61. m_digest.BlockUpdate(input);
  62. }
  63. #endif
  64. public virtual byte[] CalculateMac()
  65. {
  66. byte[] result = new byte[m_digest.GetDigestSize()];
  67. DoFinal(result, 0);
  68. return result;
  69. }
  70. public virtual void CalculateMac(byte[] output, int outOff)
  71. {
  72. DoFinal(output, outOff);
  73. }
  74. public virtual int InternalBlockSize
  75. {
  76. get { return m_digest.GetByteLength(); }
  77. }
  78. public virtual int MacLength
  79. {
  80. get { return m_digest.GetDigestSize(); }
  81. }
  82. /**
  83. * Reset the mac generator.
  84. */
  85. public virtual void Reset()
  86. {
  87. m_digest.Reset();
  88. m_digest.BlockUpdate(m_secret, 0, m_secret.Length);
  89. m_digest.BlockUpdate(IPAD, 0, m_padLength);
  90. }
  91. private void DoFinal(byte[] output, int outOff)
  92. {
  93. byte[] tmp = new byte[m_digest.GetDigestSize()];
  94. m_digest.DoFinal(tmp, 0);
  95. m_digest.BlockUpdate(m_secret, 0, m_secret.Length);
  96. m_digest.BlockUpdate(OPAD, 0, m_padLength);
  97. m_digest.BlockUpdate(tmp, 0, tmp.Length);
  98. m_digest.DoFinal(output, outOff);
  99. Reset();
  100. }
  101. private static byte[] GenPad(byte b, int count)
  102. {
  103. byte[] padding = new byte[count];
  104. Arrays.Fill(padding, b);
  105. return padding;
  106. }
  107. }
  108. }
  109. #pragma warning restore
  110. #endif