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