Skip to content

Commit 080a268

Browse files
committed
Better NETFRAMEWORK vs NETSTANDARD handling
1 parent 7e972ce commit 080a268

File tree

2 files changed

+69
-69
lines changed

2 files changed

+69
-69
lines changed

src/Renci.SshNet/Security/Cryptography/EcdsaDigitalSignature.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ public override bool Verify(byte[] input, byte[] signature)
3939
// for 521 sig_size is 132
4040
var sig_size = _key.KeyLength == 521 ? 132 : _key.KeyLength / 4;
4141
var ssh_data = new SshDataSignature(signature, sig_size);
42-
#if NETSTANDARD2_0
43-
return _key.Ecdsa.VerifyData(input, ssh_data.Signature, _key.HashAlgorithm);
44-
#else
42+
#if NETFRAMEWORK
4543
var ecdsa = (ECDsaCng)_key.Ecdsa;
4644
ecdsa.HashAlgorithm = _key.HashAlgorithm;
4745
return ecdsa.VerifyData(input, ssh_data.Signature);
46+
#else
47+
return _key.Ecdsa.VerifyData(input, ssh_data.Signature, _key.HashAlgorithm);
4848
#endif
4949
}
5050

@@ -57,12 +57,12 @@ public override bool Verify(byte[] input, byte[] signature)
5757
/// </returns>
5858
public override byte[] Sign(byte[] input)
5959
{
60-
#if NETSTANDARD2_0
61-
var signed = _key.Ecdsa.SignData(input, _key.HashAlgorithm);
62-
#else
60+
#if NETFRAMEWORK
6361
var ecdsa = (ECDsaCng)_key.Ecdsa;
6462
ecdsa.HashAlgorithm = _key.HashAlgorithm;
6563
var signed = ecdsa.SignData(input);
64+
#else
65+
var signed = _key.Ecdsa.SignData(input, _key.HashAlgorithm);
6666
#endif
6767
var ssh_data = new SshDataSignature(signed.Length);
6868
ssh_data.Signature = signed;

src/Renci.SshNet/Security/Cryptography/EcdsaKey.cs

Lines changed: 63 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class EcdsaKey : Key, IDisposable
1818
internal const string ECDSA_P384_OID_VALUE = "1.3.132.0.34"; // Also called nistP384 or secP384r1
1919
internal const string ECDSA_P521_OID_VALUE = "1.3.132.0.35"; // Also called nistP521or secP521r1
2020

21-
#if !NETSTANDARD2_0
21+
#if NETFRAMEWORK
2222
internal enum KeyBlobMagicNumber : int
2323
{
2424
BCRYPT_ECDSA_PUBLIC_P256_MAGIC = 0x31534345,
@@ -57,45 +57,45 @@ public override string ToString()
5757
return string.Format("ecdsa-sha2-nistp{0}", KeyLength);
5858
}
5959

60-
#if NETSTANDARD2_0
60+
#if NETFRAMEWORK
6161
/// <summary>
6262
/// Gets the HashAlgorithm to use
6363
/// </summary>
64-
public HashAlgorithmName HashAlgorithm
64+
public CngAlgorithm HashAlgorithm
6565
{
6666
get
6767
{
68-
switch (KeyLength)
68+
switch (Ecdsa.KeySize)
6969
{
7070
case 256:
71-
return HashAlgorithmName.SHA256;
71+
return CngAlgorithm.Sha256;
7272
case 384:
73-
return HashAlgorithmName.SHA384;
73+
return CngAlgorithm.Sha384;
7474
case 521:
75-
return HashAlgorithmName.SHA512;
75+
return CngAlgorithm.Sha512;
76+
default:
77+
throw new SshException("Unknown KeySize: " + Ecdsa.KeySize);
7678
}
77-
return HashAlgorithmName.SHA256;
7879
}
7980
}
8081
#else
8182
/// <summary>
8283
/// Gets the HashAlgorithm to use
8384
/// </summary>
84-
public CngAlgorithm HashAlgorithm
85+
public HashAlgorithmName HashAlgorithm
8586
{
8687
get
8788
{
88-
switch (Ecdsa.KeySize)
89+
switch (KeyLength)
8990
{
9091
case 256:
91-
return CngAlgorithm.Sha256;
92+
return HashAlgorithmName.SHA256;
9293
case 384:
93-
return CngAlgorithm.Sha384;
94+
return HashAlgorithmName.SHA384;
9495
case 521:
95-
return CngAlgorithm.Sha512;
96-
default:
97-
throw new SshException("Unknown KeySize: " + Ecdsa.KeySize);
96+
return HashAlgorithmName.SHA512;
9897
}
98+
return HashAlgorithmName.SHA256;
9999
}
100100
}
101101
#endif
@@ -144,28 +144,7 @@ public override BigInteger[] Public
144144
byte[] curve;
145145
byte[] qx;
146146
byte[] qy;
147-
#if NETSTANDARD2_0
148-
var parameter = Ecdsa.ExportParameters(false);
149-
qx = parameter.Q.X;
150-
qy = parameter.Q.Y;
151-
switch (parameter.Curve.Oid.FriendlyName)
152-
{
153-
case "ECDSA_P256":
154-
case "nistP256":
155-
curve = Encoding.ASCII.GetBytes("nistp256");
156-
break;
157-
case "ECDSA_P384":
158-
case "nistP384":
159-
curve = Encoding.ASCII.GetBytes("nistp384");
160-
break;
161-
case "ECDSA_P521":
162-
case "nistP521":
163-
curve = Encoding.ASCII.GetBytes("nistp521");
164-
break;
165-
default:
166-
throw new SshException("Unexpected Curve Name: " + parameter.Curve.Oid.FriendlyName);
167-
}
168-
#else
147+
#if NETFRAMEWORK
169148
var blob = key.Export(CngKeyBlobFormat.EccPublicBlob);
170149

171150
KeyBlobMagicNumber magic;
@@ -191,6 +170,27 @@ public override BigInteger[] Public
191170
default:
192171
throw new SshException("Unexpected Curve Magic: " + magic);
193172
}
173+
#else
174+
var parameter = Ecdsa.ExportParameters(false);
175+
qx = parameter.Q.X;
176+
qy = parameter.Q.Y;
177+
switch (parameter.Curve.Oid.FriendlyName)
178+
{
179+
case "ECDSA_P256":
180+
case "nistP256":
181+
curve = Encoding.ASCII.GetBytes("nistp256");
182+
break;
183+
case "ECDSA_P384":
184+
case "nistP384":
185+
curve = Encoding.ASCII.GetBytes("nistp384");
186+
break;
187+
case "ECDSA_P521":
188+
case "nistP521":
189+
curve = Encoding.ASCII.GetBytes("nistp521");
190+
break;
191+
default:
192+
throw new SshException("Unexpected Curve Name: " + parameter.Curve.Oid.FriendlyName);
193+
}
194194
#endif
195195
// Make ECPoint from x and y
196196
// Prepend 04 (uncompressed format) + qx-bytes + qy-bytes
@@ -283,32 +283,7 @@ public EcdsaKey(byte[] data)
283283

