@@ -25,7 +25,7 @@ namespace Yubico.YubiKey.Cryptography
25
25
public class EcdsaVerifyTests
26
26
{
27
27
[ Fact ]
28
- public void PivKey_Verify_Succeeds ( )
28
+ public void PivKey_VerifDigestedDatay_Succeeds ( )
29
29
{
30
30
var pubKey = new PivEccPublicKey ( GetEncodedPoint ( ) ) ;
31
31
byte [ ] digest = GetDigest ( ) ;
@@ -37,7 +37,7 @@ public void PivKey_Verify_Succeeds()
37
37
}
38
38
39
39
[ Fact ]
40
- public void CoseKey_Verify_Succeeds ( )
40
+ public void CoseKey_VeriDigestedDatafy_Succeeds ( )
41
41
{
42
42
var pubKey = new CoseEcPublicKey ( CoseEcCurve . P256 , GetX ( ) , GetY ( ) ) ;
43
43
byte [ ] digest = GetDigest ( ) ;
@@ -52,19 +52,12 @@ public void CoseKey_Verify_Succeeds()
52
52
[ InlineData ( KeyDefinitions . KeyType . P256 ) ]
53
53
[ InlineData ( KeyDefinitions . KeyType . P384 ) ]
54
54
[ InlineData ( KeyDefinitions . KeyType . P521 ) ]
55
- public void CoseKey_Verify_WithMultipleCurves_Succeeds ( KeyDefinitions . KeyType keyType )
55
+ public void CoseKey_VeriDigestedDatafy_WithMultipleCurves_Succeeds ( KeyDefinitions . KeyType keyType )
56
56
{
57
57
// 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 ) ;
66
59
67
- var ecdsa = ECDsa . Create ( eccCurve ) ;
60
+ var ecdsa = ECDsa . Create ( ecCurve ) ;
68
61
var digest = ecdsa . SignData ( Encoding . GetEncoding ( "UTF-8" ) . GetBytes ( "Hello World" ) , HashAlgorithmName . SHA256 ) ;
69
62
var signature = ecdsa . SignHash ( digest , DSASignatureFormat . Rfc3279DerSequence ) ;
70
63
var ecParams = ecdsa . ExportParameters ( false ) ;
@@ -79,7 +72,7 @@ public void CoseKey_Verify_WithMultipleCurves_Succeeds(KeyDefinitions.KeyType ke
79
72
}
80
73
81
74
[ Fact ]
82
- public void Cert_Verify_Succeeds ( )
75
+ public void Cert_Verify_DigestedDataSucceeds ( )
83
76
{
84
77
var pubKey = new X509Certificate2 ( GetCert ( ) ) ;
85
78
byte [ ] digest = GetDigest ( ) ;
@@ -90,7 +83,7 @@ public void Cert_Verify_Succeeds()
90
83
}
91
84
92
85
[ Fact ]
93
- public void ECDsa_Verify_Succeeds ( )
86
+ public void ECDsa_VerifyDigestedData_Succeeds ( )
94
87
{
95
88
var eccCurve = ECCurve . CreateFromValue ( KeyDefinitions . KeyOids . P256 ) ;
96
89
var eccParams = new ECParameters
@@ -115,19 +108,11 @@ public void ECDsa_Verify_Succeeds()
115
108
[ InlineData ( KeyDefinitions . KeyType . P256 ) ]
116
109
[ InlineData ( KeyDefinitions . KeyType . P384 ) ]
117
110
[ InlineData ( KeyDefinitions . KeyType . P521 ) ]
118
- public void ECDsa_Verify_WithIeeeFormat_Succeeds ( KeyDefinitions . KeyType keyType )
111
+ public void ECDsa_VerifyDigestedData_WithIeeeFormat_Succeeds ( KeyDefinitions . KeyType keyType )
119
112
{
120
113
// 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 ) ;
131
116
byte [ ] digest = pubKey . SignData ( Encoding . GetEncoding ( "UTF-8" ) . GetBytes ( "Hello World" ) , HashAlgorithmName . SHA256 ) ;
132
117
byte [ ] signature = pubKey . SignHash ( digest , DSASignatureFormat . IeeeP1363FixedFieldConcatenation ) ;
133
118
@@ -143,19 +128,11 @@ public void ECDsa_Verify_WithIeeeFormat_Succeeds(KeyDefinitions.KeyType keyType)
143
128
[ InlineData ( KeyDefinitions . KeyType . P256 ) ]
144
129
[ InlineData ( KeyDefinitions . KeyType . P384 ) ]
145
130
[ InlineData ( KeyDefinitions . KeyType . P521 ) ]
146
- public void ECDsa_Verify_WithDerFormat_Succeeds ( KeyDefinitions . KeyType keyType )
131
+ public void ECDsa_VerifyDigestedData_WithDerFormat_Succeeds ( KeyDefinitions . KeyType keyType )
147
132
{
148
133
// 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 ) ;
159
136
byte [ ] digest = pubKey . SignData ( Encoding . GetEncoding ( "UTF-8" ) . GetBytes ( "Hello World" ) , HashAlgorithmName . SHA256 ) ;
160
137
byte [ ] signature = pubKey . SignHash ( digest , DSASignatureFormat . Rfc3279DerSequence ) ;
161
138
@@ -167,6 +144,29 @@ public void ECDsa_Verify_WithDerFormat_Succeeds(KeyDefinitions.KeyType keyType)
167
144
Assert . True ( isVerified ) ;
168
145
}
169
146
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
+
170
170
[ Fact ]
171
171
public void EncodedKey_Verify_Succeeds ( )
172
172
{
@@ -192,6 +192,20 @@ private byte[] GetEncodedPoint()
192
192
return encoding ;
193
193
}
194
194
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
+
195
209
196
210
private byte [ ] GetX ( ) => new byte [ ]
197
211
{
0 commit comments