diff --git a/Cargo.lock b/Cargo.lock index e83ba0c1..e4a50e10 100644 --- a/Cargo.lock +++ b/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" diff --git a/Cargo.toml b/Cargo.toml index 5fe42dc1..987944e4 100644 --- a/Cargo.toml +++ b/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] diff --git a/src/audio_decrypt.rs b/src/audio_decrypt.rs index dfc384ae..1ff7256a 100644 --- a/src/audio_decrypt.rs +++ b/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 io::Seek for AudioDecrypt { 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, diff --git a/src/keys.rs b/src/keys.rs index 8cfbdc64..da8eb836 100644 --- a/src/keys.rs +++ b/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()); diff --git a/src/lib.rs b/src/lib.rs index 9b96d562..bcf893cd 100644 --- a/src/lib.rs +++ b/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; diff --git a/src/util/mod.rs b/src/util/mod.rs index eb40ad27..c3348510 100644 --- a/src/util/mod.rs +++ b/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; }