Reduce code stink (#1818)

CRC32.cpp:
Make table initialization compile time

DSi_NAND.cpp:
Fix file close / unmount / disk close on error
~L427: Remove redundant calls, as they are immediately rendered useless by `rem` being overwritten

NDS.cpp / FreeBIOS.h:
Remove unneeded size values in header
Remove unneeded memset's as they are initialized anyway

sha1.c / sha1.h:
Fix useless warning

Wifi.cpp:
Remove unneeded includes

DSi.cpp:
Reduce ugly casts
Deduplicate code

qt_sdl/main.cpp:
silence clang switch statement warning

qt_sdl/main.h:
fix override warnings

dolphin/BitSet.h:
use msvc extensions only when appropriate, fix broken bit set count under _WIN32
This commit is contained in:
jdp_
2023-08-28 14:01:15 -04:00
committed by GitHub
parent b4aa7fafc9
commit 2a3a071216
18 changed files with 177 additions and 173 deletions

View File

@ -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];