bitlocker-core 0.1.0

From-scratch reader/decryptor for BitLocker Drive Encryption (BDE) volumes — password unlock, AES-CBC + Elephant Diffuser
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
//! Public API: parse a BitLocker volume's metadata and unlock it from a password.
//!
//! [`BitLockerVolume::unlock_with_password`] runs the full chain — parse the
//! volume header, locate and parse the FVE metadata, derive the VMK from the
//! password (stretch → AES-CCM), unwrap the FVEK/TWEAK, and return a
//! [`DecryptedVolume`] exposing a plaintext `Read + Seek` view that honours
//! BitLocker's volume-header relocation and metadata-region blanking.

use std::io::{Read, Seek, SeekFrom};

use crate::crypto::{aes_ccm_unwrap, password_hash, stretch_key, SectorCipher};
use crate::error::{BdeError, Result};
use crate::header::VolumeHeader;
use crate::metadata::{FveMetadata, PROTECTION_PASSWORD, VALUE_TYPE_AES_CCM, VALUE_TYPE_STRETCH};

/// Encryption method decrypted by this build: AES-128-CBC + Elephant Diffuser.
const METHOD_AES128_CBC_DIFFUSER: u16 = 0x8000;
/// Encryption method sentinel: the volume is not encrypted.
const METHOD_NONE: u16 = 0x0000;
/// The fixed BitLocker sector size.
const SECTOR_SIZE: usize = 512;
/// How many bytes to read at a candidate metadata offset (reserved FVE metadata
/// blocks are well under this).
const METADATA_READ_LEN: usize = 64 * 1024;

/// Namespace for opening a BitLocker volume. All state lives in the returned
/// [`DecryptedVolume`]; this type carries no data itself.
pub struct BitLockerVolume;

impl BitLockerVolume {
    /// Parse the volume header and the first valid FVE metadata block from
    /// `reader`, without unlocking. Used by the forensic analyzer to audit the
    /// key protectors even when no password is known.
    ///
    /// # Errors
    /// [`BdeError::NotBitLocker`] if the header signature is absent, or
    /// [`BdeError::NoValidMetadata`] if no `-FVE-FS-` block is found.
    pub fn read_metadata<R: Read + Seek>(reader: &mut R) -> Result<FveMetadata> {
        let mut header = [0u8; SECTOR_SIZE];
        reader.seek(SeekFrom::Start(0))?;
        read_fill(reader, &mut header)?;
        let volume_header = VolumeHeader::parse(&header)?;

        let offsets = volume_header.fve_metadata_offsets;
        for &offset in &offsets {
            if offset == 0 {
                continue;
            }
            let mut block = vec![0u8; METADATA_READ_LEN];
            reader.seek(SeekFrom::Start(offset))?;
            let n = read_available(reader, &mut block)?;
            block.truncate(n);
            if let Some(meta) = FveMetadata::parse(&block, volume_header.bytes_per_sector) {
                return Ok(meta);
            }
        }
        Err(BdeError::NoValidMetadata { offsets })
    }

    /// Unlock the volume with `password`, returning a plaintext view.
    ///
    /// # Errors
    /// Fails loud on a non-BitLocker image, an unsupported cipher, a missing
    /// password protector, absent key material, or a wrong password (the AES-CCM
    /// tag fails to verify).
    pub fn unlock_with_password<R: Read + Seek>(
        mut reader: R,
        password: &str,
    ) -> Result<DecryptedVolume<R>> {
        let metadata = Self::read_metadata(&mut reader)?;

        if metadata.encryption_method != METHOD_AES128_CBC_DIFFUSER {
            return Err(BdeError::UnsupportedEncryptionMethod {
                method: metadata.encryption_method,
            });
        }

        let cipher = derive_cipher(&metadata, password)?;
        let total_size = reader.seek(SeekFrom::End(0))?;

        Ok(DecryptedVolume {
            reader,
            cipher,
            metadata,
            total_size,
            position: 0,
        })
    }
}

