forked from sshnet/SSH.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Factor out the implementations and re-add the existing constructor #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
src/Renci.SshNet/Security/Cryptography/Ciphers/AesCipher.BclImpl.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
using System; | ||
using System.Security.Cryptography; | ||
|
||
using Renci.SshNet.Common; | ||
|
||
namespace Renci.SshNet.Security.Cryptography.Ciphers | ||
{ | ||
public partial class AesCipher | ||
{ | ||
private sealed class BclImpl : BlockCipher, IDisposable | ||
{ | ||
private readonly Aes _aes; | ||
private readonly ICryptoTransform _encryptor; | ||
private readonly ICryptoTransform _decryptor; | ||
|
||
public BclImpl( | ||
byte[] key, | ||
byte[] iv, | ||
System.Security.Cryptography.CipherMode cipherMode, | ||
PaddingMode paddingMode) | ||
: base(key, 16, mode: null, padding: null) | ||
{ | ||
var aes = Aes.Create(); | ||
aes.Key = key; | ||
|
||
if (cipherMode != System.Security.Cryptography.CipherMode.ECB) | ||
{ | ||
if (iv is null) | ||
{ | ||
throw new ArgumentNullException(nameof(iv)); | ||
} | ||
|
||
aes.IV = iv.Take(16); | ||
} | ||
|
||
aes.Mode = cipherMode; | ||
aes.Padding = paddingMode; | ||
aes.FeedbackSize = 128; // We use CFB128 | ||
_aes = aes; | ||
_encryptor = aes.CreateEncryptor(); | ||
_decryptor = aes.CreateDecryptor(); | ||
} | ||
|
||
public override byte[] Encrypt(byte[] input, int offset, int length) | ||
{ | ||
if (_aes.Padding != PaddingMode.None) | ||
{ | ||
// If padding has been specified, call TransformFinalBlock to apply | ||
// the padding and reset the state. | ||
return _encryptor.TransformFinalBlock(input, offset, length); | ||
} | ||
|
||
// Otherwise, (the most important case) assume this instance is | ||
// used for one direction of an SSH connection, whereby the | ||
// encrypted data in all packets are considered a single data | ||
// stream i.e. we do not want to reset the state between calls to Encrypt. | ||
var output = new byte[length]; | ||
_ = _encryptor.TransformBlock(input, offset, length, output, 0); | ||
return output; | ||
} | ||
|
||
public override byte[] Decrypt(byte[] input, int offset, int length) | ||
{ | ||
if (_aes.Padding != PaddingMode.None) | ||
{ | ||
// If padding has been specified, call TransformFinalBlock to apply | ||
// the padding and reset the state. | ||
return _decryptor.TransformFinalBlock(input, offset, length); | ||
} | ||
|
||
// Otherwise, (the most important case) assume this instance is | ||
// used for one direction of an SSH connection, whereby the | ||
// encrypted data in all packets are considered a single data | ||
// stream i.e. we do not want to reset the state between calls to Decrypt. | ||
var output = new byte[length]; | ||
_ = _decryptor.TransformBlock(input, offset, length, output, 0); | ||
return output; | ||
} | ||
|
||
public override int EncryptBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset) | ||
{ | ||
throw new NotImplementedException($"Invalid usage of {nameof(EncryptBlock)}."); | ||
} | ||
|
||
public override int DecryptBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset) | ||
{ | ||
throw new NotImplementedException($"Invalid usage of {nameof(DecryptBlock)}."); | ||
} | ||
|
||
private void Dispose(bool disposing) | ||
{ | ||
if (disposing) | ||
{ | ||
_aes.Dispose(); | ||
_encryptor.Dispose(); | ||
_decryptor.Dispose(); | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method | ||
Dispose(disposing: true); | ||
GC.SuppressFinalize(this); | ||
} | ||
} | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/Renci.SshNet/Security/Cryptography/Ciphers/AesCipher.BlockImpl.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using System; | ||
using System.Security.Cryptography; | ||
|
||
namespace Renci.SshNet.Security.Cryptography.Ciphers | ||
{ | ||
public partial class AesCipher | ||
{ | ||
private sealed class BlockImpl : BlockCipher, IDisposable | ||
{ | ||
private readonly Aes _aes; | ||
private readonly ICryptoTransform _encryptor; | ||
private readonly ICryptoTransform _decryptor; | ||
|
||
public BlockImpl(byte[] key, CipherMode mode, CipherPadding padding) | ||
: base(key, 16, mode, padding) | ||
{ | ||
var aes = Aes.Create(); | ||
aes.Key = key; | ||
aes.Mode = System.Security.Cryptography.CipherMode.ECB; | ||
aes.Padding = PaddingMode.None; | ||
_aes = aes; | ||
_encryptor = aes.CreateEncryptor(); | ||
_decryptor = aes.CreateDecryptor(); | ||
} | ||
|
||
public override int EncryptBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset) | ||
{ | ||
return _encryptor.TransformBlock(inputBuffer, inputOffset, inputCount, outputBuffer, outputOffset); | ||
} | ||
|
||
public override int DecryptBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset) | ||
{ | ||
return _decryptor.TransformBlock(inputBuffer, inputOffset, inputCount, outputBuffer, outputOffset); | ||
} | ||
|
||
private void Dispose(bool disposing) | ||
{ | ||
if (disposing) | ||
{ | ||
_aes.Dispose(); | ||
_encryptor.Dispose(); | ||
_decryptor.Dispose(); | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method | ||
Dispose(disposing: true); | ||
GC.SuppressFinalize(this); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.