Skip to content

Commit 85c5e34

Browse files
committed
refactor: rename test methods for clarity and consistency, and extract curve retrieval logic
1 parent b0a8b13 commit 85c5e34

File tree

1 file changed

+50
-36
lines changed

1 file changed

+50
-36
lines changed

Yubico.YubiKey/tests/unit/Yubico/YubiKey/Cryptography/EcdsaVerifyTests.cs

Lines changed: 50 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace Yubico.YubiKey.Cryptography
2525
public class EcdsaVerifyTests
2626
{
2727
[Fact]
28-
public void PivKey_Verify_Succeeds()
28+
public void PivKey_VerifDigestedDatay_Succeeds()
2929
{
3030
var pubKey = new PivEccPublicKey(GetEncodedPoint());
3131
byte[] digest = GetDigest();
@@ -37,7 +37,7 @@ public void PivKey_Verify_Succeeds()
3737
}
3838

3939
[Fact]
40-
public void CoseKey_Verify_Succeeds()
40+
public void CoseKey_VeriDigestedDatafy_Succeeds()
4141
{
4242
var pubKey = new CoseEcPublicKey(CoseEcCurve.P256, GetX(), GetY());
4343
byte[] digest = GetDigest();
@@ -52,19 +52,12 @@ public void CoseKey_Verify_Succeeds()
5252
[InlineData(KeyDefinitions.KeyType.P256)]
5353
[InlineData(KeyDefinitions.KeyType.P384)]
5454
[InlineData(KeyDefinitions.KeyType.P521)]
55-
public void CoseKey_Verify_WithMultipleCurves_Succeeds(KeyDefinitions.KeyType keyType)
55+
public void CoseKey_VeriDigestedDatafy_WithMultipleCurves_Succeeds(KeyDefinitions.KeyType keyType)
5656
{
5757
// Arrange
58-
var keyDefinition = KeyDefinitions.GetByKeyType(keyType);
59-
var (eccCurve, coseCurve) = keyDefinition.Type switch
60-
{
61-
KeyDefinitions.KeyType.P256 => (ECCurve.NamedCurves.nistP256, CoseEcCurve.P256),
62-
KeyDefinitions.KeyType.P384 => (ECCurve.NamedCurves.nistP384, CoseEcCurve.P384),
63-
KeyDefinitions.KeyType.P521 => (ECCurve.NamedCurves.nistP521, CoseEcCurve.P521),
64-
_ => throw new ArgumentException("Unknown curve")
65-
};
58+
(var ecCurve, var coseCurve) = GetCurves(keyType);
6659

67-
var ecdsa = ECDsa.Create(eccCurve);
60+
var ecdsa = ECDsa.Create(ecCurve);
6861
var digest = ecdsa.SignData(Encoding.GetEncoding("UTF-8").GetBytes("Hello World"), HashAlgorithmName.SHA256);
6962
var signature = ecdsa.SignHash(digest, DSASignatureFormat.Rfc3279DerSequence);
7063
var ecParams = ecdsa.ExportParameters(false);
@@ -79,7 +72,7 @@ public void CoseKey_Verify_WithMultipleCurves_Succeeds(KeyDefinitions.KeyType ke
7972
}
8073

8174
[Fact]
82-
public void Cert_Verify_Succeeds()
75+
public void Cert_Verify_DigestedDataSucceeds()
8376
{
8477
var pubKey = new X509Certificate2(GetCert());
8578
byte[] digest = GetDigest();
@@ -90,7 +83,7 @@ public void Cert_Verify_Succeeds()
9083
}
9184

9285
[Fact]
93-
public void ECDsa_Verify_Succeeds()
86+
public void ECDsa_VerifyDigestedData_Succeeds()
9487
{
9588
var eccCurve = ECCurve.CreateFromValue(KeyDefinitions.KeyOids.P256);
9689
var eccParams = new ECParameters
@@ -115,19 +108,11 @@ public void ECDsa_Verify_Succeeds()
115108
[InlineData(KeyDefinitions.KeyType.P256)]
116109
[InlineData(KeyDefinitions.KeyType.P384)]
117110
[InlineData(KeyDefinitions.KeyType.P521)]
118-
public void ECDsa_Verify_WithIeeeFormat_Succeeds(KeyDefinitions.KeyType keyType)
111+
public void ECDsa_VerifyDigestedData_WithIeeeFormat_Succeeds(KeyDefinitions.KeyType keyType)
119112
{
120113
// Arrange
121-
var keyDefinition = KeyDefinitions.GetByKeyType(keyType);
122-
var eccCurve = keyDefinition.Type switch
123-
{
124-
KeyDefinitions.KeyType.P256 => ECCurve.NamedCurves.nistP256,
125-
KeyDefinitions.KeyType.P384 => ECCurve.NamedCurves.nistP384,
126-
KeyDefinitions.KeyType.P521 => ECCurve.NamedCurves.nistP521,
127-
_ => throw new ArgumentException("Unknown curve")
128-
};
129-
130-
var pubKey = ECDsa.Create(eccCurve);
114+
(var ecCurve, _) = GetCurves(keyType);
115+
var pubKey = ECDsa.Create(ecCurve);
131116
byte[] digest = pubKey.SignData(Encoding.GetEncoding("UTF-8").GetBytes("Hello World"), HashAlgorithmName.SHA256);
132117
byte[] signature = pubKey.SignHash(digest, DSASignatureFormat.IeeeP1363FixedFieldConcatenation);
133118

@@ -143,19 +128,11 @@ public void ECDsa_Verify_WithIeeeFormat_Succeeds(KeyDefinitions.KeyType keyType)
143128
[InlineData(KeyDefinitions.KeyType.P256)]
144129
[InlineData(KeyDefinitions.KeyType.P384)]
145130
[InlineData(KeyDefinitions.KeyType.P521)]
146-
public void ECDsa_Verify_WithDerFormat_Succeeds(KeyDefinitions.KeyType keyType)
131+
public void ECDsa_VerifyDigestedData_WithDerFormat_Succeeds(KeyDefinitions.KeyType keyType)
147132
{
148133
// Arrange
149-
var keyDefinition = KeyDefinitions.GetByKeyType(keyType);
150-
var eccCurve = keyDefinition.Type switch
151-
{
152-
KeyDefinitions.KeyType.P256 => ECCurve.NamedCurves.nistP256,
153-
KeyDefinitions.KeyType.P384 => ECCurve.NamedCurves.nistP384,
154-
KeyDefinitions.KeyType.P521 => ECCurve.NamedCurves.nistP521,
155-
_ => throw new ArgumentException("Unknown curve")
156-
};
157-
158-
var pubKey = ECDsa.Create(eccCurve);
134+
(var ecCurve, _) = GetCurves(keyType);
135+
var pubKey = ECDsa.Create(ecCurve);
159136
byte[] digest = pubKey.SignData(Encoding.GetEncoding("UTF-8").GetBytes("Hello World"), HashAlgorithmName.SHA256);
160137
byte[] signature = pubKey.SignHash(digest, DSASignatureFormat.Rfc3279DerSequence);
161138

@@ -167,6 +144,29 @@ public void ECDsa_Verify_WithDerFormat_Succeeds(KeyDefinitions.KeyType keyType)
167144
Assert.True(isVerified);
168145
}
169146

147+
148+
[Theory]
149+
[InlineData(KeyDefinitions.KeyType.P256)]
150+
[InlineData(KeyDefinitions.KeyType.P384)]
151+
[InlineData(KeyDefinitions.KeyType.P521)]
152+
public void ECDsa_VerifyData_WithDerFormat_Succeeds(KeyDefinitions.KeyType keyType)
153+
{
154+
// Arrange
155+
(var eccCurve, _) = GetCurves(keyType);
156+
var pubKey = ECDsa.Create(eccCurve);
157+
byte[] data = Encoding.GetEncoding("UTF-8").GetBytes("Hello World");
158+
byte[] digest = pubKey.SignData(data, HashAlgorithmName.SHA256);
159+
byte[] signature = pubKey.SignHash(digest, DSASignatureFormat.Rfc3279DerSequence);
160+
161+
// Act
162+
using var verifier = new EcdsaVerify(pubKey);
163+
bool isVerified = verifier.VerifyData(data, signature, true);
164+
165+
// Assert
166+
Assert.True(isVerified);
167+
}
168+
169+
170170
[Fact]
171171
public void EncodedKey_Verify_Succeeds()
172172
{
@@ -192,6 +192,20 @@ private byte[] GetEncodedPoint()
192192
return encoding;
193193
}
194194

195+
private static (ECCurve ecCurve, CoseEcCurve coseCurve) GetCurves(KeyDefinitions.KeyType keyType)
196+
{
197+
var keyDefinition = KeyDefinitions.GetByKeyType(keyType);
198+
var (eccCurve, coseCurve) = keyDefinition.Type switch
199+
{
200+
KeyDefinitions.KeyType.P256 => (ECCurve.NamedCurves.nistP256, CoseEcCurve.P256),
201+
KeyDefinitions.KeyType.P384 => (ECCurve.NamedCurves.nistP384, CoseEcCurve.P384),
202+
KeyDefinitions.KeyType.P521 => (ECCurve.NamedCurves.nistP521, CoseEcCurve.P521),
203+
_ => throw new ArgumentException("Unknown curve")
204+
};
205+
206+
return (eccCurve, coseCurve);
207+
}
208+
195209

196210
private byte[] GetX() => new byte[]
197211
{

0 commit comments

Comments
 (0)