15
15
using System ;
16
16
using System . Security . Cryptography ;
17
17
using System . Threading . Tasks ;
18
+ using Org . BouncyCastle . Crypto . Parameters ;
19
+ using Org . BouncyCastle . Crypto . Signers ;
18
20
using Xunit ;
19
21
using Yubico . Core . Tlv ;
20
22
using Yubico . YubiKey . Cryptography ;
21
23
using Yubico . YubiKey . Piv . Commands ;
22
24
using Yubico . YubiKey . TestUtilities ;
25
+ using ECPrivateKeyParameters = Yubico . YubiKey . Cryptography . ECPrivateKeyParameters ;
23
26
24
27
namespace Yubico . YubiKey . Piv
25
28
{
26
29
public class SignTests
27
30
{
31
+ [ Trait ( TraitTypes . Category , TestCategories . Simple ) ]
32
+ [ SkippableTheory ( typeof ( NotSupportedException ) , typeof ( DeviceNotFoundException ) ) ]
33
+ [ InlineData ( StandardTestDevice . Fw5 ) ]
34
+ [ InlineData ( StandardTestDevice . Fw5Fips ) ]
35
+ public void Sign_WithEd25519_RandomData_Succeeds (
36
+ StandardTestDevice testDeviceType )
37
+ {
38
+ // Arrange
39
+ var dataToSign = new byte [ 3062 ] ;
40
+ Random . Shared . NextBytes ( dataToSign ) ;
41
+
42
+ // -> Generate a Ed25519 key
43
+ using var pivSession = GetSession ( testDeviceType ) ;
44
+ var publicKeyParameters = pivSession . GenerateKeyPair ( PivSlot . Retired12 , KeyType . Ed25519 ) ;
45
+
46
+ // Act
47
+ var signature = pivSession . Sign ( PivSlot . Retired12 , dataToSign , KeyType . Ed25519 ) ;
48
+
49
+ // -> Verify the signature
50
+ var bouncyKeyParameters = GetBouncyKeyParameters ( publicKeyParameters ) ;
51
+ var verifier = new Ed25519Signer ( ) ;
52
+ verifier . Init ( false , bouncyKeyParameters ) ;
53
+ verifier . BlockUpdate ( dataToSign , 0 , dataToSign . Length ) ;
54
+
55
+ // Assert
56
+ var isValidSignature = verifier . VerifySignature ( signature ) ;
57
+ Assert . True ( isValidSignature ) ;
58
+ }
59
+
28
60
[ Trait ( TraitTypes . Category , TestCategories . Simple ) ]
29
61
[ SkippableTheory ( typeof ( NotSupportedException ) , typeof ( DeviceNotFoundException ) ) ]
30
62
[ InlineData ( StandardTestDevice . Fw5 , KeyType . RSA1024 ) ]
@@ -33,14 +65,13 @@ public class SignTests
33
65
[ InlineData ( StandardTestDevice . Fw5 , KeyType . RSA4096 ) ]
34
66
[ InlineData ( StandardTestDevice . Fw5 , KeyType . P256 ) ]
35
67
[ InlineData ( StandardTestDevice . Fw5 , KeyType . P384 ) ]
36
- [ InlineData ( StandardTestDevice . Fw5 , KeyType . Ed25519 ) ]
37
68
[ InlineData ( StandardTestDevice . Fw5Fips , KeyType . RSA1024 ) ]
38
69
[ InlineData ( StandardTestDevice . Fw5Fips , KeyType . RSA2048 ) ]
39
70
[ InlineData ( StandardTestDevice . Fw5Fips , KeyType . RSA3072 ) ]
40
71
[ InlineData ( StandardTestDevice . Fw5Fips , KeyType . RSA4096 ) ]
41
72
[ InlineData ( StandardTestDevice . Fw5Fips , KeyType . P256 ) ]
42
73
[ InlineData ( StandardTestDevice . Fw5Fips , KeyType . P384 ) ]
43
- public async ValueTask Sign_RandomData_Succeeds (
74
+ public async Task Sign_with_RSAandECDsa_Succeeds (
44
75
StandardTestDevice testDeviceType ,
45
76
KeyType keyType )
46
77
{
@@ -266,8 +297,8 @@ public void SignEcc_VerifyCSharp_CorrectObsolete(
266
297
priKey . Clear ( ) ;
267
298
}
268
299
}
269
-
270
-
300
+
301
+
271
302
[ SkippableTheory ( typeof ( DeviceNotFoundException ) ) ]
272
303
[ InlineData ( StandardTestDevice . Fw5 , KeyType . P256 , 0x94 ) ]
273
304
[ InlineData ( StandardTestDevice . Fw5Fips , KeyType . P384 , 0x95 ) ]
@@ -365,9 +396,9 @@ private async static Task<bool> ImportKey(
365
396
var testKey = TestKeys . GetTestPrivateKey ( keyType ) ;
366
397
var privateKey = AsnPrivateKeyReader . CreateKeyParameters ( testKey . EncodedKey ) ;
367
398
pivSession . ImportPrivateKey ( slotNumber , privateKey , pinPolicy , touchPolicy ) ;
368
-
399
+
369
400
await Task . Delay ( 200 ) ;
370
-
401
+
371
402
return true ;
372
403
}
373
404
@@ -415,5 +446,32 @@ private static bool ConvertEcdsaSignature(
415
446
416
447
return true ;
417
448
}
449
+
450
+ private static Ed25519PublicKeyParameters GetBouncyKeyParameters ( IPublicKeyParameters publicKeyParameters )
451
+ {
452
+ var bouncyEd25519PublicKey =
453
+ new Ed25519PublicKeyParameters (
454
+ ( ( Curve25519PublicKeyParameters ) publicKeyParameters ) . PublicPoint . ToArray ( ) ) ;
455
+ return bouncyEd25519PublicKey ;
456
+ }
457
+
458
+ private static PivSession GetSession (
459
+ StandardTestDevice testDeviceType )
460
+ {
461
+ PivSession ? pivSession = null ;
462
+ try
463
+ {
464
+ var testDevice = IntegrationTestDeviceEnumeration . GetTestDevice ( testDeviceType ) ;
465
+ pivSession = new PivSession ( testDevice ) ;
466
+ var collectorObj = new Simple39KeyCollector ( ) ;
467
+ pivSession . KeyCollector = collectorObj . Simple39KeyCollectorDelegate ;
468
+ return pivSession ;
469
+ }
470
+ catch
471
+ {
472
+ pivSession ? . Dispose ( ) ;
473
+ throw ;
474
+ }
475
+ }
418
476
}
419
477
}
0 commit comments