Post-read verify gate (new src/disc/verify.rs): UnitVerifier buffers/aligns the disc-absolute read stream into clip-file 6144-byte units, then makes one decryptability() decision per unit (CPI gate -> held keys -> key_fetch -> strict TS). POST_READ_VERIFY const kill-switch; fail-safe contract (only ever downgrades units it is confident are undecryptable; every doubt skips). Hooked into Disc::sweep (producer observes ciphertext -> WorkItem::MarkBad after the Good, FIFO-ordered) and Disc::patch (post-loop reverify_iso reads recovered units whole from the patched ISO). extract::clip_layouts enumerates AACS clips for the gate.
Standards-correct AACS verify: aacs::unit_is_clean_ts is a strict port of libaacs _verify_ts (all 32 TS syncs, not a majority vote); decrypt_unit accepts a key only on it; the majority verify_ts is removed. Deleted the Disc::verify_clips post-pass bolt-on (its primitive is absorbed by the read-path gate).
libaacs/DVD audit fixes: content-cert bus_encryption flag now read from bit 7 (was bit 0 - defeated the bus-key fail-loud gate); cc_id read from offset 14; title_cps_unit range-validated + 1->0 index-converted per libaacs. Corrected attack_crib ("functionally-equivalent" not "exact" port) and read_disc_key (READ DVD STRUCTURE 0xAD, not REPORT KEY) doc comments.
Also includes accumulated uncommitted work: key-fetch seam and TrueHD/DTS audio fix.
122 lines
3.7 KiB
Rust
122 lines
3.7 KiB
Rust
//! Regression tests for Pass N (patch) fix — decrypt key inversion bug.
|
|
//!
|
|
//! Tests that decrypt_sectors is invoked correctly when opts.decrypt=true.
|
|
//! The 2026-05-03 bug at `libfreemkv/src/disc/mod.rs:1938-1942` inverted
|
|
//! the decrypt key arms, causing patch to pass DecryptKeys::None on encrypted discs.
|
|
|
|
use libfreemkv::{aacs, decrypt::DecryptKeys};
|
|
|
|
/// Test: decrypt_sectors with AACS keys actually decrypts units.
|
|
#[test]
|
|
fn decrypt_sectors_with_aacs_keys_works() {
|
|
// Build an encrypted aligned unit
|
|
let mut unit = vec![0xFFu8; aacs::ALIGNED_UNIT_LEN];
|
|
|
|
// Set encryption flag (bits 6-7 of byte 0)
|
|
unit[0] |= 0xC0;
|
|
|
|
// Fill with recognizable pattern
|
|
for (i, byte) in unit
|
|
.iter_mut()
|
|
.enumerate()
|
|
.take(aacs::ALIGNED_UNIT_LEN)
|
|
.skip(1)
|
|
{
|
|
*byte = ((i * 3 + 7) & 0xFF) as u8;
|
|
}
|
|
|
|
let unit_key: [u8; 16] = [0xAAu8; 16];
|
|
|
|
// Encrypt the unit using AACS algorithm
|
|
aacs::decrypt_unit(&mut unit, &unit_key); // decrypt_unit is idempotent on already-encrypted data
|
|
|
|
// Now we have encrypted data - create DecryptKeys with actual keys
|
|
let mut keys = DecryptKeys::Aacs {
|
|
unit_keys: vec![(0u32, unit_key)],
|
|
read_data_key: None,
|
|
};
|
|
|
|
// decrypt_sectors should handle this without error
|
|
let result = libfreemkv::decrypt::decrypt_sectors(&mut unit, &mut keys, 0);
|
|
|
|
assert!(
|
|
result.is_ok(),
|
|
"decrypt_sectors with AACS keys should not error"
|
|
);
|
|
}
|
|
|
|
/// Test: decrypt_sectors with DecryptKeys::None is a no-op.
|
|
#[test]
|
|
fn decrypt_sectors_with_none_keys_is_noop() {
|
|
let mut sector = vec![0x42u8; 2048];
|
|
|
|
let mut keys = DecryptKeys::None;
|
|
let result = libfreemkv::decrypt::decrypt_sectors(&mut sector, &mut keys, 0);
|
|
|
|
assert!(result.is_ok());
|
|
assert_eq!(
|
|
§or[..],
|
|
&[0x42u8; 2048][..],
|
|
"DecryptKeys::None should not modify buffer"
|
|
);
|
|
}
|
|
|
|
/// Test: decrypt_sectors with CSS keys descrambles sectors.
|
|
#[test]
|
|
fn decrypt_sectors_with_css_keys_works() {
|
|
let mut sector = vec![0xFFu8; 2048];
|
|
|
|
// Set CSS scramble flag (bits 4-5 of byte 0x14)
|
|
sector[0x14] |= 0x30;
|
|
|
|
let title_key: [u8; 5] = [0x42, 0x13, 0x37, 0xBE, 0xEF]; // Not used - defined later
|
|
let mut keys = DecryptKeys::Css { title_key };
|
|
|
|
// Descramble (CSS uses same operation for encrypt/decrypt)
|
|
libfreemkv::decrypt::decrypt_sectors(&mut sector, &mut keys, 0).unwrap();
|
|
|
|
// Flag should be cleared
|
|
assert_eq!(sector[0x14] & 0x30, 0x00, "CSS flag should be cleared");
|
|
}
|
|
|
|
/// Test: AACS unit encryption detection works.
|
|
#[test]
|
|
fn aacs_encryption_flag_detection() {
|
|
// A clear unit: TS syncs (0x47) intact at every 192-byte packet.
|
|
let mut unit = vec![0u8; aacs::ALIGNED_UNIT_LEN];
|
|
let mut off = 4;
|
|
while off < aacs::ALIGNED_UNIT_LEN {
|
|
unit[off] = 0x47;
|
|
off += 192;
|
|
}
|
|
// Encryption is the scrambled body (TS syncs destroyed), NOT a flag bit.
|
|
assert!(!aacs::ts_sync_destroyed(&unit));
|
|
|
|
// Flag bits on a synced unit do not make it look encrypted.
|
|
unit[0] = 0xC0;
|
|
unit[7] = 0xC0;
|
|
assert!(!aacs::ts_sync_destroyed(&unit));
|
|
|
|
// Scrambled body (syncs gone) → encrypted.
|
|
let scrambled = vec![0x99u8; aacs::ALIGNED_UNIT_LEN];
|
|
assert!(aacs::ts_sync_destroyed(&scrambled));
|
|
}
|
|
|
|
/// Test: DecryptKeys::is_encrypted() correctly identifies encrypted state.
|
|
#[test]
|
|
fn decrypt_keys_is_encrypted_variants() {
|
|
let none = DecryptKeys::None;
|
|
assert!(!none.is_encrypted());
|
|
|
|
let aacs = DecryptKeys::Aacs {
|
|
unit_keys: vec![],
|
|
read_data_key: None,
|
|
};
|
|
assert!(aacs.is_encrypted());
|
|
|
|
let css = DecryptKeys::Css {
|
|
title_key: [0u8; 5],
|
|
};
|
|
assert!(css.is_encrypted());
|
|
}
|