From 0586338d5e8f3fd83af619a0ba07872e42508aa8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sat, 1 Aug 2020 23:11:48 +0200 Subject: [PATCH] Add some comments and const-correctness, and a pointer type fix, to the "semaphore" crypto stuff. --- Core/ELF/PrxDecrypter.cpp | 10 +++++----- Core/HLE/sceChnnlsv.cpp | 6 +++--- Core/HLE/sceKernelSemaphore.cpp | 32 +++++++++++++++++++------------- ext/libkirk/amctrl.c | 10 +++++----- ext/libkirk/kirk_engine.c | 28 ++++++++++++++-------------- ext/libkirk/kirk_engine.h | 5 ++--- 6 files changed, 48 insertions(+), 43 deletions(-) diff --git a/Core/ELF/PrxDecrypter.cpp b/Core/ELF/PrxDecrypter.cpp index 5e6f6f5f26..f87fa91fd7 100644 --- a/Core/ELF/PrxDecrypter.cpp +++ b/Core/ELF/PrxDecrypter.cpp @@ -741,7 +741,7 @@ static int pspDecryptType0(const u8 *inbuf, u8 *outbuf, u32 size) memcpy(reinterpret_cast(header)+sizeof(KIRK_CMD1_HEADER), type0.prxHeader, sizeof(type0.prxHeader)); decryptKirkHeaderType0(reinterpret_cast(header), type0.kirkBlock, xorbuf, pti->code); - if (sceUtilsBufferCopyWithRange(outbuf, size, reinterpret_cast(header), size - offset, KIRK_CMD_DECRYPT_PRIVATE) != 0) + if (kirk_sceUtilsBufferCopyWithRange(outbuf, size, reinterpret_cast(header), size - offset, KIRK_CMD_DECRYPT_PRIVATE) != 0) { return -4; } @@ -796,7 +796,7 @@ static int pspDecryptType1(const u8 *inbuf, u8 *outbuf, u32 size) memcpy(reinterpret_cast(header)+sizeof(KIRK_CMD1_HEADER), type1.prxHeader, sizeof(type1.prxHeader)); decryptKirkHeaderType0(reinterpret_cast(header), type1.kirkBlock, xorbuf, pti->code); - if (sceUtilsBufferCopyWithRange(outbuf, size, reinterpret_cast(header), size - offset, KIRK_CMD_DECRYPT_PRIVATE) != 0) + if (kirk_sceUtilsBufferCopyWithRange(outbuf, size, reinterpret_cast(header), size - offset, KIRK_CMD_DECRYPT_PRIVATE) != 0) { return -4; } @@ -860,7 +860,7 @@ static int pspDecryptType2(const u8 *inbuf, u8 *outbuf, u32 size) decryptKirkHeader(reinterpret_cast(header), type2.kirkHeader, xorbuf.cbegin()+0x10, pti->code); header->mode = 1; - if (sceUtilsBufferCopyWithRange(outbuf, size, reinterpret_cast(header), size - offset, KIRK_CMD_DECRYPT_PRIVATE) != 0) + if (kirk_sceUtilsBufferCopyWithRange(outbuf, size, reinterpret_cast(header), size - offset, KIRK_CMD_DECRYPT_PRIVATE) != 0) { return -4; } @@ -924,7 +924,7 @@ static int pspDecryptType5(const u8 *inbuf, u8 *outbuf, u32 size, const u8 *seed decryptKirkHeader(reinterpret_cast(header), type5.kirkHeader, xorbuf.cbegin()+0x10, pti->code); header->mode = 1; - if (sceUtilsBufferCopyWithRange(outbuf, size, reinterpret_cast(header), size - offset, KIRK_CMD_DECRYPT_PRIVATE) != 0) + if (kirk_sceUtilsBufferCopyWithRange(outbuf, size, reinterpret_cast(header), size - offset, KIRK_CMD_DECRYPT_PRIVATE) != 0) { return -4; } @@ -991,7 +991,7 @@ static int pspDecryptType6(const u8 *inbuf, u8 *outbuf, u32 size) header->mode = 1; header->ecdsa_hash = 1; - if (sceUtilsBufferCopyWithRange(outbuf, size, reinterpret_cast(header), size - offset, KIRK_CMD_DECRYPT_PRIVATE) != 0) + if (kirk_sceUtilsBufferCopyWithRange(outbuf, size, reinterpret_cast(header), size - offset, KIRK_CMD_DECRYPT_PRIVATE) != 0) { return -4; } diff --git a/Core/HLE/sceChnnlsv.cpp b/Core/HLE/sceChnnlsv.cpp index 5fc9c4c965..475e2ed8d2 100644 --- a/Core/HLE/sceChnnlsv.cpp +++ b/Core/HLE/sceChnnlsv.cpp @@ -99,7 +99,7 @@ static int kirkSendCmd(u8* data, int length, int num, bool encrypt) *(int*)(data+12) = num; *(int*)(data+16) = length; - if (sceUtilsBufferCopyWithRange(data, length + 20, data, length + 20, encrypt ? KIRK_CMD_ENCRYPT_IV_0 : KIRK_CMD_DECRYPT_IV_0)) + if (kirk_sceUtilsBufferCopyWithRange(data, length + 20, data, length + 20, encrypt ? KIRK_CMD_ENCRYPT_IV_0 : KIRK_CMD_DECRYPT_IV_0)) return -257; return 0; @@ -114,7 +114,7 @@ static int kirkSendFuseCmd(u8* data, int length, bool encrypt) *(int*)(data+16) = length; // Note: CMD 5 and 8 are not available, will always return -1 - if (sceUtilsBufferCopyWithRange(data, length + 20, data, length + 20, encrypt ? KIRK_CMD_ENCRYPT_IV_FUSE : KIRK_CMD_DECRYPT_IV_FUSE)) + if (kirk_sceUtilsBufferCopyWithRange(data, length + 20, data, length + 20, encrypt ? KIRK_CMD_ENCRYPT_IV_FUSE : KIRK_CMD_DECRYPT_IV_FUSE)) return -258; return 0; @@ -209,7 +209,7 @@ static int sub_1510(u8* data, int size, u8* result , int num) static int sub_17A8(u8* data) { - if (sceUtilsBufferCopyWithRange(data, 20, 0, 0, 14) == 0) + if (kirk_sceUtilsBufferCopyWithRange(data, 20, 0, 0, 14) == 0) return 0; return -261; } diff --git a/Core/HLE/sceKernelSemaphore.cpp b/Core/HLE/sceKernelSemaphore.cpp index 24a65e25fa..fe2f3c6c96 100644 --- a/Core/HLE/sceKernelSemaphore.cpp +++ b/Core/HLE/sceKernelSemaphore.cpp @@ -458,28 +458,34 @@ int sceKernelPollSema(SceUID id, int wantedCount) } } -static u32 hleUtilsBufferCopyWithRange(u32 outAddr, int outSize, u32 inAddr, int inSize, int cmd) +// The below functions don't really belong to sceKernelSemaphore. They are the core crypto functionality, +// exposed through the confusingly named "sceUtilsBufferCopyWithRange" name, which Sony placed in the +// not-at-all-suspicious "semaphore" library, which has nothing to do with semaphores. + +static u32 sceUtilsBufferCopyWithRange(u32 outAddr, int outSize, u32 inAddr, int inSize, int cmd) { - int temp = sceUtilsBufferCopyWithRange((u8*)outAddr, outSize, (u8*)inAddr, inSize, cmd); - if (temp != 0) { - ERROR_LOG(SCEKERNEL, "hleUtilsBufferCopyWithRange: Failed with %d", temp); - } - return 0; + u8 *outAddress = Memory::IsValidRange(outAddr, outSize) ? Memory::GetPointer(outAddr) : nullptr; + const u8 *inAddress = Memory::IsValidRange(inAddr, inSize) ? Memory::GetPointer(inAddr) : nullptr; + int temp = kirk_sceUtilsBufferCopyWithRange(outAddress, outSize, inAddress, inSize, cmd); + if (temp != 0) { + ERROR_LOG(SCEKERNEL, "hleUtilsBufferCopyWithRange: Failed with %d", temp); + } + return 0; } +// Note sure what difference there is between this and sceUtilsBufferCopyWithRange. static int sceUtilsBufferCopyByPollingWithRange(u32 outAddr, int outSize, u32 inAddr, int inSize, int cmd) { - return sceUtilsBufferCopyWithRange((u8*)outAddr, outSize, (u8*)inAddr, inSize, cmd); + u8 *outAddress = Memory::IsValidRange(outAddr, outSize) ? Memory::GetPointer(outAddr) : nullptr; + const u8 *inAddress = Memory::IsValidRange(inAddr, inSize) ? Memory::GetPointer(inAddr) : nullptr; + return kirk_sceUtilsBufferCopyWithRange(outAddress, outSize, inAddress, inSize, cmd); } const HLEFunction semaphore[] = { - {0x4C537C72, &WrapU_UIUII, "sceUtilsBufferCopyWithRange", 'x', "xixii" }, - {0x77E97079, &WrapI_UIUII, "sceUtilsBufferCopyByPollingWithRange", 'i', "xixii" }, - + {0x4C537C72, &WrapU_UIUII, "sceUtilsBufferCopyWithRange", 'x', "xixii" }, + {0x77E97079, &WrapI_UIUII, "sceUtilsBufferCopyByPollingWithRange", 'i', "xixii" }, }; - - void Register_semaphore() { RegisterModule("semaphore", ARRAY_SIZE(semaphore), semaphore); -} \ No newline at end of file +} diff --git a/ext/libkirk/amctrl.c b/ext/libkirk/amctrl.c index 86fbbf9b36..d144ca792c 100644 --- a/ext/libkirk/amctrl.c +++ b/ext/libkirk/amctrl.c @@ -41,7 +41,7 @@ static int do_kirk4(u8 *buf, int size, int type) header[3] = type; header[4] = size; - retv = sceUtilsBufferCopyWithRange(buf, size+0x14, buf, size, 4); + retv = kirk_sceUtilsBufferCopyWithRange(buf, size+0x14, buf, size, 4); if(retv) return 0x80510311; @@ -60,7 +60,7 @@ static int do_kirk7(u8 *buf, int size, int type) header[3] = type; header[4] = size; - retv = sceUtilsBufferCopyWithRange(buf, size+0x14, buf, size, 7); + retv = kirk_sceUtilsBufferCopyWithRange(buf, size+0x14, buf, size, 7); if(retv) return 0x80510311; @@ -78,7 +78,7 @@ static int kirk5(u8 *buf, int size) header[3] = 0x0100; header[4] = size; - retv = sceUtilsBufferCopyWithRange(buf, size+0x14, buf, size, 5); + retv = kirk_sceUtilsBufferCopyWithRange(buf, size+0x14, buf, size, 5); if(retv) return 0x80510312; @@ -96,7 +96,7 @@ static int kirk8(u8 *buf, int size) header[3] = 0x0100; header[4] = size; - retv = sceUtilsBufferCopyWithRange(buf, size+0x14, buf, size, 8); + retv = kirk_sceUtilsBufferCopyWithRange(buf, size+0x14, buf, size, 8); if(retv) return 0x80510312; @@ -107,7 +107,7 @@ static int kirk14(u8 *buf) { int retv; - retv = sceUtilsBufferCopyWithRange(buf, 0x14, 0, 0, 14); + retv = kirk_sceUtilsBufferCopyWithRange(buf, 0x14, 0, 0, 14); if(retv) return 0x80510315; diff --git a/ext/libkirk/kirk_engine.c b/ext/libkirk/kirk_engine.c index 1ff4c267e4..69a300a702 100644 --- a/ext/libkirk/kirk_engine.c +++ b/ext/libkirk/kirk_engine.c @@ -334,7 +334,7 @@ void kirk4(u8* outbuff, const u8* inbuff, size_t size, int keyId) AES_ctx aesKey; u8* key = kirk_4_7_get_key(keyId); AES_set_key(&aesKey, key, 128); - AES_cbc_encrypt(&aesKey, inbuff, outbuff, size); + AES_cbc_encrypt(&aesKey, inbuff, outbuff, (int)size); } int kirk_CMD7(u8* outbuff, u8* inbuff, int size) @@ -362,7 +362,7 @@ void kirk7(u8* outbuff, const u8* inbuff, size_t size, int keyId) AES_ctx aesKey; u8* key = kirk_4_7_get_key(keyId); AES_set_key(&aesKey, key, 128); - AES_cbc_decrypt(&aesKey, inbuff, outbuff, size); + AES_cbc_decrypt(&aesKey, inbuff, outbuff, (int)size); } int kirk_CMD10(u8* inbuff, int insize) @@ -725,21 +725,21 @@ int kirk_CMD1_ex(u8* outbuff, u8* inbuff, int size, KIRK_CMD1_HEADER* header) return ret; } - -int sceUtilsBufferCopyWithRange(u8* outbuff, int outsize, u8* inbuff, int insize, int cmd) +int kirk_sceUtilsBufferCopyWithRange(u8* outbuff, int outsize, const u8* inbuff, int insize, int cmd) { + // TODO: propagate const-correctness into all these functions. switch(cmd) { - case KIRK_CMD_DECRYPT_PRIVATE: return kirk_CMD1(outbuff, inbuff, insize); break; - case KIRK_CMD_ENCRYPT_IV_0: return kirk_CMD4(outbuff, inbuff, insize); break; - case KIRK_CMD_DECRYPT_IV_0: return kirk_CMD7(outbuff, inbuff, insize); break; - case KIRK_CMD_PRIV_SIGN_CHECK: return kirk_CMD10(inbuff, insize); break; - case KIRK_CMD_SHA1_HASH: return kirk_CMD11(outbuff, inbuff, insize); break; - case KIRK_CMD_ECDSA_GEN_KEYS: return kirk_CMD12(outbuff,outsize); break; - case KIRK_CMD_ECDSA_MULTIPLY_POINT: return kirk_CMD13(outbuff,outsize, inbuff, insize); break; - case KIRK_CMD_PRNG: return kirk_CMD14(outbuff,outsize); break; - case KIRK_CMD_ECDSA_SIGN: return kirk_CMD16(outbuff, outsize, inbuff, insize); break; - case KIRK_CMD_ECDSA_VERIFY: return kirk_CMD17(inbuff, insize); break; + case KIRK_CMD_DECRYPT_PRIVATE: return kirk_CMD1(outbuff, (u8 *)inbuff, insize); break; + case KIRK_CMD_ENCRYPT_IV_0: return kirk_CMD4(outbuff, (u8 *)inbuff, insize); break; + case KIRK_CMD_DECRYPT_IV_0: return kirk_CMD7(outbuff, (u8 *)inbuff, insize); break; + case KIRK_CMD_PRIV_SIGN_CHECK: return kirk_CMD10((u8 *)inbuff, insize); break; + case KIRK_CMD_SHA1_HASH: return kirk_CMD11(outbuff, (u8 *)inbuff, insize); break; + case KIRK_CMD_ECDSA_GEN_KEYS: return kirk_CMD12(outbuff, outsize); break; + case KIRK_CMD_ECDSA_MULTIPLY_POINT: return kirk_CMD13(outbuff, outsize, (u8 *)inbuff, insize); break; + case KIRK_CMD_PRNG: return kirk_CMD14(outbuff, outsize); break; + case KIRK_CMD_ECDSA_SIGN: return kirk_CMD16(outbuff, outsize, (u8 *)inbuff, insize); break; + case KIRK_CMD_ECDSA_VERIFY: return kirk_CMD17((u8 *)inbuff, insize); break; } return -1; } diff --git a/ext/libkirk/kirk_engine.h b/ext/libkirk/kirk_engine.h index c91795277c..5c7be012b7 100644 --- a/ext/libkirk/kirk_engine.h +++ b/ext/libkirk/kirk_engine.h @@ -218,9 +218,8 @@ u8* kirk_4_7_get_key(int key_type); //kirk "ex" functions int kirk_CMD1_ex(u8* outbuff, u8* inbuff, int size, KIRK_CMD1_HEADER* header); -//sce-like funcs -int sceUtilsSetFuseID(u8*fuse); -int sceUtilsBufferCopyWithRange(u8* outbuff, int outsize, u8* inbuff, int insize, int cmd); +//sce-like func. sceUtilsBufferCopyWithRange is clearly intentionally confusingly named. +int kirk_sceUtilsBufferCopyWithRange(u8* outbuff, int outsize, const u8* inbuff, int insize, int cmd); void decrypt_kirk16_private(u8 *dA_out, u8 *dA_enc); void encrypt_kirk16_private(u8 *dA_out, u8 *dA_dec);