Skip to content

Commit

Permalink
support remove ext key
Browse files Browse the repository at this point in the history
  • Loading branch information
dnasdw committed Jun 3, 2018
1 parent 980f7c3 commit 5fff844
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 3 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ if(APPLE)
set(CMAKE_MACOSX_RPATH 1)
endif()
set(_3DSTOOL_MAJOR 1)
set(_3DSTOOL_MINOR 1)
set(_3DSTOOL_PATCHLEVEL 1)
set(_3DSTOOL_MINOR 2)
set(_3DSTOOL_PATCHLEVEL 0)
if(NOT MSVC_IDE AND NOT XCODE_VERSION AND NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build, options are: None(CMAKE_CXX_FLAGS or CMAKE_C_FLAGS used) Debug Release RelWithDebInfo MinSizeRel." FORCE)
endif()
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ An all-in-one tool for extracting/creating 3ds roms.

## History

- v1.2.0 @ 2018.06.03 - Support auto encryption fully

### v1.1

- v1.1.0 @ 2018.01.03 - A new beginning
- v1.1.1 @ 2018.01.21 - Lock region and language

Expand Down
67 changes: 67 additions & 0 deletions bin/ext_key.txt
Original file line number Diff line number Diff line change
Expand Up @@ -315,16 +315,22 @@
0004000000172200 15670CFDA0ADD5AD546BD2833408ED35
0004000000172300 DA4E557CCA48624A47A2D6483A93D647
0004000000172400 8C7516A5B61B9B03FB2DD6052E51DC76
0004000000172500 6080AE21B45B5A401E1146BAA035B79F
0004000000172600 731523C78F32373508720FEAA74E3487
0004000000172700 66F4F2CD473FE0B9336A090FE1A2B945
0004000000172800 7B270C442AC235AA740060AE98FFB8D5
0004000000172900 F06644F00E5E1BAE46EA54177C8E73BB
0004000000172A00 5AE87C08915B13AF9E489D46305858BA
0004000000172B00 A27CD6E6E4A9929D1AA1363C05EF1B18
0004000000172C00 D5A987245CF8EE8AAFE41DE0447873E3
0004000000172D00 BF718D4B2B062297AF6C00432E5CE348
0004000000172E00 925559ECC4A9984DFCA5D57562443F5A
0004000000172F00 340D3E491280F67755218CEF5B906E7C
0004000000173000 E04FFEA75C5470E784A755B86D753238
0004000000173100 A9829FCCF044533C69B4E3BDEC617BA7
0004000000173200 CC2D67748163A8E4FFB003DBA7281374
0004000000173300 7A71D10C1DDFB9586CCF4CB6E48EFA8E
0004000000173400 D99A6C9E52F65C46521ACE5FF8EAEDC7
0004000000173500 A38288F3E7804FDE5469EEBD31AEDFCB
0004000000173600 6C6E759AA637D97C11F60A5B2F2C79AB
0004000000173700 3573CEDA93A923E411132B61C71500A8
Expand Down Expand Up @@ -835,6 +841,7 @@
000400000019FE00 9D2C29ECBCEE3B3D8A323E325E46F41C
000400000019FF00 25579E16E7E629E2490F5A29BD776EC5
00040000001A0000 20B4985FC45B5B8BA526CAF68A68540E
00040000001A0200 1D146EE1D5A0FE8F83DC43812BEA7D82
00040000001A0300 8071E2D4B9F80529EABE54DDEC627E52
00040000001A0400 A5D496F4EA8EF5A5A76D50237A7F6B58
00040000001A0500 4A3B1A299CE59BEECB8B52E1342C173F
Expand Down Expand Up @@ -1082,6 +1089,7 @@
00040000001B2800 1D665D86B6F6C787CEC6C743F0B21E4A
00040000001B2900 E103A4F7441792EA5E5480AD42708FD1
00040000001B2B00 D0A58D4E1941B87F54526223C62F24F6
00040000001B2D00 29D32BD04B4BAE73735AA16FFF5092DF
00040000001B3200 9874BC13DF0FEE3850428C9C982398C2
00040000001B3300 EAB53133066B741A6EACA991C625EA06
00040000001B3400 9CD2F0D35B91936D5337A762932728B4
Expand Down Expand Up @@ -1112,6 +1120,7 @@
00040000001B4F00 E596476C644A1CEF6A36672BB7F71861
00040000001B5000 8CD3805938CE9AE778DE50BC5C84826E
00040000001B5100 6823C354071519F380C0A72B2CD0FCE7
00040000001B5200 1A49B79CA8066A0C7D00B95D4461A4A2
00040000001B5300 9866DBA69AE7BC584EC633D934E8432A
00040000001B5400 47C083301482CE67C462AED6495FA44B
00040000001B5500 EF78AADC44F951FDD0F75CE45AA9FFD6
Expand Down Expand Up @@ -1193,6 +1202,7 @@
00040000001BBB00 BAAA1A16ED82FB5640DB9743588F6C1B
00040000001BBC00 5D75A882911C992BCC0642D4FB7888E6
00040000001BBD00 1B39A55DE707302CA716604C1E720312
00040000001BBF00 56D4EB1C5C45187E51B7FCC4282D6255
00040000001BC100 CA2A9E505D5ED8ABBDE2D91E42A338B3
00040000001BC200 A021E40EC4803D14C37E683FDB73AE45
00040000001BC300 6BE783862DE044CCEDB97A4D19CF2E56
Expand Down Expand Up @@ -1227,6 +1237,7 @@
00040000001BEF00 DF79FDD4650EFF666D68C485C742C6AC
00040000001BF000 081AE7763D4DF3A708CCC953C5BBE060
00040000001BF100 E22A40FAB4CFEEE775DBAF41D4B73ABE
00040000001BF200 9CF03762B6CD4C08FF7BE93B55CD13B1
00040000001BF300 6918C9E48CC3B7A382854B0C6EF80CAA
00040000001BF400 B540AB667EB5401C6658B10DC1AA8ABA
00040000001BF500 0044A7A320DDA93BD358522954988BDA
Expand Down Expand Up @@ -1258,6 +1269,8 @@
00040000001C1800 3D6FD0FCCF1503A5B023D5ACACE0B310
00040000001C1900 19B9AEE5B914F9ECAE44B3509CA3C822
00040000001C1A00 841B2AC0890599A9960FAA3049BDF461
00040000001C1D00 AB0C003D83D1B366B59F47F17963594D
00040000001C1E00 1C7FD9DFA01D8CB27CC4C2A78F9708E2
00040000001C1F00 0680EBFB1C9F18F25C25BBAC2B2E3C50
00040000001C2000 967411DEA911D5E20C823C401D23E0A9
00040000001C2100 006BC9B7D841D22F19E3A1417478831C
Expand Down Expand Up @@ -1288,9 +1301,11 @@
00040000001C4600 5518216EADE2C941C0DA9BEBA475A3DC
00040000001C4700 02439B9CB58887B32740552525CB6BCE
00040000001C4800 A659B95593D7FE4ECE43B1A6A728B476
00040000001C4900 850A323AE47F1E85C0D08782132D997C
00040000001C4A00 22D333CE4418BE51D90834BA42CF572D
00040000001C4D00 E621D284D01DFD84D03C632415F35735
00040000001C4E00 23183C9BCA471EE10F83A1A9C77EBA03
00040000001C4F00 FDB955B9F74D57FE00C38FEB7B3C770A
00040000001C5000 B908F1D55D62718C3093900CCD8294A5
00040000001C5100 92DCBD85F1F83B52BA1BF281992B3278
00040000001C5200 6E7E08FF45671BB67418D31FD8A8EF29
Expand Down Expand Up @@ -1320,6 +1335,8 @@
00040000001C8F00 F989E5CC074D7F7448688884F7909E7A
00040000001C9200 52C38F38E4559DF9F6B51A64F64FFD2A
00040000001C9400 DCD34B18A5E570F88B0CB056BB1253C5
00040000001C9500 3C148AFB166328B11314FEDB36382C31
00040000001C9600 879347C2F465F8118643A6B2184BC87C
00040000001C9700 C5E89BA9DA5ADD58BC0BEE772D8BD4C8
00040000001C9800 43B7BFE5F180E74836DA181F96932146
00040000001C9900 9FA6D7538F0FBFBB66D978E16A7799D1
Expand All @@ -1328,8 +1345,12 @@
00040000001C9C00 91D821E7C538FF6AABB5AA0B6C64F7A0
00040000001C9D00 863FEB091B35D8DD7A158ACEBDF12CEE
00040000001C9F00 DAF122F1D3B976E26C023A5CDCDBFEE0
00040000001CA000 7792718ECA6439E1FBA7E295C2F7B241
00040000001CA100 A188F1E33E0E40E22961D997BEA3F4D4
00040000001CA200 D3A31A447A81425C4308D2D99310E502
00040000001CA400 53D071EFCB252B0C246765A17498E458
00040000001CA800 BC766C1AAA7AD881F3705B894240D727
00040000001CAA00 8FAE83DA2D9C9569F27603EB5272103E
00040000001CAB00 28F9D733E828EC064934EC36FACE3D07
00040000001CAC00 3753BB5D4CF2B52DE87899C76C7B204C
00040000001CAD00 DFF0981B156D63BD1BFB52475C4A7BED
Expand All @@ -1348,22 +1369,54 @@
00040000001CC800 4940C5049F234584F7A5D2BA91C9ADC4
00040000001CC900 5B65547021EC988566F139EAE35001BC
00040000001CCB00 33356DA81ACA6F75D3CFC1CFBA252F95
00040000001CCD00 23DECE8B6B1E9F47EC0F4796E6C03E95
00040000001CCF00 C8692441573B560E01377A1EC6776B02
00040000001CD000 997EC3A0B18C3D6B332FAFD58E54817C
00040000001CD200 7B61A6C8F89E191B0F9EF87B61652685
00040000001CD300 B9BEC473A1BA15E4E030373652D693DC
00040000001CD500 02625F777DE1B8D8DF573D4FCA70E0C6
00040000001CD600 54112CEB304A18A8AB6088AD17E074E5
00040000001CD900 B71BB287BAE85D3620F422EC6D694E15
00040000001CDD00 E9BDC86CAE90B6BC9AFAD809FDF44BEF
00040000001CDE00 6563EB16B265D6F6237C2B4F5234C321
00040000001CE000 B9C2D4CFDD33FEC7A65A5BC474A7AE1D
00040000001CE100 71D654EAA1A6B322D23D36B8A91A4201
00040000001CE800 BA12D61C0947FE59A22CC94EB32D3B5A
00040000001CF300 36DFFFEE9A7DFCAC75384E1E6A8C84A2
00040000001CF400 92B2CAD88DD7FC96A71115B1FFB4CED4
00040000001CF500 E484CE8576A4B3F73EB5879623792866
00040000001CF700 5972355F05C9CEE372BF96A07841D007
00040000001CF800 67429904B28123065ABA9059C8460AC7
00040000001CFA00 92C3F0AB231E74CB46DA6E26B3886101
00040000001CFD00 3277ABFFD9C42B92958922EEF9D00FB5
00040000001CFE00 AECD278FE083790BF65E664EB3814BE5
00040000001CFF00 A90D0A44E38655B4D0C26F998ED9345D
00040000001D0000 DC6004811D06197A9FFFE4F4C7AD0FA5
00040000001D0100 7EE75B90E2BCA96FC55E54D50FABFBFD
00040000001D0300 86477FDF105F8B31D157BBA50AD6B92D
00040000001D0400 77EC6CAB68491322D125D171BAC36C67
00040000001D0900 E9D344283411C3C4EE702EBF98DB6F85
00040000001D0C00 9ACF1243D40F4E79C75F3CC0B7254736
00040000001D0E00 208312DE6B0ABFD40E22CE974A285D1D
00040000001D0F00 532E9503CF50506843B4C76836CB0D21
00040000001D1100 B561A912C0A3631CFA7FE5E8597690BD
00040000001D1300 09E9B2205D3DDFB05E9E440804B08E30
00040000001D1700 2B96FE16B49BDC862A17DFA0D449E43E
00040000001D1D00 A25E54024200B2CBB261FE47DE3A92D6
00040000001D2100 B0A550FACE9B99AFC9910A578C9C7865
00040000001D2300 46D91E9C044DD75B854703026B85E169
00040000001D2900 464AB26F2E87E980A5C7292B4E8E50DC
00040000001D2A00 5FB90283DE9DB528AC91BCA44428C355
00040000001D2B00 CE2F69DD4FCFFF021AE75FA77C52900D
00040000001D2C00 0CC8FD177DAA193A9043D43D305C61D8
00040000001D2F00 38F661D511341EFA239826B403385E83
00040000001D3000 F460868B270250808A56CC5E96485313
00040000001D3500 06157406D199D1CEF9CFD89F82660BD2
00040000001D3800 960160100ACA5C0E53E0CFDFCC021484
00040000001D3A00 55B883541350862ECEEAE64CA7A39EB8
00040000001D3C00 B7A0294B8712C2346438BFACD27B9D37
00040000001D3F00 1A82A36C84C2ECF1933D6156D7E3F333
00040000001D4000 80CA8BB3E11F631F01E7540AA3454CAE
000400000F700400 0D65CBD40F2728586CA4DAE3B8681313
000400000F700700 C6EE84F157E15E8B7E716090D2ED5DE4
000400000F700800 9A83F7FEB62E8D9AACB5AA7761A00590
Expand Down Expand Up @@ -1540,6 +1593,7 @@
000400000F70D700 DD261F8A373E3C07A4274A028B4C3F00
000400000F70D900 179C70E1A8615B5C53F6BDDBFEDDD299
000400000F70DB00 351B5E329DDC4DCFD624F04D1733FDDE
000400000F70DC00 A91507093B9B41EB9661FA5417511D20
000400000F70DE00 6BEFD48958D75C3AF4A1F2DD96E41B41
000400000F70E000 813C66B7C3629266F41835E4E03B4639
000400000F70E100 6D864FAB9ABED9608A84FCB09D53AA53
Expand All @@ -1554,13 +1608,26 @@
000400000F70EC00 89672B9654F4BBA88FDBD0D6F6434897
000400000F70ED00 1D720474DFE78CDAD141BBAFBA11165A
000400000F70F000 CBF11B36755CD1C72BD508D1FE2A2C07
000400000F70F300 ED696AF7E5613656B6113F8F59ADDAA5
000400000F70F400 7857DE45BDB276EB319164AB49AC311F
000400000F70F500 6DC21E5B81A851A393507A3E82A947E0
000400000F70F700 F98D224A16A96F0FCF8D808D3121AE9C
000400000F70F800 7DB693B233CFA6354224584F2B28A33A
000400000F70F900 5D9184484AB4C9211E388FD0281513C4
000400000F70FA00 6E939F87C4A1C496D91C11AC99805508
000400000F70FB00 D9F2FE442F94B131C2A9FB50BAD4D77B
000400000F70FC00 F4C6F3C47BEFFA84CD90637F361C1222
000400000F70FD00 CCA89CB11C74208B4631FECC5E3CD4E2
000400000F70FE00 F54AB07CF639670FB88002A7487EF2FA
000400000F710000 BDA5D1D434E8EA1CDBF903E30281DEBE
000400000F710100 3B114C0A85FB9FAAF33315B5B6ADA872
000400000F710200 B6EDC89C6F3C5D78FFF75E5B4A3928BC
000400000F710300 7C893F01EE88E6114ADF727F2157613B
000400000F710400 8F92C2BCA7D5801E36D8C160B4D76990
000400000F710500 E6253A2F3E1789A82D54E4CD9CC93F0F
000400000F710600 7DF7D88BBE3938303682FE47614B1866
000400000F710900 204613AD9F10E857A75E09C2681C279D
000400000F710A00 BE305B9D3A68CD6B3F9A88DC1E683710
000400000F711000 9919326A5585EC0543108A0BBCB6510E
000400000F711400 05ECFDAF4523E89731D6C7DAF8F2187E
000400000FF3E300 AA9B98FB66563D5E847A619E4D03DF15
21 changes: 20 additions & 1 deletion src/3dstool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ C3dsTool::SOption C3dsTool::s_Option[] =
{ nullptr, 0, USTR(" cci/cxi/cfa/exefs:") },
{ USTR("header"), 0, USTR("the header file of the target file") },
{ nullptr, 0, USTR(" create:") },
{ nullptr, 0, USTR(" cxi/cfa:") },
{ nullptr, 0, USTR(" cxi:") },
{ USTR("not-remove-ext-key"), 0, USTR("not remove ext key") },
{ nullptr, 0, USTR(" cfa:") },
{ USTR("not-encrypt"), 0, USTR("not encrypt, modify the flag only") },
{ USTR("fixed-key"), 0, USTR("AES-CTR encryption using fixedkey") },
{ nullptr, 0, USTR(" extract:") },
Expand Down Expand Up @@ -110,6 +112,7 @@ C3dsTool::C3dsTool()
, m_eFileType(kFileTypeUnknown)
, m_bVerbose(false)
, m_nEncryptMode(CNcch::kEncryptModeNone)
, m_bRemoveExtKey(true)
, m_bDev(false)
, m_nCompressAlign(1)
, m_eCompressType(kCompressTypeNone)
Expand Down Expand Up @@ -796,6 +799,18 @@ C3dsTool::EParseOptionReturn C3dsTool::parseOptions(const UChar* a_pName, int& a
}
m_sHeaderFileName = a_pArgv[++a_nIndex];
}
else if (UCscmp(a_pName, USTR("not-remove-ext-key")) == 0)
{
if (m_nEncryptMode == CNcch::kEncryptModeNone)
{
m_nEncryptMode = CNcch::kEncryptModeAuto;
}
else if (m_nEncryptMode != CNcch::kEncryptModeAuto)
{
return kParseOptionReturnOptionConflict;
}
m_bRemoveExtKey = false;
}
else if (UCscmp(a_pName, USTR("not-encrypt")) == 0)
{
if (m_nEncryptMode == CNcch::kEncryptModeNone)
Expand Down Expand Up @@ -1406,6 +1421,7 @@ bool C3dsTool::createFile()
ncch.SetVerbose(m_bVerbose);
ncch.SetHeaderFileName(m_sHeaderFileName);
ncch.SetEncryptMode(m_nEncryptMode);
ncch.SetRemoveExtKey(m_bRemoveExtKey);
ncch.SetDev(m_bDev);
ncch.SetExtendedHeaderFileName(m_sExtendedHeaderFileName);
ncch.SetLogoRegionFileName(m_sLogoRegionFileName);
Expand All @@ -1422,6 +1438,7 @@ bool C3dsTool::createFile()
ncch.SetVerbose(m_bVerbose);
ncch.SetHeaderFileName(m_sHeaderFileName);
ncch.SetEncryptMode(m_nEncryptMode);
ncch.SetRemoveExtKey(false);
ncch.SetDev(m_bDev);
ncch.SetExeFsFileName(m_sExeFsFileName);
ncch.SetRomFsFileName(m_sRomFsFileName);
Expand Down Expand Up @@ -1750,6 +1767,8 @@ int C3dsTool::sample()
UPrintf(USTR("3dstool -cvtf cxi 0.cxi --header ncchheader.bin --exh exh.bin --logo logo.darc.lz --plain plain.bin --exefs exefs.bin --romfs romfs.bin --fixed-key\n\n"));
UPrintf(USTR("# create cxi with auto encryption for dev\n"));
UPrintf(USTR("3dstool -cvtf cxi 0.cxi --header ncchheader.bin --exh exh.bin --logo logo.darc.lz --plain plain.bin --exefs exefs.bin --romfs romfs.bin --dev\n\n"));
UPrintf(USTR("# create cxi with auto encryption for retail, not remove ext key\n"));
UPrintf(USTR("3dstool -cvtf cxi 0.cxi --header ncchheader.bin --exh exh.bin --logo logo.darc.lz --plain plain.bin --exefs exefs.bin --romfs romfs.bin --not-remove-ext-key\n\n"));
UPrintf(USTR("# create cxi with auto encryption for retail\n"));
UPrintf(USTR("3dstool -cvtf cxi 0.cxi --header ncchheader.bin --exh exh.bin --logo logo.darc.lz --plain plain.bin --exefs exefs.bin --romfs romfs.bin\n\n"));
UPrintf(USTR("# create cfa without encryption\n"));
Expand Down
1 change: 1 addition & 0 deletions src/3dstool.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class C3dsTool
CBigNum m_Key;
CBigNum m_Counter;
UString m_sXorFileName;
bool m_bRemoveExtKey;
bool m_bDev;
n32 m_nCompressAlign;
ECompressType m_eCompressType;
Expand Down
10 changes: 10 additions & 0 deletions src/ncch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ CNcch::CNcch()
: m_eFileType(C3dsTool::kFileTypeUnknown)
, m_bVerbose(false)
, m_nEncryptMode(kEncryptModeNone)
, m_bRemoveExtKey(true)
, m_bDev(false)
, m_nDownloadBegin(-1)
, m_nDownloadEnd(-1)
Expand Down Expand Up @@ -66,6 +67,11 @@ void CNcch::SetEncryptMode(int a_nEncryptMode)
m_nEncryptMode = a_nEncryptMode;
}

