FastTlsAeadCipherImpl.cs 6.0 KB

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