-
Notifications
You must be signed in to change notification settings - Fork 13
/
desafenet.c
303 lines (254 loc) · 8.06 KB
/
desafenet.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/* desafenet
* A cross platform utility to handle E-SafeNet
* protected files.
*
* Written and placed into the public domain by
* Elias Oenal <[email protected]>
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <getopt.h>
#include <stdint.h>
#include <stdbool.h>
#include <inttypes.h>
#include "minilzo.h"
#define APPNAME "desafenet"
#define ESAFE_BLOCK 512
enum{
ESAFE_COMP_MODE_LZO = 0x62, // Data compressed using LZO
ESAFE_COMP_MODE_PSUB = 0x63 // Unknown pattern substitution algorithm
};
typedef struct safe_info{
bool is_esafe;
uint8_t mode;
uint16_t offset;
} safe_info;
safe_info verify_esafe_header(FILE* file);
bool recover_key(FILE* plaintext, FILE* ciphertext, uint8_t* key);
void esafe_crypt(uint8_t* data, const uint8_t* key, uint16_t len);
bool esafe_decompress(uint8_t* data, uint16_t size);
const char *esafe_mode_to_str(uint8_t mode);
static unsigned int verbosity = 0;
int main(int argc, char *argv[])
{
const char* keyfile_str = NULL;
const char* plaintext_str = NULL;
const char* ciphertext_str = NULL;
FILE* keyfile = NULL;
FILE* plaintext = NULL;
FILE* ciphertext = NULL;
int opt;
while((opt = getopt(argc, argv, "k:p:c:v")) != -1)
{
switch(opt)
{
case 'k':
keyfile_str = optarg;
keyfile = fopen(keyfile_str, "rb");
if(!keyfile_str || !keyfile)
goto err_abort;
break;
case 'p':
plaintext_str = optarg;
plaintext = fopen(plaintext_str, "rb");
if(!plaintext_str || !plaintext)
goto err_abort;
break;
case 'c':
ciphertext_str = optarg;
ciphertext = fopen(ciphertext_str, "rb");
if(!ciphertext_str || !ciphertext)
goto err_abort;
break;
case 'v':
verbosity = 1;
break;
}
}
safe_info info_cipher = {info_cipher.is_esafe = false, info_cipher.mode = 0, info_cipher.offset = 0};
if(ciphertext)
{
info_cipher = verify_esafe_header(ciphertext);
if(!info_cipher.is_esafe)
{
fprintf(stderr, "%s: %s: ciphertext either unencrypted or unsupported\n", APPNAME, ciphertext_str);
goto err_abort;
}
else if(verbosity > 0)
fprintf(stderr, "%s: %s: compression mode %s and data offset %u\n", APPNAME, ciphertext_str,
esafe_mode_to_str(info_cipher.mode), info_cipher.offset);
}
if(plaintext)
{
if(verify_esafe_header(plaintext).is_esafe)
{
fprintf(stderr, "%s: %s: plaintext input seems to be encrypted\n", APPNAME, plaintext_str);
goto err_abort;
}
}
if((keyfile?1:0) + (plaintext?1:0) + (ciphertext?1:0) != 2)
{
fprintf(stderr, "%s: two out of -k (key) -p (plaintext) and -c (ciphertext) required\n", APPNAME);
return 1;
}
if(plaintext && ciphertext)
{
uint8_t key[ESAFE_BLOCK];
bool success = recover_key(plaintext, ciphertext, key);
if(!success)
{
fprintf(stderr, "%s: failed to recover or verify key, try files of at least 1.5kb\n", APPNAME);
goto err_abort;
}
if(verbosity > 0)
fprintf(stderr, "%s: recovered and verified key\n", APPNAME);
fwrite(key, sizeof(uint8_t), sizeof(key), stdout);
}
else if(ciphertext && keyfile)
{
uint8_t key[ESAFE_BLOCK];
uint8_t data[ESAFE_BLOCK];
size_t read;
fseek(keyfile, 0, SEEK_SET);
if(fread(key, 1, ESAFE_BLOCK, keyfile) != sizeof(key))
{
fprintf(stderr, "%s: %s: invalid keyfile\n", APPNAME, keyfile_str);
goto err_abort;
}
fseek(ciphertext, info_cipher.offset, SEEK_SET);
read = fread(data, 1, sizeof(data) - info_cipher.offset, ciphertext);
esafe_crypt(data, key, read);
switch(info_cipher.mode)
{
case ESAFE_COMP_MODE_LZO:
{
bool status = esafe_decompress(data, read);
if(!status)
{
fprintf(stderr, "%s: error decompressing the data\n", APPNAME);
fwrite(data, sizeof(uint8_t), read, stdout);
goto err_abort;
}
fwrite(data, sizeof(uint8_t), ESAFE_BLOCK, stdout);
break;
}
case ESAFE_COMP_MODE_PSUB:
// Sadly we don't have the dictionary used for PSUB
default:
{
fprintf(stderr, "%s: %s: compression mode %s is unsupported for decompression, "
"dumping first block compressed and prefixed with 0x00\n", APPNAME, ciphertext_str,
esafe_mode_to_str(info_cipher.mode));
for(size_t i = 0; i < info_cipher.offset; i++)
fwrite("\0", 1, 1, stdout); // Padding
fwrite(data, sizeof(uint8_t), read, stdout);
break;
}
}
fseek(ciphertext, ESAFE_BLOCK * 1, SEEK_SET);
while(true)
{
read = fread(data, 1, sizeof(data), ciphertext);
esafe_crypt(data, key, read);
fwrite(data, sizeof(uint8_t), read, stdout);
if(!read || feof(ciphertext))
break;
}
}
else if(plaintext && keyfile)
{
fprintf(stderr, "%s: encryption currently unsupported\n", APPNAME);
goto err_abort;
}
return 0;
err_abort:
if(keyfile)
fclose(keyfile);
if(plaintext)
fclose(plaintext);
if(ciphertext)
fclose(ciphertext);
return 1;
}
void esafe_crypt(uint8_t* data, const uint8_t* key, uint16_t len)
{
for(size_t i = 0; i < len; i++)
data[i] ^= key[i];
}
bool esafe_decompress(uint8_t* data, uint16_t size)
{
lzo_uint out_len = ESAFE_BLOCK;
uint8_t buff[ESAFE_BLOCK] = {0};
int res = lzo1x_decompress_safe(data, size, buff, &out_len, NULL);
if(verbosity > 1)
fprintf(stderr, "res: %d siz: %d\n", res, size);
if(res != LZO_E_OK)
return false;
memcpy(data, buff, sizeof(buff));
return true;
}
bool recover_key(FILE* plaintext, FILE* ciphertext, uint8_t* key)
{
fseek(plaintext, ESAFE_BLOCK, SEEK_SET);
fseek(ciphertext, ESAFE_BLOCK, SEEK_SET);
uint8_t pt_chunk;
uint8_t ct_chunk;
// Recover key
for(int i = 0; i < ESAFE_BLOCK; i++)
{
if(fread(&pt_chunk, sizeof(uint8_t), 1, plaintext) != sizeof(uint8_t) ||
fread(&ct_chunk, sizeof(uint8_t), 1, ciphertext) != sizeof(uint8_t))
return false;
key[i] = pt_chunk ^ ct_chunk;
}
// Try to verify key
for(int i = 0; i < ESAFE_BLOCK; i++)
{
if(fread(&pt_chunk, sizeof(uint8_t), 1, plaintext) != sizeof(uint8_t) ||
fread(&ct_chunk, sizeof(uint8_t), 1, ciphertext) != sizeof(uint8_t))
return false;
if(key[i] != (pt_chunk ^ ct_chunk))
return false;
}
return true;
}
#define ESAFE_HDR 17
safe_info verify_esafe_header(FILE* file)
{
const uint8_t magic_string1[] = {0x14, 0x23, 0x65};
const uint8_t magic_string2[] = {0x01};
uint8_t buff[17] = {0};
safe_info info = {info.is_esafe = false, info.mode = 0, info.offset = 0};
if(!file)
return info;
fseek(file, 0, SEEK_SET);
size_t read = fread(buff, 1, ESAFE_HDR, file);
if(read != ESAFE_HDR)
goto err_abort;
if(!memcmp(&buff[1], magic_string1, sizeof(magic_string1)) &&
!memcmp(&buff[11], magic_string2, sizeof(magic_string2)))
{
info.is_esafe = true;
info.mode = buff[0];
info.offset = (buff[5] << 8) | buff[4];
}
err_abort:
return info;
}
const char* esafe_mode_to_str(uint8_t mode)
{
const char* lzo_str = "LZO";
const char* psub_str = "PSUB";
static char unknown_str[20];
switch(mode)
{
case ESAFE_COMP_MODE_LZO:
return lzo_str;
case ESAFE_COMP_MODE_PSUB:
return psub_str;
default:
sprintf(unknown_str, "unknown_0x%02X", mode);
return unknown_str;
}
}