-
Notifications
You must be signed in to change notification settings - Fork 735
/
Copy pathed25519_tests.rs
234 lines (196 loc) · 7.93 KB
/
ed25519_tests.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Copyright 2015-2017 Brian Smith.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#![allow(missing_docs)]
use ring::{
error, rand,
signature::{self, Ed25519KeyPair, KeyPair},
};
use ring::{test, test_file};
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure};
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
wasm_bindgen_test_configure!(run_in_browser);
/// Test vectors from BoringSSL.
#[test]
fn test_signature_ed25519() {
test::run(test_file!("ed25519_tests.txt"), |section, test_case| {
assert_eq!(section, "");
let seed = test_case.consume_bytes("SEED");
assert_eq!(32, seed.len());
let public_key = test_case.consume_bytes("PUB");
assert_eq!(32, public_key.len());
let msg = test_case.consume_bytes("MESSAGE");
let expected_sig = test_case.consume_bytes("SIG");
{
let key_pair = Ed25519KeyPair::from_seed_and_public_key(&seed, &public_key).unwrap();
let actual_sig = key_pair.sign(&msg);
assert_eq!(&expected_sig[..], actual_sig.as_ref());
}
// Test PKCS#8 generation, parsing, and private-to-public calculations.
let rng = test::rand::FixedSliceRandom { bytes: &seed };
let pkcs8 = Ed25519KeyPair::generate_pkcs8(&rng).unwrap();
let key_pair = Ed25519KeyPair::from_pkcs8(pkcs8.as_ref()).unwrap();
assert_eq!(public_key, key_pair.public_key().as_ref());
// Test Signature generation.
let actual_sig = key_pair.sign(&msg);
assert_eq!(&expected_sig[..], actual_sig.as_ref());
// Test Signature verification.
test_signature_verification(&public_key, &msg, &expected_sig, Ok(()));
let mut tampered_sig = expected_sig;
tampered_sig[0] ^= 1;
test_signature_verification(&public_key, &msg, &tampered_sig, Err(error::Unspecified));
Ok(())
});
}
/// Test vectors from BoringSSL.
#[test]
fn test_signature_ed25519_verify() {
test::run(
test_file!("ed25519_verify_tests.txt"),
|section, test_case| {
assert_eq!(section, "");
let public_key = test_case.consume_bytes("PUB");
let msg = test_case.consume_bytes("MESSAGE");
let sig = test_case.consume_bytes("SIG");
let expected_result = match test_case.consume_string("Result").as_str() {
"P" => Ok(()),
"F" => Err(error::Unspecified),
s => panic!("{:?} is not a valid result", s),
};
test_signature_verification(&public_key, &msg, &sig, expected_result);
Ok(())
},
);
}
fn test_signature_verification(
public_key: &[u8],
msg: &[u8],
sig: &[u8],
expected_result: Result<(), error::Unspecified>,
) {
assert_eq!(
expected_result,
signature::UnparsedPublicKey::new(&signature::ED25519, public_key).verify(msg, sig)
);
}
#[test]
fn test_ed25519_from_seed_and_public_key_misuse() {
const PRIVATE_KEY: &[u8] = include_bytes!("ed25519_test_private_key.bin");
const PUBLIC_KEY: &[u8] = include_bytes!("ed25519_test_public_key.bin");
assert!(Ed25519KeyPair::from_seed_and_public_key(PRIVATE_KEY, PUBLIC_KEY).is_ok());
// Truncated private key.
assert!(Ed25519KeyPair::from_seed_and_public_key(&PRIVATE_KEY[..31], PUBLIC_KEY).is_err());
// Truncated public key.
assert!(Ed25519KeyPair::from_seed_and_public_key(PRIVATE_KEY, &PUBLIC_KEY[..31]).is_err());
// Swapped public and private key.
assert!(Ed25519KeyPair::from_seed_and_public_key(PUBLIC_KEY, PRIVATE_KEY).is_err());
}
enum FromPkcs8Variant {
Checked,
MaybeUnchecked,
}
#[test]
fn test_ed25519_from_pkcs8_unchecked() {
test_ed25519_from_pkcs8_(
FromPkcs8Variant::MaybeUnchecked,
Ed25519KeyPair::from_pkcs8_maybe_unchecked,
)
}
#[test]
fn test_ed25519_from_pkcs8() {
test_ed25519_from_pkcs8_(FromPkcs8Variant::Checked, Ed25519KeyPair::from_pkcs8)
}
fn test_ed25519_from_pkcs8_(
variant: FromPkcs8Variant,
f: impl Fn(&[u8]) -> Result<Ed25519KeyPair, error::KeyRejected>,
) {
// Just test that we can parse the input.
test::run(
test_file!("ed25519_from_pkcs8_tests.txt"),
|section, test_case| {
assert_eq!(section, "");
let input = test_case.consume_bytes("Input");
let expected_error = {
let expected_checked = test_case.consume_string("Result-Checked");
let expected_maybe_unchecked = test_case.consume_string("Result-Maybe-Unchecked");
let expected_result = match variant {
FromPkcs8Variant::Checked => expected_checked,
FromPkcs8Variant::MaybeUnchecked => expected_maybe_unchecked,
};
if expected_result == "OK" {
None
} else {
Some(expected_result)
}
};
let expected_public = {
let expected_if_no_error = test_case.consume_optional_bytes("Public");
if expected_error.is_none() {
Some(expected_if_no_error.unwrap())
} else {
None
}
};
match f(&input) {
Ok(keypair) => {
assert_eq!(expected_error, None);
assert_eq!(
expected_public.as_deref(),
Some(keypair.public_key().as_ref())
);
}
Err(actual_error) => {
assert_eq!(expected_error, Some(format!("{}", actual_error)));
assert_eq!(expected_public, None);
}
}
Ok(())
},
);
}
#[test]
fn ed25519_test_generate_pkcs8() {
let rng = rand::SystemRandom::new();
let generated = Ed25519KeyPair::generate_pkcs8(&rng).unwrap();
let generated = generated.as_ref();
let _ronudtripped = Ed25519KeyPair::from_pkcs8(generated).unwrap();
// Regression test: Verify we're generating the correct encoding, as
// `Ed25519KeyPair::from_pkcs8` also accepts our old wrong encoding.
assert_eq!(generated.len(), 19 + 32 + 32);
assert_eq!(&generated[..2], &[0x30, 0x51]);
}
#[test]
fn ed25519_test_public_key_coverage() {
const PRIVATE_KEY: &[u8] = include_bytes!("ed25519_test_private_key.p8");
const PUBLIC_KEY: &[u8] = include_bytes!("ed25519_test_public_key.der");
const PUBLIC_KEY_DEBUG: &str =
"PublicKey(\"5809e9fef6dcec58f0f2e3b0d67e9880a11957e083ace85835c3b6c8fbaf6b7d\")";
let key_pair = Ed25519KeyPair::from_pkcs8(PRIVATE_KEY).unwrap();
// Test `AsRef<[u8]>`
assert_eq!(key_pair.public_key().as_ref(), PUBLIC_KEY);
// Test `Clone`.
#[allow(clippy::clone_on_copy)]
let _: <Ed25519KeyPair as KeyPair>::PublicKey = key_pair.public_key().clone();
// Test `Copy`.
let _: <Ed25519KeyPair as KeyPair>::PublicKey = *key_pair.public_key();
// Test `Debug`.
assert_eq!(PUBLIC_KEY_DEBUG, format!("{:?}", key_pair.public_key()));
assert_eq!(
format!(
"Ed25519KeyPair {{ public_key: {:?} }}",
key_pair.public_key()
),
format!("{:?}", key_pair)
);
}