Skip to content

Commit 84ee4f9

Browse files
committed
UPBGE: Fix basic functioning. Now crypt, decrypt and launch player
1 parent db065f9 commit 84ee4f9

File tree

3 files changed

+121
-33
lines changed

3 files changed

+121
-33
lines changed

CMakeLists.txt

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,33 @@ project(UPBGE_BinaryCrypt C)
44
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")
55
find_package(raylib REQUIRED)
66

7-
# -- Generate random password if not defined --
8-
if(NOT DEFINED ENV{UPBGE_BINARYCRYPT_PASSWORD})
9-
execute_process(COMMAND openssl rand -hex 32
10-
OUTPUT_VARIABLE GENERATED_PW
11-
OUTPUT_STRIP_TRAILING_WHITESPACE)
12-
set(ENV{UPBGE_BINARYCRYPT_PASSWORD} "${GENERATED_PW}")
13-
endif()
7+
# -- Generate random password --
8+
execute_process(COMMAND openssl rand -hex 32
9+
OUTPUT_VARIABLE GENERATED_PW
10+
OUTPUT_STRIP_TRAILING_WHITESPACE)
1411

15-
# -- Generate random salt if not defined --
16-
if(NOT DEFINED ENV{UPBGE_BINARYCRYPT_SALT})
17-
execute_process(COMMAND openssl rand -hex 16
18-
OUTPUT_VARIABLE GENERATED_SALT
19-
OUTPUT_STRIP_TRAILING_WHITESPACE)
20-
set(ENV{UPBGE_BINARYCRYPT_SALT} ${GENERATED_SALT})
21-
endif()
12+
# -- Generate random salt --
13+
execute_process(COMMAND openssl rand -hex 16
14+
OUTPUT_VARIABLE GENERATED_SALT
15+
OUTPUT_STRIP_TRAILING_WHITESPACE)
16+
17+
# -- Show values --
18+
message(STATUS "Generated Password: ${GENERATED_PW}")
19+
message(STATUS "Generated Salt HEX: ${GENERATED_SALT}")
2220

23-
# -- Compilation definitions --
24-
set(PASSWORD "$ENV{UPBGE_BINARYCRYPT_PASSWORD}")
25-
set(SALT_HEX $ENV{UPBGE_BINARYCRYPT_SALT})
21+
# -- Convert salt hex string to comma-separated bytes --
22+
string(REGEX REPLACE "([0-9a-fA-F][0-9a-fA-F])" "0x\\1, " SALT_BYTES "${GENERATED_SALT}")
23+
string(REGEX REPLACE ", $" "" SALT_BYTES "${SALT_BYTES}") # remove trailing comma
2624

27-
string(REGEX REPLACE "([0-9a-f][0-9a-f])" "0x\\1, " SALT_BYTES "${SALT_HEX}")
25+
add_compile_definitions(ENCRYPTION_PASSWORD="${GENERATED_PW}")
26+
add_compile_definitions(ENCRYPTION_SALT=${SALT_BYTES})
2827

29-
#add_compile_definitions(ENCRYPTION_PASSWORD="${PASSWORD}")
30-
#add_compile_definitions(ENCRYPTION_SALT=${SALT_BYTES})
28+
#add_compile_definitions(ENCRYPTION_PASSWORD="mi_contraseña_secreta")
29+
#add_compile_definitions(ENCRYPTION_SALT="0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F")
3130

32-
add_compile_definitions(ENCRYPTION_PASSWORD="mi_contraseña_secreta")
33-
add_compile_definitions(ENCRYPTION_SALT="0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F")
31+
message(STATUS "Salt bytes: ${SALT_BYTES}")
32+
message(STATUS "ENCRYPTION_PASSWORD: ${ENCRYPTION_PASSWORD}")
33+
message(STATUS "ENCRYPTION_SALT: ${ENCRYPTION_SALT}")
3434

3535
add_subdirectory(aes)
3636

Lines changed: 83 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,57 @@
11
#include <stdio.h>
22
#include <stdlib.h>
33
#include <string.h>
4+
#include <errno.h>
45
#include "aes.h"
56

67
#define SALT_LEN 16
78

89
static const char *PASSWORD_STR = ENCRYPTION_PASSWORD;
9-
static const uint8_t salt[] = { ENCRYPTION_SALT };
10+
static const uint8_t salt[16] = {ENCRYPTION_SALT};
1011

1112
void derive_key(uint8_t *key_out) {
1213
for (int i = 0; i < 32; i++) {
1314
key_out[i] = (uint8_t)(PASSWORD_STR[i % strlen(PASSWORD_STR)] ^ salt[i % SALT_LEN]);
1415
}
1516
}
1617

