diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f604a396..1a4289d0 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -46,6 +46,13 @@ add_library(core STATIC Wifi.cpp WifiAP.cpp + fatfs/diskio.c + fatfs/ff.c + fatfs/ffsystem.c + fatfs/ffunicode.c + fatfs/ffconf.h + + sha1/sha1.c tiny-AES-c/aes.c xxhash/xxhash.c ) diff --git a/src/DSi.cpp b/src/DSi.cpp index 01dcd35e..21764a7e 100644 --- a/src/DSi.cpp +++ b/src/DSi.cpp @@ -39,6 +39,7 @@ #include "DSi_DSP.h" #include "DSi_Camera.h" +#include "sha1/sha1.hpp" #include "tiny-AES-c/aes.hpp" @@ -499,7 +500,7 @@ bool LoadNAND() if (memcmp(nand_footer, nand_footer_ref, 16)) { // There is another copy of the footer at 000FF800h for the case - // that by external tools the image was cut off + // that by external tools the image was cut off // See https://problemkaputt.de/gbatek.htm#dsisdmmcimages fseek(SDMMCFile, 0x000FF800, SEEK_SET); fread(nand_footer, 1, 16, SDMMCFile); @@ -529,6 +530,71 @@ bool LoadNAND() memcpy(&ARM7Init[0x0254], &ARM7iBIOS[0xC6D0], 0x1048); memcpy(&ARM7Init[0x129C], &ARM7iBIOS[0xD718], 0x1048); + // TEST ZONE + { + SHA1_CTX sha; + u8 cidhash[20]; + u8 iv[16]; + + SHA1Init(&sha); + SHA1Update(&sha, eMMC_CID, 16); + SHA1Final(cidhash, &sha); + + printf("ASS HASH: "); + for (int i = 0; i < 20; i++) printf("%02X", cidhash[i]); + printf("\n"); + + DSi_AES::Swap16(iv, cidhash); + + printf("ASS IV: "); + for (int i = 0; i < 16; i++) printf("%02X", iv[i]); + printf("\n"); + + u8 keyX[16]; + *(u32*)&keyX[0] = (u32)ConsoleID; + *(u32*)&keyX[4] = (u32)ConsoleID ^ 0x24EE6906; + *(u32*)&keyX[8] = (u32)(ConsoleID >> 32) ^ 0xE65B601D; + *(u32*)&keyX[12] = (u32)(ConsoleID >> 32); + + u8 keyY[16]; + *(u32*)&keyY[0] = 0x0AB9DC76; + *(u32*)&keyY[4] = 0xBD4DC4D3; + *(u32*)&keyY[8] = 0x202DDD1D; + *(u32*)&keyY[12] = 0xE1A00005; + + u8 shittykey[16]; + DSi_AES::DeriveNormalKey(keyX, keyY, shittykey); + + u8 normalkey[16]; + DSi_AES::Swap16(normalkey, shittykey); + + u8 dorp[0x200]; + fseek(SDMMCFile, 0, SEEK_SET); + fread(&dorp, 0x200, 1, SDMMCFile); + + AES_ctx ctx; + AES_init_ctx_iv(&ctx, normalkey, iv); + + //AES_CTR_xcrypt_buffer(&ctx, dorp, 0x200); + for (int i = 0; i < 0x200; i+=16) + { + u8 tmp[16]; + DSi_AES::Swap16(tmp, &dorp[i]); + AES_CTR_xcrypt_buffer(&ctx, tmp, 16); + DSi_AES::Swap16(&dorp[i], tmp); + } + + //printf("%08X %08X %08X %08X\n", *(u32*)&dorp[0], *(u32*)&dorp[4], *(u32*)&dorp[8], *(u32*)&dorp[12]); + for (int i = 0; i < 0x200; i+=16) + { + for (int j = 0; j < 16; j++) + { + printf("%02X ", dorp[i+j]); + } + printf("\n"); + } + } + return true; } @@ -638,7 +704,7 @@ void MapNWRAM_A(u32 num, u8 val) // When we only update the mapping on the written MBK, we will // have priority of the last witten MBK over the others - // However the hardware has a fixed order. Therefor + // However the hardware has a fixed order. Therefor // we need to iterate through them all in a fixed order and update // the mapping, so the result is independend on the MBK write order for (unsigned int part = 0; part < 4; part++) @@ -686,7 +752,7 @@ void MapNWRAM_B(u32 num, u8 val) // When we only update the mapping on the written MBK, we will // have priority of the last witten MBK over the others - // However the hardware has a fixed order. Therefor + // However the hardware has a fixed order. Therefor // we need to iterate through them all in a fixed order and update // the mapping, so the result is independend on the MBK write order for (unsigned int part = 0; part < 8; part++) @@ -741,7 +807,7 @@ void MapNWRAM_C(u32 num, u8 val) // When we only update the mapping on the written MBK, we will // have priority of the last witten MBK over the others - // However the hardware has a fixed order. Therefor + // However the hardware has a fixed order. Therefor // we need to iterate through them all in a fixed order and update // the mapping, so the result is independend on the MBK write order for (unsigned int part = 0; part < 8; part++) @@ -2139,17 +2205,17 @@ void ARM9IOWrite32(u32 addr, u32 val) MapNWRAM_C(6, (val >> 16) & 0xFF); MapNWRAM_C(7, val >> 24); return; - case 0x04004054: + case 0x04004054: if (!(SCFG_EXT[0] & (1 << 31))) /* no access to SCFG Registers if disabled*/ return; MapNWRAMRange(0, 0, val); return; - case 0x04004058: + case 0x04004058: if (!(SCFG_EXT[0] & (1 << 31))) /* no access to SCFG Registers if disabled*/ return; - MapNWRAMRange(0, 1, val); + MapNWRAMRange(0, 1, val); return; - case 0x0400405C: + case 0x0400405C: if (!(SCFG_EXT[0] & (1 << 31))) /* no access to SCFG Registers if disabled*/ return; MapNWRAMRange(0, 2, val); @@ -2200,7 +2266,7 @@ u8 ARM7IORead8(u32 addr) { switch (addr) { - case 0x04004000: + case 0x04004000: return SCFG_BIOS & 0xFF; case 0x04004001: return SCFG_BIOS >> 8; @@ -2456,27 +2522,27 @@ void ARM7IOWrite32(u32 addr, u32 val) Set_SCFG_MC(val); return; - case 0x04004054: + case 0x04004054: if (!(SCFG_EXT[1] & (1 << 31))) /* no access to SCFG Registers if disabled*/ return; MapNWRAMRange(1, 0, val); return; - case 0x04004058: + case 0x04004058: if (!(SCFG_EXT[1] & (1 << 31))) /* no access to SCFG Registers if disabled*/ return; MapNWRAMRange(1, 1, val); return; - case 0x0400405C: + case 0x0400405C: if (!(SCFG_EXT[1] & (1 << 31))) /* no access to SCFG Registers if disabled*/ return; MapNWRAMRange(1, 2, val); return; - case 0x04004060: + case 0x04004060: if (!(SCFG_EXT[1] & (1 << 31))) /* no access to SCFG Registers if disabled*/ return; val &= 0x00FFFF0F; - MBK[0][8] = val; - MBK[1][8] = val; + MBK[0][8] = val; + MBK[1][8] = val; return; case 0x04004100: NDMACnt[1] = val & 0x800F0000; return; diff --git a/src/DSi_AES.cpp b/src/DSi_AES.cpp index 937ca860..24750880 100644 --- a/src/DSi_AES.cpp +++ b/src/DSi_AES.cpp @@ -485,16 +485,13 @@ void WriteMAC(u32 offset, u32 val, u32 mask) //printf("AES: MAC: "); _printhex(MAC, 16); } -void DeriveNormalKey(u32 slot) +void DeriveNormalKey(u8* keyX, u8* keyY, u8* normalkey) { const u8 key_const[16] = {0xFF, 0xFE, 0xFB, 0x4E, 0x29, 0x59, 0x02, 0x58, 0x2A, 0x68, 0x0F, 0x5F, 0x1A, 0x4F, 0x3E, 0x79}; u8 tmp[16]; - //printf("slot%d keyX: ", slot); _printhex(KeyX[slot], 16); - //printf("slot%d keyY: ", slot); _printhex(KeyY[slot], 16); - for (int i = 0; i < 16; i++) - tmp[i] = KeyX[slot][i] ^ KeyY[slot][i]; + tmp[i] = keyX[i] ^ keyY[i]; u32 carry = 0; for (int i = 0; i < 16; i++) @@ -506,9 +503,7 @@ void DeriveNormalKey(u32 slot) ROL16(tmp, 42); - //printf("derive normalkey %d\n", slot); _printhex(tmp, 16); - - memcpy(KeyNormal[slot], tmp, 16); + memcpy(normalkey, tmp, 16); } void WriteKeyNormal(u32 slot, u32 offset, u32 val, u32 mask) @@ -539,7 +534,7 @@ void WriteKeyY(u32 slot, u32 offset, u32 val, u32 mask) if (offset >= 0xC) { - DeriveNormalKey(slot); + DeriveNormalKey(KeyX[slot], KeyY[slot], KeyNormal[slot]); } } @@ -555,7 +550,7 @@ void GetModcryptKey(u8* romheader, u8* key) return; } - u8 oldkeys[16*3]; + /*u8 oldkeys[16*3]; memcpy(&oldkeys[16*0], KeyX[0], 16); memcpy(&oldkeys[16*1], KeyY[0], 16); memcpy(&oldkeys[16*2], KeyNormal[0], 16); @@ -576,7 +571,7 @@ void GetModcryptKey(u8* romheader, u8* key) memcpy(KeyX[0], &oldkeys[16*0], 16); memcpy(KeyY[0], &oldkeys[16*1], 16); - memcpy(KeyNormal[0], &oldkeys[16*2], 16); + memcpy(KeyNormal[0], &oldkeys[16*2], 16);*/ } void ApplyModcrypt(u8* data, u32 len, u8* key, u8* iv) diff --git a/src/DSi_AES.h b/src/DSi_AES.h index 9ee4a70d..4793b743 100644 --- a/src/DSi_AES.h +++ b/src/DSi_AES.h @@ -46,6 +46,8 @@ void WriteKeyNormal(u32 slot, u32 offset, u32 val, u32 mask); void WriteKeyX(u32 slot, u32 offset, u32 val, u32 mask); void WriteKeyY(u32 slot, u32 offset, u32 val, u32 mask); +void Swap16(u8* dst, u8* src); +void DeriveNormalKey(u8* keyX, u8* keyY, u8* normalkey); void GetModcryptKey(u8* romheader, u8* key); void ApplyModcrypt(u8* data, u32 len, u8* key, u8* iv); diff --git a/src/sha1/sha1.hpp b/src/sha1/sha1.hpp new file mode 100644 index 00000000..23762e9b --- /dev/null +++ b/src/sha1/sha1.hpp @@ -0,0 +1,9 @@ +#ifndef SHA1_HPP +#define SHA1_HPP + +extern "C" +{ +#include "sha1.h" +} + +#endif