Skip to content

Commit cd393fd

Browse files
committed
tink-signature: tweak test methods
Merge upstream commit: - 6ba9163200fb Clean up signature/subtle package.
1 parent 7f1a95a commit cd393fd

File tree

3 files changed

+56
-50
lines changed

3 files changed

+56
-50
lines changed

tests/tests/signature_test/subtle/ecdsa_signer_verifier_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ struct TestCase {
205205
}
206206

207207
#[test]
208-
fn test_wycheproof_vectors() {
208+
fn test_ecdsa_wycheproof_cases() {
209209
struct TestVector {
210210
filename: &'static str,
211211
encoding: EcdsaSignatureEncoding,

tests/tests/signature_test/subtle/ecdsa_test.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
use tink_proto::{EcdsaSignatureEncoding, EllipticCurveType, HashType};
1818

19-
struct ParamsTest {
19+
struct ParamsTestEcdsa {
2020
hash: HashType,
2121
curve: EllipticCurveType,
2222
encoding: EcdsaSignatureEncoding,
@@ -44,40 +44,40 @@ fn test_ecdsa_validate_params() {
4444
}
4545
}
4646

47-
fn gen_ecdsa_invalid_params() -> Vec<ParamsTest> {
47+
fn gen_ecdsa_invalid_params() -> Vec<ParamsTestEcdsa> {
4848
let encodings = vec![
4949
EcdsaSignatureEncoding::Der,
5050
EcdsaSignatureEncoding::IeeeP1363,
5151
];
5252
let mut test_cases = vec![
5353
// invalid encoding
54-
ParamsTest {
54+
ParamsTestEcdsa {
5555
hash: HashType::Sha256,
5656
curve: EllipticCurveType::NistP256,
5757
encoding: EcdsaSignatureEncoding::UnknownEncoding,
5858
},
5959
];
6060
for encoding in encodings {
6161
// invalid curve
62-
test_cases.push(ParamsTest {
62+
test_cases.push(ParamsTestEcdsa {
6363
hash: HashType::Sha256,
6464
curve: EllipticCurveType::UnknownCurve,
6565
encoding,
6666
});
6767
// invalid hash: P256 and SHA-512
68-
test_cases.push(ParamsTest {
68+
test_cases.push(ParamsTestEcdsa {
6969
hash: HashType::Sha512,
7070
curve: EllipticCurveType::NistP256,
7171
encoding,
7272
});
7373
// invalid hash: P521 and SHA-256
74-
test_cases.push(ParamsTest {
74+
test_cases.push(ParamsTestEcdsa {
7575
hash: HashType::Sha256,
7676
curve: EllipticCurveType::NistP521,
7777
encoding,
7878
});
7979
// invalid hash: P384 and SHA-256
80-
test_cases.push(ParamsTest {
80+
test_cases.push(ParamsTestEcdsa {
8181
hash: HashType::Sha256,
8282
curve: EllipticCurveType::NistP384,
8383
encoding,
@@ -86,44 +86,44 @@ fn gen_ecdsa_invalid_params() -> Vec<ParamsTest> {
8686
test_cases
8787
}
8888

89-
fn gen_ecdsa_valid_params() -> Vec<ParamsTest> {
89+
fn gen_ecdsa_valid_params() -> Vec<ParamsTestEcdsa> {
9090
vec![
91-
ParamsTest {
91+
ParamsTestEcdsa {
9292
hash: HashType::Sha256,
9393
curve: EllipticCurveType::NistP256,
9494
encoding: EcdsaSignatureEncoding::Der,
9595
},
96-
ParamsTest {
96+
ParamsTestEcdsa {
9797
hash: HashType::Sha256,
9898
curve: EllipticCurveType::NistP256,
9999
encoding: EcdsaSignatureEncoding::IeeeP1363,
100100
},
101-
ParamsTest {
101+
ParamsTestEcdsa {
102102
hash: HashType::Sha384,
103103
curve: EllipticCurveType::NistP384,
104104
encoding: EcdsaSignatureEncoding::Der,
105105
},
106-
ParamsTest {
106+
ParamsTestEcdsa {
107107
hash: HashType::Sha384,
108108
curve: EllipticCurveType::NistP384,
109109
encoding: EcdsaSignatureEncoding::IeeeP1363,
110110
},
111-
ParamsTest {
111+
ParamsTestEcdsa {
112112
hash: HashType::Sha512,
113113
curve: EllipticCurveType::NistP384,
114114
encoding: EcdsaSignatureEncoding::Der,
115115
},
116-
ParamsTest {
116+
ParamsTestEcdsa {
117117
hash: HashType::Sha512,
118118
curve: EllipticCurveType::NistP384,
119119
encoding: EcdsaSignatureEncoding::IeeeP1363,
120120
},
121-
ParamsTest {
121+
ParamsTestEcdsa {
122122
hash: HashType::Sha512,
123123
curve: EllipticCurveType::NistP521,
124124
encoding: EcdsaSignatureEncoding::Der,
125125
},
126-
ParamsTest {
126+
ParamsTestEcdsa {
127127
hash: HashType::Sha512,
128128
curve: EllipticCurveType::NistP521,
129129
encoding: EcdsaSignatureEncoding::IeeeP1363,

tests/tests/signature_test/subtle/ed25519_signer_verifier_test.rs

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,7 @@ struct TestCaseEd25519 {
188188
}
189189

190190
#[test]
191-
fn test_vectors_ed25519() {
192-
// signing tests are same between ecdsa and ed25519
191+
fn test_ed25519_wycheproof_cases() {
193192
let filename = "testvectors/eddsa_test.json";
194193
println!("wycheproof file '{}'", filename);
195194
let bytes = tink_tests::wycheproof_data(filename);
@@ -220,46 +219,53 @@ fn test_vectors_ed25519() {
220219
tc.case.case_id, tc.case.result, tc.case.comment
221220
);
222221
let result = signer.sign(&tc.msg);
223-
if tc.case.result == tink_tests::WycheproofResult::Valid {
224-
match result {
225-
Err(e) => panic!(
226-
"sign failed in test case {}: with error {:?}",
227-
tc.case.case_id, e
228-
),
229-
Ok(got) =>
230-
// Ed25519 is deterministic.
231-
// Getting an alternative signature may leak the private key.
232-
// This is especially the case if an attacker can also learn the valid
233-
// signature.
234-
{
235-
assert_eq!(
236-
tc.sig,
237-
got,
238-
"sign failed in test case {}: invalid signature generated {}",
239-
tc.case.case_id,
240-
hex::encode(&got)
222+
match tc.case.result {
223+
tink_tests::WycheproofResult::Valid => {
224+
match result {
225+
Err(e) => panic!(
226+
"Ed25519Signer::sign failed in test case {}: with error {:?}",
227+
tc.case.case_id, e
228+
),
229+
Ok(got) => {
230+
// Ed25519 is deterministic.
231+
// Getting an alternative signature may leak the private key.
232+
// This is especially the case if an attacker can also learn the valid
233+
// signature.
234+
assert_eq!(
235+
tc.sig,
236+
got,
237+
"Ed25519Signer::sign failed in test case {}: invalid signature generated {}",
238+
tc.case.case_id,
239+
hex::encode(&got)
240+
)
241+
}
242+
}
243+
}
244+
tink_tests::WycheproofResult::Invalid => {
245+
if result.is_ok() && tc.sig == result.unwrap() {
246+
panic!(
247+
"Ed25519Signer::sign failed in test case {}: invalid signature generated",
248+
tc.case.case_id
241249
)
242250
}
243251
}
244-
} else if result.is_ok() && tc.sig == result.unwrap() {
245-
panic!(
246-
"sign failed in test case {}: invalid signature generated",
247-
tc.case.case_id
248-
)
252+
_ => panic!("unrecognized result {}", tc.case.result),
249253
}
250254

251255
let result = verifier.verify(&tc.sig, &tc.msg);
252-
if tc.case.result == WycheproofResult::Valid && result.is_err() {
253-
panic!(
256+
match tc.case.result {
257+
WycheproofResult::Valid => assert!(
258+
result.is_ok(),
254259
"verify failed in test case {}: valid signature is rejected with error {:?}",
255-
tc.case.case_id, result
256-
)
257-
}
258-
if tc.case.result == WycheproofResult::Invalid && result.is_ok() {
259-
panic!(
260+
tc.case.case_id,
261+
result
262+
),
263+
WycheproofResult::Invalid => assert!(
264+
result.is_err(),
260265
"verify failed in test case {}: invalid signature is accepted",
261266
tc.case.case_id
262-
)
267+
),
268+
_ => panic!("unrecognized result {}", tc.case.result),
263269
}
264270
}
265271
}

0 commit comments

Comments
 (0)