aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJosh Brown <josh9051@gmail.com>2021-12-20 05:44:13 -0500
committerGitHub <noreply@github.com>2021-12-20 10:44:13 +0000
commite38144177199721c3bf3f5e2cde403c2ce29c67e (patch)
tree468b1cf281b5c0a11f7e3e8d34ef1f705832fa32
parenta74a157d66586cf7c608ef5a712badde6325dfb8 (diff)
downloadPAKEs-e38144177199721c3bf3f5e2cde403c2ce29c67e.tar.xz
srp: replace custom powm with modpow (#78)
-rw-r--r--srp/src/client.rs9
-rw-r--r--srp/src/lib.rs1
-rw-r--r--srp/src/server.rs7
-rw-r--r--srp/src/tools.rs19
-rw-r--r--srp/src/types.rs5
5 files changed, 9 insertions, 32 deletions
diff --git a/srp/src/client.rs b/srp/src/client.rs
index d47983e..815b195 100644
--- a/srp/src/client.rs
+++ b/srp/src/client.rs
@@ -62,7 +62,6 @@ use std::marker::PhantomData;
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.
@@ -102,7 +101,7 @@ impl<'a, D: Digest> SrpClient<'a, D> {
/// 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,
@@ -115,14 +114,14 @@ impl<'a, D: Digest> SrpClient<'a, D> {
/// 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
@@ -130,7 +129,7 @@ impl<'a, D: Digest> SrpClient<'a, D> {
(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())
}
diff --git a/srp/src/lib.rs b/srp/src/lib.rs
index 2d02400..ada3b8c 100644
--- a/srp/src/lib.rs
+++ b/srp/src/lib.rs
@@ -69,5 +69,4 @@
pub mod client;
pub mod groups;
pub mod server;
-mod tools;
pub mod types;
diff --git a/srp/src/server.rs b/srp/src/server.rs
index e134861..299c1ce 100644
--- a/srp/src/server.rs
+++ b/srp/src/server.rs
@@ -39,7 +39,6 @@ use std::marker::PhantomData;
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.
@@ -81,7 +80,7 @@ impl<D: Digest> SrpServer<D> {
let k = params.compute_k::<D>();
// kv + g^b
let interm = (k * &v) % &params.n;
- let b_pub = (interm + &params.powm(&b)) % &params.n;
+ let b_pub = (interm + &params.modpow(&b)) % &params.n;
// H(A || B)
let u = {
let mut d = D::new();
@@ -93,8 +92,8 @@ impl<D: Digest> SrpServer<D> {
//(Av^u) ^ b
let key = {
let u = BigUint::from_bytes_be(u.as_slice());
- let t = (&a_pub * powm(&v, &u, &params.n)) % &params.n;
- let s = powm(&t, &b, &params.n);
+ let t = (&a_pub * v.modpow(&u, &params.n)) % &params.n;
+ let s = t.modpow(&b, &params.n);
D::digest(&s.to_bytes_be())
};
Ok(Self {
diff --git a/srp/src/tools.rs b/srp/src/tools.rs
deleted file mode 100644
index 7f7da0f..0000000
--- a/srp/src/tools.rs
+++ /dev/null
@@ -1,19 +0,0 @@
-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
-}
diff --git a/srp/src/types.rs b/srp/src/types.rs
index de5958a..41742d5 100644
--- a/srp/src/types.rs
+++ b/srp/src/types.rs
@@ -1,5 +1,4 @@
//! Additional SRP types.
-use crate::tools::powm;
use digest::Digest;
use num_bigint::BigUint;
use std::{error, fmt};
@@ -32,8 +31,8 @@ pub struct SrpGroup {
}
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