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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
*
* Copyright (C) 2009 by Karl Kurbjun
* $Id$
*
* All files in this archive are subject to the GNU General Public License.
* See the file COPYING in the source tree root for full license agreement.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#include <stdio.h>
#include <errno.h>
#include <inttypes.h>
#include <string.h>
#include "mr500.h"
/* This is the patch necessary for the SVG exploit (decrypted) */
struct patch_single hack[] = { {0x29B28, 0xE12FFF30},
{0x2F960, 0xE12FFF30} };
static void usage(void)
{
printf( "Usage: mk500boot <options> <input file> [output file]\n"
"options:\n"
"\t-decrypt Decrypt the input file and save the output file\n"
"\t-encrypt Encrypt the input file and save the output file\n"
"\t-patch Patch the input file with the SVF hack\n\n");
exit(1);
}
/* This is a fake flag that is used to let the tool know what's up */
#define HEADER_DECRYPTED 0x8000
void display_header(struct olympus_header *header) {
printf("Magic Name: \t%s\n", header->magic_name);
printf("Unknown: \t0x%08hX\n", header->unknown);
printf("Header Length: \t0x%04X\n", header->header_length);
printf("Flags: \t\t0x%04X\n", header->flags);
printf("Unknonwn Zeros: 0x%08X\n", header->unknown_zeros);
printf("Image Length: \t0x%08X\n", header->image_length);
}
/* This is a demonstration of the encryption and decryption process.
* It patches a FW image to include the SVG exploit.
*/
int main (int argc, char *argv[]) {
uint32_t checksum;
uint32_t stored_crc;
enum operations {
decrypt,
encrypt,
patch
} operation;
char *encrypt_file;
char *decrypt_file;
struct olympus_header header;
if (argc < 2) {
usage();
return -1;
}
if(!strcmp(argv[1], "-decrypt")) {
if(argc < 3) {
usage();
return -1;
}
encrypt_file=argv[2];
decrypt_file=argv[3];
operation = decrypt;
} else if(!strcmp(argv[1], "-encrypt")) {
if(argc < 3) {
usage();
return -1;
}
decrypt_file = argv[2];
encrypt_file = argv[3];
operation = encrypt;
} else if(!strcmp(argv[1], "-patch")) {
decrypt_file = argv[2];
encrypt_file = argv[3];
operation = patch;
} else {
return -1;
}
/* Initialize encryption/decryption routine */
mr500_init();
if(operation == decrypt) {
/* Read in the header of the encrypted file */
if(mr500_read_header(encrypt_file, &header) < 0 ) {
printf("ERROR: Unable to read header: %s\n", strerror(errno));
return -1;
}
/* Read CRC of encrypted file */
if(mr500_read_crc(encrypt_file,
header.header_length+header.image_length, &stored_crc) < 0 ) {
printf("ERROR: Unable to read CRC: %s\n", strerror(errno));
return -1;
}
/* Display the header information */
printf("File format:\n");
printf("*****Header*****\n");
display_header(&header);
printf("****************\n\n");
printf("*****Image******\n\n");
printf("*****Footer*****\n");
printf("Checksum: \t0x%08X\n", stored_crc);
printf("****************\n\n");
printf("Writing Decrypted file...\n");
/*********************************************************************
* Save a decrypted file
**********************************************************************/
/* Check to make sure this is a encrypted file (bogus flag not set) */
if(header.flags & HEADER_DECRYPTED) {
printf("ERROR: This appears to be a decrypted file! Quitting\n");
return -1;
}
/* Check to make sure MAGIC string matches expected*/
if(strncmp((char *)header.magic_name, "OIMCFWUP", 8)) {
printf("ERROR: Magic string does not match expected! Quitting\n");
return -1;
}
/* Set a bogus flag to let the tool know that this is a decrypted file*/
header.flags |= HEADER_DECRYPTED;
/* Start by writing out the header */
if(mr500_save_header(decrypt_file, &header) < 0 ) {
printf("ERROR: Unable to save header: %s\n", strerror(errno));
return -1;
}
/* Read encrypted data and save decrypted data */
if(mr500_save_data( encrypt_file, decrypt_file, header.header_length,
header.image_length, decrypt_array) < 0 ) {
printf("ERROR: Unable to save decrypted data: %s\n", strerror(errno));
return -1;
}
printf("Calculating Checksum...\n");
/* Calculate CRC of decrypted data */
if(mr500_calculate_crc( decrypt_file, header.header_length,
header.image_length, &checksum) < 0 ) {
printf("ERROR: Unable to calculate CRC: %s\n", strerror(errno));
return -1;
}
printf("Calculated Checksum: \n\t\t0x%08X\n", checksum);
/* Double check to make sure that the two CRCs match */
if(checksum!=stored_crc) {
printf("\tERROR: \tCalculated checksum: \t0x%08X and\n", checksum);
printf("\t\tStored checksum: \t0x%08X do not match\n", stored_crc);
return -1;
} else {
printf("\tOK: Calculated checksum and stored checksum match.\n");
}
printf("Saving Checksum...\n");
/* Save the calculated CRC to the file */
if(mr500_save_crc(decrypt_file, header.header_length+header.image_length,
&checksum) < 0 ) {
printf("ERROR: Unable to save CRC: %s\n", strerror(errno));
return -1;
}
} else if(operation == patch) {
/**********************************************************************
* Patch decryped file with SVG exploit
**********************************************************************/
printf("Patching decrypted file.\n");
/* Read in the header of the encrypted file */
if(mr500_read_header(decrypt_file, &header) < 0 ) {
printf("ERROR: Unable to read header: %s\n", strerror(errno));
return -1;
}
/* Check to make sure this is a decrypted file (bogus flag not set) */
if(!(header.flags & HEADER_DECRYPTED)) {
printf("ERROR: This appears to be a encrypted file! Quitting\n");
return -1;
}
/* Check to make sure MAGIC string matches expected*/
if(strncmp((char *)header.magic_name, "OIMCFWUP", 8)) {
printf("ERROR: Magic string does not match expected! Quitting\n");
return -1;
}
printf("File Header:\n");
display_header(&header);
if(mr500_patch_file (decrypt_file, hack, 2) < 0 ) {
printf("ERROR: Unable to patch file: %s\n", strerror(errno));
return -1;
}
printf("\nCalculating new CRC\n");
/* Calculate the 'CRC' of the patched file */
if(mr500_calculate_crc( decrypt_file, header.header_length,
header.image_length, &checksum) < 0 ) {
printf("ERROR: Unable to calculate CRC: %s\n", strerror(errno));
return -1;
}
printf("Calculated CRC: \n\t\t0x%08X\n", checksum);
/* Store the calculated 'CRC' (not encrypted) */
if(mr500_save_crc(decrypt_file, header.header_length+header.image_length,
&checksum) < 0 ) {
printf("ERROR: Unable to save CRC: %s\n", strerror(errno));
return -1;
}
} else if(operation == encrypt) {
/**********************************************************************
* Save an encrypted file
**********************************************************************/
printf("Saving Encrypted file\n");
/* Read in the header of the encrypted file */
if(mr500_read_header(decrypt_file, &header) < 0 ) {
printf("ERROR: Unable to read header: %s\n", strerror(errno));
return -1;
}
/* Check to make sure this is a decrypted file (bogus flag not set) */
if(!(header.flags & HEADER_DECRYPTED)) {
printf("ERROR: This appears to be a encrypted file! Quitting\n");
return -1;
}
/* Check to make sure MAGIC string matches expected*/
if(strncmp((char *)header.magic_name, "OIMCFWUP", 7)) {
printf("ERROR: Magic string does not match expected! Quitting\n");
return -1;
}
/* Remove the bogus flag */
header.flags &= ~HEADER_DECRYPTED;
printf("File Header:\n");
display_header(&header);
/* Header is not encrypted, save it */
if(mr500_save_header(encrypt_file, &header) < 0 ) {
printf("ERROR: Unable to save header: %s\n", strerror(errno));
return -1;
}
/* Read CRC of decrypted file */
if(mr500_read_crc(decrypt_file,
header.header_length+header.image_length, &stored_crc) < 0 ) {
printf("ERROR: Unable to read CRC: %s\n", strerror(errno));
return -1;
}
/* Calculate the 'CRC' of the decrypted data */
if(mr500_calculate_crc( decrypt_file, header.header_length,
header.image_length, &checksum) < 0 ) {
printf("ERROR: Unable to calculate CRC: %s\n", strerror(errno));
return -1;
}
if(stored_crc != checksum) {
printf("\nERROR: Stored and calculated checksums do not match!\n"
"\tFile has been improperly modified. Quitting\n");
return -1;
}
printf("Encrypting data...\n");
/* Write the encrypted data to a file */
if(mr500_save_data( decrypt_file, encrypt_file, header.header_length,
header.image_length, encrypt_array) < 0 ) {
printf("ERROR: Unable to save encrypted data: %s\n", strerror(errno));
return -1;
}
printf("Saving CRC\n");
/* Store the calculated 'CRC' (not encrypted) */
if(mr500_save_crc(encrypt_file, header.header_length+header.image_length,
&checksum) < 0 ) {
printf("ERROR: Unable to save CRC: %s\n", strerror(errno));
return -1;
}
printf("File sucesfully encrypted!\n");
}
printf("Done\n");
return 0;
}
|