Skip to content

Commit

Permalink
Fully initialize the crypto system
Browse files Browse the repository at this point in the history
This allows chainloading Luma outside of boot9strap
Requires a good aeskeydb.bin, like MD5 A5B28945A7C051D7A0CD18AF0E580D1B
  • Loading branch information
d0k3 committed Aug 4, 2017
1 parent 030e939 commit 63b7604
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 166 deletions.
2 changes: 1 addition & 1 deletion source/common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
#endif

// GodMode9 version
#define VERSION "1.2.7"
#define VERSION "1.2.9"

// input / output paths
#define SUPPORT_PATHS "0:/gm9/support", "0:", "0:/files9" // legacy paths
Expand Down
130 changes: 70 additions & 60 deletions source/crypto/keydb.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,52 +61,6 @@ void CryptAesKeyInfo(AesKeyInfo* info) {
info->isEncrypted = !info->isEncrypted;
}

u32 CheckAesKeyInfo(u8* key, u32 keyslot, char type, char* id)
{
static const AesKeyHashInfo keyHashes[] = {
{ { 0x05, 'Y', "" }, KEYS_RETAIL, // Retail N3DS CTRNAND key SHA256
{ 0x98, 0x24, 0x27, 0x14, 0x22, 0xB0, 0x6B, 0xF2, 0x10, 0x96, 0x9C, 0x36, 0x42, 0x53, 0x7C, 0x86,
0x62, 0x22, 0x5C, 0xFD, 0x6F, 0xAE, 0x9B, 0x0A, 0x85, 0xA5, 0xCE, 0x21, 0xAA, 0xB6, 0xC8, 0x4D }
},
{ { 0x18, 'X', "" }, KEYS_RETAIL, // Retail NCCH Secure3 key SHA256
{ 0x76, 0xC7, 0x6B, 0x65, 0x5D, 0xB8, 0x52, 0x19, 0xC5, 0xD3, 0x5D, 0x51, 0x7F, 0xFA, 0xF7, 0xA4,
0x3E, 0xBA, 0xD6, 0x6E, 0x31, 0xFB, 0xDD, 0x57, 0x43, 0x92, 0x59, 0x37, 0xA8, 0x93, 0xCC, 0xFC }
},
{ { 0x1B, 'X', "" }, KEYS_RETAIL, // Retail NCCH Secure4 key SHA256
{ 0x9A, 0x20, 0x1E, 0x7C, 0x37, 0x37, 0xF3, 0x72, 0x2E, 0x5B, 0x57, 0x8D, 0x11, 0x83, 0x7F, 0x19,
0x7C, 0xA6, 0x5B, 0xF5, 0x26, 0x25, 0xB2, 0x69, 0x06, 0x93, 0xE4, 0x16, 0x53, 0x52, 0xC6, 0xBB }
},
{ { 0x25, 'X', "" }, KEYS_RETAIL, // Retail NCCH 7x key SHA256
{ 0x7E, 0x87, 0x8D, 0xDE, 0x92, 0x93, 0x8E, 0x4C, 0x71, 0x7D, 0xD5, 0x3D, 0x1E, 0xA3, 0x5A, 0x75,
0x63, 0x3F, 0x51, 0x30, 0xD8, 0xCF, 0xD7, 0xC7, 0x6C, 0x8F, 0x4A, 0x8F, 0xB8, 0x70, 0x50, 0xCD }
}/*,
{ { 0x18, 'X', "" }, KEYS_DEVKIT, // DevKit NCCH Secure3 key SHA256
{ 0x08, 0xE1, 0x09, 0x62, 0xF6, 0x5A, 0x09, 0xAA, 0x12, 0x2C, 0x7C, 0xBE, 0xDE, 0xA1, 0x9C, 0x4B,
0x5C, 0x9A, 0x8A, 0xC3, 0xD9, 0x8E, 0xA1, 0x62, 0x04, 0x11, 0xD7, 0xE8, 0x55, 0x70, 0xA6, 0xC2 }
},
{ { 0x1B, 'X', "" }, KEYS_DEVKIT, // DevKit NCCH Secure4 key SHA256
{ 0xA5, 0x3C, 0x3E, 0x5D, 0x09, 0x5C, 0x73, 0x35, 0x21, 0x79, 0x3F, 0x2E, 0x4C, 0x10, 0xCA, 0xAE,
0x87, 0x83, 0x51, 0x53, 0x46, 0x0B, 0x52, 0x39, 0x9B, 0x00, 0x62, 0xF6, 0x39, 0xCB, 0x62, 0x16 }
}*/
};

u8 keySha256[32];
sha_quick(keySha256, key, 16, SHA256_MODE);
for (u32 p = 0; p < sizeof(keyHashes) / sizeof(AesKeyHashInfo); p++) {
if ((keyHashes[p].desc.slot != keyslot) || (keyHashes[p].desc.type != type))
continue;
if ((!id && keyHashes[p].desc.id[0]) || (id && strncmp(id, keyHashes[p].desc.id, 10) != 0))
continue;
if (keyHashes[p].keyUnitType && (keyHashes[p].keyUnitType != GetUnitKeysType()))
continue;
if (memcmp(keySha256, keyHashes[p].keySha256, 32) == 0) {
return 0;
}
}

return 1;
}

