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
use crate::tools::powm;
use digest::Digest;
use num_bigint::BigUint;
use std::{error, fmt};
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct SrpAuthError {
pub(crate) description: &'static str,
}
impl fmt::Display for SrpAuthError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "SRP authentication error")
}
}
impl error::Error for SrpAuthError {
fn description(&self) -> &str {
self.description
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct SrpGroup {
pub n: BigUint,
pub g: BigUint,
}
impl SrpGroup {
pub(crate) fn powm(&self, v: &BigUint) -> BigUint {
powm(&self.g, v, &self.n)
}
pub(crate) fn compute_k<D: Digest>(&self) -> BigUint {
let n = self.n.to_bytes_be();
let g_bytes = self.g.to_bytes_be();
let mut buf = vec![0u8; n.len()];
let l = n.len() - g_bytes.len();
buf[l..].copy_from_slice(&g_bytes);
let mut d = D::new();
d.input(&n);
d.input(&buf);
BigUint::from_bytes_be(&d.result())
}
}
#[cfg(test)]
mod tests {
use crate::groups::G_1024;
use sha1::Sha1;
#[test]
fn test_k_1024_sha1() {
let k = G_1024.compute_k::<Sha1>().to_bytes_be();
assert_eq!(&k, include_bytes!("k_sha1_1024.bin"));
}
}