diff options
Diffstat (limited to 'src/DSi_NAND.cpp')
-rw-r--r-- | src/DSi_NAND.cpp | 65 |
1 files changed, 28 insertions, 37 deletions
diff --git a/src/DSi_NAND.cpp b/src/DSi_NAND.cpp index 38f8c52..21da7ed 100644 --- a/src/DSi_NAND.cpp +++ b/src/DSi_NAND.cpp @@ -124,6 +124,9 @@ bool Init(u8* es_keyY) if (memcmp(nand_footer, nand_footer_ref, 16)) { Log(LogLevel::Error, "ERROR: NAND missing nocash footer\n"); + CloseFile(nandfile); + f_unmount("0:"); + ff_disk_close(); return false; } } @@ -141,7 +144,7 @@ bool Init(u8* es_keyY) SHA1Update(&sha, eMMC_CID, 16); SHA1Final(tmp, &sha); - DSi_AES::Swap16(FATIV, tmp); + Bswap128(FATIV, tmp); *(u32*)&keyX[0] = (u32)ConsoleID; *(u32*)&keyX[4] = (u32)ConsoleID ^ 0x24EE6906; @@ -154,7 +157,7 @@ bool Init(u8* es_keyY) *(u32*)&keyY[12] = 0xE1A00005; DSi_AES::DeriveNormalKey(keyX, keyY, tmp); - DSi_AES::Swap16(FATKey, tmp); + Bswap128(FATKey, tmp); *(u32*)&keyX[0] = 0x4E00004A; @@ -165,7 +168,7 @@ bool Init(u8* es_keyY) memcpy(keyY, es_keyY, 16); DSi_AES::DeriveNormalKey(keyX, keyY, tmp); - DSi_AES::Swap16(ESKey, tmp); + Bswap128(ESKey, tmp); CurFile = nandfile; return true; @@ -197,7 +200,7 @@ void GetIDs(u8* emmc_cid, u64& consoleid) void SetupFATCrypto(AES_ctx* ctx, u32 ctr) { u8 iv[16]; - memcpy(iv, FATIV, 16); + memcpy(iv, FATIV, sizeof(iv)); u32 res; res = iv[15] + (ctr & 0xFF); @@ -232,9 +235,9 @@ u32 ReadFATBlock(u64 addr, u32 len, u8* buf) for (u32 i = 0; i < len; i += 16) { u8 tmp[16]; - DSi_AES::Swap16(tmp, &buf[i]); - AES_CTR_xcrypt_buffer(&ctx, tmp, 16); - DSi_AES::Swap16(&buf[i], tmp); + Bswap128(tmp, &buf[i]); + AES_CTR_xcrypt_buffer(&ctx, tmp, sizeof(tmp)); + Bswap128(&buf[i], tmp); } return len; @@ -256,9 +259,9 @@ u32 WriteFATBlock(u64 addr, u32 len, u8* buf) for (u32 i = 0; i < 0x200; i += 16) { u8 tmp[16]; - DSi_AES::Swap16(tmp, &buf[s+i]); - AES_CTR_xcrypt_buffer(&ctx, tmp, 16); - DSi_AES::Swap16(&tempbuf[i], tmp); + Bswap128(tmp, &buf[s+i]); + AES_CTR_xcrypt_buffer(&ctx, tmp, sizeof(tmp)); + Bswap128(&tempbuf[i], tmp); } u32 res = FileWrite(tempbuf, sizeof(tempbuf), 1, CurFile); @@ -320,13 +323,13 @@ bool ESEncrypt(u8* data, u32 len) { u8 tmp[16]; - DSi_AES::Swap16(tmp, &data[i]); + Bswap128(tmp, &data[i]); for (int i = 0; i < 16; i++) mac[i] ^= tmp[i]; AES_CTR_xcrypt_buffer(&ctx, tmp, 16); AES_ECB_encrypt(&ctx, mac); - DSi_AES::Swap16(&data[i], tmp); + Bswap128(&data[i], tmp); } u32 remlen = len - coarselen; @@ -334,26 +337,21 @@ bool ESEncrypt(u8* data, u32 len) { u8 rem[16]; - memset(rem, 0, 16); - - for (int i = 0; i < remlen; i++) - rem[15-i] = data[coarselen+i]; + Bswap128(rem, &data[coarselen]); for (int i = 0; i < 16; i++) mac[i] ^= rem[i]; - AES_CTR_xcrypt_buffer(&ctx, rem, 16); + AES_CTR_xcrypt_buffer(&ctx, rem, sizeof(rem)); AES_ECB_encrypt(&ctx, mac); - for (int i = 0; i < remlen; i++) - data[coarselen+i] = rem[15-i]; + Bswap128(&data[coarselen], rem); } ctx.Iv[13] = 0x00; ctx.Iv[14] = 0x00; ctx.Iv[15] = 0x00; - AES_CTR_xcrypt_buffer(&ctx, mac, 16); + AES_CTR_xcrypt_buffer(&ctx, mac, sizeof(mac)); - for (int i = 0; i < 16; i++) - data[len+i] = mac[15-i]; + Bswap128(&data[len], mac); u8 footer[16]; @@ -369,7 +367,7 @@ bool ESEncrypt(u8* data, u32 len) footer[0] = len & 0xFF; AES_ctx_set_iv(&ctx, iv); - AES_CTR_xcrypt_buffer(&ctx, footer, 16); + AES_CTR_xcrypt_buffer(&ctx, footer, sizeof(footer)); data[len+0x10] = footer[15]; data[len+0x1D] = footer[2]; @@ -407,13 +405,13 @@ bool ESDecrypt(u8* data, u32 len) { u8 tmp[16]; - DSi_AES::Swap16(tmp, &data[i]); + Bswap128(tmp, &data[i]); - AES_CTR_xcrypt_buffer(&ctx, tmp, 16); + AES_CTR_xcrypt_buffer(&ctx, tmp, sizeof(tmp)); for (int i = 0; i < 16; i++) mac[i] ^= tmp[i]; AES_ECB_encrypt(&ctx, mac); - DSi_AES::Swap16(&data[i], tmp); + Bswap128(&data[i], tmp); } u32 remlen = len - coarselen; @@ -426,20 +424,14 @@ bool ESDecrypt(u8* data, u32 len) iv[14] = (ivnum >> 8) & 0xFF; iv[15] = ivnum & 0xFF; - memset(rem, 0, 16); - AES_ctx_set_iv(&ctx, iv); - AES_CTR_xcrypt_buffer(&ctx, rem, 16); - - for (int i = 0; i < remlen; i++) - rem[15-i] = data[coarselen+i]; + Bswap128(rem, &data[coarselen]); AES_ctx_set_iv(&ctx, iv); AES_CTR_xcrypt_buffer(&ctx, rem, 16); for (int i = 0; i < 16; i++) mac[i] ^= rem[i]; AES_ECB_encrypt(&ctx, mac); - for (int i = 0; i < remlen; i++) - data[coarselen+i] = rem[15-i]; + Bswap128(&data[coarselen], rem); } ctx.Iv[13] = 0x00; @@ -455,11 +447,10 @@ bool ESDecrypt(u8* data, u32 len) for (int i = 0; i < 12; i++) iv[3+i] = data[len+0x1C-i]; iv[15] = 0x00; - for (int i = 0; i < 16; i++) - footer[15-i] = data[len+0x10+i]; + Bswap128(footer, &data[len+0x10]); AES_ctx_set_iv(&ctx, iv); - AES_CTR_xcrypt_buffer(&ctx, footer, 16); + AES_CTR_xcrypt_buffer(&ctx, footer, sizeof(footer)); data[len+0x10] = footer[15]; data[len+0x1D] = footer[2]; |