zelzip_icebrk 1.0.4

Implementation of the different algorithms used on Nintendo consoles to generate the parental control master key.
Documentation
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//
// SPDX-License-Identifier: MPL-2.0

use crate::Platform;
use aes::cipher::{KeyIvInit, StreamCipher};
use derive_jserror::JsError;
use thiserror::Error;
use wasm_bindgen::prelude::*;

type Aes128Ctr64LE = ctr::Ctr128BE<aes::Aes128>;

// Sorry, this is the only way to do this and avoid any dynamic dispatch ([`include_dir`](https://docs.rs/include_dir/latest/include_dir/) and friends)

const THE_3DS_AES_KEY_REGION_00_AND_09: &[u8] =
    include_bytes!("v2/3ds_aes_key_region_00_and_09.bin");
const THE_3DS_AES_KEY_REGION_01: &[u8] = include_bytes!("v2/3ds_aes_key_region_01.bin");
const THE_3DS_AES_KEY_REGION_02: &[u8] = include_bytes!("v2/3ds_aes_key_region_02.bin");
const THE_3DS_AES_KEY_REGION_05: &[u8] = include_bytes!("v2/3ds_aes_key_region_05.bin");

const WII_U_AES_KEY_REGION_01: &[u8] = include_bytes!("v2/wii_u_aes_key_region_01.bin");
const WII_U_AES_KEY_REGION_02: &[u8] = include_bytes!("v2/wii_u_aes_key_region_02.bin");
const WII_U_AES_KEY_REGION_03: &[u8] = include_bytes!("v2/wii_u_aes_key_region_03.bin");

const THE_3DS_HMAC_KEY_ENC_REGION_00_VERSION_0A: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_00_version_0a.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_00_VERSION_0B: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_00_version_0b.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_00_VERSION_0C: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_00_version_0c.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_00_VERSION_0D: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_00_version_0d.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_00_VERSION_0E: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_00_version_0e.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_00_VERSION_0F: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_00_version_0f.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_00_VERSION_10: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_00_version_10.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_00_VERSION_11: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_00_version_11.bin.enc");

const THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_0A: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_01_version_0a.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_0B: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_01_version_0b.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_0C: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_01_version_0c.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_0D: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_01_version_0d.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_0E: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_01_version_0e.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_0F: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_01_version_0f.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_1A: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_01_version_1a.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_1B: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_01_version_1b.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_1C: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_01_version_1c.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_1D: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_01_version_1d.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_1E: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_01_version_1e.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_1F: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_01_version_1f.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_2A: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_01_version_2a.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_2B: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_01_version_2b.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_10: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_01_version_10.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_11: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_01_version_11.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_12: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_01_version_12.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_13: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_01_version_13.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_14: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_01_version_14.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_15: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_01_version_15.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_16: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_01_version_16.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_17: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_01_version_17.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_18: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_01_version_18.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_19: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_01_version_19.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_20: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_01_version_20.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_21: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_01_version_21.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_22: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_01_version_22.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_23: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_01_version_23.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_24: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_01_version_24.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_25: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_01_version_25.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_26: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_01_version_26.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_27: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_01_version_27.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_28: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_01_version_28.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_29: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_01_version_29.bin.enc");

const THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_0A: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_02_version_0a.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_0B: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_02_version_0b.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_0C: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_02_version_0c.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_0D: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_02_version_0d.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_0E: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_02_version_0e.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_0F: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_02_version_0f.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_1A: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_02_version_1a.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_1B: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_02_version_1b.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_1C: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_02_version_1c.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_1D: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_02_version_1d.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_1E: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_02_version_1e.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_1F: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_02_version_1f.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_2A: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_02_version_2a.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_2B: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_02_version_2b.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_10: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_02_version_10.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_11: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_02_version_11.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_12: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_02_version_12.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_13: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_02_version_13.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_14: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_02_version_14.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_15: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_02_version_15.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_16: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_02_version_16.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_17: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_02_version_17.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_18: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_02_version_18.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_19: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_02_version_19.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_20: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_02_version_20.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_21: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_02_version_21.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_22: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_02_version_22.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_23: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_02_version_23.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_24: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_02_version_24.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_25: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_02_version_25.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_26: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_02_version_26.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_27: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_02_version_27.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_28: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_02_version_28.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_29: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_02_version_29.bin.enc");

