librespot/audio/src/decrypt.rs

47 lines
1.2 KiB
Rust
Raw Normal View History

2015-06-23 14:38:29 +00:00
use std::io;
2018-07-30 11:18:43 +00:00
2021-02-13 18:10:34 +00:00
use aes_ctr::cipher::generic_array::GenericArray;
use aes_ctr::cipher::{NewStreamCipher, SyncStreamCipher, SyncStreamCipherSeek};
use aes_ctr::Aes128Ctr;
2015-06-23 14:38:29 +00:00
2019-09-16 19:00:09 +00:00
use librespot_core::audio_key::AudioKey;
2015-06-23 14:38:29 +00:00
2018-07-30 11:18:43 +00:00
const AUDIO_AESIV: [u8; 16] = [
0x72, 0xe0, 0x67, 0xfb, 0xdd, 0xcb, 0xcf, 0x77, 0xeb, 0xe8, 0xbc, 0x64, 0x3f, 0x63, 0x0d, 0x93,
2018-02-26 01:50:41 +00:00
];
2015-06-23 14:38:29 +00:00
2016-01-02 15:19:39 +00:00
pub struct AudioDecrypt<T: io::Read> {
2018-07-30 11:18:43 +00:00
cipher: Aes128Ctr,
2015-06-23 14:38:29 +00:00
reader: T,
}
2016-01-02 15:19:39 +00:00
impl<T: io::Read> AudioDecrypt<T> {
2015-06-23 17:34:48 +00:00
pub fn new(key: AudioKey, reader: T) -> AudioDecrypt<T> {
2018-07-30 11:18:43 +00:00
let cipher = Aes128Ctr::new(
2021-09-01 19:25:32 +00:00
GenericArray::from_slice(&key.0),
GenericArray::from_slice(&AUDIO_AESIV),
2018-07-30 11:18:43 +00:00
);
AudioDecrypt { cipher, reader }
2015-06-23 14:38:29 +00:00
}
}
2016-01-02 15:19:39 +00:00
impl<T: io::Read> io::Read for AudioDecrypt<T> {
2015-06-23 14:38:29 +00:00
fn read(&mut self, output: &mut [u8]) -> io::Result<usize> {
let len = self.reader.read(output)?;
2015-06-23 14:38:29 +00:00
2018-07-30 11:18:43 +00:00
self.cipher.apply_keystream(&mut output[..len]);
2015-06-23 14:38:29 +00:00
Ok(len)
}
}
2016-01-02 15:19:39 +00:00
impl<T: io::Read + io::Seek> io::Seek for AudioDecrypt<T> {
2015-06-23 17:34:48 +00:00
fn seek(&mut self, pos: io::SeekFrom) -> io::Result<u64> {
let newpos = self.reader.seek(pos)?;
2015-06-23 17:34:48 +00:00
2018-07-30 11:18:43 +00:00
self.cipher.seek(newpos);
2015-06-23 17:34:48 +00:00
2018-07-30 11:18:43 +00:00
Ok(newpos)
2015-06-23 14:38:29 +00:00
}
}