-
-
Notifications
You must be signed in to change notification settings - Fork 952
[AesGcm] Falls back to use BouncyCastle if BCL doesn't support #1450
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
23 commits
Select commit
Hold shift + click to select a range
7df5108
[AesGcmCipher] Use BouncyCastle as a fallback if BCL does not support.
scott-xu dfe5b22
Switch back to collection initializer
scott-xu aff0b4d
Merge branch 'develop' into bc_aesgcm
WojciechNagorski a905b34
Merge branch 'develop' into bc_aesgcm
WojciechNagorski f1a92d3
Merge branch 'develop' into bc_aesgcm
WojciechNagorski 55f38c4
Merge branch 'develop' into bc_aesgcm
scott-xu f3121f4
Remove conditional compilation
scott-xu 1152c9d
Throw SshConnectionException with Reason MacError when authentication…
scott-xu 75f9bce
Separate BCL and BouncyCastle implementation
scott-xu 8bdf511
Update AesGcmCipher.BclImpl.cs
scott-xu 013741e
Merge branch 'develop' into bc_aesgcm
scott-xu 11ab2ee
Merge branch 'develop' into bc_aesgcm
scott-xu e36823b
Naming enhancement
scott-xu b12e3b6
Remove empty line
scott-xu a324a8c
Merge branch 'develop' of https://github.com/scott-xu/SSH.NET into bc…
scott-xu 1751ed7
Disable S1199. See https://github.com/sshnet/SSH.NET/pull/1371#discus…
scott-xu 6f47e03
Set InnerException when MAC error. Remove Message check.
scott-xu 432d6b6
Store KeyParameter as private field
scott-xu 1e9a0b4
Use GcmCipher.ProcessAadBytes to avoid the copy of associated data
scott-xu fb68303
Move nonce to constructor to avoid creating AeadParameters each packet
scott-xu b4dc013
Use const int for tag size
scott-xu 2e9228d
Merge branch 'develop' into bc_aesgcm
scott-xu ab5989d
Merge branch 'develop' into bc_aesgcm
scott-xu 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
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
72 changes: 72 additions & 0 deletions
72
src/Renci.SshNet/Security/Cryptography/Ciphers/AesGcmCipher.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,72 @@ | ||
#if NET6_0_OR_GREATER | ||
using System; | ||
using System.Security.Cryptography; | ||
|
||
using Renci.SshNet.Common; | ||
using Renci.SshNet.Messages.Transport; | ||
|
||
namespace Renci.SshNet.Security.Cryptography.Ciphers | ||
{ | ||
internal partial class AesGcmCipher | ||
{ | ||
private sealed class BclImpl : Impl | ||
{ | ||
private readonly AesGcm _aesGcm; | ||
private readonly byte[] _nonce; | ||
|
||
public BclImpl(byte[] key, byte[] nonce) | ||
{ | ||
#if NET8_0_OR_GREATER | ||
_aesGcm = new AesGcm(key, TagSizeInBytes); | ||
#else | ||
_aesGcm = new AesGcm(key); | ||
#endif | ||
_nonce = nonce; | ||
} | ||
|
||
public override void Encrypt(byte[] input, int plainTextOffset, int plainTextLength, int associatedDataOffset, int associatedDataLength, byte[] output, int cipherTextOffset) | ||
{ | ||
var cipherTextLength = plainTextLength; | ||
var plainText = new ReadOnlySpan<byte>(input, plainTextOffset, plainTextLength); | ||
var cipherText = new Span<byte>(output, cipherTextOffset, cipherTextLength); | ||
var tag = new Span<byte>(output, cipherTextOffset + cipherTextLength, TagSizeInBytes); | ||
var associatedData = new ReadOnlySpan<byte>(input, associatedDataOffset, associatedDataLength); | ||
|
||
_aesGcm.Encrypt(_nonce, plainText, cipherText, tag, associatedData); | ||
} | ||
|
||
public override void Decrypt(byte[] input, int cipherTextOffset, int cipherTextLength, int associatedDataOffset, int associatedDataLength, byte[] output, int plainTextOffset) | ||
{ | ||
var plainTextLength = cipherTextLength; | ||
var cipherText = new ReadOnlySpan<byte>(input, cipherTextOffset, cipherTextLength); | ||
var tag = new ReadOnlySpan<byte>(input, cipherTextOffset + cipherTextLength, TagSizeInBytes); | ||
var plainText = new Span<byte>(output, plainTextOffset, plainTextLength); | ||
var associatedData = new ReadOnlySpan<byte>(input, associatedDataOffset, associatedDataLength); | ||
|
||
try | ||
{ | ||
_aesGcm.Decrypt(_nonce, cipherText, tag, output, associatedData); | ||
} | ||
#if NET8_0_OR_GREATER | ||
catch (AuthenticationTagMismatchException ex) | ||
#else | ||
catch (CryptographicException ex) | ||
#endif | ||
{ | ||
throw new SshConnectionException("MAC error", DisconnectReason.MacError, ex); | ||
} | ||
} | ||
|
||
protected override void Dispose(bool disposing) | ||
{ | ||
base.Dispose(disposing); | ||
|
||
if (disposing) | ||
{ | ||
_aesGcm.Dispose(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
#endif |
48 changes: 48 additions & 0 deletions
48
src/Renci.SshNet/Security/Cryptography/Ciphers/AesGcmCipher.BouncyCastleImpl.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,48 @@ | ||
using Org.BouncyCastle.Crypto; | ||
using Org.BouncyCastle.Crypto.Engines; | ||
using Org.BouncyCastle.Crypto.Modes; | ||
using Org.BouncyCastle.Crypto.Parameters; | ||
|
||
using Renci.SshNet.Common; | ||
using Renci.SshNet.Messages.Transport; | ||
|
||
namespace Renci.SshNet.Security.Cryptography.Ciphers | ||
{ | ||
internal partial class AesGcmCipher | ||
{ | ||
private sealed class BouncyCastleImpl : Impl | ||
{ | ||
private readonly GcmBlockCipher _cipher; | ||
private readonly AeadParameters _parameters; | ||
|
||
public BouncyCastleImpl(byte[] key, byte[] nonce) | ||
{ | ||
_cipher = new GcmBlockCipher(new AesEngine()); | ||
_parameters = new AeadParameters(new KeyParameter(key), TagSizeInBytes * 8, nonce); | ||
} | ||
|
||
public override void Encrypt(byte[] input, int plainTextOffset, int plainTextLength, int associatedDataOffset, int associatedDataLength, byte[] output, int cipherTextOffset) | ||
{ | ||
_cipher.Init(forEncryption: true, _parameters); | ||
_cipher.ProcessAadBytes(input, associatedDataOffset, associatedDataLength); | ||
var cipherTextLength = _cipher.ProcessBytes(input, plainTextOffset, plainTextLength, output, cipherTextOffset); | ||
_ = _cipher.DoFinal(output, cipherTextOffset + cipherTextLength); | ||
} | ||
|
||
public override void Decrypt(byte[] input, int cipherTextOffset, int cipherTextLength, int associatedDataOffset, int associatedDataLength, byte[] output, int plainTextOffset) | ||
{ | ||
_cipher.Init(forEncryption: false, _parameters); | ||
_cipher.ProcessAadBytes(input, associatedDataOffset, associatedDataLength); | ||
var plainTextLength = _cipher.ProcessBytes(input, cipherTextOffset, cipherTextLength + TagSizeInBytes, output, plainTextOffset); | ||
try | ||
{ | ||
_ = _cipher.DoFinal(output, plainTextLength); | ||
} | ||
catch (InvalidCipherTextException ex) | ||
{ | ||
throw new SshConnectionException("MAC error", DisconnectReason.MacError, ex); | ||
} | ||
} | ||
} | ||
} | ||
} |
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
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
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.