tomolib 1.1.1

Library for reading, modifying, and extracting Tomodachi Life data formats.
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
mod crypto;
mod keys;
mod romfs;
mod verify;

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

use crate::formats::nca::crypto::{Aes128Ecb, ctr_apply, xts_decrypt_sector};
use crate::formats::nsp::PartitionFs;
use crate::{Error, Result};

pub use crate::formats::nca::keys::KeySet;
pub use crate::formats::nca::romfs::FsEntry;

const SECTOR_SIZE: usize = 0x200;
const HEADER_SECTORS: usize = 6;
const HEADER_BLOCK_SIZE: usize = 0xC00;
const PARTITION_NUM: usize = 4;

const MAGIC_NCA2: &[u8; 4] = b"NCA2";
const MAGIC_NCA3: &[u8; 4] = b"NCA3";

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ContentType {
    Program,
    Meta,
    Control,
    Manual,
    Data,
    PublicData,
    Unknown(u8),
}

impl ContentType {
    fn from_u8(v: u8) -> Self {
        match v {
            0 => Self::Program,
            1 => Self::Meta,
            2 => Self::Control,
            3 => Self::Manual,
            4 => Self::Data,
            5 => Self::PublicData,
            other => Self::Unknown(other),
        }
    }

    #[must_use]
    pub fn name(self) -> &'static str {
        match self {
            Self::Program => "Program",
            Self::Meta => "Meta",
            Self::Control => "Control",
            Self::Manual => "Manual",
            Self::Data => "Data",
            Self::PublicData => "PublicData",
            Self::Unknown(_) => "Unknown",
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DistributionType {
    Download,
    GameCard,
    Unknown(u8),
}

impl DistributionType {
    #[must_use]
    pub fn name(self) -> &'static str {
        match self {
            Self::Download => "Download",
            Self::GameCard => "GameCard",
            Self::Unknown(_) => "Unknown",
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FormatType {
    RomFs,
    PartitionFs,
    Unknown(u8),
}

impl FormatType {
    #[must_use]
    pub fn name(self) -> &'static str {
        match self {
            Self::RomFs => "RomFs",
            Self::PartitionFs => "PartitionFs",
            Self::Unknown(_) => "Unknown",
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HashType {
    None,
    HierarchicalSha256,
    HierarchicalIntegrity,
    Other(u8),
}

impl HashType {
    #[must_use]
    pub fn name(self) -> &'static str {
        match self {
            Self::None => "None",
            Self::HierarchicalSha256 => "HierarchicalSha256",
            Self::HierarchicalIntegrity => "HierarchicalIntegrity",
            Self::Other(_) => "Other",
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EncryptionType {
    None,
    AesXts,
    AesCtr,
    AesCtrEx,
    Other(u8),
}

impl EncryptionType {
    #[must_use]
    pub fn name(self) -> &'static str {
        match self {
            Self::None => "None",
            Self::AesXts => "AesXts",
            Self::AesCtr => "AesCtr",
            Self::AesCtrEx => "AesCtrEx",
            Self::Other(_) => "Other",
        }
    }
}

#[derive(Debug, Clone)]
pub struct NcaHeader {
    pub format: &'static str,
    pub distribution: DistributionType,
    pub content_type: ContentType,
    pub key_generation: u8,
    pub signature_key_generation: u8,
    pub kaek_index: u8,
    pub content_size: u64,
    pub program_id: u64,
    pub content_index: u32,
    pub sdk_addon_version: u32,
    pub rights_id: [u8; 16],
}

impl NcaHeader {
    #[must_use]
    pub fn has_rights_id(&self) -> bool {
        self.rights_id.iter().any(|&b| b != 0)
    }

    #[must_use]
    pub fn sdk_addon_version_string(&self) -> String {
        let v = self.sdk_addon_version;
        let (major, minor, build, relstep) = (
            (v >> 24) & 0xff,
            (v >> 16) & 0xff,
            (v >> 8) & 0xff,
            v & 0xff,
        );
        if relstep > 0 {
            format!("{major}.{minor}.{build}-{relstep}")
        } else {
            format!("{major}.{minor}.{build}")
        }
    }
}

#[derive(Debug, Clone)]
pub struct Partition {
    pub index: usize,
    pub offset: u64,
    pub size: u64,
    pub format: FormatType,
    pub hash_type: HashType,
    pub enc_type: EncryptionType,
    pub fs_offset: u64,
    pub fs_size: u64,
    ctr_base: [u8; 16],
    hash_meta: verify::HashMeta,
}

#[derive(Debug)]
pub struct Nca {
    pub header: NcaHeader,
    pub partitions: Vec<Partition>,
    content_key: Option<[u8; 16]>,
}

fn le_u32(b: &[u8], off: usize) -> u32 {
    u32::from_le_bytes(b[off..off + 4].try_into().unwrap())
}
fn le_u64(b: &[u8], off: usize) -> u64 {
    u64::from_le_bytes(b[off..off + 8].try_into().unwrap())
}

impl Nca {
    pub fn open<R: Read + Seek>(reader: &mut R, keys: &KeySet) -> Result<Self> {
        let mut block = vec![0u8; HEADER_BLOCK_SIZE];
        reader.seek(SeekFrom::Start(0))?;
        reader.read_exact(&mut block)?;

        let (data_cipher, tweak_cipher) = keys.header_ciphers();
        decrypt_header(&mut block, data_cipher, tweak_cipher);

        let hdr = &block[0x200..0x400];
        let magic = &hdr[0..4];
        let format = if magic == MAGIC_NCA3 {
            "NCA3"
        } else if magic == MAGIC_NCA2 {
            "NCA2"
        } else {
            return Err(Error::bad_magic("NCA"));
        };

        let distribution = match hdr[4] {
            0 => DistributionType::Download,
            1 => DistributionType::GameCard,
            other => DistributionType::Unknown(other),
        };
        let content_type = ContentType::from_u8(hdr[5]);
        let key_generation = hdr[6].max(hdr[0x20]);
        let kaek_index = hdr[7];
        let content_size = le_u64(hdr, 0x08);
        let program_id = le_u64(hdr, 0x10);
        let content_index = le_u32(hdr, 0x18);
        let sdk_addon_version = le_u32(hdr, 0x1C);
        let signature_key_generation = hdr[0x21];
        let mut rights_id = [0u8; 16];
        rights_id.copy_from_slice(&hdr[0x30..0x40]);

        let header = NcaHeader {
            format,
            distribution,
            content_type,
            key_generation,
            signature_key_generation,
            kaek_index,
            content_size,
            program_id,
            content_index,
            sdk_addon_version,
            rights_id,
        };

        let master_key_rev = if key_generation == 0 {
            0
        } else {
            key_generation - 1
        };

        let content_key = resolve_content_key(&block, &header, master_key_rev, keys);

        let mut partitions = Vec::new();
        for i in 0..PARTITION_NUM {
            let eo = 0x200 + 0x40 + i * 0x10;
            let start_blk = le_u32(&block, eo);
            let end_blk = le_u32(&block, eo + 4);
            if end_blk <= start_blk {
                continue;
            }

            let offset = u64::from(start_blk) * SECTOR_SIZE as u64;
            let size = u64::from(end_blk - start_blk) * SECTOR_SIZE as u64;

            let fo = 0x400 + i * SECTOR_SIZE;
            let fs_header = &block[fo..fo + SECTOR_SIZE];
            partitions.push(parse_partition(i, offset, size, fs_header)?);
        }

        Ok(Self {
            header,
            partitions,
            content_key,
        })
    }

    #[must_use]
    pub fn has_content_key(&self) -> bool {
        self.content_key.is_some()
    }

    fn section_cipher(&self, part: &Partition) -> Result<SectionCipher> {
        match part.enc_type {
            EncryptionType::None => Ok(SectionCipher::None),
            EncryptionType::AesCtr => {
                let key = self
                    .content_key
                    .ok_or_else(|| Error::unsupported("AES-CTR key was not determined"))?;
                Ok(SectionCipher::Ctr {
                    key,
                    base_ctr: part.ctr_base,
                })
            }
            other => Err(Error::unsupported(format!(
                "section encryption {} is not supported",
                other.name()
            ))),
        }
    }

    fn section_stream<'a, R: Read + Seek>(
        &self,
        reader: &'a mut R,
        part: &Partition,
    ) -> Result<SectionStream<'a, R>> {
        Ok(SectionStream {
            inner: reader,
            fs_abs: part.offset + part.fs_offset,
            fs_size: part.fs_size,
            pos: 0,
            inner_pos: None,
            cipher: self.section_cipher(part)?,
        })
    }

    fn section_stream_full<'a, R: Read + Seek>(
        &self,
        reader: &'a mut R,
        part: &Partition,
    ) -> Result<SectionStream<'a, R>> {
        Ok(SectionStream {
            inner: reader,
            fs_abs: part.offset,
            fs_size: part.size,
            pos: 0,
            inner_pos: None,
            cipher: self.section_cipher(part)?,
        })
    }

    pub fn verify_partition<R: Read + Seek>(&self, reader: &mut R, part: &Partition) -> Result<()> {
        let mut stream = self.section_stream_full(reader, part)?;
        verify::verify(&mut stream, &part.hash_meta)
    }

    pub fn list_partition<R: Read + Seek>(
        &self,
        reader: &mut R,
        part: &Partition,
    ) -> Result<Vec<FsEntry>> {
        let mut stream = self.section_stream(reader, part)?;
        match part.format {
            FormatType::RomFs => romfs::list(&mut stream),
            FormatType::PartitionFs => list_pfs(&mut stream),
            FormatType::Unknown(v) => {
                Err(Error::unsupported(format!("unknown partition format {v}")))
            }
        }
    }

    pub fn copy_file<R: Read + Seek, W: io::Write>(
        &self,
        reader: &mut R,
        part: &Partition,
        entry: &FsEntry,
        out: &mut W,
    ) -> Result<()> {
        let mut stream = self.section_stream(reader, part)?;
        stream.seek(SeekFrom::Start(entry.offset))?;
        let mut remaining = entry.size;
        let mut buf = vec![0u8; 1 << 20];
        while remaining > 0 {
            let want = buf
                .len()
                .min(usize::try_from(remaining).unwrap_or(usize::MAX));
            stream.read_exact(&mut buf[..want])?;
            out.write_all(&buf[..want])?;
            remaining -= want as u64;
        }
        Ok(())
    }
}

fn resolve_content_key(
    block: &[u8],
    header: &NcaHeader,
    master_key_rev: u8,
    keys: &KeySet,
) -> Option<[u8; 16]> {
    if header.has_rights_id() {
        return keys
            .decrypt_title_key(&header.rights_id, master_key_rev)
            .ok();
    }

    let key_area_off = 0x200 + 0x100;
    let mut wrapped = [0u8; 16];
    wrapped.copy_from_slice(&block[key_area_off + 0x20..key_area_off + 0x30]);
    if wrapped.iter().all(|&b| b == 0) {
        return None;
    }
    keys.decrypt_key_area_key(usize::from(header.kaek_index), master_key_rev, &wrapped)
        .ok()
}

fn parse_partition(index: usize, offset: u64, size: u64, fs_header: &[u8]) -> Result<Partition> {
    let format = match fs_header[2] {
        0 => FormatType::RomFs,
        1 => FormatType::PartitionFs,
        other => FormatType::Unknown(other),
    };
    let hash_type = match fs_header[3] {
        1 => HashType::None,
        2 => HashType::HierarchicalSha256,
        3 => HashType::HierarchicalIntegrity,
        other => HashType::Other(other),
    };
    let enc_type = match fs_header[4] {
        1 => EncryptionType::None,
        2 => EncryptionType::AesXts,
        3 => EncryptionType::AesCtr,
        4 => EncryptionType::AesCtrEx,
        other => EncryptionType::Other(other),
    };

    let generation = le_u32(fs_header, 0x140);
    let secure_value = le_u32(fs_header, 0x144);
    let mut ctr_base = [0u8; 16];
    ctr_base[0..4].copy_from_slice(&secure_value.to_be_bytes());
    ctr_base[4..8].copy_from_slice(&generation.to_be_bytes());

    let hash_info = &fs_header[0x08..0x08 + 0xF8];
    let (hash_meta, fs_offset, fs_size) = match hash_type {
        HashType::HierarchicalSha256 => verify::parse_sha256(hash_info)?,
        HashType::HierarchicalIntegrity => verify::parse_ivfc(hash_info)?,
        HashType::None => (verify::HashMeta::None, 0, size),
        HashType::Other(v) => {
            return Err(Error::unsupported(format!("unsupported hash type {v}")));
        }
    };

    Ok(Partition {
        index,
        offset,
        size,
        format,
        hash_type,
        enc_type,
        fs_offset,
        fs_size,
        ctr_base,
        hash_meta,
    })
}

fn decrypt_header(block: &mut [u8], data_cipher: &Aes128Ecb, tweak_cipher: &Aes128Ecb) {
    let mut probe = block[SECTOR_SIZE..2 * SECTOR_SIZE].to_vec();
    xts_decrypt_sector(data_cipher, tweak_cipher, 1, &mut probe);
    let nca2 = &probe[0..4] == MAGIC_NCA2;

    for i in 0..HEADER_SECTORS {
        let sector = if nca2 && i >= 2 { 0 } else { i as u64 };
        let range = i * SECTOR_SIZE..(i + 1) * SECTOR_SIZE;
        xts_decrypt_sector(data_cipher, tweak_cipher, sector, &mut block[range]);
    }
}

fn list_pfs<R: Read + Seek>(stream: &mut SectionStream<'_, R>) -> Result<Vec<FsEntry>> {
    stream.seek(SeekFrom::Start(0))?;
    let fs = PartitionFs::read_header(stream)?;
    Ok(fs
        .entries()
        .iter()
        .map(|e| FsEntry {
            path: e.name.clone(),
            offset: e.offset,
            size: e.size,
        })
        .collect())
}

enum SectionCipher {
    None,
    Ctr { key: [u8; 16], base_ctr: [u8; 16] },
}

pub struct SectionStream<'a, R> {
    inner: &'a mut R,
    fs_abs: u64,
    fs_size: u64,
    pos: u64,
    inner_pos: Option<u64>,
    cipher: SectionCipher,
}

impl<R> std::fmt::Debug for SectionStream<'_, R> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("SectionStream")
            .field("fs_abs", &self.fs_abs)
            .field("fs_size", &self.fs_size)
            .field("pos", &self.pos)
            .finish()
    }
}

impl<R: Read + Seek> Read for SectionStream<'_, R> {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        let remaining = self.fs_size.saturating_sub(self.pos);
        let n = buf
            .len()
            .min(usize::try_from(remaining).unwrap_or(usize::MAX));
        if n == 0 {
            return Ok(0);
        }
        let abs = self.fs_abs + self.pos;
        if self.inner_pos != Some(abs) {
            self.inner.seek(SeekFrom::Start(abs))?;
        }
        self.inner.read_exact(&mut buf[..n])?;
        self.inner_pos = Some(abs + n as u64);
        if let SectionCipher::Ctr { key, base_ctr } = &self.cipher {
            ctr_apply(key, base_ctr, abs, &mut buf[..n]);
        }
        self.pos += n as u64;
        Ok(n)
    }
}

impl<R: Read + Seek> Seek for SectionStream<'_, R> {
    fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
        let new = match pos {
            SeekFrom::Start(n) => Some(n),
            SeekFrom::End(n) => self.fs_size.checked_add_signed(n),
            SeekFrom::Current(n) => self.pos.checked_add_signed(n),
        };
        self.pos =
            new.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "seek out of range"))?;
        Ok(self.pos)
    }
}