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
#![forbid(unsafe_code)]
#![cfg_attr(test, deny(warnings))]

extern crate curve25519_dalek;
extern crate hex;
extern crate hkdf;
extern crate num_bigint;
extern crate rand;
extern crate sha2;

mod spake2;
pub use spake2::*;

#[cfg(test)]
mod tests {
    use spake2::{Ed25519Group, ErrorType, Identity, Password, SPAKEErr, 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(),
            SPAKEErr {
                kind: ErrorType::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(),
            SPAKEErr {
                kind: ErrorType::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);
    }

    #[test]
    fn it_works() {}

    #[test]
    #[should_panic(expected = "nope")]
    fn it_panics() {
        assert!(false, "nope");
    }
}