BufferedAeadCipher.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Modes;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  6. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto
  7. {
  8. /**
  9. * The AEAD ciphers already handle buffering internally, so this class
  10. * just takes care of implementing IBufferedCipher methods.
  11. */
  12. public class BufferedAeadCipher
  13. : BufferedCipherBase
  14. {
  15. private readonly IAeadCipher cipher;
  16. public BufferedAeadCipher(IAeadCipher cipher)
  17. {
  18. if (cipher == null)
  19. throw new ArgumentNullException("cipher");
  20. this.cipher = cipher;
  21. }
  22. public override string AlgorithmName
  23. {
  24. get { return cipher.AlgorithmName; }
  25. }
  26. /**
  27. * initialise the cipher.
  28. *
  29. * @param forEncryption if true the cipher is initialised for
  30. * encryption, if false for decryption.
  31. * @param param the key and other data required by the cipher.
  32. * @exception ArgumentException if the parameters argument is
  33. * inappropriate.
  34. */
  35. public override void Init(
  36. bool forEncryption,
  37. ICipherParameters parameters)
  38. {
  39. if (parameters is ParametersWithRandom)
  40. {
  41. parameters = ((ParametersWithRandom)parameters).Parameters;
  42. }
  43. cipher.Init(forEncryption, parameters);
  44. }
  45. /**
  46. * return the blocksize for the underlying cipher.
  47. *
  48. * @return the blocksize for the underlying cipher.
  49. */
  50. public override int GetBlockSize()
  51. {
  52. return 0;
  53. }
  54. /**
  55. * return the size of the output buffer required for an update
  56. * an input of len bytes.
  57. *
  58. * @param len the length of the input.
  59. * @return the space required to accommodate a call to update
  60. * with len bytes of input.
  61. */
  62. public override int GetUpdateOutputSize(
  63. int length)
  64. {
  65. return cipher.GetUpdateOutputSize(length);
  66. }
  67. /**
  68. * return the size of the output buffer required for an update plus a
  69. * doFinal with an input of len bytes.
  70. *
  71. * @param len the length of the input.
  72. * @return the space required to accommodate a call to update and doFinal
  73. * with len bytes of input.
  74. */
  75. public override int GetOutputSize(
  76. int length)
  77. {
  78. return cipher.GetOutputSize(length);
  79. }
  80. /**
  81. * process a single byte, producing an output block if necessary.
  82. *
  83. * @param in the input byte.
  84. * @param out the space for any output that might be produced.
  85. * @param outOff the offset from which the output will be copied.
  86. * @return the number of output bytes copied to out.
  87. * @exception DataLengthException if there isn't enough space in out.
  88. * @exception InvalidOperationException if the cipher isn't initialised.
  89. */
  90. public override int ProcessByte(byte input, byte[] output, int outOff)
  91. {
  92. return cipher.ProcessByte(input, output, outOff);
  93. }
  94. public override byte[] ProcessByte(
  95. byte input)
  96. {
  97. int outLength = GetUpdateOutputSize(1);
  98. byte[] outBytes = outLength > 0 ? new byte[outLength] : null;
  99. int pos = ProcessByte(input, outBytes, 0);
  100. if (outLength > 0 && pos < outLength)
  101. {
  102. byte[] tmp = new byte[pos];
  103. Array.Copy(outBytes, 0, tmp, 0, pos);
  104. outBytes = tmp;
  105. }
  106. return outBytes;
  107. }
  108. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  109. public override int ProcessByte(byte input, Span<byte> output)
  110. {
  111. return cipher.ProcessByte(input, output);
  112. }
  113. #endif
  114. public override byte[] ProcessBytes(
  115. byte[] input,
  116. int inOff,
  117. int length)
  118. {
  119. if (input == null)
  120. throw new ArgumentNullException("input");
  121. if (length < 1)
  122. return null;
  123. int outLength = GetUpdateOutputSize(length);
  124. byte[] outBytes = outLength > 0 ? new byte[outLength] : null;
  125. int pos = ProcessBytes(input, inOff, length, outBytes, 0);
  126. if (outLength > 0 && pos < outLength)
  127. {
  128. byte[] tmp = new byte[pos];
  129. Array.Copy(outBytes, 0, tmp, 0, pos);
  130. outBytes = tmp;
  131. }
  132. return outBytes;
  133. }
  134. /**
  135. * process an array of bytes, producing output if necessary.
  136. *
  137. * @param in the input byte array.
  138. * @param inOff the offset at which the input data starts.
  139. * @param len the number of bytes to be copied out of the input array.
  140. * @param out the space for any output that might be produced.
  141. * @param outOff the offset from which the output will be copied.
  142. * @return the number of output bytes copied to out.
  143. * @exception DataLengthException if there isn't enough space in out.
  144. * @exception InvalidOperationException if the cipher isn't initialised.
  145. */
  146. public override int ProcessBytes(
  147. byte[] input,
  148. int inOff,
  149. int length,
  150. byte[] output,
  151. int outOff)
  152. {
  153. return cipher.ProcessBytes(input, inOff, length, output, outOff);
  154. }
  155. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  156. public override int ProcessBytes(ReadOnlySpan<byte> input, Span<byte> output)
  157. {
  158. return cipher.ProcessBytes(input, output);
  159. }
  160. #endif
  161. public override byte[] DoFinal()
  162. {
  163. byte[] outBytes = new byte[GetOutputSize(0)];
  164. int pos = DoFinal(outBytes, 0);
  165. if (pos < outBytes.Length)
  166. {
  167. byte[] tmp = new byte[pos];
  168. Array.Copy(outBytes, 0, tmp, 0, pos);
  169. outBytes = tmp;
  170. }
  171. return outBytes;
  172. }
  173. public override byte[] DoFinal(
  174. byte[] input,
  175. int inOff,
  176. int inLen)
  177. {
  178. if (input == null)
  179. throw new ArgumentNullException("input");
  180. byte[] outBytes = new byte[GetOutputSize(inLen)];
  181. int pos = (inLen > 0)
  182. ? ProcessBytes(input, inOff, inLen, outBytes, 0)
  183. : 0;
  184. pos += DoFinal(outBytes, pos);
  185. if (pos < outBytes.Length)
  186. {
  187. byte[] tmp = new byte[pos];
  188. Array.Copy(outBytes, 0, tmp, 0, pos);
  189. outBytes = tmp;
  190. }
  191. return outBytes;
  192. }
  193. /**
  194. * Process the last block in the buffer.
  195. *
  196. * @param out the array the block currently being held is copied into.
  197. * @param outOff the offset at which the copying starts.
  198. * @return the number of output bytes copied to out.
  199. * @exception DataLengthException if there is insufficient space in out for
  200. * the output, or the input is not block size aligned and should be.
  201. * @exception InvalidOperationException if the underlying cipher is not
  202. * initialised.
  203. * @exception InvalidCipherTextException if padding is expected and not found.
  204. * @exception DataLengthException if the input is not block size
  205. * aligned.
  206. */
  207. public override int DoFinal(
  208. byte[] output,
  209. int outOff)
  210. {
  211. return cipher.DoFinal(output, outOff);
  212. }
  213. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  214. public override int DoFinal(Span<byte> output)
  215. {
  216. return cipher.DoFinal(output);
  217. }
  218. public override int DoFinal(ReadOnlySpan<byte> input, Span<byte> output)
  219. {
  220. int len = cipher.ProcessBytes(input, output);
  221. len += cipher.DoFinal(output[len..]);
  222. return len;
  223. }
  224. #endif
  225. /**
  226. * Reset the buffer and cipher. After resetting the object is in the same
  227. * state as it was after the last init (if there was one).
  228. */
  229. public override void Reset()
  230. {
  231. cipher.Reset();
  232. }
  233. }
  234. }
  235. #pragma warning restore
  236. #endif