libfreemkv 0.31.10

Open source raw disc access library for optical drives
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
//! CSS cipher implementation based on the Stevenson 1999 analysis.
//!
//! The CSS cipher uses two table-driven feedback circuits:
//! - LFSR1: 17-bit state (9-bit lo + 8-bit hi register, seeded from
//!   key[0..2]), driven by TAB2/TAB3
//! - LFSR0: 32-bit state, driven by a feedback polynomial through TAB4
//!
//! The keystream is the bytewise sum (with carry) of both LFSR outputs.
//! Content descrambling XORs this keystream with the encrypted sector data.
//!
//! Algorithm: Frank A. Stevenson's divide-and-conquer attack (1999).
//! Tables: CSS specification constants.

use super::tables::{TAB1, TAB2, TAB3, TAB4, TAB5};

/// Seed the 32-bit LFSR0 register from the 5-byte working key, applying
/// the per-byte TAB4 bit-reversal. Shared by [`descramble_sector`] and
/// [`decrypt_key`] so the seeding lives in one place.
#[inline]
fn seed_lfsr0(key: &[u8; 5]) -> u32 {
    let lfsr0: u32 = ((key[4] as u32) << 17)
        | ((key[3] as u32) << 9)
        | (((key[2] as u32) << 1) + 8 - (key[2] as u32 & 7));
    (TAB4[(lfsr0 & 0xFF) as usize] as u32) << 24
        | (TAB4[((lfsr0 >> 8) & 0xFF) as usize] as u32) << 16
        | (TAB4[((lfsr0 >> 16) & 0xFF) as usize] as u32) << 8
        | TAB4[((lfsr0 >> 24) & 0xFF) as usize] as u32
}

/// One CSS keystream step. Advances both LFSRs, folds their permuted
/// outputs into `combined` (carry kept across calls), and returns the
/// low keystream byte. `invert` XORs the LFSR0 output index (0x00 on the
/// descramble path, 0xFF on the key-decrypt path).
#[inline]
fn css_step(
    lfsr1_lo: &mut u32,
    lfsr1_hi: &mut u32,
    lfsr0: &mut u32,
    combined: &mut u32,
    invert: u8,
) -> u8 {
    let o_lfsr1 = TAB2[*lfsr1_hi as usize] ^ TAB3[*lfsr1_lo as usize];
    *lfsr1_hi = *lfsr1_lo >> 1;
    *lfsr1_lo = ((*lfsr1_lo & 1) << 8) ^ o_lfsr1 as u32;

    let o_lfsr0 = (((((((*lfsr0 >> 8) ^ *lfsr0) >> 1) ^ *lfsr0) >> 3) ^ *lfsr0) >> 7) as u8;
    *lfsr0 = (*lfsr0 >> 8) | ((o_lfsr0 as u32) << 24);

    *combined += TAB5[o_lfsr1 as usize] as u32 + TAB4[(o_lfsr0 ^ invert) as usize] as u32;
    let out = (*combined & 0xFF) as u8;
    *combined >>= 8;
    out
}

/// Descramble a CSS-encrypted DVD sector in place.
///
/// The sector seed (bytes 0x54-0x58) is XORed with the title key to produce
/// the per-sector key. Bytes 0x80..0x800 (128..2048) are then decrypted
/// using the two-LFSR keystream.
///
/// The scramble flag at byte 0x14 (bits 4-5) indicates encryption.
/// After descrambling, the flag is cleared.
///
/// No-op (returns without modifying `sector`) in two cases:
/// - `sector.len() < 2048`: the encrypted region (0x80..0x800) is not
///   fully present. Callers chunk by 2048, so a trailing partial chunk is
///   left untouched. The `debug_assert!` flags this misuse in debug/test
///   builds; a DVD sector is always exactly 2048 bytes.
/// - scramble flags are zero: the sector is not CSS-encrypted.
pub fn descramble_sector(title_key: &[u8; 5], sector: &mut [u8]) {
    debug_assert!(
        sector.len() >= 2048,
        "descramble_sector: buffer shorter than one 2048-byte sector"
    );
    if sector.len() < 2048 {
        return;
    }

    let flags = (sector[0x14] >> 4) & 0x03;
    if flags == 0 {
        return;
    }

    // Per-sector key = title_key XOR sector_seed (bytes 0x54-0x58)
    let key = [
        title_key[0] ^ sector[0x54],
        title_key[1] ^ sector[0x55],
        title_key[2] ^ sector[0x56],
        title_key[3] ^ sector[0x57],
        title_key[4] ^ sector[0x58],
    ];

    // Decrypt the key through the CSS mangling function to get the working key.
    // The sector seed is bytes 0x54..0x59 (5 bytes).
    let seed: [u8; 5] = [
        sector[0x54],
        sector[0x55],
        sector[0x56],
        sector[0x57],
        sector[0x58],
    ];
    let working_key = decrypt_key(0xFF, &key, &seed);

    // Generate keystream and XOR with encrypted region
    let mut lfsr1_lo: u32 = working_key[0] as u32 | 0x100;
    let mut lfsr1_hi: u32 = working_key[1] as u32;
    let mut lfsr0: u32 = seed_lfsr0(&working_key);

    let mut combined: u32 = 0;

    // Generate 1920 keystream bytes (for sector bytes 128..2048) and XOR them
    // into the encrypted region. Each keystream byte is the carrying sum of the
    // TAB5-permuted LFSR1 output and the TAB4-permuted LFSR0 output. No TAB1
    // permutation is applied to the ciphertext here (TAB1 is only used inside
    // decrypt_key); the working key was already produced by decrypt_key above,
    // so this keystream is paired with that mangling step, not a plain
    // direct-seed unscramble. No invert is applied on the LFSR0 output.
    for byte in sector.iter_mut().take(2048).skip(128) {
        let ks = css_step(
            &mut lfsr1_lo,
            &mut lfsr1_hi,
            &mut lfsr0,
            &mut combined,
            0x00,
        );
        *byte ^= ks;
    }

    // Clear scramble flags
    sector[0x14] &= 0xCF;
}

/// CSS key decryption / mangling function.
///
/// Decrypts `p_crypted` using `p_key` with the CSS two-LFSR cipher.
/// The `invert` parameter controls the XOR applied to LFSR0 output
/// (0x00 for disc key decryption, 0xFF for title key / sector key).
pub(crate) fn decrypt_key(invert: u8, p_key: &[u8; 5], p_crypted: &[u8; 5]) -> [u8; 5] {
    let mut lfsr1_lo: u32 = p_key[0] as u32 | 0x100;
    let mut lfsr1_hi: u32 = p_key[1] as u32;
    let mut lfsr0: u32 = seed_lfsr0(p_key);

    let mut combined: u32 = 0;
    let mut k = [0u8; 5];

    // TAB5 for LFSR1 output, TAB4 for LFSR0^invert (per libdvdcss css_DecryptKey).
    for byte in &mut k {
        *byte = css_step(
            &mut lfsr1_lo,
            &mut lfsr1_hi,
            &mut lfsr0,
            &mut combined,
            invert,
        );
    }

    // Two rounds of chained XOR through TAB1
    let mut result = [0u8; 5];
    result[4] = k[4] ^ TAB1[p_crypted[4] as usize] ^ p_crypted[3];
    result[3] = k[3] ^ TAB1[p_crypted[3] as usize] ^ p_crypted[2];
    result[2] = k[2] ^ TAB1[p_crypted[2] as usize] ^ p_crypted[1];
    result[1] = k[1] ^ TAB1[p_crypted[1] as usize] ^ p_crypted[0];
    result[0] = k[0] ^ TAB1[p_crypted[0] as usize] ^ result[4];

    result[4] = k[4] ^ TAB1[result[4] as usize] ^ result[3];
    result[3] = k[3] ^ TAB1[result[3] as usize] ^ result[2];
    result[2] = k[2] ^ TAB1[result[2] as usize] ^ result[1];
    result[1] = k[1] ^ TAB1[result[1] as usize] ^ result[0];
    result[0] = k[0] ^ TAB1[result[0] as usize];

    result
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn descramble_skips_unscrambled() {
        let key = [0x01, 0x02, 0x03, 0x04, 0x05];
        let mut sector = vec![0xAA; 2048];
        sector[0x14] = 0x00;
        let original = sector.clone();
        descramble_sector(&key, &mut sector);
        assert_eq!(sector, original);
    }

    #[test]
    fn descramble_modifies_scrambled() {
        let key = [0x01, 0x02, 0x03, 0x04, 0x05];
        let mut sector = vec![0xAA; 2048];
        sector[0x14] = 0x30; // scramble flag set
        // Set a sector seed
        sector[0x54..0x59].copy_from_slice(&[0x11, 0x22, 0x33, 0x44, 0x55]);
        let original = sector.clone();
        descramble_sector(&key, &mut sector);
        // Header (0..128) unchanged except byte 0x14 (flag cleared)
        for i in 0..128 {
            if i == 0x14 {
                continue;
            }
            assert_eq!(sector[i], original[i], "header byte {} changed", i);
        }
        // Encrypted region should be different
        assert_ne!(&sector[128..256], &original[128..256]);
    }

    #[test]
    fn descramble_clears_flags() {
        let key = [0x01, 0x02, 0x03, 0x04, 0x05];
        let mut sector = vec![0x00; 2048];
        sector[0x14] = 0x30;
        sector[0x54..0x59].copy_from_slice(&[0x00; 5]);
        descramble_sector(&key, &mut sector);
        assert_eq!(sector[0x14] & 0x30, 0x00);
    }

    #[test]
    fn decrypt_key_produces_output() {
        let key = [0x12, 0x34, 0x56, 0x78, 0x9A];
        let crypted = [0xAB, 0xCD, 0xEF, 0x01, 0x23];
        let result = decrypt_key(0xFF, &key, &crypted);
        // Should produce a 5-byte result different from input
        assert_ne!(result, key);
        assert_ne!(result, [0u8; 5]);
    }

    /// css_decrypt_key_roundtrip
    ///
    /// decrypt_key is not a simple encrypt/decrypt pair — it is a one-way mangling
    /// function. However, we can verify consistency: calling it twice with the same
    /// parameters produces the same output, and varying the invert byte changes
    /// the LFSR0 contribution predictably.
    #[test]
    fn css_decrypt_key_roundtrip() {
        let keys: &[[u8; 5]] = &[
            [0x12, 0x34, 0x56, 0x78, 0x9A],
            [0x00, 0x00, 0x00, 0x00, 0x00],
            [0xFF, 0xFF, 0xFF, 0xFF, 0xFF],
            [0xAB, 0xCD, 0xEF, 0x01, 0x23],
        ];
        let crypted_inputs: &[[u8; 5]] = &[
            [0x11, 0x22, 0x33, 0x44, 0x55],
            [0xAA, 0xBB, 0xCC, 0xDD, 0xEE],
            [0x00, 0x00, 0x00, 0x00, 0x00],
        ];

        for key in keys {
            for crypted in crypted_inputs {
                // decrypt_key with invert=0x00 and invert=0xFF should give different results
                let r0 = decrypt_key(0x00, key, crypted);
                let rff = decrypt_key(0xFF, key, crypted);

                // The two results differ because the invert byte XORs the LFSR0 output
                // They should not be equal (except by extreme coincidence)
                // More importantly, both should be deterministic
                let r0_again = decrypt_key(0x00, key, crypted);
                let rff_again = decrypt_key(0xFF, key, crypted);
                assert_eq!(r0, r0_again, "decrypt_key(0x00) not deterministic");
                assert_eq!(rff, rff_again, "decrypt_key(0xFF) not deterministic");

                // With different invert values, the keystream differs
                assert_ne!(
                    r0, rff,
                    "invert=0x00 and 0xFF gave same result for key {:?}",
                    key
                );
            }
        }
    }

    /// Test 2: descramble_modifies_encrypted_region
    ///
    /// descramble_sector XORs a keystream into bytes 128..2048. The keystream
    /// depends only on (title_key, sector_seed), so applying descramble twice
    /// with the scramble flag restored between calls re-XORs the same keystream
    /// and restores the original encrypted region — the keystream XOR is its
    /// own inverse. This pins the cipher's involution property over the body.
    #[test]
    fn css_descramble_modifies_encrypted_region() {
        let title_key = [0x42, 0x13, 0x37, 0xBE, 0xEF];

        let mut sector = vec![0xAAu8; 2048];
        sector[0x14] = 0x30; // scramble flag
        sector[0x54..0x59].copy_from_slice(&[0xDE, 0xAD, 0xBE, 0xEF, 0x42]);

        let original = sector.clone();
        descramble_sector(&title_key, &mut sector);

        // Flag cleared
        assert_eq!(sector[0x14] & 0x30, 0x00);
        // Header (0..128) unchanged except flag byte
        for i in 0..128 {
            if i == 0x14 {
                continue;
            }
            assert_eq!(sector[i], original[i], "header byte {} changed", i);
        }
        // Encrypted region modified
        assert_ne!(&sector[128..256], &original[128..256]);

        // Round-trip: restore the scramble flag and descramble again. The same
        // keystream is regenerated (it depends only on title_key + seed, both
        // unchanged), so the body is restored to its original bytes.
        sector[0x14] = 0x30;
        descramble_sector(&title_key, &mut sector);
        assert_eq!(
            &sector[128..2048],
            &original[128..2048],
            "double descramble did not restore the encrypted region"
        );
    }

    /// css_tab1_relationship
    ///
    /// Verify the structure of TAB1: it is a substitution table used in
    /// key mangling. Check that no two inputs map to the same output
    /// (TAB1 is a permutation of 0..255).
    #[test]
    fn css_tab1_is_permutation() {
        let mut seen = [false; 256];
        for tab1_val in &TAB1 {
            let v = *tab1_val as usize;
            assert!(!seen[v], "TAB1 maps two inputs to {:#04x}", v);
            seen[v] = true;
        }
        // Check involution property: TAB1[TAB1[x]] should map back predictably
        // TAB1 is not necessarily a strict involution, but we verify the
        // composition TAB1[TAB1[x]] is also a permutation
        let mut seen2 = [false; 256];
        for i in 0..256 {
            let v = TAB1[TAB1[i] as usize] as usize;
            assert!(!seen2[v], "TAB1[TAB1[x]] maps two inputs to {:#04x}", v);
            seen2[v] = true;
        }
    }

    /// css_tab4_is_bit_reversal
    ///
    /// TAB4 reverses the bits of each byte: TAB4[0x01] = 0x80, TAB4[0x80] = 0x01, etc.
    #[test]
    fn css_tab4_is_bit_reversal() {
        for i in 0u16..256 {
            let expected = (0..8).fold(0u8, |acc, bit| acc | (((i as u8 >> bit) & 1) << (7 - bit)));
            assert_eq!(
                TAB4[i as usize], expected,
                "TAB4[{:#04x}] = {:#04x}, expected {:#04x} (bit reversal)",
                i, TAB4[i as usize], expected
            );
        }
        // Also verify TAB4 is an involution: TAB4[TAB4[x]] == x
        for i in 0..256 {
            assert_eq!(
                TAB4[TAB4[i] as usize], i as u8,
                "TAB4 is not an involution at {:#04x}",
                i
            );
        }
    }

    // ── scramble-flag detection (byte 0x14, bits 4-5) ──────────────────────

    /// Only bits 4-5 of byte 0x14 are the CSS scramble flag: the code reads
    /// `(sector[0x14] >> 4) & 0x03`. Bit 6 (0x40) and bit 7 (0x80) are NOT
    /// part of the flag, so a sector with 0x14 == 0x40 or 0x80 must be treated
    /// as UNSCRAMBLED and left byte-for-byte unchanged. This guards against a
    /// too-wide mask silently "descrambling" (and thus corrupting) clear data.
    ///
    /// Grounding: CSS sector header byte 0x14 — copyright/scramble bits live
    /// in bits 4-5; the 2-bit value 0 means not scrambled.
    /// Mutation: change `(sector[0x14] >> 4) & 0x03` to `& 0x07` or drop the
    /// shift -> 0x40 would be seen as scrambled and the body would change.
    #[test]
    fn descramble_treats_high_bits_of_0x14_as_clear() {
        let key = [0x01, 0x02, 0x03, 0x04, 0x05];
        for &flag in &[0x40u8, 0x80, 0xC0, 0x0F, 0x4F, 0x8F] {
            let mut sector = vec![0xAA; 2048];
            sector[0x14] = flag;
            sector[0x54..0x59].copy_from_slice(&[0x11, 0x22, 0x33, 0x44, 0x55]);
            let original = sector.clone();
            descramble_sector(&key, &mut sector);
            assert_eq!(
                sector, original,
                "byte 0x14 = {flag:#04x} has flag bits 4-5 clear; sector must be untouched"
            );
        }
    }

    /// Each individual scramble bit (4 and 5) independently marks the sector
    /// as encrypted: 0x10 and 0x20 must both trigger descrambling.
    ///
    /// Grounding: `(0x10 >> 4) & 3 == 1`, `(0x20 >> 4) & 3 == 2` — both
    /// nonzero.
    /// Mutation: change `!= 0` early-return condition to `== 3` -> a sector
    /// flagged only 0x10 or 0x20 would be skipped and left scrambled.
    #[test]
    fn descramble_triggers_on_either_flag_bit() {
        let key = [0x01, 0x02, 0x03, 0x04, 0x05];
        for &flag in &[0x10u8, 0x20, 0x30] {
            let mut sector = vec![0xAA; 2048];
            sector[0x14] = flag;
            sector[0x54..0x59].copy_from_slice(&[0xDE, 0xAD, 0xBE, 0xEF, 0x42]);
            let original = sector.clone();
            descramble_sector(&key, &mut sector);
            assert_ne!(
                &sector[128..256],
                &original[128..256],
                "flag {flag:#04x} (bits 4-5 nonzero) must descramble the body"
            );
        }
    }

    /// After descrambling, ONLY the two scramble bits are cleared (`& 0xCF`);
    /// bits 6 and 7 of byte 0x14 must be preserved. A sector with 0x14 == 0xF0
    /// becomes 0xC0 (bits 6,7 kept, bits 4,5 cleared), NOT 0x00.
    ///
    /// Grounding: code does `sector[0x14] &= 0xCF`; 0xF0 & 0xCF == 0xC0.
    /// Mutation: change `&= 0xCF` to `= 0` or `&= 0x0F` -> the preserved
    /// high bits assert fails.
    #[test]
    fn descramble_clear_preserves_high_bits_of_0x14() {
        let key = [0x01, 0x02, 0x03, 0x04, 0x05];
        let mut sector = vec![0x00; 2048];
        sector[0x14] = 0xF0; // bits 4-7 set; bits 4-5 are the flag
        sector[0x54..0x59].copy_from_slice(&[0x00; 5]);
        descramble_sector(&key, &mut sector);
        assert_eq!(
            sector[0x14], 0xC0,
            "scramble bits cleared, bits 6-7 preserved (0xF0 & 0xCF)"
        );
    }

    // ── header / body boundary (encrypted region is 0x80..0x800) ───────────

    /// The encrypted region is exactly bytes 0x80..0x800. Bytes 0x00..0x80
    /// (the header) must NOT be modified by the keystream — except byte 0x14
    /// whose flag is cleared. In particular the sector-seed bytes 0x54..0x59
    /// (which live inside the header) must survive untouched, since the
    /// descrambler reads them but never writes them.
    ///
    /// Grounding: loop is `sector.iter_mut().take(2048).skip(128)` -> indices
    /// 128..2048 only.
    /// Mutation: change `.skip(128)` to `.skip(0)` -> header bytes (incl. the
    /// seed) get XORed and this fails.
    #[test]
    fn descramble_leaves_header_and_seed_intact() {
        let key = [0x42, 0x13, 0x37, 0xBE, 0xEF];
        let mut sector = vec![0x5Au8; 2048];
        sector[0x14] = 0x30;
        let seed = [0xDE, 0xAD, 0xBE, 0xEF, 0x42];
        sector[0x54..0x59].copy_from_slice(&seed);
        let original = sector.clone();
        descramble_sector(&key, &mut sector);
        for i in 0..0x80usize {
            if i == 0x14 {
                continue;
            }
            assert_eq!(
                sector[i], original[i],
                "header byte {i:#04x} must be untouched"
            );
        }
        assert_eq!(&sector[0x54..0x59], &seed, "sector seed must survive");
    }

    /// The descrambler must touch the WHOLE body 0x80..0x800, not just a
    /// prefix. With a constant body and constant key, the keystream is
    /// non-degenerate enough that the very last sector byte (index 2047) is
    /// altered. This guards the loop bound `.take(2048)` against an
    /// off-by-one that would leave the final byte(s) scrambled.
    ///
    /// Grounding: encrypted region end is 0x800 == 2048 (exclusive).
    /// Mutation: change `.take(2048)` to `.take(2047)` -> last byte unchanged,
    /// assert fires (keystream byte for the last position is verified nonzero
    /// below by the round-trip, and this body is all-zero so any XOR shows).
    #[test]
    fn descramble_covers_final_body_byte() {
        let key = [0x42, 0x13, 0x37, 0xBE, 0xEF];
        let mut sector = vec![0x00u8; 2048];
        sector[0x14] = 0x30;
        sector[0x54..0x59].copy_from_slice(&[0x11, 0x22, 0x33, 0x44, 0x55]);
        descramble_sector(&key, &mut sector);
        // Body was all zero; any nonzero in [0x80,0x800) is keystream. Confirm
        // the keystream reaches the final byte. (If the last keystream byte
        // happened to be 0 this could be a flaky test, so assert the run-end
        // region as a whole differs from zero.)
        assert_ne!(
            &sector[2040..2048],
            &[0u8; 8][..],
            "the tail of the body must be descrambled (loop must reach index 2047)"
        );
    }

    /// Descramble is keyed by `title_key XOR seed`: two different title keys
    /// produce two different bodies for the same scrambled input. A cipher
    /// that ignored the title key (or mixed it in wrongly) would yield
    /// identical output — silent wrong-key decryption.
    ///
    /// Grounding: per-sector key = title_key[i] ^ sector[0x54+i].
    /// Mutation: in the `key` array drop the `title_key[i] ^` term -> both
    /// keys give the same body, assert fires.
    #[test]
    fn descramble_output_depends_on_title_key() {
        let seed = [0xDE, 0xAD, 0xBE, 0xEF, 0x42];
        let make = |k: &[u8; 5]| {
            let mut s = vec![0x00u8; 2048];
            s[0x14] = 0x30;
            s[0x54..0x59].copy_from_slice(&seed);
            descramble_sector(k, &mut s);
            s
        };
        let a = make(&[0x01, 0x02, 0x03, 0x04, 0x05]);
        let b = make(&[0x01, 0x02, 0x03, 0x04, 0x06]); // differs in last byte
        assert_ne!(
            &a[128..2048],
            &b[128..2048],
            "different title keys must descramble differently"
        );
    }

    /// Descramble is keyed by the sector seed too: same title key, different
    /// seed -> different body. Pins that bytes 0x54..0x59 actually feed the
    /// keystream (not just the per-sector XOR key).
    ///
    /// Mutation: replace `seed` array reads with a constant -> both seeds give
    /// the same body, assert fires.
    #[test]
    fn descramble_output_depends_on_seed() {
        let key = [0x01, 0x02, 0x03, 0x04, 0x05];
        let make = |seed: [u8; 5]| {
            let mut s = vec![0x00u8; 2048];
            s[0x14] = 0x30;
            s[0x54..0x59].copy_from_slice(&seed);
            descramble_sector(&key, &mut s);
            s
        };
        let a = make([0x11, 0x22, 0x33, 0x44, 0x55]);
        let b = make([0x11, 0x22, 0x33, 0x44, 0x56]);
        assert_ne!(
            &a[128..2048],
            &b[128..2048],
            "different seeds must descramble differently"
        );
    }

    // ── decrypt_key chained-XOR dependency structure ───────────────────────

    /// css_DecryptKey's two TAB1 rounds form a fixed dependency chain. After
    /// both rounds, `result[0]` is the last value computed and depends on the
    /// full key/crypted state; but the FIRST-round seed for `result[4]` is
    /// `k[4] ^ TAB1[p_crypted[4]] ^ p_crypted[3]`. Changing ONLY p_crypted[4]
    /// must change the output (p_crypted[4] feeds result[4] which propagates).
    ///
    /// Grounding: lines computing result[4] use p_crypted[4] and p_crypted[3].
    /// Mutation: in `result[4] = k[4] ^ TAB1[p_crypted[4]] ^ p_crypted[3]`
    /// drop the `TAB1[p_crypted[4]]` term -> output stops depending on
    /// p_crypted[4], this assert fires.
    #[test]
    fn decrypt_key_depends_on_every_crypted_byte() {
        let key = [0x12, 0x34, 0x56, 0x78, 0x9A];
        let base = [0xAB, 0xCD, 0xEF, 0x01, 0x23];
        let base_out = decrypt_key(0xFF, &key, &base);
        for i in 0..5 {
            let mut c = base;
            c[i] ^= 0x01;
            assert_ne!(
                decrypt_key(0xFF, &key, &c),
                base_out,
                "flipping crypted byte {i} did not change the decrypted key"
            );
        }
    }

    /// Likewise every key byte feeds the LFSR seeding (key[0],key[1] seed
    /// LFSR1; key[2..5] seed LFSR0 via seed_lfsr0). Flipping any single key
    /// byte must change the output.
    ///
    /// Grounding: lfsr1_lo=key[0]|0x100, lfsr1_hi=key[1], seed_lfsr0(key) uses
    /// key[2],key[3],key[4].
    /// Mutation: in seed_lfsr0 drop the `(key[4] as u32) << 17` term -> key[4]
    /// no longer influences LFSR0, this assert fires for i==4.
    #[test]
    fn decrypt_key_depends_on_every_key_byte() {
        let base_key = [0x12, 0x34, 0x56, 0x78, 0x9A];
        let crypted = [0xAB, 0xCD, 0xEF, 0x01, 0x23];
        let base_out = decrypt_key(0xFF, &base_key, &crypted);
        for i in 0..5 {
            let mut k = base_key;
            k[i] ^= 0x01;
            assert_ne!(
                decrypt_key(0xFF, &k, &crypted),
                base_out,
                "flipping key byte {i} did not change the decrypted key"
            );
        }
    }

    /// The invert byte (0x00 vs 0xFF) selects the LFSR0 output index in
    /// css_step via `TAB4[(o_lfsr0 ^ invert) as usize]`. For a non-degenerate
    /// key it must change the keystream and hence the result. (Pins that the
    /// invert parameter is actually wired into the LFSR0 path, distinguishing
    /// the disc-key vs title-key code paths.)
    ///
    /// Grounding: css_step's `TAB4[(o_lfsr0 ^ invert)]`.
    /// Mutation: hardcode `invert` to 0 inside css_step -> r0 == rff, fails.
    #[test]
    fn decrypt_key_invert_changes_result() {
        let key = [0x12, 0x34, 0x56, 0x78, 0x9A];
        let crypted = [0xAB, 0xCD, 0xEF, 0x01, 0x23];
        assert_ne!(
            decrypt_key(0x00, &key, &crypted),
            decrypt_key(0xFF, &key, &crypted),
            "invert must alter the LFSR0 keystream"
        );
    }
}