@@ -52,20 +52,24 @@ public void CoseKey_VeriDigestedDatafy_Succeeds()
52
52
[ InlineData ( KeyDefinitions . KeyType . P256 ) ]
53
53
[ InlineData ( KeyDefinitions . KeyType . P384 ) ]
54
54
[ InlineData ( KeyDefinitions . KeyType . P521 ) ]
55
- public void CoseKey_VeriDigestedDatafy_WithMultipleCurves_Succeeds ( KeyDefinitions . KeyType keyType )
55
+ public void CoseKey_VerifyDigestedData_WithMultipleCurves_Succeeds ( KeyDefinitions . KeyType keyType )
56
56
{
57
57
// Arrange
58
58
( var ecCurve , var coseCurve ) = GetCurves ( keyType ) ;
59
59
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 ) ;
65
69
66
70
// 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 ) ;
69
73
70
74
// Assert
71
75
Assert . True ( isVerified ) ;
@@ -112,13 +116,17 @@ public void ECDsa_VerifyDigestedData_WithIeeeFormat_Succeeds(KeyDefinitions.KeyT
112
116
{
113
117
// Arrange
114
118
( var ecCurve , _ ) = GetCurves ( keyType ) ;
119
+
115
120
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 ) ;
118
126
119
127
// Act
120
128
using var verifier = new EcdsaVerify ( pubKey ) ;
121
- bool isVerified = verifier . VerifyDigestedData ( digest , signature , false ) ;
129
+ var isVerified = verifier . VerifyDigestedData ( hash , signature , false ) ;
122
130
123
131
// Assert
124
132
Assert . True ( isVerified ) ;
@@ -132,51 +140,64 @@ public void ECDsa_VerifyDigestedData_WithDerFormat_Succeeds(KeyDefinitions.KeyTy
132
140
{
133
141
// Arrange
134
142
( var ecCurve , _ ) = GetCurves ( keyType ) ;
143
+
135
144
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 ) ;
138
150
139
151
// Act
140
152
using var verifier = new EcdsaVerify ( pubKey ) ;
141
- bool isVerified = verifier . VerifyDigestedData ( digest , signature , true ) ;
153
+ bool isVerified = verifier . VerifyDigestedData ( hash , signature , true ) ;
142
154
143
155
// Assert
144
156
Assert . True ( isVerified ) ;
145
157
}
146
158
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
-
159
159
[ Theory ]
160
160
[ InlineData ( KeyDefinitions . KeyType . P256 ) ]
161
161
[ InlineData ( KeyDefinitions . KeyType . P384 ) ]
162
162
[ InlineData ( KeyDefinitions . KeyType . P521 ) ]
163
163
public void ECDsa_VerifyData_WithDerFormat_Succeeds ( KeyDefinitions . KeyType keyType )
164
164
{
165
165
// 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 ) ;
171
180
172
181
// Act
173
182
using var verifier = new EcdsaVerify ( pubKey ) ;
174
- bool isVerified = verifier . VerifyData ( data , signature , true ) ;
183
+ var isVerified = verifier . VerifyData ( dataToSign , signature , true ) ;
175
184
176
185
// Assert
177
186
Assert . True ( isVerified ) ;
178
187
}
179
188
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
+
180
201
private byte [ ] GetEncodedPoint ( )
181
202
{
182
203
byte [ ] xCoord = GetX ( ) ;
0 commit comments