summaryrefslogtreecommitdiffstats
path: root/src/dh.rs
blob: 5317b29ae1496726ae8400325e57ff84487c6d0f (plain) (blame)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
use num_bigint::RandBigInt;
use rand::Rng;

#[derive(Debug)]
pub struct DHKeyPair {
    pub p: num_bigint::BigUint,
    pub g: num_bigint::BigUint,
    pub pubkey: num_bigint::BigUint,
    privkey: Option<num_bigint::BigUint>,
}

impl DHKeyPair {
    pub fn new(p: num_bigint::BigUint, g: num_bigint::BigUint) -> DHKeyPair {
        let privkey = rand::thread_rng().gen_biguint_below(&p);
        let pubkey = g.modpow(&privkey, &p);
        DHKeyPair {
            p,
            g,
            pubkey,
            privkey: Some(privkey),
        }
    }

    pub fn key_exchange(
        &self,
        other_pubkey: &num_bigint::BigUint,
    ) -> num_bigint::BigUint {
        other_pubkey.modpow(self.privkey.as_ref().unwrap(), &self.p)
    }
}

#[derive(Debug)]
pub struct SRPServer {
    users: std::collections::HashMap<String, SRPUser>,
    sessions: std::collections::HashMap<Vec<u8>, SRPSession>,
    n: num_bigint::BigUint,
    g: num_bigint::BigUint,
    k: num_bigint::BigUint,
}

impl SRPServer {
    pub fn new(
        n: num_bigint::BigUint,
        g: num_bigint::BigUint,
        k: num_bigint::BigUint,
    ) -> SRPServer {
        SRPServer {
            users: std::collections::HashMap::new(),
            sessions: std::collections::HashMap::new(),
            n,
            g,
            k,
        }
    }

    pub fn register(
        &mut self,
        identity: &str,
        salt: &[u8],
        verifier: &num_bigint::BigUint,
    ) {
        self.users
            .insert(identity.to_string(), SRPUser::new(salt, verifier));
    }

    pub fn exchange_pubkeys(
        &mut self,
        user: &str,
        a_pub: &num_bigint::BigUint,
    ) -> (Vec<u8>, Vec<u8>, num_bigint::BigUint) {
        let userdata = self.users.get(user).unwrap();
        let b_priv = rand::thread_rng().gen_biguint_below(&self.n);
        let kv = self.k.clone() * userdata.verifier.clone();
        let b_pub = self.g.modpow(&b_priv, &self.n) + kv;

        let session = SRPSession {
            a_pub: a_pub.clone(),
            b_priv: b_priv,
            b_pub: b_pub.clone(),
            v: userdata.verifier.clone(),
            salt: userdata.salt.clone(),
        };
        let mut session_key = [0; 16];
        rand::thread_rng().fill(&mut session_key);
        let session_key = session_key.to_vec();
        self.sessions.insert(session_key.clone(), session);

        (session_key, userdata.salt.to_vec(), b_pub)
    }

    pub fn verify(&mut self, session: Vec<u8>, hmac: Vec<u8>) -> bool {
        let n = &self.n.clone();

        let session = self.sessions.get(&session).unwrap();

        let uinput =
            [session.a_pub.to_bytes_le(), session.b_pub.to_bytes_le()]
                .concat();
        let uh = crate::sha1::sha1(&uinput);
        let u = num_bigint::BigUint::from_bytes_le(&uh[..]);

        let s = (session.a_pub.clone() * session.v.modpow(&u, n))
            .modpow(&session.b_priv, n);
        let k = crate::sha1::sha1(&s.to_bytes_le());
        let server_hmac = crate::sha1::sha1_hmac(&k, &session.salt);

        hmac == server_hmac
    }
}

#[derive(Debug)]
pub struct SRPUser {
    salt: Vec<u8>,
    verifier: num_bigint::BigUint,
}

impl SRPUser {
    pub fn new(salt: &[u8], verifier: &num_bigint::BigUint) -> SRPUser {
        SRPUser {
            salt: salt.to_vec(),
            verifier: verifier.clone(),
        }
    }
}

#[derive(Debug)]
pub struct SRPSession {
    a_pub: num_bigint::BigUint,
    b_priv: num_bigint::BigUint,
    b_pub: num_bigint::BigUint,
    v: num_bigint::BigUint,
    salt: Vec<u8>,
}

#[derive(Debug)]
pub struct SRPClient<'a> {
    server: &'a mut SRPServer,
}

impl<'a> SRPClient<'a> {
    pub fn new(server: &'a mut SRPServer) -> SRPClient<'a> {
        SRPClient { server }
    }

    pub fn register(&mut self, user: &str, pass: &str) {
        let mut salt = [0; 16];
        rand::thread_rng().fill(&mut salt);
        let input = [&salt[..], pass.as_bytes()].concat();
        let xh = crate::sha1::sha1(&input);
        let x = num_bigint::BigUint::from_bytes_le(&xh[..]);
        let v = self.server.g.modpow(&x, &self.server.n);
        self.server.register(user, &salt, &v);
    }

    pub fn key_exchange(
        &mut self,
        user: &str,
        pass: &str,
    ) -> Option<num_bigint::BigUint> {
        let n = &self.server.n.clone();
        let g = &self.server.g.clone();
        let k = &self.server.k.clone();

        let a_priv = rand::thread_rng().gen_biguint_below(n);
        let a_pub = g.modpow(&a_priv, n);
        let (session, salt, b_pub) =
            self.server.exchange_pubkeys(user, &a_pub);

        let uinput = [a_pub.to_bytes_le(), b_pub.to_bytes_le()].concat();
        let uh = crate::sha1::sha1(&uinput);
        let u = num_bigint::BigUint::from_bytes_le(&uh[..]);

        let xinput = [salt.clone(), pass.as_bytes().to_vec()].concat();
        let xh = crate::sha1::sha1(&xinput);
        let x = num_bigint::BigUint::from_bytes_le(&xh[..]);

        let s = (b_pub - k * g.modpow(&x, n)).modpow(&(a_priv + u * x), n);
        let k = crate::sha1::sha1(&s.to_bytes_le());
        let hmac = crate::sha1::sha1_hmac(&k, &salt);

        if !self.server.verify(session, hmac.to_vec()) {
            return None
        }

        Some(s)
    }
}