forked from tiliarou/4NXCI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pki.c
230 lines (209 loc) · 14.4 KB
/
pki.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
#include <stdio.h>
#include <string.h>
#include "aes.h"
#include "pki.h"
static const nxci_keyset_t nca_keys_retail = {
.nca_hdr_fixed_key_modulus = {/* Fixed RSA key used to validate NCA signature 0. */
0xBF, 0xBE, 0x40, 0x6C, 0xF4, 0xA7, 0x80, 0xE9, 0xF0, 0x7D, 0x0C, 0x99, 0x61, 0x1D, 0x77, 0x2F,
0x96, 0xBC, 0x4B, 0x9E, 0x58, 0x38, 0x1B, 0x03, 0xAB, 0xB1, 0x75, 0x49, 0x9F, 0x2B, 0x4D, 0x58,
0x34, 0xB0, 0x05, 0xA3, 0x75, 0x22, 0xBE, 0x1A, 0x3F, 0x03, 0x73, 0xAC, 0x70, 0x68, 0xD1, 0x16,
0xB9, 0x04, 0x46, 0x5E, 0xB7, 0x07, 0x91, 0x2F, 0x07, 0x8B, 0x26, 0xDE, 0xF6, 0x00, 0x07, 0xB2,
0xB4, 0x51, 0xF8, 0x0D, 0x0A, 0x5E,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D, 0xE4, 0xF4, 0x03},
.acid_fixed_key_modulus = {/* Fixed RSA key used to validate ACID signatures. */
0xDD, 0xC8, 0xDD, 0xF2, 0x4E, 0x6D, 0xF0, 0xCA, 0x9E, 0xC7, 0x5D, 0xC7, 0x7B, 0xAD, 0xFE, 0x7D, 0x23, 0x89, 0x69, 0xB6, 0xF2, 0x06, 0xA2, 0x02, 0x88, 0xE1, 0x55, 0x91, 0xAB, 0xCB, 0x4D, 0x50, 0x2E, 0xFC, 0x9D, 0x94, 0x76, 0xD6, 0x4C, 0xD8, 0xFF, 0x10, 0xFA, 0x5E, 0x93, 0x0A, 0xB4, 0x57, 0xAC, 0x51, 0xC7, 0x16, 0x66, 0xF4, 0x1A, 0x54, 0xC2, 0xC5, 0x04, 0x3D, 0x1B, 0xFE, 0x30, 0x20, 0x8A, 0xAC, 0x6F, 0x6F, 0xF5, 0xC7, 0xB6, 0x68, 0xB8, 0xC9, 0x40, 0x6B, 0x42, 0xAD, 0x11, 0x21, 0xE7, 0x8B, 0xE9, 0x75, 0x01, 0x86, 0xE4, 0x48, 0x9B, 0x0A, 0x0A, 0xF8, 0x7F, 0xE8, 0x87, 0xF2, 0x82, 0x01, 0xE6, 0xA3, 0x0F, 0xE4, 0x66, 0xAE, 0x83, 0x3F, 0x4E, 0x9F, 0x5E, 0x01, 0x30, 0xA4, 0x00, 0xB9, 0x9A, 0xAE, 0x5F, 0x03, 0xCC, 0x18, 0x60, 0xE5, 0xEF, 0x3B, 0x5E, 0x15, 0x16, 0xFE, 0x1C, 0x82, 0x78, 0xB5, 0x2F, 0x47, 0x7C, 0x06, 0x66, 0x88, 0x5D, 0x35, 0xA2, 0x67, 0x20, 0x10, 0xE7, 0x6C, 0x43, 0x68, 0xD3, 0xE4, 0x5A, 0x68, 0x2A, 0x5A, 0xE2, 0x6D, 0x73, 0xB0, 0x31, 0x53, 0x1C, 0x20, 0x09, 0x44, 0xF5, 0x1A, 0x9D, 0x22, 0xBE, 0x12, 0xA1, 0x77, 0x11, 0xE2, 0xA1, 0xCD, 0x40, 0x9A, 0xA2, 0x8B, 0x60, 0x9B, 0xEF, 0xA0, 0xD3, 0x48, 0x63, 0xA2, 0xF8, 0xA3, 0x2C, 0x08, 0x56, 0x52, 0x2E, 0x60, 0x19, 0x67, 0x5A, 0xA7, 0x9F, 0xDC, 0x3F, 0x3F, 0x69, 0x2B, 0x31, 0x6A, 0xB7, 0x88, 0x4A, 0x14, 0x84, 0x80, 0x33, 0x3C, 0x9D, 0x44, 0xB7, 0x3F, 0x4C, 0xE1, 0x75, 0xEA, 0x37, 0xEA, 0xE8, 0x1E, 0x7C, 0x77, 0xB7, 0xC6, 0x1A, 0xA2, 0xF0, 0x9F, 0x10, 0x61, 0xCD, 0x7B, 0x5B, 0x32, 0x4C, 0x37, 0xEF, 0xB1, 0x71, 0x68, 0x53, 0x0A, 0xED, 0x51, 0x7D, 0x35, 0x22, 0xFD},
.package2_fixed_key_modulus = {/* Fixed RSA key used to validate PK21 signatures. */
0x8D, 0x13, 0xA7, 0x77, 0x6A, 0xE5, 0xDC, 0xC0, 0x3B, 0x25, 0xD0, 0x58, 0xE4, 0x20, 0x69, 0x59, 0x55, 0x4B, 0xAB, 0x70, 0x40, 0x08, 0x28, 0x07, 0xA8, 0xA7, 0xFD, 0x0F, 0x31, 0x2E, 0x11, 0xFE, 0x47, 0xA0, 0xF9, 0x9D, 0xDF, 0x80, 0xDB, 0x86, 0x5A, 0x27, 0x89, 0xCD, 0x97, 0x6C, 0x85, 0xC5, 0x6C, 0x39, 0x7F, 0x41, 0xF2, 0xFF, 0x24, 0x20, 0xC3, 0x95, 0xA6, 0xF7, 0x9D, 0x4A, 0x45, 0x74, 0x8B, 0x5D, 0x28, 0x8A, 0xC6, 0x99, 0x35, 0x68, 0x85, 0xA5, 0x64, 0x32, 0x80, 0x9F, 0xD3, 0x48, 0x39, 0xA2, 0x1D, 0x24, 0x67, 0x69, 0xDF, 0x75, 0xAC, 0x12, 0xB5, 0xBD, 0xC3, 0x29, 0x90, 0xBE, 0x37, 0xE4, 0xA0, 0x80, 0x9A, 0xBE, 0x36, 0xBF, 0x1F, 0x2C, 0xAB, 0x2B, 0xAD, 0xF5, 0x97, 0x32, 0x9A, 0x42, 0x9D, 0x09, 0x8B, 0x08, 0xF0, 0x63, 0x47, 0xA3, 0xE9, 0x1B, 0x36, 0xD8, 0x2D, 0x8A, 0xD7, 0xE1, 0x54, 0x11, 0x95, 0xE4, 0x45, 0x88, 0x69, 0x8A, 0x2B, 0x35, 0xCE, 0xD0, 0xA5, 0x0B, 0xD5, 0x5D, 0xAC, 0xDB, 0xAF, 0x11, 0x4D, 0xCA, 0xB8, 0x1E, 0xE7, 0x01, 0x9E, 0xF4, 0x46, 0xA3, 0x8A, 0x94, 0x6D, 0x76, 0xBD, 0x8A, 0xC8, 0x3B, 0xD2, 0x31, 0x58, 0x0C, 0x79, 0xA8, 0x26, 0xE9, 0xD1, 0x79, 0x9C, 0xCB, 0xD4, 0x2B, 0x6A, 0x4F, 0xC6, 0xCC, 0xCF, 0x90, 0xA7, 0xB9, 0x98, 0x47, 0xFD, 0xFA, 0x4C, 0x6C, 0x6F, 0x81, 0x87, 0x3B, 0xCA, 0xB8, 0x50, 0xF6, 0x3E, 0x39, 0x5D, 0x4D, 0x97, 0x3F, 0x0F, 0x35, 0x39, 0x53, 0xFB, 0xFA, 0xCD, 0xAB, 0xA8, 0x7A, 0x62, 0x9A, 0x3F, 0xF2, 0x09, 0x27, 0x96, 0x3F, 0x07, 0x9A, 0x91, 0xF7, 0x16, 0xBF, 0xC6, 0x3A, 0x82, 0x5A, 0x4B, 0xCF, 0x49, 0x50, 0x95, 0x8C, 0x55, 0x80, 0x7E, 0x39, 0xB1, 0x48, 0x05, 0x1E, 0x21, 0xC7, 0x24, 0x4F}};
static void generate_kek(unsigned char *dst, const unsigned char *src, const unsigned char *master_key, const unsigned char *kek_seed, const unsigned char *key_seed)
{
unsigned char kek[0x10];
unsigned char src_kek[0x10];
aes_ctx_t *master_ctx = new_aes_ctx(master_key, 0x10, AES_MODE_ECB);
aes_decrypt(master_ctx, kek, kek_seed, 0x10);
free_aes_ctx(master_ctx);
aes_ctx_t *kek_ctx = new_aes_ctx(kek, 0x10, AES_MODE_ECB);
aes_decrypt(kek_ctx, src_kek, src, 0x10);
free_aes_ctx(kek_ctx);
if (key_seed != NULL)
{
aes_ctx_t *key_ctx = new_aes_ctx(src_kek, 0x10, AES_MODE_ECB);
aes_decrypt(key_ctx, dst, key_seed, 0x10);
free_aes_ctx(key_ctx);
}
else
{
memcpy(dst, src_kek, 0x10);
}
}
void pki_derive_keys(nxci_keyset_t *keyset) {
unsigned char zeroes[0x100];
unsigned char cmac[0x10];
memset(zeroes, 0, 0x100);
memset(cmac, 0, 0x10);
/* Derive keys as necessary. */
for (unsigned int i = 0; i < 0x6; i++) {
/* Start by deriving keyblob keys. */
if (memcmp(&keyset->secure_boot_key, zeroes, 0x10) == 0) {
continue;
}
if (memcmp(&keyset->tsec_key, zeroes, 0x10) == 0) {
continue;
}
if (memcmp(&keyset->keyblob_key_sources[i], zeroes, 0x10) == 0) {
continue;
}
aes_ctx_t *sbk_ctx = new_aes_ctx(keyset->secure_boot_key, 0x10, AES_MODE_ECB);
aes_ctx_t *tsec_ctx = new_aes_ctx(keyset->tsec_key, 0x10, AES_MODE_ECB);
aes_decrypt(tsec_ctx, &keyset->keyblob_keys[i], &keyset->keyblob_key_sources[i], 0x10);
aes_decrypt(sbk_ctx, &keyset->keyblob_keys[i], &keyset->keyblob_keys[i], 0x10);
free_aes_ctx(tsec_ctx);
free_aes_ctx(sbk_ctx);
if (memcmp(keyset->keyblob_mac_key_source, zeroes, 0x10) == 0) {
continue;
}
aes_ctx_t *mac_gen_ctx = new_aes_ctx(&keyset->keyblob_keys[i], 0x10, AES_MODE_ECB);
aes_decrypt(mac_gen_ctx, &keyset->keyblob_mac_keys[i], keyset->keyblob_mac_key_source, 0x10);
free_aes_ctx(mac_gen_ctx);
}
for (unsigned int i = 0; i < 0x6; i++) {
/* Then we decrypt keyblobs. */
if (memcmp(&keyset->keyblob_keys[i], zeroes, 0x10) == 0) {
continue;
}
if (memcmp(&keyset->keyblob_mac_keys[i], zeroes, 0x10) == 0) {
continue;
}
if (memcmp(&keyset->encrypted_keyblobs[i], zeroes, 0xB0) == 0) {
continue;
}
aes_calculate_cmac(cmac, &keyset->encrypted_keyblobs[i][0x10], 0xA0, keyset->keyblob_mac_keys[i]);
if (memcmp(cmac, &keyset->encrypted_keyblobs[i][0], 0x10) != 0) {
fprintf(stderr, "[ WARN ] Keyblob MAC %02x is invalid. Are SBK/TSEC key correct?\n", i);
continue;
}
aes_ctx_t *keyblob_ctx = new_aes_ctx(&keyset->keyblob_keys[i], 0x10, AES_MODE_CTR);
aes_setiv(keyblob_ctx, &keyset->encrypted_keyblobs[i][0x10], 0x10);
aes_decrypt(keyblob_ctx, &keyset->keyblobs[i], &keyset->encrypted_keyblobs[i][0x20], sizeof(keyset->keyblobs[i]));
free_aes_ctx(keyblob_ctx);
}
for (unsigned int i = 0; i < 0x6; i++) {
/* Set package1 key as relevant. */
if (memcmp(keyset->keyblobs[i] + 0x80, zeroes, 0x10) != 0) {
memcpy(&keyset->package1_keys[i], &keyset->keyblobs[i][0x80], 0x10);
}
/* Set master kek as relevant. */
if (memcmp(keyset->keyblobs[i] + 0x00, zeroes, 0x10) != 0) {
memcpy(&keyset->master_keks[i], &keyset->keyblobs[i][0x00], 0x10);
}
}
for (unsigned int i = 0x6; i < 0x20; i++) {
/* Derive new 6.2.0+ keks. */
if (memcmp(keyset->tsec_auth_signatures[i-6], zeroes, 0x10) == 0) {
continue;
}
/* Derive TSEC root key. */
if (memcmp(keyset->tsec_root_kek, zeroes, 0x10) != 0) {
aes_ctx_t *tsec_root_ctx = new_aes_ctx(keyset->tsec_root_kek, 0x10, AES_MODE_ECB);
aes_encrypt(tsec_root_ctx, keyset->tsec_root_keys[i-6], keyset->tsec_auth_signatures[i-6], 0x10);
free_aes_ctx(tsec_root_ctx);
}
/* Derive package1 MAC key */
if (memcmp(keyset->package1_mac_kek, zeroes, 0x10) != 0) {
aes_ctx_t *pk11_mac_ctx = new_aes_ctx(keyset->package1_mac_kek, 0x10, AES_MODE_ECB);
aes_encrypt(pk11_mac_ctx, keyset->package1_mac_keys[i], keyset->tsec_auth_signatures[i-6], 0x10);
free_aes_ctx(pk11_mac_ctx);
}
/* Derive package1 key */
if (memcmp(keyset->package1_kek, zeroes, 0x10) != 0) {
aes_ctx_t *pk11_ctx = new_aes_ctx(keyset->package1_kek, 0x10, AES_MODE_ECB);
aes_encrypt(pk11_ctx, keyset->package1_keys[i], keyset->tsec_auth_signatures[i-6], 0x10);
free_aes_ctx(pk11_ctx);
}
}
for (unsigned int i = 0x6; i < 0x20; i++) {
/* Do new keygen for 6.2.0+. */
if (memcmp(keyset->tsec_root_keys[i-6], zeroes, 0x10) == 0) {
continue;
}
if (memcmp(keyset->master_kek_sources[i], zeroes, 0x10) == 0) {
continue;
}
aes_ctx_t *tsec_root_ctx = new_aes_ctx(keyset->tsec_root_keys[i-6], 0x10, AES_MODE_ECB);
aes_decrypt(tsec_root_ctx, keyset->master_keks[i], keyset->master_kek_sources[i], 0x10);
free_aes_ctx(tsec_root_ctx);
}
for (unsigned int i = 0; i < 0x20; i++) {
/* Then we derive master keys. */
if (memcmp(keyset->master_key_source, zeroes, 0x10) == 0) {
continue;
}
/* We need a non-zero master kek. */
if (memcmp(keyset->master_keks[i], zeroes, 0x10) == 0) {
continue;
}
/* Derive Master Keys. */
aes_ctx_t *master_gen_ctx = new_aes_ctx(&keyset->master_keks[i], 0x10, AES_MODE_ECB);
aes_decrypt(master_gen_ctx, &keyset->master_keys[i], keyset->master_key_source, 0x10);
free_aes_ctx(master_gen_ctx);
}
for (unsigned int i = 0; i < 0x20; i++) {
if (memcmp(&keyset->master_keys[i], zeroes, 0x10) == 0) {
continue;
}
aes_ctx_t *master_ctx = new_aes_ctx(&keyset->master_keys[i], 0x10, AES_MODE_ECB);
/* Derive Key Area Encryption Keys */
if (memcmp(keyset->key_area_key_application_source, zeroes, 0x10) != 0) {
generate_kek(keyset->key_area_keys[i][0], keyset->key_area_key_application_source, keyset->master_keys[i], keyset->aes_kek_generation_source, keyset->aes_key_generation_source);
}
if (memcmp(keyset->key_area_key_ocean_source, zeroes, 0x10) != 0) {
generate_kek(keyset->key_area_keys[i][1], keyset->key_area_key_ocean_source, keyset->master_keys[i], keyset->aes_kek_generation_source, keyset->aes_key_generation_source);
}
if (memcmp(keyset->key_area_key_system_source, zeroes, 0x10) != 0) {
generate_kek(keyset->key_area_keys[i][2], keyset->key_area_key_system_source, keyset->master_keys[i], keyset->aes_kek_generation_source, keyset->aes_key_generation_source);
}
/* Derive Titlekek */
if (memcmp(keyset->titlekek_source, zeroes, 0x10) != 0) {
aes_decrypt(master_ctx, &keyset->titlekeks[i], keyset->titlekek_source, 0x10);
}
/* Derive Package2 Key */
if (memcmp(keyset->package2_key_source, zeroes, 0x10) != 0) {
aes_decrypt(master_ctx, &keyset->package2_keys[i], keyset->package2_key_source, 0x10);
}
/* Derive Header Key */
if (i == 0 && memcmp(keyset->header_kek_source, zeroes, 0x10) != 0 && memcmp(keyset->header_key_source, zeroes, 0x20) != 0) {
unsigned char header_kek[0x10];
generate_kek(header_kek, keyset->header_kek_source, keyset->master_keys[i], keyset->aes_kek_generation_source, keyset->aes_key_generation_source);
aes_ctx_t *header_ctx = new_aes_ctx(header_kek, 0x10, AES_MODE_ECB);
aes_decrypt(header_ctx, keyset->header_key, keyset->header_key_source, 0x20);
free_aes_ctx(header_ctx);
}
/* Derive SD Card Key */
if (i == 0 && memcmp(keyset->sd_card_kek_source, zeroes, 0x10) != 0) {
unsigned char sd_kek[0x10];
generate_kek(sd_kek, keyset->sd_card_kek_source, keyset->master_keys[i], keyset->aes_kek_generation_source, keyset->aes_key_generation_source);
aes_ctx_t *sd_ctx = new_aes_ctx(sd_kek, 0x10, AES_MODE_ECB);
for (unsigned int k = 0; k < 2; k++) {
if (memcmp(keyset->sd_card_key_sources[k], zeroes, 0x20) != 0) {
aes_decrypt(sd_ctx, keyset->sd_card_keys[k], keyset->sd_card_key_sources[k], 0x20);
}
}
free_aes_ctx(sd_ctx);
}
free_aes_ctx(master_ctx);
}
}
void pki_initialize_keyset(nxci_keyset_t *keyset)
{
memcpy(keyset, &nca_keys_retail, sizeof(*keyset));
pki_derive_keys(keyset);
}