const THE_3DS_HMAC_KEY_ENC_REGION_05_VERSION_1A: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_05_version_1a.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_05_VERSION_1B: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_05_version_1b.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_05_VERSION_1C: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_05_version_1c.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_05_VERSION_1D: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_05_version_1d.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_05_VERSION_1E: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_05_version_1e.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_05_VERSION_1F: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_05_version_1f.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_05_VERSION_2A: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_05_version_2a.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_05_VERSION_12: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_05_version_12.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_05_VERSION_13: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_05_version_13.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_05_VERSION_14: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_05_version_14.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_05_VERSION_15: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_05_version_15.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_05_VERSION_16: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_05_version_16.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_05_VERSION_17: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_05_version_17.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_05_VERSION_18: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_05_version_18.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_05_VERSION_19: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_05_version_19.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_05_VERSION_20: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_05_version_20.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_05_VERSION_21: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_05_version_21.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_05_VERSION_22: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_05_version_22.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_05_VERSION_23: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_05_version_23.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_05_VERSION_24: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_05_version_24.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_05_VERSION_25: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_05_version_25.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_05_VERSION_26: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_05_version_26.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_05_VERSION_27: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_05_version_27.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_05_VERSION_28: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_05_version_28.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_05_VERSION_29: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_05_version_29.bin.enc");

const THE_3DS_HMAC_KEY_ENC_REGION_09_VERSION_1A: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_09_version_1a.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_09_VERSION_1B: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_09_version_1b.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_09_VERSION_1C: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_09_version_1c.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_09_VERSION_1D: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_09_version_1d.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_09_VERSION_1E: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_09_version_1e.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_09_VERSION_1F: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_09_version_1f.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_09_VERSION_2A: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_09_version_2a.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_09_VERSION_2B: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_09_version_2b.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_09_VERSION_12: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_09_version_12.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_09_VERSION_13: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_09_version_13.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_09_VERSION_14: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_09_version_14.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_09_VERSION_15: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_09_version_15.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_09_VERSION_16: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_09_version_16.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_09_VERSION_17: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_09_version_17.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_09_VERSION_18: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_09_version_18.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_09_VERSION_19: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_09_version_19.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_09_VERSION_20: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_09_version_20.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_09_VERSION_21: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_09_version_21.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_09_VERSION_22: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_09_version_22.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_09_VERSION_23: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_09_version_23.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_09_VERSION_24: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_09_version_24.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_09_VERSION_25: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_09_version_25.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_09_VERSION_26: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_09_version_26.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_09_VERSION_27: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_09_version_27.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_09_VERSION_28: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_09_version_28.bin.enc");
const THE_3DS_HMAC_KEY_ENC_REGION_09_VERSION_29: &[u8] =
    include_bytes!("v2/3ds_hmac_key_region_09_version_29.bin.enc");

const WII_U_HMAC_KEY_ENC_REGION_01: &[u8] = include_bytes!("v2/wii_u_hmac_key_region_01.bin.enc");
const WII_U_HMAC_KEY_ENC_REGION_02: &[u8] = include_bytes!("v2/wii_u_hmac_key_region_02.bin.enc");
const WII_U_HMAC_KEY_ENC_REGION_03: &[u8] = include_bytes!("v2/wii_u_hmac_key_region_03.bin.enc");

#[derive(Error, JsError, Debug)]
#[allow(missing_docs)]
pub enum V2Error {
    #[error("Unknown region encoded inside the inquiry number: {0}")]
    UnknownRegion(u64),

    #[error("Unknown region encoded inside the inquiry number: ({0}, {1})")]
    UnknownRegionOrVersion(u64, u64),
}

