-
Notifications
You must be signed in to change notification settings - Fork 4
/
twintig.c
459 lines (361 loc) · 9.02 KB
/
twintig.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
// Copyright 2007,2008 Segher Boessenkool <[email protected]>
// Licensed under the terms of the GNU GPL, version 2
// http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "tools.h"
static int verbose = 0;
#define MAXFILES 1000
#define ERROR(s) do { fprintf(stderr, s "\n"); exit(1); } while (0)
//Uncomment line if building in cygwin.
#define ISFILEDIR_WIN32
static u8 sd_key[16];
static u8 sd_iv[16];
static u8 md5_blanker[16];
static u32 ng_id;
static u32 ng_key_id;
static u8 ng_mac[6];
static u8 ng_priv[30];
static u8 ng_sig[60];
static FILE *fp;
static u8 header[0xf0c0];
static u32 n_files;
static u32 files_size;
static u8 files[MAXFILES][0x80];
static int read_image(u8 *data, u32 w, u32 h, const char *name)
{
FILE *fp;
u32 x, y;
u32 ww, hh;
fp = fopen(name, "rb");
if (!fp)
return -1;
if (fscanf(fp, "P6 %d %d 255\n", &ww, &hh) != 2)
ERROR("bad ppm");
if (ww != w || hh != h)
ERROR("wrong size ppm");
for (y = 0; y < h; y++)
for (x = 0; x < w; x++) {
u8 pix[3];
u16 raw;
u32 x0, x1, y0, y1, off;
x0 = x & 3;
x1 = x >> 2;
y0 = y & 3;
y1 = y >> 2;
off = x0 + 4 * y0 + 16 * x1 + 4 * w * y1;
if (fread(pix, 3, 1, fp) != 1)
fatal("read %s", name);
raw = (pix[0] & 0xf8) << 7;
raw |= (pix[1] & 0xf8) << 2;
raw |= (pix[2] & 0xf8) >> 3;
raw |= 0x8000;
wbe16(data + 2*off, raw);
}
fclose(fp);
return 0;
}
static u8 perm_from_path(const char *path)
{
struct stat sb;
mode_t mode;
u8 perm;
u32 i;
if (stat(path, &sb))
fatal("stat %s", path);
perm = 0;
mode = sb.st_mode;
for (i = 0; i < 3; i++) {
perm <<= 2;
if (mode & 0200)
perm |= 2;
if (mode & 0400)
perm |= 1;
mode <<= 3;
}
return perm;
}
static void do_file_header(u64 title_id)
{
u8 md5_calc[16];
FILE *in;
char name[256];
u32 i;
memset(header, 0, sizeof header);
wbe64(header, title_id);
header[0x0c] = perm_from_path(".");
memcpy(header + 0x0e, md5_blanker, 16);
memcpy(header + 0x20, "WIBN", 4);
// XXX: what about the stuff at 0x24?
in = fopen("###title###", "rb");
if (!in)
fatal("open ###title###");
if (fread(header + 0x40, 0x80, 1, in) != 1)
fatal("read ###title###");
fclose(in);
if(read_image(header + 0xc0, 192, 64, "###banner###.ppm"))
fatal("open %s", "###bannr###.ppm");
in = fopen("###icon###.ppm", "rb");
if (in) {
fclose(in);
wbe32(header + 8, 0x72a0);
if(read_image(header + 0x60c0, 48, 48, "###icon###.ppm"))
fatal("open %s", "###icon###.ppm");
} else {
for (i = 0; i < 8; i++) {
snprintf(name, sizeof name, "###icon%d###.ppm", i);
if(read_image(header + 0x60c0 + 0x1200*i, 48, 48, name))
{
if(i==0)
fatal("open %s", "###icon0###.ppm");
else
break;
}
}
wbe32(header + 8, 0x60A0 + (i * 0x1200));
}
md5(header, sizeof header, md5_calc);
memcpy(header + 0x0e, md5_calc, 16);
aes_cbc_enc(sd_key, sd_iv, header, sizeof header, header);
if (fwrite(header, 0xf0c0, 1, fp) != 1)
fatal("write header");
}
#ifdef ISFILEDIR_WIN32
static int isdir(char *name)
{
if(chdir(name) < 0) return 0;
chdir("..");
return 1;
}
static int isfile(char *name)
{
FILE *fp;
if((fp=fopen(name,"rb"))==NULL) return 0;
fclose(fp);
return 1;
}
#endif
static void find_files_recursive(const char *path)
{
DIR *dir;
struct dirent *de;
char name[53];
u32 len;
int is_dir;
u8 *p;
struct stat sb;
u32 size;
dir = opendir(path ? path : ".");
if (!dir)
fatal("opendir %s", path ? path : ".");
while ((de = readdir(dir))) {
if (strcmp(de->d_name, ".") == 0)
continue;
if (strcmp(de->d_name, "..") == 0)
continue;
if (strncmp(de->d_name, "###", 3) == 0)
continue;
if (path == 0)
len = snprintf(name, sizeof name, "%s", de->d_name);
else
len = snprintf(name, sizeof name, "%s/%s", path,
de->d_name);
if (len >= sizeof name)
ERROR("path too long");
#ifdef ISFILEDIR_WIN32
if(!isfile(de->d_name) && !isdir(de->d_name))
ERROR("not a regular file or a directory");
is_dir = isdir(de->d_name);
#else
if (de->d_type != DT_REG && de->d_type != DT_DIR)
ERROR("not a regular file or a directory");
is_dir = (de->d_type == DT_DIR);
#endif
if (is_dir)
size = 0;
else {
if (stat(name, &sb))
fatal("stat %s", name);
size = sb.st_size;
}
p = files[n_files++];
wbe32(p, 0x3adf17e);
wbe32(p + 4, size);
p[8] = perm_from_path(name);
p[0x0a] = is_dir ? 2 : 1;
strcpy(p + 0x0b, name);
// maybe fill up with dirt
size = round_up(size, 0x40);
files_size += 0x80 + size;
if (is_dir)
find_files_recursive(name);
}
if (closedir(dir))
fatal("closedir");
}
static int compar(const void *a, const void *b)
{
return strcmp((char *)a + 0x0b, (char *)b + 0x0b);
}
static void find_files(void)
{
n_files = 0;
files_size = 0;
memset(files, 0, sizeof files);
find_files_recursive(0);
qsort(files, n_files, 0x80, compar);
}
static void do_backup_header(u64 title_id)
{
u8 header[0x80];
memset(header, 0, sizeof header);
wbe32(header, 0x70);
wbe32(header + 4, 0x426b0001);
wbe32(header + 8, ng_id);
wbe32(header + 0x0c, n_files);
wbe32(header + 0x10, files_size);
wbe32(header + 0x1c, files_size + 0x3c0);
wbe64(header + 0x60, title_id);
memcpy(header + 0x68, ng_mac, 6);
if (fwrite(header, sizeof header, 1, fp) != 1)
fatal("write Bk header");
}
static void do_file(u32 file_no)
{
u8 *header;
u32 size;
u32 rounded_size;
u8 perm, attr, type;
char *name;
u8 *data;
FILE *in;
header = files[file_no];
size = be32(header + 4);
perm = header[8];
attr = header[9];
type = header[10];
name = header + 11;
if (verbose)
printf(
"file: size=%08x perm=%02x attr=%02x type=%02x name=%s\n",
size, perm, attr, type, name);
if (fwrite(header, 0x80, 1, fp) != 1)
fatal("write file header %d", file_no);
if (type == 1) {
rounded_size = round_up(size, 0x40);
data = malloc(rounded_size);
if (!data)
fatal("malloc data");
in = fopen(name, "rb");
if (!in)
fatal("open %s", name);
if (fread(data, size, 1, in) != 1)
fatal("read %s", name);
fclose(in);
memset(data + size, 0, rounded_size - size);
aes_cbc_enc(sd_key, header + 0x50, data, rounded_size, data);
if (fwrite(data, rounded_size, 1, fp) != 1)
fatal("write file %d", file_no);
free(data);
}
}
static void make_ec_cert(u8 *cert, u8 *sig, char *signer, char *name, u8 *priv,
u32 key_id)
{
memset(cert, 0, 0x180);
wbe32(cert, 0x10002);
memcpy(cert + 4, sig, 60);
strcpy(cert + 0x80, signer);
wbe32(cert + 0xc0, 2);
strcpy(cert + 0xc4, name);
wbe32(cert + 0x104, key_id);
ec_priv_to_pub(priv, cert + 0x108);
}
static void do_sig(void)
{
u8 sig[0x40];
u8 ng_cert[0x180];
u8 ap_cert[0x180];
u8 hash[0x14];
u8 ap_priv[30];
u8 ap_sig[60];
char signer[64];
char name[64];
u8 *data;
u32 data_size;
sprintf(signer, "Root-CA00000001-MS00000002");
sprintf(name, "NG%08x", ng_id);
make_ec_cert(ng_cert, ng_sig, signer, name, ng_priv, ng_key_id);
memset(ap_priv, 0, sizeof ap_priv);
ap_priv[10] = 1;
memset(ap_sig, 81, sizeof ap_sig); // temp
sprintf(signer, "Root-CA00000001-MS00000002-NG%08x", ng_id);
sprintf(name, "AP%08x%08x", 1, 2);
make_ec_cert(ap_cert, ap_sig, signer, name, ap_priv, 0);
sha(ap_cert + 0x80, 0x100, hash);
generate_ecdsa(ap_sig, ap_sig + 30, ng_priv, hash);
make_ec_cert(ap_cert, ap_sig, signer, name, ap_priv, 0);
data_size = files_size + 0x80;
data = malloc(data_size);
if (!data)
fatal("malloc");
fseek(fp, 0xf0c0, SEEK_SET);
if (fread(data, data_size, 1, fp) != 1)
fatal("read data for sig check");
sha(data, data_size, hash);
sha(hash, 20, hash);
free(data);
generate_ecdsa(sig, sig + 30, ap_priv, hash);
wbe32(sig + 60, 0x2f536969);
if (fwrite(sig, sizeof sig, 1, fp) != 1)
fatal("write sig");
if (fwrite(ng_cert, sizeof ng_cert, 1, fp) != 1)
fatal("write NG cert");
if (fwrite(ap_cert, sizeof ap_cert, 1, fp) != 1)
fatal("write AP cert");
}
int main(int argc, char **argv)
{
u64 title_id;
u8 tmp[4];
char wiiname[256] = "default";
u32 i;
if (argc < 3) {
fprintf(stderr, "Usage: %s <srcdir> <data.bin> [wii_key_name]\n", argv[0]);
return 1;
}
if(argc > 3)
snprintf(wiiname, sizeof wiiname, "%s", argv[3]);
get_key("sd-key", sd_key, 16);
get_key("sd-iv", sd_iv, 16);
get_key("md5-blanker", md5_blanker, 16);
get_wii_key(wiiname,"NG-id", tmp, 4, 0);
ng_id = be32(tmp);
get_wii_key(wiiname,"NG-key-id", tmp, 4, 0);
ng_key_id = be32(tmp);
get_wii_key(wiiname,"NG-mac", ng_mac, 6, 0);
get_wii_key(wiiname,"NG-priv", ng_priv, 30, 0);
get_wii_key(wiiname,"NG-sig", ng_sig, 60, 0);
if (sscanf(argv[1], "%016llx", &title_id) != 1)
ERROR("not a correct title id");
fp = fopen(argv[2], "wb+");
if (!fp)
fatal("open %s", argv[2]);
if (chdir(argv[1]))
fatal("chdir %s", argv[1]);
do_file_header(title_id);
find_files();
do_backup_header(title_id);
for (i = 0; i < n_files; i++)
do_file(i);
if (chdir(".."))
fatal("chdir ..");
do_sig();
fclose(fp);
return 0;
}