-
Notifications
You must be signed in to change notification settings - Fork 7
/
bl30sig.c
182 lines (152 loc) · 5.97 KB
/
bl30sig.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
// aml_encrypt_g12b --bl3sig
//
// This is supposed to replicate this
// ./aml_encrypt_g12b --bl30sig --input bl30_new.bin \
// --output bl30_new.bin.g12a.enc --level v3
#include <string.h>
#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;
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(1);
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(1);
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 = stat_info.st_size + 0x1ff & 0xffffe00;
if (input_size16k > 0xf000) {
printf("BL30 file length [%s] len=0x%x, max is 0x%x\n",
opt_values.input,
(uint32_t)input_size16k,
0xf000);
exit(-INPUTBADSIZE);
}
FILE *fin = fopen(opt_values.input, "rb");
unlink(opt_values.output);
FILE *fout = fopen(opt_values.output, "wb+");
uint8_t *payload = calloc(1,0x10000);
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();
/* if(withUserKey) ... */
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 && key_file != NULL) */
{
fwrite(nonce, 1, 0x10, fout);
}
bzero(payload, 0x10000);
fread(payload, 1, input_size16k, fin);
if(0x4c4d4140 == *(uint32_t*)&payload[0x02] &&
*(uint32_t*)&payload[0x14] == *(uint32_t*)&payload[0x2c] + *(uint32_t *)&payload[0x3c]) {
bzero(payload, 0x10000);
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;
/* if (withUserKey) ... */
/* else */
{
if(skip != 0)
sha256size = skip * 0x200 - 0x50;
header.encrypted = 0;
header.key_len = 0;
header.payload2_with_key = 0xdb0;
}
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;
uint8_t *buf = (uint8_t*)calloc(1, header.total_size);
if(buf == NULL) exit(-ENOMEM);
memcpy(&buf[0x0], &header, header.hdr_size);
/* if(withUserKey) ... */
/* 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);
}
}