Skip to content

Commit 1e5923a

Browse files
committed
tests: rename test methods for clarity and fixed tests
1 parent e2f7241 commit 1e5923a

File tree

1 file changed

+53
-32
lines changed

1 file changed

+53
-32
lines changed

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

Lines changed: 53 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,24 @@ public void CoseKey_VeriDigestedDatafy_Succeeds()
5252
[InlineData(KeyDefinitions.KeyType.P256)]
5353
[InlineData(KeyDefinitions.KeyType.P384)]
5454
[InlineData(KeyDefinitions.KeyType.P521)]
55-
public void CoseKey_VeriDigestedDatafy_WithMultipleCurves_Succeeds(KeyDefinitions.KeyType keyType)
55+
public void CoseKey_VerifyDigestedData_WithMultipleCurves_Succeeds(KeyDefinitions.KeyType keyType)
5656
{
5757
// Arrange
5858
(var ecCurve, var coseCurve) = GetCurves(keyType);
5959

60-
var ecdsa = ECDsa.Create(ecCurve);
61-
var digest = ecdsa.SignData(Encoding.GetEncoding("UTF-8").GetBytes("Hello World"), HashAlgorithmName.SHA256);
62-
var signature = ecdsa.SignHash(digest, DSASignatureFormat.Rfc3279DerSequence);
63-
var ecParams = ecdsa.ExportParameters(false);
64-
var pubKey = new CoseEcPublicKey(coseCurve, ecParams.Q.X, ecParams.Q.Y);
60+
var pubKey = ECDsa.Create(ecCurve);
61+
var sha256 = SHA256.Create();
62+
63+
var dataToSign = Encoding.GetEncoding("UTF-8").GetBytes("Hello World");
64+
var hash = sha256.ComputeHash(dataToSign);
65+
var signature = pubKey.SignHash(hash, DSASignatureFormat.Rfc3279DerSequence);
66+
67+
var ecParams = pubKey.ExportParameters(false);
68+
var pubKeyCose = new CoseEcPublicKey(coseCurve, ecParams.Q.X, ecParams.Q.Y);
6569

6670
// Act
67-
using var verifier = new EcdsaVerify(pubKey);
68-
bool isVerified = verifier.VerifyDigestedData(digest, signature);
71+
using var verifier = new EcdsaVerify(pubKeyCose);
72+
var isVerified = verifier.VerifyDigestedData(hash, signature);
6973

7074
// Assert
7175
Assert.True(isVerified);
@@ -112,13 +116,17 @@ public void ECDsa_VerifyDigestedData_WithIeeeFormat_Succeeds(KeyDefinitions.KeyT
112116
{
113117
// Arrange
114118
(var ecCurve, _) = GetCurves(keyType);
119+
115120
var pubKey = ECDsa.Create(ecCurve);
116-
byte[] digest = pubKey.SignData(Encoding.GetEncoding("UTF-8").GetBytes("Hello World"), HashAlgorithmName.SHA256);
117-
byte[] signature = pubKey.SignHash(digest, DSASignatureFormat.IeeeP1363FixedFieldConcatenation);
121+
var sha256 = SHA256.Create();
122+
123+
var dataToSign = Encoding.GetEncoding("UTF-8").GetBytes("Hello World");
124+
var hash = sha256.ComputeHash(dataToSign);
125+
var signature = pubKey.SignHash(hash, DSASignatureFormat.IeeeP1363FixedFieldConcatenation);
118126

119127
// Act
120128
using var verifier = new EcdsaVerify(pubKey);
121-
bool isVerified = verifier.VerifyDigestedData(digest, signature, false);
129+
var isVerified = verifier.VerifyDigestedData(hash, signature, false);
122130

123131
// Assert
124132
Assert.True(isVerified);
@@ -132,51 +140,64 @@ public void ECDsa_VerifyDigestedData_WithDerFormat_Succeeds(KeyDefinitions.KeyTy
132140
{
133141
// Arrange
134142
(var ecCurve, _) = GetCurves(keyType);
143+
135144
var pubKey = ECDsa.Create(ecCurve);
136-
byte[] digest = pubKey.SignData(Encoding.GetEncoding("UTF-8").GetBytes("Hello World"), HashAlgorithmName.SHA256);
137-
byte[] signature = pubKey.SignHash(digest, DSASignatureFormat.Rfc3279DerSequence);
145+
var sha256 = SHA256.Create();
146+
147+
var dataToSign = Encoding.GetEncoding("UTF-8").GetBytes("Hello World");
148+
var hash = sha256.ComputeHash(dataToSign);
149+
var signature = pubKey.SignHash(hash, DSASignatureFormat.Rfc3279DerSequence);
138150

139151
// Act
140152
using var verifier = new EcdsaVerify(pubKey);
141-
bool isVerified = verifier.VerifyDigestedData(digest, signature, true);
153+
bool isVerified = verifier.VerifyDigestedData(hash, signature, true);
142154

143155
// Assert
144156
Assert.True(isVerified);
145157
}
146158

147-
[Fact]
148-
public void EncodedKey_VerifyDigestedData_Succeeds()
149-
{
150-
byte[] pubKey = GetEncodedPoint();
151-
byte[] digest = GetDigest();
152-
byte[] signature = GetSignature();
153-
154-
using var verifier = new EcdsaVerify(pubKey);
155-
bool isVerified = verifier.VerifyDigestedData(digest, signature);
156-
Assert.True(isVerified);
157-
}
158-
159159
[Theory]
160160
[InlineData(KeyDefinitions.KeyType.P256)]
161161
[InlineData(KeyDefinitions.KeyType.P384)]
162162
[InlineData(KeyDefinitions.KeyType.P521)]
163163
public void ECDsa_VerifyData_WithDerFormat_Succeeds(KeyDefinitions.KeyType keyType)
164164
{
165165
// Arrange
166-
(var eccCurve, _) = GetCurves(keyType);
167-
var pubKey = ECDsa.Create(eccCurve);
168-
byte[] data = Encoding.GetEncoding("UTF-8").GetBytes("Hello World");
169-
byte[] digest = pubKey.SignData(data, HashAlgorithmName.SHA256);
170-
byte[] signature = pubKey.SignHash(digest, DSASignatureFormat.Rfc3279DerSequence);
166+
(var ecCurve, _) = GetCurves(keyType);
167+
168+
var pubKey = ECDsa.Create(ecCurve);
169+
HashAlgorithm hashAlgorithm = keyType switch
170+
{
171+
KeyDefinitions.KeyType.P256 => CryptographyProviders.Sha256Creator(),
172+
KeyDefinitions.KeyType.P384 => CryptographyProviders.Sha384Creator(),
173+
KeyDefinitions.KeyType.P521 => CryptographyProviders.Sha512Creator(),
174+
_ => throw new ArgumentException(ExceptionMessages.UnsupportedAlgorithm),
175+
};
176+
177+
var dataToSign = Encoding.GetEncoding("UTF-8").GetBytes("Hello World");
178+
var hash = hashAlgorithm.ComputeHash(dataToSign);
179+
var signature = pubKey.SignHash(hash, DSASignatureFormat.Rfc3279DerSequence);
171180

172181
// Act
173182
using var verifier = new EcdsaVerify(pubKey);
174-
bool isVerified = verifier.VerifyData(data, signature, true);
183+
var isVerified = verifier.VerifyData(dataToSign, signature, true);
175184

176185
// Assert
177186
Assert.True(isVerified);
178187
}
179188

189+
[Fact]
190+
public void EncodedKey_VerifyDigestedData_Succeeds()
191+
{
192+
byte[] pubKey = GetEncodedPoint();
193+
byte[] digest = GetDigest();
194+
byte[] signature = GetSignature();
195+
196+
using var verifier = new EcdsaVerify(pubKey);
197+
bool isVerified = verifier.VerifyDigestedData(digest, signature);
198+
Assert.True(isVerified);
199+
}
200+
180201
private byte[] GetEncodedPoint()
181202
{
182203
byte[] xCoord = GetX();

0 commit comments

Comments
 (0)