/// Derive the sector cipher (FVEK + TWEAK) from the password-protected VMK.
fn derive_cipher(metadata: &FveMetadata, password: &str) -> Result<SectorCipher> {
    // 1. Locate the password-protected VMK.
    let vmk = metadata
        .vmk_entries()
        .find(|e| e.protection_type() == Some(PROTECTION_PASSWORD))
        .ok_or_else(|| BdeError::NoPasswordProtector {
            found: metadata.protector_types(),
        })?;

    // VMK properties are nested entries starting at value-data offset 28.
    let props = vmk.nested(28);
    let stretch = props
        .iter()
        .find(|e| e.value_type == VALUE_TYPE_STRETCH)
        .ok_or(BdeError::MissingKeyMaterial {
            what: "stretch key",
        })?;
    let vmk_ccm = props
        .iter()
        .find(|e| e.value_type == VALUE_TYPE_AES_CCM)
        .ok_or(BdeError::MissingKeyMaterial {
            what: "VMK AES-CCM key",
        })?;

    // Salt is at stretch value-data offset 4 (after the 4-byte method).
    let mut salt = [0u8; 16];
    let salt_src = stretch
        .data
        .get(4..20)
        .ok_or(BdeError::MissingKeyMaterial {
            what: "stretch salt",
        })?;
    salt.copy_from_slice(salt_src);

    // 2. Password -> stretched key -> unwrap the VMK.
    let stretched = stretch_key(&password_hash(password), &salt);
    let vmk_container =
        aes_ccm_unwrap(&stretched, &vmk_ccm.data).ok_or(BdeError::AuthenticationFailed {
            what: "volume master key",
        })?;
    let vmk_key = take_key32(&vmk_container, 12, "volume master key")?;

    // 3. FVEK entry -> unwrap with the VMK -> FVEK + TWEAK (first 16 bytes each).
    let fvek_entry = metadata
        .fvek_entry()
        .ok_or(BdeError::MissingKeyMaterial { what: "FVEK entry" })?;
    let fvek_container = aes_ccm_unwrap(&vmk_key, &fvek_entry.data)
        .ok_or(BdeError::AuthenticationFailed { what: "FVEK" })?;
    let fvek = take_key16(&fvek_container, 12, "FVEK")?;
    let tweak = take_key16(&fvek_container, 44, "FVEK")?;

    Ok(SectorCipher::new(fvek, tweak))
}

fn take_key32(container: &[u8], off: usize, what: &'static str) -> Result<[u8; 32]> {
    let s = container
        .get(off..off + 32)
        .ok_or(BdeError::MalformedKeyContainer {
            what,
            got: container.len(),
            need: off + 32,
        })?;
    let mut k = [0u8; 32];
    k.copy_from_slice(s);
    Ok(k)
}

fn take_key16(container: &[u8], off: usize, what: &'static str) -> Result<[u8; 16]> {
    let s = container
        .get(off..off + 16)
        .ok_or(BdeError::MalformedKeyContainer {
            what,
            got: container.len(),
            need: off + 16,
        })?;
    let mut k = [0u8; 16];
    k.copy_from_slice(s);
    Ok(k)
}

/// A plaintext view of an unlocked BitLocker volume.
pub struct DecryptedVolume<R> {
    reader: R,
    cipher: SectorCipher,
    metadata: FveMetadata,
    total_size: u64,
    position: u64,
}

impl<R: Read + Seek> DecryptedVolume<R> {
    /// The parsed FVE metadata (cipher, protectors, volume GUID, …).
    #[must_use]
    pub fn metadata(&self) -> &FveMetadata {
        &self.metadata
    }

    /// The total size of the volume in bytes.
    #[must_use]
    pub fn volume_size(&self) -> u64 {
        self.total_size
    }

    /// Read decrypted bytes at logical `offset` into `buf`, filling it completely
    /// (bytes past the end of the volume read back as zero).
    ///
    /// # Errors
    /// Propagates I/O errors from the underlying reader.
    pub fn read_at(&mut self, offset: u64, buf: &mut [u8]) -> Result<()> {
        let mut done = 0usize;
        while done < buf.len() {
            let pos = offset + done as u64;
            let sector_start = pos - (pos % SECTOR_SIZE as u64);
            let within = (pos - sector_start) as usize;
            let plain = self.decrypt_logical_sector(sector_start)?;
            let take = (SECTOR_SIZE - within).min(buf.len() - done);
            buf[done..done + take].copy_from_slice(&plain[within..within + take]);
            done += take;
        }
        Ok(())
    }

