wxtla 0.3.0

Wired eXploring Target Layer Accessor
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
//! BitLocker key derivation and sector cryptography.

use aes::{
  Aes128, Aes256,
  cipher::{
    BlockCipher, BlockDecrypt, BlockEncrypt, BlockSizeUser, KeyInit, consts::U16 as BlockU16,
    generic_array::GenericArray,
  },
};
use ccm::{
  Ccm,
  aead::{
    AeadInPlace,
    consts::{U12, U16},
  },
};
use sha2::{Digest, Sha256};
use xts_mode::{Xts128, get_tweak_default};

use super::metadata::BitlockerEncryptionMethod;
use crate::{Error, Result};

type Aes256Ccm = Ccm<Aes256, U16, U12>;

pub(super) fn password_hash(password: &str) -> [u8; 32] {
  let utf16le = password
    .encode_utf16()
    .chain(std::iter::once(0))
    .flat_map(u16::to_le_bytes)
    .collect::<Vec<_>>();
  let first = Sha256::digest(&utf16le);
  let second = Sha256::digest(first);
  second.into()
}

pub(super) fn recovery_password_hash(recovery_password: &str) -> Result<[u8; 32]> {
  let segments = recovery_password.split('-').collect::<Vec<_>>();
  if segments.len() != 8 {
    return Err(Error::InvalidSourceReference(
      "bitlocker recovery passwords must contain 8 numeric groups".to_string(),
    ));
  }

  let mut binary = [0u8; 16];
  for (index, segment) in segments.iter().enumerate() {
    let value = segment.parse::<u64>().map_err(|_| {
      Error::InvalidSourceReference(
        "bitlocker recovery passwords must be decimal numbers".to_string(),
      )
    })?;
    if value % 11 != 0 {
      return Err(Error::InvalidSourceReference(
        "bitlocker recovery password groups must be divisible by 11".to_string(),
      ));
    }
    let decoded = value / 11;
    let decoded = u16::try_from(decoded).map_err(|_| {
      Error::InvalidSourceReference(
        "bitlocker recovery password groups are out of range".to_string(),
      )
    })?;
    binary[index * 2..index * 2 + 2].copy_from_slice(&decoded.to_le_bytes());
  }

  Ok(Sha256::digest(binary).into())
}

pub(super) fn password_aes_ccm_key(password_hash: &[u8; 32], salt: &[u8; 16]) -> [u8; 32] {
  let mut last_hash = [0u8; 32];
  for iteration_count in 0..0x0FFFFFu64 {
    let mut state = [0u8; 88];
    state[0..32].copy_from_slice(&last_hash);
    state[32..64].copy_from_slice(password_hash);
    state[64..80].copy_from_slice(salt);
    state[80..88].copy_from_slice(&iteration_count.to_le_bytes());
    last_hash.copy_from_slice(&Sha256::digest(state));
  }

  let mut state = [0u8; 88];
  state[0..32].copy_from_slice(&last_hash);
  state[32..64].copy_from_slice(password_hash);
  state[64..80].copy_from_slice(salt);
  state[80..88].copy_from_slice(&(0x0FFFFFu64).to_le_bytes());
  Sha256::digest(state).into()
}

pub(super) fn decrypt_aes_ccm_key_blob(
  aes_ccm_key: &[u8; 32], nonce: &[u8; 12], encrypted: &[u8],
) -> Result<Vec<u8>> {
  if encrypted.len() < 16 {
    return Err(Error::InvalidFormat(
      "bitlocker AES-CCM key blobs must include a 16-byte authentication tag".to_string(),
    ));
  }

  let cipher = Aes256Ccm::new_from_slice(aes_ccm_key)
    .map_err(|_| Error::InvalidFormat("bitlocker AES-CCM key size is invalid".to_string()))?;
  let tag = &encrypted[..16];
  let mut ciphertext = encrypted[16..].to_vec();
  let tag = GenericArray::from_slice(tag);
  cipher
    .decrypt_in_place_detached(nonce.into(), b"", &mut ciphertext, tag)
    .map_err(|_| Error::InvalidSourceReference("bitlocker key decryption failed".to_string()))?;

  let mut output = Vec::with_capacity(encrypted.len());
  output.extend_from_slice(&encrypted[..16]);
  output.extend_from_slice(&ciphertext);
  Ok(output)
}

