]> git.lizzy.rs Git - PAKEs.git/blob - spake2/src/lib.rs
add doc logo
[PAKEs.git] / spake2 / src / lib.rs
1 #![doc(html_logo_url =
2     "https://raw.githubusercontent.com/RustCrypto/meta/master/logo_small.png")]
3 #![forbid(unsafe_code)]
4 #![cfg_attr(test, deny(warnings))]
5
6 extern crate curve25519_dalek;
7 extern crate hex;
8 extern crate hkdf;
9 extern crate num_bigint;
10 extern crate rand;
11 extern crate sha2;
12
13 mod spake2;
14 pub use spake2::*;
15
16 #[cfg(test)]
17 mod tests {
18     use spake2::{Ed25519Group, ErrorType, Identity, Password, SPAKEErr, SPAKE2};
19
20     #[test]
21     fn test_basic() {
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"password"),
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_eq!(key1, key2);
35     }
36
37     #[test]
38     fn test_mismatch() {
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 (s2, msg2) = SPAKE2::<Ed25519Group>::start_b(
45             &Password::new(b"password2"),
46             &Identity::new(b"idA"),
47             &Identity::new(b"idB"),
48         );
49         let key1 = s1.finish(msg2.as_slice()).unwrap();
50         let key2 = s2.finish(msg1.as_slice()).unwrap();
51         assert_ne!(key1, key2);
52     }
53
54     #[test]
55     fn test_reflected_message() {
56         let (s1, msg1) = SPAKE2::<Ed25519Group>::start_a(
57             &Password::new(b"password"),
58             &Identity::new(b"idA"),
59             &Identity::new(b"idB"),
60         );
61         let r = s1.finish(msg1.as_slice());
62         assert_eq!(
63             r.unwrap_err(),
64             SPAKEErr {
65                 kind: ErrorType::BadSide,
66             }
67         );
68     }
69
70     #[test]
71     fn test_bad_length() {
72         let (s1, msg1) = SPAKE2::<Ed25519Group>::start_a(
73             &Password::new(b"password"),
74             &Identity::new(b"idA"),
75             &Identity::new(b"idB"),
76         );
77         let mut msg2 = Vec::<u8>::with_capacity(msg1.len() + 1);
78         msg2.resize(msg1.len() + 1, 0u8);
79         let r = s1.finish(&msg2);
80         assert_eq!(
81             r.unwrap_err(),
82             SPAKEErr {
83                 kind: ErrorType::WrongLength,
84             }
85         );
86     }
87
88     #[test]
89     fn test_basic_symmetric() {
90         let (s1, msg1) = SPAKE2::<Ed25519Group>::start_symmetric(
91             &Password::new(b"password"),
92             &Identity::new(b"idS"),
93         );
94         let (s2, msg2) = SPAKE2::<Ed25519Group>::start_symmetric(
95             &Password::new(b"password"),
96             &Identity::new(b"idS"),
97         );
98         let key1 = s1.finish(msg2.as_slice()).unwrap();
99         let key2 = s2.finish(msg1.as_slice()).unwrap();
100         assert_eq!(key1, key2);
101     }
102
103     #[test]
104     fn it_works() {}
105
106     #[test]
107     #[should_panic(expected = "nope")]
108     fn it_panics() {
109         assert!(false, "nope");
110     }
111 }