mirror of
https://github.com/Thealexbarney/LibHac.git
synced 2024-11-14 10:49:41 +01:00
Decrypt AES-CTR sections
This commit is contained in:
parent
e3d8e60b0e
commit
b632a7df0c
8 changed files with 680 additions and 269 deletions
153
libhac/Aes128CounterMode.cs
Normal file
153
libhac/Aes128CounterMode.cs
Normal file
|
@ -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<byte> _xorMask = new Queue<byte>();
|
||||
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()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
167
libhac/AesCtrStream.cs
Normal file
167
libhac/AesCtrStream.cs
Normal file
|
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// Xts sector-based
|
||||
/// </summary>
|
||||
public class AesCtrStream : SectorStream
|
||||
{
|
||||
/// <summary>
|
||||
/// The default sector size
|
||||
/// </summary>
|
||||
public const int DefaultSectorSize = 16;
|
||||
|
||||
private readonly long _counterOffset;
|
||||
private readonly byte[] _tempBuffer;
|
||||
private readonly Aes _aes;
|
||||
private CounterModeCryptoTransform _decryptor;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new stream
|
||||
/// </summary>
|
||||
/// <param name="baseStream">The base stream</param>
|
||||
/// <param name="key">The decryption key</param>
|
||||
/// <param name="counterOffset">Offset to add to the counter</param>
|
||||
public AesCtrStream(Stream baseStream, byte[] key, long counterOffset = 0)
|
||||
: this(baseStream, key, 0, baseStream.Length, counterOffset) { }
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new stream
|
||||
/// </summary>
|
||||
/// <param name="baseStream">The base stream</param>
|
||||
/// <param name="key">The decryption key</param>
|
||||
/// <param name="offset">Offset to start at in the input stream</param>
|
||||
/// <param name="length">The length of the created stream</param>
|
||||
/// <param name="counterOffset">Offset to add to the counter</param>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Releases the unmanaged resources used by the <see cref="T:System.IO.Stream"/> and optionally releases the managed resources.
|
||||
/// </summary>
|
||||
/// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
|
||||
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();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
|
||||
/// </summary>
|
||||
/// <param name="buffer">An array of bytes. This method copies <paramref name="count"/> bytes from <paramref name="buffer"/> to the current stream.</param>
|
||||
/// <param name="offset">The zero-based byte offset in <paramref name="buffer"/> at which to begin copying bytes to the current stream.</param>
|
||||
/// <param name="count">The number of bytes to be written to the current stream.</param>
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
|
||||
/// </summary>
|
||||
/// <returns>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.</returns>
|
||||
/// <param name="buffer">An array of bytes. When this method returns, the buffer contains the specified byte array with the values between <paramref name="offset"/> and (<paramref name="offset"/> + <paramref name="count"/> - 1) replaced by the bytes read from the current source. </param>
|
||||
/// <param name="offset">The zero-based byte offset in <paramref name="buffer"/> at which to begin storing the data read from the current stream.</param>
|
||||
/// <param name="count">The maximum number of bytes to be read from the current stream.</param>
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
322
libhac/Nca.cs
322
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<byte[][]>(4, 0x10);
|
||||
public Stream Stream { get; private set; }
|
||||
|
||||
public List<NcaSection> Sections = new List<NcaSection>();
|
||||
|
||||
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
|
||||
private NcaSection ParseSection(Keyset keyset, Stream stream, int index)
|
||||
{
|
||||
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;
|
||||
var entry = Header.SectionEntries[index];
|
||||
var header = Header.FsHeaders[index];
|
||||
if (entry.MediaStartOffset == 0) return null;
|
||||
|
||||
public NcaSectionEntry[] SectionEntries = new NcaSectionEntry[4];
|
||||
public byte[][] SectionHashes = new byte[4][];
|
||||
public byte[][] EncryptedKeys = new byte[4][];
|
||||
var sect = new NcaSection();
|
||||
|
||||
public NcaFsHeader[] FsHeaders = new NcaFsHeader[4];
|
||||
sect.SectionNum = index;
|
||||
sect.Offset = Util.MediaToReal(entry.MediaStartOffset);
|
||||
sect.Size = Util.MediaToReal(entry.MediaEndOffset) - sect.Offset;
|
||||
sect.Header = header;
|
||||
sect.Type = header.Type;
|
||||
|
||||
public static NcaHeader Read(BinaryReader reader)
|
||||
if (sect.Type == SectionType.Pfs0)
|
||||
{
|
||||
var head = new NcaHeader();
|
||||
sect.Pfs0 = new Pfs0 { Superblock = header.Pfs0 };
|
||||
}
|
||||
|
||||
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;
|
||||
return sect;
|
||||
}
|
||||
}
|
||||
|
||||
head.SdkVersion = reader.ReadUInt32();
|
||||
head.CryptoType2 = reader.ReadByte();
|
||||
reader.BaseStream.Position += 0xF;
|
||||
|
||||
head.RightsId = reader.ReadBytes(0x10);
|
||||
|
||||
for (int i = 0; i < 4; i++)
|
||||
public class NcaSection
|
||||
{
|
||||
head.SectionEntries[i] = new NcaSectionEntry(reader);
|
||||
}
|
||||
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; }
|
||||
|
||||
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 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; }
|
||||
}
|
||||
}
|
||||
|
|
255
libhac/NcaStructs.cs
Normal file
255
libhac/NcaStructs.cs
Normal file
|
@ -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
|
||||
}
|
||||
}
|
32
libhac/Pfs0.cs
Normal file
32
libhac/Pfs0.cs
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,4 +1,5 @@
|
|||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
|
@ -26,12 +27,21 @@ namespace libhac
|
|||
public IEnumerable<Nca> ReadAllNca()
|
||||
{
|
||||
foreach (var file in Files)
|
||||
{
|
||||
Nca nca = null;
|
||||
try
|
||||
{
|
||||
var sdPath = "/" + Util.GetRelativePath(file, ContentsDir).Replace('\\', '/');
|
||||
var nax0 = new Nax0(Keyset, file, sdPath);
|
||||
var nca = new Nca(Keyset, nax0.Stream);
|
||||
nca = new Nca(Keyset, nax0.Stream);
|
||||
nca.Name = Path.GetFileName(file);
|
||||
yield return nca;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"{ex.Message} {file}");
|
||||
}
|
||||
|
||||
if (nca != null) yield return nca;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -162,7 +162,7 @@ namespace libhac
|
|||
return result;
|
||||
}
|
||||
|
||||
internal static ulong MediaToReal(ulong media)
|
||||
internal static long MediaToReal(long media)
|
||||
{
|
||||
return MediaSize * media;
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
<PropertyGroup>
|
||||
<OutputType>Library</OutputType>
|
||||
<TargetFramework>netcoreapp2.1</TargetFramework>
|
||||
<TargetFrameworks>netcoreapp2.1;net45</TargetFrameworks>
|
||||
<LangVersion>7.3</LangVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
|
|
Loading…
Reference in a new issue