From b3d94e6abe7eb87725974f175d0fe0ffe28905a9 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Mon, 12 May 2025 19:28:29 +1000 Subject: [PATCH] Avoid duplicating encryption code for POSIX and Console systems --- src/Platform_32x.c | 17 ++++---- src/Platform_3DS.c | 2 + src/Platform_Dreamcast.c | 2 + src/Platform_GBA.c | 16 ++++--- src/Platform_GCWii.c | 2 + src/Platform_N64.c | 2 + src/Platform_NDS.c | 2 + src/Platform_PS1.c | 1 + src/Platform_PS3.c | 1 + src/Platform_PS4.c | 1 + src/Platform_PSP.c | 2 + src/Platform_PSVita.c | 2 + src/Platform_Posix.c | 87 +------------------------------------ src/Platform_Switch.c | 2 + src/Platform_WiiU.c | 1 + src/Platform_Xbox.c | 2 + src/Platform_Xbox360.c | 2 + src/_GraphicsBase.h | 3 +- src/_PlatformBase.h | 94 ++++++++++++++++++++++++++++++++++++++++ src/_PlatformConsole.h | 85 +----------------------------------- 20 files changed, 140 insertions(+), 186 deletions(-) diff --git a/src/Platform_32x.c b/src/Platform_32x.c index c9e9c6cbd..d48d7d397 100644 --- a/src/Platform_32x.c +++ b/src/Platform_32x.c @@ -1,7 +1,7 @@ #include "Core.h" #if defined PLAT_32X -#define OVERRIDE_MEM_FUNCTIONS +#define OVERRIDE_MEM_FUNCTIONS #include "_PlatformBase.h" #include "Stream.h" #include "ExtMath.h" @@ -269,14 +269,15 @@ cc_result Process_StartOpen(const cc_string* args) { return ERR_NOT_SUPPORTED; } +cc_result Platform_Encrypt(const void* data, int len, cc_string* dst) { + return ERR_NOT_SUPPORTED; +} -/*########################################################################################################################* -*-------------------------------------------------------Encryption--------------------------------------------------------* -*#########################################################################################################################*/ -#define MACHINE_KEY "32x32x32x32xSEGA" +cc_result Platform_Decrypt(const void* data, int len, cc_string* dst) { + return ERR_NOT_SUPPORTED; +} -static cc_result GetMachineID(cc_uint32* key) { - Mem_Copy(key, MACHINE_KEY, sizeof(MACHINE_KEY) - 1); - return 0; +cc_result Platform_GetEntropy(void* data, int len) { + return ERR_NOT_SUPPORTED; } #endif diff --git a/src/Platform_3DS.c b/src/Platform_3DS.c index c9f422336..0c1b08ffb 100644 --- a/src/Platform_3DS.c +++ b/src/Platform_3DS.c @@ -1,5 +1,7 @@ #include "Core.h" #if defined CC_BUILD_3DS + +#define CC_XTEA_ENCRYPTION #include "_PlatformBase.h" #include "Stream.h" #include "ExtMath.h" diff --git a/src/Platform_Dreamcast.c b/src/Platform_Dreamcast.c index ea127bf78..28fe69ed0 100644 --- a/src/Platform_Dreamcast.c +++ b/src/Platform_Dreamcast.c @@ -1,5 +1,7 @@ #include "Core.h" #if defined CC_BUILD_DREAMCAST + +#define CC_XTEA_ENCRYPTION #include "_PlatformBase.h" #include "Stream.h" #include "ExtMath.h" diff --git a/src/Platform_GBA.c b/src/Platform_GBA.c index e2a7614ed..b8cc64749 100644 --- a/src/Platform_GBA.c +++ b/src/Platform_GBA.c @@ -1,5 +1,6 @@ #include "Core.h" #if defined CC_BUILD_GBA + #include "_PlatformBase.h" #include "Stream.h" #include "ExtMath.h" @@ -256,14 +257,15 @@ cc_result Process_StartOpen(const cc_string* args) { return ERR_NOT_SUPPORTED; } +cc_result Platform_Encrypt(const void* data, int len, cc_string* dst) { + return ERR_NOT_SUPPORTED; +} -/*########################################################################################################################* -*-------------------------------------------------------Encryption--------------------------------------------------------* -*#########################################################################################################################*/ -#define MACHINE_KEY "GBA_GBA_GBA_GBA_" +cc_result Platform_Decrypt(const void* data, int len, cc_string* dst) { + return ERR_NOT_SUPPORTED; +} -static cc_result GetMachineID(cc_uint32* key) { - Mem_Copy(key, MACHINE_KEY, sizeof(MACHINE_KEY) - 1); - return 0; +cc_result Platform_GetEntropy(void* data, int len) { + return ERR_NOT_SUPPORTED; } #endif diff --git a/src/Platform_GCWii.c b/src/Platform_GCWii.c index eb867b476..ec5728ccf 100644 --- a/src/Platform_GCWii.c +++ b/src/Platform_GCWii.c @@ -1,5 +1,7 @@ #include "Core.h" #if defined CC_BUILD_GCWII + +#define CC_XTEA_ENCRYPTION #include "_PlatformBase.h" #include "Stream.h" #include "ExtMath.h" diff --git a/src/Platform_N64.c b/src/Platform_N64.c index ccff4cac4..59a4ac098 100644 --- a/src/Platform_N64.c +++ b/src/Platform_N64.c @@ -1,5 +1,7 @@ #include "Core.h" #if defined CC_BUILD_N64 + +#define CC_XTEA_ENCRYPTION #include "_PlatformBase.h" #include "Stream.h" #include "ExtMath.h" diff --git a/src/Platform_NDS.c b/src/Platform_NDS.c index 76602d4c4..4929f39d1 100644 --- a/src/Platform_NDS.c +++ b/src/Platform_NDS.c @@ -1,5 +1,7 @@ #include "Core.h" #if defined CC_BUILD_NDS + +#define CC_XTEA_ENCRYPTION #include "_PlatformBase.h" #include "Stream.h" #include "ExtMath.h" diff --git a/src/Platform_PS1.c b/src/Platform_PS1.c index d4f7f0ea9..546e543bc 100644 --- a/src/Platform_PS1.c +++ b/src/Platform_PS1.c @@ -1,6 +1,7 @@ #include "Core.h" #if defined PLAT_PS1 +#define CC_XTEA_ENCRYPTION #include "_PlatformBase.h" #include "Stream.h" #include "ExtMath.h" diff --git a/src/Platform_PS3.c b/src/Platform_PS3.c index 3f2e259f5..756f8cd58 100644 --- a/src/Platform_PS3.c +++ b/src/Platform_PS3.c @@ -1,6 +1,7 @@ #include "Core.h" #if defined PLAT_PS3 +#define CC_XTEA_ENCRYPTION #include "_PlatformBase.h" #include "Stream.h" #include "ExtMath.h" diff --git a/src/Platform_PS4.c b/src/Platform_PS4.c index 1a208d712..29ff50f9a 100644 --- a/src/Platform_PS4.c +++ b/src/Platform_PS4.c @@ -1,6 +1,7 @@ #include "Core.h" #if defined CC_BUILD_PS4 +#define CC_XTEA_ENCRYPTION #include "_PlatformBase.h" #include "Stream.h" #include "ExtMath.h" diff --git a/src/Platform_PSP.c b/src/Platform_PSP.c index 8b9fd62a2..1976868e1 100644 --- a/src/Platform_PSP.c +++ b/src/Platform_PSP.c @@ -1,5 +1,7 @@ #include "Core.h" #if defined CC_BUILD_PSP + +#define CC_XTEA_ENCRYPTION #include "_PlatformBase.h" #include "Stream.h" #include "ExtMath.h" diff --git a/src/Platform_PSVita.c b/src/Platform_PSVita.c index f8431f2f5..eb028ee40 100644 --- a/src/Platform_PSVita.c +++ b/src/Platform_PSVita.c @@ -1,5 +1,7 @@ #include "Core.h" #if defined CC_BUILD_PSVITA + +#define CC_XTEA_ENCRYPTION #include "_PlatformBase.h" #include "Stream.h" #include "ExtMath.h" diff --git a/src/Platform_Posix.c b/src/Platform_Posix.c index 7c07f0dfa..1a2842f9a 100644 --- a/src/Platform_Posix.c +++ b/src/Platform_Posix.c @@ -1,6 +1,7 @@ #include "Core.h" #if defined CC_BUILD_POSIX +#define CC_XTEA_ENCRYPTION #include "_PlatformBase.h" #include "Stream.h" #include "ExtMath.h" @@ -1398,41 +1399,6 @@ void Platform_Init(void) { /*########################################################################################################################* *-------------------------------------------------------Encryption--------------------------------------------------------* *#########################################################################################################################*/ -/* Encrypts data using XTEA block cipher, with OS specific method to get machine-specific key */ - -static void EncipherBlock(cc_uint32* v, const cc_uint32* key, cc_string* dst) { - cc_uint32 v0 = v[0], v1 = v[1], sum = 0, delta = 0x9E3779B9; - int i; - - for (i = 0; i < 12; i++) - { - v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + key[sum & 3]); - sum += delta; - v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + key[(sum>>11) & 3]); - } - v[0] = v0; v[1] = v1; - String_AppendAll(dst, v, 8); -} - -static void DecipherBlock(cc_uint32* v, const cc_uint32* key) { - cc_uint32 v0 = v[0], v1 = v[1], delta = 0x9E3779B9, sum = delta * 12; - int i; - - for (i = 0; i < 12; i++) - { - v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + key[(sum>>11) & 3]); - sum -= delta; - v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + key[sum & 3]); - } - v[0] = v0; v[1] = v1; -} - -#define ENC1 0xCC005EC0 -#define ENC2 0x0DA4A0DE -#define ENC3 0xC0DED000 -#define MACHINEID_LEN 32 -#define ENC_SIZE 8 /* 2 32 bit ints per block */ - /* "b3 c5a-0d9" --> 0xB3C5A0D9 */ static void DecodeMachineID(char* tmp, int len, cc_uint32* key) { int hex[MACHINEID_LEN] = { 0 }, i, j, c; @@ -1578,57 +1544,6 @@ static cc_result GetMachineID(cc_uint32* key) { static cc_result GetMachineID(cc_uint32* key) { return ERR_NOT_SUPPORTED; } #endif -cc_result Platform_Encrypt(const void* data, int len, cc_string* dst) { - const cc_uint8* src = (const cc_uint8*)data; - cc_uint32 header[4], key[4]; - cc_result res; - if ((res = GetMachineID(key))) return res; - - header[0] = ENC1; header[1] = ENC2; - header[2] = ENC3; header[3] = len; - EncipherBlock(header + 0, key, dst); - EncipherBlock(header + 2, key, dst); - - for (; len > 0; len -= ENC_SIZE, src += ENC_SIZE) - { - header[0] = 0; header[1] = 0; - Mem_Copy(header, src, min(len, ENC_SIZE)); - EncipherBlock(header, key, dst); - } - return 0; -} - -cc_result Platform_Decrypt(const void* data, int len, cc_string* dst) { - const cc_uint8* src = (const cc_uint8*)data; - cc_uint32 header[4], key[4]; - cc_result res; - int dataLen; - - /* Total size must be >= header size */ - if (len < 16) return ERR_END_OF_STREAM; - if ((res = GetMachineID(key))) return res; - - Mem_Copy(header, src, 16); - DecipherBlock(header + 0, key); - DecipherBlock(header + 2, key); - - if (header[0] != ENC1 || header[1] != ENC2 || header[2] != ENC3) return ERR_INVALID_ARGUMENT; - len -= 16; src += 16; - - if (header[3] > len) return ERR_INVALID_ARGUMENT; - dataLen = header[3]; - - for (; dataLen > 0; len -= ENC_SIZE, src += ENC_SIZE, dataLen -= ENC_SIZE) - { - header[0] = 0; header[1] = 0; - Mem_Copy(header, src, min(len, ENC_SIZE)); - - DecipherBlock(header, key); - String_AppendAll(dst, header, min(dataLen, ENC_SIZE)); - } - return 0; -} - cc_result Platform_GetEntropy(void* data, int len) { int ret; int fd = open("/dev/urandom", O_RDONLY); diff --git a/src/Platform_Switch.c b/src/Platform_Switch.c index d2914c73d..cf1fa9405 100644 --- a/src/Platform_Switch.c +++ b/src/Platform_Switch.c @@ -1,5 +1,7 @@ #include "Core.h" #if defined CC_BUILD_SWITCH + +#define CC_XTEA_ENCRYPTION #include "_PlatformBase.h" #include "Stream.h" #include "ExtMath.h" diff --git a/src/Platform_WiiU.c b/src/Platform_WiiU.c index 4dd453d47..66b86b10b 100644 --- a/src/Platform_WiiU.c +++ b/src/Platform_WiiU.c @@ -1,6 +1,7 @@ #include "Core.h" #if defined CC_BUILD_WIIU +#define CC_XTEA_ENCRYPTION #include "_PlatformBase.h" #include "Stream.h" #include "ExtMath.h" diff --git a/src/Platform_Xbox.c b/src/Platform_Xbox.c index 319017a5b..34b8957f9 100644 --- a/src/Platform_Xbox.c +++ b/src/Platform_Xbox.c @@ -1,5 +1,7 @@ #include "Core.h" #if defined CC_BUILD_XBOX + +#define CC_XTEA_ENCRYPTION #include "_PlatformBase.h" #include "Stream.h" #include "Funcs.h" diff --git a/src/Platform_Xbox360.c b/src/Platform_Xbox360.c index 2eb1b5e4f..025b40536 100644 --- a/src/Platform_Xbox360.c +++ b/src/Platform_Xbox360.c @@ -1,5 +1,7 @@ #include "Core.h" #if defined CC_BUILD_XBOX360 + +#define CC_XTEA_ENCRYPTION #include "_PlatformBase.h" #include "Stream.h" #include "Funcs.h" diff --git a/src/_GraphicsBase.h b/src/_GraphicsBase.h index 75f26a42b..f6e1c9ca9 100644 --- a/src/_GraphicsBase.h +++ b/src/_GraphicsBase.h @@ -243,8 +243,9 @@ void Gfx_Make2DQuad(const struct Texture* tex, PackedCol color, struct VertexTex static cc_bool gfx_hadFog; void Gfx_Begin2D(int width, int height) { - gfx_rendering2D = true; struct Matrix ortho; + gfx_rendering2D = true; + /* intentionally biased more towards positive Z to reduce 2D clipping issues on the DS */ Gfx_CalcOrthoMatrix(&ortho, (float)width, (float)height, -100.0f, 1000.0f); Gfx_LoadMatrix(MATRIX_PROJ, &ortho); diff --git a/src/_PlatformBase.h b/src/_PlatformBase.h index 7c846d33b..444f15734 100644 --- a/src/_PlatformBase.h +++ b/src/_PlatformBase.h @@ -3,6 +3,7 @@ #include "Logger.h" #include "Constants.h" #include "Errors.h" +#include "Funcs.h" #if CC_BUILD_MAXSTACK < (64 * 1024) CC_BIG_VAR char temp_mem[45000]; @@ -183,3 +184,96 @@ cc_bool DynamicLib_LoadAll(const cc_string* path, const struct DynamicLibSym* sy } return foundAllRequired; } + + +/*########################################################################################################################* +*-------------------------------------------------------Encryption--------------------------------------------------------* +*#########################################################################################################################*/ +#ifdef CC_XTEA_ENCRYPTION + +/* Encrypts data using XTEA block cipher, with OS specific method to get machine-specific key */ +static void EncipherBlock(cc_uint32* v, const cc_uint32* key, cc_string* dst) { + cc_uint32 v0 = v[0], v1 = v[1], sum = 0, delta = 0x9E3779B9; + + for (int i = 0; i < 12; i++) + { + v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + key[sum & 3]); + sum += delta; + v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + key[(sum>>11) & 3]); + } + v[0] = v0; v[1] = v1; + String_AppendAll(dst, v, 8); +} + +static void DecipherBlock(cc_uint32* v, const cc_uint32* key) { + cc_uint32 v0 = v[0], v1 = v[1], delta = 0x9E3779B9, sum = delta * 12; + + for (int i = 0; i < 12; i++) + { + v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + key[(sum>>11) & 3]); + sum -= delta; + v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + key[sum & 3]); + } + v[0] = v0; v[1] = v1; +} + +#define ENC1 0xCC005EC0 +#define ENC2 0x0DA4A0DE +#define ENC3 0xC0DED000 +#define MACHINEID_LEN 32 +#define ENC_SIZE 8 /* 2 32 bit ints per block */ + +static cc_result GetMachineID(cc_uint32* key); + +cc_result Platform_Encrypt(const void* data, int len, cc_string* dst) { + const cc_uint8* src = (const cc_uint8*)data; + cc_uint32 header[4], key[4] = { 0 }; + cc_result res; + if ((res = GetMachineID(key))) return res; + + header[0] = ENC1; header[1] = ENC2; + header[2] = ENC3; header[3] = len; + EncipherBlock(header + 0, key, dst); + EncipherBlock(header + 2, key, dst); + + for (; len > 0; len -= ENC_SIZE, src += ENC_SIZE) + { + header[0] = 0; header[1] = 0; + Mem_Copy(header, src, min(len, ENC_SIZE)); + EncipherBlock(header, key, dst); + } + return 0; +} + +cc_result Platform_Decrypt(const void* data, int len, cc_string* dst) { + const cc_uint8* src = (const cc_uint8*)data; + cc_uint32 header[4], key[4] = { 0 }; + cc_result res; + int dataLen; + + /* Total size must be >= header size */ + if (len < 16) return ERR_END_OF_STREAM; + if ((res = GetMachineID(key))) return res; + + Mem_Copy(header, src, 16); + DecipherBlock(header + 0, key); + DecipherBlock(header + 2, key); + + if (header[0] != ENC1 || header[1] != ENC2 || header[2] != ENC3) return ERR_INVALID_ARGUMENT; + len -= 16; src += 16; + + if (header[3] > len) return ERR_INVALID_ARGUMENT; + dataLen = header[3]; + + for (; dataLen > 0; len -= ENC_SIZE, src += ENC_SIZE, dataLen -= ENC_SIZE) + { + header[0] = 0; header[1] = 0; + Mem_Copy(header, src, min(len, ENC_SIZE)); + + DecipherBlock(header, key); + String_AppendAll(dst, header, min(dataLen, ENC_SIZE)); + } + return 0; +} +#endif + diff --git a/src/_PlatformConsole.h b/src/_PlatformConsole.h index e42c52e84..46ce5dfe3 100644 --- a/src/_PlatformConsole.h +++ b/src/_PlatformConsole.h @@ -109,95 +109,12 @@ cc_bool DynamicLib_DescribeError(cc_string* dst) { } -/* TODO: Copy pasted from Platform_Posix.c, bad idea */ /*########################################################################################################################* *-------------------------------------------------------Encryption--------------------------------------------------------* *#########################################################################################################################*/ -/* Encrypts data using XTEA block cipher, with OS specific method to get machine-specific key */ -static void EncipherBlock(cc_uint32* v, const cc_uint32* key, cc_string* dst) { - cc_uint32 v0 = v[0], v1 = v[1], sum = 0, delta = 0x9E3779B9; - - for (int i = 0; i < 12; i++) - { - v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + key[sum & 3]); - sum += delta; - v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + key[(sum>>11) & 3]); - } - v[0] = v0; v[1] = v1; - String_AppendAll(dst, v, 8); -} - -static void DecipherBlock(cc_uint32* v, const cc_uint32* key) { - cc_uint32 v0 = v[0], v1 = v[1], delta = 0x9E3779B9, sum = delta * 12; - - for (int i = 0; i < 12; i++) - { - v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + key[(sum>>11) & 3]); - sum -= delta; - v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + key[sum & 3]); - } - v[0] = v0; v[1] = v1; -} - -#define ENC1 0xCC005EC0 -#define ENC2 0x0DA4A0DE -#define ENC3 0xC0DED000 -#define MACHINEID_LEN 32 -#define ENC_SIZE 8 /* 2 32 bit ints per block */ - -static cc_result GetMachineID(cc_uint32* key); - -cc_result Platform_Encrypt(const void* data, int len, cc_string* dst) { - const cc_uint8* src = (const cc_uint8*)data; - cc_uint32 header[4], key[4] = { 0 }; - cc_result res; - if ((res = GetMachineID(key))) return res; - - header[0] = ENC1; header[1] = ENC2; - header[2] = ENC3; header[3] = len; - EncipherBlock(header + 0, key, dst); - EncipherBlock(header + 2, key, dst); - - for (; len > 0; len -= ENC_SIZE, src += ENC_SIZE) { - header[0] = 0; header[1] = 0; - Mem_Copy(header, src, min(len, ENC_SIZE)); - EncipherBlock(header, key, dst); - } - return 0; -} - -cc_result Platform_Decrypt(const void* data, int len, cc_string* dst) { - const cc_uint8* src = (const cc_uint8*)data; - cc_uint32 header[4], key[4] = { 0 }; - cc_result res; - int dataLen; - - /* Total size must be >= header size */ - if (len < 16) return ERR_END_OF_STREAM; - if ((res = GetMachineID(key))) return res; - - Mem_Copy(header, src, 16); - DecipherBlock(header + 0, key); - DecipherBlock(header + 2, key); - - if (header[0] != ENC1 || header[1] != ENC2 || header[2] != ENC3) return ERR_INVALID_ARGUMENT; - len -= 16; src += 16; - - if (header[3] > len) return ERR_INVALID_ARGUMENT; - dataLen = header[3]; - - for (; dataLen > 0; len -= ENC_SIZE, src += ENC_SIZE, dataLen -= ENC_SIZE) { - header[0] = 0; header[1] = 0; - Mem_Copy(header, src, min(len, ENC_SIZE)); - - DecipherBlock(header, key); - String_AppendAll(dst, header, min(dataLen, ENC_SIZE)); - } - return 0; -} - #ifndef CC_BUILD_3DS cc_result Platform_GetEntropy(void* data, int len) { return ERR_NOT_SUPPORTED; } #endif +