mirror of
https://github.com/DarkStore-3DS/Project_CTR.git
synced 2026-07-04 16:59:02 +00:00
big update
lots cleaned, added cia to cci conv, it 's called a block, separated reading from building, improved ncch keyx stuff, and basic verbose for keys, elf checking and romfs
This commit is contained in:
+63
-47
@@ -1,14 +1,16 @@
|
||||
#include "lib.h"
|
||||
|
||||
// KeyData
|
||||
#include "tpki.h" // Test PKI
|
||||
#include "ppki.h" // Production PKI
|
||||
#include "dpki.h" // Development PKI
|
||||
#include "pki/test.h" // Test PKI
|
||||
#include "pki/prod.h" // Production PKI
|
||||
#include "pki/dev.h" // Development PKI
|
||||
|
||||
// Private Prototypes
|
||||
int SetRsaKeySet(u8 **PrivDest, u8 *PrivSource, u8 **PubDest, u8 *PubSource);
|
||||
int SetunFixedKey(keys_struct *keys, u8 *unFixedKey);
|
||||
void InitcommonKeySlots(keys_struct *keys);
|
||||
void InitCommonKeySlots(keys_struct *keys);
|
||||
void InitNcchKeyXSlots(keys_struct *keys);
|
||||
int SetNcchKeyX(keys_struct *keys, u8 *keyX, u8 index);
|
||||
|
||||
FILE* keyset_OpenFile(char *dir, char *name, bool FileRequired);
|
||||
void keysetOpenError(char *file);
|
||||
@@ -33,11 +35,12 @@ void DumpKeyset(keys_struct *keys);
|
||||
void InitKeys(keys_struct *keys)
|
||||
{
|
||||
memset(keys,0,sizeof(keys_struct));
|
||||
InitcommonKeySlots(keys);
|
||||
InitCommonKeySlots(keys);
|
||||
InitNcchKeyXSlots(keys);
|
||||
keys->rsa.cxiHdrPub = malloc(RSA_2048_KEY_SIZE);
|
||||
keys->rsa.cxiHdrPvt = malloc(RSA_2048_KEY_SIZE);
|
||||
keys->aes.unFixedKey0 = malloc(16);
|
||||
keys->aes.unFixedKey1 = malloc(16);
|
||||
keys->aes.ncchKey0 = malloc(AES_128_KEY_SIZE);
|
||||
keys->aes.ncchKey1 = malloc(AES_128_KEY_SIZE);
|
||||
}
|
||||
|
||||
void PrintBadKeySize(char *path, u32 size)
|
||||
@@ -45,6 +48,21 @@ void PrintBadKeySize(char *path, u32 size)
|
||||
fprintf(stderr,"[KEYSET ERROR] %s has invalid size (0x%x)\n",path,size);
|
||||
}
|
||||
|
||||
u8* AesKeyScrambler(u8 *key, u8 *keyX, u8 *keyY)
|
||||
{
|
||||
// Process keyX/keyY to get raw normal key
|
||||
for(int i = 0; i < 16; i++)
|
||||
key[i] = keyX[i] ^ ((keyY[i] >> 2) | ((keyY[i < 15 ? i+1 : 0] & 3) << 6)); // keyX[i] ^
|
||||
|
||||
const u8 SCRAMBLE_SECRET[16] = {0x51, 0xD7, 0x5D, 0xBE, 0xFD, 0x07, 0x57, 0x6A, 0x1C, 0xFC, 0x2A, 0xF0, 0x94, 0x4B, 0xD5, 0x6C};
|
||||
|
||||
// Apply Secret to get final normal key
|
||||
for(int i = 0; i < 16; i++)
|
||||
key[i] = key[i] ^ SCRAMBLE_SECRET[i];
|
||||
|
||||
return key;
|
||||
}
|
||||
|
||||
int SetKeys(keys_struct *keys)
|
||||
{
|
||||
int result = 0;
|
||||
@@ -90,9 +108,9 @@ int LoadKeysFromResources(keys_struct *keys)
|
||||
keys->keysetLoaded = true;
|
||||
/* AES Keys */
|
||||
// CIA
|
||||
for(int i = 0; i < 2; i++){
|
||||
for(int i = 0; i < 2; i++)
|
||||
SetCommonKey(keys,(u8*)ctr_common_etd_key_dpki[i],i);
|
||||
}
|
||||
|
||||
if(keys->aes.currentCommonKey > 0xff)
|
||||
SetCurrentCommonKey(keys,0);
|
||||
|
||||
@@ -100,10 +118,9 @@ int LoadKeysFromResources(keys_struct *keys)
|
||||
SetNormalKey(keys,(u8*)dev_fixed_ncch_key[0]);
|
||||
SetSystemFixedKey(keys,(u8*)dev_fixed_ncch_key[1]);
|
||||
|
||||
/*
|
||||
keys->aes.ncchKeyX0 = (u8*)dev_unfixed_ncch_keyX[0];
|
||||
keys->aes.ncchKeyX1 = (u8*)dev_unfixed_ncch_keyX[1];
|
||||
*/
|
||||
for(int i = 0; i < 2; i++)
|
||||
SetNcchKeyX(keys,(u8*)dev_unfixed_ncch_keyX[i],i);
|
||||
|
||||
|
||||
/* RSA Keys */
|
||||
// CIA
|
||||
@@ -123,18 +140,19 @@ int LoadKeysFromResources(keys_struct *keys)
|
||||
keys->keysetLoaded = true;
|
||||
/* AES Keys */
|
||||
// CIA
|
||||
for(int i = 0; i < 6; i++){
|
||||
keys->aes.commonKey[i] = malloc(16);
|
||||
AesKeyScrambler(keys->aes.commonKey[i],(u8*)ctr_common_etd_keyX_ppki,(u8*)ctr_common_etd_keyY_ppki[i]);
|
||||
}
|
||||
SetCurrentCommonKey(keys,1);
|
||||
//for(int i = 0; i < 6; i++){
|
||||
// keys->aes.commonKey[i] = malloc(16);
|
||||
// AesKeyScrambler(keys->aes.commonKey[i],(u8*)ctr_common_etd_keyX_ppki,(u8*)ctr_common_etd_keyY_ppki[i]);
|
||||
//}
|
||||
if(keys->aes.currentCommonKey > 0xff)
|
||||
SetCurrentCommonKey(keys,0);
|
||||
|
||||
// NCCH
|
||||
keys->aes.normalKey = NULL;
|
||||
keys->aes.systemFixedKey = NULL;
|
||||
/*
|
||||
keys->aes.ncchKeyX0 = (u8*)prod_unfixed_ncch_keyX[0];
|
||||
keys->aes.ncchKeyX1 = (u8*)prod_unfixed_ncch_keyX[1];
|
||||
for(int i = 0; i < 2; i++)
|
||||
SetNcchKeyX(keys,(u8*)prod_unfixed_ncch_keyX[i],i);
|
||||
*/
|
||||
|
||||
/* RSA Keys */
|
||||
@@ -264,15 +282,19 @@ void FreeKeys(keys_struct *keys)
|
||||
{
|
||||
// AES
|
||||
if(keys->aes.commonKey){
|
||||
for(int i = 0; i < 256; i++){
|
||||
for(int i = 0; i <= MAX_CMN_KEY; i++)
|
||||
free(keys->aes.commonKey[i]);
|
||||
}
|
||||
}
|
||||
free(keys->aes.commonKey);
|
||||
free(keys->aes.normalKey);
|
||||
free(keys->aes.systemFixedKey);
|
||||
free(keys->aes.unFixedKey0);
|
||||
free(keys->aes.unFixedKey1);
|
||||
if(keys->aes.ncchKeyX){
|
||||
for(int i = 0; i <= MAX_NCCH_KEYX; i++)
|
||||
free(keys->aes.ncchKeyX[i]);
|
||||
}
|
||||
free(keys->aes.ncchKeyX);
|
||||
free(keys->aes.ncchKey0);
|
||||
free(keys->aes.ncchKey1);
|
||||
|
||||
// RSA
|
||||
free(keys->rsa.xsPvt);
|
||||
@@ -309,18 +331,28 @@ int SetRsaKeySet(u8 **PrivDest, u8 *PrivSource, u8 **PubDest, u8 *PubSource)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int SetCommonKey(keys_struct *keys, u8 *commonKey, u8 Index)
|
||||
int SetCommonKey(keys_struct *keys, u8 *commonKey, u8 index)
|
||||
{
|
||||
if(!keys) return -1;
|
||||
return CopyData(&keys->aes.commonKey[Index],commonKey,16);
|
||||
return CopyData(&keys->aes.commonKey[index],commonKey,AES_128_KEY_SIZE);
|
||||
}
|
||||
|
||||
void InitcommonKeySlots(keys_struct *keys)
|
||||
void InitCommonKeySlots(keys_struct *keys)
|
||||
{
|
||||
if(!keys->aes.commonKey){
|
||||
keys->aes.commonKey = malloc(sizeof(u8*)*256);
|
||||
memset(keys->aes.commonKey,0,sizeof(u8*)*256);
|
||||
}
|
||||
if(!keys->aes.commonKey)
|
||||
keys->aes.commonKey = calloc(MAX_CMN_KEY+1,sizeof(u8*));
|
||||
}
|
||||
|
||||
int SetNcchKeyX(keys_struct *keys, u8 *keyX, u8 index)
|
||||
{
|
||||
if(!keys) return -1;
|
||||
return CopyData(&keys->aes.ncchKeyX[index],keyX,AES_128_KEY_SIZE);
|
||||
}
|
||||
|
||||
void InitNcchKeyXSlots(keys_struct *keys)
|
||||
{
|
||||
if(!keys->aes.ncchKeyX)
|
||||
keys->aes.ncchKeyX = calloc(MAX_NCCH_KEYX+1,sizeof(u8*));
|
||||
}
|
||||
|
||||
int SetCurrentCommonKey(keys_struct *keys, u8 Index)
|
||||
@@ -342,22 +374,6 @@ int SetSystemFixedKey(keys_struct *keys, u8 *systemFixedKey)
|
||||
return CopyData(&keys->aes.systemFixedKey,systemFixedKey,16);
|
||||
}
|
||||
|
||||
int SetNcchUnfixedKeys(keys_struct *keys, u8 *ncchSig)
|
||||
{
|
||||
if(!keys) return -1;
|
||||
|
||||
//memdump(stdout,"keyY: ",ncchSig,16);
|
||||
//memdump(stdout,"keyX0: ",keys->aes.ncchKeyX0,16);
|
||||
//memdump(stdout,"keyX1: ",keys->aes.ncchKeyX1,16);
|
||||
|
||||
if(keys->aes.ncchKeyX0)
|
||||
AesKeyScrambler(keys->aes.unFixedKey0,keys->aes.ncchKeyX0,ncchSig);
|
||||
if(keys->aes.ncchKeyX1)
|
||||
AesKeyScrambler(keys->aes.unFixedKey1,keys->aes.ncchKeyX1,ncchSig);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int SetTIK_RsaKey(keys_struct *keys, u8 *PrivateExp, u8 *PublicMod)
|
||||
{
|
||||
if(!keys) return -1;
|
||||
|
||||
Reference in New Issue
Block a user