Minor stuff

This commit is contained in:
Aurora 2016-09-20 14:29:26 +02:00
parent fd33ef8496
commit f228cb241f
3 changed files with 31 additions and 31 deletions

View File

@ -22,7 +22,7 @@
/* /*
* Crypto libs from http://github.com/b1l1s/ctr * Crypto libs from http://github.com/b1l1s/ctr
* ARM9Loader code originally adapted from https://github.com/Reisyukaku/ReiNand/blob/228c378255ba693133dec6f3368e14d386f2cde7/source/crypto.c#L233 * kernel9Loader code originally adapted from https://github.com/Reisyukaku/ReiNand/blob/228c378255ba693133dec6f3368e14d386f2cde7/source/crypto.c#L233
* decryptNusFirm code adapted from https://github.com/mid-kid/CakesForeveryWan/blob/master/source/firm.c * decryptNusFirm code adapted from https://github.com/mid-kid/CakesForeveryWan/blob/master/source/firm.c
*/ */
@ -398,48 +398,48 @@ void decryptNusFirm(const u8 *inbuf, u8 *outbuf, u32 ncchSize)
decryptExeFs(outbuf); decryptExeFs(outbuf);
} }
void arm9Loader(u8 *arm9Section) void kernel9Loader(u8 *arm9Section)
{ {
//Determine the arm9loader version //Determine the kernel9loader version
u32 a9lVersion; u32 k9lVersion;
switch(arm9Section[0x53]) switch(arm9Section[0x53])
{ {
case 0xFF: case 0xFF:
a9lVersion = 0; k9lVersion = 0;
break; break;
case '1': case '1':
a9lVersion = 1; k9lVersion = 1;
break; break;
default: default:
a9lVersion = 2; k9lVersion = 2;
break; break;
} }
bool needToDecrypt = *(u32 *)(arm9Section + 0x800) != 0x47704770; bool needToDecrypt = *(u32 *)(arm9Section + 0x800) != 0x47704770;
if(a9lVersion == 2 || (a9lVersion == 1 && needToDecrypt)) if(!isDevUnit && (k9lVersion == 2 || (k9lVersion == 1 && needToDecrypt)))
{ {
if(!isDevUnit) //Set 0x11 keyslot
{ const u8 __attribute__((aligned(4))) key1[AES_BLOCK_SIZE] = {0x07, 0x29, 0x44, 0x38, 0xF8, 0xC9, 0x75, 0x93, 0xAA, 0x0E, 0x4A, 0xB4, 0xAE, 0x84, 0xC1, 0xD8};
//Set 0x11 keyslot const u8 __attribute__((aligned(4))) key2[AES_BLOCK_SIZE] = {0x42, 0x3F, 0x81, 0x7A, 0x23, 0x52, 0x58, 0x31, 0x6E, 0x75, 0x8E, 0x3A, 0x39, 0x43, 0x2E, 0xD0};
const u8 __attribute__((aligned(4))) key1[AES_BLOCK_SIZE] = {0x07, 0x29, 0x44, 0x38, 0xF8, 0xC9, 0x75, 0x93, 0xAA, 0x0E, 0x4A, 0xB4, 0xAE, 0x84, 0xC1, 0xD8}; aes_setkey(0x11, k9lVersion == 2 ? key2 : key1, AES_KEYNORMAL, AES_INPUT_BE | AES_INPUT_NORMAL);
const u8 __attribute__((aligned(4))) key2[AES_BLOCK_SIZE] = {0x42, 0x3F, 0x81, 0x7A, 0x23, 0x52, 0x58, 0x31, 0x6E, 0x75, 0x8E, 0x3A, 0x39, 0x43, 0x2E, 0xD0}; }
aes_setkey(0x11, a9lVersion == 2 ? key2 : key1, AES_KEYNORMAL, AES_INPUT_BE | AES_INPUT_NORMAL);
}
if(needToDecrypt) if(needToDecrypt)
{
u8 arm9BinSlot;
if(k9lVersion == 0) arm9BinSlot = 0x15;
else
{ {
arm9BinSlot = 0x16;
//Set keyX //Set keyX
u8 __attribute__((aligned(4))) keyX[AES_BLOCK_SIZE]; u8 __attribute__((aligned(4))) keyX[AES_BLOCK_SIZE];
aes_use_keyslot(0x11); aes_use_keyslot(0x11);
aes(keyX, arm9Section + 0x60, 1, NULL, AES_ECB_DECRYPT_MODE, 0); aes(keyX, arm9Section + 0x60, 1, NULL, AES_ECB_DECRYPT_MODE, 0);
aes_setkey(0x16, keyX, AES_KEYX, AES_INPUT_BE | AES_INPUT_NORMAL); aes_setkey(0x16, keyX, AES_KEYX, AES_INPUT_BE | AES_INPUT_NORMAL);
} }
}
if(needToDecrypt)
{
u8 arm9BinSlot = a9lVersion > 0 ? 0x16 : 0x15;
//Set keyY //Set keyY
u8 __attribute__((aligned(4))) keyY[AES_BLOCK_SIZE]; u8 __attribute__((aligned(4))) keyY[AES_BLOCK_SIZE];
@ -456,13 +456,13 @@ void arm9Loader(u8 *arm9Section)
for(u8 *tmp = arm9Section + 0x30; *tmp != 0; tmp++) for(u8 *tmp = arm9Section + 0x30; *tmp != 0; tmp++)
arm9BinSize = (arm9BinSize << 3) + (arm9BinSize << 1) + *tmp - '0'; arm9BinSize = (arm9BinSize << 3) + (arm9BinSize << 1) + *tmp - '0';
//Decrypt arm9bin //Decrypt ARM9 binary
aes_use_keyslot(arm9BinSlot); aes_use_keyslot(arm9BinSlot);
aes(arm9Section + 0x800, arm9Section + 0x800, arm9BinSize / AES_BLOCK_SIZE, arm9BinCtr, AES_CTR_MODE, AES_INPUT_BE | AES_INPUT_NORMAL); aes(arm9Section + 0x800, arm9Section + 0x800, arm9BinSize / AES_BLOCK_SIZE, arm9BinCtr, AES_CTR_MODE, AES_INPUT_BE | AES_INPUT_NORMAL);
} }
//Set >=9.6 KeyXs //Set >=9.6 KeyXs
if(a9lVersion == 2) if(k9lVersion == 2)
{ {
u8 __attribute__((aligned(4))) keyData[AES_BLOCK_SIZE] = {0xDD, 0xDA, 0xA4, 0xC6, 0x2C, 0xC4, 0x50, 0xE9, 0xDA, 0xB6, 0x9B, 0x0D, 0x9D, 0x2A, 0x21, 0x98}; u8 __attribute__((aligned(4))) keyData[AES_BLOCK_SIZE] = {0xDD, 0xDA, 0xA4, 0xC6, 0x2C, 0xC4, 0x50, 0xE9, 0xDA, 0xB6, 0x9B, 0x0D, 0x9D, 0x2A, 0x21, 0x98};
u8 __attribute__((aligned(4))) decKey[sizeof(keyData)]; u8 __attribute__((aligned(4))) decKey[sizeof(keyData)];

View File

@ -22,7 +22,7 @@
/* /*
* Crypto libs from http://github.com/b1l1s/ctr * Crypto libs from http://github.com/b1l1s/ctr
* ARM9Loader code originally adapted from https://github.com/Reisyukaku/ReiNand/blob/228c378255ba693133dec6f3368e14d386f2cde7/source/crypto.c#L233 * kernel9Loader code originally adapted from https://github.com/Reisyukaku/ReiNand/blob/228c378255ba693133dec6f3368e14d386f2cde7/source/crypto.c#L233
* decryptNusFirm code adapted from https://github.com/mid-kid/CakesForeveryWan/blob/master/source/firm.c * decryptNusFirm code adapted from https://github.com/mid-kid/CakesForeveryWan/blob/master/source/firm.c
*/ */
@ -110,5 +110,5 @@ u32 ctrNandRead(u32 sector, u32 sectorCount, u8 *outbuf);
void set6x7xKeys(void); void set6x7xKeys(void);
void decryptExeFs(u8 *inbuf); void decryptExeFs(u8 *inbuf);
void decryptNusFirm(const u8 *inbuf, u8 *outbuf, u32 ncchSize); void decryptNusFirm(const u8 *inbuf, u8 *outbuf, u32 ncchSize);
void arm9Loader(u8 *arm9Section); void kernel9Loader(u8 *arm9Section);
void computePinHash(u8 *outbuf, const u8 *inbuf); void computePinHash(u8 *outbuf, const u8 *inbuf);

View File

@ -347,8 +347,8 @@ static inline void patchNativeFirm(u32 firmVersion, FirmwareSource nandType, u32
if(isN3DS) if(isN3DS)
{ {
//Decrypt ARM9Bin and patch ARM9 entrypoint to skip arm9loader //Decrypt ARM9Bin and patch ARM9 entrypoint to skip kernel9loader
arm9Loader(arm9Section); kernel9Loader(arm9Section);
firm->arm9Entry = (u8 *)0x801B01C; firm->arm9Entry = (u8 *)0x801B01C;
} }
@ -425,10 +425,10 @@ static inline void patchLegacyFirm(FirmwareType firmType, u32 firmVersion, u32 d
{ {
u8 *arm9Section = (u8 *)firm + section[3].offset; u8 *arm9Section = (u8 *)firm + section[3].offset;
//On N3DS, decrypt ARM9Bin and patch ARM9 entrypoint to skip arm9loader //On N3DS, decrypt ARM9Bin and patch ARM9 entrypoint to skip kernel9loader
if(isN3DS) if(isN3DS)
{ {
arm9Loader(arm9Section); kernel9Loader(arm9Section);
firm->arm9Entry = (u8 *)0x801301C; firm->arm9Entry = (u8 *)0x801301C;
} }
@ -445,8 +445,8 @@ static inline void patch1x2xNativeAndSafeFirm(u32 devMode)
if(isN3DS) if(isN3DS)
{ {
//Decrypt ARM9Bin and patch ARM9 entrypoint to skip arm9loader //Decrypt ARM9Bin and patch ARM9 entrypoint to skip kernel9loader
arm9Loader(arm9Section); kernel9Loader(arm9Section);
firm->arm9Entry = (u8 *)0x801B01C; firm->arm9Entry = (u8 *)0x801B01C;
patchFirmWrites(arm9Section, section[2].size); patchFirmWrites(arm9Section, section[2].size);