void CNcch::SetRemoveExtKey(bool a_bRemoveExtKey)
{
m_bRemoveExtKey = a_bRemoveExtKey;
}

void CNcch::SetDev(bool a_bDev)
{
m_bDev = a_bDev;
Expand Down Expand Up @@ -711,6 +717,10 @@ bool CNcch::createHeader()
{
m_NcchHeader.Ncch.Flags[Flag] &= ~SDW_BIT32(NoEncrypto);
m_NcchHeader.Ncch.Flags[Flag] &= ~SDW_BIT32(FixedCryptoKey);
if (m_bRemoveExtKey)
{
m_NcchHeader.Ncch.Flags[Flag] &= ~SDW_BIT32(kFlagExtKey);
}
}
fwrite(&m_NcchHeader, sizeof(m_NcchHeader), 1, m_fpNcch);
return true;
Expand Down
2 changes: 2 additions & 0 deletions src/ncch.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ class CNcch
void SetVerbose(bool a_bVerbose);
void SetHeaderFileName(const UString& a_sHeaderFileName);
void SetEncryptMode(int a_nEncryptMode);
void SetRemoveExtKey(bool a_bRemoveExtKey);
void SetDev(bool a_bDev);
void SetDownloadBegin(n32 a_nDownloadBegin);
void SetDownloadEnd(n32 a_nDownloadEnd);
Expand Down Expand Up @@ -166,6 +167,7 @@ class CNcch
bool m_bVerbose;
UString m_sHeaderFileName;
int m_nEncryptMode;
bool m_bRemoveExtKey;
bool m_bDev;
CBigNum m_Key[kEncryptKeyIndexCount];
n32 m_nDownloadBegin;
Expand Down

0 comments on commit 5fff844

Please sign in to comment.