//! Additional SRP types.
-use std::{fmt, error};
-use num::BigUint;
-use tools::powm;
use digest::Digest;
+use num_bigint::BigUint;
+use std::{error, fmt};
-/// SRP authentification error.
+/// SRP authentication error.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct SrpAuthError {
- pub(crate) description: &'static str
+ pub(crate) description: &'static str,
}
impl fmt::Display for SrpAuthError {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- write!(f, "SRP authentification error")
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ write!(f, "SRP authentication error")
}
}
}
impl SrpGroup {
- pub(crate) fn powm(&self, v: &BigUint) -> BigUint {
- powm(&self.g, v, &self.n)
+ pub(crate) fn modpow(&self, v: &BigUint) -> BigUint {
+ self.g.modpow(v, &self.n)
}
/// Compute `k` with given hash function and return SRP parameters
buf[l..].copy_from_slice(&g_bytes);
let mut d = D::new();
- d.input(&n);
- d.input(&buf);
- BigUint::from_bytes_be(&d.result())
+ d.update(&n);
+ d.update(&buf);
+ BigUint::from_bytes_be(&d.finalize().as_slice())
+ }
+
+ /// Compute `Hash(N) xor Hash(g)` with given hash function and return SRP parameters
+ pub(crate) fn compute_hash_n_xor_hash_g<D: Digest>(&self) -> Vec<u8> {
+ 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.update(&n);
+ let h = d.finalize_reset();
+ let h_n: &[u8] = h.as_slice();
+ d.update(&buf);
+ let h = d.finalize_reset();
+ let h_g: &[u8] = h.as_slice();
+
+ h_n.iter()
+ .zip(h_g.iter())
+ .map(|(&x1, &x2)| x1 ^ x2)
+ .collect()
}
}
#[cfg(test)]
mod tests {
- use ::groups::G_1024;
- use sha_1::Sha1;
+ use crate::groups::G_1024;
+ use sha1::Sha1;
#[test]
fn test_k_1024_sha1() {