mirror of
https://github.com/ClassiCube/ClassiCube.git
synced 2025-08-04 03:07:07 -04:00
Avoid duplicating encryption code for POSIX and Console systems
This commit is contained in:
parent
9284db5b65
commit
b3d94e6abe
@ -1,7 +1,7 @@
|
|||||||
#include "Core.h"
|
#include "Core.h"
|
||||||
#if defined PLAT_32X
|
#if defined PLAT_32X
|
||||||
#define OVERRIDE_MEM_FUNCTIONS
|
|
||||||
|
|
||||||
|
#define OVERRIDE_MEM_FUNCTIONS
|
||||||
#include "_PlatformBase.h"
|
#include "_PlatformBase.h"
|
||||||
#include "Stream.h"
|
#include "Stream.h"
|
||||||
#include "ExtMath.h"
|
#include "ExtMath.h"
|
||||||
@ -269,14 +269,15 @@ cc_result Process_StartOpen(const cc_string* args) {
|
|||||||
return ERR_NOT_SUPPORTED;
|
return ERR_NOT_SUPPORTED;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cc_result Platform_Encrypt(const void* data, int len, cc_string* dst) {
|
||||||
|
return ERR_NOT_SUPPORTED;
|
||||||
|
}
|
||||||
|
|
||||||
/*########################################################################################################################*
|
cc_result Platform_Decrypt(const void* data, int len, cc_string* dst) {
|
||||||
*-------------------------------------------------------Encryption--------------------------------------------------------*
|
return ERR_NOT_SUPPORTED;
|
||||||
*#########################################################################################################################*/
|
}
|
||||||
#define MACHINE_KEY "32x32x32x32xSEGA"
|
|
||||||
|
|
||||||
static cc_result GetMachineID(cc_uint32* key) {
|
cc_result Platform_GetEntropy(void* data, int len) {
|
||||||
Mem_Copy(key, MACHINE_KEY, sizeof(MACHINE_KEY) - 1);
|
return ERR_NOT_SUPPORTED;
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
#include "Core.h"
|
#include "Core.h"
|
||||||
#if defined CC_BUILD_3DS
|
#if defined CC_BUILD_3DS
|
||||||
|
|
||||||
|
#define CC_XTEA_ENCRYPTION
|
||||||
#include "_PlatformBase.h"
|
#include "_PlatformBase.h"
|
||||||
#include "Stream.h"
|
#include "Stream.h"
|
||||||
#include "ExtMath.h"
|
#include "ExtMath.h"
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
#include "Core.h"
|
#include "Core.h"
|
||||||
#if defined CC_BUILD_DREAMCAST
|
#if defined CC_BUILD_DREAMCAST
|
||||||
|
|
||||||
|
#define CC_XTEA_ENCRYPTION
|
||||||
#include "_PlatformBase.h"
|
#include "_PlatformBase.h"
|
||||||
#include "Stream.h"
|
#include "Stream.h"
|
||||||
#include "ExtMath.h"
|
#include "ExtMath.h"
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
#include "Core.h"
|
#include "Core.h"
|
||||||
#if defined CC_BUILD_GBA
|
#if defined CC_BUILD_GBA
|
||||||
|
|
||||||
#include "_PlatformBase.h"
|
#include "_PlatformBase.h"
|
||||||
#include "Stream.h"
|
#include "Stream.h"
|
||||||
#include "ExtMath.h"
|
#include "ExtMath.h"
|
||||||
@ -256,14 +257,15 @@ cc_result Process_StartOpen(const cc_string* args) {
|
|||||||
return ERR_NOT_SUPPORTED;
|
return ERR_NOT_SUPPORTED;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cc_result Platform_Encrypt(const void* data, int len, cc_string* dst) {
|
||||||
|
return ERR_NOT_SUPPORTED;
|
||||||
|
}
|
||||||
|
|
||||||
/*########################################################################################################################*
|
cc_result Platform_Decrypt(const void* data, int len, cc_string* dst) {
|
||||||
*-------------------------------------------------------Encryption--------------------------------------------------------*
|
return ERR_NOT_SUPPORTED;
|
||||||
*#########################################################################################################################*/
|
}
|
||||||
#define MACHINE_KEY "GBA_GBA_GBA_GBA_"
|
|
||||||
|
|
||||||
static cc_result GetMachineID(cc_uint32* key) {
|
cc_result Platform_GetEntropy(void* data, int len) {
|
||||||
Mem_Copy(key, MACHINE_KEY, sizeof(MACHINE_KEY) - 1);
|
return ERR_NOT_SUPPORTED;
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
#include "Core.h"
|
#include "Core.h"
|
||||||
#if defined CC_BUILD_GCWII
|
#if defined CC_BUILD_GCWII
|
||||||
|
|
||||||
|
#define CC_XTEA_ENCRYPTION
|
||||||
#include "_PlatformBase.h"
|
#include "_PlatformBase.h"
|
||||||
#include "Stream.h"
|
#include "Stream.h"
|
||||||
#include "ExtMath.h"
|
#include "ExtMath.h"
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
#include "Core.h"
|
#include "Core.h"
|
||||||
#if defined CC_BUILD_N64
|
#if defined CC_BUILD_N64
|
||||||
|
|
||||||
|
#define CC_XTEA_ENCRYPTION
|
||||||
#include "_PlatformBase.h"
|
#include "_PlatformBase.h"
|
||||||
#include "Stream.h"
|
#include "Stream.h"
|
||||||
#include "ExtMath.h"
|
#include "ExtMath.h"
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
#include "Core.h"
|
#include "Core.h"
|
||||||
#if defined CC_BUILD_NDS
|
#if defined CC_BUILD_NDS
|
||||||
|
|
||||||
|
#define CC_XTEA_ENCRYPTION
|
||||||
#include "_PlatformBase.h"
|
#include "_PlatformBase.h"
|
||||||
#include "Stream.h"
|
#include "Stream.h"
|
||||||
#include "ExtMath.h"
|
#include "ExtMath.h"
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#include "Core.h"
|
#include "Core.h"
|
||||||
#if defined PLAT_PS1
|
#if defined PLAT_PS1
|
||||||
|
|
||||||
|
#define CC_XTEA_ENCRYPTION
|
||||||
#include "_PlatformBase.h"
|
#include "_PlatformBase.h"
|
||||||
#include "Stream.h"
|
#include "Stream.h"
|
||||||
#include "ExtMath.h"
|
#include "ExtMath.h"
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#include "Core.h"
|
#include "Core.h"
|
||||||
#if defined PLAT_PS3
|
#if defined PLAT_PS3
|
||||||
|
|
||||||
|
#define CC_XTEA_ENCRYPTION
|
||||||
#include "_PlatformBase.h"
|
#include "_PlatformBase.h"
|
||||||
#include "Stream.h"
|
#include "Stream.h"
|
||||||
#include "ExtMath.h"
|
#include "ExtMath.h"
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#include "Core.h"
|
#include "Core.h"
|
||||||
#if defined CC_BUILD_PS4
|
#if defined CC_BUILD_PS4
|
||||||
|
|
||||||
|
#define CC_XTEA_ENCRYPTION
|
||||||
#include "_PlatformBase.h"
|
#include "_PlatformBase.h"
|
||||||
#include "Stream.h"
|
#include "Stream.h"
|
||||||
#include "ExtMath.h"
|
#include "ExtMath.h"
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
#include "Core.h"
|
#include "Core.h"
|
||||||
#if defined CC_BUILD_PSP
|
#if defined CC_BUILD_PSP
|
||||||
|
|
||||||
|
#define CC_XTEA_ENCRYPTION
|
||||||
#include "_PlatformBase.h"
|
#include "_PlatformBase.h"
|
||||||
#include "Stream.h"
|
#include "Stream.h"
|
||||||
#include "ExtMath.h"
|
#include "ExtMath.h"
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
#include "Core.h"
|
#include "Core.h"
|
||||||
#if defined CC_BUILD_PSVITA
|
#if defined CC_BUILD_PSVITA
|
||||||
|
|
||||||
|
#define CC_XTEA_ENCRYPTION
|
||||||
#include "_PlatformBase.h"
|
#include "_PlatformBase.h"
|
||||||
#include "Stream.h"
|
#include "Stream.h"
|
||||||
#include "ExtMath.h"
|
#include "ExtMath.h"
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#include "Core.h"
|
#include "Core.h"
|
||||||
#if defined CC_BUILD_POSIX
|
#if defined CC_BUILD_POSIX
|
||||||
|
|
||||||
|
#define CC_XTEA_ENCRYPTION
|
||||||
#include "_PlatformBase.h"
|
#include "_PlatformBase.h"
|
||||||
#include "Stream.h"
|
#include "Stream.h"
|
||||||
#include "ExtMath.h"
|
#include "ExtMath.h"
|
||||||
@ -1398,41 +1399,6 @@ void Platform_Init(void) {
|
|||||||
/*########################################################################################################################*
|
/*########################################################################################################################*
|
||||||
*-------------------------------------------------------Encryption--------------------------------------------------------*
|
*-------------------------------------------------------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 */
|
/* "b3 c5a-0d9" --> 0xB3C5A0D9 */
|
||||||
static void DecodeMachineID(char* tmp, int len, cc_uint32* key) {
|
static void DecodeMachineID(char* tmp, int len, cc_uint32* key) {
|
||||||
int hex[MACHINEID_LEN] = { 0 }, i, j, c;
|
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; }
|
static cc_result GetMachineID(cc_uint32* key) { return ERR_NOT_SUPPORTED; }
|
||||||
#endif
|
#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) {
|
cc_result Platform_GetEntropy(void* data, int len) {
|
||||||
int ret;
|
int ret;
|
||||||
int fd = open("/dev/urandom", O_RDONLY);
|
int fd = open("/dev/urandom", O_RDONLY);
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
#include "Core.h"
|
#include "Core.h"
|
||||||
#if defined CC_BUILD_SWITCH
|
#if defined CC_BUILD_SWITCH
|
||||||
|
|
||||||
|
#define CC_XTEA_ENCRYPTION
|
||||||
#include "_PlatformBase.h"
|
#include "_PlatformBase.h"
|
||||||
#include "Stream.h"
|
#include "Stream.h"
|
||||||
#include "ExtMath.h"
|
#include "ExtMath.h"
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#include "Core.h"
|
#include "Core.h"
|
||||||
#if defined CC_BUILD_WIIU
|
#if defined CC_BUILD_WIIU
|
||||||
|
|
||||||
|
#define CC_XTEA_ENCRYPTION
|
||||||
#include "_PlatformBase.h"
|
#include "_PlatformBase.h"
|
||||||
#include "Stream.h"
|
#include "Stream.h"
|
||||||
#include "ExtMath.h"
|
#include "ExtMath.h"
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
#include "Core.h"
|
#include "Core.h"
|
||||||
#if defined CC_BUILD_XBOX
|
#if defined CC_BUILD_XBOX
|
||||||
|
|
||||||
|
#define CC_XTEA_ENCRYPTION
|
||||||
#include "_PlatformBase.h"
|
#include "_PlatformBase.h"
|
||||||
#include "Stream.h"
|
#include "Stream.h"
|
||||||
#include "Funcs.h"
|
#include "Funcs.h"
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
#include "Core.h"
|
#include "Core.h"
|
||||||
#if defined CC_BUILD_XBOX360
|
#if defined CC_BUILD_XBOX360
|
||||||
|
|
||||||
|
#define CC_XTEA_ENCRYPTION
|
||||||
#include "_PlatformBase.h"
|
#include "_PlatformBase.h"
|
||||||
#include "Stream.h"
|
#include "Stream.h"
|
||||||
#include "Funcs.h"
|
#include "Funcs.h"
|
||||||
|
@ -243,8 +243,9 @@ void Gfx_Make2DQuad(const struct Texture* tex, PackedCol color, struct VertexTex
|
|||||||
static cc_bool gfx_hadFog;
|
static cc_bool gfx_hadFog;
|
||||||
|
|
||||||
void Gfx_Begin2D(int width, int height) {
|
void Gfx_Begin2D(int width, int height) {
|
||||||
gfx_rendering2D = true;
|
|
||||||
struct Matrix ortho;
|
struct Matrix ortho;
|
||||||
|
gfx_rendering2D = true;
|
||||||
|
|
||||||
/* intentionally biased more towards positive Z to reduce 2D clipping issues on the DS */
|
/* 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_CalcOrthoMatrix(&ortho, (float)width, (float)height, -100.0f, 1000.0f);
|
||||||
Gfx_LoadMatrix(MATRIX_PROJ, &ortho);
|
Gfx_LoadMatrix(MATRIX_PROJ, &ortho);
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
#include "Logger.h"
|
#include "Logger.h"
|
||||||
#include "Constants.h"
|
#include "Constants.h"
|
||||||
#include "Errors.h"
|
#include "Errors.h"
|
||||||
|
#include "Funcs.h"
|
||||||
|
|
||||||
#if CC_BUILD_MAXSTACK < (64 * 1024)
|
#if CC_BUILD_MAXSTACK < (64 * 1024)
|
||||||
CC_BIG_VAR char temp_mem[45000];
|
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;
|
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
|
||||||
|
|
||||||
|
@ -109,95 +109,12 @@ cc_bool DynamicLib_DescribeError(cc_string* dst) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* TODO: Copy pasted from Platform_Posix.c, bad idea */
|
|
||||||
/*########################################################################################################################*
|
/*########################################################################################################################*
|
||||||
*-------------------------------------------------------Encryption--------------------------------------------------------*
|
*-------------------------------------------------------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
|
#ifndef CC_BUILD_3DS
|
||||||
cc_result Platform_GetEntropy(void* data, int len) {
|
cc_result Platform_GetEntropy(void* data, int len) {
|
||||||
return ERR_NOT_SUPPORTED;
|
return ERR_NOT_SUPPORTED;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user