| | | 1 | | using System; |
| | | 2 | | using Renci.SshNet.Security.Cryptography.Ciphers; |
| | | 3 | | |
| | | 4 | | namespace Renci.SshNet.Security.Cryptography |
| | | 5 | | { |
| | | 6 | | /// <summary> |
| | | 7 | | /// Base class for block cipher implementations. |
| | | 8 | | /// </summary> |
| | | 9 | | public abstract class BlockCipher : SymmetricCipher |
| | | 10 | | { |
| | | 11 | | private readonly CipherMode _mode; |
| | | 12 | | |
| | | 13 | | private readonly CipherPadding _padding; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Gets the size of the block in bytes. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <value> |
| | | 19 | | /// The size of the block in bytes. |
| | | 20 | | /// </value> |
| | | 21 | | private readonly byte _blockSize; |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Gets the minimum data size. |
| | | 25 | | /// </summary> |
| | | 26 | | /// <value> |
| | | 27 | | /// The minimum data size. |
| | | 28 | | /// </value> |
| | | 29 | | public override byte MinimumSize |
| | | 30 | | { |
| | 303537 | 31 | | get { return BlockSize; } |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Gets the size of the block. |
| | | 36 | | /// </summary> |
| | | 37 | | /// <value> |
| | | 38 | | /// The size of the block. |
| | | 39 | | /// </value> |
| | | 40 | | public byte BlockSize |
| | | 41 | | { |
| | | 42 | | get |
| | 555470 | 43 | | { |
| | 555470 | 44 | | return _blockSize; |
| | 555470 | 45 | | } |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Initializes a new instance of the <see cref="BlockCipher"/> class. |
| | | 50 | | /// </summary> |
| | | 51 | | /// <param name="key">The key.</param> |
| | | 52 | | /// <param name="blockSize">Size of the block.</param> |
| | | 53 | | /// <param name="mode">Cipher mode.</param> |
| | | 54 | | /// <param name="padding">Cipher padding.</param> |
| | | 55 | | /// <exception cref="ArgumentNullException"><paramref name="key"/> is <see langword="null"/>.</exception> |
| | | 56 | | protected BlockCipher(byte[] key, byte blockSize, CipherMode mode, CipherPadding padding) |
| | 5517 | 57 | | : base(key) |
| | 5517 | 58 | | { |
| | 5517 | 59 | | _blockSize = blockSize; |
| | 5517 | 60 | | _mode = mode; |
| | 5517 | 61 | | _padding = padding; |
| | | 62 | | |
| | 5517 | 63 | | _mode?.Init(this); |
| | 5517 | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Encrypts the specified data. |
| | | 68 | | /// </summary> |
| | | 69 | | /// <param name="input">The data.</param> |
| | | 70 | | /// <param name="offset">The zero-based offset in <paramref name="input"/> at which to begin encrypting.</param> |
| | | 71 | | /// <param name="length">The number of bytes to encrypt from <paramref name="input"/>.</param> |
| | | 72 | | /// <returns> |
| | | 73 | | /// The encrypted data. |
| | | 74 | | /// </returns> |
| | | 75 | | public override byte[] Encrypt(byte[] input, int offset, int length) |
| | 77 | 76 | | { |
| | 77 | 77 | | if (length % _blockSize > 0) |
| | 3 | 78 | | { |
| | 3 | 79 | | if (_padding is null) |
| | 0 | 80 | | { |
| | 0 | 81 | | throw new ArgumentException("data"); |
| | | 82 | | } |
| | | 83 | | |
| | 3 | 84 | | var paddingLength = _blockSize - (length % _blockSize); |
| | 3 | 85 | | input = _padding.Pad(input, offset, length, paddingLength); |
| | 3 | 86 | | length += paddingLength; |
| | 3 | 87 | | offset = 0; |
| | 3 | 88 | | } |
| | | 89 | | |
| | 77 | 90 | | var output = new byte[length]; |
| | 77 | 91 | | var writtenBytes = 0; |
| | | 92 | | |
| | 1040 | 93 | | for (var i = 0; i < length / _blockSize; i++) |
| | 443 | 94 | | { |
| | 443 | 95 | | if (_mode is null) |
| | 3 | 96 | | { |
| | 3 | 97 | | writtenBytes += EncryptBlock(input, offset + (i * _blockSize), _blockSize, output, i * _blockSize); |
| | 3 | 98 | | } |
| | | 99 | | else |
| | 440 | 100 | | { |
| | 440 | 101 | | writtenBytes += _mode.EncryptBlock(input, offset + (i * _blockSize), _blockSize, output, i * _blockS |
| | 440 | 102 | | } |
| | 443 | 103 | | } |
| | | 104 | | |
| | 77 | 105 | | if (writtenBytes < length) |
| | 0 | 106 | | { |
| | 0 | 107 | | throw new InvalidOperationException("Encryption error."); |
| | | 108 | | } |
| | | 109 | | |
| | 77 | 110 | | return output; |
| | 77 | 111 | | } |
| | | 112 | | |
| | | 113 | | /// <summary> |
| | | 114 | | /// Decrypts the specified data. |
| | | 115 | | /// </summary> |
| | | 116 | | /// <param name="input">The data.</param> |
| | | 117 | | /// <returns> |
| | | 118 | | /// The decrypted data. |
| | | 119 | | /// </returns> |
| | | 120 | | public override byte[] Decrypt(byte[] input) |
| | 52841 | 121 | | { |
| | 52841 | 122 | | return Decrypt(input, 0, input.Length); |
| | 52841 | 123 | | } |
| | | 124 | | |
| | | 125 | | /// <summary> |
| | | 126 | | /// Decrypts the specified input. |
| | | 127 | | /// </summary> |
| | | 128 | | /// <param name="input">The input.</param> |
| | | 129 | | /// <param name="offset">The zero-based offset in <paramref name="input"/> at which to begin decrypting.</param> |
| | | 130 | | /// <param name="length">The number of bytes to decrypt from <paramref name="input"/>.</param> |
| | | 131 | | /// <returns> |
| | | 132 | | /// The decrypted data. |
| | | 133 | | /// </returns> |
| | | 134 | | public override byte[] Decrypt(byte[] input, int offset, int length) |
| | 128 | 135 | | { |
| | 128 | 136 | | if (length % _blockSize > 0) |
| | 6 | 137 | | { |
| | 6 | 138 | | if (_padding is null) |
| | 0 | 139 | | { |
| | 0 | 140 | | throw new ArgumentException("data"); |
| | | 141 | | } |
| | | 142 | | |
| | 6 | 143 | | input = _padding.Pad(_blockSize, input, offset, length); |
| | 6 | 144 | | offset = 0; |
| | 6 | 145 | | length = input.Length; |
| | 6 | 146 | | } |
| | | 147 | | |
| | 128 | 148 | | var output = new byte[length]; |
| | | 149 | | |
| | 128 | 150 | | var writtenBytes = 0; |
| | 5070 | 151 | | for (var i = 0; i < length / _blockSize; i++) |
| | 2407 | 152 | | { |
| | 2407 | 153 | | if (_mode is null) |
| | 3 | 154 | | { |
| | 3 | 155 | | writtenBytes += DecryptBlock(input, offset + (i * _blockSize), _blockSize, output, i * _blockSize); |
| | 3 | 156 | | } |
| | | 157 | | else |
| | 2404 | 158 | | { |
| | 2404 | 159 | | writtenBytes += _mode.DecryptBlock(input, offset + (i * _blockSize), _blockSize, output, i * _blockS |
| | 2404 | 160 | | } |
| | 2407 | 161 | | } |
| | | 162 | | |
| | 128 | 163 | | if (writtenBytes < length) |
| | 0 | 164 | | { |
| | 0 | 165 | | throw new InvalidOperationException("Encryption error."); |
| | | 166 | | } |
| | | 167 | | |
| | 128 | 168 | | return output; |
| | 128 | 169 | | } |
| | | 170 | | } |
| | | 171 | | } |