From e3768ee32752d229ebf591209362a15663ea95ac Mon Sep 17 00:00:00 2001 From: parrado Date: Mon, 4 Jan 2021 09:53:31 -0500 Subject: [PATCH] Fixes arguments number bug and adds FMCB/FHDB header to KELF --- src/kelf.cpp | 20 ++++++++++++++++---- src/kelf.h | 4 ++-- src/kelftool.cpp | 34 +++++++++++++++++++++++++++------- src/keystore.cpp | 6 +++++- 4 files changed, 50 insertions(+), 14 deletions(-) diff --git a/src/kelf.cpp b/src/kelf.cpp index ce5f152..92a50c5 100644 --- a/src/kelf.cpp +++ b/src/kelf.cpp @@ -155,20 +155,32 @@ int Kelf::LoadKelf(std::string filename) return 0; } -int Kelf::SaveKelf(std::string filename) +int Kelf::SaveKelf(std::string filename,int headerid) { FILE* f = fopen(filename.c_str(), "wb"); KELFHeader header; - static uint8_t PSX_USER[] = { 0x01, 0x03, 0x00, 0x04, 0x00, 0x02, 0x00, 0x4A, 0x00, 0x07, 0x01, 0x00, 0x00, 0x00, 0x01, 0x78 }; + + static uint8_t *PSX_USER; + + static uint8_t USER_HEADER_FMCB[]={ 0x01, 0x00, 0x00, 0x01, 0x00, 0x03, 0x00, 0x4A, 0x00, 0x01, 0x02, 0x19, 0x00, 0x00, 0x00, 0x56 }; + + static uint8_t USER_HEADER_FHDB[]={ 0x01, 0x00, 0x00, 0x04, 0x00, 0x02, 0x00, 0x4A, 0x00, 0x08, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1B }; + + + if(headerid==0) + PSX_USER = USER_HEADER_FMCB; + else + PSX_USER = USER_HEADER_FHDB; + memcpy(header.UserDefined, PSX_USER, 16); header.ContentSize = Content.size(); header.HeaderSize = sizeof(KELFHeader) + 8 + 16 + 16 + 8 + 16 + 16 + 8 + 8; // header + header signature + kbit + kc + bittable + bittable signature + root signature - header.SystemType = SYSTEM_TYPE_PSX; + header.SystemType = SYSTEM_TYPE_PS2; header.ApplicationType = 1; // 1 = xosdmain, 5 = dvdplayer kirx 7 = dvdplayer kelf header.Flags = 0x22C; header.BitCount = 0; - header.MGZones = 1; // Japan + header.MGZones = 0xFF; // ?? std::string HeaderSignature = GetHeaderSignature(header); std::string BitTableSignature = GetBitTableSignature(); diff --git a/src/kelf.h b/src/kelf.h index fc4bf62..0c024d4 100644 --- a/src/kelf.h +++ b/src/kelf.h @@ -73,7 +73,7 @@ class Kelf Kelf(KeyStore& _ks) : ks(_ks) { } int LoadKelf(std::string filename); - int SaveKelf(std::string filename); + int SaveKelf(std::string filename,int header); int LoadContent(std::string filename); int SaveContent(std::string filename); @@ -87,4 +87,4 @@ class Kelf int VerifyContentSignature(); }; -#endif \ No newline at end of file +#endif diff --git a/src/kelftool.cpp b/src/kelftool.cpp index c952e8a..6893d64 100644 --- a/src/kelftool.cpp +++ b/src/kelftool.cpp @@ -31,7 +31,7 @@ std::string getKeyStorePath() int decrypt(int argc, char** argv) { - if (argc < 2) + if (argc < 3) { printf("%s decrypt \n", argv[0]); return -1; @@ -49,7 +49,7 @@ int decrypt(int argc, char** argv) ret = kelf.LoadKelf(argv[1]); if (ret != 0) { - printf("Failed to LoadKelf!\n"); + printf("Failed to LoadKelf %d!\n",ret); return ret; } ret = kelf.SaveContent(argv[2]); @@ -64,12 +64,29 @@ int decrypt(int argc, char** argv) int encrypt(int argc, char** argv) { - if (argc < 2) + + int headerid=-1; + + if (argc < 4) { - printf("%s decrypt \n", argv[0]); + printf("%s encrypt \n", argv[0]); + printf(": fmcb,fhdb\n"); return -1; } +if (strcmp("fmcb", argv[1]) == 0) + headerid=0; + +if (strcmp("fhdb", argv[1]) == 0) + headerid=1; + + if(headerid==-1){ + + printf("Invalid header: %s\n",argv[1]); + return -1; + + } + KeyStore ks; int ret = ks.Load(getKeyStorePath()); if (ret != 0) @@ -79,13 +96,16 @@ int encrypt(int argc, char** argv) } Kelf kelf(ks); - ret = kelf.LoadContent(argv[1]); + ret = kelf.LoadContent(argv[2]); if (ret != 0) { printf("Failed to LoadContent!\n"); return ret; } - ret = kelf.SaveKelf(argv[2]); + + + + ret = kelf.SaveKelf(argv[3],headerid); if (ret != 0) { printf("Failed to SaveKelf!\n"); @@ -102,7 +122,7 @@ int main(int argc, char** argv) printf("usage: %s \n", argv[0]); printf("Available submodules:\n"); printf("\tdecrypt - decrypt and check signature of kelf files\n"); - printf("\tencrypt - encrypt and sign kelf files\n"); + printf("\tencrypt - encrypt and sign kelf files : fmcb,fhdb\n"); return -1; } diff --git a/src/keystore.cpp b/src/keystore.cpp index 5607068..c51d7c7 100644 --- a/src/keystore.cpp +++ b/src/keystore.cpp @@ -18,6 +18,7 @@ #include #include #include +#include std::vector split(const std::string& s, char delimiter) { @@ -63,10 +64,13 @@ int KeyStore::Load(std::string filename) while (std::getline(infile, line)) { std::vector tokens = split(line, '='); - + + if (tokens.size() != 2) + return KEYSTORE_ERROR_LINE_NOT_KEY_VALUE; + std::string key = tokens[0]; std::string value = tokens[1];