From 404600faa37dd87b994f3e9d1ea6cbc67dffa9b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sun, 7 Apr 2024 11:33:47 +0200 Subject: [PATCH 1/4] SavedataParam/ParamSFO: Minor code cleanups --- Core/Dialog/SavedataParam.cpp | 8 +++----- Core/Dialog/SavedataParam.h | 2 ++ Core/ELF/ParamSFO.cpp | 4 ++-- Core/ELF/ParamSFO.h | 3 ++- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/Core/Dialog/SavedataParam.cpp b/Core/Dialog/SavedataParam.cpp index c9533f2f68..f22b8ac13c 100644 --- a/Core/Dialog/SavedataParam.cpp +++ b/Core/Dialog/SavedataParam.cpp @@ -145,7 +145,7 @@ namespace int align16(int address) { - return ((address + 0xF) >> 4) << 4; + return (address + 15) & ~15; } int GetSDKMainVersion(int sdkVersion) @@ -537,10 +537,8 @@ int SavedataParam::Save(SceUtilitySavedataParam* param, const std::string &saveD delete[] updatedList; // Init param with 0. This will be used to detect crypted save or not on loading - u8 *tmpData = new u8[128]; - memset(tmpData, 0, 128); - sfoFile->SetValue("SAVEDATA_PARAMS", tmpData, 128, 128); - delete[] tmpData; + u8 zeroes[128]{}; + sfoFile->SetValue("SAVEDATA_PARAMS", zeroes, 128, 128); u8 *sfoData; size_t sfoSize; diff --git a/Core/Dialog/SavedataParam.h b/Core/Dialog/SavedataParam.h index 04c8dca5e4..b10cfe6c6b 100644 --- a/Core/Dialog/SavedataParam.h +++ b/Core/Dialog/SavedataParam.h @@ -375,6 +375,8 @@ private: int DecryptSave(unsigned int mode, unsigned char *data, int *dataLen, int *alignedLen, unsigned char *cryptkey, const u8 *expectedHash); int EncryptData(unsigned int mode, unsigned char *data, int *dataLen, int *alignedLen, unsigned char *hash, unsigned char *cryptkey); int UpdateHash(u8* sfoData, int sfoSize, int sfoDataParamsOffset, int encryptmode); + + // WARNING: this writes zeroes to data from len to alignedLen. This means that data must have space for the full alignedLen! int BuildHash(unsigned char *output, unsigned char *data, unsigned int len, unsigned int alignedLen, int mode, unsigned char *cryptkey); int DetermineCryptMode(const SceUtilitySavedataParam *param) const; diff --git a/Core/ELF/ParamSFO.cpp b/Core/ELF/ParamSFO.cpp index 856221d36c..0703b8f2af 100644 --- a/Core/ELF/ParamSFO.cpp +++ b/Core/ELF/ParamSFO.cpp @@ -195,7 +195,7 @@ bool ParamSFOData::ReadSFO(const u8 *paramsfo, size_t size) { return true; } -int ParamSFOData::GetDataOffset(const u8 *paramsfo, const std::string &dataName) { +int ParamSFOData::GetDataOffset(const u8 *paramsfo, const char *dataName) { const Header *header = (const Header *)paramsfo; if (header->magic != 0x46535000) return -1; @@ -210,7 +210,7 @@ int ParamSFOData::GetDataOffset(const u8 *paramsfo, const std::string &dataName) for (u32 i = 0; i < header->index_table_entries; i++) { const char *key = (const char *)(key_start + indexTables[i].key_table_offset); - if (!strcmp(key, dataName.c_str())) + if (!strcmp(key, dataName)) { return data_start + indexTables[i].data_table_offset; } diff --git a/Core/ELF/ParamSFO.h b/Core/ELF/ParamSFO.h index 3d7a719e50..8531ded275 100644 --- a/Core/ELF/ParamSFO.h +++ b/Core/ELF/ParamSFO.h @@ -55,7 +55,8 @@ public: } } - int GetDataOffset(const u8 *paramsfo, const std::string &dataName); + // If not found, returns a negative value. + int GetDataOffset(const u8 *paramsfo, const char *dataName); void Clear(); From 79c553d004d1e9be82a2a19ce853bc11a463d5e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sun, 7 Apr 2024 11:56:51 +0200 Subject: [PATCH 2/4] Remove the zero-write in BuildHash --- Core/Dialog/SavedataParam.cpp | 5 +++-- Core/Dialog/SavedataParam.h | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Core/Dialog/SavedataParam.cpp b/Core/Dialog/SavedataParam.cpp index f22b8ac13c..ad78b52fec 100644 --- a/Core/Dialog/SavedataParam.cpp +++ b/Core/Dialog/SavedataParam.cpp @@ -1008,6 +1008,7 @@ int SavedataParam::DecryptSave(unsigned int mode, unsigned char *data, int *data return 0; } +// Requires sfoData to be padded with zeroes to the next 16-byte boundary (due to BuildHash) int SavedataParam::UpdateHash(u8* sfoData, int sfoSize, int sfoDataParamsOffset, int encryptmode) { int alignedLen = align16(sfoSize); @@ -1057,8 +1058,9 @@ int SavedataParam::UpdateHash(u8* sfoData, int sfoSize, int sfoDataParamsOffset, return 0; } +// Requires sfoData to be padded with zeroes to the next 16-byte boundary. int SavedataParam::BuildHash(unsigned char *output, - unsigned char *data, + const unsigned char *data, unsigned int len, unsigned int alignedLen, int mode, @@ -1069,7 +1071,6 @@ int SavedataParam::BuildHash(unsigned char *output, /* Set up buffers */ memset(&ctx1, 0, sizeof(pspChnnlsvContext1)); memset(output, 0, 0x10); - memset(data + len, 0, alignedLen - len); /* Perform the magic */ if (sceSdSetIndex_(ctx1, mode & 0xFF) < 0) diff --git a/Core/Dialog/SavedataParam.h b/Core/Dialog/SavedataParam.h index b10cfe6c6b..d580aabb29 100644 --- a/Core/Dialog/SavedataParam.h +++ b/Core/Dialog/SavedataParam.h @@ -376,8 +376,8 @@ private: int EncryptData(unsigned int mode, unsigned char *data, int *dataLen, int *alignedLen, unsigned char *hash, unsigned char *cryptkey); int UpdateHash(u8* sfoData, int sfoSize, int sfoDataParamsOffset, int encryptmode); - // WARNING: this writes zeroes to data from len to alignedLen. This means that data must have space for the full alignedLen! - int BuildHash(unsigned char *output, unsigned char *data, unsigned int len, unsigned int alignedLen, int mode, unsigned char *cryptkey); + // data must be zero-padded from len to alignedLen! + int BuildHash(unsigned char *output, const unsigned char *data, unsigned int len, unsigned int alignedLen, int mode, unsigned char *cryptkey); int DetermineCryptMode(const SceUtilitySavedataParam *param) const; std::vector GetSFOEntries(const std::string &dirPath); From 79bdc8a9ff50a82ed856b47feaa37760c715935d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sun, 7 Apr 2024 11:57:08 +0200 Subject: [PATCH 3/4] Some constification in sceChnnlsv --- Core/HLE/sceChnnlsv.cpp | 8 ++++---- Core/HLE/sceChnnlsv.h | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Core/HLE/sceChnnlsv.cpp b/Core/HLE/sceChnnlsv.cpp index b82e447151..c7fcd2cef3 100644 --- a/Core/HLE/sceChnnlsv.cpp +++ b/Core/HLE/sceChnnlsv.cpp @@ -134,7 +134,7 @@ static int sub_15B0(u8* data, int alignedLen, u8* buf, int val) return 0; } -static int sub_0000(u8* data_out, u8* data, int alignedLen, u8* data2, int& data3, int mode) +static int sub_0000(u8* data_out, u8* data, int alignedLen, const u8* data2, int& data3, int mode) { memcpy(data_out+20, data2, 16); // Mode 1:2 is 83, 3:4 is 87, 5:6 is 100 @@ -222,7 +222,7 @@ static int sceSdGetLastIndex(u32 addressCtx, u32 addressHash, u32 addressKey) { return hleLogSuccessI(SCEMISC, sceSdGetLastIndex_(*ctx, hash, Memory::GetPointerWrite(addressKey))); } -int sceSdGetLastIndex_(pspChnnlsvContext1& ctx, u8* in_hash, u8* in_key) +int sceSdGetLastIndex_(pspChnnlsvContext1& ctx, u8* in_hash, const u8* in_key) { if(ctx.keyLength >= 17) return -1026; @@ -348,7 +348,7 @@ static int sceSdRemoveValue(u32 addressCtx, u32 addressData, int length) { return hleLogSuccessI(SCEMISC, sceSdRemoveValue_(*ctx, Memory::GetPointerWrite(addressData), length)); } -int sceSdRemoveValue_(pspChnnlsvContext1& ctx, u8* data, int length) +int sceSdRemoveValue_(pspChnnlsvContext1& ctx, const u8* data, int length) { if(ctx.keyLength >= 17) return -1026; @@ -400,7 +400,7 @@ static int sceSdCreateList(u32 ctx2Addr, int mode, int unkwn, u32 dataAddr, u32 return hleLogSuccessI(SCEMISC, sceSdCreateList_(*ctx2, mode, unkwn, data, cryptkey)); } -int sceSdCreateList_(pspChnnlsvContext2& ctx2, int mode, int uknw, u8* data, u8* cryptkey) +int sceSdCreateList_(pspChnnlsvContext2& ctx2, int mode, int uknw, u8* data, const u8* cryptkey) { ctx2.mode = mode; ctx2.unkn = 1; diff --git a/Core/HLE/sceChnnlsv.h b/Core/HLE/sceChnnlsv.h index 3636e14b08..d8a030932c 100644 --- a/Core/HLE/sceChnnlsv.h +++ b/Core/HLE/sceChnnlsv.h @@ -35,10 +35,10 @@ typedef struct _pspChnnlsvContext2 { } pspChnnlsvContext2; int sceSdSetIndex_(pspChnnlsvContext1& ctx, int value); -int sceSdRemoveValue_(pspChnnlsvContext1& ctx, u8* data, int length); -int sceSdCreateList_(pspChnnlsvContext2& ctx2, int mode, int uknw, u8* data, u8* cryptkey); +int sceSdRemoveValue_(pspChnnlsvContext1& ctx, const u8* data, int length); +int sceSdCreateList_(pspChnnlsvContext2& ctx2, int mode, int uknw, u8* data, const u8* cryptkey); int sceSdSetMember_(pspChnnlsvContext2& ctx, u8* data, int alignedLen); int sceSdCleanList_(pspChnnlsvContext2& ctx); -int sceSdGetLastIndex_(pspChnnlsvContext1& ctx, u8* in_hash, u8* in_key); +int sceSdGetLastIndex_(pspChnnlsvContext1& ctx, u8* in_hash, const u8* in_key); void Register_sceChnnlsv(); From b7f0a9029eb8216f38a084c8013468654d471bd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sun, 7 Apr 2024 12:36:32 +0200 Subject: [PATCH 4/4] More cleanup --- Core/Dialog/SavedataParam.cpp | 13 +++++-------- Core/Dialog/SavedataParam.h | 4 +++- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/Core/Dialog/SavedataParam.cpp b/Core/Dialog/SavedataParam.cpp index ad78b52fec..fd0477f572 100644 --- a/Core/Dialog/SavedataParam.cpp +++ b/Core/Dialog/SavedataParam.cpp @@ -581,16 +581,14 @@ int SavedataParam::Save(SceUtilitySavedataParam* param, const std::string &saveD // copy back save name in request strncpy(param->saveName, saveDirName.c_str(), 20); - if (fileName.empty()) { - delete[] cryptedData; - } else { + if (!fileName.empty()) { if (!WritePSPFile(filePath, data_, saveSize)) { ERROR_LOG(SCEUTILITY, "Error writing file %s", filePath.c_str()); delete[] cryptedData; return SCE_UTILITY_SAVEDATA_ERROR_SAVE_MS_NOSPACE; } - delete[] cryptedData; } + delete[] cryptedData; } @@ -1059,13 +1057,12 @@ int SavedataParam::UpdateHash(u8* sfoData, int sfoSize, int sfoDataParamsOffset, } // Requires sfoData to be padded with zeroes to the next 16-byte boundary. -int SavedataParam::BuildHash(unsigned char *output, - const unsigned char *data, +int SavedataParam::BuildHash(uint8_t *output, + const uint8_t *data, unsigned int len, unsigned int alignedLen, int mode, - unsigned char *cryptkey) -{ + const uint8_t *cryptkey) { pspChnnlsvContext1 ctx1; /* Set up buffers */ diff --git a/Core/Dialog/SavedataParam.h b/Core/Dialog/SavedataParam.h index d580aabb29..15e7015d57 100644 --- a/Core/Dialog/SavedataParam.h +++ b/Core/Dialog/SavedataParam.h @@ -21,6 +21,8 @@ #include #include #include +#include + #include "Common/CommonTypes.h" #include "Core/ELF/ParamSFO.h" #include "Core/MemMap.h" @@ -377,7 +379,7 @@ private: int UpdateHash(u8* sfoData, int sfoSize, int sfoDataParamsOffset, int encryptmode); // data must be zero-padded from len to alignedLen! - int BuildHash(unsigned char *output, const unsigned char *data, unsigned int len, unsigned int alignedLen, int mode, unsigned char *cryptkey); + int BuildHash(uint8_t *output, const uint8_t *data, unsigned int len, unsigned int alignedLen, int mode, const uint8_t *cryptkey); int DetermineCryptMode(const SceUtilitySavedataParam *param) const; std::vector GetSFOEntries(const std::string &dirPath);