| | | 1 | | using Renci.SshNet.Common; |
| | | 2 | | |
| | | 3 | | namespace Renci.SshNet.Security.Cryptography.Ciphers |
| | | 4 | | { |
| | | 5 | | /// <summary> |
| | | 6 | | /// Base class for cipher mode implementations. |
| | | 7 | | /// </summary> |
| | | 8 | | public abstract class CipherMode |
| | | 9 | | { |
| | | 10 | | #pragma warning disable SA1401 // Fields should be private |
| | | 11 | | #pragma warning disable SA1306 // Field names should begin with lower-case letter |
| | | 12 | | /// <summary> |
| | | 13 | | /// Gets the cipher. |
| | | 14 | | /// </summary> |
| | | 15 | | protected BlockCipher Cipher; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Gets the IV vector. |
| | | 19 | | /// </summary> |
| | | 20 | | protected byte[] IV; |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Holds block size of the cipher. |
| | | 24 | | /// </summary> |
| | | 25 | | protected int _blockSize; |
| | | 26 | | #pragma warning restore SA1306 // Field names should begin with lower-case letter |
| | | 27 | | #pragma warning restore SA1401 // Fields should be private |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Initializes a new instance of the <see cref="CipherMode"/> class. |
| | | 31 | | /// </summary> |
| | | 32 | | /// <param name="iv">The iv.</param> |
| | 115 | 33 | | protected CipherMode(byte[] iv) |
| | 115 | 34 | | { |
| | 115 | 35 | | IV = iv; |
| | 115 | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Initializes the specified cipher mode. |
| | | 40 | | /// </summary> |
| | | 41 | | /// <param name="cipher">The cipher.</param> |
| | | 42 | | internal void Init(BlockCipher cipher) |
| | 115 | 43 | | { |
| | 115 | 44 | | Cipher = cipher; |
| | 115 | 45 | | _blockSize = cipher.BlockSize; |
| | 115 | 46 | | IV = IV.Take(_blockSize); |
| | 115 | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Encrypts the specified region of the input byte array and copies the encrypted data to the specified region |
| | | 51 | | /// </summary> |
| | | 52 | | /// <param name="inputBuffer">The input data to encrypt.</param> |
| | | 53 | | /// <param name="inputOffset">The offset into the input byte array from which to begin using data.</param> |
| | | 54 | | /// <param name="inputCount">The number of bytes in the input byte array to use as data.</param> |
| | | 55 | | /// <param name="outputBuffer">The output to which to write encrypted data.</param> |
| | | 56 | | /// <param name="outputOffset">The offset into the output byte array from which to begin writing data.</param> |
| | | 57 | | /// <returns> |
| | | 58 | | /// The number of bytes encrypted. |
| | | 59 | | /// </returns> |
| | | 60 | | public abstract int EncryptBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int o |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Decrypts the specified region of the input byte array and copies the decrypted data to the specified region |
| | | 64 | | /// </summary> |
| | | 65 | | /// <param name="inputBuffer">The input data to decrypt.</param> |
| | | 66 | | /// <param name="inputOffset">The offset into the input byte array from which to begin using data.</param> |
| | | 67 | | /// <param name="inputCount">The number of bytes in the input byte array to use as data.</param> |
| | | 68 | | /// <param name="outputBuffer">The output to which to write decrypted data.</param> |
| | | 69 | | /// <param name="outputOffset">The offset into the output byte array from which to begin writing data.</param> |
| | | 70 | | /// <returns> |
| | | 71 | | /// The number of bytes decrypted. |
| | | 72 | | /// </returns> |
| | | 73 | | public abstract int DecryptBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int o |
| | | 74 | | } |
| | | 75 | | } |