|
| 1 | +using System; |
| 2 | +using System.Security.Cryptography; |
| 3 | + |
| 4 | +using Renci.SshNet.Common; |
| 5 | + |
| 6 | +namespace Renci.SshNet.Security.Cryptography.Ciphers |
| 7 | +{ |
| 8 | + public partial class AesCipher |
| 9 | + { |
| 10 | + private sealed class BclImpl : BlockCipher, IDisposable |
| 11 | + { |
| 12 | + private readonly Aes _aes; |
| 13 | + private readonly ICryptoTransform _encryptor; |
| 14 | + private readonly ICryptoTransform _decryptor; |
| 15 | + |
| 16 | + public BclImpl( |
| 17 | + byte[] key, |
| 18 | + byte[] iv, |
| 19 | + System.Security.Cryptography.CipherMode cipherMode, |
| 20 | + PaddingMode paddingMode) |
| 21 | + : base(key, 16, mode: null, padding: null) |
| 22 | + { |
| 23 | + var aes = Aes.Create(); |
| 24 | + aes.Key = key; |
| 25 | + |
| 26 | + if (cipherMode != System.Security.Cryptography.CipherMode.ECB) |
| 27 | + { |
| 28 | + if (iv is null) |
| 29 | + { |
| 30 | + throw new ArgumentNullException(nameof(iv)); |
| 31 | + } |
| 32 | + |
| 33 | + aes.IV = iv.Take(16); |
| 34 | + } |
| 35 | + |
| 36 | + aes.Mode = cipherMode; |
| 37 | + aes.Padding = paddingMode; |
| 38 | + aes.FeedbackSize = 128; // We use CFB128 |
| 39 | + _aes = aes; |
| 40 | + _encryptor = aes.CreateEncryptor(); |
| 41 | + _decryptor = aes.CreateDecryptor(); |
| 42 | + } |
| 43 | + |
| 44 | + public override byte[] Encrypt(byte[] input, int offset, int length) |
| 45 | + { |
| 46 | + if (_aes.Padding != PaddingMode.None) |
| 47 | + { |
| 48 | + // If padding has been specified, call TransformFinalBlock to apply |
| 49 | + // the padding and reset the state. |
| 50 | + return _encryptor.TransformFinalBlock(input, offset, length); |
| 51 | + } |
| 52 | + |
| 53 | + // Otherwise, (the most important case) assume this instance is |
| 54 | + // used for one direction of an SSH connection, whereby the |
| 55 | + // encrypted data in all packets are considered a single data |
| 56 | + // stream i.e. we do not want to reset the state between calls to Encrypt. |
| 57 | + var output = new byte[length]; |
| 58 | + _ = _encryptor.TransformBlock(input, offset, length, output, 0); |
| 59 | + return output; |
| 60 | + } |
| 61 | + |
| 62 | + public override byte[] Decrypt(byte[] input, int offset, int length) |
| 63 | + { |
| 64 | + if (_aes.Padding != PaddingMode.None) |
| 65 | + { |
| 66 | + // If padding has been specified, call TransformFinalBlock to apply |
| 67 | + // the padding and reset the state. |
| 68 | + return _decryptor.TransformFinalBlock(input, offset, length); |
| 69 | + } |
| 70 | + |
| 71 | + // Otherwise, (the most important case) assume this instance is |
| 72 | + // used for one direction of an SSH connection, whereby the |
| 73 | + // encrypted data in all packets are considered a single data |
| 74 | + // stream i.e. we do not want to reset the state between calls to Decrypt. |
| 75 | + var output = new byte[length]; |
| 76 | + _ = _decryptor.TransformBlock(input, offset, length, output, 0); |
| 77 | + return output; |
| 78 | + } |
| 79 | + |
| 80 | + public override int EncryptBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset) |
| 81 | + { |
| 82 | + throw new NotImplementedException($"Invalid usage of {nameof(EncryptBlock)}."); |
| 83 | + } |
| 84 | + |
| 85 | + public override int DecryptBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset) |
| 86 | + { |
| 87 | + throw new NotImplementedException($"Invalid usage of {nameof(DecryptBlock)}."); |
| 88 | + } |
| 89 | + |
| 90 | + private void Dispose(bool disposing) |
| 91 | + { |
| 92 | + if (disposing) |
| 93 | + { |
| 94 | + _aes.Dispose(); |
| 95 | + _encryptor.Dispose(); |
| 96 | + _decryptor.Dispose(); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + public void Dispose() |
| 101 | + { |
| 102 | + // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method |
| 103 | + Dispose(disposing: true); |
| 104 | + GC.SuppressFinalize(this); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments