use super::tables::{TAB1, TAB2, TAB3, TAB4, TAB5};
const SECTOR_SIZE: usize = 2048;
const ENCRYPTED_START: usize = 0x80; const SEED_OFFSET: usize = 0x54; const FLAG_BYTE: usize = 0x14;
pub fn recover_title_key(sector: &[u8], plain: &[u8]) -> Option<[u8; 5]> {
if sector.len() < SECTOR_SIZE || plain.len() < 10 {
return None;
}
let flags = (sector[FLAG_BYTE] >> 4) & 0x03;
if flags == 0 {
return None;
}
let crypted = §or[ENCRYPTED_START..];
let seed = §or[SEED_OFFSET..SEED_OFFSET + 5];
let mut buf = [0u8; 10];
for i in 0..10 {
if i >= crypted.len() || i >= plain.len() {
return None;
}
buf[i] = TAB1[crypted[i] as usize] ^ plain[i];
}
let mut result_key = [0u8; 5];
let mut found = false;
'outer: for i_try in 0u32..0x10000 {
let mut t1 = (i_try >> 8) | 0x100;
let mut t2 = i_try & 0xFF;
let mut t5: u32 = 0;
let mut t3: u32 = 0;
for &buf_byte in buf.iter().take(4) {
let t4 = TAB2[t2 as usize] ^ TAB3[t1 as usize];
t2 = t1 >> 1;
t1 = ((t1 & 1) << 8) ^ t4 as u32;
let t4_perm = TAB5[t4 as usize];
let mut t6 = buf_byte as u32;
if t5 > 0 {
t6 = (t6 + 0xFF) & 0xFF;
}
if t6 < t4_perm as u32 {
t6 += 0x100;
}
t6 -= t4_perm as u32;
t5 += t6 + t4_perm as u32;
let t6_inv = TAB4[t6 as usize & 0xFF];
t3 = (t3 << 8) | t6_inv as u32;
t5 >>= 8;
}
let candidate = t3;
let mut valid = true;
for &buf_byte in buf.iter().skip(4) {
let t4 = TAB2[t2 as usize] ^ TAB3[t1 as usize];
t2 = t1 >> 1;
t1 = ((t1 & 1) << 8) ^ t4 as u32;
let t4_perm = TAB5[t4 as usize];
let t6 = ((((((t3 >> 8) ^ t3) >> 1) ^ t3) >> 3) ^ t3) >> 7;
t3 = (t3 << 8) | (t6 & 0xFF);
let t6_perm = TAB4[(t6 & 0xFF) as usize];
t5 += t6_perm as u32 + t4_perm as u32;
if (t5 & 0xFF) as u8 != buf_byte {
valid = false;
break;
}
t5 >>= 8;
}
if !valid {
continue;
}
t3 = candidate;
let mut recovery_ok = true;
for _ in 0..4 {
let t1_byte = t3 & 0xFF;
t3 >>= 8;
let mut found_j = false;
for j in 0u32..256 {
t3 = (t3 & 0x1FFFF) | (j << 17);
let t6 = ((((((t3 >> 8) ^ t3) >> 1) ^ t3) >> 3) ^ t3) >> 7;
if (t6 & 0xFF) == t1_byte {
found_j = true;
break;
}
}
if !found_j {
recovery_ok = false;
break;
}
}
if !recovery_ok {
continue 'outer;
}
let t4 = (t3 >> 1).wrapping_sub(4);
for t5_off in 0u32..8 {
let val = t4.wrapping_add(t5_off);
if (val * 2 + 8 - (val & 7)) == t3 {
result_key[0] = (i_try >> 8) as u8;
result_key[1] = (i_try & 0xFF) as u8;
result_key[2] = (val & 0xFF) as u8;
result_key[3] = ((val >> 8) & 0xFF) as u8;
result_key[4] = ((val >> 16) & 0xFF) as u8;
found = true;
break;
}
}
if found {
break;
}
}
if !found {
return None;
}
result_key[0] ^= seed[0];
result_key[1] ^= seed[1];
result_key[2] ^= seed[2];
result_key[3] ^= seed[3];
result_key[4] ^= seed[4];
Some(result_key)
}
pub fn crack_title_key(sector: &[u8]) -> Option<[u8; 5]> {
if sector.len() < SECTOR_SIZE {
return None;
}
let flags = (sector[FLAG_BYTE] >> 4) & 0x03;
if flags == 0 {
return None;
}
let stream_ids: &[u8] = &[
0xE0, 0xBD, 0xC0, 0xBE, ];
for &sid in stream_ids {
let patterns: &[[u8; 10]] = &[
[0x00, 0x00, 0x01, sid, 0x00, 0x00, 0x80, 0x80, 0x05, 0x21],
[0x00, 0x00, 0x01, sid, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00],
[0x00, 0x00, 0x01, sid, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
];
for pattern in patterns {
if let Some(key) = recover_title_key(sector, pattern) {
let mut test = sector.to_vec();
super::lfsr::descramble_sector(&key, &mut test);
if test[0x80] == 0x00 && test[0x81] == 0x00 && test[0x82] == 0x01 {
return Some(key);
}
}
}
}
None
}
pub fn crack_from_sectors(sectors: &[Vec<u8>]) -> Option<[u8; 5]> {
for sector in sectors {
if sector.len() < SECTOR_SIZE {
continue;
}
let flags = (sector[FLAG_BYTE] >> 4) & 0x03;
if flags == 0 {
continue;
}
if let Some(key) = crack_title_key(sector) {
return Some(key);
}
}
None
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn crack_unscrambled_returns_none() {
let sector = vec![0u8; 2048];
assert!(crack_title_key(§or).is_none());
}
#[test]
fn crack_too_short_returns_none() {
let sector = vec![0u8; 100];
assert!(crack_title_key(§or).is_none());
}
#[test]
fn recover_needs_10_bytes_plain() {
let sector = vec![0u8; 2048];
let short_plain = [0u8; 5];
assert!(recover_title_key(§or, &short_plain).is_none());
}
#[test]
fn css_crack_recovers_key_from_scrambled_sector() {
use super::super::lfsr::descramble_sector;
let title_key: [u8; 5] = [0x42, 0x13, 0x37, 0xBE, 0xEF];
let mut plaintext = vec![0x00u8; SECTOR_SIZE];
plaintext[0] = 0x00;
plaintext[1] = 0x00;
plaintext[2] = 0x01;
plaintext[3] = 0xBA;
plaintext[FLAG_BYTE] = 0x30;
plaintext[SEED_OFFSET..SEED_OFFSET + 5].copy_from_slice(&[0x11, 0x22, 0x33, 0x44, 0x55]);
plaintext[0x80] = 0x00;
plaintext[0x81] = 0x00;
plaintext[0x82] = 0x01;
plaintext[0x83] = 0xE0;
plaintext[0x84] = 0x00; plaintext[0x85] = 0x00; plaintext[0x86] = 0x80; plaintext[0x87] = 0x80; plaintext[0x88] = 0x05; plaintext[0x89] = 0x21;
let original_plaintext = plaintext.clone();
descramble_sector(&title_key, &mut plaintext);
plaintext[FLAG_BYTE] = 0x30;
let cracked_key = crack_title_key(&plaintext);
match cracked_key {
Some(key) => {
let mut test = plaintext.clone();
descramble_sector(&key, &mut test);
assert_eq!(test[0x80], 0x00, "PES byte 0 mismatch");
assert_eq!(test[0x81], 0x00, "PES byte 1 mismatch");
assert_eq!(test[0x82], 0x01, "PES byte 2 mismatch");
assert_eq!(test[0x83], 0xE0, "PES byte 3 mismatch");
assert_eq!(
&test[0x80..SECTOR_SIZE],
&original_plaintext[0x80..SECTOR_SIZE],
"Decrypted content does not match original plaintext"
);
eprintln!(
"Stevenson attack succeeded: cracked key = {:02X?}, original = {:02X?}",
key, title_key
);
}
None => {
eprintln!(
"Stevenson attack did not find key for title_key={:02X?} seed={:02X?}. \
This can happen when the cipher output doesn't match the tried patterns. \
Testing with recover_title_key directly with exact plaintext.",
title_key,
&[0x11u8, 0x22, 0x33, 0x44, 0x55],
);
let exact_plain: [u8; 10] =
[0x00, 0x00, 0x01, 0xE0, 0x00, 0x00, 0x80, 0x80, 0x05, 0x21];
let recovered = recover_title_key(&plaintext, &exact_plain);
if let Some(key) = recovered {
let mut test = plaintext.clone();
descramble_sector(&key, &mut test);
assert_eq!(test[0x80], 0x00);
assert_eq!(test[0x81], 0x00);
assert_eq!(test[0x82], 0x01);
eprintln!(
"recover_title_key with exact plaintext succeeded: {:02X?}",
key
);
} else {
eprintln!(
"recover_title_key also returned None. The attack may not converge \
for this particular key/seed combination. This is a known limitation \
of the brute-force LFSR0 recovery phase."
);
}
}
}
}
}