From 6ebef3c42aee8b373cbb3f837b3fa66e8ba41917 Mon Sep 17 00:00:00 2001 From: xprism1 Date: Mon, 25 Jan 2021 17:26:22 +0800 Subject: [PATCH 1/4] Add support for custom decrypted TitleKey --- makerom/cia.c | 6 ++++++ makerom/tik.c | 2 +- makerom/user_settings.c | 9 +++++++++ makerom/user_settings.h | 1 + 4 files changed, 17 insertions(+), 1 deletion(-) diff --git a/makerom/cia.c b/makerom/cia.c index 492181a..fb9f178 100644 --- a/makerom/cia.c +++ b/makerom/cia.c @@ -184,6 +184,12 @@ int GetSettingsFromUsrset(cia_settings *ciaset, user_settings *usrset) if(usrset->cia.randomTitleKey) rndset(ciaset->common.titleKey,AES_128_KEY_SIZE); + else if(usrset->cia.titleKey){ + for (size_t count = 0; count < sizeof(ciaset->common.titleKey)/sizeof(ciaset->common.titleKey[0]); count++) { + sscanf(usrset->cia.titleKey, "%2hhx", &ciaset->common.titleKey[count]); + usrset->cia.titleKey += 2; + } + } else clrmem(ciaset->common.titleKey,AES_128_KEY_SIZE); diff --git a/makerom/tik.c b/makerom/tik.c index df95599..f28f2e5 100644 --- a/makerom/tik.c +++ b/makerom/tik.c @@ -65,7 +65,7 @@ void SetupTicketHeader(tik_hdr *hdr, cia_settings *ciaset) SetLimits(hdr,ciaset); // Crypt TitleKey - if(ciaset->content.encryptCia) + if(ciaset->content.encryptCia || ciaset->common.titleKey) CryptTitleKey(ciaset->common.titleKey, hdr->encryptedTitleKey, hdr->titleId, ciaset->keys, ENC); else rndset(hdr->encryptedTitleKey,AES_128_KEY_SIZE); diff --git a/makerom/user_settings.c b/makerom/user_settings.c index 3e498b6..e5e07e9 100644 --- a/makerom/user_settings.c +++ b/makerom/user_settings.c @@ -559,6 +559,14 @@ int SetArgument(int argc, int i, char *argv[], user_settings *set) set->cia.randomTitleKey = true; return 1; } + else if (strcmp(argv[i], "-titlekey") == 0) { + if (ParamNum != 1) { + PrintArgReqParam(argv[i], 1); + return USR_ARG_REQ_PARAM; + } + set->cia.titleKey = argv[i + 1]; + return 2; + } else if (strcmp(argv[i], "-dlc") == 0) { if (ParamNum) { PrintNoNeedParam(argv[i]); @@ -990,6 +998,7 @@ void DisplayExtendedHelp(char *app_name) printf(" -dver Data-title version\n"); printf(" -deviceid 3DS unique device ID\n"); printf(" -esaccid e-Shop account ID\n"); + printf(" -titlekey Specify decrypted title key\n"); printf(" -rand Use a random title key\n"); printf(" -dlc Create DLC CIA\n"); printf(" -srl Package a TWL SRL in a CIA\n"); diff --git a/makerom/user_settings.h b/makerom/user_settings.h index ba6af79..e09ec50 100644 --- a/makerom/user_settings.h +++ b/makerom/user_settings.h @@ -284,6 +284,7 @@ typedef struct struct{ bool randomTitleKey; + char *titleKey; bool encryptCia; bool DlcContent; bool includeUpdateNcch; From 928d9151965df067103b23507690e410d8735ae3 Mon Sep 17 00:00:00 2001 From: xprism1 Date: Wed, 27 Jan 2021 02:09:39 +0800 Subject: [PATCH 2/4] Respect content.encryptCia --- makerom/tik.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/makerom/tik.c b/makerom/tik.c index f28f2e5..df95599 100644 --- a/makerom/tik.c +++ b/makerom/tik.c @@ -65,7 +65,7 @@ void SetupTicketHeader(tik_hdr *hdr, cia_settings *ciaset) SetLimits(hdr,ciaset); // Crypt TitleKey - if(ciaset->content.encryptCia || ciaset->common.titleKey) + if(ciaset->content.encryptCia) CryptTitleKey(ciaset->common.titleKey, hdr->encryptedTitleKey, hdr->titleId, ciaset->keys, ENC); else rndset(hdr->encryptedTitleKey,AES_128_KEY_SIZE); From 24149fcf6e52fd74ead01c457e55e79109c18207 Mon Sep 17 00:00:00 2001 From: xprism1 Date: Wed, 27 Jan 2021 02:12:47 +0800 Subject: [PATCH 3/4] Override content.encryptCia when titlekey specified --- makerom/cia.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/makerom/cia.c b/makerom/cia.c index fb9f178..00c7bdd 100644 --- a/makerom/cia.c +++ b/makerom/cia.c @@ -160,7 +160,7 @@ int GetSettingsFromUsrset(cia_settings *ciaset, user_settings *usrset) ciaset->verbose = usrset->common.verbose; ciaset->tmd.titleType = TYPE_CTR; - ciaset->content.encryptCia = usrset->common.rsfSet.Option.EnableCrypt; + ciaset->content.encryptCia = usrset->common.rsfSet.Option.EnableCrypt || usrset->cia.titleKey != NULL; ciaset->content.IsDlc = usrset->cia.DlcContent; if(ciaset->keys->aes.commonKey[ciaset->keys->aes.currentCommonKey] == NULL && ciaset->content.encryptCia){ fprintf(stderr,"[CIA WARNING] Common Key could not be loaded, CIA will not be encrypted\n"); @@ -185,10 +185,10 @@ int GetSettingsFromUsrset(cia_settings *ciaset, user_settings *usrset) if(usrset->cia.randomTitleKey) rndset(ciaset->common.titleKey,AES_128_KEY_SIZE); else if(usrset->cia.titleKey){ - for (size_t count = 0; count < sizeof(ciaset->common.titleKey)/sizeof(ciaset->common.titleKey[0]); count++) { - sscanf(usrset->cia.titleKey, "%2hhx", &ciaset->common.titleKey[count]); - usrset->cia.titleKey += 2; - } + for (size_t count = 0; count < sizeof(ciaset->common.titleKey)/sizeof(ciaset->common.titleKey[0]); count++) { + sscanf(usrset->cia.titleKey, "%2hhx", &ciaset->common.titleKey[count]); + usrset->cia.titleKey += 2; + } } else clrmem(ciaset->common.titleKey,AES_128_KEY_SIZE); From 73aeef12b8b977bdda43b7f67b0dcca9a0cea068 Mon Sep 17 00:00:00 2001 From: xprism1 Date: Wed, 27 Jan 2021 02:19:55 +0800 Subject: [PATCH 4/4] Override content.encryptCia when titlekey specified --- makerom/user_settings.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/makerom/user_settings.c b/makerom/user_settings.c index e5e07e9..3583e9c 100644 --- a/makerom/user_settings.c +++ b/makerom/user_settings.c @@ -564,7 +564,7 @@ int SetArgument(int argc, int i, char *argv[], user_settings *set) PrintArgReqParam(argv[i], 1); return USR_ARG_REQ_PARAM; } - set->cia.titleKey = argv[i + 1]; + set->cia.titleKey = argv[i + 1]; return 2; } else if (strcmp(argv[i], "-dlc") == 0) { @@ -998,7 +998,7 @@ void DisplayExtendedHelp(char *app_name) printf(" -dver Data-title version\n"); printf(" -deviceid 3DS unique device ID\n"); printf(" -esaccid e-Shop account ID\n"); - printf(" -titlekey Specify decrypted title key\n"); + printf(" -titlekey Specify decrypted title key (Overrides \"EnableCrypt\" in RSF to true)\n"); printf(" -rand Use a random title key\n"); printf(" -dlc Create DLC CIA\n"); printf(" -srl Package a TWL SRL in a CIA\n");