@@ -18,7 +18,7 @@ public class EcdsaKey : Key, IDisposable
18
18
internal const string ECDSA_P384_OID_VALUE = "1.3.132.0.34" ; // Also called nistP384 or secP384r1
19
19
internal const string ECDSA_P521_OID_VALUE = "1.3.132.0.35" ; // Also called nistP521or secP521r1
20
20
21
- #if ! NETSTANDARD2_0
21
+ #if NETFRAMEWORK
22
22
internal enum KeyBlobMagicNumber : int
23
23
{
24
24
BCRYPT_ECDSA_PUBLIC_P256_MAGIC = 0x31534345 ,
@@ -57,45 +57,45 @@ public override string ToString()
57
57
return string . Format ( "ecdsa-sha2-nistp{0}" , KeyLength ) ;
58
58
}
59
59
60
- #if NETSTANDARD2_0
60
+ #if NETFRAMEWORK
61
61
/// <summary>
62
62
/// Gets the HashAlgorithm to use
63
63
/// </summary>
64
- public HashAlgorithmName HashAlgorithm
64
+ public CngAlgorithm HashAlgorithm
65
65
{
66
66
get
67
67
{
68
- switch ( KeyLength )
68
+ switch ( Ecdsa . KeySize )
69
69
{
70
70
case 256 :
71
- return HashAlgorithmName . SHA256 ;
71
+ return CngAlgorithm . Sha256 ;
72
72
case 384 :
73
- return HashAlgorithmName . SHA384 ;
73
+ return CngAlgorithm . Sha384 ;
74
74
case 521 :
75
- return HashAlgorithmName . SHA512 ;
75
+ return CngAlgorithm . Sha512 ;
76
+ default :
77
+ throw new SshException ( "Unknown KeySize: " + Ecdsa . KeySize ) ;
76
78
}
77
- return HashAlgorithmName . SHA256 ;
78
79
}
79
80
}
80
81
#else
81
82
/// <summary>
82
83
/// Gets the HashAlgorithm to use
83
84
/// </summary>
84
- public CngAlgorithm HashAlgorithm
85
+ public HashAlgorithmName HashAlgorithm
85
86
{
86
87
get
87
88
{
88
- switch ( Ecdsa . KeySize )
89
+ switch ( KeyLength )
89
90
{
90
91
case 256 :
91
- return CngAlgorithm . Sha256 ;
92
+ return HashAlgorithmName . SHA256 ;
92
93
case 384 :
93
- return CngAlgorithm . Sha384 ;
94
+ return HashAlgorithmName . SHA384 ;
94
95
case 521 :
95
- return CngAlgorithm . Sha512 ;
96
- default :
97
- throw new SshException ( "Unknown KeySize: " + Ecdsa . KeySize ) ;
96
+ return HashAlgorithmName . SHA512 ;
98
97
}
98
+ return HashAlgorithmName . SHA256 ;
99
99
}
100
100
}
101
101
#endif
@@ -144,28 +144,7 @@ public override BigInteger[] Public
144
144
byte [ ] curve ;
145
145
byte [ ] qx ;
146
146
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
169
148
var blob = key . Export ( CngKeyBlobFormat . EccPublicBlob ) ;
170
149
171
150
KeyBlobMagicNumber magic ;
@@ -191,6 +170,27 @@ public override BigInteger[] Public
191
170
default :
192
171
throw new SshException ( "Unexpected Curve Magic: " + magic ) ;
193
172
}
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
+ }
194
194
#endif
195
195
// Make ECPoint from x and y
196
196
// Prepend 04 (uncompressed format) + qx-bytes + qy-bytes
@@ -283,32 +283,7 @@ public EcdsaKey(byte[] data)
283
283
284
284
private void Import ( string curve_oid , byte [ ] publickey , byte [ ] privatekey )
285
285
{
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
312
287
var curve_magic = KeyBlobMagicNumber . BCRYPT_ECDH_PRIVATE_GENERIC_MAGIC ;
313
288
switch ( GetCurveName ( curve_oid ) )
314
289
{
@@ -366,6 +341,31 @@ private void Import(string curve_oid, byte[] publickey, byte[] privatekey)
366
341
key = CngKey . Import ( blob , privatekey == null ? CngKeyBlobFormat . EccPublicBlob : CngKeyBlobFormat . EccPrivateBlob ) ;
367
342
368
343
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 ) ;
369
369
#endif
370
370
}
371
371
0 commit comments