/// Calculate the master key for the parental control using the v2 algorithm. The inquire number
/// cannot be bigger than 10 digits and the date must be valid (there are some loose checks).
///
/// Remember that the given master key must be presented with the correct amount of leading zeroes
/// to always have 5 digits.
///
/// Only works on 3DS (from 7.2.0 to 11.15.0) and Wii U (from 5.0.0 to 5.5.5)
///
/// This function internal uses a set of HMAC and AES keys, it's unknown if all keys have been
/// found.
#[wasm_bindgen]
pub fn calculate_v2_master_key(
    platform: Platform,
    inquiry_number: u64,
    day: u8,
    month: u8,
) -> Result<u32, V2Error> {
    assert!(inquiry_number <= 9_999_999_999);

    assert!(day > 0);
    assert!(day <= 31);

    assert!(month > 0);
    assert!(month <= 12);

    let region = inquiry_number / 1_000_000_000;
    let version = (inquiry_number / 10_000_000) % 100;

    #[allow(clippy::expect_used)]
    let aes_key: &[u8; 16] = match platform {
        Platform::WiiU => match region {
            0x01 => WII_U_AES_KEY_REGION_01,
            0x02 => WII_U_AES_KEY_REGION_02,
            0x03 => WII_U_AES_KEY_REGION_03,

            _ => return Err(V2Error::UnknownRegion(region)),
        },

        Platform::The3ds => match region {
            0x00 | 0x09 => THE_3DS_AES_KEY_REGION_00_AND_09,
            0x01 => THE_3DS_AES_KEY_REGION_01,
            0x02 => THE_3DS_AES_KEY_REGION_02,
            0x05 => THE_3DS_AES_KEY_REGION_05,

            _ => return Err(V2Error::UnknownRegion(region)),
        },

        _ => panic!("The v2 algorithm is only available on the 3DS and the Wii U platforms"),
    }
    .try_into()
    .expect("The v2 AES key is not long enough");

    let hmac_enc = match platform {
        Platform::WiiU => match region {
            0x01 => WII_U_HMAC_KEY_ENC_REGION_01,
            0x02 => WII_U_HMAC_KEY_ENC_REGION_02,
            0x03 => WII_U_HMAC_KEY_ENC_REGION_03,

            _ => return Err(V2Error::UnknownRegion(region)),
        },

        Platform::The3ds => match (region, version) {
            (0x00, 0x0A) => THE_3DS_HMAC_KEY_ENC_REGION_00_VERSION_0A,
            (0x00, 0x0B) => THE_3DS_HMAC_KEY_ENC_REGION_00_VERSION_0B,
            (0x00, 0x0C) => THE_3DS_HMAC_KEY_ENC_REGION_00_VERSION_0C,
            (0x00, 0x0D) => THE_3DS_HMAC_KEY_ENC_REGION_00_VERSION_0D,
            (0x00, 0x0E) => THE_3DS_HMAC_KEY_ENC_REGION_00_VERSION_0E,
            (0x00, 0x0F) => THE_3DS_HMAC_KEY_ENC_REGION_00_VERSION_0F,
            (0x00, 0x10) => THE_3DS_HMAC_KEY_ENC_REGION_00_VERSION_10,
            (0x00, 0x11) => THE_3DS_HMAC_KEY_ENC_REGION_00_VERSION_11,

            (0x01, 0x0A) => THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_0A,
            (0x01, 0x0B) => THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_0B,
            (0x01, 0x0C) => THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_0C,
            (0x01, 0x0D) => THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_0D,
            (0x01, 0x0E) => THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_0E,
            (0x01, 0x0F) => THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_0F,
            (0x01, 0x1A) => THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_1A,
            (0x01, 0x1B) => THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_1B,
            (0x01, 0x1C) => THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_1C,
            (0x01, 0x1D) => THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_1D,
            (0x01, 0x1E) => THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_1E,
            (0x01, 0x1F) => THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_1F,
            (0x01, 0x2A) => THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_2A,
            (0x01, 0x2B) => THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_2B,
            (0x01, 0x10) => THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_10,
            (0x01, 0x11) => THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_11,
            (0x01, 0x12) => THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_12,
            (0x01, 0x13) => THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_13,
            (0x01, 0x14) => THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_14,
            (0x01, 0x15) => THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_15,
            (0x01, 0x16) => THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_16,
            (0x01, 0x17) => THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_17,
            (0x01, 0x18) => THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_18,
            (0x01, 0x19) => THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_19,
            (0x01, 0x20) => THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_20,
            (0x01, 0x21) => THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_21,
            (0x01, 0x22) => THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_22,
            (0x01, 0x23) => THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_23,
            (0x01, 0x24) => THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_24,
            (0x01, 0x25) => THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_25,
            (0x01, 0x26) => THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_26,
            (0x01, 0x27) => THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_27,
            (0x01, 0x28) => THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_28,
            (0x01, 0x29) => THE_3DS_HMAC_KEY_ENC_REGION_01_VERSION_29,

            (0x02, 0x0A) => THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_0A,
            (0x02, 0x0B) => THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_0B,
            (0x02, 0x0C) => THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_0C,
            (0x02, 0x0D) => THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_0D,
            (0x02, 0x0E) => THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_0E,
            (0x02, 0x0F) => THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_0F,
            (0x02, 0x1A) => THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_1A,
            (0x02, 0x1B) => THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_1B,
            (0x02, 0x1C) => THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_1C,
            (0x02, 0x1D) => THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_1D,
            (0x02, 0x1E) => THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_1E,
            (0x02, 0x1F) => THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_1F,
            (0x02, 0x2A) => THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_2A,
            (0x02, 0x2B) => THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_2B,
            (0x02, 0x10) => THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_10,
            (0x02, 0x11) => THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_11,
            (0x02, 0x12) => THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_12,
            (0x02, 0x13) => THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_13,
            (0x02, 0x14) => THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_14,
            (0x02, 0x15) => THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_15,
            (0x02, 0x16) => THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_16,
            (0x02, 0x17) => THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_17,
            (0x02, 0x18) => THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_18,
            (0x02, 0x19) => THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_19,
            (0x02, 0x20) => THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_20,
            (0x02, 0x21) => THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_21,
            (0x02, 0x22) => THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_22,
            (0x02, 0x23) => THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_23,
            (0x02, 0x24) => THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_24,
            (0x02, 0x25) => THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_25,
            (0x02, 0x26) => THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_26,
            (0x02, 0x27) => THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_27,
            (0x02, 0x28) => THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_28,
            (0x02, 0x29) => THE_3DS_HMAC_KEY_ENC_REGION_02_VERSION_29,

            (0x05, 0x1A) => THE_3DS_HMAC_KEY_ENC_REGION_05_VERSION_1A,
            (0x05, 0x1B) => THE_3DS_HMAC_KEY_ENC_REGION_05_VERSION_1B,
            (0x05, 0x1C) => THE_3DS_HMAC_KEY_ENC_REGION_05_VERSION_1C,
            (0x05, 0x1D) => THE_3DS_HMAC_KEY_ENC_REGION_05_VERSION_1D,
            (0x05, 0x1E) => THE_3DS_HMAC_KEY_ENC_REGION_05_VERSION_1E,
            (0x05, 0x1F) => THE_3DS_HMAC_KEY_ENC_REGION_05_VERSION_1F,
            (0x05, 0x2A) => THE_3DS_HMAC_KEY_ENC_REGION_05_VERSION_2A,
            (0x05, 0x12) => THE_3DS_HMAC_KEY_ENC_REGION_05_VERSION_12,
            (0x05, 0x13) => THE_3DS_HMAC_KEY_ENC_REGION_05_VERSION_13,
            (0x05, 0x14) => THE_3DS_HMAC_KEY_ENC_REGION_05_VERSION_14,
            (0x05, 0x15) => THE_3DS_HMAC_KEY_ENC_REGION_05_VERSION_15,
            (0x05, 0x16) => THE_3DS_HMAC_KEY_ENC_REGION_05_VERSION_16,
            (0x05, 0x17) => THE_3DS_HMAC_KEY_ENC_REGION_05_VERSION_17,
            (0x05, 0x18) => THE_3DS_HMAC_KEY_ENC_REGION_05_VERSION_18,
            (0x05, 0x19) => THE_3DS_HMAC_KEY_ENC_REGION_05_VERSION_19,
            (0x05, 0x20) => THE_3DS_HMAC_KEY_ENC_REGION_05_VERSION_20,
            (0x05, 0x21) => THE_3DS_HMAC_KEY_ENC_REGION_05_VERSION_21,
            (0x05, 0x22) => THE_3DS_HMAC_KEY_ENC_REGION_05_VERSION_22,
            (0x05, 0x23) => THE_3DS_HMAC_KEY_ENC_REGION_05_VERSION_23,
            (0x05, 0x24) => THE_3DS_HMAC_KEY_ENC_REGION_05_VERSION_24,
            (0x05, 0x25) => THE_3DS_HMAC_KEY_ENC_REGION_05_VERSION_25,
            (0x05, 0x26) => THE_3DS_HMAC_KEY_ENC_REGION_05_VERSION_26,
            (0x05, 0x27) => THE_3DS_HMAC_KEY_ENC_REGION_05_VERSION_27,
            (0x05, 0x28) => THE_3DS_HMAC_KEY_ENC_REGION_05_VERSION_28,
            (0x05, 0x29) => THE_3DS_HMAC_KEY_ENC_REGION_05_VERSION_29,

            (0x09, 0x1A) => THE_3DS_HMAC_KEY_ENC_REGION_09_VERSION_1A,
            (0x09, 0x1B) => THE_3DS_HMAC_KEY_ENC_REGION_09_VERSION_1B,
            (0x09, 0x1C) => THE_3DS_HMAC_KEY_ENC_REGION_09_VERSION_1C,
            (0x09, 0x1D) => THE_3DS_HMAC_KEY_ENC_REGION_09_VERSION_1D,
            (0x09, 0x1E) => THE_3DS_HMAC_KEY_ENC_REGION_09_VERSION_1E,
            (0x09, 0x1F) => THE_3DS_HMAC_KEY_ENC_REGION_09_VERSION_1F,
            (0x09, 0x2A) => THE_3DS_HMAC_KEY_ENC_REGION_09_VERSION_2A,
            (0x09, 0x2B) => THE_3DS_HMAC_KEY_ENC_REGION_09_VERSION_2B,
            (0x09, 0x12) => THE_3DS_HMAC_KEY_ENC_REGION_09_VERSION_12,
            (0x09, 0x13) => THE_3DS_HMAC_KEY_ENC_REGION_09_VERSION_13,
            (0x09, 0x14) => THE_3DS_HMAC_KEY_ENC_REGION_09_VERSION_14,
            (0x09, 0x15) => THE_3DS_HMAC_KEY_ENC_REGION_09_VERSION_15,
            (0x09, 0x16) => THE_3DS_HMAC_KEY_ENC_REGION_09_VERSION_16,
            (0x09, 0x17) => THE_3DS_HMAC_KEY_ENC_REGION_09_VERSION_17,
            (0x09, 0x18) => THE_3DS_HMAC_KEY_ENC_REGION_09_VERSION_18,
            (0x09, 0x19) => THE_3DS_HMAC_KEY_ENC_REGION_09_VERSION_19,
            (0x09, 0x20) => THE_3DS_HMAC_KEY_ENC_REGION_09_VERSION_20,
            (0x09, 0x21) => THE_3DS_HMAC_KEY_ENC_REGION_09_VERSION_21,
            (0x09, 0x22) => THE_3DS_HMAC_KEY_ENC_REGION_09_VERSION_22,
            (0x09, 0x23) => THE_3DS_HMAC_KEY_ENC_REGION_09_VERSION_23,
            (0x09, 0x24) => THE_3DS_HMAC_KEY_ENC_REGION_09_VERSION_24,
            (0x09, 0x25) => THE_3DS_HMAC_KEY_ENC_REGION_09_VERSION_25,
            (0x09, 0x26) => THE_3DS_HMAC_KEY_ENC_REGION_09_VERSION_26,
            (0x09, 0x27) => THE_3DS_HMAC_KEY_ENC_REGION_09_VERSION_27,
            (0x09, 0x28) => THE_3DS_HMAC_KEY_ENC_REGION_09_VERSION_28,
            (0x09, 0x29) => THE_3DS_HMAC_KEY_ENC_REGION_09_VERSION_29,

            _ => return Err(V2Error::UnknownRegionOrVersion(region, version)),
        },
        _ => panic!("The v2 algorithm is only available on the 3DS and the Wii U platforms"),
    };

    #[allow(clippy::expect_used)]
    let aes_counter: &[u8; 16] = hmac_enc[16..32]
        .try_into()
        .expect("The encoded v2 HMAC file is not big enough");

    #[allow(clippy::expect_used)]
    let mut hmac_key: [u8; 32] = hmac_enc[32..64]
        .try_into()
        .expect("The encoded v2 HMAC file is not big enough");

    let mut aes = Aes128Ctr64LE::new(aes_key.into(), aes_counter.into());
    aes.apply_keystream(&mut hmac_key);

    Ok(crate::calculate_master_key_shared_v1_and_v2(
        &hmac_key,
        inquiry_number,
        day,
        month,
        platform == Platform::WiiU,
    ))
}

// NOTE: No tests because making all combinations would be insane