Skip to content

Commit ecab1e9

Browse files
committed
tests: add test to verify ed25519 signature using gen key
1 parent 3d816f3 commit ecab1e9

File tree

1 file changed

+64
-6
lines changed
  • Yubico.YubiKey/tests/integration/Yubico/YubiKey/Piv

1 file changed

+64
-6
lines changed

Yubico.YubiKey/tests/integration/Yubico/YubiKey/Piv/SignTests.cs

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,48 @@
1515
using System;
1616
using System.Security.Cryptography;
1717
using System.Threading.Tasks;
18+
using Org.BouncyCastle.Crypto.Parameters;
19+
using Org.BouncyCastle.Crypto.Signers;
1820
using Xunit;
1921
using Yubico.Core.Tlv;
2022
using Yubico.YubiKey.Cryptography;
2123
using Yubico.YubiKey.Piv.Commands;
2224
using Yubico.YubiKey.TestUtilities;
25+
using ECPrivateKeyParameters = Yubico.YubiKey.Cryptography.ECPrivateKeyParameters;
2326

2427
namespace Yubico.YubiKey.Piv
2528
{
2629
public class SignTests
2730
{
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+
2860
[Trait(TraitTypes.Category, TestCategories.Simple)]
2961
[SkippableTheory(typeof(NotSupportedException), typeof(DeviceNotFoundException))]
3062
[InlineData(StandardTestDevice.Fw5, KeyType.RSA1024)]
@@ -33,14 +65,13 @@ public class SignTests
3365
[InlineData(StandardTestDevice.Fw5, KeyType.RSA4096)]
3466
[InlineData(StandardTestDevice.Fw5, KeyType.P256)]
3567
[InlineData(StandardTestDevice.Fw5, KeyType.P384)]
36-
[InlineData(StandardTestDevice.Fw5, KeyType.Ed25519)]
3768
[InlineData(StandardTestDevice.Fw5Fips, KeyType.RSA1024)]
3869
[InlineData(StandardTestDevice.Fw5Fips, KeyType.RSA2048)]
3970
[InlineData(StandardTestDevice.Fw5Fips, KeyType.RSA3072)]
4071
[InlineData(StandardTestDevice.Fw5Fips, KeyType.RSA4096)]
4172
[InlineData(StandardTestDevice.Fw5Fips, KeyType.P256)]
4273
[InlineData(StandardTestDevice.Fw5Fips, KeyType.P384)]
43-
public async ValueTask Sign_RandomData_Succeeds(
74+
public async Task Sign_with_RSAandECDsa_Succeeds(
4475
StandardTestDevice testDeviceType,
4576
KeyType keyType)
4677
{
@@ -266,8 +297,8 @@ public void SignEcc_VerifyCSharp_CorrectObsolete(
266297
priKey.Clear();
267298
}
268299
}
269-
270-
300+
301+
271302
[SkippableTheory(typeof(DeviceNotFoundException))]
272303
[InlineData(StandardTestDevice.Fw5, KeyType.P256, 0x94)]
273304
[InlineData(StandardTestDevice.Fw5Fips, KeyType.P384, 0x95)]
@@ -365,9 +396,9 @@ private async static Task<bool> ImportKey(
365396
var testKey = TestKeys.GetTestPrivateKey(keyType);
366397
var privateKey = AsnPrivateKeyReader.CreateKeyParameters(testKey.EncodedKey);
367398
pivSession.ImportPrivateKey(slotNumber, privateKey, pinPolicy, touchPolicy);
368-
399+
369400
await Task.Delay(200);
370-
401+
371402
return true;
372403
}
373404

@@ -415,5 +446,32 @@ private static bool ConvertEcdsaSignature(
415446

416447
return true;
417448
}
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+
}
418476
}
419477
}

0 commit comments

Comments
 (0)