BufferedAeadBlockCipher.cs 7.2 KB

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