FastTlsAeadCipherImpl.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.PlatformSupport.Memory;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Modes;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Tls;
  9. using BestHTTP.SecureProtocol.Org.BouncyCastle.Tls.Crypto.Impl;
  10. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  11. namespace BestHTTP.Connections.TLS.Crypto.Impl
  12. {
  13. public sealed class NoCopyKeyParameter
  14. : ICipherParameters
  15. {
  16. private readonly byte[] key;
  17. public NoCopyKeyParameter(byte[] key)
  18. :this(key, 0, key.Length)
  19. {
  20. }
  21. public NoCopyKeyParameter(
  22. byte[] key,
  23. int keyOff,
  24. int keyLen)
  25. {
  26. if (key == null)
  27. throw new ArgumentNullException("key");
  28. if (keyOff < 0 || keyOff > key.Length)
  29. throw new ArgumentOutOfRangeException("keyOff");
  30. if (keyLen < 0 || keyLen > (key.Length - keyOff))
  31. throw new ArgumentOutOfRangeException("keyLen");
  32. this.key = new byte[keyLen];
  33. Array.Copy(key, keyOff, this.key, 0, keyLen);
  34. }
  35. public byte[] GetKey()
  36. {
  37. return key;// (byte[])key.Clone();
  38. }
  39. }
  40. public sealed class FastAeadParameters
  41. : ICipherParameters
  42. {
  43. private readonly byte[] associatedText;
  44. private readonly byte[] nonce;
  45. private readonly NoCopyKeyParameter key;
  46. private readonly int macSize;
  47. /**
  48. * Base constructor.
  49. *
  50. * @param key key to be used by underlying cipher
  51. * @param macSize macSize in bits
  52. * @param nonce nonce to be used
  53. */
  54. public FastAeadParameters(NoCopyKeyParameter key, int macSize, byte[] nonce)
  55. : this(key, macSize, nonce, null)
  56. {
  57. }
  58. /**
  59. * Base constructor.
  60. *
  61. * @param key key to be used by underlying cipher
  62. * @param macSize macSize in bits
  63. * @param nonce nonce to be used
  64. * @param associatedText associated text, if any
  65. */
  66. public FastAeadParameters(
  67. NoCopyKeyParameter key,
  68. int macSize,
  69. byte[] nonce,
  70. byte[] associatedText)
  71. {
  72. this.key = key;
  73. this.nonce = nonce;
  74. this.macSize = macSize;
  75. this.associatedText = associatedText;
  76. }
  77. public NoCopyKeyParameter Key
  78. {
  79. get { return key; }
  80. }
  81. public int MacSize
  82. {
  83. get { return macSize; }
  84. }
  85. public byte[] GetAssociatedText()
  86. {
  87. return associatedText;
  88. }
  89. public byte[] GetNonce()
  90. {
  91. return nonce;
  92. }
  93. }
  94. public sealed class FastParametersWithIV
  95. : ICipherParameters
  96. {
  97. private readonly ICipherParameters parameters;
  98. private readonly byte[] iv;
  99. public FastParametersWithIV(ICipherParameters parameters,
  100. byte[] iv)
  101. : this(parameters, iv, 0, iv.Length)
  102. {
  103. }
  104. public FastParametersWithIV(ICipherParameters parameters,
  105. byte[] iv, int ivOff, int ivLen)
  106. {
  107. // NOTE: 'parameters' may be null to imply key re-use
  108. if (iv == null)
  109. throw new ArgumentNullException("iv");
  110. this.parameters = parameters;
  111. this.iv = Arrays.CopyOfRange(iv, ivOff, ivOff + ivLen);
  112. }
  113. public byte[] GetIV()
  114. {
  115. return iv; // (byte[])iv.Clone();
  116. }
  117. public ICipherParameters Parameters
  118. {
  119. get { return parameters; }
  120. }
  121. }
  122. internal sealed class FastTlsAeadCipherImpl
  123. : TlsAeadCipherImpl
  124. {
  125. private readonly bool m_isEncrypting;
  126. private readonly IAeadBlockCipher m_cipher;
  127. private NoCopyKeyParameter key;
  128. internal FastTlsAeadCipherImpl(IAeadBlockCipher cipher, bool isEncrypting)
  129. {
  130. this.m_cipher = cipher;
  131. this.m_isEncrypting = isEncrypting;
  132. }
  133. public void SetKey(byte[] key, int keyOff, int keyLen)
  134. {
  135. this.key = new NoCopyKeyParameter(key, keyOff, keyLen);
  136. }
  137. public void Init(byte[] nonce, int macSize, byte[] additionalData)
  138. {
  139. m_cipher.Init(m_isEncrypting, new FastAeadParameters(key, macSize * 8, nonce, additionalData));
  140. }
  141. public int GetOutputSize(int inputLength)
  142. {
  143. return m_cipher.GetOutputSize(inputLength);
  144. }
  145. public int DoFinal(byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset)
  146. {
  147. int len = m_cipher.ProcessBytes(input, inputOffset, inputLength, output, outputOffset);
  148. try
  149. {
  150. len += m_cipher.DoFinal(output, outputOffset + len);
  151. }
  152. catch (InvalidCipherTextException e)
  153. {
  154. throw new TlsFatalAlert(AlertDescription.bad_record_mac, e);
  155. }
  156. return len;
  157. }
  158. }
  159. }
  160. #pragma warning restore
  161. #endif