forked from FIX94/triforce-nand-iso-extract
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
243 lines (232 loc) · 6.79 KB
/
main.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
/*
* Copyright (C) 2017 FIX94, 2022 Anthony Ryuki
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
#include <stdio.h>
#include <stdbool.h>
#include <inttypes.h>
#include <malloc.h>
#include <string.h>
#include "des.h"
#define KEYS_AVAIL 3
/* Triforce games are encrypted a little weird */
static inline void do64BitSwap(void *in, void *out)
{
*(unsigned long long*)out = __builtin_bswap64(*(unsigned long long*)in);
}
static struct _des_ctx DESctx;
/* Just a helper function for me to not get confused */
static inline void des_ecb_decrypt_swapped(struct _des_ctx *ctx, void *in, void *out)
{
do64BitSwap(in,out); //out is now the swapped input
des_ecb_decrypt(ctx,out,out); //out is now swapped decrypted
do64BitSwap(out,out); //out is now decrypted
}
static inline void interleave(uint8_t *in1, uint8_t *in2, uint32_t inlen, uint8_t *out)
{
uint32_t i;
for(i = 0; i < inlen; i++)
{
out[(i<<1)|0]=in1[i];
out[(i<<1)|1]=in2[i];
}
}
static inline void des_decrypt_block(uint8_t *buf)
{
uint32_t i;
for(i = 0; i < 0x400; i+=8)
des_ecb_decrypt_swapped(&DESctx, buf+i, buf+i);
}
static void combine_dec(char *in1, char *in2, uint32_t inlen, FILE *out)
{
FILE *f1 = fopen(in1,"rb");
FILE *f2 = fopen(in2,"rb");
uint32_t i;
uint8_t buf1[0x200], buf2[0x200], outbuf[0x400];
for(i = 0; i < inlen; i+=0x200)
{
//Skip F-Zero AX Monster Ride dummy data
if (strlen(in1) == 7 && strlen(in2) == 7)
{
if (i == 0x84000 ||
i == 0x3E89200 ||
i == 0x40D2E00 ||
i == 0x40E3600 ||
i == 0x4339800 ||
i == 0x79FA400 ||
i == 0x7A0AC00 ||
i == 0x8091600)
{
fseek(f1,0x4200,SEEK_CUR);
fseek(f2,0x4200,SEEK_CUR);
i += 0x4200;
}
}
else if (strlen(in1) == 8 && strlen(in2) == 8)
{
if (i == 0x84000 ||
i == 0x48B4000 ||
i == 0x48C4800)
{
fseek(f1,0x4200,SEEK_CUR);
fseek(f2,0x4200,SEEK_CUR);
i += 0x4200;
}
}
fread(buf1,1,0x200,f1);
fread(buf2,1,0x200,f2);
interleave(buf1,buf2,0x200,outbuf);
des_decrypt_block(outbuf);
fwrite(outbuf,1,0x400,out);
//skip over verification? block
fseek(f1,0x10,SEEK_CUR);
fseek(f2,0x10,SEEK_CUR);
i+=0x10;
}
fclose(f1);
fclose(f2);
}
static bool verifyFiles(unsigned int game)
{
FILE *f;
int i;
char name[32];
if(game == 1)
{
for(i = 1; i <= 6; i++)
{
if(i == 3 || i == 4)
sprintf(name,"ic%i5_k9f1208u0b",i);
else
sprintf(name,"ic%i_k9f1208u0b",i);
f = fopen(name,"rb");
if(!f)
{
printf("%s missing!\n", name);
return false;
}
fseek(f,0,SEEK_END);
if(ftell(f) != 0x4200000)
{
printf("%s has the wrong length!\n", name);
fclose(f);
return false;
}
fclose(f);
}
}
else if(game == 2)
{
for(i = 1; i <= 8; i++)
{
sprintf(name,"ic%i_k9f1208u0b.bin",i);
f = fopen(name,"rb");
if(!f)
{
printf("%s missing!\n", name);
return false;
}
fseek(f,0,SEEK_END);
if(ftell(f) != 0x4200000)
{
printf("%s has the wrong length!\n", name);
fclose(f);
return false;
}
fclose(f);
}
}
else if(game == 3)
{
for(i = 1; i <= 4; i++)
{
if(i == 3 || i == 4)
sprintf(name, "ic%is.bin",i);
else
sprintf(name,"ic%i.bin",i);
f = fopen(name,"rb");
if(!f)
{
printf("%s missing!\n", name);
return false;
}
fseek(f,0,SEEK_END);
if(ftell(f) != 0x8400000)
{
printf("%s has the wrong length!\n", name);
fclose(f);
return false;
}
fclose(f);
}
}
return true;
}
static const unsigned long long trikeys[KEYS_AVAIL] = {
0xF767A7B0019E6751, //Mario Kart Arcade GP (Japan)
0xCFA3131991992F2B, //Mario Kart Arcade GP 2 (Japan)
0x6DBAD0D96758FE7F, //F-Zero AX Monster Ride
};
int main()
{
unsigned int game;
printf("Triforce NAND ISO Extract v3.0 by FIX94 and Anthony Ryuki\n");
printf("Games:\n1: Mario Kart Arcade GP (Japan)\n2: Mario Kart Arcade GP 2 (Japan)\n3: F-Zero AX Monster Ride\nPlease enter game number... ");
scanf("%d", &game);
if (game == 0 || game > 3)
{
printf("Not a valid choice\n");
return 0;
}
printf("Checking files...\n");
if(!verifyFiles(game))
return 0;
FILE *out = fopen("OUT.BIN","wb+");
if(!out)
{
printf("OUT.BIN not writable!\n");
return 0;
}
if(game == 1)
{
des_setkey(&DESctx, (unsigned char*)(trikeys));
printf("Combining and decrypting");
combine_dec("ic1_k9f1208u0b","ic2_k9f1208u0b",0x4200000,out);
printf(".");
combine_dec("ic35_k9f1208u0b","ic45_k9f1208u0b",0x4200000,out);
printf(".");
combine_dec("ic5_k9f1208u0b","ic6_k9f1208u0b",0x293FFF0,out);
printf(".\n");
fclose(out);
printf("Done!\n");
}
else if(game == 2)
{
des_setkey(&DESctx, (unsigned char*)(trikeys+1));
printf("Combining and decrypting");
combine_dec("ic1_k9f1208u0b.bin","ic2_k9f1208u0b.bin",0x4200000,out);
printf(".");
combine_dec("ic3_k9f1208u0b.bin","ic4_k9f1208u0b.bin",0x4200000,out);
printf(".");
combine_dec("ic5_k9f1208u0b.bin","ic6_k9f1208u0b.bin",0x4200000,out);
printf(".");
combine_dec("ic7_k9f1208u0b.bin","ic8_k9f1208u0b.bin",0x317FFF0,out);
printf(".\n");
fclose(out);
printf("Done!\n");
}
else if(game == 3)
{
des_setkey(&DESctx, (unsigned char*)(trikeys+2));
printf("Combining and decrypting");
combine_dec("ic1.bin","ic2.bin",0x8400000,out);
printf(".");
combine_dec("ic3s.bin","ic4s.bin",0x5296F10,out);
printf(".\n");
fclose(out);
printf("Done!\n");
}
return 0;
}