IAeadCipher.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  5. namespace BestHTTP.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. /**
  41. * Encrypt/decrypt a single byte.
  42. *
  43. * @param input the byte to be processed.
  44. * @param outBytes the output buffer the processed byte goes into.
  45. * @param outOff the offset into the output byte array the processed data starts at.
  46. * @return the number of bytes written to out.
  47. * @exception DataLengthException if the output buffer is too small.
  48. */
  49. int ProcessByte(byte input, byte[] outBytes, int outOff);
  50. /**
  51. * Process a block of bytes from in putting the result into out.
  52. *
  53. * @param inBytes the input byte array.
  54. * @param inOff the offset into the in array where the data to be processed starts.
  55. * @param len the number of bytes to be processed.
  56. * @param outBytes the output buffer the processed bytes go into.
  57. * @param outOff the offset into the output byte array the processed data starts at.
  58. * @return the number of bytes written to out.
  59. * @exception DataLengthException if the output buffer is too small.
  60. */
  61. int ProcessBytes(byte[] inBytes, int inOff, int len, byte[] outBytes, int outOff);
  62. /**
  63. * Finish the operation either appending or verifying the MAC at the end of the data.
  64. *
  65. * @param outBytes space for any resulting output data.
  66. * @param outOff offset into out to start copying the data at.
  67. * @return number of bytes written into out.
  68. * @throws InvalidOperationException if the cipher is in an inappropriate state.
  69. * @throws InvalidCipherTextException if the MAC fails to match.
  70. */
  71. int DoFinal(byte[] outBytes, int outOff);
  72. /**
  73. * Return the value of the MAC associated with the last stream processed.
  74. *
  75. * @return MAC for plaintext data.
  76. */
  77. byte[] GetMac();
  78. /**
  79. * Return the size of the output buffer required for a ProcessBytes
  80. * an input of len bytes.
  81. *
  82. * @param len the length of the input.
  83. * @return the space required to accommodate a call to ProcessBytes
  84. * with len bytes of input.
  85. */
  86. int GetUpdateOutputSize(int len);
  87. /**
  88. * Return the size of the output buffer required for a ProcessBytes plus a
  89. * DoFinal with an input of len bytes.
  90. *
  91. * @param len the length of the input.
  92. * @return the space required to accommodate a call to ProcessBytes and DoFinal
  93. * with len bytes of input.
  94. */
  95. int GetOutputSize(int len);
  96. /// <summary>
  97. /// Reset the cipher to the same state as it was after the last init (if there was one).
  98. /// </summary>
  99. void Reset();
  100. }
  101. }
  102. #pragma warning restore
  103. #endif