Browse Source

Replace GMP by num’s Bignum implementation.

This is awfully slow in debug builds, but simplifies the build process a
lot.
Paul Lietar 8 years ago
parent
commit
fe56604037
6 changed files with 36 additions and 32 deletions
  1. 0 9
      Cargo.lock
  2. 0 2
      Cargo.toml
  3. 3 4
      src/audio_decrypt.rs
  4. 8 9
      src/keys.rs
  5. 0 1
      src/lib.rs
  6. 25 7
      src/util/mod.rs

+ 0 - 9
Cargo.lock

@@ -15,7 +15,6 @@ dependencies = [
  "rand 0.3.11 (registry+https://github.com/rust-lang/crates.io-index)",
  "rpassword 0.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
  "rust-crypto 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)",
- "rust-gmp 0.2.0 (git+https://github.com/plietar/rust-gmp.git)",
  "shannon 0.1.0 (git+https://github.com/plietar/rust-shannon.git)",
  "tempfile 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
  "time 0.1.33 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -215,14 +214,6 @@ dependencies = [
  "time 0.1.33 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
-[[package]]
-name = "rust-gmp"
-version = "0.2.0"
-source = "git+https://github.com/plietar/rust-gmp.git#d1bb4448fdbfa2505edadb83b6aac6257fe08ba2"
-dependencies = [
- "num 0.1.27 (registry+https://github.com/rust-lang/crates.io-index)",
-]
-
 [[package]]
 name = "rustc-serialize"
 version = "0.3.16"

+ 0 - 2
Cargo.toml

@@ -32,8 +32,6 @@ vorbis      = "~0.0.12"
 
 [dependencies.protobuf_macros]
 git = "https://github.com/plietar/rust-protobuf-macros.git"
-[dependencies.rust-gmp]
-git = "https://github.com/plietar/rust-gmp.git"
 [dependencies.shannon]
 git = "https://github.com/plietar/rust-shannon.git"
 [dependencies.portaudio]

+ 3 - 4
src/audio_decrypt.rs

@@ -1,9 +1,8 @@
 use crypto::aes;
 use crypto::symmetriccipher::SynchronousStreamCipher;
+use num::{BigUint, FromPrimitive};
 use std::io;
 use std::ops::Add;
-use num::FromPrimitive;
-use gmp::Mpz;
 
 use audio_key::AudioKey;
 
@@ -46,8 +45,8 @@ impl <T : io::Read + io::Seek> io::Seek for AudioDecrypt<T> {
         let newpos = try!(self.reader.seek(pos));
         let skip = newpos % 16;
 
-        let iv = Mpz::from_bytes_be(AUDIO_AESIV)
-                    .add(Mpz::from_u64(newpos / 16).unwrap())
+        let iv = BigUint::from_bytes_be(AUDIO_AESIV)
+                    .add(BigUint::from_u64(newpos / 16).unwrap())
                     .to_bytes_be();
         self.cipher = aes::ctr(aes::KeySize::KeySize128,
                                &self.key,

+ 8 - 9
src/keys.rs

@@ -1,15 +1,14 @@
 use crypto;
 use crypto::mac::Mac;
-use gmp::Mpz;
-use num::FromPrimitive;
+use num::{BigUint, FromPrimitive};
 use rand;
 use std::io::Write;
 
 use util;
 
 lazy_static! {
-    static ref DH_GENERATOR: Mpz = Mpz::from_u64(0x2).unwrap();
-    static ref DH_PRIME: Mpz = Mpz::from_bytes_be(&[
+    static ref DH_GENERATOR: BigUint = BigUint::from_u64(0x2).unwrap();
+    static ref DH_PRIME: BigUint = BigUint::from_bytes_be(&[
         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc9,
         0x0f, 0xda, 0xa2, 0x21, 0x68, 0xc2, 0x34, 0xc4, 0xc6,
         0x62, 0x8b, 0x80, 0xdc, 0x1c, 0xd1, 0x29, 0x02, 0x4e,
@@ -24,8 +23,8 @@ lazy_static! {
 }
 
 pub struct PrivateKeys {
-    private_key: Mpz,
-    public_key: Mpz,
+    private_key: BigUint,
+    public_key: BigUint,
 }
 
 pub struct SharedKeys {
@@ -42,8 +41,8 @@ impl PrivateKeys {
     }
 
     pub fn new_with_key(key_data: &[u8]) -> PrivateKeys {
-        let private_key = Mpz::from_bytes_be(key_data);
-        let public_key = DH_GENERATOR.powm(&private_key, &DH_PRIME);
+        let private_key = BigUint::from_bytes_be(key_data);
+        let public_key = util::powm(&DH_GENERATOR, &private_key, &DH_PRIME);
 
         PrivateKeys {
             private_key: private_key,
@@ -62,7 +61,7 @@ impl PrivateKeys {
     }
 
     pub fn add_remote_key(self, remote_key: &[u8], client_packet: &[u8], server_packet: &[u8]) -> SharedKeys {
-        let shared_key = Mpz::from_bytes_be(remote_key).powm(&self.private_key, &DH_PRIME);
+        let shared_key = util::powm(&BigUint::from_bytes_be(remote_key), &self.private_key, &DH_PRIME);
 
         let mut data = Vec::with_capacity(0x64);
         let mut mac = crypto::hmac::Hmac::new(crypto::sha1::Sha1::new(), &shared_key.to_bytes_be());

+ 0 - 1
src/lib.rs

@@ -9,7 +9,6 @@ extern crate bit_set;
 extern crate byteorder;
 extern crate crypto;
 extern crate eventual;
-extern crate gmp;
 extern crate num;
 extern crate portaudio;
 extern crate protobuf;

+ 25 - 7
src/util/mod.rs

@@ -1,8 +1,10 @@
+use num::{BigUint, Integer, Zero, One};
 use rand::{Rng,Rand};
-use time;
 use std::io;
+use std::ops::{Mul, Rem, Shr};
 use std::fs;
 use std::path::Path;
+use time;
 
 mod int128;
 mod spotify_id;
@@ -79,11 +81,27 @@ pub fn now_ms() -> i64 {
 }
 
 pub fn mkdir_existing(path: &Path) -> io::Result<()> {
-        fs::create_dir(path)
-            .or_else(|err| if err.kind() == io::ErrorKind::AlreadyExists {
-                Ok(())
-            } else {
-                Err(err)
-            })
+    fs::create_dir(path)
+        .or_else(|err| if err.kind() == io::ErrorKind::AlreadyExists {
+            Ok(())
+        } else {
+            Err(err)
+        })
+}
+
+pub fn powm(base: &BigUint, exp: &BigUint, modulus: &BigUint) -> BigUint {
+    let mut base = base.clone();
+    let mut exp = exp.clone();
+    let mut result : BigUint = One::one();
+
+    while !exp.is_zero() {
+        if exp.is_odd() {
+            result = result.mul(&base).rem(modulus);
+        }
+        exp = exp.shr(1);
+        base = (&base).mul(&base).rem(modulus);
+    }
+
+    return result;
 }