u32 CheckKeySlot(u32 keyslot, char type)
{
static const AesNcchSampleInfo keyNcchSamples[] = {
Expand Down Expand Up @@ -146,7 +100,7 @@ u32 CheckKeySlot(u32 keyslot, char type)
set_ctr(zeroes);
aes_decrypt(sample, sample, 1, AES_CNT_CTRNAND_MODE);
if (memcmp(keyNcchSamples[p].sample, sample, 16) == 0) {
keyXState |= (u64) 1 << keyslot;
keyXState |= 1ull << keyslot;
return 0;
}
}
Expand All @@ -155,21 +109,22 @@ u32 CheckKeySlot(u32 keyslot, char type)
return 1;
}

u32 LoadKeyFromFile(u8* key, u32 keyslot, char type, char* id)
u32 LoadKeyFromFile(void* key, u32 keyslot, char type, char* id)
{
const char* base[] = { SUPPORT_PATHS };
u8 keystore[16] __attribute__((aligned(32))) = {0};
bool found = false;

// use keystore if key == NULL
if (!key) key = keystore;

// checking the obvious
if ((keyslot >= 0x40) || ((type != 'X') && (type != 'Y') && (type != 'N') && (type != 'I')))
return 1;

// check if already loaded
if (!id && (CheckKeySlot(keyslot, type) == 0)) return 0;
if (!key && !id && (CheckKeySlot(keyslot, type) == 0)) return 0;

// use keystore if key == NULL
if (!key) key = keystore;

// try to get key from 'aeskeydb.bin' file
for (u32 i = 0; !found && (i < (sizeof(base)/sizeof(char*))); i++) {
FIL fp;
Expand Down Expand Up @@ -213,23 +168,78 @@ u32 LoadKeyFromFile(u8* key, u32 keyslot, char type, char* id)
// done if this is an IV
if (type == 'I') return 0;

// verify key (verification is disabled)
// if (CheckAesKeyInfo(key, keyslot, type, id) != 0) return 1;

// now, setup the key
if (type == 'X') {
setup_aeskeyX(keyslot, key);
keyXState |= (u64) 1 << keyslot;
keyXState |= 1ull << keyslot;
} else if (type == 'Y') {
setup_aeskeyY(keyslot, key);
keyYState |= (u64) 1 << keyslot;
keyYState |= 1ull << keyslot;
} else { // normalKey includes keyX & keyY
setup_aeskey(keyslot, key);
keyState |= (u64) 1 << keyslot;
keyXState |= (u64) 1 << keyslot;
keyYState |= (u64) 1 << keyslot;
keyState |= 1ull << keyslot;
keyXState |= 1ull << keyslot;
keyYState |= 1ull << keyslot;
}
use_aeskey(keyslot);

return 0;
}

u32 InitKeyDb( void )
{
// use this to quickly initialize all applicable keys in aeskeydb.bin
static const u64 keyslot_whitelist = (1ull<<0x02)|(1ull<<0x03)|(1ull<<0x05)|(1ull<<0x18)|(1ull<<0x19)|(1ull<<0x1A)|(1ull<<0x1B)|
(1ull<<0x1C)|(1ull<<0x1D)|(1ull<<0x1E)|(1ull<<0x1F)|(1ull<<0x24)|(1ull<<0x25)|(1ull<<0x2F);
AesKeyInfo* keydb = (AesKeyInfo*) (void*) TEMP_BUFFER;
u32 nkeys = 0;

// try to load aeskeydb.bin file
const char* base[] = { SUPPORT_PATHS };
for (u32 i = 0; !nkeys && (i < (sizeof(base)/sizeof(char*))); i++) {
FIL fp;
UINT btr;
char path[64];
snprintf(path, 64, "%s/%s", base[i], KEYDB_NAME);
if (f_open(&fp, path, FA_READ | FA_OPEN_EXISTING) != FR_OK) continue;
if ((f_read(&fp, keydb, TEMP_BUFFER_SIZE, &btr) == FR_OK) &&
(btr > 0) && (btr < TEMP_BUFFER_SIZE) && !(btr % sizeof(AesKeyInfo)))
nkeys = btr / sizeof(AesKeyInfo);
f_close(&fp);
}

// failed if arriving here with empty hands
if (!nkeys) return 1;

// apply all applicable keys
for (u32 i = 0; i < nkeys; i++) {
AesKeyInfo* info = &(keydb[i]);
if ((info->slot >= 0x40) || ((info->type != 'X') && (info->type != 'Y') && (info->type != 'N') && (info->type != 'I')))
return 1; // looks faulty, better stop right here
if (!((1ull<<info->slot)&keyslot_whitelist)) continue; // not in keyslot whitelist
if ((info->type == 'I') || (*(info->id)) || (info->keyUnitType && (info->keyUnitType != GetUnitKeysType())) ||
(CheckKeySlot(info->slot, info->type) == 0)) continue; // most likely valid, but not applicable or already set
if (info->isEncrypted) CryptAesKeyInfo(info); // decrypt key

// apply key
u8 key[16] __attribute__((aligned(32))) = {0};
char type = info->type;
u32 keyslot = info->slot;
memcpy(key, info->key, 16);
if (type == 'X') {
setup_aeskeyX(keyslot, key);
keyXState |= 1ull << keyslot;
} else if (type == 'Y') {
setup_aeskeyY(keyslot, key);
keyYState |= 1ull << keyslot;
} else { // normalKey includes keyX & keyY
setup_aeskey(keyslot, key);
keyState |= 1ull << keyslot;
keyXState |= 1ull << keyslot;
keyYState |= 1ull << keyslot;
}
use_aeskey(keyslot);
}

return 0;
}
3 changes: 2 additions & 1 deletion source/crypto/keydb.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ typedef struct {

u32 GetUnitKeysType(void);
void CryptAesKeyInfo(AesKeyInfo* info);
u32 LoadKeyFromFile(u8* key, u32 keyslot, char type, char* id);
u32 LoadKeyFromFile(void* key, u32 keyslot, char type, char* id);
u32 InitKeyDb(void);
4 changes: 2 additions & 2 deletions source/godmode.c
Original file line number Diff line number Diff line change
Expand Up @@ -1412,7 +1412,7 @@ u32 FileHandlerMenu(char* current_path, u32* cursor, u32* scroll, DirStruct* cur
(ValidateFirm(TEMP_BUFFER, firm_size) != 0)) {
// fix the boot path first ("sdmc"/"nand" for Luma et al, hacky af)
const char* bootpath = curr_entry->path;
char fixpath[256] = { 0 }; // for Luma et al, hacky as fuck
char fixpath[256] = { 0 };
if ((*bootpath != '0') && (*bootpath != '1')) {
optionstr[0] = "Make a copy at " OUTPUT_PATH "/temp.firm";
optionstr[1] = "Try to boot anyways";
Expand Down Expand Up @@ -1605,7 +1605,7 @@ u32 GodMode() {

InitSDCardFS();
AutoEmuNandBase(true);
InitNandCrypto();
InitNandCrypto(true);
InitExtFS();

// this takes long - do it while splash is displayed
Expand Down
Loading

0 comments on commit 63b7604

Please sign in to comment.