Skip to content

Commit 2ee2344

Browse files
committed
Add a basic unit test that covers the valid use cases. Invalid cases are already covered by the pkcs#8 test.
Signed-off-by: Vladimir Pouzanov <farcaller@gmail.com>
1 parent 207dd68 commit 2ee2344

2 files changed

Lines changed: 74 additions & 0 deletions

File tree

tests/ecdsa_from_der_tests.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Curve = P-256
2+
Input = 30770201010420090460075f15d2a256248000fb02d83ad77593dde4ae59fc5e96142dffb2bd07a00a06082a8648ce3d030107a14403420004cf0d13a3a7577231ea1b66cf4021cd54f21f4ac4f5f2fdd28e05bc7d2bd099d1374cd08d2ef654d6f04498db462f73e0282058dd661a4c9b0437af3f7af6e724
3+
4+
Curve = P-384
5+
Input = 3081a40201010430fc0603810412769beeabbf97ce9764e104bca45b3b7428006fb42d1fa69a344bf475ce17bf06daf553c4eccffcfecc26a00706052b81040022a1640362000417e425506a81d85e607a3caeaccbe6cc7ef58b559115b9867175ef9911f66ea77eb5b7f43e42f3129a1fe2841f6717ed4fc02bf8cfe2d10cac06a150dcba7ae9f035ec9b6b034a4ddc554da7c2da4719a1d990097fbb451a3ea1e664fc444cfa
6+
7+
# A P-256 key where the ECPrivateKey contains a parameters field that matches the PKCS#8 algorithm identifier.
8+
Curve = P-256
9+
Input = 30770201010420090460075f15d2a256248000fb02d83ad77593dde4ae59fc5e96142dffb2bd07a00a06082a8648ce3d030107a14403420004cf0d13a3a7577231ea1b66cf4021cd54f21f4ac4f5f2fdd28e05bc7d2bd099d1374cd08d2ef654d6f04498db462f73e0282058dd661a4c9b0437af3f7af6e724
10+
11+
# A P-384 key where the ECPrivateKey contains a parameters field identifying P-384.
12+
Curve = P-384
13+
Input = 3081a40201010430fc0603810412769beeabbf97ce9764e104bca45b3b7428006fb42d1fa69a344bf475ce17bf06daf553c4eccffcfecc26a00706052b81040022a1640362000417e425506a81d85e607a3caeaccbe6cc7ef58b559115b9867175ef9911f66ea77eb5b7f43e42f3129a1fe2841f6717ed4fc02bf8cfe2d10cac06a150dcba7ae9f035ec9b6b034a4ddc554da7c2da4719a1d990097fbb451a3ea1e664fc444cfa

tests/ecdsa_tests.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,67 @@ fn ecdsa_from_pkcs8_test() {
8686
);
8787
}
8888

89+
#[test]
90+
fn ecdsa_from_der_test() {
91+
test::run(
92+
test_file!("ecdsa_from_der_tests.txt"),
93+
|section, test_case| {
94+
assert_eq!(section, "");
95+
96+
let curve_name = test_case.consume_string("Curve");
97+
let ((this_fixed, this_asn1), (other_fixed, other_asn1)) = match curve_name.as_str() {
98+
"P-256" => (
99+
(
100+
&signature::ECDSA_P256_SHA256_FIXED_SIGNING,
101+
&signature::ECDSA_P256_SHA256_ASN1_SIGNING,
102+
),
103+
(
104+
&signature::ECDSA_P384_SHA384_FIXED_SIGNING,
105+
&signature::ECDSA_P384_SHA384_ASN1_SIGNING,
106+
),
107+
),
108+
"P-384" => (
109+
(
110+
&signature::ECDSA_P384_SHA384_FIXED_SIGNING,
111+
&signature::ECDSA_P384_SHA384_ASN1_SIGNING,
112+
),
113+
(
114+
&signature::ECDSA_P256_SHA256_FIXED_SIGNING,
115+
&signature::ECDSA_P256_SHA256_ASN1_SIGNING,
116+
),
117+
),
118+
_ => unreachable!(),
119+
};
120+
121+
let input = test_case.consume_bytes("Input");
122+
123+
let error = test_case.consume_optional_string("Error");
124+
125+
match (
126+
signature::EcdsaKeyPair::from_der(this_fixed, &input),
127+
error.clone(),
128+
) {
129+
(Ok(_), None) => (),
130+
(Err(e), None) => panic!("Failed with error \"{}\", but expected to succeed", e),
131+
(Ok(_), Some(e)) => panic!("Succeeded, but expected error \"{}\"", e),
132+
(Err(actual), Some(expected)) => assert_eq!(format!("{}", actual), expected),
133+
};
134+
135+
match (signature::EcdsaKeyPair::from_der(this_asn1, &input), error) {
136+
(Ok(_), None) => (),
137+
(Err(e), None) => panic!("Failed with error \"{}\", but expected to succeed", e),
138+
(Ok(_), Some(e)) => panic!("Succeeded, but expected error \"{}\"", e),
139+
(Err(actual), Some(expected)) => assert_eq!(format!("{}", actual), expected),
140+
};
141+
142+
assert!(signature::EcdsaKeyPair::from_der(other_fixed, &input).is_err());
143+
assert!(signature::EcdsaKeyPair::from_der(other_asn1, &input).is_err());
144+
145+
Ok(())
146+
},
147+
);
148+
}
149+
89150
// Verify that, at least, we generate PKCS#8 documents that we can read.
90151
#[test]
91152
fn ecdsa_generate_pkcs8_test() {

0 commit comments

Comments
 (0)