    /// Decrypt the logical 512-byte sector starting at `sector_start` (a multiple
    /// of 512), applying metadata-region blanking, volume-header relocation, and
    /// the encrypted-volume-size boundary exactly as BitLocker's read path does.
    fn decrypt_logical_sector(&mut self, sector_start: u64) -> Result<[u8; SECTOR_SIZE]> {
        // Metadata block regions read back as zero in the decrypted view.
        let meta_size = u64::from(self.metadata.metadata_size);
        for &m in &self.metadata.metadata_offsets {
            if m != 0 && sector_start >= m && sector_start < m + meta_size {
                return Ok([0u8; SECTOR_SIZE]);
            }
        }

        // The first `volume_header_size` bytes are stored, encrypted, elsewhere;
        // the physical location doubles as the IV sector address.
        let physical = if sector_start < self.metadata.volume_header_size {
            self.metadata.volume_header_offset + sector_start
        } else {
            sector_start
        };

        let mut ct = [0u8; SECTOR_SIZE];
        self.reader.seek(SeekFrom::Start(physical))?;
        read_available(&mut self.reader, &mut ct)?;

        // Not encrypted: NONE method, or past the still-encrypted region.
        let evs = self.metadata.encrypted_volume_size;
        if self.metadata.encryption_method == METHOD_NONE || (evs != 0 && physical >= evs) {
            return Ok(ct);
        }

        let plain = self.cipher.decrypt_sector(&ct, physical);
        let mut out = [0u8; SECTOR_SIZE];
        let n = plain.len().min(SECTOR_SIZE);
        out[..n].copy_from_slice(&plain[..n]);
        Ok(out)
    }
}

impl<R: Read + Seek> Read for DecryptedVolume<R> {
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        if self.position >= self.total_size {
            return Ok(0);
        }
        let remaining = self.total_size - self.position;
        let n = (buf.len() as u64).min(remaining) as usize;
        self.read_at(self.position, &mut buf[..n])
            .map_err(|e| std::io::Error::other(e.to_string()))?;
        self.position += n as u64;
        Ok(n)
    }
}

impl<R: Read + Seek> Seek for DecryptedVolume<R> {
    fn seek(&mut self, pos: SeekFrom) -> std::io::Result<u64> {
        let new = match pos {
            SeekFrom::Start(o) => i128::from(o),
            SeekFrom::End(o) => i128::from(self.total_size) + i128::from(o),
            SeekFrom::Current(o) => i128::from(self.position) + i128::from(o),
        };
        if new < 0 {
            return Err(std::io::Error::new(
                std::io::ErrorKind::InvalidInput,
                "seek before start",
            ));
        }
        self.position = new as u64;
        Ok(self.position)
    }
}

/// Read exactly `buf.len()` bytes, erroring on premature EOF.
fn read_fill<R: Read>(reader: &mut R, buf: &mut [u8]) -> Result<()> {
    reader.read_exact(buf)?;
    Ok(())
}