pub(super) fn decrypt_sector(
  method: BitlockerEncryptionMethod, fvek: &[u8], tweak_key: Option<&[u8]>, block_key: u64,
  data: &mut [u8],
) -> Result<()> {
  match method {
    BitlockerEncryptionMethod::None => Ok(()),
    BitlockerEncryptionMethod::Aes128Cbc => aes_cbc_crypt::<Aes128>(false, fvek, block_key, data),
    BitlockerEncryptionMethod::Aes256Cbc => aes_cbc_crypt::<Aes256>(false, fvek, block_key, data),
    BitlockerEncryptionMethod::Aes128CbcDiffuser => {
      aes_cbc_crypt::<Aes128>(false, fvek, block_key, data)?;
      let tweak = tweak_key.ok_or_else(|| {
        Error::InvalidFormat("bitlocker diffuser encryption requires a tweak key".to_string())
      })?;
      diffuser_apply::<Aes128>(false, tweak, block_key, data)
    }
    BitlockerEncryptionMethod::Aes256CbcDiffuser => {
      aes_cbc_crypt::<Aes256>(false, fvek, block_key, data)?;
      let tweak = tweak_key.ok_or_else(|| {
        Error::InvalidFormat("bitlocker diffuser encryption requires a tweak key".to_string())
      })?;
      diffuser_apply::<Aes256>(false, tweak, block_key, data)
    }
    BitlockerEncryptionMethod::Aes128Xts => xts_crypt::<Aes128>(false, fvek, block_key, data),
    BitlockerEncryptionMethod::Aes256Xts => xts_crypt::<Aes256>(false, fvek, block_key, data),
  }
}

#[cfg(test)]
pub(super) fn encrypt_sector(
  method: BitlockerEncryptionMethod, fvek: &[u8], tweak_key: Option<&[u8]>, block_key: u64,
  data: &mut [u8],
) -> Result<()> {
  match method {
    BitlockerEncryptionMethod::None => Ok(()),
    BitlockerEncryptionMethod::Aes128Cbc => aes_cbc_crypt::<Aes128>(true, fvek, block_key, data),
    BitlockerEncryptionMethod::Aes256Cbc => aes_cbc_crypt::<Aes256>(true, fvek, block_key, data),
    BitlockerEncryptionMethod::Aes128CbcDiffuser => {
      let tweak = tweak_key.ok_or_else(|| {
        Error::InvalidFormat("bitlocker diffuser encryption requires a tweak key".to_string())
      })?;
      diffuser_apply::<Aes128>(true, tweak, block_key, data)?;
      aes_cbc_crypt::<Aes128>(true, fvek, block_key, data)
    }
    BitlockerEncryptionMethod::Aes256CbcDiffuser => {
      let tweak = tweak_key.ok_or_else(|| {
        Error::InvalidFormat("bitlocker diffuser encryption requires a tweak key".to_string())
      })?;
      diffuser_apply::<Aes256>(true, tweak, block_key, data)?;
      aes_cbc_crypt::<Aes256>(true, fvek, block_key, data)
    }
    BitlockerEncryptionMethod::Aes128Xts => xts_crypt::<Aes128>(true, fvek, block_key, data),
    BitlockerEncryptionMethod::Aes256Xts => xts_crypt::<Aes256>(true, fvek, block_key, data),
  }
}

fn aes_cbc_crypt<C>(encrypt: bool, key: &[u8], block_key: u64, data: &mut [u8]) -> Result<()>
where
  C: BlockEncrypt + BlockDecrypt + KeyInit + BlockSizeUser<BlockSize = BlockU16>, {
  if !data.len().is_multiple_of(16) {
    return Err(Error::InvalidFormat(
      "bitlocker AES-CBC sectors must be aligned to 16-byte blocks".to_string(),
    ));
  }
  let cipher = C::new_from_slice(key).map_err(|_| {
    Error::InvalidFormat("bitlocker FVEK length does not match the AES mode".to_string())
  })?;
  let mut iv_block = [0u8; 16];
  iv_block[..8].copy_from_slice(&block_key.to_le_bytes());
  let iv = encrypt_block_ecb(&cipher, iv_block);
  let mut prev = iv;

  for block in data.chunks_exact_mut(16) {
    let mut array = GenericArray::clone_from_slice(block);
    if encrypt {
      xor_in_place(block, &prev);
      array.copy_from_slice(block);
      cipher.encrypt_block(&mut array);
      block.copy_from_slice(&array);
      prev.copy_from_slice(block);
    } else {
      let mut current_ciphertext = [0u8; 16];
      current_ciphertext.copy_from_slice(block);
      cipher.decrypt_block(&mut array);
      let mut plaintext = [0u8; 16];
      plaintext.copy_from_slice(&array);
      xor_in_place(&mut plaintext, &prev);
      block.copy_from_slice(&plaintext);
      prev = current_ciphertext;
    }
  }

  Ok(())
}

