mirror of
https://github.com/freemkv/libfreemkv.git
synced 2026-10-07 18:13:10 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c8eb42b490 | ||
|
|
48570ac065 |
+1
-1
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "libfreemkv"
|
||||
version = "0.26.8"
|
||||
version = "0.26.11"
|
||||
edition = "2024"
|
||||
rust-version = "1.86"
|
||||
license = "AGPL-3.0-only"
|
||||
|
||||
+27
-6
@@ -69,9 +69,21 @@ pub(crate) fn aes_cbc_decrypt(key: &[u8; 16], data: &mut [u8]) {
|
||||
|
||||
// ── Content decryption ──────────────────────────────────────────────────────
|
||||
|
||||
/// Check if a 6144-byte aligned unit is encrypted (copy_permission_indicator bits).
|
||||
/// Check if a 6144-byte aligned unit is encrypted.
|
||||
///
|
||||
/// AACS encrypts at aligned-unit granularity (all-or-nothing per unit) and
|
||||
/// signals it via the TS `transport_scrambling_control` (TSC) bits — the top
|
||||
/// two bits of TS-header byte 3, which is byte 7 of the unit (4-byte
|
||||
/// TP_extra_header + sync + PID/flags). TSC `00` = clear, non-zero = scrambled.
|
||||
/// Byte 7 sits inside the clear 16-byte seed, so this is readable without the
|
||||
/// key.
|
||||
///
|
||||
/// NOTE: the earlier check read byte 0's TP_extra copy-control bits (`& 0xC0`),
|
||||
/// which are copy-permission, NOT encryption status — they false-positive on
|
||||
/// clear navigation units (PAT/PMT at a clip's start), causing a correct key to
|
||||
/// be decrypted against clear data and wrongly rejected.
|
||||
pub fn is_unit_encrypted(unit: &[u8]) -> bool {
|
||||
unit.len() >= ALIGNED_UNIT_LEN && (unit[0] & 0xC0) != 0
|
||||
unit.len() >= ALIGNED_UNIT_LEN && (unit[7] >> 6) & 0x03 != 0
|
||||
}
|
||||
|
||||
/// Verify decrypted unit by checking TS sync bytes at expected offsets.
|
||||
@@ -124,8 +136,16 @@ pub fn decrypt_unit(unit: &mut [u8], unit_key: &[u8; 16]) -> bool {
|
||||
// Step 3: Decrypt bytes 16..6143 with AES-CBC
|
||||
aes_cbc_decrypt(&decrypt_key, &mut unit[16..ALIGNED_UNIT_LEN]);
|
||||
|
||||
// Step 4: Clear encryption flag
|
||||
unit[0] &= !0xC0;
|
||||
// Step 4: Clear the encryption flag — the TS transport_scrambling_control
|
||||
// bits (top two of TS-header byte 3) of every packet, so the output is
|
||||
// valid unscrambled TS. Each 192-byte cell's TS header byte 3 sits at
|
||||
// offset 7 within the cell. (The old code cleared byte 0's TP_extra
|
||||
// copy-control bits, which are NOT the scrambling flag.)
|
||||
let mut off = 7;
|
||||
while off < ALIGNED_UNIT_LEN {
|
||||
unit[off] &= 0x3F;
|
||||
off += TS_PACKET_LEN;
|
||||
}
|
||||
|
||||
// Verify
|
||||
verify_ts(unit)
|
||||
@@ -253,8 +273,9 @@ mod tests {
|
||||
plain[offset] = TS_SYNC;
|
||||
offset += TS_PACKET_LEN;
|
||||
}
|
||||
// Set encryption flag
|
||||
plain[0] |= 0xC0;
|
||||
// Set the encryption flag: TS transport_scrambling_control (top two
|
||||
// bits of byte 7), inside the clear seed.
|
||||
plain[7] |= 0x80;
|
||||
|
||||
// Now encrypt bytes 16..6143 using the AACS algorithm (reverse of decrypt)
|
||||
let header: [u8; 16] = plain[..16].try_into().unwrap();
|
||||
|
||||
@@ -223,6 +223,13 @@ impl super::provider::KeyProvider for KeyDb {
|
||||
fn processing_keys(&self) -> Vec<[u8; 16]> {
|
||||
self.processing_keys.clone()
|
||||
}
|
||||
fn media_keys(&self) -> Vec<[u8; 16]> {
|
||||
// Every per-disc Media Key in the db. The resolver dedups; MKs are
|
||||
// MKB-scoped so the same value recurs across a pressing's discs.
|
||||
self.iter_disc_entries()
|
||||
.filter_map(|e| e.media_key)
|
||||
.collect()
|
||||
}
|
||||
fn host_certs(&self) -> Vec<HostCert> {
|
||||
self.host_certs.clone()
|
||||
}
|
||||
|
||||
@@ -1103,6 +1103,30 @@ fn resolve_keys_classical(ctx: &ResolveContext<'_>, version: AacsVersion) -> Opt
|
||||
return Some(build(Some(vuk), derive_uks(&vuk), 2));
|
||||
}
|
||||
tracing::warn!(target: "freemkv::disc", phase = "resolve_keys_path2_miss", pk_count = all_pks.len(), "PK derivation failed");
|
||||
|
||||
// Path 2.5: MK-pool brute. keydb stores Media Keys per-disc, but an
|
||||
// MK is MKB-scoped (shared across a pressing/MKB-family). A disc
|
||||
// whose own hash/VID isn't keyed can still resolve if ANY stored MK
|
||||
// verifies against its MKB. Try every distinct MK via km_verifies;
|
||||
// a UNIQUE pass is this disc's Km → derive VUK (needs VID) → UK.
|
||||
// km_verifies is one AES-D + magic check per candidate (cheap).
|
||||
let mks = providers.media_keys();
|
||||
let mut mk_hits: Vec<[u8; 16]> = Vec::new();
|
||||
for mk in &mks {
|
||||
if probe::km_verifies(mkb, mk) && !mk_hits.contains(mk) {
|
||||
mk_hits.push(*mk);
|
||||
if mk_hits.len() > 1 {
|
||||
break; // ambiguous — bail to avoid a wrong key
|
||||
}
|
||||
}
|
||||
}
|
||||
if mk_hits.len() == 1 {
|
||||
let vuk = derive_vuk(&mk_hits[0], ctx.volume_id);
|
||||
tracing::warn!(target: "freemkv::disc", phase = "resolve_keys_path2_5_hit", mk_pool = mks.len(), "media key from keydb MK-pool brute (km_verifies)");
|
||||
// Same class as path 3 (KEYDB MK → derived VUK).
|
||||
return Some(build(Some(vuk), derive_uks(&vuk), 3));
|
||||
}
|
||||
tracing::warn!(target: "freemkv::disc", phase = "resolve_keys_path2_5_miss", mk_pool = mks.len(), mk_hits = mk_hits.len(), "MK-pool brute: no unique verifying MK");
|
||||
} else {
|
||||
tracing::warn!(target: "freemkv::disc", phase = "resolve_keys_no_mkb", "no MKB; paths 1/2 skipped");
|
||||
}
|
||||
@@ -1954,6 +1978,68 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resolve_keys_path2_5_mk_pool_brute_resolves_unkeyed_disc() {
|
||||
// The Dunkirk case: this disc's own hash/VID are NOT in keydb, but its
|
||||
// Media Key IS — filed under a sibling disc that shares its MKB. Path
|
||||
// 2.5 must km_verifies that MK against the MKB and resolve.
|
||||
use super::super::decrypt::aes_ecb_encrypt as enc;
|
||||
|
||||
let km = [0x11u8; 16];
|
||||
let vid = [0x22u8; 16];
|
||||
|
||||
// MKB: 0x10 type/version + 0x86 verify record whose mk_dv decrypts under
|
||||
// km to the AACS verify magic, so km_verifies(mkb, km) == true.
|
||||
let mut vd = [0u8; 16];
|
||||
vd[..8].copy_from_slice(&[0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF]);
|
||||
let mk_dv = enc(&km, &vd);
|
||||
let mut mkb = mkb_record(0x10, &[0, 0, 0, 0x20, 0, 0, 0, 0x4D]);
|
||||
mkb.extend_from_slice(&mkb_record(0x86, &mk_dv));
|
||||
assert!(
|
||||
probe::km_verifies(&mkb, &km),
|
||||
"fixture: km must verify the MKB"
|
||||
);
|
||||
|
||||
// This disc's inf (its hash will NOT be in keydb).
|
||||
let uk_ro = minimal_unit_key_ro();
|
||||
|
||||
// keydb: a SIBLING disc carries our km, keyed by the sibling's own
|
||||
// hash + VID (neither matches THIS disc) — so only the MK-pool brute
|
||||
// (km_verifies) can find it.
|
||||
let mut keydb = KeyDb::empty();
|
||||
keydb.disc_entries.insert(
|
||||
"0xsibling".to_string(),
|
||||
DiscEntry {
|
||||
disc_hash: "0xsibling".to_string(),
|
||||
title: "sibling".to_string(),
|
||||
media_key: Some(km),
|
||||
disc_id: Some([0x99u8; 16]),
|
||||
vuk: None,
|
||||
unit_keys: Vec::new(),
|
||||
},
|
||||
);
|
||||
|
||||
let providers: &[&dyn super::super::KeyProvider] = &[&keydb];
|
||||
let ctx = ResolveContext {
|
||||
unit_key_ro: &uk_ro,
|
||||
content_cert: None,
|
||||
volume_id: &vid,
|
||||
providers,
|
||||
mkb: Some(&mkb),
|
||||
};
|
||||
let resolved = resolve_keys_v1(&ctx)
|
||||
.expect("MK-pool brute (path 2.5) must resolve a disc whose MK is in keydb");
|
||||
assert_eq!(
|
||||
resolved.key_source, 3,
|
||||
"MK-pool brute is the KEYDB-derived class"
|
||||
);
|
||||
assert_eq!(
|
||||
resolved.vuk,
|
||||
Some(derive_vuk(&km, &vid)),
|
||||
"VUK must derive from the verified Km + this disc's VID"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_content_cert_parse() {
|
||||
// AACS 1.0 cert
|
||||
|
||||
@@ -48,6 +48,14 @@ pub trait KeyProvider: Send + Sync {
|
||||
Vec::new()
|
||||
}
|
||||
|
||||
/// Every Media Key this provider holds, regardless of which disc it was
|
||||
/// filed under. An MK is MKB-scoped (shared across a pressing/MKB-family),
|
||||
/// so the resolver can verify each against the disc's MKB (`km_verifies`)
|
||||
/// and resolve a disc whose own hash/VID isn't directly keyed.
|
||||
fn media_keys(&self) -> Vec<[u8; 16]> {
|
||||
Vec::new()
|
||||
}
|
||||
|
||||
/// AACS host certificates (with their private keys) for drive
|
||||
/// authentication. Multiple in case some are revoked.
|
||||
fn host_certs(&self) -> Vec<HostCert> {
|
||||
@@ -85,6 +93,15 @@ impl Providers<'_> {
|
||||
self.0.iter().flat_map(|p| p.processing_keys()).collect()
|
||||
}
|
||||
|
||||
/// Union of distinct Media Keys across every provider, for the MK-pool
|
||||
/// brute (`km_verifies` against the disc's MKB).
|
||||
pub fn media_keys(&self) -> Vec<[u8; 16]> {
|
||||
let mut v: Vec<[u8; 16]> = self.0.iter().flat_map(|p| p.media_keys()).collect();
|
||||
v.sort_unstable();
|
||||
v.dedup();
|
||||
v
|
||||
}
|
||||
|
||||
/// Union — gather host certs from every provider. Not yet wired into
|
||||
/// the SCSI handshake (which still reads `KeyDb.host_certs` directly);
|
||||
/// kept here so a provider-aware handshake refactor is a drop-in.
|
||||
|
||||
+44
-33
@@ -89,8 +89,9 @@ fn aacs_decrypt_unit_roundtrip() {
|
||||
plain[offset] = 0x47; // TS sync byte
|
||||
offset += 192;
|
||||
}
|
||||
// Set encryption flag (bits 6-7 of byte 0)
|
||||
plain[0] |= 0xC0;
|
||||
// Set encryption flag: TS transport_scrambling_control (top two bits of
|
||||
// byte 7), inside the clear seed.
|
||||
plain[7] |= 0x80;
|
||||
|
||||
// Save original plaintext for comparison
|
||||
let expected = plain.clone();
|
||||
@@ -156,18 +157,15 @@ fn aacs_decrypt_unit_roundtrip() {
|
||||
sync_count, expected_syncs
|
||||
);
|
||||
|
||||
// Compare all bytes except byte 0 (encryption flag cleared)
|
||||
// decrypt clears the TSC bits (byte 7, top two) — the only change from the
|
||||
// original plaintext. Everything else round-trips exactly.
|
||||
assert_eq!(plain[7], expected[7] & 0x3F, "TSC bits should be cleared");
|
||||
assert_eq!(&plain[..7], &expected[..7], "bytes 0..7 mismatch");
|
||||
assert_eq!(
|
||||
&plain[1..aacs::ALIGNED_UNIT_LEN],
|
||||
&expected[1..aacs::ALIGNED_UNIT_LEN],
|
||||
&plain[8..aacs::ALIGNED_UNIT_LEN],
|
||||
&expected[8..aacs::ALIGNED_UNIT_LEN],
|
||||
"decrypted unit body does not match original"
|
||||
);
|
||||
// Byte 0: original had 0xC0 set, decrypted has it cleared
|
||||
assert_eq!(
|
||||
plain[0] & !0xC0,
|
||||
expected[0] & !0xC0,
|
||||
"byte 0 mismatch ignoring flag"
|
||||
);
|
||||
}
|
||||
|
||||
/// Test 7: aacs_disc_hash_deterministic
|
||||
@@ -270,20 +268,27 @@ fn aacs_is_unit_encrypted_detection() {
|
||||
"zero unit should not be encrypted"
|
||||
);
|
||||
|
||||
unit[0] = 0x40; // bit 6 set
|
||||
// The encryption flag is the TS transport_scrambling_control (top two bits
|
||||
// of byte 7). Any non-zero TSC = encrypted.
|
||||
unit[7] = 0x40; // TSC = 01
|
||||
assert!(aacs::is_unit_encrypted(&unit));
|
||||
|
||||
unit[0] = 0x80; // bit 7 set
|
||||
unit[7] = 0x80; // TSC = 10
|
||||
assert!(aacs::is_unit_encrypted(&unit));
|
||||
|
||||
unit[0] = 0xC0; // both bits set
|
||||
unit[7] = 0xC0; // TSC = 11
|
||||
assert!(aacs::is_unit_encrypted(&unit));
|
||||
|
||||
unit[0] = 0x3F; // bits 6-7 clear
|
||||
unit[7] = 0x3F; // top two bits clear
|
||||
assert!(!aacs::is_unit_encrypted(&unit));
|
||||
|
||||
// Byte 0's TP_extra copy-control bits are NOT the encryption flag.
|
||||
unit[7] = 0x00;
|
||||
unit[0] = 0xC0;
|
||||
assert!(
|
||||
!aacs::is_unit_encrypted(&unit),
|
||||
"byte-0 copy-control bits must not be read as encryption"
|
||||
);
|
||||
|
||||
// Too short
|
||||
let short = vec![0xC0u8; 100];
|
||||
let short = vec![0xFFu8; 100];
|
||||
assert!(
|
||||
!aacs::is_unit_encrypted(&short),
|
||||
"short buffer should not be detected"
|
||||
@@ -296,7 +301,7 @@ fn aacs_is_unit_encrypted_detection() {
|
||||
#[test]
|
||||
fn aacs_decrypt_unit_unencrypted_passthrough() {
|
||||
let mut unit = vec![0x42u8; aacs::ALIGNED_UNIT_LEN];
|
||||
unit[0] = 0x00; // no encryption flag
|
||||
unit[7] &= 0x3F; // TSC = 0 → clear/unencrypted unit
|
||||
let original = unit.clone();
|
||||
let key = [0xAA; 16];
|
||||
|
||||
@@ -369,8 +374,8 @@ fn aacs_cross_validation_encrypt_then_decrypt() {
|
||||
plaintext[i] = (i % 251) as u8;
|
||||
}
|
||||
}
|
||||
// Set encryption flag
|
||||
plaintext[0] = 0xC0;
|
||||
// Set encryption flag: TSC bits of packet 0 (byte 7).
|
||||
plaintext[7] |= 0x80;
|
||||
|
||||
let expected = plaintext.clone();
|
||||
|
||||
@@ -401,14 +406,19 @@ fn aacs_cross_validation_encrypt_then_decrypt() {
|
||||
ok,
|
||||
"decrypt_unit returned false (TS sync verification failed)"
|
||||
);
|
||||
assert_eq!(plaintext[0] & 0xC0, 0x00, "encryption flag not cleared");
|
||||
assert_eq!(plaintext[7] >> 6, 0, "TSC bits not cleared");
|
||||
|
||||
// Compare (byte 0 flag was cleared)
|
||||
// decrypt clears the TSC bits of every packet (byte 7 of each 192-byte
|
||||
// cell). Clear the same positions in the expected copy before comparing.
|
||||
let mut expected_cleared = expected.clone();
|
||||
expected_cleared[0] &= !0xC0;
|
||||
let mut o = 7;
|
||||
while o < aacs::ALIGNED_UNIT_LEN {
|
||||
expected_cleared[o] &= 0x3F;
|
||||
o += 192;
|
||||
}
|
||||
assert_eq!(
|
||||
&plaintext[1..aacs::ALIGNED_UNIT_LEN],
|
||||
&expected_cleared[1..aacs::ALIGNED_UNIT_LEN],
|
||||
&plaintext[..],
|
||||
&expected_cleared[..],
|
||||
"decrypted unit does not match original plaintext"
|
||||
);
|
||||
}
|
||||
@@ -428,7 +438,7 @@ fn aacs_cross_validation_alternate_key() {
|
||||
plaintext[off] = 0x47;
|
||||
off += 192;
|
||||
}
|
||||
plaintext[0] = 0xC0;
|
||||
plaintext[7] |= 0x80; // TSC encryption flag
|
||||
let expected = plaintext.clone();
|
||||
|
||||
let mut header = [0u8; 16];
|
||||
@@ -447,11 +457,12 @@ fn aacs_cross_validation_alternate_key() {
|
||||
assert!(aacs::decrypt_unit(&mut plaintext, &unit_key));
|
||||
|
||||
let mut expected_cleared = expected;
|
||||
expected_cleared[0] &= !0xC0;
|
||||
assert_eq!(
|
||||
&plaintext[1..aacs::ALIGNED_UNIT_LEN],
|
||||
&expected_cleared[1..aacs::ALIGNED_UNIT_LEN],
|
||||
);
|
||||
let mut o = 7;
|
||||
while o < aacs::ALIGNED_UNIT_LEN {
|
||||
expected_cleared[o] &= 0x3F;
|
||||
o += 192;
|
||||
}
|
||||
assert_eq!(&plaintext[..], &expected_cleared[..]);
|
||||
}
|
||||
|
||||
/// Verify that `decrypt_bus` correctly reverses AES-CBC encryption applied
|
||||
|
||||
@@ -84,20 +84,23 @@ fn decrypt_sectors_with_css_keys_works() {
|
||||
fn aacs_encryption_flag_detection() {
|
||||
let mut unit = vec![0u8; aacs::ALIGNED_UNIT_LEN];
|
||||
|
||||
// No encryption flag
|
||||
// The encryption flag is the TS transport_scrambling_control (top two bits
|
||||
// of byte 7), not byte 0's copy-control bits.
|
||||
assert!(!aacs::is_unit_encrypted(&unit));
|
||||
|
||||
// Set bit 6
|
||||
unit[0] |= 0x40;
|
||||
unit[7] = 0x40; // TSC = 01
|
||||
assert!(aacs::is_unit_encrypted(&unit));
|
||||
|
||||
// Set bit 7
|
||||
unit[0] = 0x80;
|
||||
unit[7] = 0x80; // TSC = 10
|
||||
assert!(aacs::is_unit_encrypted(&unit));
|
||||
|
||||
// Both bits set
|
||||
unit[7] = 0xC0; // TSC = 11
|
||||
assert!(aacs::is_unit_encrypted(&unit));
|
||||
|
||||
// Byte 0 copy-control bits must NOT count as encryption.
|
||||
unit[7] = 0x00;
|
||||
unit[0] = 0xC0;
|
||||
assert!(aacs::is_unit_encrypted(&unit));
|
||||
assert!(!aacs::is_unit_encrypted(&unit));
|
||||
}
|
||||
|
||||
/// Test: DecryptKeys::is_encrypted() correctly identifies encrypted state.
|
||||
|
||||
Reference in New Issue
Block a user