TlsSuiteHmac.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  5. namespace BestHTTP.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. ProtocolVersion serverVersion = m_cryptoParams.ServerVersion;
  54. bool isSsl = serverVersion.IsSsl;
  55. byte[] macHeader = new byte[isSsl ? 11 : 13];
  56. TlsUtilities.WriteUint64(seqNo, macHeader, 0);
  57. TlsUtilities.WriteUint8(type, macHeader, 8);
  58. if (!isSsl)
  59. {
  60. TlsUtilities.WriteVersion(serverVersion, macHeader, 9);
  61. }
  62. TlsUtilities.WriteUint16(msgLen, macHeader, macHeader.Length - 2);
  63. m_mac.Update(macHeader, 0, macHeader.Length);
  64. m_mac.Update(msg, msgOff, msgLen);
  65. return Truncate(m_mac.CalculateMac());
  66. }
  67. public virtual byte[] CalculateMacConstantTime(long seqNo, short type, byte[] msg, int msgOff, int msgLen,
  68. int fullLength, byte[] dummyData)
  69. {
  70. /*
  71. * Actual MAC only calculated on 'length' bytes...
  72. */
  73. byte[] result = CalculateMac(seqNo, type, msg, msgOff, msgLen);
  74. /*
  75. * ...but ensure a constant number of complete digest blocks are processed (as many as would
  76. * be needed for 'fullLength' bytes of input).
  77. */
  78. int headerLength = TlsImplUtilities.IsSsl(m_cryptoParams) ? 11 : 13;
  79. // How many extra full blocks do we need to calculate?
  80. int extra = GetDigestBlockCount(headerLength + fullLength) - GetDigestBlockCount(headerLength + msgLen);
  81. while (--extra >= 0)
  82. {
  83. m_mac.Update(dummyData, 0, m_digestBlockSize);
  84. }
  85. // One more byte in case the implementation is "lazy" about processing blocks
  86. m_mac.Update(dummyData, 0, 1);
  87. m_mac.Reset();
  88. return result;
  89. }
  90. protected virtual int GetDigestBlockCount(int inputLength)
  91. {
  92. // NOTE: The input pad for HMAC is always a full digest block
  93. // NOTE: This calculation assumes a minimum of 1 pad byte
  94. return (inputLength + m_digestOverhead) / m_digestBlockSize;
  95. }
  96. protected virtual byte[] Truncate(byte[] bs)
  97. {
  98. if (bs.Length <= m_macSize)
  99. return bs;
  100. return Arrays.CopyOf(bs, m_macSize);
  101. }
  102. }
  103. }
  104. #pragma warning restore
  105. #endif