| | | 1 | | using System; |
| | | 2 | | |
| | | 3 | | namespace Renci.SshNet.Security.Cryptography.Ciphers |
| | | 4 | | { |
| | | 5 | | /// <summary> |
| | | 6 | | /// Implements 3DES cipher algorithm. |
| | | 7 | | /// </summary> |
| | | 8 | | public sealed class TripleDesCipher : DesCipher |
| | | 9 | | { |
| | | 10 | | private int[] _encryptionKey1; |
| | | 11 | | private int[] _encryptionKey2; |
| | | 12 | | private int[] _encryptionKey3; |
| | | 13 | | private int[] _decryptionKey1; |
| | | 14 | | private int[] _decryptionKey2; |
| | | 15 | | private int[] _decryptionKey3; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Initializes a new instance of the <see cref="TripleDesCipher"/> class. |
| | | 19 | | /// </summary> |
| | | 20 | | /// <param name="key">The key.</param> |
| | | 21 | | /// <param name="mode">The mode.</param> |
| | | 22 | | /// <param name="padding">The padding.</param> |
| | | 23 | | /// <exception cref="ArgumentNullException"><paramref name="key"/> is <see langword="null"/>.</exception> |
| | | 24 | | public TripleDesCipher(byte[] key, CipherMode mode, CipherPadding padding) |
| | 25 | 25 | | : base(key, mode, padding) |
| | 25 | 26 | | { |
| | 25 | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Encrypts the specified region of the input byte array and copies the encrypted data to the specified region |
| | | 31 | | /// </summary> |
| | | 32 | | /// <param name="inputBuffer">The input data to encrypt.</param> |
| | | 33 | | /// <param name="inputOffset">The offset into the input byte array from which to begin using data.</param> |
| | | 34 | | /// <param name="inputCount">The number of bytes in the input byte array to use as data.</param> |
| | | 35 | | /// <param name="outputBuffer">The output to which to write encrypted data.</param> |
| | | 36 | | /// <param name="outputOffset">The offset into the output byte array from which to begin writing data.</param> |
| | | 37 | | /// <returns> |
| | | 38 | | /// The number of bytes encrypted. |
| | | 39 | | /// </returns> |
| | | 40 | | public override int EncryptBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int o |
| | 539 | 41 | | { |
| | 539 | 42 | | if ((inputOffset + BlockSize) > inputBuffer.Length) |
| | 0 | 43 | | { |
| | 0 | 44 | | throw new ArgumentException("input buffer too short"); |
| | | 45 | | } |
| | | 46 | | |
| | 539 | 47 | | if ((outputOffset + BlockSize) > outputBuffer.Length) |
| | 0 | 48 | | { |
| | 0 | 49 | | throw new ArgumentException("output buffer too short"); |
| | | 50 | | } |
| | | 51 | | |
| | 539 | 52 | | if (_encryptionKey1 is null || _encryptionKey2 is null || _encryptionKey3 is null) |
| | 9 | 53 | | { |
| | 9 | 54 | | var part1 = new byte[8]; |
| | 9 | 55 | | var part2 = new byte[8]; |
| | | 56 | | |
| | 9 | 57 | | Buffer.BlockCopy(Key, 0, part1, 0, 8); |
| | 9 | 58 | | Buffer.BlockCopy(Key, 8, part2, 0, 8); |
| | | 59 | | |
| | 9 | 60 | | _encryptionKey1 = GenerateWorkingKey(encrypting: true, part1); |
| | 9 | 61 | | _encryptionKey2 = GenerateWorkingKey(encrypting: false, part2); |
| | | 62 | | |
| | 9 | 63 | | if (Key.Length == 24) |
| | 9 | 64 | | { |
| | 9 | 65 | | var part3 = new byte[8]; |
| | 9 | 66 | | Buffer.BlockCopy(Key, 16, part3, 0, 8); |
| | | 67 | | |
| | 9 | 68 | | _encryptionKey3 = GenerateWorkingKey(encrypting: true, part3); |
| | 9 | 69 | | } |
| | | 70 | | else |
| | 0 | 71 | | { |
| | 0 | 72 | | _encryptionKey3 = _encryptionKey1; |
| | 0 | 73 | | } |
| | 9 | 74 | | } |
| | | 75 | | |
| | 539 | 76 | | var temp = new byte[BlockSize]; |
| | | 77 | | |
| | 539 | 78 | | DesFunc(_encryptionKey1, inputBuffer, inputOffset, temp, 0); |
| | 539 | 79 | | DesFunc(_encryptionKey2, temp, 0, temp, 0); |
| | 539 | 80 | | DesFunc(_encryptionKey3, temp, 0, outputBuffer, outputOffset); |
| | | 81 | | |
| | 539 | 82 | | return BlockSize; |
| | 539 | 83 | | } |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Decrypts the specified region of the input byte array and copies the decrypted data to the specified region |
| | | 87 | | /// </summary> |
| | | 88 | | /// <param name="inputBuffer">The input data to decrypt.</param> |
| | | 89 | | /// <param name="inputOffset">The offset into the input byte array from which to begin using data.</param> |
| | | 90 | | /// <param name="inputCount">The number of bytes in the input byte array to use as data.</param> |
| | | 91 | | /// <param name="outputBuffer">The output to which to write decrypted data.</param> |
| | | 92 | | /// <param name="outputOffset">The offset into the output byte array from which to begin writing data.</param> |
| | | 93 | | /// <returns> |
| | | 94 | | /// The number of bytes decrypted. |
| | | 95 | | /// </returns> |
| | | 96 | | public override int DecryptBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int o |
| | 1618 | 97 | | { |
| | 1618 | 98 | | if ((inputOffset + BlockSize) > inputBuffer.Length) |
| | 0 | 99 | | { |
| | 0 | 100 | | throw new ArgumentException("input buffer too short"); |
| | | 101 | | } |
| | | 102 | | |
| | 1618 | 103 | | if ((outputOffset + BlockSize) > outputBuffer.Length) |
| | 0 | 104 | | { |
| | 0 | 105 | | throw new ArgumentException("output buffer too short"); |
| | | 106 | | } |
| | | 107 | | |
| | 1618 | 108 | | if (_decryptionKey1 is null || _decryptionKey2 is null || _decryptionKey3 is null) |
| | 16 | 109 | | { |
| | 16 | 110 | | var part1 = new byte[8]; |
| | 16 | 111 | | var part2 = new byte[8]; |
| | | 112 | | |
| | 16 | 113 | | Buffer.BlockCopy(Key, 0, part1, 0, 8); |
| | 16 | 114 | | Buffer.BlockCopy(Key, 8, part2, 0, 8); |
| | | 115 | | |
| | 16 | 116 | | _decryptionKey1 = GenerateWorkingKey(encrypting: false, part1); |
| | 16 | 117 | | _decryptionKey2 = GenerateWorkingKey(encrypting: true, part2); |
| | | 118 | | |
| | 16 | 119 | | if (Key.Length == 24) |
| | 16 | 120 | | { |
| | 16 | 121 | | var part3 = new byte[8]; |
| | 16 | 122 | | Buffer.BlockCopy(Key, 16, part3, 0, 8); |
| | | 123 | | |
| | 16 | 124 | | _decryptionKey3 = GenerateWorkingKey(encrypting: false, part3); |
| | 16 | 125 | | } |
| | | 126 | | else |
| | 0 | 127 | | { |
| | 0 | 128 | | _decryptionKey3 = _decryptionKey1; |
| | 0 | 129 | | } |
| | 16 | 130 | | } |
| | | 131 | | |
| | 1618 | 132 | | var temp = new byte[BlockSize]; |
| | | 133 | | |
| | 1618 | 134 | | DesFunc(_decryptionKey3, inputBuffer, inputOffset, temp, 0); |
| | 1618 | 135 | | DesFunc(_decryptionKey2, temp, 0, temp, 0); |
| | 1618 | 136 | | DesFunc(_decryptionKey1, temp, 0, outputBuffer, outputOffset); |
| | | 137 | | |
| | 1618 | 138 | | return BlockSize; |
| | 1618 | 139 | | } |
| | | 140 | | |
| | | 141 | | /// <summary> |
| | | 142 | | /// Validates the key. |
| | | 143 | | /// </summary> |
| | | 144 | | protected override void ValidateKey() |
| | 75 | 145 | | { |
| | 75 | 146 | | var keySize = Key.Length * 8; |
| | | 147 | | |
| | 75 | 148 | | if (keySize is not (128 or 128 + 64)) |
| | 0 | 149 | | { |
| | 0 | 150 | | throw new ArgumentException(string.Format("KeySize '{0}' is not valid for this algorithm.", keySize)); |
| | | 151 | | } |
| | 75 | 152 | | } |
| | | 153 | | } |
| | | 154 | | } |