-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcczdecrypt.c
239 lines (190 loc) · 6.93 KB
/
cczdecrypt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include "libdeflate.h"
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
#define MX (((z >> 5 ^ y << 2) + (y >> 3 ^ z << 4)) ^ ((sum ^ y) + (s_uEncryptedPvrKeyParts[(p & 3) ^ e] ^ z)))
#define DELTA 0x9e3779b9
#define FREAD_CHUNK 0x100000
#define HEADER_SIZE 16
#define ENCRYPTED_OFFSET 12
#define assert(expr, str) \
if (!(expr)) { \
fprintf(stderr, str); \
return 1; \
}
struct __attribute__((scalar_storage_order("big-endian"))) CCZHeader {
u8 sig[4];
u16 compression_type;
u16 version;
u32 checksum;
u32 len;
};
u32 s_uEncryptedPvrKeyParts[4];
u32 s_uEncryptionKey[1024];
const u16 enclen = 1024;
void initKey(void) {
u32 y, p, e;
u32 rounds = 6;
u32 sum = 0;
u32 z = s_uEncryptionKey[enclen - 1];
do {
sum += DELTA;
e = (sum >> 2) & 3;
for (p = 0; p < enclen - 1; p++) {
y = s_uEncryptionKey[p + 1];
z = s_uEncryptionKey[p] += MX;
}
y = s_uEncryptionKey[0];
z = s_uEncryptionKey[enclen - 1] += MX;
} while (--rounds);
}
void decryptCCZ(u32 *data, u32 len) {
static const u32 securelen = 512;
static const u32 distance = 64;
u32 i = 0;
u16 b = 0;
for (; i < len && i < securelen; i++) {
data[i] ^= s_uEncryptionKey[b++];
if (b >= enclen)
b = 0;
}
for (; i < len; i += distance) {
data[i] ^= s_uEncryptionKey[b++];
if (b >= enclen)
b = 0;
}
}
u32 checksumCCZ(u32 *data, u32 len) {
static const u32 cslen = 128;
u32 cs = 0;
len = (len < cslen) ? len : cslen;
for (u32 i = 0; i < len; i++)
cs = cs ^ data[i];
return cs;
}
int main(int argc, char **argv) {
if (argc > 1 && strcmp(argv[1], "--help") == 0) {
printf("Usage: cczdecrypt <infile> <key> [options]\n"
"Decrypt Cocos2d encrypted CCZ `file` with given `key`.\n"
"`infile` can be absolute or relative to cwd.\n"
"`key` must be 32 hexadecimal digits.\n"
"Example: cczdecrypt data.pvr.ccz 83c1e28613d11f53ead45ea59da9fca3\n\n"
"Additional options:\n"
" -o[outfile] Write output to `outfile` instead of default\n"
" -i, --ignore-checksum Bypass CCZ checksum check\n");
return 0;
}
assert(argc >= 3, "Usage: cczdecrypt <file> <key> [options]\nType cczdecrypt --help for more information.\n");
u32 i;
u8 checksum = 1;
char *outfile = NULL;
for (i = 3; i < (u32)argc; i++) {
if (strcmp(argv[i], "-i") == 0 || strcmp(argv[i], "--ignore-checksum") == 0) {
checksum = 0;
} else if (argv[i][0] == '-' && argv[i][1] == 'o') {
outfile = argv[i] + 2;
} else {
fprintf(stderr, "Unrecognized argument %s.", argv[i]);
return 1;
}
}
assert(
strlen(argv[2]) == 32 && sscanf(argv[2], "%8x%8x%8x%8x", s_uEncryptedPvrKeyParts, s_uEncryptedPvrKeyParts + 1, s_uEncryptedPvrKeyParts + 2, s_uEncryptedPvrKeyParts + 3) == 4,
"Invalid key. Must be 32 hexadecimal digits.\n"
);
initKey();
u8 *buf = malloc(FREAD_CHUNK);
assert(buf, "Memory allocation failed.\n");
u32 l = strlen(argv[1]);
#ifdef _WIN32
while (argv[1][l - 1] == '.' || argv[1][l - 1] == ' ')
l--;
argv[1][l] = 0;
#endif
u32 len = 0, read;
FILE *infp = fopen(argv[1], "rb");
assert(infp, "File read error.\n");
while (1) {
read = fread(buf + len, 1, FREAD_CHUNK, infp);
if (read < FREAD_CHUNK) {
len += read;
break;
}
len += FREAD_CHUNK;
buf = realloc(buf, len + FREAD_CHUNK);
assert(buf, "Memory allocation failed.\n");
}
assert(!ferror(infp), "File read error.\n");
fclose(infp);
assert(len > HEADER_SIZE, "Invalid CCZ file.\n");
struct CCZHeader *header = (struct CCZHeader *)buf;
assert(header->sig[0] == 'C' && header->sig[1] == 'C' && header->sig[2] == 'Z', "Invalid CCZ file.");
decryptCCZ((u32 *)(buf + ENCRYPTED_OFFSET), (len - ENCRYPTED_OFFSET) >> 2);
assert(
!checksum || checksumCCZ((u32 *)(buf + ENCRYPTED_OFFSET), (len - ENCRYPTED_OFFSET) >> 2) == header->checksum,
"Checksum mismatch, check that your key is correct.\n"
);
u8 *out = malloc(header->len);
assert(out, "Memory allocation failed.\n");
struct libdeflate_decompressor *decompressor = libdeflate_alloc_decompressor();
assert(decompressor, "Decompressor allocation failed.\n");
int zerr = libdeflate_zlib_decompress(decompressor, buf + HEADER_SIZE, len - HEADER_SIZE, out, header->len, NULL);
switch (zerr) {
case LIBDEFLATE_BAD_DATA:
fprintf(stderr, "libdeflate error LIBDEFLATE_BAD_DATA.\n");
return 1;
case LIBDEFLATE_SHORT_OUTPUT:
fprintf(stderr, "libdeflate error LIBDEFLATE_SHORT_OUTPUT.\n");
return 1;
case LIBDEFLATE_INSUFFICIENT_SPACE:
fprintf(stderr, "libdeflate error LIBDEFLATE_INSUFFICIENT_SPACE.\n");
return 1;
}
if (outfile) {
argv[1] = outfile;
} else {
i = l;
while (i > 0 && argv[1][i - 1] != '/' && argv[1][i - 1] != '\\')
i--;
if ((argv[1][l - 1] == 'z' || argv[1][l - 1] == 'Z') && (argv[1][l - 2] == 'c' || argv[1][l - 2] == 'C') && (argv[1][l - 3] == 'c' || argv[1][l - 3] == 'C') && argv[1][l - 4] == '.') {
l = l - 4;
#ifdef _WIN32
while (l > i && (argv[1][l - 1] == '.' || argv[1][l - 1] == ' '))
l--;
#endif
if (l > i) {
argv[1][l] = 0;
} else {
char *outf = malloc(i + 11);
assert(outf, "Memory allocation failed.\n");
memcpy(outf, argv[1], i);
memcpy(outf + i, "decrypted_", 11);
argv[1] = outf;
}
} else {
char *outf = malloc(l + 11);
assert(outf, "Memory allocation failed.\n");
while (i > 0 && argv[1][i] != '/' && argv[1][i] != '\\')
i--;
i++;
if (i == 1 && (argv[1][i] == '/' || argv[1][i] == '\\'))
i = 0;
memcpy(outf, argv[1], i);
memcpy(outf + i, "decrypted_", 10);
memcpy(outf + i + 10, argv[1] + i, l - i + 1);
argv[1] = outf;
}
}
FILE *outfp = fopen(argv[1], "wb");
assert(
outfp && fwrite(out, 1, header->len, outfp) >= header->len && !ferror(outfp),
"File write error.\n"
);
fclose(outfp);
printf("Wrote decrypted file to %s.\n", argv[1]);
return 0;
}