1 //! Additional SRP types.
2 use crate::tools::powm;
4 use num_bigint::BigUint;
7 /// SRP authentication error.
8 #[derive(Debug, Copy, Clone, Eq, PartialEq)]
9 pub struct SrpAuthError {
10 pub(crate) description: &'static str,
13 impl fmt::Display for SrpAuthError {
14 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
15 write!(f, "SRP authentication error")
19 impl error::Error for SrpAuthError {
20 fn description(&self) -> &str {
25 /// Group used for SRP computations
26 #[derive(Debug, Clone, Eq, PartialEq)]
28 /// A large safe prime (N = 2q+1, where q is prime)
30 /// A generator modulo N
35 pub(crate) fn powm(&self, v: &BigUint) -> BigUint {
36 powm(&self.g, v, &self.n)
39 /// Compute `k` with given hash function and return SRP parameters
40 pub(crate) fn compute_k<D: Digest>(&self) -> BigUint {
41 let n = self.n.to_bytes_be();
42 let g_bytes = self.g.to_bytes_be();
43 let mut buf = vec![0u8; n.len()];
44 let l = n.len() - g_bytes.len();
45 buf[l..].copy_from_slice(&g_bytes);
50 BigUint::from_bytes_be(&d.finalize().as_slice())
53 /// Compute `Hash(N) xor Hash(g)` with given hash function and return SRP parameters
54 pub(crate) fn compute_hash_n_xor_hash_g<D: Digest>(&self) -> Vec<u8> {
55 let n = self.n.to_bytes_be();
56 let g_bytes = self.g.to_bytes_be();
57 let mut buf = vec![0u8; n.len()];
58 let l = n.len() - g_bytes.len();
59 buf[l..].copy_from_slice(&g_bytes);
63 let h = d.finalize_reset();
64 let h_n: &[u8] = h.as_slice();
66 let h = d.finalize_reset();
67 let h_g: &[u8] = h.as_slice();
71 .map(|(&x1, &x2)| x1 ^ x2)
78 use crate::groups::G_1024;
82 fn test_k_1024_sha1() {
83 let k = G_1024.compute_k::<Sha1>().to_bytes_be();
84 assert_eq!(&k, include_bytes!("k_sha1_1024.bin"));