-
-
Notifications
You must be signed in to change notification settings - Fork 959
Use hardware-accelerated AES CryptoServiceProvider #865
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 24 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
af5b858
Add FEATURE_AES_CSP to use hardware-accelerated AesCryptoServiceProvider
9622631
Restructure, move most of the feature code to AesCipher.cs
75fa982
Fix the AES Padding
5cc5d4a
fix rebase issues
086ad43
Minor fixes
a9f68fb
Merge branch 'develop' into feature_AES_CSP
zybexXL 39db35b
Rework based on suggestions
63bfc76
Move all changes to AesCypher.cs, as per Rob-Hague suggestion
5ef1df6
update AesCipherTest.cs generator
ac2c36d
Fix continuous session encrypt/decrypt
ada6ccb
Reduce CTR memory usage in Net 6+
799a5c5
Factor out the implementations and re-add the existing constructor
Rob-Hague 0a9be47
Merge branch 'develop' into feature_AES_CSP
zybexXL 0d5ecd9
Merge branch 'sshnet:develop' into feature_AES_CSP
zybexXL 36cea67
remove ctor; revert tests; remove unused _iv member
Rob-Hague d53c12c
Merge pull request #1 from Rob-Hague/feature_AES_CSP
zybexXL 2a11508
Merge branch 'develop' into feature_AES_CSP
zybexXL 84df6e4
Reorder Encryption cipher preference list
c056fbc
Remove redundant AES tests
1902ed7
Refactor ArrayXOR()
d34f521
Merge branch 'develop' into feature_AES_CSP
zybexXL 6529a63
Add test for IV overflow
Rob-Hague 2cedd26
Performance bump for AES CTR (thanks @robhague)
15de75e
fix merge conflict
554c2d6
Move AesCipherMode enum to its own file
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
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
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.