Ssl3Mac.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  7. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Tls
  8. {
  9. /**
  10. * HMAC implementation based on original internet draft for HMAC (RFC 2104)
  11. *
  12. * The difference is that padding is concatentated versus XORed with the key
  13. *
  14. * H(K + opad, H(K + ipad, text))
  15. */
  16. public class Ssl3Mac
  17. : IMac
  18. {
  19. private const byte IPAD_BYTE = 0x36;
  20. private const byte OPAD_BYTE = 0x5C;
  21. internal static readonly byte[] IPAD = GenPad(IPAD_BYTE, 48);
  22. internal static readonly byte[] OPAD = GenPad(OPAD_BYTE, 48);
  23. private readonly IDigest digest;
  24. private readonly int padLength;
  25. private byte[] secret;
  26. /**
  27. * Base constructor for one of the standard digest algorithms that the byteLength of
  28. * the algorithm is know for. Behaviour is undefined for digests other than MD5 or SHA1.
  29. *
  30. * @param digest the digest.
  31. */
  32. public Ssl3Mac(IDigest digest)
  33. {
  34. this.digest = digest;
  35. if (digest.GetDigestSize() == 20)
  36. {
  37. this.padLength = 40;
  38. }
  39. else
  40. {
  41. this.padLength = 48;
  42. }
  43. }
  44. public virtual string AlgorithmName
  45. {
  46. get { return digest.AlgorithmName + "/SSL3MAC"; }
  47. }
  48. public virtual void Init(ICipherParameters parameters)
  49. {
  50. secret = Arrays.Clone(((KeyParameter)parameters).GetKey());
  51. Reset();
  52. }
  53. public virtual int GetMacSize()
  54. {
  55. return digest.GetDigestSize();
  56. }
  57. public virtual void Update(byte input)
  58. {
  59. digest.Update(input);
  60. }
  61. public virtual void BlockUpdate(byte[] input, int inOff, int len)
  62. {
  63. digest.BlockUpdate(input, inOff, len);
  64. }
  65. public virtual int DoFinal(byte[] output, int outOff)
  66. {
  67. byte[] tmp = new byte[digest.GetDigestSize()];
  68. digest.DoFinal(tmp, 0);
  69. digest.BlockUpdate(secret, 0, secret.Length);
  70. digest.BlockUpdate(OPAD, 0, padLength);
  71. digest.BlockUpdate(tmp, 0, tmp.Length);
  72. int len = digest.DoFinal(output, outOff);
  73. Reset();
  74. return len;
  75. }
  76. /**
  77. * Reset the mac generator.
  78. */
  79. public virtual void Reset()
  80. {
  81. digest.Reset();
  82. digest.BlockUpdate(secret, 0, secret.Length);
  83. digest.BlockUpdate(IPAD, 0, padLength);
  84. }
  85. private static byte[] GenPad(byte b, int count)
  86. {
  87. byte[] padding = new byte[count];
  88. Arrays.Fill(padding, b);
  89. return padding;
  90. }
  91. }
  92. }
  93. #pragma warning restore
  94. #endif