diff --git a/KEYS.md b/KEYS.md index 1a16a865..e8aa1be2 100644 --- a/KEYS.md +++ b/KEYS.md @@ -95,6 +95,8 @@ bis_key_source_01 = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX bis_key_source_02 = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX bis_kek_source = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX +save_mac_kek_source = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX +save_mac_key_source = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ``` ### Console-unique keys @@ -141,6 +143,8 @@ bis_kek_source bis_key_source_00 bis_key_source_01 bis_key_source_02 +save_mac_kek_source +save_mac_key_source header_key xci_header_key diff --git a/LibHac/Savefile/Savefile.cs b/LibHac/Savefile/Savefile.cs index 2d61b7db..b7389d8d 100644 --- a/LibHac/Savefile/Savefile.cs +++ b/LibHac/Savefile/Savefile.cs @@ -9,6 +9,7 @@ namespace LibHac.Savefile { public Header Header { get; } private RemapStream FileRemap { get; } + public SharedStreamSource SavefileSource { get; } public SharedStreamSource FileRemapSource { get; } private RemapStream MetaRemap { get; } public SharedStreamSource MetaRemapSource { get; } @@ -39,12 +40,15 @@ namespace LibHac.Savefile public Savefile(Keyset keyset, Stream file, IProgressReport logger = null) { - using (var reader = new BinaryReader(file, Encoding.Default, true)) + SavefileSource = new SharedStreamSource(file); + + using (var reader = new BinaryReader(SavefileSource.CreateStream(), Encoding.Default, true)) { Header = new Header(keyset, reader, logger); - var layout = Header.Layout; + FsLayout layout = Header.Layout; + FileRemap = new RemapStream( - new SubStream(file, layout.FileMapDataOffset, layout.FileMapDataSize), + SavefileSource.CreateStream(layout.FileMapDataOffset, layout.FileMapDataSize), Header.FileMapEntries, Header.FileRemap.MapSegmentCount); FileRemapSource = new SharedStreamSource(FileRemap); @@ -220,6 +224,25 @@ namespace LibHac.Savefile return entries; } + + public bool SignHeader(Keyset keyset) + { + if (keyset.SaveMacKey.IsEmpty()) return false; + + var data = new byte[0x200]; + var cmac = new byte[0x10]; + + var headerStream = SavefileSource.CreateStream(); + headerStream.Position = 0x100; + headerStream.Read(data, 0, 0x200); + + Crypto.CalculateAesCmac(keyset.SaveMacKey, data, 0, cmac, 0, 0x200); + + headerStream.Position = 0; + headerStream.Write(cmac, 0, 0x10); + + return true; + } } public static class SavefileExtensions diff --git a/README.md b/README.md index 5b53ba5f..a267035f 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,7 @@ Switch FS options: Savefile options: --outdir