TlsSuiteHmac.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  5. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Tls.Crypto.Impl
  6. {
  7. /// <summary>A generic TLS MAC implementation, acting as an HMAC based on some underlying Digest.</summary>
  8. public class TlsSuiteHmac
  9. : TlsSuiteMac
  10. {
  11. protected static int GetMacSize(TlsCryptoParameters cryptoParams, TlsMac mac)
  12. {
  13. int macSize = mac.MacLength;
  14. if (cryptoParams.SecurityParameters.IsTruncatedHmac)
  15. {
  16. macSize = System.Math.Min(macSize, 10);
  17. }
  18. return macSize;
  19. }
  20. protected readonly TlsCryptoParameters m_cryptoParams;
  21. protected readonly TlsHmac m_mac;
  22. protected readonly int m_digestBlockSize;
  23. protected readonly int m_digestOverhead;
  24. protected readonly int m_macSize;
  25. /// <summary>Generate a new instance of a TlsMac.</summary>
  26. /// <param name="cryptoParams">the TLS client context specific crypto parameters.</param>
  27. /// <param name="mac">The MAC to use.</param>
  28. public TlsSuiteHmac(TlsCryptoParameters cryptoParams, TlsHmac mac)
  29. {
  30. this.m_cryptoParams = cryptoParams;
  31. this.m_mac = mac;
  32. this.m_macSize = GetMacSize(cryptoParams, mac);
  33. this.m_digestBlockSize = mac.InternalBlockSize;
  34. // TODO This should check the actual algorithm, not assume based on the digest size
  35. if (TlsImplUtilities.IsSsl(cryptoParams) && mac.MacLength == 20)
  36. {
  37. /*
  38. * NOTE: For the SSL 3.0 MAC with SHA-1, the secret + input pad is not block-aligned.
  39. */
  40. this.m_digestOverhead = 4;
  41. }
  42. else
  43. {
  44. this.m_digestOverhead = m_digestBlockSize / 8;
  45. }
  46. }
  47. public virtual int Size
  48. {
  49. get { return m_macSize; }
  50. }
  51. public virtual byte[] CalculateMac(long seqNo, short type, byte[] msg, int msgOff, int msgLen)
  52. {
  53. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  54. return CalculateMac(seqNo, type, msg.AsSpan(msgOff, msgLen));
  55. #else
  56. ProtocolVersion serverVersion = m_cryptoParams.ServerVersion;
  57. bool isSsl = serverVersion.IsSsl;
  58. byte[] macHeader = new byte[isSsl ? 11 : 13];
  59. TlsUtilities.WriteUint64(seqNo, macHeader, 0);
  60. TlsUtilities.WriteUint8(type, macHeader, 8);
  61. if (!isSsl)
  62. {
  63. TlsUtilities.WriteVersion(serverVersion, macHeader, 9);
  64. }
  65. TlsUtilities.WriteUint16(msgLen, macHeader, macHeader.Length - 2);
  66. m_mac.Update(macHeader, 0, macHeader.Length);
  67. m_mac.Update(msg, msgOff, msgLen);
  68. return Truncate(m_mac.CalculateMac());
  69. #endif
  70. }
  71. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  72. public virtual byte[] CalculateMac(long seqNo, short type, ReadOnlySpan<byte> message)
  73. {
  74. ProtocolVersion serverVersion = m_cryptoParams.ServerVersion;
  75. bool isSsl = serverVersion.IsSsl;
  76. byte[] macHeader = new byte[isSsl ? 11 : 13];
  77. TlsUtilities.WriteUint64(seqNo, macHeader, 0);
  78. TlsUtilities.WriteUint8(type, macHeader, 8);
  79. if (!isSsl)
  80. {
  81. TlsUtilities.WriteVersion(serverVersion, macHeader, 9);
  82. }
  83. TlsUtilities.WriteUint16(message.Length, macHeader, macHeader.Length - 2);
  84. m_mac.Update(macHeader);
  85. m_mac.Update(message);
  86. return Truncate(m_mac.CalculateMac());
  87. }
  88. #endif
  89. public virtual byte[] CalculateMacConstantTime(long seqNo, short type, byte[] msg, int msgOff, int msgLen,
  90. int fullLength, byte[] dummyData)
  91. {
  92. /*
  93. * Actual MAC only calculated on 'length' bytes...
  94. */
  95. byte[] result = CalculateMac(seqNo, type, msg, msgOff, msgLen);
  96. /*
  97. * ...but ensure a constant number of complete digest blocks are processed (as many as would
  98. * be needed for 'fullLength' bytes of input).
  99. */
  100. int headerLength = TlsImplUtilities.IsSsl(m_cryptoParams) ? 11 : 13;
  101. // How many extra full blocks do we need to calculate?
  102. int extra = GetDigestBlockCount(headerLength + fullLength) - GetDigestBlockCount(headerLength + msgLen);
  103. while (--extra >= 0)
  104. {
  105. m_mac.Update(dummyData, 0, m_digestBlockSize);
  106. }
  107. // One more byte in case the implementation is "lazy" about processing blocks
  108. m_mac.Update(dummyData, 0, 1);
  109. m_mac.Reset();
  110. return result;
  111. }
  112. protected virtual int GetDigestBlockCount(int inputLength)
  113. {
  114. // NOTE: The input pad for HMAC is always a full digest block
  115. // NOTE: This calculation assumes a minimum of 1 pad byte
  116. return (inputLength + m_digestOverhead) / m_digestBlockSize;
  117. }
  118. protected virtual byte[] Truncate(byte[] bs)
  119. {
  120. if (bs.Length <= m_macSize)
  121. return bs;
  122. return Arrays.CopyOf(bs, m_macSize);
  123. }
  124. }
  125. }
  126. #pragma warning restore
  127. #endif