]> git.lizzy.rs Git - PAKEs.git/blob - srp/src/utils.rs
a9372bd0649f182c3b49bcbf3458e7873b4c2409
[PAKEs.git] / srp / src / utils.rs
1 use digest::{Digest, Output};
2 use num_bigint::BigUint;
3
4 use crate::types::SrpGroup;
5
6 // u = H(PAD(A) | PAD(B))
7 pub fn compute_u<D: Digest>(a_pub: &[u8], b_pub: &[u8]) -> BigUint {
8     let mut u = D::new();
9     u.update(a_pub);
10     u.update(b_pub);
11     BigUint::from_bytes_be(&u.finalize())
12 }
13
14 // k = H(N | PAD(g))
15 pub fn compute_k<D: Digest>(params: &SrpGroup) -> BigUint {
16     let n = params.n.to_bytes_be();
17     let g_bytes = params.g.to_bytes_be();
18     let mut buf = vec![0u8; n.len()];
19     let l = n.len() - g_bytes.len();
20     buf[l..].copy_from_slice(&g_bytes);
21
22     let mut d = D::new();
23     d.update(&n);
24     d.update(&buf);
25     BigUint::from_bytes_be(d.finalize().as_slice())
26 }
27
28 // M1 = H(A, B, K) this doesn't follow the spec but apparently no one does for M1
29 // M1 should equal =  H(H(N) XOR H(g) | H(U) | s | A | B | K) according to the spec
30 pub fn compute_m1<D: Digest>(a_pub: &[u8], b_pub: &[u8], key: &[u8]) -> Output<D> {
31     let mut d = D::new();
32     d.update(a_pub);
33     d.update(b_pub);
34     d.update(key);
35     d.finalize()
36 }
37
38 // M2 = H(A, M1, K)
39 pub fn compute_m2<D: Digest>(a_pub: &[u8], m1: &Output<D>, key: &[u8]) -> Output<D> {
40     let mut d = D::new();
41     d.update(&a_pub);
42     d.update(&m1);
43     d.update(&key);
44     d.finalize()
45 }