]> git.lizzy.rs Git - PAKEs.git/blob - spake2/tests/mod.rs
spake2 code reorganization (#6)
[PAKEs.git] / spake2 / tests / mod.rs
1 extern crate spake2;
2
3 use spake2::{Ed25519Group, ErrorType, Identity, Password, SPAKEErr, SPAKE2};
4
5 #[test]
6 fn test_basic() {
7     let (s1, msg1) = SPAKE2::<Ed25519Group>::start_a(
8         &Password::new(b"password"),
9         &Identity::new(b"idA"),
10         &Identity::new(b"idB"),
11     );
12     let (s2, msg2) = SPAKE2::<Ed25519Group>::start_b(
13         &Password::new(b"password"),
14         &Identity::new(b"idA"),
15         &Identity::new(b"idB"),
16     );
17     let key1 = s1.finish(msg2.as_slice()).unwrap();
18     let key2 = s2.finish(msg1.as_slice()).unwrap();
19     assert_eq!(key1, key2);
20 }
21
22 #[test]
23 fn test_mismatch() {
24     let (s1, msg1) = SPAKE2::<Ed25519Group>::start_a(
25         &Password::new(b"password"),
26         &Identity::new(b"idA"),
27         &Identity::new(b"idB"),
28     );
29     let (s2, msg2) = SPAKE2::<Ed25519Group>::start_b(
30         &Password::new(b"password2"),
31         &Identity::new(b"idA"),
32         &Identity::new(b"idB"),
33     );
34     let key1 = s1.finish(msg2.as_slice()).unwrap();
35     let key2 = s2.finish(msg1.as_slice()).unwrap();
36     assert_ne!(key1, key2);
37 }
38
39 #[test]
40 fn test_reflected_message() {
41     let (s1, msg1) = SPAKE2::<Ed25519Group>::start_a(
42         &Password::new(b"password"),
43         &Identity::new(b"idA"),
44         &Identity::new(b"idB"),
45     );
46     let r = s1.finish(msg1.as_slice());
47     assert_eq!(
48         r.unwrap_err(),
49         SPAKEErr {
50             kind: ErrorType::BadSide,
51         }
52     );
53 }
54
55 #[test]
56 fn test_bad_length() {
57     let (s1, msg1) = SPAKE2::<Ed25519Group>::start_a(
58         &Password::new(b"password"),
59         &Identity::new(b"idA"),
60         &Identity::new(b"idB"),
61     );
62     let mut msg2 = Vec::<u8>::with_capacity(msg1.len() + 1);
63     msg2.resize(msg1.len() + 1, 0u8);
64     let r = s1.finish(&msg2);
65     assert_eq!(
66         r.unwrap_err(),
67         SPAKEErr {
68             kind: ErrorType::WrongLength,
69         }
70     );
71 }
72
73 #[test]
74 fn test_basic_symmetric() {
75     let (s1, msg1) = SPAKE2::<Ed25519Group>::start_symmetric(
76         &Password::new(b"password"),
77         &Identity::new(b"idS"),
78     );
79     let (s2, msg2) = SPAKE2::<Ed25519Group>::start_symmetric(
80         &Password::new(b"password"),
81         &Identity::new(b"idS"),
82     );
83     let key1 = s1.finish(msg2.as_slice()).unwrap();
84     let key2 = s2.finish(msg1.as_slice()).unwrap();
85     assert_eq!(key1, key2);
86 }