forked from angerman/meson64-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbl2sig.c
243 lines (209 loc) · 8.39 KB
/
bl2sig.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
// aml_encrypt_g12b --bl2sig
//
// This is supposed to replicate this
// ./aml_encrypt_g12b --bl2sig --input bl2_new.bin \
// --output bl2.n.bin.sig
#include "types.h"
#include "lib.h"
int main(int argc, char ** argv) {
opt_values_bl30sig_t opt_values;
char **opt_array_view = (char**)&opt_values;
/* these options are the same for
* bl2_sig, bl2_sig_v3,
* bl30_sig, bl40_sig
*/
option_t opts[21] = {
{ "input", 1, 0, 0x30 }, // 0x00
{ "amluserkey", 1, 0, 0x31 }, // 0x01
{ "userkey", 1, 0, 0x32 }, // 0x02
{ "keymax", 1, 0, 0x33 }, // 0x03
{ "aeskey", 1, 0, 0x34 }, // 0x04
{ "output", 1, 0, 0x35 }, // 0x05
{ "efuse", 1, 0, 0x36 }, // 0x06
{ "skipsector", 1, 0, 0x37 }, // 0x07
{ "level", 1, 0, 0x38 } // 0x08
};
bzero(&opt_values, sizeof opt_values);
int getopt_result = 0;
while(getopt_long(argc, argv, "", opts, &getopt_result) != -1)
opt_array_view[getopt_result] = optarg;
struct stat stat_info;
if(opt_values.input == NULL || stat(opt_values.input, &stat_info) != 0)
exit(-ENOINPUT);
bool_t withUserKey = !(opt_values.amluserkey == NULL && (opt_values.userkey == NULL || opt_values.keymax == NULL));
size_t skip = 0;
if(opt_values.skipsector != NULL) skip = strtouq(opt_values.skipsector, NULL, 0x10);
if(skip > 4) skip = 4;
if(withUserKey && skip != 0)
exit(-EINVAL);
char output_filename[0x100];
bzero(output_filename, 0x100);
if(opt_values.output == NULL) {
sprintf(output_filename, "%s%s", opt_values.input, withUserKey ? ".encrypt" : ".pkg");
opt_values.output = output_filename;
} else {
strcpy(output_filename, opt_values.output);
}
if(0 == stat(opt_values.input, &stat_info)) {
size_t input_size16k = 0xf000;
if(stat_info.st_size != input_size16k) {
printf("BL2 file length [%s] len=0x%llx, expected 0x%lx\n",
opt_values.input,
stat_info.st_size, input_size16k);
exit(-INPUTBADSIZE);
}
FILE *fin = fopen(opt_values.input, "rb");
unlink(opt_values.output);
FILE *fout = fopen(opt_values.output, "wb+");
uint8_t *buf = calloc(1,0x10000);
if(NULL == buf) exit(-ENOMEM);
uint8_t *payload = calloc(1,0xf000);
if(NULL == payload) exit(-ENOMEM);
uint8_t nonce[0x10];
/* inject 16 bytes of random nonce */
for(int i = 0; i < 0x10; i++)
nonce[i] = rand();
FILE *fkey = NULL;
uint8_t key_header[0x20];
if(withUserKey) {
if(opt_values.amluserkey == NULL) {
if(opt_values.keymax == NULL) {
// no key.
exit(-EKEY);
}
fkey = fopen(opt_values.keymax, "rb");
if(fkey == NULL) exit(-(EKEY | ENOENT));
fseek(fkey, 0, SEEK_END);
if(0x8d8 != ftell(fkey)) exit(-EKEYBADSIZE);
fseek(fkey, 0, SEEK_SET);
} else {
fkey = fopen(opt_values.amluserkey, "rb");
if(fkey == NULL) exit(-(EKEY | ENOENT));
fseek(fkey, 0, SEEK_END);
if(0x1b40 != ftell(fkey)) exit(-EKEYBADSIZE);
fseek(fkey, 0x1b20, SEEK_SET);
fread(key_header, 1, 0x20, fkey);
fseek(fkey, 0x1248, SEEK_SET);
}
}
bl30_sig_header_t header;
bzero(&header, 0x40);
header.magic = 0x4c4d4140; // @AML
header.total_size = 0x40; // for now.
header.hdr_size = 0x40;
header.unk1 = 0x1;
header.unk2 = 0x1;
if(withUserKey && NULL != fkey) {
bzero(payload, 0xf00);
fread(payload, 1, 0x8d8, fkey);
memcpy(payload, nonce, 0x10);
fwrite(payload, 1, 0x8d8, fout);
memcpy(buf, payload, 0x8d8);
fclose(fkey);
fkey = NULL;
if(!(payload[0xd6] == '@' || payload[0xd6] == 0x80 || payload[0xd6] == ' '))
exit(-EKEYBAD);
} else {
fwrite(nonce, 1, 0x10, fout);
}
bzero(payload, input_size16k);
fread(payload, 1, input_size16k, fin);
if(0x4c4d4140 == *(uint32_t*)&payload[0x02] &&
*(uint32_t*)&payload[0x14] == *(uint32_t*)&payload[0x2c] + *(uint32_t *)&payload[0x3c]) {
fseek(fin, 0x1000, SEEK_SET);
fread(payload, 1, input_size16k, fin);
}
fclose(fin);
fin = NULL;
// So the output has the following soncstruction:
// -0x10 <nonce:0x10>
// 0x00 <header:0x40>
// 0x40 <???:0x200> -- signature
// 0x40 <sha256(header):0x20>
// 0x240 <???:0x30> -- key info
// 0x240 <key.magic("@KEY"):0x4>
// 0x244 <key.size:0x4>
// 0x248 <key.unk0:0x1>
// 0x249 <key.len:0x1>
// 0x24a <key.unk1:0x1>
// 0x24b <key.0-pad:0x5>
// 0x250 <key.sha256:0x20>
// 0x270 <???:0xd80> -- we write in payload2:0xd80 here.
// 0xff0 <payload>
size_t payload2_size = 0xd80;
size_t sha256size = 0x20;
header.encrypted = 0;
header.key_len = 0;
header.payload2_with_key = 0xdb0;
if (withUserKey) {
// char *key_file = opt_values.userkey != NULL
// ? opt_values.userkey
// : opt_values.amluserkey;
// if(0 != load_rsa_file(&ctx, key_file))
// exit(-EKEYBAD);
// key_header_t key_hdr;
// bzero(&key_hdr, 0x30);
// key_hdr.magic = 0x59454b40; // "@KEY"
// key_hdr.unk0 = 0x01;
// key_hdr.unk1 = 0x00;
// key_hdr.size = 0x30;
// size_t key_size = create_key(key_file, payload, &key_hdr, header.hdr_size + 0x230);
// bzero(payload + key_size, 0xf);
// size_t rounded_key_size = key_size + 0xf & 0xfffffff0;
// if(rounded_key_size != 0x4b8) {
// bzero(payload + rounded_key_size, 0x4b8 - rounded_key_size);
// rounded_key_size = 0x4b8;
// }
// header.encrypted = 1;
// header.key_len = key_hdr.type;
// header.payload2_with_key = rounded_key_size + 0x30;
// // so now payload2_with_key will be 0x4e8;
} else {
if(skip != 0)
sha256size = skip * 0x200 - 0x50;
}
header.block_size = 0x200;
header.header_size = header.hdr_size;
header.sig_body_offset = sha256size + header.header_size;
header.key_offset = header.header_size + 0x200;
header.payload_offset = header.payload2_with_key + header.key_offset;
header.total_size = input_size16k + header.payload2_with_key + header.total_size + 0x200;
header.body_size = header.total_size - header.sig_body_offset;
header.input_size16k = input_size16k;
if(NULL != buf) free(buf);
buf = (uint8_t*)calloc(1, header.total_size);
if(buf == NULL) exit(-ENOMEM);
memcpy(&buf[0x0], &header, header.hdr_size);
/* if(withUserKey) ... */
/* else */
/* 0xff0 = 0x40 + 0x200 + 0x30 + 0xd80 */
memcpy(&buf[0xff0], payload, input_size16k);
/* verify our sizes are correct */
if(&buf[0x0] + header.total_size != &buf[0xff0] + header.input_size16k) {
printf("calucation failed %04x != %04x\n", header.total_size, 0xff0 + header.input_size16k);
exit(-EINVAL);
}
/* if(withUserKey) ... */
/* else */
{
*(uint32_t*)&buf[0x24] = 0x240;
*(uint16_t*)&buf[0x258] = 0x298;
*(uint32_t*)&buf[0x8ec] = 0x240;
*(uint16_t*)&buf[0xb20] = 0x298;
}
/* if(withUserKey) ...
* compute the sha256 as below, but pkcs1_sign it
* before writing it into &buf[0x40]
*/
/* else */
{
sha256n(2,
(uchar_t*[]){&buf[0x0], &buf[0x60]},
(size_t[]){header.hdr_size, header.body_size},
&buf[0x40]);
}
fwrite(&buf[0x0], 1, header.total_size, fout);
fclose(fout);
exit(0);
}
}