changes the CheckMemoryHashTable to Validity

This commit is contained in:
jonnysp 2018-09-29 07:50:11 +02:00
parent 13ea564aec
commit fcb44f8a2f
4 changed files with 6 additions and 30 deletions

View file

@ -12,12 +12,12 @@ namespace LibHac
internal const int Aes128Size = 0x10; internal const int Aes128Size = 0x10;
internal const int Sha256DigestSize = 0x20; internal const int Sha256DigestSize = 0x20;
public static bool CheckMemoryHashTable(byte[] data, byte[] hash) public static Validity CheckMemoryHashTable(byte[] data, byte[] hash, int offset , int count)
{ {
bool comp = false; Validity comp = Validity.Invalid ;
using (var _SHA = SHA256.Create()) using (var _SHA = SHA256.Create())
{ {
comp = hash.SequenceEqual(_SHA.ComputeHash(data)); comp = hash.SequenceEqual(_SHA.ComputeHash(data , offset , count)) ? Validity.Valid : Validity.Invalid;
} }
return comp; return comp;
} }

View file

@ -141,14 +141,7 @@ namespace LibHac
for (int i = 0; i < NumFiles; i++) for (int i = 0; i < NumFiles; i++)
{ {
reader.BaseStream.Position = HeaderSize + Files[i].Offset; reader.BaseStream.Position = HeaderSize + Files[i].Offset;
if (Crypto.CheckMemoryHashTable(reader.ReadBytes((int)Files[i].HashedRegionSize), Files[i].Hash)) Files[i].HashValidity = Crypto.CheckMemoryHashTable(reader.ReadBytes(Files[i].HashedRegionSize), Files[i].Hash, 0, Files[i].HashedRegionSize);
{
Files[i].HashValidity = Validity.Valid;
}
else
{
Files[i].HashValidity = Validity.Invalid;
}
} }
} }

View file

@ -1,5 +1,4 @@
using System.IO; using System.IO;
using System.Security.Cryptography;
using System.Linq; using System.Linq;
namespace LibHac.Savefile namespace LibHac.Savefile
@ -81,21 +80,12 @@ namespace LibHac.Savefile
MetaMapEntries[i] = new MapEntry(reader); MetaMapEntries[i] = new MapEntry(reader);
} }
HeaderHashValidity = ValidateHeaderHash(); HeaderHashValidity = Crypto.CheckMemoryHashTable(Data, Layout.Hash, 0x300, 0x3d00);
SignatureValidity = ValidateSignature(keyset); SignatureValidity = ValidateSignature(keyset);
logger?.LogMessage($"Header hash is {HeaderHashValidity}"); logger?.LogMessage($"Header hash is {HeaderHashValidity}");
} }
private Validity ValidateHeaderHash()
{
using (SHA256 sha256 = SHA256.Create())
{
var hash = sha256.ComputeHash(Data, 0x300, 0x3d00);
return hash.SequenceEqual(Layout.Hash) ? Validity.Valid : Validity.Invalid;
}
}
private Validity ValidateSignature(Keyset keyset) private Validity ValidateSignature(Keyset keyset)
{ {
var calculatedCmac = new byte[0x10]; var calculatedCmac = new byte[0x10];

View file

@ -134,14 +134,7 @@ namespace LibHac
} }
reader.BaseStream.Position = PartitionFsHeaderAddress; reader.BaseStream.Position = PartitionFsHeaderAddress;
PartitionFsHeaderValidity = Crypto.CheckMemoryHashTable(reader.ReadBytes((int)PartitionFsHeaderSize), PartitionFsHeaderHash, 0, (int)PartitionFsHeaderSize);
if (Crypto.CheckMemoryHashTable(reader.ReadBytes((int)PartitionFsHeaderSize), PartitionFsHeaderHash)) {
PartitionFsHeaderValidity = Validity.Valid;
}
else
{
PartitionFsHeaderValidity = Validity.Invalid;
}
} }