mirror of
https://github.com/librespot-org/librespot.git
synced 2025-10-03 09:49:31 +02:00
aes-ctr
This commit is contained in:
parent
e4677027d2
commit
1f1cd116e7
7 changed files with 87 additions and 38 deletions
|
@ -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 }
|
||||
|
|
|
@ -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<T: io::Read> {
|
||||
cipher: Box<SynchronousStreamCipher + 'static>,
|
||||
key: AudioKey,
|
||||
cipher: Aes128Ctr,
|
||||
reader: T,
|
||||
}
|
||||
|
||||
impl<T: io::Read> AudioDecrypt<T> {
|
||||
pub fn new(key: AudioKey, reader: T) -> AudioDecrypt<T> {
|
||||
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<T: io::Read> io::Read for AudioDecrypt<T> {
|
||||
fn read(&mut self, output: &mut [u8]) -> io::Result<usize> {
|
||||
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<T: io::Read> io::Read for AudioDecrypt<T> {
|
|||
impl<T: io::Read + io::Seek> io::Seek for AudioDecrypt<T> {
|
||||
fn seek(&mut self, pos: io::SeekFrom) -> io::Result<u64> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue