]> git.lizzy.rs Git - PAKEs.git/blob - srp/src/utils.rs
clippy: needless borrow
[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(H(N) XOR H(g) | H(U) | s | A | B | K)
29 pub fn compute_m1<D: Digest>(
30     params: &SrpGroup,
31     a_pub: &[u8],
32     b_pub: &[u8],
33     key: &[u8],
34 ) -> Output<D> {
35     let mut d_n = D::new();
36     d_n.update(params.n.to_bytes_be());
37     let h_n = d_n.finalize();
38
39     let mut d_g = D::new();
40     d_g.update(params.g.to_bytes_be());
41     let h_g = d_g.finalize();
42
43     let ng_xor: Vec<u8> = h_n.iter().zip(h_g.iter()).map(|(n, g)| n ^ g).collect();
44
45     let mut d = D::new();
46     d.update(ng_xor);
47     d.update(a_pub);
48     d.update(b_pub);
49     d.update(key);
50     d.finalize()
51 }
52
53 // M2 = H(A, M1, K)
54 pub fn compute_m2<D: Digest>(a_pub: &[u8], m1: &Output<D>, key: &[u8]) -> Output<D> {
55     let mut d = D::new();
56     d.update(a_pub);
57     d.update(m1);
58     d.update(key);
59     d.finalize()
60 }