IAeadCipher.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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.Parameters;
  5. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Modes
  6. {
  7. /// <summary>
  8. /// A cipher mode that includes authenticated encryption with a streaming mode and optional
  9. /// associated data.
  10. /// </summary>
  11. /// <remarks>
  12. /// Implementations of this interface may operate in a packet mode (where all input data is
  13. /// buffered and processed during the call to DoFinal, or in a streaming mode (where output
  14. /// data is incrementally produced with each call to ProcessByte or ProcessBytes. This is
  15. /// important to consider during decryption: in a streaming mode, unauthenticated plaintext
  16. /// data may be output prior to the call to DoFinal that results in an authentication failure.
  17. /// The higher level protocol utilising this cipher must ensure the plaintext data is handled
  18. /// appropriately until the end of data is reached and the entire ciphertext is authenticated.
  19. /// </remarks>
  20. /// <see cref="AeadParameters"/>
  21. public interface IAeadCipher
  22. {
  23. /// <summary>The name of the algorithm this cipher implements.</summary>
  24. string AlgorithmName { get; }
  25. /// <summary>Initialise the cipher.</summary>
  26. /// <remarks>Parameter can either be an AeadParameters or a ParametersWithIV object.</remarks>
  27. /// <param name="forEncryption">Initialise for encryption if true, for decryption if false.</param>
  28. /// <param name="parameters">The key or other data required by the cipher.</param>
  29. void Init(bool forEncryption, ICipherParameters parameters);
  30. /// <summary>Add a single byte to the associated data check.</summary>
  31. /// <remarks>If the implementation supports it, this will be an online operation and will not retain the associated data.</remarks>
  32. /// <param name="input">The byte to be processed.</param>
  33. void ProcessAadByte(byte input);
  34. /// <summary>Add a sequence of bytes to the associated data check.</summary>
  35. /// <remarks>If the implementation supports it, this will be an online operation and will not retain the associated data.</remarks>
  36. /// <param name="inBytes">The input byte array.</param>
  37. /// <param name="inOff">The offset into the input array where the data to be processed starts.</param>
  38. /// <param name="len">The number of bytes to be processed.</param>
  39. void ProcessAadBytes(byte[] inBytes, int inOff, int len);
  40. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  41. /// <summary>Add a span of bytes to the associated data check.</summary>
  42. /// <remarks>If the implementation supports it, this will be an online operation and will not retain the associated data.</remarks>
  43. /// <param name="input">the span containing the data.</param>
  44. void ProcessAadBytes(ReadOnlySpan<byte> input);
  45. #endif
  46. /**
  47. * Encrypt/decrypt a single byte.
  48. *
  49. * @param input the byte to be processed.
  50. * @param outBytes the output buffer the processed byte goes into.
  51. * @param outOff the offset into the output byte array the processed data starts at.
  52. * @return the number of bytes written to out.
  53. * @exception DataLengthException if the output buffer is too small.
  54. */
  55. int ProcessByte(byte input, byte[] outBytes, int outOff);
  56. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  57. int ProcessByte(byte input, Span<byte> output);
  58. #endif
  59. /**
  60. * Process a block of bytes from in putting the result into out.
  61. *
  62. * @param inBytes the input byte array.
  63. * @param inOff the offset into the in array where the data to be processed starts.
  64. * @param len the number of bytes to be processed.
  65. * @param outBytes the output buffer the processed bytes go into.
  66. * @param outOff the offset into the output byte array the processed data starts at.
  67. * @return the number of bytes written to out.
  68. * @exception DataLengthException if the output buffer is too small.
  69. */
  70. int ProcessBytes(byte[] inBytes, int inOff, int len, byte[] outBytes, int outOff);
  71. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  72. int ProcessBytes(ReadOnlySpan<byte> input, Span<byte> output);
  73. #endif
  74. /**
  75. * Finish the operation either appending or verifying the MAC at the end of the data.
  76. *
  77. * @param outBytes space for any resulting output data.
  78. * @param outOff offset into out to start copying the data at.
  79. * @return number of bytes written into out.
  80. * @throws InvalidOperationException if the cipher is in an inappropriate state.
  81. * @throws InvalidCipherTextException if the MAC fails to match.
  82. */
  83. int DoFinal(byte[] outBytes, int outOff);
  84. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  85. int DoFinal(Span<byte> output);
  86. #endif
  87. /**
  88. * Return the value of the MAC associated with the last stream processed.
  89. *
  90. * @return MAC for plaintext data.
  91. */
  92. byte[] GetMac();
  93. /**
  94. * Return the size of the output buffer required for a ProcessBytes
  95. * an input of len bytes.
  96. *
  97. * @param len the length of the input.
  98. * @return the space required to accommodate a call to ProcessBytes
  99. * with len bytes of input.
  100. */
  101. int GetUpdateOutputSize(int len);
  102. /**
  103. * Return the size of the output buffer required for a ProcessBytes plus a
  104. * DoFinal with an input of len bytes.
  105. *
  106. * @param len the length of the input.
  107. * @return the space required to accommodate a call to ProcessBytes and DoFinal
  108. * with len bytes of input.
  109. */
  110. int GetOutputSize(int len);
  111. /// <summary>
  112. /// Reset the cipher to the same state as it was after the last init (if there was one).
  113. /// </summary>
  114. void Reset();
  115. }
  116. }
  117. #pragma warning restore
  118. #endif