fn xts_crypt<C>(encrypt: bool, key: &[u8], sector_index: u64, data: &mut [u8]) -> Result<()>
where
  C: BlockEncrypt
    + BlockDecrypt
    + BlockCipher
    + KeyInit
    + Clone
    + BlockSizeUser<BlockSize = BlockU16>, {
  let half = key.len() / 2;
  let cipher_1 = C::new_from_slice(&key[..half]).map_err(|_| {
    Error::InvalidFormat("bitlocker XTS key length does not match the AES mode".to_string())
  })?;
  let cipher_2 = C::new_from_slice(&key[half..]).map_err(|_| {
    Error::InvalidFormat("bitlocker XTS key length does not match the AES mode".to_string())
  })?;
  let xts = Xts128::<C>::new(cipher_1, cipher_2);
  if encrypt {
    xts.encrypt_sector(data, get_tweak_default(u128::from(sector_index)));
  } else {
    xts.decrypt_sector(data, get_tweak_default(u128::from(sector_index)));
  }
  Ok(())
}

fn diffuser_apply<C>(
  encrypt: bool, tweak_key: &[u8], block_key: u64, data: &mut [u8],
) -> Result<()>
where
  C: BlockEncrypt + KeyInit + BlockSizeUser<BlockSize = BlockU16>, {
  if !data.len().is_multiple_of(4) || data.len() < 32 {
    return Err(Error::InvalidFormat(
      "bitlocker diffuser blocks must be at least 32 bytes and 4-byte aligned".to_string(),
    ));
  }

  let cipher = C::new_from_slice(tweak_key).map_err(|_| {
    Error::InvalidFormat("bitlocker tweak key length does not match the AES mode".to_string())
  })?;
  let mut first_input = [0u8; 16];
  first_input[..8].copy_from_slice(&block_key.to_le_bytes());
  let first = encrypt_block_ecb(&cipher, first_input);
  let mut second_input = first_input;
  second_input[15] = 0x80;
  let second = encrypt_block_ecb(&cipher, second_input);
  let mut sector_key = [0u8; 32];
  sector_key[..16].copy_from_slice(&first);
  sector_key[16..].copy_from_slice(&second);

  if encrypt {
    xor_repeat(data, &sector_key);
    diffuser_encrypt(data);
  } else {
    diffuser_decrypt(data);
    xor_repeat(data, &sector_key);
  }

  Ok(())
}

fn encrypt_block_ecb<C>(cipher: &C, input: [u8; 16]) -> [u8; 16]
where
  C: BlockEncrypt + BlockSizeUser<BlockSize = BlockU16>, {
  let mut block = GenericArray::clone_from_slice(&input);
  cipher.encrypt_block(&mut block);
  let mut output = [0u8; 16];
  output.copy_from_slice(&block);
  output
}

fn xor_repeat(data: &mut [u8], key: &[u8; 32]) {
  for (index, byte) in data.iter_mut().enumerate() {
    *byte ^= key[index % 32];
  }
}

fn xor_in_place(left: &mut [u8], right: &[u8]) {
  for (l, r) in left.iter_mut().zip(right.iter()) {
    *l ^= *r;
  }
}

fn diffuser_decrypt(data: &mut [u8]) {
  let mut values = data
    .chunks_exact(4)
    .map(|chunk| u32::from_le_bytes(chunk.try_into().unwrap()))
    .collect::<Vec<_>>();
  diffuser_b_decrypt(&mut values);
  diffuser_a_decrypt(&mut values);
  for (chunk, value) in data.chunks_exact_mut(4).zip(values) {
    chunk.copy_from_slice(&value.to_le_bytes());
  }
}

fn diffuser_encrypt(data: &mut [u8]) {
  let mut values = data
    .chunks_exact(4)
    .map(|chunk| u32::from_le_bytes(chunk.try_into().unwrap()))
    .collect::<Vec<_>>();
  diffuser_a_encrypt(&mut values);
  diffuser_b_encrypt(&mut values);
  for (chunk, value) in data.chunks_exact_mut(4).zip(values) {
    chunk.copy_from_slice(&value.to_le_bytes());
  }
}

fn diffuser_a_decrypt(values: &mut [u32]) {
  let n = values.len();
  for _ in 0..5 {
    let mut i1 = 0usize;
    let mut i2 = n - 2;
    let mut i3 = n - 5;
    while i1 < n - 1 {
      values[i1] = values[i1].wrapping_add(values[i2] ^ values[i3].rotate_left(9));
      i1 += 1;
      i2 = (i2 + 1) % n;
      i3 = (i3 + 1) % n;

      values[i1] = values[i1].wrapping_add(values[i2] ^ values[i3]);
      i1 += 1;
      i2 = (i2 + 1) % n;
      i3 = (i3 + 1) % n;

      values[i1] = values[i1].wrapping_add(values[i2] ^ values[i3].rotate_left(13));
      i1 += 1;
      i2 = (i2 + 1) % n;
      i3 = (i3 + 1) % n;

      values[i1] = values[i1].wrapping_add(values[i2] ^ values[i3]);
      i1 += 1;
      i2 = (i2 + 1) % n;
      i3 = (i3 + 1) % n;
    }
  }
}

