Skip to content

Commit 79932f3

Browse files
committed
Some refactor
1 parent bd9db16 commit 79932f3

File tree

4 files changed

+18
-71
lines changed

4 files changed

+18
-71
lines changed

src/Renci.SshNet/Security/KeyExchangeECCurve25519.cs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ namespace Renci.SshNet.Security
66
{
77
internal partial class KeyExchangeECCurve25519 : KeyExchangeEC
88
{
9+
#pragma warning disable SA1401 // Fields should be private
910
#if NET
10-
private Impl _impl;
11+
protected Impl _impl;
1112
#else
12-
private BouncyCastleImpl _impl;
13+
protected BouncyCastleImpl _impl;
1314
#endif
15+
#pragma warning restore SA1401 // Fields should be private
1416

1517
/// <summary>
1618
/// Gets algorithm name.
@@ -35,17 +37,6 @@ protected override int HashSize
3537
public override void Start(Session session, KeyExchangeInitMessage message, bool sendClientInitMessage)
3638
{
3739
base.Start(session, message, sendClientInitMessage);
38-
StartImpl();
39-
}
40-
41-
/// <summary>
42-
/// The implementation of start key exchange algorithm.
43-
/// </summary>
44-
protected virtual void StartImpl()
45-
{
46-
Session.RegisterMessage("SSH_MSG_KEX_ECDH_REPLY");
47-
48-
Session.KeyExchangeEcdhReplyMessageReceived += Session_KeyExchangeEcdhReplyMessageReceived;
4940
#if NET
5041
if (System.OperatingSystem.IsWindowsVersionAtLeast(10))
5142
{
@@ -58,6 +49,18 @@ protected virtual void StartImpl()
5849
_impl = new BouncyCastleImpl();
5950
}
6051

52+
StartImpl();
53+
}
54+
55+
/// <summary>
56+
/// The implementation of start key exchange algorithm.
57+
/// </summary>
58+
protected virtual void StartImpl()
59+
{
60+
Session.RegisterMessage("SSH_MSG_KEX_ECDH_REPLY");
61+
62+
Session.KeyExchangeEcdhReplyMessageReceived += Session_KeyExchangeEcdhReplyMessageReceived;
63+
6164
_clientExchangeValue = _impl.GenerateClientECPoint();
6265

6366
SendMessage(new KeyExchangeEcdhInitMessage(_clientExchangeValue));

src/Renci.SshNet/Security/KeyExchangeECDH.BouncyCastleImpl.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ internal abstract partial class KeyExchangeECDH
1212
private sealed class BouncyCastleImpl : Impl
1313
{
1414
private readonly ECDomainParameters _domainParameters;
15-
private readonly ECDHCBasicAgreement _keyAgreement;
15+
private readonly ECDHBasicAgreement _keyAgreement;
1616

1717
public BouncyCastleImpl(X9ECParameters curveParameters)
1818
{
1919
_domainParameters = new ECDomainParameters(curveParameters);
20-
_keyAgreement = new ECDHCBasicAgreement();
20+
_keyAgreement = new ECDHBasicAgreement();
2121
}
2222

2323
public override byte[] GenerateClientECPoint()

src/Renci.SshNet/Security/KeyExchangeMLKem768X25519Sha256.cs

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@ namespace Renci.SshNet.Security
1414
internal sealed class KeyExchangeMLKem768X25519Sha256 : KeyExchangeECCurve25519
1515
{
1616
private MLKemDecapsulator _mlkemDecapsulator;
17-
#if NET
18-
private Impl _impl;
19-
#else
20-
private BouncyCastleImpl _impl;
21-
#endif
2217

2318
/// <summary>
2419
/// Gets algorithm name.
@@ -54,17 +49,6 @@ protected override void StartImpl()
5449
_mlkemDecapsulator.Init(mlkem768KeyPair.Private);
5550

5651
var mlkem768PublicKey = ((MLKemPublicKeyParameters)mlkem768KeyPair.Public).GetEncoded();
57-
#if NET
58-
if (System.OperatingSystem.IsWindowsVersionAtLeast(10))
59-
{
60-
var curve = System.Security.Cryptography.ECCurve.CreateFromFriendlyName("Curve25519");
61-
_impl = new BclImpl(curve);
62-
}
63-
else
64-
#endif
65-
{
66-
_impl = new BouncyCastleImpl();
67-
}
6852

6953
var x25519PublicKey = _impl.GenerateClientECPoint();
7054

@@ -131,16 +115,5 @@ private void HandleServerHybridReply(byte[] hostKey, byte[] serverExchangeValue,
131115

132116
SharedKey = CryptoAbstraction.HashSHA256(mlkemSecret.Concat(x25519Agreement));
133117
}
134-
135-
/// <inheritdoc/>
136-
protected override void Dispose(bool disposing)
137-
{
138-
base.Dispose(disposing);
139-
140-
if (disposing)
141-
{
142-
_impl?.Dispose();
143-
}
144-
}
145118
}
146119
}

src/Renci.SshNet/Security/KeyExchangeSNtruP761X25519Sha512.cs

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@ namespace Renci.SshNet.Security
1313
internal sealed class KeyExchangeSNtruP761X25519Sha512 : KeyExchangeECCurve25519
1414
{
1515
private SNtruPrimeKemExtractor _sntrup761Extractor;
16-
#if NET
17-
private Impl _impl;
18-
#else
19-
private BouncyCastleImpl _impl;
20-
#endif
2116

2217
/// <summary>
2318
/// Gets algorithm name.
@@ -52,24 +47,11 @@ protected override void StartImpl()
5247
_sntrup761Extractor = new SNtruPrimeKemExtractor((SNtruPrimePrivateKeyParameters)sntrup761KeyPair.Private);
5348

5449
var sntrup761PublicKey = ((SNtruPrimePublicKeyParameters)sntrup761KeyPair.Public).GetEncoded();
55-
#if NET
56-
if (System.OperatingSystem.IsWindowsVersionAtLeast(10))
57-
{
58-
var curve = System.Security.Cryptography.ECCurve.CreateFromFriendlyName("Curve25519");
59-
_impl = new BclImpl(curve);
60-
}
61-
else
62-
#endif
63-
{
64-
_impl = new BouncyCastleImpl();
65-
}
6650

6751
var x25519PublicKey = _impl.GenerateClientECPoint();
6852

6953
_clientExchangeValue = sntrup761PublicKey.Concat(x25519PublicKey);
7054

71-
_clientExchangeValue = sntrup761PublicKey.Concat(x25519PublicKey);
72-
7355
SendMessage(new KeyExchangeEcdhInitMessage(_clientExchangeValue));
7456
}
7557

@@ -131,16 +113,5 @@ private void HandleServerEcdhReply(byte[] hostKey, byte[] serverExchangeValue, b
131113

132114
SharedKey = CryptoAbstraction.HashSHA512(sntrup761Secret.Concat(x25519Agreement));
133115
}
134-
135-
/// <inheritdoc/>
136-
protected override void Dispose(bool disposing)
137-
{
138-
base.Dispose(disposing);
139-
140-
if (disposing)
141-
{
142-
_impl?.Dispose();
143-
}
144-
}
145116
}
146117
}

0 commit comments

Comments
 (0)