BufferedAeadBlockCipher.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Modes;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  6. namespace BestHTTP.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(
  92. byte input,
  93. byte[] output,
  94. int outOff)
  95. {
  96. return cipher.ProcessByte(input, output, outOff);
  97. }
  98. public override byte[] ProcessByte(
  99. byte input)
  100. {
  101. int outLength = GetUpdateOutputSize(1);
  102. byte[] outBytes = outLength > 0 ? new byte[outLength] : null;
  103. int pos = ProcessByte(input, outBytes, 0);
  104. if (outLength > 0 && pos < outLength)
  105. {
  106. byte[] tmp = new byte[pos];
  107. Array.Copy(outBytes, 0, tmp, 0, pos);
  108. outBytes = tmp;
  109. }
  110. return outBytes;
  111. }
  112. public override byte[] ProcessBytes(
  113. byte[] input,
  114. int inOff,
  115. int length)
  116. {
  117. if (input == null)
  118. throw new ArgumentNullException("input");
  119. if (length < 1)
  120. return null;
  121. int outLength = GetUpdateOutputSize(length);
  122. byte[] outBytes = outLength > 0 ? new byte[outLength] : null;
  123. int pos = ProcessBytes(input, inOff, length, outBytes, 0);
  124. if (outLength > 0 && pos < outLength)
  125. {
  126. byte[] tmp = new byte[pos];
  127. Array.Copy(outBytes, 0, tmp, 0, pos);
  128. outBytes = tmp;
  129. }
  130. return outBytes;
  131. }
  132. /**
  133. * process an array of bytes, producing output if necessary.
  134. *
  135. * @param in the input byte array.
  136. * @param inOff the offset at which the input data starts.
  137. * @param len the number of bytes to be copied out of the input array.
  138. * @param out the space for any output that might be produced.
  139. * @param outOff the offset from which the output will be copied.
  140. * @return the number of output bytes copied to out.
  141. * @exception DataLengthException if there isn't enough space in out.
  142. * @exception InvalidOperationException if the cipher isn't initialised.
  143. */
  144. public override int ProcessBytes(
  145. byte[] input,
  146. int inOff,
  147. int length,
  148. byte[] output,
  149. int outOff)
  150. {
  151. return cipher.ProcessBytes(input, inOff, length, output, outOff);
  152. }
  153. public override byte[] DoFinal()
  154. {
  155. byte[] outBytes = new byte[GetOutputSize(0)];
  156. int pos = DoFinal(outBytes, 0);
  157. if (pos < outBytes.Length)
  158. {
  159. byte[] tmp = new byte[pos];
  160. Array.Copy(outBytes, 0, tmp, 0, pos);
  161. outBytes = tmp;
  162. }
  163. return outBytes;
  164. }
  165. public override byte[] DoFinal(
  166. byte[] input,
  167. int inOff,
  168. int inLen)
  169. {
  170. if (input == null)
  171. throw new ArgumentNullException("input");
  172. byte[] outBytes = new byte[GetOutputSize(inLen)];
  173. int pos = (inLen > 0)
  174. ? ProcessBytes(input, inOff, inLen, outBytes, 0)
  175. : 0;
  176. pos += DoFinal(outBytes, pos);
  177. if (pos < outBytes.Length)
  178. {
  179. byte[] tmp = new byte[pos];
  180. Array.Copy(outBytes, 0, tmp, 0, pos);
  181. outBytes = tmp;
  182. }
  183. return outBytes;
  184. }
  185. /**
  186. * Process the last block in the buffer.
  187. *
  188. * @param out the array the block currently being held is copied into.
  189. * @param outOff the offset at which the copying starts.
  190. * @return the number of output bytes copied to out.
  191. * @exception DataLengthException if there is insufficient space in out for
  192. * the output, or the input is not block size aligned and should be.
  193. * @exception InvalidOperationException if the underlying cipher is not
  194. * initialised.
  195. * @exception InvalidCipherTextException if padding is expected and not found.
  196. * @exception DataLengthException if the input is not block size
  197. * aligned.
  198. */
  199. public override int DoFinal(
  200. byte[] output,
  201. int outOff)
  202. {
  203. return cipher.DoFinal(output, outOff);
  204. }
  205. /**
  206. * Reset the buffer and cipher. After resetting the object is in the same
  207. * state as it was after the last init (if there was one).
  208. */
  209. public override void Reset()
  210. {
  211. cipher.Reset();
  212. }
  213. }
  214. }
  215. #pragma warning restore
  216. #endif