mirror of
https://git.um-react.app/um/lib_um_crypto_rust.git
synced 2026-03-08 04:29:54 +00:00
feat: add kgm v5 (kgg) support.
This commit is contained in:
BIN
um_crypto/kgm/src/__fixtures__/kgm_invalid_magic.bin
Normal file
BIN
um_crypto/kgm/src/__fixtures__/kgm_invalid_magic.bin
Normal file
Binary file not shown.
BIN
um_crypto/kgm/src/__fixtures__/kgm_v3_hdr.bin
Normal file
BIN
um_crypto/kgm/src/__fixtures__/kgm_v3_hdr.bin
Normal file
Binary file not shown.
BIN
um_crypto/kgm/src/__fixtures__/kgm_v5_hdr.bin
Normal file
BIN
um_crypto/kgm/src/__fixtures__/kgm_v5_hdr.bin
Normal file
Binary file not shown.
@@ -1,39 +1,69 @@
|
||||
use crate::KugouError;
|
||||
use byteorder::{ByteOrder, LE};
|
||||
use byteorder::{ReadBytesExt, LE};
|
||||
use std::io::{BufReader, Read};
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Header {
|
||||
pub magic: [u8; 0x10],
|
||||
pub offset_to_data: usize,
|
||||
pub crypto_version: u32,
|
||||
pub key_slot: u32,
|
||||
pub key_slot: i32,
|
||||
pub decrypt_test_data: [u8; 0x10],
|
||||
pub file_key: [u8; 0x10],
|
||||
|
||||
challenge_data: [u8; 0x10],
|
||||
pub audio_hash: String,
|
||||
}
|
||||
|
||||
impl Header {
|
||||
pub fn from_buffer<T>(buffer: T) -> Result<Self, KugouError>
|
||||
where
|
||||
T: AsRef<[u8]>,
|
||||
{
|
||||
let buffer = buffer.as_ref();
|
||||
if buffer.len() < 0x3c {
|
||||
Err(KugouError::HeaderTooSmall(0x3c))?;
|
||||
}
|
||||
pub trait HeaderReaderHelper: Read {
|
||||
fn read_u32_le(&mut self) -> Result<u32, KugouError> {
|
||||
self.read_u32::<LE>()
|
||||
.map_err(KugouError::HeaderParseIOError)
|
||||
}
|
||||
|
||||
fn read_i32_le(&mut self) -> Result<i32, KugouError> {
|
||||
self.read_i32::<LE>()
|
||||
.map_err(KugouError::HeaderParseIOError)
|
||||
}
|
||||
|
||||
fn read_buff<T: AsMut<[u8]>>(&mut self, buffer: &mut T) -> Result<(), KugouError> {
|
||||
self.read_exact(buffer.as_mut())
|
||||
.map_err(KugouError::HeaderParseIOError)
|
||||
}
|
||||
}
|
||||
impl<R: Read + ?Sized> HeaderReaderHelper for R {}
|
||||
|
||||
impl Header {
|
||||
pub fn from_reader<T>(reader: &mut T) -> Result<Self, KugouError>
|
||||
where
|
||||
T: Read,
|
||||
{
|
||||
let mut magic = [0u8; 0x10];
|
||||
magic.copy_from_slice(&buffer[..0x10]);
|
||||
reader.read_buff(&mut magic)?;
|
||||
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]);
|
||||
let mut decrypt_test_data = [0u8; 0x10];
|
||||
decrypt_test_data.copy_from_slice(&buffer[0x1c..0x2c]);
|
||||
let mut file_key = [0u8; 0x10];
|
||||
file_key.copy_from_slice(&buffer[0x2c..0x3c]);
|
||||
let mut audio_hash = "".to_string();
|
||||
|
||||
let offset_to_data = reader.read_u32_le()? as usize;
|
||||
let crypto_version = reader.read_u32_le()?;
|
||||
let key_slot = reader.read_i32_le()?;
|
||||
reader.read_buff(&mut decrypt_test_data)?;
|
||||
reader.read_buff(&mut file_key)?;
|
||||
|
||||
if crypto_version == 5 {
|
||||
let mut unused_padding = [0u8; 0x08];
|
||||
reader.read_buff(&mut unused_padding)?; // seek 8 bytes
|
||||
let audio_hash_size = reader.read_u32_le()? as usize;
|
||||
if audio_hash_size != 0x20 {
|
||||
Err(KugouError::HeaderInvalidAudioHash(audio_hash_size))?;
|
||||
}
|
||||
|
||||
let mut audio_hash_bytes = vec![0u8; audio_hash_size];
|
||||
reader.read_buff(&mut audio_hash_bytes)?;
|
||||
audio_hash = String::from_utf8_lossy(&audio_hash_bytes).to_string();
|
||||
}
|
||||
|
||||
Ok(Self {
|
||||
magic,
|
||||
@@ -43,9 +73,23 @@ impl Header {
|
||||
decrypt_test_data,
|
||||
file_key,
|
||||
challenge_data,
|
||||
audio_hash,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn from_buffer<T>(buffer: T) -> Result<Self, KugouError>
|
||||
where
|
||||
T: AsRef<[u8]>,
|
||||
{
|
||||
let buffer = buffer.as_ref();
|
||||
if buffer.len() < 0x40 {
|
||||
Err(KugouError::HeaderTooSmall(0x40))?;
|
||||
}
|
||||
|
||||
let mut reader = BufReader::new(buffer);
|
||||
Self::from_reader(&mut reader)
|
||||
}
|
||||
|
||||
pub fn get_challenge_data(&self) -> [u8; 0x10] {
|
||||
self.challenge_data
|
||||
}
|
||||
@@ -74,3 +118,50 @@ pub const VPR_HEADER: [u8; 16] = [
|
||||
pub const VPR_TEST_DATA: [u8; 16] = [
|
||||
0x1D, 0x5A, 0x05, 0x34, 0x0C, 0x41, 0x8D, 0x42, 0x9C, 0x83, 0x92, 0x6C, 0xAE, 0x16, 0xFE, 0x56,
|
||||
];
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::header::Header;
|
||||
use crate::KugouError;
|
||||
|
||||
#[test]
|
||||
fn parse_header_error_too_small() {
|
||||
assert!(matches!(
|
||||
Header::from_buffer(b"invalid file"),
|
||||
Err(KugouError::HeaderTooSmall(_))
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_header_error_file_magic() {
|
||||
assert!(matches!(
|
||||
Header::from_buffer(include_bytes!("__fixtures__/kgm_invalid_magic.bin")),
|
||||
Err(KugouError::NotKGMFile)
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_header_v2() -> Result<(), KugouError> {
|
||||
let hdr = Header::from_buffer(include_bytes!("__fixtures__/kgm_v2_hdr.bin"))?;
|
||||
assert_eq!(hdr.key_slot, 1);
|
||||
assert_eq!(hdr.crypto_version, 2);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_header_v3() -> Result<(), KugouError> {
|
||||
let hdr = Header::from_buffer(include_bytes!("__fixtures__/kgm_v3_hdr.bin"))?;
|
||||
assert_eq!(hdr.key_slot, 1);
|
||||
assert_eq!(hdr.crypto_version, 3);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_header_v5() -> Result<(), KugouError> {
|
||||
let hdr = Header::from_buffer(include_bytes!("__fixtures__/kgm_v5_hdr.bin"))?;
|
||||
assert_eq!(hdr.key_slot, -1);
|
||||
assert_eq!(hdr.crypto_version, 5);
|
||||
assert_eq!(hdr.audio_hash, "81a26217da847692e7688e0a5ebe9da1");
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ pub mod header;
|
||||
mod pc_db_decrypt;
|
||||
pub mod v2;
|
||||
pub mod v3;
|
||||
mod v5;
|
||||
|
||||
pub use pc_db_decrypt::decrypt_db;
|
||||
|
||||
@@ -10,6 +11,7 @@ use crate::v2::DecipherV2;
|
||||
use crate::v3::DecipherV3;
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::v5::DecipherV5;
|
||||
use block_padding::UnpadError;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
@@ -18,11 +20,14 @@ pub enum KugouError {
|
||||
HeaderTooSmall(usize),
|
||||
|
||||
#[error("Unsupported key slot: {0}")]
|
||||
UnsupportedKeySlot(u32),
|
||||
UnsupportedKeySlot(i32),
|
||||
|
||||
#[error("Unsupported cipher version: {0}")]
|
||||
UnsupportedCipherVersion(u32),
|
||||
|
||||
#[error("V5 requires ekey.")]
|
||||
V5EKeyRequired,
|
||||
|
||||
#[error("Not KGM File (magic mismatch)")]
|
||||
NotKGMFile,
|
||||
|
||||
@@ -40,23 +45,42 @@ pub enum KugouError {
|
||||
|
||||
#[error("Database does not seem valid")]
|
||||
InvalidPage1Header,
|
||||
|
||||
#[error("QMC2EKeyError: {0}")]
|
||||
QMC2EKeyError(String),
|
||||
|
||||
#[error("Parse KGM header with i/o error: {0}")]
|
||||
HeaderParseIOError(std::io::Error),
|
||||
|
||||
#[error("Invalid audio hash size: {0}")]
|
||||
HeaderInvalidAudioHash(usize),
|
||||
}
|
||||
|
||||
pub enum Decipher {
|
||||
V2(DecipherV2),
|
||||
V3(DecipherV3),
|
||||
V5(DecipherV5),
|
||||
}
|
||||
|
||||
impl Decipher {
|
||||
pub fn new(header: &Header) -> Result<Self, KugouError> {
|
||||
Self::new_v5(header, None)
|
||||
}
|
||||
|
||||
pub fn new_v5(header: &Header, ekey: Option<String>) -> Result<Self, KugouError> {
|
||||
let slot_key: &[u8] = match header.key_slot {
|
||||
1 => b"l,/'",
|
||||
-1 => b"", // unused, kgm v5 (kgg)
|
||||
slot => Err(KugouError::UnsupportedKeySlot(slot))?,
|
||||
};
|
||||
|
||||
let decipher = match header.crypto_version {
|
||||
2 => Decipher::V2(DecipherV2::new(header, slot_key)?),
|
||||
3 => Decipher::V3(DecipherV3::new(header, slot_key)?),
|
||||
5 => match ekey {
|
||||
Some(ekey) => Decipher::V5(DecipherV5::new(&ekey)?),
|
||||
_ => Err(KugouError::V5EKeyRequired)?,
|
||||
},
|
||||
version => Err(KugouError::UnsupportedCipherVersion(version))?,
|
||||
};
|
||||
|
||||
@@ -73,6 +97,7 @@ impl Decipher {
|
||||
match self {
|
||||
Decipher::V2(decipher) => decipher.decrypt(buffer, offset),
|
||||
Decipher::V3(decipher) => decipher.decrypt(buffer, offset),
|
||||
Decipher::V5(decipher) => decipher.decrypt(buffer, offset),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ fn next_page_iv(seed: u32) -> u32 {
|
||||
fn derive_page_aes_key(seed: u32) -> [u8; 0x10] {
|
||||
let mut master_key = DEFAULT_MASTER_KEY;
|
||||
LE::write_u32(&mut master_key[0x10..0x14], seed);
|
||||
md5(&mut master_key)
|
||||
md5(master_key)
|
||||
}
|
||||
|
||||
fn derive_page_aes_iv(seed: u32) -> [u8; 0x10] {
|
||||
|
||||
@@ -21,8 +21,8 @@ impl DecipherV2 {
|
||||
|
||||
#[test]
|
||||
fn test_v2_init() -> Result<(), KugouError> {
|
||||
let hdr_v2 = Header::from_buffer(include_bytes!("__fixtures__/kgm_v2_hdr.bin"))?;
|
||||
DecipherV2::new(&hdr_v2, b"1234")?;
|
||||
let hdr = Header::from_buffer(include_bytes!("__fixtures__/kgm_v2_hdr.bin"))?;
|
||||
DecipherV2::new(&hdr, b"1234")?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
17
um_crypto/kgm/src/v5.rs
Normal file
17
um_crypto/kgm/src/v5.rs
Normal file
@@ -0,0 +1,17 @@
|
||||
use crate::KugouError;
|
||||
use umc_qmc::QMCv2Cipher;
|
||||
|
||||
pub struct DecipherV5(QMCv2Cipher);
|
||||
|
||||
impl DecipherV5 {
|
||||
pub fn new(ekey: &str) -> Result<Self, KugouError> {
|
||||
let cipher = QMCv2Cipher::new_from_ekey(ekey)
|
||||
.map_err(|e| KugouError::QMC2EKeyError(e.to_string()))?;
|
||||
|
||||
Ok(Self(cipher))
|
||||
}
|
||||
|
||||
pub fn decrypt<T: AsMut<[u8]> + ?Sized>(&self, buffer: &mut T, offset: usize) {
|
||||
self.0.decrypt(buffer, offset)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user