use digest::{Digest, Output};
use num_bigint::BigUint;
-use crate::tools::powm;
use crate::types::{SrpAuthError, SrpGroup};
/// SRP client state before handshake with the server.
/// Create new SRP client instance.
pub fn new(a: &[u8], params: &'a SrpGroup) -> Self {
let a = BigUint::from_bytes_be(a);
- let a_pub = params.powm(&a);
+ let a_pub = params.modpow(&a);
Self {
params,
/// Get password verfier for user registration on the server
pub fn get_password_verifier(&self, private_key: &[u8]) -> Vec<u8> {
let x = BigUint::from_bytes_be(private_key);
- let v = self.params.powm(&x);
+ let v = self.params.modpow(&x);
v.to_bytes_be()
}
fn calc_key(&self, b_pub: &BigUint, x: &BigUint, u: &BigUint) -> Output<D> {
let n = &self.params.n;
let k = self.params.compute_k::<D>();
- let interm = (k * self.params.powm(x)) % n;
+ let interm = (k * self.params.modpow(x)) % n;
// Because we do operation in modulo N we can get: (kv + g^b) < kv
let v = if *b_pub > interm {
(b_pub - &interm) % n
(n + b_pub - &interm) % n
};
// S = |B - kg^x| ^ (a + ux)
- let s = powm(&v, &(&self.a + (u * x) % n), n);
+ let s = v.modpow(&(&self.a + (u * x) % n), n);
D::digest(&s.to_bytes_be())
}
pub mod client;
pub mod groups;
pub mod server;
-mod tools;
pub mod types;
use digest::{Digest, Output};
use num_bigint::BigUint;
-use crate::tools::powm;
use crate::types::{SrpAuthError, SrpGroup};
/// Data provided by users upon registration, usually stored in the database.
let k = params.compute_k::<D>();
// kv + g^b
let interm = (k * &v) % ¶ms.n;
- let b_pub = (interm + ¶ms.powm(&b)) % ¶ms.n;
+ let b_pub = (interm + ¶ms.modpow(&b)) % ¶ms.n;
// H(A || B)
let u = {
let mut d = D::new();
//(Av^u) ^ b
let key = {
let u = BigUint::from_bytes_be(u.as_slice());
- let t = (&a_pub * powm(&v, &u, ¶ms.n)) % ¶ms.n;
- let s = powm(&t, &b, ¶ms.n);
+ let t = (&a_pub * v.modpow(&u, ¶ms.n)) % ¶ms.n;
+ let s = t.modpow(&b, ¶ms.n);
D::digest(&s.to_bytes_be())
};
Ok(Self {
+++ /dev/null
-use num_bigint::BigUint;
-
-pub fn powm(base: &BigUint, exp: &BigUint, modulus: &BigUint) -> BigUint {
- let zero = BigUint::from(0u32);
- let one = BigUint::from(1u32);
- let two = BigUint::from(2u32);
- let mut exp = exp.clone();
- let mut result = one.clone();
- let mut base = base % modulus;
-
- while exp > zero {
- if &exp % &two == one {
- result = (result * &base) % modulus;
- }
- exp >>= 1;
- base = (&base * &base) % modulus;
- }
- result
-}
//! Additional SRP types.
-use crate::tools::powm;
use digest::Digest;
use num_bigint::BigUint;
use std::{error, fmt};
}
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