aboutsummaryrefslogtreecommitdiff
path: root/spake2/tests/spake2.rs
blob: eeced912e863bbe4460065e0d9ebdfc8902b5a97 (plain)
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
use spake2::{Ed25519Group, Error, Identity, Password, Spake2};

#[test]
fn test_basic() {
    let (s1, msg1) = Spake2::<Ed25519Group>::start_a(
        &Password::new(b"password"),
        &Identity::new(b"idA"),
        &Identity::new(b"idB"),
    );
    let (s2, msg2) = Spake2::<Ed25519Group>::start_b(
        &Password::new(b"password"),
        &Identity::new(b"idA"),
        &Identity::new(b"idB"),
    );
    let key1 = s1.finish(msg2.as_slice()).unwrap();
    let key2 = s2.finish(msg1.as_slice()).unwrap();
    assert_eq!(key1, key2);
}

#[test]
fn test_mismatch() {
    let (s1, msg1) = Spake2::<Ed25519Group>::start_a(
        &Password::new(b"password"),
        &Identity::new(b"idA"),
        &Identity::new(b"idB"),
    );
    let (s2, msg2) = Spake2::<Ed25519Group>::start_b(
        &Password::new(b"password2"),
        &Identity::new(b"idA"),
        &Identity::new(b"idB"),
    );
    let key1 = s1.finish(msg2.as_slice()).unwrap();
    let key2 = s2.finish(msg1.as_slice()).unwrap();
    assert_ne!(key1, key2);
}

#[test]
fn test_reflected_message() {
    let (s1, msg1) = Spake2::<Ed25519Group>::start_a(
        &Password::new(b"password"),
        &Identity::new(b"idA"),
        &Identity::new(b"idB"),
    );
    let r = s1.finish(msg1.as_slice());
    assert_eq!(r.unwrap_err(), Error::BadSide);
}

#[test]
fn test_bad_length() {
    let (s1, msg1) = Spake2::<Ed25519Group>::start_a(
        &Password::new(b"password"),
        &Identity::new(b"idA"),
        &Identity::new(b"idB"),
    );
    let mut msg2 = Vec::<u8>::with_capacity(msg1.len() + 1);
    msg2.resize(msg1.len() + 1, 0u8);
    let r = s1.finish(&msg2);
    assert_eq!(r.unwrap_err(), Error::WrongLength);
}

#[test]
fn test_basic_symmetric() {
    let (s1, msg1) = Spake2::<Ed25519Group>::start_symmetric(
        &Password::new(b"password"),
        &Identity::new(b"idS"),
    );
    let (s2, msg2) = Spake2::<Ed25519Group>::start_symmetric(
        &Password::new(b"password"),
        &Identity::new(b"idS"),
    );
    let key1 = s1.finish(msg2.as_slice()).unwrap();
    let key2 = s2.finish(msg1.as_slice()).unwrap();
    assert_eq!(key1, key2);
}