diff --git a/libhac/Aes128CounterMode.cs b/libhac/Aes128CounterMode.cs new file mode 100644 index 00000000..4f52da2a --- /dev/null +++ b/libhac/Aes128CounterMode.cs @@ -0,0 +1,153 @@ +// The MIT License (MIT) + +// Copyright (c) 2014 Hans Wolff + +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: + +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +using System; +using System.Collections.Generic; +using System.Security.Cryptography; + +namespace libhac +{ + public class Aes128CounterMode : SymmetricAlgorithm + { + private readonly byte[] _counter = new byte[0x10]; + private readonly AesManaged _aes; + + public Aes128CounterMode() + { + _aes = new AesManaged + { + Mode = CipherMode.ECB, + Padding = PaddingMode.None + }; + } + + public override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[] ignoredParameter) + { + return new CounterModeCryptoTransform(_aes, rgbKey, _counter); + } + + public override ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[] ignoredParameter) + { + return new CounterModeCryptoTransform(_aes, rgbKey, _counter); + } + + public override void GenerateKey() + { + _aes.GenerateKey(); + } + + public override void GenerateIV() + { + // IV not needed in Counter Mode + } + } + + public class CounterModeCryptoTransform : ICryptoTransform + { + private readonly byte[] _counter; + private readonly ICryptoTransform _counterEncryptor; + private readonly Queue _xorMask = new Queue(); + private readonly SymmetricAlgorithm _symmetricAlgorithm; + + public CounterModeCryptoTransform(SymmetricAlgorithm symmetricAlgorithm, byte[] key, byte[] counter) + { + if (symmetricAlgorithm == null) throw new ArgumentNullException(nameof(symmetricAlgorithm)); + if (key == null) throw new ArgumentNullException(nameof(key)); + if (counter == null) throw new ArgumentNullException(nameof(counter)); + if (counter.Length != symmetricAlgorithm.BlockSize / 8) + throw new ArgumentException(String.Format("Counter size must be same as block size (actual: {0}, expected: {1})", + counter.Length, symmetricAlgorithm.BlockSize / 8)); + + _symmetricAlgorithm = symmetricAlgorithm; + _counter = counter; + + var zeroIv = new byte[_symmetricAlgorithm.BlockSize / 8]; + _counterEncryptor = symmetricAlgorithm.CreateEncryptor(key, zeroIv); + } + + public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount) + { + var output = new byte[inputCount]; + TransformBlock(inputBuffer, inputOffset, inputCount, output, 0); + return output; + } + + public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset) + { + for (var i = 0; i < inputCount; i++) + { + if (NeedMoreXorMaskBytes()) EncryptCounterThenIncrement(); + + var mask = _xorMask.Dequeue(); + outputBuffer[outputOffset + i] = (byte)(inputBuffer[inputOffset + i] ^ mask); + } + + return inputCount; + } + + public void UpdateCounter(long offset) + { + offset >>= 4; + for (uint j = 0; j < 0x8; j++) + { + _counter[0x10 - j - 1] = (byte)(offset & 0xFF); + offset >>= 8; + } + } + + private bool NeedMoreXorMaskBytes() + { + return _xorMask.Count == 0; + } + + private void EncryptCounterThenIncrement() + { + var counterModeBlock = new byte[_symmetricAlgorithm.BlockSize / 8]; + + _counterEncryptor.TransformBlock(_counter, 0, _counter.Length, counterModeBlock, 0); + IncrementCounter(); + + foreach (var b in counterModeBlock) + { + _xorMask.Enqueue(b); + } + } + + private void IncrementCounter() + { + for (var i = _counter.Length - 1; i >= 0; i--) + { + if (++_counter[i] != 0) + break; + } + } + + public int InputBlockSize => _symmetricAlgorithm.BlockSize / 8; + public int OutputBlockSize => _symmetricAlgorithm.BlockSize / 8; + public bool CanTransformMultipleBlocks => true; + public bool CanReuseTransform => false; + + public void Dispose() + { + } + } +} \ No newline at end of file diff --git a/libhac/AesCtrStream.cs b/libhac/AesCtrStream.cs new file mode 100644 index 00000000..d4fb33ae --- /dev/null +++ b/libhac/AesCtrStream.cs @@ -0,0 +1,167 @@ +// Copyright (c) 2010 Gareth Lennox (garethl@dwakn.com) +// All rights reserved. + +// Redistribution and use in source and binary forms, with or without modification, +// are permitted provided that the following conditions are met: + +// * Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// * Neither the name of Gareth Lennox nor the names of its +// contributors may be used to endorse or promote products derived from this +// software without specific prior written permission. + +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE +// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +using System.IO; +using System.Security.Cryptography; +using libhac.XTSSharp; + +namespace libhac +{ + /// + /// Xts sector-based + /// + public class AesCtrStream : SectorStream + { + /// + /// The default sector size + /// + public const int DefaultSectorSize = 16; + + private readonly long _counterOffset; + private readonly byte[] _tempBuffer; + private readonly Aes _aes; + private CounterModeCryptoTransform _decryptor; + + /// + /// Creates a new stream + /// + /// The base stream + /// The decryption key + /// Offset to add to the counter + public AesCtrStream(Stream baseStream, byte[] key, long counterOffset = 0) + : this(baseStream, key, 0, baseStream.Length, counterOffset) { } + + /// + /// Creates a new stream + /// + /// The base stream + /// The decryption key + /// Offset to start at in the input stream + /// The length of the created stream + /// Offset to add to the counter + public AesCtrStream(Stream baseStream, byte[] key, long offset, long length, long counterOffset) + : base(baseStream, 0x10, offset) + { + _counterOffset = counterOffset; + Length = length; + _tempBuffer = new byte[0x10]; + + _aes = Aes.Create(); + if (_aes == null) throw new CryptographicException("Unable to create AES object"); + _aes.Key = key; + _aes.Mode = CipherMode.ECB; + _aes.Padding = PaddingMode.None; + _decryptor = CreateDecryptor(); + + } + + private CounterModeCryptoTransform CreateDecryptor() + { + var dec = new CounterModeCryptoTransform(_aes, _aes.Key, new byte[0x10]); + dec.UpdateCounter(_counterOffset + Position); + return dec; + } + + /// + /// Releases the unmanaged resources used by the and optionally releases the managed resources. + /// + /// true to release both managed and unmanaged resources; false to release only unmanaged resources. + protected override void Dispose(bool disposing) + { + base.Dispose(disposing); + _decryptor?.Dispose(); + } + + public override void Flush() + { + throw new System.NotImplementedException(); + } + + public override long Seek(long offset, SeekOrigin origin) + { + throw new System.NotImplementedException(); + } + + public override void SetLength(long value) + { + throw new System.NotImplementedException(); + } + + /// + /// Writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written. + /// + /// An array of bytes. This method copies bytes from to the current stream. + /// The zero-based byte offset in at which to begin copying bytes to the current stream. + /// The number of bytes to be written to the current stream. + public override void Write(byte[] buffer, int offset, int count) + { + throw new System.NotImplementedException(); + } + + public override bool CanRead => true; + public override bool CanSeek => false; + public override bool CanWrite => false; + public override long Length { get; } + + public override long Position + { + get => base.Position; + set + { + base.Position = value; + _decryptor.UpdateCounter(_counterOffset + base.Position); + } + } + + /// + /// Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read. + /// + /// The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached. + /// An array of bytes. When this method returns, the buffer contains the specified byte array with the values between and ( + - 1) replaced by the bytes read from the current source. + /// The zero-based byte offset in at which to begin storing the data read from the current stream. + /// The maximum number of bytes to be read from the current stream. + public override int Read(byte[] buffer, int offset, int count) + { + ValidateSize(count); + + //read the sector from the base stream + var ret = base.Read(_tempBuffer, 0, count); + + if (ret == 0) + return 0; + + if (_decryptor == null) + _decryptor = CreateDecryptor(); + + //decrypt the sector + var retV = _decryptor.TransformBlock(_tempBuffer, 0, ret, buffer, offset); + + //Console.WriteLine("Decrypting sector {0} == {1} bytes", currentSector, retV); + + return retV; + } + } +} \ No newline at end of file diff --git a/libhac/Nca.cs b/libhac/Nca.cs index b365038d..185b0166 100644 --- a/libhac/Nca.cs +++ b/libhac/Nca.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.IO; using libhac.XTSSharp; @@ -11,9 +12,13 @@ namespace libhac public bool HasRightsId { get; private set; } public int CryptoType { get; private set; } public byte[][] DecryptedKeys { get; } = Util.CreateJaggedArray(4, 0x10); + public Stream Stream { get; private set; } + + public List Sections = new List(); public Nca(Keyset keyset, Stream stream) { + Stream = stream; ReadHeader(keyset, stream); CryptoType = Math.Max(Header.CryptoType, Header.CryptoType2); @@ -25,6 +30,35 @@ namespace libhac { DecryptKeyArea(keyset); } + + for (int i = 0; i < 4; i++) + { + var section = ParseSection(keyset, stream, i); + if (section != null) Sections.Add(section); + } + } + + public Stream OpenSection(int index) + { + if (index >= Sections.Count) throw new ArgumentOutOfRangeException(nameof(index)); + var sect = Sections[index]; + Stream.Position = sect.Offset; + + switch (sect.Header.CryptType) + { + case SectionCryptType.None: + break; + case SectionCryptType.XTS: + break; + case SectionCryptType.CTR: + return new RandomAccessSectorStream(new AesCtrStream(Stream, DecryptedKeys[2], sect.Offset, sect.Size, sect.Offset)); + case SectionCryptType.BKTR: + break; + default: + throw new ArgumentOutOfRangeException(); + } + + return null; } private void ReadHeader(Keyset keyset, Stream stream) @@ -47,279 +81,39 @@ namespace libhac DecryptedKeys[i], 0x10); } } - } - public class NcaHeader - { - public byte[] Signature1; // RSA-PSS signature over header with fixed key. - public byte[] Signature2; // RSA-PSS signature over header with key in NPDM. - public string Magic; - public byte Distribution; // System vs gamecard. - public ContentType ContentType; - public byte CryptoType; // Which keyblob (field 1) - public byte KaekInd; // Which kaek index? - public ulong NcaSize; // Entire archive size. - public ulong TitleId; - public uint SdkVersion; // What SDK was this built with? - public byte CryptoType2; // Which keyblob (field 2) - public byte[] RightsId; - public string Name; - - public NcaSectionEntry[] SectionEntries = new NcaSectionEntry[4]; - public byte[][] SectionHashes = new byte[4][]; - public byte[][] EncryptedKeys = new byte[4][]; - - public NcaFsHeader[] FsHeaders = new NcaFsHeader[4]; - - public static NcaHeader Read(BinaryReader reader) + private NcaSection ParseSection(Keyset keyset, Stream stream, int index) { - var head = new NcaHeader(); + var entry = Header.SectionEntries[index]; + var header = Header.FsHeaders[index]; + if (entry.MediaStartOffset == 0) return null; - head.Signature1 = reader.ReadBytes(0x100); - head.Signature2 = reader.ReadBytes(0x100); - head.Magic = reader.ReadAscii(4); - head.Distribution = reader.ReadByte(); - head.ContentType = (ContentType)reader.ReadByte(); - head.CryptoType = reader.ReadByte(); - head.KaekInd = reader.ReadByte(); - head.NcaSize = reader.ReadUInt64(); - head.TitleId = reader.ReadUInt64(); - reader.BaseStream.Position += 4; + var sect = new NcaSection(); - head.SdkVersion = reader.ReadUInt32(); - head.CryptoType2 = reader.ReadByte(); - reader.BaseStream.Position += 0xF; + sect.SectionNum = index; + sect.Offset = Util.MediaToReal(entry.MediaStartOffset); + sect.Size = Util.MediaToReal(entry.MediaEndOffset) - sect.Offset; + sect.Header = header; + sect.Type = header.Type; - head.RightsId = reader.ReadBytes(0x10); - - for (int i = 0; i < 4; i++) + if (sect.Type == SectionType.Pfs0) { - head.SectionEntries[i] = new NcaSectionEntry(reader); + sect.Pfs0 = new Pfs0 { Superblock = header.Pfs0 }; } - for (int i = 0; i < 4; i++) - { - head.SectionHashes[i] = reader.ReadBytes(0x20); - } - - for (int i = 0; i < 4; i++) - { - head.EncryptedKeys[i] = reader.ReadBytes(0x10); - } - - reader.BaseStream.Position += 0xC0; - - for (int i = 0; i < 4; i++) - { - head.FsHeaders[i] = new NcaFsHeader(reader); - } - return head; + return sect; } } - public class NcaSectionEntry + public class NcaSection { - public uint MediaStartOffset; - public uint MediaEndOffset; + public Stream Stream; + public NcaFsHeader Header { get; set; } + public SectionType Type { get; set; } + public int SectionNum { get; set; } + public long Offset { get; set; } + public long Size { get; set; } - public NcaSectionEntry(BinaryReader reader) - { - MediaStartOffset = reader.ReadUInt32(); - MediaEndOffset = reader.ReadUInt32(); - reader.BaseStream.Position += 8; - } - } - - public class NcaFsHeader - { - public byte Field0; - public byte Field1; - public SectionPartitionType PartitionType; - public SectionFsType FsType; - public SectionCryptType CryptType; - public SectionType Type; - - public Pfs0Superblock Pfs0; - public RomfsSuperblock Romfs; - public BktrSuperblock Bktr; - - public NcaFsHeader(BinaryReader reader) - { - Field0 = reader.ReadByte(); - Field1 = reader.ReadByte(); - PartitionType = (SectionPartitionType)reader.ReadByte(); - FsType = (SectionFsType)reader.ReadByte(); - CryptType = (SectionCryptType)reader.ReadByte(); - reader.BaseStream.Position += 3; - - if (PartitionType == SectionPartitionType.Pfs0 && FsType == SectionFsType.Pfs0) - { - Type = SectionType.Pfs0; - Pfs0 = new Pfs0Superblock(reader); - } - else if (PartitionType == SectionPartitionType.Romfs && FsType == SectionFsType.Romfs) - { - if (CryptType == SectionCryptType.BKTR) - { - Type = SectionType.Bktr; - Bktr = new BktrSuperblock(reader); - } - else - { - Type = SectionType.Romfs; - Romfs = new RomfsSuperblock(reader); - } - } - } - } - - public class Pfs0Superblock - { - public byte[] MasterHash; /* SHA-256 hash of the hash table. */ - public uint BlockSize; /* In bytes. */ - public uint Always2; - public ulong HashTableOffset; /* Normally zero. */ - public ulong HashTableSize; - public ulong Pfs0Offset; - public ulong Pfs0Size; - - public Pfs0Superblock(BinaryReader reader) - { - MasterHash = reader.ReadBytes(0x20); - BlockSize = reader.ReadUInt32(); - Always2 = reader.ReadUInt32(); - HashTableOffset = reader.ReadUInt64(); - HashTableSize = reader.ReadUInt64(); - Pfs0Offset = reader.ReadUInt64(); - Pfs0Size = reader.ReadUInt64(); - reader.BaseStream.Position += 0xF0; - } - } - - public class RomfsSuperblock - { - public IvfcHeader IvfcHeader; - - public RomfsSuperblock(BinaryReader reader) - { - IvfcHeader = new IvfcHeader(reader); - reader.BaseStream.Position += 0x58; - } - } - - public class BktrSuperblock - { - public IvfcHeader IvfcHeader; - public BktrHeader RelocationHeader; - public BktrHeader SubsectionHeader; - - public BktrSuperblock(BinaryReader reader) - { - IvfcHeader = new IvfcHeader(reader); - reader.BaseStream.Position += 0x18; - RelocationHeader = new BktrHeader(reader); - SubsectionHeader = new BktrHeader(reader); - } - } - - public class IvfcHeader - { - public string Magic; - public uint Id; - public uint MasterHashSize; - public uint NumLevels; - public IvfcLevelHeader[] LevelHeaders = new IvfcLevelHeader[6]; - public byte[] MasterHash; - - - public IvfcHeader(BinaryReader reader) - { - Magic = reader.ReadAscii(4); - Id = reader.ReadUInt32(); - MasterHashSize = reader.ReadUInt32(); - NumLevels = reader.ReadUInt32(); - - for (int i = 0; i < LevelHeaders.Length; i++) - { - LevelHeaders[i] = new IvfcLevelHeader(reader); - } - - reader.BaseStream.Position += 0x20; - MasterHash = reader.ReadBytes(0x20); - } - } - - public class IvfcLevelHeader - { - public ulong LogicalOffset; - public ulong HashDataSize; - public uint BlockSize; - public uint Reserved; - - public IvfcLevelHeader(BinaryReader reader) - { - LogicalOffset = reader.ReadUInt64(); - HashDataSize = reader.ReadUInt64(); - BlockSize = reader.ReadUInt32(); - Reserved = reader.ReadUInt32(); - } - } - - public class BktrHeader - { - public ulong Offset; - public ulong Size; - public uint Magic; - public uint Field14; - public uint NumEntries; - public uint Field1C; - - public BktrHeader(BinaryReader reader) - { - Offset = reader.ReadUInt64(); - Size = reader.ReadUInt64(); - Magic = reader.ReadUInt32(); - Field14 = reader.ReadUInt32(); - NumEntries = reader.ReadUInt32(); - Field1C = reader.ReadUInt32(); - } - } - - public enum ContentType - { - Program, - Meta, - Control, - Manual, - Data, - Unknown - } - - public enum SectionCryptType - { - None = 1, - XTS, - CTR, - BKTR - } - - public enum SectionFsType - { - Pfs0 = 2, - Romfs - } - - public enum SectionPartitionType - { - Romfs, - Pfs0 - } - - public enum SectionType - { - Invalid, - Pfs0, - Romfs, - Bktr + public Pfs0 Pfs0 { get; set; } } } diff --git a/libhac/NcaStructs.cs b/libhac/NcaStructs.cs new file mode 100644 index 00000000..7b7b1042 --- /dev/null +++ b/libhac/NcaStructs.cs @@ -0,0 +1,255 @@ +using System.IO; + +namespace libhac +{ + public class NcaHeader + { + public byte[] Signature1; // RSA-PSS signature over header with fixed key. + public byte[] Signature2; // RSA-PSS signature over header with key in NPDM. + public string Magic; + public byte Distribution; // System vs gamecard. + public ContentType ContentType; + public byte CryptoType; // Which keyblob (field 1) + public byte KaekInd; // Which kaek index? + public ulong NcaSize; // Entire archive size. + public ulong TitleId; + public uint SdkVersion; // What SDK was this built with? + public byte CryptoType2; // Which keyblob (field 2) + public byte[] RightsId; + public string Name; + + public NcaSectionEntry[] SectionEntries = new NcaSectionEntry[4]; + public byte[][] SectionHashes = new byte[4][]; + public byte[][] EncryptedKeys = new byte[4][]; + + public NcaFsHeader[] FsHeaders = new NcaFsHeader[4]; + + public static NcaHeader Read(BinaryReader reader) + { + var head = new NcaHeader(); + + head.Signature1 = reader.ReadBytes(0x100); + head.Signature2 = reader.ReadBytes(0x100); + head.Magic = reader.ReadAscii(4); + head.Distribution = reader.ReadByte(); + head.ContentType = (ContentType)reader.ReadByte(); + head.CryptoType = reader.ReadByte(); + head.KaekInd = reader.ReadByte(); + head.NcaSize = reader.ReadUInt64(); + head.TitleId = reader.ReadUInt64(); + reader.BaseStream.Position += 4; + + head.SdkVersion = reader.ReadUInt32(); + head.CryptoType2 = reader.ReadByte(); + reader.BaseStream.Position += 0xF; + + head.RightsId = reader.ReadBytes(0x10); + + for (int i = 0; i < 4; i++) + { + head.SectionEntries[i] = new NcaSectionEntry(reader); + } + + for (int i = 0; i < 4; i++) + { + head.SectionHashes[i] = reader.ReadBytes(0x20); + } + + for (int i = 0; i < 4; i++) + { + head.EncryptedKeys[i] = reader.ReadBytes(0x10); + } + + reader.BaseStream.Position += 0xC0; + + for (int i = 0; i < 4; i++) + { + head.FsHeaders[i] = new NcaFsHeader(reader); + } + return head; + } + } + + public class NcaSectionEntry + { + public uint MediaStartOffset; + public uint MediaEndOffset; + + public NcaSectionEntry(BinaryReader reader) + { + MediaStartOffset = reader.ReadUInt32(); + MediaEndOffset = reader.ReadUInt32(); + reader.BaseStream.Position += 8; + } + } + + public class NcaFsHeader + { + public byte Field0; + public byte Field1; + public SectionPartitionType PartitionType; + public SectionFsType FsType; + public SectionCryptType CryptType; + public SectionType Type; + + public Pfs0Superblock Pfs0; + public RomfsSuperblock Romfs; + public BktrSuperblock Bktr; + + public NcaFsHeader(BinaryReader reader) + { + Field0 = reader.ReadByte(); + Field1 = reader.ReadByte(); + PartitionType = (SectionPartitionType)reader.ReadByte(); + FsType = (SectionFsType)reader.ReadByte(); + CryptType = (SectionCryptType)reader.ReadByte(); + reader.BaseStream.Position += 3; + + if (PartitionType == SectionPartitionType.Pfs0 && FsType == SectionFsType.Pfs0) + { + Type = SectionType.Pfs0; + Pfs0 = new Pfs0Superblock(reader); + } + else if (PartitionType == SectionPartitionType.Romfs && FsType == SectionFsType.Romfs) + { + if (CryptType == SectionCryptType.BKTR) + { + Type = SectionType.Bktr; + Bktr = new BktrSuperblock(reader); + } + else + { + Type = SectionType.Romfs; + Romfs = new RomfsSuperblock(reader); + } + } + } + } + + public class RomfsSuperblock + { + public IvfcHeader IvfcHeader; + + public RomfsSuperblock(BinaryReader reader) + { + IvfcHeader = new IvfcHeader(reader); + reader.BaseStream.Position += 0x58; + } + } + + public class BktrSuperblock + { + public IvfcHeader IvfcHeader; + public BktrHeader RelocationHeader; + public BktrHeader SubsectionHeader; + + public BktrSuperblock(BinaryReader reader) + { + IvfcHeader = new IvfcHeader(reader); + reader.BaseStream.Position += 0x18; + RelocationHeader = new BktrHeader(reader); + SubsectionHeader = new BktrHeader(reader); + } + } + + public class IvfcHeader + { + public string Magic; + public uint Id; + public uint MasterHashSize; + public uint NumLevels; + public IvfcLevelHeader[] LevelHeaders = new IvfcLevelHeader[6]; + public byte[] MasterHash; + + + public IvfcHeader(BinaryReader reader) + { + Magic = reader.ReadAscii(4); + Id = reader.ReadUInt32(); + MasterHashSize = reader.ReadUInt32(); + NumLevels = reader.ReadUInt32(); + + for (int i = 0; i < LevelHeaders.Length; i++) + { + LevelHeaders[i] = new IvfcLevelHeader(reader); + } + + reader.BaseStream.Position += 0x20; + MasterHash = reader.ReadBytes(0x20); + } + } + + public class IvfcLevelHeader + { + public ulong LogicalOffset; + public ulong HashDataSize; + public uint BlockSize; + public uint Reserved; + + public IvfcLevelHeader(BinaryReader reader) + { + LogicalOffset = reader.ReadUInt64(); + HashDataSize = reader.ReadUInt64(); + BlockSize = reader.ReadUInt32(); + Reserved = reader.ReadUInt32(); + } + } + + public class BktrHeader + { + public ulong Offset; + public ulong Size; + public uint Magic; + public uint Field14; + public uint NumEntries; + public uint Field1C; + + public BktrHeader(BinaryReader reader) + { + Offset = reader.ReadUInt64(); + Size = reader.ReadUInt64(); + Magic = reader.ReadUInt32(); + Field14 = reader.ReadUInt32(); + NumEntries = reader.ReadUInt32(); + Field1C = reader.ReadUInt32(); + } + } + + public enum ContentType + { + Program, + Meta, + Control, + Manual, + Data, + Unknown + } + + public enum SectionCryptType + { + None = 1, + XTS, + CTR, + BKTR + } + + public enum SectionFsType + { + Pfs0 = 2, + Romfs + } + + public enum SectionPartitionType + { + Romfs, + Pfs0 + } + + public enum SectionType + { + Invalid, + Pfs0, + Romfs, + Bktr + } +} diff --git a/libhac/Pfs0.cs b/libhac/Pfs0.cs new file mode 100644 index 00000000..2b4f7b75 --- /dev/null +++ b/libhac/Pfs0.cs @@ -0,0 +1,32 @@ +using System.IO; + +namespace libhac +{ + public class Pfs0 + { + public Pfs0Superblock Superblock { get; set; } + } + + public class Pfs0Superblock + { + public byte[] MasterHash; /* SHA-256 hash of the hash table. */ + public uint BlockSize; /* In bytes. */ + public uint Always2; + public ulong HashTableOffset; /* Normally zero. */ + public ulong HashTableSize; + public ulong Pfs0Offset; + public ulong Pfs0Size; + + public Pfs0Superblock(BinaryReader reader) + { + MasterHash = reader.ReadBytes(0x20); + BlockSize = reader.ReadUInt32(); + Always2 = reader.ReadUInt32(); + HashTableOffset = reader.ReadUInt64(); + HashTableSize = reader.ReadUInt64(); + Pfs0Offset = reader.ReadUInt64(); + Pfs0Size = reader.ReadUInt64(); + reader.BaseStream.Position += 0xF0; + } + } +} diff --git a/libhac/SdFs.cs b/libhac/SdFs.cs index f2110dda..b318d8a5 100644 --- a/libhac/SdFs.cs +++ b/libhac/SdFs.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.IO; using System.Linq; @@ -27,11 +28,20 @@ namespace libhac { foreach (var file in Files) { - var sdPath = "/" + Util.GetRelativePath(file, ContentsDir).Replace('\\', '/'); - var nax0 = new Nax0(Keyset, file, sdPath); - var nca = new Nca(Keyset, nax0.Stream); - nca.Name = Path.GetFileName(file); - yield return nca; + Nca nca = null; + try + { + var sdPath = "/" + Util.GetRelativePath(file, ContentsDir).Replace('\\', '/'); + var nax0 = new Nax0(Keyset, file, sdPath); + nca = new Nca(Keyset, nax0.Stream); + nca.Name = Path.GetFileName(file); + } + catch (Exception ex) + { + Console.WriteLine($"{ex.Message} {file}"); + } + + if (nca != null) yield return nca; } } diff --git a/libhac/Util.cs b/libhac/Util.cs index 69cf2f35..25586971 100644 --- a/libhac/Util.cs +++ b/libhac/Util.cs @@ -162,7 +162,7 @@ namespace libhac return result; } - internal static ulong MediaToReal(ulong media) + internal static long MediaToReal(long media) { return MediaSize * media; } diff --git a/libhac/libhac.csproj b/libhac/libhac.csproj index 7a9f5707..edcf3908 100644 --- a/libhac/libhac.csproj +++ b/libhac/libhac.csproj @@ -2,7 +2,7 @@ Library - netcoreapp2.1 + netcoreapp2.1;net45 7.3