]> git.lizzy.rs Git - PAKEs.git/blobdiff - srp/src/server.rs
reformat all code to match rustfmt-1.29.0
[PAKEs.git] / srp / src / server.rs
index 137d6b3324d35bdcc1bfe49fa39c3dc6bcc5bf6c..a5d49ab56ba310741bc68abaca9b4df63dddb923 100644 (file)
@@ -4,7 +4,7 @@
 //! First receive user's username and public value `a_pub`, retrieve from a
 //! database `UserRecord` for a given username, generate `b` (e.g. 512 bits
 //! long) and initialize SRP server instance:
-//! 
+//!
 //! ```ignore
 //! use srp::groups::G_2048;
 //!
 //! let b = rng.gen_iter::<u8>().take(64).collect::<Vec<u8>>();
 //! let server = SrpServer::<Sha256>::new(&user, &a_pub, &b, &G_2048)?;
 //! ```
-//! 
+//!
 //! Next send to user `b_pub` and `salt` from user record:
-//! 
+//!
 //! ```ignore
 //! let b_pub = server.get_b_pub();
 //! conn.reply_to_handshake(&user.salt, b_pub);
 //! ```
-//! 
+//!
 //! And finally recieve user proof, verify it and send server proof in the
 //! reply:
-//! 
+//!
 //! ```ignore
 //! let user_proof = conn.receive_proof();
 //! let server_proof = server.verify(user_proof)?;
 //! conn.send_proof(server_proof);
 //! ```
-//! 
+//!
 //! To get the shared secret use `get_key` method. As alternative to using
 //! `verify` method it's also possible to use this key for authentificated
 //! encryption.
 use std::marker::PhantomData;
 
-use num::{BigUint, Zero};
 use digest::Digest;
 use generic_array::GenericArray;
+use num::{BigUint, Zero};
 
 use tools::powm;
 use types::{SrpAuthError, SrpGroup};
@@ -58,21 +58,26 @@ pub struct SrpServer<D: Digest> {
 
     key: GenericArray<u8, D::OutputSize>,
 
-    d: PhantomData<D>
+    d: PhantomData<D>,
 }
 
-impl< D: Digest> SrpServer< D> {
+impl<D: Digest> SrpServer<D> {
     /// Create new server state.
-    pub fn new(user: &UserRecord, a_pub: &[u8], b: &[u8], params: &SrpGroup)
-        -> Result<Self, SrpAuthError>
-    {
+    pub fn new(
+        user: &UserRecord,
+        a_pub: &[u8],
+        b: &[u8],
+        params: &SrpGroup,
+    ) -> Result<Self, SrpAuthError> {
         let a_pub = BigUint::from_bytes_be(a_pub);
         // Safeguard against malicious A
         if &a_pub % &params.n == BigUint::zero() {
-            return Err(SrpAuthError { description: "Malicious a_pub value" })
+            return Err(SrpAuthError {
+                description: "Malicious a_pub value",
+            });
         }
         let v = BigUint::from_bytes_be(user.verifier);
-        let b = BigUint::from_bytes_be(b)  % &params.n;
+        let b = BigUint::from_bytes_be(b) % &params.n;
         let k = params.compute_k::<D>();
         // kv + g^b
         let interm = (k * &v) % &params.n;
@@ -87,12 +92,18 @@ impl< D: Digest> SrpServer< D> {
         let d = Default::default();
         //(Av^u) ^ b
         let key = {
-            let u =  BigUint::from_bytes_be(&u);
+            let u = BigUint::from_bytes_be(&u);
             let t = (&a_pub * powm(&v, &u, &params.n)) % &params.n;
             let s = powm(&t, &b, &params.n);
             D::digest(&s.to_bytes_be())
         };
-        Ok(Self { b, a_pub, b_pub, key, d})
+        Ok(Self {
+            b,
+            a_pub,
+            b_pub,
+            key,
+            d,
+        })
     }
 
     /// Get private `b` value. (see `new_with_b` documentation)
@@ -113,9 +124,10 @@ impl< D: Digest> SrpServer< D> {
 
     /// Process user proof of having the same shared secret and compute
     /// server proof for sending to the user.
-    pub fn verify(&self, user_proof: &[u8])
-        -> Result<GenericArray<u8, D::OutputSize>, SrpAuthError>
-    {
+    pub fn verify(
+        &self,
+        user_proof: &[u8],
+    ) -> Result<GenericArray<u8, D::OutputSize>, SrpAuthError> {
         // M = H(A, B, K)
         let mut d = D::new();
         d.input(&self.a_pub.to_bytes_be());
@@ -130,7 +142,9 @@ impl< D: Digest> SrpServer< D> {
             d.input(&self.key);
             Ok(d.result())
         } else {
-            Err(SrpAuthError { description: "Incorrect user proof" })
+            Err(SrpAuthError {
+                description: "Incorrect user proof",
+            })
         }
     }
 }