feat(kgm): move slot_key logic out
All checks were successful
Build and Publish / build (push) Successful in 52s

This commit is contained in:
鲁树人 2025-02-25 04:51:44 +09:00
parent 6706eb85e5
commit ba422e3cce
4 changed files with 19 additions and 13 deletions

View File

@ -1,5 +1,6 @@
pub mod header;
mod pc_db_decrypt;
mod slot_keys;
pub mod v2;
pub mod v3;
mod v5;
@ -68,15 +69,9 @@ impl Decipher {
}
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)?),
2 => Decipher::V2(DecipherV2::new(header)?),
3 => Decipher::V3(DecipherV3::new(header)?),
5 => match ekey {
Some(ekey) => Decipher::V5(DecipherV5::new(&ekey)?),
_ => Err(KugouError::V5EKeyRequired)?,

View File

@ -0,0 +1,9 @@
use crate::header::Header;
use crate::KugouError;
pub fn get_slot_key(header: &Header) -> Result<&'static [u8], KugouError> {
match header.key_slot {
1 => Ok(b"l,/'"),
slot => Err(KugouError::UnsupportedKeySlot(slot)),
}
}

View File

@ -1,4 +1,5 @@
use crate::header::Header;
use crate::slot_keys::get_slot_key;
use crate::KugouError;
pub struct DecipherV2 {
@ -6,9 +7,9 @@ pub struct DecipherV2 {
}
impl DecipherV2 {
pub fn new(_header: &Header, slot_key: &[u8]) -> Result<Self, KugouError> {
pub fn new(header: &Header) -> Result<Self, KugouError> {
let mut key = [0u8; 4];
key.copy_from_slice(slot_key);
key.copy_from_slice(get_slot_key(&header)?);
Ok(Self { key })
}
@ -22,7 +23,7 @@ impl DecipherV2 {
#[test]
fn test_v2_init() -> Result<(), KugouError> {
let hdr = Header::from_buffer(include_bytes!("__fixtures__/kgm_v2_hdr.bin"))?;
DecipherV2::new(&hdr, b"1234")?;
DecipherV2::new(&hdr)?;
Ok(())
}

View File

@ -1,4 +1,5 @@
use crate::header::Header;
use crate::slot_keys::get_slot_key;
use crate::KugouError;
pub struct DecipherV3 {
@ -17,8 +18,8 @@ impl DecipherV3 {
result
}
pub fn new(header: &Header, slot_key: &[u8]) -> Result<Self, KugouError> {
let slot_key = Self::hash_key(slot_key);
pub fn new(header: &Header) -> Result<Self, KugouError> {
let slot_key = Self::hash_key(get_slot_key(header)?);
let mut file_key = [0x6b; 17];
file_key[..16].copy_from_slice(&Self::hash_key(header.file_key));