From 65038633e4768a15e918f4eba093bbd6f470e55e Mon Sep 17 00:00:00 2001 From: applestash Date: Sat, 28 Jun 2014 18:38:52 +1000 Subject: [PATCH] many small changes added function to fill memory with random bytes (cleaning some code with that). Removed public build #ifdefs since 3dsguy apparently uploaded the entire source with all "private" things. --- makerom/Makefile | 4 ++-- makerom/accessdesc.c | 8 -------- makerom/cia.c | 9 +++------ makerom/crypto.c | 2 -- makerom/keyset.c | 4 ---- makerom/ncch.c | 2 +- makerom/ncsd.c | 24 +++--------------------- makerom/tik.c | 16 +++++++--------- makerom/tmd.c | 6 +++--- makerom/user_settings.c | 20 ++++++-------------- makerom/utils.c | 12 ++++++++++++ makerom/utils.h | 2 ++ 12 files changed, 39 insertions(+), 70 deletions(-) diff --git a/makerom/Makefile b/makerom/Makefile index 5c41704..79e3ead 100644 --- a/makerom/Makefile +++ b/makerom/Makefile @@ -19,9 +19,9 @@ CFLAGS = --std=c99 -Wall -Wno-unused-but-set-variable -Wno-unused-value -I. -DMA CC = gcc # MAKEROM Build Settings -MAKEROM_BUILD_FLAGS = #-DPUBLIC_BUILD #-DDEBUG +MAKEROM_BUILD_FLAGS = #-DDEBUG VER_MAJOR = 0 -VER_MINOR = 8 +VER_MINOR = 9 OUTPUT = makerom main: build diff --git a/makerom/accessdesc.c b/makerom/accessdesc.c index 7247ab6..8c316c5 100644 --- a/makerom/accessdesc.c +++ b/makerom/accessdesc.c @@ -6,10 +6,8 @@ #include "polarssl/base64.h" #include "desc_presets.h" -#ifndef PUBLIC_BUILD #include "desc_dev_sigdata.h" #include "desc_prod_sigdata.h" -#endif const int RSF_RSA_DATA_LEN = 344; const int RSF_DESC_DATA_LEN = 684; @@ -19,9 +17,7 @@ int accessdesc_SignWithKey(exheader_settings *exhdrset); int accessdesc_GetSignFromRsf(exheader_settings *exhdrset); int accessdesc_GetSignFromPreset(exheader_settings *exhdrset); void accessdesc_GetPresetData(u8 **desc, u8 **accessDesc, u8 **depList, keys_struct *keys); -#ifndef PUBLIC_BUILD void accessdesc_GetPresetSigData(u8 **accessDescSig, u8 **cxiPubk, u8 **cxiPvtk, keys_struct *keys); -#endif bool IsValidB64Char(char chr); u32 b64_strlen(char *str); @@ -153,9 +149,7 @@ int accessdesc_GetSignFromPreset(exheader_settings *exhdrset) u8 *cxiPvtk = NULL; accessdesc_GetPresetData(&desc,&accessDesc,&depList,exhdrset->keys); -#ifndef PUBLIC_BUILD accessdesc_GetPresetSigData(&accessDescSig,&cxiPubk,&cxiPvtk,exhdrset->keys); -#endif // Error Checking if(!desc || !depList){ @@ -307,7 +301,6 @@ void accessdesc_GetPresetData(u8 **desc, u8 **accessDesc, u8 **depList, keys_str } } -#ifndef PUBLIC_BUILD void accessdesc_GetPresetSigData(u8 **accessDescSig, u8 **cxiPubk, u8 **cxiPvtk, keys_struct *keys) { if(keys->accessDescSign.presetType == desc_preset_APP){ @@ -459,7 +452,6 @@ void accessdesc_GetPresetSigData(u8 **accessDescSig, u8 **cxiPubk, u8 **cxiPvtk, } } } -#endif bool IsValidB64Char(char chr) { diff --git a/makerom/cia.c b/makerom/cia.c index 55967dc..7d53932 100644 --- a/makerom/cia.c +++ b/makerom/cia.c @@ -182,14 +182,11 @@ int GetSettingsFromUsrset(cia_settings *ciaset, user_settings *usrset) } // Ticket Data - u64_to_u8(ciaset->tik.ticketId,u64GetRand(),BE); + rndset(ciaset->tik.ticketId,16); if(usrset->cia.randomTitleKey) - { - u64_to_u8(ciaset->common.titleKey,u64GetRand(),BE); - u64_to_u8((ciaset->common.titleKey+8),u64GetRand(),BE); - } + rndset(ciaset->common.titleKey,16); else - memset(ciaset->common.titleKey,0,16); + clrmem(ciaset->common.titleKey,16); ciaset->tik.formatVersion = 1; diff --git a/makerom/crypto.c b/makerom/crypto.c index e3cdf46..fc482e0 100644 --- a/makerom/crypto.c +++ b/makerom/crypto.c @@ -15,13 +15,11 @@ u8* AesKeyScrambler(u8 *Key, u8 *KeyX, u8 *KeyY) for(int i = 0; i < 16; i++) Key[i] = KeyX[i] ^ ((KeyY[i] >> 2) | ((KeyY[i < 15 ? i+1 : 0] & 3) << 6)); -#ifndef PUBLIC_BUILD 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]; -#endif return Key; } diff --git a/makerom/keyset.c b/makerom/keyset.c index 38e5cf8..b44ecb6 100644 --- a/makerom/keyset.c +++ b/makerom/keyset.c @@ -2,10 +2,8 @@ // KeyData #include "tpki.h" // Test PKI -#ifndef PUBLIC_BUILD #include "ppki.h" // Production PKI #include "dpki.h" // Development PKI -#endif // Private Prototypes int SetRsaKeySet(u8 **PrivDest, u8 *PrivSource, u8 **PubDest, u8 *PubSource); @@ -88,7 +86,6 @@ int LoadKeysFromResources(keys_struct *keys) /* RSA Keys */ keys->rsa.isFalseSign = true; } - #ifndef PUBLIC_BUILD else if(keys->keyset == pki_DEVELOPMENT){ keys->keysetLoaded = true; /* AES Keys */ @@ -154,7 +151,6 @@ int LoadKeysFromResources(keys_struct *keys) SetTikCert(keys,(u8*)xsC_ppki_cert); SetTmdCert(keys,(u8*)cpB_ppki_cert); } -#endif return 0; } diff --git a/makerom/ncch.c b/makerom/ncch.c index 231451d..cc53630 100644 --- a/makerom/ncch.c +++ b/makerom/ncch.c @@ -64,7 +64,7 @@ int build_NCCH(user_settings *usrset) int result; // Init Settings\n"); - ncch_settings *ncchset = malloc(sizeof(ncch_settings)); + ncch_settings *ncchset = calloc(1,sizeof(ncch_settings)); if(!ncchset) { fprintf(stderr,"[NCCH ERROR] Not enough memory\n"); return MEM_ERROR; diff --git a/makerom/ncsd.c b/makerom/ncsd.c index 34aac36..f8ee18e 100644 --- a/makerom/ncsd.c +++ b/makerom/ncsd.c @@ -367,34 +367,16 @@ int GetDataFromContent0(cci_settings *cciset, user_settings *usrset) memcpy(cciset->header.mediaId,hdr->titleId,8); memcpy(&cciset->content.titleId[0],hdr->titleId,8); -#ifndef PUBLIC_BUILD if(usrset->cci.useSDKStockData){ memcpy(cciset->cardinfo.initialData,stock_initial_data,0x30); memcpy(cciset->cardinfo.titleKey,stock_title_key,0x10); cciset->option.useDevCardInfo = true; } else{ - for(int i = 0; i < 0x2c/sizeof(u32); i++) - { - u32 val = u32GetRand(); - memcpy((cciset->cardinfo.initialData+i*sizeof(u32)),&val,4); - } - /* - for(int i = 0; i < 2; i++) - { - u64 val = u64GetRand(); - memcpy((cciset->cardinfo.titleKey+i*8),&val,8); - } - cciset->option.useDevCardInfo = true; - */ + rndset(cciset->cardinfo.initialData,0x2c); + //rndset(cciset->cardinfo.titleKey,0x10); + //cciset->option.useDevCardInfo = true; } -#else - for(int i = 0; i < 0x2c/sizeof(u32); i++) - { - u32 val = u32GetRand(); - memcpy((cciset->cardinfo.initialData+i*sizeof(u32)),&val,4); - } -#endif cciset->header.flags[MediaUnitSize] = hdr->flags[ContentUnitSize]; cciset->option.mediaUnit = GetNCCH_MediaUnitSize(hdr); diff --git a/makerom/tik.c b/makerom/tik.c index 5d47d5e..ea312f0 100644 --- a/makerom/tik.c +++ b/makerom/tik.c @@ -39,7 +39,7 @@ int SetupTicketBuffer(buffer_struct *tik) int SetupTicketHeader(tik_hdr *hdr, cia_settings *ciaset) { - memset(hdr,0,sizeof(tik_hdr)); + clrmem(hdr,sizeof(tik_hdr)); memcpy(hdr->issuer,ciaset->tik.issuer,0x40); hdr->formatVersion = ciaset->tik.formatVersion; @@ -47,10 +47,8 @@ int SetupTicketHeader(tik_hdr *hdr, cia_settings *ciaset) hdr->signerCrlVersion = ciaset->cert.signerCrlVersion; if(ciaset->content.encryptCia) CryptTitleKey(hdr->encryptedTitleKey, ciaset->common.titleKey,ciaset->common.titleId,ciaset->keys,ENC); - else{ - u64_to_u8(hdr->encryptedTitleKey,u64GetRand(),BE); - u64_to_u8((hdr->encryptedTitleKey+8),u64GetRand(),BE); - } + else + rndset(hdr->encryptedTitleKey,16); memcpy(hdr->ticketId,ciaset->tik.ticketId,8); memcpy(hdr->deviceId,ciaset->tik.deviceId,8); memcpy(hdr->titleId,ciaset->common.titleId,8); @@ -66,7 +64,7 @@ int SetupTicketHeader(tik_hdr *hdr, cia_settings *ciaset) int SignTicketHeader(tik_hdr *hdr, tik_signature *sig, keys_struct *keys) { - memset(sig,0,sizeof(tik_signature)); + clrmem(sig,sizeof(tik_signature)); u32_to_u8(sig->sigType,RSA_2048_SHA256,BE); return ctr_sig((u8*)hdr,sizeof(tik_hdr),sig->data,keys->rsa.xsPub,keys->rsa.xsPvt,RSA_2048_SHA256,CTR_RSA_SIGN); } @@ -80,7 +78,7 @@ int CryptTitleKey(u8 *EncTitleKey, u8 *DecTitleKey, u8 *TitleID, keys_struct *ke //Setting up Aes Context ctr_aes_context ctx; - memset(&ctx,0x0,sizeof(ctr_aes_context)); + clrmem(&ctx,sizeof(ctr_aes_context)); //Crypting TitleKey ctr_init_aes_cbc(&ctx,keys->aes.commonKey[keys->aes.currentCommonKey],iv,mode); @@ -91,12 +89,12 @@ int CryptTitleKey(u8 *EncTitleKey, u8 *DecTitleKey, u8 *TitleID, keys_struct *ke return 0; } -void SetLimits(tik_hdr *hdr, cia_settings *ciaset) +void SetLimits(tik_hdr *hdr, cia_settings *ciaset) // TODO? { memset(hdr->limits,0,0x40); } -void SetContentIndexData(tik_hdr *hdr, cia_settings *ciaset) +void SetContentIndexData(tik_hdr *hdr, cia_settings *ciaset) // TODO? { memset(hdr->contentIndex,0,0xAC); memcpy(hdr->contentIndex,default_contentIndex,0x30); diff --git a/makerom/tmd.c b/makerom/tmd.c index 744fe05..173c1e4 100644 --- a/makerom/tmd.c +++ b/makerom/tmd.c @@ -48,7 +48,7 @@ int SetupTMDBuffer(buffer_struct *tmd) int SetupTMDHeader(tmd_hdr *hdr, tmd_content_info_record *info_record, cia_settings *ciaset) { - memset(hdr,0,sizeof(tmd_hdr)); + clrmem(hdr,sizeof(tmd_hdr)); memcpy(hdr->issuer,ciaset->tmd.issuer,0x40); hdr->formatVersion = ciaset->tmd.formatVersion; @@ -67,14 +67,14 @@ int SetupTMDHeader(tmd_hdr *hdr, tmd_content_info_record *info_record, cia_setti int SignTMDHeader(tmd_hdr *hdr, tmd_signature *sig, keys_struct *keys) { - memset(sig,0,sizeof(tmd_signature)); + clrmem(sig,sizeof(tmd_signature)); u32_to_u8(sig->sigType,RSA_2048_SHA256,BE); return ctr_sig((u8*)hdr,sizeof(tmd_hdr),sig->data,keys->rsa.cpPub,keys->rsa.cpPvt,RSA_2048_SHA256,CTR_RSA_SIGN); } int SetupTMDInfoRecord(tmd_content_info_record *info_record, u8 *content_record, u16 ContentCount) { - memset(info_record,0x0,sizeof(tmd_content_info_record)*0x40); + clrmem(info_record,sizeof(tmd_content_info_record)*0x40); u16_to_u8(info_record->contentIndexOffset,0x0,BE); u16_to_u8(info_record->contentCommandCount,ContentCount,BE); ctr_sha(content_record,sizeof(tmd_content_chunk)*ContentCount,info_record->contentChunkHash,CTR_SHA_256); diff --git a/makerom/user_settings.c b/makerom/user_settings.c index 9ec263e..1376293 100644 --- a/makerom/user_settings.c +++ b/makerom/user_settings.c @@ -29,12 +29,11 @@ int ParseArgs(int argc, char *argv[], user_settings *usr_settings) } // Allocating Memory for Content Path Ptrs - usr_settings->common.contentPath = malloc(CIA_MAX_CONTENT*sizeof(char*)); + usr_settings->common.contentPath = calloc(CIA_MAX_CONTENT,sizeof(char*)); if(usr_settings->common.contentPath == NULL){ fprintf(stderr,"[SETTING ERROR] Not Enough Memory\n"); return USR_MEM_ERROR; } - memset(usr_settings->common.contentPath,0,CIA_MAX_CONTENT*sizeof(char*)); // Initialise Keys InitKeys(&usr_settings->common.keys); @@ -81,7 +80,7 @@ int ParseArgs(int argc, char *argv[], user_settings *usr_settings) else if(usr_settings->common.workingFileType == infile_ncsd || usr_settings->common.workingFileType == infile_srl) source_path = usr_settings->common.workingFilePath; else source_path = usr_settings->common.contentPath[0]; u16 outfile_len = strlen(source_path) + 3; - usr_settings->common.outFileName = malloc(outfile_len); + usr_settings->common.outFileName = calloc(outfile_len,sizeof(char)); if(!usr_settings->common.outFileName){ fprintf(stderr,"[SETTING ERROR] Not Enough Memory\n"); return USR_MEM_ERROR; @@ -270,8 +269,7 @@ int SetArgument(int argc, int i, char *argv[], user_settings *set) } u32 app_type_len = (u32)(tmp2-tmp); - char *app_type = malloc(app_type_len+1); - memset(app_type,0,app_type_len+1); + char *app_type = calloc(app_type_len+1,sizeof(char)); memcpy(app_type,tmp,app_type_len); if(strcasecmp(app_type,"App") == 0 || strcasecmp(app_type,"SDApp") == 0) set->common.keys.accessDescSign.presetType = desc_preset_APP; @@ -366,7 +364,6 @@ int SetArgument(int argc, int i, char *argv[], user_settings *set) return 2; } // Cci Options -#ifndef PUBLIC_BUILD else if(strcmp(argv[i],"-devcardcci") == 0){ if(ParamNum){ PrintNoNeedParam("-devcardcci"); @@ -375,7 +372,6 @@ int SetArgument(int argc, int i, char *argv[], user_settings *set) set->cci.useSDKStockData = true; return 1; } -#endif else if(strcmp(argv[i],"-nomodtid") == 0){ if(ParamNum){ PrintNoNeedParam("-nomodtid"); @@ -958,15 +954,12 @@ void PrintNoNeedParam(char *arg) void DisplayHelp(char *app_name) { printf("CTR MAKEROM %d.%d",MAKEROM_VER_MAJOR,MAKEROM_VER_MINOR); -#ifndef PUBLIC_BUILD - printf(" PRIVATE BUILD"); -#endif printf("\n(C) 3DSGuy 2014\n"); printf("Usage: %s [options... ]\n",app_name); printf("Option Parameter Explanation\n"); printf("GLOBAL OPTIONS:\n"); printf(" -help Display this text\n"); - printf(" -rsf RSF File\n"); + printf(" -rsf Rom Specification File (*.rsf)\n"); printf(" -f Output Format, defaults to 'cxi'\n"); //printf(" 'cxi' CTR Executable Image\n"); //printf(" 'cfa' CTR File Archive\n"); @@ -976,7 +969,8 @@ void DisplayHelp(char *app_name) //printf(" -v Verbose\n"); printf(" -DNAME=VALUE Substitute values in Spec files\n"); printf("KEY OPTIONS:\n"); - printf(" -target Target for crypto, defaults to 't'\n"); + //printf(" -target Target for crypto, defaults to 't'\n"); + printf(" -target Target for crypto, defaults to 't'\n"); printf(" 't' Test(false) Keys & prod Certs\n"); printf(" 'd' Development Keys & Certs\n"); printf(" 'p' Production Keys & Certs\n"); @@ -1005,9 +999,7 @@ void DisplayHelp(char *app_name) printf(" -romfs RomFS File\n"); printf("CCI OPTIONS:\n"); printf(" -content : Specify content files\n"); -#ifndef PUBLIC_BUILD printf(" -devcardcci Use SDK CardInfo Method\n"); -#endif printf(" -nomodtid Don't Modify Content TitleIDs\n"); printf(" -alignwr Align Writeable Region to the end of last NCCH\n"); printf(" -genupdatenote Create Update Partition Notes\n"); diff --git a/makerom/utils.c b/makerom/utils.c index 44dcdbb..45b509d 100644 --- a/makerom/utils.c +++ b/makerom/utils.c @@ -44,6 +44,18 @@ int CopyData(u8 **dest, u8 *source, u64 size) return 0; } +void rndset(void *ptr, u64 num) +{ + u8 *tmp = (u8*)ptr; + for(u64 i = 0; i < num ; i++) + tmp[i] = u8GetRand(); +} + +void clrmem(void *ptr, u64 num) +{ + memset(ptr,0,num); +} + // Misc u64 align(u64 value, u64 alignment) { diff --git a/makerom/utils.h b/makerom/utils.h index 930e31b..5d3f10a 100644 --- a/makerom/utils.h +++ b/makerom/utils.h @@ -10,6 +10,8 @@ typedef struct void char_to_u8_array(unsigned char destination[], char source[], int size, int endianness, int base); void endian_memcpy(u8 *destination, u8 *source, u32 size, int endianness); int CopyData(u8 **dest, u8 *source, u64 size); +void rndset(void *ptr, u64 num); +void clrmem(void *ptr, u64 num); // MISC u64 align(u64 value, u64 alignment);