From 1f1cd116e7767579da0c2949347f15b59ae30d1c Mon Sep 17 00:00:00 2001 From: newpavlov Date: Mon, 30 Jul 2018 14:18:43 +0300 Subject: [PATCH] aes-ctr --- Cargo.lock | 57 ++++++++++++++++++++++++++++++++++++++-- audio/Cargo.toml | 2 +- audio/src/decrypt.rs | 47 ++++++++++++++------------------- audio/src/lib.rs | 1 + connect/Cargo.toml | 2 +- connect/src/discovery.rs | 14 ++++++---- connect/src/lib.rs | 2 +- 7 files changed, 87 insertions(+), 38 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9d291d73..4b2c187b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,6 +8,17 @@ dependencies = [ "block-cipher-trait 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "aes-ctr" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "aes-soft 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "aesni 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "ctr 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "stream-cipher 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "aes-soft" version = "0.1.0" @@ -18,6 +29,16 @@ dependencies = [ "opaque-debug 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "aes-soft" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "block-cipher-trait 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", + "byte-tools 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "opaque-debug 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "aesni" version = "0.3.5" @@ -27,6 +48,16 @@ dependencies = [ "opaque-debug 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "aesni" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "block-cipher-trait 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", + "opaque-debug 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "stream-cipher 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "aho-corasick" version = "0.6.4" @@ -213,6 +244,15 @@ dependencies = [ "generic-array 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "ctr" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "block-cipher-trait 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", + "stream-cipher 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "digest" version = "0.7.5" @@ -518,7 +558,7 @@ dependencies = [ name = "librespot-audio" version = "0.1.0" dependencies = [ - "aes 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "aes-ctr 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "bit-set 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "byteorder 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", @@ -536,7 +576,7 @@ dependencies = [ name = "librespot-connect" version = "0.1.0" dependencies = [ - "aes 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "aes-ctr 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "base64 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", "block-modes 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "dns-sd 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1115,6 +1155,14 @@ dependencies = [ "ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "stream-cipher" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "generic-array 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "syn" version = "0.11.11" @@ -1538,8 +1586,11 @@ dependencies = [ [metadata] "checksum aes 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0275405eedf13afd19de588add12a3b0d481a50b194eeb826e9dece11e741331" +"checksum aes-ctr 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f65958ff3692041c36fc009261ccd63f24cd8e0dc1164266f068c2387e8b4e4f" "checksum aes-soft 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "91f401742d8c1b0a3d01f53563f98d8ef0beea460b8d37322faf9fb4c7977cfa" +"checksum aes-soft 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "67cc03b0a090a05cb01e96998a01905d7ceedce1bc23b756c0bb7faa0682ccb1" "checksum aesni 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "b1ca074691b47c3dc585e05e45f6d069c75d0209069ca09b1c49ea37720e7b5f" +"checksum aesni 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f2838c142db62c0c6aea0a24054c46d35488532fdaea0f51dbeba430f0985df5" "checksum aho-corasick 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "d6531d44de723825aa81398a6415283229725a00fa30713812ab9323faa82fc4" "checksum alsa 0.0.1 (git+https://github.com/plietar/rust-alsa)" = "" "checksum arrayref 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "0fd1479b7c29641adbd35ff3b5c293922d696a92f25c8c975da3e0acbc87258f" @@ -1566,6 +1617,7 @@ dependencies = [ "checksum crossbeam-utils 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "2760899e32a1d58d5abb31129f8fae5de75220bc2176e77ff7c627ae45c918d9" "checksum crossbeam-utils 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d636a8b3bcc1b409d7ffd3facef8f21dcb4009626adbd0c5e6c4305c07253c7b" "checksum crypto-mac 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7afa06d05a046c7a47c3a849907ec303504608c927f4e85f7bfff22b7180d971" +"checksum ctr 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "50ac3add446ec1f8fe3dc007cd838f5b22bbf33186394feac505451ecc43c018" "checksum digest 0.7.5 (registry+https://github.com/rust-lang/crates.io-index)" = "5b29c278aa8fd30796bd977169e8004b4aa88cdcd2f32a6eb22bc2d5d38df94a" "checksum dns-parser 0.3.2 (git+https://github.com/plietar/dns-parser)" = "" "checksum dns-sd 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d748509dea20228f63ba519bf142ce2593396386125b01f5b0d6412dab972087" @@ -1660,6 +1712,7 @@ dependencies = [ "checksum slab 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fdeff4cd9ecff59ec7e3744cbca73dfe5ac35c2aedb2cfba8a1c715a18912e9d" "checksum smallvec 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4c8cbcd6df1e117c2210e13ab5109635ad68a929fcbb8964dc965b76cb5ee013" "checksum socket2 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "36b4896961171cd3317c7e9603d88f379f8c6e45342212235d356496680c68fd" +"checksum stream-cipher 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac49bc6cb2847200d18bfb738ce89448570f4aa1c34ac0348db6205ee69a0777" "checksum syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad" "checksum syn 0.13.4 (registry+https://github.com/rust-lang/crates.io-index)" = "90d5efaad92a0f96c629ae16302cc9591915930fd49ff0dcc6b4cde146782397" "checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6" diff --git a/audio/Cargo.toml b/audio/Cargo.toml index 29b9522d..5e37e717 100644 --- a/audio/Cargo.toml +++ b/audio/Cargo.toml @@ -15,7 +15,7 @@ log = "0.3.5" num-bigint = "0.1.35" num-traits = "0.1.36" tempfile = "2.1" -aes = "0.1" +aes-ctr = "0.1.0" tremor = { git = "https://github.com/plietar/rust-tremor", optional = true } vorbis = { version ="0.1.0", optional = true } diff --git a/audio/src/decrypt.rs b/audio/src/decrypt.rs index 3b2967d4..31663ae9 100644 --- a/audio/src/decrypt.rs +++ b/audio/src/decrypt.rs @@ -1,39 +1,38 @@ -use crypto::aes; -use crypto::symmetriccipher::SynchronousStreamCipher; -use num_bigint::BigUint; -use num_traits::FromPrimitive; use std::io; -use std::ops::Add; + +use aes_ctr::Aes128Ctr; +use aes_ctr::stream_cipher::{ + NewFixStreamCipher, StreamCipherCore, StreamCipherSeek +}; +use aes_ctr::stream_cipher::generic_array::GenericArray; use core::audio_key::AudioKey; -const AUDIO_AESIV: &'static [u8] = &[ - 0x72, 0xe0, 0x67, 0xfb, 0xdd, 0xcb, 0xcf, 0x77, 0xeb, 0xe8, 0xbc, 0x64, 0x3f, 0x63, 0x0d, 0x93, +const AUDIO_AESIV: [u8; 16] = [ + 0x72, 0xe0, 0x67, 0xfb, 0xdd, 0xcb, 0xcf, 0x77, + 0xeb, 0xe8, 0xbc, 0x64, 0x3f, 0x63, 0x0d, 0x93, ]; pub struct AudioDecrypt { - cipher: Box, - key: AudioKey, + cipher: Aes128Ctr, reader: T, } impl AudioDecrypt { pub fn new(key: AudioKey, reader: T) -> AudioDecrypt { - let cipher = aes::ctr(aes::KeySize::KeySize128, &key.0, AUDIO_AESIV); - AudioDecrypt { - cipher: cipher, - key: key, - reader: reader, - } + let cipher = Aes128Ctr::new( + &GenericArray::from_slice(&key.0), + &GenericArray::from_slice(&AUDIO_AESIV), + ); + AudioDecrypt { cipher, reader } } } impl io::Read for AudioDecrypt { fn read(&mut self, output: &mut [u8]) -> io::Result { - let mut buffer = vec![0u8; output.len()]; - let len = try!(self.reader.read(&mut buffer)); + let len = try!(self.reader.read(output)); - self.cipher.process(&buffer[..len], &mut output[..len]); + self.cipher.apply_keystream(&mut output[..len]); Ok(len) } @@ -42,17 +41,9 @@ impl io::Read for AudioDecrypt { impl io::Seek for AudioDecrypt { fn seek(&mut self, pos: io::SeekFrom) -> io::Result { let newpos = try!(self.reader.seek(pos)); - let skip = newpos % 16; - 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.0, &iv); + self.cipher.seek(newpos); - let buf = vec![0u8; skip as usize]; - let mut buf2 = vec![0u8; skip as usize]; - self.cipher.process(&buf, &mut buf2); - - Ok(newpos as u64) + Ok(newpos) } } diff --git a/audio/src/lib.rs b/audio/src/lib.rs index 7073ece2..cd6f28e3 100644 --- a/audio/src/lib.rs +++ b/audio/src/lib.rs @@ -8,6 +8,7 @@ extern crate byteorder; extern crate num_bigint; extern crate num_traits; extern crate tempfile; +extern crate aes_ctr; extern crate librespot_core as core; diff --git a/connect/Cargo.toml b/connect/Cargo.toml index 91a1111c..c21c9c66 100644 --- a/connect/Cargo.toml +++ b/connect/Cargo.toml @@ -25,7 +25,7 @@ tokio-core = "0.1.2" url = "1.3" sha-1 = "0.7.0" hmac = "0.6.2" -aes = "0.1.0" +aes-ctr = "0.1.0" block-modes = "0.1.0" dns-sd = { version = "0.1.3", optional = true } diff --git a/connect/src/discovery.rs b/connect/src/discovery.rs index 87b857e8..28707baa 100644 --- a/connect/src/discovery.rs +++ b/connect/src/discovery.rs @@ -1,7 +1,9 @@ use base64; use sha1::{Sha1, Digest}; use hmac::{Hmac, Mac}; -use aes::Aes128; +use aes_ctr::Aes128Ctr; +use aes_ctr::stream_cipher::{NewFixStreamCipher, StreamCipherCore}; +use aes_ctr::stream_cipher::generic_array::GenericArray; use futures::sync::mpsc; use futures::{Future, Poll, Stream}; use hyper::server::{Http, Request, Response, Service}; @@ -135,10 +137,12 @@ impl Discovery { assert_eq!(&mac[..], cksum); let decrypted = { - let mut data = vec![0u8; encrypted.len()]; - //let mut cipher = - // crypto::aes::ctr(crypto::aes::KeySize::KeySize128, &encryption_key[0..16], iv); - //cipher.process(encrypted, &mut data); + let mut data = encrypted.to_vec(); + let mut cipher = Aes128Ctr::new( + &GenericArray::from_slice(&encryption_key[0..16]), + &GenericArray::from_slice(iv), + ); + cipher.apply_keystream(&mut data); String::from_utf8(data).unwrap() }; diff --git a/connect/src/lib.rs b/connect/src/lib.rs index a09cd739..ec94faf2 100644 --- a/connect/src/lib.rs +++ b/connect/src/lib.rs @@ -14,7 +14,7 @@ extern crate url; extern crate sha1; extern crate hmac; -extern crate aes; +extern crate aes_ctr; extern crate block_modes; #[cfg(feature = "with-dns-sd")]