/// Read up to `buf.len()` bytes, zero-filling the remainder on EOF. Returns the
/// number of real bytes read.
fn read_available<R: Read>(reader: &mut R, buf: &mut [u8]) -> Result<usize> {
    let mut filled = 0;
    while filled < buf.len() {
        match reader.read(&mut buf[filled..]) {
            Ok(0) => break,
            Ok(n) => filled += n,
            Err(ref e) if e.kind() == std::io::ErrorKind::Interrupted => {}
            Err(e) => return Err(e.into()),
        }
    }
    for b in &mut buf[filled..] {
        *b = 0;
    }
    Ok(filled)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::crypto::{aes_ccm_wrap, password_hash, stretch_key, SectorCipher};
    use crate::metadata::{
        ENTRY_TYPE_FVEK, ENTRY_TYPE_VMK, ENTRY_TYPE_VOLUME_HEADER, PROTECTION_PASSWORD,
    };
    use std::io::Cursor;

    const RELOCATED_OFFSET: u64 = 0x4000;
    const META_BLOCK_OFFSET: u64 = 0x1000;
    const IMAGE_SIZE: usize = 0x5000;
    // Smaller than the image so a physical offset past it exercises the
    // still-encrypted-region boundary (bytes beyond are returned raw).
    const ENCRYPTED_SIZE: u64 = 0x4800;

    fn entry(entry_type: u16, value_type: u16, data: &[u8]) -> Vec<u8> {
        let size = (8 + data.len()) as u16;
        let mut v = Vec::new();
        v.extend_from_slice(&size.to_le_bytes());
        v.extend_from_slice(&entry_type.to_le_bytes());
        v.extend_from_slice(&value_type.to_le_bytes());
        v.extend_from_slice(&1u16.to_le_bytes());
        v.extend_from_slice(data);
        v
    }

    /// Build a minimal synthetic BitLocker To Go volume plus the plaintext of its
    /// relocated first sector.
    fn build_volume(password: &str) -> (Vec<u8>, [u8; 512]) {
        let salt = [0x33u8; 16];
        let fvek = [0x11u8; 16];
        let tweak = [0x22u8; 16];
        let vmk = [0x44u8; 32];

        let mut vmk_container = vec![0u8; 44];
        vmk_container[12..44].copy_from_slice(&vmk);
        let stretched = stretch_key(&password_hash(password), &salt);
        let vmk_ccm = aes_ccm_wrap(&stretched, &[0x55; 12], &vmk_container);

        let mut fvek_container = vec![0u8; 76];
        fvek_container[12..28].copy_from_slice(&fvek);
        fvek_container[44..60].copy_from_slice(&tweak);
        let fvek_ccm = aes_ccm_wrap(&vmk, &[0x66; 12], &fvek_container);

        let mut stretch_data = vec![0u8; 4]; // 4-byte method
        stretch_data.extend_from_slice(&salt);
        let stretch_entry = entry(0, VALUE_TYPE_STRETCH, &stretch_data);
        let vmk_ccm_entry = entry(0, VALUE_TYPE_AES_CCM, &vmk_ccm);

        let mut vmk_data = vec![0u8; 28];
        vmk_data[26..28].copy_from_slice(&PROTECTION_PASSWORD.to_le_bytes());
        vmk_data.extend_from_slice(&stretch_entry);
        vmk_data.extend_from_slice(&vmk_ccm_entry);
        let vmk_entry = entry(ENTRY_TYPE_VMK, VALUE_TYPE_VMK, &vmk_data);

        let fvek_entry = entry(ENTRY_TYPE_FVEK, VALUE_TYPE_AES_CCM, &fvek_ccm);

        let mut vh_data = Vec::new();
        vh_data.extend_from_slice(&RELOCATED_OFFSET.to_le_bytes());
        vh_data.extend_from_slice(&512u64.to_le_bytes());
        let vh_entry = entry(ENTRY_TYPE_VOLUME_HEADER, ENTRY_TYPE_VOLUME_HEADER, &vh_data);

        let mut entries = Vec::new();
        entries.extend_from_slice(&vh_entry);
        entries.extend_from_slice(&vmk_entry);
        entries.extend_from_slice(&fvek_entry);
        let metadata_size = 48 + entries.len();

        let mut image = vec![0u8; IMAGE_SIZE];
        image[0..3].copy_from_slice(&[0xeb, 0x58, 0x90]);
        image[3..11].copy_from_slice(b"MSWIN4.1");
        image[12] = 0x02; // bytes per sector = 512
        image[440..448].copy_from_slice(&META_BLOCK_OFFSET.to_le_bytes());

        let mb = META_BLOCK_OFFSET as usize;
        image[mb..mb + 8].copy_from_slice(b"-FVE-FS-");
        image[mb + 10..mb + 12].copy_from_slice(&2u16.to_le_bytes());
        image[mb + 16..mb + 24].copy_from_slice(&ENCRYPTED_SIZE.to_le_bytes());
        image[mb + 28..mb + 32].copy_from_slice(&1u32.to_le_bytes());
        image[mb + 32..mb + 40].copy_from_slice(&META_BLOCK_OFFSET.to_le_bytes());
        image[mb + 56..mb + 64].copy_from_slice(&RELOCATED_OFFSET.to_le_bytes());
        image[mb + 64..mb + 68].copy_from_slice(&(metadata_size as u32).to_le_bytes());
        image[mb + 64 + 36..mb + 64 + 38].copy_from_slice(&0x8000u16.to_le_bytes());
        image[mb + 64 + 48..mb + 64 + 48 + entries.len()].copy_from_slice(&entries);

        let mut plain = [0u8; 512];
        for (i, b) in plain.iter_mut().enumerate() {
            *b = (i as u8) ^ 0x5a;
        }
        let cipher = SectorCipher::new(fvek, tweak);
        let ct = cipher.encrypt_sector(&plain, RELOCATED_OFFSET);
        let ro = RELOCATED_OFFSET as usize;
        image[ro..ro + 512].copy_from_slice(&ct);

        (image, plain)
    }

    use crate::metadata::{VALUE_TYPE_AES_CCM, VALUE_TYPE_STRETCH, VALUE_TYPE_VMK};

    #[test]
    fn unlock_and_read_synthetic_volume() {
        // Self-consistency (Tier-3): we author both encoder and decoder here, so
        // this proves the pipeline is internally consistent, NOT that it matches
        // BitLocker. The Tier-1 pybde oracle (oracle_bdetogo.rs) is the real proof.
        let (image, plain) = build_volume("test-pw");
        let mut vol = BitLockerVolume::unlock_with_password(Cursor::new(image), "test-pw").unwrap();
        let mut buf = [0u8; 512];
        vol.read_at(0, &mut buf).unwrap();
        assert_eq!(buf, plain);
        assert_eq!(vol.metadata().encryption_method, 0x8000);

        // A metadata-block region reads back as zero in the decrypted view.
        let mut z = [1u8; 512];
        vol.read_at(META_BLOCK_OFFSET, &mut z).unwrap();
        assert_eq!(z, [0u8; 512]);

        // A sector past the relocated volume-header region but still encrypted:
        // physical == logical, decrypted in place (no panic).
        let mut e = [0u8; 512];
        vol.read_at(0x600, &mut e).unwrap();

        // A sector at/after the encrypted-volume-size boundary is returned raw
        // (the image is zero there).
        let mut r = [0u8; 512];
        vol.read_at(ENCRYPTED_SIZE, &mut r).unwrap();
        assert_eq!(r, [0u8; 512]);
    }

    #[test]
    fn seek_variants_and_eof() {
        use std::io::{Read as _, Seek as _};
        let (image, _) = build_volume("test-pw");
        let mut vol = BitLockerVolume::unlock_with_password(Cursor::new(image), "test-pw").unwrap();

        assert_eq!(vol.seek(SeekFrom::End(0)).unwrap(), IMAGE_SIZE as u64);
        let mut b = [0u8; 16];
        assert_eq!(vol.read(&mut b).unwrap(), 0); // read at EOF

        assert_eq!(vol.seek(SeekFrom::Start(10)).unwrap(), 10);
        assert_eq!(vol.seek(SeekFrom::Current(5)).unwrap(), 15);
        assert!(vol.seek(SeekFrom::Current(-100)).is_err()); // before start
    }

    /// A metadata-only image (no key material) with configurable cipher,
    /// protectors, header offsets, and whether the block is actually present.
    fn meta_only_image(
        method: u16,
        protectors: &[u16],
        header_offsets: [u64; 3],
        block_at: Option<usize>,
    ) -> Vec<u8> {
        let mut entries = Vec::new();
        for p in protectors {
            let mut d = vec![0u8; 28];
            d[26..28].copy_from_slice(&p.to_le_bytes());
            entries.extend(entry(0x0002, 0x0008, &d));
        }
        let metadata_size = 48 + entries.len();
        let mut image = vec![0u8; 0x2000];
        image[0..3].copy_from_slice(&[0xeb, 0x58, 0x90]);
        image[3..11].copy_from_slice(b"MSWIN4.1");
        image[12] = 0x02;
        image[440..448].copy_from_slice(&header_offsets[0].to_le_bytes());
        image[448..456].copy_from_slice(&header_offsets[1].to_le_bytes());
        image[456..464].copy_from_slice(&header_offsets[2].to_le_bytes());
        if let Some(mb) = block_at {
            image[mb..mb + 8].copy_from_slice(b"-FVE-FS-");
            image[mb + 10..mb + 12].copy_from_slice(&2u16.to_le_bytes());
            image[mb + 32..mb + 40].copy_from_slice(&(mb as u64).to_le_bytes());
            image[mb + 64..mb + 68].copy_from_slice(&(metadata_size as u32).to_le_bytes());
            image[mb + 64 + 36..mb + 64 + 38].copy_from_slice(&method.to_le_bytes());
            image[mb + 64 + 48..mb + 64 + 48 + entries.len()].copy_from_slice(&entries);
        }
        image
    }

    #[test]
    fn unsupported_method_errors() {
        let img = meta_only_image(0x8004, &[0x2000], [0x1000, 0, 0], Some(0x1000));
        let res = BitLockerVolume::unlock_with_password(Cursor::new(img), "x");
        assert!(matches!(
            res,
            Err(BdeError::UnsupportedEncryptionMethod { method: 0x8004 })
        ));
    }

    #[test]
    fn no_password_protector_errors() {
        // Recovery-only volume — no password protector to unlock with.
        let img = meta_only_image(0x8000, &[0x0800], [0x1000, 0, 0], Some(0x1000));
        let res = BitLockerVolume::unlock_with_password(Cursor::new(img), "x");
        assert!(matches!(res, Err(BdeError::NoPasswordProtector { .. })));
    }

    #[test]
    fn no_valid_metadata_errors() {
        // Valid BitLocker header, but the block offsets point to non-FVE bytes.
        let img = meta_only_image(0x8000, &[0x2000], [0x1000, 0x1200, 0x1400], None);
        let err = BitLockerVolume::read_metadata(&mut Cursor::new(img)).unwrap_err();
        assert!(matches!(err, BdeError::NoValidMetadata { .. }));
    }

    #[test]
    fn read_metadata_skips_zero_first_offset() {
        // First offset zero (skipped), second valid — covers the `continue`.
        let img = meta_only_image(0x8000, &[0x2000], [0, 0x1000, 0], Some(0x1000));
        let meta = BitLockerVolume::read_metadata(&mut Cursor::new(img)).unwrap();
        assert_eq!(meta.encryption_method, 0x8000);
    }

    /// A reader that returns a valid header once, then a transient `Interrupted`,
    /// then a hard error — to exercise the I/O-error arms of `read_available`.
    struct FlakyReader {
        header: Vec<u8>,
        phase: usize,
    }

    impl Read for FlakyReader {
        fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
            self.phase += 1;
            match self.phase {
                1 => {
                    let n = buf.len().min(self.header.len());
                    buf[..n].copy_from_slice(&self.header[..n]);
                    Ok(n)
                }
                2 => Err(std::io::Error::new(
                    std::io::ErrorKind::Interrupted,
                    "eintr",
                )),
                _ => Err(std::io::Error::other("boom")),
            }
        }
    }

    impl Seek for FlakyReader {
        fn seek(&mut self, _pos: SeekFrom) -> std::io::Result<u64> {
            Ok(0)
        }
    }

    #[test]
    fn io_error_during_block_read_propagates() {
        let mut header = vec![0u8; 512];
        header[0..3].copy_from_slice(&[0xeb, 0x58, 0x90]);
        header[3..11].copy_from_slice(b"MSWIN4.1");
        header[12] = 0x02;
        header[440..448].copy_from_slice(&0x1000u64.to_le_bytes());
        let mut reader = FlakyReader { header, phase: 0 };
        let res = BitLockerVolume::read_metadata(&mut reader);
        assert!(matches!(res, Err(BdeError::Io(_))));
    }

    #[test]
    fn wrong_password_fails_authentication() {
        let (image, _) = build_volume("test-pw");
        let res = BitLockerVolume::unlock_with_password(Cursor::new(image), "wrong");
        assert!(matches!(
            res,
            Err(BdeError::AuthenticationFailed { what }) if what == "volume master key"
        ));
    }

    #[test]
    fn read_and_seek_traits() {
        use std::io::{Read as _, Seek as _};
        let (image, plain) = build_volume("test-pw");
        let mut vol = BitLockerVolume::unlock_with_password(Cursor::new(image), "test-pw").unwrap();
        vol.seek(SeekFrom::Start(0)).unwrap();
        let mut buf = [0u8; 256];
        vol.read_exact(&mut buf).unwrap();
        assert_eq!(&buf[..], &plain[..256]);
        assert_eq!(vol.volume_size(), IMAGE_SIZE as u64);
    }

    #[test]
    fn non_bitlocker_reader_errors() {
        let r = BitLockerVolume::unlock_with_password(Cursor::new(vec![0u8; 1024]), "x");
        assert!(matches!(r, Err(BdeError::NotBitLocker { .. })));
    }

    #[test]
    fn read_metadata_reports_protectors() {
        let (image, _) = build_volume("test-pw");
        let meta = BitLockerVolume::read_metadata(&mut Cursor::new(image)).unwrap();
        assert_eq!(meta.protector_types(), vec![PROTECTION_PASSWORD]);
    }
}