Merge pull request #13233 from hrydgard/kirk-cleanup

Add some comments and const-correctness, and a pointer type fix, to the "semaphore" crypto stuff.
This commit is contained in:
Henrik Rydgård 2020-08-02 01:11:06 +02:00 committed by GitHub
commit 4766e4fcb9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 48 additions and 43 deletions

View File

@ -741,7 +741,7 @@ static int pspDecryptType0(const u8 *inbuf, u8 *outbuf, u32 size)
memcpy(reinterpret_cast<u8*>(header)+sizeof(KIRK_CMD1_HEADER), type0.prxHeader, sizeof(type0.prxHeader));
decryptKirkHeaderType0(reinterpret_cast<u8*>(header), type0.kirkBlock, xorbuf, pti->code);
if (sceUtilsBufferCopyWithRange(outbuf, size, reinterpret_cast<u8*>(header), size - offset, KIRK_CMD_DECRYPT_PRIVATE) != 0)
if (kirk_sceUtilsBufferCopyWithRange(outbuf, size, reinterpret_cast<const u8*>(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<u8*>(header)+sizeof(KIRK_CMD1_HEADER), type1.prxHeader, sizeof(type1.prxHeader));
decryptKirkHeaderType0(reinterpret_cast<u8*>(header), type1.kirkBlock, xorbuf, pti->code);
if (sceUtilsBufferCopyWithRange(outbuf, size, reinterpret_cast<u8*>(header), size - offset, KIRK_CMD_DECRYPT_PRIVATE) != 0)
if (kirk_sceUtilsBufferCopyWithRange(outbuf, size, reinterpret_cast<const u8*>(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<u8*>(header), type2.kirkHeader, xorbuf.cbegin()+0x10, pti->code);
header->mode = 1;
if (sceUtilsBufferCopyWithRange(outbuf, size, reinterpret_cast<u8*>(header), size - offset, KIRK_CMD_DECRYPT_PRIVATE) != 0)
if (kirk_sceUtilsBufferCopyWithRange(outbuf, size, reinterpret_cast<const u8*>(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<u8*>(header), type5.kirkHeader, xorbuf.cbegin()+0x10, pti->code);
header->mode = 1;
if (sceUtilsBufferCopyWithRange(outbuf, size, reinterpret_cast<u8*>(header), size - offset, KIRK_CMD_DECRYPT_PRIVATE) != 0)
if (kirk_sceUtilsBufferCopyWithRange(outbuf, size, reinterpret_cast<const u8*>(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<u8*>(header), size - offset, KIRK_CMD_DECRYPT_PRIVATE) != 0)
if (kirk_sceUtilsBufferCopyWithRange(outbuf, size, reinterpret_cast<const u8*>(header), size - offset, KIRK_CMD_DECRYPT_PRIVATE) != 0)
{
return -4;
}

View File

@ -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;
}

View File

@ -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<hleUtilsBufferCopyWithRange>, "sceUtilsBufferCopyWithRange", 'x', "xixii" },
{0x77E97079, &WrapI_UIUII<sceUtilsBufferCopyByPollingWithRange>, "sceUtilsBufferCopyByPollingWithRange", 'i', "xixii" },
{0x4C537C72, &WrapU_UIUII<sceUtilsBufferCopyWithRange>, "sceUtilsBufferCopyWithRange", 'x', "xixii" },
{0x77E97079, &WrapI_UIUII<sceUtilsBufferCopyByPollingWithRange>, "sceUtilsBufferCopyByPollingWithRange", 'i', "xixii" },
};
void Register_semaphore() {
RegisterModule("semaphore", ARRAY_SIZE(semaphore), semaphore);
}
}

View File

@ -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;

View File

@ -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;
}

View File

@ -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);