Skip to content

Commit

Permalink
Fixes arguments number bug and adds FMCB/FHDB header to KELF
Browse files Browse the repository at this point in the history
  • Loading branch information
parrado committed Jan 4, 2021
1 parent 6b9b471 commit e3768ee
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 14 deletions.
20 changes: 16 additions & 4 deletions src/kelf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions src/kelf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -87,4 +87,4 @@ class Kelf
int VerifyContentSignature();
};

#endif
#endif
34 changes: 27 additions & 7 deletions src/kelftool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ std::string getKeyStorePath()

int decrypt(int argc, char** argv)
{
if (argc < 2)
if (argc < 3)
{
printf("%s decrypt <input> <output>\n", argv[0]);
return -1;
Expand All @@ -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]);
Expand All @@ -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 <input> <output>\n", argv[0]);
printf("%s encrypt <headerid> <input> <output>\n", argv[0]);
printf("<headerid>: 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)
Expand All @@ -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");
Expand All @@ -102,7 +122,7 @@ int main(int argc, char** argv)
printf("usage: %s <submodule> <args>\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 <headerid> - encrypt and sign kelf files <headerid>: fmcb,fhdb\n");
return -1;
}

Expand Down
6 changes: 5 additions & 1 deletion src/keystore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <fstream>
#include <vector>
#include <sstream>
#include <iostream>

std::vector<std::string> split(const std::string& s, char delimiter)
{
Expand Down Expand Up @@ -63,10 +64,13 @@ int KeyStore::Load(std::string filename)
while (std::getline(infile, line))
{
std::vector<std::string> tokens = split(line, '=');



if (tokens.size() != 2)

return KEYSTORE_ERROR_LINE_NOT_KEY_VALUE;


std::string key = tokens[0];
std::string value = tokens[1];

Expand Down

0 comments on commit e3768ee

Please sign in to comment.