Compare commits

...

2 Commits

6 changed files with 60 additions and 30 deletions

View File

@ -11,6 +11,8 @@ pub struct Header {
pub key_slot: u32,
pub decrypt_test_data: [u8; 0x10],
pub file_key: [u8; 0x10],
challenge_data: [u8; 0x10],
}
impl Header {
@ -25,6 +27,8 @@ impl Header {
let mut magic = [0u8; 0x10];
magic.copy_from_slice(&buffer[..0x10]);
let challenge_data = get_challenge_data(&magic)?;
let offset_to_data = LE::read_u32(&buffer[0x10..0x14]) as usize;
let crypto_version = LE::read_u32(&buffer[0x14..0x18]);
let key_slot = LE::read_u32(&buffer[0x18..0x1C]);
@ -40,6 +44,7 @@ impl Header {
key_slot,
decrypt_test_data,
file_key,
challenge_data,
})
}
@ -54,6 +59,37 @@ impl Header {
3 => Box::from(DecipherV3::new(self, slot_key)?),
version => Err(KugouError::UnsupportedCipherVersion(version))?,
};
let mut test_data = self.decrypt_test_data;
decipher.decrypt(&mut test_data, 0);
if self.challenge_data != test_data {
Err(KugouError::SelfTestFailed)?;
}
Ok(decipher)
}
}
fn get_challenge_data(magic_header: &[u8; 0x10]) -> Result<[u8; 0x10], KugouError> {
match magic_header {
&KGM_HEADER => Ok(KGM_TEST_DATA),
&VPR_HEADER => Ok(VPR_TEST_DATA),
_ => Err(KugouError::NotKGMFile)?,
}
}
pub const KGM_HEADER: [u8; 16] = [
0x7C, 0xD5, 0x32, 0xEB, 0x86, 0x02, 0x7F, 0x4B, 0xA8, 0xAF, 0xA6, 0x8E, 0x0F, 0xFF, 0x99, 0x14,
];
pub const KGM_TEST_DATA: [u8; 16] = [
0x38, 0x85, 0xED, 0x92, 0x79, 0x5F, 0xF8, 0x4C, 0xB3, 0x03, 0x61, 0x41, 0x16, 0xA0, 0x1D, 0x47,
];
pub const VPR_HEADER: [u8; 16] = [
0x05, 0x28, 0xBC, 0x96, 0xE9, 0xE4, 0x5A, 0x43, 0x91, 0xAA, 0xBD, 0xD0, 0x7A, 0xF5, 0x36, 0x31,
];
pub const VPR_TEST_DATA: [u8; 16] = [
0x1D, 0x5A, 0x05, 0x34, 0x0C, 0x41, 0x8D, 0x42, 0x9C, 0x83, 0x92, 0x6C, 0xAE, 0x16, 0xFE, 0x56,
];

View File

@ -14,6 +14,12 @@ pub enum KugouError {
#[error("Unsupported cipher version: {0}")]
UnsupportedCipherVersion(u32),
#[error("Not KGM File (magic mismatch)")]
NotKGMFile,
#[error("Unsupported cipher (self-test failed)")]
SelfTestFailed,
}
pub trait Decipher {

View File

@ -15,9 +15,8 @@ impl DecipherV2 {
impl Decipher for DecipherV2 {
fn decrypt(&self, buffer: &mut [u8], offset: usize) {
let key_stream = self.key.iter().cycle().skip(offset % self.key.len());
for (datum, &k) in buffer.iter_mut().zip(key_stream) {
*datum ^= k;
for (datum, offset) in buffer.iter_mut().zip(offset..) {
*datum ^= self.key[offset % self.key.len()];
}
}
}

View File

@ -25,34 +25,27 @@ impl DecipherV3 {
Ok(Self { slot_key, file_key })
}
fn offset_key(offset: usize) -> u8 {
let offset_key = (offset as u32).to_ne_bytes();
offset_key[0] ^ offset_key[1] ^ offset_key[2] ^ offset_key[3]
}
}
impl Decipher for DecipherV3 {
fn decrypt(&self, buffer: &mut [u8], offset: usize) {
let slot_key_stream = self
.slot_key
.iter()
.cycle()
.skip(offset % self.slot_key.len());
let file_key_stream = self
.file_key
.iter()
.cycle()
.skip(offset % self.file_key.len());
for (datum, offset) in buffer.iter_mut().zip(offset..) {
let offset_key = Self::offset_key(offset);
let mut offset = offset as u32;
let key_stream = slot_key_stream.zip(file_key_stream);
for (datum, (&slot_key, &file_key)) in buffer.iter_mut().zip(key_stream) {
let offset_key = offset.to_ne_bytes().iter().fold(0, |acc, &x| acc ^ x);
let file_key = self.file_key[offset % self.file_key.len()];
let slot_key = self.slot_key[offset % self.slot_key.len()];
let mut temp = *datum;
temp ^= file_key;
temp ^= temp.wrapping_shl(4);
temp ^= temp << 4;
temp ^= slot_key;
temp ^= offset_key;
*datum = temp;
offset = offset.wrapping_add(1);
}
}
}

View File

@ -11,10 +11,8 @@ const KEY: [u8; 0x20] = [
impl CipherV1 {
pub fn new(resource_id: u32) -> Self {
let mut key = KEY;
for (k, r) in key
.iter_mut()
.zip(resource_id.to_string().as_bytes().iter().cycle())
{
let resource_id = resource_id.to_string();
for (k, &r) in key.iter_mut().zip(resource_id.as_bytes().iter().cycle()) {
*k ^= r;
}
@ -26,10 +24,9 @@ impl CipherV1 {
T: AsMut<[u8]> + ?Sized,
{
let data = data.as_mut();
let key_stream = self.key.iter().cycle().skip(offset % self.key.len());
for (datum, key) in data.iter_mut().zip(key_stream) {
*datum ^= *key;
for (datum, offset) in data.iter_mut().zip(offset..) {
*datum ^= self.key[offset % self.key.len()];
}
}
}

View File

@ -182,9 +182,8 @@ impl NCMFile {
where
T: AsMut<[u8]> + ?Sized,
{
let key_stream = self.audio_rc4_key_stream.iter().cycle().skip(offset & 0xff);
for (datum, &key) in data.as_mut().iter_mut().zip(key_stream) {
*datum ^= key;
for (datum, offset) in data.as_mut().iter_mut().zip(offset..) {
*datum ^= self.audio_rc4_key_stream[offset % self.audio_rc4_key_stream.len()];
}
}
}