]> git.lizzy.rs Git - PAKEs.git/commitdiff
srp: replace custom powm with modpow (#78)
authorJosh Brown <josh9051@gmail.com>
Mon, 20 Dec 2021 10:44:13 +0000 (05:44 -0500)
committerGitHub <noreply@github.com>
Mon, 20 Dec 2021 10:44:13 +0000 (10:44 +0000)
srp/src/client.rs
srp/src/lib.rs
srp/src/server.rs
srp/src/tools.rs [deleted file]
srp/src/types.rs

index d47983e8459a8056ded91ecd47221b31f62d26c9..815b195327940c6d12c948a1106bd3f3bd50934a 100644 (file)
@@ -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())
     }
 
index 2d0240093cce69765ae7bf98d80b6395f0983779..ada3b8c0f6e2d95d3cd10ee1774459bb28916f39 100644 (file)
@@ -69,5 +69,4 @@
 pub mod client;
 pub mod groups;
 pub mod server;
-mod tools;
 pub mod types;
index e134861d7e47fa9f0735ba30dada45b39457473d..299c1ced60fc3920afe67a703e33f673e7dc45f3 100644 (file)
@@ -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 (file)
index 7f7da0f..0000000
+++ /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
-}
index de5958a9795bfd5449c6f542618baddffbc9ecf4..41742d53589af270b1c00bc9ff0e1a45c21ad58a 100644 (file)
@@ -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