1
0
Fork 0
mirror of https://github.com/Scandal-UK/Incognito_RCM.git synced 2024-11-22 20:06:42 +00:00

read prodinfo partition and build hash blockwise

This commit is contained in:
James Tophoven 2019-09-24 15:15:18 +02:00
parent 0cca558fe5
commit 5c0fdfd4b6

View file

@ -600,7 +600,7 @@ static inline u32 _read_le_u32(const void *buffer, u32 offset) {
// ctr[0x10-i-1] = (u8)(ofs & 0xff);
// }
bool readData(u8* buffer, u32 offset, u32 length)
bool readData(u8 *buffer, u32 offset, u32 length)
{
u32 sector = (offset / NX_EMMC_BLOCKSIZE);
@ -612,13 +612,11 @@ bool readData(u8* buffer, u32 offset, u32 length)
// EPRINTF("TOO BIG!!");
// }
//bool needMultipleSectors = newOffset + length > NX_EMMC_BLOCKSIZE;
u8 *tmp = (u8 *)malloc(sectorCount * NX_EMMC_BLOCKSIZE);
disk_read_mod(tmp, sector, sectorCount, &storage, prodinfo_part);
// if (!needMultipleSectors)
// {
// gfx_hexdump(0, tmp + newOffset, length);
@ -632,44 +630,42 @@ bool readData(u8* buffer, u32 offset, u32 length)
// memcpy(buffer + newLength, tmp, length - newLength);
// }
free(tmp);
return true;
}
bool verifyHash(u64 hashOffset, u64 offset, u64 sz)
{
bool verifyHash(u32 hashOffset, u32 offset, u32 sz)
{
bool result = false;
u8* buffer = (u8 *)malloc(sz);//new u8[sz];
if (!readData(buffer, offset, sz))
{
EPRINTF("error: failed reading calibration data\n");
//printf("error: failed reading calibration data\n");
}
else
{
u8 hash1[0x20];
u8 hash2[0x20];
u8 *buffer = (u8 *)malloc(NX_EMMC_BLOCKSIZE);
SHA256_CTX ctx;
sha256_init(&ctx);
while (sz > NX_EMMC_BLOCKSIZE)
{
readData(buffer, offset, NX_EMMC_BLOCKSIZE);
sha256_update(&ctx, buffer, NX_EMMC_BLOCKSIZE);
sz -= NX_EMMC_BLOCKSIZE;
offset += NX_EMMC_BLOCKSIZE;
}
readData(buffer, offset, sz);
sha256_update(&ctx, buffer, sz);
u8 *hash1 = (u8 *)malloc(0x20);
sha256_final(&ctx, hash1);
u8 *hash2 = (u8 *)malloc(0x20);
//se_calc_sha256(hash1, buffer, sz);
//sha256CalculateHash(hash1, buffer, sz);
if (!readData(hash2, hashOffset, sizeof(hash2)))
{
EPRINTF("error: failed reading hash\n");
//printf("error: failed reading hash\n");
}
else
{
if (memcmp(hash1, hash2, sizeof(hash1)))
readData(hash2, hashOffset, 0x20);
if (memcmp(hash1, hash2, 0x20))
{
EPRINTF("error: hash verification failed\n");
//printf("error: hash verification failed for %x %d\n", (long)offset, (long)sz);
@ -681,46 +677,42 @@ bool readData(u8* buffer, u32 offset, u32 length)
{
result = true;
}
}
gfx_hexdump(0, hash1, 0x08);
gfx_hexdump(0, hash2, 0x08);
}
free(buffer);
free(hash1);
free(hash2);
return result;
}
}
u32 certSize()
{
u32 certSize()
{
u32 buffer;
readData((u8 *)&buffer, 0x0AD0, sizeof(buffer));
EPRINTF("certSize");
gfx_hexdump(0, (u8 *)&buffer, sizeof(buffer));
buffer = _read_le_u32(&buffer, 0);
gfx_hexdump(0, (u8 *)&buffer, sizeof(buffer));
return buffer;
}
}
u32 calibrationDataSize()
{
u32 calibrationDataSize()
{
u32 buffer;
readData((u8 *)&buffer, 0x08, sizeof(buffer));
EPRINTF("calSize");
gfx_hexdump(0, (u8 *)&buffer, sizeof(buffer));
buffer = _read_le_u32(&buffer, 0);
gfx_hexdump(0, (u8 *)&buffer, sizeof(buffer));
return buffer;
}
}
bool verify()
{
bool verify()
{
bool r = verifyHash(0x12E0, 0x0AE0, certSize()); // client cert hash
r &= verifyHash(0x20, 0x0040, calibrationDataSize()); // calibration hash
return r;
}
// u32 calibrationDataSize()
// {
// return read<u32>(0x08);
// }
}