-
-
Notifications
You must be signed in to change notification settings - Fork 955
Use BCL ECDiffieHellman for KeyExchange instead of BouncyCastle (.NET 8.0 onward only) #1371
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
33 commits
Select commit
Hold shift + click to select a range
901fd5a
Use BCL ECDiffieHellman for KeyExchange (.NET 8.0 onward only)
scott-xu f78488c
Merge branch 'develop' into ecdh-bcl
scott-xu 4b8be40
Add back an empty line
scott-xu 8e6d079
Merge branch 'ecdh-bcl' of https://github.com/scott-xu/SSH.NET into e…
scott-xu 61e44a4
Remove the BouncyCastle dependency when target .NET 8.0 onward.
scott-xu 767e692
Merge branch 'develop' into ecdh-bcl
scott-xu 4628a73
Run KeyExchangeAlgorithmTests for .NET 6.0
scott-xu 23c4ac2
Merge branch 'ecdh-bcl' of https://github.com/scott-xu/SSH.NET into e…
scott-xu 23a1dd3
Build Renci.SshNet.IntegrationTests.csproj for net6.0
scott-xu 182f586
Update filter
scott-xu fed031b
Merge branch 'develop' into ecdh-bcl
scott-xu cfd950f
Merge branch 'develop' into ecdh-bcl
scott-xu 8392edb
Merge branch 'develop' into ecdh-bcl
scott-xu 8b42e59
Merge branch 'develop' of https://github.com/scott-xu/SSH.NET into ec…
scott-xu b35ffdf
Merge branch 'ecdh-bcl' of https://github.com/scott-xu/SSH.NET into e…
scott-xu 4b73a96
Merge branch 'develop' into ecdh-bcl
scott-xu 5502918
Add back BouncyCastle as fallback
scott-xu f76ffeb
Merge branch 'develop' into ecdh-bcl
scott-xu a1b00e3
Add back the missing `SendMessage`
scott-xu 6ed28ae
Merge branch 'ecdh-bcl' of https://github.com/scott-xu/SSH.NET into e…
scott-xu d754e93
Merge branch 'develop' into ecdh-bcl
scott-xu 08c9594
Merge branch 'develop' of https://github.com/scott-xu/SSH.NET into ec…
scott-xu 4495c57
Merge branch 'ecdh-bcl' of https://github.com/scott-xu/SSH.NET into e…
scott-xu bd093c7
Merge branch 'develop' into ecdh-bcl
scott-xu 57bf19d
Merge branch 'develop' into ecdh-bcl
WojciechNagorski 13dd7e1
Run ECDH KEX integration tests under .NET48
scott-xu 9fa0c14
Merge branch 'develop' into ecdh-bcl
scott-xu 344b744
Merge branch 'develop' into ecdh-bcl
scott-xu 385e087
Use SshNamedCurves instead of SecNamedCurves for BouncyCastle.
scott-xu 387e6da
typo
scott-xu 73c9446
Fix build
scott-xu db0a98e
Use System.Security.Cryptography namespace if NET8_0_OR_GREATER;
scott-xu 2441f77
Separate BCL and BouncyCastle implementation
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#if NET8_0_OR_GREATER | ||
using System; | ||
using System.Security.Cryptography; | ||
|
||
namespace Renci.SshNet.Security | ||
{ | ||
internal abstract partial class KeyExchangeECDH | ||
{ | ||
private sealed class BclImpl : Impl | ||
{ | ||
private readonly ECCurve _curve; | ||
private readonly ECDiffieHellman _clientECDH; | ||
|
||
public BclImpl(ECCurve curve) | ||
{ | ||
_curve = curve; | ||
_clientECDH = ECDiffieHellman.Create(); | ||
} | ||
|
||
public override byte[] GenerateClientECPoint() | ||
{ | ||
_clientECDH.GenerateKey(_curve); | ||
|
||
var q = _clientECDH.PublicKey.ExportParameters().Q; | ||
|
||
return EncodeECPoint(q); | ||
} | ||
|
||
public override byte[] CalculateAgreement(byte[] serverECPoint) | ||
{ | ||
var q = DecodeECPoint(serverECPoint); | ||
|
||
var parameters = new ECParameters | ||
{ | ||
Curve = _curve, | ||
Q = q, | ||
}; | ||
|
||
using var serverECDH = ECDiffieHellman.Create(parameters); | ||
|
||
return _clientECDH.DeriveRawSecretAgreement(serverECDH.PublicKey); | ||
} | ||
|
||
private static byte[] EncodeECPoint(ECPoint point) | ||
{ | ||
var q = new byte[1 + point.X.Length + point.Y.Length]; | ||
q[0] = 0x04; | ||
Buffer.BlockCopy(point.X, 0, q, 1, point.X.Length); | ||
Buffer.BlockCopy(point.Y, 0, q, point.X.Length + 1, point.Y.Length); | ||
|
||
return q; | ||
} | ||
|
||
private static ECPoint DecodeECPoint(byte[] q) | ||
{ | ||
var cordSize = (q.Length - 1) / 2; | ||
var x = new byte[cordSize]; | ||
var y = new byte[cordSize]; | ||
Buffer.BlockCopy(q, 1, x, 0, x.Length); | ||
Buffer.BlockCopy(q, cordSize + 1, y, 0, y.Length); | ||
|
||
return new ECPoint { X = x, Y = y }; | ||
} | ||
|
||
protected override void Dispose(bool disposing) | ||
{ | ||
base.Dispose(disposing); | ||
|
||
if (disposing) | ||
{ | ||
_clientECDH.Dispose(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
#endif |
44 changes: 44 additions & 0 deletions
44
src/Renci.SshNet/Security/KeyExchangeECDH.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,44 @@ | ||
using Org.BouncyCastle.Asn1.X9; | ||
using Org.BouncyCastle.Crypto.Agreement; | ||
using Org.BouncyCastle.Crypto.Generators; | ||
using Org.BouncyCastle.Crypto.Parameters; | ||
|
||
using Renci.SshNet.Abstractions; | ||
|
||
namespace Renci.SshNet.Security | ||
{ | ||
internal abstract partial class KeyExchangeECDH | ||
{ | ||
private sealed class BouncyCastleImpl : Impl | ||
{ | ||
private readonly ECDomainParameters _domainParameters; | ||
private readonly ECDHCBasicAgreement _keyAgreement; | ||
|
||
public BouncyCastleImpl(X9ECParameters curveParameters) | ||
{ | ||
_domainParameters = new ECDomainParameters(curveParameters); | ||
_keyAgreement = new ECDHCBasicAgreement(); | ||
} | ||
|
||
public override byte[] GenerateClientECPoint() | ||
{ | ||
var g = new ECKeyPairGenerator(); | ||
g.Init(new ECKeyGenerationParameters(_domainParameters, CryptoAbstraction.SecureRandom)); | ||
|
||
var aKeyPair = g.GenerateKeyPair(); | ||
_keyAgreement.Init(aKeyPair.Private); | ||
|
||
return ((ECPublicKeyParameters)aKeyPair.Public).Q.GetEncoded(); | ||
} | ||
|
||
public override byte[] CalculateAgreement(byte[] serverECPoint) | ||
{ | ||
var c = _domainParameters.Curve; | ||
var q = c.DecodePoint(serverECPoint); | ||
var publicKey = new ECPublicKeyParameters("ECDH", q, _domainParameters); | ||
|
||
return _keyAgreement.CalculateAgreement(publicKey).ToByteArray(); | ||
} | ||
} | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was going to suggest this but I imagine our style settings might have something to say about it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the style setting doesn't like it.
Here's the same situation: https://github.com/sshnet/SSH.NET/pull/1450/files#diff-4d37cacac3721823c238e98586d95622950554a40aff236a571339d813a53794R67-R72