fn diffuser_b_decrypt(values: &mut [u32]) {
  let n = values.len();
  for _ in 0..3 {
    let mut i1 = 0usize;
    let mut i2 = n - 2;
    let mut i3 = n - 3;
    while i1 < n - 1 {
      values[i1] = values[i1].wrapping_add(values[i2] ^ values[i3].rotate_left(13));
      i1 += 1;
      i2 = (i2 + 1) % n;
      i3 = (i3 + 1) % n;

      values[i1] = values[i1].wrapping_add(values[i2] ^ values[i3]);
      i1 += 1;
      i2 = (i2 + 1) % n;
      i3 = (i3 + 1) % n;

      values[i1] = values[i1].wrapping_add(values[i2] ^ values[i3].rotate_left(10));
      i1 += 1;
      i2 = (i2 + 1) % n;
      i3 = (i3 + 1) % n;

      values[i1] = values[i1].wrapping_add(values[i2] ^ values[i3]);
      i1 += 1;
      i2 = (i2 + 1) % n;
      i3 = (i3 + 1) % n;
    }
  }
}

fn diffuser_a_encrypt(values: &mut [u32]) {
  let n = values.len();
  for _ in 0..5 {
    let mut i1 = n - 1;
    let mut i2 = n - 2;
    let mut i3 = n - 5;
    while i1 > 0 {
      values[i1] = values[i1].wrapping_sub(values[i2] ^ values[i3]);
      i1 -= 1;
      i2 = dec_index(i2, n);
      i3 = dec_index(i3, n);

      values[i1] = values[i1].wrapping_sub(values[i2] ^ values[i3].rotate_left(13));
      i1 -= 1;
      i2 = dec_index(i2, n);
      i3 = dec_index(i3, n);

      values[i1] = values[i1].wrapping_sub(values[i2] ^ values[i3]);
      i1 -= 1;
      i2 = dec_index(i2, n);
      i3 = dec_index(i3, n);

      values[i1] = values[i1].wrapping_sub(values[i2] ^ values[i3].rotate_left(9));
      if i1 == 0 {
        break;
      }
      i1 -= 1;
      i2 = dec_index(i2, n);
      i3 = dec_index(i3, n);
    }
  }
}

fn diffuser_b_encrypt(values: &mut [u32]) {
  let n = values.len();
  for _ in 0..3 {
    let mut i1 = n - 1;
    let mut i2 = n - 2;
    let mut i3 = n - 3;
    while i1 > 0 {
      values[i1] = values[i1].wrapping_sub(values[i2] ^ values[i3]);
      i1 -= 1;
      i2 = dec_index(i2, n);
      i3 = dec_index(i3, n);

      values[i1] = values[i1].wrapping_sub(values[i2] ^ values[i3].rotate_left(10));
      i1 -= 1;
      i2 = dec_index(i2, n);
      i3 = dec_index(i3, n);

      values[i1] = values[i1].wrapping_sub(values[i2] ^ values[i3]);
      i1 -= 1;
      i2 = dec_index(i2, n);
      i3 = dec_index(i3, n);

      values[i1] = values[i1].wrapping_sub(values[i2] ^ values[i3].rotate_left(13));
      if i1 == 0 {
        break;
      }
      i1 -= 1;
      i2 = dec_index(i2, n);
      i3 = dec_index(i3, n);
    }
  }
}

fn dec_index(index: usize, len: usize) -> usize {
  if index == 0 { len - 1 } else { index - 1 }
}

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

  fn to_hex(data: &[u8]) -> String {
    let mut output = String::with_capacity(data.len() * 2);
    for byte in data {
      use std::fmt::Write as _;
      let _ = write!(&mut output, "{byte:02x}");
    }
    output
  }

  #[test]
  fn derives_password_hash_like_libbde() {
    assert_eq!(
      to_hex(&password_hash("TeSt")),
      "f8559b5acf ab5409c126e8ac8a5939bffaa893f62ae8373b689ceea64bd47569".replace(' ', "")
    );
  }

  #[test]
  fn derives_recovery_password_hash() {
    let hash =
      recovery_password_hash("471207-278498-422125-177177-561902-537405-468006-693451").unwrap();
    assert_eq!(
      to_hex(&hash),
      "a4c9244478110805015c94d1b001a933ada2ab0b716bac342bb86c844b63491f"
    );
  }

  #[test]
  fn derives_password_aes_ccm_key() {
    let password_hash = password_hash("TeSt");
    let salt: [u8; 16] = [
      0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE,
      0xFF,
    ];
    let key = password_aes_ccm_key(&password_hash, &salt);
    assert_eq!(
      to_hex(&key),
      "241eddbf84a8f855da22ef18995a8e841c93ae93e4e5f3f90ace59b19c698188"
    );
  }
}