284284
private void Import(string curve_oid, byte[] publickey, byte[] privatekey)
285285
{
286-
#if NETSTANDARD2_0
287-
var curve = ECCurve.CreateFromValue(curve_oid);
288-
var parameter = new ECParameters
289-
{
290-
Curve = curve
291-
};
292-
293-
// ECPoint as BigInteger(2)
294-
var cord_size = (publickey.Length - 1) / 2;
295-
var qx = new byte[cord_size];
296-
Buffer.BlockCopy(publickey, 1, qx, 0, qx.Length);
297-
298-
var qy = new byte[cord_size];
299-
Buffer.BlockCopy(publickey, cord_size + 1, qy, 0, qy.Length);
300-
301-
parameter.Q.X = qx;
302-
parameter.Q.Y = qy;
303-
304-
if (privatekey != null)
305-
{
306-
parameter.D = privatekey.TrimLeadingZeros().Pad(cord_size);
307-
PrivateKey = parameter.D;
308-
}
309-
310-
Ecdsa = ECDsa.Create(parameter);
311-
#else
286+
#if NETFRAMEWORK
312287
var curve_magic = KeyBlobMagicNumber.BCRYPT_ECDH_PRIVATE_GENERIC_MAGIC;
313288
switch (GetCurveName(curve_oid))
314289
{
@@ -366,6 +341,31 @@ private void Import(string curve_oid, byte[] publickey, byte[] privatekey)
366341
key = CngKey.Import(blob, privatekey == null ? CngKeyBlobFormat.EccPublicBlob : CngKeyBlobFormat.EccPrivateBlob);
367342

368343
Ecdsa = new ECDsaCng(key);
344+
#else
345+
var curve = ECCurve.CreateFromValue(curve_oid);
346+
var parameter = new ECParameters
347+
{
348+
Curve = curve
349+
};
350+
351+
// ECPoint as BigInteger(2)
352+
var cord_size = (publickey.Length - 1) / 2;
353+
var qx = new byte[cord_size];
354+
Buffer.BlockCopy(publickey, 1, qx, 0, qx.Length);
355+
356+
var qy = new byte[cord_size];
357+
Buffer.BlockCopy(publickey, cord_size + 1, qy, 0, qy.Length);
358+
359+
parameter.Q.X = qx;
360+
parameter.Q.Y = qy;
361+
362+
if (privatekey != null)
363+
{
364+
parameter.D = privatekey.TrimLeadingZeros().Pad(cord_size);
365+
PrivateKey = parameter.D;
366+
}
367+
368+
Ecdsa = ECDsa.Create(parameter);
369369
#endif
370370
}
371371

0 commit comments

Comments
 (0)