17-
void decrypt_file(const char *input, const char *output) {
18+
static int decrypt_file(const char *input, const char *output) {
1819
FILE *fin = fopen(input, "rb");
19-
if (!fin) return;
20+
if (!fin) {
21+
fprintf(stderr, "Error al abrir archivo de entrada '%s': %s\n", input, strerror(errno));
22+
return 1;
23+
}
2024
fseek(fin, 0, SEEK_END);
2125
long len = ftell(fin);
2226
rewind(fin);
2327

28+
if (len <= 0) {
29+
fprintf(stderr, "Archivo de entrada vacío o inválido\n");
30+
fclose(fin);
31+
return 1;
32+
}
33+
34+
// Ajustar para múltiplos de 16 para AES ECB
35+
if (len % 16 != 0) {
36+
fprintf(stderr, "El archivo no es múltiplo de 16 bytes, AES ECB requiere múltiplos de 16\n");
37+
fclose(fin);
38+
return 1;
39+
}
40+
2441
uint8_t *buffer = malloc(len);
25-
fread(buffer, 1, len, fin);
42+
if (!buffer) {
43+
fprintf(stderr, "Error asignando memoria\n");
44+
fclose(fin);
45+
return 1;
46+
}
47+
48+
size_t read_bytes = fread(buffer, 1, len, fin);
2649
fclose(fin);
50+
if (read_bytes != len) {
51+
fprintf(stderr, "Error leyendo archivo de entrada\n");
52+
free(buffer);
53+
return 1;
54+
}
2755

2856
struct AES_ctx ctx;
2957
uint8_t key[32];
@@ -34,19 +62,67 @@ void decrypt_file(const char *input, const char *output) {
3462
AES_ECB_decrypt(&ctx, buffer + i);
3563
}
3664

65+
// Obtener valor de padding (último byte)
66+
uint8_t pad_val = buffer[len - 1];
67+
if (pad_val == 0 || pad_val > 16) {
68+
fprintf(stderr, "Error: padding inválido\n");
69+
free(buffer);
70+
return 1;
71+
}
72+
73+
// Validar padding PKCS#7
74+
for (long i = len - pad_val; i < len; i++) {
75+
if (buffer[i] != pad_val) {
76+
fprintf(stderr, "Error: padding corrupto\n");
77+
free(buffer);
78+
return 1;
79+
}
80+
}
81+
82+
long output_len = len - pad_val;
83+
3784
FILE *fout = fopen(output, "wb");
38-
fwrite(buffer, 1, len, fout);
85+
if (!fout) {
86+
fprintf(stderr, "Error al abrir archivo de salida '%s': %s\n", output, strerror(errno));
87+
free(buffer);
88+
return 1;
89+
}
90+
fwrite(buffer, 1, output_len, fout);
3991
fclose(fout);
4092
free(buffer);
93+
printf("Archivo desencriptado creado correctamente: %s\n", output);
94+
return 0;
95+
}
96+
97+
int launch_blenderplayer(const char *blend_file) {
98+
char cmd[512];
99+
100+
#if defined(_WIN32)
101+
snprintf(cmd, sizeof(cmd), "blenderplayer\\blenderplayer.exe \"%s\"", blend_file);
102+
#else
103+
snprintf(cmd, sizeof(cmd), "./blenderplayer/blenderplayer \"%s\"", blend_file);
104+
#endif
105+
106+
printf("Ejecutando: %s\n", cmd);
107+
int ret = system(cmd);
108+
if (ret != 0) {
109+
fprintf(stderr, "Error ejecutando blenderplayer (code %d)\n", ret);
110+
}
111+
return ret;
41112
}
42113

43114
int main(void) {
44115
const char *encrypted = "game_encrypted.block";
45116
const char *decrypted = "game_decrypted.blend";
46117

47-
decrypt_file(encrypted, decrypted);
118+
if (decrypt_file(encrypted, decrypted)) {
119+
fprintf(stderr, "Error encriptando el archivo. No se lanza el blenderplayer: %s\n", strerror(errno));
120+
return 1;
121+
}
122+
123+
printf("Ejecutando blenderplayer con archivo %s\n", decrypted);
124+
return launch_blenderplayer(decrypted);
48125

49-
//execlp("blenderplayer", "blenderplayer", decrypted, NULL);
50126
return 0;
51127
}
52128

upbge_binarycrypt_tool/main_tool.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#define SALT_LEN 16
88

99
static const char *PASSWORD_STR = ENCRYPTION_PASSWORD;
10-
static const uint8_t salt[] = {ENCRYPTION_SALT};
10+
static const uint8_t salt[16] = {ENCRYPTION_SALT};
1111

1212
static void derive_key(uint8_t *key_out) {
1313
for (int i = 0; i < 32; i++) {
@@ -26,21 +26,33 @@ static void encrypt_file(const char *input, const char *output) {
2626
fread(buffer, 1, len, fin);
2727
fclose(fin);
2828

29+
// Calculate padding PKCS#7
30+
int padding = 16 - (len % 16);
31+
if (padding == 0) padding = 16; // Add complete padding always
32+
long padded_len = len + padding;
33+
34+
uint8_t *buffer_padded = malloc(padded_len);
35+
memcpy(buffer_padded, buffer, len);
36+
memset(buffer_padded + len, padding, padding); // Filled with value 'padding'
37+
2938
struct AES_ctx ctx;
3039
uint8_t key[32];
3140
derive_key(key);
3241
AES_init_ctx(&ctx, key);
3342

34-
for (long i = 0; i < len; i += 16) {
35-
AES_ECB_encrypt(&ctx, buffer + i);
43+
for (long i = 0; i < padded_len; i += 16) {
44+
AES_ECB_encrypt(&ctx, buffer_padded + i);
3645
}
3746

3847
FILE *fout = fopen(output, "wb");
39-
fwrite(buffer, 1, len, fout);
48+
fwrite(buffer_padded, 1, padded_len, fout);
4049
fclose(fout);
50+
4151
free(buffer);
52+
free(buffer_padded);
4253
}
4354

55+
4456
int main(void) {
4557
const int screenWidth = 600;
4658
const int screenHeight = 200;

0 commit comments

Comments
 (0)